Re: Getting a tree node for a field of a variable

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Ferad Zyulkyarov writes:
 > Hi,
 > 
 > > Look in tree.def.
 > >
 > > Given the RECORD_TYPE node, walk down TYPE_FIELDS looking for the
 > > FIELD_DECL that you want.
 > >
 > > To assign to a field use a COMPONENT_REF.
 > 
 > Is it possible to write a short example how a it could be referred the
 > tree of variable field? Let's say that we have somewhere defined the
 > following source code:
 > 
 > --- code ---
 > typedef struct MyType
 > {
 >   int field1;
 >   int field2;
 > }
 > 
 > MyType *var;
 > --- code ---
 > 
 > What I want is to get the tree node of "var->field1". For now I can
 > get the tree of the "var" declaration with the code bellow.
 > 
 > tree var_decl = lookup_name(get_identifier("var"));
 > 
 > And I suppose that, having "var_decl" I can get a tree to the "field1"
 > field of "var". Is there any function e.g. lookup_decl_field that does
 > it?

Pseudocode:

You want to use TREE_TYPE to get the type of the var_decl, and then:

lookup_field (name, type)
{
      for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
	if (DECL_NAME (field) == name)
	  return field;
}

use it like this:

      ref = build3 (COMPONENT_REF, sometype, ref,
		    lookup_field (name, record_type),
		    NULL_TREE);

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux