Browse Source

[cs/java] extract DefaultArguments from gencommon into PASS 1.5 filters (#6134)

* run SetHXGen in filters

* [cs/java] move DefaultArguments filter into pre-analyzer/dce stage

* get rid of gencommon dependency for the DefaultArguments filter
Dan Korostelev 8 years ago
parent
commit
b83c8b8a11

+ 8 - 0
src/context/common.ml

@@ -38,6 +38,14 @@ type basic_types = {
 	mutable tarray : t -> t;
 }
 
+let const_type basic const default =
+	match const with
+	| TString _ -> basic.tstring
+	| TInt _ -> basic.tint
+	| TFloat _ -> basic.tfloat
+	| TBool _ -> basic.tbool
+	| _ -> default
+
 type stats = {
 	s_files_parsed : int ref;
 	s_classes_built : int ref;

+ 0 - 8
src/generators/gencommon.ml

@@ -168,14 +168,6 @@ let anon_class t =
 	| _ -> assert false
 
 
-let const_type basic const default =
-	match const with
-	| TString _ -> basic.tstring
-	| TInt _ -> basic.tint
-	| TFloat _ -> basic.tfloat
-	| TBool _ -> basic.tbool
-	| _ -> default
-
 let get_cl mt = match mt with TClassDecl cl -> cl | _ -> failwith (Printf.sprintf "Unexpected module type (class expected) for %s: %s" (s_type_path (t_path mt)) (s_module_type_kind mt))
 let get_abstract mt = match mt with TAbstractDecl a -> a | _ -> failwith (Printf.sprintf "Unexpected module type (abstract expected) for %s: %s" (s_type_path (t_path mt)) (s_module_type_kind mt))
 

+ 10 - 23
src/generators/gencommon/defaultArguments.ml

@@ -19,15 +19,12 @@
 open Common
 open Type
 open Codegen
-open Gencommon
+open Codegen.ExprBuilder
 
 (*
 	This Module Filter will go through all defined functions in all modules and change them
 	so they set all default arguments to be of a Nullable type, and adds the unroll from nullable to
 	the not-nullable type in the beginning of the function.
-
-	dependencies:
-		It must run before OverloadingConstructor, since OverloadingConstructor will change optional structures behavior
 *)
 
 let gen_check basic t nullable_var const pos =
@@ -38,12 +35,12 @@ let gen_check basic t nullable_var const pos =
 
 	let const_t = const_type basic const t in
 	let const = mk (TConst const) const_t pos in
-	let const = if needs_cast t const_t then mk_cast t const else const in
+	let const = if needs_cast t const_t then mk_cast const t pos else const in
 
-	let arg = mk_local nullable_var pos in
-	let arg = if needs_cast t nullable_var.v_type then mk_cast t arg else arg in
+	let arg = make_local nullable_var pos in
+	let arg = if needs_cast t nullable_var.v_type then mk_cast arg t pos else arg in
 
-	let check = binop Ast.OpEq (mk_local nullable_var pos) (null nullable_var.v_type pos) basic.tbool pos in
+	let check = binop Ast.OpEq (make_local nullable_var pos) (null nullable_var.v_type pos) basic.tbool pos in
 	mk (TIf (check, const, Some arg)) t pos
 
 let add_opt com block pos (var,opt) =
@@ -55,10 +52,7 @@ let add_opt com block pos (var,opt) =
 		(var, opt)
 	| Some const ->
 		let basic = com.basic in
-		let nullable_var = mk_temp var.v_name (basic.tnull var.v_type) in
-		let orig_name = var.v_name in
-		var.v_name <- nullable_var.v_name;
-		nullable_var.v_name <- orig_name;
+		let nullable_var = alloc_var var.v_name (basic.tnull var.v_type) pos in
 		(* var v = (temp_var == null) ? const : cast temp_var; *)
 		let evar = mk (TVar(var, Some(gen_check basic var.v_type nullable_var const pos))) basic.tvoid pos in
 		block := evar :: !block;
@@ -102,7 +96,7 @@ let rec change_func com cl cf =
 					(* check if the class really needs the super as the first statement -
 					just to make sure we don't inadvertently break any existing code *)
 					let rec check cl =
-						if not (is_hxgen (TClassDecl cl)) then
+						if not (Meta.has Meta.HxGen cl.cl_meta) then
 							()
 						else match cl.cl_super with
 							| None ->
@@ -139,7 +133,7 @@ let rec change_func com cl cf =
 					Type.concat { tf.tf_expr with eexpr = TBlock !block; etype = basic.tvoid } tf.tf_expr
 			in
 
-			args := fun_args tf_args;
+			args := List.map (fun (v,s) -> (v.v_name, (s <> None), v.v_type)) tf_args;
 
 			let cf_type = TFun (!args, ret) in
 			cf.cf_expr <- Some { texpr with
@@ -156,17 +150,10 @@ let rec change_func com cl cf =
 	| _, _ -> assert false
 
 let run com md =
-	(match md with
+	match md with
 	| TClassDecl cl ->
 		let apply = change_func com cl in
 		List.iter apply cl.cl_ordered_fields;
 		List.iter apply cl.cl_ordered_statics;
 		Option.may apply cl.cl_constructor;
-	| _ -> ());
-	md
-
-
-let name = "default_arguments"
-let priority = solve_deps name [ DBefore OverloadingConstructor.priority ]
-let configure gen =
-	gen.gmodule_filters#add name (PCustom priority) (run gen.gcon)
+	| _ -> ()

+ 1 - 1
src/generators/gencommon/fixOverrides.ml

@@ -37,7 +37,7 @@ open Gencommon
 
 *)
 let name = "fix_overrides"
-let priority = solve_deps name [DAfter DefaultArguments.priority]
+let priority = solve_deps name []
 
 (*
 	if the platform allows explicit interface implementation (C#),

+ 0 - 3
src/generators/gencs.ml

@@ -2631,8 +2631,6 @@ let generate con =
 		gen.greal_type <- real_type;
 		gen.greal_type_param <- change_param_type;
 
-		SetHXGen.run_filter gen.gcon gen.gtypes_list;
-
 		(* before running the filters, follow all possible types *)
 		(* this is needed so our module transformations don't break some core features *)
 		(* like multitype selection *)
@@ -3073,7 +3071,6 @@ let generate con =
 
 		ArrayDeclSynf.configure gen native_arr_cl change_param_type;
 
-		DefaultArguments.configure gen;
 		InterfaceMetas.configure gen;
 
 		CSharpSpecificSynf.configure gen runtime_cl;

+ 0 - 3
src/generators/genjava.ml

@@ -2059,8 +2059,6 @@ let generate con =
 	gen.greal_type <- real_type;
 	gen.greal_type_param <- change_param_type;
 
-	SetHXGen.run_filter gen.gcon gen.gtypes_list;
-
 	(* before running the filters, follow all possible types *)
 	(* this is needed so our module transformations don't break some core features *)
 	(* like multitype selection *)
@@ -2379,7 +2377,6 @@ let generate con =
 
 	ArrayDeclSynf.configure gen native_arr_cl change_param_type;
 
-	DefaultArguments.configure gen;
 	InterfaceMetas.configure gen;
 
 	JavaSpecificSynf.configure gen runtime_cl;

+ 31 - 13
src/optimization/filters.ml

@@ -1130,23 +1130,41 @@ let run com tctx main =
 		Optimizer.reduce_expression tctx;
 		captured_vars com;
 	] in
-	let filters = match com.platform with
-	| Cs ->
-		filters @ [
-			TryCatchWrapper.configure_cs com
-		]
-	| Java ->
-		filters @ [
-			TryCatchWrapper.configure_java com
-		]
-	| _ -> filters
+	let filters =
+		match com.platform with
+		| Cs ->
+			SetHXGen.run_filter com new_types;
+			filters @ [
+				TryCatchWrapper.configure_cs com
+			]
+		| Java ->
+			SetHXGen.run_filter com new_types;
+			filters @ [
+				TryCatchWrapper.configure_java com
+			]
+		| _ -> filters
 	in
 	List.iter (run_expression_filters tctx filters) new_types;
+
 	(* PASS 1.5: pre-analyzer type filters *)
-	List.iter (fun t ->
-		if com.platform = Cs then check_cs_events tctx.com t;
-	) new_types;
+	let filters =
+		match com.platform with
+		| Cs ->
+			[
+				check_cs_events tctx.com;
+				DefaultArguments.run com;
+			]
+		| Java ->
+			[
+				DefaultArguments.run com;
+			]
+		| _ ->
+			[]
+	in
+	List.iter (fun f -> List.iter f new_types) filters;
+
 	if com.platform <> Cross then Analyzer.Run.run_on_types tctx new_types;
+
 	let filters = [
 		Optimizer.sanitize com;
 		if com.config.pf_add_final_return then add_final_return else (fun e -> e);