blob: bec777ddf4f914a5ee98dbaadbef0c2d8154edb0 [file] [log] [blame]
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +00001//===- USRGeneration.cpp - Routines for USR generation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Argyrios Kyrtzidis15a2fcc2013-08-17 00:40:41 +000010#include "clang/Index/USRGeneration.h"
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000011#include "clang/AST/ASTContext.h"
12#include "clang/AST/DeclTemplate.h"
13#include "clang/AST/DeclVisitor.h"
Dmitri Gribenko237769e2014-03-28 22:21:26 +000014#include "clang/Lex/PreprocessingRecord.h"
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000015#include "llvm/Support/Path.h"
16#include "llvm/Support/raw_ostream.h"
17
18using namespace clang;
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +000019using namespace clang::index;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000020
21//===----------------------------------------------------------------------===//
22// USR generation.
23//===----------------------------------------------------------------------===//
24
Dmitri Gribenko237769e2014-03-28 22:21:26 +000025/// \returns true on error.
26static bool printLoc(llvm::raw_ostream &OS, SourceLocation Loc,
27 const SourceManager &SM, bool IncludeOffset) {
28 if (Loc.isInvalid()) {
29 return true;
30 }
31 Loc = SM.getExpansionLoc(Loc);
32 const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(Loc);
33 const FileEntry *FE = SM.getFileEntryForID(Decomposed.first);
34 if (FE) {
35 OS << llvm::sys::path::filename(FE->getName());
36 } else {
37 // This case really isn't interesting.
38 return true;
39 }
40 if (IncludeOffset) {
41 // Use the offest into the FileID to represent the location. Using
42 // a line/column can cause us to look back at the original source file,
43 // which is expensive.
44 OS << '@' << Decomposed.second;
45 }
46 return false;
47}
48
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000049namespace {
50class USRGenerator : public ConstDeclVisitor<USRGenerator> {
51 SmallVectorImpl<char> &Buf;
52 llvm::raw_svector_ostream Out;
53 bool IgnoreResults;
54 ASTContext *Context;
55 bool generatedLoc;
56
57 llvm::DenseMap<const Type *, unsigned> TypeSubstitutions;
58
59public:
60 explicit USRGenerator(ASTContext *Ctx, SmallVectorImpl<char> &Buf)
61 : Buf(Buf),
62 Out(Buf),
63 IgnoreResults(false),
64 Context(Ctx),
65 generatedLoc(false)
66 {
67 // Add the USR space prefix.
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +000068 Out << getUSRSpacePrefix();
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000069 }
70
71 bool ignoreResults() const { return IgnoreResults; }
72
73 // Visitation methods from generating USRs from AST elements.
74 void VisitDeclContext(const DeclContext *D);
75 void VisitFieldDecl(const FieldDecl *D);
76 void VisitFunctionDecl(const FunctionDecl *D);
77 void VisitNamedDecl(const NamedDecl *D);
78 void VisitNamespaceDecl(const NamespaceDecl *D);
79 void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D);
80 void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D);
81 void VisitClassTemplateDecl(const ClassTemplateDecl *D);
82 void VisitObjCContainerDecl(const ObjCContainerDecl *CD);
83 void VisitObjCMethodDecl(const ObjCMethodDecl *MD);
84 void VisitObjCPropertyDecl(const ObjCPropertyDecl *D);
85 void VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D);
86 void VisitTagDecl(const TagDecl *D);
87 void VisitTypedefDecl(const TypedefDecl *D);
88 void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D);
89 void VisitVarDecl(const VarDecl *D);
90 void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D);
91 void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D);
Eugene Zelenko1ced5092016-02-12 22:53:10 +000092
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000093 void VisitLinkageSpecDecl(const LinkageSpecDecl *D) {
94 IgnoreResults = true;
95 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +000096
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +000097 void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
98 IgnoreResults = true;
99 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000100
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000101 void VisitUsingDecl(const UsingDecl *D) {
102 IgnoreResults = true;
103 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000104
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000105 void VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D) {
106 IgnoreResults = true;
107 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000108
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000109 void VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D) {
110 IgnoreResults = true;
111 }
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000112
113 bool ShouldGenerateLocation(const NamedDecl *D);
114
115 bool isLocal(const NamedDecl *D) {
Craig Topper236bde32014-05-26 06:21:51 +0000116 return D->getParentFunctionOrMethod() != nullptr;
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000117 }
118
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000119 /// Generate the string component containing the location of the
120 /// declaration.
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000121 bool GenLoc(const Decl *D, bool IncludeOffset);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000122
123 /// String generation methods used both by the visitation methods
124 /// and from other clients that want to directly generate USRs. These
125 /// methods do not construct complete USRs (which incorporate the parents
126 /// of an AST element), but only the fragments concerning the AST element
127 /// itself.
128
129 /// Generate a USR for an Objective-C class.
130 void GenObjCClass(StringRef cls) {
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +0000131 generateUSRForObjCClass(cls, Out);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000132 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000133
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000134 /// Generate a USR for an Objective-C class category.
135 void GenObjCCategory(StringRef cls, StringRef cat) {
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +0000136 generateUSRForObjCCategory(cls, cat, Out);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000137 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000138
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000139 /// Generate a USR fragment for an Objective-C property.
Argyrios Kyrtzidisd9849a92016-07-15 22:18:19 +0000140 void GenObjCProperty(StringRef prop, bool isClassProp) {
141 generateUSRForObjCProperty(prop, isClassProp, Out);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000142 }
Eugene Zelenko1ced5092016-02-12 22:53:10 +0000143
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000144 /// Generate a USR for an Objective-C protocol.
145 void GenObjCProtocol(StringRef prot) {
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +0000146 generateUSRForObjCProtocol(prot, Out);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000147 }
148
149 void VisitType(QualType T);
150 void VisitTemplateParameterList(const TemplateParameterList *Params);
151 void VisitTemplateName(TemplateName Name);
152 void VisitTemplateArgument(const TemplateArgument &Arg);
153
154 /// Emit a Decl's name using NamedDecl::printName() and return true if
155 /// the decl had no name.
156 bool EmitDeclName(const NamedDecl *D);
157};
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000158} // end anonymous namespace
159
160//===----------------------------------------------------------------------===//
161// Generating USRs from ASTS.
162//===----------------------------------------------------------------------===//
163
164bool USRGenerator::EmitDeclName(const NamedDecl *D) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000165 const unsigned startSize = Buf.size();
166 D->printName(Out);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000167 const unsigned endSize = Buf.size();
168 return startSize == endSize;
169}
170
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000171bool USRGenerator::ShouldGenerateLocation(const NamedDecl *D) {
172 if (D->isExternallyVisible())
173 return false;
174 if (D->getParentFunctionOrMethod())
175 return true;
Argyrios Kyrtzidisf12918d2016-11-02 23:42:33 +0000176 SourceLocation Loc = D->getLocation();
177 if (Loc.isInvalid())
178 return false;
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000179 const SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisf12918d2016-11-02 23:42:33 +0000180 return !SM.isInSystemHeader(Loc);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000181}
182
183void USRGenerator::VisitDeclContext(const DeclContext *DC) {
184 if (const NamedDecl *D = dyn_cast<NamedDecl>(DC))
185 Visit(D);
186}
187
188void USRGenerator::VisitFieldDecl(const FieldDecl *D) {
189 // The USR for an ivar declared in a class extension is based on the
190 // ObjCInterfaceDecl, not the ObjCCategoryDecl.
191 if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D))
192 Visit(ID);
193 else
194 VisitDeclContext(D->getDeclContext());
195 Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@");
196 if (EmitDeclName(D)) {
197 // Bit fields can be anonymous.
198 IgnoreResults = true;
199 return;
200 }
201}
202
203void USRGenerator::VisitFunctionDecl(const FunctionDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000204 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000205 return;
206
207 VisitDeclContext(D->getDeclContext());
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000208 bool IsTemplate = false;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000209 if (FunctionTemplateDecl *FunTmpl = D->getDescribedFunctionTemplate()) {
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000210 IsTemplate = true;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000211 Out << "@FT@";
212 VisitTemplateParameterList(FunTmpl->getTemplateParameters());
213 } else
214 Out << "@F@";
Argyrios Kyrtzidisd5719082016-02-15 01:32:36 +0000215
216 PrintingPolicy Policy(Context->getLangOpts());
217 // Forward references can have different template argument names. Suppress the
218 // template argument names in constructors to make their USR more stable.
219 Policy.SuppressTemplateArgsInCXXConstructors = true;
220 D->getDeclName().print(Out, Policy);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000221
222 ASTContext &Ctx = *Context;
Argyrios Kyrtzidis2682f9e2016-03-04 07:17:48 +0000223 if ((!Ctx.getLangOpts().CPlusPlus || D->isExternC()) &&
224 !D->hasAttr<OverloadableAttr>())
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000225 return;
226
227 if (const TemplateArgumentList *
228 SpecArgs = D->getTemplateSpecializationArgs()) {
229 Out << '<';
230 for (unsigned I = 0, N = SpecArgs->size(); I != N; ++I) {
231 Out << '#';
232 VisitTemplateArgument(SpecArgs->get(I));
233 }
234 Out << '>';
235 }
236
237 // Mangle in type information for the arguments.
David Majnemer59f77922016-06-24 04:05:48 +0000238 for (auto PD : D->parameters()) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000239 Out << '#';
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +0000240 VisitType(PD->getType());
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000241 }
242 if (D->isVariadic())
243 Out << '.';
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000244 if (IsTemplate) {
245 // Function templates can be overloaded by return type, for example:
246 // \code
247 // template <class T> typename T::A foo() {}
248 // template <class T> typename T::B foo() {}
249 // \endcode
250 Out << '#';
251 VisitType(D->getReturnType());
252 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000253 Out << '#';
254 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
255 if (MD->isStatic())
256 Out << 'S';
257 if (unsigned quals = MD->getTypeQualifiers())
258 Out << (char)('0' + quals);
Argyrios Kyrtzidisf5819092014-12-08 08:48:21 +0000259 switch (MD->getRefQualifier()) {
260 case RQ_None: break;
261 case RQ_LValue: Out << '&'; break;
262 case RQ_RValue: Out << "&&"; break;
263 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000264 }
265}
266
267void USRGenerator::VisitNamedDecl(const NamedDecl *D) {
268 VisitDeclContext(D->getDeclContext());
269 Out << "@";
270
271 if (EmitDeclName(D)) {
272 // The string can be empty if the declaration has no name; e.g., it is
273 // the ParmDecl with no name for declaration of a function pointer type,
274 // e.g.: void (*f)(void *);
275 // In this case, don't generate a USR.
276 IgnoreResults = true;
277 }
278}
279
280void USRGenerator::VisitVarDecl(const VarDecl *D) {
281 // VarDecls can be declared 'extern' within a function or method body,
282 // but their enclosing DeclContext is the function, not the TU. We need
283 // to check the storage class to correctly generate the USR.
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000284 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000285 return;
286
287 VisitDeclContext(D->getDeclContext());
288
289 // Variables always have simple names.
290 StringRef s = D->getName();
291
292 // The string can be empty if the declaration has no name; e.g., it is
293 // the ParmDecl with no name for declaration of a function pointer type, e.g.:
294 // void (*f)(void *);
295 // In this case, don't generate a USR.
296 if (s.empty())
297 IgnoreResults = true;
298 else
299 Out << '@' << s;
300}
301
302void USRGenerator::VisitNonTypeTemplateParmDecl(
303 const NonTypeTemplateParmDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000304 GenLoc(D, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000305}
306
307void USRGenerator::VisitTemplateTemplateParmDecl(
308 const TemplateTemplateParmDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000309 GenLoc(D, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000310}
311
312void USRGenerator::VisitNamespaceDecl(const NamespaceDecl *D) {
313 if (D->isAnonymousNamespace()) {
314 Out << "@aN";
315 return;
316 }
317
318 VisitDeclContext(D->getDeclContext());
319 if (!IgnoreResults)
320 Out << "@N@" << D->getName();
321}
322
323void USRGenerator::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
324 VisitFunctionDecl(D->getTemplatedDecl());
325}
326
327void USRGenerator::VisitClassTemplateDecl(const ClassTemplateDecl *D) {
328 VisitTagDecl(D->getTemplatedDecl());
329}
330
331void USRGenerator::VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
332 VisitDeclContext(D->getDeclContext());
333 if (!IgnoreResults)
334 Out << "@NA@" << D->getName();
335}
336
337void USRGenerator::VisitObjCMethodDecl(const ObjCMethodDecl *D) {
338 const DeclContext *container = D->getDeclContext();
339 if (const ObjCProtocolDecl *pd = dyn_cast<ObjCProtocolDecl>(container)) {
340 Visit(pd);
341 }
342 else {
343 // The USR for a method declared in a class extension or category is based on
344 // the ObjCInterfaceDecl, not the ObjCCategoryDecl.
345 const ObjCInterfaceDecl *ID = D->getClassInterface();
346 if (!ID) {
347 IgnoreResults = true;
348 return;
349 }
350 Visit(ID);
351 }
352 // Ideally we would use 'GenObjCMethod', but this is such a hot path
353 // for Objective-C code that we don't want to use
354 // DeclarationName::getAsString().
355 Out << (D->isInstanceMethod() ? "(im)" : "(cm)")
356 << DeclarationName(D->getSelector());
357}
358
359void USRGenerator::VisitObjCContainerDecl(const ObjCContainerDecl *D) {
360 switch (D->getKind()) {
361 default:
362 llvm_unreachable("Invalid ObjC container.");
363 case Decl::ObjCInterface:
364 case Decl::ObjCImplementation:
365 GenObjCClass(D->getName());
366 break;
367 case Decl::ObjCCategory: {
368 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
369 const ObjCInterfaceDecl *ID = CD->getClassInterface();
370 if (!ID) {
371 // Handle invalid code where the @interface might not
372 // have been specified.
373 // FIXME: We should be able to generate this USR even if the
374 // @interface isn't available.
375 IgnoreResults = true;
376 return;
377 }
378 // Specially handle class extensions, which are anonymous categories.
379 // We want to mangle in the location to uniquely distinguish them.
380 if (CD->IsClassExtension()) {
381 Out << "objc(ext)" << ID->getName() << '@';
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000382 GenLoc(CD, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000383 }
384 else
385 GenObjCCategory(ID->getName(), CD->getName());
386
387 break;
388 }
389 case Decl::ObjCCategoryImpl: {
390 const ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
391 const ObjCInterfaceDecl *ID = CD->getClassInterface();
392 if (!ID) {
393 // Handle invalid code where the @interface might not
394 // have been specified.
395 // FIXME: We should be able to generate this USR even if the
396 // @interface isn't available.
397 IgnoreResults = true;
398 return;
399 }
400 GenObjCCategory(ID->getName(), CD->getName());
401 break;
402 }
403 case Decl::ObjCProtocol:
404 GenObjCProtocol(cast<ObjCProtocolDecl>(D)->getName());
405 break;
406 }
407}
408
409void USRGenerator::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
410 // The USR for a property declared in a class extension or category is based
411 // on the ObjCInterfaceDecl, not the ObjCCategoryDecl.
412 if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D))
413 Visit(ID);
414 else
415 Visit(cast<Decl>(D->getDeclContext()));
Argyrios Kyrtzidisd9849a92016-07-15 22:18:19 +0000416 GenObjCProperty(D->getName(), D->isClassProperty());
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000417}
418
419void USRGenerator::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
420 if (ObjCPropertyDecl *PD = D->getPropertyDecl()) {
421 VisitObjCPropertyDecl(PD);
422 return;
423 }
424
425 IgnoreResults = true;
426}
427
428void USRGenerator::VisitTagDecl(const TagDecl *D) {
429 // Add the location of the tag decl to handle resolution across
430 // translation units.
Argyrios Kyrtzidis73321062016-03-04 07:17:53 +0000431 if (!isa<EnumDecl>(D) &&
432 ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000433 return;
434
435 D = D->getCanonicalDecl();
436 VisitDeclContext(D->getDeclContext());
437
438 bool AlreadyStarted = false;
439 if (const CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(D)) {
440 if (ClassTemplateDecl *ClassTmpl = CXXRecord->getDescribedClassTemplate()) {
441 AlreadyStarted = true;
442
443 switch (D->getTagKind()) {
444 case TTK_Interface:
Argyrios Kyrtzidisf66cef72014-12-08 08:48:33 +0000445 case TTK_Class:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000446 case TTK_Struct: Out << "@ST"; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000447 case TTK_Union: Out << "@UT"; break;
448 case TTK_Enum: llvm_unreachable("enum template");
449 }
450 VisitTemplateParameterList(ClassTmpl->getTemplateParameters());
451 } else if (const ClassTemplatePartialSpecializationDecl *PartialSpec
452 = dyn_cast<ClassTemplatePartialSpecializationDecl>(CXXRecord)) {
453 AlreadyStarted = true;
454
455 switch (D->getTagKind()) {
456 case TTK_Interface:
Argyrios Kyrtzidisf66cef72014-12-08 08:48:33 +0000457 case TTK_Class:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000458 case TTK_Struct: Out << "@SP"; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000459 case TTK_Union: Out << "@UP"; break;
460 case TTK_Enum: llvm_unreachable("enum partial specialization");
461 }
462 VisitTemplateParameterList(PartialSpec->getTemplateParameters());
463 }
464 }
465
466 if (!AlreadyStarted) {
467 switch (D->getTagKind()) {
468 case TTK_Interface:
Argyrios Kyrtzidisf66cef72014-12-08 08:48:33 +0000469 case TTK_Class:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000470 case TTK_Struct: Out << "@S"; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000471 case TTK_Union: Out << "@U"; break;
472 case TTK_Enum: Out << "@E"; break;
473 }
474 }
475
476 Out << '@';
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000477 assert(Buf.size() > 0);
478 const unsigned off = Buf.size() - 1;
479
480 if (EmitDeclName(D)) {
481 if (const TypedefNameDecl *TD = D->getTypedefNameForAnonDecl()) {
482 Buf[off] = 'A';
483 Out << '@' << *TD;
484 }
Argyrios Kyrtzidis80537a42014-12-08 08:48:37 +0000485 else {
486 if (D->isEmbeddedInDeclarator() && !D->isFreeStanding()) {
487 printLoc(Out, D->getLocation(), Context->getSourceManager(), true);
Argyrios Kyrtzidis73321062016-03-04 07:17:53 +0000488 } else {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000489 Buf[off] = 'a';
Argyrios Kyrtzidis73321062016-03-04 07:17:53 +0000490 if (auto *ED = dyn_cast<EnumDecl>(D)) {
491 // Distinguish USRs of anonymous enums by using their first enumerator.
492 auto enum_range = ED->enumerators();
493 if (enum_range.begin() != enum_range.end()) {
494 Out << '@' << **enum_range.begin();
495 }
496 }
497 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000498 }
Argyrios Kyrtzidis80537a42014-12-08 08:48:37 +0000499 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000500
501 // For a class template specialization, mangle the template arguments.
502 if (const ClassTemplateSpecializationDecl *Spec
503 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
504 const TemplateArgumentList &Args = Spec->getTemplateInstantiationArgs();
505 Out << '>';
506 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
507 Out << '#';
508 VisitTemplateArgument(Args.get(I));
509 }
510 }
511}
512
513void USRGenerator::VisitTypedefDecl(const TypedefDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000514 if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000515 return;
516 const DeclContext *DC = D->getDeclContext();
517 if (const NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
518 Visit(DCN);
519 Out << "@T@";
520 Out << D->getName();
521}
522
523void USRGenerator::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000524 GenLoc(D, /*IncludeOffset=*/true);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000525}
526
Argyrios Kyrtzidisd3ba4102014-02-23 18:23:29 +0000527bool USRGenerator::GenLoc(const Decl *D, bool IncludeOffset) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000528 if (generatedLoc)
529 return IgnoreResults;
530 generatedLoc = true;
Dmitri Gribenko237769e2014-03-28 22:21:26 +0000531
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000532 // Guard against null declarations in invalid code.
533 if (!D) {
534 IgnoreResults = true;
535 return true;
536 }
537
538 // Use the location of canonical decl.
539 D = D->getCanonicalDecl();
540
Dmitri Gribenko237769e2014-03-28 22:21:26 +0000541 IgnoreResults =
542 IgnoreResults || printLoc(Out, D->getLocStart(),
543 Context->getSourceManager(), IncludeOffset);
544
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000545 return IgnoreResults;
546}
547
548void USRGenerator::VisitType(QualType T) {
549 // This method mangles in USR information for types. It can possibly
550 // just reuse the naming-mangling logic used by codegen, although the
551 // requirements for USRs might not be the same.
552 ASTContext &Ctx = *Context;
553
554 do {
555 T = Ctx.getCanonicalType(T);
556 Qualifiers Q = T.getQualifiers();
557 unsigned qVal = 0;
558 if (Q.hasConst())
559 qVal |= 0x1;
560 if (Q.hasVolatile())
561 qVal |= 0x2;
562 if (Q.hasRestrict())
563 qVal |= 0x4;
564 if(qVal)
565 Out << ((char) ('0' + qVal));
566
567 // Mangle in ObjC GC qualifiers?
568
569 if (const PackExpansionType *Expansion = T->getAs<PackExpansionType>()) {
570 Out << 'P';
571 T = Expansion->getPattern();
572 }
573
574 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
575 unsigned char c = '\0';
576 switch (BT->getKind()) {
577 case BuiltinType::Void:
578 c = 'v'; break;
579 case BuiltinType::Bool:
580 c = 'b'; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000581 case BuiltinType::UChar:
582 c = 'c'; break;
583 case BuiltinType::Char16:
584 c = 'q'; break;
585 case BuiltinType::Char32:
586 c = 'w'; break;
587 case BuiltinType::UShort:
588 c = 's'; break;
589 case BuiltinType::UInt:
590 c = 'i'; break;
591 case BuiltinType::ULong:
592 c = 'l'; break;
593 case BuiltinType::ULongLong:
594 c = 'k'; break;
595 case BuiltinType::UInt128:
596 c = 'j'; break;
Argyrios Kyrtzidis56af7e62014-12-08 09:09:05 +0000597 case BuiltinType::Char_U:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000598 case BuiltinType::Char_S:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000599 c = 'C'; break;
Argyrios Kyrtzidisca044542014-12-08 08:48:17 +0000600 case BuiltinType::SChar:
601 c = 'r'; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000602 case BuiltinType::WChar_S:
603 case BuiltinType::WChar_U:
604 c = 'W'; break;
605 case BuiltinType::Short:
606 c = 'S'; break;
607 case BuiltinType::Int:
608 c = 'I'; break;
609 case BuiltinType::Long:
610 c = 'L'; break;
611 case BuiltinType::LongLong:
612 c = 'K'; break;
613 case BuiltinType::Int128:
614 c = 'J'; break;
615 case BuiltinType::Half:
616 c = 'h'; break;
617 case BuiltinType::Float:
618 c = 'f'; break;
619 case BuiltinType::Double:
620 c = 'd'; break;
621 case BuiltinType::LongDouble:
622 c = 'D'; break;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000623 case BuiltinType::Float128:
624 c = 'Q'; break;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000625 case BuiltinType::NullPtr:
626 c = 'n'; break;
627#define BUILTIN_TYPE(Id, SingletonId)
628#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
629#include "clang/AST/BuiltinTypes.def"
630 case BuiltinType::Dependent:
Alexey Bader954ba212016-04-08 13:40:33 +0000631#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
632 case BuiltinType::Id:
Alexey Baderb62f1442016-04-13 08:33:41 +0000633#include "clang/Basic/OpenCLImageTypes.def"
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000634 case BuiltinType::OCLEvent:
Alexey Bader9c8453f2015-09-15 11:18:52 +0000635 case BuiltinType::OCLClkEvent:
636 case BuiltinType::OCLQueue:
637 case BuiltinType::OCLNDRange:
638 case BuiltinType::OCLReserveID:
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000639 case BuiltinType::OCLSampler:
640 IgnoreResults = true;
641 return;
642 case BuiltinType::ObjCId:
643 c = 'o'; break;
644 case BuiltinType::ObjCClass:
645 c = 'O'; break;
646 case BuiltinType::ObjCSel:
647 c = 'e'; break;
648 }
649 Out << c;
650 return;
651 }
652
653 // If we have already seen this (non-built-in) type, use a substitution
654 // encoding.
655 llvm::DenseMap<const Type *, unsigned>::iterator Substitution
656 = TypeSubstitutions.find(T.getTypePtr());
657 if (Substitution != TypeSubstitutions.end()) {
658 Out << 'S' << Substitution->second << '_';
659 return;
660 } else {
661 // Record this as a substitution.
662 unsigned Number = TypeSubstitutions.size();
663 TypeSubstitutions[T.getTypePtr()] = Number;
664 }
665
666 if (const PointerType *PT = T->getAs<PointerType>()) {
667 Out << '*';
668 T = PT->getPointeeType();
669 continue;
670 }
Argyrios Kyrtzidis9c998672016-03-04 07:17:43 +0000671 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
672 Out << '*';
673 T = OPT->getPointeeType();
674 continue;
675 }
Argyrios Kyrtzidis0e387dc2014-12-08 08:48:27 +0000676 if (const RValueReferenceType *RT = T->getAs<RValueReferenceType>()) {
677 Out << "&&";
678 T = RT->getPointeeType();
679 continue;
680 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000681 if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
682 Out << '&';
683 T = RT->getPointeeType();
684 continue;
685 }
686 if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) {
687 Out << 'F';
Alp Toker314cc812014-01-25 16:55:45 +0000688 VisitType(FT->getReturnType());
Aaron Ballman40bd0aa2014-03-17 15:23:01 +0000689 for (const auto &I : FT->param_types())
690 VisitType(I);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000691 if (FT->isVariadic())
692 Out << '.';
693 return;
694 }
695 if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) {
696 Out << 'B';
697 T = BT->getPointeeType();
698 continue;
699 }
700 if (const ComplexType *CT = T->getAs<ComplexType>()) {
701 Out << '<';
702 T = CT->getElementType();
703 continue;
704 }
705 if (const TagType *TT = T->getAs<TagType>()) {
706 Out << '$';
707 VisitTagDecl(TT->getDecl());
708 return;
709 }
Argyrios Kyrtzidis9c998672016-03-04 07:17:43 +0000710 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
711 Out << '$';
712 VisitObjCInterfaceDecl(OIT->getDecl());
713 return;
714 }
715 if (const ObjCObjectType *OIT = T->getAs<ObjCObjectType>()) {
716 Out << 'Q';
717 VisitType(OIT->getBaseType());
718 for (auto *Prot : OIT->getProtocols())
719 VisitObjCProtocolDecl(Prot);
720 return;
721 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000722 if (const TemplateTypeParmType *TTP = T->getAs<TemplateTypeParmType>()) {
723 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
724 return;
725 }
726 if (const TemplateSpecializationType *Spec
727 = T->getAs<TemplateSpecializationType>()) {
728 Out << '>';
729 VisitTemplateName(Spec->getTemplateName());
730 Out << Spec->getNumArgs();
731 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
732 VisitTemplateArgument(Spec->getArg(I));
733 return;
734 }
Argyrios Kyrtzidisd06ce402014-12-08 08:48:11 +0000735 if (const DependentNameType *DNT = T->getAs<DependentNameType>()) {
736 Out << '^';
737 // FIXME: Encode the qualifier, don't just print it.
738 PrintingPolicy PO(Ctx.getLangOpts());
739 PO.SuppressTagKeyword = true;
740 PO.SuppressUnwrittenScope = true;
741 PO.ConstantArraySizeAsWritten = false;
742 PO.AnonymousTagLocations = false;
743 DNT->getQualifier()->print(Out, PO);
744 Out << ':' << DNT->getIdentifier()->getName();
745 return;
746 }
Argyrios Kyrtzidis1d5e5422014-12-08 08:48:43 +0000747 if (const InjectedClassNameType *InjT = T->getAs<InjectedClassNameType>()) {
748 T = InjT->getInjectedSpecializationType();
749 continue;
750 }
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000751
752 // Unhandled type.
753 Out << ' ';
754 break;
755 } while (true);
756}
757
758void USRGenerator::VisitTemplateParameterList(
759 const TemplateParameterList *Params) {
760 if (!Params)
761 return;
762 Out << '>' << Params->size();
763 for (TemplateParameterList::const_iterator P = Params->begin(),
764 PEnd = Params->end();
765 P != PEnd; ++P) {
766 Out << '#';
767 if (isa<TemplateTypeParmDecl>(*P)) {
768 if (cast<TemplateTypeParmDecl>(*P)->isParameterPack())
769 Out<< 'p';
770 Out << 'T';
771 continue;
772 }
773
774 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
775 if (NTTP->isParameterPack())
776 Out << 'p';
777 Out << 'N';
778 VisitType(NTTP->getType());
779 continue;
780 }
781
782 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
783 if (TTP->isParameterPack())
784 Out << 'p';
785 Out << 't';
786 VisitTemplateParameterList(TTP->getTemplateParameters());
787 }
788}
789
790void USRGenerator::VisitTemplateName(TemplateName Name) {
791 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
792 if (TemplateTemplateParmDecl *TTP
793 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
794 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
795 return;
796 }
797
798 Visit(Template);
799 return;
800 }
801
802 // FIXME: Visit dependent template names.
803}
804
805void USRGenerator::VisitTemplateArgument(const TemplateArgument &Arg) {
806 switch (Arg.getKind()) {
807 case TemplateArgument::Null:
808 break;
809
810 case TemplateArgument::Declaration:
811 Visit(Arg.getAsDecl());
812 break;
813
814 case TemplateArgument::NullPtr:
815 break;
816
817 case TemplateArgument::TemplateExpansion:
818 Out << 'P'; // pack expansion of...
819 // Fall through
820 case TemplateArgument::Template:
821 VisitTemplateName(Arg.getAsTemplateOrTemplatePattern());
822 break;
823
824 case TemplateArgument::Expression:
825 // FIXME: Visit expressions.
826 break;
827
828 case TemplateArgument::Pack:
829 Out << 'p' << Arg.pack_size();
Aaron Ballman2a89e852014-07-15 21:32:31 +0000830 for (const auto &P : Arg.pack_elements())
831 VisitTemplateArgument(P);
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000832 break;
833
834 case TemplateArgument::Type:
835 VisitType(Arg.getAsType());
836 break;
837
838 case TemplateArgument::Integral:
839 Out << 'V';
840 VisitType(Arg.getIntegralType());
841 Out << Arg.getAsIntegral();
842 break;
843 }
844}
845
846//===----------------------------------------------------------------------===//
847// USR generation functions.
848//===----------------------------------------------------------------------===//
849
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +0000850void clang::index::generateUSRForObjCClass(StringRef Cls, raw_ostream &OS) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000851 OS << "objc(cs)" << Cls;
852}
853
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +0000854void clang::index::generateUSRForObjCCategory(StringRef Cls, StringRef Cat,
855 raw_ostream &OS) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000856 OS << "objc(cy)" << Cls << '@' << Cat;
857}
858
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +0000859void clang::index::generateUSRForObjCIvar(StringRef Ivar, raw_ostream &OS) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000860 OS << '@' << Ivar;
861}
862
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +0000863void clang::index::generateUSRForObjCMethod(StringRef Sel,
864 bool IsInstanceMethod,
865 raw_ostream &OS) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000866 OS << (IsInstanceMethod ? "(im)" : "(cm)") << Sel;
867}
868
Argyrios Kyrtzidisd9849a92016-07-15 22:18:19 +0000869void clang::index::generateUSRForObjCProperty(StringRef Prop, bool isClassProp,
870 raw_ostream &OS) {
871 OS << (isClassProp ? "(cpy)" : "(py)") << Prop;
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000872}
873
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +0000874void clang::index::generateUSRForObjCProtocol(StringRef Prot, raw_ostream &OS) {
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000875 OS << "objc(pl)" << Prot;
876}
877
Argyrios Kyrtzidis5234b492013-08-21 00:49:25 +0000878bool clang::index::generateUSRForDecl(const Decl *D,
879 SmallVectorImpl<char> &Buf) {
Argyrios Kyrtzidisf12918d2016-11-02 23:42:33 +0000880 if (!D)
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000881 return true;
Argyrios Kyrtzidisf12918d2016-11-02 23:42:33 +0000882 // We don't ignore decls with invalid source locations. Implicit decls, like
883 // C++'s operator new function, can have invalid locations but it is fine to
884 // create USRs that can identify them.
Argyrios Kyrtzidis4b2b4602013-08-16 18:17:55 +0000885
886 USRGenerator UG(&D->getASTContext(), Buf);
887 UG.Visit(D);
888 return UG.ignoreResults();
889}
Dmitri Gribenko237769e2014-03-28 22:21:26 +0000890
Richard Smith66a81862015-05-04 02:25:31 +0000891bool clang::index::generateUSRForMacro(const MacroDefinitionRecord *MD,
Dmitri Gribenko237769e2014-03-28 22:21:26 +0000892 const SourceManager &SM,
893 SmallVectorImpl<char> &Buf) {
894 // Don't generate USRs for things with invalid locations.
895 if (!MD || MD->getLocation().isInvalid())
896 return true;
897
898 llvm::raw_svector_ostream Out(Buf);
899
900 // Assume that system headers are sane. Don't put source location
901 // information into the USR if the macro comes from a system header.
902 SourceLocation Loc = MD->getLocation();
903 bool ShouldGenerateLocation = !SM.isInSystemHeader(Loc);
904
905 Out << getUSRSpacePrefix();
906 if (ShouldGenerateLocation)
907 printLoc(Out, Loc, SM, /*IncludeOffset=*/true);
908 Out << "@macro@";
Alp Toker541d5072014-06-07 23:30:53 +0000909 Out << MD->getName()->getName();
Dmitri Gribenko237769e2014-03-28 22:21:26 +0000910 return false;
911}