Browse Source

enforced static variable generation order.

Nicolas Cannasse 19 years ago
parent
commit
371dbbab78
4 changed files with 14 additions and 8 deletions
  1. 2 2
      genneko.ml
  2. 1 1
      genswf8.ml
  3. 2 0
      type.ml
  4. 9 5
      typer.ml

+ 2 - 2
genneko.ml

@@ -345,7 +345,7 @@ let gen_static_vars t =
 		if c.cl_extern then
 			[]
 		else
-			PMap.fold (fun f acc ->
+			List.fold_right (fun f acc ->
 				match f.cf_expr with
 				| None -> acc
 				| Some e ->
@@ -357,7 +357,7 @@ let gen_static_vars t =
 							(field p (gen_type_path p c.cl_path) f.cf_name),
 							gen_expr e
 						),p) :: acc
-			) c.cl_statics []
+			) c.cl_ordered_statics []
 
 let gen_packages h t =
 	let rec loop acc p =

+ 1 - 1
genswf8.ml

@@ -1096,7 +1096,7 @@ let gen_type_def ctx t =
 		setvar ctx VarObj;
 		push ctx [VReg 1; VStr "__class__"; VReg 0];
 		setvar ctx VarObj;
-		PMap.iter (fun _ f -> gen_class_static_field ctx id f) c.cl_statics;
+		List.iter (gen_class_static_field ctx id) c.cl_ordered_statics;
 		PMap.iter (fun _ f -> gen_class_field ctx f) c.cl_fields;
 	| TEnumDecl e ->
 		let id = gen_type ctx e.e_path false in

+ 2 - 0
type.ml

@@ -97,6 +97,7 @@ and tclass = {
 	mutable cl_implements : (tclass * t list) list;
 	mutable cl_fields : (string , tclass_field) PMap.t;
 	mutable cl_statics : (string, tclass_field) PMap.t;
+	mutable cl_ordered_statics : tclass_field list;
 	mutable cl_dynamic : t option;
 	mutable cl_constructor : tclass_field option;
 }
@@ -142,6 +143,7 @@ let mk_class path pos doc =
 		cl_super = None;
 		cl_implements = [];
 		cl_fields = PMap.empty;
+		cl_ordered_statics = [];
 		cl_statics = PMap.empty;
 		cl_dynamic = None;
 		cl_constructor = None;

+ 9 - 5
typer.ml

@@ -1084,13 +1084,15 @@ let init_class ctx c p types herits fields =
 			c.cl_constructor <- Some f;
 		end else begin
 			if PMap.mem f.cf_name (if static then c.cl_statics else c.cl_fields) then error ("Duplicate class field declaration : " ^ f.cf_name) p;
-			if static then
-				c.cl_statics <- PMap.add f.cf_name f c.cl_statics
-			else
+			if static then begin
+				c.cl_statics <- PMap.add f.cf_name f c.cl_statics;
+				c.cl_ordered_statics <- f :: c.cl_ordered_statics;
+			end else
 				c.cl_fields <- PMap.add f.cf_name f c.cl_fields;
 		end;
 		delayed
 	) fields in
+	c.cl_ordered_statics <- List.rev c.cl_ordered_statics;
 	(* define an default inherited constructor *)
 	(match c.cl_constructor, c.cl_super with
 	| None , Some ({ cl_constructor = Some f } as csuper, cparams) ->
@@ -1371,13 +1373,15 @@ let types ctx main =
 		);
 		let path = ([],"@Main") in
 		let c = mk_class path null_pos None in
-		c.cl_statics <- PMap.add "init" {
+		let f = {
 			cf_name = "init";
 			cf_type = mk_mono();
 			cf_public = false;
 			cf_doc = None;
 			cf_expr = Some (mk (TCall (mk (TField (mk (TType t) (mk_mono()) null_pos,"main")) (mk_mono()) null_pos,[])) (mk_mono()) null_pos);
-		} c.cl_statics;
+		} in
+		c.cl_statics <- PMap.add "init" f c.cl_statics;
+		c.cl_ordered_statics <- f :: c.cl_ordered_statics;
 		types := TClassDecl c :: !types
 	);
 	List.rev !types