This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Getting a tree node for a field of a variable
- From: Andrew Haley <aph at redhat dot com>
- To: "Ferad Zyulkyarov" <feradz at gmail dot com>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Fri, 19 Jan 2007 11:54:20 +0000
- Subject: Re: Getting a tree node for a field of a variable
- References: <f7b109350701180748i2d022e2dy9607645cb8996be8@mail.gmail.com> <m3vej49td9.fsf@dhcp-172-18-118-195.corp.google.com> <f7b109350701190342y7f13e975pf97f4f0c1197e845@mail.gmail.com>
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);