| 1 | open Base |
| 2 | open Ppxlib |
| 3 | |
| 4 | let payload_never ~loc = |
| 5 | let (module B) = Ast_builder.make loc in |
| 6 | let open B in |
| 7 | PStr [ pstr_eval (pexp_ident (Located.lident "never")) [] ] |
| 8 | ;; |
| 9 | |
| 10 | let expand_cold_attribute attr = |
| 11 | assert (String.equal attr.attr_name.txt "cold"); |
| 12 | let loc = { attr.attr_name.loc with loc_ghost = true } in |
| 13 | let payload = payload_never ~loc in |
| 14 | [ Loc.make ~loc "ocaml.inline", payload |
| 15 | ; Loc.make ~loc "ocaml.local", payload |
| 16 | ; Loc.make ~loc "ocaml.specialise", payload |
| 17 | ] |
| 18 | |> List.map ~f:(fun (name, payload) -> |
| 19 | Ast_builder.Default.attribute ~loc ~name ~payload) |
| 20 | ;; |
| 21 | |
| 22 | class attributes_mapper = |
| 23 | object |
| 24 | inherit Ast_traverse.map as super |
| 25 | |
| 26 | method! attributes attrs = |
| 27 | let attrs = |
| 28 | List.concat_map attrs ~f:(function |
| 29 | | { attr_name = { txt = "cold"; _ }; attr_payload = PStr []; attr_loc = _ } as |
| 30 | attr -> |
| 31 | Attribute.mark_as_handled_manually attr; |
| 32 | expand_cold_attribute attr |
| 33 | | attr -> [ attr ]) |
| 34 | in |
| 35 | super#attributes attrs |
| 36 | end |
| 37 | |
| 38 | let expand_cold = (new attributes_mapper)#structure |
| 39 | let () = Driver.register_transformation "cold" ~impl:expand_cold |