Skip to content

Commit

Permalink
Merge pull request #103 from ceastlund/omp-2.0
Browse files Browse the repository at this point in the history
OMP 2.0
  • Loading branch information
ceastlund authored Jul 22, 2020
2 parents b59baa2 + 4ccf593 commit 279be4d
Show file tree
Hide file tree
Showing 99 changed files with 137 additions and 36,115 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
------------------

- No longer expose the unwrapped modules
- Remove everything but Ast versions and upgrade/downgrade conversions.

v1.7.3 2020-05-07 Canterbury
----------------------------
Expand Down
339 changes: 0 additions & 339 deletions MANUAL.md

This file was deleted.

6 changes: 2 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@ test:

.PHONY: all-supported-ocaml-versions
all-supported-ocaml-versions:
dune runtest --workspace dune-workspace.dev
dune build @install --workspace dune-workspace.dev --debug-dep

.PHONY: cinaps
cinaps:
cinaps -styler ocp-indent -i src/migrate_parsetree_versions.ml*
cinaps -styler ocp-indent -i src/migrate_parsetree_4??_4??.ml*
cinaps -styler ocp-indent -i src/migrate_parsetree.ml
dune build @cinaps

.PHONY: clean
clean:
Expand Down
115 changes: 18 additions & 97 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,18 @@ Convert OCaml parsetrees between different major versions

This library converts between parsetrees of different OCaml versions.

Supported versions are 4.02, 4.03, 4.04, 4.05, 4.06, 4.07, 4.08 and 4.09.
For each version, there is a snapshot of the parsetree and conversion functions
to the next and/or previous version.
Supported versions are 4.02, 4.03, 4.04, 4.05, 4.06, 4.07, 4.08, 4.09,
4.10, and 4.11. For each version, there is a snapshot of the parsetree
and conversion functions to the next and/or previous version.

## Asts

