Compare commits

...

21 Commits

Author SHA1 Message Date
Juraj Kirchheim
56fe8d7346 Release 0.23.0 2021-04-02 10:09:41 +02:00
Juraj Kirchheim
7e2dfad607 Make TypeMap work exacly with anons. 2021-03-14 13:25:20 +01:00
Juraj Kirchheim
94d02c91a8 Haxe 3 support. 2021-03-14 12:59:38 +01:00
Juraj Kirchheim
99a1fc3fa6 Exact type to string conversion. 2021-03-14 12:58:10 +01:00
Juraj Kirchheim
a383f7692b Argh ... 2021-02-25 11:37:04 +01:00
Juraj Kirchheim
5e5781e8bc Faster intersection for haxe4. 2021-02-25 11:36:26 +01:00
Juraj Kirchheim
63ce1853f5 Release 0.22.0 2021-02-22 11:46:09 +01:00
Juraj Kirchheim
f3ddaa6496 Merge pull request #33 from haxetink/deduce_common_type
Deduce common base type from a list of types
2020-12-23 09:32:31 +01:00
Kevin Leung
f46e49ce66 Fix haxe3 2020-12-22 17:15:28 +08:00
Kevin Leung
007c73d58e Deduce common base type from a list of types 2020-12-22 16:56:45 +08:00
Juraj Kirchheim
8c5903833c Merge pull request #32 from haxetink/isAbstract
haxe 4.2+ fix
2020-12-20 09:21:31 +01:00
Dan Korostelev
59d7407d1b haxe 4.2+ fix 2020-12-19 22:58:31 +01:00
Juraj Kirchheim
0680220a77 Release 0.21.1 2020-08-20 13:09:07 +02:00
Juraj Kirchheim
acaedc170a Fix issue with build field retrieval. 2020-08-20 13:08:28 +02:00
Juraj Kirchheim
8b9dbba624 Release 0.21.0 2020-08-20 12:47:53 +02:00
Juraj Kirchheim
f9348d4a46 Release 0.20.2 2020-08-20 12:46:40 +02:00
Juraj Kirchheim
6f4e6b9227 Workaround https://github.com/HaxeFoundation/haxe/issues/9853 2020-08-20 12:45:33 +02:00
Juraj Kirchheim
59135d5cea Release 0.20.1 2020-08-12 23:14:38 +02:00
Juraj Kirchheim
4accf55b41 Avoid messing with typing order. 2020-08-12 23:10:43 +02:00
Juraj Kirchheim
fd3b01ef0f Release 0.20.0 2020-08-12 14:25:07 +02:00
Juraj Kirchheim
5825c2b617 Add TypedExpr helpers. 2020-08-12 14:22:00 +02:00
12 changed files with 283 additions and 49 deletions

View File

