From d65301ced0304fa658a226095927b659034918f3 Mon Sep 17 00:00:00 2001 From: Rudy Ges Date: Wed, 4 Oct 2023 11:16:10 +0200 Subject: [PATCH 01/17] Move global_metadata to common context --- src/context/common.ml | 2 ++ src/context/typecore.ml | 1 - src/typing/macroContext.ml | 16 ++++++++++++++-- src/typing/typeloadCheck.ml | 2 +- src/typing/typer.ml | 1 - 5 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/context/common.ml b/src/context/common.ml index 3c16ca156c8..45dc48fc9cf 100644 --- a/src/context/common.ml +++ b/src/context/common.ml @@ -397,6 +397,7 @@ type context = { mutable user_metas : (string, Meta.user_meta) Hashtbl.t; mutable get_macros : unit -> context option; (* typing state *) + mutable global_metadata : (string list * metadata_entry * (bool * bool * bool)) list; shared : shared_context; display_information : display_information; file_lookup_cache : (string,string option) lookup; @@ -829,6 +830,7 @@ let create compilation_step cs version args = file = ""; types = []; callbacks = new compiler_callbacks; + global_metadata = []; modules = []; module_lut = new hashtbl_lookup; module_nonexistent_lut = new hashtbl_lookup; diff --git a/src/context/typecore.ml b/src/context/typecore.ml index 8760eafd96f..d756d1fdbca 100644 --- a/src/context/typecore.ml +++ b/src/context/typecore.ml @@ -80,7 +80,6 @@ type typer_globals = { mutable macros : ((unit -> unit) * typer) option; mutable std : module_def; type_patches : (path, (string * bool, type_patch) Hashtbl.t * type_patch) Hashtbl.t; - mutable global_metadata : (string list * metadata_entry * (bool * bool * bool)) list; mutable module_check_policies : (string list * module_check_policy list * bool) list; mutable global_using : (tclass * pos) list; (* Indicates that Typer.create() finished building this instance *) diff --git a/src/typing/macroContext.ml b/src/typing/macroContext.ml index 713b4fcade0..ff77a76e9ca 100644 --- a/src/typing/macroContext.ml +++ b/src/typing/macroContext.ml @@ -117,6 +117,14 @@ let typing_timer ctx need_type f = raise e let make_macro_com_api com p = + let parse_metadata s p = + try + match ParserEntry.parse_string Grammar.parse_meta com.defines s null_pos raise_typing_error false with + | ParseSuccess(meta,_,_) -> meta + | ParseError(_,_,_) -> raise_typing_error "Malformed metadata string" p + with _ -> + raise_typing_error "Malformed metadata string" p + in { MacroApi.pos = p; get_com = (fun () -> com); @@ -278,7 +286,11 @@ let make_macro_com_api com p = Interp.exc_string "unsupported" ); add_global_metadata = (fun s1 s2 config p -> - Interp.exc_string "unsupported" + let meta = parse_metadata s2 p in + List.iter (fun (m,el,_) -> + let m = (m,el,p) in + com.global_metadata <- (ExtString.String.nsplit s1 ".",m,config) :: com.global_metadata; + ) meta; ); add_module_check_policy = (fun sl il b i -> Interp.exc_string "unsupported" @@ -537,7 +549,7 @@ let make_macro_api ctx p = let meta = parse_metadata s2 p in List.iter (fun (m,el,_) -> let m = (m,el,p) in - ctx.g.global_metadata <- (ExtString.String.nsplit s1 ".",m,config) :: ctx.g.global_metadata; + ctx.com.global_metadata <- (ExtString.String.nsplit s1 ".",m,config) :: ctx.com.global_metadata; ) meta; ); MacroApi.add_module_check_policy = (fun sl il b i -> diff --git a/src/typing/typeloadCheck.ml b/src/typing/typeloadCheck.ml index 3a6e41a3d24..fd2ff03c618 100644 --- a/src/typing/typeloadCheck.ml +++ b/src/typing/typeloadCheck.ml @@ -312,7 +312,7 @@ let check_global_metadata ctx meta f_add mpath tpath so = List.iter (fun (sl2,m,(recursive,to_types,to_fields)) -> let add = ((field_mode && to_fields) || (not field_mode && to_types)) && (match_path recursive sl1 sl2) in if add then f_add m - ) ctx.g.global_metadata; + ) ctx.com.global_metadata; if ctx.is_display_file then delay ctx PCheckConstraint (fun () -> DisplayEmitter.check_display_metadata ctx meta) let check_module_types ctx m p t = diff --git a/src/typing/typer.ml b/src/typing/typer.ml index 3e2aa66022a..70ab2b271c5 100644 --- a/src/typing/typer.ml +++ b/src/typing/typer.ml @@ -1999,7 +1999,6 @@ let create com = core_api = None; macros = None; type_patches = Hashtbl.create 0; - global_metadata = []; module_check_policies = []; delayed = []; debug_delayed = []; From 7921929c812cebea5fb49ee9a61dce3ca43816fd Mon Sep 17 00:00:00 2001 From: Rudy Ges Date: Wed, 4 Oct 2023 11:38:25 +0200 Subject: [PATCH 02/17] Revert "Move global_metadata to common context" This reverts commit 319e0746745c7a3fd30739f30e06f281d34ac62e. --- src/context/common.ml | 2 -- src/context/typecore.ml | 1 + src/typing/macroContext.ml | 16 ++-------------- src/typing/typeloadCheck.ml | 2 +- src/typing/typer.ml | 1 + 5 files changed, 5 insertions(+), 17 deletions(-) diff --git a/src/context/common.ml b/src/context/common.ml index 45dc48fc9cf..3c16ca156c8 100644 --- a/src/context/common.ml +++ b/src/context/common.ml @@ -397,7 +397,6 @@ type context = { mutable user_metas : (string, Meta.user_meta) Hashtbl.t; mutable get_macros : unit -> context option; (* typing state *) - mutable global_metadata : (string list * metadata_entry * (bool * bool * bool)) list; shared : shared_context; display_information : display_information; file_lookup_cache : (string,string option) lookup; @@ -830,7 +829,6 @@ let create compilation_step cs version args = file = ""; types = []; callbacks = new compiler_callbacks; - global_metadata = []; modules = []; module_lut = new hashtbl_lookup; module_nonexistent_lut = new hashtbl_lookup; diff --git a/src/context/typecore.ml b/src/context/typecore.ml index d756d1fdbca..8760eafd96f 100644 --- a/src/context/typecore.ml +++ b/src/context/typecore.ml @@ -80,6 +80,7 @@ type typer_globals = { mutable macros : ((unit -> unit) * typer) option; mutable std : module_def; type_patches : (path, (string * bool, type_patch) Hashtbl.t * type_patch) Hashtbl.t; + mutable global_metadata : (string list * metadata_entry * (bool * bool * bool)) list; mutable module_check_policies : (string list * module_check_policy list * bool) list; mutable global_using : (tclass * pos) list; (* Indicates that Typer.create() finished building this instance *) diff --git a/src/typing/macroContext.ml b/src/typing/macroContext.ml index ff77a76e9ca..713b4fcade0 100644 --- a/src/typing/macroContext.ml +++ b/src/typing/macroContext.ml @@ -117,14 +117,6 @@ let typing_timer ctx need_type f = raise e let make_macro_com_api com p = - let parse_metadata s p = - try - match ParserEntry.parse_string Grammar.parse_meta com.defines s null_pos raise_typing_error false with - | ParseSuccess(meta,_,_) -> meta - | ParseError(_,_,_) -> raise_typing_error "Malformed metadata string" p - with _ -> - raise_typing_error "Malformed metadata string" p - in { MacroApi.pos = p; get_com = (fun () -> com); @@ -286,11 +278,7 @@ let make_macro_com_api com p = Interp.exc_string "unsupported" ); add_global_metadata = (fun s1 s2 config p -> - let meta = parse_metadata s2 p in - List.iter (fun (m,el,_) -> - let m = (m,el,p) in - com.global_metadata <- (ExtString.String.nsplit s1 ".",m,config) :: com.global_metadata; - ) meta; + Interp.exc_string "unsupported" ); add_module_check_policy = (fun sl il b i -> Interp.exc_string "unsupported" @@ -549,7 +537,7 @@ let make_macro_api ctx p = let meta = parse_metadata s2 p in List.iter (fun (m,el,_) -> let m = (m,el,p) in - ctx.com.global_metadata <- (ExtString.String.nsplit s1 ".",m,config) :: ctx.com.global_metadata; + ctx.g.global_metadata <- (ExtString.String.nsplit s1 ".",m,config) :: ctx.g.global_metadata; ) meta; ); MacroApi.add_module_check_policy = (fun sl il b i -> diff --git a/src/typing/typeloadCheck.ml b/src/typing/typeloadCheck.ml index fd2ff03c618..3a6e41a3d24 100644 --- a/src/typing/typeloadCheck.ml +++ b/src/typing/typeloadCheck.ml @@ -312,7 +312,7 @@ let check_global_metadata ctx meta f_add mpath tpath so = List.iter (fun (sl2,m,(recursive,to_types,to_fields)) -> let add = ((field_mode && to_fields) || (not field_mode && to_types)) && (match_path recursive sl1 sl2) in if add then f_add m - ) ctx.com.global_metadata; + ) ctx.g.global_metadata; if ctx.is_display_file then delay ctx PCheckConstraint (fun () -> DisplayEmitter.check_display_metadata ctx meta) let check_module_types ctx m p t = diff --git a/src/typing/typer.ml b/src/typing/typer.ml index 70ab2b271c5..3e2aa66022a 100644 --- a/src/typing/typer.ml +++ b/src/typing/typer.ml @@ -1999,6 +1999,7 @@ let create com = core_api = None; macros = None; type_patches = Hashtbl.create 0; + global_metadata = []; module_check_policies = []; delayed = []; debug_delayed = []; From 454b9374585493fa552e41556fdb83324d127113 Mon Sep 17 00:00:00 2001 From: Rudy Ges Date: Sat, 30 Sep 2023 21:24:42 +0200 Subject: [PATCH 03/17] [typer] delay typer init to after init macros --- src/compiler/compiler.ml | 40 ++++++++++++++++-------------- src/compiler/displayOutput.ml | 2 +- src/context/common.ml | 2 ++ src/context/typecore.ml | 3 +-- src/typing/macroContext.ml | 46 +++++++++++++++++++++++++---------- src/typing/typeload.ml | 2 +- src/typing/typeloadCheck.ml | 2 +- src/typing/typer.ml | 5 ++-- 8 files changed, 63 insertions(+), 39 deletions(-) diff --git a/src/compiler/compiler.ml b/src/compiler/compiler.ml index 47f3a39072f..ee5bb5939d5 100644 --- a/src/compiler/compiler.ml +++ b/src/compiler/compiler.ml @@ -156,7 +156,7 @@ module Setup = struct add_std "eval"; "eval" - let create_typer_context ctx native_libs = + let create_typer_context ctx macros native_libs = let com = ctx.com in Common.log com ("Classpath: " ^ (String.concat ";" com.class_path)); let buffer = Buffer.create 64 in @@ -172,7 +172,7 @@ module Setup = struct let fl = List.map (fun (file,extern) -> NativeLibraryHandler.add_native_lib com file extern) (List.rev native_libs) in (* Native lib pass 2: Initialize *) List.iter (fun f -> f()) fl; - Typer.create com + Typer.create com macros let executable_path() = Extc.executable_path() @@ -270,15 +270,18 @@ let check_defines com = end (** Creates the typer context and types [classes] into it. *) -let do_type ctx tctx actx = - let com = tctx.Typecore.com in +let do_type ctx mctx actx = + let com = ctx.com in let t = Timer.timer ["typing"] in let cs = com.cs in CommonCache.maybe_add_context_sign cs com "before_init_macros"; com.stage <- CInitMacrosStart; - List.iter (MacroContext.call_init_macro tctx) (List.rev actx.config_macros); + let (mctx, api) = List.fold_left (fun (mctx,api) path -> + (MacroContext.call_init_macro ctx.com mctx api path) + ) (Option.map (fun (_,mctx) -> mctx) mctx, None) (List.rev actx.config_macros) in com.stage <- CInitMacrosDone; - ServerMessage.compiler_stage com; + let macros = match mctx with None -> None | Some mctx -> mctx.g.macros in + let tctx = Setup.create_typer_context ctx macros actx.native_libs in check_defines ctx.com; CommonCache.lock_signature com "after_init_macros"; com.callbacks#run com.callbacks#get_after_init_macros; @@ -295,7 +298,8 @@ let do_type ctx tctx actx = | (DMUsage _ | DMImplementation) -> FindReferences.find_possible_references tctx cs; | _ -> () end; - t() + t(); + tctx let finalize_typing ctx tctx = let t = Timer.timer ["finalize"] in @@ -326,7 +330,8 @@ let compile ctx actx callbacks = let com = ctx.com in (* Set up display configuration *) DisplayProcessing.process_display_configuration ctx; - let display_file_dot_path = DisplayProcessing.process_display_file com actx in + (* TODO handle display *) + (* let display_file_dot_path = DisplayProcessing.process_display_file com actx in *) let mctx = match com.platform with | CustomTarget name -> begin try @@ -351,19 +356,18 @@ let compile ctx actx callbacks = if actx.cmds = [] && not actx.did_something then actx.raise_usage(); end else begin (* Actual compilation starts here *) - let tctx = Setup.create_typer_context ctx actx.native_libs in - tctx.g.macros <- mctx; com.stage <- CTyperCreated; - ServerMessage.compiler_stage com; - let display_file_dot_path = DisplayProcessing.maybe_load_display_file_before_typing tctx display_file_dot_path in - begin try - do_type ctx tctx actx + (* let display_file_dot_path = DisplayProcessing.maybe_load_display_file_before_typing tctx display_file_dot_path in *) + let tctx = try + do_type ctx mctx actx with TypeloadParse.DisplayInMacroBlock -> - ignore(DisplayProcessing.load_display_module_in_macro tctx display_file_dot_path true); - end; - DisplayProcessing.handle_display_after_typing ctx tctx display_file_dot_path; + (* TODO *) + (* ignore(DisplayProcessing.load_display_module_in_macro tctx display_file_dot_path true); *) + assert false + in + (* DisplayProcessing.handle_display_after_typing ctx tctx display_file_dot_path; *) finalize_typing ctx tctx; - DisplayProcessing.handle_display_after_finalization ctx tctx display_file_dot_path; + (* DisplayProcessing.handle_display_after_finalization ctx tctx display_file_dot_path; *) filter ctx tctx; if ctx.has_error then raise Abort; Generate.check_auxiliary_output com actx; diff --git a/src/compiler/displayOutput.ml b/src/compiler/displayOutput.ml index 09e2c31aeef..eaccba4d847 100644 --- a/src/compiler/displayOutput.ml +++ b/src/compiler/displayOutput.ml @@ -344,7 +344,7 @@ let handle_type_path_exception ctx p c is_import pos = | None -> DisplayPath.TypePathHandler.complete_type_path com p | Some (c,cur_package) -> - let ctx = Typer.create com in + let ctx = Typer.create com None in DisplayPath.TypePathHandler.complete_type_path_inner ctx p c cur_package is_import end with Common.Abort msg -> error_ext ctx msg; diff --git a/src/context/common.ml b/src/context/common.ml index 3c16ca156c8..45dc48fc9cf 100644 --- a/src/context/common.ml +++ b/src/context/common.ml @@ -397,6 +397,7 @@ type context = { mutable user_metas : (string, Meta.user_meta) Hashtbl.t; mutable get_macros : unit -> context option; (* typing state *) + mutable global_metadata : (string list * metadata_entry * (bool * bool * bool)) list; shared : shared_context; display_information : display_information; file_lookup_cache : (string,string option) lookup; @@ -829,6 +830,7 @@ let create compilation_step cs version args = file = ""; types = []; callbacks = new compiler_callbacks; + global_metadata = []; modules = []; module_lut = new hashtbl_lookup; module_nonexistent_lut = new hashtbl_lookup; diff --git a/src/context/typecore.ml b/src/context/typecore.ml index 8760eafd96f..36a8d8a90cd 100644 --- a/src/context/typecore.ml +++ b/src/context/typecore.ml @@ -80,7 +80,6 @@ type typer_globals = { mutable macros : ((unit -> unit) * typer) option; mutable std : module_def; type_patches : (path, (string * bool, type_patch) Hashtbl.t * type_patch) Hashtbl.t; - mutable global_metadata : (string list * metadata_entry * (bool * bool * bool)) list; mutable module_check_policies : (string list * module_check_policy list * bool) list; mutable global_using : (tclass * pos) list; (* Indicates that Typer.create() finished building this instance *) @@ -217,7 +216,7 @@ let analyzer_run_on_expr_ref : (Common.context -> string -> texpr -> texpr) ref let cast_or_unify_raise_ref : (typer -> ?uctx:unification_context option -> Type.t -> texpr -> pos -> texpr) ref = ref (fun _ ?uctx _ _ _ -> assert false) let type_generic_function_ref : (typer -> field_access -> (unit -> texpr) field_call_candidate -> WithType.t -> pos -> texpr) ref = ref (fun _ _ _ _ _ -> assert false) -let create_context_ref : (Common.context -> typer) ref = ref (fun _ -> assert false) +let create_context_ref : (Common.context -> ((unit -> unit) * typer) option -> typer) ref = ref (fun _ -> assert false) let pass_name = function | PBuildModule -> "build-module" diff --git a/src/typing/macroContext.ml b/src/typing/macroContext.ml index 713b4fcade0..2b9848d108f 100644 --- a/src/typing/macroContext.ml +++ b/src/typing/macroContext.ml @@ -117,6 +117,14 @@ let typing_timer ctx need_type f = raise e let make_macro_com_api com p = + let parse_metadata s p = + try + match ParserEntry.parse_string Grammar.parse_meta com.defines s null_pos raise_typing_error false with + | ParseSuccess(meta,_,_) -> meta + | ParseError(_,_,_) -> raise_typing_error "Malformed metadata string" p + with _ -> + raise_typing_error "Malformed metadata string" p + in { MacroApi.pos = p; get_com = (fun () -> com); @@ -278,7 +286,11 @@ let make_macro_com_api com p = Interp.exc_string "unsupported" ); add_global_metadata = (fun s1 s2 config p -> - Interp.exc_string "unsupported" + let meta = parse_metadata s2 p in + List.iter (fun (m,el,_) -> + let m = (m,el,p) in + com.global_metadata <- (ExtString.String.nsplit s1 ".",m,config) :: com.global_metadata; + ) meta; ); add_module_check_policy = (fun sl il b i -> Interp.exc_string "unsupported" @@ -537,7 +549,7 @@ let make_macro_api ctx p = let meta = parse_metadata s2 p in List.iter (fun (m,el,_) -> let m = (m,el,p) in - ctx.g.global_metadata <- (ExtString.String.nsplit s1 ".",m,config) :: ctx.g.global_metadata; + ctx.com.global_metadata <- (ExtString.String.nsplit s1 ".",m,config) :: ctx.com.global_metadata; ) meta; ); MacroApi.add_module_check_policy = (fun sl il b i -> @@ -712,7 +724,7 @@ let create_macro_context com = com2.defines.defines_signature <- None; com2.platform <- !Globals.macro_platform; Common.init_platform com2; - let mctx = !create_context_ref com2 in + let mctx = !create_context_ref com2 None in mctx.is_display_file <- false; CommonCache.lock_signature com2 "get_macro_context"; mctx @@ -724,9 +736,8 @@ let get_macro_context ctx = ctx | None -> let mctx = create_macro_context ctx.com in - let api = make_macro_api ctx null_pos in + let api = make_macro_api mctx null_pos in let init = create_macro_interp api mctx in - ctx.g.macros <- Some (init,mctx); mctx.g.macros <- Some (init,mctx); mctx @@ -803,10 +814,8 @@ let do_call_macro com api cpath f args p = if com.verbose then Common.log com ("Exiting macro " ^ s_type_path cpath ^ "." ^ f); r -let load_macro ctx display cpath f p = - let api = make_macro_api ctx p in - let mctx = get_macro_context ctx in - let meth,mloaded = load_macro'' ctx.com mctx display cpath f p in +let load_macro ctx com mctx api display cpath f p = + let meth,mloaded = load_macro'' com mctx display cpath f p in let _,_,{cl_path = cpath},_ = meth in let call args = add_dependency ctx.m.curmod mloaded; @@ -820,7 +829,9 @@ type macro_arg_type = | MAOther let type_macro ctx mode cpath f (el:Ast.expr list) p = - let mctx, (margs,mret,mclass,mfield), call_macro = load_macro ctx (mode = MDisplay) cpath f p in + let api = make_macro_api ctx p in + let mctx = get_macro_context ctx in + let mctx, (margs,mret,mclass,mfield), call_macro = load_macro ctx ctx.com mctx api (mode = MDisplay) cpath f p in let margs = (* Replace "rest:haxe.Rest" in macro signatures with "rest:Array". @@ -1031,10 +1042,19 @@ let resolve_init_macro com e = | _ -> raise_typing_error "Invalid macro call" p -let call_init_macro ctx e = - let (path,meth,args,p) = resolve_init_macro ctx.com e in - let mctx, (margs,_,mclass,mfield), call = load_macro ctx false path meth p in +let call_init_macro com mctx api e = + let mctx = match mctx with Some mctx -> mctx | None -> create_macro_context com in + let api = match api with Some api -> api | None -> + let api = make_macro_com_api com null_pos in + let init = create_macro_interp api mctx in + init(); + api + in + + let (path,meth,args,p) = resolve_init_macro com e in + let mctx, (margs,_,mclass,mfield), call = load_macro mctx com mctx api false path meth p in ignore(call_macro mctx args margs call p); + (Some mctx, Some api) module MacroLight = struct let load_macro_light com mctx api display cpath f p = diff --git a/src/typing/typeload.ml b/src/typing/typeload.ml index 73e8f79a89f..9944878c7b3 100644 --- a/src/typing/typeload.ml +++ b/src/typing/typeload.ml @@ -823,7 +823,7 @@ let load_core_class ctx c = com2.class_path <- ctx.com.std_path; if com2.display.dms_check_core_api then com2.display <- {com2.display with dms_check_core_api = false}; CommonCache.lock_signature com2 "load_core_class"; - let ctx2 = !create_context_ref com2 in + let ctx2 = !create_context_ref com2 ctx.g.macros in ctx.g.core_api <- Some ctx2; ctx2 | Some c -> diff --git a/src/typing/typeloadCheck.ml b/src/typing/typeloadCheck.ml index 3a6e41a3d24..fd2ff03c618 100644 --- a/src/typing/typeloadCheck.ml +++ b/src/typing/typeloadCheck.ml @@ -312,7 +312,7 @@ let check_global_metadata ctx meta f_add mpath tpath so = List.iter (fun (sl2,m,(recursive,to_types,to_fields)) -> let add = ((field_mode && to_fields) || (not field_mode && to_types)) && (match_path recursive sl1 sl2) in if add then f_add m - ) ctx.g.global_metadata; + ) ctx.com.global_metadata; if ctx.is_display_file then delay ctx PCheckConstraint (fun () -> DisplayEmitter.check_display_metadata ctx meta) let check_module_types ctx m p t = diff --git a/src/typing/typer.ml b/src/typing/typer.ml index 3e2aa66022a..0a71bbb9b20 100644 --- a/src/typing/typer.ml +++ b/src/typing/typer.ml @@ -1991,15 +1991,14 @@ and type_expr ?(mode=MGet) ctx (e,p) (with_type:WithType.t) = (* ---------------------------------------------------------------------- *) (* TYPER INITIALIZATION *) -let create com = +let create com macros = let ctx = { com = com; t = com.basic; g = { core_api = None; - macros = None; + macros = macros; type_patches = Hashtbl.create 0; - global_metadata = []; module_check_policies = []; delayed = []; debug_delayed = []; From b04b503933deeaa0aa6dbb65f6b0b2145bfb179c Mon Sep 17 00:00:00 2001 From: Rudy Ges Date: Mon, 2 Oct 2023 20:53:44 +0200 Subject: [PATCH 04/17] [debug] add compiler stage debug --- src/compiler/compiler.ml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/compiler/compiler.ml b/src/compiler/compiler.ml index ee5bb5939d5..d8a65ac9517 100644 --- a/src/compiler/compiler.ml +++ b/src/compiler/compiler.ml @@ -276,10 +276,12 @@ let do_type ctx mctx actx = let cs = com.cs in CommonCache.maybe_add_context_sign cs com "before_init_macros"; com.stage <- CInitMacrosStart; + ServerMessage.compiler_stage com; let (mctx, api) = List.fold_left (fun (mctx,api) path -> (MacroContext.call_init_macro ctx.com mctx api path) ) (Option.map (fun (_,mctx) -> mctx) mctx, None) (List.rev actx.config_macros) in com.stage <- CInitMacrosDone; + ServerMessage.compiler_stage com; let macros = match mctx with None -> None | Some mctx -> mctx.g.macros in let tctx = Setup.create_typer_context ctx macros actx.native_libs in check_defines ctx.com; @@ -357,6 +359,7 @@ let compile ctx actx callbacks = end else begin (* Actual compilation starts here *) com.stage <- CTyperCreated; + ServerMessage.compiler_stage com; (* let display_file_dot_path = DisplayProcessing.maybe_load_display_file_before_typing tctx display_file_dot_path in *) let tctx = try do_type ctx mctx actx From 04ff266cc40c5b6e569b09c73d1d29ff735d0e44 Mon Sep 17 00:00:00 2001 From: Rudy Ges Date: Tue, 3 Oct 2023 08:05:28 +0200 Subject: [PATCH 05/17] [typer] fix macro context when no init macros --- src/typing/macroContext.ml | 1 + 1 file changed, 1 insertion(+) diff --git a/src/typing/macroContext.ml b/src/typing/macroContext.ml index 2b9848d108f..4b8d832be65 100644 --- a/src/typing/macroContext.ml +++ b/src/typing/macroContext.ml @@ -738,6 +738,7 @@ let get_macro_context ctx = let mctx = create_macro_context ctx.com in let api = make_macro_api mctx null_pos in let init = create_macro_interp api mctx in + ctx.g.macros <- Some (init,mctx); mctx.g.macros <- Some (init,mctx); mctx From 70b78a77597b691de56a227df8d61112ad9d1c35 Mon Sep 17 00:00:00 2001 From: Rudy Ges Date: Tue, 3 Oct 2023 12:30:43 +0200 Subject: [PATCH 06/17] [typer] promote macro api to full api after init macros --- src/compiler/compiler.ml | 11 ++++++++--- src/macro/macroApi.ml | 7 +++++-- src/typing/macroContext.ml | 24 +++++++++++++++--------- 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/compiler/compiler.ml b/src/compiler/compiler.ml index d8a65ac9517..4456ec94498 100644 --- a/src/compiler/compiler.ml +++ b/src/compiler/compiler.ml @@ -275,13 +275,17 @@ let do_type ctx mctx actx = let t = Timer.timer ["typing"] in let cs = com.cs in CommonCache.maybe_add_context_sign cs com "before_init_macros"; + let macro_cache_enabled = !MacroContext.macro_enable_cache in + MacroContext.macro_enable_cache := true; com.stage <- CInitMacrosStart; ServerMessage.compiler_stage com; - let (mctx, api) = List.fold_left (fun (mctx,api) path -> - (MacroContext.call_init_macro ctx.com mctx api path) - ) (Option.map (fun (_,mctx) -> mctx) mctx, None) (List.rev actx.config_macros) in + + let mctx = List.fold_left (fun mctx path -> + Some (MacroContext.call_init_macro ctx.com mctx path) + ) (Option.map (fun (_,mctx) -> mctx) mctx) (List.rev actx.config_macros) in com.stage <- CInitMacrosDone; ServerMessage.compiler_stage com; + MacroContext.macro_enable_cache := macro_cache_enabled; let macros = match mctx with None -> None | Some mctx -> mctx.g.macros in let tctx = Setup.create_typer_context ctx macros actx.native_libs in check_defines ctx.com; @@ -330,6 +334,7 @@ let call_light_init_macro com path = let compile ctx actx callbacks = let com = ctx.com in + MacroContext.macro_interp_cache := None; (* Set up display configuration *) DisplayProcessing.process_display_configuration ctx; (* TODO handle display *) diff --git a/src/macro/macroApi.ml b/src/macro/macroApi.ml index e825da0938e..4eb2bc0a7b8 100644 --- a/src/macro/macroApi.ml +++ b/src/macro/macroApi.ml @@ -19,6 +19,7 @@ type compiler_options = { **) type 'value compiler_api = { + is_full : bool; pos : Globals.pos; get_com : unit -> Common.context; get_macro_stack : unit -> pos list; @@ -1893,8 +1894,10 @@ let macro_api ccom get_api = encode_array (List.map encode_type ((get_api()).get_module (decode_string s))) ); "on_after_init_macros", vfun1 (fun f -> - let f = prepare_callback f 1 in - (get_api()).after_init_macros (fun tl -> ignore(f [])); + if (get_api()).is_full then begin + let f = prepare_callback f 1 in + (get_api()).after_init_macros (fun tctx -> ignore(f [])); + end; vnull ); "on_after_typing", vfun1 (fun f -> diff --git a/src/typing/macroContext.ml b/src/typing/macroContext.ml index 4b8d832be65..1c3d447f37f 100644 --- a/src/typing/macroContext.ml +++ b/src/typing/macroContext.ml @@ -127,6 +127,7 @@ let make_macro_com_api com p = in { MacroApi.pos = p; + MacroApi.is_full = false; get_com = (fun () -> com); get_macro_stack = (fun () -> let envs = Interp.call_stack (Interp.get_eval (Interp.get_ctx ())) in @@ -316,7 +317,7 @@ let make_macro_com_api com p = ); } -let make_macro_api ctx p = +and promote_com_api com_api ctx p = let parse_metadata s p = try match ParserEntry.parse_string Grammar.parse_meta ctx.com.defines s null_pos raise_typing_error false with @@ -325,9 +326,9 @@ let make_macro_api ctx p = with _ -> raise_typing_error "Malformed metadata string" p in - let com_api = make_macro_com_api ctx.com p in { com_api with + MacroApi.is_full = true; MacroApi.get_type = (fun s -> typing_timer ctx false (fun() -> let path = parse_path s in @@ -609,6 +610,10 @@ let make_macro_api ctx p = ); } +let make_macro_api ctx p = + let com_api = make_macro_com_api ctx.com p in + promote_com_api com_api ctx p + let init_macro_interp mctx mint = let p = null_pos in ignore(TypeloadModule.load_module mctx (["haxe";"macro"],"Expr") p); @@ -1043,19 +1048,20 @@ let resolve_init_macro com e = | _ -> raise_typing_error "Invalid macro call" p -let call_init_macro com mctx api e = +let call_init_macro com mctx e = + let (path,meth,args,p) = resolve_init_macro com e in + let mctx = match mctx with Some mctx -> mctx | None -> create_macro_context com in - let api = match api with Some api -> api | None -> - let api = make_macro_com_api com null_pos in + let api = make_macro_com_api com p in + (match !macro_interp_cache with + | None -> let init = create_macro_interp api mctx in init(); - api - in + | _ -> ()); - let (path,meth,args,p) = resolve_init_macro com e in let mctx, (margs,_,mclass,mfield), call = load_macro mctx com mctx api false path meth p in ignore(call_macro mctx args margs call p); - (Some mctx, Some api) + mctx module MacroLight = struct let load_macro_light com mctx api display cpath f p = From 240f037561b3bde87e7bd64182e49d68286c6ecf Mon Sep 17 00:00:00 2001 From: Rudy Ges Date: Tue, 3 Oct 2023 15:00:04 +0200 Subject: [PATCH 07/17] [typer] remove CTyperCreated stage --- src/compiler/compiler.ml | 1 - src/context/common.ml | 2 -- 2 files changed, 3 deletions(-) diff --git a/src/compiler/compiler.ml b/src/compiler/compiler.ml index 4456ec94498..129f1bf3ba5 100644 --- a/src/compiler/compiler.ml +++ b/src/compiler/compiler.ml @@ -363,7 +363,6 @@ let compile ctx actx callbacks = if actx.cmds = [] && not actx.did_something then actx.raise_usage(); end else begin (* Actual compilation starts here *) - com.stage <- CTyperCreated; ServerMessage.compiler_stage com; (* let display_file_dot_path = DisplayProcessing.maybe_load_display_file_before_typing tctx display_file_dot_path in *) let tctx = try diff --git a/src/context/common.ml b/src/context/common.ml index 45dc48fc9cf..418f70efb32 100644 --- a/src/context/common.ml +++ b/src/context/common.ml @@ -256,7 +256,6 @@ type json_api = { type compiler_stage = | CCreated (* Context was just created *) | CInitialized (* Context was initialized (from CLI args and such). *) - | CTyperCreated (* The typer context was just created. *) | CInitMacrosStart (* Init macros are about to run. *) | CInitMacrosDone (* Init macros did run - at this point the signature is locked. *) | CTypingDone (* The typer is done - at this point com.types/modules/main is filled. *) @@ -274,7 +273,6 @@ type compiler_stage = let s_compiler_stage = function | CCreated -> "CCreated" | CInitialized -> "CInitialized" - | CTyperCreated -> "CTyperCreated" | CInitMacrosStart -> "CInitMacrosStart" | CInitMacrosDone -> "CInitMacrosDone" | CTypingDone -> "CTypingDone" From 280df4ffbfdbf6768e7d27cc018e883ac89ec1ef Mon Sep 17 00:00:00 2001 From: Rudy Ges Date: Tue, 3 Oct 2023 15:01:00 +0200 Subject: [PATCH 08/17] [display] restore DisplayProcessing --- src/compiler/compiler.ml | 41 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/src/compiler/compiler.ml b/src/compiler/compiler.ml index 129f1bf3ba5..caede6bd5fd 100644 --- a/src/compiler/compiler.ml +++ b/src/compiler/compiler.ml @@ -270,7 +270,7 @@ let check_defines com = end (** Creates the typer context and types [classes] into it. *) -let do_type ctx mctx actx = +let do_type ctx mctx actx display_file_dot_path = let com = ctx.com in let t = Timer.timer ["typing"] in let cs = com.cs in @@ -288,14 +288,22 @@ let do_type ctx mctx actx = MacroContext.macro_enable_cache := macro_cache_enabled; let macros = match mctx with None -> None | Some mctx -> mctx.g.macros in let tctx = Setup.create_typer_context ctx macros actx.native_libs in + let display_file_dot_path = DisplayProcessing.maybe_load_display_file_before_typing tctx display_file_dot_path in check_defines ctx.com; CommonCache.lock_signature com "after_init_macros"; - com.callbacks#run com.callbacks#get_after_init_macros; - run_or_diagnose ctx (fun () -> - if com.display.dms_kind <> DMNone then DisplayTexpr.check_display_file tctx cs; - List.iter (fun cpath -> ignore(tctx.Typecore.g.Typecore.do_load_module tctx cpath null_pos)) (List.rev actx.classes); - Finalization.finalize tctx; - ) (); + + (try begin + com.callbacks#run com.callbacks#get_after_init_macros; + + run_or_diagnose ctx (fun () -> + if com.display.dms_kind <> DMNone then DisplayTexpr.check_display_file tctx cs; + List.iter (fun cpath -> ignore(tctx.Typecore.g.Typecore.do_load_module tctx cpath null_pos)) (List.rev actx.classes); + Finalization.finalize tctx; + ) (); + end with TypeloadParse.DisplayInMacroBlock -> + ignore(DisplayProcessing.load_display_module_in_macro tctx display_file_dot_path true) + ); + com.stage <- CTypingDone; ServerMessage.compiler_stage com; (* If we are trying to find references, let's syntax-explore everything we know to check for the @@ -305,7 +313,7 @@ let do_type ctx mctx actx = | _ -> () end; t(); - tctx + (tctx, display_file_dot_path) let finalize_typing ctx tctx = let t = Timer.timer ["finalize"] in @@ -337,8 +345,7 @@ let compile ctx actx callbacks = MacroContext.macro_interp_cache := None; (* Set up display configuration *) DisplayProcessing.process_display_configuration ctx; - (* TODO handle display *) - (* let display_file_dot_path = DisplayProcessing.process_display_file com actx in *) + let display_file_dot_path = DisplayProcessing.process_display_file com actx in let mctx = match com.platform with | CustomTarget name -> begin try @@ -363,18 +370,10 @@ let compile ctx actx callbacks = if actx.cmds = [] && not actx.did_something then actx.raise_usage(); end else begin (* Actual compilation starts here *) - ServerMessage.compiler_stage com; - (* let display_file_dot_path = DisplayProcessing.maybe_load_display_file_before_typing tctx display_file_dot_path in *) - let tctx = try - do_type ctx mctx actx - with TypeloadParse.DisplayInMacroBlock -> - (* TODO *) - (* ignore(DisplayProcessing.load_display_module_in_macro tctx display_file_dot_path true); *) - assert false - in - (* DisplayProcessing.handle_display_after_typing ctx tctx display_file_dot_path; *) + let (tctx,display_file_dot_path) = do_type ctx mctx actx display_file_dot_path in + DisplayProcessing.handle_display_after_typing ctx tctx display_file_dot_path; finalize_typing ctx tctx; - (* DisplayProcessing.handle_display_after_finalization ctx tctx display_file_dot_path; *) + DisplayProcessing.handle_display_after_finalization ctx tctx display_file_dot_path; filter ctx tctx; if ctx.has_error then raise Abort; Generate.check_auxiliary_output com actx; From a3d7d03169dc03d14983db916f0b388b3d71de74 Mon Sep 17 00:00:00 2001 From: Rudy Ges Date: Tue, 3 Oct 2023 16:37:47 +0200 Subject: [PATCH 09/17] [typer] rework after init macro --- src/compiler/compiler.ml | 2 ++ src/macro/macroApi.ml | 7 +++---- src/typing/macroContext.ml | 13 ++++++++----- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/compiler/compiler.ml b/src/compiler/compiler.ml index caede6bd5fd..f24b8d9d42b 100644 --- a/src/compiler/compiler.ml +++ b/src/compiler/compiler.ml @@ -286,11 +286,13 @@ let do_type ctx mctx actx display_file_dot_path = com.stage <- CInitMacrosDone; ServerMessage.compiler_stage com; MacroContext.macro_enable_cache := macro_cache_enabled; + let macros = match mctx with None -> None | Some mctx -> mctx.g.macros in let tctx = Setup.create_typer_context ctx macros actx.native_libs in let display_file_dot_path = DisplayProcessing.maybe_load_display_file_before_typing tctx display_file_dot_path in check_defines ctx.com; CommonCache.lock_signature com "after_init_macros"; + Option.may (fun mctx -> MacroContext.finalize_macro_api tctx mctx) mctx; (try begin com.callbacks#run com.callbacks#get_after_init_macros; diff --git a/src/macro/macroApi.ml b/src/macro/macroApi.ml index 4eb2bc0a7b8..24ae6f6ebe0 100644 --- a/src/macro/macroApi.ml +++ b/src/macro/macroApi.ml @@ -19,7 +19,6 @@ type compiler_options = { **) type 'value compiler_api = { - is_full : bool; pos : Globals.pos; get_com : unit -> Common.context; get_macro_stack : unit -> pos list; @@ -1894,10 +1893,10 @@ let macro_api ccom get_api = encode_array (List.map encode_type ((get_api()).get_module (decode_string s))) ); "on_after_init_macros", vfun1 (fun f -> - if (get_api()).is_full then begin + (get_api()).after_init_macros (fun tctx -> let f = prepare_callback f 1 in - (get_api()).after_init_macros (fun tctx -> ignore(f [])); - end; + ignore(f []) + ); vnull ); "on_after_typing", vfun1 (fun f -> diff --git a/src/typing/macroContext.ml b/src/typing/macroContext.ml index 1c3d447f37f..c515dd07623 100644 --- a/src/typing/macroContext.ml +++ b/src/typing/macroContext.ml @@ -127,7 +127,6 @@ let make_macro_com_api com p = in { MacroApi.pos = p; - MacroApi.is_full = false; get_com = (fun () -> com); get_macro_stack = (fun () -> let envs = Interp.call_stack (Interp.get_eval (Interp.get_ctx ())) in @@ -328,7 +327,6 @@ and promote_com_api com_api ctx p = in { com_api with - MacroApi.is_full = true; MacroApi.get_type = (fun s -> typing_timer ctx false (fun() -> let path = parse_path s in @@ -712,7 +710,7 @@ let create_macro_interp api mctx = init(); let init = (fun() -> Interp.select mint) in mctx.g.macros <- Some (init,mctx); - init + (init, mint) let create_macro_context com = let com2 = Common.clone com true in @@ -742,7 +740,7 @@ let get_macro_context ctx = | None -> let mctx = create_macro_context ctx.com in let api = make_macro_api mctx null_pos in - let init = create_macro_interp api mctx in + let init,_ = create_macro_interp api mctx in ctx.g.macros <- Some (init,mctx); mctx.g.macros <- Some (init,mctx); mctx @@ -1055,7 +1053,7 @@ let call_init_macro com mctx e = let api = make_macro_com_api com p in (match !macro_interp_cache with | None -> - let init = create_macro_interp api mctx in + let init,_ = create_macro_interp api mctx in init(); | _ -> ()); @@ -1063,6 +1061,11 @@ let call_init_macro com mctx e = ignore(call_macro mctx args margs call p); mctx +let finalize_macro_api tctx mctx = + let api = make_macro_api tctx null_pos in + let mint = (match !macro_interp_cache with None -> snd (create_macro_interp api mctx) | Some mint -> mint) in + Interp.do_reuse mint api; + module MacroLight = struct let load_macro_light com mctx api display cpath f p = let api = {api with MacroApi.pos = p} in From 19ec8c69007a1e90a5b03601746244f441b629f7 Mon Sep 17 00:00:00 2001 From: Rudy Ges Date: Tue, 3 Oct 2023 20:44:46 +0200 Subject: [PATCH 10/17] [typer] fix macro api context when no init macros --- src/typing/macroContext.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/typing/macroContext.ml b/src/typing/macroContext.ml index c515dd07623..31f96fc62d5 100644 --- a/src/typing/macroContext.ml +++ b/src/typing/macroContext.ml @@ -739,7 +739,7 @@ let get_macro_context ctx = ctx | None -> let mctx = create_macro_context ctx.com in - let api = make_macro_api mctx null_pos in + let api = make_macro_api ctx null_pos in let init,_ = create_macro_interp api mctx in ctx.g.macros <- Some (init,mctx); mctx.g.macros <- Some (init,mctx); From 85fb34bd16a12701473a8ed73f5280b9ae6f569e Mon Sep 17 00:00:00 2001 From: Rudy Ges Date: Wed, 4 Oct 2023 07:15:25 +0200 Subject: [PATCH 11/17] [macro] don't reset static prototype after init macros --- src/typing/macroContext.ml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/typing/macroContext.ml b/src/typing/macroContext.ml index 31f96fc62d5..866fbd261dd 100644 --- a/src/typing/macroContext.ml +++ b/src/typing/macroContext.ml @@ -1053,18 +1053,20 @@ let call_init_macro com mctx e = let api = make_macro_com_api com p in (match !macro_interp_cache with | None -> + (* trace "create macro interp for init macro"; *) let init,_ = create_macro_interp api mctx in init(); | _ -> ()); let mctx, (margs,_,mclass,mfield), call = load_macro mctx com mctx api false path meth p in ignore(call_macro mctx args margs call p); + (* (Some mctx, Some api) *) mctx let finalize_macro_api tctx mctx = let api = make_macro_api tctx null_pos in let mint = (match !macro_interp_cache with None -> snd (create_macro_interp api mctx) | Some mint -> mint) in - Interp.do_reuse mint api; + mint.curapi <- api; module MacroLight = struct let load_macro_light com mctx api display cpath f p = From f4d4a526b04c13db461697bac1eaacbd283b1154 Mon Sep 17 00:00:00 2001 From: Rudy Ges Date: Wed, 4 Oct 2023 07:46:29 +0200 Subject: [PATCH 12/17] small cleanup --- src/typing/macroContext.ml | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/typing/macroContext.ml b/src/typing/macroContext.ml index 866fbd261dd..c9aae175d3b 100644 --- a/src/typing/macroContext.ml +++ b/src/typing/macroContext.ml @@ -1053,14 +1053,12 @@ let call_init_macro com mctx e = let api = make_macro_com_api com p in (match !macro_interp_cache with | None -> - (* trace "create macro interp for init macro"; *) let init,_ = create_macro_interp api mctx in init(); | _ -> ()); let mctx, (margs,_,mclass,mfield), call = load_macro mctx com mctx api false path meth p in ignore(call_macro mctx args margs call p); - (* (Some mctx, Some api) *) mctx let finalize_macro_api tctx mctx = From f618741a0f1b464f43265417c35baa7847de8cad Mon Sep 17 00:00:00 2001 From: Rudy Ges Date: Wed, 4 Oct 2023 16:24:45 +0200 Subject: [PATCH 13/17] Remove MacroLight, simplify init macro context creation --- src/compiler/compiler.ml | 12 ++---------- src/typing/macroContext.ml | 26 ++------------------------ 2 files changed, 4 insertions(+), 34 deletions(-) diff --git a/src/compiler/compiler.ml b/src/compiler/compiler.ml index f24b8d9d42b..45e2fe5a0ec 100644 --- a/src/compiler/compiler.ml +++ b/src/compiler/compiler.ml @@ -282,7 +282,7 @@ let do_type ctx mctx actx display_file_dot_path = let mctx = List.fold_left (fun mctx path -> Some (MacroContext.call_init_macro ctx.com mctx path) - ) (Option.map (fun (_,mctx) -> mctx) mctx) (List.rev actx.config_macros) in + ) mctx (List.rev actx.config_macros) in com.stage <- CInitMacrosDone; ServerMessage.compiler_stage com; MacroContext.macro_enable_cache := macro_cache_enabled; @@ -334,14 +334,6 @@ let filter ctx tctx = Filters.run tctx ctx.com.main; t() -let call_light_init_macro com path = - let open MacroContext in - let mctx = create_macro_context com in - let api = make_macro_com_api com null_pos in - let init = create_macro_interp api mctx in - MacroContext.MacroLight.call_init_macro com mctx api path; - (init,mctx) - let compile ctx actx callbacks = let com = ctx.com in MacroContext.macro_interp_cache := None; @@ -351,7 +343,7 @@ let compile ctx actx callbacks = let mctx = match com.platform with | CustomTarget name -> begin try - Some (call_light_init_macro com (Printf.sprintf "%s.Init.init()" name)) + Some (MacroContext.call_init_macro com None (Printf.sprintf "%s.Init.init()" name)) with (Error.Error { err_message = Module_not_found ([pack],"Init") }) when pack = name -> (* ignore if .Init doesn't exist *) None diff --git a/src/typing/macroContext.ml b/src/typing/macroContext.ml index c9aae175d3b..6cd990d391a 100644 --- a/src/typing/macroContext.ml +++ b/src/typing/macroContext.ml @@ -1051,12 +1051,7 @@ let call_init_macro com mctx e = let mctx = match mctx with Some mctx -> mctx | None -> create_macro_context com in let api = make_macro_com_api com p in - (match !macro_interp_cache with - | None -> - let init,_ = create_macro_interp api mctx in - init(); - | _ -> ()); - + if Option.is_none !macro_interp_cache then (fst (create_macro_interp api mctx)) (); let mctx, (margs,_,mclass,mfield), call = load_macro mctx com mctx api false path meth p in ignore(call_macro mctx args margs call p); mctx @@ -1064,24 +1059,7 @@ let call_init_macro com mctx e = let finalize_macro_api tctx mctx = let api = make_macro_api tctx null_pos in let mint = (match !macro_interp_cache with None -> snd (create_macro_interp api mctx) | Some mint -> mint) in - mint.curapi <- api; - -module MacroLight = struct - let load_macro_light com mctx api display cpath f p = - let api = {api with MacroApi.pos = p} in - let meth,mloaded = load_macro'' com mctx display cpath f p in - let _,_,{cl_path = cpath},_ = meth in - let call args = - do_call_macro com api cpath f args p - in - mctx, meth, call - - let call_init_macro com mctx api e = - let (path,meth,args,p) = resolve_init_macro com e in - let mctx, (margs,_,mclass,mfield), call = load_macro_light com mctx api false path meth p in - ignore(call_macro mctx args margs call p); - -end + mint.curapi <- api let interpret ctx = let mctx = Interp.create ctx.com (make_macro_api ctx null_pos) false in From daaf8c7b0e7246a564577001e548931dd625b9e0 Mon Sep 17 00:00:00 2001 From: Rudy Ges Date: Wed, 4 Oct 2023 16:35:05 +0200 Subject: [PATCH 14/17] Reduce diff --- src/macro/macroApi.ml | 6 ++---- src/typing/macroContext.ml | 12 +++++------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/macro/macroApi.ml b/src/macro/macroApi.ml index 24ae6f6ebe0..670a6de5c2a 100644 --- a/src/macro/macroApi.ml +++ b/src/macro/macroApi.ml @@ -1893,10 +1893,8 @@ let macro_api ccom get_api = encode_array (List.map encode_type ((get_api()).get_module (decode_string s))) ); "on_after_init_macros", vfun1 (fun f -> - (get_api()).after_init_macros (fun tctx -> - let f = prepare_callback f 1 in - ignore(f []) - ); + let f = prepare_callback f 1 in + (get_api()).after_init_macros (fun tctx -> ignore(f [])); vnull ); "on_after_typing", vfun1 (fun f -> diff --git a/src/typing/macroContext.ml b/src/typing/macroContext.ml index 6cd990d391a..7763e056d46 100644 --- a/src/typing/macroContext.ml +++ b/src/typing/macroContext.ml @@ -316,7 +316,7 @@ let make_macro_com_api com p = ); } -and promote_com_api com_api ctx p = +let make_macro_api ctx p = let parse_metadata s p = try match ParserEntry.parse_string Grammar.parse_meta ctx.com.defines s null_pos raise_typing_error false with @@ -325,6 +325,7 @@ and promote_com_api com_api ctx p = with _ -> raise_typing_error "Malformed metadata string" p in + let com_api = make_macro_com_api ctx.com p in { com_api with MacroApi.get_type = (fun s -> @@ -608,10 +609,6 @@ and promote_com_api com_api ctx p = ); } -let make_macro_api ctx p = - let com_api = make_macro_com_api ctx.com p in - promote_com_api com_api ctx p - let init_macro_interp mctx mint = let p = null_pos in ignore(TypeloadModule.load_module mctx (["haxe";"macro"],"Expr") p); @@ -1058,8 +1055,9 @@ let call_init_macro com mctx e = let finalize_macro_api tctx mctx = let api = make_macro_api tctx null_pos in - let mint = (match !macro_interp_cache with None -> snd (create_macro_interp api mctx) | Some mint -> mint) in - mint.curapi <- api + match !macro_interp_cache with + | None -> ignore(create_macro_interp api mctx) + | Some mint -> mint.curapi <- api let interpret ctx = let mctx = Interp.create ctx.com (make_macro_api ctx null_pos) false in From 4a981cf0a7b51c9cc85d1804c88ffa2dd248e111 Mon Sep 17 00:00:00 2001 From: Rudy Ges Date: Wed, 4 Oct 2023 17:10:07 +0200 Subject: [PATCH 15/17] Cleanup interp cache handling --- src/compiler/compiler.ml | 1 - src/typing/macroContext.ml | 14 +++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/compiler/compiler.ml b/src/compiler/compiler.ml index 45e2fe5a0ec..3364958afa6 100644 --- a/src/compiler/compiler.ml +++ b/src/compiler/compiler.ml @@ -336,7 +336,6 @@ let filter ctx tctx = let compile ctx actx callbacks = let com = ctx.com in - MacroContext.macro_interp_cache := None; (* Set up display configuration *) DisplayProcessing.process_display_configuration ctx; let display_file_dot_path = DisplayProcessing.process_display_file com actx in diff --git a/src/typing/macroContext.ml b/src/typing/macroContext.ml index 7763e056d46..db71db80a87 100644 --- a/src/typing/macroContext.ml +++ b/src/typing/macroContext.ml @@ -1045,10 +1045,18 @@ let resolve_init_macro com e = let call_init_macro com mctx e = let (path,meth,args,p) = resolve_init_macro com e in + let (mctx, api) = match mctx with + | Some mctx -> + let api = make_macro_com_api com p in + (mctx, api) + | None -> + let mctx = create_macro_context com in + let api = make_macro_com_api com p in + let init,_ = create_macro_interp api mctx in + mctx.g.macros <- Some (init,mctx); + (mctx, api) + in - let mctx = match mctx with Some mctx -> mctx | None -> create_macro_context com in - let api = make_macro_com_api com p in - if Option.is_none !macro_interp_cache then (fst (create_macro_interp api mctx)) (); let mctx, (margs,_,mclass,mfield), call = load_macro mctx com mctx api false path meth p in ignore(call_macro mctx args margs call p); mctx From 7074ea16d47839bf79a0ac515ac5657de40ba825 Mon Sep 17 00:00:00 2001 From: Rudy Ges Date: Thu, 5 Oct 2023 15:29:55 +0200 Subject: [PATCH 16/17] [custom targets] enable macro cache before setting up targets --- src/compiler/compiler.ml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/compiler/compiler.ml b/src/compiler/compiler.ml index 3364958afa6..0cd8ba39179 100644 --- a/src/compiler/compiler.ml +++ b/src/compiler/compiler.ml @@ -270,13 +270,11 @@ let check_defines com = end (** Creates the typer context and types [classes] into it. *) -let do_type ctx mctx actx display_file_dot_path = +let do_type ctx mctx actx display_file_dot_path macro_cache_enabled = let com = ctx.com in let t = Timer.timer ["typing"] in let cs = com.cs in CommonCache.maybe_add_context_sign cs com "before_init_macros"; - let macro_cache_enabled = !MacroContext.macro_enable_cache in - MacroContext.macro_enable_cache := true; com.stage <- CInitMacrosStart; ServerMessage.compiler_stage com; @@ -339,6 +337,8 @@ let compile ctx actx callbacks = (* Set up display configuration *) DisplayProcessing.process_display_configuration ctx; let display_file_dot_path = DisplayProcessing.process_display_file com actx in + let macro_cache_enabled = !MacroContext.macro_enable_cache in + MacroContext.macro_enable_cache := true; let mctx = match com.platform with | CustomTarget name -> begin try @@ -363,7 +363,7 @@ let compile ctx actx callbacks = if actx.cmds = [] && not actx.did_something then actx.raise_usage(); end else begin (* Actual compilation starts here *) - let (tctx,display_file_dot_path) = do_type ctx mctx actx display_file_dot_path in + let (tctx,display_file_dot_path) = do_type ctx mctx actx display_file_dot_path macro_cache_enabled in DisplayProcessing.handle_display_after_typing ctx tctx display_file_dot_path; finalize_typing ctx tctx; DisplayProcessing.handle_display_after_finalization ctx tctx display_file_dot_path; From de6493fa1388bd149739d1e6ed9cd40cb310b309 Mon Sep 17 00:00:00 2001 From: Rudy Ges Date: Fri, 6 Oct 2023 08:15:49 +0200 Subject: [PATCH 17/17] [tests] add test for custom target and macro com API limitations --- tests/misc/projects/Issue11128/InitMacro.hx | 25 +++++++++++++++++++ tests/misc/projects/Issue11128/Main2.hx | 1 + tests/misc/projects/Issue11128/compile5.hxml | 3 +++ tests/misc/projects/Issue11128/mylang/Init.hx | 6 +++++ 4 files changed, 35 insertions(+) create mode 100644 tests/misc/projects/Issue11128/InitMacro.hx create mode 100644 tests/misc/projects/Issue11128/Main2.hx create mode 100644 tests/misc/projects/Issue11128/compile5.hxml create mode 100644 tests/misc/projects/Issue11128/mylang/Init.hx diff --git a/tests/misc/projects/Issue11128/InitMacro.hx b/tests/misc/projects/Issue11128/InitMacro.hx new file mode 100644 index 00000000000..32fcea44bac --- /dev/null +++ b/tests/misc/projects/Issue11128/InitMacro.hx @@ -0,0 +1,25 @@ +import haxe.macro.Compiler; +import haxe.macro.Context; +import haxe.macro.Type; + +class InitMacro { + static function setup() { + switch (Compiler.getConfiguration().platform) { + case CustomTarget("mylang"): {} + case _: throw "this shouldnt happen."; + } + + Context.onAfterTyping(check); + } + + static function check(types:Array) { + for (m in types) { + switch (m) { + case TClassDecl(_.get() => c): + for (f in c.fields.get()) f.expr(); + + case _: + } + } + } +} diff --git a/tests/misc/projects/Issue11128/Main2.hx b/tests/misc/projects/Issue11128/Main2.hx new file mode 100644 index 00000000000..a71cf3b3e00 --- /dev/null +++ b/tests/misc/projects/Issue11128/Main2.hx @@ -0,0 +1 @@ +function main() {} diff --git a/tests/misc/projects/Issue11128/compile5.hxml b/tests/misc/projects/Issue11128/compile5.hxml new file mode 100644 index 00000000000..33c3a1a1ca8 --- /dev/null +++ b/tests/misc/projects/Issue11128/compile5.hxml @@ -0,0 +1,3 @@ +-main Main2 +--custom-target mylang=out +--macro InitMacro.setup() diff --git a/tests/misc/projects/Issue11128/mylang/Init.hx b/tests/misc/projects/Issue11128/mylang/Init.hx new file mode 100644 index 00000000000..7a4649b31e7 --- /dev/null +++ b/tests/misc/projects/Issue11128/mylang/Init.hx @@ -0,0 +1,6 @@ +package mylang; + +class Init { + public static function init() { + } +}