<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Wed, Mar 16, 2016 at 2:07 PM, Daniel Dilts via cfe-dev <span dir="ltr"><<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div>I have a type that I have gotten with this expression:</div><div>callExpr->getArg(i)->IgnoreImplicit()->getType()</div><div><br></div><div>I am matching it against a set of types that I want to process. Currently I am doing a string compare with the result of getAsString() and the desired type. This works for trivial types (int, float, etc.) but it doesn't scale to more complex types.</div></div></blockquote><div>Using getAsString() isn't the best way to determine types are the same. ASTContext has all the things you need to do this. Instead of doing:</div><div><br></div><div> QualType Ty = callExpr->getArg(i)->IgnoreImplicit()->getType();</div><div> if (Ty.getAsString() == "int") {...}</div><div><br></div><div>Use the type comparison function and the saved types by:</div><div><br></div><div> if (Context.hasSameType(Ty, Context.IntTy) {...}</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div><br></div><div>How would I test to see if the type is an instantiation of std::vector, and find the element type of the vector?</div></div></blockquote><div>This requires a bit more work and you'll need to traverse the many classes of Type. The specific Type is TemplateSpecializationType.</div><div><br></div><div><div> QualType Ty = callExpr->getArg(i)->IgnoreImplicit()->getType();</div></div><div> const TemplateSpecializationType *TST = Ty->getAs<TemplateSpecializationType>();</div><div> if (!TST) return;</div><div><br></div><div>Make sure you are dealing with a template specialization.</div><div><br></div><div> TemplatedDecl *TD = TST->getTemplateName().getAsTemplateDecl();</div><div> if (!TD) return;</div><div><br></div><div>The TemplateDecl stores information about the template being specialized, from it, you can check:</div><div><br></div><div> if (!TD->getIdentifier()->isStr("vector")) return;</div><div> if (!TD->isInStdNamespace()) return;</div><div><br></div><div>isStr() is a specialized function to check if the identifier matches a string literal.</div><div>isInStdNamespace() checks the namespace, looking through inline namespaces.</div><div><br></div><div> if (TST->getNumArgs < 1) return;</div><div><br></div><div>Checks that specialization of vector has at least on template argument. Use "< 1" instead of "!= 1" because std::vector is usually defined with additional template arguments beyond the first one, but are mostly defaulted away so users don't see them.</div><div><br></div><div> const TemplateArgument &TA = TST->getArg(0);</div><div><br></div><div>Get the first template argument.</div><div><br></div><div> if (TA.getKind() != TemplateArgument::Type) return;</div><div><br></div><div>Make sure the template argument is a type.</div><div><br></div><div> QualType ElementType = TA.getAsType();</div><div><br></div><div>Retrieve the type from the template argument, which for std::vector is the element type.</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<br>_______________________________________________<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="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
<br></blockquote></div><br></div></div>