Skip to content

Commit

Permalink
mess around
Browse files Browse the repository at this point in the history
  • Loading branch information
Simn committed Nov 10, 2023
1 parent 90ed19f commit d3c0d0c
Show file tree
Hide file tree
Showing 17 changed files with 115 additions and 109 deletions.
2 changes: 1 addition & 1 deletion src/context/display/displayTexpr.ml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ let check_display_class ctx decls c =
ignore(Typeload.type_type_params ctx TPHType c.cl_path (fun() -> c.cl_params) null_pos sc.d_params);
List.iter (function
| (HExtends(ct,p) | HImplements(ct,p)) when display_position#enclosed_in p ->
ignore(Typeload.load_instance ~allow_display:true ctx (ct,p) false)
ignore(Typeload.load_instance ~allow_display:true ctx (ct,p) ParamNormal)
| _ ->
()
) sc.d_flags;
Expand Down
2 changes: 1 addition & 1 deletion src/context/typecore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ type delay = {

type build_kind =
| BuildNormal
| BuildGeneric
| BuildGeneric of tclass
| BuildGenericBuild
| BuildMacroType

Expand Down
10 changes: 5 additions & 5 deletions src/core/inheritDoc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ let rec get_class_field c field_name =
| None -> raise Not_found
| Some (csup, _) -> get_class_field csup field_name

let find_type ctx tp allow_no_params =
try Typeload.load_instance' ctx tp allow_no_params
let find_type ctx tp =
try Typeload.load_instance' ctx tp ParamSpawnMonos
with _ -> raise Not_found

(**
Expand Down Expand Up @@ -160,7 +160,7 @@ and get_target_doc ctx e_target =
| _ ->
mk_type_path path
in
let t = (find_type ctx (tp,snd e_target) true) in
let t = (find_type ctx (tp,snd e_target)) in
try
match follow t with
| TInst (c, _) ->
Expand Down Expand Up @@ -207,11 +207,11 @@ and get_target_doc ctx e_target =
in
let resolve_type () =
let tp = mk_type_path path, snd e_target in
resolve_type_t (find_type ctx tp true)
resolve_type_t (find_type ctx tp)
in
let resolve_sub_type sub =
let tp = mk_type_path ~sub path, snd e_target in
resolve_type_t (find_type ctx tp true)
resolve_type_t (find_type ctx tp)
in
try
match sub with
Expand Down
12 changes: 6 additions & 6 deletions src/filters/exceptions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -532,23 +532,23 @@ let filter tctx =
(mk_type_path (pack,name), null_pos)
in
let wildcard_catch_type =
let t = Typeload.load_instance tctx (tp config.ec_wildcard_catch) true in
let t = Typeload.load_instance tctx (tp config.ec_wildcard_catch) ParamSpawnMonos in
if is_dynamic t then t_dynamic
else t
and base_throw_type =
let t = Typeload.load_instance tctx (tp config.ec_base_throw) true in
let t = Typeload.load_instance tctx (tp config.ec_base_throw) ParamSpawnMonos in
if is_dynamic t then t_dynamic
else t
and haxe_exception_type, haxe_exception_class =
match Typeload.load_instance tctx (tp haxe_exception_type_path) true with
match Typeload.load_instance tctx (tp haxe_exception_type_path) ParamSpawnMonos with
| TInst(cls,_) as t -> t,cls
| _ -> raise_typing_error "haxe.Exception is expected to be a class" null_pos
and value_exception_type, value_exception_class =
match Typeload.load_instance tctx (tp value_exception_type_path) true with
match Typeload.load_instance tctx (tp value_exception_type_path) ParamSpawnMonos with
| TInst(cls,_) as t -> t,cls
| _ -> raise_typing_error "haxe.ValueException is expected to be a class" null_pos
and haxe_native_stack_trace =
match Typeload.load_instance tctx (tp (["haxe"],"NativeStackTrace")) true with
match Typeload.load_instance tctx (tp (["haxe"],"NativeStackTrace")) ParamSpawnMonos with
| TInst(cls,_) -> cls
| TAbstract({ a_impl = Some cls },_) -> cls
| _ -> raise_typing_error "haxe.NativeStackTrace is expected to be a class or an abstract" null_pos
Expand Down Expand Up @@ -665,7 +665,7 @@ let insert_save_stacks tctx =
*)
let patch_constructors tctx =
let tp = (mk_type_path haxe_exception_type_path, null_pos) in
match Typeload.load_instance tctx tp true with
match Typeload.load_instance tctx tp ParamSpawnMonos with
(* Add only if `__shiftStack` method exists *)
| TInst(cls,_) when PMap.mem "__shiftStack" cls.cl_fields ->
(fun mt ->
Expand Down
4 changes: 2 additions & 2 deletions src/typing/generic.ml
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ let static_method_container gctx c cf p =
let pack = fst c.cl_path in
let name = (snd c.cl_path) ^ "_" ^ cf.cf_name ^ "_" ^ gctx.name in
try
let t = Typeload.load_instance ctx (mk_type_path (pack,name),p) true in
let t = Typeload.load_instance ctx (mk_type_path (pack,name),p) ParamSpawnMonos in
match t with
| TInst(cg,_) -> cg
| _ -> raise_typing_error ("Cannot specialize @:generic static method because the generated type name is already used: " ^ name) p
Expand Down Expand Up @@ -236,7 +236,7 @@ let rec build_generic_class ctx c p tl =
let gctx = make_generic ctx c.cl_params tl p in
let name = (snd c.cl_path) ^ "_" ^ gctx.name in
try
let t = Typeload.load_instance ctx (mk_type_path (pack,name),p) false in
let t = Typeload.load_instance ctx (mk_type_path (pack,name),p) ParamNormal in
match t with
| TInst({ cl_kind = KGenericInstance (csup,_) },_) when c == csup -> t
| _ -> raise_typing_error ("Cannot specialize @:generic because the generated type name is already used: " ^ name) p
Expand Down
2 changes: 1 addition & 1 deletion src/typing/instanceBuilder.ml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ let get_build_info ctx mtype p =
in
let kind,f = match c.cl_kind with
| KGeneric ->
BuildGeneric,build (fun tl -> Generic.build_generic_class ctx c p tl) "build_generic"
(BuildGeneric c),build (fun tl -> Generic.build_generic_class ctx c p tl) "build_generic"
| KGenericBuild cfl ->
BuildGenericBuild,build (fun tl -> build_macro_build ctx c tl cfl p) "build_generic_build"
| KMacroType ->
Expand Down
12 changes: 6 additions & 6 deletions src/typing/macroContext.ml
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ let make_macro_api ctx mctx p =
mk_type_path path
in
try
let m = Some (Typeload.load_instance ctx (tp,p) true) in
let m = Some (Typeload.load_instance ctx (tp,p) ParamSpawnMonos) in
m
with Error { err_message = Module_not_found _; err_pos = p2 } when p == p2 ->
None
Expand Down Expand Up @@ -478,7 +478,7 @@ let make_macro_api ctx mctx p =
MacroApi.define_type = (fun v mdep ->
let cttype = mk_type_path ~sub:"TypeDefinition" (["haxe";"macro"],"Expr") in
let mctx = (match ctx.g.macros with None -> die "" __LOC__ | Some (_,mctx) -> mctx) in
let ttype = Typeload.load_instance mctx (cttype,p) false in
let ttype = Typeload.load_instance mctx (cttype,p) ParamNormal in
let f () = Interp.decode_type_def v in
let m, tdef, pos = safe_decode ctx.com v "TypeDefinition" ttype p f in
let has_native_meta = match tdef with
Expand Down Expand Up @@ -846,7 +846,7 @@ let type_macro ctx mode cpath f (el:Ast.expr list) p =
in
let mpos = mfield.cf_pos in
let ctexpr = mk_type_path (["haxe";"macro"],"Expr") in
let expr = Typeload.load_instance mctx (ctexpr,p) false in
let expr = Typeload.load_instance mctx (ctexpr,p) ParamNormal in
(match mode with
| MDisplay ->
raise Exit (* We don't have to actually call the macro. *)
Expand All @@ -855,18 +855,18 @@ let type_macro ctx mode cpath f (el:Ast.expr list) p =
| MBuild ->
let params = [TPType (CTPath (mk_type_path ~sub:"Field" (["haxe";"macro"],"Expr")),null_pos)] in
let ctfields = mk_type_path ~params ([],"Array") in
let tfields = Typeload.load_instance mctx (ctfields,p) false in
let tfields = Typeload.load_instance mctx (ctfields,p) ParamNormal in
unify mctx mret tfields mpos
| MMacroType ->
let cttype = mk_type_path (["haxe";"macro"],"Type") in
let ttype = Typeload.load_instance mctx (cttype,p) false in
let ttype = Typeload.load_instance mctx (cttype,p) ParamNormal in
try
unify_raise mret ttype mpos;
(* TODO: enable this again in the future *)
(* warning ctx WDeprecated "Returning Type from @:genericBuild macros is deprecated, consider returning ComplexType instead" p; *)
with Error { err_message = Unify _ } ->
let cttype = mk_type_path ~sub:"ComplexType" (["haxe";"macro"],"Expr") in
let ttype = Typeload.load_instance mctx (cttype,p) false in
let ttype = Typeload.load_instance mctx (cttype,p) ParamNormal in
unify_raise mret ttype mpos;
);
(*
Expand Down
2 changes: 1 addition & 1 deletion src/typing/matcher/exprToPattern.ml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ let get_general_module_type ctx mt p =
end
| _ -> raise_typing_error "Cannot use this type as a value" p
in
Typeload.load_instance ctx ({tname=loop mt;tpackage=[];tsub=None;tparams=[]},p) true
Typeload.load_instance ctx ({tname=loop mt;tpackage=[];tsub=None;tparams=[]},p) ParamSpawnMonos

let unify_type_pattern ctx mt t p =
let tcl = get_general_module_type ctx mt p in
Expand Down
2 changes: 1 addition & 1 deletion src/typing/operators.ml
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ let make_binop ctx op e1 e2 is_assign_op with_type p =
unify ctx e2.etype b p;
mk_op e1 e2 b
| OpInterval ->
let t = Typeload.load_instance ctx (mk_type_path (["std"],"IntIterator"),null_pos) false in
let t = Typeload.load_instance ctx (mk_type_path (["std"],"IntIterator"),null_pos) ParamNormal in
let e1 = AbstractCast.cast_or_unify_raise ctx tint e1 e1.epos in
let e2 = AbstractCast.cast_or_unify_raise ctx tint e2 e2.epos in
BinopSpecial (mk (TNew ((match t with TInst (c,[]) -> c | _ -> die "" __LOC__),[],[e1;e2])) t p,false)
Expand Down
43 changes: 28 additions & 15 deletions src/typing/typeload.ml
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,12 @@ let check_param_constraints ctx t map c p =

) ctl

let rec load_and_apply_params ctx info params allow_no_params p =
type load_instance_param_mode =
| ParamNormal
| ParamSpawnMonos
| ParamCustom of (build_info -> Type.t list)

let rec load_and_apply_params ctx info params p =
let is_rest = info.build_kind = BuildGenericBuild && (match info.build_params with [{ttp_name="Rest"}] -> true | _ -> false) in
let is_java_rest = ctx.com.platform = Java && info.build_extern in
let is_rest = is_rest || is_java_rest in
Expand Down Expand Up @@ -346,7 +351,7 @@ let rec load_and_apply_params ctx info params allow_no_params p =
in
let is_rest = is_rest || name = "Rest" && info.build_kind = BuildGenericBuild in
let t = match follow t2 with
| TInst ({ cl_kind = KTypeParameter [] } as c, []) when info.build_kind <> BuildGeneric ->
| TInst ({ cl_kind = KTypeParameter [] } as c, []) when (match info.build_kind with BuildGeneric _ -> false | _ -> true) ->
check_const c;
t
| TInst (c,[]) ->
Expand Down Expand Up @@ -400,10 +405,10 @@ let rec load_and_apply_params ctx info params allow_no_params p =
) checks
);
end;
info.build_apply params
params

(* build an instance from a full type *)
and load_instance' ctx (t,p) allow_no_params =
and load_instance' ctx (t,p) get_params =
try
if t.tpackage <> [] || t.tsub <> None then raise Not_found;
let pt = lookup_param t.tname ctx.type_params in
Expand All @@ -414,19 +419,27 @@ and load_instance' ctx (t,p) allow_no_params =
let info = ctx.g.get_build_info ctx mt p in
(* TODO: this is currently duplicated, but it seems suspcious anyway... *)
let is_rest = info.build_kind = BuildGenericBuild && (match info.build_params with [{ttp_name="Rest"}] -> true | _ -> false) in
if allow_no_params && t.tparams = [] && not is_rest then begin
let monos = Monomorph.spawn_constrained_monos (fun t -> t) info.build_params in
info.build_apply (monos)
end else if info.build_path = ([],"Dynamic") then match t.tparams with
if info.build_path = ([],"Dynamic") then match t.tparams with
| [] -> t_dynamic
| [TPType t] -> TDynamic (Some (load_complex_type ctx true t))
| _ -> raise_typing_error "Too many parameters for Dynamic" p
else
load_and_apply_params ctx info t.tparams allow_no_params p
else begin
let tl = if t.tparams = [] && not is_rest then begin match get_params with
| ParamNormal ->
load_and_apply_params ctx info t.tparams p
| ParamSpawnMonos ->
Monomorph.spawn_constrained_monos (fun t -> t) info.build_params
| ParamCustom f ->
f info
end else
load_and_apply_params ctx info t.tparams p
in
info.build_apply tl
end

and load_instance ctx ?(allow_display=false) ((_,pn) as tp) allow_no_params =
and load_instance ctx ?(allow_display=false) ((_,pn) as tp) get_params =
try
let t = load_instance' ctx tp allow_no_params in
let t = load_instance' ctx tp get_params in
if allow_display then DisplayEmitter.check_display_type ctx t tp;
t
with Error { err_message = Module_not_found path } when ctx.macro_depth <= 0 && (ctx.com.display.dms_kind = DMDefault) && DisplayPosition.display_position#enclosed_in pn ->
Expand All @@ -440,7 +453,7 @@ and load_complex_type' ctx allow_display (t,p) =
match t with
| CTParent t -> load_complex_type ctx allow_display t
| CTPath { tpackage = ["$"]; tname = "_hx_mono" } -> spawn_monomorph ctx p
| CTPath t -> load_instance ~allow_display ctx (t,p) false
| CTPath t -> load_instance ~allow_display ctx (t,p) ParamNormal
| CTOptional _ -> raise_typing_error "Optional type not allowed here" p
| CTNamed _ -> raise_typing_error "Named type not allowed here" p
| CTIntersection tl ->
Expand Down Expand Up @@ -488,7 +501,7 @@ and load_complex_type' ctx allow_display (t,p) =
in
let il = List.map (fun (t,pn) ->
try
(load_instance ctx ~allow_display (t,pn) false,pn)
(load_instance ctx ~allow_display (t,pn) ParamNormal,pn)
with DisplayException(DisplayFields ({fkind = CRTypeHint} as r)) ->
let l = List.filter (fun item -> match item.ci_kind with
| ITType({kind = Struct},_) -> true
Expand Down Expand Up @@ -824,7 +837,7 @@ let load_core_class ctx c =
| KAbstractImpl a -> mk_type_path a.a_path
| _ -> mk_type_path c.cl_path
in
let t = load_instance ctx2 (tpath,c.cl_pos) true in
let t = load_instance ctx2 (tpath,c.cl_pos) ParamSpawnMonos in
flush_pass ctx2 PFinal "core_final";
match t with
| TInst (ccore,_) | TAbstract({a_impl = Some ccore}, _) ->
Expand Down
2 changes: 1 addition & 1 deletion src/typing/typeloadCheck.ml
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ module Inheritance = struct
let fl = ExtList.List.filter_map (fun (is_extends,(ct,p)) ->
try
let t = try
Typeload.load_instance ~allow_display:true ctx (ct,p) false
Typeload.load_instance ~allow_display:true ctx (ct,p) ParamNormal
with DisplayException(DisplayFields ({fkind = CRTypeHint} as r)) ->
(* We don't allow `implements` on interfaces. Just raise fields completion with no fields. *)
if not is_extends && (has_class_flag c CInterface) then raise_fields [] CRImplements r.fsubject;
Expand Down
2 changes: 1 addition & 1 deletion src/typing/typeloadFields.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1343,7 +1343,7 @@ let create_method (ctx,cctx,fctx) c f fd p =
| None -> ()
| Some (CTPath ({ tpackage = []; tname = "Void" } as tp),p) ->
if ctx.is_display_file && DisplayPosition.display_position#enclosed_in p then
ignore(load_instance ~allow_display:true ctx (tp,p) false);
ignore(load_instance ~allow_display:true ctx (tp,p) ParamNormal);
| _ -> raise_typing_error "A class constructor can't have a return type" p;
end
| false,_ ->
Expand Down
2 changes: 1 addition & 1 deletion src/typing/typeloadModule.ml
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ let type_types_into_module ctx m tdecls p =
if ctx.g.std != null_module then begin
add_dependency m ctx.g.std;
(* this will ensure both String and (indirectly) Array which are basic types which might be referenced *)
ignore(load_instance ctx (mk_type_path (["std"],"String"),null_pos) false)
ignore(load_instance ctx (mk_type_path (["std"],"String"),null_pos) ParamNormal)
end;
ModuleLevel.init_type_params ctx decls;
(* setup module types *)
Expand Down
Loading

0 comments on commit d3c0d0c

Please sign in to comment.