```ocaml
module Ast_402, Ast_403, Ast_404, Ast_405, Ast_406, Ast_407, Ast_408, Ast_409 : sig
(* These two modules didn't change between compiler versions.
Just share the ones from compiler-libs. *)
module Location = Location
module Longident = Longident
module Ast_{402,403,404,405,406,407,408,409,410,411} : sig
(* Version specific copy of AST *)
module Asttypes
module Parsetree
module Outcometree
(* Other modules that are useful for implementing PPX.
Docstrings and Ast_mapper only contain general definitions
In particular, the internal state used by compiler-libs has been
removed.
Also equalities are lost for abstract types (Docstring.docstring). *)
module Docstrings
module Ast_helper
module Ast_mapper
(* Magic numbers used for marshalling *)
module Config : sig
Expand All @@ -42,86 +26,30 @@ end

These embed copies of AST definitions for each supported OCaml major version.

The AST matching the version of the OCaml toolchain will contain equalities
relating the copy of types to the definitions from compiler-libs. For
instance, when installed with OCaml 4.04.x, `Ast_404.Parsetree` looks
like.
The AST matching the version of the OCaml toolchain will contain
equalities relating the copy of types to the definitions from
compiler-libs. For instance, when installed with OCaml 4.04.x the
type `Ast_404.Parsetree.expression` is equal to the type
`Parsetree.expression` from the compiler libraries.

## Migration modules

For each pair of versions `$(n)` and `$(n+1)`, the two modules
`Migrate_parsetree_$(n)_$(n+1)` and `Migrate_parsetree_$(n+1)_$(n)` convert the AST forward and backward.

The forward conversion is total while the backward conversion is partial: when
a feature is not available in a previous version of the parsetree, a
`Migrate_parsetree_def.Migration_error` exception is raised detailing the
failure case.

`Migrate_parsetree_versions` abstract versions of the compiler. Each version is
represented as a module with `OCaml_version` signature. Instances are named
`OCaml_402`, `OCaml_403`, ... `OCaml_current` is an alias to the version of the
current compiler.
The `Convert` functor takes two versions of OCaml and produce conversion
functions.

Finally, the `Migrate_parsetree_ast_io` provides an easy interface for
marshalling/unmarshalling.

## Migrate_parsetree.Driver

The `Migrate_parsetree.Driver` provides an API for ppx rewriters to
register OCaml AST rewriters. Ppx rewriters using this API can be used
as standalone rewriter executable or as part of a _driver_ including
several rewriters.

Using a single driver for several rewritings has the advantage that it
is faster. Especially when using many ppx rewriters, it can speed up
compilation a lot.

If using [Dune](https://github.com/ocaml/dune), you can
consult the dune manual to see how to define and use ppx
rewriters. Dune automatically creates drivers based on
ocaml-migrate-parsetree on demand.
`Migrate_parsetree_$(n)_$(n+1)` and `Migrate_parsetree_$(n+1)_$(n)`
convert the AST forward and backward.

The rest of this section describes how to do things manually or with
[ocamlbuild](https://github.com/ocaml/ocamlbuild).

## Building a custom driver using ocamlfind

To build a custom driver using ocamlfind, simply link all the ppx
rewriter libraries together with the
`ocaml-migrate-parsetree.driver-main` package at the end:

ocamlfind ocamlopt -predicates ppx_driver -o ppx -linkpkg \
-package ppx_sexp_conv -package ppx_bin_prot \
-package ocaml-migrate-parsetree.driver-main

Normally, ocaml-migrate-parsetree based rewriters should be build with
the approriate `-linkall` option on individual libraries. If one is
missing this option, the rewriter might not get linked in. If this is
the case, a workaround is to pass `-linkall` when linking the custom
driver.

The resulting `ppx` program can be used as follow:

- `./ppx file.ml` to print the transformed code
- `ocamlc -pp './ppx --as-pp' ...` to use it as a pre-processor
- `ocamlc -ppx './ppx --as-ppx' ...` to use it as a `-ppx` rewriter

# Development

It started from the work of Alain Frisch in
[ppx\_tools](https://github.com/alainfrisch/ppx_tools).

The library is distributed under LGPL 2.1 and is copyright INRIA.
The forward conversion is total while the backward conversion is
partial: when a feature is not available in a previous version of the
parsetree, a `Migrate_parsetree_def.Migration_error` exception is
raised detailing the failure case.

## Adding a new OCaml version

We use [Cinaps](https://github.com/janestreet/cinaps) to generate boilerplate.
You can install it via opam: `opam install cinaps`.
We use [Cinaps](https://github.com/janestreet/cinaps) to generate
boilerplate. You can install it via opam: `opam install cinaps`.

Add the new version in
[src/cinaps_helpers](https://github.com/ocaml-ppx/ocaml-migrate-parsetree/blob/master/src/cinaps_helpers)
[src/cinaps_helpers/cinaps_helpers.ml](https://github.com/ocaml-ppx/ocaml-migrate-parsetree/blob/master/src/cinaps_helpers/cinaps_helpers.ml)
`supported_versions`.

Copy the last `src/ast_xxx.ml` file to `src/ast_<new_version>.ml`,
Expand All @@ -147,16 +75,9 @@ _build/default/tools/gencopy.exe -I . -I src/ -I +compiler-libs -map Ast_409:Ast
_build/default/tools/gencopy.exe -I . -I src/ -I +compiler-libs -map Ast_408:Ast_409 Ast_408.Parsetree.{expression,expr,pattern,pat,core_type,typ,toplevel_phrase} Ast_408.Outcometree.{out_phrase,out_type_extension} > src/migrate_parsetree_408_409_migrate.ml
```
- Fix the generated code by implementing new cases
- The migration functor expects specific names, look at `Migrate_parsetree_versions` interface.

*TODO*: specialize and improve gencopy for these cases

Add mapper lifting functions in the files `migrate_parsetree_NEW_408.ml` and
`migrate_parsetree_408_NEW.ml`:
- include the corresponding `Migrate_parsetree_40x_40y_migrate` module
- define `copy_mapper` function, look at existing `Migrate_parsetree_40x_40y`
for guidance.

At any time, you can expand boilerplate code by running `make cinaps`.

Update build system:
Expand Down
4 changes: 0 additions & 4 deletions dune

This file was deleted.

3 changes: 2 additions & 1 deletion dune-project
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
(lang dune 1.9)
(lang dune 1.11)
(name ocaml-migrate-parsetree)
(allow_approximate_merlin)
(using cinaps 1.0)
8 changes: 0 additions & 8 deletions examples/omp_ppx_define/META

This file was deleted.

40 changes: 0 additions & 40 deletions examples/omp_ppx_define/Makefile

This file was deleted.

80 changes: 0 additions & 80 deletions examples/omp_ppx_define/ppx_define.ml

This file was deleted.

4 changes: 0 additions & 4 deletions examples/omp_ppx_define/standalone.ml

This file was deleted.

1 change: 0 additions & 1 deletion examples/omp_ppx_define/test.ml

This file was deleted.

8 changes: 0 additions & 8 deletions examples/omp_ppx_here/META

This file was deleted.

34 changes: 0 additions & 34 deletions examples/omp_ppx_here/Makefile

This file was deleted.

Loading

0 comments on commit 279be4d

Please sign in to comment.