Compare commits

..

76 Commits

Author SHA1 Message Date
Juraj Kirchheim
f27d2796b1 Release 0.15.0 2017-09-25 10:52:48 +02:00
Juraj Kirchheim
417a92cabc Make it compile for Haxe 4. 2017-09-25 10:50:36 +02:00
Kevin Leung
4457ea7dee readme tweaks 2017-09-21 15:02:16 +08:00
Juraj Kirchheim
209847e5ea Release 0.14.2 2017-09-11 10:36:42 +02:00
Juraj Kirchheim
c94e4a0337 Add way to get constructor args. 2017-09-11 10:35:54 +02:00
Juraj Kirchheim
cc009b2026 Release 0.14.1 2017-08-28 15:02:56 +02:00
Juraj Kirchheim
0a36ee8e2b Merge pull request #16 from ibilon/haxe4
Support for haxe 4 EIn change to EBinop(OpIn)
2017-08-18 16:00:37 +02:00
Juraj Kirchheim
5879773566 Fix typo. 2017-08-18 15:41:31 +02:00
Juraj Kirchheim
58262a1810 Use reification to abstract over Haxe version specific differences. 2017-08-18 15:38:58 +02:00
Valentin Lemière
9ceadeb88f Support for haxe 4 EIn change to EBinop(OpIn) 2017-08-18 15:13:28 +02:00
Juraj Kirchheim
15d75965ad Release 0.14.0 2017-06-05 10:13:08 +02:00
Juraj Kirchheim
c7634e0a29 Just use Error.rethrow 2017-06-01 13:14:39 +02:00
Juraj Kirchheim
49e48bc801 Merge pull request #15 from Simn/eval
don't use neko.Lib.rethrow when not in neko
2017-06-01 13:06:27 +02:00
Simon Krajewski
c150b84f18 don't use neko.Lib.rethrow when not in neko 2017-06-01 13:01:52 +02:00
Kevin Leung
04f86ce341 official gitter badge [ci skip] 2017-05-26 07:59:52 +08:00
Kevin Leung
45380371d8 Add badge [ci skip] 2017-05-19 17:54:35 +08:00
Juraj Kirchheim
d94d946301 Update README.md 2017-05-18 13:43:49 +02:00
Juraj Kirchheim
56b7caaf01 Release 0.13.5 2017-05-06 12:42:01 +02:00
Juraj Kirchheim
73e9da8435 Fix 3.2.1. 2017-04-13 21:07:44 +02:00
Juraj Kirchheim
bf23337923 Release 0.13.4 2017-04-13 21:04:00 +02:00
Juraj Kirchheim
02bd3bf69d Add Types.getMeta. 2017-04-13 21:02:58 +02:00
Juraj Kirchheim
82a3417b97 Gitignore. 2017-04-13 20:58:16 +02:00
Juraj Kirchheim
1f986c3732 VSCode setup. 2017-04-13 20:57:29 +02:00
Kevin Leung
20e998fd12 Release 0.13.3 2017-03-20 20:01:41 +08:00
Kevin Leung
0f9c0d1e97 what the haxe? 2017-03-20 19:49:21 +08:00
Kevin Leung
2ae13d534e my turn 2017-03-20 19:41:16 +08:00
back2dos
65d341f1e1 "push and read travis log" is the best kind of debugging ... 2017-03-20 11:50:32 +01:00
back2dos
68fa50c7c3 Let's see if we can fix that. 2017-03-20 11:32:50 +01:00
back2dos
32aa2d6570 Bump version. 2017-03-20 11:20:59 +01:00
back2dos
d81642e733 Use travix release. 2017-03-20 11:19:49 +01:00
Juraj Kirchheim
d5f4987ddb Release 0.13.1 2017-03-16 16:31:14 +01:00
Juraj Kirchheim
671f1bc995 Merge pull request #11 from haxetink/kevinresol-patch-1
Add TypeMap alias
2017-03-16 16:02:34 +01:00
back2dos
b1012937fc Release 0.13.0 2017-03-06 17:58:24 +01:00
back2dos
bd4b8c436f Build cache works better with compiler server. 2017-03-06 17:58:07 +01:00
back2dos
dbebd0d70d Allow querying whether a type exists. 2017-03-06 17:57:30 +01:00
Juraj Kirchheim
4c523e02a5 Release 0.12.3 2017-03-06 14:31:20 +01:00
Juraj Kirchheim
f39fc3b296 Try to save TMono. 2017-03-06 09:16:31 +01:00
Kevin Leung
e06c40325b Add TypeMap alias 2017-03-04 22:33:13 +08:00
back2dos
7a3914094d Release 0.12.2 2017-03-01 18:26:05 +01:00
back2dos
a597d7fa50 Workaround for https://github.com/HaxeFoundation/haxe/issues/6063 2017-03-01 18:25:21 +01:00
back2dos
3d5626d66f Give up on trying to work around cache problems. 2017-03-01 18:17:42 +01:00
Juraj Kirchheim
88a1ffba0b Release 0.12.1 2017-02-18 19:01:45 +01:00
Juraj Kirchheim
8f81959da9 Cleanup. 2017-02-18 19:00:51 +01:00
Juraj Kirchheim
b20986ecd0 Remove some old trace statements. 2017-02-18 19:00:07 +01:00
Juraj Kirchheim
e55b72b697 Merge branch 'master' of https://github.com/haxetink/tink_macro 2017-02-18 18:40:57 +01:00
Juraj Kirchheim
240ace34ec Try to deal with caching problems. 2017-02-18 18:40:48 +01:00
Juraj Kirchheim
d851094aa3 Update README.md 2016-12-07 06:44:36 +01:00
Juraj Kirchheim
ea16f79090 Merge pull request #8 from kevinresol/concat_block
Add concat()
2016-12-07 06:42:24 +01:00
Kevin Leung
aad311f524 Don't even need an outcome 2016-12-07 10:10:22 +08:00
Kevin Leung
f0d5d80266 Remove unused error message 2016-12-07 10:09:20 +08:00
Kevin Leung
38aa0f4dc4 Concat any exprs 2016-12-07 10:08:47 +08:00
Kevin Leung
f4fb7d63c3 Merge branch 'master' into concat_block 2016-12-07 10:01:38 +08:00
Juraj Kirchheim
27de7bb3e7 Release 0.12.0 2016-11-05 16:28:11 +01:00
Juraj Kirchheim
371f393299 Merge branch 'master' of https://github.com/haxetink/tink_macro
# Conflicts:
#	src/tink/MacroApi.hx
2016-11-05 16:27:39 +01:00
Juraj Kirchheim
abbb9e5a4c Fixes #10. 2016-11-05 16:26:59 +01:00
back2dos
921ae162c8 Release 0.11.0 2016-10-13 09:43:55 +02:00
Juraj Kirchheim
fe779872d2 Merge pull request #9 from psmtec-dklein/fix-option
Type name tink.Option is redefined from module tink.MacroApi
2016-10-12 12:52:13 +02:00
David Klein
f8ab1c7354 tink.Option<T> is already pulled in by 'using tink.CoreApi;' 2016-10-12 11:26:06 +02:00
Juraj Kirchheim
ba5349f55f Slightly better error reporting in build cache. 2016-10-06 10:39:33 +02:00
kevinresol
241919549c Add concat() 2016-08-21 15:02:42 +08:00
Juraj Kirchheim
92f60cbe4f Release 0.10.0 2016-07-08 11:49:30 +02:00
Juraj Kirchheim
3b822b4cdb Merge branch 'master' of https://github.com/haxetink/tink_macro 2016-07-08 11:49:00 +02:00
Juraj Kirchheim
f2b670f9e4 Expand build cache for 2 and 3 type parameters. 2016-07-08 11:48:40 +02:00
Kevin Leung
ff491fe7e7 Add gitter link 2016-07-06 15:22:47 +08:00
Juraj Kirchheim
7e869cd9c8 Fix logic in Exprs.has 2016-06-23 05:19:53 +02:00
Juraj Kirchheim
983ffa16af Avoid stringly approach. 2016-06-10 08:20:09 +02:00
Juraj Kirchheim
4207dea754 Release 0.9.1 2016-05-20 12:41:35 +02:00
Juraj Kirchheim
3470f1cc47 Store user defined constructors. 2016-05-20 12:41:20 +02:00
Juraj Kirchheim
d84fb38d73 Release 0.9.0 2016-05-18 07:59:04 +02:00
Juraj Kirchheim
065788c198 Replace MacroType with @:genericBuild for direct type converions. 2016-05-18 07:58:28 +02:00
Juraj Kirchheim
810c2f9630 Release 0.8.1 2016-05-02 17:58:30 +02:00
Juraj Kirchheim
331f186dcd Minor API improvement. 2016-05-02 17:58:09 +02:00
Juraj Kirchheim
c406e0b844 Release 0.8.0 2016-05-02 17:54:13 +02:00
Juraj Kirchheim
43fb9eb16a Add build cache utility. 2016-05-02 17:45:45 +02:00
Juraj Kirchheim
3734bcb513 Release 0.7.1 2016-04-19 18:23:57 +02:00
Juraj Kirchheim
d53937f836 Avoid stale fields cache. 2016-04-19 18:23:40 +02:00
19 changed files with 474 additions and 108 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/bin

