瀏覽代碼

add CfStatic class flag (#9442)

Dan Korostelev 5 年之前
父節點
當前提交
39e0d2834e

+ 1 - 1
src/codegen/codegen.ml

@@ -60,7 +60,7 @@ let add_property_field com c =
 		) (PMap.empty,[]) props in
 		let t = mk_anon ~fields (ref Closed) in
 		let e = mk (TObjectDecl values) t p in
-		let cf = mk_field "__properties__" t p null_pos in
+		let cf = mk_field ~static:true "__properties__" t p null_pos in
 		cf.cf_expr <- Some e;
 		c.cl_statics <- PMap.add cf.cf_name cf c.cl_statics;
 		c.cl_ordered_statics <- cf :: c.cl_ordered_statics

+ 1 - 1
src/codegen/gencommon/closuresToClass.ml

@@ -540,7 +540,7 @@ let configure gen ft =
 			match captured, tparams with
 			| [], [] ->
 				let cache_var = mk_internal_name "hx" "current" in
-				let cache_cf = mk_class_field cache_var (TInst(cls,[])) false func_expr.epos (Var({ v_read = AccNormal; v_write = AccNormal })) [] in
+				let cache_cf = mk_class_field ~static:true cache_var (TInst(cls,[])) false func_expr.epos (Var({ v_read = AccNormal; v_write = AccNormal })) [] in
 				cls.cl_ordered_statics <- cache_cf :: cls.cl_ordered_statics;
 				cls.cl_statics <- PMap.add cache_var cache_cf cls.cl_statics;
 

+ 2 - 2
src/codegen/gencommon/enumToClass.ml

@@ -82,7 +82,7 @@ struct
 
 		(match Texpr.build_metadata gen.gcon.basic (TEnumDecl en) with
 			| Some expr ->
-				let cf = mk_class_field "__meta__" expr.etype false expr.epos (Var { v_read = AccNormal; v_write = AccNormal }) [] in
+				let cf = mk_class_field ~static:true "__meta__" expr.etype false expr.epos (Var { v_read = AccNormal; v_write = AccNormal }) [] in
 				cf.cf_expr <- Some expr;
 				cl.cl_statics <- PMap.add "__meta__" cf cl.cl_statics;
 				cl.cl_ordered_statics <- cf :: cl.cl_ordered_statics
@@ -173,7 +173,7 @@ struct
 			cl.cl_statics <- PMap.add cf.cf_name cf cl.cl_statics;
 			cf
 		) en.e_names in
-		let constructs_cf = mk_class_field "__hx_constructs" (gen.gclasses.nativearray basic.tstring) true pos (Var { v_read = AccNormal; v_write = AccNever }) [] in
+		let constructs_cf = mk_class_field ~static:true "__hx_constructs" (gen.gclasses.nativearray basic.tstring) true pos (Var { v_read = AccNormal; v_write = AccNever }) [] in
 		constructs_cf.cf_meta <- [Meta.ReadOnly,[],pos];
 		constructs_cf.cf_expr <- Some (mk_nativearray_decl gen basic.tstring (List.map (fun s -> { eexpr = TConst(TString s); etype = basic.tstring; epos = pos }) en.e_names) pos);
 

+ 4 - 4
src/codegen/gencommon/enumToClass2.ml

@@ -34,7 +34,7 @@ let add_field c cf override =
 
 let add_meta com en cl_enum =
 	Option.may (fun expr ->
-		let cf_meta = mk_field "__meta__" expr.etype expr.epos expr.epos in
+		let cf_meta = mk_field ~static:true "__meta__" expr.etype expr.epos expr.epos in
 		cf_meta.cf_expr <- Some expr;
 		add_static cl_enum cf_meta;
 	) (Texpr.build_metadata com.basic (TEnumDecl en));
@@ -69,7 +69,7 @@ module EnumToClass2Modf = struct
 		(* add constructs field (for reflection) *)
 		if has_feature gen.gcon "Type.getEnumConstructs" then begin
 			let e_constructs = mk_array_decl basic.tstring (List.map (fun s -> make_string gen.gcon.basic s pos) en.e_names) pos in
-			let cf_constructs = mk_field "__hx_constructs" e_constructs.etype pos pos in
+			let cf_constructs = mk_field ~static:true "__hx_constructs" e_constructs.etype pos pos in
 			cf_constructs.cf_kind <- Var { v_read = AccNormal; v_write = AccNever };
 			cf_constructs.cf_meta <- (Meta.ReadOnly,[],pos) :: (Meta.Protected,[],pos) :: cf_constructs.cf_meta;
 			cf_constructs.cf_expr <- Some e_constructs;
@@ -192,7 +192,7 @@ module EnumToClass2Modf = struct
 					};
 					add_field cl_ctor cf_toString true;
 
