<div>Awesome, thanks!</div><div dir="auto"><br></div><div dir="auto">Tobi</div><div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Jul 8, 2020 at 4:48 PM David Rector <<a href="mailto:davrecthreads@gmail.com">davrecthreads@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;padding-left:1ex;border-left-color:rgb(204,204,204)"><div style="word-wrap:break-word;line-break:after-white-space"><div style="margin:0px;font-stretch:normal;line-height:normal">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:</div><div style="margin:0px;font-stretch:normal;line-height:normal;min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;line-height:normal">  1. We need the GetBaseType() function I reference below to become a public Type method</div><div style="margin:0px;font-stretch:normal;line-height:normal">  2. We probably should get rid of methods like getPointeeCXXRecordDecl(), which just add clutter and are straightforward to do with getAs<T>() and getDecl().</div><div style="margin:0px;font-stretch:normal;line-height:normal">  3. Type::getAs<T>() should be expanded to the following to make it more flexible:</div><div style="margin:0px;font-stretch:normal;line-height:normal;min-height:14px"><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div style="margin:0px;font-stretch:normal;line-height:normal;min-height:14px"><font face="Monaco" size="1" style="font-family:Monaco;color:rgb(0,0,0)"><span style="font-style:normal;font-family:Monaco">class Type {</span></font></div><div style="margin:0px;font-stretch:normal;line-height:normal;min-height:14px"><span style="font-family:Monaco;font-size:x-small">  //…</span></div><div style="margin:0px;font-stretch:normal;line-height:normal;min-height:14px"><span style="font-family:Monaco;font-size:x-small">  template<typename T, </span><font face="Monaco" size="1" style="font-family:Monaco;color:rgb(0,0,0)">         </font></div><div style="margin:0px;font-stretch:normal;line-height:normal;min-height:14px"><font face="Monaco" size="1" style="font-family:Monaco;color:rgb(0,0,0)">           boo</font><span style="font-size:10px;font-family:Monaco">l LookThroughSemantics = false> //PROPOSED</span></div><div style="margin:0px;font-stretch:normal;line-height:normal;min-height:14px"><span style="font-family:Monaco;font-size:10px">  T *getAs() {</span></div><div style="margin:0px;font-stretch:normal;line-height:normal;min-height:14px"><span style="font-family:Monaco;font-size:10px">    // First, desugar as usual, looking for T as you go.</span></div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">    // Then if you haven’t found T and LookThroughSemantics==true, </div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">    // you call e.g. getPointeeType(), getElementType(), etc. if  </div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">    // the type is some kind of canonical wrapper type, then keep looking</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">    // through until you run out of both sugar and pointer/array/other </div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">    // simple wrapper types (i.e. are down to a BuiltinType or a TagType).</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">  }</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">};</div></blockquote><div style="margin:0px;font-stretch:normal;line-height:normal;min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="white-space:pre-wrap">  </span>(Side note: if the previously-proposed Expr::getAs<T>() ever gets off the ground, we can make corresponding adjustments to make it more flexible as well:)</div><div style="margin:0px;font-stretch:normal;line-height:normal;min-height:14px"><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">class Expr : public Stmt {</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">  //…</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">  //PROPOSED:</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">  template<typename T, </div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco"><span style="white-space:pre-wrap;font-family:Monaco">       </span>   bool LookThroughSemantics = true, </div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco"><span style="white-space:pre-wrap;font-family:Monaco"> </span>   bool LookThroughParens = true, </div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco"><span style="white-space:pre-wrap;font-family:Monaco">    </span>   bool LookThroughUnaryOps = false></div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">  T *getAs() {</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">    // peel of implicit semantics, and/or ParenExprs etc and/or unary ops or whatever,</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">    // depending on options, looking for T as you go.</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">  }</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">};</div></blockquote><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco;min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;line-height:normal">  4. I think we should even allow <span style="font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">someType->getAs<CXXRecordDecl>()</span>, which would just call <span style="font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">dyn_cast<CXXRecordDecl>(someType->getAs<RecordType>()->getDecl())</span>.  I.e. allow getAs<T>() 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<T>() 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.</div><p style="margin:0px;font-stretch:normal;line-height:normal;min-height:14px"><span style="white-space:pre-wrap">      </span><br></p><div style="margin:0px;font-stretch:normal;line-height:normal">—</div><div style="margin:0px;font-stretch:normal;line-height:normal;min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;line-height:normal">Now, onto solving your problem, if I understand it correctly:</div><div style="margin:0px;font-stretch:normal;line-height:normal"><br></div><div style="margin:0px;font-stretch:normal;line-height:normal">If you know your VarDecl *VD has a pointer or reference type, and you just want to know if the pointee type, after removing const/volatile/etc. qualifiers, is a struct named "foo", this should do it:</div><div style="margin:0px;font-stretch:normal;line-height:normal;min-height:14px"><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">bool hasPointeeRecordTypeNamedFoo(VarDecl *VD) {</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">  QualType PointeeType = VD->getType()->getPointeeType();</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">  assert(!PointeeType.isNull() && "Expected VD’s type had a pointee type");</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">  if (RecordType *RT = PointeeType->getAs<RecordType>()) {</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">    if (RT->getDecl()->getName() == "foo")</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">      return true;</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">  }</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">  return false;</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">}</div></blockquote><div style="margin:0px;font-stretch:normal;line-height:normal;min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;line-height:normal">If however you are not sure whether the type of VD is a pointer type, or a pointer to a pointer type, or an array, or an array of pointers etc, and really you just want to know if the base type after removing all the pointers and arrays and whatever. is "struct foo", this static method in clang/lib/AST/DeclPrinter.cpp helps:</div><div style="margin:0px;font-stretch:normal;line-height:normal;min-height:14px"><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">//>From clang/lib/AST/DeclPrinter.cpp:</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">static QualType GetBaseType(QualType T) {</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">  // FIXME: This should be in the Type class!</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">  QualType BaseType = T;</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">  while (!BaseType->isSpecifierType()) {</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">    if (const PointerType *PTy = BaseType->getAs<PointerType>())</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">      BaseType = PTy->getPointeeType();</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">    else if (const BlockPointerType *BPy = BaseType->getAs<BlockPointerType>())</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">      BaseType = BPy->getPointeeType();</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">    else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">      BaseType = ATy->getElementType();</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">    else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">      BaseType = FTy->getReturnType();</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">    else if (const VectorType *VTy = BaseType->getAs<VectorType>())</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">      BaseType = VTy->getElementType();</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">    else if (const ReferenceType *RTy = BaseType->getAs<ReferenceType>())</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">      BaseType = RTy->getPointeeType();</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">    else if (const AutoType *ATy = BaseType->getAs<AutoType>())</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">      BaseType = ATy->getDeducedType();</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">    else if (const ParenType *PTy = BaseType->getAs<ParenType>())</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">      BaseType = PTy->desugar();</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">    else</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">      // This must be a syntax error.</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">      break;</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">  }</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">  return BaseType;</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">} </div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">bool hasBaseRecordTypeNamedFoo(VarDecl *VD) {</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">  QualType BaseType = GetBaseType(VD->getType());</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">  if (BaseType->getAs<RecordType>()) {</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">    if (RT->getDecl()->getName() == "foo")</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">      return true;</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">  }</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">  else</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">    assert(isa<BuiltinType>(BaseType.getTypePtr()) ||</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">           isa<EnumType>(BaseType.getTypePtr() &&</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">           "Fix GetBaseType, it did not unwrap this as expected…");</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">  return false;</div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco">}</div></blockquote><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco;min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;line-height:normal">Hope that helps, good luck,</div><div style="margin:0px;font-stretch:normal;line-height:normal;min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;line-height:normal">Dave</div><div><br><blockquote type="cite"></blockquote></div></div><div style="word-wrap:break-word;line-break:after-white-space"><div><blockquote type="cite"><div>On Jul 8, 2020, at 3:08 PM, Tobi Popoola via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:</div><br></blockquote></div></div><div style="word-wrap:break-word;line-break:after-white-space"><div><blockquote type="cite"><div></div></blockquote></div></div><div style="word-wrap:break-word;line-break:after-white-space"><div><blockquote type="cite"><div><div style="word-wrap:break-word;line-break:after-white-space">Hi,<div><br></div><div><br></div><div>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</div><div><br></div><div><br></div><div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco"><span style="font-variant-ligatures:no-common-ligatures;font-family:Monaco">auto</span><span style="font-variant-ligatures:no-common-ligatures;font-family:Monaco"> ParamStructType = VarDecl->getOriginalType().getPointeeCXXRecordDec();</span></div></div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco"><span style="font-variant-ligatures:no-common-ligatures;font-family:Monaco"><br></span></div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco"><span style="font-variant-ligatures:no-common-ligatures;font-family:Monaco">// trying to check if ParamStructType is some 'struct foo’</span></div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco"><span style="font-variant-ligatures:no-common-ligatures;font-family:Monaco"><br></span></div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco"><span style="font-variant-ligatures:no-common-ligatures;font-family:Monaco"><br></span></div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco"><span style="font-variant-ligatures:no-common-ligatures;font-family:Monaco"><span style="font-family:Helvetica;font-size:12px">Tobi </span></span></div><div style="margin:0px;font-stretch:normal;font-size:10px;line-height:normal;font-family:Monaco"><br></div></div></div></blockquote></div></div><div style="word-wrap:break-word;line-break:after-white-space"><div><blockquote type="cite"><div>_______________________________________________<br>cfe-dev mailing list<br><a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br><a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br></div></blockquote></div><br></div></blockquote></div></div>-- <br><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div>Regards,</div><div>Tobi Goodness Popoola</div><div>President,</div><div><b><span><span>National Society of Black Engineers (NSBE)</span></span></b></div><div><b><span><span>Boise State University<br></span></span></b></div><div><a href="mailto:tobipopoola@u.boisestate.edu" target="_blank">tobipopoola@u.boisestate.edu</a><br></div></div></div></div></div></div></div>