blob: e503f1ef165c8fb75a484f8976eea4b06e39717e [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"
Reid Klecknere08464f2020-02-29 09:10:42 -080014#include "clang/Basic/FileManager.h"
Dmitri Gribenko237769e2014-03-28 22:21:26 +000015#include "clang/Lex/PreprocessingRecord.h"
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000016#include "llvm/Support/Path.h"
17#include "llvm/Support/raw_ostream.h"
18
19using namespace clang;
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +000020using namespace clang::index;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000021
22//===----------------------------------------------------------------------===//
23// USR generation.
24//===----------------------------------------------------------------------===//
25
Dmitri Gribenko237769e2014-03-28 22:21:26 +000026/// \returns true on error.
27static bool printLoc(llvm::raw_ostream &OS, SourceLocation Loc,
28 const SourceManager &SM, bool IncludeOffset) {
29 if (Loc.isInvalid()) {
30 return true;
31 }
32 Loc = SM.getExpansionLoc(Loc);
33 const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(Loc);
34 const FileEntry *FE = SM.getFileEntryForID(Decomposed.first);
35 if (FE) {
36 OS << llvm::sys::path::filename(FE->getName());
37 } else {
38 // This case really isn't interesting.
39 return true;
40 }
41 if (IncludeOffset) {
42 // Use the offest into the FileID to represent the location. Using
43 // a line/column can cause us to look back at the original source file,
44 // which is expensive.
45 OS << '@' << Decomposed.second;
46 }
47 return false;
48}
49
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +000050static StringRef GetExternalSourceContainer(const NamedDecl *D) {
51 if (!D)
52 return StringRef();
Argyrios Kyrtzidis11d70482017-05-20 04:11:33 +000053 if (auto *attr = D->getExternalSourceSymbolAttr()) {
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +000054 return attr->getDefinedIn();
55 }
56 return StringRef();
57}
58
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000059namespace {
60class USRGenerator : public ConstDeclVisitor<USRGenerator> {
61 SmallVectorImpl<char> &Buf;
62 llvm::raw_svector_ostream Out;
63 bool IgnoreResults;
64 ASTContext *Context;
65 bool generatedLoc;
Fangrui Song6907ce22018-07-30 19:24:48 +000066
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000067 llvm::DenseMap<const Type *, unsigned> TypeSubstitutions;
Fangrui Song6907ce22018-07-30 19:24:48 +000068
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000069public:
70 explicit USRGenerator(ASTContext *Ctx, SmallVectorImpl<char> &Buf)
71 : Buf(Buf),
72 Out(Buf),
73 IgnoreResults(false),
74 Context(Ctx),
75 generatedLoc(false)
76 {
77 // Add the USR space prefix.
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +000078 Out << getUSRSpacePrefix();
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000079 }
80
81 bool ignoreResults() const { return IgnoreResults; }
82
83 // Visitation methods from generating USRs from AST elements.
84 void VisitDeclContext(const DeclContext *D);
85 void VisitFieldDecl(const FieldDecl *D);
86 void VisitFunctionDecl(const FunctionDecl *D);
87 void VisitNamedDecl(const NamedDecl *D);
88 void VisitNamespaceDecl(const NamespaceDecl *D);
89 void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D);
90 void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D);
91 void VisitClassTemplateDecl(const ClassTemplateDecl *D);
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +000092 void VisitObjCContainerDecl(const ObjCContainerDecl *CD,
93 const ObjCCategoryDecl *CatD = nullptr);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000094 void VisitObjCMethodDecl(const ObjCMethodDecl *MD);
95 void VisitObjCPropertyDecl(const ObjCPropertyDecl *D);
96 void VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D);
97 void VisitTagDecl(const TagDecl *D);
98 void VisitTypedefDecl(const TypedefDecl *D);
99 void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D);
100 void VisitVarDecl(const VarDecl *D);
Fangrui Song63a8b6c2018-10-09 01:02:56 +0000101 void VisitBindingDecl(const BindingDecl *D);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000102 void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D);
103 void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D);
Ben Langmuirfd6e39c2017-08-16 23:12:21 +0000104 void VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D);
105 void VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D);
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000106
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000107 void VisitLinkageSpecDecl(const LinkageSpecDecl *D) {
Sam McCall2e50ae62018-02-02 14:13:37 +0000108 IgnoreResults = true; // No USRs for linkage specs themselves.
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000109 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000110
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000111 void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
112 IgnoreResults = true;
113 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000114
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000115 void VisitUsingDecl(const UsingDecl *D) {
Kadir Cetinkayaa87ada02019-02-26 14:23:12 +0000116 VisitDeclContext(D->getDeclContext());
117 Out << "@UD@";
118
119 bool EmittedDeclName = !EmitDeclName(D);
120 assert(EmittedDeclName && "EmitDeclName can not fail for UsingDecls");
121 (void)EmittedDeclName;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000122 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000123
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000124 bool ShouldGenerateLocation(const NamedDecl *D);
125
126 bool isLocal(const NamedDecl *D) {
Craig Topper236bde32014-05-26 06:21:51 +0000127 return D->getParentFunctionOrMethod() != nullptr;
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000128 }
129
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000130 void GenExtSymbolContainer(const NamedDecl *D);
131
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000132 /// Generate the string component containing the location of the
133 /// declaration.
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000134 bool GenLoc(const Decl *D, bool IncludeOffset);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000135
136 /// String generation methods used both by the visitation methods
137 /// and from other clients that want to directly generate USRs. These
138 /// methods do not construct complete USRs (which incorporate the parents
139 /// of an AST element), but only the fragments concerning the AST element
140 /// itself.
141
142 /// Generate a USR for an Objective-C class.
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +0000143 void GenObjCClass(StringRef cls, StringRef ExtSymDefinedIn,
144 StringRef CategoryContextExtSymbolDefinedIn) {
145 generateUSRForObjCClass(cls, Out, ExtSymDefinedIn,
146 CategoryContextExtSymbolDefinedIn);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000147 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000148
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000149 /// Generate a USR for an Objective-C class category.
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000150 void GenObjCCategory(StringRef cls, StringRef cat,
151 StringRef clsExt, StringRef catExt) {
152 generateUSRForObjCCategory(cls, cat, Out, clsExt, catExt);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000153 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000154
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000155 /// Generate a USR fragment for an Objective-C property.
Argyrios Kyrtzidisd9849a92016-07-15 22:18:19 +0000156 void GenObjCProperty(StringRef prop, bool isClassProp) {
157 generateUSRForObjCProperty(prop, isClassProp, Out);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000158 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000159
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000160 /// Generate a USR for an Objective-C protocol.
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000161 void GenObjCProtocol(StringRef prot, StringRef ext) {
162 generateUSRForObjCProtocol(prot, Out, ext);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000163 }
164
165 void VisitType(QualType T);
166 void VisitTemplateParameterList(const TemplateParameterList *Params);
167 void VisitTemplateName(TemplateName Name);
168 void VisitTemplateArgument(const TemplateArgument &Arg);
Fangrui Song6907ce22018-07-30 19:24:48 +0000169
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000170 /// Emit a Decl's name using NamedDecl::printName() and return true if
171 /// the decl had no name.
172 bool EmitDeclName(const NamedDecl *D);
173};
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000174} // end anonymous namespace
175
176//===----------------------------------------------------------------------===//
177// Generating USRs from ASTS.
178//===----------------------------------------------------------------------===//
179
180bool USRGenerator::EmitDeclName(const NamedDecl *D) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000181 const unsigned startSize = Buf.size();
182 D->printName(Out);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000183 const unsigned endSize = Buf.size();
184 return startSize == endSize;
185}
186
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000187bool USRGenerator::ShouldGenerateLocation(const NamedDecl *D) {
188 if (D->isExternallyVisible())
189 return false;
190 if (D->getParentFunctionOrMethod())
191 return true;
Argyrios Kyrtzidisf12918d2016-11-02 23:42:33 +0000192 SourceLocation Loc = D->getLocation();
193 if (Loc.isInvalid())
194 return false;
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000195 const SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisf12918d2016-11-02 23:42:33 +0000196 return !SM.isInSystemHeader(Loc);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000197}
198
199void USRGenerator::VisitDeclContext(const DeclContext *DC) {
200 if (const NamedDecl *D = dyn_cast<NamedDecl>(DC))
201 Visit(D);
Sam McCall2e50ae62018-02-02 14:13:37 +0000202 else if (isa<LinkageSpecDecl>(DC)) // Linkage specs are transparent in USRs.
203 VisitDeclContext(DC->getParent());
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000204}
205
206void USRGenerator::VisitFieldDecl(const FieldDecl *D) {
207 // The USR for an ivar declared in a class extension is based on the
208 // ObjCInterfaceDecl, not the ObjCCategoryDecl.
209 if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D))
210 Visit(ID);
211 else
212 VisitDeclContext(D->getDeclContext());
213 Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@");
214 if (EmitDeclName(D)) {
215 // Bit fields can be anonymous.
216 IgnoreResults = true;
217 return;
218 }
219}
220
221void USRGenerator::VisitFunctionDecl(const FunctionDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000222 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000223 return;
224
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000225 const unsigned StartSize = Buf.size();
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000226 VisitDeclContext(D->getDeclContext());
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000227 if (Buf.size() == StartSize)
228 GenExtSymbolContainer(D);
229
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000230 bool IsTemplate = false;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000231 if (FunctionTemplateDecl *FunTmpl = D->getDescribedFunctionTemplate()) {
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000232 IsTemplate = true;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000233 Out << "@FT@";
234 VisitTemplateParameterList(FunTmpl->getTemplateParameters());
235 } else
236 Out << "@F@";
Argyrios Kyrtzidisd5719082016-02-15 01:32:36 +0000237
238 PrintingPolicy Policy(Context->getLangOpts());
239 // Forward references can have different template argument names. Suppress the
240 // template argument names in constructors to make their USR more stable.
241 Policy.SuppressTemplateArgsInCXXConstructors = true;
242 D->getDeclName().print(Out, Policy);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000243
244 ASTContext &Ctx = *Context;
Argyrios Kyrtzidis2682f9e2016-03-04 07:17:48 +0000245 if ((!Ctx.getLangOpts().CPlusPlus || D->isExternC()) &&
246 !D->hasAttr<OverloadableAttr>())
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000247 return;
248
249 if (const TemplateArgumentList *
250 SpecArgs = D->getTemplateSpecializationArgs()) {
251 Out << '<';
252 for (unsigned I = 0, N = SpecArgs->size(); I != N; ++I) {
253 Out << '#';
254 VisitTemplateArgument(SpecArgs->get(I));
255 }
256 Out << '>';
257 }
258
259 // Mangle in type information for the arguments.
David Majnemer59f77922016-06-24 04:05:48 +0000260 for (auto PD : D->parameters()) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000261 Out << '#';
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +0000262 VisitType(PD->getType());
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000263 }
264 if (D->isVariadic())
265 Out << '.';
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000266 if (IsTemplate) {
267 // Function templates can be overloaded by return type, for example:
268 // \code
269 // template <class T> typename T::A foo() {}
270 // template <class T> typename T::B foo() {}
271 // \endcode
272 Out << '#';
273 VisitType(D->getReturnType());
274 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000275 Out << '#';
276 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
277 if (MD->isStatic())
278 Out << 'S';
Mikael Nilsson9d2872d2018-12-13 10:15:27 +0000279 // FIXME: OpenCL: Need to consider address spaces
Anastasia Stulovac61eaa52019-01-28 11:37:49 +0000280 if (unsigned quals = MD->getMethodQualifiers().getCVRUQualifiers())
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000281 Out << (char)('0' + quals);
Argyrios Kyrtzidisf5819092014-12-08 08:48:21 +0000282 switch (MD->getRefQualifier()) {
283 case RQ_None: break;
284 case RQ_LValue: Out << '&'; break;
285 case RQ_RValue: Out << "&&"; break;
286 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000287 }
288}
289
290void USRGenerator::VisitNamedDecl(const NamedDecl *D) {
291 VisitDeclContext(D->getDeclContext());
292 Out << "@";
293
294 if (EmitDeclName(D)) {
295 // The string can be empty if the declaration has no name; e.g., it is
296 // the ParmDecl with no name for declaration of a function pointer type,
297 // e.g.: void (*f)(void *);
298 // In this case, don't generate a USR.
299 IgnoreResults = true;
300 }
301}
302
303void USRGenerator::VisitVarDecl(const VarDecl *D) {
304 // VarDecls can be declared 'extern' within a function or method body,
305 // but their enclosing DeclContext is the function, not the TU. We need
306 // to check the storage class to correctly generate the USR.
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000307 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000308 return;
309
310 VisitDeclContext(D->getDeclContext());
311
Argyrios Kyrtzidis9d8ab722016-11-07 21:20:15 +0000312 if (VarTemplateDecl *VarTmpl = D->getDescribedVarTemplate()) {
313 Out << "@VT";
314 VisitTemplateParameterList(VarTmpl->getTemplateParameters());
315 } else if (const VarTemplatePartialSpecializationDecl *PartialSpec
316 = dyn_cast<VarTemplatePartialSpecializationDecl>(D)) {
317 Out << "@VP";
318 VisitTemplateParameterList(PartialSpec->getTemplateParameters());
319 }
320
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000321 // Variables always have simple names.
322 StringRef s = D->getName();
323
324 // The string can be empty if the declaration has no name; e.g., it is
325 // the ParmDecl with no name for declaration of a function pointer type, e.g.:
326 // void (*f)(void *);
327 // In this case, don't generate a USR.
328 if (s.empty())
329 IgnoreResults = true;
330 else
331 Out << '@' << s;
Argyrios Kyrtzidis9d8ab722016-11-07 21:20:15 +0000332
333 // For a template specialization, mangle the template arguments.
334 if (const VarTemplateSpecializationDecl *Spec
335 = dyn_cast<VarTemplateSpecializationDecl>(D)) {
Argyrios Kyrtzidis7d90ed02017-02-15 16:16:27 +0000336 const TemplateArgumentList &Args = Spec->getTemplateArgs();
Argyrios Kyrtzidis9d8ab722016-11-07 21:20:15 +0000337 Out << '>';
338 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
339 Out << '#';
340 VisitTemplateArgument(Args.get(I));
341 }
342 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000343}
344
Fangrui Song63a8b6c2018-10-09 01:02:56 +0000345void USRGenerator::VisitBindingDecl(const BindingDecl *D) {
346 if (isLocal(D) && GenLoc(D, /*IncludeOffset=*/true))
347 return;
348 VisitNamedDecl(D);
349}
350
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000351void USRGenerator::VisitNonTypeTemplateParmDecl(
352 const NonTypeTemplateParmDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000353 GenLoc(D, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000354}
355
356void USRGenerator::VisitTemplateTemplateParmDecl(
357 const TemplateTemplateParmDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000358 GenLoc(D, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000359}
360
361void USRGenerator::VisitNamespaceDecl(const NamespaceDecl *D) {
362 if (D->isAnonymousNamespace()) {
363 Out << "@aN";
364 return;
365 }
366
367 VisitDeclContext(D->getDeclContext());
368 if (!IgnoreResults)
369 Out << "@N@" << D->getName();
370}
371
372void USRGenerator::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
373 VisitFunctionDecl(D->getTemplatedDecl());
374}
375
376void USRGenerator::VisitClassTemplateDecl(const ClassTemplateDecl *D) {
377 VisitTagDecl(D->getTemplatedDecl());
378}
379
380void USRGenerator::VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
381 VisitDeclContext(D->getDeclContext());
382 if (!IgnoreResults)
Fangrui Song6907ce22018-07-30 19:24:48 +0000383 Out << "@NA@" << D->getName();
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000384}
385
Argyrios Kyrtzidis2b174382020-03-07 15:04:23 -0800386static const ObjCCategoryDecl *getCategoryContext(const NamedDecl *D) {
387 if (auto *CD = dyn_cast<ObjCCategoryDecl>(D->getDeclContext()))
388 return CD;
389 if (auto *ICD = dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
390 return ICD->getCategoryDecl();
391 return nullptr;
Michael Liao073dbaa2020-03-08 12:59:21 -0400392}
Argyrios Kyrtzidis2b174382020-03-07 15:04:23 -0800393
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000394void USRGenerator::VisitObjCMethodDecl(const ObjCMethodDecl *D) {
395 const DeclContext *container = D->getDeclContext();
396 if (const ObjCProtocolDecl *pd = dyn_cast<ObjCProtocolDecl>(container)) {
397 Visit(pd);
398 }
399 else {
400 // The USR for a method declared in a class extension or category is based on
401 // the ObjCInterfaceDecl, not the ObjCCategoryDecl.
402 const ObjCInterfaceDecl *ID = D->getClassInterface();
403 if (!ID) {
404 IgnoreResults = true;
405 return;
406 }
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +0000407 auto *CD = getCategoryContext(D);
408 VisitObjCContainerDecl(ID, CD);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000409 }
410 // Ideally we would use 'GenObjCMethod', but this is such a hot path
411 // for Objective-C code that we don't want to use
412 // DeclarationName::getAsString().
413 Out << (D->isInstanceMethod() ? "(im)" : "(cm)")
414 << DeclarationName(D->getSelector());
415}
416
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +0000417void USRGenerator::VisitObjCContainerDecl(const ObjCContainerDecl *D,
418 const ObjCCategoryDecl *CatD) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000419 switch (D->getKind()) {
420 default:
421 llvm_unreachable("Invalid ObjC container.");
422 case Decl::ObjCInterface:
423 case Decl::ObjCImplementation:
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +0000424 GenObjCClass(D->getName(), GetExternalSourceContainer(D),
425 GetExternalSourceContainer(CatD));
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000426 break;
427 case Decl::ObjCCategory: {
428 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
429 const ObjCInterfaceDecl *ID = CD->getClassInterface();
430 if (!ID) {
431 // Handle invalid code where the @interface might not
432 // have been specified.
433 // FIXME: We should be able to generate this USR even if the
434 // @interface isn't available.
435 IgnoreResults = true;
436 return;
437 }
438 // Specially handle class extensions, which are anonymous categories.
439 // We want to mangle in the location to uniquely distinguish them.
440 if (CD->IsClassExtension()) {
441 Out << "objc(ext)" << ID->getName() << '@';
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000442 GenLoc(CD, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000443 }
444 else
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000445 GenObjCCategory(ID->getName(), CD->getName(),
446 GetExternalSourceContainer(ID),
447 GetExternalSourceContainer(CD));
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000448
449 break;
450 }
451 case Decl::ObjCCategoryImpl: {
452 const ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
453 const ObjCInterfaceDecl *ID = CD->getClassInterface();
454 if (!ID) {
455 // Handle invalid code where the @interface might not
456 // have been specified.
457 // FIXME: We should be able to generate this USR even if the
458 // @interface isn't available.
459 IgnoreResults = true;
460 return;
461 }
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000462 GenObjCCategory(ID->getName(), CD->getName(),
463 GetExternalSourceContainer(ID),
464 GetExternalSourceContainer(CD));
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000465 break;
466 }
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000467 case Decl::ObjCProtocol: {
468 const ObjCProtocolDecl *PD = cast<ObjCProtocolDecl>(D);
469 GenObjCProtocol(PD->getName(), GetExternalSourceContainer(PD));
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000470 break;
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000471 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000472 }
473}
474
475void USRGenerator::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
476 // The USR for a property declared in a class extension or category is based
477 // on the ObjCInterfaceDecl, not the ObjCCategoryDecl.
478 if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D))
Argyrios Kyrtzidis2b174382020-03-07 15:04:23 -0800479 VisitObjCContainerDecl(ID, getCategoryContext(D));
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000480 else
481 Visit(cast<Decl>(D->getDeclContext()));
Argyrios Kyrtzidisd9849a92016-07-15 22:18:19 +0000482 GenObjCProperty(D->getName(), D->isClassProperty());
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000483}
484
485void USRGenerator::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
486 if (ObjCPropertyDecl *PD = D->getPropertyDecl()) {
487 VisitObjCPropertyDecl(PD);
488 return;
489 }
490
491 IgnoreResults = true;
492}
493
494void USRGenerator::VisitTagDecl(const TagDecl *D) {
495 // Add the location of the tag decl to handle resolution across
496 // translation units.
Argyrios Kyrtzidis73321062016-03-04 07:17:53 +0000497 if (!isa<EnumDecl>(D) &&
498 ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000499 return;
500
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000501 GenExtSymbolContainer(D);
502
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000503 D = D->getCanonicalDecl();
504 VisitDeclContext(D->getDeclContext());
505
506 bool AlreadyStarted = false;
507 if (const CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(D)) {
508 if (ClassTemplateDecl *ClassTmpl = CXXRecord->getDescribedClassTemplate()) {
509 AlreadyStarted = true;
Fangrui Song6907ce22018-07-30 19:24:48 +0000510
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000511 switch (D->getTagKind()) {
512 case TTK_Interface:
Argyrios Kyrtzidisf66cef72014-12-08 08:48:33 +0000513 case TTK_Class:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000514 case TTK_Struct: Out << "@ST"; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000515 case TTK_Union: Out << "@UT"; break;
516 case TTK_Enum: llvm_unreachable("enum template");
517 }
518 VisitTemplateParameterList(ClassTmpl->getTemplateParameters());
519 } else if (const ClassTemplatePartialSpecializationDecl *PartialSpec
520 = dyn_cast<ClassTemplatePartialSpecializationDecl>(CXXRecord)) {
521 AlreadyStarted = true;
Fangrui Song6907ce22018-07-30 19:24:48 +0000522
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000523 switch (D->getTagKind()) {
524 case TTK_Interface:
Argyrios Kyrtzidisf66cef72014-12-08 08:48:33 +0000525 case TTK_Class:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000526 case TTK_Struct: Out << "@SP"; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000527 case TTK_Union: Out << "@UP"; break;
528 case TTK_Enum: llvm_unreachable("enum partial specialization");
Fangrui Song6907ce22018-07-30 19:24:48 +0000529 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000530 VisitTemplateParameterList(PartialSpec->getTemplateParameters());
531 }
532 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000533
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000534 if (!AlreadyStarted) {
535 switch (D->getTagKind()) {
536 case TTK_Interface:
Argyrios Kyrtzidisf66cef72014-12-08 08:48:33 +0000537 case TTK_Class:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000538 case TTK_Struct: Out << "@S"; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000539 case TTK_Union: Out << "@U"; break;
540 case TTK_Enum: Out << "@E"; break;
541 }
542 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000543
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000544 Out << '@';
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000545 assert(Buf.size() > 0);
546 const unsigned off = Buf.size() - 1;
547
548 if (EmitDeclName(D)) {
549 if (const TypedefNameDecl *TD = D->getTypedefNameForAnonDecl()) {
550 Buf[off] = 'A';
551 Out << '@' << *TD;
552 }
Argyrios Kyrtzidis80537a42014-12-08 08:48:37 +0000553 else {
554 if (D->isEmbeddedInDeclarator() && !D->isFreeStanding()) {
555 printLoc(Out, D->getLocation(), Context->getSourceManager(), true);
Argyrios Kyrtzidis73321062016-03-04 07:17:53 +0000556 } else {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000557 Buf[off] = 'a';
Argyrios Kyrtzidis73321062016-03-04 07:17:53 +0000558 if (auto *ED = dyn_cast<EnumDecl>(D)) {
559 // Distinguish USRs of anonymous enums by using their first enumerator.
560 auto enum_range = ED->enumerators();
561 if (enum_range.begin() != enum_range.end()) {
562 Out << '@' << **enum_range.begin();
563 }
564 }
565 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000566 }
Argyrios Kyrtzidis80537a42014-12-08 08:48:37 +0000567 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000568
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000569 // For a class template specialization, mangle the template arguments.
570 if (const ClassTemplateSpecializationDecl *Spec
571 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
Argyrios Kyrtzidis7d90ed02017-02-15 16:16:27 +0000572 const TemplateArgumentList &Args = Spec->getTemplateArgs();
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000573 Out << '>';
574 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
575 Out << '#';
576 VisitTemplateArgument(Args.get(I));
577 }
578 }
579}
580
581void USRGenerator::VisitTypedefDecl(const TypedefDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000582 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000583 return;
584 const DeclContext *DC = D->getDeclContext();
585 if (const NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
586 Visit(DCN);
587 Out << "@T@";
588 Out << D->getName();
589}
590
591void USRGenerator::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000592 GenLoc(D, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000593}
594
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +0000595void USRGenerator::GenExtSymbolContainer(const NamedDecl *D) {
596 StringRef Container = GetExternalSourceContainer(D);
597 if (!Container.empty())
598 Out << "@M@" << Container;
599}
600
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000601bool USRGenerator::GenLoc(const Decl *D, bool IncludeOffset) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000602 if (generatedLoc)
603 return IgnoreResults;
604 generatedLoc = true;
Dmitri Gribenko237769e2014-03-28 22:21:26 +0000605
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000606 // Guard against null declarations in invalid code.
607 if (!D) {
608 IgnoreResults = true;
609 return true;
610 }
611
612 // Use the location of canonical decl.
613 D = D->getCanonicalDecl();
614
Dmitri Gribenko237769e2014-03-28 22:21:26 +0000615 IgnoreResults =
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000616 IgnoreResults || printLoc(Out, D->getBeginLoc(),
Dmitri Gribenko237769e2014-03-28 22:21:26 +0000617 Context->getSourceManager(), IncludeOffset);
618
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000619 return IgnoreResults;
620}
621
Ben Langmuirfd6e39c2017-08-16 23:12:21 +0000622static void printQualifier(llvm::raw_ostream &Out, ASTContext &Ctx, NestedNameSpecifier *NNS) {
623 // FIXME: Encode the qualifier, don't just print it.
624 PrintingPolicy PO(Ctx.getLangOpts());
625 PO.SuppressTagKeyword = true;
626 PO.SuppressUnwrittenScope = true;
627 PO.ConstantArraySizeAsWritten = false;
628 PO.AnonymousTagLocations = false;
629 NNS->print(Out, PO);
630}
631
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000632void USRGenerator::VisitType(QualType T) {
633 // This method mangles in USR information for types. It can possibly
634 // just reuse the naming-mangling logic used by codegen, although the
635 // requirements for USRs might not be the same.
636 ASTContext &Ctx = *Context;
637
638 do {
639 T = Ctx.getCanonicalType(T);
640 Qualifiers Q = T.getQualifiers();
641 unsigned qVal = 0;
642 if (Q.hasConst())
643 qVal |= 0x1;
644 if (Q.hasVolatile())
645 qVal |= 0x2;
646 if (Q.hasRestrict())
647 qVal |= 0x4;
648 if(qVal)
649 Out << ((char) ('0' + qVal));
650
651 // Mangle in ObjC GC qualifiers?
652
653 if (const PackExpansionType *Expansion = T->getAs<PackExpansionType>()) {
654 Out << 'P';
655 T = Expansion->getPattern();
656 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000657
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000658 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
659 unsigned char c = '\0';
660 switch (BT->getKind()) {
661 case BuiltinType::Void:
662 c = 'v'; break;
663 case BuiltinType::Bool:
664 c = 'b'; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000665 case BuiltinType::UChar:
666 c = 'c'; break;
Richard Smith3a8244d2018-05-01 05:02:45 +0000667 case BuiltinType::Char8:
668 c = 'u'; break; // FIXME: Check this doesn't collide
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000669 case BuiltinType::Char16:
670 c = 'q'; break;
671 case BuiltinType::Char32:
672 c = 'w'; break;
673 case BuiltinType::UShort:
674 c = 's'; break;
675 case BuiltinType::UInt:
676 c = 'i'; break;
677 case BuiltinType::ULong:
678 c = 'l'; break;
679 case BuiltinType::ULongLong:
680 c = 'k'; break;
681 case BuiltinType::UInt128:
682 c = 'j'; break;
Argyrios Kyrtzidis56af7e62014-12-08 09:09:05 +0000683 case BuiltinType::Char_U:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000684 case BuiltinType::Char_S:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000685 c = 'C'; break;
Argyrios Kyrtzidisca044542014-12-08 08:48:17 +0000686 case BuiltinType::SChar:
687 c = 'r'; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000688 case BuiltinType::WChar_S:
689 case BuiltinType::WChar_U:
690 c = 'W'; break;
691 case BuiltinType::Short:
692 c = 'S'; break;
693 case BuiltinType::Int:
694 c = 'I'; break;
695 case BuiltinType::Long:
696 c = 'L'; break;
697 case BuiltinType::LongLong:
698 c = 'K'; break;
699 case BuiltinType::Int128:
700 c = 'J'; break;
Sjoerd Meijercc623ad2017-09-08 15:15:00 +0000701 case BuiltinType::Float16:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000702 case BuiltinType::Half:
703 c = 'h'; break;
704 case BuiltinType::Float:
705 c = 'f'; break;
706 case BuiltinType::Double:
707 c = 'd'; break;
708 case BuiltinType::LongDouble:
709 c = 'D'; break;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000710 case BuiltinType::Float128:
711 c = 'Q'; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000712 case BuiltinType::NullPtr:
713 c = 'n'; break;
714#define BUILTIN_TYPE(Id, SingletonId)
715#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
716#include "clang/AST/BuiltinTypes.def"
717 case BuiltinType::Dependent:
Alexey Bader954ba212016-04-08 13:40:33 +0000718#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
719 case BuiltinType::Id:
Alexey Baderb62f1442016-04-13 08:33:41 +0000720#include "clang/Basic/OpenCLImageTypes.def"
Andrew Savonichev3fee3512018-11-08 11:25:41 +0000721#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
722 case BuiltinType::Id:
723#include "clang/Basic/OpenCLExtensionTypes.def"
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000724 case BuiltinType::OCLEvent:
Alexey Bader9c8453f2015-09-15 11:18:52 +0000725 case BuiltinType::OCLClkEvent:
726 case BuiltinType::OCLQueue:
Alexey Bader9c8453f2015-09-15 11:18:52 +0000727 case BuiltinType::OCLReserveID:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000728 case BuiltinType::OCLSampler:
Richard Sandifordeb485fb2019-08-09 08:52:54 +0000729#define SVE_TYPE(Name, Id, SingletonId) \
730 case BuiltinType::Id:
731#include "clang/Basic/AArch64SVEACLETypes.def"
Leonard Chanf921d852018-06-04 16:07:52 +0000732 case BuiltinType::ShortAccum:
733 case BuiltinType::Accum:
734 case BuiltinType::LongAccum:
735 case BuiltinType::UShortAccum:
736 case BuiltinType::UAccum:
737 case BuiltinType::ULongAccum:
Leonard Chanab80f3c2018-06-14 14:53:51 +0000738 case BuiltinType::ShortFract:
739 case BuiltinType::Fract:
740 case BuiltinType::LongFract:
741 case BuiltinType::UShortFract:
742 case BuiltinType::UFract:
743 case BuiltinType::ULongFract:
744 case BuiltinType::SatShortAccum:
745 case BuiltinType::SatAccum:
746 case BuiltinType::SatLongAccum:
747 case BuiltinType::SatUShortAccum:
748 case BuiltinType::SatUAccum:
749 case BuiltinType::SatULongAccum:
750 case BuiltinType::SatShortFract:
751 case BuiltinType::SatFract:
752 case BuiltinType::SatLongFract:
753 case BuiltinType::SatUShortFract:
754 case BuiltinType::SatUFract:
755 case BuiltinType::SatULongFract:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000756 IgnoreResults = true;
757 return;
758 case BuiltinType::ObjCId:
759 c = 'o'; break;
760 case BuiltinType::ObjCClass:
761 c = 'O'; break;
762 case BuiltinType::ObjCSel:
763 c = 'e'; break;
764 }
765 Out << c;
766 return;
767 }
768
769 // If we have already seen this (non-built-in) type, use a substitution
770 // encoding.
771 llvm::DenseMap<const Type *, unsigned>::iterator Substitution
772 = TypeSubstitutions.find(T.getTypePtr());
773 if (Substitution != TypeSubstitutions.end()) {
774 Out << 'S' << Substitution->second << '_';
775 return;
776 } else {
777 // Record this as a substitution.
778 unsigned Number = TypeSubstitutions.size();
779 TypeSubstitutions[T.getTypePtr()] = Number;
780 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000781
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000782 if (const PointerType *PT = T->getAs<PointerType>()) {
783 Out << '*';
784 T = PT->getPointeeType();
785 continue;
786 }
Argyrios Kyrtzidis9c998672016-03-04 07:17:43 +0000787 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
788 Out << '*';
789 T = OPT->getPointeeType();
790 continue;
791 }
Argyrios Kyrtzidis0e387dc2014-12-08 08:48:27 +0000792 if (const RValueReferenceType *RT = T->getAs<RValueReferenceType>()) {
793 Out << "&&";
794 T = RT->getPointeeType();
795 continue;
796 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000797 if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
798 Out << '&';
799 T = RT->getPointeeType();
800 continue;
801 }
802 if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) {
803 Out << 'F';
Alp Toker314cc812014-01-25 16:55:45 +0000804 VisitType(FT->getReturnType());
Jan Korouse6a02422017-10-10 00:35:16 +0000805 Out << '(';
806 for (const auto &I : FT->param_types()) {
807 Out << '#';
Aaron Ballman40bd0aa2014-03-17 15:23:01 +0000808 VisitType(I);
Jan Korouse6a02422017-10-10 00:35:16 +0000809 }
810 Out << ')';
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000811 if (FT->isVariadic())
812 Out << '.';
813 return;
814 }
815 if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) {
816 Out << 'B';
817 T = BT->getPointeeType();
818 continue;
819 }
820 if (const ComplexType *CT = T->getAs<ComplexType>()) {
821 Out << '<';
822 T = CT->getElementType();
823 continue;
824 }
825 if (const TagType *TT = T->getAs<TagType>()) {
826 Out << '$';
827 VisitTagDecl(TT->getDecl());
828 return;
829 }
Argyrios Kyrtzidis9c998672016-03-04 07:17:43 +0000830 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
831 Out << '$';
832 VisitObjCInterfaceDecl(OIT->getDecl());
833 return;
834 }
835 if (const ObjCObjectType *OIT = T->getAs<ObjCObjectType>()) {
836 Out << 'Q';
837 VisitType(OIT->getBaseType());
838 for (auto *Prot : OIT->getProtocols())
839 VisitObjCProtocolDecl(Prot);
840 return;
841 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000842 if (const TemplateTypeParmType *TTP = T->getAs<TemplateTypeParmType>()) {
843 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
844 return;
845 }
846 if (const TemplateSpecializationType *Spec
847 = T->getAs<TemplateSpecializationType>()) {
848 Out << '>';
849 VisitTemplateName(Spec->getTemplateName());
850 Out << Spec->getNumArgs();
851 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
852 VisitTemplateArgument(Spec->getArg(I));
853 return;
854 }
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000855 if (const DependentNameType *DNT = T->getAs<DependentNameType>()) {
856 Out << '^';
Ben Langmuirfd6e39c2017-08-16 23:12:21 +0000857 printQualifier(Out, Ctx, DNT->getQualifier());
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000858 Out << ':' << DNT->getIdentifier()->getName();
859 return;
860 }
Argyrios Kyrtzidis1d5e5422014-12-08 08:48:43 +0000861 if (const InjectedClassNameType *InjT = T->getAs<InjectedClassNameType>()) {
862 T = InjT->getInjectedSpecializationType();
863 continue;
864 }
Alex Lorenz45c423b2017-04-28 09:46:36 +0000865 if (const auto *VT = T->getAs<VectorType>()) {
866 Out << (T->isExtVectorType() ? ']' : '[');
867 Out << VT->getNumElements();
868 T = VT->getElementType();
869 continue;
870 }
Jan Korous663ba152017-10-09 19:51:33 +0000871 if (const auto *const AT = dyn_cast<ArrayType>(T)) {
872 Out << '{';
873 switch (AT->getSizeModifier()) {
874 case ArrayType::Static:
875 Out << 's';
876 break;
877 case ArrayType::Star:
878 Out << '*';
879 break;
880 case ArrayType::Normal:
881 Out << 'n';
882 break;
883 }
884 if (const auto *const CAT = dyn_cast<ConstantArrayType>(T))
885 Out << CAT->getSize();
886
887 T = AT->getElementType();
888 continue;
889 }
Alex Lorenz45c423b2017-04-28 09:46:36 +0000890
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000891 // Unhandled type.
892 Out << ' ';
893 break;
894 } while (true);
895}
896
897void USRGenerator::VisitTemplateParameterList(
898 const TemplateParameterList *Params) {
899 if (!Params)
900 return;
901 Out << '>' << Params->size();
902 for (TemplateParameterList::const_iterator P = Params->begin(),
903 PEnd = Params->end();
904 P != PEnd; ++P) {
905 Out << '#';
906 if (isa<TemplateTypeParmDecl>(*P)) {
907 if (cast<TemplateTypeParmDecl>(*P)->isParameterPack())
908 Out<< 'p';
909 Out << 'T';
910 continue;
911 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000912
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000913 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
914 if (NTTP->isParameterPack())
915 Out << 'p';
916 Out << 'N';
917 VisitType(NTTP->getType());
918 continue;
919 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000920
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000921 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
922 if (TTP->isParameterPack())
923 Out << 'p';
924 Out << 't';
925 VisitTemplateParameterList(TTP->getTemplateParameters());
926 }
927}
928
929void USRGenerator::VisitTemplateName(TemplateName Name) {
930 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
931 if (TemplateTemplateParmDecl *TTP
932 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
933 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
934 return;
935 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000936
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000937 Visit(Template);
938 return;
939 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000940
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000941 // FIXME: Visit dependent template names.
942}
943
944void USRGenerator::VisitTemplateArgument(const TemplateArgument &Arg) {
945 switch (Arg.getKind()) {
946 case TemplateArgument::Null:
947 break;
948
949 case TemplateArgument::Declaration:
950 Visit(Arg.getAsDecl());
951 break;
952
953 case TemplateArgument::NullPtr:
954 break;
955
956 case TemplateArgument::TemplateExpansion:
957 Out << 'P'; // pack expansion of...
Reid Kleckner4dc0b1a2018-11-01 19:54:45 +0000958 LLVM_FALLTHROUGH;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000959 case TemplateArgument::Template:
960 VisitTemplateName(Arg.getAsTemplateOrTemplatePattern());
961 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000962
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000963 case TemplateArgument::Expression:
964 // FIXME: Visit expressions.
965 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000966
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000967 case TemplateArgument::Pack:
968 Out << 'p' << Arg.pack_size();
Aaron Ballman2a89e852014-07-15 21:32:31 +0000969 for (const auto &P : Arg.pack_elements())
970 VisitTemplateArgument(P);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000971 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000972
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000973 case TemplateArgument::Type:
974 VisitType(Arg.getAsType());
975 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000976
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000977 case TemplateArgument::Integral:
978 Out << 'V';
979 VisitType(Arg.getIntegralType());
980 Out << Arg.getAsIntegral();
981 break;
982 }
983}
984
Ben Langmuirfd6e39c2017-08-16 23:12:21 +0000985void USRGenerator::VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D) {
986 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
987 return;
988 VisitDeclContext(D->getDeclContext());
989 Out << "@UUV@";
990 printQualifier(Out, D->getASTContext(), D->getQualifier());
991 EmitDeclName(D);
992}
993
994void USRGenerator::VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D) {
995 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
996 return;
997 VisitDeclContext(D->getDeclContext());
998 Out << "@UUT@";
999 printQualifier(Out, D->getASTContext(), D->getQualifier());
1000 Out << D->getName(); // Simple name.
1001}
1002
1003
1004
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001005//===----------------------------------------------------------------------===//
1006// USR generation functions.
1007//===----------------------------------------------------------------------===//
1008
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +00001009static void combineClassAndCategoryExtContainers(StringRef ClsSymDefinedIn,
1010 StringRef CatSymDefinedIn,
1011 raw_ostream &OS) {
1012 if (ClsSymDefinedIn.empty() && CatSymDefinedIn.empty())
1013 return;
1014 if (CatSymDefinedIn.empty()) {
1015 OS << "@M@" << ClsSymDefinedIn << '@';
1016 return;
1017 }
1018 OS << "@CM@" << CatSymDefinedIn << '@';
1019 if (ClsSymDefinedIn != CatSymDefinedIn) {
1020 OS << ClsSymDefinedIn << '@';
1021 }
1022}
1023
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +00001024void clang::index::generateUSRForObjCClass(StringRef Cls, raw_ostream &OS,
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +00001025 StringRef ExtSymDefinedIn,
1026 StringRef CategoryContextExtSymbolDefinedIn) {
1027 combineClassAndCategoryExtContainers(ExtSymDefinedIn,
1028 CategoryContextExtSymbolDefinedIn, OS);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001029 OS << "objc(cs)" << Cls;
1030}
1031
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +00001032void clang::index::generateUSRForObjCCategory(StringRef Cls, StringRef Cat,
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +00001033 raw_ostream &OS,
1034 StringRef ClsSymDefinedIn,
1035 StringRef CatSymDefinedIn) {
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +00001036 combineClassAndCategoryExtContainers(ClsSymDefinedIn, CatSymDefinedIn, OS);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001037 OS << "objc(cy)" << Cls << '@' << Cat;
1038}
1039
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +00001040void clang::index::generateUSRForObjCIvar(StringRef Ivar, raw_ostream &OS) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001041 OS << '@' << Ivar;
1042}
1043
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +00001044void clang::index::generateUSRForObjCMethod(StringRef Sel,
1045 bool IsInstanceMethod,
1046 raw_ostream &OS) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001047 OS << (IsInstanceMethod ? "(im)" : "(cm)") << Sel;
1048}
1049
Argyrios Kyrtzidisd9849a92016-07-15 22:18:19 +00001050void clang::index::generateUSRForObjCProperty(StringRef Prop, bool isClassProp,
1051 raw_ostream &OS) {
1052 OS << (isClassProp ? "(cpy)" : "(py)") << Prop;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001053}
1054
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +00001055void clang::index::generateUSRForObjCProtocol(StringRef Prot, raw_ostream &OS,
1056 StringRef ExtSymDefinedIn) {
1057 if (!ExtSymDefinedIn.empty())
1058 OS << "@M@" << ExtSymDefinedIn << '@';
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001059 OS << "objc(pl)" << Prot;
1060}
1061
Argyrios Kyrtzidis6e5ca5b2017-04-21 05:42:46 +00001062void clang::index::generateUSRForGlobalEnum(StringRef EnumName, raw_ostream &OS,
1063 StringRef ExtSymDefinedIn) {
1064 if (!ExtSymDefinedIn.empty())
1065 OS << "@M@" << ExtSymDefinedIn;
1066 OS << "@E@" << EnumName;
1067}
1068
Argyrios Kyrtzidisf3634742017-04-21 22:27:06 +00001069void clang::index::generateUSRForEnumConstant(StringRef EnumConstantName,
1070 raw_ostream &OS) {
1071 OS << '@' << EnumConstantName;
1072}
1073
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +00001074bool clang::index::generateUSRForDecl(const Decl *D,
1075 SmallVectorImpl<char> &Buf) {
Argyrios Kyrtzidisf12918d2016-11-02 23:42:33 +00001076 if (!D)
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001077 return true;
Argyrios Kyrtzidisf12918d2016-11-02 23:42:33 +00001078 // We don't ignore decls with invalid source locations. Implicit decls, like
1079 // C++'s operator new function, can have invalid locations but it is fine to
1080 // create USRs that can identify them.
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001081
1082 USRGenerator UG(&D->getASTContext(), Buf);
1083 UG.Visit(D);
1084 return UG.ignoreResults();
1085}
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001086
Richard Smith66a81862015-05-04 02:25:31 +00001087bool clang::index::generateUSRForMacro(const MacroDefinitionRecord *MD,
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001088 const SourceManager &SM,
1089 SmallVectorImpl<char> &Buf) {
Argyrios Kyrtzidis5b7a09a2017-02-02 16:13:10 +00001090 if (!MD)
1091 return true;
1092 return generateUSRForMacro(MD->getName()->getName(), MD->getLocation(),
1093 SM, Buf);
1094
1095}
1096
1097bool clang::index::generateUSRForMacro(StringRef MacroName, SourceLocation Loc,
1098 const SourceManager &SM,
1099 SmallVectorImpl<char> &Buf) {
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001100 // Don't generate USRs for things with invalid locations.
Argyrios Kyrtzidis5b7a09a2017-02-02 16:13:10 +00001101 if (MacroName.empty() || Loc.isInvalid())
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001102 return true;
1103
1104 llvm::raw_svector_ostream Out(Buf);
1105
1106 // Assume that system headers are sane. Don't put source location
1107 // information into the USR if the macro comes from a system header.
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001108 bool ShouldGenerateLocation = !SM.isInSystemHeader(Loc);
1109
1110 Out << getUSRSpacePrefix();
1111 if (ShouldGenerateLocation)
1112 printLoc(Out, Loc, SM, /*IncludeOffset=*/true);
1113 Out << "@macro@";
Argyrios Kyrtzidis5b7a09a2017-02-02 16:13:10 +00001114 Out << MacroName;
Dmitri Gribenko237769e2014-03-28 22:21:26 +00001115 return false;
1116}
Argyrios Kyrtzidis32e5d862018-09-18 15:02:56 +00001117
Ilya Biryukova6224842018-11-26 15:24:48 +00001118bool clang::index::generateUSRForType(QualType T, ASTContext &Ctx,
1119 SmallVectorImpl<char> &Buf) {
1120 if (T.isNull())
1121 return true;
1122 T = T.getCanonicalType();
1123
1124 USRGenerator UG(&Ctx, Buf);
1125 UG.VisitType(T);
1126 return UG.ignoreResults();
1127}
1128
Argyrios Kyrtzidis32e5d862018-09-18 15:02:56 +00001129bool clang::index::generateFullUSRForModule(const Module *Mod,
1130 raw_ostream &OS) {
1131 if (!Mod->Parent)
1132 return generateFullUSRForTopLevelModuleName(Mod->Name, OS);
1133 if (generateFullUSRForModule(Mod->Parent, OS))
1134 return true;
1135 return generateUSRFragmentForModule(Mod, OS);
1136}
1137
1138bool clang::index::generateFullUSRForTopLevelModuleName(StringRef ModName,
1139 raw_ostream &OS) {
1140 OS << getUSRSpacePrefix();
1141 return generateUSRFragmentForModuleName(ModName, OS);
1142}
1143
1144bool clang::index::generateUSRFragmentForModule(const Module *Mod,
1145 raw_ostream &OS) {
1146 return generateUSRFragmentForModuleName(Mod->Name, OS);
1147}
1148
1149bool clang::index::generateUSRFragmentForModuleName(StringRef ModName,
1150 raw_ostream &OS) {
1151 OS << "@M@" << ModName;
1152 return false;
1153}