Selaa lähdekoodia

added Context.withOptions (disable inlining) and TypeTools.setVarName

Nicolas Cannasse 3 vuotta sitten
vanhempi
commit
53e8812721

+ 1 - 0
src/context/typecore.ml

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

+ 18 - 0
src/macro/macroApi.ml

@@ -5,6 +5,10 @@ open Common
 exception Invalid_expr
 exception Abort
 
+type compiler_options = {
+	opt_inlining : bool option;
+}
+
 (**
 	Our access to the compiler from the macro api
 **)
@@ -52,6 +56,7 @@ type 'value compiler_api = {
 	flush_context : (unit -> t) -> t;
 	display_error : (string -> pos -> unit);
 	with_imports : 'a . import list -> placed_name list list -> (unit -> 'a) -> 'a;
+	with_options : 'a . compiler_options -> (unit -> 'a) -> 'a;
 	warning : Warning.warning -> string -> pos -> unit;
 }
 
@@ -2042,5 +2047,18 @@ let macro_api ccom get_api =
 			let f = prepare_callback f 0 in
 			(get_api()).with_imports imports usings (fun () -> f [])
 		);
+		"with_options", vfun2(fun opts f ->
+			let o = {
+				opt_inlining = opt decode_bool (field opts "inlining");
+			} in
+			let f = prepare_callback f 0 in
+			(get_api()).with_options o (fun() -> f []);
+		);
+		"set_var_name", vfun2(fun v name ->
+			let v = decode_tvar v in
+			let name = decode_string name in
+			v.v_name <- name;
+			vnull;
+		)
 	]
 end

+ 1 - 1
src/optimization/inline.ml

@@ -7,7 +7,7 @@ open Typecore
 open Error
 
 let needs_inline ctx is_extern_class cf =
-	cf.cf_kind = Method MethInline
+	cf.cf_kind = Method MethInline && ctx.allow_inline
 	&& (ctx.g.doinline || is_extern_class || has_class_field_flag cf CfExtern)
 
 let mk_untyped_call name p params =

+ 3 - 3
src/typing/fields.ml

@@ -114,7 +114,7 @@ let field_access ctx mode f fh e pfield =
 	let is_set = match mode with MSet _ -> true | _ -> false in
 	check_no_closure_meta ctx f fh mode pfield;
 	let bypass_accessor = if ctx.bypass_accessor > 0 then (ctx.bypass_accessor <- ctx.bypass_accessor - 1; true) else false in
-	let make_access inline = FieldAccess.create e f fh inline pfull in
+	let make_access inline = FieldAccess.create e f fh (inline && ctx.allow_inline) pfull in
 	match f.cf_kind with
 	| Method m ->
 		let normal () = AKField(make_access false) in
@@ -130,7 +130,7 @@ let field_access ctx mode f fh e pfield =
 		in
 		let default () =
 			match m, mode with
-			| MethInline, _ when ctx.g.doinline ->
+			| MethInline, _ when ctx.g.doinline && ctx.allow_inline ->
 				AKField (make_access true)
 			| MethMacro, MGet ->
 				display_error ctx.com "Macro functions must be called immediately" pfield; normal()
@@ -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 ctx.in_display && DisplayPosition.display_position#enclosed_in pfull ->
+		| AccCall when (not ctx.allow_inline) || (ctx.in_display && DisplayPosition.display_position#enclosed_in pfull) ->
 			normal false
 		| AccCall ->
 			let m = (match mode with MSet _ -> "set_" | _ -> "get_") ^ f.cf_name in

+ 13 - 0
src/typing/macroContext.ml

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

+ 1 - 0
src/typing/typeloadModule.ml

@@ -720,6 +720,7 @@ let create_typer_context_for_module ctx m = {
 		get_build_infos = (fun() -> None);
 		macro_depth = 0;
 		curclass = null_class;
+		allow_inline = true;
 		curfield = null_field;
 		tthis = mk_mono();
 		ret = mk_mono();

+ 1 - 0
src/typing/typer.ml

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

+ 9 - 0
std/haxe/macro/Context.hx

@@ -642,6 +642,15 @@ class Context {
 		return load("with_imports", 3)(imports, usings, code);
 	}
 
+
+	/**
+		Executes `code` in a context that has some compiler options set, restore the compiler to its
+		default behavior afterwards.
+	**/
+	public static function withOptions<X>(options:{?inlining:Bool}, code : () -> X) : X {
+		return load("with_options", 2)(options, code);
+	}
+
 	@:deprecated
 	public static function registerModuleReuseCall(modulePath:String, macroCall:String) {
 		throw "This method is no longer supported. See https://github.com/HaxeFoundation/haxe/issues/5746";

+ 8 - 0
std/haxe/macro/TypeTools.hx

@@ -381,4 +381,12 @@ class TypeTools {
 		var field = (isStatic ? c.statics : c.fields).get().find(function(field) return field.name == name);
 		return if (field != null) field; else if (c.superClass != null) findField(c.superClass.t.get(), name, isStatic); else null;
 	}
+	
+	/**
+		Changes the name of the variable in the typed expression.
+	**/
+	static public function setVarName(t:TVar, name:String) {
+		Context.load("set_var_name",2)(t,name);
+	}
+
 }