Compare commits

...

16 Commits

Author SHA1 Message Date
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
Juraj Kirchheim
328a8476a5 Release 0.19.3 2020-06-26 15:26:51 +02:00
Juraj Kirchheim
1589665652 Fix haxe 3. 2020-06-26 15:24:00 +02:00
Juraj Kirchheim
08f15a6739 Release 0.19.2 2020-06-26 15:09:56 +02:00
Juraj Kirchheim
b5e992b820 Fix issues in ComplexType -> Type conversion for functions. 2020-06-26 15:09:21 +02:00
Juraj Kirchheim
8c50dcf04b Release 0.19.1 2020-04-29 20:58:00 +02:00
Juraj Kirchheim
4bc63c6eb0 Resolves #23. 2020-04-24 15:55:39 +02:00
Juraj Kirchheim
acb7237ae4 Resolves #12. Resolves #18. 2020-04-24 15:38:51 +02:00
Juraj Kirchheim
e3bac681ce Allow for typemaps that reduce everything except null. 2020-04-24 15:29:24 +02:00
Juraj Kirchheim
34516086c9 Update setup. 2020-04-24 14:42:55 +02:00
15 changed files with 233 additions and 115 deletions

View File

@@ -15,14 +15,14 @@ os:
env:
- HAXE_VERSION=3.4.7
- HAXE_VERSION=edge
install:
- npm i -g lix@15.5.4
- lix install haxe $HAXE_VERSION
- lix download
script:
- lix run travix node -lib hx3compat
- lix run travix node
jobs:
include:

View File

@@ -1,3 +1,3 @@
-D hx3compat=1.0.0
# @install: lix --silent download "haxelib:/hx3compat#1.0.0" into hx3compat/1.0.0/haxelib
-cp ${HAXE_LIBCACHE}/hx3compat/1.0.0/haxelib/std
# @install: lix --silent download "haxelib:/hx3compat#1.0.3" into hx3compat/1.0.3/haxelib
-cp ${HAXE_LIBCACHE}/hx3compat/1.0.3/haxelib/std
-D hx3compat=1.0.3

View File

@@ -1,6 +1,6 @@
# @install: lix --silent download "haxelib:hxnodejs#4.0.9" into hxnodejs/4.0.9/haxelib
-D hxnodejs=4.0.9
-cp ${HAXESHIM_LIBCACHE}/hxnodejs/4.0.9/haxelib/src
-D nodejs
# @install: lix --silent download "haxelib:/hxnodejs#12.0.0" into hxnodejs/12.0.0/haxelib
-cp ${HAXE_LIBCACHE}/hxnodejs/12.0.0/haxelib/src
-D hxnodejs=12.0.0
--macro allowPackage('sys')
--macro _hxnodejs.VersionWarning.include()
# should behave like other target defines and not be defined in macro context
--macro define('nodejs')

View File

@@ -1,20 +1,20 @@
{
"name": "tink_macro",
"license": "MIT",
"description": "The macro toolkit ;)",
"classPath": "src",
"dependencies": {
"tink_core": ""
},
"url": "https://github.com/haxetink/tink_macro",
"contributors": [
"back2dos"
],
"version": "0.21.0",
"releasenote": "Expose autocompletion contents. Work around compiler metadata completion issues.",
"tags": [
"tink",
"macro",
"utility"
],
"classPath": "src",
"description": "The macro toolkit ;)",
"contributors": [
"back2dos"
],
"releasenote": "Internal refactor: remove reflections",
"version": "0.19.0",
"url": "http://haxetink.org/tink_macro",
"dependencies": {
"tink_core": ""
}
"license": "MIT"
}

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

