1

I want to handle template arguments diffrently so for the code:

template <class T> class A { 
public:
     A() {} 
};

void faa(A<int>& param);

I would like to know that param is a template specialisation and get access it's parameters.

So I wrote an ASTVisitor with function

 bool VisitFunctionDecl(FunctionDecl *f) {
    std::cout<< "VisitFunctionDecl" <<std::endl;

    const DependentTemplateSpecializationType* t1;
    const TemplateSpecializationType* t2;
    for(ParmVarDecl* p :f->params())
    {

        t1=p->getType()->getAs<DependentTemplateSpecializationType>();

        t2=p->getType()->getAs<TemplateSpecializationType>();

        if(t1!=nullptr||t2!=nullptr)
        {
            std::cout<< "template param found" <<std::endl;
        }
    }

    return true;
}

But those casts are both nullptr always - I never get the template param found output.

What am I doing wrong? Is there any other way to cast t to some king of type allowing checking of the template parameters?

1 Answer 1

1

Type of A<int>& is LValueReference (can be checked with getTypeClassName()). What you are probably trying to get is type pointed by reference. You can get it with getNonReferenceType() method.

bool VisitFunctionDecl(FunctionDecl *f) {
    llvm::errs() << "VisitFunctionDecl:" << f->getQualifiedNameAsString()
            << "\n";
    for (ParmVarDecl* p : f->params()) {

        llvm::errs() << p->getType().getAsString() << " -> "
                << p->getType()->getTypeClassName() << "\n";
        llvm::errs() << "isPointerType: "
                << p->getType()->hasPointerRepresentation() << "\n"
                << "isTemplateSpecialization: "
                << (nullptr != p->getType().getNonReferenceType()->getAs<
                                TemplateSpecializationType>()) << "\n";
    }
    return true;
}

output is:

VisitFunctionDecl:faa
const A<int> & -> LValueReference
isPointerType: 1
isTemplateSpecialization: 1
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.