Browse Source

moved Typeload.make_const to Optimizer.make_constant_expression

Simon Krajewski 12 years ago
parent
commit
79279c97be
2 changed files with 24 additions and 23 deletions
  1. 21 0
      optimizer.ml
  2. 3 23
      typeload.ml

+ 21 - 0
optimizer.ml

@@ -891,6 +891,27 @@ let rec reduce_loop ctx e =
 let reduce_expression ctx e =
 	if ctx.com.foptimize then reduce_loop ctx e else e
 
+let rec make_constant_expression ctx e =
+	let e = ctx.g.do_optimize ctx e in
+	match e.eexpr with
+	| TConst _ -> Some e
+	| TBinop ((OpAdd|OpSub|OpMult|OpDiv|OpMod) as op,e1,e2) -> (match make_constant_expression ctx e1,make_constant_expression ctx e2 with
+		| Some e1, Some e2 -> Some (mk (TBinop(op, e1, e2)) e.etype e.epos)
+		| _ -> None)
+	| TParenthesis e -> Some e
+	| TTypeExpr _ -> Some e
+	(* try to inline static function calls *)
+	| TCall ({ etype = TFun(_,ret); eexpr = TField (_,FStatic (c,cf)) },el) ->
+		(try
+			let func = match cf.cf_expr with Some ({eexpr = TFunction func}) -> func | _ -> raise Not_found in
+			let ethis = mk (TConst TThis) t_dynamic e.epos in
+			let inl = (try type_inline ctx cf func ethis el ret e.epos false with Error (Custom _,_) -> None) in
+			(match inl with
+			| None -> None
+			| Some e -> make_constant_expression ctx e)
+		with Not_found -> None)
+	| _ -> None
+
 (* ---------------------------------------------------------------------- *)
 (* COMPLETION *)
 

+ 3 - 23
typeload.ml

@@ -1101,27 +1101,7 @@ let init_class ctx c p context_init herits fields =
 		let p = cf.cf_pos in
 		if not stat && has_field cf.cf_name c.cl_super then error ("Redefinition of variable " ^ cf.cf_name ^ " in subclass is not allowed") p;
 		let t = cf.cf_type in
-		let rec make_const e =
-			let e = ctx.g.do_optimize ctx e in
-			match e.eexpr with
-			| TConst _ -> Some e
-			| TBinop ((OpAdd|OpSub|OpMult|OpDiv|OpMod) as op,e1,e2) -> (match make_const e1,make_const e2 with
-				| Some e1, Some e2 -> Some (mk (TBinop(op, e1, e2)) e.etype e.epos)
-				| _ -> None)
-			| TParenthesis e -> Some e
-			| TTypeExpr _ -> Some e
-			(* try to inline static function calls *)
-			| TCall ({ etype = TFun(_,ret); eexpr = TField (_,FStatic (c,cf)) },el) ->
-				(try
-					let func = match cf.cf_expr with Some ({eexpr = TFunction func}) -> func | _ -> raise Not_found in
-					let ethis = mk (TConst TThis) t_dynamic e.epos in
-					let inl = (try Optimizer.type_inline ctx cf func ethis el ret e.epos false with Error (Custom _,_) -> None) in
-					(match inl with
-					| None -> None
-					| Some e -> make_const e)
-				with Not_found -> None)
-			| _ -> None
-		in
+
 		match e with
 		| None -> ()
 		| Some e ->
@@ -1140,12 +1120,12 @@ let init_class ctx c p context_init herits fields =
 							display_error ctx "Extern non-inline variables may not be initialized" p;
 							e
 						end else begin
-							match make_const e with
+							match Optimizer.make_constant_expression ctx e with
 							| Some e -> e
 							| None -> display_error ctx "Extern variable initialization must be a constant value" p; e
 						end
 					| Var v when not stat || (v.v_read = AccInline && Common.defined ctx.com Define.Haxe3) ->
-						let e = match make_const e with Some e -> e | None -> display_error ctx "Variable initialization must be a constant value" p; e in
+						let e = match Optimizer.make_constant_expression ctx e with Some e -> e | None -> display_error ctx "Variable initialization must be a constant value" p; e in
 						e
 					| _ ->
 						e