Reviewed constructor API.
This commit is contained in:
@@ -44,14 +44,16 @@ class ClassBuilder {
|
|||||||
addMember(field);
|
addMember(field);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getConstructor():Constructor {
|
public function getConstructor(?fallback:Function):Constructor {
|
||||||
if (constructor == null)
|
if (constructor == null)
|
||||||
if (target.superClass != null && target.superClass.t.get().constructor != null) {
|
if (fallback != null)
|
||||||
|
new Constructor(fallback);
|
||||||
|
else if (target.superClass != null && target.superClass.t.get().constructor != null) {
|
||||||
try {
|
try {
|
||||||
var ctor = Context.getLocalClass().get().superClass.t.get().constructor.get();
|
var ctor = Context.getLocalClass().get().superClass.t.get().constructor.get();
|
||||||
var func = Context.getTypedExpr(ctor.expr()).getFunction().sure();
|
var func = Context.getTypedExpr(ctor.expr()).getFunction().sure();
|
||||||
|
|
||||||
//TODO: Make sure the code below is no longer necessary
|
//TODO: Check that the code below is no longer necessary
|
||||||
// for (arg in func.args)
|
// for (arg in func.args)
|
||||||
// arg.type = null;
|
// arg.type = null;
|
||||||
|
|
||||||
@@ -87,18 +89,16 @@ class ClassBuilder {
|
|||||||
}
|
}
|
||||||
for (m in macros)
|
for (m in macros)
|
||||||
ret.push(m);
|
ret.push(m);
|
||||||
|
|
||||||
if (verbose)
|
if (verbose)
|
||||||
for (field in ret)
|
for (field in ret)
|
||||||
Context.warning(new Printer().printField(field), field.pos);
|
Context.warning(new Printer().printField(field), field.pos);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
public function iterator():Iterator<Member>
|
public function iterator():Iterator<Member>
|
||||||
return this.memberList.copy().iterator();
|
return this.memberList.copy().iterator();
|
||||||
|
|
||||||
public function getOwnMember(name:String):Member
|
|
||||||
return
|
|
||||||
memberMap.get(name);
|
|
||||||
|
|
||||||
public function hasOwnMember(name:String):Bool
|
public function hasOwnMember(name:String):Bool
|
||||||
return
|
return
|
||||||
macros.exists(name) || memberMap.exists(name);
|
macros.exists(name) || memberMap.exists(name);
|
||||||
@@ -136,7 +136,8 @@ class ClassBuilder {
|
|||||||
|
|
||||||
if (hasOwnMember(m.name))
|
if (hasOwnMember(m.name))
|
||||||
m.pos.error('duplicate member declaration ' + m.name);
|
m.pos.error('duplicate member declaration ' + m.name);
|
||||||
|
if (!m.isStatic && hasSuperField(m.name))
|
||||||
|
m.overrides = true;
|
||||||
memberMap.set(m.name, m);
|
memberMap.set(m.name, m);
|
||||||
if (front)
|
if (front)
|
||||||
memberList.unshift(m);
|
memberList.unshift(m);
|
||||||
|
|||||||
@@ -1,9 +1,15 @@
|
|||||||
package tink.macro;
|
package tink.macro;
|
||||||
|
|
||||||
import haxe.macro.Expr;
|
import haxe.macro.Expr;
|
||||||
|
import tink.core.Pair;
|
||||||
using tink.MacroApi;
|
using tink.MacroApi;
|
||||||
|
|
||||||
|
enum FieldInit {
|
||||||
|
Value(e:Expr);
|
||||||
|
Arg(?t:ComplexType, ?noPublish:Bool);
|
||||||
|
OptArg(?e:Expr, ?t:ComplexType, ?noPublish:Bool);
|
||||||
|
}
|
||||||
|
|
||||||
class Constructor {
|
class Constructor {
|
||||||
var oldStatements:Array<Expr>;
|
var oldStatements:Array<Expr>;
|
||||||
var nuStatements:Array<Expr>;
|
var nuStatements:Array<Expr>;
|
||||||
@@ -15,7 +21,7 @@ class Constructor {
|
|||||||
var superCall:Expr;
|
var superCall:Expr;
|
||||||
public var isPublic:Null<Bool>;
|
public var isPublic:Null<Bool>;
|
||||||
|
|
||||||
public function new(f:Function, ?isPublic:Null<Bool> = null, ?pos:Position) {
|
public function new(f:Function, ?isPublic:Null<Bool> = null, ?pos:Position, ?meta:Metadata) {
|
||||||
this.nuStatements = [];
|
this.nuStatements = [];
|
||||||
this.isPublic = isPublic;
|
this.isPublic = isPublic;
|
||||||
this.pos = pos.sanitize();
|
this.pos = pos.sanitize();
|
||||||
@@ -56,19 +62,30 @@ class Constructor {
|
|||||||
this.nuStatements.unshift(e)
|
this.nuStatements.unshift(e)
|
||||||
else
|
else
|
||||||
this.nuStatements.push(e);
|
this.nuStatements.push(e);
|
||||||
|
|
||||||
|
public function addArg(name:String, ?t:ComplexType, ?e:Expr, ?opt = false)
|
||||||
|
args.push( { name : name, opt : opt || e != null, type : t, value: e } );
|
||||||
|
|
||||||
|
public function init(name:String, pos:Position, with:FieldInit, ?prepend:Bool) {
|
||||||
|
var e =
|
||||||
|
switch with {
|
||||||
|
case Arg(t, noPublish):
|
||||||
|
if (noPublish != true)
|
||||||
|
publish();
|
||||||
|
args.push( { name : name, opt : false, type : t } );
|
||||||
|
name.resolve(pos);
|
||||||
|
case OptArg(e, t, noPublish):
|
||||||
|
if (noPublish != true)
|
||||||
|
publish();
|
||||||
|
args.push( { name : name, opt : true, type : t, value: e } );
|
||||||
|
name.resolve(pos);
|
||||||
|
case Value(e): e;
|
||||||
|
}
|
||||||
|
|
||||||
public function init(name:String, pos:Position, ?e:Expr, ?def:Expr, ?prepend:Bool, ?t:ComplexType) {
|
var tmp = MacroApi.tempName();
|
||||||
if (e == null) {
|
|
||||||
e = name.resolve(pos);
|
addStatement(macro @:pos(pos) if (false) { var $tmp = this.$name; $i{tmp} = $e; }, true);
|
||||||
args.push( { name : name, opt : def != null, type : t, value : def } );
|
addStatement(macro @:pos(pos) (untyped this).$name = $e, prepend);
|
||||||
if (isPublic == null)
|
|
||||||
isPublic = true;
|
|
||||||
}
|
|
||||||
if (t != null)
|
|
||||||
e = ECheckType(e, t).at(e.pos);
|
|
||||||
var s = EUntyped('this'.resolve(pos)).at(pos).field(name, pos).assign(e, pos);
|
|
||||||
|
|
||||||
addStatement(s, prepend);
|
|
||||||
}
|
}
|
||||||
public inline function publish()
|
public inline function publish()
|
||||||
if (isPublic == null)
|
if (isPublic == null)
|
||||||
@@ -88,6 +105,7 @@ class Constructor {
|
|||||||
params: []
|
params: []
|
||||||
};
|
};
|
||||||
for (hook in onGenerateHooks) hook(f);
|
for (hook in onGenerateHooks) hook(f);
|
||||||
|
onGenerateHooks = [];
|
||||||
return {
|
return {
|
||||||
name: 'new',
|
name: 'new',
|
||||||
doc : null,
|
doc : null,
|
||||||
|
|||||||
Reference in New Issue
Block a user