Skip to content

Commit

Permalink
Split up typer context (#11534)
Browse files Browse the repository at this point in the history
* add ctx.c

* create class context earlier to avoid some heritage awkwardness

* add ctx.e

* start on ctx.f

* curfield

* vthis

* untyped

* in_loop

* bypass_accessor & meta

* with_type_stack & call_argument_stack

* in_call_args & in_overload_call_args

* in_display

* is_display_file

* in_call_args needs to be on ctx.f

also do some cleanup

* why did I change that

* macro_depth

* delayed_display

* allow_inline & allow_tranform

* rename to ctx_c to make difference visible

* more renaming, less ctx

because that will totally fix something

* I'm committed now

* make some context cloning explicit

* clone for enum fields too

and random cleanup

* Revert "allow_inline & allow_tranform"

This reverts commit f35e83c.

---------

Co-authored-by: Rudy Ges <[email protected]>
  • Loading branch information
Simn and kLabz authored Feb 2, 2024
1 parent 8c5e022 commit 2b0e8ce
Show file tree
Hide file tree
Showing 37 changed files with 659 additions and 637 deletions.
8 changes: 4 additions & 4 deletions src/context/abstractCast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ let rec make_static_call ctx c cf a pl args t p =
match args with
| [e] ->
let e,f = push_this ctx e in
ctx.with_type_stack <- (WithType.with_type t) :: ctx.with_type_stack;
ctx.e.with_type_stack <- (WithType.with_type t) :: ctx.e.with_type_stack;
let e = match ctx.g.do_macro ctx MExpr c.cl_path cf.cf_name [e] p with
| MSuccess e -> type_expr ctx e (WithType.with_type t)
| _ -> type_expr ctx (EConst (Ident "null"),p) WithType.value
in
ctx.with_type_stack <- List.tl ctx.with_type_stack;
ctx.e.with_type_stack <- List.tl ctx.e.with_type_stack;
let e = try cast_or_unify_raise ctx t e p with Error { err_message = Unify _ } -> raise Not_found in
f();
e
Expand All @@ -40,7 +40,7 @@ and do_check_cast ctx uctx tleft eright p =
raise_error_msg (Unify l) eright.epos)
| _ -> ()
end;
if cf == ctx.curfield || rec_stack_memq cf cast_stack then raise_typing_error "Recursive implicit cast" p;
if cf == ctx.f.curfield || rec_stack_memq cf cast_stack then raise_typing_error "Recursive implicit cast" p;
rec_stack_loop cast_stack cf f ()
in
let make (a,tl,(tcf,cf)) =
Expand Down Expand Up @@ -118,7 +118,7 @@ and cast_or_unify ctx tleft eright p =
eright

