This commit is contained in:
Juraj Kirchheim
2016-11-05 16:26:59 +01:00
parent ba5349f55f
commit abbb9e5a4c
2 changed files with 75 additions and 25 deletions

View File

@@ -19,7 +19,6 @@ typedef Unops = tink.macro.Ops.Unary;
typedef MacroOutcome<D, F> = tink.core.Outcome<D, F>; typedef MacroOutcome<D, F> = tink.core.Outcome<D, F>;
typedef MacroOutcomeTools = tink.OutcomeTools; typedef MacroOutcomeTools = tink.OutcomeTools;
typedef Option<T> = haxe.ds.Option<T>;
typedef Member = tink.macro.Member; typedef Member = tink.macro.Member;
typedef Constructor = tink.macro.Constructor; typedef Constructor = tink.macro.Constructor;

View File

@@ -5,8 +5,15 @@ import haxe.macro.Expr;
import haxe.macro.Type; import haxe.macro.Type;
import tink.macro.TypeMap; import tink.macro.TypeMap;
using haxe.macro.ComplexTypeTools; using haxe.macro.Tools;
using haxe.macro.TypeTools;
typedef BuildContextN = {
pos:Position,
types:Array<Type>,
usings:Array<TypePath>,
name:String,
}
typedef BuildContext = { typedef BuildContext = {
pos:Position, pos:Position,
@@ -25,20 +32,7 @@ typedef BuildContext3 = {>BuildContext2,
class BuildCache { 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) { static public function getType3(name, ?types, ?pos:Position, build:BuildContext3->TypeDefinition) {
if (types == null) 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) { static public function getType2(name, ?types, ?pos:Position, build:BuildContext2->TypeDefinition) {
if (types == null) if (types == null)
switch Context.getLocalType() { switch Context.getLocalType() {
@@ -103,14 +124,32 @@ class BuildCache {
var forName = var forName =
switch cache[name] { switch cache[name] {
case null: cache[name] = new TypeMap(); case null: cache[name] = new Group(name);
case v: v; case v: v;
} }
if (!forName.exists(type)) { return forName.get(type, pos, build);
var path = '$name${Lambda.count(forName)}', }
usings = []; }
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({ var def = build({
pos: pos, pos: pos,
type: type, type: type,
@@ -119,9 +158,21 @@ class BuildCache {
}); });
Context.defineModule(path, [def], usings); 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);
}
}
} }
} }