Compare commits
82 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
2310b6d8e4 | ||
![]() |
7f13916154 | ||
![]() |
2b7204f8d3 | ||
![]() |
7ca0d4a650 | ||
![]() |
e38f5ae147 | ||
![]() |
4186225eb5 | ||
![]() |
f5a3d73c55 | ||
![]() |
f65df0e94b | ||
![]() |
55a4b1463f | ||
![]() |
0a6e39969b | ||
![]() |
f051345863 | ||
![]() |
74e37b4bf1 | ||
![]() |
42821979d9 | ||
![]() |
2aa0b07e9f | ||
![]() |
67e680f67f | ||
![]() |
ebade45e63 | ||
![]() |
40497bb1fc | ||
![]() |
d80b03fe9d | ||
![]() |
f269d13abf | ||
![]() |
f271cc880a | ||
![]() |
9cd59d3989 | ||
![]() |
40ee99df39 | ||
![]() |
1cb27daf46 | ||
![]() |
a9bea4287a | ||
![]() |
0be2090232 | ||
![]() |
0dc774db21 | ||
![]() |
a7c22bf5af | ||
![]() |
a164b335ed | ||
![]() |
d02306eebd | ||
![]() |
ff9ef59445 | ||
![]() |
bfc5c2b88f | ||
![]() |
57042819b1 | ||
![]() |
82d15d9ffc | ||
![]() |
0d9fff7e01 | ||
![]() |
45d5f3e0ea | ||
![]() |
800891bee2 | ||
![]() |
0802bf7f79 | ||
![]() |
42897c7cd5 | ||
![]() |
0e902745b1 | ||
![]() |
3dec4fa404 | ||
![]() |
c0ec918f52 | ||
![]() |
4eb9729f0e | ||
![]() |
7586b54ece | ||
![]() |
21c7d12be7 | ||
![]() |
f27d2796b1 | ||
![]() |
417a92cabc | ||
![]() |
4457ea7dee | ||
![]() |
209847e5ea | ||
![]() |
c94e4a0337 | ||
![]() |
cc009b2026 | ||
![]() |
0a36ee8e2b | ||
![]() |
5879773566 | ||
![]() |
58262a1810 | ||
![]() |
9ceadeb88f | ||
![]() |
15d75965ad | ||
![]() |
c7634e0a29 | ||
![]() |
49e48bc801 | ||
![]() |
c150b84f18 | ||
![]() |
04f86ce341 | ||
![]() |
45380371d8 | ||
![]() |
d94d946301 | ||
![]() |
56b7caaf01 | ||
![]() |
73e9da8435 | ||
![]() |
bf23337923 | ||
![]() |
02bd3bf69d | ||
![]() |
82a3417b97 | ||
![]() |
1f986c3732 | ||
![]() |
20e998fd12 | ||
![]() |
0f9c0d1e97 | ||
![]() |
2ae13d534e | ||
![]() |
65d341f1e1 | ||
![]() |
68fa50c7c3 | ||
![]() |
32aa2d6570 | ||
![]() |
d81642e733 | ||
![]() |
d5f4987ddb | ||
![]() |
671f1bc995 | ||
![]() |
b1012937fc | ||
![]() |
bd4b8c436f | ||
![]() |
dbebd0d70d | ||
![]() |
4c523e02a5 | ||
![]() |
f39fc3b296 | ||
![]() |
e06c40325b |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/bin
|
27
.travis.yml
27
.travis.yml
@@ -2,18 +2,27 @@ 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
|
||||
- haxelib install hx3compat
|
||||
|
||||
script:
|
||||
- haxelib run travix node
|
||||
- haxelib run travix node -lib hx3compat
|
||||
|
||||
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
5
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"haxe.displayConfigurations": [
|
||||
["tests.hxml"]
|
||||
]
|
||||
}
|
6
.vscode/tasks.json
vendored
Normal file
6
.vscode/tasks.json
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"version": "0.1.0",
|
||||
"command": "haxelib",
|
||||
"args": ["run", "travix", "node"],
|
||||
"problemMatcher": "$haxe"
|
||||
}
|
53
README.md
53
README.md
@@ -1,5 +1,7 @@
|
||||
# Tinkerbell Macro Library
|
||||
[](https://gitter.im/haxetink/public)
|
||||
|
||||
[](https://travis-ci.org/haxetink/tink_macro)
|
||||
[](https://gitter.im/haxetink/public)
|
||||
|
||||
Explained in current marketing speak, `tink_macro` is *the* macro toolkit ;)
|
||||
|
||||
@@ -15,18 +17,17 @@ The library is build on top of the haxe macro API and `tink_core`, having three
|
||||
|
||||
<!-- START INDEX -->
|
||||
- [Macro API](#macro-api)
|
||||
-
|
||||
- [Expression Tools](#expression-tools)
|
||||
- [Basic Helpers](#basic-helpers)
|
||||
- [Extracting Constants](#extracting-constants)
|
||||
- [Shortcuts](#shortcuts)
|
||||
- [Type Inspection](#type-inspection)
|
||||
- [Advanced Transformations](#advanced-transformations)
|
||||
- [Position Tools](#position-tools)
|
||||
- [Type Tools](#type-tools)
|
||||
- [Function Tools](#function-tools)
|
||||
- [Operation Tools](#operation-tools)
|
||||
- [Metadata Tools](#metadata-tools)
|
||||
- [Expression Tools](#expression-tools)
|
||||
- [Basic Helpers](#basic-helpers)
|
||||
- [Extracting Constants](#extracting-constants)
|
||||
- [Shortcuts](#shortcuts)
|
||||
- [Type Inspection](#type-inspection)
|
||||
- [Advanced Transformations](#advanced-transformations)
|
||||
- [Position Tools](#position-tools)
|
||||
- [Type Tools](#type-tools)
|
||||
- [Function Tools](#function-tools)
|
||||
- [Operation Tools](#operation-tools)
|
||||
- [Metadata Tools](#metadata-tools)
|
||||
- [Build Infrastructure](#build-infrastructure)
|
||||
- [Member](#member)
|
||||
- [ClassBuilder](#classbuilder)
|
||||
@@ -49,9 +50,9 @@ It is suggested to use this API by `using tink.MacroApi;`
|
||||
|
||||
Apart form `tink_macro` specific things, it will also use `haxe.macro.ExprTools` and `tink.core.Outcome`.
|
||||
|
||||
### Expression Tools
|
||||
## Expression Tools
|
||||
|
||||
#### Basic Helpers
|
||||
### Basic Helpers
|
||||
|
||||
- `at(e:ExprDef, ?pos:Position):Expr`
|
||||
A short hand for creating expression as for example `EReturn(value).at(position)`, instead of the more verbose `{ expr: EReturn(value), pos: position }`.
|
||||
@@ -67,7 +68,7 @@ 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
|
||||
### Extracting Constants
|
||||
|
||||
- `isWildcard(e:Expr):Bool`
|
||||
Checks whether an expression is the identifier `_`
|
||||
@@ -80,7 +81,7 @@ Attempts extracting an identifier from an expression. Note that an identifier ca
|
||||
- `getName(e:Expr):Outcome<String, tink.core.Error>`
|
||||
Attempts extracting a name, i.e. a string constant or identifier from an expression
|
||||
|
||||
#### Shortcuts
|
||||
### Shortcuts
|
||||
|
||||
Often reification is prefereable to these shortcuts - if applicable. Unlike reification, the position of these expressions will default to `Context.currentPos()` rather than the position where they were created.
|
||||
|
||||
@@ -117,7 +118,7 @@ Generates a simple if statement.
|
||||
- `iterate(target:Expr, body:Expr, ?loopVar:String = 'i', ?pos:Position):Expr`
|
||||
Will loop over `target` with a loop variable called `loopVar` using `body`-
|
||||
|
||||
#### Type Inspection
|
||||
### Type Inspection
|
||||
|
||||
- `is(e:Expr, c:ComplexType):Bool`
|
||||
Tells you whether a given expression has a given type.
|
||||
@@ -127,7 +128,7 @@ Inspects, whether an expression can be iterated over and if so returns the eleme
|
||||
- `typeof(expr:Expr, ?locals:Array<Var>):Outcome<Type, tink.core.Error>`
|
||||
Attempts to determine the type of an expression. Note that you can use `locals` to hint the compiler the type of certain identifiers. For example if you are in a build macro, and you want to get the type of a subexpression of a method body, you could "fake" the other members of the class as local variables, because in that context, the other members do not yet exists from the compiler's perspective.
|
||||
|
||||
#### Advanced Transformations
|
||||
### Advanced Transformations
|
||||
|
||||
- `has(e:Expr, condition:Expr->Bool, ?options: { ?enterFunctions: Bool })`
|
||||
This function actually does no transformation, but is very close to the rest of these functions. It allows you to check whether an expression has a sub-expression that satisfies `condition`. By default, it does not enter nested functions.
|
||||
@@ -185,7 +186,7 @@ This will traverse an expression and will apply the `yielder` to the "leafs", wh
|
||||
|
||||
If you set `options.leaveLoops` to `true`, then loops (both for and while) will be considered leafs.
|
||||
|
||||
### Position Tools
|
||||
## Position Tools
|
||||
|
||||
- `sanitize(pos:Position):Position`
|
||||
Returns the position ITself or `Context.currentPos()` if it's null.
|
||||
@@ -200,7 +201,7 @@ Creates a failed `Outcome` associated with the supplied position.
|
||||
- `getOutcome<D, F>(pos:Position, outcome:Outcome<D, F>):D`
|
||||
Attempts getting the result of the supplied outcome. If it is a failure, it will cause an error at the given position.
|
||||
|
||||
### Type Tools
|
||||
## Type Tools
|
||||
|
||||
- `getID(t:Type, ?reduced = true):Null<String>`
|
||||
Returns a String identifier for a type if available. By default, the type will be reduced prior to getting its name (typedefs are resolved etc.). With `reduced = false` you can also get the name of a typedef.
|
||||
@@ -223,7 +224,7 @@ Will tell you whether a field is a variable or not. Signature is likely to chang
|
||||
- `toComplex(type:Type, ?option:{ ?direct: Bool }):ComplexType`
|
||||
Will convert a `Type` to a `ComplexType`. Ideally this is done with `Context.toComplexType` but for monomorphs and the like, this builtin method fails and `tink_macro` uses a hack to make it work none the less. You can also use `{ direct : true }` to force this hack in case the translation fails (which can be the case with private types).
|
||||
|
||||
### Function Tools
|
||||
## Function Tools
|
||||
|
||||
- `asExpr(f:Function, ?name:String, ?pos:Position):Expr`
|
||||
Converts a function to an expression, i.e. a local function definition.
|
||||
@@ -234,7 +235,7 @@ A shorthand to create function arguments.
|
||||
- `getArgIdents(f:Function):Array<Expr>`
|
||||
Will extract the argument list of a function as an expression list of identifiers (usefull when writing call-forwarding macros or the like).
|
||||
|
||||
### Operation Tools
|
||||
## Operation Tools
|
||||
|
||||
- `get(o:Binop, e:Expr):Outcome<{ e1:Expr, e2:Expr, pos:Position }, tink.core.Error>`
|
||||
Attempts to extract a specific binary operation from an expression.
|
||||
@@ -248,7 +249,7 @@ Attempts to extract a specific unary operation from an expression.
|
||||
- `getUnop(e:Expr):Outcome<{ op:Unop, e:Expr, postFix:Bool, pos:Position }, tink.core.Error>`
|
||||
Attempts to decompose an expression into the parts of a unary operation.
|
||||
|
||||
### Metadata Tools
|
||||
## Metadata Tools
|
||||
|
||||
- `toMap(m:Metadata):Map<String, Array<Array<Expr>>`
|
||||
Will deconstruct an array of metadata tags to a `Map` mapping the tag names to an array of the argument lists of each tag with that name. So `@foo(1) @foo(2) @bar` becomes `["foo" => [[1], [2]], "bar" => [[]]]`
|
||||
@@ -301,7 +302,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.
|
||||
|
||||
@@ -389,7 +390,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.
|
||||
|
||||
|
1
bin/.gitignore
vendored
1
bin/.gitignore
vendored
@@ -1 +0,0 @@
|
||||
*.n
|
6
haxe_libraries/hxnodejs.hxml
Normal file
6
haxe_libraries/hxnodejs.hxml
Normal file
@@ -0,0 +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
|
||||
--macro allowPackage('sys')
|
||||
--macro _hxnodejs.VersionWarning.include()
|
3
haxe_libraries/tink_core.hxml
Normal file
3
haxe_libraries/tink_core.hxml
Normal file
@@ -0,0 +1,3 @@
|
||||
# @install: lix --silent download "haxelib:tink_core#1.16.1" into tink_core/1.16.1/haxelib
|
||||
-D tink_core=1.16.1
|
||||
-cp ${HAXESHIM_LIBCACHE}/tink_core/1.16.1/haxelib/src
|
3
haxe_libraries/tink_macro.hxml
Normal file
3
haxe_libraries/tink_macro.hxml
Normal file
@@ -0,0 +1,3 @@
|
||||
-D tink_macro
|
||||
-cp src
|
||||
-lib tink_core
|
4
haxe_libraries/travix.hxml
Normal file
4
haxe_libraries/travix.hxml
Normal file
@@ -0,0 +1,4 @@
|
||||
# @run: haxelib run-dir travix ${HAXESHIM_LIBCACHE}/travix/0.10.3/haxelib
|
||||
# @install: lix --silent download "haxelib:travix#0.10.3" into travix/0.10.3/haxelib
|
||||
-D travix=0.10.3
|
||||
-cp ${HAXESHIM_LIBCACHE}/travix/0.10.3/haxelib/src
|
@@ -11,8 +11,8 @@
|
||||
"contributors": [
|
||||
"back2dos"
|
||||
],
|
||||
"releasenote": "Fix issue with .toComplex()",
|
||||
"version": "0.12.2",
|
||||
"releasenote": "Fix haxe 4 compat.",
|
||||
"version": "0.16.7",
|
||||
"url": "http://haxetink.org/tink_macro",
|
||||
"dependencies": {
|
||||
"tink_core": ""
|
||||
|
@@ -14,6 +14,7 @@ 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>;
|
||||
@@ -35,4 +36,51 @@ class MacroApi {
|
||||
static public function pos()
|
||||
return haxe.macro.Context.currentPos();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#if (haxe_ver >= 4)
|
||||
typedef ObjectField = haxe.macro.Expr.ObjectField;
|
||||
typedef QuoteStatus = haxe.macro.Expr.QuoteStatus;
|
||||
#else
|
||||
enum QuoteStatus {
|
||||
Unquoted;
|
||||
Quoted;
|
||||
}
|
||||
private typedef F = {
|
||||
var field:String;
|
||||
var expr:haxe.macro.Expr;
|
||||
}
|
||||
|
||||
@:forward
|
||||
abstract ObjectField(F) to F {
|
||||
|
||||
static var QUOTED = "@$__hx__";
|
||||
|
||||
inline function new(o) this = o;
|
||||
|
||||
public var field(get, never):String;
|
||||
|
||||
function get_field()
|
||||
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:
|
||||
new ObjectField({ field: o.field, expr: o.expr });
|
||||
default:
|
||||
new ObjectField({ field: QUOTED + o.field, expr: o.expr });
|
||||
}
|
||||
|
||||
@:from static function ofOld(o:F):ObjectField
|
||||
return new ObjectField(o);
|
||||
}
|
||||
#end
|
@@ -5,6 +5,7 @@ import haxe.macro.Expr;
|
||||
import haxe.macro.Type;
|
||||
import tink.macro.TypeMap;
|
||||
|
||||
using tink.MacroApi;
|
||||
using haxe.macro.Tools;
|
||||
|
||||
typedef BuildContextN = {
|
||||
@@ -73,7 +74,11 @@ class BuildCache {
|
||||
var compound = ComplexType.TAnonymous([for (i in 0...types.length) {
|
||||
name: 't$i',
|
||||
pos: pos,
|
||||
kind: FVar(types[i].toComplexType()),
|
||||
kind: FVar(switch types[i] {
|
||||
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({
|
||||
@@ -104,23 +109,31 @@ class BuildCache {
|
||||
usings: ctx.usings
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
static public function getParams(name:String, ?pos:Position)
|
||||
return
|
||||
switch Context.getLocalType() {
|
||||
case TInst(_.toString() == name => true, v):
|
||||
Success(v);
|
||||
case TInst(_.get() => { pos: pos }, _):
|
||||
pos.makeFailure('Expected $name');
|
||||
case v:
|
||||
pos.makeFailure('$v should be a class');
|
||||
}
|
||||
|
||||
static public function getParam(name:String, ?pos:Position)
|
||||
return
|
||||
getParams(name, pos)
|
||||
.flatMap(function (args:Array<Type>) return switch args {
|
||||
case [v]: Success(v);
|
||||
case []: pos.makeFailure('type parameter expected');
|
||||
default: pos.makeFailure('too many parameters');
|
||||
});
|
||||
|
||||
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';
|
||||
}
|
||||
type = getParam(name, pos).sure();
|
||||
|
||||
var forName =
|
||||
switch cache[name] {
|
||||
@@ -128,7 +141,7 @@ class BuildCache {
|
||||
case v: v;
|
||||
}
|
||||
|
||||
return forName.get(type, pos, build);
|
||||
return forName.get(type, pos.sanitize(), build);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -156,18 +169,29 @@ private class Group {
|
||||
usings: usings,
|
||||
name: path.split('.').pop()
|
||||
});
|
||||
|
||||
Context.defineModule(path, [def], usings);
|
||||
|
||||
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:
|
||||
make('$name${counter++}');
|
||||
doMake();
|
||||
case v:
|
||||
Context.getType(v.name);
|
||||
switch v.name.definedType() {
|
||||
case Some(v): v;
|
||||
default: doMake();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -9,51 +9,51 @@ 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)
|
||||
constructor = new Constructor(this, fallback);
|
||||
else {
|
||||
@@ -63,19 +63,21 @@ class ClassBuilder {
|
||||
if (cl.constructor != null) {
|
||||
try {
|
||||
var ctor = cl.constructor.get();
|
||||
var func = Context.getTypedExpr(ctor.expr()).getFunction().sure();
|
||||
|
||||
var ctorExpr = ctor.expr();
|
||||
if (ctorExpr == null) throw 'Super constructor has no expression';
|
||||
var func = Context.getTypedExpr(ctorExpr).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 +87,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 +110,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,55 +141,61 @@ 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 addMembers(td:TypeDefinition):Array<Member> {
|
||||
for (f in td.fields)
|
||||
addMember(f);
|
||||
return td.fields;
|
||||
}
|
||||
|
||||
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)
|
||||
p(builder);
|
||||
return builder.export(verbose);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
3
src/tink/macro/ConstParam.hx
Normal file
3
src/tink/macro/ConstParam.hx
Normal file
@@ -0,0 +1,3 @@
|
||||
package tink.macro;
|
||||
|
||||
class ConstParam<Const> {}
|
@@ -1,8 +1,10 @@
|
||||
package tink.macro;
|
||||
|
||||
import haxe.macro.Type;
|
||||
import haxe.macro.Expr;
|
||||
import haxe.macro.Context;
|
||||
import tink.core.Pair;
|
||||
using haxe.macro.Tools;
|
||||
using tink.MacroApi;
|
||||
|
||||
enum FieldInit {
|
||||
@@ -62,6 +64,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 +102,43 @@ class Constructor {
|
||||
case Success(member): member.addMeta(':isVar');
|
||||
default:
|
||||
}
|
||||
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
|
||||
addStatement((function () {
|
||||
var fields = [for (f in (macro this).typeof().sure().getClass().fields.get()) f.name => f];
|
||||
|
||||
function setDirectly(t:TypedExpr) {
|
||||
var direct = null;
|
||||
function seek(t:TypedExpr) {
|
||||
switch t.expr {
|
||||
case TField({ expr: TConst(TThis) }, FInstance(_, _, f)) if (f.get().name == name): direct = t;
|
||||
default: t.iter(seek);
|
||||
}
|
||||
}
|
||||
seek(t);
|
||||
if (direct == null) pos.error('nope');
|
||||
var direct = Context.storeTypedExpr(direct);
|
||||
return macro @:pos(pos) $direct = $e;
|
||||
}
|
||||
|
||||
return switch fields[name] {
|
||||
case null:
|
||||
pos.error('this direct initialization causes the compiler to do really weird things');
|
||||
case f:
|
||||
switch f.kind {
|
||||
case FVar(_, AccNormal | AccNo):
|
||||
macro @:pos(pos) this.$name = $e;
|
||||
case FVar(AccNever, AccNever):
|
||||
macro @:pos(pos) this.$name = $e;
|
||||
case FVar(AccNo | AccNormal, AccNever):
|
||||
setDirectly(Context.typeExpr(macro @:pos(pos) this.$name));
|
||||
case FVar(AccCall, AccNever):
|
||||
setDirectly(fields['get_$name'].expr());
|
||||
case FVar(_, AccCall):
|
||||
setDirectly(fields['set_$name'].expr());
|
||||
default:
|
||||
pos.error('not implemented');
|
||||
}
|
||||
}
|
||||
}).bounce(), options.prepend);
|
||||
}
|
||||
else
|
||||
addStatement(macro @:pos(pos) this.$name = $e, options.prepend);
|
||||
@@ -132,4 +174,4 @@ class Constructor {
|
||||
meta : this.meta,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -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))
|
||||
@@ -311,17 +311,45 @@ class Exprs {
|
||||
|
||||
static public inline function resolve(s:String, ?pos)
|
||||
return drill(s.split('.'), pos);
|
||||
|
||||
static var scopes = new Array<Array<Var>>();
|
||||
|
||||
static function inScope<T>(a:Array<Var>, f:Void->T) {
|
||||
scopes.push(a);
|
||||
|
||||
inline function leave()
|
||||
scopes.pop();
|
||||
try {
|
||||
var ret = f();
|
||||
leave();
|
||||
return ret;
|
||||
}
|
||||
catch (e:Dynamic) {
|
||||
leave();
|
||||
return Error.rethrow(e);
|
||||
}
|
||||
}
|
||||
|
||||
static public function scoped<T>(f:Void->T)
|
||||
return inScope([], f);
|
||||
|
||||
static public function inSubScope<T>(f:Void->T, a:Array<Var>)
|
||||
return inScope(switch scopes[scopes.length - 1] {
|
||||
case null: a;
|
||||
case v: v.concat(a);
|
||||
}, f);
|
||||
|
||||
static public function typeof(expr:Expr, ?locals)
|
||||
return
|
||||
try {
|
||||
if (locals == null)
|
||||
locals = scopes[scopes.length - 1];
|
||||
if (locals != null)
|
||||
expr = [EVars(locals).at(expr.pos), expr].toMBlock(expr.pos);
|
||||
Success(Context.typeof(expr));
|
||||
}
|
||||
catch (e:Error) {
|
||||
var m:Dynamic = e.message;
|
||||
e.pos.makeFailure(m);
|
||||
catch (e:haxe.macro.Error) {
|
||||
e.pos.makeFailure(e.message);
|
||||
}
|
||||
catch (e:Dynamic) {
|
||||
expr.pos.makeFailure(e);
|
||||
@@ -400,6 +428,22 @@ class Exprs {
|
||||
}
|
||||
}
|
||||
|
||||
static var FIRST = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
static var LATER = FIRST + '0123456789';
|
||||
|
||||
static public function shortIdent(i:Int) {
|
||||
var ret = FIRST.charAt(i % FIRST.length);
|
||||
|
||||
i = Std.int(i / FIRST.length);
|
||||
|
||||
while (i > 0) {
|
||||
ret += LATER.charAt(i % LATER.length);
|
||||
i = Std.int(i / LATER.length);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
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";
|
||||
|
@@ -32,6 +32,7 @@ abstract Member(Field) from Field to Field {
|
||||
}
|
||||
|
||||
public var name(get, set):String;
|
||||
public var meta(get, set):Metadata;
|
||||
public var doc(get, set):Null<String>;
|
||||
public var kind(get, set):FieldType;
|
||||
public var pos(get, set):Position;
|
||||
@@ -76,6 +77,11 @@ abstract Member(Field) from Field to Field {
|
||||
}
|
||||
return pos.makeFailure('missing @$name');
|
||||
}
|
||||
|
||||
public function metaNamed(name)
|
||||
return
|
||||
if (this.meta == null) [];
|
||||
else [for (tag in this.meta) if (tag.name == name) tag];
|
||||
|
||||
public inline function asField():Field return this;
|
||||
public function publish()
|
||||
@@ -86,6 +92,12 @@ abstract Member(Field) from Field to Field {
|
||||
this.access.push(APublic);
|
||||
}
|
||||
|
||||
inline function get_meta() return switch this.meta {
|
||||
case null: this.meta = [];
|
||||
case v: v;
|
||||
}
|
||||
inline function set_meta(param) return this.meta = param;
|
||||
|
||||
inline function get_name() return this.name;
|
||||
inline function set_name(param) return this.name = param;
|
||||
|
||||
|
@@ -12,7 +12,7 @@ class Positions {
|
||||
return
|
||||
switch outcome {
|
||||
case Success(d): d;
|
||||
case Failure(f): sanitize(pos).error(f);
|
||||
case Failure(f): pos.error(f);
|
||||
}
|
||||
|
||||
static public function makeBlankType(pos:Position):ComplexType
|
||||
@@ -32,7 +32,17 @@ class Positions {
|
||||
|
||||
|
||||
static public function error(pos:Position, error:Dynamic):Dynamic
|
||||
return errorFunc(sanitize(pos), Std.string(error));
|
||||
return
|
||||
switch Std.instance(error, tink.core.Error) {
|
||||
case null: errorFunc(sanitize(pos), Std.string(error));
|
||||
case error:
|
||||
errorFunc(
|
||||
error.pos,
|
||||
error.message +
|
||||
if (pos == null) ''
|
||||
else ' (referenced from ' + Std.string(pos).substr(5)
|
||||
);
|
||||
}
|
||||
|
||||
static function contextError(pos:Position, error:String):Dynamic
|
||||
return Context.fatalError(error, pos);
|
||||
|
@@ -17,6 +17,7 @@ class Sisyphus {
|
||||
case AccCall: getOrSet;
|
||||
case AccInline: "default";
|
||||
case AccRequire(_, _): "default";
|
||||
default: throw "not implemented";
|
||||
}
|
||||
}
|
||||
if (cf.params.length == 0) {
|
||||
@@ -52,21 +53,24 @@ class Sisyphus {
|
||||
}
|
||||
}
|
||||
|
||||
public static function toComplexType(type : Null<Type>) : Null<ComplexType> return
|
||||
|
||||
public static function toComplexType(type : Null<Type>) : Null<ComplexType> return {
|
||||
inline function direct()
|
||||
return Types.toComplex(type, { direct: true });
|
||||
switch (type) {
|
||||
case null:
|
||||
null;
|
||||
case TMono(_.get() => t):
|
||||
t == null ? null : toComplexType(t);
|
||||
case TEnum(_.get().isPrivate => true, _): direct();
|
||||
case TInst(_.get().isPrivate => true, _): direct();
|
||||
case TType(_.get().isPrivate => true, _): direct();
|
||||
case TAbstract(_.get().isPrivate => true, _): direct();
|
||||
case TMono(_): direct();
|
||||
case TEnum(_.get() => baseType, params):
|
||||
TPath(toTypePath(baseType, params));
|
||||
case TInst(_.get() => classType, params):
|
||||
switch (classType.kind) {
|
||||
case KTypeParameter(_):
|
||||
TPath({
|
||||
name: classType.name,
|
||||
pack: [],
|
||||
});
|
||||
direct();//TODO: check if the parameter is in scope, in which case the name can simply be used
|
||||
default:
|
||||
TPath(toTypePath(classType, params));
|
||||
}
|
||||
@@ -92,14 +96,23 @@ class Sisyphus {
|
||||
default:
|
||||
throw "Invalid type";
|
||||
}
|
||||
|
||||
}
|
||||
static function toTypePath(baseType : BaseType, params : Array<Type>) : TypePath return {
|
||||
var module = baseType.module;
|
||||
var name = module.substring(module.lastIndexOf(".") + 1);
|
||||
var sub = switch baseType.name {
|
||||
case _ == name => true: null;
|
||||
case v: v;
|
||||
}
|
||||
|
||||
{
|
||||
pack: baseType.pack,
|
||||
name: module.substring(module.lastIndexOf(".") + 1),
|
||||
sub: baseType.name,
|
||||
params: [ for (t in params) TPType(toComplexType(t)) ],
|
||||
name: name,
|
||||
sub: sub,
|
||||
params: [for (t in params) switch t {
|
||||
case TInst(_.get().kind => KExpr(e), _): TPExpr(e);
|
||||
default: TPType(toComplexType(t));
|
||||
}],
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -13,6 +13,16 @@ 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;
|
||||
static public function getID(t:Type, ?reduced = true)
|
||||
@@ -39,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)) {
|
||||
@@ -74,7 +94,7 @@ class Types {
|
||||
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');
|
||||
default: Context.currentPos().makeFailure('type $t has no fields');
|
||||
}
|
||||
|
||||
static public function getStatics(t:Type)
|
||||
@@ -126,8 +146,16 @@ class Types {
|
||||
static public inline function asComplexType(s:String, ?params)
|
||||
return TPath(asTypePath(s, params));
|
||||
|
||||
static public inline function reduce(type:Type, ?once)
|
||||
return Context.follow(type, once);
|
||||
static public function reduce(type:Type, ?once) {
|
||||
function rec(t:Type)
|
||||
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));
|
||||
default: type;
|
||||
}
|
||||
}
|
||||
|
||||
static public function isVar(field:ClassField)
|
||||
return switch (field.kind) {
|
||||
@@ -163,14 +191,14 @@ class Types {
|
||||
params : [TPExpr(register(f).toExpr())],
|
||||
sub : null,
|
||||
});
|
||||
|
||||
|
||||
static function resolveDirectType()
|
||||
return
|
||||
return
|
||||
switch reduce(Context.getLocalType()) {
|
||||
case TInst(_, [TInst(_.get() => { kind: KExpr(e) }, _)]):
|
||||
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:
|
||||
default:
|
||||
throw 'assert';
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@@ -1,5 +1,6 @@
|
||||
package ;
|
||||
|
||||
import haxe.macro.Context;
|
||||
import haxe.macro.Expr;
|
||||
using tink.MacroApi;
|
||||
|
||||
@@ -7,6 +8,14 @@ class Exprs extends Base {
|
||||
function exprEq(e1:Expr, e2:Expr) {
|
||||
assertEquals(e1.toString(), e2.toString());
|
||||
}
|
||||
|
||||
function testShort() {
|
||||
for (i in 0...100) {
|
||||
var id = (100 * i).shortIdent();
|
||||
Context.parseInlineString(id, (macro null).pos);
|
||||
assertTrue(id.length <= 3);
|
||||
}
|
||||
}
|
||||
function testGet() {
|
||||
assertEquals('foo', (macro foo).getIdent().sure());
|
||||
assertEquals('foo', (macro "foo").getString().sure());
|
||||
|
@@ -44,4 +44,13 @@ class Types extends Base {
|
||||
|
||||
MacroApi.pos().makeBlankType().toString();
|
||||
}
|
||||
|
||||
function testExpr() {
|
||||
assertEquals('VarChar<255>', (macro : VarChar<255>).toType().sure().toComplex().toString());
|
||||
}
|
||||
|
||||
function testToComplex() {
|
||||
assertEquals('String', Context.getType('String').toComplex().toString());
|
||||
assertEquals('tink.CoreApi.Noise', Context.getType('tink.CoreApi.Noise').toComplex().toString());
|
||||
}
|
||||
}
|
1
tests/VarChar.hx
Normal file
1
tests/VarChar.hx
Normal file
@@ -0,0 +1 @@
|
||||
typedef VarChar<Const> = String;
|
Reference in New Issue
Block a user