Compare commits
19 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
27de7bb3e7 | ||
![]() |
371f393299 | ||
![]() |
abbb9e5a4c | ||
![]() |
921ae162c8 | ||
![]() |
fe779872d2 | ||
![]() |
f8ab1c7354 | ||
![]() |
ba5349f55f | ||
![]() |
92f60cbe4f | ||
![]() |
3b822b4cdb | ||
![]() |
f2b670f9e4 | ||
![]() |
ff491fe7e7 | ||
![]() |
7e869cd9c8 | ||
![]() |
983ffa16af | ||
![]() |
4207dea754 | ||
![]() |
3470f1cc47 | ||
![]() |
d84fb38d73 | ||
![]() |
065788c198 | ||
![]() |
810c2f9630 | ||
![]() |
331f186dcd |
@@ -1,4 +1,5 @@
|
||||
# Tinkerbell Macro Library
|
||||
[](https://gitter.im/haxetink/public)
|
||||
|
||||
Explained in current marketing speak, `tink_macro` is *the* macro toolkit ;)
|
||||
|
||||
@@ -412,4 +413,4 @@ Because the state of a constructor is rather delicate, the API prohibits you to
|
||||
|
||||
# TypeMap
|
||||
|
||||
You can find a type map, i.e. a map where the keys are `haxe.macro.Type`, in `tink.macro.TypeMap`. It's pretty much an ordinary map. Currently, it relies rather strongly on [`haxe.macro.TypeTools.toString()`](http://api.haxe.org/haxe/macro/TypeTools.html#toString) and it remains to be determined whether that is a reliable choice. Please report any issues you might face.
|
||||
You can find a type map, i.e. a map where the keys are `haxe.macro.Type`, in `tink.macro.TypeMap`. It's pretty much an ordinary map. Currently, it relies rather strongly on [`haxe.macro.TypeTools.toString()`](http://api.haxe.org/haxe/macro/TypeTools.html#toString) and it remains to be determined whether that is a reliable choice. Please report any issues you might face.
|
||||
|
@@ -11,8 +11,8 @@
|
||||
"contributors": [
|
||||
"back2dos"
|
||||
],
|
||||
"releasenote": "Add build cache utility.",
|
||||
"version": "0.8.0",
|
||||
"releasenote": "Improved build cache.",
|
||||
"version": "0.12.0",
|
||||
"url": "http://haxetink.org/tink_macro",
|
||||
"dependencies": {
|
||||
"tink_core": ""
|
||||
|
@@ -19,8 +19,6 @@ typedef Unops = tink.macro.Ops.Unary;
|
||||
typedef MacroOutcome<D, F> = tink.core.Outcome<D, F>;
|
||||
typedef MacroOutcomeTools = tink.OutcomeTools;
|
||||
|
||||
typedef Option<T> = haxe.ds.Option<T>;
|
||||
|
||||
typedef Member = tink.macro.Member;
|
||||
typedef Constructor = tink.macro.Constructor;
|
||||
typedef ClassBuilder = tink.macro.ClassBuilder;
|
||||
|
@@ -5,24 +5,107 @@ import haxe.macro.Expr;
|
||||
import haxe.macro.Type;
|
||||
import tink.macro.TypeMap;
|
||||
|
||||
using haxe.macro.Tools;
|
||||
|
||||
typedef BuildContextN = {
|
||||
pos:Position,
|
||||
types:Array<Type>,
|
||||
usings:Array<TypePath>,
|
||||
name:String,
|
||||
}
|
||||
|
||||
|
||||
typedef BuildContext = {
|
||||
pos:Position,
|
||||
type:Type,
|
||||
usings:Array<TypePath>,
|
||||
name:String,
|
||||
}
|
||||
|
||||
typedef BuildContext2 = {>BuildContext,
|
||||
type2:Type,
|
||||
}
|
||||
|
||||
typedef BuildContext3 = {>BuildContext2,
|
||||
type3:Type,
|
||||
}
|
||||
|
||||
class BuildCache {
|
||||
|
||||
static var cache = init();
|
||||
static var cache = new Map();
|
||||
|
||||
static function init() {
|
||||
|
||||
function refresh() {
|
||||
cache = new Map();
|
||||
return true;
|
||||
}
|
||||
|
||||
Context.onMacroContextReused(refresh);
|
||||
refresh();
|
||||
|
||||
return cache;
|
||||
static public function getType3(name, ?types, ?pos:Position, build:BuildContext3->TypeDefinition) {
|
||||
if (types == null)
|
||||
switch Context.getLocalType() {
|
||||
case TInst(_.toString() == name => true, [t1, t2, t3]):
|
||||
types = { t1: t1, t2: t2, t3: t3 };
|
||||
default:
|
||||
throw 'assert';
|
||||
}
|
||||
|
||||
var t1 = types.t1.toComplexType(),
|
||||
t2 = types.t2.toComplexType(),
|
||||
t3 = types.t2.toComplexType();
|
||||
|
||||
return getType(name, (macro : { t1: $t1, t2: $t2, t3: $t3 } ).toType(), pos, function (ctx) return build({
|
||||
type: types.t1,
|
||||
type2: types.t2,
|
||||
type3: types.t3,
|
||||
pos: ctx.pos,
|
||||
name: ctx.name,
|
||||
usings: ctx.usings
|
||||
}));
|
||||
}
|
||||
|
||||
static public function getType(name, ?type, ?pos:Position, build:Position->Type->Array<TypePath>->String->TypeDefinition) {
|
||||
static public function getTypeN(name, ?types, ?pos:Position, build:BuildContextN->TypeDefinition) {
|
||||
|
||||
if (pos == null)
|
||||
pos = Context.currentPos();
|
||||
|
||||
if (types == null)
|
||||
switch Context.getLocalType() {
|
||||
case TInst(_.toString() == name => true, params):
|
||||
types = params;
|
||||
default:
|
||||
throw 'assert';
|
||||
}
|
||||
|
||||
var compound = ComplexType.TAnonymous([for (i in 0...types.length) {
|
||||
name: 't$i',
|
||||
pos: pos,
|
||||
kind: FVar(types[i].toComplexType()),
|
||||
}]).toType();
|
||||
|
||||
return getType(name, compound, pos, function (ctx) return build({
|
||||
types: types,
|
||||
pos: ctx.pos,
|
||||
name: ctx.name,
|
||||
usings: ctx.usings
|
||||
}));
|
||||
}
|
||||
|
||||
static public function getType2(name, ?types, ?pos:Position, build:BuildContext2->TypeDefinition) {
|
||||
if (types == null)
|
||||
switch Context.getLocalType() {
|
||||
case TInst(_.toString() == name => true, [t1, t2]):
|
||||
types = { t1: t1, t2: t2 };
|
||||
default:
|
||||
throw 'assert';
|
||||
}
|
||||
|
||||
var t1 = types.t1.toComplexType(),
|
||||
t2 = types.t2.toComplexType();
|
||||
|
||||
return getType(name, (macro : { t1: $t1, t2: $t2 } ).toType(), pos, function (ctx) return build({
|
||||
type: types.t1,
|
||||
type2: types.t2,
|
||||
pos: ctx.pos,
|
||||
name: ctx.name,
|
||||
usings: ctx.usings
|
||||
}));
|
||||
}
|
||||
|
||||
static public function getType(name, ?type, ?pos:Position, build:BuildContext->TypeDefinition) {
|
||||
|
||||
if (pos == null)
|
||||
pos = Context.currentPos();
|
||||
@@ -31,26 +114,65 @@ class BuildCache {
|
||||
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';
|
||||
}
|
||||
|
||||
var forName =
|
||||
switch cache[name] {
|
||||
case null: cache[name] = new TypeMap();
|
||||
case null: cache[name] = new Group(name);
|
||||
case v: v;
|
||||
}
|
||||
|
||||
if (!forName.exists(type)) {
|
||||
var path = '$name${Lambda.count(forName)}',
|
||||
usings = [];
|
||||
|
||||
var def = build(pos, type, usings, path.split('.').pop());
|
||||
|
||||
return forName.get(type, pos, build);
|
||||
}
|
||||
}
|
||||
|
||||
private typedef Entry = {
|
||||
name:String,
|
||||
}
|
||||
|
||||
private class Group {
|
||||
|
||||
var name:String;
|
||||
var counter = 0;
|
||||
var entries = new TypeMap<Entry>();
|
||||
|
||||
public function new(name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public function get(type:Type, pos:Position, build:BuildContext->TypeDefinition):Type {
|
||||
|
||||
function make(path:String) {
|
||||
var usings = [];
|
||||
var def = build({
|
||||
pos: pos,
|
||||
type: type,
|
||||
usings: usings,
|
||||
name: path.split('.').pop()
|
||||
});
|
||||
|
||||
Context.defineModule(path, [def], usings);
|
||||
forName.set(type, Context.getType(path));
|
||||
entries.set(type, { name: path } );
|
||||
return Context.getType(path);
|
||||
}
|
||||
|
||||
return forName.get(type);
|
||||
return
|
||||
switch entries.get(type) {
|
||||
case null:
|
||||
make('$name${counter++}');
|
||||
case v:
|
||||
try {
|
||||
Context.getType(v.name);
|
||||
}
|
||||
catch (e:Dynamic) {
|
||||
make(v.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -55,7 +55,7 @@ class ClassBuilder {
|
||||
init();
|
||||
if (constructor == null)
|
||||
if (fallback != null)
|
||||
new Constructor(this, fallback);
|
||||
constructor = new Constructor(this, fallback);
|
||||
else {
|
||||
var sup = target.superClass;
|
||||
while (sup != null) {
|
||||
|
@@ -96,7 +96,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);
|
||||
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
|
||||
}
|
||||
else
|
||||
addStatement(macro @:pos(pos) this.$name = $e, options.prepend);
|
||||
|
6
src/tink/macro/DirectType.hx
Normal file
6
src/tink/macro/DirectType.hx
Normal file
@@ -0,0 +1,6 @@
|
||||
package tink.macro;
|
||||
|
||||
@:genericBuild(tink.macro.Types.resolveDirectType())
|
||||
class DirectType<Const> {
|
||||
|
||||
}
|
@@ -27,10 +27,10 @@ private class Heureka { public function new() {} }
|
||||
class Exprs {
|
||||
|
||||
static public function has(e:Expr, condition:Expr->Bool, ?options: { ?enterFunctions: Bool }) {
|
||||
var enterFunctions = options != null && options.enterFunctions;
|
||||
var skipFunctions = options == null || options.enterFunctions != true;
|
||||
function seek(e:Expr)
|
||||
switch e {
|
||||
case { expr: EFunction(_) } if (options != null || options.enterFunctions != true):
|
||||
case { expr: EFunction(_) } if (skipFunctions):
|
||||
case _ if (condition(e)): throw new Heureka();
|
||||
default: haxe.macro.ExprTools.iter(e, seek);
|
||||
}
|
||||
@@ -394,4 +394,4 @@ class Exprs {
|
||||
static inline var NOT_A_STRING = "string constant expected";
|
||||
static inline var NOT_A_NAME = "name expected";
|
||||
static inline var NOT_A_FUNCTION = "function expected";
|
||||
}
|
||||
}
|
||||
|
@@ -35,7 +35,7 @@ class Positions {
|
||||
return errorFunc(sanitize(pos), Std.string(error));
|
||||
|
||||
static function contextError(pos:Position, error:String):Dynamic
|
||||
return Context.error(error, pos);
|
||||
return Context.fatalError(error, pos);
|
||||
|
||||
static function abortTypeBuild(pos:Position, error:String):Dynamic
|
||||
return throw new AbortBuild(error, pos);
|
||||
|
@@ -16,8 +16,8 @@ class Types {
|
||||
static var types = new Map<Int,Void->Type>();
|
||||
static var idCounter = 0;
|
||||
|
||||
@:noUsing macro static public function getType(id:Int):Type
|
||||
return types.get(id)();
|
||||
//@:noUsing macro static public function getType(id:Int):Type
|
||||
//return types.get(id)();
|
||||
|
||||
static public function getID(t:Type, ?reduced = true)
|
||||
return
|
||||
@@ -162,9 +162,19 @@ class Types {
|
||||
static public function lazyComplex(f:Void->Type)
|
||||
return
|
||||
TPath({
|
||||
pack : ['haxe','macro'],
|
||||
name : 'MacroType',
|
||||
params : [TPExpr('tink.macro.Types.getType'.resolve().call([register(f).toExpr()]))],
|
||||
pack : ['tink','macro'],
|
||||
name : 'DirectType',
|
||||
params : [TPExpr(register(f).toExpr())],
|
||||
sub : null,
|
||||
});
|
||||
|
||||
static function resolveDirectType()
|
||||
return
|
||||
switch reduce(Context.getLocalType()) {
|
||||
case TInst(_, [TInst(_.get() => { kind: KExpr(e) }, _)]):
|
||||
types[e.getInt().sure()]();
|
||||
default:
|
||||
throw 'assert';
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user