-					let cf_static_ctor = mk_class_field name ef_type true pos (Method MethNormal) [] in
+					let cf_static_ctor = mk_class_field ~static:true name ef_type true pos (Method MethNormal) [] in
 					cf_static_ctor.cf_expr <- Some {
 						eexpr = TFunction {
 							tf_args = !static_ctor_args;
@@ -273,7 +273,7 @@ module EnumToClass2Modf = struct
 					};
 					cl_ctor.cl_constructor <- Some cf_ctor;
 
-					let cf_static_inst = mk_class_field name cl_enum_t true pos (Var { v_read = AccNormal; v_write = AccNever }) [] in
+					let cf_static_inst = mk_class_field ~static:true name cl_enum_t true pos (Var { v_read = AccNormal; v_write = AccNever }) [] in
 					cf_static_inst.cf_meta <- [Meta.ReadOnly,[],pos];
 					cf_static_inst.cf_expr <- Some {
 						eexpr = TNew(cl_ctor, [], []);

+ 2 - 2
src/codegen/gencommon/gencommon.ml

@@ -1085,8 +1085,8 @@ let rec replace_mono t =
 		replace_mono (lazy_type f)
 
 (* helper *)
-let mk_class_field name t public pos kind params =
-	let f = mk_field name ~public t pos null_pos in
+let mk_class_field ?(static = false) name t public pos kind params =
+	let f = mk_field name ~public ~static t pos null_pos in
 	f.cf_meta <- [ Meta.CompilerGenerated, [], null_pos ]; (* annotate that this class field was generated by the compiler *)
 	f.cf_kind <- kind;
 	f.cf_params <- params;

+ 1 - 1
src/codegen/gencommon/overloadingConstructor.ml

@@ -145,7 +145,7 @@ let create_static_ctor com ~empty_ctor_expr cl ctor follow_type =
 			(new_v, b)
 		) cur_tf_args in
 
-		let static_ctor = mk_class_field static_ctor_name fn_type false ctor.cf_pos (Method MethNormal) ctor_types in
+		let static_ctor = mk_class_field ~static:true static_ctor_name fn_type false ctor.cf_pos (Method MethNormal) ctor_types in
 		let static_ctor_meta = if cl.cl_final then Meta.Private else Meta.Protected in
 		static_ctor.cf_meta <- (static_ctor_meta,[],ctor.cf_pos) :: static_ctor.cf_meta;
 

+ 1 - 1
src/codegen/gencommon/realTypeParams.ml

@@ -589,7 +589,7 @@ struct
 		let basic = gen.gcon.basic in
 		let cparams = List.map (fun (s,t) -> ("To_" ^ s, TInst (map_param (get_cl_t t), []))) cf.cf_params in
 		let me_type = TInst(iface,[]) in
-		let cfield = mk_class_field "__hx_cast" (TFun(["me",false,me_type], t_dynamic)) false iface.cl_pos (Method MethNormal) (cparams) in
+		let cfield = mk_class_field ~static:true "__hx_cast" (TFun(["me",false,me_type], t_dynamic)) false iface.cl_pos (Method MethNormal) (cparams) in
 		let params = List.map snd cparams in
 
 		let me = alloc_var "me" me_type in

+ 5 - 2
src/core/tFunctions.ml

@@ -137,7 +137,7 @@ let module_extra file sign time kind policy =
 	}
 
 
