Skip to content

Commit

Permalink
typer: merge upstream changes into vendored typer
Browse files Browse the repository at this point in the history
  • Loading branch information
voodoos committed Feb 5, 2024
1 parent 74b377b commit 6e970a1
Show file tree
Hide file tree
Showing 102 changed files with 16,788 additions and 13,115 deletions.
12 changes: 7 additions & 5 deletions src/ocaml/parsing/ast_helper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ module Typ = struct
let poly ?loc ?attrs a b = mk ?loc ?attrs (Ptyp_poly (a, b))
let package ?loc ?attrs a b = mk ?loc ?attrs (Ptyp_package (a, b))
let extension ?loc ?attrs a = mk ?loc ?attrs (Ptyp_extension a)
let open_ ?loc ?attrs mod_ident t = mk ?loc ?attrs (Ptyp_open (mod_ident, t))

let force_poly t =
match t.ptyp_desc with
Expand Down Expand Up @@ -107,9 +108,9 @@ module Typ = struct
Ptyp_object (List.map loop_object_field lst, o)
| Ptyp_class (longident, lst) ->
Ptyp_class (longident, List.map loop lst)
| Ptyp_alias(core_type, string) ->
check_variable var_names t.ptyp_loc string;
Ptyp_alias(loop core_type, string)
| Ptyp_alias(core_type, alias) ->
check_variable var_names alias.loc alias.txt;
Ptyp_alias(loop core_type, alias)
| Ptyp_variant(row_field_list, flag, lbl_lst_option) ->
Ptyp_variant(List.map loop_row_field row_field_list,
flag, lbl_lst_option)
Expand All @@ -119,6 +120,8 @@ module Typ = struct
Ptyp_poly(string_lst, loop core_type)
| Ptyp_package(longident,lst) ->
Ptyp_package(longident,List.map (fun (n,typ) -> (n,loop typ) ) lst)
| Ptyp_open (mod_ident, core_type) ->
Ptyp_open (mod_ident, loop core_type)
| Ptyp_extension (s, arg) ->
Ptyp_extension (s, arg)
in
Expand Down Expand Up @@ -186,8 +189,7 @@ module Exp = struct
let ident ?loc ?attrs a = mk ?loc ?attrs (Pexp_ident a)
let constant ?loc ?attrs a = mk ?loc ?attrs (Pexp_constant a)
let let_ ?loc ?attrs a b c = mk ?loc ?attrs (Pexp_let (a, b, c))
let fun_ ?loc ?attrs a b c d = mk ?loc ?attrs (Pexp_fun (a, b, c, d))
let function_ ?loc ?attrs a = mk ?loc ?attrs (Pexp_function a)
let function_ ?loc ?attrs a b c = mk ?loc ?attrs (Pexp_function (a, b, c))
let apply ?loc ?attrs a b = mk ?loc ?attrs (Pexp_apply (a, b))
let match_ ?loc ?attrs a b = mk ?loc ?attrs (Pexp_match (a, b))
let try_ ?loc ?attrs a b = mk ?loc ?attrs (Pexp_try (a, b))
Expand Down
10 changes: 6 additions & 4 deletions src/ocaml/parsing/ast_helper.mli
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,14 @@ module Typ :
val object_: ?loc:loc -> ?attrs:attrs -> object_field list
-> closed_flag -> core_type
val class_: ?loc:loc -> ?attrs:attrs -> lid -> core_type list -> core_type
val alias: ?loc:loc -> ?attrs:attrs -> core_type -> string -> core_type
val alias: ?loc:loc -> ?attrs:attrs -> core_type -> string with_loc
-> core_type
val variant: ?loc:loc -> ?attrs:attrs -> row_field list -> closed_flag
-> label list option -> core_type
val poly: ?loc:loc -> ?attrs:attrs -> str list -> core_type -> core_type
val package: ?loc:loc -> ?attrs:attrs -> lid -> (lid * core_type) list
-> core_type
val open_ : ?loc:loc -> ?attrs:attrs -> lid -> core_type -> core_type
val extension: ?loc:loc -> ?attrs:attrs -> extension -> core_type