@@ -9,8 +9,8 @@
"contributors": [
"back2dos"
],
"version": "0.19.3",
"releasenote": "Fix issues in ComplexType -> Type conversion for functions.",
"version": "0.23.0",
"releasenote": "Make TypeMap work exacly with anons.",
"tags": [
"tink",
"macro",

View File

@@ -8,6 +8,8 @@ using StringTools;
typedef Positions = tink.macro.Positions;
typedef ExprTools = haxe.macro.ExprTools;
typedef TypedExprTools = haxe.macro.TypedExprTools;
typedef TypedExprs = tink.macro.TypedExprs;
typedef Exprs = tink.macro.Exprs;
typedef Functions = tink.macro.Functions;
typedef Metadatas = tink.macro.Metadatas;
@@ -28,11 +30,11 @@ typedef ClassBuilder = tink.macro.ClassBuilder;
typedef TypeResolution = Ref<Either<String, TypeDefinition>>;
class MacroApi {
static var MAIN_CANDIDATES = ['-main', '-x', '--run'];
static public function getMainClass():Option<String> {
var args = Sys.args();
for (c in MAIN_CANDIDATES)
switch args.indexOf(c) {
case -1:
@@ -42,19 +44,26 @@ class MacroApi {
return None;
}
@:persistent static var idCounter = 0;
@:persistent static var idCounter = 0;
@:noUsing static public inline function tempName(?prefix:String = 'tmp'):String
return '__tink_' + prefix + Std.string(idCounter++);
static public function pos()
static public function pos()
return haxe.macro.Context.currentPos();
static public var completionPoint(default, null):Option<{
var file(default, never):String;
var content(default, never):Null<String>;
var pos(default, never):Int;
}>;
static public function getBuildFields():Option<Array<haxe.macro.Expr.Field>>
return switch completionPoint {
case Some(v) if (v.content != null && (v.content.charAt(v.pos - 1) == '@' || (v.content.charAt(v.pos - 1) == ':' && v.content.charAt(v.pos - 2) == '@'))): None;
default: Some(haxe.macro.Context.getBuildFields());
}
static public var args(default, null):Iterable<String>;
static var initialized = initArgs();
@@ -63,7 +72,7 @@ class MacroApi {
args = sysArgs;
completionPoint = switch sysArgs.indexOf('--display') {
case -1: None;
case sysArgs[_ + 1] => arg:
case sysArgs[_ + 1] => arg:
if (arg.startsWith('{"jsonrpc":')) {
var payload:{
jsonrpc:String,
@@ -71,12 +80,15 @@ class MacroApi {
params:{
file:String,
offset:Int,
contents:String,
}
} = haxe.Json.parse(arg);
switch payload {
case { jsonrpc: '2.0', method: 'display/completion' }:
Some({
file: payload.params.file,
content: payload.params.contents,
pos: payload.params.offset,
});
default: None;
@@ -108,26 +120,26 @@ class MacroApi {
@:forward
abstract ObjectField(F) to F {
static var QUOTED = "@$__hx__";
static var QUOTED = "@$__hx__";
inline function new(o) this = o;
public var field(get, never):String;
function get_field()
return
if (quotes == Quoted)
return
if (quotes == Quoted)
this.field.substr(QUOTED.length);
else this.field;
public var quotes(get, never):QuoteStatus;
function get_quotes()
return if (StringTools.startsWith(this.field, QUOTED)) Quoted else Unquoted;
@:from static function ofFull(o:{>F, quotes:QuoteStatus }):ObjectField
return switch o.quotes {
case null | Unquoted:
case null | Unquoted:
new ObjectField({ field: o.field, expr: o.expr });
default:
new ObjectField({ field: QUOTED + o.field, expr: o.expr });

View File

@@ -23,7 +23,10 @@ class ClassBuilder {
target = Context.getLocalClass().get();
if (fields == null)
fields = Context.getBuildFields();
fields = switch MacroApi.getBuildFields() {
case None: target.pos.error('Impossible to get builds fields now. Possible cause: https://github.com/HaxeFoundation/haxe/issues/9853');
case Some(v): v;
}
this.initializeFrom = fields;
this.target = target;
@@ -192,10 +195,14 @@ class ClassBuilder {
return m;
}
static public function run(plugins:Array<ClassBuilder->Void>, ?verbose) {
var builder = new ClassBuilder();
for (p in plugins)
p(builder);
return builder.export(verbose);
}
static public function run(plugins:Array<ClassBuilder->Void>, ?verbose)
return switch MacroApi.getBuildFields() {
case None: null;
case Some(fields):
var builder = new ClassBuilder(fields);
for (p in plugins)
p(builder);
return builder.export(verbose);
}
}

View File

@@ -2,6 +2,8 @@ package tink.macro;
import haxe.macro.Expr;
import haxe.macro.Type;
using haxe.macro.Tools;
using tink.MacroApi;
class Sisyphus {
@@ -61,8 +63,7 @@ class Sisyphus {
}
}
public static function toComplexType(type : Null<Type>) : Null<ComplexType> return {
static public function toComplexType(type : Null<Type>) : Null<ComplexType> return {
inline function direct()
return Types.toComplex(type, { direct: true });
switch (type) {
@@ -136,4 +137,89 @@ class Sisyphus {
}],
}
}
static function exactBase<T:BaseType>(r:Ref<T>, params:Array<Type>) {
var t = r.get();
var isMain = !t.isPrivate && switch t.pack {
case []: t.module == t.name || t.module == 'StdTypes';
default: StringTools.endsWith(t.module, '.${t.name}');
}
return (
if (isMain) t.pack.concat([t.name]).join('.')
else t.module + '.' + t.name
) + switch params {
case []: '';
case params:
'<${params.map(toExactString).join(', ')}>';
}
}
static inline function isFinal(c:ClassField)
return #if haxe4 c.isFinal #else false #end;
static function exactAnonField(c:ClassField) {
var kw =
switch c.kind {
case FMethod(_): 'function';
case FVar(_):
if (isFinal(c)) 'final' else 'var';
}
return [for (m in c.meta.get()) m.toString() + ' '].join('') + '$kw ${c.name}' + (switch c.kind {
case FVar(read, write):
(
if (isFinal(c) || (read == AccNormal && write == AccNormal)) ''
else '(${read.accessToName()}, ${read.accessToName(false)})'
) + ':' + c.type.toExactString();
case FMethod(_):
switch haxe.macro.Context.follow(c.type) {
case TFun(arg, ret): exactSig(arg, ret, ':');
default: throw 'assert';
}
}) + ';';
}
static function exactSig(args:Array<{name:String, opt:Bool, t:Type}>, ret:Type, sep:String)
return '(${[for (a in args) (if (a.opt) '?' else '') + a.name + ':' + toExactString(a.t)].join(', ')})$sep${toExactString(ret)}';
static public function toExactString(t:Type)
return switch t {
case TMono(t): t.toString();
case TEnum(r, params): exactBase(r, params);
case TInst(r, params): exactBase(r, params);
case TType(r, params): exactBase(r, params);
case TAbstract(r, params): exactBase(r, params);
case TFun(args, ret): exactSig(args, ret, '->');
case TAnonymous(a): '{ ${[for (f in a.get().fields) exactAnonField(f)].join(' ')} }';
case TDynamic(null): 'Dynamic';
case TDynamic(t): 'Dynamic<${toExactString(t)}>';
case TLazy(f): toExactString(f());
}
static function eager(t:Type)
return switch t {
case TLazy(f): eager(f());
default: t;
}
static public function compare(t1:Type, t2:Type, ?follow:Bool = true) {
if (follow) {
t1 = t1.reduce();
t2 = t2.reduce();
}
else {
t1 = eager(t1);
t2 = eager(t2);
}
return switch t1.getIndex() - t2.getIndex() {
case 0:
switch Reflect.compare(t1.toString(), t2.toString()) {
case 0: Reflect.compare(t1.toExactString(), t2.toExactString());
case v: v;
}
case v: v;
}
}
}

View File

@@ -0,0 +1,55 @@
package tink.macro;
import haxe.macro.Type;
import haxe.ds.Option;
using haxe.macro.Tools;
class TypedExprs {
static public function extractAll<T>(t:TypedExpr, f:TypedExpr->Option<T>):Array<T> {
var out = [];
function rec(t:TypedExpr)
if (t != null) {
switch f(t) {
case Some(v): out.push(v);
default:
}
t.iter(rec);
}
rec(t);
return out;
}
static public function extract<T>(t:TypedExpr, f:TypedExpr->Option<T>):Option<T> {
try extractAll(t, function (t) {
var ret = f(t);
if (ret != None)
throw ret;
return ret;
})
catch (e:Option<Dynamic>) return cast e;
return None;
}
static public function isThis(t:TypedExpr):Bool
return switch t {
case null: false;
case { expr: TConst(TThis) | TLocal({ name: '`this' })}: true;
default: false;
}
static public inline function hasThis(t)
return contains(t, isThis);
static public function findAll(t:TypedExpr, f:TypedExpr->Bool):Array<TypedExpr>
return extractAll(t, collect(f));
static public function find(t:TypedExpr, f:TypedExpr->Bool):Option<TypedExpr>
return extract(t, collect(f));
static public function contains(t:TypedExpr, f:TypedExpr->Bool):Bool
return find(t, f) != None;
static inline function collect(f)
return function (t) return if (f(t)) Some(t) else None;
}

View File

@@ -10,6 +10,8 @@ using haxe.macro.Tools;
using tink.MacroApi;
using tink.CoreApi;
import haxe.macro.Type.Ref;
class Types {
static public function definedType(typeName:String)
@@ -172,6 +174,9 @@ class Types {
#if haxe4
isExtern: field.isExtern,
isFinal: field.isFinal,
#if (haxe >= version("4.2.0-rc.1"))
isAbstract: field.isAbstract,
#end
#end
}:ClassField)
]);
@@ -205,7 +210,22 @@ class Types {
default: Failure('type "$t" has no position');
}
static public function deduceCommonType(types:Array<Type>):Outcome<Type, Error> {
var exprs = types.map(function(t) {
var ct = t.toComplex();
return macro (null:$ct);
});
return switch (macro $a{exprs}).typeof() {
case Success(TInst(_, [v])): Success(v);
case Success(_): throw 'unreachable';
case Failure(e): Failure(new Error('Unable to deduce common type among $types'));
}
}
/// like haxe.macro.TypeTools.toString, but not lossy
static public function toExactString(t:Type)
return Sisyphus.toExactString(t);
static public function toString(t:ComplexType)
return new Printer().printComplexType(t);
@@ -258,8 +278,7 @@ class Types {
return if (once) t else reduce(t, false);
return switch type {
case TAbstract(_.get() => { name: 'Null', pack: [] }, [t]): rec(t);
case TLazy(f): rec(f());
case TType(_, _): rec(Context.follow(type, once));
case TLazy(_) | TType(_): rec(Context.follow(type, once));
default: type;
}
}
@@ -294,6 +313,9 @@ class Types {
if (types.length == 1) return Success(types[1]);
#if haxe4
return Success(TIntersection(types));
#end
var paths = [],
fields = [];
@@ -335,18 +357,8 @@ class Types {
throw 'assert';
}
static public function compare(t1:Type, t2:Type, ?follow:Bool = true) {
if (follow) {
t1 = t1.reduce();
t2 = t2.reduce();
}
return switch t1.getIndex() - t2.getIndex() {
case 0:
Reflect.compare(t1.toString(), t2.toString());//much to my surprise, this actually seems to work (at least with 3.4)
case v: v;
}
}
static public function compare(t1:Type, t2:Type, ?follow:Bool = true)
return Sisyphus.compare(t1, t2, follow);
static var SUGGESTIONS = ~/ \(Suggestions?: .*\)$/;

8
tests/Dummy.hx Normal file
View File

@@ -0,0 +1,8 @@
class Dummy {
public function new() {}
static public var p(default, never) = new Private();
}
private class Private {
public function new() {}
}

21
tests/ExactStrings.hx Normal file
View File

@@ -0,0 +1,21 @@
import haxe.macro.Context.typeof;
using tink.MacroApi;
class ExactStrings extends Base {
function test() {
function expect(s:String, e, ?pos)
assertEquals(s, typeof(e).toExactString(), pos);
expect('Dummy', macro new Dummy());
expect('nested.Dummy', macro new nested.Dummy());
expect('Dummy.Private', macro Dummy.p);
expect('nested.Dummy.Private', macro nested.Dummy.p);
expect('{ @foo var x:Int; }', macro (null:{@foo var x:Int;}));
expect('{ @foo @bar var x:Int; }', macro (null:{@foo @bar var x:Int;}));
expect('{ @bar @foo var x:Int; }', macro (null:{@bar @foo var x:Int;}));// not 100% sure this is always the best choice, but let's roll with it
expect('{ @bar var x:Int; }', macro (null:{@bar var x:Int;}));
expect('{ var x:Int; var y:Int; }', macro (null:{x:Int,y:Int}));
expect('{ var x:Int; var y:Int; }', macro (null:{y:Int,x:Int}));
expect('{ function foo(x:Int, ?y:Int):Void; }', macro (null:{ function foo(x:Int, ?y:Int):Void; }));
}
}

View File

@@ -1,6 +1,9 @@
package ;
import haxe.unit.*;
#if macro
using haxe.macro.Tools;
#end
class Run {
#if !macro
@@ -14,6 +17,7 @@ class Run {
new TypeMapTest(),
new Functions(),
new Misc(),
new ExactStrings(),
];
#end
macro static function test() {

View File

@@ -11,6 +11,8 @@ class TypeMapTest extends TestCase {
var t = new TypeMap();
var t1 = (macro [{ foo: [{ bar: '5' }]}]).typeof().sure();
var t2 = (macro [{ foo: [{ bar: 5 }]}]).typeof().sure();
var t3 = (macro [{ foo: [{ bar: 5 }]}]).typeof().sure();
var t4 = (macro [{ foo: [({ bar: 5 }:{ @foo var bar:Int; })]}]).typeof().sure();
t.set(t1, 0);
assertEquals(Lambda.count(t), 1);
@@ -19,7 +21,10 @@ class TypeMapTest extends TestCase {
t.set(t1, 2);
assertEquals(Lambda.count(t), 2);
t.set(t2, 3);
t.set(t3, 3);
assertEquals(Lambda.count(t), 2);
t.set(t4, 4);
assertEquals(Lambda.count(t), 3);
assertEquals(t.get(t1), 2);
assertEquals(t.get(t2), 3);

View File

@@ -10,36 +10,35 @@ using tink.MacroApi;
class Types extends Base {
function type(c:ComplexType)
return c.toType().sure();
function resolve(type:String)
return Context.getType(type);
inline function assertSuccess<S, F>(o:Outcome<S, F>)
assertTrue(o.isSuccess());
inline function assertFailure<S, F>(o:Outcome<S, F>)
assertFalse(o.isSuccess());
function testIs() {
assertSuccess(resolve('Int').isSubTypeOf(resolve('Float')));
assertFailure(resolve('Float').isSubTypeOf(resolve('Int')));
}
}
function testFields() {
var expected = type(macro : Void -> Iterator<Arrayish>),
iterator = type(macro : haxe.ds.StringMap<Arrayish>).getFields(true).sure().filter(function (c) return c.name == 'iterator')[0];
assertSuccess(iterator.type.isSubTypeOf(expected));
assertSuccess(expected.isSubTypeOf(iterator.type));
}
function testConvert() {
assertSuccess((macro : Int).toType());
assertFailure((macro : Tni).toType());
function blank()
return type(MacroApi.pos().makeBlankType());
var bool = type(macro : Bool);
assertTrue(blank().isSubTypeOf(bool).isSuccess());
assertTrue(bool.isSubTypeOf(blank()).isSuccess());
@@ -65,5 +64,20 @@ class Types extends Base {
assertEquals('String', Context.getType('String').toComplex().toString());
assertEquals('tink.CoreApi.Noise', Context.getType('tink.CoreApi.Noise').toComplex().toString());
}
function testDeduceCommonType() {
function ct2t(ct:ComplexType) return ct.toType().sure();
assertEquals('StdTypes.Float', tink.macro.Types.deduceCommonType([(macro:Float), (macro:Int)].map(ct2t)).sure().toComplex().toString());
assertEquals('Types.CommonI1', tink.macro.Types.deduceCommonType([(macro:Types.CommonA), (macro:Types.CommonB), (macro:Types.CommonC)].map(ct2t)).sure().toComplex().toString());
assertEquals('Types.CommonI2', tink.macro.Types.deduceCommonType([(macro:Types.CommonB), (macro:Types.CommonC)].map(ct2t)).sure().toComplex().toString());
// assertEquals('Types.CommonI3', tink.macro.Types.deduceCommonType([(macro:Types.CommonC)].map(ct2t)).sure().toComplex().toString());
}
}
#end
#end
interface CommonI1 {}
interface CommonI2 {}
interface CommonI3 {}
class CommonA implements CommonI1 {}
class CommonB implements CommonI2 implements CommonI1 {}
class CommonC implements CommonI3 implements CommonI2 implements CommonI1 {}

10
tests/nested/Dummy.hx Normal file
View File

@@ -0,0 +1,10 @@
package nested;
class Dummy {
public function new() {}
static public var p(default, never) = new Private();
}
private class Private {
public function new() {}
}