ocaml / dune

A composable build system for OCaml.
https://dune.build/
MIT License
1.57k stars 396 forks source link

PPX context #5269

Open progman1 opened 2 years ago

progman1 commented 2 years ago

Where one wishes to have access to the PPX context added to the parsetree by the compiler, one must use

(preprocessor (staged_pps X))

and this has a cost as the preprocessor X cannot be integrated with other

(pps Y ...)

processors and gives a slower compilation.

But since all the available flags and other information comprising this context is supplied by dune to the compiler anyway, dune could itself embed this context in the AST.

If this were the case I could change my staged_pps processor into the regular variety and avoid the slowdown.

Is this reasonable?

rgrinberg commented 2 years ago

I don't know if this is reasonable, but I think you should propose a more fledged out design for us to evaluate. Which information do you want dune to embed into the ast? And why the ast? Why not through a command line flag for example.

progman1 commented 2 years ago

In parsing/ast_mapper.ml there is the PpxContext module which has

  let make ~tool_name () =
    let fields =
      [
        lid "tool_name",    make_string tool_name;
        lid "include_dirs", make_list make_string !Clflags.include_dirs;
        lid "load_path",    make_list make_string (Load_path.get_paths ());
        lid "open_modules", make_list make_string !Clflags.open_modules;
        lid "for_package",  make_option make_string !Clflags.for_package;
        lid "debug",        make_bool !Clflags.debug;
        lid "use_threads",  make_bool !Clflags.use_threads;
        lid "use_vmthreads", make_bool false;
        lid "recursive_types", make_bool !Clflags.recursive_types;
        lid "principal", make_bool !Clflags.principal;
        lid "transparent_modules", make_bool !Clflags.transparent_modules;
        lid "unboxed_types", make_bool !Clflags.unboxed_types;
        lid "unsafe_string", make_bool !Clflags.unsafe_string;
        get_cookies ()
      ]
    in
    mk fields

the compiler plucks some of the flags it was invoked with and forms an AST node with them, prepending it to the main AST. This is for ppx tools to pick up. But when ppxlib is used the generated ppx.exe incorporates the compiler-libs parser directly and there is no compiler-directed insertion of compilation flag context, just the vanilla AST. my thesis is that since dune is generating that ppx.exe it ought to supply that AST context as it has relieved the external compiler of control and sets all the compilation flags itself. downstream ppx processors should not see any difference in this respect I believe.