Browse Source

support metadata on type parameters (see #3836)

Simon Krajewski 10 years ago
parent
commit
15fc1686ee
7 changed files with 37 additions and 21 deletions
  1. 2 1
      ast.ml
  2. 3 0
      gencs.ml
  3. 3 0
      genjava.ml
  4. 2 0
      interp.ml
  5. 2 1
      parser.ml
  6. 24 19
      std/haxe/macro/Expr.hx
  7. 1 0
      typeload.ml

+ 2 - 1
ast.ml

@@ -366,6 +366,7 @@ and type_param = {
 	tp_name : string;
 	tp_params :	type_param list;
 	tp_constraints : complex_type list;
+	tp_meta : metadata;
 }
 
 and documentation = string option
@@ -701,7 +702,7 @@ let map_expr loop (e,p) =
 		| CTExtend (tl,fl) -> CTExtend (List.map tpath tl, List.map cfield fl)
 		| CTOptional t -> CTOptional (ctype t)
 	and tparamdecl t =
-		{ tp_name = t.tp_name; tp_constraints = List.map ctype t.tp_constraints; tp_params = List.map tparamdecl t.tp_params }
+		{ tp_name = t.tp_name; tp_constraints = List.map ctype t.tp_constraints; tp_params = List.map tparamdecl t.tp_params; tp_meta = t.tp_meta }
 	and func f =
 		{
 			f_params = List.map tparamdecl f.f_params;

+ 3 - 0
gencs.ml

@@ -3738,6 +3738,7 @@ let convert_ilmethod ctx p m is_explicit_impl =
 				tp_name = "M" ^ string_of_int t.tnumber;
 				tp_params = [];
 				tp_constraints = [];
+				tp_meta = [];
 			}
 		) m.mtypes in
 		FFun {
@@ -3906,6 +3907,7 @@ let convert_delegate ctx p ilcls =
 			tp_name = "T" ^ string_of_int t.tnumber;
 			tp_params = [];
 			tp_constraints = [];
+			tp_meta = [];
 		}
 	) ilcls.ctypes in
 	let mk_op_fn op name p =
@@ -4087,6 +4089,7 @@ let convert_ilclass ctx p ?(delegate=false) ilcls = match ilcls.csuper with
 					tp_name = "T" ^ string_of_int p.tnumber;
 					tp_params = [];
 					tp_constraints = [];
+					tp_meta = [];
 				}) ilcls.ctypes
 			in
 

+ 3 - 0
genjava.ml

@@ -2711,6 +2711,7 @@ let convert_param ctx p parent param =
 			tp_name = name;
 			tp_params = [];
 			tp_constraints = List.map (convert_signature ctx p) constraints;
+			tp_meta = [];
 		}
 
 let get_type_path ctx ct = match ct with | CTPath p -> p | _ -> assert false
@@ -2831,12 +2832,14 @@ let convert_java_enum ctx p pe =
 								tp_name = name;
 								tp_params = [];
 								tp_constraints = List.map (convert_signature ctx p) (ext :: impl);
+								tp_meta = [];
 							}
 						| (name, None, impl) ->
 							{
 								tp_name = name;
 								tp_params = [];
 								tp_constraints = List.map (convert_signature ctx p) (impl);
+								tp_meta = [];
 							}
 					) field.jf_types in
 					ctx.jtparams <- old_types;

+ 2 - 0
interp.ml

@@ -3883,6 +3883,7 @@ and encode_tparam_decl tp =
 		"name", enc_string tp.tp_name;
 		"params", enc_array (List.map encode_tparam_decl tp.tp_params);
 		"constraints", enc_array (List.map encode_ctype tp.tp_constraints);
+		"meta", encode_meta_content tp.tp_meta;
 	]
 
 and encode_fun f =
@@ -4119,6 +4120,7 @@ and decode_tparam_decl v =
 		tp_name = dec_string (field v "name");
 		tp_constraints = (match field v "constraints" with VNull -> [] | a -> List.map decode_ctype (dec_array a));
 		tp_params = decode_tparams (field v "params");
+		tp_meta = decode_meta_content (field v "meta");
 	}
 
 and decode_fun v =

+ 2 - 1
parser.ml

@@ -1050,7 +1050,7 @@ and parse_constraint_params = parser
 	| [< >] -> []
 
 and parse_constraint_param = parser
-	| [< name = type_name; s >] ->
+	| [< meta = parse_meta; name = type_name; s >] ->
 		let params = (match s with parser
 			| [< >] -> []
 		) in
@@ -1066,6 +1066,7 @@ and parse_constraint_param = parser
 			tp_name = name;
 			tp_params = params;
 			tp_constraints = ctl;
+			tp_meta = meta;
 		}
 
 and parse_class_herit = parser

+ 24 - 19
std/haxe/macro/Expr.hx

