pygccxml icon indicating copy to clipboard operation
pygccxml copied to clipboard

Loop over declarations inside a template

Open ombre5733 opened this issue 8 years ago • 1 comments

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?

ombre5733 avatar Apr 15 '17 18:04 ombre5733

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.

iMichka avatar Apr 15 '17 21:04 iMichka

Closing as this has been documented / explained. Please open a new issue if new needs arise.

iMichka avatar Nov 06 '24 22:11 iMichka