Compare commits
37 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
0be2090232 | ||
![]() |
0dc774db21 | ||
![]() |
a7c22bf5af | ||
![]() |
a164b335ed | ||
![]() |
d02306eebd | ||
![]() |
ff9ef59445 | ||
![]() |
bfc5c2b88f | ||
![]() |
57042819b1 | ||
![]() |
82d15d9ffc | ||
![]() |
0d9fff7e01 | ||
![]() |
45d5f3e0ea | ||
![]() |
800891bee2 | ||
![]() |
0802bf7f79 | ||
![]() |
42897c7cd5 | ||
![]() |
0e902745b1 | ||
![]() |
3dec4fa404 | ||
![]() |
c0ec918f52 | ||
![]() |
4eb9729f0e | ||
![]() |
7586b54ece | ||
![]() |
21c7d12be7 | ||
![]() |
f27d2796b1 | ||
![]() |
417a92cabc | ||
![]() |
4457ea7dee | ||
![]() |
209847e5ea | ||
![]() |
c94e4a0337 | ||
![]() |
cc009b2026 | ||
![]() |
0a36ee8e2b | ||
![]() |
5879773566 | ||
![]() |
58262a1810 | ||
![]() |
9ceadeb88f | ||
![]() |
15d75965ad | ||
![]() |
c7634e0a29 | ||
![]() |
49e48bc801 | ||
![]() |
c150b84f18 | ||
![]() |
04f86ce341 | ||
![]() |
45380371d8 | ||
![]() |
d94d946301 |
@@ -1,5 +1,7 @@
|
||||
# Tinkerbell Macro Library
|
||||
[](https://gitter.im/haxetink/public)
|
||||
|
||||
[](https://travis-ci.org/haxetink/tink_macro)
|
||||
[](https://gitter.im/haxetink/public)
|
||||
|
||||
Explained in current marketing speak, `tink_macro` is *the* macro toolkit ;)
|
||||
|
||||
@@ -301,7 +303,7 @@ abstract Member from Field to Field {
|
||||
|
||||
Most of the API should be self-explaining. The `isBound` property is a bad name to convey the concept that a field can be either `inline` (`true`) or `dynamic` (`false`) or neither (`null`). Equally, `isPublic` is nullable which means that normally defaults to `private`.
|
||||
|
||||
The `publish` method will make a field `public` if it is not `private`. This can also be done with `if (m.isPublic == null) m.isPublic = true;` but the implementation is far more efficient - for what its worth.
|
||||
The `publish` method will make a field `public` if it is not explicitly marked as `private`. This can also be done with `if (m.isPublic == null) m.isPublic = true;` but the implementation is far more efficient - for what its worth.
|
||||
|
||||
The `extractMeta` method will "peel of" the first tag with a given `name` - if available. Note that the tag will be removed from the member.
|
||||
|
||||
@@ -389,7 +391,7 @@ The `init` method is the swiss army knife of initializing fields. The `options.p
|
||||
|
||||
#### Setter Bypass
|
||||
|
||||
It is important to know that when you initialize a field with `options.bypass` set to true, existing setters will by bypassed. That's particularly helpful if your setter triggers a side effect that you don't want triggered. This is achieved by generating the assignment as `(untyped this).$name = $value`. To make the code typesafe again, this is prefixed with `if (false) { var __tmp = this.$name; __tmp = $value; }`. This code is later thrown out by the compiler. Its role is to ensure type safety without interfering with the normal typing order.
|
||||
It is important to know that when you initialize a field with `options.bypass` set to true, existing setters will be bypassed. That's particularly helpful if your setter triggers a side effect that you don't want triggered. This is achieved by generating the assignment as `(untyped this).$name = $value`. To make the code typesafe again, this is prefixed with `if (false) { var __tmp = this.$name; __tmp = $value; }`. This code is later thrown out by the compiler. Its role is to ensure type safety without interfering with the normal typing order.
|
||||
|
||||
Setter bypass also causes the field to gain an `@:isVar`. And currently, with `-dce full`, additional code will be generated to avoid the field to be eliminated.
|
||||
|
||||
|
@@ -11,8 +11,8 @@
|
||||
"contributors": [
|
||||
"back2dos"
|
||||
],
|
||||
"releasenote": "Fix 3.2.1.",
|
||||
"version": "0.13.5",
|
||||
"releasenote": "Support artificial scopes when typing. Expose more BuildCache logic. Further improve Type -> ComplexType transformation.",
|
||||
"version": "0.16.0",
|
||||
"url": "http://haxetink.org/tink_macro",
|
||||
"dependencies": {
|
||||
"tink_core": ""
|
||||
|
@@ -106,22 +106,30 @@ class BuildCache {
|
||||
}));
|
||||
}
|
||||
|
||||
static public function getParams(name:String, ?pos:Position)
|
||||
return
|
||||
switch Context.getLocalType() {
|
||||
case TInst(_.toString() == name => true, v):
|
||||
Success(v);
|
||||
case TInst(_.get() => { pos: pos }, _):
|
||||
pos.makeFailure('Expected $name');
|
||||
case v:
|
||||
pos.makeFailure('$v should be a class');
|
||||
}
|
||||
|
||||
static public function getParam(name:String, ?pos:Position)
|
||||
return
|
||||
getParams(name, pos)
|
||||
.flatMap(function (args:Array<Type>) return switch args {
|
||||
case [v]: Success(v);
|
||||
case []: pos.makeFailure('type parameter expected');
|
||||
default: pos.makeFailure('too many parameters');
|
||||
});
|
||||
|
||||
static public function getType(name, ?type, ?pos:Position, build:BuildContext->TypeDefinition) {
|
||||
|
||||
if (pos == null)
|
||||
pos = Context.currentPos();
|
||||
|
||||
if (type == null)
|
||||
switch Context.getLocalType() {
|
||||
case TInst(_.toString() == name => true, [v]):
|
||||
type = v;
|
||||
case TInst(_.toString() == name => true, _):
|
||||
Context.fatalError('type parameter expected', pos);
|
||||
case TInst(_.get() => { pos: pos }, _):
|
||||
Context.fatalError('Expected $name', pos);
|
||||
default:
|
||||
throw 'assert';
|
||||
}
|
||||
type = getParam(name, pos).sure();
|
||||
|
||||
var forName =
|
||||
switch cache[name] {
|
||||
@@ -129,7 +137,7 @@ class BuildCache {
|
||||
case v: v;
|
||||
}
|
||||
|
||||
return forName.get(type, pos, build);
|
||||
return forName.get(type, pos.sanitize(), build);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -9,51 +9,51 @@ using tink.MacroApi;
|
||||
using Lambda;
|
||||
|
||||
class ClassBuilder {
|
||||
|
||||
|
||||
var memberList:Array<Member>;
|
||||
var macros:Map<String,Field>;
|
||||
var constructor:Null<Constructor>;
|
||||
public var target(default, null):ClassType;
|
||||
var superFields:Map<String,Bool>;
|
||||
|
||||
|
||||
var initializeFrom:Array<Field>;
|
||||
|
||||
public function new(?target, ?fields) {
|
||||
if (target == null)
|
||||
|
||||
public function new(?target, ?fields) {
|
||||
if (target == null)
|
||||
target = Context.getLocalClass().get();
|
||||
|
||||
|
||||
if (fields == null)
|
||||
fields = Context.getBuildFields();
|
||||
|
||||
|
||||
this.initializeFrom = fields;
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
|
||||
function init() {
|
||||
if (initializeFrom == null) return;
|
||||
|
||||
|
||||
var fields = initializeFrom;
|
||||
initializeFrom = null;
|
||||
|
||||
|
||||
this.memberList = [];
|
||||
this.macros = new Map();
|
||||
|
||||
for (field in fields)
|
||||
|
||||
for (field in fields)
|
||||
if (field.access.has(AMacro))
|
||||
macros.set(field.name, field)
|
||||
else if (field.name == 'new') {
|
||||
var m:Member = field;
|
||||
this.constructor = new Constructor(this, m.getFunction().sure(), m.isPublic, m.pos, field.meta);
|
||||
}
|
||||
else
|
||||
else
|
||||
doAddMember(field);
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function getConstructor(?fallback:Function):Constructor {
|
||||
init();
|
||||
if (constructor == null)
|
||||
if (constructor == null)
|
||||
if (fallback != null)
|
||||
constructor = new Constructor(this, fallback);
|
||||
else {
|
||||
@@ -64,18 +64,18 @@ class ClassBuilder {
|
||||
try {
|
||||
var ctor = cl.constructor.get();
|
||||
var func = Context.getTypedExpr(ctor.expr()).getFunction().sure();
|
||||
|
||||
|
||||
for (arg in func.args) //this is to deal with type parameter substitutions
|
||||
arg.type = null;
|
||||
|
||||
|
||||
func.expr = "super".resolve().call(func.getArgIdents());
|
||||
constructor = new Constructor(this, func);
|
||||
if (ctor.isPublic)
|
||||
constructor.publish();
|
||||
constructor.publish();
|
||||
}
|
||||
catch (e:Dynamic) {//fails for unknown reason
|
||||
if (e == 'assert')
|
||||
neko.Lib.rethrow(e);
|
||||
tink.core.Error.rethrow(e);
|
||||
constructor = new Constructor(this, null);
|
||||
}
|
||||
break;
|
||||
@@ -85,15 +85,15 @@ class ClassBuilder {
|
||||
if (constructor == null)
|
||||
constructor = new Constructor(this, null);
|
||||
}
|
||||
|
||||
|
||||
return constructor;
|
||||
}
|
||||
|
||||
|
||||
public function hasConstructor():Bool {
|
||||
init();
|
||||
return this.constructor != null;
|
||||
}
|
||||
|
||||
|
||||
public function export(?verbose):Array<Field> {
|
||||
if (initializeFrom != null) return null;
|
||||
var ret = (constructor == null || target.isInterface) ? [] : [constructor.toHaxe()];
|
||||
@@ -108,24 +108,24 @@ class ClassBuilder {
|
||||
}
|
||||
for (m in macros)
|
||||
ret.push(m);
|
||||
|
||||
if (verbose)
|
||||
for (field in ret)
|
||||
|
||||
if (verbose)
|
||||
for (field in ret)
|
||||
Context.warning(new Printer().printField(field), field.pos);
|
||||
|
||||
return ret;
|
||||
|
||||
return ret;
|
||||
}
|
||||
public function iterator():Iterator<Member> {
|
||||
init();
|
||||
return this.memberList.copy().iterator();
|
||||
}
|
||||
|
||||
|
||||
public function hasOwnMember(name:String):Bool {
|
||||
init();
|
||||
return
|
||||
return
|
||||
macros.exists(name) || memberByName(name).isSuccess();
|
||||
}
|
||||
|
||||
|
||||
public function hasSuperField(name:String):Bool {
|
||||
if (superFields == null) {
|
||||
superFields = new Map();
|
||||
@@ -139,51 +139,57 @@ class ClassBuilder {
|
||||
}
|
||||
return superFields.get(name);
|
||||
}
|
||||
|
||||
|
||||
public function memberByName(name:String, ?pos:Position) {
|
||||
init();
|
||||
for (m in memberList)
|
||||
if (m.name == name)
|
||||
if (m.name == name)
|
||||
return Success(m);
|
||||
|
||||
|
||||
return pos.makeFailure('unknown member $name');
|
||||
}
|
||||
|
||||
|
||||
public function removeMember(member:Member):Bool {
|
||||
init();
|
||||
return
|
||||
return
|
||||
memberList.remove(member);
|
||||
}
|
||||
|
||||
public function hasMember(name:String):Bool
|
||||
|
||||
public function hasMember(name:String):Bool
|
||||
return hasOwnMember(name) || hasSuperField(name);
|
||||
|
||||
function doAddMember(m:Member, ?front:Bool = false):Member {
|
||||
init();
|
||||
|
||||
if (m.name == 'new')
|
||||
|
||||
if (m.name == 'new')
|
||||
throw 'Constructor must not be registered as ordinary member';
|
||||
|
||||
//if (hasOwnMember(m.name))
|
||||
|
||||
//if (hasOwnMember(m.name))
|
||||
//m.pos.error('duplicate member declaration ' + m.name);
|
||||
|
||||
if (front)
|
||||
|
||||
if (front)
|
||||
memberList.unshift(m);
|
||||
else
|
||||
memberList.push(m);
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
public function addMember(m:Member, ?front:Bool = false):Member {
|
||||
doAddMember(m, front);
|
||||
|
||||
if (!m.isStatic && hasSuperField(m.name))
|
||||
m.overrides = true;
|
||||
|
||||
else
|
||||
memberList.push(m);
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
|
||||
public function addMembers(td:TypeDefinition):Array<Member> {
|
||||
for (f in td.fields)
|
||||
addMember(f);
|
||||
return td.fields;
|
||||
}
|
||||
|
||||
public function addMember(m:Member, ?front:Bool = false):Member {
|
||||
doAddMember(m, front);
|
||||
|
||||
if (!m.isStatic && hasSuperField(m.name))
|
||||
m.overrides = true;
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
static public function run(plugins:Array<ClassBuilder->Void>, ?verbose) {
|
||||
var builder = new ClassBuilder();
|
||||
for (p in plugins)
|
||||
|
@@ -62,6 +62,10 @@ class Constructor {
|
||||
default: [].toBlock();
|
||||
}
|
||||
}
|
||||
|
||||
public function getArgList():Array<FunctionArg>
|
||||
return beforeArgs.concat(args).concat(afterArgs);
|
||||
|
||||
public function addStatement(e:Expr, ?prepend)
|
||||
if (prepend)
|
||||
this.nuStatements.unshift(e)
|
||||
@@ -96,7 +100,7 @@ class Constructor {
|
||||
case Success(member): member.addMeta(':isVar');
|
||||
default:
|
||||
}
|
||||
addStatement(macro @:pos(pos) (cast this).$name = if (true) $e else this.$name, options.prepend);//TODO: this seems to report type errors here rather than at the declaration position
|
||||
addStatement(macro @:pos(pos) (cast this).$name = if (false) this.$name else $e, options.prepend);//TODO: this seems to report type errors here rather than at the declaration position
|
||||
}
|
||||
else
|
||||
addStatement(macro @:pos(pos) this.$name = $e, options.prepend);
|
||||
@@ -132,4 +136,4 @@ class Constructor {
|
||||
meta : this.meta,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -241,7 +241,7 @@ class Exprs {
|
||||
}
|
||||
|
||||
static public inline function iterate(target:Expr, body:Expr, ?loopVar:String = 'i', ?pos:Position)
|
||||
return EFor(EIn(loopVar.resolve(pos), target).at(pos), body).at(pos);
|
||||
return macro @:pos(pos.sanitize()) for ($i{loopVar} in $target) $body;
|
||||
|
||||
static public function toFields(object:Dynamic<Expr>, ?pos:Position)
|
||||
return EObjectDecl([for (field in Reflect.fields(object))
|
||||
@@ -311,17 +311,45 @@ class Exprs {
|
||||
|
||||
static public inline function resolve(s:String, ?pos)
|
||||
return drill(s.split('.'), pos);
|
||||
|
||||
static var scopes = new Array<Array<Var>>();
|
||||
|
||||
static function inScope<T>(a:Array<Var>, f:Void->T) {
|
||||
scopes.push(a);
|
||||
|
||||
inline function leave()
|
||||
scopes.pop();
|
||||
try {
|
||||
var ret = f();
|
||||
leave();
|
||||
return ret;
|
||||
}
|
||||
catch (e:Dynamic) {
|
||||
leave();
|
||||
return Error.rethrow(e);
|
||||
}
|
||||
}
|
||||
|
||||
static public function scoped<T>(f:Void->T)
|
||||
return inScope([], f);
|
||||
|
||||
static public function inSubScope<T>(f:Void->T, a:Array<Var>)
|
||||
return inScope(switch scopes[scopes.length - 1] {
|
||||
case null: a;
|
||||
case v: v.concat(a);
|
||||
}, f);
|
||||
|
||||
static public function typeof(expr:Expr, ?locals)
|
||||
return
|
||||
try {
|
||||
if (locals == null)
|
||||
locals = scopes[scopes.length - 1];
|
||||
if (locals != null)
|
||||
expr = [EVars(locals).at(expr.pos), expr].toMBlock(expr.pos);
|
||||
Success(Context.typeof(expr));
|
||||
}
|
||||
catch (e:Error) {
|
||||
var m:Dynamic = e.message;
|
||||
e.pos.makeFailure(m);
|
||||
catch (e:haxe.macro.Error) {
|
||||
e.pos.makeFailure(e.message);
|
||||
}
|
||||
catch (e:Dynamic) {
|
||||
expr.pos.makeFailure(e);
|
||||
|
@@ -12,7 +12,7 @@ class Positions {
|
||||
return
|
||||
switch outcome {
|
||||
case Success(d): d;
|
||||
case Failure(f): sanitize(pos).error(f);
|
||||
case Failure(f): pos.error(f);
|
||||
}
|
||||
|
||||
static public function makeBlankType(pos:Position):ComplexType
|
||||
@@ -32,7 +32,17 @@ class Positions {
|
||||
|
||||
|
||||
static public function error(pos:Position, error:Dynamic):Dynamic
|
||||
return errorFunc(sanitize(pos), Std.string(error));
|
||||
return
|
||||
switch Std.instance(error, tink.core.Error) {
|
||||
case null: errorFunc(sanitize(pos), Std.string(error));
|
||||
case error:
|
||||
errorFunc(
|
||||
error.pos,
|
||||
error.message +
|
||||
if (pos == null) ''
|
||||
else ' (referenced from ' + Std.string(pos).substr(5)
|
||||
);
|
||||
}
|
||||
|
||||
static function contextError(pos:Position, error:String):Dynamic
|
||||
return Context.fatalError(error, pos);
|
||||
|
@@ -17,6 +17,7 @@ class Sisyphus {
|
||||
case AccCall: getOrSet;
|
||||
case AccInline: "default";
|
||||
case AccRequire(_, _): "default";
|
||||
default: throw "not implemented";
|
||||
}
|
||||
}
|
||||
if (cf.params.length == 0) {
|
||||
@@ -52,21 +53,24 @@ class Sisyphus {
|
||||
}
|
||||
}
|
||||
|
||||
public static function toComplexType(type : Null<Type>) : Null<ComplexType> return
|
||||
|
||||
public static function toComplexType(type : Null<Type>) : Null<ComplexType> return {
|
||||
inline function direct()
|
||||
return Types.toComplex(type, { direct: true });
|
||||
switch (type) {
|
||||
case null:
|
||||
null;
|
||||
case TMono(_.get() => t):
|
||||
t == null ? Types.toComplex(type, { direct: true }) : toComplexType(t);
|
||||
case TEnum(_.get().isPrivate => true, _): direct();
|
||||
case TInst(_.get().isPrivate => true, _): direct();
|
||||
case TType(_.get().isPrivate => true, _): direct();
|
||||
case TAbstract(_.get().isPrivate => true, _): direct();
|
||||
case TMono(_): direct();
|
||||
case TEnum(_.get() => baseType, params):
|
||||
TPath(toTypePath(baseType, params));
|
||||
case TInst(_.get() => classType, params):
|
||||
switch (classType.kind) {
|
||||
case KTypeParameter(_):
|
||||
TPath({
|
||||
name: classType.name,
|
||||
pack: [],
|
||||
});
|
||||
direct();//TODO: check if the parameter is in scope, in which case the name can simply be used
|
||||
default:
|
||||
TPath(toTypePath(classType, params));
|
||||
}
|
||||
@@ -92,7 +96,7 @@ class Sisyphus {
|
||||
default:
|
||||
throw "Invalid type";
|
||||
}
|
||||
|
||||
}
|
||||
static function toTypePath(baseType : BaseType, params : Array<Type>) : TypePath return {
|
||||
var module = baseType.module;
|
||||
{
|
||||
@@ -102,4 +106,4 @@ class Sisyphus {
|
||||
params: [ for (t in params) TPType(toComplexType(t)) ],
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -19,10 +19,10 @@ class Types {
|
||||
try {
|
||||
Some(Context.getType(typeName));
|
||||
}
|
||||
catch (e:Dynamic)
|
||||
catch (e:Dynamic)
|
||||
if (Std.string(e) == 'Type not found \'$typeName\'') None;
|
||||
else neko.Lib.rethrow(e);
|
||||
|
||||
else tink.core.Error.rethrow(e);
|
||||
|
||||
static var types = new Map<Int,Void->Type>();
|
||||
static var idCounter = 0;
|
||||
static public function getID(t:Type, ?reduced = true)
|
||||
@@ -49,7 +49,7 @@ class Types {
|
||||
throw 'not implemented';
|
||||
}
|
||||
|
||||
static public function getMeta(type:Type)
|
||||
static public function getMeta(type:Type)
|
||||
return switch type {
|
||||
case TInst(_.get().meta => m, _): [m];
|
||||
case TEnum(_.get().meta => m, _): [m];
|
||||
@@ -94,7 +94,7 @@ class Types {
|
||||
fieldsCache.remove(id);//TODO: find a proper solution to avoid stale cache
|
||||
ret;
|
||||
case TAnonymous(anon): Success(anon.get().fields);
|
||||
default: Context.currentPos().makeFailure('type has no fields');
|
||||
default: Context.currentPos().makeFailure('type $t has no fields');
|
||||
}
|
||||
|
||||
static public function getStatics(t:Type)
|
||||
@@ -183,14 +183,14 @@ class Types {
|
||||
params : [TPExpr(register(f).toExpr())],
|
||||
sub : null,
|
||||
});
|
||||
|
||||
|
||||
static function resolveDirectType()
|
||||
return
|
||||
return
|
||||
switch reduce(Context.getLocalType()) {
|
||||
case TInst(_, [TInst(_.get() => { kind: KExpr(e) }, _)]):
|
||||
case TInst(_, [TInst(_.get() => { kind: KExpr(e) }, _)]):
|
||||
types[e.getInt().sure()]();//When using compiler server, this call throws on occasion, in which case modifying this file (to update mtime and invalidate the cache) will solve the problem
|
||||
default:
|
||||
default:
|
||||
throw 'assert';
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user