diff --git a/src/tink/macro/Sisyphus.hx b/src/tink/macro/Sisyphus.hx new file mode 100644 index 0000000..55b559b --- /dev/null +++ b/src/tink/macro/Sisyphus.hx @@ -0,0 +1,105 @@ +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"; + } + } + 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) : Null return + switch (type) { + case null: + null; + case TMono(_.get() => t): + t == null ? null : 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) : 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)) ], + } + } +} \ No newline at end of file diff --git a/src/tink/macro/Types.hx b/src/tink/macro/Types.hx index 77c3a2e..4a40208 100644 --- a/src/tink/macro/Types.hx +++ b/src/tink/macro/Types.hx @@ -15,10 +15,6 @@ using tink.CoreApi; class Types { static var types = new MapType>(); 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) @@ -152,7 +148,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);