@@ -31,23 +31,23 @@ typedef BuildContext3 = {>BuildContext2,
type3:Type,
}
class BuildCache {
class BuildCache {
@:persistent static var cache = new Map();
static public function getType3(name, ?types, ?pos:Position, build:BuildContext3->TypeDefinition) {
static public function getType3(name, ?types, ?pos:Position, build:BuildContext3->TypeDefinition, ?normalizer:Type->Type) {
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,
@@ -55,62 +55,62 @@ class BuildCache {
pos: ctx.pos,
name: ctx.name,
usings: ctx.usings
}));
}), normalizer);
}
static public function getTypeN(name, ?types, ?pos:Position, build:BuildContextN->TypeDefinition) {
static public function getTypeN(name, ?types, ?pos:Position, build:BuildContextN->TypeDefinition, ?normalizer:Type->Type) {
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(switch types[i] {
case TInst(_.get().kind => KExpr(e), _):
case TInst(_.get().kind => KExpr(e), _):
TPath('tink.macro.ConstParam'.asTypePath([TPExpr(e)]));
case t: t.toComplex();
}),
}]).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) {
}), normalizer);
}
static public function getType2(name, ?types, ?pos:Position, build:BuildContext2->TypeDefinition, ?normalizer:Type->Type) {
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
}));
}), normalizer);
}
static public function getParams(name:String, ?pos:Position)
static public function getParams(name:String, ?pos:Position)
return
switch Context.getLocalType() {
case TInst(_.toString() == name => true, v):
@@ -119,9 +119,9 @@ class BuildCache {
pos.makeFailure('Expected $name');
case v:
pos.makeFailure('$v should be a class');
}
}
static public function getParam(name:String, ?pos:Position)
static public function getParam(name:String, ?pos:Position)
return
getParams(name, pos)
.flatMap(function (args:Array<Type>) return switch args {
@@ -130,18 +130,18 @@ class BuildCache {
default: pos.makeFailure('too many parameters');
});
static public function getType(name, ?type, ?pos:Position, build:BuildContext->TypeDefinition) {
static public function getType(name, ?type, ?pos:Position, build:BuildContext->TypeDefinition, ?normalizer:Type->Type) {
if (type == null)
type = getParam(name, pos).sure();
var forName =
var forName =
switch cache[name] {
case null: cache[name] = new Group(name);
case null: cache[name] = new Group(name, normalizer);
case v: v;
}
var ret = forName.get(type, pos.sanitize(), build);
var ret = forName.get(type, pos.sanitize(), build);
ret.getFields();// workaround for https://github.com/HaxeFoundation/haxe/issues/7905
return ret;
}
@@ -152,23 +152,24 @@ private typedef Entry = {
}
private class Group {
var name:String;
var counter = 0;
var entries = new TypeMap<Entry>();
public function new(name) {
var entries:TypeMap<Entry>;
public function new(name, ?normalizer) {
this.name = name;
this.entries = new TypeMap(normalizer);
}
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,
pos: pos,
type: type,
usings: usings,
name: path.split('.').pop()
});
@@ -177,15 +178,15 @@ private class Group {
return Context.getType(path);
}
function doMake()
while (true)
function doMake()
while (true)
switch '$name${counter++}' {
case _.definedType() => Some(_):
case v:
return make(v);
}
}
return
return
switch entries.get(type) {
case null:
doMake();

View File

@@ -23,7 +23,10 @@ class ClassBuilder {
target = Context.getLocalClass().get();
if (fields == null)
fields = Context.getBuildFields();
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(_):
}
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

@@ -23,8 +23,8 @@ class Sisyphus {
if (cf.params.length == 0) {
name: cf.name,
doc: cf.doc,
access:
(cf.isPublic ? [ APublic ] : [ APrivate ])
access:
(cf.isPublic ? [ APublic ] : [ APrivate ])
#if haxe4 .concat(if (cf.isFinal) [AFinal] else []) #end
,
kind: switch([ cf.kind, cf.type ]) {
@@ -84,7 +84,7 @@ class Sisyphus {
cl.kind.match(KTypeParameter(_))
&& cl.module == classType.module
&& cl.pack.join('.') == classType.pack.join('.')
): ct;
): ct;
default:
direct();
}
@@ -95,8 +95,12 @@ class Sisyphus {
TPath(toTypePath(baseType, params));
case TFun(args, ret):
TFunction(
[ for (a in args) a.opt ? nullable(toComplexType(a.t)) : toComplexType(a.t) ],
toComplexType(ret));
[for (a in args) {
var t = #if haxe4 TNamed(a.name, #else ( #end toComplexType(a.t));
if (a.opt) TOptional(t) else t;
}],
toComplexType(ret)
);
case TAnonymous(_.get() => { fields: fields }):
TAnonymous([ for (cf in fields) toField(cf) ]);
case TDynamic(t):
@@ -121,7 +125,7 @@ class Sisyphus {
case _ == name => true: null;
case v: v;
}
{
pack: baseType.pack,
name: name,

View File

@@ -8,14 +8,33 @@ import haxe.macro.Type;
using tink.MacroApi;
class TypeMap<V> extends BalancedTree<Type, V> implements IMap<Type, V> {
var follow:Bool;
public function new(?noFollow:Bool) {
this.follow = noFollow != true;
var normalize:Type->Type;
public function new(?normalize:Type->Type) {
this.normalize = switch normalize {
case null: function (t) return t.reduce();
case fn: fn;
}
super();
}
override function compare(k1:Type, k2:Type):Int
return k1.compare(k2, follow);
return normalize(k1).compare(normalize(k2), false);
static public function noFollow(t:Type)
return t;
static public function keepNull(t:Type):Type
return switch t {
case TAbstract(_.get() => { name: 'Null', pack: [] }, [t])
#if !haxe4 | TType(_.get() => { name: 'Null', pack: []}, [t]) #end
:
var ct = keepNull(t).toComplex({ direct: true });
(macro : Null<$ct>).toType().sure();
case TLazy(f): keepNull(f());
case TType(_, _): keepNull(Context.follow(t, true));
default: t;
}
}

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

@@ -258,8 +258,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;
}
}

View File

@@ -1,2 +1,3 @@
-cp tests
-lib hx3compat
-main Run

1
tests/MyString.hx Normal file
View File

@@ -0,0 +1 @@
typedef MyString = String;

View File

@@ -3,8 +3,8 @@ package ;
import haxe.unit.*;
class Run {
#if !macro
static function main()
#if !macro
static function main()
test();//It compiles ...
#else
static var cases:Array<TestCase> = [
@@ -19,12 +19,13 @@ class Run {
macro static function test() {
var runner = new TestRunner();
tink.macro.ClassBuilder;
tink.macro.BuildCache;
for (c in cases)
runner.add(c);
runner.run();
if (!runner.result.success)
haxe.macro.Context.error(runner.result.toString(), haxe.macro.Context.currentPos());
return macro {
trace('Let\'s ship it!');
}

View File

@@ -1,17 +1,17 @@
package;
import haxe.unit.TestCase;
import haxe.macro.Expr;
import tink.macro.TypeMap;
using haxe.macro.Context;
using tink.MacroApi;
class TypeMapTest extends TestCase {
function testMap() {
var t = new TypeMap();
var t1 = (macro [{ foo: [{ bar: '5' }]}]).typeof();
var t2 = (macro [{ foo: [{ bar: 5 }]}]).typeof();
var t1 = (macro [{ foo: [{ bar: '5' }]}]).typeof().sure();
var t2 = (macro [{ foo: [{ bar: 5 }]}]).typeof().sure();
t.set(t1, 0);
assertEquals(Lambda.count(t), 1);
t.set(t2, 1);
@@ -20,11 +20,29 @@ class TypeMapTest extends TestCase {
assertEquals(Lambda.count(t), 2);
t.set(t2, 3);
assertEquals(Lambda.count(t), 2);
assertEquals(t.get(t1), 2);
assertEquals(t.get(t2), 3);
assertTrue(true);
}
function testNormalization() {
for (settings in [
{ count: 4, map: new TypeMap(TypeMap.noFollow)},
{ count: 2, map: new TypeMap(TypeMap.keepNull)},
{ count: 1, map: new TypeMap()},
]) {
function set(ct:ComplexType)
settings.map.set(ct.toType().sure(), ct);
set(macro : String);
set(macro : MyString);
set(macro : Null<String>);
set(macro : Null<MyString>);
assertEquals(settings.count, Lambda.count(settings.map));
}
}
}