@@ -73,11 +73,11 @@ enum Constant {
 
 	/*
 		Represents a regular expression literal.
-		
-		Example: `~/haxe/i`  
+
+		Example: `~/haxe/i`
 		 * The first argument _haxe_ is a string with regular expression pattern.
 		 * The second argument _i_ is a string with regular expression flags.
-		 
+
 		@see http://haxe.org/manual/std-regex.html
 	**/
 	CRegexp( r : String, opt : String );
@@ -262,7 +262,7 @@ typedef Expr = {
 }
 
 /**
-	Represents a AST node identical to `Expr`, but it allows constraining the 
+	Represents a AST node identical to `Expr`, but it allows constraining the
 	type of accepted expressions.
 	@see http://haxe.org/manual/macro-ExprOf.html
 **/
@@ -279,7 +279,7 @@ typedef Case = {
 	var values : Array<Expr>;
 
 	/**
-		The optional guard expressions of the case, if available. 
+		The optional guard expressions of the case, if available.
 	**/
 	@:optional var guard : Null<Expr>;
 
@@ -382,7 +382,7 @@ enum ExprDef {
 
 	/**
 		An unary operator `op` on `e`:
-		
+
 		* e++ (op = OpIncrement, postFix = true)
 		* e-- (op = OpDecrement, postFix = true)
 		* ++e (op = OpIncrement, postFix = false)
@@ -542,7 +542,7 @@ enum ComplexType {
 **/
 typedef TypePath = {
 	/**
-		Represents the package of the type path. 
+		Represents the package of the type path.
 	**/
 	var pack : Array<String>;
 
@@ -557,7 +557,7 @@ typedef TypePath = {
 	@:optional var params : Array<TypeParam>;
 
 	/**
-		Sub is set on module sub-type access:  
+		Sub is set on module sub-type access:
 		`pack.Module.Type` has name = Module, sub = Type, if available.
 	**/
 	@:optional var sub : Null<String>;
@@ -565,19 +565,19 @@ typedef TypePath = {
 
 /**
 	Represents a concrete type parameters in the AST.
-	
-	Haxe allows expressions in concrete type parameters, e.g. 
-	`new YourType<["hello", "world"]>`. In that case the value is `TPExpr` while 
+
+	Haxe allows expressions in concrete type parameters, e.g.
+	`new YourType<["hello", "world"]>`. In that case the value is `TPExpr` while
 	in the normal case it's `TPType`.
 **/
 enum TypeParam {
 	/**
-		
+
 	**/
 	TPType( t : ComplexType );
 
 	/**
-		
+
 	**/
 	TPExpr( e : Expr );
 }
@@ -600,6 +600,11 @@ typedef TypeParamDecl = {
 		The optional parameters of the type parameter.
 	**/
 	@:optional var params : Array<TypeParamDecl>;
+
+	/**
+		The metadata of the type parameter.
+	**/
+	@:optional var meta : Metadata;
 }
 
 /**
@@ -687,7 +692,7 @@ typedef Field = {
 	var name : String;
 
 	/**
-		The documentation of the field, if available. If the field has no 
+		The documentation of the field, if available. If the field has no
 		documentation, the value is `null`.
 	**/
 	@:optional var doc : Null<String>;
@@ -727,14 +732,14 @@ enum Access {
 	APublic;
 
 	/**
-		Private access modifier, grants access to class and its sub-classes 
+		Private access modifier, grants access to class and its sub-classes
 		only.
 		@see http://haxe.org/manual/class-field-visibility.html
 	**/
 	APrivate;
 
 	/**
-		Static access modifier. 
+		Static access modifier.
 	**/
 	AStatic;
 
@@ -745,20 +750,20 @@ enum Access {
 	AOverride;
 
 	/**
-		Dynamic (re-)bindable access modifier. 
+		Dynamic (re-)bindable access modifier.
 		@see http://haxe.org/manual/class-field-dynamic.html
 	**/
 	ADynamic;
 
 	/**
-		Inline access modifier. Allows expressions to be directly inserted in 
+		Inline access modifier. Allows expressions to be directly inserted in
 		place of calls to them.
 		@see http://haxe.org/manual/class-field-inline.html
 	**/
 	AInline;
 
 	/**
-		Macros access modifier. Allows expression macro functions. These are 
+		Macros access modifier. Allows expression macro functions. These are
 		normal functions which are executed as soon as they are typed.
 	**/
 	AMacro;

+ 1 - 0
typeload.ml

@@ -1427,6 +1427,7 @@ let rec type_type_params ?(enum_constructor=false) ctx path get_params p tp =
 	let c = mk_class ctx.m.curmod (fst path @ [snd path],n) p in
 	c.cl_params <- List.map (type_type_params ctx c.cl_path get_params p) tp.tp_params;
 	c.cl_kind <- KTypeParameter [];
+	c.cl_meta <- tp.Ast.tp_meta;
 	if enum_constructor then c.cl_meta <- (Meta.EnumConstructorParam,[],c.cl_pos) :: c.cl_meta;
 	let t = TInst (c,List.map snd c.cl_params) in
 	match tp.tp_constraints with