Compare commits
14 Commits
0.19.1
...
isAbstract
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
59d7407d1b | ||
|
|
0680220a77 | ||
|
|
acaedc170a | ||
|
|
8b9dbba624 | ||
|
|
f9348d4a46 | ||
|
|
6f4e6b9227 | ||
|
|
59135d5cea | ||
|
|
4accf55b41 | ||
|
|
fd3b01ef0f | ||
|
|
5825c2b617 | ||
|
|
328a8476a5 | ||
|
|
1589665652 | ||
|
|
08f15a6739 | ||
|
|
b5e992b820 |
@@ -9,8 +9,8 @@
|
|||||||
"contributors": [
|
"contributors": [
|
||||||
"back2dos"
|
"back2dos"
|
||||||
],
|
],
|
||||||
"version": "0.19.1",
|
"version": "0.21.1",
|
||||||
"releasenote": "More fine grained control over type normalization in BuildCache.",
|
"releasenote": "Fix issue with build field retrieval.",
|
||||||
"tags": [
|
"tags": [
|
||||||
"tink",
|
"tink",
|
||||||
"macro",
|
"macro",
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ using StringTools;
|
|||||||
|
|
||||||
typedef Positions = tink.macro.Positions;
|
typedef Positions = tink.macro.Positions;
|
||||||
typedef ExprTools = haxe.macro.ExprTools;
|
typedef ExprTools = haxe.macro.ExprTools;
|
||||||
|
typedef TypedExprTools = haxe.macro.TypedExprTools;
|
||||||
|
typedef TypedExprs = tink.macro.TypedExprs;
|
||||||
typedef Exprs = tink.macro.Exprs;
|
typedef Exprs = tink.macro.Exprs;
|
||||||
typedef Functions = tink.macro.Functions;
|
typedef Functions = tink.macro.Functions;
|
||||||
typedef Metadatas = tink.macro.Metadatas;
|
typedef Metadatas = tink.macro.Metadatas;
|
||||||
@@ -28,11 +30,11 @@ typedef ClassBuilder = tink.macro.ClassBuilder;
|
|||||||
typedef TypeResolution = Ref<Either<String, TypeDefinition>>;
|
typedef TypeResolution = Ref<Either<String, TypeDefinition>>;
|
||||||
|
|
||||||
class MacroApi {
|
class MacroApi {
|
||||||
|
|
||||||
static var MAIN_CANDIDATES = ['-main', '-x', '--run'];
|
static var MAIN_CANDIDATES = ['-main', '-x', '--run'];
|
||||||
static public function getMainClass():Option<String> {
|
static public function getMainClass():Option<String> {
|
||||||
var args = Sys.args();
|
var args = Sys.args();
|
||||||
|
|
||||||
for (c in MAIN_CANDIDATES)
|
for (c in MAIN_CANDIDATES)
|
||||||
switch args.indexOf(c) {
|
switch args.indexOf(c) {
|
||||||
case -1:
|
case -1:
|
||||||
@@ -42,19 +44,26 @@ class MacroApi {
|
|||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
@:persistent static var idCounter = 0;
|
@:persistent static var idCounter = 0;
|
||||||
|
|
||||||
@:noUsing static public inline function tempName(?prefix:String = 'tmp'):String
|
@:noUsing static public inline function tempName(?prefix:String = 'tmp'):String
|
||||||
return '__tink_' + prefix + Std.string(idCounter++);
|
return '__tink_' + prefix + Std.string(idCounter++);
|
||||||
|
|
||||||
static public function pos()
|
static public function pos()
|
||||||
return haxe.macro.Context.currentPos();
|
return haxe.macro.Context.currentPos();
|
||||||
|
|
||||||
static public var completionPoint(default, null):Option<{
|
static public var completionPoint(default, null):Option<{
|
||||||
var file(default, never):String;
|
var file(default, never):String;
|
||||||
|
var content(default, never):Null<String>;
|
||||||
var pos(default, never):Int;
|
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 public var args(default, null):Iterable<String>;
|
||||||
static var initialized = initArgs();
|
static var initialized = initArgs();
|
||||||
|
|
||||||
@@ -63,7 +72,7 @@ class MacroApi {
|
|||||||
args = sysArgs;
|
args = sysArgs;
|
||||||
completionPoint = switch sysArgs.indexOf('--display') {
|
completionPoint = switch sysArgs.indexOf('--display') {
|
||||||
case -1: None;
|
case -1: None;
|
||||||
case sysArgs[_ + 1] => arg:
|
case sysArgs[_ + 1] => arg:
|
||||||
if (arg.startsWith('{"jsonrpc":')) {
|
if (arg.startsWith('{"jsonrpc":')) {
|
||||||
var payload:{
|
var payload:{
|
||||||
jsonrpc:String,
|
jsonrpc:String,
|
||||||
@@ -71,12 +80,15 @@ class MacroApi {
|
|||||||
params:{
|
params:{
|
||||||
file:String,
|
file:String,
|
||||||
offset:Int,
|
offset:Int,
|
||||||
|
contents:String,
|
||||||
}
|
}
|
||||||
} = haxe.Json.parse(arg);
|
} = haxe.Json.parse(arg);
|
||||||
|
|
||||||
switch payload {
|
switch payload {
|
||||||
case { jsonrpc: '2.0', method: 'display/completion' }:
|
case { jsonrpc: '2.0', method: 'display/completion' }:
|
||||||
Some({
|
Some({
|
||||||
file: payload.params.file,
|
file: payload.params.file,
|
||||||
|
content: payload.params.contents,
|
||||||
pos: payload.params.offset,
|
pos: payload.params.offset,
|
||||||
});
|
});
|
||||||
default: None;
|
default: None;
|
||||||
@@ -108,26 +120,26 @@ class MacroApi {
|
|||||||
@:forward
|
@:forward
|
||||||
abstract ObjectField(F) to F {
|
abstract ObjectField(F) to F {
|
||||||
|
|
||||||
static var QUOTED = "@$__hx__";
|
static var QUOTED = "@$__hx__";
|
||||||
|
|
||||||
inline function new(o) this = o;
|
inline function new(o) this = o;
|
||||||
|
|
||||||
public var field(get, never):String;
|
public var field(get, never):String;
|
||||||
|
|
||||||
function get_field()
|
function get_field()
|
||||||
return
|
return
|
||||||
if (quotes == Quoted)
|
if (quotes == Quoted)
|
||||||
this.field.substr(QUOTED.length);
|
this.field.substr(QUOTED.length);
|
||||||
else this.field;
|
else this.field;
|
||||||
|
|
||||||
public var quotes(get, never):QuoteStatus;
|
public var quotes(get, never):QuoteStatus;
|
||||||
|
|
||||||
function get_quotes()
|
function get_quotes()
|
||||||
return if (StringTools.startsWith(this.field, QUOTED)) Quoted else Unquoted;
|
return if (StringTools.startsWith(this.field, QUOTED)) Quoted else Unquoted;
|
||||||
|
|
||||||
@:from static function ofFull(o:{>F, quotes:QuoteStatus }):ObjectField
|
@:from static function ofFull(o:{>F, quotes:QuoteStatus }):ObjectField
|
||||||
return switch o.quotes {
|
return switch o.quotes {
|
||||||
case null | Unquoted:
|
case null | Unquoted:
|
||||||
new ObjectField({ field: o.field, expr: o.expr });
|
new ObjectField({ field: o.field, expr: o.expr });
|
||||||
default:
|
default:
|
||||||
new ObjectField({ field: QUOTED + o.field, expr: o.expr });
|
new ObjectField({ field: QUOTED + o.field, expr: o.expr });
|
||||||
|
|||||||
@@ -23,7 +23,10 @@ class ClassBuilder {
|
|||||||
target = Context.getLocalClass().get();
|
target = Context.getLocalClass().get();
|
||||||
|
|
||||||
if (fields == null)
|
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.initializeFrom = fields;
|
||||||
this.target = target;
|
this.target = target;
|
||||||
@@ -192,10 +195,14 @@ class ClassBuilder {
|
|||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
static public function run(plugins:Array<ClassBuilder->Void>, ?verbose) {
|
static public function run(plugins:Array<ClassBuilder->Void>, ?verbose)
|
||||||
var builder = new ClassBuilder();
|
return switch MacroApi.getBuildFields() {
|
||||||
for (p in plugins)
|
case None: null;
|
||||||
p(builder);
|
case Some(fields):
|
||||||
return builder.export(verbose);
|
var builder = new ClassBuilder(fields);
|
||||||
}
|
for (p in plugins)
|
||||||
|
p(builder);
|
||||||
|
return builder.export(verbose);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,8 +23,8 @@ class Sisyphus {
|
|||||||
if (cf.params.length == 0) {
|
if (cf.params.length == 0) {
|
||||||
name: cf.name,
|
name: cf.name,
|
||||||
doc: cf.doc,
|
doc: cf.doc,
|
||||||
access:
|
access:
|
||||||
(cf.isPublic ? [ APublic ] : [ APrivate ])
|
(cf.isPublic ? [ APublic ] : [ APrivate ])
|
||||||
#if haxe4 .concat(if (cf.isFinal) [AFinal] else []) #end
|
#if haxe4 .concat(if (cf.isFinal) [AFinal] else []) #end
|
||||||
,
|
,
|
||||||
kind: switch([ cf.kind, cf.type ]) {
|
kind: switch([ cf.kind, cf.type ]) {
|
||||||
@@ -84,7 +84,7 @@ class Sisyphus {
|
|||||||
cl.kind.match(KTypeParameter(_))
|
cl.kind.match(KTypeParameter(_))
|
||||||
&& cl.module == classType.module
|
&& cl.module == classType.module
|
||||||
&& cl.pack.join('.') == classType.pack.join('.')
|
&& cl.pack.join('.') == classType.pack.join('.')
|
||||||
): ct;
|
): ct;
|
||||||
default:
|
default:
|
||||||
direct();
|
direct();
|
||||||
}
|
}
|
||||||
@@ -95,8 +95,12 @@ class Sisyphus {
|
|||||||
TPath(toTypePath(baseType, params));
|
TPath(toTypePath(baseType, params));
|
||||||
case TFun(args, ret):
|
case TFun(args, ret):
|
||||||
TFunction(
|
TFunction(
|
||||||
[ for (a in args) a.opt ? nullable(toComplexType(a.t)) : toComplexType(a.t) ],
|
[for (a in args) {
|
||||||
toComplexType(ret));
|
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 }):
|
case TAnonymous(_.get() => { fields: fields }):
|
||||||
TAnonymous([ for (cf in fields) toField(cf) ]);
|
TAnonymous([ for (cf in fields) toField(cf) ]);
|
||||||
case TDynamic(t):
|
case TDynamic(t):
|
||||||
@@ -121,7 +125,7 @@ class Sisyphus {
|
|||||||
case _ == name => true: null;
|
case _ == name => true: null;
|
||||||
case v: v;
|
case v: v;
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
pack: baseType.pack,
|
pack: baseType.pack,
|
||||||
name: name,
|
name: name,
|
||||||
|
|||||||
55
src/tink/macro/TypedExprs.hx
Normal file
55
src/tink/macro/TypedExprs.hx
Normal 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;
|
||||||
|
}
|
||||||
@@ -172,6 +172,9 @@ class Types {
|
|||||||
#if haxe4
|
#if haxe4
|
||||||
isExtern: field.isExtern,
|
isExtern: field.isExtern,
|
||||||
isFinal: field.isFinal,
|
isFinal: field.isFinal,
|
||||||
|
#if (haxe >= version("4.2.0-rc.1"))
|
||||||
|
isAbstract: field.isAbstract,
|
||||||
|
#end
|
||||||
#end
|
#end
|
||||||
}:ClassField)
|
}:ClassField)
|
||||||
]);
|
]);
|
||||||
@@ -258,8 +261,7 @@ class Types {
|
|||||||
return if (once) t else reduce(t, false);
|
return if (once) t else reduce(t, false);
|
||||||
return switch type {
|
return switch type {
|
||||||
case TAbstract(_.get() => { name: 'Null', pack: [] }, [t]): rec(t);
|
case TAbstract(_.get() => { name: 'Null', pack: [] }, [t]): rec(t);
|
||||||
case TLazy(f): rec(f());
|
case TLazy(_) | TType(_): rec(Context.follow(type, once));
|
||||||
case TType(_, _): rec(Context.follow(type, once));
|
|
||||||
default: type;
|
default: type;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user