瀏覽代碼

[macro] add overloads to ClassField

Simon Krajewski 10 年之前
父節點
當前提交
6ea7fcfec9
共有 4 個文件被更改,包括 37 次插入2 次删除
  1. 3 2
      interp.ml
  2. 5 0
      std/haxe/macro/Type.hx
  3. 13 0
      tests/unit/src/unit/issues/Issue3460.hx
  4. 16 0
      tests/unit/src/unit/issues/misc/Issue3460Macro.hx

+ 3 - 2
interp.ml

@@ -277,7 +277,7 @@ let constants =
 	"constructs";"names";"superClass";"interfaces";"fields";"statics";"constructor";"init";"t";
 	"gid";"uid";"atime";"mtime";"ctime";"dev";"ino";"nlink";"rdev";"size";"mode";"pos";"len";
 	"binops";"unops";"from";"to";"array";"op";"isPostfix";"impl";"resolve";
-	"id";"capture";"extra";"v";"ids";"vars";"en";"overrides";"status"];
+	"id";"capture";"extra";"v";"ids";"vars";"en";"overrides";"status";"overloads"];
 	h
 
 let h_get = hash "__get" and h_set = hash "__set"
@@ -4398,6 +4398,7 @@ and encode_cfield f =
 		"kind", encode_field_kind f.cf_kind;
 		"pos", encode_pos f.cf_pos;
 		"doc", null enc_string f.cf_doc;
+		"overloads", encode_ref f.cf_overloads (encode_array encode_cfield) (fun() -> "overloads");
 	]
 
 and encode_field_kind k =
@@ -4762,7 +4763,7 @@ let decode_cfield v =
 		cf_kind = decode_field_kind (field v "kind");
 		cf_params = decode_type_params (field v "params");
 		cf_expr = None;
-		cf_overloads = [];
+		cf_overloads = decode_ref (field v "overloads");
 	}
 
 let decode_efield v =

+ 5 - 0
std/haxe/macro/Type.hx

@@ -229,6 +229,11 @@ typedef ClassField = {
 		The associated documentation of the class field.
 	**/
 	var doc : Null<String>;
+
+	/**
+		The overload fields of the class field.
+	**/
+	var overloads : Ref<Array<ClassField>>;
 }
 
 /**

+ 13 - 0
tests/unit/src/unit/issues/Issue3460.hx

@@ -0,0 +1,13 @@
+package unit.issues;
+
+private class MyClass {
+	@:overload(function (x:Int):String {})
+	@:overload(function (x:String, y:Bool):Int {})
+	function test() { }
+}
+
+class Issue3460 extends Test {
+	function test() {
+		eq("x : Int -> String, x : String -> y : Bool -> Int", unit.issues.misc.Issue3460Macro.getOverloadString((null : MyClass)));
+	}
+}

+ 16 - 0
tests/unit/src/unit/issues/misc/Issue3460Macro.hx

@@ -0,0 +1,16 @@
+package unit.issues.misc;
+
+import haxe.macro.Context;
+import haxe.macro.Expr;
+
+class Issue3460Macro {
+	macro static public function getOverloadString(e:Expr) {
+		var c = switch (Context.typeof(e)) {
+			case TInst(c, _): c.get();
+			case _: throw "Something went wrong";
+		}
+		var cf = c.fields.get()[0];
+		var s = cf.overloads.get().map(function (cf) return haxe.macro.TypeTools.toString(cf.type)).join(", ");
+		return macro $v{s};
+	}
+}