blob: c6f4d7c248702dc50b19545eff065da4f88a4e1f [file] [log] [blame]
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001//===- CIndexUSR.cpp - Clang-C Source Indexing Library --------------------===//
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//
10// This file implements the generation and use of USRs from CXEntities.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CIndexer.h"
15#include "CXCursor.h"
16#include "CXString.h"
17#include "clang/AST/DeclTemplate.h"
18#include "clang/AST/DeclVisitor.h"
19#include "clang/Frontend/ASTUnit.h"
20#include "clang/Lex/PreprocessingRecord.h"
21#include "llvm/ADT/SmallString.h"
22#include "llvm/Support/raw_ostream.h"
23
24using namespace clang;
Guy Benyei7f92f2d2012-12-18 14:30:41 +000025
26//===----------------------------------------------------------------------===//
27// USR generation.
28//===----------------------------------------------------------------------===//
29
30namespace {
Dmitri Gribenkoe414cc12013-02-03 13:21:23 +000031class USRGenerator : public ConstDeclVisitor<USRGenerator> {
Guy Benyei7f92f2d2012-12-18 14:30:41 +000032 OwningPtr<SmallString<128> > OwnedBuf;
33 SmallVectorImpl<char> &Buf;
34 llvm::raw_svector_ostream Out;
35 bool IgnoreResults;
36 ASTContext *Context;
37 bool generatedLoc;
38
39 llvm::DenseMap<const Type *, unsigned> TypeSubstitutions;
40
41public:
42 explicit USRGenerator(ASTContext *Ctx = 0, SmallVectorImpl<char> *extBuf = 0)
43 : OwnedBuf(extBuf ? 0 : new SmallString<128>()),
44 Buf(extBuf ? *extBuf : *OwnedBuf.get()),
45 Out(Buf),
46 IgnoreResults(false),
47 Context(Ctx),
48 generatedLoc(false)
49 {
50 // Add the USR space prefix.
51 Out << "c:";
52 }
53
54 StringRef str() {
55 return Out.str();
56 }
57
58 USRGenerator* operator->() { return this; }
59
60 template <typename T>
61 llvm::raw_svector_ostream &operator<<(const T &x) {
62 Out << x;
63 return Out;
64 }
65
66 bool ignoreResults() const { return IgnoreResults; }
67
68 // Visitation methods from generating USRs from AST elements.
Dmitri Gribenkoe414cc12013-02-03 13:21:23 +000069 void VisitDeclContext(const DeclContext *D);
70 void VisitFieldDecl(const FieldDecl *D);
71 void VisitFunctionDecl(const FunctionDecl *D);
72 void VisitNamedDecl(const NamedDecl *D);
73 void VisitNamespaceDecl(const NamespaceDecl *D);
74 void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D);
75 void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D);
76 void VisitClassTemplateDecl(const ClassTemplateDecl *D);
77 void VisitObjCContainerDecl(const ObjCContainerDecl *CD);
78 void VisitObjCMethodDecl(const ObjCMethodDecl *MD);
79 void VisitObjCPropertyDecl(const ObjCPropertyDecl *D);
80 void VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D);
81 void VisitTagDecl(const TagDecl *D);
82 void VisitTypedefDecl(const TypedefDecl *D);
83 void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D);
84 void VisitVarDecl(const VarDecl *D);
85 void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D);
86 void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D);
87 void VisitLinkageSpecDecl(const LinkageSpecDecl *D) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +000088 IgnoreResults = true;
89 }
Dmitri Gribenkoe414cc12013-02-03 13:21:23 +000090 void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +000091 IgnoreResults = true;
92 }
Dmitri Gribenkoe414cc12013-02-03 13:21:23 +000093 void VisitUsingDecl(const UsingDecl *D) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +000094 IgnoreResults = true;
95 }
Dmitri Gribenkoe414cc12013-02-03 13:21:23 +000096 void VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +000097 IgnoreResults = true;
98 }
Dmitri Gribenkoe414cc12013-02-03 13:21:23 +000099 void VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000100 IgnoreResults = true;
101 }
102
103 /// Generate the string component containing the location of the
104 /// declaration.
105 bool GenLoc(const Decl *D);
106
107 /// String generation methods used both by the visitation methods
108 /// and from other clients that want to directly generate USRs. These
109 /// methods do not construct complete USRs (which incorporate the parents
110 /// of an AST element), but only the fragments concerning the AST element
111 /// itself.
112
113 /// Generate a USR for an Objective-C class.
114 void GenObjCClass(StringRef cls);
115 /// Generate a USR for an Objective-C class category.
116 void GenObjCCategory(StringRef cls, StringRef cat);
117 /// Generate a USR fragment for an Objective-C instance variable. The
118 /// complete USR can be created by concatenating the USR for the
119 /// encompassing class with this USR fragment.
120 void GenObjCIvar(StringRef ivar);
121 /// Generate a USR fragment for an Objective-C method.
122 void GenObjCMethod(StringRef sel, bool isInstanceMethod);
123 /// Generate a USR fragment for an Objective-C property.
124 void GenObjCProperty(StringRef prop);
125 /// Generate a USR for an Objective-C protocol.
126 void GenObjCProtocol(StringRef prot);
127
128 void VisitType(QualType T);
129 void VisitTemplateParameterList(const TemplateParameterList *Params);
130 void VisitTemplateName(TemplateName Name);
131 void VisitTemplateArgument(const TemplateArgument &Arg);
132
133 /// Emit a Decl's name using NamedDecl::printName() and return true if
134 /// the decl had no name.
135 bool EmitDeclName(const NamedDecl *D);
136};
137
138} // end anonymous namespace
139
140//===----------------------------------------------------------------------===//
141// Generating USRs from ASTS.
142//===----------------------------------------------------------------------===//
143
144bool USRGenerator::EmitDeclName(const NamedDecl *D) {
145 Out.flush();
146 const unsigned startSize = Buf.size();
147 D->printName(Out);
148 Out.flush();
149 const unsigned endSize = Buf.size();
150 return startSize == endSize;
151}
152
153static inline bool ShouldGenerateLocation(const NamedDecl *D) {
154 return D->getLinkage() != ExternalLinkage;
155}
156
Dmitri Gribenkoe414cc12013-02-03 13:21:23 +0000157void USRGenerator::VisitDeclContext(const DeclContext *DC) {
158 if (const NamedDecl *D = dyn_cast<NamedDecl>(DC))
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000159 Visit(D);
160}
161
Dmitri Gribenkoe414cc12013-02-03 13:21:23 +0000162void USRGenerator::VisitFieldDecl(const FieldDecl *D) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000163 // The USR for an ivar declared in a class extension is based on the
164 // ObjCInterfaceDecl, not the ObjCCategoryDecl.
Dmitri Gribenkoe414cc12013-02-03 13:21:23 +0000165 if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D))
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000166 Visit(ID);
167 else
168 VisitDeclContext(D->getDeclContext());
169 Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@");
170 if (EmitDeclName(D)) {
171 // Bit fields can be anonymous.
172 IgnoreResults = true;
173 return;
174 }
175}
176
Dmitri Gribenkoe414cc12013-02-03 13:21:23 +0000177void USRGenerator::VisitFunctionDecl(const FunctionDecl *D) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000178 if (ShouldGenerateLocation(D) && GenLoc(D))
179 return;
180
181 VisitDeclContext(D->getDeclContext());
182 if (FunctionTemplateDecl *FunTmpl = D->getDescribedFunctionTemplate()) {
183 Out << "@FT@";
184 VisitTemplateParameterList(FunTmpl->getTemplateParameters());
185 } else
186 Out << "@F@";
187 D->printName(Out);
188
189 ASTContext &Ctx = *Context;
190 if (!Ctx.getLangOpts().CPlusPlus || D->isExternC())
191 return;
192
193 if (const TemplateArgumentList *
194 SpecArgs = D->getTemplateSpecializationArgs()) {
195 Out << '<';
196 for (unsigned I = 0, N = SpecArgs->size(); I != N; ++I) {
197 Out << '#';
198 VisitTemplateArgument(SpecArgs->get(I));
199 }
200 Out << '>';
201 }
202
203 // Mangle in type information for the arguments.
Dmitri Gribenkoe414cc12013-02-03 13:21:23 +0000204 for (FunctionDecl::param_const_iterator I = D->param_begin(),
205 E = D->param_end();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000206 I != E; ++I) {
207 Out << '#';
208 if (ParmVarDecl *PD = *I)
209 VisitType(PD->getType());
210 }
211 if (D->isVariadic())
212 Out << '.';
213 Out << '#';
Dmitri Gribenkoe414cc12013-02-03 13:21:23 +0000214 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000215 if (MD->isStatic())
216 Out << 'S';
217 if (unsigned quals = MD->getTypeQualifiers())
218 Out << (char)('0' + quals);
219 }
220}
221
Dmitri Gribenkoe414cc12013-02-03 13:21:23 +0000222void USRGenerator::VisitNamedDecl(const NamedDecl *D) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000223 VisitDeclContext(D->getDeclContext());
224 Out << "@";
225
226 if (EmitDeclName(D)) {
227 // The string can be empty if the declaration has no name; e.g., it is
228 // the ParmDecl with no name for declaration of a function pointer type,
229 // e.g.: void (*f)(void *);
230 // In this case, don't generate a USR.
231 IgnoreResults = true;
232 }
233}
234
Dmitri Gribenkoe414cc12013-02-03 13:21:23 +0000235void USRGenerator::VisitVarDecl(const VarDecl *D) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000236 // VarDecls can be declared 'extern' within a function or method body,
237 // but their enclosing DeclContext is the function, not the TU. We need
238 // to check the storage class to correctly generate the USR.
239 if (ShouldGenerateLocation(D) && GenLoc(D))
240 return;
241
242 VisitDeclContext(D->getDeclContext());
243
244 // Variables always have simple names.
245 StringRef s = D->getName();
246
247 // The string can be empty if the declaration has no name; e.g., it is
248 // the ParmDecl with no name for declaration of a function pointer type, e.g.:
249 // void (*f)(void *);
250 // In this case, don't generate a USR.
251 if (s.empty())
252 IgnoreResults = true;
253 else
254 Out << '@' << s;
255}
256
Dmitri Gribenkoe414cc12013-02-03 13:21:23 +0000257void USRGenerator::VisitNonTypeTemplateParmDecl(
258 const NonTypeTemplateParmDecl *D) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000259 GenLoc(D);
260 return;
261}
262
Dmitri Gribenkoe414cc12013-02-03 13:21:23 +0000263void USRGenerator::VisitTemplateTemplateParmDecl(
264 const TemplateTemplateParmDecl *D) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000265 GenLoc(D);
266 return;
267}
268
Dmitri Gribenkoe414cc12013-02-03 13:21:23 +0000269void USRGenerator::VisitNamespaceDecl(const NamespaceDecl *D) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000270 if (D->isAnonymousNamespace()) {
271 Out << "@aN";
272 return;
273 }
274
275 VisitDeclContext(D->getDeclContext());
276 if (!IgnoreResults)
277 Out << "@N@" << D->getName();
278}
279
Dmitri Gribenkoe414cc12013-02-03 13:21:23 +0000280void USRGenerator::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000281 VisitFunctionDecl(D->getTemplatedDecl());
282}
283
Dmitri Gribenkoe414cc12013-02-03 13:21:23 +0000284void USRGenerator::VisitClassTemplateDecl(const ClassTemplateDecl *D) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000285 VisitTagDecl(D->getTemplatedDecl());
286}
287
Dmitri Gribenkoe414cc12013-02-03 13:21:23 +0000288void USRGenerator::VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000289 VisitDeclContext(D->getDeclContext());
290 if (!IgnoreResults)
291 Out << "@NA@" << D->getName();
292}
293
Dmitri Gribenkoe414cc12013-02-03 13:21:23 +0000294void USRGenerator::VisitObjCMethodDecl(const ObjCMethodDecl *D) {
295 const DeclContext *container = D->getDeclContext();
296 if (const ObjCProtocolDecl *pd = dyn_cast<ObjCProtocolDecl>(container)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000297 Visit(pd);
298 }
299 else {
300 // The USR for a method declared in a class extension or category is based on
301 // the ObjCInterfaceDecl, not the ObjCCategoryDecl.
Dmitri Gribenkoe414cc12013-02-03 13:21:23 +0000302 const ObjCInterfaceDecl *ID = D->getClassInterface();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000303 if (!ID) {
304 IgnoreResults = true;
305 return;
306 }
307 Visit(ID);
308 }
309 // Ideally we would use 'GenObjCMethod', but this is such a hot path
310 // for Objective-C code that we don't want to use
311 // DeclarationName::getAsString().
312 Out << (D->isInstanceMethod() ? "(im)" : "(cm)");
313 DeclarationName N(D->getSelector());
314 N.printName(Out);
315}
316
Dmitri Gribenkoe414cc12013-02-03 13:21:23 +0000317void USRGenerator::VisitObjCContainerDecl(const ObjCContainerDecl *D) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000318 switch (D->getKind()) {
319 default:
320 llvm_unreachable("Invalid ObjC container.");
321 case Decl::ObjCInterface:
322 case Decl::ObjCImplementation:
323 GenObjCClass(D->getName());
324 break;
325 case Decl::ObjCCategory: {
Dmitri Gribenkoe414cc12013-02-03 13:21:23 +0000326 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
327 const ObjCInterfaceDecl *ID = CD->getClassInterface();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000328 if (!ID) {
329 // Handle invalid code where the @interface might not
330 // have been specified.
331 // FIXME: We should be able to generate this USR even if the
332 // @interface isn't available.
333 IgnoreResults = true;
334 return;
335 }
336 // Specially handle class extensions, which are anonymous categories.
337 // We want to mangle in the location to uniquely distinguish them.
338 if (CD->IsClassExtension()) {
339 Out << "objc(ext)" << ID->getName() << '@';
340 GenLoc(CD);
341 }
342 else
343 GenObjCCategory(ID->getName(), CD->getName());
344
345 break;
346 }
347 case Decl::ObjCCategoryImpl: {
Dmitri Gribenkoe414cc12013-02-03 13:21:23 +0000348 const ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
349 const ObjCInterfaceDecl *ID = CD->getClassInterface();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000350 if (!ID) {
351 // Handle invalid code where the @interface might not
352 // have been specified.
353 // FIXME: We should be able to generate this USR even if the
354 // @interface isn't available.
355 IgnoreResults = true;
356 return;
357 }
358 GenObjCCategory(ID->getName(), CD->getName());
359 break;
360 }
361 case Decl::ObjCProtocol:
362 GenObjCProtocol(cast<ObjCProtocolDecl>(D)->getName());
363 break;
364 }
365}
366
Dmitri Gribenkoe414cc12013-02-03 13:21:23 +0000367void USRGenerator::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000368 // The USR for a property declared in a class extension or category is based
369 // on the ObjCInterfaceDecl, not the ObjCCategoryDecl.
Dmitri Gribenkoe414cc12013-02-03 13:21:23 +0000370 if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D))
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000371 Visit(ID);
372 else
373 Visit(cast<Decl>(D->getDeclContext()));
374 GenObjCProperty(D->getName());
375}
376
Dmitri Gribenkoe414cc12013-02-03 13:21:23 +0000377void USRGenerator::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000378 if (ObjCPropertyDecl *PD = D->getPropertyDecl()) {
379 VisitObjCPropertyDecl(PD);
380 return;
381 }
382
383 IgnoreResults = true;
384}
385
Dmitri Gribenkoe414cc12013-02-03 13:21:23 +0000386void USRGenerator::VisitTagDecl(const TagDecl *D) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000387 // Add the location of the tag decl to handle resolution across
388 // translation units.
389 if (ShouldGenerateLocation(D) && GenLoc(D))
390 return;
391
392 D = D->getCanonicalDecl();
393 VisitDeclContext(D->getDeclContext());
394
395 bool AlreadyStarted = false;
Dmitri Gribenkoe414cc12013-02-03 13:21:23 +0000396 if (const CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(D)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000397 if (ClassTemplateDecl *ClassTmpl = CXXRecord->getDescribedClassTemplate()) {
398 AlreadyStarted = true;
399
400 switch (D->getTagKind()) {
401 case TTK_Interface:
402 case TTK_Struct: Out << "@ST"; break;
403 case TTK_Class: Out << "@CT"; break;
404 case TTK_Union: Out << "@UT"; break;
405 case TTK_Enum: llvm_unreachable("enum template");
406 }
407 VisitTemplateParameterList(ClassTmpl->getTemplateParameters());
Dmitri Gribenkoe414cc12013-02-03 13:21:23 +0000408 } else if (const ClassTemplatePartialSpecializationDecl *PartialSpec
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000409 = dyn_cast<ClassTemplatePartialSpecializationDecl>(CXXRecord)) {
410 AlreadyStarted = true;
411
412 switch (D->getTagKind()) {
413 case TTK_Interface:
414 case TTK_Struct: Out << "@SP"; break;
415 case TTK_Class: Out << "@CP"; break;
416 case TTK_Union: Out << "@UP"; break;
417 case TTK_Enum: llvm_unreachable("enum partial specialization");
418 }
419 VisitTemplateParameterList(PartialSpec->getTemplateParameters());
420 }
421 }
422
423 if (!AlreadyStarted) {
424 switch (D->getTagKind()) {
425 case TTK_Interface:
426 case TTK_Struct: Out << "@S"; break;
427 case TTK_Class: Out << "@C"; break;
428 case TTK_Union: Out << "@U"; break;
429 case TTK_Enum: Out << "@E"; break;
430 }
431 }
432
433 Out << '@';
434 Out.flush();
435 assert(Buf.size() > 0);
436 const unsigned off = Buf.size() - 1;
437
438 if (EmitDeclName(D)) {
439 if (const TypedefNameDecl *TD = D->getTypedefNameForAnonDecl()) {
440 Buf[off] = 'A';
441 Out << '@' << *TD;
442 }
443 else
444 Buf[off] = 'a';
445 }
446
447 // For a class template specialization, mangle the template arguments.
Dmitri Gribenkoe414cc12013-02-03 13:21:23 +0000448 if (const ClassTemplateSpecializationDecl *Spec
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000449 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
450 const TemplateArgumentList &Args = Spec->getTemplateInstantiationArgs();
451 Out << '>';
452 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
453 Out << '#';
454 VisitTemplateArgument(Args.get(I));
455 }
456 }
457}
458
Dmitri Gribenkoe414cc12013-02-03 13:21:23 +0000459void USRGenerator::VisitTypedefDecl(const TypedefDecl *D) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000460 if (ShouldGenerateLocation(D) && GenLoc(D))
461 return;
Dmitri Gribenkoe414cc12013-02-03 13:21:23 +0000462 const DeclContext *DC = D->getDeclContext();
463 if (const NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000464 Visit(DCN);
465 Out << "@T@";
466 Out << D->getName();
467}
468
Dmitri Gribenkoe414cc12013-02-03 13:21:23 +0000469void USRGenerator::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000470 GenLoc(D);
471 return;
472}
473
474bool USRGenerator::GenLoc(const Decl *D) {
475 if (generatedLoc)
476 return IgnoreResults;
477 generatedLoc = true;
478
479 // Guard against null declarations in invalid code.
480 if (!D) {
481 IgnoreResults = true;
482 return true;
483 }
484
485 // Use the location of canonical decl.
486 D = D->getCanonicalDecl();
487
488 const SourceManager &SM = Context->getSourceManager();
489 SourceLocation L = D->getLocStart();
490 if (L.isInvalid()) {
491 IgnoreResults = true;
492 return true;
493 }
494 L = SM.getExpansionLoc(L);
495 const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(L);
496 const FileEntry *FE = SM.getFileEntryForID(Decomposed.first);
497 if (FE) {
498 Out << llvm::sys::path::filename(FE->getName());
499 }
500 else {
501 // This case really isn't interesting.
502 IgnoreResults = true;
503 return true;
504 }
505 // Use the offest into the FileID to represent the location. Using
506 // a line/column can cause us to look back at the original source file,
507 // which is expensive.
508 Out << '@' << Decomposed.second;
509 return IgnoreResults;
510}
511
512void USRGenerator::VisitType(QualType T) {
513 // This method mangles in USR information for types. It can possibly
514 // just reuse the naming-mangling logic used by codegen, although the
515 // requirements for USRs might not be the same.
516 ASTContext &Ctx = *Context;
517
518 do {
519 T = Ctx.getCanonicalType(T);
520 Qualifiers Q = T.getQualifiers();
521 unsigned qVal = 0;
522 if (Q.hasConst())
523 qVal |= 0x1;
524 if (Q.hasVolatile())
525 qVal |= 0x2;
526 if (Q.hasRestrict())
527 qVal |= 0x4;
528 if(qVal)
529 Out << ((char) ('0' + qVal));
530
531 // Mangle in ObjC GC qualifiers?
532
533 if (const PackExpansionType *Expansion = T->getAs<PackExpansionType>()) {
534 Out << 'P';
535 T = Expansion->getPattern();
536 }
537
538 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
539 unsigned char c = '\0';
540 switch (BT->getKind()) {
541 case BuiltinType::Void:
542 c = 'v'; break;
543 case BuiltinType::Bool:
544 c = 'b'; break;
545 case BuiltinType::Char_U:
546 case BuiltinType::UChar:
547 c = 'c'; break;
548 case BuiltinType::Char16:
549 c = 'q'; break;
550 case BuiltinType::Char32:
551 c = 'w'; break;
552 case BuiltinType::UShort:
553 c = 's'; break;
554 case BuiltinType::UInt:
555 c = 'i'; break;
556 case BuiltinType::ULong:
557 c = 'l'; break;
558 case BuiltinType::ULongLong:
559 c = 'k'; break;
560 case BuiltinType::UInt128:
561 c = 'j'; break;
562 case BuiltinType::Char_S:
563 case BuiltinType::SChar:
564 c = 'C'; break;
565 case BuiltinType::WChar_S:
566 case BuiltinType::WChar_U:
567 c = 'W'; break;
568 case BuiltinType::Short:
569 c = 'S'; break;
570 case BuiltinType::Int:
571 c = 'I'; break;
572 case BuiltinType::Long:
573 c = 'L'; break;
574 case BuiltinType::LongLong:
575 c = 'K'; break;
576 case BuiltinType::Int128:
577 c = 'J'; break;
578 case BuiltinType::Half:
579 c = 'h'; break;
580 case BuiltinType::Float:
581 c = 'f'; break;
582 case BuiltinType::Double:
583 c = 'd'; break;
584 case BuiltinType::LongDouble:
585 c = 'D'; break;
586 case BuiltinType::NullPtr:
587 c = 'n'; break;
588#define BUILTIN_TYPE(Id, SingletonId)
589#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
590#include "clang/AST/BuiltinTypes.def"
591 case BuiltinType::Dependent:
Guy Benyeib13621d2012-12-18 14:38:23 +0000592 case BuiltinType::OCLImage1d:
593 case BuiltinType::OCLImage1dArray:
594 case BuiltinType::OCLImage1dBuffer:
595 case BuiltinType::OCLImage2d:
596 case BuiltinType::OCLImage2dArray:
597 case BuiltinType::OCLImage3d:
Guy Benyeie6b9d802013-01-20 12:31:11 +0000598 case BuiltinType::OCLEvent:
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000599 IgnoreResults = true;
600 return;
601 case BuiltinType::ObjCId:
602 c = 'o'; break;
603 case BuiltinType::ObjCClass:
604 c = 'O'; break;
605 case BuiltinType::ObjCSel:
606 c = 'e'; break;
607 }
608 Out << c;
609 return;
610 }
611
612 // If we have already seen this (non-built-in) type, use a substitution
613 // encoding.
614 llvm::DenseMap<const Type *, unsigned>::iterator Substitution
615 = TypeSubstitutions.find(T.getTypePtr());
616 if (Substitution != TypeSubstitutions.end()) {
617 Out << 'S' << Substitution->second << '_';
618 return;
619 } else {
620 // Record this as a substitution.
621 unsigned Number = TypeSubstitutions.size();
622 TypeSubstitutions[T.getTypePtr()] = Number;
623 }
624
625 if (const PointerType *PT = T->getAs<PointerType>()) {
626 Out << '*';
627 T = PT->getPointeeType();
628 continue;
629 }
630 if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
631 Out << '&';
632 T = RT->getPointeeType();
633 continue;
634 }
635 if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) {
636 Out << 'F';
637 VisitType(FT->getResultType());
638 for (FunctionProtoType::arg_type_iterator
639 I = FT->arg_type_begin(), E = FT->arg_type_end(); I!=E; ++I) {
640 VisitType(*I);
641 }
642 if (FT->isVariadic())
643 Out << '.';
644 return;
645 }
646 if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) {
647 Out << 'B';
648 T = BT->getPointeeType();
649 continue;
650 }
651 if (const ComplexType *CT = T->getAs<ComplexType>()) {
652 Out << '<';
653 T = CT->getElementType();
654 continue;
655 }
656 if (const TagType *TT = T->getAs<TagType>()) {
657 Out << '$';
658 VisitTagDecl(TT->getDecl());
659 return;
660 }
661 if (const TemplateTypeParmType *TTP = T->getAs<TemplateTypeParmType>()) {
662 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
663 return;
664 }
665 if (const TemplateSpecializationType *Spec
666 = T->getAs<TemplateSpecializationType>()) {
667 Out << '>';
668 VisitTemplateName(Spec->getTemplateName());
669 Out << Spec->getNumArgs();
670 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
671 VisitTemplateArgument(Spec->getArg(I));
672 return;
673 }
674
675 // Unhandled type.
676 Out << ' ';
677 break;
678 } while (true);
679}
680
681void USRGenerator::VisitTemplateParameterList(
682 const TemplateParameterList *Params) {
683 if (!Params)
684 return;
685 Out << '>' << Params->size();
686 for (TemplateParameterList::const_iterator P = Params->begin(),
687 PEnd = Params->end();
688 P != PEnd; ++P) {
689 Out << '#';
690 if (isa<TemplateTypeParmDecl>(*P)) {
691 if (cast<TemplateTypeParmDecl>(*P)->isParameterPack())
692 Out<< 'p';
693 Out << 'T';
694 continue;
695 }
696
697 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
698 if (NTTP->isParameterPack())
699 Out << 'p';
700 Out << 'N';
701 VisitType(NTTP->getType());
702 continue;
703 }
704
705 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
706 if (TTP->isParameterPack())
707 Out << 'p';
708 Out << 't';
709 VisitTemplateParameterList(TTP->getTemplateParameters());
710 }
711}
712
713void USRGenerator::VisitTemplateName(TemplateName Name) {
714 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
715 if (TemplateTemplateParmDecl *TTP
716 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
717 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
718 return;
719 }
720
721 Visit(Template);
722 return;
723 }
724
725 // FIXME: Visit dependent template names.
726}
727
728void USRGenerator::VisitTemplateArgument(const TemplateArgument &Arg) {
729 switch (Arg.getKind()) {
730 case TemplateArgument::Null:
731 break;
732
733 case TemplateArgument::Declaration:
734 Visit(Arg.getAsDecl());
735 break;
736
737 case TemplateArgument::NullPtr:
738 break;
739
740 case TemplateArgument::TemplateExpansion:
741 Out << 'P'; // pack expansion of...
742 // Fall through
743 case TemplateArgument::Template:
744 VisitTemplateName(Arg.getAsTemplateOrTemplatePattern());
745 break;
746
747 case TemplateArgument::Expression:
748 // FIXME: Visit expressions.
749 break;
750
751 case TemplateArgument::Pack:
752 Out << 'p' << Arg.pack_size();
753 for (TemplateArgument::pack_iterator P = Arg.pack_begin(), PEnd = Arg.pack_end();
754 P != PEnd; ++P)
755 VisitTemplateArgument(*P);
756 break;
757
758 case TemplateArgument::Type:
759 VisitType(Arg.getAsType());
760 break;
761
762 case TemplateArgument::Integral:
763 Out << 'V';
764 VisitType(Arg.getIntegralType());
765 Out << Arg.getAsIntegral();
766 break;
767 }
768}
769
770//===----------------------------------------------------------------------===//
771// General purpose USR generation methods.
772//===----------------------------------------------------------------------===//
773
774void USRGenerator::GenObjCClass(StringRef cls) {
775 Out << "objc(cs)" << cls;
776}
777
778void USRGenerator::GenObjCCategory(StringRef cls, StringRef cat) {
779 Out << "objc(cy)" << cls << '@' << cat;
780}
781
782void USRGenerator::GenObjCIvar(StringRef ivar) {
783 Out << '@' << ivar;
784}
785
786void USRGenerator::GenObjCMethod(StringRef meth, bool isInstanceMethod) {
787 Out << (isInstanceMethod ? "(im)" : "(cm)") << meth;
788}
789
790void USRGenerator::GenObjCProperty(StringRef prop) {
791 Out << "(py)" << prop;
792}
793
794void USRGenerator::GenObjCProtocol(StringRef prot) {
795 Out << "objc(pl)" << prot;
796}
797
798//===----------------------------------------------------------------------===//
799// API hooks.
800//===----------------------------------------------------------------------===//
801
802static inline StringRef extractUSRSuffix(StringRef s) {
803 return s.startswith("c:") ? s.substr(2) : "";
804}
805
806bool cxcursor::getDeclCursorUSR(const Decl *D, SmallVectorImpl<char> &Buf) {
807 // Don't generate USRs for things with invalid locations.
808 if (!D || D->getLocStart().isInvalid())
809 return true;
810
811 USRGenerator UG(&D->getASTContext(), &Buf);
Dmitri Gribenko8d0eb2f2013-02-03 13:26:20 +0000812 UG->Visit(D);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000813
814 if (UG->ignoreResults())
815 return true;
816
817 return false;
818}
819
820extern "C" {
821
822CXString clang_getCursorUSR(CXCursor C) {
823 const CXCursorKind &K = clang_getCursorKind(C);
824
825 if (clang_isDeclaration(K)) {
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +0000826 const Decl *D = cxcursor::getCursorDecl(C);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000827 if (!D)
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +0000828 return cxstring::createEmpty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000829
830 CXTranslationUnit TU = cxcursor::getCursorTU(C);
831 if (!TU)
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +0000832 return cxstring::createEmpty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000833
Dmitri Gribenkoaca3e562013-02-03 13:52:47 +0000834 cxstring::CXStringBuf *buf = cxstring::getCXStringBuf(TU);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000835 if (!buf)
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +0000836 return cxstring::createEmpty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000837
838 bool Ignore = cxcursor::getDeclCursorUSR(D, buf->Data);
839 if (Ignore) {
Dmitri Gribenko9c48d162013-01-26 22:44:19 +0000840 buf->dispose();
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +0000841 return cxstring::createEmpty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000842 }
843
844 // Return the C-string, but don't make a copy since it is already in
845 // the string buffer.
846 buf->Data.push_back('\0');
847 return createCXString(buf);
848 }
849
850 if (K == CXCursor_MacroDefinition) {
851 CXTranslationUnit TU = cxcursor::getCursorTU(C);
852 if (!TU)
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +0000853 return cxstring::createEmpty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000854
Dmitri Gribenkoaca3e562013-02-03 13:52:47 +0000855 cxstring::CXStringBuf *buf = cxstring::getCXStringBuf(TU);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000856 if (!buf)
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +0000857 return cxstring::createEmpty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000858
859 {
860 USRGenerator UG(&cxcursor::getCursorASTUnit(C)->getASTContext(),
861 &buf->Data);
862 UG << "macro@"
863 << cxcursor::getCursorMacroDefinition(C)->getName()->getNameStart();
864 }
865 buf->Data.push_back('\0');
866 return createCXString(buf);
867 }
868
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +0000869 return cxstring::createEmpty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000870}
871
872CXString clang_constructUSR_ObjCIvar(const char *name, CXString classUSR) {
873 USRGenerator UG;
874 UG << extractUSRSuffix(clang_getCString(classUSR));
875 UG->GenObjCIvar(name);
Dmitri Gribenko5595ded2013-02-02 02:19:29 +0000876 return cxstring::createDup(UG.str());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000877}
878
879CXString clang_constructUSR_ObjCMethod(const char *name,
880 unsigned isInstanceMethod,
881 CXString classUSR) {
882 USRGenerator UG;
883 UG << extractUSRSuffix(clang_getCString(classUSR));
884 UG->GenObjCMethod(name, isInstanceMethod);
Dmitri Gribenko5595ded2013-02-02 02:19:29 +0000885 return cxstring::createDup(UG.str());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000886}
887
888CXString clang_constructUSR_ObjCClass(const char *name) {
889 USRGenerator UG;
890 UG->GenObjCClass(name);
Dmitri Gribenko5595ded2013-02-02 02:19:29 +0000891 return cxstring::createDup(UG.str());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000892}
893
894CXString clang_constructUSR_ObjCProtocol(const char *name) {
895 USRGenerator UG;
896 UG->GenObjCProtocol(name);
Dmitri Gribenko5595ded2013-02-02 02:19:29 +0000897 return cxstring::createDup(UG.str());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000898}
899
900CXString clang_constructUSR_ObjCCategory(const char *class_name,
901 const char *category_name) {
902 USRGenerator UG;
903 UG->GenObjCCategory(class_name, category_name);
Dmitri Gribenko5595ded2013-02-02 02:19:29 +0000904 return cxstring::createDup(UG.str());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000905}
906
907CXString clang_constructUSR_ObjCProperty(const char *property,
908 CXString classUSR) {
909 USRGenerator UG;
910 UG << extractUSRSuffix(clang_getCString(classUSR));
911 UG->GenObjCProperty(property);
Dmitri Gribenko5595ded2013-02-02 02:19:29 +0000912 return cxstring::createDup(UG.str());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000913}
914
915} // end extern "C"