Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
27de7bb3e7 | ||
![]() |
371f393299 | ||
![]() |
abbb9e5a4c | ||
![]() |
921ae162c8 | ||
![]() |
fe779872d2 | ||
![]() |
f8ab1c7354 | ||
![]() |
ba5349f55f |
@@ -11,8 +11,8 @@
|
||||
"contributors": [
|
||||
"back2dos"
|
||||
],
|
||||
"releasenote": "Expand build cache for 2 and 3 type parameters.",
|
||||
"version": "0.10.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,8 +5,15 @@ import haxe.macro.Expr;
|
||||
import haxe.macro.Type;
|
||||
import tink.macro.TypeMap;
|
||||
|
||||
using haxe.macro.ComplexTypeTools;
|
||||
using haxe.macro.TypeTools;
|
||||
using haxe.macro.Tools;
|
||||
|
||||
typedef BuildContextN = {
|
||||
pos:Position,
|
||||
types:Array<Type>,
|
||||
usings:Array<TypePath>,
|
||||
name:String,
|
||||
}
|
||||
|
||||
|
||||
typedef BuildContext = {
|
||||
pos:Position,
|
||||
@@ -25,20 +32,7 @@ typedef BuildContext3 = {>BuildContext2,
|
||||
|
||||
class BuildCache {
|
||||
|
||||
static var cache = init();
|
||||
|
||||
static function init() {
|
||||
|
||||
function refresh() {
|
||||
cache = new Map();
|
||||
return true;
|
||||
}
|
||||
|
||||
Context.onMacroContextReused(refresh);
|
||||
refresh();
|
||||
|
||||
return cache;
|
||||
}
|
||||
static var cache = new Map();
|
||||
|
||||
static public function getType3(name, ?types, ?pos:Position, build:BuildContext3->TypeDefinition) {
|
||||
if (types == null)
|
||||
@@ -63,6 +57,33 @@ class BuildCache {
|
||||
}));
|
||||
}
|
||||
|
||||
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() {
|
||||
@@ -93,20 +114,42 @@ 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 = [];
|
||||
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,
|
||||
@@ -115,9 +158,21 @@ class BuildCache {
|
||||
});
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -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);
|
||||
|
@@ -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);
|
||||
|
Reference in New Issue
Block a user