Skip to content

Commit 77ef6f4

Browse files
committed
add __MODULE_PATH__ and __VALUE_PATH__ magic constants, and hook them up to injectable sourceLoc
1 parent bce8b40 commit 77ef6f4

File tree

11 files changed

+94
-7
lines changed

11 files changed

+94
-7
lines changed

compiler/ml/lambda.ml

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,14 @@
1313
(* *)
1414
(**************************************************************************)
1515

16-
type loc_kind = Loc_FILE | Loc_LINE | Loc_MODULE | Loc_LOC | Loc_POS
16+
type loc_kind =
17+
| Loc_FILE
18+
| Loc_LINE
19+
| Loc_MODULE
20+
| Loc_MODULE_PATH
21+
| Loc_VALUE_PATH
22+
| Loc_LOC
23+
| Loc_POS
1724

1825
type tag_info =
1926
| Blk_constructor of {
@@ -697,7 +704,8 @@ let raise_kind = function
697704
| Raise_regular -> "raise"
698705
| Raise_reraise -> "reraise"
699706

700-
let lam_of_loc kind loc =
707+
let lam_of_loc ?(root_path : Path.t option)
708+
?(current_value_ident : Ident.t option) kind loc =
701709
let loc_start = loc.Location.loc_start in
702710
let file, lnum, cnum = Location.get_pos_info loc_start in
703711
let file = Filename.basename file in
@@ -716,6 +724,21 @@ let lam_of_loc kind loc =
716724
Const_base (Const_int enum);
717725
] ))
718726
| Loc_FILE -> Lconst (Const_immstring file)
727+
| Loc_MODULE_PATH -> (
728+
match root_path with
729+
| None -> Lconst (Const_immstring "<none>")
730+
| Some path -> Lconst (Const_immstring (Path.name path)))
731+
| Loc_VALUE_PATH -> (
732+
match root_path with
733+
| None -> Lconst (Const_immstring "<none>")
734+
| Some path ->
735+
Lconst
736+
(Const_immstring
737+
(Path.name path
738+
^
739+
match current_value_ident with
740+
| None -> ""
741+
| Some ident -> "." ^ Ident.name ident)))
719742
| Loc_MODULE ->
720743
let filename = Filename.basename file in
721744
let name = Env.get_unit_name () in

compiler/ml/lambda.mli

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@
1717

1818
open Asttypes
1919

20-
type loc_kind = Loc_FILE | Loc_LINE | Loc_MODULE | Loc_LOC | Loc_POS
20+
type loc_kind =
21+
| Loc_FILE
22+
| Loc_LINE
23+
| Loc_MODULE
24+
| Loc_MODULE_PATH
25+
| Loc_VALUE_PATH
26+
| Loc_LOC
27+
| Loc_POS
2128

2229
type tag_info =
2330
| Blk_constructor of {
@@ -415,4 +422,9 @@ val is_guarded : lambda -> bool
415422
val patch_guarded : lambda -> lambda -> lambda
416423

417424
val raise_kind : raise_kind -> string
418-
val lam_of_loc : loc_kind -> Location.t -> lambda
425+
val lam_of_loc :
426+
?root_path:Path.t ->
427+
?current_value_ident:Ident.t ->
428+
loc_kind ->
429+
Location.t ->
430+
lambda

compiler/ml/predef.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,10 @@ let common_initial_env add_type add_extension empty_env =
345345
(* __POS__ *)
346346
mk_field "pos"
347347
(newgenty (Ttuple [type_string; type_int; type_int; type_int]));
348+
(* __MODULE_PATH__ *)
349+
mk_field "modulePath" type_string;
350+
(* __VALUE_PATH__ *)
351+
mk_field "valuePath" type_string;
348352
],
349353
Record_regular ));
350354
}

compiler/ml/printlambda.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ let string_of_loc_kind = function
5555
| Loc_FILE -> "loc_FILE"
5656
| Loc_LINE -> "loc_LINE"
5757
| Loc_MODULE -> "loc_MODULE"
58+
| Loc_MODULE_PATH -> "loc_MODULE_PATH"
59+
| Loc_VALUE_PATH -> "loc_VALUE_PATH"
5860
| Loc_POS -> "loc_POS"
5961
| Loc_LOC -> "loc_LOC"
6062

compiler/ml/translcore.ml

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ let transl_module =
3434
(fun _cc _rootpath _modl -> assert false
3535
: module_coercion -> Path.t option -> module_expr -> lambda)
3636

37+
let current_root_path = ref None
38+
let current_value_ident = ref None
39+
3740
(* Compile an exception/extension definition *)
3841

