sdgfsdh
sdgfsdh

Reputation: 37045

How do I get the name of a TemplateTypeParmType in Clang?

I am using Clang to print out some information about my AST.

For example:

template<typename T>
class Slices : public Fruit {
  T fruit;
  int numberOfSlices;
  public: 
    Slices(T fruit, int numberOfSlices)
      : fruit(fruit), numberOfSlices(numberOfSlices) {
    }
    std::string getName() {
      return "Sliced " + fruit.getName();
    }
    int getNumberOfSlices() {
      return numberOfSlices;
    }
};

Should print something like:

template<typename T>
Slices
  Fields: 
    T fruit;
    int numberOfSlices
  Methods: 
...

I have written a visitor that hits a TemplateTypeParmType node at T in T fruit.

However, this node doesn't seem to have any identifiers attached to it. These are both null:

x->getDecl()
x->getIdentifier()

How do I extract the templated-name (here T) from this node?

Upvotes: 2

Views: 692

Answers (1)

realharo
realharo

Reputation: 21

If you also have the QualType for that type (you can get it from the FieldDecl for every field, etc.), you can just call getAsString() on that. If not, you can create one with no qualifiers, i.e.

auto name = clang::QualType(type, 0).getAsString();

Of course the qualifier information (such as const) will then be missing. Make sure the type you have is not a canonical type (http://clang.llvm.org/docs/InternalsManual.html#canonical-types), otherwise you'll just get a name like type-parameter-0-0.

I haven't found a way to get the decl or the identifier, but if you're willing to step through the clang code with a debugger, you can probably find where the getAsString method gets this information from.

Upvotes: 2

Related Questions