blob: f3eb653f10fa4c7b5a758b8d927a6aefb31530e1 [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"
Reid Kleckner60573ae2019-11-15 17:31:55 -080011#include "clang/AST/Attr.h"
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000012#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);
Fangrui Song63a8b6c2018-10-09 01:02:56 +0000100 void VisitBindingDecl(const BindingDecl *D);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000101 void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D);
102 void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D);
Ben Langmuirfd6e39c2017-08-16 23:12:21 +0000103 void VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D);
104 void VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D);
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000105
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000106 void VisitLinkageSpecDecl(const LinkageSpecDecl *D) {
Sam McCall2e50ae62018-02-02 14:13:37 +0000107 IgnoreResults = true; // No USRs for linkage specs themselves.
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000108 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000109
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000110 void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
111 IgnoreResults = true;
112 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000113
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000114 void VisitUsingDecl(const UsingDecl *D) {
Kadir Cetinkayaa87ada02019-02-26 14:23:12 +0000115 VisitDeclContext(D->getDeclContext());
116 Out << "@UD@";
117
118 bool EmittedDeclName = !EmitDeclName(D);
119 assert(EmittedDeclName && "EmitDeclName can not fail for UsingDecls");
120 (void)EmittedDeclName;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000121 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000122
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000123 bool ShouldGenerateLocation(const NamedDecl *D);
124
125 bool isLocal(const NamedDecl *D) {
Craig Topper236bde32014-05-26 06:21:51 +0000126 return D->getParentFunctionOrMethod() != nullptr;
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000127 }
128
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000129 void GenExtSymbolContainer(const NamedDecl *D);
130
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000131 /// Generate the string component containing the location of the
132 /// declaration.
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000133 bool GenLoc(const Decl *D, bool IncludeOffset);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000134
135 /// String generation methods used both by the visitation methods
136 /// and from other clients that want to directly generate USRs. These
137 /// methods do not construct complete USRs (which incorporate the parents
138 /// of an AST element), but only the fragments concerning the AST element
139 /// itself.
140
141 /// Generate a USR for an Objective-C class.
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +0000142 void GenObjCClass(StringRef cls, StringRef ExtSymDefinedIn,
143 StringRef CategoryContextExtSymbolDefinedIn) {
144 generateUSRForObjCClass(cls, Out, ExtSymDefinedIn,
145 CategoryContextExtSymbolDefinedIn);
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 for an Objective-C class category.
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000149 void GenObjCCategory(StringRef cls, StringRef cat,
150 StringRef clsExt, StringRef catExt) {
151 generateUSRForObjCCategory(cls, cat, Out, clsExt, catExt);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000152 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000153
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000154 /// Generate a USR fragment for an Objective-C property.
Argyrios Kyrtzidisd9849a92016-07-15 22:18:19 +0000155 void GenObjCProperty(StringRef prop, bool isClassProp) {
156 generateUSRForObjCProperty(prop, isClassProp, Out);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000157 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000158
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000159 /// Generate a USR for an Objective-C protocol.
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000160 void GenObjCProtocol(StringRef prot, StringRef ext) {
161 generateUSRForObjCProtocol(prot, Out, ext);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000162 }
163
164 void VisitType(QualType T);
165 void VisitTemplateParameterList(const TemplateParameterList *Params);
166 void VisitTemplateName(TemplateName Name);
167 void VisitTemplateArgument(const TemplateArgument &Arg);
Fangrui Song6907ce22018-07-30 19:24:48 +0000168
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000169 /// Emit a Decl's name using NamedDecl::printName() and return true if
170 /// the decl had no name.
171 bool EmitDeclName(const NamedDecl *D);
172};
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000173} // end anonymous namespace
174
175//===----------------------------------------------------------------------===//
176// Generating USRs from ASTS.
177//===----------------------------------------------------------------------===//
178
179bool USRGenerator::EmitDeclName(const NamedDecl *D) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000180 const unsigned startSize = Buf.size();
181 D->printName(Out);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000182 const unsigned endSize = Buf.size();
183 return startSize == endSize;
184}
185
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000186bool USRGenerator::ShouldGenerateLocation(const NamedDecl *D) {
187 if (D->isExternallyVisible())
188 return false;
189 if (D->getParentFunctionOrMethod())
190 return true;
Argyrios Kyrtzidisf12918d2016-11-02 23:42:33 +0000191 SourceLocation Loc = D->getLocation();
192 if (Loc.isInvalid())
193 return false;
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000194 const SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisf12918d2016-11-02 23:42:33 +0000195 return !SM.isInSystemHeader(Loc);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000196}
197
198void USRGenerator::VisitDeclContext(const DeclContext *DC) {
199 if (const NamedDecl *D = dyn_cast<NamedDecl>(DC))
200 Visit(D);
Sam McCall2e50ae62018-02-02 14:13:37 +0000201 else if (isa<LinkageSpecDecl>(DC)) // Linkage specs are transparent in USRs.
202 VisitDeclContext(DC->getParent());
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000203}
204
205void USRGenerator::VisitFieldDecl(const FieldDecl *D) {
206 // The USR for an ivar declared in a class extension is based on the
207 // ObjCInterfaceDecl, not the ObjCCategoryDecl.
208 if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D))
209 Visit(ID);
210 else
211 VisitDeclContext(D->getDeclContext());
212 Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@");
213 if (EmitDeclName(D)) {
214 // Bit fields can be anonymous.
215 IgnoreResults = true;
216 return;
217 }
218}
219
220void USRGenerator::VisitFunctionDecl(const FunctionDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000221 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000222 return;
223
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000224 const unsigned StartSize = Buf.size();
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000225 VisitDeclContext(D->getDeclContext());
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000226 if (Buf.size() == StartSize)
227 GenExtSymbolContainer(D);
228
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000229 bool IsTemplate = false;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000230 if (FunctionTemplateDecl *FunTmpl = D->getDescribedFunctionTemplate()) {
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000231 IsTemplate = true;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000232 Out << "@FT@";
233 VisitTemplateParameterList(FunTmpl->getTemplateParameters());
234 } else
235 Out << "@F@";
Argyrios Kyrtzidisd5719082016-02-15 01:32:36 +0000236
237 PrintingPolicy Policy(Context->getLangOpts());
238 // Forward references can have different template argument names. Suppress the
239 // template argument names in constructors to make their USR more stable.
240 Policy.SuppressTemplateArgsInCXXConstructors = true;
241 D->getDeclName().print(Out, Policy);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000242
243 ASTContext &Ctx = *Context;
Argyrios Kyrtzidis2682f9e2016-03-04 07:17:48 +0000244 if ((!Ctx.getLangOpts().CPlusPlus || D->isExternC()) &&
245 !D->hasAttr<OverloadableAttr>())
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000246 return;
247
248 if (const TemplateArgumentList *
249 SpecArgs = D->getTemplateSpecializationArgs()) {
250 Out << '<';
251 for (unsigned I = 0, N = SpecArgs->size(); I != N; ++I) {
252 Out << '#';
253 VisitTemplateArgument(SpecArgs->get(I));
254 }
255 Out << '>';
256 }
257
258 // Mangle in type information for the arguments.
David Majnemer59f77922016-06-24 04:05:48 +0000259 for (auto PD : D->parameters()) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000260 Out << '#';
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +0000261 VisitType(PD->getType());
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000262 }
263 if (D->isVariadic())
264 Out << '.';
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000265 if (IsTemplate) {
266 // Function templates can be overloaded by return type, for example:
267 // \code
268 // template <class T> typename T::A foo() {}
269 // template <class T> typename T::B foo() {}
270 // \endcode
271 Out << '#';
272 VisitType(D->getReturnType());
273 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000274 Out << '#';
275 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
276 if (MD->isStatic())
277 Out << 'S';
Mikael Nilsson9d2872d2018-12-13 10:15:27 +0000278 // FIXME: OpenCL: Need to consider address spaces
Anastasia Stulovac61eaa52019-01-28 11:37:49 +0000279 if (unsigned quals = MD->getMethodQualifiers().getCVRUQualifiers())
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000280 Out << (char)('0' + quals);
Argyrios Kyrtzidisf5819092014-12-08 08:48:21 +0000281 switch (MD->getRefQualifier()) {
282 case RQ_None: break;
283 case RQ_LValue: Out << '&'; break;
284 case RQ_RValue: Out << "&&"; break;
285 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000286 }
287}
288
289void USRGenerator::VisitNamedDecl(const NamedDecl *D) {
290 VisitDeclContext(D->getDeclContext());
291 Out << "@";
292
293 if (EmitDeclName(D)) {
294 // The string can be empty if the declaration has no name; e.g., it is
295 // the ParmDecl with no name for declaration of a function pointer type,
296 // e.g.: void (*f)(void *);
297 // In this case, don't generate a USR.
298 IgnoreResults = true;
299 }
300}
301
302void USRGenerator::VisitVarDecl(const VarDecl *D) {
303 // VarDecls can be declared 'extern' within a function or method body,
304 // but their enclosing DeclContext is the function, not the TU. We need
305 // to check the storage class to correctly generate the USR.
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000306 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000307 return;
308
309 VisitDeclContext(D->getDeclContext());
310
Argyrios Kyrtzidis9d8ab722016-11-07 21:20:15 +0000311 if (VarTemplateDecl *VarTmpl = D->getDescribedVarTemplate()) {
312 Out << "@VT";
313 VisitTemplateParameterList(VarTmpl->getTemplateParameters());
314 } else if (const VarTemplatePartialSpecializationDecl *PartialSpec
315 = dyn_cast<VarTemplatePartialSpecializationDecl>(D)) {
316 Out << "@VP";
317 VisitTemplateParameterList(PartialSpec->getTemplateParameters());
318 }
319
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000320 // Variables always have simple names.
321 StringRef s = D->getName();
322
323 // The string can be empty if the declaration has no name; e.g., it is
324 // the ParmDecl with no name for declaration of a function pointer type, e.g.:
325 // void (*f)(void *);
326 // In this case, don't generate a USR.
327 if (s.empty())
328 IgnoreResults = true;
329 else
330 Out << '@' << s;
Argyrios Kyrtzidis9d8ab722016-11-07 21:20:15 +0000331
332 // For a template specialization, mangle the template arguments.
333 if (const VarTemplateSpecializationDecl *Spec
334 = dyn_cast<VarTemplateSpecializationDecl>(D)) {
Argyrios Kyrtzidis7d90ed02017-02-15 16:16:27 +0000335 const TemplateArgumentList &Args = Spec->getTemplateArgs();
Argyrios Kyrtzidis9d8ab722016-11-07 21:20:15 +0000336 Out << '>';
337 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
338 Out << '#';
339 VisitTemplateArgument(Args.get(I));
340 }
341 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000342}
343
Fangrui Song63a8b6c2018-10-09 01:02:56 +0000344void USRGenerator::VisitBindingDecl(const BindingDecl *D) {
345 if (isLocal(D) && GenLoc(D, /*IncludeOffset=*/true))
346 return;
347 VisitNamedDecl(D);
348}
349
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000350void USRGenerator::VisitNonTypeTemplateParmDecl(
351 const NonTypeTemplateParmDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000352 GenLoc(D, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000353}
354
355void USRGenerator::VisitTemplateTemplateParmDecl(
356 const TemplateTemplateParmDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000357 GenLoc(D, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000358}
359
360void USRGenerator::VisitNamespaceDecl(const NamespaceDecl *D) {
361 if (D->isAnonymousNamespace()) {
362 Out << "@aN";
363 return;
364 }
365
366 VisitDeclContext(D->getDeclContext());
367 if (!IgnoreResults)
368 Out << "@N@" << D->getName();
369}
370
371void USRGenerator::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
372 VisitFunctionDecl(D->getTemplatedDecl());
373}
374
375void USRGenerator::VisitClassTemplateDecl(const ClassTemplateDecl *D) {
376 VisitTagDecl(D->getTemplatedDecl());
377}
378
379void USRGenerator::VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
380 VisitDeclContext(D->getDeclContext());
381 if (!IgnoreResults)
Fangrui Song6907ce22018-07-30 19:24:48 +0000382 Out << "@NA@" << D->getName();
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000383}
384
Argyrios Kyrtzidis2b174382020-03-07 15:04:23 -0800385static const ObjCCategoryDecl *getCategoryContext(const NamedDecl *D) {
386 if (auto *CD = dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
387 return CD;
388 if (auto *ICD = dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
389 return ICD->getCategoryDecl();
390 return nullptr;
391};
392
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000393void USRGenerator::VisitObjCMethodDecl(const ObjCMethodDecl *D) {
394 const DeclContext *container = D->getDeclContext();
395 if (const ObjCProtocolDecl *pd = dyn_cast<ObjCProtocolDecl>(container)) {
396 Visit(pd);
397 }
398 else {
399 // The USR for a method declared in a class extension or category is based on
400 // the ObjCInterfaceDecl, not the ObjCCategoryDecl.
401 const ObjCInterfaceDecl *ID = D->getClassInterface();
402 if (!ID) {
403 IgnoreResults = true;
404 return;
405 }
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +0000406 auto *CD = getCategoryContext(D);
407 VisitObjCContainerDecl(ID, CD);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000408 }
409 // Ideally we would use 'GenObjCMethod', but this is such a hot path
410 // for Objective-C code that we don't want to use
411 // DeclarationName::getAsString().
412 Out << (D->isInstanceMethod() ? "(im)" : "(cm)")
413 << DeclarationName(D->getSelector());
414}
415
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +0000416void USRGenerator::VisitObjCContainerDecl(const ObjCContainerDecl *D,
417 const ObjCCategoryDecl *CatD) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000418 switch (D->getKind()) {
419 default:
420 llvm_unreachable("Invalid ObjC container.");
421 case Decl::ObjCInterface:
422 case Decl::ObjCImplementation:
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +0000423 GenObjCClass(D->getName(), GetExternalSourceContainer(D),
424 GetExternalSourceContainer(CatD));
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000425 break;
426 case Decl::ObjCCategory: {
427 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
428 const ObjCInterfaceDecl *ID = CD->getClassInterface();
429 if (!ID) {
430 // Handle invalid code where the @interface might not
431 // have been specified.
432 // FIXME: We should be able to generate this USR even if the
433 // @interface isn't available.
434 IgnoreResults = true;
435 return;
436 }
437 // Specially handle class extensions, which are anonymous categories.
438 // We want to mangle in the location to uniquely distinguish them.
439 if (CD->IsClassExtension()) {
440 Out << "objc(ext)" << ID->getName() << '@';
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000441 GenLoc(CD, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000442 }
443 else
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000444 GenObjCCategory(ID->getName(), CD->getName(),
445 GetExternalSourceContainer(ID),
446 GetExternalSourceContainer(CD));
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000447
448 break;
449 }
450 case Decl::ObjCCategoryImpl: {
451 const ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
452 const ObjCInterfaceDecl *ID = CD->getClassInterface();
453 if (!ID) {
454 // Handle invalid code where the @interface might not
455 // have been specified.
456 // FIXME: We should be able to generate this USR even if the
457 // @interface isn't available.
458 IgnoreResults = true;
459 return;
460 }
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000461 GenObjCCategory(ID->getName(), CD->getName(),
462 GetExternalSourceContainer(ID),
463 GetExternalSourceContainer(CD));
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000464 break;
465 }
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000466 case Decl::ObjCProtocol: {
467 const ObjCProtocolDecl *PD = cast<ObjCProtocolDecl>(D);
468 GenObjCProtocol(PD->getName(), GetExternalSourceContainer(PD));
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000469 break;
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000470 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000471 }
472}
473
474void USRGenerator::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
475 // The USR for a property declared in a class extension or category is based
476 // on the ObjCInterfaceDecl, not the ObjCCategoryDecl.
477 if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D))
Argyrios Kyrtzidis2b174382020-03-07 15:04:23 -0800478 VisitObjCContainerDecl(ID, getCategoryContext(D));
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000479 else
480 Visit(cast<Decl>(D->getDeclContext()));
Argyrios Kyrtzidisd9849a92016-07-15 22:18:19 +0000481 GenObjCProperty(D->getName(), D->isClassProperty());
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000482}
483
484void USRGenerator::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
485 if (ObjCPropertyDecl *PD = D->getPropertyDecl()) {
486 VisitObjCPropertyDecl(PD);
487 return;
488 }
489
490 IgnoreResults = true;
491}
492
493void USRGenerator::VisitTagDecl(const TagDecl *D) {
494 // Add the location of the tag decl to handle resolution across
495 // translation units.
Argyrios Kyrtzidis73321062016-03-04 07:17:53 +0000496 if (!isa<EnumDecl>(D) &&
497 ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000498 return;
499
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000500 GenExtSymbolContainer(D);
501
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000502 D = D->getCanonicalDecl();
503 VisitDeclContext(D->getDeclContext());
504
505 bool AlreadyStarted = false;
506 if (const CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(D)) {
507 if (ClassTemplateDecl *ClassTmpl = CXXRecord->getDescribedClassTemplate()) {
508 AlreadyStarted = true;
Fangrui Song6907ce22018-07-30 19:24:48 +0000509
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000510 switch (D->getTagKind()) {
511 case TTK_Interface:
Argyrios Kyrtzidisf66cef72014-12-08 08:48:33 +0000512 case TTK_Class:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000513 case TTK_Struct: Out << "@ST"; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000514 case TTK_Union: Out << "@UT"; break;
515 case TTK_Enum: llvm_unreachable("enum template");
516 }
517 VisitTemplateParameterList(ClassTmpl->getTemplateParameters());
518 } else if (const ClassTemplatePartialSpecializationDecl *PartialSpec
519 = dyn_cast<ClassTemplatePartialSpecializationDecl>(CXXRecord)) {
520 AlreadyStarted = true;
Fangrui Song6907ce22018-07-30 19:24:48 +0000521
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000522 switch (D->getTagKind()) {
523 case TTK_Interface:
Argyrios Kyrtzidisf66cef72014-12-08 08:48:33 +0000524 case TTK_Class:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000525 case TTK_Struct: Out << "@SP"; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000526 case TTK_Union: Out << "@UP"; break;
527 case TTK_Enum: llvm_unreachable("enum partial specialization");
Fangrui Song6907ce22018-07-30 19:24:48 +0000528 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000529 VisitTemplateParameterList(PartialSpec->getTemplateParameters());
530 }
531 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000532
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000533 if (!AlreadyStarted) {
534 switch (D->getTagKind()) {
535 case TTK_Interface:
Argyrios Kyrtzidisf66cef72014-12-08 08:48:33 +0000536 case TTK_Class:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000537 case TTK_Struct: Out << "@S"; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000538 case TTK_Union: Out << "@U"; break;
539 case TTK_Enum: Out << "@E"; break;
540 }
541 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000542
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000543 Out << '@';
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000544 assert(Buf.size() > 0);
545 const unsigned off = Buf.size() - 1;
546
547 if (EmitDeclName(D)) {
548 if (const TypedefNameDecl *TD = D->getTypedefNameForAnonDecl()) {
549 Buf[off] = 'A';
550 Out << '@' << *TD;
551 }
Argyrios Kyrtzidis80537a42014-12-08 08:48:37 +0000552 else {
553 if (D->isEmbeddedInDeclarator() && !D->isFreeStanding()) {
554 printLoc(Out, D->getLocation(), Context->getSourceManager(), true);
Argyrios Kyrtzidis73321062016-03-04 07:17:53 +0000555 } else {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000556 Buf[off] = 'a';
Argyrios Kyrtzidis73321062016-03-04 07:17:53 +0000557 if (auto *ED = dyn_cast<EnumDecl>(D)) {
558 // Distinguish USRs of anonymous enums by using their first enumerator.
559 auto enum_range = ED->enumerators();
560 if (enum_range.begin() != enum_range.end()) {
561 Out << '@' << **enum_range.begin();
562 }
563 }
564 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000565 }
Argyrios Kyrtzidis80537a42014-12-08 08:48:37 +0000566 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000567
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000568 // For a class template specialization, mangle the template arguments.
569 if (const ClassTemplateSpecializationDecl *Spec
570 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
Argyrios Kyrtzidis7d90ed02017-02-15 16:16:27 +0000571 const TemplateArgumentList &Args = Spec->getTemplateArgs();
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000572 Out << '>';
573 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
574 Out << '#';
575 VisitTemplateArgument(Args.get(I));
576 }
577 }
578}
579
580void USRGenerator::VisitTypedefDecl(const TypedefDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000581 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000582 return;
583 const DeclContext *DC = D->getDeclContext();
584 if (const NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
585 Visit(DCN);
586 Out << "@T@";
587 Out << D->getName();
588}
589
590void USRGenerator::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000591 GenLoc(D, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000592}
593
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000594void USRGenerator::GenExtSymbolContainer(const NamedDecl *D) {
595 StringRef Container = GetExternalSourceContainer(D);
596 if (!Container.empty())
597 Out << "@M@" << Container;
598}
599
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000600bool USRGenerator::GenLoc(const Decl *D, bool IncludeOffset) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000601 if (generatedLoc)
602 return IgnoreResults;
603 generatedLoc = true;
Dmitri Gribenko237769e2014-03-28 22:21:26 +0000604
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000605 // Guard against null declarations in invalid code.
606 if (!D) {
607 IgnoreResults = true;
608 return true;
609 }
610
611 // Use the location of canonical decl.
612 D = D->getCanonicalDecl();
613
Dmitri Gribenko237769e2014-03-28 22:21:26 +0000614 IgnoreResults =
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000615 IgnoreResults || printLoc(Out, D->getBeginLoc(),
Dmitri Gribenko237769e2014-03-28 22:21:26 +0000616 Context->getSourceManager(), IncludeOffset);
617
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000618 return IgnoreResults;
619}
620
Ben Langmuirfd6e39c2017-08-16 23:12:21 +0000621static void printQualifier(llvm::raw_ostream &Out, ASTContext &Ctx, NestedNameSpecifier *NNS) {
622 // FIXME: Encode the qualifier, don't just print it.
623 PrintingPolicy PO(Ctx.getLangOpts());
624 PO.SuppressTagKeyword = true;
625 PO.SuppressUnwrittenScope = true;
626 PO.ConstantArraySizeAsWritten = false;
627 PO.AnonymousTagLocations = false;
628 NNS->print(Out, PO);
629}
630
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000631void USRGenerator::VisitType(QualType T) {
632 // This method mangles in USR information for types. It can possibly
633 // just reuse the naming-mangling logic used by codegen, although the
634 // requirements for USRs might not be the same.
635 ASTContext &Ctx = *Context;
636
637 do {
638 T = Ctx.getCanonicalType(T);
639 Qualifiers Q = T.getQualifiers();
640 unsigned qVal = 0;
641 if (Q.hasConst())
642 qVal |= 0x1;
643 if (Q.hasVolatile())
644 qVal |= 0x2;
645 if (Q.hasRestrict())
646 qVal |= 0x4;
647 if(qVal)
648 Out << ((char) ('0' + qVal));
649
650 // Mangle in ObjC GC qualifiers?
651
652 if (const PackExpansionType *Expansion = T->getAs<PackExpansionType>()) {
653 Out << 'P';
654 T = Expansion->getPattern();
655 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000656
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000657 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
658 unsigned char c = '\0';
659 switch (BT->getKind()) {
660 case BuiltinType::Void:
661 c = 'v'; break;
662 case BuiltinType::Bool:
663 c = 'b'; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000664 case BuiltinType::UChar:
665 c = 'c'; break;
Richard Smith3a8244d2018-05-01 05:02:45 +0000666 case BuiltinType::Char8:
667 c = 'u'; break; // FIXME: Check this doesn't collide
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000668 case BuiltinType::Char16:
669 c = 'q'; break;
670 case BuiltinType::Char32:
671 c = 'w'; break;
672 case BuiltinType::UShort:
673 c = 's'; break;
674 case BuiltinType::UInt:
675 c = 'i'; break;
676 case BuiltinType::ULong:
677 c = 'l'; break;
678 case BuiltinType::ULongLong:
679 c = 'k'; break;
680 case BuiltinType::UInt128:
681 c = 'j'; break;
Argyrios Kyrtzidis56af7e62014-12-08 09:09:05 +0000682 case BuiltinType::Char_U:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000683 case BuiltinType::Char_S:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000684 c = 'C'; break;
Argyrios Kyrtzidisca044542014-12-08 08:48:17 +0000685 case BuiltinType::SChar:
686 c = 'r'; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000687 case BuiltinType::WChar_S:
688 case BuiltinType::WChar_U:
689 c = 'W'; break;
690 case BuiltinType::Short:
691 c = 'S'; break;
692 case BuiltinType::Int:
693 c = 'I'; break;
694 case BuiltinType::Long:
695 c = 'L'; break;
696 case BuiltinType::LongLong:
697 c = 'K'; break;
698 case BuiltinType::Int128:
699 c = 'J'; break;
Sjoerd Meijercc623ad2017-09-08 15:15:00 +0000700 case BuiltinType::Float16:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000701 case BuiltinType::Half:
702 c = 'h'; break;
703 case BuiltinType::Float:
704 c = 'f'; break;
705 case BuiltinType::Double:
706 c = 'd'; break;
707 case BuiltinType::LongDouble:
708 c = 'D'; break;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000709 case BuiltinType::Float128:
710 c = 'Q'; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000711 case BuiltinType::NullPtr:
712 c = 'n'; break;
713#define BUILTIN_TYPE(Id, SingletonId)
714#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
715#include "clang/AST/BuiltinTypes.def"
716 case BuiltinType::Dependent:
Alexey Bader954ba212016-04-08 13:40:33 +0000717#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
718 case BuiltinType::Id:
Alexey Baderb62f1442016-04-13 08:33:41 +0000719#include "clang/Basic/OpenCLImageTypes.def"
Andrew Savonichev3fee3512018-11-08 11:25:41 +0000720#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
721 case BuiltinType::Id:
722#include "clang/Basic/OpenCLExtensionTypes.def"
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000723 case BuiltinType::OCLEvent:
Alexey Bader9c8453f2015-09-15 11:18:52 +0000724 case BuiltinType::OCLClkEvent:
725 case BuiltinType::OCLQueue:
Alexey Bader9c8453f2015-09-15 11:18:52 +0000726 case BuiltinType::OCLReserveID:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000727 case BuiltinType::OCLSampler:
Richard Sandifordeb485fb2019-08-09 08:52:54 +0000728#define SVE_TYPE(Name, Id, SingletonId) \
729 case BuiltinType::Id:
730#include "clang/Basic/AArch64SVEACLETypes.def"
Leonard Chanf921d852018-06-04 16:07:52 +0000731 case BuiltinType::ShortAccum:
732 case BuiltinType::Accum:
733 case BuiltinType::LongAccum:
734 case BuiltinType::UShortAccum:
735 case BuiltinType::UAccum:
736 case BuiltinType::ULongAccum:
Leonard Chanab80f3c2018-06-14 14:53:51 +0000737 case BuiltinType::ShortFract:
738 case BuiltinType::Fract:
739 case BuiltinType::LongFract:
740 case BuiltinType::UShortFract:
741 case BuiltinType::UFract:
742 case BuiltinType::ULongFract:
743 case BuiltinType::SatShortAccum:
744 case BuiltinType::SatAccum:
745 case BuiltinType::SatLongAccum:
746 case BuiltinType::SatUShortAccum:
747 case BuiltinType::SatUAccum:
748 case BuiltinType::SatULongAccum:
749 case BuiltinType::SatShortFract:
750 case BuiltinType::SatFract:
751 case BuiltinType::SatLongFract:
752 case BuiltinType::SatUShortFract:
753 case BuiltinType::SatUFract:
754 case BuiltinType::SatULongFract:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000755 IgnoreResults = true;
756 return;
757 case BuiltinType::ObjCId:
758 c = 'o'; break;
759 case BuiltinType::ObjCClass:
760 c = 'O'; break;
761 case BuiltinType::ObjCSel:
762 c = 'e'; break;
763 }
764 Out << c;
765 return;
766 }
767
768 // If we have already seen this (non-built-in) type, use a substitution
769 // encoding.
770 llvm::DenseMap<const Type *, unsigned>::iterator Substitution
771 = TypeSubstitutions.find(T.getTypePtr());
772 if (Substitution != TypeSubstitutions.end()) {
773 Out << 'S' << Substitution->second << '_';
774 return;
775 } else {
776 // Record this as a substitution.
777 unsigned Number = TypeSubstitutions.size();
778 TypeSubstitutions[T.getTypePtr()] = Number;
779 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000780
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000781 if (const PointerType *PT = T->getAs<PointerType>()) {
782 Out << '*';
783 T = PT->getPointeeType();
784 continue;
785 }
Argyrios Kyrtzidis9c998672016-03-04 07:17:43 +0000786 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
787 Out << '*';
788 T = OPT->getPointeeType();
789 continue;
790 }
Argyrios Kyrtzidis0e387dc2014-12-08 08:48:27 +0000791 if (const RValueReferenceType *RT = T->getAs<RValueReferenceType>()) {
792 Out << "&&";
793 T = RT->getPointeeType();
794 continue;
795 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000796 if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
797 Out << '&';
798 T = RT->getPointeeType();
799 continue;
800 }
801 if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) {
802 Out << 'F';
Alp Toker314cc812014-01-25 16:55:45 +0000803 VisitType(FT->getReturnType());
Jan Korouse6a02422017-10-10 00:35:16 +0000804 Out << '(';
805 for (const auto &I : FT->param_types()) {
806 Out << '#';
Aaron Ballman40bd0aa2014-03-17 15:23:01 +0000807 VisitType(I);
Jan Korouse6a02422017-10-10 00:35:16 +0000808 }
809 Out << ')';
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000810 if (FT->isVariadic())
811 Out << '.';
812 return;
813 }
814 if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) {
815 Out << 'B';
816 T = BT->getPointeeType();
817 continue;
818 }
819 if (const ComplexType *CT = T->getAs<ComplexType>()) {
820 Out << '<';
821 T = CT->getElementType();
822 continue;
823 }
824 if (const TagType *TT = T->getAs<TagType>()) {
825 Out << '$';
826 VisitTagDecl(TT->getDecl());
827 return;
828 }
Argyrios Kyrtzidis9c998672016-03-04 07:17:43 +0000829 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
830 Out << '$';
831 VisitObjCInterfaceDecl(OIT->getDecl());
832 return;
833 }
834 if (const ObjCObjectType *OIT = T->getAs<ObjCObjectType>()) {
835 Out << 'Q';
836 VisitType(OIT->getBaseType());
837 for (auto *Prot : OIT->getProtocols())
838 VisitObjCProtocolDecl(Prot);
839 return;
840 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000841 if (const TemplateTypeParmType *TTP = T->getAs<TemplateTypeParmType>()) {
842 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
843 return;
844 }
845 if (const TemplateSpecializationType *Spec
846 = T->getAs<TemplateSpecializationType>()) {
847 Out << '>';
848 VisitTemplateName(Spec->getTemplateName());
849 Out << Spec->getNumArgs();
850 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
851 VisitTemplateArgument(Spec->getArg(I));
852 return;
853 }
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000854 if (const DependentNameType *DNT = T->getAs<DependentNameType>()) {
855 Out << '^';
Ben Langmuirfd6e39c2017-08-16 23:12:21 +0000856 printQualifier(Out, Ctx, DNT->getQualifier());
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000857 Out << ':' << DNT->getIdentifier()->getName();
858 return;
859 }
Argyrios Kyrtzidis1d5e5422014-12-08 08:48:43 +0000860 if (const InjectedClassNameType *InjT = T->getAs<InjectedClassNameType>()) {
861 T = InjT->getInjectedSpecializationType();
862 continue;
863 }
Alex Lorenz45c423b2017-04-28 09:46:36 +0000864 if (const auto *VT = T->getAs<VectorType>()) {
865 Out << (T->isExtVectorType() ? ']' : '[');
866 Out << VT->getNumElements();
867 T = VT->getElementType();
868 continue;
869 }
Jan Korous663ba152017-10-09 19:51:33 +0000870 if (const auto *const AT = dyn_cast<ArrayType>(T)) {
871 Out << '{';
872 switch (AT->getSizeModifier()) {
873 case ArrayType::Static:
874 Out << 's';
875 break;
876 case ArrayType::Star:
877 Out << '*';
878 break;
879 case ArrayType::Normal:
880 Out << 'n';
881 break;
882 }
883 if (const auto *const CAT = dyn_cast<ConstantArrayType>(T))
884 Out << CAT->getSize();
885
886 T = AT->getElementType();
887 continue;
888 }
Alex Lorenz45c423b2017-04-28 09:46:36 +0000889
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000890 // Unhandled type.
891 Out << ' ';
892 break;
893 } while (true);
894}
895
896void USRGenerator::VisitTemplateParameterList(
897 const TemplateParameterList *Params) {
898 if (!Params)
899 return;
900 Out << '>' << Params->size();
901 for (TemplateParameterList::const_iterator P = Params->begin(),
902 PEnd = Params->end();
903 P != PEnd; ++P) {
904 Out << '#';
905 if (isa<TemplateTypeParmDecl>(*P)) {
906 if (cast<TemplateTypeParmDecl>(*P)->isParameterPack())
907 Out<< 'p';
908 Out << 'T';
909 continue;
910 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000911
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000912 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
913 if (NTTP->isParameterPack())
914 Out << 'p';
915 Out << 'N';
916 VisitType(NTTP->getType());
917 continue;
918 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000919
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000920 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
921 if (TTP->isParameterPack())
922 Out << 'p';
923 Out << 't';
924 VisitTemplateParameterList(TTP->getTemplateParameters());
925 }
926}
927
928void USRGenerator::VisitTemplateName(TemplateName Name) {
929 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
930 if (TemplateTemplateParmDecl *TTP
931 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
932 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
933 return;
934 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000935
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000936 Visit(Template);
937 return;
938 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000939
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000940 // FIXME: Visit dependent template names.
941}
942
943void USRGenerator::VisitTemplateArgument(const TemplateArgument &Arg) {
944 switch (Arg.getKind()) {
945 case TemplateArgument::Null:
946 break;
947
948 case TemplateArgument::Declaration:
949 Visit(Arg.getAsDecl());
950 break;
951
952 case TemplateArgument::NullPtr:
953 break;
954
955 case TemplateArgument::TemplateExpansion:
956 Out << 'P'; // pack expansion of...
Reid Kleckner4dc0b1a2018-11-01 19:54:45 +0000957 LLVM_FALLTHROUGH;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000958 case TemplateArgument::Template:
959 VisitTemplateName(Arg.getAsTemplateOrTemplatePattern());
960 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000961
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000962 case TemplateArgument::Expression:
963 // FIXME: Visit expressions.
964 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000965
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000966 case TemplateArgument::Pack:
967 Out << 'p' << Arg.pack_size();
Aaron Ballman2a89e852014-07-15 21:32:31 +0000968 for (const auto &P : Arg.pack_elements())
969 VisitTemplateArgument(P);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000970 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000971
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000972 case TemplateArgument::Type:
973 VisitType(Arg.getAsType());
974 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000975
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000976 case TemplateArgument::Integral:
977 Out << 'V';
978 VisitType(Arg.getIntegralType());
979 Out << Arg.getAsIntegral();
980 break;
981 }
982}
983
Ben Langmuirfd6e39c2017-08-16 23:12:21 +0000984void USRGenerator::VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D) {
985 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
986 return;
987 VisitDeclContext(D->getDeclContext());
988 Out << "@UUV@";
989 printQualifier(Out, D->getASTContext(), D->getQualifier());
990 EmitDeclName(D);
991}
992
993void USRGenerator::VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D) {
994 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
995 return;
996 VisitDeclContext(D->getDeclContext());
997 Out << "@UUT@";
998 printQualifier(Out, D->getASTContext(), D->getQualifier());
999 Out << D->getName(); // Simple name.
1000}
1001
1002
1003
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001004//===----------------------------------------------------------------------===//
1005// USR generation functions.
1006//===----------------------------------------------------------------------===//
1007
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +00001008static void combineClassAndCategoryExtContainers(StringRef ClsSymDefinedIn,
1009 StringRef CatSymDefinedIn,
1010 raw_ostream &OS) {
1011 if (ClsSymDefinedIn.empty() && CatSymDefinedIn.empty())
1012 return;
1013 if (CatSymDefinedIn.empty()) {
1014 OS << "@M@" << ClsSymDefinedIn << '@';
1015 return;
1016 }
1017 OS << "@CM@" << CatSymDefinedIn << '@';
1018 if (ClsSymDefinedIn != CatSymDefinedIn) {
1019 OS << ClsSymDefinedIn << '@';
1020 }
1021}
1022
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +00001023void clang::index::generateUSRForObjCClass(StringRef Cls, raw_ostream &OS,
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +00001024 StringRef ExtSymDefinedIn,
1025 StringRef CategoryContextExtSymbolDefinedIn) {
1026 combineClassAndCategoryExtContainers(ExtSymDefinedIn,
1027 CategoryContextExtSymbolDefinedIn, OS);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001028 OS << "objc(cs)" << Cls;
1029}
1030
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +00001031void clang::index::generateUSRForObjCCategory(StringRef Cls, StringRef Cat,
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +00001032 raw_ostream &OS,
1033 StringRef ClsSymDefinedIn,
1034 StringRef CatSymDefinedIn) {
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +00001035 combineClassAndCategoryExtContainers(ClsSymDefinedIn, CatSymDefinedIn, OS);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001036 OS << "objc(cy)" << Cls << '@' << Cat;
1037}
1038
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +00001039void clang::index::generateUSRForObjCIvar(StringRef Ivar, raw_ostream &OS) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001040 OS << '@' << Ivar;
1041}
1042
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +00001043void clang::index::generateUSRForObjCMethod(StringRef Sel,
1044 bool IsInstanceMethod,
1045 raw_ostream &OS) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001046 OS << (IsInstanceMethod ? "(im)" : "(cm)") << Sel;
1047}
1048
Argyrios Kyrtzidisd9849a92016-07-15 22:18:19 +00001049void clang::index::generateUSRForObjCProperty(StringRef Prop, bool isClassProp,
1050 raw_ostream &OS) {
1051 OS << (isClassProp ? "(cpy)" : "(py)") << Prop;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001052}
1053
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +00001054void clang::index::generateUSRForObjCProtocol(StringRef Prot, raw_ostream &OS,
1055 StringRef ExtSymDefinedIn) {
1056 if (!ExtSymDefinedIn.empty())
1057 OS << "@M@" << ExtSymDefinedIn << '@';
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001058 OS << "objc(pl)" << Prot;
1059}
1060
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +00001061void clang::index::generateUSRForGlobalEnum(StringRef EnumName, raw_ostream &OS,
1062 StringRef ExtSymDefinedIn) {
1063 if (!ExtSymDefinedIn.empty())
1064 OS << "@M@" << ExtSymDefinedIn;
1065 OS << "@E@" << EnumName;
1066}
1067
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +00001068void clang::index::generateUSRForEnumConstant(StringRef EnumConstantName,
1069 raw_ostream &OS) {
1070 OS << '@' << EnumConstantName;
1071}
1072
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +00001073bool clang::index::generateUSRForDecl(const Decl *D,
1074 SmallVectorImpl<char> &Buf) {
Argyrios Kyrtzidisf12918d2016-11-02 23:42:33 +00001075 if (!D)
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001076 return true;
Argyrios Kyrtzidisf12918d2016-11-02 23:42:33 +00001077 // We don't ignore decls with invalid source locations. Implicit decls, like
1078 // C++'s operator new function, can have invalid locations but it is fine to
1079 // create USRs that can identify them.
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001080
1081 USRGenerator UG(&D->getASTContext(), Buf);
1082 UG.Visit(D);
1083 return UG.ignoreResults();
1084}
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001085
Richard Smith66a81862015-05-04 02:25:31 +00001086bool clang::index::generateUSRForMacro(const MacroDefinitionRecord *MD,
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001087 const SourceManager &SM,
1088 SmallVectorImpl<char> &Buf) {
Argyrios Kyrtzidis5b7a09a2017-02-02 16:13:10 +00001089 if (!MD)
1090 return true;
1091 return generateUSRForMacro(MD->getName()->getName(), MD->getLocation(),
1092 SM, Buf);
1093
1094}
1095
1096bool clang::index::generateUSRForMacro(StringRef MacroName, SourceLocation Loc,
1097 const SourceManager &SM,
1098 SmallVectorImpl<char> &Buf) {
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001099 // Don't generate USRs for things with invalid locations.
Argyrios Kyrtzidis5b7a09a2017-02-02 16:13:10 +00001100 if (MacroName.empty() || Loc.isInvalid())
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001101 return true;
1102
1103 llvm::raw_svector_ostream Out(Buf);
1104
1105 // Assume that system headers are sane. Don't put source location
1106 // information into the USR if the macro comes from a system header.
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001107 bool ShouldGenerateLocation = !SM.isInSystemHeader(Loc);
1108
1109 Out << getUSRSpacePrefix();
1110 if (ShouldGenerateLocation)
1111 printLoc(Out, Loc, SM, /*IncludeOffset=*/true);
1112 Out << "@macro@";
Argyrios Kyrtzidis5b7a09a2017-02-02 16:13:10 +00001113 Out << MacroName;
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001114 return false;
1115}
Argyrios Kyrtzidis32e5d862018-09-18 15:02:56 +00001116
Ilya Biryukova6224842018-11-26 15:24:48 +00001117bool clang::index::generateUSRForType(QualType T, ASTContext &Ctx,
1118 SmallVectorImpl<char> &Buf) {
1119 if (T.isNull())
1120 return true;
1121 T = T.getCanonicalType();
1122
1123 USRGenerator UG(&Ctx, Buf);
1124 UG.VisitType(T);
1125 return UG.ignoreResults();
1126}
1127
Argyrios Kyrtzidis32e5d862018-09-18 15:02:56 +00001128bool clang::index::generateFullUSRForModule(const Module *Mod,
1129 raw_ostream &OS) {
1130 if (!Mod->Parent)
1131 return generateFullUSRForTopLevelModuleName(Mod->Name, OS);
1132 if (generateFullUSRForModule(Mod->Parent, OS))
1133 return true;
1134 return generateUSRFragmentForModule(Mod, OS);
1135}
1136
1137bool clang::index::generateFullUSRForTopLevelModuleName(StringRef ModName,
1138 raw_ostream &OS) {
1139 OS << getUSRSpacePrefix();
1140 return generateUSRFragmentForModuleName(ModName, OS);
1141}
1142
1143bool clang::index::generateUSRFragmentForModule(const Module *Mod,
1144 raw_ostream &OS) {
1145 return generateUSRFragmentForModuleName(Mod->Name, OS);
1146}
1147
1148bool clang::index::generateUSRFragmentForModuleName(StringRef ModName,
1149 raw_ostream &OS) {
1150 OS << "@M@" << ModName;
1151 return false;
1152}