فهرست منبع

[typer] add `a_meta` to replace Meta.Enum on abstracts

Simon Krajewski 5 سال پیش
والد
کامیت
fb7ec06f3a

+ 2 - 2
src/codegen/swfLoader.ml

@@ -423,8 +423,8 @@ let build_class com c file =
 			d_name = path.tname,null_pos;
 			d_doc = None;
 			d_params = [];
-			d_meta = [(Meta.Enum,[],null_pos);(Meta.Native,[(EConst (String(native_path,SDoubleQuotes)),null_pos)],null_pos)];
-			d_flags = [AbExtern; AbOver (real_type,pos); AbFrom (real_type,pos)];
+			d_meta = [(Meta.Native,[(EConst (String(native_path,SDoubleQuotes)),null_pos)],null_pos)];
+			d_flags = [AbEnum;AbExtern; AbOver (real_type,pos); AbFrom (real_type,pos)];
 			d_data = constr;
 		} in
 		(path.tpackage, [(EAbstract abstract_data,pos)])

+ 1 - 1
src/context/display/displayToplevel.ml

@@ -340,7 +340,7 @@ let collect ctx tk with_type sort =
 		(* enum constructors *)
 		let rec enum_ctors t =
 			match t with
-			| TAbstractDecl ({a_impl = Some c} as a) when Meta.has Meta.Enum a.a_meta && 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.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

+ 1 - 1
src/context/display/documentSymbols.ml

@@ -104,7 +104,7 @@ let collect_module_symbols mname with_locals (pack,decls) =
 				ignore(add_type d TypeAlias)
 			)
 		| EAbstract d ->
-			let kind = if Meta.has Meta.Enum d.d_meta then EnumAbstract else Abstract in
+			let kind = if List.mem AbEnum d.d_flags then EnumAbstract else Abstract in
 			let parent = add_type d kind in
 			List.iter (field parent kind) d.d_data
 		| EStatic d ->

+ 1 - 0
src/core/ast.ml

@@ -300,6 +300,7 @@ type abstract_flag =
 	| AbTo of type_hint
 	| AbOver of type_hint
 	| AbExtern
+	| AbEnum
 
 type enum_constructor = {
 	ec_name : placed_name;

+ 2 - 2
src/core/display/completionItem.ml

@@ -152,7 +152,7 @@ module CompletionModuleType = struct
 				doc = d.d_doc;
 				is_extern = List.mem AbExtern d.d_flags;
 				is_final = false;
-				kind = if Meta.has Meta.Enum d.d_meta then EnumAbstract else Abstract;
+				kind = if List.mem AbEnum d.d_flags then EnumAbstract else Abstract;
 				has_constructor = ctor;
 				source = Syntax td;
 			}
@@ -207,7 +207,7 @@ module CompletionModuleType = struct
 				in
 				false,false,kind,ctor
 			| TAbstractDecl a ->
-				false,false,(if Meta.has Meta.Enum a.a_meta then EnumAbstract else Abstract),actor a
+				false,false,(if a.a_enum then EnumAbstract else Abstract),actor a
 		in
 		let infos = t_infos mt in
 		let convert_type_param (s,t) = match follow t with

+ 1 - 0
src/core/tFunctions.ml

@@ -212,6 +212,7 @@ let null_abstract = {
 	a_array = [];
 	a_read = None;
 	a_write = None;
+	a_enum = false;
 }
 
 let add_dependency m mdep =

+ 1 - 0
src/core/tType.ml

@@ -311,6 +311,7 @@ and tabstract = {
 	mutable a_array : tclass_field list;
 	mutable a_read : tclass_field option;
 	mutable a_write : tclass_field option;
+	a_enum : bool;
 }
 
 and module_type =

+ 1 - 1
src/generators/genhxold.ml

@@ -268,7 +268,7 @@ let generate_type com t =
 		print_meta a.a_meta;
 		Option.may (fun c -> try print_meta [Meta.get Meta.Require c.cl_meta] with Not_found -> ()) a.a_impl;
 		p "extern ";
-		let is_enum = Meta.has Meta.Enum a.a_meta in
+		let is_enum = a.a_enum in
 		if is_enum then p "enum ";
 		p "abstract %s" (stype (TAbstract (a,List.map snd a.a_params)));
 		if not (Meta.has Meta.CoreType a.a_meta) then p "(%s)" (stype a.a_this);

