Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 1 | //===- USRGeneration.cpp - Routines for USR generation --------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Argyrios Kyrtzidis | 15a2fcc | 2013-08-17 00:40:41 +0000 | [diff] [blame] | 10 | #include "clang/Index/USRGeneration.h" |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 11 | #include "clang/AST/ASTContext.h" |
| 12 | #include "clang/AST/DeclTemplate.h" |
| 13 | #include "clang/AST/DeclVisitor.h" |
Dmitri Gribenko | 237769e | 2014-03-28 22:21:26 +0000 | [diff] [blame] | 14 | #include "clang/Lex/PreprocessingRecord.h" |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Path.h" |
| 16 | #include "llvm/Support/raw_ostream.h" |
| 17 | |
| 18 | using namespace clang; |
Argyrios Kyrtzidis | 5234b49 | 2013-08-21 00:49:25 +0000 | [diff] [blame] | 19 | using namespace clang::index; |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 20 | |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | // USR generation. |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | |
Dmitri Gribenko | 237769e | 2014-03-28 22:21:26 +0000 | [diff] [blame] | 25 | /// \returns true on error. |
| 26 | static bool printLoc(llvm::raw_ostream &OS, SourceLocation Loc, |
| 27 | const SourceManager &SM, bool IncludeOffset) { |
| 28 | if (Loc.isInvalid()) { |
| 29 | return true; |
| 30 | } |
| 31 | Loc = SM.getExpansionLoc(Loc); |
| 32 | const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(Loc); |
| 33 | const FileEntry *FE = SM.getFileEntryForID(Decomposed.first); |
| 34 | if (FE) { |
| 35 | OS << llvm::sys::path::filename(FE->getName()); |
| 36 | } else { |
| 37 | // This case really isn't interesting. |
| 38 | return true; |
| 39 | } |
| 40 | if (IncludeOffset) { |
| 41 | // Use the offest into the FileID to represent the location. Using |
| 42 | // a line/column can cause us to look back at the original source file, |
| 43 | // which is expensive. |
| 44 | OS << '@' << Decomposed.second; |
| 45 | } |
| 46 | return false; |
| 47 | } |
| 48 | |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 49 | static StringRef GetExternalSourceContainer(const NamedDecl *D) { |
| 50 | if (!D) |
| 51 | return StringRef(); |
| 52 | if (auto *attr = D->getAttr<ExternalSourceSymbolAttr>()) { |
| 53 | return attr->getDefinedIn(); |
| 54 | } |
| 55 | return StringRef(); |
| 56 | } |
| 57 | |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 58 | namespace { |
| 59 | class USRGenerator : public ConstDeclVisitor<USRGenerator> { |
| 60 | SmallVectorImpl<char> &Buf; |
| 61 | llvm::raw_svector_ostream Out; |
| 62 | bool IgnoreResults; |
| 63 | ASTContext *Context; |
| 64 | bool generatedLoc; |
| 65 | |
| 66 | llvm::DenseMap<const Type *, unsigned> TypeSubstitutions; |
| 67 | |
| 68 | public: |
| 69 | explicit USRGenerator(ASTContext *Ctx, SmallVectorImpl<char> &Buf) |
| 70 | : Buf(Buf), |
| 71 | Out(Buf), |
| 72 | IgnoreResults(false), |
| 73 | Context(Ctx), |
| 74 | generatedLoc(false) |
| 75 | { |
| 76 | // Add the USR space prefix. |
Argyrios Kyrtzidis | 5234b49 | 2013-08-21 00:49:25 +0000 | [diff] [blame] | 77 | Out << getUSRSpacePrefix(); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | bool ignoreResults() const { return IgnoreResults; } |
| 81 | |
| 82 | // Visitation methods from generating USRs from AST elements. |
| 83 | void VisitDeclContext(const DeclContext *D); |
| 84 | void VisitFieldDecl(const FieldDecl *D); |
| 85 | void VisitFunctionDecl(const FunctionDecl *D); |
| 86 | void VisitNamedDecl(const NamedDecl *D); |
| 87 | void VisitNamespaceDecl(const NamespaceDecl *D); |
| 88 | void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D); |
| 89 | void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D); |
| 90 | void VisitClassTemplateDecl(const ClassTemplateDecl *D); |
| 91 | void VisitObjCContainerDecl(const ObjCContainerDecl *CD); |
| 92 | void VisitObjCMethodDecl(const ObjCMethodDecl *MD); |
| 93 | void VisitObjCPropertyDecl(const ObjCPropertyDecl *D); |
| 94 | void VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D); |
| 95 | void VisitTagDecl(const TagDecl *D); |
| 96 | void VisitTypedefDecl(const TypedefDecl *D); |
| 97 | void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D); |
| 98 | void VisitVarDecl(const VarDecl *D); |
| 99 | void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D); |
| 100 | void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D); |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 101 | |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 102 | void VisitLinkageSpecDecl(const LinkageSpecDecl *D) { |
| 103 | IgnoreResults = true; |
| 104 | } |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 105 | |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 106 | void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) { |
| 107 | IgnoreResults = true; |
| 108 | } |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 109 | |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 110 | void VisitUsingDecl(const UsingDecl *D) { |
| 111 | IgnoreResults = true; |
| 112 | } |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 113 | |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 114 | void VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D) { |
| 115 | IgnoreResults = true; |
| 116 | } |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 117 | |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 118 | void VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D) { |
| 119 | IgnoreResults = true; |
| 120 | } |
Argyrios Kyrtzidis | d3ba410 | 2014-02-23 18:23:29 +0000 | [diff] [blame] | 121 | |
| 122 | bool ShouldGenerateLocation(const NamedDecl *D); |
| 123 | |
| 124 | bool isLocal(const NamedDecl *D) { |
Craig Topper | 236bde3 | 2014-05-26 06:21:51 +0000 | [diff] [blame] | 125 | return D->getParentFunctionOrMethod() != nullptr; |
Argyrios Kyrtzidis | d3ba410 | 2014-02-23 18:23:29 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 128 | void GenExtSymbolContainer(const NamedDecl *D); |
| 129 | |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 130 | /// Generate the string component containing the location of the |
| 131 | /// declaration. |
Argyrios Kyrtzidis | d3ba410 | 2014-02-23 18:23:29 +0000 | [diff] [blame] | 132 | bool GenLoc(const Decl *D, bool IncludeOffset); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 133 | |
| 134 | /// String generation methods used both by the visitation methods |
| 135 | /// and from other clients that want to directly generate USRs. These |
| 136 | /// methods do not construct complete USRs (which incorporate the parents |
| 137 | /// of an AST element), but only the fragments concerning the AST element |
| 138 | /// itself. |
| 139 | |
| 140 | /// Generate a USR for an Objective-C class. |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 141 | void GenObjCClass(StringRef cls, StringRef ext) { |
| 142 | generateUSRForObjCClass(cls, Out, ext); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 143 | } |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 144 | |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 145 | /// Generate a USR for an Objective-C class category. |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 146 | void GenObjCCategory(StringRef cls, StringRef cat, |
| 147 | StringRef clsExt, StringRef catExt) { |
| 148 | generateUSRForObjCCategory(cls, cat, Out, clsExt, catExt); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 149 | } |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 150 | |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 151 | /// Generate a USR fragment for an Objective-C property. |
Argyrios Kyrtzidis | d9849a9 | 2016-07-15 22:18:19 +0000 | [diff] [blame] | 152 | void GenObjCProperty(StringRef prop, bool isClassProp) { |
| 153 | generateUSRForObjCProperty(prop, isClassProp, Out); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 154 | } |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 155 | |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 156 | /// Generate a USR for an Objective-C protocol. |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 157 | void GenObjCProtocol(StringRef prot, StringRef ext) { |
| 158 | generateUSRForObjCProtocol(prot, Out, ext); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void VisitType(QualType T); |
| 162 | void VisitTemplateParameterList(const TemplateParameterList *Params); |
| 163 | void VisitTemplateName(TemplateName Name); |
| 164 | void VisitTemplateArgument(const TemplateArgument &Arg); |
| 165 | |
| 166 | /// Emit a Decl's name using NamedDecl::printName() and return true if |
| 167 | /// the decl had no name. |
| 168 | bool EmitDeclName(const NamedDecl *D); |
| 169 | }; |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 170 | } // end anonymous namespace |
| 171 | |
| 172 | //===----------------------------------------------------------------------===// |
| 173 | // Generating USRs from ASTS. |
| 174 | //===----------------------------------------------------------------------===// |
| 175 | |
| 176 | bool USRGenerator::EmitDeclName(const NamedDecl *D) { |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 177 | const unsigned startSize = Buf.size(); |
| 178 | D->printName(Out); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 179 | const unsigned endSize = Buf.size(); |
| 180 | return startSize == endSize; |
| 181 | } |
| 182 | |
Argyrios Kyrtzidis | d3ba410 | 2014-02-23 18:23:29 +0000 | [diff] [blame] | 183 | bool USRGenerator::ShouldGenerateLocation(const NamedDecl *D) { |
| 184 | if (D->isExternallyVisible()) |
| 185 | return false; |
| 186 | if (D->getParentFunctionOrMethod()) |
| 187 | return true; |
Argyrios Kyrtzidis | f12918d | 2016-11-02 23:42:33 +0000 | [diff] [blame] | 188 | SourceLocation Loc = D->getLocation(); |
| 189 | if (Loc.isInvalid()) |
| 190 | return false; |
Argyrios Kyrtzidis | d3ba410 | 2014-02-23 18:23:29 +0000 | [diff] [blame] | 191 | const SourceManager &SM = Context->getSourceManager(); |
Argyrios Kyrtzidis | f12918d | 2016-11-02 23:42:33 +0000 | [diff] [blame] | 192 | return !SM.isInSystemHeader(Loc); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | void USRGenerator::VisitDeclContext(const DeclContext *DC) { |
| 196 | if (const NamedDecl *D = dyn_cast<NamedDecl>(DC)) |
| 197 | Visit(D); |
| 198 | } |
| 199 | |
| 200 | void USRGenerator::VisitFieldDecl(const FieldDecl *D) { |
| 201 | // The USR for an ivar declared in a class extension is based on the |
| 202 | // ObjCInterfaceDecl, not the ObjCCategoryDecl. |
| 203 | if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D)) |
| 204 | Visit(ID); |
| 205 | else |
| 206 | VisitDeclContext(D->getDeclContext()); |
| 207 | Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@"); |
| 208 | if (EmitDeclName(D)) { |
| 209 | // Bit fields can be anonymous. |
| 210 | IgnoreResults = true; |
| 211 | return; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | void USRGenerator::VisitFunctionDecl(const FunctionDecl *D) { |
Argyrios Kyrtzidis | d3ba410 | 2014-02-23 18:23:29 +0000 | [diff] [blame] | 216 | if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D))) |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 217 | return; |
| 218 | |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 219 | const unsigned StartSize = Buf.size(); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 220 | VisitDeclContext(D->getDeclContext()); |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 221 | if (Buf.size() == StartSize) |
| 222 | GenExtSymbolContainer(D); |
| 223 | |
Argyrios Kyrtzidis | d06ce40 | 2014-12-08 08:48:11 +0000 | [diff] [blame] | 224 | bool IsTemplate = false; |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 225 | if (FunctionTemplateDecl *FunTmpl = D->getDescribedFunctionTemplate()) { |
Argyrios Kyrtzidis | d06ce40 | 2014-12-08 08:48:11 +0000 | [diff] [blame] | 226 | IsTemplate = true; |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 227 | Out << "@FT@"; |
| 228 | VisitTemplateParameterList(FunTmpl->getTemplateParameters()); |
| 229 | } else |
| 230 | Out << "@F@"; |
Argyrios Kyrtzidis | d571908 | 2016-02-15 01:32:36 +0000 | [diff] [blame] | 231 | |
| 232 | PrintingPolicy Policy(Context->getLangOpts()); |
| 233 | // Forward references can have different template argument names. Suppress the |
| 234 | // template argument names in constructors to make their USR more stable. |
| 235 | Policy.SuppressTemplateArgsInCXXConstructors = true; |
| 236 | D->getDeclName().print(Out, Policy); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 237 | |
| 238 | ASTContext &Ctx = *Context; |
Argyrios Kyrtzidis | 2682f9e | 2016-03-04 07:17:48 +0000 | [diff] [blame] | 239 | if ((!Ctx.getLangOpts().CPlusPlus || D->isExternC()) && |
| 240 | !D->hasAttr<OverloadableAttr>()) |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 241 | return; |
| 242 | |
| 243 | if (const TemplateArgumentList * |
| 244 | SpecArgs = D->getTemplateSpecializationArgs()) { |
| 245 | Out << '<'; |
| 246 | for (unsigned I = 0, N = SpecArgs->size(); I != N; ++I) { |
| 247 | Out << '#'; |
| 248 | VisitTemplateArgument(SpecArgs->get(I)); |
| 249 | } |
| 250 | Out << '>'; |
| 251 | } |
| 252 | |
| 253 | // Mangle in type information for the arguments. |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 254 | for (auto PD : D->parameters()) { |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 255 | Out << '#'; |
Aaron Ballman | f6bf62e | 2014-03-07 15:12:56 +0000 | [diff] [blame] | 256 | VisitType(PD->getType()); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 257 | } |
| 258 | if (D->isVariadic()) |
| 259 | Out << '.'; |
Argyrios Kyrtzidis | d06ce40 | 2014-12-08 08:48:11 +0000 | [diff] [blame] | 260 | if (IsTemplate) { |
| 261 | // Function templates can be overloaded by return type, for example: |
| 262 | // \code |
| 263 | // template <class T> typename T::A foo() {} |
| 264 | // template <class T> typename T::B foo() {} |
| 265 | // \endcode |
| 266 | Out << '#'; |
| 267 | VisitType(D->getReturnType()); |
| 268 | } |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 269 | Out << '#'; |
| 270 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { |
| 271 | if (MD->isStatic()) |
| 272 | Out << 'S'; |
| 273 | if (unsigned quals = MD->getTypeQualifiers()) |
| 274 | Out << (char)('0' + quals); |
Argyrios Kyrtzidis | f581909 | 2014-12-08 08:48:21 +0000 | [diff] [blame] | 275 | switch (MD->getRefQualifier()) { |
| 276 | case RQ_None: break; |
| 277 | case RQ_LValue: Out << '&'; break; |
| 278 | case RQ_RValue: Out << "&&"; break; |
| 279 | } |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
| 283 | void USRGenerator::VisitNamedDecl(const NamedDecl *D) { |
| 284 | VisitDeclContext(D->getDeclContext()); |
| 285 | Out << "@"; |
| 286 | |
| 287 | if (EmitDeclName(D)) { |
| 288 | // The string can be empty if the declaration has no name; e.g., it is |
| 289 | // the ParmDecl with no name for declaration of a function pointer type, |
| 290 | // e.g.: void (*f)(void *); |
| 291 | // In this case, don't generate a USR. |
| 292 | IgnoreResults = true; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | void USRGenerator::VisitVarDecl(const VarDecl *D) { |
| 297 | // VarDecls can be declared 'extern' within a function or method body, |
| 298 | // but their enclosing DeclContext is the function, not the TU. We need |
| 299 | // to check the storage class to correctly generate the USR. |
Argyrios Kyrtzidis | d3ba410 | 2014-02-23 18:23:29 +0000 | [diff] [blame] | 300 | if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D))) |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 301 | return; |
| 302 | |
| 303 | VisitDeclContext(D->getDeclContext()); |
| 304 | |
Argyrios Kyrtzidis | 9d8ab72 | 2016-11-07 21:20:15 +0000 | [diff] [blame] | 305 | if (VarTemplateDecl *VarTmpl = D->getDescribedVarTemplate()) { |
| 306 | Out << "@VT"; |
| 307 | VisitTemplateParameterList(VarTmpl->getTemplateParameters()); |
| 308 | } else if (const VarTemplatePartialSpecializationDecl *PartialSpec |
| 309 | = dyn_cast<VarTemplatePartialSpecializationDecl>(D)) { |
| 310 | Out << "@VP"; |
| 311 | VisitTemplateParameterList(PartialSpec->getTemplateParameters()); |
| 312 | } |
| 313 | |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 314 | // Variables always have simple names. |
| 315 | StringRef s = D->getName(); |
| 316 | |
| 317 | // The string can be empty if the declaration has no name; e.g., it is |
| 318 | // the ParmDecl with no name for declaration of a function pointer type, e.g.: |
| 319 | // void (*f)(void *); |
| 320 | // In this case, don't generate a USR. |
| 321 | if (s.empty()) |
| 322 | IgnoreResults = true; |
| 323 | else |
| 324 | Out << '@' << s; |
Argyrios Kyrtzidis | 9d8ab72 | 2016-11-07 21:20:15 +0000 | [diff] [blame] | 325 | |
| 326 | // For a template specialization, mangle the template arguments. |
| 327 | if (const VarTemplateSpecializationDecl *Spec |
| 328 | = dyn_cast<VarTemplateSpecializationDecl>(D)) { |
Argyrios Kyrtzidis | 7d90ed0 | 2017-02-15 16:16:27 +0000 | [diff] [blame] | 329 | const TemplateArgumentList &Args = Spec->getTemplateArgs(); |
Argyrios Kyrtzidis | 9d8ab72 | 2016-11-07 21:20:15 +0000 | [diff] [blame] | 330 | Out << '>'; |
| 331 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 332 | Out << '#'; |
| 333 | VisitTemplateArgument(Args.get(I)); |
| 334 | } |
| 335 | } |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | void USRGenerator::VisitNonTypeTemplateParmDecl( |
| 339 | const NonTypeTemplateParmDecl *D) { |
Argyrios Kyrtzidis | d3ba410 | 2014-02-23 18:23:29 +0000 | [diff] [blame] | 340 | GenLoc(D, /*IncludeOffset=*/true); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | void USRGenerator::VisitTemplateTemplateParmDecl( |
| 344 | const TemplateTemplateParmDecl *D) { |
Argyrios Kyrtzidis | d3ba410 | 2014-02-23 18:23:29 +0000 | [diff] [blame] | 345 | GenLoc(D, /*IncludeOffset=*/true); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | void USRGenerator::VisitNamespaceDecl(const NamespaceDecl *D) { |
| 349 | if (D->isAnonymousNamespace()) { |
| 350 | Out << "@aN"; |
| 351 | return; |
| 352 | } |
| 353 | |
| 354 | VisitDeclContext(D->getDeclContext()); |
| 355 | if (!IgnoreResults) |
| 356 | Out << "@N@" << D->getName(); |
| 357 | } |
| 358 | |
| 359 | void USRGenerator::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) { |
| 360 | VisitFunctionDecl(D->getTemplatedDecl()); |
| 361 | } |
| 362 | |
| 363 | void USRGenerator::VisitClassTemplateDecl(const ClassTemplateDecl *D) { |
| 364 | VisitTagDecl(D->getTemplatedDecl()); |
| 365 | } |
| 366 | |
| 367 | void USRGenerator::VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) { |
| 368 | VisitDeclContext(D->getDeclContext()); |
| 369 | if (!IgnoreResults) |
| 370 | Out << "@NA@" << D->getName(); |
| 371 | } |
| 372 | |
| 373 | void USRGenerator::VisitObjCMethodDecl(const ObjCMethodDecl *D) { |
| 374 | const DeclContext *container = D->getDeclContext(); |
| 375 | if (const ObjCProtocolDecl *pd = dyn_cast<ObjCProtocolDecl>(container)) { |
| 376 | Visit(pd); |
| 377 | } |
| 378 | else { |
| 379 | // The USR for a method declared in a class extension or category is based on |
| 380 | // the ObjCInterfaceDecl, not the ObjCCategoryDecl. |
| 381 | const ObjCInterfaceDecl *ID = D->getClassInterface(); |
| 382 | if (!ID) { |
| 383 | IgnoreResults = true; |
| 384 | return; |
| 385 | } |
| 386 | Visit(ID); |
| 387 | } |
| 388 | // Ideally we would use 'GenObjCMethod', but this is such a hot path |
| 389 | // for Objective-C code that we don't want to use |
| 390 | // DeclarationName::getAsString(). |
| 391 | Out << (D->isInstanceMethod() ? "(im)" : "(cm)") |
| 392 | << DeclarationName(D->getSelector()); |
| 393 | } |
| 394 | |
| 395 | void USRGenerator::VisitObjCContainerDecl(const ObjCContainerDecl *D) { |
| 396 | switch (D->getKind()) { |
| 397 | default: |
| 398 | llvm_unreachable("Invalid ObjC container."); |
| 399 | case Decl::ObjCInterface: |
| 400 | case Decl::ObjCImplementation: |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 401 | GenObjCClass(D->getName(), GetExternalSourceContainer(D)); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 402 | break; |
| 403 | case Decl::ObjCCategory: { |
| 404 | const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D); |
| 405 | const ObjCInterfaceDecl *ID = CD->getClassInterface(); |
| 406 | if (!ID) { |
| 407 | // Handle invalid code where the @interface might not |
| 408 | // have been specified. |
| 409 | // FIXME: We should be able to generate this USR even if the |
| 410 | // @interface isn't available. |
| 411 | IgnoreResults = true; |
| 412 | return; |
| 413 | } |
| 414 | // Specially handle class extensions, which are anonymous categories. |
| 415 | // We want to mangle in the location to uniquely distinguish them. |
| 416 | if (CD->IsClassExtension()) { |
| 417 | Out << "objc(ext)" << ID->getName() << '@'; |
Argyrios Kyrtzidis | d3ba410 | 2014-02-23 18:23:29 +0000 | [diff] [blame] | 418 | GenLoc(CD, /*IncludeOffset=*/true); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 419 | } |
| 420 | else |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 421 | GenObjCCategory(ID->getName(), CD->getName(), |
| 422 | GetExternalSourceContainer(ID), |
| 423 | GetExternalSourceContainer(CD)); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 424 | |
| 425 | break; |
| 426 | } |
| 427 | case Decl::ObjCCategoryImpl: { |
| 428 | const ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D); |
| 429 | const ObjCInterfaceDecl *ID = CD->getClassInterface(); |
| 430 | if (!ID) { |
| 431 | // Handle invalid code where the @interface might not |
| 432 | // have been specified. |
| 433 | // FIXME: We should be able to generate this USR even if the |
| 434 | // @interface isn't available. |
| 435 | IgnoreResults = true; |
| 436 | return; |
| 437 | } |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 438 | GenObjCCategory(ID->getName(), CD->getName(), |
| 439 | GetExternalSourceContainer(ID), |
| 440 | GetExternalSourceContainer(CD)); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 441 | break; |
| 442 | } |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 443 | case Decl::ObjCProtocol: { |
| 444 | const ObjCProtocolDecl *PD = cast<ObjCProtocolDecl>(D); |
| 445 | GenObjCProtocol(PD->getName(), GetExternalSourceContainer(PD)); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 446 | break; |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 447 | } |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | |
| 451 | void USRGenerator::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) { |
| 452 | // The USR for a property declared in a class extension or category is based |
| 453 | // on the ObjCInterfaceDecl, not the ObjCCategoryDecl. |
| 454 | if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D)) |
| 455 | Visit(ID); |
| 456 | else |
| 457 | Visit(cast<Decl>(D->getDeclContext())); |
Argyrios Kyrtzidis | d9849a9 | 2016-07-15 22:18:19 +0000 | [diff] [blame] | 458 | GenObjCProperty(D->getName(), D->isClassProperty()); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | void USRGenerator::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) { |
| 462 | if (ObjCPropertyDecl *PD = D->getPropertyDecl()) { |
| 463 | VisitObjCPropertyDecl(PD); |
| 464 | return; |
| 465 | } |
| 466 | |
| 467 | IgnoreResults = true; |
| 468 | } |
| 469 | |
| 470 | void USRGenerator::VisitTagDecl(const TagDecl *D) { |
| 471 | // Add the location of the tag decl to handle resolution across |
| 472 | // translation units. |
Argyrios Kyrtzidis | 7332106 | 2016-03-04 07:17:53 +0000 | [diff] [blame] | 473 | if (!isa<EnumDecl>(D) && |
| 474 | ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D))) |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 475 | return; |
| 476 | |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 477 | GenExtSymbolContainer(D); |
| 478 | |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 479 | D = D->getCanonicalDecl(); |
| 480 | VisitDeclContext(D->getDeclContext()); |
| 481 | |
| 482 | bool AlreadyStarted = false; |
| 483 | if (const CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(D)) { |
| 484 | if (ClassTemplateDecl *ClassTmpl = CXXRecord->getDescribedClassTemplate()) { |
| 485 | AlreadyStarted = true; |
| 486 | |
| 487 | switch (D->getTagKind()) { |
| 488 | case TTK_Interface: |
Argyrios Kyrtzidis | f66cef7 | 2014-12-08 08:48:33 +0000 | [diff] [blame] | 489 | case TTK_Class: |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 490 | case TTK_Struct: Out << "@ST"; break; |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 491 | case TTK_Union: Out << "@UT"; break; |
| 492 | case TTK_Enum: llvm_unreachable("enum template"); |
| 493 | } |
| 494 | VisitTemplateParameterList(ClassTmpl->getTemplateParameters()); |
| 495 | } else if (const ClassTemplatePartialSpecializationDecl *PartialSpec |
| 496 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(CXXRecord)) { |
| 497 | AlreadyStarted = true; |
| 498 | |
| 499 | switch (D->getTagKind()) { |
| 500 | case TTK_Interface: |
Argyrios Kyrtzidis | f66cef7 | 2014-12-08 08:48:33 +0000 | [diff] [blame] | 501 | case TTK_Class: |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 502 | case TTK_Struct: Out << "@SP"; break; |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 503 | case TTK_Union: Out << "@UP"; break; |
| 504 | case TTK_Enum: llvm_unreachable("enum partial specialization"); |
| 505 | } |
| 506 | VisitTemplateParameterList(PartialSpec->getTemplateParameters()); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | if (!AlreadyStarted) { |
| 511 | switch (D->getTagKind()) { |
| 512 | case TTK_Interface: |
Argyrios Kyrtzidis | f66cef7 | 2014-12-08 08:48:33 +0000 | [diff] [blame] | 513 | case TTK_Class: |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 514 | case TTK_Struct: Out << "@S"; break; |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 515 | case TTK_Union: Out << "@U"; break; |
| 516 | case TTK_Enum: Out << "@E"; break; |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | Out << '@'; |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 521 | assert(Buf.size() > 0); |
| 522 | const unsigned off = Buf.size() - 1; |
| 523 | |
| 524 | if (EmitDeclName(D)) { |
| 525 | if (const TypedefNameDecl *TD = D->getTypedefNameForAnonDecl()) { |
| 526 | Buf[off] = 'A'; |
| 527 | Out << '@' << *TD; |
| 528 | } |
Argyrios Kyrtzidis | 80537a4 | 2014-12-08 08:48:37 +0000 | [diff] [blame] | 529 | else { |
| 530 | if (D->isEmbeddedInDeclarator() && !D->isFreeStanding()) { |
| 531 | printLoc(Out, D->getLocation(), Context->getSourceManager(), true); |
Argyrios Kyrtzidis | 7332106 | 2016-03-04 07:17:53 +0000 | [diff] [blame] | 532 | } else { |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 533 | Buf[off] = 'a'; |
Argyrios Kyrtzidis | 7332106 | 2016-03-04 07:17:53 +0000 | [diff] [blame] | 534 | if (auto *ED = dyn_cast<EnumDecl>(D)) { |
| 535 | // Distinguish USRs of anonymous enums by using their first enumerator. |
| 536 | auto enum_range = ED->enumerators(); |
| 537 | if (enum_range.begin() != enum_range.end()) { |
| 538 | Out << '@' << **enum_range.begin(); |
| 539 | } |
| 540 | } |
| 541 | } |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 542 | } |
Argyrios Kyrtzidis | 80537a4 | 2014-12-08 08:48:37 +0000 | [diff] [blame] | 543 | } |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 544 | |
| 545 | // For a class template specialization, mangle the template arguments. |
| 546 | if (const ClassTemplateSpecializationDecl *Spec |
| 547 | = dyn_cast<ClassTemplateSpecializationDecl>(D)) { |
Argyrios Kyrtzidis | 7d90ed0 | 2017-02-15 16:16:27 +0000 | [diff] [blame] | 548 | const TemplateArgumentList &Args = Spec->getTemplateArgs(); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 549 | Out << '>'; |
| 550 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 551 | Out << '#'; |
| 552 | VisitTemplateArgument(Args.get(I)); |
| 553 | } |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | void USRGenerator::VisitTypedefDecl(const TypedefDecl *D) { |
Argyrios Kyrtzidis | d3ba410 | 2014-02-23 18:23:29 +0000 | [diff] [blame] | 558 | if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D))) |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 559 | return; |
| 560 | const DeclContext *DC = D->getDeclContext(); |
| 561 | if (const NamedDecl *DCN = dyn_cast<NamedDecl>(DC)) |
| 562 | Visit(DCN); |
| 563 | Out << "@T@"; |
| 564 | Out << D->getName(); |
| 565 | } |
| 566 | |
| 567 | void USRGenerator::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) { |
Argyrios Kyrtzidis | d3ba410 | 2014-02-23 18:23:29 +0000 | [diff] [blame] | 568 | GenLoc(D, /*IncludeOffset=*/true); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 569 | } |
| 570 | |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 571 | void USRGenerator::GenExtSymbolContainer(const NamedDecl *D) { |
| 572 | StringRef Container = GetExternalSourceContainer(D); |
| 573 | if (!Container.empty()) |
| 574 | Out << "@M@" << Container; |
| 575 | } |
| 576 | |
Argyrios Kyrtzidis | d3ba410 | 2014-02-23 18:23:29 +0000 | [diff] [blame] | 577 | bool USRGenerator::GenLoc(const Decl *D, bool IncludeOffset) { |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 578 | if (generatedLoc) |
| 579 | return IgnoreResults; |
| 580 | generatedLoc = true; |
Dmitri Gribenko | 237769e | 2014-03-28 22:21:26 +0000 | [diff] [blame] | 581 | |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 582 | // Guard against null declarations in invalid code. |
| 583 | if (!D) { |
| 584 | IgnoreResults = true; |
| 585 | return true; |
| 586 | } |
| 587 | |
| 588 | // Use the location of canonical decl. |
| 589 | D = D->getCanonicalDecl(); |
| 590 | |
Dmitri Gribenko | 237769e | 2014-03-28 22:21:26 +0000 | [diff] [blame] | 591 | IgnoreResults = |
| 592 | IgnoreResults || printLoc(Out, D->getLocStart(), |
| 593 | Context->getSourceManager(), IncludeOffset); |
| 594 | |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 595 | return IgnoreResults; |
| 596 | } |
| 597 | |
| 598 | void USRGenerator::VisitType(QualType T) { |
| 599 | // This method mangles in USR information for types. It can possibly |
| 600 | // just reuse the naming-mangling logic used by codegen, although the |
| 601 | // requirements for USRs might not be the same. |
| 602 | ASTContext &Ctx = *Context; |
| 603 | |
| 604 | do { |
| 605 | T = Ctx.getCanonicalType(T); |
| 606 | Qualifiers Q = T.getQualifiers(); |
| 607 | unsigned qVal = 0; |
| 608 | if (Q.hasConst()) |
| 609 | qVal |= 0x1; |
| 610 | if (Q.hasVolatile()) |
| 611 | qVal |= 0x2; |
| 612 | if (Q.hasRestrict()) |
| 613 | qVal |= 0x4; |
| 614 | if(qVal) |
| 615 | Out << ((char) ('0' + qVal)); |
| 616 | |
| 617 | // Mangle in ObjC GC qualifiers? |
| 618 | |
| 619 | if (const PackExpansionType *Expansion = T->getAs<PackExpansionType>()) { |
| 620 | Out << 'P'; |
| 621 | T = Expansion->getPattern(); |
| 622 | } |
| 623 | |
| 624 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
| 625 | unsigned char c = '\0'; |
| 626 | switch (BT->getKind()) { |
| 627 | case BuiltinType::Void: |
| 628 | c = 'v'; break; |
| 629 | case BuiltinType::Bool: |
| 630 | c = 'b'; break; |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 631 | case BuiltinType::UChar: |
| 632 | c = 'c'; break; |
| 633 | case BuiltinType::Char16: |
| 634 | c = 'q'; break; |
| 635 | case BuiltinType::Char32: |
| 636 | c = 'w'; break; |
| 637 | case BuiltinType::UShort: |
| 638 | c = 's'; break; |
| 639 | case BuiltinType::UInt: |
| 640 | c = 'i'; break; |
| 641 | case BuiltinType::ULong: |
| 642 | c = 'l'; break; |
| 643 | case BuiltinType::ULongLong: |
| 644 | c = 'k'; break; |
| 645 | case BuiltinType::UInt128: |
| 646 | c = 'j'; break; |
Argyrios Kyrtzidis | 56af7e6 | 2014-12-08 09:09:05 +0000 | [diff] [blame] | 647 | case BuiltinType::Char_U: |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 648 | case BuiltinType::Char_S: |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 649 | c = 'C'; break; |
Argyrios Kyrtzidis | ca04454 | 2014-12-08 08:48:17 +0000 | [diff] [blame] | 650 | case BuiltinType::SChar: |
| 651 | c = 'r'; break; |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 652 | case BuiltinType::WChar_S: |
| 653 | case BuiltinType::WChar_U: |
| 654 | c = 'W'; break; |
| 655 | case BuiltinType::Short: |
| 656 | c = 'S'; break; |
| 657 | case BuiltinType::Int: |
| 658 | c = 'I'; break; |
| 659 | case BuiltinType::Long: |
| 660 | c = 'L'; break; |
| 661 | case BuiltinType::LongLong: |
| 662 | c = 'K'; break; |
| 663 | case BuiltinType::Int128: |
| 664 | c = 'J'; break; |
| 665 | case BuiltinType::Half: |
| 666 | c = 'h'; break; |
| 667 | case BuiltinType::Float: |
| 668 | c = 'f'; break; |
| 669 | case BuiltinType::Double: |
| 670 | c = 'd'; break; |
| 671 | case BuiltinType::LongDouble: |
| 672 | c = 'D'; break; |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 673 | case BuiltinType::Float128: |
| 674 | c = 'Q'; break; |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 675 | case BuiltinType::NullPtr: |
| 676 | c = 'n'; break; |
| 677 | #define BUILTIN_TYPE(Id, SingletonId) |
| 678 | #define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id: |
| 679 | #include "clang/AST/BuiltinTypes.def" |
| 680 | case BuiltinType::Dependent: |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 681 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| 682 | case BuiltinType::Id: |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 683 | #include "clang/Basic/OpenCLImageTypes.def" |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 684 | case BuiltinType::OCLEvent: |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 685 | case BuiltinType::OCLClkEvent: |
| 686 | case BuiltinType::OCLQueue: |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 687 | case BuiltinType::OCLReserveID: |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 688 | case BuiltinType::OCLSampler: |
| 689 | IgnoreResults = true; |
| 690 | return; |
| 691 | case BuiltinType::ObjCId: |
| 692 | c = 'o'; break; |
| 693 | case BuiltinType::ObjCClass: |
| 694 | c = 'O'; break; |
| 695 | case BuiltinType::ObjCSel: |
| 696 | c = 'e'; break; |
| 697 | } |
| 698 | Out << c; |
| 699 | return; |
| 700 | } |
| 701 | |
| 702 | // If we have already seen this (non-built-in) type, use a substitution |
| 703 | // encoding. |
| 704 | llvm::DenseMap<const Type *, unsigned>::iterator Substitution |
| 705 | = TypeSubstitutions.find(T.getTypePtr()); |
| 706 | if (Substitution != TypeSubstitutions.end()) { |
| 707 | Out << 'S' << Substitution->second << '_'; |
| 708 | return; |
| 709 | } else { |
| 710 | // Record this as a substitution. |
| 711 | unsigned Number = TypeSubstitutions.size(); |
| 712 | TypeSubstitutions[T.getTypePtr()] = Number; |
| 713 | } |
| 714 | |
| 715 | if (const PointerType *PT = T->getAs<PointerType>()) { |
| 716 | Out << '*'; |
| 717 | T = PT->getPointeeType(); |
| 718 | continue; |
| 719 | } |
Argyrios Kyrtzidis | 9c99867 | 2016-03-04 07:17:43 +0000 | [diff] [blame] | 720 | if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) { |
| 721 | Out << '*'; |
| 722 | T = OPT->getPointeeType(); |
| 723 | continue; |
| 724 | } |
Argyrios Kyrtzidis | 0e387dc | 2014-12-08 08:48:27 +0000 | [diff] [blame] | 725 | if (const RValueReferenceType *RT = T->getAs<RValueReferenceType>()) { |
| 726 | Out << "&&"; |
| 727 | T = RT->getPointeeType(); |
| 728 | continue; |
| 729 | } |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 730 | if (const ReferenceType *RT = T->getAs<ReferenceType>()) { |
| 731 | Out << '&'; |
| 732 | T = RT->getPointeeType(); |
| 733 | continue; |
| 734 | } |
| 735 | if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) { |
| 736 | Out << 'F'; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 737 | VisitType(FT->getReturnType()); |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 738 | for (const auto &I : FT->param_types()) |
| 739 | VisitType(I); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 740 | if (FT->isVariadic()) |
| 741 | Out << '.'; |
| 742 | return; |
| 743 | } |
| 744 | if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) { |
| 745 | Out << 'B'; |
| 746 | T = BT->getPointeeType(); |
| 747 | continue; |
| 748 | } |
| 749 | if (const ComplexType *CT = T->getAs<ComplexType>()) { |
| 750 | Out << '<'; |
| 751 | T = CT->getElementType(); |
| 752 | continue; |
| 753 | } |
| 754 | if (const TagType *TT = T->getAs<TagType>()) { |
| 755 | Out << '$'; |
| 756 | VisitTagDecl(TT->getDecl()); |
| 757 | return; |
| 758 | } |
Argyrios Kyrtzidis | 9c99867 | 2016-03-04 07:17:43 +0000 | [diff] [blame] | 759 | if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) { |
| 760 | Out << '$'; |
| 761 | VisitObjCInterfaceDecl(OIT->getDecl()); |
| 762 | return; |
| 763 | } |
| 764 | if (const ObjCObjectType *OIT = T->getAs<ObjCObjectType>()) { |
| 765 | Out << 'Q'; |
| 766 | VisitType(OIT->getBaseType()); |
| 767 | for (auto *Prot : OIT->getProtocols()) |
| 768 | VisitObjCProtocolDecl(Prot); |
| 769 | return; |
| 770 | } |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 771 | if (const TemplateTypeParmType *TTP = T->getAs<TemplateTypeParmType>()) { |
| 772 | Out << 't' << TTP->getDepth() << '.' << TTP->getIndex(); |
| 773 | return; |
| 774 | } |
| 775 | if (const TemplateSpecializationType *Spec |
| 776 | = T->getAs<TemplateSpecializationType>()) { |
| 777 | Out << '>'; |
| 778 | VisitTemplateName(Spec->getTemplateName()); |
| 779 | Out << Spec->getNumArgs(); |
| 780 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
| 781 | VisitTemplateArgument(Spec->getArg(I)); |
| 782 | return; |
| 783 | } |
Argyrios Kyrtzidis | d06ce40 | 2014-12-08 08:48:11 +0000 | [diff] [blame] | 784 | if (const DependentNameType *DNT = T->getAs<DependentNameType>()) { |
| 785 | Out << '^'; |
| 786 | // FIXME: Encode the qualifier, don't just print it. |
| 787 | PrintingPolicy PO(Ctx.getLangOpts()); |
| 788 | PO.SuppressTagKeyword = true; |
| 789 | PO.SuppressUnwrittenScope = true; |
| 790 | PO.ConstantArraySizeAsWritten = false; |
| 791 | PO.AnonymousTagLocations = false; |
| 792 | DNT->getQualifier()->print(Out, PO); |
| 793 | Out << ':' << DNT->getIdentifier()->getName(); |
| 794 | return; |
| 795 | } |
Argyrios Kyrtzidis | 1d5e542 | 2014-12-08 08:48:43 +0000 | [diff] [blame] | 796 | if (const InjectedClassNameType *InjT = T->getAs<InjectedClassNameType>()) { |
| 797 | T = InjT->getInjectedSpecializationType(); |
| 798 | continue; |
| 799 | } |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 800 | |
| 801 | // Unhandled type. |
| 802 | Out << ' '; |
| 803 | break; |
| 804 | } while (true); |
| 805 | } |
| 806 | |
| 807 | void USRGenerator::VisitTemplateParameterList( |
| 808 | const TemplateParameterList *Params) { |
| 809 | if (!Params) |
| 810 | return; |
| 811 | Out << '>' << Params->size(); |
| 812 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 813 | PEnd = Params->end(); |
| 814 | P != PEnd; ++P) { |
| 815 | Out << '#'; |
| 816 | if (isa<TemplateTypeParmDecl>(*P)) { |
| 817 | if (cast<TemplateTypeParmDecl>(*P)->isParameterPack()) |
| 818 | Out<< 'p'; |
| 819 | Out << 'T'; |
| 820 | continue; |
| 821 | } |
| 822 | |
| 823 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 824 | if (NTTP->isParameterPack()) |
| 825 | Out << 'p'; |
| 826 | Out << 'N'; |
| 827 | VisitType(NTTP->getType()); |
| 828 | continue; |
| 829 | } |
| 830 | |
| 831 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); |
| 832 | if (TTP->isParameterPack()) |
| 833 | Out << 'p'; |
| 834 | Out << 't'; |
| 835 | VisitTemplateParameterList(TTP->getTemplateParameters()); |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | void USRGenerator::VisitTemplateName(TemplateName Name) { |
| 840 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 841 | if (TemplateTemplateParmDecl *TTP |
| 842 | = dyn_cast<TemplateTemplateParmDecl>(Template)) { |
| 843 | Out << 't' << TTP->getDepth() << '.' << TTP->getIndex(); |
| 844 | return; |
| 845 | } |
| 846 | |
| 847 | Visit(Template); |
| 848 | return; |
| 849 | } |
| 850 | |
| 851 | // FIXME: Visit dependent template names. |
| 852 | } |
| 853 | |
| 854 | void USRGenerator::VisitTemplateArgument(const TemplateArgument &Arg) { |
| 855 | switch (Arg.getKind()) { |
| 856 | case TemplateArgument::Null: |
| 857 | break; |
| 858 | |
| 859 | case TemplateArgument::Declaration: |
| 860 | Visit(Arg.getAsDecl()); |
| 861 | break; |
| 862 | |
| 863 | case TemplateArgument::NullPtr: |
| 864 | break; |
| 865 | |
| 866 | case TemplateArgument::TemplateExpansion: |
| 867 | Out << 'P'; // pack expansion of... |
| 868 | // Fall through |
| 869 | case TemplateArgument::Template: |
| 870 | VisitTemplateName(Arg.getAsTemplateOrTemplatePattern()); |
| 871 | break; |
| 872 | |
| 873 | case TemplateArgument::Expression: |
| 874 | // FIXME: Visit expressions. |
| 875 | break; |
| 876 | |
| 877 | case TemplateArgument::Pack: |
| 878 | Out << 'p' << Arg.pack_size(); |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 879 | for (const auto &P : Arg.pack_elements()) |
| 880 | VisitTemplateArgument(P); |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 881 | break; |
| 882 | |
| 883 | case TemplateArgument::Type: |
| 884 | VisitType(Arg.getAsType()); |
| 885 | break; |
| 886 | |
| 887 | case TemplateArgument::Integral: |
| 888 | Out << 'V'; |
| 889 | VisitType(Arg.getIntegralType()); |
| 890 | Out << Arg.getAsIntegral(); |
| 891 | break; |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | //===----------------------------------------------------------------------===// |
| 896 | // USR generation functions. |
| 897 | //===----------------------------------------------------------------------===// |
| 898 | |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 899 | void clang::index::generateUSRForObjCClass(StringRef Cls, raw_ostream &OS, |
| 900 | StringRef ExtSymDefinedIn) { |
| 901 | if (!ExtSymDefinedIn.empty()) |
| 902 | OS << "@M@" << ExtSymDefinedIn << '@'; |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 903 | OS << "objc(cs)" << Cls; |
| 904 | } |
| 905 | |
Argyrios Kyrtzidis | 5234b49 | 2013-08-21 00:49:25 +0000 | [diff] [blame] | 906 | void clang::index::generateUSRForObjCCategory(StringRef Cls, StringRef Cat, |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 907 | raw_ostream &OS, |
| 908 | StringRef ClsSymDefinedIn, |
| 909 | StringRef CatSymDefinedIn) { |
| 910 | if (!CatSymDefinedIn.empty() || !ClsSymDefinedIn.empty()) { |
| 911 | OS << "@M@"; |
| 912 | if (!CatSymDefinedIn.empty() && !ClsSymDefinedIn.empty()) |
| 913 | OS << CatSymDefinedIn << '-' << ClsSymDefinedIn; |
| 914 | else if (!CatSymDefinedIn.empty()) |
| 915 | OS << CatSymDefinedIn; |
| 916 | else |
| 917 | OS << ClsSymDefinedIn; |
| 918 | OS << '@'; |
| 919 | } |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 920 | OS << "objc(cy)" << Cls << '@' << Cat; |
| 921 | } |
| 922 | |
Argyrios Kyrtzidis | 5234b49 | 2013-08-21 00:49:25 +0000 | [diff] [blame] | 923 | void clang::index::generateUSRForObjCIvar(StringRef Ivar, raw_ostream &OS) { |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 924 | OS << '@' << Ivar; |
| 925 | } |
| 926 | |
Argyrios Kyrtzidis | 5234b49 | 2013-08-21 00:49:25 +0000 | [diff] [blame] | 927 | void clang::index::generateUSRForObjCMethod(StringRef Sel, |
| 928 | bool IsInstanceMethod, |
| 929 | raw_ostream &OS) { |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 930 | OS << (IsInstanceMethod ? "(im)" : "(cm)") << Sel; |
| 931 | } |
| 932 | |
Argyrios Kyrtzidis | d9849a9 | 2016-07-15 22:18:19 +0000 | [diff] [blame] | 933 | void clang::index::generateUSRForObjCProperty(StringRef Prop, bool isClassProp, |
| 934 | raw_ostream &OS) { |
| 935 | OS << (isClassProp ? "(cpy)" : "(py)") << Prop; |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 936 | } |
| 937 | |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 938 | void clang::index::generateUSRForObjCProtocol(StringRef Prot, raw_ostream &OS, |
| 939 | StringRef ExtSymDefinedIn) { |
| 940 | if (!ExtSymDefinedIn.empty()) |
| 941 | OS << "@M@" << ExtSymDefinedIn << '@'; |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 942 | OS << "objc(pl)" << Prot; |
| 943 | } |
| 944 | |
Argyrios Kyrtzidis | 6e5ca5b | 2017-04-21 05:42:46 +0000 | [diff] [blame] | 945 | void clang::index::generateUSRForGlobalEnum(StringRef EnumName, raw_ostream &OS, |
| 946 | StringRef ExtSymDefinedIn) { |
| 947 | if (!ExtSymDefinedIn.empty()) |
| 948 | OS << "@M@" << ExtSymDefinedIn; |
| 949 | OS << "@E@" << EnumName; |
| 950 | } |
| 951 | |
Argyrios Kyrtzidis | 5234b49 | 2013-08-21 00:49:25 +0000 | [diff] [blame] | 952 | bool clang::index::generateUSRForDecl(const Decl *D, |
| 953 | SmallVectorImpl<char> &Buf) { |
Argyrios Kyrtzidis | f12918d | 2016-11-02 23:42:33 +0000 | [diff] [blame] | 954 | if (!D) |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 955 | return true; |
Argyrios Kyrtzidis | f12918d | 2016-11-02 23:42:33 +0000 | [diff] [blame] | 956 | // We don't ignore decls with invalid source locations. Implicit decls, like |
| 957 | // C++'s operator new function, can have invalid locations but it is fine to |
| 958 | // create USRs that can identify them. |
Argyrios Kyrtzidis | 4b2b460 | 2013-08-16 18:17:55 +0000 | [diff] [blame] | 959 | |
| 960 | USRGenerator UG(&D->getASTContext(), Buf); |
| 961 | UG.Visit(D); |
| 962 | return UG.ignoreResults(); |
| 963 | } |
Dmitri Gribenko | 237769e | 2014-03-28 22:21:26 +0000 | [diff] [blame] | 964 | |
Richard Smith | 66a8186 | 2015-05-04 02:25:31 +0000 | [diff] [blame] | 965 | bool clang::index::generateUSRForMacro(const MacroDefinitionRecord *MD, |
Dmitri Gribenko | 237769e | 2014-03-28 22:21:26 +0000 | [diff] [blame] | 966 | const SourceManager &SM, |
| 967 | SmallVectorImpl<char> &Buf) { |
Argyrios Kyrtzidis | 5b7a09a | 2017-02-02 16:13:10 +0000 | [diff] [blame] | 968 | if (!MD) |
| 969 | return true; |
| 970 | return generateUSRForMacro(MD->getName()->getName(), MD->getLocation(), |
| 971 | SM, Buf); |
| 972 | |
| 973 | } |
| 974 | |
| 975 | bool clang::index::generateUSRForMacro(StringRef MacroName, SourceLocation Loc, |
| 976 | const SourceManager &SM, |
| 977 | SmallVectorImpl<char> &Buf) { |
Dmitri Gribenko | 237769e | 2014-03-28 22:21:26 +0000 | [diff] [blame] | 978 | // Don't generate USRs for things with invalid locations. |
Argyrios Kyrtzidis | 5b7a09a | 2017-02-02 16:13:10 +0000 | [diff] [blame] | 979 | if (MacroName.empty() || Loc.isInvalid()) |
Dmitri Gribenko | 237769e | 2014-03-28 22:21:26 +0000 | [diff] [blame] | 980 | return true; |
| 981 | |
| 982 | llvm::raw_svector_ostream Out(Buf); |
| 983 | |
| 984 | // Assume that system headers are sane. Don't put source location |
| 985 | // information into the USR if the macro comes from a system header. |
Dmitri Gribenko | 237769e | 2014-03-28 22:21:26 +0000 | [diff] [blame] | 986 | bool ShouldGenerateLocation = !SM.isInSystemHeader(Loc); |
| 987 | |
| 988 | Out << getUSRSpacePrefix(); |
| 989 | if (ShouldGenerateLocation) |
| 990 | printLoc(Out, Loc, SM, /*IncludeOffset=*/true); |
| 991 | Out << "@macro@"; |
Argyrios Kyrtzidis | 5b7a09a | 2017-02-02 16:13:10 +0000 | [diff] [blame] | 992 | Out << MacroName; |
Dmitri Gribenko | 237769e | 2014-03-28 22:21:26 +0000 | [diff] [blame] | 993 | return false; |
| 994 | } |