3942
let transl_extension_constructor env path ext =
@@ -244,6 +247,8 @@ let primitives_table =
244247
("%loc_LINE", Ploc Loc_LINE);
245248
("%loc_POS", Ploc Loc_POS);
246249
("%loc_MODULE", Ploc Loc_MODULE);
250+
("%loc_MODULE_PATH", Ploc Loc_MODULE_PATH);
251+
("%loc_VALUE_PATH", Ploc Loc_VALUE_PATH);
247252
(* BEGIN Triples for ref data type *)
248253
("%makeref", Pmakeblock Lambda.ref_tag_info);
249254
("%refset", Psetfield (0, Lambda.ref_field_set_info));
@@ -448,7 +453,10 @@ let transl_primitive loc p env ty =
448453
in
449454
match prim with
450455
| Ploc kind -> (
451-
let lam = lam_of_loc kind loc in
456+
let lam =
457+
lam_of_loc ?current_value_ident:!current_value_ident
458+
?root_path:!current_root_path kind loc
459+
in
452460
match p.prim_arity with
453461
| 0 -> lam
454462
| 1 ->
@@ -741,9 +749,14 @@ and transl_exp0 (e : Typedtree.expression) : Lambda.lambda =
741749
| _ -> k
742750
in
743751
wrap (Lprim (Praise k, [targ], e.exp_loc))
744-
| Ploc kind, [] -> lam_of_loc kind e.exp_loc
752+
| Ploc kind, [] ->
753+
lam_of_loc ?current_value_ident:!current_value_ident
754+
?root_path:!current_root_path kind e.exp_loc
745755
| Ploc kind, [arg1] ->
746-
let lam = lam_of_loc kind arg1.exp_loc in
756+
let lam =
757+
lam_of_loc ?current_value_ident:!current_value_ident
758+
?root_path:!current_root_path kind arg1.exp_loc
759+
in
747760
Lprim (Pmakeblock Blk_tuple, lam :: argl, e.exp_loc)
748761
| Ploc _, _ -> assert false
749762
| _, _ -> (
@@ -1055,6 +1068,21 @@ and transl_function loc partial param case =
10551068
is_base_type exp_env exp_type Predef.path_unit )
10561069

10571070
and transl_let rec_flag pat_expr_list body =
1071+
let old_value_ident = !current_value_ident in
1072+
1073+
let binding_name =
1074+
pat_expr_list |> List.rev
1075+
|> List.find_map (fun {vb_pat} ->
1076+
match vb_pat.pat_desc with
1077+
| Tpat_var (id, _) -> Some id
1078+
| _ -> None)
1079+
in
1080+
current_value_ident := binding_name;
1081+
let res = transl_let_inner rec_flag pat_expr_list body in
1082+
current_value_ident := old_value_ident;
1083+
res
1084+
1085+
and transl_let_inner rec_flag pat_expr_list body =
10581086
match rec_flag with
10591087
| Nonrecursive ->
10601088
let rec transl = function

compiler/ml/translcore.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,6 @@ val transl_module :
4141
Typedtree.module_expr ->
4242
Lambda.lambda)
4343
ref
44+
45+
val current_root_path : Path.t option ref
46+
val current_value_ident : Ident.t option ref

compiler/ml/translmod.ml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,13 @@ let rec compile_functor mexp coercion root_path loc =
253253

254254
(* Compile a module expression *)
255255
and transl_module cc rootpath mexp =
256+
let current_root_path = !Translcore.current_root_path in
257+
Translcore.current_root_path := rootpath;
258+
let res = transl_module_inner cc rootpath mexp in
259+
Translcore.current_root_path := current_root_path;
260+
res
261+
262+
and transl_module_inner cc rootpath mexp =
256263
List.iter (Translattribute.check_attribute_on_module mexp) mexp.mod_attributes;
257264
let loc = mexp.mod_loc in
258265
match mexp.mod_type with

compiler/ml/typecore.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2254,6 +2254,8 @@ let expand_injectable_args ~(apply_expr : Parsetree.expression) ~exp_type
22542254
mk_source_loc_field "filename" "__FILE__";
22552255
mk_source_loc_field "module_" "__MODULE__";
22562256
mk_source_loc_field "pos" "__POS__";
2257+
mk_source_loc_field "modulePath" "__MODULE_PATH__";
2258+
mk_source_loc_field "valuePath" "__VALUE_PATH__";
22572259
]
22582260
None );
22592261
]

runtime/Pervasives.res

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ external __LOC__: string = "%loc_LOC"
3434
external __FILE__: string = "%loc_FILE"
3535
external __LINE__: int = "%loc_LINE"
3636
external __MODULE__: string = "%loc_MODULE"
37+
external __MODULE_PATH__: string = "%loc_MODULE_PATH"
38+
external __VALUE_PATH__: string = "%loc_VALUE_PATH"
3739
external __POS__: (string, int, int, int) = "%loc_POS"
3840

3941
external __LOC_OF__: 'a => (string, 'a) = "%loc_LOC"

runtime/Pervasives_mini.res

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ external __LOC__: string = "%loc_LOC"
77
external __FILE__: string = "%loc_FILE"
88
external __LINE__: int = "%loc_LINE"
99
external __MODULE__: string = "%loc_MODULE"
10+
external __MODULE_PATH__: string = "%loc_MODULE_PATH"
11+
external __VALUE_PATH__: string = "%loc_VALUE_PATH"
1012
external __POS__: (string, int, int, int) = "%loc_POS"
1113

1214
external __LOC_OF__: 'a => (string, 'a) = "%loc_LOC"

0 commit comments

Comments
 (0)