Resolving and listing fields of a struct

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Resolving and listing fields of a struct

Jens Frieben
Hi,

I am trying to programmatically list all types used as parameters in function calls like 'AddPortInfo("myperson", this->aPersonField);'

The 'aPersonField' is defined in a header file via 'personStruct aPersonField;'

I index the project and create an AST of the currently opened file. Via a visitor, I find all FunctionCallExpressions, and the necessary fields. I now face the problem of finding the definition of the Struct in the header files to list all fields of that struct.

struct personStruct{
    int   age;
    float weight;
    char  name[25];
  };

Can someone point me in the right direction how to search the index for the declaration, so that I can list the fields in the struct? I am currently trying to get this information by using the index and bindings. However, this is not working.

IASTName sourceName = fieldReference.getFieldName();
IBinding myBinding = index.findBinding(sourceName);
IBinding resovledBinding = sourceName.resolveBinding();
IBinding owner = resovledBinding.getOwner();
System.out.println("Owner: " + owner.getName());

My ultimate goal is to write an XML file similar to
<Port name="myperson" type="personStruct">
        <Types>
            <td:Struct name="personStruct">
                <td:Fields>
                    <td:Field name="age" type="int" />
                    <td:Field name="weight" type="int" />
                    <td:Field name="name" type="string" />
                </td:Fields>
            </td:Struct>
        </Types>
</Port>

Any help is appreciated.
Thanks and best regards.

Jens

_______________________________________________
cdt-dev mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/cdt-dev
Reply | Threaded
Open this post in threaded view
|

Re: Resolving and listing fields of a struct

Nathan Ridge
> Can someone point me in the right direction how to search the
> index for the declaration, so that I can list the fields in the struct? I
> am currently trying to get this information by using the index and
> bindings. However, this is not working.
>
> IASTName sourceName = fieldReference.getFieldName();
> IBinding myBinding = index.findBinding(sourceName);
> IBinding resovledBinding = sourceName.resolveBinding();
> IBinding owner = resovledBinding.getOwner();
> System.out.println("Owner: " + owner.getName());

Apart from the call to index.findBinding(), which you don't need and whose result you're not using anyways, this looks fine. 'resolvedBinding' should be the binding representing the field, and 'owner' the binding representing the structure that contains the field.

If that's not what you're seeing, could you be more specific about what you are seeing? Is something null?

Regards,
Nate
_______________________________________________
cdt-dev mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/cdt-dev