blob: a911ce5e952a3b89356f7f93b4a261b1dd621f89 [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 Benyei21f18c42013-02-07 10:55:47 +0000599 case BuiltinType::OCLSampler:
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000600 IgnoreResults = true;
601 return;
602 case BuiltinType::ObjCId:
603 c = 'o'; break;
604 case BuiltinType::ObjCClass:
605 c = 'O'; break;
606 case BuiltinType::ObjCSel:
607 c = 'e'; break;
608 }
609 Out << c;
610 return;
611 }
612
613 // If we have already seen this (non-built-in) type, use a substitution
614 // encoding.
615 llvm::DenseMap<const Type *, unsigned>::iterator Substitution
616 = TypeSubstitutions.find(T.getTypePtr());
617 if (Substitution != TypeSubstitutions.end()) {
618 Out << 'S' << Substitution->second << '_';
619 return;
620 } else {
621 // Record this as a substitution.
622 unsigned Number = TypeSubstitutions.size();
623 TypeSubstitutions[T.getTypePtr()] = Number;
624 }
625
626 if (const PointerType *PT = T->getAs<PointerType>()) {
627 Out << '*';
628 T = PT->getPointeeType();
629 continue;
630 }
631 if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
632 Out << '&';
633 T = RT->getPointeeType();
634 continue;
635 }
636 if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) {
637 Out << 'F';
638 VisitType(FT->getResultType());
639 for (FunctionProtoType::arg_type_iterator
640 I = FT->arg_type_begin(), E = FT->arg_type_end(); I!=E; ++I) {
641 VisitType(*I);
642 }
643 if (FT->isVariadic())
644 Out << '.';
645 return;
646 }
647 if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) {
648 Out << 'B';
649 T = BT->getPointeeType();
650 continue;
651 }
652 if (const ComplexType *CT = T->getAs<ComplexType>()) {
653 Out << '<';
654 T = CT->getElementType();
655 continue;
656 }
657 if (const TagType *TT = T->getAs<TagType>()) {
658 Out << '$';
659 VisitTagDecl(TT->getDecl());
660 return;
661 }
662 if (const TemplateTypeParmType *TTP = T->getAs<TemplateTypeParmType>()) {
663 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
664 return;
665 }
666 if (const TemplateSpecializationType *Spec
667 = T->getAs<TemplateSpecializationType>()) {
668 Out << '>';
669 VisitTemplateName(Spec->getTemplateName());
670 Out << Spec->getNumArgs();
671 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
672 VisitTemplateArgument(Spec->getArg(I));
673 return;
674 }
675
676 // Unhandled type.
677 Out << ' ';
678 break;
679 } while (true);
680}
681
682void USRGenerator::VisitTemplateParameterList(
683 const TemplateParameterList *Params) {
684 if (!Params)
685 return;
686 Out << '>' << Params->size();
687 for (TemplateParameterList::const_iterator P = Params->begin(),
688 PEnd = Params->end();
689 P != PEnd; ++P) {
690 Out << '#';
691 if (isa<TemplateTypeParmDecl>(*P)) {
692 if (cast<TemplateTypeParmDecl>(*P)->isParameterPack())
693 Out<< 'p';
694 Out << 'T';
695 continue;
696 }
697
698 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
699 if (NTTP->isParameterPack())
700 Out << 'p';
701 Out << 'N';
702 VisitType(NTTP->getType());
703 continue;
704 }
705
706 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
707 if (TTP->isParameterPack())
708 Out << 'p';
709 Out << 't';
710 VisitTemplateParameterList(TTP->getTemplateParameters());
711 }
712}
713
714void USRGenerator::VisitTemplateName(TemplateName Name) {
715 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
716 if (TemplateTemplateParmDecl *TTP
717 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
718 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
719 return;
720 }
721
722 Visit(Template);
723 return;
724 }
725
726 // FIXME: Visit dependent template names.
727}
728
729void USRGenerator::VisitTemplateArgument(const TemplateArgument &Arg) {
730 switch (Arg.getKind()) {
731 case TemplateArgument::Null:
732 break;
733
734 case TemplateArgument::Declaration:
735 Visit(Arg.getAsDecl());
736 break;
737
738 case TemplateArgument::NullPtr:
739 break;
740
741 case TemplateArgument::TemplateExpansion:
742 Out << 'P'; // pack expansion of...
743 // Fall through
744 case TemplateArgument::Template:
745 VisitTemplateName(Arg.getAsTemplateOrTemplatePattern());
746 break;
747
748 case TemplateArgument::Expression:
749 // FIXME: Visit expressions.
750 break;
751
752 case TemplateArgument::Pack:
753 Out << 'p' << Arg.pack_size();
754 for (TemplateArgument::pack_iterator P = Arg.pack_begin(), PEnd = Arg.pack_end();
755 P != PEnd; ++P)
756 VisitTemplateArgument(*P);
757 break;
758
759 case TemplateArgument::Type:
760 VisitType(Arg.getAsType());
761 break;
762
763 case TemplateArgument::Integral:
764 Out << 'V';
765 VisitType(Arg.getIntegralType());
766 Out << Arg.getAsIntegral();
767 break;
768 }
769}
770
771//===----------------------------------------------------------------------===//
772// General purpose USR generation methods.
773//===----------------------------------------------------------------------===//
774
775void USRGenerator::GenObjCClass(StringRef cls) {
776 Out << "objc(cs)" << cls;
777}
778
779void USRGenerator::GenObjCCategory(StringRef cls, StringRef cat) {
780 Out << "objc(cy)" << cls << '@' << cat;
781}
782
783void USRGenerator::GenObjCIvar(StringRef ivar) {
784 Out << '@' << ivar;
785}
786
787void USRGenerator::GenObjCMethod(StringRef meth, bool isInstanceMethod) {
788 Out << (isInstanceMethod ? "(im)" : "(cm)") << meth;
789}
790
791void USRGenerator::GenObjCProperty(StringRef prop) {
792 Out << "(py)" << prop;
793}
794
795void USRGenerator::GenObjCProtocol(StringRef prot) {
796 Out << "objc(pl)" << prot;
797}
798
799//===----------------------------------------------------------------------===//
800// API hooks.
801//===----------------------------------------------------------------------===//
802
803static inline StringRef extractUSRSuffix(StringRef s) {
804 return s.startswith("c:") ? s.substr(2) : "";
805}
806
807bool cxcursor::getDeclCursorUSR(const Decl *D, SmallVectorImpl<char> &Buf) {
808 // Don't generate USRs for things with invalid locations.
809 if (!D || D->getLocStart().isInvalid())
810 return true;
811
812 USRGenerator UG(&D->getASTContext(), &Buf);
Dmitri Gribenko8d0eb2f2013-02-03 13:26:20 +0000813 UG->Visit(D);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000814
815 if (UG->ignoreResults())
816 return true;
817
818 return false;
819}
820
821extern "C" {
822
823CXString clang_getCursorUSR(CXCursor C) {
824 const CXCursorKind &K = clang_getCursorKind(C);
825
826 if (clang_isDeclaration(K)) {
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +0000827 const Decl *D = cxcursor::getCursorDecl(C);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000828 if (!D)
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +0000829 return cxstring::createEmpty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000830
831 CXTranslationUnit TU = cxcursor::getCursorTU(C);
832 if (!TU)
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +0000833 return cxstring::createEmpty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000834
Dmitri Gribenkoaca3e562013-02-03 13:52:47 +0000835 cxstring::CXStringBuf *buf = cxstring::getCXStringBuf(TU);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000836 if (!buf)
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +0000837 return cxstring::createEmpty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000838
839 bool Ignore = cxcursor::getDeclCursorUSR(D, buf->Data);
840 if (Ignore) {
Dmitri Gribenko9c48d162013-01-26 22:44:19 +0000841 buf->dispose();
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +0000842 return cxstring::createEmpty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000843 }
844
845 // Return the C-string, but don't make a copy since it is already in
846 // the string buffer.
847 buf->Data.push_back('\0');
848 return createCXString(buf);
849 }
850
851 if (K == CXCursor_MacroDefinition) {
852 CXTranslationUnit TU = cxcursor::getCursorTU(C);
853 if (!TU)
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +0000854 return cxstring::createEmpty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000855
Dmitri Gribenkoaca3e562013-02-03 13:52:47 +0000856 cxstring::CXStringBuf *buf = cxstring::getCXStringBuf(TU);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000857 if (!buf)
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +0000858 return cxstring::createEmpty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000859
860 {
861 USRGenerator UG(&cxcursor::getCursorASTUnit(C)->getASTContext(),
862 &buf->Data);
863 UG << "macro@"
864 << cxcursor::getCursorMacroDefinition(C)->getName()->getNameStart();
865 }
866 buf->Data.push_back('\0');
867 return createCXString(buf);
868 }
869
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +0000870 return cxstring::createEmpty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000871}
872
873CXString clang_constructUSR_ObjCIvar(const char *name, CXString classUSR) {
874 USRGenerator UG;
875 UG << extractUSRSuffix(clang_getCString(classUSR));
876 UG->GenObjCIvar(name);
Dmitri Gribenko5595ded2013-02-02 02:19:29 +0000877 return cxstring::createDup(UG.str());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000878}
879
880CXString clang_constructUSR_ObjCMethod(const char *name,
881 unsigned isInstanceMethod,
882 CXString classUSR) {
883 USRGenerator UG;
884 UG << extractUSRSuffix(clang_getCString(classUSR));
885 UG->GenObjCMethod(name, isInstanceMethod);
Dmitri Gribenko5595ded2013-02-02 02:19:29 +0000886 return cxstring::createDup(UG.str());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000887}
888
889CXString clang_constructUSR_ObjCClass(const char *name) {
890 USRGenerator UG;
891 UG->GenObjCClass(name);
Dmitri Gribenko5595ded2013-02-02 02:19:29 +0000892 return cxstring::createDup(UG.str());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000893}
894
895CXString clang_constructUSR_ObjCProtocol(const char *name) {
896 USRGenerator UG;
897 UG->GenObjCProtocol(name);
Dmitri Gribenko5595ded2013-02-02 02:19:29 +0000898 return cxstring::createDup(UG.str());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000899}
900
901CXString clang_constructUSR_ObjCCategory(const char *class_name,
902 const char *category_name) {
903 USRGenerator UG;
904 UG->GenObjCCategory(class_name, category_name);
Dmitri Gribenko5595ded2013-02-02 02:19:29 +0000905 return cxstring::createDup(UG.str());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000906}
907
908CXString clang_constructUSR_ObjCProperty(const char *property,
909 CXString classUSR) {
910 USRGenerator UG;
911 UG << extractUSRSuffix(clang_getCString(classUSR));
912 UG->GenObjCProperty(property);
Dmitri Gribenko5595ded2013-02-02 02:19:29 +0000913 return cxstring::createDup(UG.str());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000914}
915
916} // end extern "C"