Checking Datatype of ParmVarDecl

Hi,

Is there a way to check the struct type of a ParmVarDecl object. Here is a snippet of what I have been trying to do so far

auto ParamStructType = VarDecl->getOriginalType().getPointeeCXXRecordDec();

// trying to check if ParamStructType is some 'struct foo’

Tobi

I’ll show you exactly how to do what I believe you want below, but first at the top here I want to make a point for those who design the Type interface how I think we could make it easier to use and understand:

  1. We need the GetBaseType() function I reference below to become a public Type method
  2. We probably should get rid of methods like getPointeeCXXRecordDecl(), which just add clutter and are straightforward to do with getAs() and getDecl().
  3. Type::getAs() should be expanded to the following to make it more flexible:

class Type {
//…
template<typename T,
bool LookThroughSemantics = false> //PROPOSED
T *getAs() {
// First, desugar as usual, looking for T as you go.
// Then if you haven’t found T and LookThroughSemantics==true,
// you call e.g. getPointeeType(), getElementType(), etc. if
// the type is some kind of canonical wrapper type, then keep looking
// through until you run out of both sugar and pointer/array/other
// simple wrapper types (i.e. are down to a BuiltinType or a TagType).
}
};

(Side note: if the previously-proposed Expr::getAs() ever gets off the ground, we can make corresponding adjustments to make it more flexible as well:)

class Expr : public Stmt {
//…
//PROPOSED:
template<typename T,
bool LookThroughSemantics = true,
bool LookThroughParens = true,
bool LookThroughUnaryOps = false>
T *getAs() {
// peel of implicit semantics, and/or ParenExprs etc and/or unary ops or whatever,
// depending on options, looking for T as you go.
}
};

  1. I think we should even allow someType->getAs(), which would just call dyn_cast(someType->getAs()->getDecl()). I.e. allow getAs() to go between node categories, whenever it is straightforward and unambiguous to do so. Could really simplify the interface if you always knew to just try getAs() as a multi-purpose tool to get one thing as another, with bool template params to specify what kind of stuff you’re okay discarding in the search for T.

Awesome, thanks!

Tobi