Browse Source

split allow_inline/allow_transform, fix null padding being done on args

Nicolas Cannasse 3 years ago
parent
commit
648f706704

+ 1 - 0
src/context/typecore.ml

@@ -115,6 +115,7 @@ and typer = {
 	mutable get_build_infos : unit -> (module_type * t list * class_field list) option;
 	(* per-function *)
 	mutable allow_inline : bool;
+	mutable allow_transform : bool;
 	mutable curfield : tclass_field;
 	mutable untyped : bool;
 	mutable in_function : bool;

+ 3 - 1
src/macro/macroApi.ml

@@ -7,6 +7,7 @@ exception Abort
 
 type compiler_options = {
 	opt_inlining : bool option;
+	opt_transform : bool option;
 }
 
 (**
@@ -2049,7 +2050,8 @@ let macro_api ccom get_api =
 		);
 		"with_options", vfun2(fun opts f ->
 			let o = {
-				opt_inlining = opt decode_bool (field opts "inlining");
+				opt_inlining = opt decode_bool (field opts "allowInlining");
+				opt_transform = opt decode_bool (field opts "allowTransform");
 			} in
 			let f = prepare_callback f 0 in
 			(get_api()).with_options o (fun() -> f []);

+ 4 - 1
src/typing/callUnification.ml

@@ -131,7 +131,10 @@ let rec unify_call_args ctx el args r callp inline force_inline in_overload =
 		| [],(_,false,_) :: _ ->
 			call_error (Not_enough_arguments args) callp
 		| [],(name,true,t) :: args ->
-			begin match loop [] args with
+			if not ctx.allow_transform then begin
+				ignore(loop [] args);
+				[]
+			end else begin match loop [] args with
 				| [] when not (inline && (ctx.g.doinline || force_inline)) && not ctx.com.config.pf_pad_nulls ->
 					if is_pos_infos t then [mk_pos_infos t]
 					else []

+ 3 - 3
src/typing/calls.ml

@@ -215,7 +215,7 @@ let rec acc_get ctx g p =
 			else
 				typing_error "Invalid macro access" p
 		| _ ->
-			if fa.fa_inline && ctx.allow_inline then
+			if fa.fa_inline then
 				inline_read fa
 			else
 				FieldAccess.get_field_expr fa FRead
@@ -329,7 +329,7 @@ let type_bind ctx (e : texpr) (args,ret) params p =
 			let a = if is_pos_infos t then
 					let infos = mk_infos ctx p [] in
 					ordered_args @ [type_expr ctx infos (WithType.with_argument t n)]
-				else if ctx.com.config.pf_pad_nulls then
+				else if ctx.com.config.pf_pad_nulls && ctx.allow_transform then
 					(ordered_args @ [(mk (TConst TNull) t_dynamic p)])
 				else
 					ordered_args
@@ -395,7 +395,7 @@ let array_access ctx e1 e2 mode p =
 			| _ ->
 				has_abstract_array_access := true;
 				let f = AbstractCast.find_array_access ctx a pl e2 None p in
-				if not ctx.allow_inline then
+				if not ctx.allow_transform then
 					let _,_,r,_,_ = f in
 					AKExpr { eexpr = TArray(e1,e2); epos = p; etype = r }
 				else begin

+ 1 - 1
src/typing/fields.ml

@@ -204,7 +204,7 @@ let field_access ctx mode f fh e pfield =
 				if ctx.untyped then normal false else AKNo f.cf_name)
 		| AccNormal | AccNo ->
 			normal false
-		| AccCall when (not ctx.allow_inline) || (ctx.in_display && DisplayPosition.display_position#enclosed_in pfull) ->
+		| AccCall when (not ctx.allow_transform) || (ctx.in_display && DisplayPosition.display_position#enclosed_in pfull) ->
 			normal false
 		| AccCall ->
 			let m = (match mode with MSet _ -> "set_" | _ -> "get_") ^ f.cf_name in

+ 6 - 4
src/typing/macroContext.ml

@@ -405,16 +405,18 @@ let make_macro_api ctx p =
 		);
 		MacroApi.with_options = (fun opts f ->
 			let old_inline = ctx.allow_inline in
+			let old_transform = ctx.allow_transform in
 			(match opts.opt_inlining with
 			| None -> ()
 			| Some v -> ctx.allow_inline <- v);
-			let run() =
-				f();
-			in
+			(match opts.opt_transform with
+			| None -> ()
+			| Some v -> ctx.allow_transform <- v);
 			let restore() =
 				ctx.allow_inline <- old_inline;
+				ctx.allow_transform <- old_transform;
 			in
-			Std.finally restore run ()
+			Std.finally restore f ()
 		);
 		MacroApi.warning = (fun w msg p ->
 			warning ctx w msg p

+ 1 - 0
src/typing/typeloadModule.ml

@@ -721,6 +721,7 @@ let create_typer_context_for_module ctx m = {
 		macro_depth = 0;
 		curclass = null_class;
 		allow_inline = true;
+		allow_transform = true;
 		curfield = null_field;
 		tthis = mk_mono();
 		ret = mk_mono();

+ 1 - 0
src/typing/typer.ml

@@ -1991,6 +1991,7 @@ let rec create com =
 		in_loop = false;
 		in_display = false;
 		allow_inline = true;
+		allow_transform = true;
 		get_build_infos = (fun() -> None);
 		ret = mk_mono();
 		locals = PMap.empty;

+ 6 - 1
std/haxe/macro/Context.hx

@@ -646,8 +646,13 @@ class Context {
 	/**
 		Executes `code` in a context that has some compiler options set, restore the compiler to its
 		default behavior afterwards.
+
+		`allowInlining`: enable or disable inlining during typing with `typeExpr`.
+
+		`allowTransform`: when disabled, the code typed with `typeExpr` will be almost exactly the same
+		as the input code. This will disable some abstract types transformations.
 	**/
-	public static function withOptions<X>(options:{?inlining:Bool}, code : () -> X) : X {
+	public static function withOptions<X>(options:{?allowInlining:Bool,?allowTransform:Bool}, code : () -> X) : X {
 		return load("with_options", 2)(options, code);
 	}