+ 1 - 1
src/optimization/dce.ml

@@ -669,7 +669,7 @@ let fix_accessors com =
 		(* filter empty abstract implementation classes (issue #1885). *)
 		| TClassDecl({cl_kind = KAbstractImpl _} as c) when c.cl_ordered_statics = [] && c.cl_ordered_fields = [] && not (Meta.has Meta.Used c.cl_meta) ->
 			add_class_flag c CExtern;
-		| TClassDecl({cl_kind = KAbstractImpl a} as c) when Meta.has Meta.Enum a.a_meta ->
+		| TClassDecl({cl_kind = KAbstractImpl a} as c) when a.a_enum ->
 			let is_runtime_field cf =
 				not (Meta.has Meta.Enum cf.cf_meta)
 			in

+ 2 - 3
src/syntax/grammar.mly

@@ -163,7 +163,6 @@ and parse_abstract doc meta flags p1 = parser
 			| [< '(BrOpen,_); fl, p2 = parse_class_fields false p1 >] -> fl,p2
 			| [< >] -> syntax_error (Expected ["{";"to";"from"]) s ([],last_pos s)
 		in
-		let flags = ExtList.List.filter_map decl_flag_to_abstract_flag flags in
 		let flags = (match st with None -> flags | Some t -> AbOver t :: flags) in
 		({
 			d_name = name;
@@ -267,7 +266,7 @@ and parse_type_decl mode s =
 			}, punion p1 p2)
 		| [< '(Kwd Enum,p1) >] ->
 			begin match s with parser
-			| [< '(Kwd Abstract,p1); a,p = parse_abstract doc ((Meta.Enum,[],null_pos) :: meta) c p1 >] ->
+			| [< '(Kwd Abstract,p1); a,p = parse_abstract doc meta (AbEnum :: (convert_abstract_flags c)) p1 >] ->
 				(EAbstract a,p)
 			| [< name = type_name; tl = parse_constraint_params; '(BrOpen,_); l = plist parse_enum; '(BrClose,p2) >] ->
 				(EEnum {
@@ -295,7 +294,7 @@ and parse_type_decl mode s =
 			}, punion p1 (pos t))
 		| [< '(Kwd Abstract,p1) >] ->
 			begin match s with parser
-			| [< a,p = parse_abstract doc meta c p1 >] ->
+			| [< a,p = parse_abstract doc meta (convert_abstract_flags c) p1 >] ->
 				EAbstract a,p
 			| [< >] ->
 				let c2 = parse_common_flags s in

+ 4 - 1
src/syntax/parser.ml

@@ -416,4 +416,7 @@ let check_signature_mark e p1 p2 =
 			if (display_position#get).pmin = p1.pmax then (mk_display_expr e DKMarked)
 			else e
 		end
-	end
+	end
+
+let convert_abstract_flags flags =
+	ExtList.List.filter_map decl_flag_to_abstract_flag flags

+ 1 - 1
src/typing/calls.ml

@@ -659,7 +659,7 @@ let rec acc_get ctx g p =
 			| FHInstance(c,tl) ->
 				(fun t -> t)
 			| FHAbstract(a,tl,c) ->
-				if Meta.has Meta.Enum a.a_meta then begin
+				if a.a_enum then begin
 					(* Enum abstracts have to apply their type parameters because they are basically statics with type params (#8700). *)
 					let monos = Monomorph.spawn_constrained_monos (fun t -> t) a.a_params in
 					apply_params a.a_params monos;

+ 3 - 3
src/typing/matcher.ml

@@ -294,7 +294,7 @@ module Pattern = struct
 					let sl = match follow t with
 						| TEnum(en,_) ->
 							en.e_names
-						| TAbstract({a_impl = Some c} as a,pl) when Meta.has Meta.Enum a.a_meta ->
+						| TAbstract({a_impl = Some c} as a,pl) when a.a_enum ->
 							ExtList.List.filter_map (fun cf ->
 								if has_class_field_flag cf CfImpl && Meta.has Meta.Enum cf.cf_meta then Some cf.cf_name else None
 							) c.cl_ordered_statics
@@ -1381,7 +1381,7 @@ module TexprConverter = struct
 				add (ConConst(TBool true),null_pos);
 				add (ConConst(TBool false),null_pos);
 				SKValue,RunTimeFinite
-			| TAbstract({a_impl = Some c} as a,pl) when Meta.has Meta.Enum a.a_meta ->
+			| TAbstract({a_impl = Some c} as a,pl) when a.a_enum ->
 				List.iter (fun cf ->
 					ignore(follow cf.cf_type);
 					if has_class_field_flag cf CfImpl && Meta.has Meta.Enum cf.cf_meta then match cf.cf_expr with
@@ -1425,7 +1425,7 @@ module TexprConverter = struct
 
 	let report_not_exhaustive v_lookup e_subject unmatched =
 		let sl = match follow e_subject.etype with
-			| TAbstract({a_impl = Some c} as a,tl) when Meta.has Meta.Enum a.a_meta ->
+			| TAbstract({a_impl = Some c} as a,tl) when a.a_enum ->
 				List.map (fun (con,_) -> match fst con with
 					| ConConst ct1 ->
 						let cf = List.find (fun cf ->

+ 20 - 22
src/typing/typeloadFields.ml

@@ -431,7 +431,7 @@ let apply_macro ctx mode path el p =
 	ctx.g.do_macro ctx mode cpath meth el p
 
 let build_module_def ctx mt meta fvars context_init fbuild =
-	let loop (f_build,f_enum) = function
+	let loop f_build = function
 		| Meta.Build,args,p -> (fun () ->
 				let epath, el = (match args with
 					| [ECall (epath,el),p] -> epath, el
@@ -447,19 +447,7 @@ let build_module_def ctx mt meta fvars context_init fbuild =
 				(match r with
 				| None -> error "Build failure" p
 				| Some e -> fbuild e)
-			) :: f_build,f_enum
-		| Meta.Enum,_,p -> f_build,Some (fun () ->
-				begin match mt with
-					| TClassDecl ({cl_kind = KAbstractImpl a} as c) ->
-						(* if p <> null_pos && not (Define.is_haxe3_compat ctx.com.defines) then
-							ctx.com.warning "`@:enum abstract` is deprecated in favor of `enum abstract`" p; *)
-						context_init#run;
-						let e = build_enum_abstract ctx c a (fvars()) p in
-						fbuild e;
-					| _ ->
-						()
-				end
-			)
+			) :: f_build
 		| Meta.Using,el,p -> (fun () ->
 			List.iter (fun e ->
 				try
@@ -474,23 +462,33 @@ let build_module_def ctx mt meta fvars context_init fbuild =
 				with Exit ->
 					error "dot path expected" (pos e)
 			) el;
-		) :: f_build,f_enum
+		) :: f_build
 		| _ ->
-			f_build,f_enum
+			f_build
 	in
 	(* let errors go through to prevent resume if build fails *)
-	let f_build,f_enum = List.fold_left loop ([],None) meta in
+	let f_build = List.fold_left loop [] meta in
 	(* Go for @:using in parents and interfaces *)
-	(match mt with
+	let f_enum = match mt with
+		| TClassDecl ({cl_kind = KAbstractImpl a} as c) when a.a_enum ->
+			Some (fun () ->
+				(* if p <> null_pos && not (Define.is_haxe3_compat ctx.com.defines) then
+					ctx.com.warning "`@:enum abstract` is deprecated in favor of `enum abstract`" p; *)
+				context_init#run;
+				let e = build_enum_abstract ctx c a (fvars()) a.a_name_pos in
+				fbuild e;
+			)
 		| TClassDecl { cl_super = csup; cl_implements = interfaces; cl_kind = kind } ->
 			let ti = t_infos mt in
 			let inherit_using (c,_) =
 				ti.mt_using <- ti.mt_using @ (t_infos (TClassDecl c)).mt_using
 			in
 			Option.may inherit_using csup;
-			List.iter inherit_using interfaces
-		| _ -> ()
-	);
+			List.iter inherit_using interfaces;
+			None
+		| _ ->
+			None
+	in
 	List.iter (fun f -> f()) (List.rev f_build);
 	(match f_enum with None -> () | Some f -> f())
 
@@ -858,7 +856,7 @@ let bind_var (ctx,cctx,fctx) cf e =
 				| Var v when v.v_read = AccInline ->
 					let e = require_constant_expression e "Inline variable initialization must be a constant value" in
 					begin match c.cl_kind with
-						| KAbstractImpl a when Meta.has Meta.Enum cf.cf_meta && Meta.has Meta.Enum a.a_meta ->
+						| KAbstractImpl a when Meta.has Meta.Enum cf.cf_meta && a.a_enum ->
 							unify ctx t (TAbstract(a,(Monomorph.spawn_constrained_monos (fun t -> t) a.a_params))) p;
 							let e1 = match e.eexpr with TCast(e1,None) -> e1 | _ -> e in
 							unify ctx e1.etype a.a_this e1.epos

+ 1 - 1
src/typing/typeloadFunction.ml

@@ -86,7 +86,7 @@ let type_function_arg_value ctx t c do_display =
 			let rec loop e = match e.eexpr with
 				| TConst _ -> Some e
 				| TField({eexpr = TTypeExpr _},FEnum _) -> Some e
-				| TField({eexpr = TTypeExpr _},FStatic({cl_kind = KAbstractImpl a},cf)) when Meta.has Meta.Enum a.a_meta && Meta.has Meta.Enum cf.cf_meta -> Some e
+				| TField({eexpr = TTypeExpr _},FStatic({cl_kind = KAbstractImpl a},cf)) when a.a_enum && Meta.has Meta.Enum cf.cf_meta -> Some e
 				| TCast(e,None) -> loop e
 				| _ ->
 					if ctx.com.display.dms_kind = DMNone || ctx.com.display.dms_inline && ctx.com.display.dms_error_policy = EPCollect then

+ 2 - 1
src/typing/typeloadModule.ml

@@ -330,6 +330,7 @@ let module_pass_1 ctx m tdecls loadp =
 				a_this = mk_mono();
 				a_read = None;
 				a_write = None;
+				a_enum = List.mem AbEnum d.d_flags;
 			} in
 			decls := (TAbstractDecl a, decl) :: !decls;
 			match d.d_data with
@@ -894,7 +895,7 @@ let init_module_type ctx context_init (decl,p) =
 				is_type := true;
 			| AbExtern ->
 				(match a.a_impl with Some c -> add_class_flag c CExtern | None -> (* Hmmmm.... *) ())
-			| AbPrivate -> ()
+			| AbPrivate | AbEnum -> ()
 		) d.d_flags;
 		a.a_from <- List.rev a.a_from;
 		a.a_to <- List.rev a.a_to;

+ 3 - 3
src/typing/typer.ml

@@ -103,7 +103,7 @@ let maybe_type_against_enum ctx f with_type iscall p =
 			let rec loop stack t = match follow t with
 				| TEnum (en,_) ->
 					true,en.e_path,en.e_names,TEnumDecl en
-				| TAbstract ({a_impl = Some c} as a,_) when has_meta Meta.Enum a.a_meta ->
+				| TAbstract ({a_impl = Some c} as a,_) when a.a_enum ->
 					let fields = ExtList.List.filter_map (fun cf ->
 						if Meta.has Meta.Enum cf.cf_meta then Some cf.cf_name else None
 					) c.cl_ordered_statics in
@@ -404,7 +404,7 @@ let rec type_ident_raise ctx i p mode with_type =
 			| [] -> raise Not_found
 			| (t,pt) :: l ->
 				match t with
-				| TAbstractDecl ({a_impl = Some c} as a) when Meta.has Meta.Enum a.a_meta ->
+				| TAbstractDecl ({a_impl = Some c} as a) when a.a_enum ->
 					begin try
 						let cf = PMap.find i c.cl_statics in
 						if not (Meta.has Meta.Enum cf.cf_meta) then
@@ -427,7 +427,7 @@ let rec type_ident_raise ctx i p mode with_type =
 				| TTypeDecl t ->
 					(match follow t.t_type with
 					| TEnum (e,_) -> loop ((TEnumDecl e,pt) :: l)
-					| TAbstract (a,_) when Meta.has Meta.Enum a.a_meta -> loop ((TAbstractDecl a,pt) :: l)
+					| TAbstract (a,_) when a.a_enum -> loop ((TAbstractDecl a,pt) :: l)
 					| _ -> loop l)
 				| TEnumDecl e ->
 					try