CastXML / pygccxml

pygccxml is a specialized XML reader that reads the output from CastXML or GCCXML. It provides a simple framework to navigate C++ declarations, using Python classes.
Boost Software License 1.0
130 stars 44 forks source link

Loop over declarations inside a template #77

Open ombre5733 opened 7 years ago

ombre5733 commented 7 years ago

Is there a way to loop over the types defined inside a template? I'm currently testing the library with the following code:

namespace A {
struct B {
  struct D { bool d; };
};
struct D {};

template <typename T1, typename T2>
struct T {
  struct E { int e; };
};

T<B::D, bool> fun();
}

Starting with namespace A, I'm able to go to the template declaration but then I'm stuck.

>>> decls = pgx.parser.parse_string(cppsource, pgx_config)
>>> globalNamespace = pgx.declarations.get_global_namespace(decls)
>>> aNamespace = globalNamespace.namespace("A")

>>> x = aNamespace.declarations[2]
>>> x.decl_string
'::A::T<A::B::D, bool>'
>>> x.declarations
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'class_declaration_t' object has no attribute 'declarations'

Is there somewhere a link from class_declaration_t to the scope so that I can recurse into the template?

iMichka commented 7 years ago

I added a small example with c++ templates: http://pygccxml.readthedocs.io/en/develop/examples/templates/example.html

That should help you.

For the moment, castxml (and previously gccxml) output only the following: <Struct id="_15" name="T<ns::B::D, bool>" context="_7" location="f1:14" file="f1" line="14" incomplete="1"/>

So for the moment, we can only rely on the name attribute to extract information about the template parameters. The example will show you how to do this, using the declarations.templates.split helper method. This will allow you to loop over the template parameters.

The only thing is that what you will get is strings, not some pygccxml object that you can use directly. Ideally one would expect some declaration_t objects instead, which you can work on. But for this, I will need to change the castxml code, so this would be a long term project.

I hope this helps you.