Przeglądaj źródła

make property type optional (when an initial value is set)

Nicolas Cannasse 13 lat temu
rodzic
commit
856439e781
7 zmienionych plików z 22 dodań i 11 usunięć
  1. 3 3
      ast.ml
  2. 2 0
      doc/CHANGES.txt
  3. 2 2
      genswf.ml
  4. 2 2
      interp.ml
  5. 5 1
      parser.ml
  6. 1 1
      std/haxe/macro/Expr.hx
  7. 7 2
      typeload.ml

+ 3 - 3
ast.ml

@@ -217,7 +217,7 @@ and access =
 and class_field_kind =
 	| FVar of complex_type option * expr option
 	| FFun of func
-	| FProp of string * string * complex_type * expr option
+	| FProp of string * string * complex_type option * expr option
 
 and class_field = {
 	cff_name : string;
@@ -472,7 +472,7 @@ let map_expr loop (e,p) =
 		{ f with cff_kind = (match f.cff_kind with
 			| FVar (t,e) -> FVar (opt ctype t, opt loop e)
 			| FFun f -> FFun (func f)
-			| FProp (get,set,t,e) -> FProp (get,set,ctype t,opt loop e))
+			| FProp (get,set,t,e) -> FProp (get,set,opt ctype t,opt loop e))
 		}
 	and ctype = function
 		| CTPath t -> CTPath (tpath t)
@@ -656,7 +656,7 @@ let reify in_macro =
 			let n, vl = (match k with
 				| FVar (ct,e) -> "FVar", [to_opt to_ctype ct p;to_opt to_expr e p]
 				| FFun f -> "FFun", [to_fun f p]
-				| FProp (get,set,t,e) -> "FProp", [to_string get p; to_string set p; to_ctype t p; to_opt to_expr e p]
+				| FProp (get,set,t,e) -> "FProp", [to_string get p; to_string set p; to_opt to_ctype t p; to_opt to_expr e p]
 			) in
 			mk_enum "FieldType" n vl p
 		in

+ 2 - 0
doc/CHANGES.txt

@@ -4,6 +4,8 @@
 	macro : added $[expr-list] support in reification
 	all : fixed using + overload usage
 	all : allow any type constraint for type parameters
+	all : make property type optional (when a initial value is set)
+	all : new post-compilation DCE
 	
 2012-07-16: 2.10
 	java/cs : added two new targets (beta)

+ 2 - 2
genswf.ml

@@ -269,7 +269,7 @@ let build_class com c file =
 		match f.hlf_kind with
 		| HFVar v ->
 			if v.hlv_const then
-				cf.cff_kind <- FProp ("default","never",make_type v.hlv_type,None)
+				cf.cff_kind <- FProp ("default","never",Some (make_type v.hlv_type),None)
 			else
 				cf.cff_kind <- FVar (Some (make_type v.hlv_type),None);
 			cf :: acc
@@ -372,7 +372,7 @@ let build_class com c file =
 			cff_doc = None;
 			cff_access = flags;
 			cff_meta = [];
-			cff_kind = if get && set then FVar (Some (make_type t), None) else FProp ((if get then "default" else "never"),(if set then "default" else "never"),make_type t,None);
+			cff_kind = if get && set then FVar (Some (make_type t), None) else FProp ((if get then "default" else "never"),(if set then "default" else "never"),Some (make_type t),None);
 		}
 	in
 	let fields = Hashtbl.fold (fun (name,stat) t acc ->

+ 2 - 2
interp.ml

@@ -3409,7 +3409,7 @@ and encode_field (f:class_field) =
 	let tag, pl = match f.cff_kind with
 		| FVar (t,e) -> 0, [null encode_ctype t; null encode_expr e]
 		| FFun f -> 1, [encode_fun f]
-		| FProp (get,set, t, e) -> 2, [enc_string get; enc_string set; encode_ctype t; null encode_expr e]
+		| FProp (get,set, t, e) -> 2, [enc_string get; enc_string set; null encode_ctype t; null encode_expr e]
 	in
 	enc_obj [
 		"name",enc_string f.cff_name;
@@ -3688,7 +3688,7 @@ and decode_field v =
 		| 1, [f] ->
 			FFun (decode_fun f)
 		| 2, [get;set; t; e] ->
-			FProp (dec_string get, dec_string set, decode_ctype t, opt decode_expr e)
+			FProp (dec_string get, dec_string set, opt decode_ctype t, opt decode_expr e)
 		| _ ->
 			raise Invalid_expr
 	in

+ 5 - 1
parser.ml

@@ -466,7 +466,11 @@ and parse_class_field s =
 		let name, pos, k = (match s with parser
 		| [< '(Kwd Var,p1); name, _ = ident; s >] ->
 			(match s with parser
-			| [< '(POpen,_); i1 = property_ident; '(Comma,_); i2 = property_ident; '(PClose,_); '(DblDot,_); t = parse_complex_type >] ->
+			| [< '(POpen,_); i1 = property_ident; '(Comma,_); i2 = property_ident; '(PClose,_) >] ->
+				let t = (match s with parser
+					| [< '(DblDot,_); t = parse_complex_type >] -> Some t
+					| [< >] -> None
+				) in
 				let e , p2 = (match s with parser
 				| [< '(Binop OpAssign,_); e = toplevel_expr; p2 = semicolon >] -> Some e , p2
 				| [< '(Semicolon,p2) >] -> None , p2

+ 1 - 1
std/haxe/macro/Expr.hx

@@ -190,7 +190,7 @@ enum Access {
 enum FieldType {
 	FVar( t : Null<ComplexType>, ?e : Null<Expr> );
 	FFun( f : Function );
-	FProp( get : String, set : String, t : ComplexType, ?e : Null<Expr> );
+	FProp( get : String, set : String, ?t : Null<ComplexType>, ?e : Null<Expr> );
 }
 
 typedef TypeDefinition = {

+ 7 - 2
typeload.ml

@@ -272,6 +272,7 @@ and load_complex_type ctx p t =
 						| "dynamic" -> AccCall ((if get then "get_"  else "set_") ^ n)
 						| _ -> AccCall m
 					in
+					let t = (match t with None -> error "Type required for structure property" p | Some t -> t) in
 					load_complex_type ctx p t, Var { v_read = access i1 true; v_write = access i2 false }
 			) in
 			let cf = {
@@ -805,7 +806,7 @@ let patch_class ctx c fields =
 					| Some t ->
 						f.cff_kind <- match f.cff_kind with
 						| FVar (_,e) -> FVar (Some t,e)
-						| FProp (get,set,_,eo) -> FProp (get,set,t,eo)
+						| FProp (get,set,_,eo) -> FProp (get,set,Some t,eo)
 						| FFun f -> FFun { f with f_type = Some t });
 					loop (f :: acc) l
 		in
@@ -1132,7 +1133,11 @@ let init_class ctx c p herits fields =
 			f, constr, cf, delay
 		| FProp (get,set,t,eo) ->
 			if override then error "You cannot override properties" p;
-			let ret = load_complex_type ctx p t in
+			let ret = (match t, eo with
+				| None, None -> error "Property must either define a type or a default value" p;
+				| None, _ -> mk_mono()
+				| Some t, _ -> load_complex_type ctx p t
+			) in
 			let check_get = ref (fun() -> ()) in
 			let check_set = ref (fun() -> ()) in
 			let check_method m t () =