-let mk_field name ?(public = true) t p name_pos = {
+let mk_field name ?(public = true) ?(static = false) t p name_pos = {
 	cf_name = name;
 	cf_type = t;
 	cf_pos = p;
@@ -149,7 +149,10 @@ let mk_field name ?(public = true) t p name_pos = {
 	cf_expr_unoptimized = None;
 	cf_params = [];
 	cf_overloads = [];
-	cf_flags = if public then set_flag 0 (int_of_class_field_flag CfPublic) else 0;
+	cf_flags = (
+		let flags = if static then set_flag 0 (int_of_class_field_flag CfStatic) else 0 in
+		if public then set_flag flags (int_of_class_field_flag CfPublic) else flags
+	);
 }
 
 let null_module = {

+ 1 - 0
src/core/tType.ml

@@ -369,6 +369,7 @@ type class_field_scope =
 
 type flag_tclass_field =
 	| CfPublic
+	| CfStatic
 	| CfExtern (* This is only set if the field itself is extern, not just the class. *)
 	| CfFinal
 	| CfModifiesThis (* This is set for methods which reassign `this`. E.g. `this = value` *)

+ 1 - 1
src/filters/ES6Ctors.ml

@@ -212,7 +212,7 @@ let rewrite_ctors com =
 					end;
 
 					if cl == root then begin
-						let cf_skip_ctor = mk_field skip_ctor_flag_name com.basic.tbool null_pos null_pos in
+						let cf_skip_ctor = mk_field ~static:true skip_ctor_flag_name com.basic.tbool null_pos null_pos in
 						cf_skip_ctor.cf_expr <- Some e_false;
 						cl.cl_ordered_statics <- cf_skip_ctor :: cl.cl_ordered_statics;
 						cl.cl_statics <- PMap.add cf_skip_ctor.cf_name cf_skip_ctor cl.cl_statics;

+ 2 - 2
src/filters/filters.ml

@@ -435,7 +435,7 @@ let add_rtti ctx t =
 	in
 	match t with
 	| TClassDecl c when has_rtti c && not (PMap.mem "__rtti" c.cl_statics) ->
-		let f = mk_field "__rtti" ctx.t.tstring c.cl_pos null_pos in
+		let f = mk_field ~static:true "__rtti" ctx.t.tstring c.cl_pos null_pos in
 		let str = Genxml.gen_type_string ctx.com t in
 		f.cf_expr <- Some (mk (TConst (TString str)) f.cf_type c.cl_pos);
 		c.cl_ordered_statics <- f :: c.cl_ordered_statics;
@@ -513,7 +513,7 @@ let add_meta_field ctx t = match t with
 		| None -> ()
 		| Some e ->
 			add_feature ctx.com "has_metadata";
-			let cf = mk_field "__meta__" e.etype e.epos null_pos in
+			let cf = mk_field ~static:true "__meta__" e.etype e.epos null_pos in
 			cf.cf_expr <- Some e;
 			let can_deal_with_interface_metadata () = match ctx.com.platform with
 				| Cs | Java -> false

+ 1 - 1
src/typing/calls.ml

@@ -436,7 +436,7 @@ let type_generic_function ctx (e,fa) el ?(using_param=None) with_type p =
 				cf2.cf_meta <- (Meta.NoCompletion,[],p) :: (Meta.NoUsing,[],p) :: (Meta.GenericInstance,[],p) :: metadata
 			in
 			let mk_cf2 name =
-				mk_field name (map_monos cf.cf_type) cf.cf_pos cf.cf_name_pos
+				mk_field ~static:stat name (map_monos cf.cf_type) cf.cf_pos cf.cf_name_pos
 			in
 			if stat then begin
 				if Meta.has Meta.GenericClassPerMethod c.cl_meta then begin

+ 15 - 10
src/typing/typeloadFields.ml

@@ -616,7 +616,8 @@ let rec get_parent c name =
 		with
 			Not_found -> get_parent csup name
 
-let add_field c cf is_static =
+let add_field c cf =
+	let is_static = has_class_field_flag cf CfStatic in
 	if is_static then begin
 		c.cl_statics <- PMap.add cf.cf_name cf c.cl_statics;
 		c.cl_ordered_statics <- cf :: c.cl_ordered_statics;
@@ -1405,14 +1406,18 @@ let init_field (ctx,cctx,fctx) f =
 		| Some a when fctx.is_abstract_member -> ctx.type_params <- a.a_params;
 		| _ -> ()
 	end;
-	match f.cff_kind with
-	| FVar (t,e) ->
-		create_variable (ctx,cctx,fctx) c f t e p
-	| FFun fd ->
-		reject_final_static_method ctx cctx fctx f;
-		create_method (ctx,cctx,fctx) c f fd p
-	| FProp (get,set,t,eo) ->
-		create_property (ctx,cctx,fctx) c f (get,set,t,eo) p
+	let cf = 
+		match f.cff_kind with
+		| FVar (t,e) ->
+			create_variable (ctx,cctx,fctx) c f t e p
+		| FFun fd ->
+			reject_final_static_method ctx cctx fctx f;
+			create_method (ctx,cctx,fctx) c f fd p
+		| FProp (get,set,t,eo) ->
+			create_property (ctx,cctx,fctx) c f (get,set,t,eo) p
+	in
+	(if (fctx.is_static || fctx.is_macro && ctx.in_macro) then add_class_field_flag cf CfStatic);
+	cf
 
 let check_overload ctx f fs =
 	try
@@ -1552,7 +1557,7 @@ let init_class ctx c p context_init herits fields =
 						in
 						display_error ctx ("Duplicate " ^ type_kind ^ " field declaration : " ^ s_type_path path ^ "." ^ cf.cf_name) cf.cf_name_pos
 				else
-				if fctx.do_add then add_field c cf (fctx.is_static || fctx.is_macro && ctx.in_macro)
+				if fctx.do_add then add_field c cf
 			end
 		with Error (Custom str,p2) when p = p2 ->
 			display_error ctx str p