Macro.hx 899 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package;
  2. import haxe.ds.Option;
  3. import haxe.macro.Context;
  4. import haxe.macro.Expr;
  5. import haxe.macro.Type;
  6. class Macro {
  7. public static function build() {
  8. switch Context.getLocalType() {
  9. case TInst(_, [Context.follow(_) => TAnonymous(_.get() => a)]):
  10. return buildAnon(a);
  11. case _: throw 'assert';
  12. }
  13. }
  14. static function buildAnon(a:AnonType):Type {
  15. var sig = Context.signature(a);
  16. var name = "Type" + sig;
  17. try return Context.getType(name) catch(e:Dynamic) {}
  18. var pos = Context.currentPos();
  19. var fields = [];
  20. for(field in a.fields) {
  21. var meta = field.meta.get();
  22. fields.push({
  23. kind: FieldType.FVar(Context.toComplexType(field.type)),
  24. name: field.name,
  25. pos: field.pos,
  26. meta: meta,
  27. });
  28. }
  29. Context.defineType({
  30. fields: fields,
  31. kind: TDStructure,
  32. name: name,
  33. pack: [],
  34. pos: pos,
  35. });
  36. return Context.getType(name);
  37. }
  38. }