blob: 8194c9508fb7e632559f0926750a6aac05a28bda [file] [log] [blame]
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001//===- 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 Kyrtzidis15a2fcc2013-08-17 00:40:41 +000010#include "clang/Index/USRGeneration.h"
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000011#include "clang/AST/ASTContext.h"
12#include "clang/AST/DeclTemplate.h"
13#include "clang/AST/DeclVisitor.h"
Dmitri Gribenko237769e2014-03-28 22:21:26 +000014#include "clang/Lex/PreprocessingRecord.h"
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000015#include "llvm/Support/Path.h"
16#include "llvm/Support/raw_ostream.h"
17
18using namespace clang;
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +000019using namespace clang::index;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000020
21//===----------------------------------------------------------------------===//
22// USR generation.
23//===----------------------------------------------------------------------===//
24
Dmitri Gribenko237769e2014-03-28 22:21:26 +000025/// \returns true on error.
26static 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 Kyrtzidis6e5ca5b2017-04-21 05:42:46 +000049static StringRef GetExternalSourceContainer(const NamedDecl *D) {
50 if (!D)
51 return StringRef();
Argyrios Kyrtzidis11d70482017-05-20 04:11:33 +000052 if (auto *attr = D->getExternalSourceSymbolAttr()) {
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +000053 return attr->getDefinedIn();
54 }
55 return StringRef();
56}
57
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000058namespace {
59class USRGenerator : public ConstDeclVisitor<USRGenerator> {
60 SmallVectorImpl<char> &Buf;
61 llvm::raw_svector_ostream Out;
62 bool IgnoreResults;
63 ASTContext *Context;
64 bool generatedLoc;
Fangrui Song6907ce22018-07-30 19:24:48 +000065
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000066 llvm::DenseMap<const Type *, unsigned> TypeSubstitutions;
Fangrui Song6907ce22018-07-30 19:24:48 +000067
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000068public:
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 Kyrtzidis5234b492013-08-21 00:49:25 +000077 Out << getUSRSpacePrefix();
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000078 }
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);
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +000091 void VisitObjCContainerDecl(const ObjCContainerDecl *CD,
92 const ObjCCategoryDecl *CatD = nullptr);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000093 void VisitObjCMethodDecl(const ObjCMethodDecl *MD);
94 void VisitObjCPropertyDecl(const ObjCPropertyDecl *D);
95 void VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D);
96 void VisitTagDecl(const TagDecl *D);
97 void VisitTypedefDecl(const TypedefDecl *D);
98 void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D);
99 void VisitVarDecl(const VarDecl *D);
100 void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D);
101 void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D);
Ben Langmuirfd6e39c2017-08-16 23:12:21 +0000102 void VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D);
103 void VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D);
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000104
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000105 void VisitLinkageSpecDecl(const LinkageSpecDecl *D) {
Sam McCall2e50ae62018-02-02 14:13:37 +0000106 IgnoreResults = true; // No USRs for linkage specs themselves.
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000107 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000108
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000109 void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
110 IgnoreResults = true;
111 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000112
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000113 void VisitUsingDecl(const UsingDecl *D) {
114 IgnoreResults = true;
115 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000116
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000117 bool ShouldGenerateLocation(const NamedDecl *D);
118
119 bool isLocal(const NamedDecl *D) {
Craig Topper236bde32014-05-26 06:21:51 +0000120 return D->getParentFunctionOrMethod() != nullptr;
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000121 }
122
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000123 void GenExtSymbolContainer(const NamedDecl *D);
124
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000125 /// Generate the string component containing the location of the
126 /// declaration.
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000127 bool GenLoc(const Decl *D, bool IncludeOffset);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000128
129 /// String generation methods used both by the visitation methods
130 /// and from other clients that want to directly generate USRs. These
131 /// methods do not construct complete USRs (which incorporate the parents
132 /// of an AST element), but only the fragments concerning the AST element
133 /// itself.
134
135 /// Generate a USR for an Objective-C class.
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +0000136 void GenObjCClass(StringRef cls, StringRef ExtSymDefinedIn,
137 StringRef CategoryContextExtSymbolDefinedIn) {
138 generateUSRForObjCClass(cls, Out, ExtSymDefinedIn,
139 CategoryContextExtSymbolDefinedIn);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000140 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000141
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000142 /// Generate a USR for an Objective-C class category.
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000143 void GenObjCCategory(StringRef cls, StringRef cat,
144 StringRef clsExt, StringRef catExt) {
145 generateUSRForObjCCategory(cls, cat, Out, clsExt, catExt);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000146 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000147
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000148 /// Generate a USR fragment for an Objective-C property.
Argyrios Kyrtzidisd9849a92016-07-15 22:18:19 +0000149 void GenObjCProperty(StringRef prop, bool isClassProp) {
150 generateUSRForObjCProperty(prop, isClassProp, Out);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000151 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000152
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000153 /// Generate a USR for an Objective-C protocol.
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000154 void GenObjCProtocol(StringRef prot, StringRef ext) {
155 generateUSRForObjCProtocol(prot, Out, ext);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000156 }
157
158 void VisitType(QualType T);
159 void VisitTemplateParameterList(const TemplateParameterList *Params);
160 void VisitTemplateName(TemplateName Name);
161 void VisitTemplateArgument(const TemplateArgument &Arg);
Fangrui Song6907ce22018-07-30 19:24:48 +0000162
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000163 /// Emit a Decl's name using NamedDecl::printName() and return true if
164 /// the decl had no name.
165 bool EmitDeclName(const NamedDecl *D);
166};
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000167} // end anonymous namespace
168
169//===----------------------------------------------------------------------===//
170// Generating USRs from ASTS.
171//===----------------------------------------------------------------------===//
172
173bool USRGenerator::EmitDeclName(const NamedDecl *D) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000174 const unsigned startSize = Buf.size();
175 D->printName(Out);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000176 const unsigned endSize = Buf.size();
177 return startSize == endSize;
178}
179
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000180bool USRGenerator::ShouldGenerateLocation(const NamedDecl *D) {
181 if (D->isExternallyVisible())
182 return false;
183 if (D->getParentFunctionOrMethod())
184 return true;
Argyrios Kyrtzidisf12918d2016-11-02 23:42:33 +0000185 SourceLocation Loc = D->getLocation();
186 if (Loc.isInvalid())
187 return false;
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000188 const SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisf12918d2016-11-02 23:42:33 +0000189 return !SM.isInSystemHeader(Loc);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000190}
191
192void USRGenerator::VisitDeclContext(const DeclContext *DC) {
193 if (const NamedDecl *D = dyn_cast<NamedDecl>(DC))
194 Visit(D);
Sam McCall2e50ae62018-02-02 14:13:37 +0000195 else if (isa<LinkageSpecDecl>(DC)) // Linkage specs are transparent in USRs.
196 VisitDeclContext(DC->getParent());
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000197}
198
199void USRGenerator::VisitFieldDecl(const FieldDecl *D) {
200 // The USR for an ivar declared in a class extension is based on the
201 // ObjCInterfaceDecl, not the ObjCCategoryDecl.
202 if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D))
203 Visit(ID);
204 else
205 VisitDeclContext(D->getDeclContext());
206 Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@");
207 if (EmitDeclName(D)) {
208 // Bit fields can be anonymous.
209 IgnoreResults = true;
210 return;
211 }
212}
213
214void USRGenerator::VisitFunctionDecl(const FunctionDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000215 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000216 return;
217
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000218 const unsigned StartSize = Buf.size();
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000219 VisitDeclContext(D->getDeclContext());
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000220 if (Buf.size() == StartSize)
221 GenExtSymbolContainer(D);
222
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000223 bool IsTemplate = false;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000224 if (FunctionTemplateDecl *FunTmpl = D->getDescribedFunctionTemplate()) {
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000225 IsTemplate = true;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000226 Out << "@FT@";
227 VisitTemplateParameterList(FunTmpl->getTemplateParameters());
228 } else
229 Out << "@F@";
Argyrios Kyrtzidisd5719082016-02-15 01:32:36 +0000230
231 PrintingPolicy Policy(Context->getLangOpts());
232 // Forward references can have different template argument names. Suppress the
233 // template argument names in constructors to make their USR more stable.
234 Policy.SuppressTemplateArgsInCXXConstructors = true;
235 D->getDeclName().print(Out, Policy);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000236
237 ASTContext &Ctx = *Context;
Argyrios Kyrtzidis2682f9e2016-03-04 07:17:48 +0000238 if ((!Ctx.getLangOpts().CPlusPlus || D->isExternC()) &&
239 !D->hasAttr<OverloadableAttr>())
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000240 return;
241
242 if (const TemplateArgumentList *
243 SpecArgs = D->getTemplateSpecializationArgs()) {
244 Out << '<';
245 for (unsigned I = 0, N = SpecArgs->size(); I != N; ++I) {
246 Out << '#';
247 VisitTemplateArgument(SpecArgs->get(I));
248 }
249 Out << '>';
250 }
251
252 // Mangle in type information for the arguments.
David Majnemer59f77922016-06-24 04:05:48 +0000253 for (auto PD : D->parameters()) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000254 Out << '#';
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +0000255 VisitType(PD->getType());
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000256 }
257 if (D->isVariadic())
258 Out << '.';
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000259 if (IsTemplate) {
260 // Function templates can be overloaded by return type, for example:
261 // \code
262 // template <class T> typename T::A foo() {}
263 // template <class T> typename T::B foo() {}
264 // \endcode
265 Out << '#';
266 VisitType(D->getReturnType());
267 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000268 Out << '#';
269 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
270 if (MD->isStatic())
271 Out << 'S';
272 if (unsigned quals = MD->getTypeQualifiers())
273 Out << (char)('0' + quals);
Argyrios Kyrtzidisf5819092014-12-08 08:48:21 +0000274 switch (MD->getRefQualifier()) {
275 case RQ_None: break;
276 case RQ_LValue: Out << '&'; break;
277 case RQ_RValue: Out << "&&"; break;
278 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000279 }
280}
281
282void USRGenerator::VisitNamedDecl(const NamedDecl *D) {
283 VisitDeclContext(D->getDeclContext());
284 Out << "@";
285
286 if (EmitDeclName(D)) {
287 // The string can be empty if the declaration has no name; e.g., it is
288 // the ParmDecl with no name for declaration of a function pointer type,
289 // e.g.: void (*f)(void *);
290 // In this case, don't generate a USR.
291 IgnoreResults = true;
292 }
293}
294
295void USRGenerator::VisitVarDecl(const VarDecl *D) {
296 // VarDecls can be declared 'extern' within a function or method body,
297 // but their enclosing DeclContext is the function, not the TU. We need
298 // to check the storage class to correctly generate the USR.
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000299 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000300 return;
301
302 VisitDeclContext(D->getDeclContext());
303
Argyrios Kyrtzidis9d8ab722016-11-07 21:20:15 +0000304 if (VarTemplateDecl *VarTmpl = D->getDescribedVarTemplate()) {
305 Out << "@VT";
306 VisitTemplateParameterList(VarTmpl->getTemplateParameters());
307 } else if (const VarTemplatePartialSpecializationDecl *PartialSpec
308 = dyn_cast<VarTemplatePartialSpecializationDecl>(D)) {
309 Out << "@VP";
310 VisitTemplateParameterList(PartialSpec->getTemplateParameters());
311 }
312
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000313 // Variables always have simple names.
314 StringRef s = D->getName();
315
316 // The string can be empty if the declaration has no name; e.g., it is
317 // the ParmDecl with no name for declaration of a function pointer type, e.g.:
318 // void (*f)(void *);
319 // In this case, don't generate a USR.
320 if (s.empty())
321 IgnoreResults = true;
322 else
323 Out << '@' << s;
Argyrios Kyrtzidis9d8ab722016-11-07 21:20:15 +0000324
325 // For a template specialization, mangle the template arguments.
326 if (const VarTemplateSpecializationDecl *Spec
327 = dyn_cast<VarTemplateSpecializationDecl>(D)) {
Argyrios Kyrtzidis7d90ed02017-02-15 16:16:27 +0000328 const TemplateArgumentList &Args = Spec->getTemplateArgs();
Argyrios Kyrtzidis9d8ab722016-11-07 21:20:15 +0000329 Out << '>';
330 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
331 Out << '#';
332 VisitTemplateArgument(Args.get(I));
333 }
334 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000335}
336
337void USRGenerator::VisitNonTypeTemplateParmDecl(
338 const NonTypeTemplateParmDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000339 GenLoc(D, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000340}
341
342void USRGenerator::VisitTemplateTemplateParmDecl(
343 const TemplateTemplateParmDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000344 GenLoc(D, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000345}
346
347void USRGenerator::VisitNamespaceDecl(const NamespaceDecl *D) {
348 if (D->isAnonymousNamespace()) {
349 Out << "@aN";
350 return;
351 }
352
353 VisitDeclContext(D->getDeclContext());
354 if (!IgnoreResults)
355 Out << "@N@" << D->getName();
356}
357
358void USRGenerator::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
359 VisitFunctionDecl(D->getTemplatedDecl());
360}
361
362void USRGenerator::VisitClassTemplateDecl(const ClassTemplateDecl *D) {
363 VisitTagDecl(D->getTemplatedDecl());
364}
365
366void USRGenerator::VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
367 VisitDeclContext(D->getDeclContext());
368 if (!IgnoreResults)
Fangrui Song6907ce22018-07-30 19:24:48 +0000369 Out << "@NA@" << D->getName();
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000370}
371
372void USRGenerator::VisitObjCMethodDecl(const ObjCMethodDecl *D) {
373 const DeclContext *container = D->getDeclContext();
374 if (const ObjCProtocolDecl *pd = dyn_cast<ObjCProtocolDecl>(container)) {
375 Visit(pd);
376 }
377 else {
378 // The USR for a method declared in a class extension or category is based on
379 // the ObjCInterfaceDecl, not the ObjCCategoryDecl.
380 const ObjCInterfaceDecl *ID = D->getClassInterface();
381 if (!ID) {
382 IgnoreResults = true;
383 return;
384 }
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +0000385 auto getCategoryContext = [](const ObjCMethodDecl *D) ->
386 const ObjCCategoryDecl * {
387 if (auto *CD = dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
388 return CD;
389 if (auto *ICD = dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
390 return ICD->getCategoryDecl();
391 return nullptr;
392 };
393 auto *CD = getCategoryContext(D);
394 VisitObjCContainerDecl(ID, CD);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000395 }
396 // Ideally we would use 'GenObjCMethod', but this is such a hot path
397 // for Objective-C code that we don't want to use
398 // DeclarationName::getAsString().
399 Out << (D->isInstanceMethod() ? "(im)" : "(cm)")
400 << DeclarationName(D->getSelector());
401}
402
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +0000403void USRGenerator::VisitObjCContainerDecl(const ObjCContainerDecl *D,
404 const ObjCCategoryDecl *CatD) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000405 switch (D->getKind()) {
406 default:
407 llvm_unreachable("Invalid ObjC container.");
408 case Decl::ObjCInterface:
409 case Decl::ObjCImplementation:
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +0000410 GenObjCClass(D->getName(), GetExternalSourceContainer(D),
411 GetExternalSourceContainer(CatD));
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000412 break;
413 case Decl::ObjCCategory: {
414 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
415 const ObjCInterfaceDecl *ID = CD->getClassInterface();
416 if (!ID) {
417 // Handle invalid code where the @interface might not
418 // have been specified.
419 // FIXME: We should be able to generate this USR even if the
420 // @interface isn't available.
421 IgnoreResults = true;
422 return;
423 }
424 // Specially handle class extensions, which are anonymous categories.
425 // We want to mangle in the location to uniquely distinguish them.
426 if (CD->IsClassExtension()) {
427 Out << "objc(ext)" << ID->getName() << '@';
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000428 GenLoc(CD, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000429 }
430 else
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000431 GenObjCCategory(ID->getName(), CD->getName(),
432 GetExternalSourceContainer(ID),
433 GetExternalSourceContainer(CD));
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000434
435 break;
436 }
437 case Decl::ObjCCategoryImpl: {
438 const ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
439 const ObjCInterfaceDecl *ID = CD->getClassInterface();
440 if (!ID) {
441 // Handle invalid code where the @interface might not
442 // have been specified.
443 // FIXME: We should be able to generate this USR even if the
444 // @interface isn't available.
445 IgnoreResults = true;
446 return;
447 }
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000448 GenObjCCategory(ID->getName(), CD->getName(),
449 GetExternalSourceContainer(ID),
450 GetExternalSourceContainer(CD));
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000451 break;
452 }
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000453 case Decl::ObjCProtocol: {
454 const ObjCProtocolDecl *PD = cast<ObjCProtocolDecl>(D);
455 GenObjCProtocol(PD->getName(), GetExternalSourceContainer(PD));
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000456 break;
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000457 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000458 }
459}
460
461void USRGenerator::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
462 // The USR for a property declared in a class extension or category is based
463 // on the ObjCInterfaceDecl, not the ObjCCategoryDecl.
464 if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D))
465 Visit(ID);
466 else
467 Visit(cast<Decl>(D->getDeclContext()));
Argyrios Kyrtzidisd9849a92016-07-15 22:18:19 +0000468 GenObjCProperty(D->getName(), D->isClassProperty());
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000469}
470
471void USRGenerator::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
472 if (ObjCPropertyDecl *PD = D->getPropertyDecl()) {
473 VisitObjCPropertyDecl(PD);
474 return;
475 }
476
477 IgnoreResults = true;
478}
479
480void USRGenerator::VisitTagDecl(const TagDecl *D) {
481 // Add the location of the tag decl to handle resolution across
482 // translation units.
Argyrios Kyrtzidis73321062016-03-04 07:17:53 +0000483 if (!isa<EnumDecl>(D) &&
484 ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000485 return;
486
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000487 GenExtSymbolContainer(D);
488
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000489 D = D->getCanonicalDecl();
490 VisitDeclContext(D->getDeclContext());
491
492 bool AlreadyStarted = false;
493 if (const CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(D)) {
494 if (ClassTemplateDecl *ClassTmpl = CXXRecord->getDescribedClassTemplate()) {
495 AlreadyStarted = true;
Fangrui Song6907ce22018-07-30 19:24:48 +0000496
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000497 switch (D->getTagKind()) {
498 case TTK_Interface:
Argyrios Kyrtzidisf66cef72014-12-08 08:48:33 +0000499 case TTK_Class:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000500 case TTK_Struct: Out << "@ST"; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000501 case TTK_Union: Out << "@UT"; break;
502 case TTK_Enum: llvm_unreachable("enum template");
503 }
504 VisitTemplateParameterList(ClassTmpl->getTemplateParameters());
505 } else if (const ClassTemplatePartialSpecializationDecl *PartialSpec
506 = dyn_cast<ClassTemplatePartialSpecializationDecl>(CXXRecord)) {
507 AlreadyStarted = true;
Fangrui Song6907ce22018-07-30 19:24:48 +0000508
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000509 switch (D->getTagKind()) {
510 case TTK_Interface:
Argyrios Kyrtzidisf66cef72014-12-08 08:48:33 +0000511 case TTK_Class:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000512 case TTK_Struct: Out << "@SP"; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000513 case TTK_Union: Out << "@UP"; break;
514 case TTK_Enum: llvm_unreachable("enum partial specialization");
Fangrui Song6907ce22018-07-30 19:24:48 +0000515 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000516 VisitTemplateParameterList(PartialSpec->getTemplateParameters());
517 }
518 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000519
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000520 if (!AlreadyStarted) {
521 switch (D->getTagKind()) {
522 case TTK_Interface:
Argyrios Kyrtzidisf66cef72014-12-08 08:48:33 +0000523 case TTK_Class:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000524 case TTK_Struct: Out << "@S"; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000525 case TTK_Union: Out << "@U"; break;
526 case TTK_Enum: Out << "@E"; break;
527 }
528 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000529
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000530 Out << '@';
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000531 assert(Buf.size() > 0);
532 const unsigned off = Buf.size() - 1;
533
534 if (EmitDeclName(D)) {
535 if (const TypedefNameDecl *TD = D->getTypedefNameForAnonDecl()) {
536 Buf[off] = 'A';
537 Out << '@' << *TD;
538 }
Argyrios Kyrtzidis80537a42014-12-08 08:48:37 +0000539 else {
540 if (D->isEmbeddedInDeclarator() && !D->isFreeStanding()) {
541 printLoc(Out, D->getLocation(), Context->getSourceManager(), true);
Argyrios Kyrtzidis73321062016-03-04 07:17:53 +0000542 } else {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000543 Buf[off] = 'a';
Argyrios Kyrtzidis73321062016-03-04 07:17:53 +0000544 if (auto *ED = dyn_cast<EnumDecl>(D)) {
545 // Distinguish USRs of anonymous enums by using their first enumerator.
546 auto enum_range = ED->enumerators();
547 if (enum_range.begin() != enum_range.end()) {
548 Out << '@' << **enum_range.begin();
549 }
550 }
551 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000552 }
Argyrios Kyrtzidis80537a42014-12-08 08:48:37 +0000553 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000554
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000555 // For a class template specialization, mangle the template arguments.
556 if (const ClassTemplateSpecializationDecl *Spec
557 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
Argyrios Kyrtzidis7d90ed02017-02-15 16:16:27 +0000558 const TemplateArgumentList &Args = Spec->getTemplateArgs();
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000559 Out << '>';
560 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
561 Out << '#';
562 VisitTemplateArgument(Args.get(I));
563 }
564 }
565}
566
567void USRGenerator::VisitTypedefDecl(const TypedefDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000568 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000569 return;
570 const DeclContext *DC = D->getDeclContext();
571 if (const NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
572 Visit(DCN);
573 Out << "@T@";
574 Out << D->getName();
575}
576
577void USRGenerator::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000578 GenLoc(D, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000579}
580
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000581void USRGenerator::GenExtSymbolContainer(const NamedDecl *D) {
582 StringRef Container = GetExternalSourceContainer(D);
583 if (!Container.empty())
584 Out << "@M@" << Container;
585}
586
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000587bool USRGenerator::GenLoc(const Decl *D, bool IncludeOffset) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000588 if (generatedLoc)
589 return IgnoreResults;
590 generatedLoc = true;
Dmitri Gribenko237769e2014-03-28 22:21:26 +0000591
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000592 // Guard against null declarations in invalid code.
593 if (!D) {
594 IgnoreResults = true;
595 return true;
596 }
597
598 // Use the location of canonical decl.
599 D = D->getCanonicalDecl();
600
Dmitri Gribenko237769e2014-03-28 22:21:26 +0000601 IgnoreResults =
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000602 IgnoreResults || printLoc(Out, D->getBeginLoc(),
Dmitri Gribenko237769e2014-03-28 22:21:26 +0000603 Context->getSourceManager(), IncludeOffset);
604
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000605 return IgnoreResults;
606}
607
Ben Langmuirfd6e39c2017-08-16 23:12:21 +0000608static void printQualifier(llvm::raw_ostream &Out, ASTContext &Ctx, NestedNameSpecifier *NNS) {
609 // FIXME: Encode the qualifier, don't just print it.
610 PrintingPolicy PO(Ctx.getLangOpts());
611 PO.SuppressTagKeyword = true;
612 PO.SuppressUnwrittenScope = true;
613 PO.ConstantArraySizeAsWritten = false;
614 PO.AnonymousTagLocations = false;
615 NNS->print(Out, PO);
616}
617
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000618void USRGenerator::VisitType(QualType T) {
619 // This method mangles in USR information for types. It can possibly
620 // just reuse the naming-mangling logic used by codegen, although the
621 // requirements for USRs might not be the same.
622 ASTContext &Ctx = *Context;
623
624 do {
625 T = Ctx.getCanonicalType(T);
626 Qualifiers Q = T.getQualifiers();
627 unsigned qVal = 0;
628 if (Q.hasConst())
629 qVal |= 0x1;
630 if (Q.hasVolatile())
631 qVal |= 0x2;
632 if (Q.hasRestrict())
633 qVal |= 0x4;
634 if(qVal)
635 Out << ((char) ('0' + qVal));
636
637 // Mangle in ObjC GC qualifiers?
638
639 if (const PackExpansionType *Expansion = T->getAs<PackExpansionType>()) {
640 Out << 'P';
641 T = Expansion->getPattern();
642 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000643
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000644 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
645 unsigned char c = '\0';
646 switch (BT->getKind()) {
647 case BuiltinType::Void:
648 c = 'v'; break;
649 case BuiltinType::Bool:
650 c = 'b'; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000651 case BuiltinType::UChar:
652 c = 'c'; break;
Richard Smith3a8244d2018-05-01 05:02:45 +0000653 case BuiltinType::Char8:
654 c = 'u'; break; // FIXME: Check this doesn't collide
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000655 case BuiltinType::Char16:
656 c = 'q'; break;
657 case BuiltinType::Char32:
658 c = 'w'; break;
659 case BuiltinType::UShort:
660 c = 's'; break;
661 case BuiltinType::UInt:
662 c = 'i'; break;
663 case BuiltinType::ULong:
664 c = 'l'; break;
665 case BuiltinType::ULongLong:
666 c = 'k'; break;
667 case BuiltinType::UInt128:
668 c = 'j'; break;
Argyrios Kyrtzidis56af7e62014-12-08 09:09:05 +0000669 case BuiltinType::Char_U:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000670 case BuiltinType::Char_S:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000671 c = 'C'; break;
Argyrios Kyrtzidisca044542014-12-08 08:48:17 +0000672 case BuiltinType::SChar:
673 c = 'r'; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000674 case BuiltinType::WChar_S:
675 case BuiltinType::WChar_U:
676 c = 'W'; break;
677 case BuiltinType::Short:
678 c = 'S'; break;
679 case BuiltinType::Int:
680 c = 'I'; break;
681 case BuiltinType::Long:
682 c = 'L'; break;
683 case BuiltinType::LongLong:
684 c = 'K'; break;
685 case BuiltinType::Int128:
686 c = 'J'; break;
Sjoerd Meijercc623ad2017-09-08 15:15:00 +0000687 case BuiltinType::Float16:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000688 case BuiltinType::Half:
689 c = 'h'; break;
690 case BuiltinType::Float:
691 c = 'f'; break;
692 case BuiltinType::Double:
693 c = 'd'; break;
694 case BuiltinType::LongDouble:
695 c = 'D'; break;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000696 case BuiltinType::Float128:
697 c = 'Q'; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000698 case BuiltinType::NullPtr:
699 c = 'n'; break;
700#define BUILTIN_TYPE(Id, SingletonId)
701#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
702#include "clang/AST/BuiltinTypes.def"
703 case BuiltinType::Dependent:
Alexey Bader954ba212016-04-08 13:40:33 +0000704#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
705 case BuiltinType::Id:
Alexey Baderb62f1442016-04-13 08:33:41 +0000706#include "clang/Basic/OpenCLImageTypes.def"
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000707 case BuiltinType::OCLEvent:
Alexey Bader9c8453f2015-09-15 11:18:52 +0000708 case BuiltinType::OCLClkEvent:
709 case BuiltinType::OCLQueue:
Alexey Bader9c8453f2015-09-15 11:18:52 +0000710 case BuiltinType::OCLReserveID:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000711 case BuiltinType::OCLSampler:
Leonard Chanf921d852018-06-04 16:07:52 +0000712 case BuiltinType::ShortAccum:
713 case BuiltinType::Accum:
714 case BuiltinType::LongAccum:
715 case BuiltinType::UShortAccum:
716 case BuiltinType::UAccum:
717 case BuiltinType::ULongAccum:
Leonard Chanab80f3c2018-06-14 14:53:51 +0000718 case BuiltinType::ShortFract:
719 case BuiltinType::Fract:
720 case BuiltinType::LongFract:
721 case BuiltinType::UShortFract:
722 case BuiltinType::UFract:
723 case BuiltinType::ULongFract:
724 case BuiltinType::SatShortAccum:
725 case BuiltinType::SatAccum:
726 case BuiltinType::SatLongAccum:
727 case BuiltinType::SatUShortAccum:
728 case BuiltinType::SatUAccum:
729 case BuiltinType::SatULongAccum:
730 case BuiltinType::SatShortFract:
731 case BuiltinType::SatFract:
732 case BuiltinType::SatLongFract:
733 case BuiltinType::SatUShortFract:
734 case BuiltinType::SatUFract:
735 case BuiltinType::SatULongFract:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000736 IgnoreResults = true;
737 return;
738 case BuiltinType::ObjCId:
739 c = 'o'; break;
740 case BuiltinType::ObjCClass:
741 c = 'O'; break;
742 case BuiltinType::ObjCSel:
743 c = 'e'; break;
744 }
745 Out << c;
746 return;
747 }
748
749 // If we have already seen this (non-built-in) type, use a substitution
750 // encoding.
751 llvm::DenseMap<const Type *, unsigned>::iterator Substitution
752 = TypeSubstitutions.find(T.getTypePtr());
753 if (Substitution != TypeSubstitutions.end()) {
754 Out << 'S' << Substitution->second << '_';
755 return;
756 } else {
757 // Record this as a substitution.
758 unsigned Number = TypeSubstitutions.size();
759 TypeSubstitutions[T.getTypePtr()] = Number;
760 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000761
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000762 if (const PointerType *PT = T->getAs<PointerType>()) {
763 Out << '*';
764 T = PT->getPointeeType();
765 continue;
766 }
Argyrios Kyrtzidis9c998672016-03-04 07:17:43 +0000767 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
768 Out << '*';
769 T = OPT->getPointeeType();
770 continue;
771 }
Argyrios Kyrtzidis0e387dc2014-12-08 08:48:27 +0000772 if (const RValueReferenceType *RT = T->getAs<RValueReferenceType>()) {
773 Out << "&&";
774 T = RT->getPointeeType();
775 continue;
776 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000777 if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
778 Out << '&';
779 T = RT->getPointeeType();
780 continue;
781 }
782 if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) {
783 Out << 'F';
Alp Toker314cc812014-01-25 16:55:45 +0000784 VisitType(FT->getReturnType());
Jan Korouse6a02422017-10-10 00:35:16 +0000785 Out << '(';
786 for (const auto &I : FT->param_types()) {
787 Out << '#';
Aaron Ballman40bd0aa2014-03-17 15:23:01 +0000788 VisitType(I);
Jan Korouse6a02422017-10-10 00:35:16 +0000789 }
790 Out << ')';
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000791 if (FT->isVariadic())
792 Out << '.';
793 return;
794 }
795 if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) {
796 Out << 'B';
797 T = BT->getPointeeType();
798 continue;
799 }
800 if (const ComplexType *CT = T->getAs<ComplexType>()) {
801 Out << '<';
802 T = CT->getElementType();
803 continue;
804 }
805 if (const TagType *TT = T->getAs<TagType>()) {
806 Out << '$';
807 VisitTagDecl(TT->getDecl());
808 return;
809 }
Argyrios Kyrtzidis9c998672016-03-04 07:17:43 +0000810 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
811 Out << '$';
812 VisitObjCInterfaceDecl(OIT->getDecl());
813 return;
814 }
815 if (const ObjCObjectType *OIT = T->getAs<ObjCObjectType>()) {
816 Out << 'Q';
817 VisitType(OIT->getBaseType());
818 for (auto *Prot : OIT->getProtocols())
819 VisitObjCProtocolDecl(Prot);
820 return;
821 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000822 if (const TemplateTypeParmType *TTP = T->getAs<TemplateTypeParmType>()) {
823 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
824 return;
825 }
826 if (const TemplateSpecializationType *Spec
827 = T->getAs<TemplateSpecializationType>()) {
828 Out << '>';
829 VisitTemplateName(Spec->getTemplateName());
830 Out << Spec->getNumArgs();
831 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
832 VisitTemplateArgument(Spec->getArg(I));
833 return;
834 }
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000835 if (const DependentNameType *DNT = T->getAs<DependentNameType>()) {
836 Out << '^';
Ben Langmuirfd6e39c2017-08-16 23:12:21 +0000837 printQualifier(Out, Ctx, DNT->getQualifier());
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000838 Out << ':' << DNT->getIdentifier()->getName();
839 return;
840 }
Argyrios Kyrtzidis1d5e5422014-12-08 08:48:43 +0000841 if (const InjectedClassNameType *InjT = T->getAs<InjectedClassNameType>()) {
842 T = InjT->getInjectedSpecializationType();
843 continue;
844 }
Alex Lorenz45c423b2017-04-28 09:46:36 +0000845 if (const auto *VT = T->getAs<VectorType>()) {
846 Out << (T->isExtVectorType() ? ']' : '[');
847 Out << VT->getNumElements();
848 T = VT->getElementType();
849 continue;
850 }
Jan Korous663ba152017-10-09 19:51:33 +0000851 if (const auto *const AT = dyn_cast<ArrayType>(T)) {
852 Out << '{';
853 switch (AT->getSizeModifier()) {
854 case ArrayType::Static:
855 Out << 's';
856 break;
857 case ArrayType::Star:
858 Out << '*';
859 break;
860 case ArrayType::Normal:
861 Out << 'n';
862 break;
863 }
864 if (const auto *const CAT = dyn_cast<ConstantArrayType>(T))
865 Out << CAT->getSize();
866
867 T = AT->getElementType();
868 continue;
869 }
Alex Lorenz45c423b2017-04-28 09:46:36 +0000870
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000871 // Unhandled type.
872 Out << ' ';
873 break;
874 } while (true);
875}
876
877void USRGenerator::VisitTemplateParameterList(
878 const TemplateParameterList *Params) {
879 if (!Params)
880 return;
881 Out << '>' << Params->size();
882 for (TemplateParameterList::const_iterator P = Params->begin(),
883 PEnd = Params->end();
884 P != PEnd; ++P) {
885 Out << '#';
886 if (isa<TemplateTypeParmDecl>(*P)) {
887 if (cast<TemplateTypeParmDecl>(*P)->isParameterPack())
888 Out<< 'p';
889 Out << 'T';
890 continue;
891 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000892
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000893 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
894 if (NTTP->isParameterPack())
895 Out << 'p';
896 Out << 'N';
897 VisitType(NTTP->getType());
898 continue;
899 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000900
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000901 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
902 if (TTP->isParameterPack())
903 Out << 'p';
904 Out << 't';
905 VisitTemplateParameterList(TTP->getTemplateParameters());
906 }
907}
908
909void USRGenerator::VisitTemplateName(TemplateName Name) {
910 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
911 if (TemplateTemplateParmDecl *TTP
912 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
913 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
914 return;
915 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000916
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000917 Visit(Template);
918 return;
919 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000920
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000921 // FIXME: Visit dependent template names.
922}
923
924void USRGenerator::VisitTemplateArgument(const TemplateArgument &Arg) {
925 switch (Arg.getKind()) {
926 case TemplateArgument::Null:
927 break;
928
929 case TemplateArgument::Declaration:
930 Visit(Arg.getAsDecl());
931 break;
932
933 case TemplateArgument::NullPtr:
934 break;
935
936 case TemplateArgument::TemplateExpansion:
937 Out << 'P'; // pack expansion of...
938 // Fall through
939 case TemplateArgument::Template:
940 VisitTemplateName(Arg.getAsTemplateOrTemplatePattern());
941 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000942
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000943 case TemplateArgument::Expression:
944 // FIXME: Visit expressions.
945 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000946
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000947 case TemplateArgument::Pack:
948 Out << 'p' << Arg.pack_size();
Aaron Ballman2a89e852014-07-15 21:32:31 +0000949 for (const auto &P : Arg.pack_elements())
950 VisitTemplateArgument(P);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000951 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000952
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000953 case TemplateArgument::Type:
954 VisitType(Arg.getAsType());
955 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000956
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000957 case TemplateArgument::Integral:
958 Out << 'V';
959 VisitType(Arg.getIntegralType());
960 Out << Arg.getAsIntegral();
961 break;
962 }
963}
964
Ben Langmuirfd6e39c2017-08-16 23:12:21 +0000965void USRGenerator::VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D) {
966 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
967 return;
968 VisitDeclContext(D->getDeclContext());
969 Out << "@UUV@";
970 printQualifier(Out, D->getASTContext(), D->getQualifier());
971 EmitDeclName(D);
972}
973
974void USRGenerator::VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D) {
975 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
976 return;
977 VisitDeclContext(D->getDeclContext());
978 Out << "@UUT@";
979 printQualifier(Out, D->getASTContext(), D->getQualifier());
980 Out << D->getName(); // Simple name.
981}
982
983
984
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000985//===----------------------------------------------------------------------===//
986// USR generation functions.
987//===----------------------------------------------------------------------===//
988
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +0000989static void combineClassAndCategoryExtContainers(StringRef ClsSymDefinedIn,
990 StringRef CatSymDefinedIn,
991 raw_ostream &OS) {
992 if (ClsSymDefinedIn.empty() && CatSymDefinedIn.empty())
993 return;
994 if (CatSymDefinedIn.empty()) {
995 OS << "@M@" << ClsSymDefinedIn << '@';
996 return;
997 }
998 OS << "@CM@" << CatSymDefinedIn << '@';
999 if (ClsSymDefinedIn != CatSymDefinedIn) {
1000 OS << ClsSymDefinedIn << '@';
1001 }
1002}
1003
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +00001004void clang::index::generateUSRForObjCClass(StringRef Cls, raw_ostream &OS,
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +00001005 StringRef ExtSymDefinedIn,
1006 StringRef CategoryContextExtSymbolDefinedIn) {
1007 combineClassAndCategoryExtContainers(ExtSymDefinedIn,
1008 CategoryContextExtSymbolDefinedIn, OS);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001009 OS << "objc(cs)" << Cls;
1010}
1011
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +00001012void clang::index::generateUSRForObjCCategory(StringRef Cls, StringRef Cat,
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +00001013 raw_ostream &OS,
1014 StringRef ClsSymDefinedIn,
1015 StringRef CatSymDefinedIn) {
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +00001016 combineClassAndCategoryExtContainers(ClsSymDefinedIn, CatSymDefinedIn, OS);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001017 OS << "objc(cy)" << Cls << '@' << Cat;
1018}
1019
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +00001020void clang::index::generateUSRForObjCIvar(StringRef Ivar, raw_ostream &OS) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001021 OS << '@' << Ivar;
1022}
1023
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +00001024void clang::index::generateUSRForObjCMethod(StringRef Sel,
1025 bool IsInstanceMethod,
1026 raw_ostream &OS) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001027 OS << (IsInstanceMethod ? "(im)" : "(cm)") << Sel;
1028}
1029
Argyrios Kyrtzidisd9849a92016-07-15 22:18:19 +00001030void clang::index::generateUSRForObjCProperty(StringRef Prop, bool isClassProp,
1031 raw_ostream &OS) {
1032 OS << (isClassProp ? "(cpy)" : "(py)") << Prop;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001033}
1034
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +00001035void clang::index::generateUSRForObjCProtocol(StringRef Prot, raw_ostream &OS,
1036 StringRef ExtSymDefinedIn) {
1037 if (!ExtSymDefinedIn.empty())
1038 OS << "@M@" << ExtSymDefinedIn << '@';
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001039 OS << "objc(pl)" << Prot;
1040}
1041
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +00001042void clang::index::generateUSRForGlobalEnum(StringRef EnumName, raw_ostream &OS,
1043 StringRef ExtSymDefinedIn) {
1044 if (!ExtSymDefinedIn.empty())
1045 OS << "@M@" << ExtSymDefinedIn;
1046 OS << "@E@" << EnumName;
1047}
1048
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +00001049void clang::index::generateUSRForEnumConstant(StringRef EnumConstantName,
1050 raw_ostream &OS) {
1051 OS << '@' << EnumConstantName;
1052}
1053
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +00001054bool clang::index::generateUSRForDecl(const Decl *D,
1055 SmallVectorImpl<char> &Buf) {
Argyrios Kyrtzidisf12918d2016-11-02 23:42:33 +00001056 if (!D)
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001057 return true;
Argyrios Kyrtzidisf12918d2016-11-02 23:42:33 +00001058 // We don't ignore decls with invalid source locations. Implicit decls, like
1059 // C++'s operator new function, can have invalid locations but it is fine to
1060 // create USRs that can identify them.
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001061
1062 USRGenerator UG(&D->getASTContext(), Buf);
1063 UG.Visit(D);
1064 return UG.ignoreResults();
1065}
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001066
Richard Smith66a81862015-05-04 02:25:31 +00001067bool clang::index::generateUSRForMacro(const MacroDefinitionRecord *MD,
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001068 const SourceManager &SM,
1069 SmallVectorImpl<char> &Buf) {
Argyrios Kyrtzidis5b7a09a2017-02-02 16:13:10 +00001070 if (!MD)
1071 return true;
1072 return generateUSRForMacro(MD->getName()->getName(), MD->getLocation(),
1073 SM, Buf);
1074
1075}
1076
1077bool clang::index::generateUSRForMacro(StringRef MacroName, SourceLocation Loc,
1078 const SourceManager &SM,
1079 SmallVectorImpl<char> &Buf) {
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001080 // Don't generate USRs for things with invalid locations.
Argyrios Kyrtzidis5b7a09a2017-02-02 16:13:10 +00001081 if (MacroName.empty() || Loc.isInvalid())
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001082 return true;
1083
1084 llvm::raw_svector_ostream Out(Buf);
1085
1086 // Assume that system headers are sane. Don't put source location
1087 // information into the USR if the macro comes from a system header.
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001088 bool ShouldGenerateLocation = !SM.isInSystemHeader(Loc);
1089
1090 Out << getUSRSpacePrefix();
1091 if (ShouldGenerateLocation)
1092 printLoc(Out, Loc, SM, /*IncludeOffset=*/true);
1093 Out << "@macro@";
Argyrios Kyrtzidis5b7a09a2017-02-02 16:13:10 +00001094 Out << MacroName;
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001095 return false;
1096}
Argyrios Kyrtzidis32e5d862018-09-18 15:02:56 +00001097
1098bool clang::index::generateFullUSRForModule(const Module *Mod,
1099 raw_ostream &OS) {
1100 if (!Mod->Parent)
1101 return generateFullUSRForTopLevelModuleName(Mod->Name, OS);
1102 if (generateFullUSRForModule(Mod->Parent, OS))
1103 return true;
1104 return generateUSRFragmentForModule(Mod, OS);
1105}
1106
1107bool clang::index::generateFullUSRForTopLevelModuleName(StringRef ModName,
1108 raw_ostream &OS) {
1109 OS << getUSRSpacePrefix();
1110 return generateUSRFragmentForModuleName(ModName, OS);
1111}
1112
1113bool clang::index::generateUSRFragmentForModule(const Module *Mod,
1114 raw_ostream &OS) {
1115 return generateUSRFragmentForModuleName(Mod->Name, OS);
1116}
1117
1118bool clang::index::generateUSRFragmentForModuleName(StringRef ModName,
1119 raw_ostream &OS) {
1120 OS << "@M@" << ModName;
1121 return false;
1122}