Skip to content

Commit

Permalink
add ctx.c
Browse files Browse the repository at this point in the history
  • Loading branch information
Simn committed Jan 31, 2024
1 parent 706f607 commit 98c9171
Show file tree
Hide file tree
Showing 21 changed files with 145 additions and 113 deletions.
6 changes: 3 additions & 3 deletions src/context/display/displayFields.ml
Original file line number Diff line number Diff line change
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 @@ -404,15 +404,15 @@ let handle_missing_field_raise ctx tthis i mode with_type pfield =
let handle_missing_ident ctx i mode with_type p =
match ctx.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
18 changes: 9 additions & 9 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 @@ -332,16 +332,16 @@ 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
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 ->
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 @@ -435,14 +435,14 @@ let collect ctx tk with_type sort =
add (make_ci_literal "false" (tpair ctx.com.basic.tbool)) (Some "false");
begin match ctx.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
55 changes: 40 additions & 15 deletions src/context/typecore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ type typer_module = {
mutable import_statements : import list;
}

type typer_class = {
mutable curclass : tclass; (* TODO: should not be mutable *)
mutable tthis : t;
mutable get_build_infos : unit -> (module_type * t list * class_field list) option;
}

type build_kind =
| BuildNormal
| BuildGeneric of tclass
Expand Down Expand Up @@ -133,20 +139,18 @@ and typer = {
com : context;
t : basic_types;
g : typer_globals;
mutable m : typer_module;
c : typer_class;
mutable bypass_accessor : int;
mutable meta : metadata;
mutable with_type_stack : WithType.t list;
mutable call_argument_stack : expr list list;
(* variable *)
mutable pass : typer_pass;
(* per-module *)
mutable m : typer_module;
mutable is_display_file : bool;
(* per-class *)
mutable curclass : tclass;
mutable tthis : t;
mutable type_params : type_params;
mutable get_build_infos : unit -> (module_type * t list * class_field list) option;
(* per-function *)
mutable allow_inline : bool;
mutable allow_transform : bool;
Expand All @@ -173,6 +177,27 @@ and monomorphs = {
mutable perfunction : (tmono * pos) list;
}

module TyperManager = struct
let clone_for_class ctx c =
let ctx = {
ctx with
c = {
curclass = c;
tthis = (match c.cl_kind with
| KAbstractImpl a ->
(match a.a_this with
| TMono r when r.tm_type = None -> TAbstract (a,extract_param_types c.cl_params)
| t -> t)
| _ ->
TInst (c,extract_param_types c.cl_params));
get_build_infos = (fun () -> None);
};
type_params = (match c.cl_kind with KAbstractImpl a -> a.a_params | _ -> c.cl_params);
pass = PBuildClass;
} in
ctx
end

type field_host =
| FHStatic of tclass
| FHInstance of tclass * tparams
Expand Down Expand Up @@ -252,7 +277,7 @@ let pass_name = function
| PFinal -> "final"

let warning ?(depth=0) ctx w msg p =
let options = (Warning.from_meta ctx.curclass.cl_meta) @ (Warning.from_meta ctx.curfield.cf_meta) in
let options = (Warning.from_meta ctx.c.curclass.cl_meta) @ (Warning.from_meta ctx.curfield.cf_meta) in
match Warning.get_mode w options with
| WMEnable ->
module_warning ctx.com ctx.m.curmod w options msg p
Expand Down Expand Up @@ -525,7 +550,7 @@ let clone_type_parameter map path ttp =
let can_access ctx c cf stat =
if (has_class_field_flag cf CfPublic) then
true
else if c == ctx.curclass then
else if c == ctx.c.curclass then
true
else match ctx.m.curmod.m_statics with
| Some c' when c == c' ->
Expand Down Expand Up @@ -578,7 +603,7 @@ let can_access ctx c cf stat =
in
loop c.cl_meta || loop f.cf_meta
in
let module_path = ctx.curclass.cl_module.m_path in
let module_path = ctx.c.curclass.cl_module.m_path in
let cur_paths = ref [fst module_path @ [snd module_path], false] in
let rec loop c is_current_path =
cur_paths := (make_path c ctx.curfield, is_current_path) :: !cur_paths;
Expand All @@ -588,14 +613,14 @@ let can_access ctx c cf stat =
end;
List.iter (fun (c,_) -> loop c false) c.cl_implements;
in
loop ctx.curclass true;
loop ctx.c.curclass true;
let is_constr = cf.cf_name = "new" in
let rec loop c =
try
has Meta.Access ctx.curclass ctx.curfield ((make_path c cf), true)
has Meta.Access ctx.c.curclass ctx.curfield ((make_path c cf), true)
|| (
(* if our common ancestor declare/override the field, then we can access it *)
let allowed f = extends ctx.curclass c || (List.exists (has Meta.Allow c f) !cur_paths) in
let allowed f = extends ctx.c.curclass c || (List.exists (has Meta.Allow c f) !cur_paths) in
if is_constr then (
match c.cl_constructor with
| Some cf ->
Expand Down Expand Up @@ -705,7 +730,7 @@ let mk_infos ctx p params =
(EObjectDecl (
(("fileName",null_pos,NoQuotes) , (EConst (String(file,SDoubleQuotes)) , p)) ::
(("lineNumber",null_pos,NoQuotes) , (EConst (Int (string_of_int (Lexer.get_error_line p), None)),p)) ::
(("className",null_pos,NoQuotes) , (EConst (String (s_type_path ctx.curclass.cl_path,SDoubleQuotes)),p)) ::
(("className",null_pos,NoQuotes) , (EConst (String (s_type_path ctx.c.curclass.cl_path,SDoubleQuotes)),p)) ::
if ctx.curfield.cf_name = "" then
params
else
Expand Down Expand Up @@ -756,7 +781,7 @@ let push_this ctx e = match e.eexpr with

let create_deprecation_context ctx = {
(DeprecationCheck.create_context ctx.com) with
class_meta = ctx.curclass.cl_meta;
class_meta = ctx.c.curclass.cl_meta;
field_meta = ctx.curfield.cf_meta;
curmod = ctx.m.curmod;
}
Expand Down Expand Up @@ -803,13 +828,13 @@ let debug com (path : string list) str =
end
let init_class_done ctx =
let path = fst ctx.curclass.cl_path @ [snd ctx.curclass.cl_path] in
debug ctx.com path ("init_class_done " ^ s_type_path ctx.curclass.cl_path);
let path = fst ctx.c.curclass.cl_path @ [snd ctx.c.curclass.cl_path] in
debug ctx.com path ("init_class_done " ^ s_type_path ctx.c.curclass.cl_path);
init_class_done ctx
let ctx_pos ctx =
let inf = fst ctx.m.curmod.m_path @ [snd ctx.m.curmod.m_path]in
let inf = (match snd ctx.curclass.cl_path with "" -> inf | n when n = snd ctx.m.curmod.m_path -> inf | n -> inf @ [n]) in
let inf = (match snd ctx.c.curclass.cl_path with "" -> inf | n when n = snd ctx.m.curmod.m_path -> inf | n -> inf @ [n]) in
let inf = (match ctx.curfield.cf_name with "" -> inf | n -> inf @ [n]) in
inf
Expand Down
4 changes: 2 additions & 2 deletions src/filters/exceptions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ let haxe_exception_static_call ctx method_name args p =
| TFun(_,t) -> t
| _ -> raise_typing_error ("haxe.Exception." ^ method_name ^ " is not a function and cannot be called") p
in
add_dependency ctx.typer.curclass.cl_module ctx.haxe_exception_class.cl_module;
add_dependency ctx.typer.c.curclass.cl_module ctx.haxe_exception_class.cl_module;
make_static_call ctx.typer ctx.haxe_exception_class method_field (fun t -> t) args return_type p

(**
Expand Down Expand Up @@ -605,7 +605,7 @@ let insert_save_stacks tctx =
in
let catch_local = mk (TLocal catch_var) catch_var.v_type catch_var.v_pos in
begin
add_dependency tctx.curclass.cl_module native_stack_trace_cls.cl_module;
add_dependency tctx.c.curclass.cl_module native_stack_trace_cls.cl_module;
make_static_call tctx native_stack_trace_cls method_field (fun t -> t) [catch_local] return_type catch_var.v_pos
end
else
Expand Down
6 changes: 3 additions & 3 deletions src/filters/filters.ml
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ let destruction tctx detail_times main locals =
check_private_path com;
Naming.apply_native_paths;
add_rtti com;
(match com.platform with | Java | Cs -> (fun _ -> ()) | _ -> (fun mt -> AddFieldInits.add_field_inits tctx.curclass.cl_path locals com mt));
(match com.platform with | Java | Cs -> (fun _ -> ()) | _ -> (fun mt -> AddFieldInits.add_field_inits tctx.c.curclass.cl_path locals com mt));
(match com.platform with Hl -> (fun _ -> ()) | _ -> add_meta_field com);
check_void_field;
(match com.platform with | Cpp -> promote_first_interface_to_super | _ -> (fun _ -> ()));
Expand All @@ -560,7 +560,7 @@ let destruction tctx detail_times main locals =
List.iter (fun t ->
begin match t with
| TClassDecl c ->
tctx.curclass <- c
tctx.c.curclass <- c
| _ ->
()
end;
Expand Down Expand Up @@ -811,7 +811,7 @@ let run tctx main before_destruction =
"RenameVars",(match com.platform with
| Eval -> (fun e -> e)
| Java when defined com Jvm -> (fun e -> e)
| _ -> (fun e -> RenameVars.run tctx.curclass.cl_path locals e));
| _ -> (fun e -> RenameVars.run tctx.c.curclass.cl_path locals e));
"mark_switch_break_loops",mark_switch_break_loops;
] in
List.iter (run_expression_filters tctx detail_times filters) new_types;
Expand Down
2 changes: 1 addition & 1 deletion src/filters/filtersCommon.ml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ let run_expression_filters ?(ignore_processed_status=false) ctx detail_times fil
match t with
| TClassDecl c when is_removable_class c -> ()
| TClassDecl c ->
ctx.curclass <- c;
ctx.c.curclass <- c;
ctx.m <- TypeloadModule.make_curmod ctx c.cl_module;
let rec process_field f =
if ignore_processed_status || not (has_class_field_flag f CfPostProcessed) then begin
Expand Down
4 changes: 2 additions & 2 deletions src/filters/localStatic.ml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type lscontext = {

let promote_local_static lsctx run v eo =
let name = Printf.sprintf "%s_%s" lsctx.ctx.curfield.cf_name v.v_name in
let c = lsctx.ctx.curclass in
let c = lsctx.ctx.c.curclass in
begin try
let cf = PMap.find name c.cl_statics in
display_error lsctx.ctx.com (Printf.sprintf "The expanded name of this local (%s) conflicts with another static field" name) v.v_pos;
Expand Down Expand Up @@ -56,7 +56,7 @@ let run ctx e =
lut = Hashtbl.create 0;
added_fields = [];
} in
let c = ctx.curclass in
let c = ctx.c.curclass in
let rec run e = match e.eexpr with
| TBlock el ->
let el = ExtList.List.filter_map (fun e -> match e.eexpr with
Expand Down
4 changes: 2 additions & 2 deletions src/filters/tre.ml
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,12 @@ let run ctx =
| Method MethDynamic -> false
| Method MethInline -> true
| Method MethNormal ->
PMap.mem ctx.curfield.cf_name ctx.curclass.cl_statics
PMap.mem ctx.curfield.cf_name ctx.c.curclass.cl_statics
| _ ->
has_class_field_flag ctx.curfield CfFinal
in
let is_recursive_call callee args =
is_tre_eligible && is_recursive_method_call ctx.curclass ctx.curfield callee args
is_tre_eligible && is_recursive_method_call ctx.c.curclass ctx.curfield callee args
in
if has_tail_recursion is_recursive_call false true fn.tf_expr then
(* print_endline ("TRE: " ^ ctx.curfield.cf_pos.pfile ^ ": " ^ ctx.curfield.cf_name); *)
Expand Down
2 changes: 1 addition & 1 deletion src/optimization/inline.ml
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ class inline_state ctx ethis params cf f p = object(self)
in
let e = (if PMap.is_empty subst then e else inline_params false false e) in
let init = match vars with [] -> None | l -> Some l in
let md = ctx.curclass.cl_module.m_extra.m_display in
let md = ctx.c.curclass.cl_module.m_extra.m_display in
md.m_inline_calls <- (cf.cf_name_pos,{p with pmax = p.pmin + String.length cf.cf_name}) :: md.m_inline_calls;
let wrap e =
(* we can't mute the type of the expression because it is not correct to do so *)
Expand Down
4 changes: 2 additions & 2 deletions src/typing/calls.ml
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ let make_call ctx e params t ?(force_inline=false) p =
end;
let config = Inline.inline_config cl f params t in
ignore(follow f.cf_type); (* force evaluation *)
(match cl, ctx.curclass.cl_kind, params with
| Some c, KAbstractImpl _, { eexpr = TLocal { v_meta = v_meta } } :: _ when c == ctx.curclass ->
(match cl, ctx.c.curclass.cl_kind, params with
| Some c, KAbstractImpl _, { eexpr = TLocal { v_meta = v_meta } } :: _ when c == ctx.c.curclass ->
if
f.cf_name <> "_new"
&& has_meta Meta.This v_meta
Expand Down
12 changes: 6 additions & 6 deletions src/typing/fields.ml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ let no_abstract_constructor c p =

let check_constructor_access ctx c f p =
if (Meta.has Meta.CompilerGenerated f.cf_meta) then display_error ctx.com (error_msg (No_constructor (TClassDecl c))) p;
if not (can_access ctx c f true || extends ctx.curclass c) && not ctx.untyped then display_error ctx.com (Printf.sprintf "Cannot access private constructor of %s" (s_class_path c)) p
if not (can_access ctx c f true || extends ctx.c.curclass c) && not ctx.untyped then display_error ctx.com (Printf.sprintf "Cannot access private constructor of %s" (s_class_path c)) p

let check_no_closure_meta ctx cf fa mode p =
match mode with
Expand Down Expand Up @@ -193,11 +193,11 @@ let field_access ctx mode f fh e pfield =
match (match mode with MGet | MCall _ -> v.v_read | MSet _ -> v.v_write) with
| AccNo when not (Meta.has Meta.PrivateAccess ctx.meta) ->
(match follow e.etype with
| TInst (c,_) when extends ctx.curclass c || can_access ctx c { f with cf_flags = unset_flag f.cf_flags (int_of_class_field_flag CfPublic) } false ->
| TInst (c,_) when extends ctx.c.curclass c || can_access ctx c { f with cf_flags = unset_flag f.cf_flags (int_of_class_field_flag CfPublic) } false ->
normal false
| TAnon a ->
(match !(a.a_status) with
| ClassStatics c2 when ctx.curclass == c2 || can_access ctx c2 { f with cf_flags = unset_flag f.cf_flags (int_of_class_field_flag CfPublic) } true -> normal false
| ClassStatics c2 when ctx.c.curclass == c2 || can_access ctx c2 { f with cf_flags = unset_flag f.cf_flags (int_of_class_field_flag CfPublic) } true -> normal false
| _ -> if ctx.untyped then normal false else normal_failure())
| _ ->
if ctx.untyped then normal false else normal_failure())
Expand All @@ -214,7 +214,7 @@ let field_access ctx mode f fh e pfield =
match e.eexpr with
| TConst TThis -> true
| TLocal v -> Option.map_default (fun vthis -> v == vthis) false ctx.vthis
| TTypeExpr (TClassDecl c) when c == ctx.curclass -> true
| TTypeExpr (TClassDecl c) when c == ctx.c.curclass -> true
| _ -> false
) || bypass_accessor ()
in
Expand All @@ -239,10 +239,10 @@ let field_access ctx mode f fh e pfield =
normal true
| AccCtor ->
let is_child_of_abstract c =
has_class_flag c CAbstract && extends ctx.curclass c
has_class_flag c CAbstract && extends ctx.c.curclass c
in
(match ctx.curfun, fh with
| FunConstructor, FHInstance(c,_) when c == ctx.curclass || is_child_of_abstract c -> normal false
| FunConstructor, FHInstance(c,_) when c == ctx.c.curclass || is_child_of_abstract c -> normal false
| _ -> normal_failure()
)
| AccRequire (r,msg) ->
Expand Down
Loading

0 comments on commit 98c9171

Please sign in to comment.