View File

@@ -2,18 +2,26 @@ sudo: required
dist: trusty
language: haxe
haxe:
- "3.2.1"
- development
- 3.2.1
- development
matrix:
allow_failures:
- haxe: development
- haxe: development
install:
- haxelib install travix
- haxelib run travix install
- haxelib install travix
- haxelib run travix install
script:
- haxelib run travix node
- haxelib run travix node
env:
secure: T4SCtY5qmEsK1ARWPevJmqLm23tv4CobLrbPOQV3FsoQno7FCP1S/+9GmuoJKzeTjWMzdTeDsp8TVwZ6AyGjvhl2nZNjhU+QTsir4tfbYYRyvsz/QK6pveFbPQVv7OsnnaB4wbZtqGZ8mzFeQf7Ol4tsNe7iUFJb/iVc+4/lUxo=
deploy:
provider: script
script: haxe && haxelib run travix release
on:
tags: true

5
.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,5 @@
{
"haxe.displayConfigurations": [
["tests.hxml"]
]
}

6
.vscode/tasks.json vendored Normal file
View File

@@ -0,0 +1,6 @@
{
"version": "0.1.0",
"command": "haxelib",
"args": ["run", "travix", "node"],
"problemMatcher": "$haxe"
}

View File

@@ -1,5 +1,8 @@
# Tinkerbell Macro Library
[![Build Status](https://travis-ci.org/haxetink/tink_macro.svg?branch=master)](https://travis-ci.org/haxetink/tink_macro)
[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/haxetink/public)
Explained in current marketing speak, `tink_macro` is *the* macro toolkit ;)
### History and Mission
@@ -63,6 +66,8 @@ Rejects an expression and displays a generic or custom error message
Converts an expression into the corresponding Haxe source code
- `log(e:Expr, ?pos:Position):Expr`
Traces the string representation of an expression and returns it.
- `concat(e1:Expr, e2:Expr):Expr`
Concats two expressions into a block. If either sub-expression is a block itself, it gets flattened into the resulting block.
#### Extracting Constants
@@ -298,7 +303,7 @@ abstract Member from Field to Field {
Most of the API should be self-explaining. The `isBound` property is a bad name to convey the concept that a field can be either `inline` (`true`) or `dynamic` (`false`) or neither (`null`). Equally, `isPublic` is nullable which means that normally defaults to `private`.
The `publish` method will make a field `public` if it is not `private`. This can also be done with `if (m.isPublic == null) m.isPublic = true;` but the implementation is far more efficient - for what its worth.
The `publish` method will make a field `public` if it is not explicitly marked as `private`. This can also be done with `if (m.isPublic == null) m.isPublic = true;` but the implementation is far more efficient - for what its worth.
The `extractMeta` method will "peel of" the first tag with a given `name` - if available. Note that the tag will be removed from the member.
@@ -386,7 +391,7 @@ The `init` method is the swiss army knife of initializing fields. The `options.p
#### Setter Bypass
It is important to know that when you initialize a field with `options.bypass` set to true, existing setters will by bypassed. That's particularly helpful if your setter triggers a side effect that you don't want triggered. This is achieved by generating the assignment as `(untyped this).$name = $value`. To make the code typesafe again, this is prefixed with `if (false) { var __tmp = this.$name; __tmp = $value; }`. This code is later thrown out by the compiler. Its role is to ensure type safety without interfering with the normal typing order.
It is important to know that when you initialize a field with `options.bypass` set to true, existing setters will be bypassed. That's particularly helpful if your setter triggers a side effect that you don't want triggered. This is achieved by generating the assignment as `(untyped this).$name = $value`. To make the code typesafe again, this is prefixed with `if (false) { var __tmp = this.$name; __tmp = $value; }`. This code is later thrown out by the compiler. Its role is to ensure type safety without interfering with the normal typing order.
Setter bypass also causes the field to gain an `@:isVar`. And currently, with `-dce full`, additional code will be generated to avoid the field to be eliminated.
@@ -412,4 +417,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.

View File

@@ -1,3 +0,0 @@
cd bin
neko tests.n
pause

1
bin/.gitignore vendored
View File

@@ -1 +0,0 @@
*.n

View File

@@ -1,20 +1,20 @@
{
"name": "tink_macro",
"license": "MIT",
"description": "The macro toolkit ;)",
"classPath": "src",
"dependencies": {
"tink_core": ""
},
"url": "http://haxetink.org/tink_macro",
"contributors": [
"back2dos"
],
"version": "0.15.0",
"releasenote": "Keep up with Haxe 4.",
"tags": [
"tink",
"macro",
"utility"
],
"classPath": "src",
"description": "The macro toolkit ;)",
"contributors": [
"back2dos"
],
"releasenote": "Add TypeMap.",
"version": "0.7.0",
"url": "http://haxetink.org/tink_macro",
"dependencies": {
"tink_core": ""
}
"license": "MIT"
}

View File

@@ -14,13 +14,12 @@ typedef Bouncer = tink.macro.Bouncer;
typedef Types = tink.macro.Types;
typedef Binops = tink.macro.Ops.Binary;
typedef Unops = tink.macro.Ops.Unary;
typedef TypeMap<T> = tink.macro.TypeMap<T>;
//TODO: consider adding stuff from haxe.macro.Expr here
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;
@@ -37,4 +36,4 @@ class MacroApi {
static public function pos()
return haxe.macro.Context.currentPos();
}
}

View File

@@ -0,0 +1,185 @@
package tink.macro;
import haxe.macro.Context;
import haxe.macro.Expr;
import haxe.macro.Type;
import tink.macro.TypeMap;
using tink.MacroApi;
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 = new Map();
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 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();
if (type == null)
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 Group(name);
case v: v;
}
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()
});
entries.set(type, { name: path } );
Context.defineModule(path, [def], usings);
return Context.getType(path);
}
function doMake()
while (true)
switch '$name${counter++}' {
case _.definedType() => Some(_):
case v:
return make(v);
}
return
switch entries.get(type) {
case null:
doMake();
case v:
switch v.name.definedType() {
case Some(v): v;
default: doMake();
}
}
}
}

View File

@@ -9,53 +9,53 @@ using tink.MacroApi;
using Lambda;
class ClassBuilder {
var memberList:Array<Member>;
var macros:Map<String,Field>;
var constructor:Null<Constructor>;
public var target(default, null):ClassType;
var superFields:Map<String,Bool>;
var initializeFrom:Array<Field>;
public function new(?target, ?fields) {
if (target == null)
public function new(?target, ?fields) {
if (target == null)
target = Context.getLocalClass().get();
if (fields == null)
fields = Context.getBuildFields();
this.initializeFrom = fields;
this.target = target;
}
function init() {
if (initializeFrom == null) return;
var fields = initializeFrom;
initializeFrom = null;
this.memberList = [];
this.macros = new Map();
for (field in fields)
for (field in fields)
if (field.access.has(AMacro))
macros.set(field.name, field)
else if (field.name == 'new') {
var m:Member = field;
this.constructor = new Constructor(this, m.getFunction().sure(), m.isPublic, m.pos, field.meta);
}
else
else
doAddMember(field);
}
public function getConstructor(?fallback:Function):Constructor {
init();
if (constructor == null)
if (constructor == null)
if (fallback != null)
new Constructor(this, fallback);
constructor = new Constructor(this, fallback);
else {
var sup = target.superClass;
while (sup != null) {
@@ -64,18 +64,18 @@ class ClassBuilder {
try {
var ctor = cl.constructor.get();
var func = Context.getTypedExpr(ctor.expr()).getFunction().sure();
for (arg in func.args) //this is to deal with type parameter substitutions
arg.type = null;
func.expr = "super".resolve().call(func.getArgIdents());
constructor = new Constructor(this, func);
if (ctor.isPublic)
constructor.publish();
constructor.publish();
}
catch (e:Dynamic) {//fails for unknown reason
if (e == 'assert')
neko.Lib.rethrow(e);
tink.core.Error.rethrow(e);
constructor = new Constructor(this, null);
}
break;
@@ -85,15 +85,15 @@ class ClassBuilder {
if (constructor == null)
constructor = new Constructor(this, null);
}
return constructor;
}
public function hasConstructor():Bool {
init();
return this.constructor != null;
}
public function export(?verbose):Array<Field> {
if (initializeFrom != null) return null;
var ret = (constructor == null || target.isInterface) ? [] : [constructor.toHaxe()];
@@ -108,24 +108,24 @@ class ClassBuilder {
}
for (m in macros)
ret.push(m);
if (verbose)
for (field in ret)
if (verbose)
for (field in ret)
Context.warning(new Printer().printField(field), field.pos);
return ret;
return ret;
}
public function iterator():Iterator<Member> {
init();
return this.memberList.copy().iterator();
}
public function hasOwnMember(name:String):Bool {
init();
return
return
macros.exists(name) || memberByName(name).isSuccess();
}
public function hasSuperField(name:String):Bool {
if (superFields == null) {
superFields = new Map();
@@ -139,51 +139,51 @@ class ClassBuilder {
}
return superFields.get(name);
}
public function memberByName(name:String, ?pos:Position) {
init();
for (m in memberList)
if (m.name == name)
if (m.name == name)
return Success(m);
return pos.makeFailure('unknown member $name');
}
public function removeMember(member:Member):Bool {
init();
return
return
memberList.remove(member);
}
public function hasMember(name:String):Bool
public function hasMember(name:String):Bool
return hasOwnMember(name) || hasSuperField(name);
function doAddMember(m:Member, ?front:Bool = false):Member {
init();
if (m.name == 'new')
if (m.name == 'new')
throw 'Constructor must not be registered as ordinary member';
//if (hasOwnMember(m.name))
//if (hasOwnMember(m.name))
//m.pos.error('duplicate member declaration ' + m.name);
if (front)
if (front)
memberList.unshift(m);
else
memberList.push(m);
return m;
}
public function addMember(m:Member, ?front:Bool = false):Member {
doAddMember(m, front);
if (!m.isStatic && hasSuperField(m.name))
m.overrides = true;
else
memberList.push(m);
return m;
}
public function addMember(m:Member, ?front:Bool = false):Member {
doAddMember(m, front);
if (!m.isStatic && hasSuperField(m.name))
m.overrides = true;
return m;
}
static public function run(plugins:Array<ClassBuilder->Void>, ?verbose) {
var builder = new ClassBuilder();
for (p in plugins)

View File

@@ -62,6 +62,10 @@ class Constructor {
default: [].toBlock();
}
}
public function getArgList():Array<FunctionArg>
return beforeArgs.concat(args).concat(afterArgs);
public function addStatement(e:Expr, ?prepend)
if (prepend)
this.nuStatements.unshift(e)
@@ -96,7 +100,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);

View File

@@ -0,0 +1,6 @@
package tink.macro;
@:genericBuild(tink.macro.Types.resolveDirectType())
class DirectType<Const> {
}

View File

@@ -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);
}
@@ -241,7 +241,7 @@ class Exprs {
}
static public inline function iterate(target:Expr, body:Expr, ?loopVar:String = 'i', ?pos:Position)
return EFor(EIn(loopVar.resolve(pos), target).at(pos), body).at(pos);
return macro @:pos(pos.sanitize()) for ($i{loopVar} in $target) $body;
static public function toFields(object:Dynamic<Expr>, ?pos:Position)
return EObjectDecl([for (field in Reflect.fields(object))
@@ -388,10 +388,21 @@ class Exprs {
case EFunction(_, f): Success(f);
default: e.pos.makeFailure(NOT_A_FUNCTION);
}
static public function concat(e:Expr, with:Expr, ?pos) {
if(pos == null) pos = e.pos;
return
switch [e.expr, with.expr] {
case [EBlock(e1), EBlock(e2)]: EBlock(e1.concat(e2)).at(pos);
case [EBlock(e1), e2]: EBlock(e1.concat([with])).at(pos);
case [e1, EBlock(e2)]: EBlock([e].concat(e2)).at(pos);
default: EBlock([e, with]).at(pos);
}
}
static inline var NOT_AN_INT = "integer constant expected";
static inline var NOT_AN_IDENT = "identifier expected";
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";
}
}

View File

@@ -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);

106
src/tink/macro/Sisyphus.hx Normal file
View File

@@ -0,0 +1,106 @@
package tink.macro;
import haxe.macro.Expr;
import haxe.macro.Type;
class Sisyphus {
static function nullable(complexType : ComplexType) : ComplexType return macro : Null<$complexType>;
static function toField(cf : ClassField) : Field return {
function varAccessToString(va : VarAccess, getOrSet : String) : String return {
switch (va) {
case AccNormal: "default";
case AccNo: "null";
case AccNever: "never";
case AccResolve: throw "Invalid TAnonymous";
case AccCall: getOrSet;
case AccInline: "default";
case AccRequire(_, _): "default";
default: throw "not implemented";
}
}
if (cf.params.length == 0) {
name: cf.name,
doc: cf.doc,
access: cf.isPublic ? [ APublic ] : [ APrivate ],
kind: switch([ cf.kind, cf.type ]) {
case [ FVar(read, write), ret ]:
FProp(
varAccessToString(read, "get"),
varAccessToString(write, "set"),
toComplexType(ret),
null);
case [ FMethod(_), TFun(args, ret) ]:
FFun({
args: [
for (a in args) {
name: a.name,
opt: a.opt,
type: toComplexType(a.t),
}
],
ret: toComplexType(ret),
expr: null,
});
default:
throw "Invalid TAnonymous";
},
pos: cf.pos,
meta: cf.meta.get(),
} else {
throw "Invalid TAnonymous";
}
}
public static function toComplexType(type : Null<Type>) : Null<ComplexType> return
switch (type) {
case null:
null;
case TMono(_.get() => t):
t == null ? Types.toComplex(type, { direct: true }) : toComplexType(t);
case TEnum(_.get() => baseType, params):
TPath(toTypePath(baseType, params));
case TInst(_.get() => classType, params):
switch (classType.kind) {
case KTypeParameter(_):
TPath({
name: classType.name,
pack: [],
});
default:
TPath(toTypePath(classType, params));
}
case TType(_.get() => baseType, params):
TPath(toTypePath(baseType, params));
case TFun(args, ret):
TFunction(
[ for (a in args) a.opt ? nullable(toComplexType(a.t)) : toComplexType(a.t) ],
toComplexType(ret));
case TAnonymous(_.get() => { fields: fields }):
TAnonymous([ for (cf in fields) toField(cf) ]);
case TDynamic(t):
if (t == null) {
macro : Dynamic;
} else {
var ct = toComplexType(t);
macro : Dynamic<$ct>;
}
case TLazy(f):
toComplexType(f());
case TAbstract(_.get() => baseType, params):
TPath(toTypePath(baseType, params));
default:
throw "Invalid type";
}
static function toTypePath(baseType : BaseType, params : Array<Type>) : TypePath return {
var module = baseType.module;
{
pack: baseType.pack,
name: module.substring(module.lastIndexOf(".") + 1),
sub: baseType.name,
params: [ for (t in params) TPType(toComplexType(t)) ],
}
}
}

View File

@@ -17,14 +17,15 @@ class TypeMap<V> extends BalancedTree<Type, V> implements IMap<Type, V> {
}
override function compare(k1:Type, k2:Type):Int {
if (follow) {
k1 = k1.reduce();
k2 = k2.reduce();
}
//trace(k1.toString());
//trace(k2.toString());
return switch k1.getIndex() - k2.getIndex() {
case 0: Reflect.compare(k1.toString(), k2.toString());//TODO: this may be rather expensive and not very reliable
case 0:
Reflect.compare(k1.toString(), k2.toString());//much to my surprise, this actually seems to work (at least with 3.4)
case v: v;
}
}

View File

@@ -13,12 +13,18 @@ using tink.macro.Functions;
using tink.CoreApi;
class Types {
static public function definedType(typeName:String)
return
try {
Some(Context.getType(typeName));
}
catch (e:Dynamic)
if (Std.string(e) == 'Type not found \'$typeName\'') None;
else tink.core.Error.rethrow(e);
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)();
static public function getID(t:Type, ?reduced = true)
return
if (reduced)
@@ -43,6 +49,16 @@ class Types {
throw 'not implemented';
}
static public function getMeta(type:Type)
return switch type {
case TInst(_.get().meta => m, _): [m];
case TEnum(_.get().meta => m, _): [m];
case TAbstract(_.get().meta => m, _): [m];
case TType(_.get() => t, _): [t.meta].concat(getMeta(t.type));
case TLazy(f): getMeta(f());
default: [];
}
static function getDeclaredFields(t:ClassType, out:Array<ClassField>, marker:Map<String,Bool>) {
for (field in t.fields.get())
if (!marker.exists(field.name)) {
@@ -70,12 +86,12 @@ class Types {
}
var ret = fieldsCache.get(id);
if (substituteParams && ret.isSuccess()) {
var e = ECheckType(macro null, toComplex(t)).at();
var fields = Reflect.copy(ret.sure());
for (field in fields)
field.type = haxe.macro.TypeTools.applyTypeParameters(field.type, c.params, params);
}
fieldsCache.remove(id);//TODO: find a proper solution to avoid stale cache
ret;
case TAnonymous(anon): Success(anon.get().fields);
default: Context.currentPos().makeFailure('type has no fields');
@@ -152,7 +168,7 @@ class Types {
static public function toComplex(type:Type, ?options:{ ?direct: Bool }):ComplexType {
var ret =
if (options == null || options.direct != true) haxe.macro.TypeTools.toComplexType(type);
if (options == null || options.direct != true) tink.macro.Sisyphus.toComplexType(type);
else null;
if (ret == null)
ret = lazyComplex(function () return type);
@@ -162,9 +178,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()]();//When using compiler server, this call throws on occasion, in which case modifying this file (to update mtime and invalidate the cache) will solve the problem
default:
throw 'assert';
}
}

View File

@@ -80,4 +80,11 @@ class Exprs extends Base {
])
);
}
function testConcat() {
exprEq(macro {a; b;}, (macro a).concat(macro b));
exprEq(macro {a; b; c;}, (macro {a; b;}).concat(macro c));
exprEq(macro {a; b; c;}, (macro a).concat(macro {b; c;}));
exprEq(macro {a; b; c; d;}, (macro {a; b;}).concat(macro {c; d;}));
}
}