digitalmars.D.learn - offTi not working... workaround for bug 1348? Tuples perhaps?
- Chad J <gamerChad _spamIsBad_gmail.com> Aug 14 2007
- Robert Fraser <fraserofthenight gmail.com> Aug 14 2007
- Chad J <gamerChad _spamIsBad_gmail.com> Aug 14 2007
My objective is to make a parsetree framework that lets me dump the
parsetree into a visual graph for debugging and whatnot. Here is my
current setup:
// Class that all elements in the tree inherit from.
class Node
{
void printTree() {...} //right now it just prints to console,
//graphics/gui come later
template registerNode()
{
Node[] _getChildren()
{
OffsetTypeInfo[] otis = typeid(typeof(this)).offTi;
writefln( otis.length ); // prints 0 always, crap! bug 1348.
... // normally would do stuff with otis
}
}
}
// Generic element of the parsetree
class A
{
// Boilerplate defined in "Node"
mixin registerNode!();
// Children in various incantations.
Node lhs;
Node rhs;
Node[] otherNodes;
// Just a variable that shouldn't end
// up being a vertex in the resulting graph.
int someStateValue = 0;
}
Inside of the _getChildren function I am trying to find some way to
iterate through ALL members of each Node. By ALL members, I mean the
members of the most descended class, and its base class, and it base
class's base class, and so on up until, but not including, Node's members.
Anyhow, now the problem part. I can't seem to iterate through the
members of a class by using OffsetTypeInfo, since any class's
corresponding offTi always has a length of zero. I have also tried
tuples by using the this.tupleof property. Then I set up a loop like
this inside of _getChildren:
Node[] children = new Node[0];
foreach( member; this.tupleof )
{
static if ( is( Node : typeof(member) )
{
children ~= member;
}
}
Originally I expected 'member' to just be some kind of alias for its
corresponding identifier, such that member might be one of 'lhs', 'rhs',
or 'otherNodes' in this example. Unfortunately, member is always null.
I suspect that's because 'member' is a part of a compile-time
expression tuple, and is itself a compile-time constant, thus at compile
time it is always null. Well if I can somehow coax a run-time value out
of member, I'd be in business, but I don't know how to do that.
So, does anyone have a way of doing this type of thing?
TIA,
Chad
Aug 14 2007
Chad J Wrote:Node[] children = new Node[0]; foreach( member; this.tupleof ) { static if ( is( Node : typeof(member) ) { children ~= member; } }
if(cast(Node) member) ...? Haven't tested it, but it might work.
Aug 14 2007
Robert Fraser wrote:Chad J Wrote:Node[] children = new Node[0]; foreach( member; this.tupleof ) { static if ( is( Node : typeof(member) ) { children ~= member; } }
if(cast(Node) member) ...? Haven't tested it, but it might work.
Ah, I probably should've mentioned that the "children ~= member;" line is where the problem occurs. The static if works fine. It is appending something to children when it should be, but that something is always null :(
Aug 14 2007








Chad J <gamerChad _spamIsBad_gmail.com>