blob: f4316fe7d0674790b98db54ef9cc970b85ad63bb [file] [log] [blame]
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001//===- USRGeneration.cpp - Routines for USR generation --------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00006//
7//===----------------------------------------------------------------------===//
8
Argyrios Kyrtzidis15a2fcc2013-08-17 00:40:41 +00009#include "clang/Index/USRGeneration.h"
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000010#include "clang/AST/ASTContext.h"
11#include "clang/AST/DeclTemplate.h"
12#include "clang/AST/DeclVisitor.h"
Dmitri Gribenko237769e2014-03-28 22:21:26 +000013#include "clang/Lex/PreprocessingRecord.h"
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000014#include "llvm/Support/Path.h"
15#include "llvm/Support/raw_ostream.h"
16
17using namespace clang;
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +000018using namespace clang::index;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000019
20//===----------------------------------------------------------------------===//
21// USR generation.
22//===----------------------------------------------------------------------===//
23
Dmitri Gribenko237769e2014-03-28 22:21:26 +000024/// \returns true on error.
25static bool printLoc(llvm::raw_ostream &OS, SourceLocation Loc,
26 const SourceManager &SM, bool IncludeOffset) {
27 if (Loc.isInvalid()) {
28 return true;
29 }
30 Loc = SM.getExpansionLoc(Loc);
31 const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(Loc);
32 const FileEntry *FE = SM.getFileEntryForID(Decomposed.first);
33 if (FE) {
34 OS << llvm::sys::path::filename(FE->getName());
35 } else {
36 // This case really isn't interesting.
37 return true;
38 }
39 if (IncludeOffset) {
40 // Use the offest into the FileID to represent the location. Using
41 // a line/column can cause us to look back at the original source file,
42 // which is expensive.
43 OS << '@' << Decomposed.second;
44 }
45 return false;
46}
47
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +000048static StringRef GetExternalSourceContainer(const NamedDecl *D) {
49 if (!D)
50 return StringRef();
Argyrios Kyrtzidis11d70482017-05-20 04:11:33 +000051 if (auto *attr = D->getExternalSourceSymbolAttr()) {
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +000052 return attr->getDefinedIn();
53 }
54 return StringRef();
55}
56
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000057namespace {
58class USRGenerator : public ConstDeclVisitor<USRGenerator> {
59 SmallVectorImpl<char> &Buf;
60 llvm::raw_svector_ostream Out;
61 bool IgnoreResults;
62 ASTContext *Context;
63 bool generatedLoc;
Fangrui Song6907ce22018-07-30 19:24:48 +000064
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000065 llvm::DenseMap<const Type *, unsigned> TypeSubstitutions;
Fangrui Song6907ce22018-07-30 19:24:48 +000066
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000067public:
68 explicit USRGenerator(ASTContext *Ctx, SmallVectorImpl<char> &Buf)
69 : Buf(Buf),
70 Out(Buf),
71 IgnoreResults(false),
72 Context(Ctx),
73 generatedLoc(false)
74 {
75 // Add the USR space prefix.
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +000076 Out << getUSRSpacePrefix();
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000077 }
78
79 bool ignoreResults() const { return IgnoreResults; }
80
81 // Visitation methods from generating USRs from AST elements.
82 void VisitDeclContext(const DeclContext *D);
83 void VisitFieldDecl(const FieldDecl *D);
84 void VisitFunctionDecl(const FunctionDecl *D);
85 void VisitNamedDecl(const NamedDecl *D);
86 void VisitNamespaceDecl(const NamespaceDecl *D);
87 void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D);
88 void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D);
89 void VisitClassTemplateDecl(const ClassTemplateDecl *D);
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +000090 void VisitObjCContainerDecl(const ObjCContainerDecl *CD,
91 const ObjCCategoryDecl *CatD = nullptr);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000092 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);
Fangrui Song63a8b6c2018-10-09 01:02:56 +000099 void VisitBindingDecl(const BindingDecl *D);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000100 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) {
Kadir Cetinkayaa87ada02019-02-26 14:23:12 +0000114 VisitDeclContext(D->getDeclContext());
115 Out << "@UD@";
116
117 bool EmittedDeclName = !EmitDeclName(D);
118 assert(EmittedDeclName && "EmitDeclName can not fail for UsingDecls");
119 (void)EmittedDeclName;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000120 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000121
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000122 bool ShouldGenerateLocation(const NamedDecl *D);
123
124 bool isLocal(const NamedDecl *D) {
Craig Topper236bde32014-05-26 06:21:51 +0000125 return D->getParentFunctionOrMethod() != nullptr;
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000126 }
127
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000128 void GenExtSymbolContainer(const NamedDecl *D);
129
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000130 /// Generate the string component containing the location of the
131 /// declaration.
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000132 bool GenLoc(const Decl *D, bool IncludeOffset);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000133
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 Kyrtzidisf3634742017-04-21 22:27:06 +0000141 void GenObjCClass(StringRef cls, StringRef ExtSymDefinedIn,
142 StringRef CategoryContextExtSymbolDefinedIn) {
143 generateUSRForObjCClass(cls, Out, ExtSymDefinedIn,
144 CategoryContextExtSymbolDefinedIn);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000145 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000146
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000147 /// Generate a USR for an Objective-C class category.
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000148 void GenObjCCategory(StringRef cls, StringRef cat,
149 StringRef clsExt, StringRef catExt) {
150 generateUSRForObjCCategory(cls, cat, Out, clsExt, catExt);
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 fragment for an Objective-C property.
Argyrios Kyrtzidisd9849a92016-07-15 22:18:19 +0000154 void GenObjCProperty(StringRef prop, bool isClassProp) {
155 generateUSRForObjCProperty(prop, isClassProp, Out);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000156 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000157
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000158 /// Generate a USR for an Objective-C protocol.
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000159 void GenObjCProtocol(StringRef prot, StringRef ext) {
160 generateUSRForObjCProtocol(prot, Out, ext);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000161 }
162
163 void VisitType(QualType T);
164 void VisitTemplateParameterList(const TemplateParameterList *Params);
165 void VisitTemplateName(TemplateName Name);
166 void VisitTemplateArgument(const TemplateArgument &Arg);
Fangrui Song6907ce22018-07-30 19:24:48 +0000167
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000168 /// Emit a Decl's name using NamedDecl::printName() and return true if
169 /// the decl had no name.
170 bool EmitDeclName(const NamedDecl *D);
171};
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000172} // end anonymous namespace
173
174//===----------------------------------------------------------------------===//
175// Generating USRs from ASTS.
176//===----------------------------------------------------------------------===//
177
178bool USRGenerator::EmitDeclName(const NamedDecl *D) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000179 const unsigned startSize = Buf.size();
180 D->printName(Out);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000181 const unsigned endSize = Buf.size();
182 return startSize == endSize;
183}
184
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000185bool USRGenerator::ShouldGenerateLocation(const NamedDecl *D) {
186 if (D->isExternallyVisible())
187 return false;
188 if (D->getParentFunctionOrMethod())
189 return true;
Argyrios Kyrtzidisf12918d2016-11-02 23:42:33 +0000190 SourceLocation Loc = D->getLocation();
191 if (Loc.isInvalid())
192 return false;
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000193 const SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisf12918d2016-11-02 23:42:33 +0000194 return !SM.isInSystemHeader(Loc);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000195}
196
197void USRGenerator::VisitDeclContext(const DeclContext *DC) {
198 if (const NamedDecl *D = dyn_cast<NamedDecl>(DC))
199 Visit(D);
Sam McCall2e50ae62018-02-02 14:13:37 +0000200 else if (isa<LinkageSpecDecl>(DC)) // Linkage specs are transparent in USRs.
201 VisitDeclContext(DC->getParent());
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000202}
203
204void USRGenerator::VisitFieldDecl(const FieldDecl *D) {
205 // The USR for an ivar declared in a class extension is based on the
206 // ObjCInterfaceDecl, not the ObjCCategoryDecl.
207 if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D))
208 Visit(ID);
209 else
210 VisitDeclContext(D->getDeclContext());
211 Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@");
212 if (EmitDeclName(D)) {
213 // Bit fields can be anonymous.
214 IgnoreResults = true;
215 return;
216 }
217}
218
219void USRGenerator::VisitFunctionDecl(const FunctionDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000220 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000221 return;
222
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000223 const unsigned StartSize = Buf.size();
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000224 VisitDeclContext(D->getDeclContext());
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000225 if (Buf.size() == StartSize)
226 GenExtSymbolContainer(D);
227
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000228 bool IsTemplate = false;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000229 if (FunctionTemplateDecl *FunTmpl = D->getDescribedFunctionTemplate()) {
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000230 IsTemplate = true;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000231 Out << "@FT@";
232 VisitTemplateParameterList(FunTmpl->getTemplateParameters());
233 } else
234 Out << "@F@";
Argyrios Kyrtzidisd5719082016-02-15 01:32:36 +0000235
236 PrintingPolicy Policy(Context->getLangOpts());
237 // Forward references can have different template argument names. Suppress the
238 // template argument names in constructors to make their USR more stable.
239 Policy.SuppressTemplateArgsInCXXConstructors = true;
240 D->getDeclName().print(Out, Policy);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000241
242 ASTContext &Ctx = *Context;
Argyrios Kyrtzidis2682f9e2016-03-04 07:17:48 +0000243 if ((!Ctx.getLangOpts().CPlusPlus || D->isExternC()) &&
244 !D->hasAttr<OverloadableAttr>())
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000245 return;
246
247 if (const TemplateArgumentList *
248 SpecArgs = D->getTemplateSpecializationArgs()) {
249 Out << '<';
250 for (unsigned I = 0, N = SpecArgs->size(); I != N; ++I) {
251 Out << '#';
252 VisitTemplateArgument(SpecArgs->get(I));
253 }
254 Out << '>';
255 }
256
257 // Mangle in type information for the arguments.
David Majnemer59f77922016-06-24 04:05:48 +0000258 for (auto PD : D->parameters()) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000259 Out << '#';
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +0000260 VisitType(PD->getType());
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000261 }
262 if (D->isVariadic())
263 Out << '.';
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000264 if (IsTemplate) {
265 // Function templates can be overloaded by return type, for example:
266 // \code
267 // template <class T> typename T::A foo() {}
268 // template <class T> typename T::B foo() {}
269 // \endcode
270 Out << '#';
271 VisitType(D->getReturnType());
272 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000273 Out << '#';
274 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
275 if (MD->isStatic())
276 Out << 'S';
Mikael Nilsson9d2872d2018-12-13 10:15:27 +0000277 // FIXME: OpenCL: Need to consider address spaces
Anastasia Stulovac61eaa52019-01-28 11:37:49 +0000278 if (unsigned quals = MD->getMethodQualifiers().getCVRUQualifiers())
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000279 Out << (char)('0' + quals);
Argyrios Kyrtzidisf5819092014-12-08 08:48:21 +0000280 switch (MD->getRefQualifier()) {
281 case RQ_None: break;
282 case RQ_LValue: Out << '&'; break;
283 case RQ_RValue: Out << "&&"; break;
284 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000285 }
286}
287
288void USRGenerator::VisitNamedDecl(const NamedDecl *D) {
289 VisitDeclContext(D->getDeclContext());
290 Out << "@";
291
292 if (EmitDeclName(D)) {
293 // The string can be empty if the declaration has no name; e.g., it is
294 // the ParmDecl with no name for declaration of a function pointer type,
295 // e.g.: void (*f)(void *);
296 // In this case, don't generate a USR.
297 IgnoreResults = true;
298 }
299}
300
301void USRGenerator::VisitVarDecl(const VarDecl *D) {
302 // VarDecls can be declared 'extern' within a function or method body,
303 // but their enclosing DeclContext is the function, not the TU. We need
304 // to check the storage class to correctly generate the USR.
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000305 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000306 return;
307
308 VisitDeclContext(D->getDeclContext());
309
Argyrios Kyrtzidis9d8ab722016-11-07 21:20:15 +0000310 if (VarTemplateDecl *VarTmpl = D->getDescribedVarTemplate()) {
311 Out << "@VT";
312 VisitTemplateParameterList(VarTmpl->getTemplateParameters());
313 } else if (const VarTemplatePartialSpecializationDecl *PartialSpec
314 = dyn_cast<VarTemplatePartialSpecializationDecl>(D)) {
315 Out << "@VP";
316 VisitTemplateParameterList(PartialSpec->getTemplateParameters());
317 }
318
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000319 // Variables always have simple names.
320 StringRef s = D->getName();
321
322 // The string can be empty if the declaration has no name; e.g., it is
323 // the ParmDecl with no name for declaration of a function pointer type, e.g.:
324 // void (*f)(void *);
325 // In this case, don't generate a USR.
326 if (s.empty())
327 IgnoreResults = true;
328 else
329 Out << '@' << s;
Argyrios Kyrtzidis9d8ab722016-11-07 21:20:15 +0000330
331 // For a template specialization, mangle the template arguments.
332 if (const VarTemplateSpecializationDecl *Spec
333 = dyn_cast<VarTemplateSpecializationDecl>(D)) {
Argyrios Kyrtzidis7d90ed02017-02-15 16:16:27 +0000334 const TemplateArgumentList &Args = Spec->getTemplateArgs();
Argyrios Kyrtzidis9d8ab722016-11-07 21:20:15 +0000335 Out << '>';
336 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
337 Out << '#';
338 VisitTemplateArgument(Args.get(I));
339 }
340 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000341}
342
Fangrui Song63a8b6c2018-10-09 01:02:56 +0000343void USRGenerator::VisitBindingDecl(const BindingDecl *D) {
344 if (isLocal(D) && GenLoc(D, /*IncludeOffset=*/true))
345 return;
346 VisitNamedDecl(D);
347}
348
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000349void USRGenerator::VisitNonTypeTemplateParmDecl(
350 const NonTypeTemplateParmDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000351 GenLoc(D, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000352}
353
354void USRGenerator::VisitTemplateTemplateParmDecl(
355 const TemplateTemplateParmDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000356 GenLoc(D, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000357}
358
359void USRGenerator::VisitNamespaceDecl(const NamespaceDecl *D) {
360 if (D->isAnonymousNamespace()) {
361 Out << "@aN";
362 return;
363 }
364
365 VisitDeclContext(D->getDeclContext());
366 if (!IgnoreResults)
367 Out << "@N@" << D->getName();
368}
369
370void USRGenerator::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
371 VisitFunctionDecl(D->getTemplatedDecl());
372}
373
374void USRGenerator::VisitClassTemplateDecl(const ClassTemplateDecl *D) {
375 VisitTagDecl(D->getTemplatedDecl());
376}
377
378void USRGenerator::VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
379 VisitDeclContext(D->getDeclContext());
380 if (!IgnoreResults)
Fangrui Song6907ce22018-07-30 19:24:48 +0000381 Out << "@NA@" << D->getName();
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000382}
383
384void USRGenerator::VisitObjCMethodDecl(const ObjCMethodDecl *D) {
385 const DeclContext *container = D->getDeclContext();
386 if (const ObjCProtocolDecl *pd = dyn_cast<ObjCProtocolDecl>(container)) {
387 Visit(pd);
388 }
389 else {
390 // The USR for a method declared in a class extension or category is based on
391 // the ObjCInterfaceDecl, not the ObjCCategoryDecl.
392 const ObjCInterfaceDecl *ID = D->getClassInterface();
393 if (!ID) {
394 IgnoreResults = true;
395 return;
396 }
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +0000397 auto getCategoryContext = [](const ObjCMethodDecl *D) ->
398 const ObjCCategoryDecl * {
399 if (auto *CD = dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
400 return CD;
401 if (auto *ICD = dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
402 return ICD->getCategoryDecl();
403 return nullptr;
404 };
405 auto *CD = getCategoryContext(D);
406 VisitObjCContainerDecl(ID, CD);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000407 }
408 // Ideally we would use 'GenObjCMethod', but this is such a hot path
409 // for Objective-C code that we don't want to use
410 // DeclarationName::getAsString().
411 Out << (D->isInstanceMethod() ? "(im)" : "(cm)")
412 << DeclarationName(D->getSelector());
413}
414
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +0000415void USRGenerator::VisitObjCContainerDecl(const ObjCContainerDecl *D,
416 const ObjCCategoryDecl *CatD) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000417 switch (D->getKind()) {
418 default:
419 llvm_unreachable("Invalid ObjC container.");
420 case Decl::ObjCInterface:
421 case Decl::ObjCImplementation:
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +0000422 GenObjCClass(D->getName(), GetExternalSourceContainer(D),
423 GetExternalSourceContainer(CatD));
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000424 break;
425 case Decl::ObjCCategory: {
426 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
427 const ObjCInterfaceDecl *ID = CD->getClassInterface();
428 if (!ID) {
429 // Handle invalid code where the @interface might not
430 // have been specified.
431 // FIXME: We should be able to generate this USR even if the
432 // @interface isn't available.
433 IgnoreResults = true;
434 return;
435 }
436 // Specially handle class extensions, which are anonymous categories.
437 // We want to mangle in the location to uniquely distinguish them.
438 if (CD->IsClassExtension()) {
439 Out << "objc(ext)" << ID->getName() << '@';
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000440 GenLoc(CD, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000441 }
442 else
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000443 GenObjCCategory(ID->getName(), CD->getName(),
444 GetExternalSourceContainer(ID),
445 GetExternalSourceContainer(CD));
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000446
447 break;
448 }
449 case Decl::ObjCCategoryImpl: {
450 const ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
451 const ObjCInterfaceDecl *ID = CD->getClassInterface();
452 if (!ID) {
453 // Handle invalid code where the @interface might not
454 // have been specified.
455 // FIXME: We should be able to generate this USR even if the
456 // @interface isn't available.
457 IgnoreResults = true;
458 return;
459 }
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000460 GenObjCCategory(ID->getName(), CD->getName(),
461 GetExternalSourceContainer(ID),
462 GetExternalSourceContainer(CD));
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000463 break;
464 }
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000465 case Decl::ObjCProtocol: {
466 const ObjCProtocolDecl *PD = cast<ObjCProtocolDecl>(D);
467 GenObjCProtocol(PD->getName(), GetExternalSourceContainer(PD));
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000468 break;
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000469 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000470 }
471}
472
473void USRGenerator::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
474 // The USR for a property declared in a class extension or category is based
475 // on the ObjCInterfaceDecl, not the ObjCCategoryDecl.
476 if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D))
477 Visit(ID);
478 else
479 Visit(cast<Decl>(D->getDeclContext()));
Argyrios Kyrtzidisd9849a92016-07-15 22:18:19 +0000480 GenObjCProperty(D->getName(), D->isClassProperty());
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000481}
482
483void USRGenerator::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
484 if (ObjCPropertyDecl *PD = D->getPropertyDecl()) {
485 VisitObjCPropertyDecl(PD);
486 return;
487 }
488
489 IgnoreResults = true;
490}
491
492void USRGenerator::VisitTagDecl(const TagDecl *D) {
493 // Add the location of the tag decl to handle resolution across
494 // translation units.
Argyrios Kyrtzidis73321062016-03-04 07:17:53 +0000495 if (!isa<EnumDecl>(D) &&
496 ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000497 return;
498
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000499 GenExtSymbolContainer(D);
500
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000501 D = D->getCanonicalDecl();
502 VisitDeclContext(D->getDeclContext());
503
504 bool AlreadyStarted = false;
505 if (const CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(D)) {
506 if (ClassTemplateDecl *ClassTmpl = CXXRecord->getDescribedClassTemplate()) {
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 << "@ST"; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000513 case TTK_Union: Out << "@UT"; break;
514 case TTK_Enum: llvm_unreachable("enum template");
515 }
516 VisitTemplateParameterList(ClassTmpl->getTemplateParameters());
517 } else if (const ClassTemplatePartialSpecializationDecl *PartialSpec
518 = dyn_cast<ClassTemplatePartialSpecializationDecl>(CXXRecord)) {
519 AlreadyStarted = true;
Fangrui Song6907ce22018-07-30 19:24:48 +0000520
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000521 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 << "@SP"; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000525 case TTK_Union: Out << "@UP"; break;
526 case TTK_Enum: llvm_unreachable("enum partial specialization");
Fangrui Song6907ce22018-07-30 19:24:48 +0000527 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000528 VisitTemplateParameterList(PartialSpec->getTemplateParameters());
529 }
530 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000531
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000532 if (!AlreadyStarted) {
533 switch (D->getTagKind()) {
534 case TTK_Interface:
Argyrios Kyrtzidisf66cef72014-12-08 08:48:33 +0000535 case TTK_Class:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000536 case TTK_Struct: Out << "@S"; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000537 case TTK_Union: Out << "@U"; break;
538 case TTK_Enum: Out << "@E"; break;
539 }
540 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000541
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000542 Out << '@';
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000543 assert(Buf.size() > 0);
544 const unsigned off = Buf.size() - 1;
545
546 if (EmitDeclName(D)) {
547 if (const TypedefNameDecl *TD = D->getTypedefNameForAnonDecl()) {
548 Buf[off] = 'A';
549 Out << '@' << *TD;
550 }
Argyrios Kyrtzidis80537a42014-12-08 08:48:37 +0000551 else {
552 if (D->isEmbeddedInDeclarator() && !D->isFreeStanding()) {
553 printLoc(Out, D->getLocation(), Context->getSourceManager(), true);
Argyrios Kyrtzidis73321062016-03-04 07:17:53 +0000554 } else {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000555 Buf[off] = 'a';
Argyrios Kyrtzidis73321062016-03-04 07:17:53 +0000556 if (auto *ED = dyn_cast<EnumDecl>(D)) {
557 // Distinguish USRs of anonymous enums by using their first enumerator.
558 auto enum_range = ED->enumerators();
559 if (enum_range.begin() != enum_range.end()) {
560 Out << '@' << **enum_range.begin();
561 }
562 }
563 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000564 }
Argyrios Kyrtzidis80537a42014-12-08 08:48:37 +0000565 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000566
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000567 // For a class template specialization, mangle the template arguments.
568 if (const ClassTemplateSpecializationDecl *Spec
569 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
Argyrios Kyrtzidis7d90ed02017-02-15 16:16:27 +0000570 const TemplateArgumentList &Args = Spec->getTemplateArgs();
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000571 Out << '>';
572 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
573 Out << '#';
574 VisitTemplateArgument(Args.get(I));
575 }
576 }
577}
578
579void USRGenerator::VisitTypedefDecl(const TypedefDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000580 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000581 return;
582 const DeclContext *DC = D->getDeclContext();
583 if (const NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
584 Visit(DCN);
585 Out << "@T@";
586 Out << D->getName();
587}
588
589void USRGenerator::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000590 GenLoc(D, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000591}
592
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000593void USRGenerator::GenExtSymbolContainer(const NamedDecl *D) {
594 StringRef Container = GetExternalSourceContainer(D);
595 if (!Container.empty())
596 Out << "@M@" << Container;
597}
598
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000599bool USRGenerator::GenLoc(const Decl *D, bool IncludeOffset) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000600 if (generatedLoc)
601 return IgnoreResults;
602 generatedLoc = true;
Dmitri Gribenko237769e2014-03-28 22:21:26 +0000603
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000604 // Guard against null declarations in invalid code.
605 if (!D) {
606 IgnoreResults = true;
607 return true;
608 }
609
610 // Use the location of canonical decl.
611 D = D->getCanonicalDecl();
612
Dmitri Gribenko237769e2014-03-28 22:21:26 +0000613 IgnoreResults =
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000614 IgnoreResults || printLoc(Out, D->getBeginLoc(),
Dmitri Gribenko237769e2014-03-28 22:21:26 +0000615 Context->getSourceManager(), IncludeOffset);
616
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000617 return IgnoreResults;
618}
619
Ben Langmuirfd6e39c2017-08-16 23:12:21 +0000620static void printQualifier(llvm::raw_ostream &Out, ASTContext &Ctx, NestedNameSpecifier *NNS) {
621 // FIXME: Encode the qualifier, don't just print it.
622 PrintingPolicy PO(Ctx.getLangOpts());
623 PO.SuppressTagKeyword = true;
624 PO.SuppressUnwrittenScope = true;
625 PO.ConstantArraySizeAsWritten = false;
626 PO.AnonymousTagLocations = false;
627 NNS->print(Out, PO);
628}
629
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000630void USRGenerator::VisitType(QualType T) {
631 // This method mangles in USR information for types. It can possibly
632 // just reuse the naming-mangling logic used by codegen, although the
633 // requirements for USRs might not be the same.
634 ASTContext &Ctx = *Context;
635
636 do {
637 T = Ctx.getCanonicalType(T);
638 Qualifiers Q = T.getQualifiers();
639 unsigned qVal = 0;
640 if (Q.hasConst())
641 qVal |= 0x1;
642 if (Q.hasVolatile())
643 qVal |= 0x2;
644 if (Q.hasRestrict())
645 qVal |= 0x4;
646 if(qVal)
647 Out << ((char) ('0' + qVal));
648
649 // Mangle in ObjC GC qualifiers?
650
651 if (const PackExpansionType *Expansion = T->getAs<PackExpansionType>()) {
652 Out << 'P';
653 T = Expansion->getPattern();
654 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000655
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000656 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
657 unsigned char c = '\0';
658 switch (BT->getKind()) {
659 case BuiltinType::Void:
660 c = 'v'; break;
661 case BuiltinType::Bool:
662 c = 'b'; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000663 case BuiltinType::UChar:
664 c = 'c'; break;
Richard Smith3a8244d2018-05-01 05:02:45 +0000665 case BuiltinType::Char8:
666 c = 'u'; break; // FIXME: Check this doesn't collide
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000667 case BuiltinType::Char16:
668 c = 'q'; break;
669 case BuiltinType::Char32:
670 c = 'w'; break;
671 case BuiltinType::UShort:
672 c = 's'; break;
673 case BuiltinType::UInt:
674 c = 'i'; break;
675 case BuiltinType::ULong:
676 c = 'l'; break;
677 case BuiltinType::ULongLong:
678 c = 'k'; break;
679 case BuiltinType::UInt128:
680 c = 'j'; break;
Argyrios Kyrtzidis56af7e62014-12-08 09:09:05 +0000681 case BuiltinType::Char_U:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000682 case BuiltinType::Char_S:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000683 c = 'C'; break;
Argyrios Kyrtzidisca044542014-12-08 08:48:17 +0000684 case BuiltinType::SChar:
685 c = 'r'; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000686 case BuiltinType::WChar_S:
687 case BuiltinType::WChar_U:
688 c = 'W'; break;
689 case BuiltinType::Short:
690 c = 'S'; break;
691 case BuiltinType::Int:
692 c = 'I'; break;
693 case BuiltinType::Long:
694 c = 'L'; break;
695 case BuiltinType::LongLong:
696 c = 'K'; break;
697 case BuiltinType::Int128:
698 c = 'J'; break;
Sjoerd Meijercc623ad2017-09-08 15:15:00 +0000699 case BuiltinType::Float16:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000700 case BuiltinType::Half:
701 c = 'h'; break;
702 case BuiltinType::Float:
703 c = 'f'; break;
704 case BuiltinType::Double:
705 c = 'd'; break;
706 case BuiltinType::LongDouble:
707 c = 'D'; break;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000708 case BuiltinType::Float128:
709 c = 'Q'; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000710 case BuiltinType::NullPtr:
711 c = 'n'; break;
712#define BUILTIN_TYPE(Id, SingletonId)
713#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
714#include "clang/AST/BuiltinTypes.def"
715 case BuiltinType::Dependent:
Alexey Bader954ba212016-04-08 13:40:33 +0000716#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
717 case BuiltinType::Id:
Alexey Baderb62f1442016-04-13 08:33:41 +0000718#include "clang/Basic/OpenCLImageTypes.def"
Andrew Savonichev3fee3512018-11-08 11:25:41 +0000719#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
720 case BuiltinType::Id:
721#include "clang/Basic/OpenCLExtensionTypes.def"
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000722 case BuiltinType::OCLEvent:
Alexey Bader9c8453f2015-09-15 11:18:52 +0000723 case BuiltinType::OCLClkEvent:
724 case BuiltinType::OCLQueue:
Alexey Bader9c8453f2015-09-15 11:18:52 +0000725 case BuiltinType::OCLReserveID:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000726 case BuiltinType::OCLSampler:
Richard Sandifordeb485fb2019-08-09 08:52:54 +0000727#define SVE_TYPE(Name, Id, SingletonId) \
728 case BuiltinType::Id:
729#include "clang/Basic/AArch64SVEACLETypes.def"
Leonard Chanf921d852018-06-04 16:07:52 +0000730 case BuiltinType::ShortAccum:
731 case BuiltinType::Accum:
732 case BuiltinType::LongAccum:
733 case BuiltinType::UShortAccum:
734 case BuiltinType::UAccum:
735 case BuiltinType::ULongAccum:
Leonard Chanab80f3c2018-06-14 14:53:51 +0000736 case BuiltinType::ShortFract:
737 case BuiltinType::Fract:
738 case BuiltinType::LongFract:
739 case BuiltinType::UShortFract:
740 case BuiltinType::UFract:
741 case BuiltinType::ULongFract:
742 case BuiltinType::SatShortAccum:
743 case BuiltinType::SatAccum:
744 case BuiltinType::SatLongAccum:
745 case BuiltinType::SatUShortAccum:
746 case BuiltinType::SatUAccum:
747 case BuiltinType::SatULongAccum:
748 case BuiltinType::SatShortFract:
749 case BuiltinType::SatFract:
750 case BuiltinType::SatLongFract:
751 case BuiltinType::SatUShortFract:
752 case BuiltinType::SatUFract:
753 case BuiltinType::SatULongFract:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000754 IgnoreResults = true;
755 return;
756 case BuiltinType::ObjCId:
757 c = 'o'; break;
758 case BuiltinType::ObjCClass:
759 c = 'O'; break;
760 case BuiltinType::ObjCSel:
761 c = 'e'; break;
762 }
763 Out << c;
764 return;
765 }
766
767 // If we have already seen this (non-built-in) type, use a substitution
768 // encoding.
769 llvm::DenseMap<const Type *, unsigned>::iterator Substitution
770 = TypeSubstitutions.find(T.getTypePtr());
771 if (Substitution != TypeSubstitutions.end()) {
772 Out << 'S' << Substitution->second << '_';
773 return;
774 } else {
775 // Record this as a substitution.
776 unsigned Number = TypeSubstitutions.size();
777 TypeSubstitutions[T.getTypePtr()] = Number;
778 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000779
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000780 if (const PointerType *PT = T->getAs<PointerType>()) {
781 Out << '*';
782 T = PT->getPointeeType();
783 continue;
784 }
Argyrios Kyrtzidis9c998672016-03-04 07:17:43 +0000785 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
786 Out << '*';
787 T = OPT->getPointeeType();
788 continue;
789 }
Argyrios Kyrtzidis0e387dc2014-12-08 08:48:27 +0000790 if (const RValueReferenceType *RT = T->getAs<RValueReferenceType>()) {
791 Out << "&&";
792 T = RT->getPointeeType();
793 continue;
794 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000795 if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
796 Out << '&';
797 T = RT->getPointeeType();
798 continue;
799 }
800 if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) {
801 Out << 'F';
Alp Toker314cc812014-01-25 16:55:45 +0000802 VisitType(FT->getReturnType());
Jan Korouse6a02422017-10-10 00:35:16 +0000803 Out << '(';
804 for (const auto &I : FT->param_types()) {
805 Out << '#';
Aaron Ballman40bd0aa2014-03-17 15:23:01 +0000806 VisitType(I);
Jan Korouse6a02422017-10-10 00:35:16 +0000807 }
808 Out << ')';
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000809 if (FT->isVariadic())
810 Out << '.';
811 return;
812 }
813 if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) {
814 Out << 'B';
815 T = BT->getPointeeType();
816 continue;
817 }
818 if (const ComplexType *CT = T->getAs<ComplexType>()) {
819 Out << '<';
820 T = CT->getElementType();
821 continue;
822 }
823 if (const TagType *TT = T->getAs<TagType>()) {
824 Out << '$';
825 VisitTagDecl(TT->getDecl());
826 return;
827 }
Argyrios Kyrtzidis9c998672016-03-04 07:17:43 +0000828 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
829 Out << '$';
830 VisitObjCInterfaceDecl(OIT->getDecl());
831 return;
832 }
833 if (const ObjCObjectType *OIT = T->getAs<ObjCObjectType>()) {
834 Out << 'Q';
835 VisitType(OIT->getBaseType());
836 for (auto *Prot : OIT->getProtocols())
837 VisitObjCProtocolDecl(Prot);
838 return;
839 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000840 if (const TemplateTypeParmType *TTP = T->getAs<TemplateTypeParmType>()) {
841 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
842 return;
843 }
844 if (const TemplateSpecializationType *Spec
845 = T->getAs<TemplateSpecializationType>()) {
846 Out << '>';
847 VisitTemplateName(Spec->getTemplateName());
848 Out << Spec->getNumArgs();
849 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
850 VisitTemplateArgument(Spec->getArg(I));
851 return;
852 }
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000853 if (const DependentNameType *DNT = T->getAs<DependentNameType>()) {
854 Out << '^';
Ben Langmuirfd6e39c2017-08-16 23:12:21 +0000855 printQualifier(Out, Ctx, DNT->getQualifier());
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000856 Out << ':' << DNT->getIdentifier()->getName();
857 return;
858 }
Argyrios Kyrtzidis1d5e5422014-12-08 08:48:43 +0000859 if (const InjectedClassNameType *InjT = T->getAs<InjectedClassNameType>()) {
860 T = InjT->getInjectedSpecializationType();
861 continue;
862 }
Alex Lorenz45c423b2017-04-28 09:46:36 +0000863 if (const auto *VT = T->getAs<VectorType>()) {
864 Out << (T->isExtVectorType() ? ']' : '[');
865 Out << VT->getNumElements();
866 T = VT->getElementType();
867 continue;
868 }
Jan Korous663ba152017-10-09 19:51:33 +0000869 if (const auto *const AT = dyn_cast<ArrayType>(T)) {
870 Out << '{';
871 switch (AT->getSizeModifier()) {
872 case ArrayType::Static:
873 Out << 's';
874 break;
875 case ArrayType::Star:
876 Out << '*';
877 break;
878 case ArrayType::Normal:
879 Out << 'n';
880 break;
881 }
882 if (const auto *const CAT = dyn_cast<ConstantArrayType>(T))
883 Out << CAT->getSize();
884
885 T = AT->getElementType();
886 continue;
887 }
Alex Lorenz45c423b2017-04-28 09:46:36 +0000888
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000889 // Unhandled type.
890 Out << ' ';
891 break;
892 } while (true);
893}
894
895void USRGenerator::VisitTemplateParameterList(
896 const TemplateParameterList *Params) {
897 if (!Params)
898 return;
899 Out << '>' << Params->size();
900 for (TemplateParameterList::const_iterator P = Params->begin(),
901 PEnd = Params->end();
902 P != PEnd; ++P) {
903 Out << '#';
904 if (isa<TemplateTypeParmDecl>(*P)) {
905 if (cast<TemplateTypeParmDecl>(*P)->isParameterPack())
906 Out<< 'p';
907 Out << 'T';
908 continue;
909 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000910
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000911 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
912 if (NTTP->isParameterPack())
913 Out << 'p';
914 Out << 'N';
915 VisitType(NTTP->getType());
916 continue;
917 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000918
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000919 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
920 if (TTP->isParameterPack())
921 Out << 'p';
922 Out << 't';
923 VisitTemplateParameterList(TTP->getTemplateParameters());
924 }
925}
926
927void USRGenerator::VisitTemplateName(TemplateName Name) {
928 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
929 if (TemplateTemplateParmDecl *TTP
930 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
931 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
932 return;
933 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000934
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000935 Visit(Template);
936 return;
937 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000938
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000939 // FIXME: Visit dependent template names.
940}
941
942void USRGenerator::VisitTemplateArgument(const TemplateArgument &Arg) {
943 switch (Arg.getKind()) {
944 case TemplateArgument::Null:
945 break;
946
947 case TemplateArgument::Declaration:
948 Visit(Arg.getAsDecl());
949 break;
950
951 case TemplateArgument::NullPtr:
952 break;
953
954 case TemplateArgument::TemplateExpansion:
955 Out << 'P'; // pack expansion of...
Reid Kleckner4dc0b1a2018-11-01 19:54:45 +0000956 LLVM_FALLTHROUGH;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000957 case TemplateArgument::Template:
958 VisitTemplateName(Arg.getAsTemplateOrTemplatePattern());
959 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000960
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000961 case TemplateArgument::Expression:
962 // FIXME: Visit expressions.
963 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000964
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000965 case TemplateArgument::Pack:
966 Out << 'p' << Arg.pack_size();
Aaron Ballman2a89e852014-07-15 21:32:31 +0000967 for (const auto &P : Arg.pack_elements())
968 VisitTemplateArgument(P);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000969 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000970
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000971 case TemplateArgument::Type:
972 VisitType(Arg.getAsType());
973 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000974
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000975 case TemplateArgument::Integral:
976 Out << 'V';
977 VisitType(Arg.getIntegralType());
978 Out << Arg.getAsIntegral();
979 break;
980 }
981}
982
Ben Langmuirfd6e39c2017-08-16 23:12:21 +0000983void USRGenerator::VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D) {
984 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
985 return;
986 VisitDeclContext(D->getDeclContext());
987 Out << "@UUV@";
988 printQualifier(Out, D->getASTContext(), D->getQualifier());
989 EmitDeclName(D);
990}
991
992void USRGenerator::VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D) {
993 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
994 return;
995 VisitDeclContext(D->getDeclContext());
996 Out << "@UUT@";
997 printQualifier(Out, D->getASTContext(), D->getQualifier());
998 Out << D->getName(); // Simple name.
999}
1000
1001
1002
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001003//===----------------------------------------------------------------------===//
1004// USR generation functions.
1005//===----------------------------------------------------------------------===//
1006
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +00001007static void combineClassAndCategoryExtContainers(StringRef ClsSymDefinedIn,
1008 StringRef CatSymDefinedIn,
1009 raw_ostream &OS) {
1010 if (ClsSymDefinedIn.empty() && CatSymDefinedIn.empty())
1011 return;
1012 if (CatSymDefinedIn.empty()) {
1013 OS << "@M@" << ClsSymDefinedIn << '@';
1014 return;
1015 }
1016 OS << "@CM@" << CatSymDefinedIn << '@';
1017 if (ClsSymDefinedIn != CatSymDefinedIn) {
1018 OS << ClsSymDefinedIn << '@';
1019 }
1020}
1021
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +00001022void clang::index::generateUSRForObjCClass(StringRef Cls, raw_ostream &OS,
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +00001023 StringRef ExtSymDefinedIn,
1024 StringRef CategoryContextExtSymbolDefinedIn) {
1025 combineClassAndCategoryExtContainers(ExtSymDefinedIn,
1026 CategoryContextExtSymbolDefinedIn, OS);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001027 OS << "objc(cs)" << Cls;
1028}
1029
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +00001030void clang::index::generateUSRForObjCCategory(StringRef Cls, StringRef Cat,
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +00001031 raw_ostream &OS,
1032 StringRef ClsSymDefinedIn,
1033 StringRef CatSymDefinedIn) {
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +00001034 combineClassAndCategoryExtContainers(ClsSymDefinedIn, CatSymDefinedIn, OS);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001035 OS << "objc(cy)" << Cls << '@' << Cat;
1036}
1037
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +00001038void clang::index::generateUSRForObjCIvar(StringRef Ivar, raw_ostream &OS) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001039 OS << '@' << Ivar;
1040}
1041
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +00001042void clang::index::generateUSRForObjCMethod(StringRef Sel,
1043 bool IsInstanceMethod,
1044 raw_ostream &OS) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001045 OS << (IsInstanceMethod ? "(im)" : "(cm)") << Sel;
1046}
1047
Argyrios Kyrtzidisd9849a92016-07-15 22:18:19 +00001048void clang::index::generateUSRForObjCProperty(StringRef Prop, bool isClassProp,
1049 raw_ostream &OS) {
1050 OS << (isClassProp ? "(cpy)" : "(py)") << Prop;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001051}
1052
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +00001053void clang::index::generateUSRForObjCProtocol(StringRef Prot, raw_ostream &OS,
1054 StringRef ExtSymDefinedIn) {
1055 if (!ExtSymDefinedIn.empty())
1056 OS << "@M@" << ExtSymDefinedIn << '@';
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001057 OS << "objc(pl)" << Prot;
1058}
1059
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +00001060void clang::index::generateUSRForGlobalEnum(StringRef EnumName, raw_ostream &OS,
1061 StringRef ExtSymDefinedIn) {
1062 if (!ExtSymDefinedIn.empty())
1063 OS << "@M@" << ExtSymDefinedIn;
1064 OS << "@E@" << EnumName;
1065}
1066
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +00001067void clang::index::generateUSRForEnumConstant(StringRef EnumConstantName,
1068 raw_ostream &OS) {
1069 OS << '@' << EnumConstantName;
1070}
1071
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +00001072bool clang::index::generateUSRForDecl(const Decl *D,
1073 SmallVectorImpl<char> &Buf) {
Argyrios Kyrtzidisf12918d2016-11-02 23:42:33 +00001074 if (!D)
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001075 return true;
Argyrios Kyrtzidisf12918d2016-11-02 23:42:33 +00001076 // We don't ignore decls with invalid source locations. Implicit decls, like
1077 // C++'s operator new function, can have invalid locations but it is fine to
1078 // create USRs that can identify them.
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001079
1080 USRGenerator UG(&D->getASTContext(), Buf);
1081 UG.Visit(D);
1082 return UG.ignoreResults();
1083}
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001084
Richard Smith66a81862015-05-04 02:25:31 +00001085bool clang::index::generateUSRForMacro(const MacroDefinitionRecord *MD,
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001086 const SourceManager &SM,
1087 SmallVectorImpl<char> &Buf) {
Argyrios Kyrtzidis5b7a09a2017-02-02 16:13:10 +00001088 if (!MD)
1089 return true;
1090 return generateUSRForMacro(MD->getName()->getName(), MD->getLocation(),
1091 SM, Buf);
1092
1093}
1094
1095bool clang::index::generateUSRForMacro(StringRef MacroName, SourceLocation Loc,
1096 const SourceManager &SM,
1097 SmallVectorImpl<char> &Buf) {
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001098 // Don't generate USRs for things with invalid locations.
Argyrios Kyrtzidis5b7a09a2017-02-02 16:13:10 +00001099 if (MacroName.empty() || Loc.isInvalid())
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001100 return true;
1101
1102 llvm::raw_svector_ostream Out(Buf);
1103
1104 // Assume that system headers are sane. Don't put source location
1105 // information into the USR if the macro comes from a system header.
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001106 bool ShouldGenerateLocation = !SM.isInSystemHeader(Loc);
1107
1108 Out << getUSRSpacePrefix();
1109 if (ShouldGenerateLocation)
1110 printLoc(Out, Loc, SM, /*IncludeOffset=*/true);
1111 Out << "@macro@";
Argyrios Kyrtzidis5b7a09a2017-02-02 16:13:10 +00001112 Out << MacroName;
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001113 return false;
1114}
Argyrios Kyrtzidis32e5d862018-09-18 15:02:56 +00001115
Ilya Biryukova6224842018-11-26 15:24:48 +00001116bool clang::index::generateUSRForType(QualType T, ASTContext &Ctx,
1117 SmallVectorImpl<char> &Buf) {
1118 if (T.isNull())
1119 return true;
1120 T = T.getCanonicalType();
1121
1122 USRGenerator UG(&Ctx, Buf);
1123 UG.VisitType(T);
1124 return UG.ignoreResults();
1125}
1126
Argyrios Kyrtzidis32e5d862018-09-18 15:02:56 +00001127bool clang::index::generateFullUSRForModule(const Module *Mod,
1128 raw_ostream &OS) {
1129 if (!Mod->Parent)
1130 return generateFullUSRForTopLevelModuleName(Mod->Name, OS);
1131 if (generateFullUSRForModule(Mod->Parent, OS))
1132 return true;
1133 return generateUSRFragmentForModule(Mod, OS);
1134}
1135
1136bool clang::index::generateFullUSRForTopLevelModuleName(StringRef ModName,
1137 raw_ostream &OS) {
1138 OS << getUSRSpacePrefix();
1139 return generateUSRFragmentForModuleName(ModName, OS);
1140}
1141
1142bool clang::index::generateUSRFragmentForModule(const Module *Mod,
1143 raw_ostream &OS) {
1144 return generateUSRFragmentForModuleName(Mod->Name, OS);
1145}
1146
1147bool clang::index::generateUSRFragmentForModuleName(StringRef ModName,
1148 raw_ostream &OS) {
1149 OS << "@M@" << ModName;
1150 return false;
1151}