Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove some global state #11483

Merged
merged 6 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/compiler/args.ml
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ let parse_args com =
| None -> com.main_class <- Some cpath);
actx.classes <- cpath :: actx.classes;
Common.define com Define.Interp;
set_platform com (!Globals.macro_platform) "";
set_platform com Eval "";
actx.interp <- true;
),"<class>","interpret the program using internal macro system");
("Target",["--interp"],[], Arg.Unit (fun() ->
Common.define com Define.Interp;
set_platform com (!Globals.macro_platform) "";
set_platform com Eval "";
actx.interp <- true;
),"","interpret the program using internal macro system");
("Target",["--run"],[], Arg.Unit (fun() ->
Expand Down
7 changes: 2 additions & 5 deletions src/compiler/compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ let check_defines com =
end

(** Creates the typer context and types [classes] into it. *)
let do_type ctx mctx actx display_file_dot_path macro_cache_enabled =
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
Expand All @@ -286,7 +286,6 @@ let do_type ctx mctx actx display_file_dot_path macro_cache_enabled =
) mctx (List.rev actx.config_macros) in
enter_stage com 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
Setup.init_native_libs com actx.native_libs;
Expand Down Expand Up @@ -338,8 +337,6 @@ 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
Expand All @@ -364,7 +361,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 macro_cache_enabled in
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;
if is_diagnostics com then
Expand Down
1 change: 0 additions & 1 deletion src/compiler/server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,6 @@ let do_connect ip port args =

let enable_cache_mode sctx =
TypeloadModule.type_module_hook := type_module sctx;
MacroContext.macro_enable_cache := true;
ServerCompilationContext.ensure_macro_setup sctx;
TypeloadParse.parse_hook := parse_file sctx.cs

Expand Down
1 change: 0 additions & 1 deletion src/compiler/serverCompilationContext.ml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ let reset sctx =
Hashtbl.clear sctx.changed_directories;
sctx.was_compilation <- false;
Parser.reset_state();
return_partial_type := false;
measure_times := false;
Hashtbl.clear DeprecationCheck.warned_positions;
close_times();
Expand Down
2 changes: 1 addition & 1 deletion src/context/common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1288,7 +1288,7 @@ let adapt_defines_to_macro_context defines =
defines_signature = None
} in
Define.define macro_defines Define.Macro;
Define.raw_define macro_defines (platform_name !Globals.macro_platform);
Define.raw_define macro_defines (platform_name Eval);
macro_defines