val force_poly: core_type -> core_type
Expand Down Expand Up @@ -139,9 +141,9 @@ module Exp:
val constant: ?loc:loc -> ?attrs:attrs -> constant -> expression
val let_: ?loc:loc -> ?attrs:attrs -> rec_flag -> value_binding list
-> expression -> expression
val fun_: ?loc:loc -> ?attrs:attrs -> arg_label -> expression option
-> pattern -> expression -> expression
val function_: ?loc:loc -> ?attrs:attrs -> case list -> expression
val function_ : ?loc:loc -> ?attrs:attrs -> function_param list
-> type_constraint option -> function_body
-> expression
val apply: ?loc:loc -> ?attrs:attrs -> expression
-> (arg_label * expression) list -> expression
val match_: ?loc:loc -> ?attrs:attrs -> expression -> case list
Expand Down
59 changes: 54 additions & 5 deletions src/ocaml/parsing/ast_iterator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type iterator = {
class_type_declaration: iterator -> class_type_declaration -> unit;
class_type_field: iterator -> class_type_field -> unit;
constructor_declaration: iterator -> constructor_declaration -> unit;
directive_argument: iterator -> directive_argument -> unit;
expr: iterator -> expression -> unit;
extension: iterator -> extension -> unit;
extension_constructor: iterator -> extension_constructor -> unit;
Expand All @@ -61,6 +62,8 @@ type iterator = {
signature_item: iterator -> signature_item -> unit;
structure: iterator -> structure -> unit;
structure_item: iterator -> structure_item -> unit;
toplevel_directive: iterator -> toplevel_directive -> unit;
toplevel_phrase: iterator -> toplevel_phrase -> unit;
typ: iterator -> core_type -> unit;
row_field: iterator -> row_field -> unit;
object_field: iterator -> object_field -> unit;
Expand Down Expand Up @@ -132,6 +135,9 @@ module T = struct
| Ptyp_package (lid, l) ->
iter_loc sub lid;
List.iter (iter_tuple (iter_loc sub) (sub.typ sub)) l
| Ptyp_open (mod_ident, t) ->
iter_loc sub mod_ident;
sub.typ sub t
| Ptyp_extension x -> sub.extension sub x

let iter_type_declaration sub
Expand Down Expand Up @@ -348,6 +354,32 @@ end
module E = struct
(* Value expressions for the core language *)

let iter_function_param sub { pparam_loc = loc; pparam_desc = desc } =
sub.location sub loc;
match desc with
| Pparam_val (_lab, def, p) ->
iter_opt (sub.expr sub) def;
sub.pat sub p
| Pparam_newtype ty ->
iter_loc sub ty

let iter_body sub body =
match body with
| Pfunction_body e ->
sub.expr sub e
| Pfunction_cases (cases, loc, attrs) ->
sub.cases sub cases;
sub.location sub loc;
sub.attributes sub attrs

let iter_constraint sub constraint_ =
match constraint_ with
| Pconstraint ty ->
sub.typ sub ty
| Pcoerce (ty1, ty2) ->
iter_opt (sub.typ sub) ty1;
sub.typ sub ty2

let iter sub {pexp_loc = loc; pexp_desc = desc; pexp_attributes = attrs} =
sub.location sub loc;
sub.attributes sub attrs;
Expand All @@ -357,11 +389,10 @@ module E = struct
| Pexp_let (_r, vbs, e) ->
List.iter (sub.value_binding sub) vbs;
sub.expr sub e
| Pexp_fun (_lab, def, p, e) ->
iter_opt (sub.expr sub) def;
sub.pat sub p;
sub.expr sub e
| Pexp_function pel -> sub.cases sub pel
| Pexp_function (params, constraint_, body) ->
List.iter (iter_function_param sub) params;
iter_opt (iter_constraint sub) constraint_;
iter_body sub body
| Pexp_apply (e, l) ->
sub.expr sub e; List.iter (iter_snd (sub.expr sub)) l
| Pexp_match (e, pel) ->
Expand Down Expand Up @@ -694,4 +725,22 @@ let default_iterator =
| PTyp x -> this.typ this x
| PPat (x, g) -> this.pat this x; iter_opt (this.expr this) g
);

directive_argument =
(fun this a ->
this.location this a.pdira_loc
);

toplevel_directive =
(fun this d ->
iter_loc this d.pdir_name;
iter_opt (this.directive_argument this) d.pdir_arg;
this.location this d.pdir_loc
);

toplevel_phrase =
(fun this -> function
| Ptop_def s -> this.structure this s
| Ptop_dir d -> this.toplevel_directive this d
);
}
3 changes: 3 additions & 0 deletions src/ocaml/parsing/ast_iterator.mli
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type iterator = {
class_type_declaration: iterator -> class_type_declaration -> unit;
class_type_field: iterator -> class_type_field -> unit;
constructor_declaration: iterator -> constructor_declaration -> unit;
directive_argument: iterator -> directive_argument -> unit;
expr: iterator -> expression -> unit;
extension: iterator -> extension -> unit;
extension_constructor: iterator -> extension_constructor -> unit;
Expand All @@ -64,6 +65,8 @@ type iterator = {
signature_item: iterator -> signature_item -> unit;
structure: iterator -> structure -> unit;
structure_item: iterator -> structure_item -> unit;
toplevel_directive: iterator -> toplevel_directive -> unit;
toplevel_phrase: iterator -> toplevel_phrase -> unit;
typ: iterator -> core_type -> unit;
row_field: iterator -> row_field -> unit;
object_field: iterator -> object_field -> unit;
Expand Down
84 changes: 75 additions & 9 deletions src/ocaml/parsing/ast_mapper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
(* Ensure that record patterns don't miss any field. *)
*)

[@@@ocaml.warning "-60"] module Str = Ast_helper.Str (* For ocamldep *)
[@@@ocaml.warning "+60"]

open Parsetree
open Ast_helper
open Location
Expand All @@ -45,6 +48,7 @@ type mapper = {
constant: mapper -> constant -> constant;
constructor_declaration: mapper -> constructor_declaration
-> constructor_declaration;
directive_argument: mapper -> directive_argument -> directive_argument;
expr: mapper -> expression -> expression;
extension: mapper -> extension -> extension;
extension_constructor: mapper -> extension_constructor
Expand All @@ -68,6 +72,8 @@ type mapper = {
signature_item: mapper -> signature_item -> signature_item;
structure: mapper -> structure -> structure;
structure_item: mapper -> structure_item -> structure_item;
toplevel_directive: mapper -> toplevel_directive -> toplevel_directive;
toplevel_phrase: mapper -> toplevel_phrase -> toplevel_phrase;
typ: mapper -> core_type -> core_type;
type_declaration: mapper -> type_declaration -> type_declaration;
type_extension: mapper -> type_extension -> type_extension;
Expand Down Expand Up @@ -144,14 +150,18 @@ module T = struct
object_ ~loc ~attrs (List.map (object_field sub) l) o
| Ptyp_class (lid, tl) ->
class_ ~loc ~attrs (map_loc sub lid) (List.map (sub.typ sub) tl)
| Ptyp_alias (t, s) -> alias ~loc ~attrs (sub.typ sub t) s
| Ptyp_alias (t, s) ->
let s = map_loc sub s in
alias ~loc ~attrs (sub.typ sub t) s
| Ptyp_variant (rl, b, ll) ->
variant ~loc ~attrs (List.map (row_field sub) rl) b ll
| Ptyp_poly (sl, t) -> poly ~loc ~attrs
(List.map (map_loc sub) sl) (sub.typ sub t)
| Ptyp_package (lid, l) ->
package ~loc ~attrs (map_loc sub lid)
(List.map (map_tuple (map_loc sub) (sub.typ sub)) l)
| Ptyp_open (mod_ident, t) ->
open_ ~loc ~attrs (map_loc sub mod_ident) (sub.typ sub t)
| Ptyp_extension x -> extension ~loc ~attrs (sub.extension sub x)

let map_type_declaration sub
Expand Down Expand Up @@ -387,6 +397,35 @@ end
module E = struct
(* Value expressions for the core language *)

let map_function_param sub { pparam_loc = loc; pparam_desc = desc } =
let loc = sub.location sub loc in
let desc =
match desc with
| Pparam_val (lab, def, p) ->
Pparam_val
(lab,
map_opt (sub.expr sub) def,
sub.pat sub p)
| Pparam_newtype ty ->
Pparam_newtype (map_loc sub ty)
in
{ pparam_loc = loc; pparam_desc = desc }

let map_function_body sub body =
match body with
| Pfunction_body e ->
Pfunction_body (sub.expr sub e)
| Pfunction_cases (cases, loc, attributes) ->
let cases = sub.cases sub cases in
let loc = sub.location sub loc in
let attributes = sub.attributes sub attributes in
Pfunction_cases (cases, loc, attributes)

let map_constraint sub c =
match c with
| Pconstraint ty -> Pconstraint (sub.typ sub ty)
| Pcoerce (ty1, ty2) -> Pcoerce (map_opt (sub.typ sub) ty1, sub.typ sub ty2)

let map sub {pexp_loc = loc; pexp_desc = desc; pexp_attributes = attrs} =
let open Exp in
let loc = sub.location sub loc in
Expand All @@ -397,10 +436,11 @@ module E = struct
| Pexp_let (r, vbs, e) ->
let_ ~loc ~attrs r (List.map (sub.value_binding sub) vbs)
(sub.expr sub e)
| Pexp_fun (lab, def, p, e) ->
fun_ ~loc ~attrs lab (map_opt (sub.expr sub) def) (sub.pat sub p)
(sub.expr sub e)
| Pexp_function pel -> function_ ~loc ~attrs (sub.cases sub pel)
| Pexp_function (ps, c, b) ->
function_ ~loc ~attrs
(List.map (map_function_param sub) ps)
(map_opt (map_constraint sub) c)
(map_function_body sub b)
| Pexp_apply (e, l) ->
apply ~loc ~attrs (sub.expr sub e) (List.map (map_snd (sub.expr sub)) l)
| Pexp_match (e, pel) ->
Expand Down Expand Up @@ -767,6 +807,22 @@ let default_mapper =
| PTyp x -> PTyp (this.typ this x)
| PPat (x, g) -> PPat (this.pat this x, map_opt (this.expr this) g)
);

directive_argument =
(fun this a ->
{ pdira_desc= a.pdira_desc
; pdira_loc= this.location this a.pdira_loc} );

toplevel_directive =
(fun this d ->
{ pdir_name= map_loc this d.pdir_name
; pdir_arg= map_opt (this.directive_argument this) d.pdir_arg
; pdir_loc= this.location this d.pdir_loc } );

toplevel_phrase =
(fun this -> function
| Ptop_def s -> Ptop_def (this.structure this s)
| Ptop_dir d -> Ptop_dir (this.toplevel_directive this d) );
}

let extension_of_error {kind; main; sub} =
Expand Down Expand Up @@ -844,11 +900,16 @@ module PpxContext = struct
}

let make ~tool_name () =
let Load_path.{ visible; hidden } = Load_path.get_paths () in
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 "include_dirs", make_list make_string (!Clflags.include_dirs);
lid "hidden_include_dirs",
make_list make_string (!Clflags.hidden_include_dirs);
lid "load_path",
make_pair (make_list make_string) (make_list make_string)
(visible, hidden);
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;
Expand Down Expand Up @@ -917,6 +978,8 @@ module PpxContext = struct
tool_name_ref := get_string payload
| "include_dirs" ->
Clflags.include_dirs := get_list get_string payload
| "hidden_include_dirs" ->
Clflags.hidden_include_dirs := get_list get_string payload
| "load_path" ->
(* Duplicates Compmisc.auto_include, since we can't reference Compmisc
from this module. *)
Expand All @@ -927,8 +990,11 @@ module PpxContext = struct
let alert = Location.auto_include_alert in
Load_path.auto_include_otherlibs alert find_in_dir fn
in *)
Load_path.(init
~auto_include:no_auto_include (get_list get_string payload))
let visible, hidden =
get_pair (get_list get_string) (get_list get_string) payload
in
let auto_include = Load_path.no_auto_include in
Load_path.init ~auto_include ~visible ~hidden
| "open_modules" ->
Clflags.open_modules := get_list get_string payload
| "for_package" ->
Expand Down
9 changes: 6 additions & 3 deletions src/ocaml/parsing/ast_mapper.mli
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ let test_mapper argv =
expr = fun mapper expr ->
match expr with
| { pexp_desc = Pexp_extension ({ txt = "test" }, PStr [])} ->
Ast_helper.Exp.constant (Const_int 42)
Ast_helper.Exp.constant (Pconst_integer ("42", None))
| other -> default_mapper.expr mapper other; }
let () =
Expand Down Expand Up @@ -74,6 +74,7 @@ type mapper = {
constant: mapper -> constant -> constant;
constructor_declaration: mapper -> constructor_declaration
-> constructor_declaration;
directive_argument: mapper -> directive_argument -> directive_argument;
expr: mapper -> expression -> expression;
extension: mapper -> extension -> extension;
extension_constructor: mapper -> extension_constructor
Expand All @@ -97,6 +98,8 @@ type mapper = {
signature_item: mapper -> signature_item -> signature_item;
structure: mapper -> structure -> structure;
structure_item: mapper -> structure_item -> structure_item;
toplevel_directive: mapper -> toplevel_directive -> toplevel_directive;
toplevel_phrase: mapper -> toplevel_phrase -> toplevel_phrase;
typ: mapper -> core_type -> core_type;
type_declaration: mapper -> type_declaration -> type_declaration;
type_extension: mapper -> type_extension -> type_extension;
Expand All @@ -122,8 +125,8 @@ val tool_name: unit -> string
["ocaml"], ... Some global variables that reflect command-line
options are automatically synchronized between the calling tool
and the ppx preprocessor: {!Clflags.include_dirs},
{!Load_path}, {!Clflags.open_modules}, {!Clflags.for_package},
{!Clflags.debug}. *)
{!Clflags.hidden_include_dirs}, {!Load_path}, {!Clflags.open_modules},
{!Clflags.for_package}, {!Clflags.debug}. *)


val apply: source:string -> target:string -> mapper -> unit
Expand Down
Loading

0 comments on commit 6e970a1

Please sign in to comment.