You can use Context.typeof()to get the type of expression - then you need to run several patterns to find out the type name. The following only works with classes, since it only matches TInst, but can be extended:
import haxe.macro.Context;
import haxe.macro.Expr;
class Container {
public macro function add(self:Expr, object:Expr):Expr {
var name = switch (Context.typeof(object)) {
case TInst(_.get() => t, _): t.name;
case _: throw "object type not found";
}
return macro $self.addChild($i{name}.id_, $object);
}
}
This will lead to the creation of the following code:
var c = new Container();
c.addChild(Child1.id_, new Child1());
c.addChild(Child2.id_, new Child2());
, _id , ( ) - t.pack $p{}, .