let adapt_defines_to_display_context defines =
Expand Down
14 changes: 7 additions & 7 deletions src/context/display/displayEmitter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ let rec display_type ctx t p =
try
display_module_type ctx (module_type_of_type t) p
with Exit ->
match follow t,follow !t_dynamic_def with
match follow t,follow ctx.g.t_dynamic_def with
| _,TDynamic _ -> () (* sanity check in case it's still t_dynamic *)
| TDynamic _,_ -> display_type ctx !t_dynamic_def p
| TDynamic _,_ -> display_type ctx ctx.g.t_dynamic_def p
| _ ->
match dm.dms_kind with
| DMHover ->
Expand All @@ -77,14 +77,14 @@ let check_display_type ctx t ptp =
add_type_hint();
maybe_display_type()

let raise_position_of_type t =
let raise_position_of_type ctx t =
let mt =
let rec follow_null t =
match t with
| TMono r -> (match r.tm_type with None -> raise_positions [null_pos] | Some t -> follow_null t)
| TLazy f -> follow_null (lazy_type f)
| TAbstract({a_path = [],"Null"},[t]) -> follow_null t
| TDynamic _ -> !t_dynamic_def
| TDynamic _ -> ctx.g.t_dynamic_def
| _ -> t
in
try
Expand All @@ -96,7 +96,7 @@ let raise_position_of_type t =

let display_variable ctx v p = match ctx.com.display.dms_kind with
| DMDefinition -> raise_positions [v.v_pos]
| DMTypeDefinition -> raise_position_of_type v.v_type
| DMTypeDefinition -> raise_position_of_type ctx v.v_type
| DMUsage _ -> ReferencePosition.set (v.v_name,v.v_pos,SKVariable v)
| DMHover ->
let ct = CompletionType.from_type (get_import_status ctx) ~values:(get_value_meta v.v_meta) v.v_type in
Expand All @@ -105,7 +105,7 @@ let display_variable ctx v p = match ctx.com.display.dms_kind with

let display_field ctx origin scope cf p = match ctx.com.display.dms_kind with
| DMDefinition -> raise_positions [cf.cf_name_pos]
| DMTypeDefinition -> raise_position_of_type cf.cf_type
| DMTypeDefinition -> raise_position_of_type ctx cf.cf_type
| DMUsage _ | DMImplementation ->
let name,kind = match cf.cf_name,origin with
| "new",(Self (TClassDecl c) | Parent(TClassDecl c)) ->
Expand Down Expand Up @@ -136,7 +136,7 @@ let maybe_display_field ctx origin scope cf p =

let display_enum_field ctx en ef p = match ctx.com.display.dms_kind with
| DMDefinition -> raise_positions [ef.ef_name_pos]
| DMTypeDefinition -> raise_position_of_type ef.ef_type
| DMTypeDefinition -> raise_position_of_type ctx ef.ef_type
| DMUsage _ -> ReferencePosition.set (ef.ef_name,ef.ef_name_pos,SKEnumField ef)
| DMHover ->
let ct = CompletionType.from_type (get_import_status ctx) ef.ef_type in
Expand Down
4 changes: 2 additions & 2 deletions src/context/display/displayFields.ml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ let collect_static_extensions ctx items e p =
let opt_type t =
match t with
| TLazy f ->
return_partial_type := true;
ctx.g.return_partial_type <- true;
let t = lazy_type f in
return_partial_type := false;
ctx.g.return_partial_type <- false;
t
| _ ->
t
Expand Down
5 changes: 3 additions & 2 deletions src/context/typecore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ type typer_globals = {
mutable type_hints : (module_def_display * pos * t) list;
mutable load_only_cached_modules : bool;
functional_interface_lut : (path,tclass_field) lookup;
mutable return_partial_type : bool;
mutable build_count : int;
mutable t_dynamic_def : Type.t;
(* api *)
do_macro : typer -> macro_mode -> path -> string -> expr list -> pos -> macro_result;
do_load_macro : typer -> bool -> path -> string -> pos -> ((string * bool * t) list * t * tclass * Type.tclass_field);
Expand Down Expand Up @@ -216,8 +219,6 @@ exception WithTypeError of error

let memory_marker = [|Unix.time()|]

let locate_macro_error = ref true

let make_call_ref : (typer -> texpr -> texpr list -> t -> ?force_inline:bool -> pos -> texpr) ref = ref (fun _ _ _ _ ?force_inline:bool _ -> die "" __LOC__)
let type_expr_ref : (?mode:access_mode -> typer -> expr -> WithType.t -> texpr) ref = ref (fun ?(mode=MGet) _ _ _ -> die "" __LOC__)
let type_block_ref : (typer -> expr list -> WithType.t -> pos -> texpr) ref = ref (fun _ _ _ _ -> die "" __LOC__)
Expand Down
4 changes: 0 additions & 4 deletions src/core/globals.ml
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ let trace_call_stack ?(n:int = 5) () =
Option.may (fun loc -> print_endline (Printf.sprintf " called from %s" (loc_to_string loc))) loc;
done

let macro_platform = ref Neko

let return_partial_type = ref false

let is_windows = Sys.os_type = "Win32" || Sys.os_type = "Cygwin"

let max_custom_target_len = 16
Expand Down
2 changes: 0 additions & 2 deletions src/core/json/genjson.ml
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,7 @@ let rec generate_type ctx t =
| Some t -> loop t
end
| TLazy f ->
(* return_partial_type := true; *)
let t = lazy_type f in
(* return_partial_type := false; *)
loop t
| TDynamic None -> "TDynamic", Some jnull
| TDynamic (Some t) -> "TDynamic",Some (generate_type ctx t)
Expand Down
6 changes: 0 additions & 6 deletions src/core/tFunctions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,6 @@ let mk_anon ?fields status =
let fields = match fields with Some fields -> fields | None -> PMap.empty in
TAnon { a_fields = fields; a_status = status; }

(* We use this for display purposes because otherwise we never see the Dynamic type that
is defined in StdTypes.hx. This is set each time a typer is created, but this is fine
because Dynamic is the same in all contexts. If this ever changes we'll have to review
how we handle this. *)
let t_dynamic_def = ref t_dynamic

let tfun pl r = TFun (List.map (fun t -> "",false,t) pl,r)

let fun_args l = List.map (fun (a,c,t) -> a, c <> None, t) l
Expand Down
3 changes: 1 addition & 2 deletions src/macro/eval/evalMain.ml
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,7 @@ let setup get_api =
let api = get_api (fun() -> (get_ctx()).curapi.get_com()) (fun() -> (get_ctx()).curapi) in
List.iter (fun (n,v) ->
Hashtbl.replace GlobalState.macro_lib n v
) api;
Globals.macro_platform := Globals.Eval
) api

let do_reuse ctx api =
ctx.curapi <- api;
Expand Down
1 change: 0 additions & 1 deletion src/macro/macroApi.ml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ type 'value compiler_api = {
define_module : string -> 'value list -> ((string * Globals.pos) list * Ast.import_mode) list -> Ast.type_path list -> unit;
module_dependency : string -> string -> unit;
current_module : unit -> module_def;
use_cache : unit -> bool;
format_string : string -> Globals.pos -> Ast.expr;
cast_or_unify : Type.t -> texpr -> Globals.pos -> bool;
add_global_metadata : string -> string -> (bool * bool * bool) -> pos -> unit;
Expand Down
2 changes: 0 additions & 2 deletions src/typing/callUnification.ml
Original file line number Diff line number Diff line change
Expand Up @@ -513,9 +513,7 @@ object(self)
let ep = err.err_pos in
(* display additional info in the case the error is not part of our original call *)
if ep.pfile <> p.pfile || ep.pmax < p.pmin || ep.pmin > p.pmax then begin
locate_macro_error := false;
old (if (ep = null_pos) then { err with err_pos = p } else err);
locate_macro_error := true;
(* TODO add as sub for above error *)
if ep <> null_pos then old (make_error ~depth:(err.err_depth+1) (Custom (compl_msg "Called from macro here")) p);
end else
Expand Down
12 changes: 3 additions & 9 deletions src/typing/macroContext.ml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ module Interp = struct
include BuiltApi
end

let macro_enable_cache = ref false
let macro_interp_cache = ref None

let safe_decode com v expected t p f =
Expand Down Expand Up @@ -270,9 +269,6 @@ let make_macro_com_api com mcom p =
current_module = (fun() ->
null_module
);
use_cache = (fun() ->
!macro_enable_cache
);
format_string = (fun s p ->
FormatString.format_string com.defines s p (fun e p -> (e,p))
);
Expand Down Expand Up @@ -610,9 +606,7 @@ let init_macro_interp mctx mint =
ignore(TypeloadModule.load_module mctx (["haxe";"macro"],"Expr") p);
ignore(TypeloadModule.load_module mctx (["haxe";"macro"],"Type") p);
Interp.init mint;
if !macro_enable_cache && not (Common.defined mctx.com Define.NoMacroCache) then begin
macro_interp_cache := Some mint;
end
macro_interp_cache := Some mint

and flush_macro_context mint mctx =
let t = macro_timer mctx.com ["flush"] in
Expand Down Expand Up @@ -712,12 +706,12 @@ let create_macro_context com =
(* Inherit most display settings, but require normal typing. *)
com2.display <- {com.display with dms_kind = DMNone; dms_full_typing = true; dms_force_macro_typing = true; dms_inline = true; };
com2.class_path <- List.filter (fun s -> not (ExtString.String.exists s "/_std/")) com2.class_path;
let name = platform_name !Globals.macro_platform in
let name = platform_name Eval in
com2.class_path <- List.map (fun p -> p ^ name ^ "/_std/") com2.std_path @ com2.class_path;
let defines = adapt_defines_to_macro_context com2.defines; in
com2.defines.values <- defines.values;
com2.defines.defines_signature <- None;
com2.platform <- !Globals.macro_platform;
com2.platform <- Eval;
Common.init_platform com2;
let mctx = !create_context_ref com2 None in
mctx.is_display_file <- false;
Expand Down
2 changes: 0 additions & 2 deletions src/typing/typeload.ml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ open Typecore
open Error
open Globals

let build_count = ref 0

let type_function_params_ref = ref (fun _ _ _ _ _ -> die "" __LOC__)

let check_field_access ctx cff =
Expand Down
5 changes: 2 additions & 3 deletions src/typing/typeloadFields.ml
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,6 @@ let create_class_context c p =
cctx

let create_typer_context_for_class ctx cctx p =
locate_macro_error := true;
incr stats.s_classes_built;
let c = cctx.tclass in
if cctx.is_lib && not (has_class_flag c CExtern) then ctx.com.error "@:libType can only be used in extern classes" c.cl_pos;
Expand Down Expand Up @@ -858,7 +857,7 @@ module TypeBinding = struct
in
let r = make_lazy ~force:false ctx t (fun r ->
(* type constant init fields (issue #1956) *)
if not !return_partial_type || (match fst e with EConst _ -> true | _ -> false) then begin
if not ctx.g.return_partial_type || (match fst e with EConst _ -> true | _ -> false) then begin
enter_field_typing_pass ctx ("bind_var_expression",fst ctx.curclass.cl_path @ [snd ctx.curclass.cl_path;ctx.curfield.cf_name]);
if (Meta.has (Meta.Custom ":debug.typing") (c.cl_meta @ cf.cf_meta)) then ctx.com.print (Printf.sprintf "Typing field %s.%s\n" (s_type_path c.cl_path) cf.cf_name);
let e = type_var_field ctx t e fctx.is_static fctx.is_display_field p in
Expand Down Expand Up @@ -987,7 +986,7 @@ module TypeBinding = struct
end;
in
let maybe_bind r =
if not !return_partial_type then bind r;
if not ctx.g.return_partial_type then bind r;
t
in
let r = make_lazy ~force:false ctx t maybe_bind "type_fun" in
Expand Down
8 changes: 4 additions & 4 deletions src/typing/typeloadModule.ml
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ module TypeLevel = struct
add_class_flag c CFinal;
end
) d.d_meta;
let prev_build_count = ref (!build_count - 1) in
let prev_build_count = ref (ctx.g.build_count - 1) in
let build() =
c.cl_build <- (fun()-> Building [c]);
let fl = TypeloadCheck.Inheritance.set_heritance ctx c herits p in
Expand All @@ -404,7 +404,7 @@ module TypeLevel = struct
List.iter (fun f -> f()) fl;
TypeloadFields.init_class ctx c p d.d_flags d.d_data;
c.cl_build <- (fun()-> Built);
incr build_count;
ctx.g.build_count <- ctx.g.build_count + 1;
List.iter (fun tp -> ignore(follow tp.ttp_type)) c.cl_params;
Built;
with TypeloadCheck.Build_canceled state ->
Expand All @@ -415,8 +415,8 @@ module TypeLevel = struct
(match state with
| Built -> die "" __LOC__
| Building cl ->
if !build_count = !prev_build_count then raise_typing_error ("Loop in class building prevent compiler termination (" ^ String.concat "," (List.map (fun c -> s_type_path c.cl_path) cl) ^ ")") c.cl_pos;
prev_build_count := !build_count;
if ctx.g.build_count = !prev_build_count then raise_typing_error ("Loop in class building prevent compiler termination (" ^ String.concat "," (List.map (fun c -> s_type_path c.cl_path) cl) ^ ")") c.cl_pos;
prev_build_count := ctx.g.build_count;
rebuild();
Building (c :: cl)
| BuildMacro f ->
Expand Down
2 changes: 1 addition & 1 deletion src/typing/typerDisplay.ml
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ and display_expr ctx e_ast e dk mode with_type p =
let pl = loop e in
raise_positions pl
| DMTypeDefinition ->
raise_position_of_type e.etype
raise_position_of_type ctx e.etype
| DMDefault when not (!Parser.had_resume)->
let display_fields e_ast e1 so =
let l = match so with None -> 0 | Some s -> String.length s in
Expand Down
5 changes: 4 additions & 1 deletion src/typing/typerEntry.ml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ let create com macros =
complete = false;
type_hints = [];
load_only_cached_modules = false;
return_partial_type = false;
build_count = 0;
t_dynamic_def = t_dynamic;
functional_interface_lut = new Lookup.pmap_lookup;
do_macro = MacroContext.type_macro;
do_load_macro = MacroContext.load_macro';
Expand Down Expand Up @@ -107,7 +110,7 @@ let create com macros =
Type.unify t ctx.t.tbool;
ctx.t.tbool <- t
| "Dynamic" ->
t_dynamic_def := TAbstract(a,extract_param_types a.a_params);
ctx.g.t_dynamic_def <- TAbstract(a,extract_param_types a.a_params);
| "Null" ->
let mk_null t =
try
Expand Down