let prepare_array_access_field ctx a pl cf p =
let monos = List.map (fun _ -> spawn_monomorph ctx p) cf.cf_params in
let monos = List.map (fun _ -> spawn_monomorph ctx.e p) cf.cf_params in
let map t = apply_params a.a_params pl (apply_params cf.cf_params monos t) in
let check_constraints () =
List.iter2 (fun m ttp -> match get_constraints ttp with
Expand Down
2 changes: 1 addition & 1 deletion src/context/display/displayEmitter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ let check_display_type ctx t ptp =
ctx.g.type_hints <- (ctx.m.curmod.m_extra.m_display,ptp.pos_full,t) :: ctx.g.type_hints;
in
let maybe_display_type () =
if ctx.is_display_file && display_position#enclosed_in ptp.pos_full then
if ctx.m.is_display_file && display_position#enclosed_in ptp.pos_full then
display_type ctx t ptp.pos_path
in
add_type_hint();
Expand Down
10 changes: 5 additions & 5 deletions src/context/display/displayFields.ml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ let collect_static_extensions ctx items e p =
let rec dup t = Type.map dup t in
let handle_field c f acc =
let f = { f with cf_type = opt_type f.cf_type } in
let monos = List.map (fun _ -> spawn_monomorph ctx p) f.cf_params in
let monos = List.map (fun _ -> spawn_monomorph ctx.e p) f.cf_params in
let map = apply_params f.cf_params monos in
match follow (map f.cf_type) with
| TFun((_,_,TType({t_path=["haxe";"macro"], "ExprOf"}, [t])) :: args, ret)
Expand Down Expand Up @@ -112,7 +112,7 @@ let collect ctx e_ast e dk with_type p =
let opt_args args ret = TFun(List.map(fun (n,o,t) -> n,true,t) args,ret) in
let should_access c cf stat =
if Meta.has Meta.NoCompletion cf.cf_meta then false
else if c != ctx.curclass && not (has_class_field_flag cf CfPublic) && String.length cf.cf_name > 4 then begin match String.sub cf.cf_name 0 4 with
else if c != ctx.c.curclass && not (has_class_field_flag cf CfPublic) && String.length cf.cf_name > 4 then begin match String.sub cf.cf_name 0 4 with
| "get_" | "set_" -> false
| _ -> can_access ctx c cf stat
end else
Expand Down Expand Up @@ -402,17 +402,17 @@ let handle_missing_field_raise ctx tthis i mode with_type pfield =
display.module_diagnostics <- MissingFields diag :: display.module_diagnostics

let handle_missing_ident ctx i mode with_type p =
match ctx.curfun with
match ctx.e.curfun with
| FunStatic ->
let e_self = Texpr.Builder.make_static_this ctx.curclass p in
let e_self = Texpr.Builder.make_static_this ctx.c.curclass p in
begin try
handle_missing_field_raise ctx e_self.etype i mode with_type p
with Exit ->
()
end
| _ ->
begin try
handle_missing_field_raise ctx ctx.tthis i mode with_type p
handle_missing_field_raise ctx ctx.c.tthis i mode with_type p
with Exit ->
()
end
1 change: 0 additions & 1 deletion src/context/display/displayPath.ml
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ let resolve_position_by_path ctx path p =
let p = (t_infos mt).mt_pos in
raise_positions [p]


let handle_path_display ctx path p =
let class_field c name =
ignore(c.cl_build());
Expand Down
4 changes: 2 additions & 2 deletions src/context/display/displayTexpr.ml
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ let check_display_module ctx decls m =
| (EImport _ | EUsing _),_ -> true
| _ -> false
) decls in
let imports = TypeloadModule.ModuleLevel.handle_import_hx ctx m imports null_pos in
let ctx = TypeloadModule.type_types_into_module ctx m imports null_pos in
let imports = TypeloadModule.ModuleLevel.handle_import_hx ctx.com ctx.g m imports null_pos in
let ctx = TypeloadModule.type_types_into_module ctx.com ctx.g m imports null_pos in
List.iter (fun md ->
let infos = t_infos md in
if display_position#enclosed_in infos.mt_name_pos then
Expand Down
26 changes: 13 additions & 13 deletions src/context/display/displayToplevel.ml
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ let is_pack_visible pack =
let collect ctx tk with_type sort =
let t = Timer.timer ["display";"toplevel collect"] in
let cctx = CollectionContext.create ctx in
let curpack = fst ctx.curclass.cl_path in
let curpack = fst ctx.c.curclass.cl_path in
(* Note: This checks for the explicit `ServerConfig.legacy_completion` setting instead of using
`is_legacy_completion com` because the latter is always false for the old protocol, yet we have
tests which assume advanced completion even in the old protocol. This means that we can only
Expand Down Expand Up @@ -302,7 +302,7 @@ let collect ctx tk with_type sort =
PMap.iter (fun _ v ->
if not (is_gen_local v) then
add (make_ci_local v (tpair ~values:(get_value_meta v.v_meta) v.v_type)) (Some v.v_name)
) ctx.locals;
) ctx.f.locals;
t();

let add_field scope origin cf =
Expand Down Expand Up @@ -331,22 +331,22 @@ let collect ctx tk with_type sort =

let t = Timer.timer ["display";"toplevel collect";"fields"] in
(* member fields *)
if ctx.curfun <> FunStatic then begin
let all_fields = Type.TClass.get_all_fields ctx.curclass (extract_param_types ctx.curclass.cl_params) in
if ctx.e.curfun <> FunStatic then begin
let all_fields = Type.TClass.get_all_fields ctx.c.curclass (extract_param_types ctx.c.curclass.cl_params) in
PMap.iter (fun _ (c,cf) ->
let origin = if c == ctx.curclass then Self (TClassDecl c) else Parent (TClassDecl c) in
let origin = if c == ctx.c.curclass then Self (TClassDecl c) else Parent (TClassDecl c) in
maybe_add_field CFSMember origin cf
) all_fields;
(* TODO: local using? *)
end;

(* statics *)
begin match ctx.curclass.cl_kind with
begin match ctx.c.curclass.cl_kind with
| KAbstractImpl ({a_impl = Some c} as a) ->
let origin = Self (TAbstractDecl a) in
List.iter (fun cf ->
if has_class_field_flag cf CfImpl then begin
if ctx.curfun = FunStatic then ()
if ctx.e.curfun = FunStatic then ()
else begin
let cf = prepare_using_field cf in
maybe_add_field CFSMember origin cf
Expand All @@ -355,15 +355,15 @@ let collect ctx tk with_type sort =
maybe_add_field CFSStatic origin cf
) c.cl_ordered_statics
| _ ->
List.iter (maybe_add_field CFSStatic (Self (TClassDecl ctx.curclass))) ctx.curclass.cl_ordered_statics
List.iter (maybe_add_field CFSStatic (Self (TClassDecl ctx.c.curclass))) ctx.c.curclass.cl_ordered_statics
end;
t();

let t = Timer.timer ["display";"toplevel collect";"enum ctors"] in
(* enum constructors *)
let rec enum_ctors t =
match t with
| TAbstractDecl ({a_impl = Some c} as a) when a.a_enum && not (path_exists cctx a.a_path) && ctx.curclass != c ->
| TAbstractDecl ({a_impl = Some c} as a) when a.a_enum && not (path_exists cctx a.a_path) && ctx.c.curclass != c ->
add_path cctx a.a_path;
List.iter (fun cf ->
let ccf = CompletionClassField.make cf CFSMember (Self (decl_of_class c)) true in
Expand Down Expand Up @@ -433,16 +433,16 @@ let collect ctx tk with_type sort =
add (make_ci_literal "null" (tpair t_dynamic)) (Some "null");
add (make_ci_literal "true" (tpair ctx.com.basic.tbool)) (Some "true");
add (make_ci_literal "false" (tpair ctx.com.basic.tbool)) (Some "false");
begin match ctx.curfun with
begin match ctx.e.curfun with
| FunMember | FunConstructor | FunMemberClassLocal ->
let t = TInst(ctx.curclass,extract_param_types ctx.curclass.cl_params) in
let t = TInst(ctx.c.curclass,extract_param_types ctx.c.curclass.cl_params) in
add (make_ci_literal "this" (tpair t)) (Some "this");
begin match ctx.curclass.cl_super with
begin match ctx.c.curclass.cl_super with
| Some(c,tl) -> add (make_ci_literal "super" (tpair (TInst(c,tl)))) (Some "super")
| None -> ()
end
| FunMemberAbstract ->
let t = TInst(ctx.curclass,extract_param_types ctx.curclass.cl_params) in
let t = TInst(ctx.c.curclass,extract_param_types ctx.c.curclass.cl_params) in
add (make_ci_literal "abstract" (tpair t)) (Some "abstract");
| _ ->
()
Expand Down
2 changes: 1 addition & 1 deletion src/context/display/importHandling.ml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ let init_import ctx path mode p =
let check_alias mt name pname =
if not (name.[0] >= 'A' && name.[0] <= 'Z') then
raise_typing_error "Type aliases must start with an uppercase letter" pname;
if ctx.is_display_file && DisplayPosition.display_position#enclosed_in pname then
if ctx.m.is_display_file && DisplayPosition.display_position#enclosed_in pname then
DisplayEmitter.display_alias ctx name (type_of_module_type mt) pname;
in
let add_static_init t name s =
Expand Down
Loading

0 comments on commit 2b0e8ce

Please sign in to comment.