blob: e22980df394521a5a48d55aeab82c0072f704085 [file] [log] [blame]
Ted Kremenek1b6869a2010-01-05 22:06:45 +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"
Ted Kremenekcf84aa42010-01-18 20:23:29 +000015#include "CXCursor.h"
Douglas Gregorfe72e9c2010-08-31 17:01:39 +000016#include "clang/AST/DeclTemplate.h"
Benjamin Kramer9895c6a2010-01-12 11:32:40 +000017#include "clang/AST/DeclVisitor.h"
Ted Kremenek6f153952010-04-15 21:51:13 +000018#include "clang/Frontend/ASTUnit.h"
Benjamin Kramerb846deb2010-04-12 19:45:50 +000019#include "clang/Lex/PreprocessingRecord.h"
Ted Kremenek87763822010-01-12 02:07:58 +000020#include "llvm/ADT/SmallString.h"
Benjamin Kramer9895c6a2010-01-12 11:32:40 +000021#include "llvm/Support/raw_ostream.h"
Ted Kremenek6f153952010-04-15 21:51:13 +000022
Benjamin Kramerb846deb2010-04-12 19:45:50 +000023using namespace clang;
Ted Kremenekee4db4f2010-02-17 00:41:08 +000024using namespace clang::cxstring;
25
Ted Kremenekc50277f2010-01-12 23:33:42 +000026//===----------------------------------------------------------------------===//
27// USR generation.
28//===----------------------------------------------------------------------===//
29
30namespace {
Ted Kremenek2fee4e62010-01-14 01:50:21 +000031class USRGenerator : public DeclVisitor<USRGenerator> {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +000032 llvm::SmallString<1024> Buf;
33 llvm::raw_svector_ostream Out;
Ted Kremenek3adca6d2010-01-18 22:02:49 +000034 bool IgnoreResults;
Ted Kremenek1865cfe2010-04-15 21:04:25 +000035 ASTUnit *AU;
Ted Kremenekcbd66f02010-05-06 23:38:28 +000036 bool generatedLoc;
Douglas Gregor66b7fbf2010-09-20 20:37:39 +000037
38 llvm::DenseMap<const Type *, unsigned> TypeSubstitutions;
39
Ted Kremenek2fee4e62010-01-14 01:50:21 +000040public:
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +000041 USRGenerator(const CXCursor *C = 0)
42 : Out(Buf),
43 IgnoreResults(false),
44 AU(C ? cxcursor::getCursorASTUnit(*C) : 0),
45 generatedLoc(false)
46 {
47 // Add the USR space prefix.
48 Out << "c:";
49 }
50
51 llvm::StringRef str() {
52 return Out.str();
53 }
54
55 USRGenerator* operator->() { return this; }
56
57 template <typename T>
58 llvm::raw_svector_ostream &operator<<(const T &x) {
59 Out << x;
60 return Out;
61 }
Ted Kremenek896b70f2010-03-13 02:50:34 +000062
Ted Kremenek3adca6d2010-01-18 22:02:49 +000063 bool ignoreResults() const { return IgnoreResults; }
Ted Kremenek896b70f2010-03-13 02:50:34 +000064
65 // Visitation methods from generating USRs from AST elements.
Ted Kremenek2fee4e62010-01-14 01:50:21 +000066 void VisitDeclContext(DeclContext *D);
Ted Kremenek3adca6d2010-01-18 22:02:49 +000067 void VisitFieldDecl(FieldDecl *D);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000068 void VisitFunctionDecl(FunctionDecl *D);
69 void VisitNamedDecl(NamedDecl *D);
70 void VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor69319002010-08-31 23:48:11 +000071 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +000072 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Douglas Gregor39d6f072010-08-31 19:02:00 +000073 void VisitClassTemplateDecl(ClassTemplateDecl *D);
Ted Kremeneke74ef122010-04-16 21:31:52 +000074 void VisitObjCClassDecl(ObjCClassDecl *CD);
Ted Kremenek896b70f2010-03-13 02:50:34 +000075 void VisitObjCContainerDecl(ObjCContainerDecl *CD);
Ted Kremeneke74ef122010-04-16 21:31:52 +000076 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *P);
Ted Kremeneke542f772010-04-20 23:15:40 +000077 void VisitObjCMethodDecl(ObjCMethodDecl *MD);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000078 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Ted Kremeneke542f772010-04-20 23:15:40 +000079 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Ted Kremenekb82b3be2010-01-18 22:42:20 +000080 void VisitTagDecl(TagDecl *D);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000081 void VisitTypedefDecl(TypedefDecl *D);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +000082 void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Ted Kremeneke542f772010-04-20 23:15:40 +000083 void VisitVarDecl(VarDecl *D);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +000084 void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
85 void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
Ted Kremenek8e672192010-05-07 01:04:32 +000086 void VisitLinkageSpecDecl(LinkageSpecDecl *D) {
87 IgnoreResults = true;
Ted Kremenek8e672192010-05-07 01:04:32 +000088 }
Douglas Gregor0a35bce2010-09-01 03:07:18 +000089 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
90 IgnoreResults = true;
91 }
Douglas Gregor7e242562010-09-01 19:52:22 +000092 void VisitUsingDecl(UsingDecl *D) {
93 IgnoreResults = true;
94 }
95 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
96 IgnoreResults = true;
97 }
98 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
99 IgnoreResults = true;
100 }
Douglas Gregor0a35bce2010-09-01 03:07:18 +0000101
Ted Kremenek6f153952010-04-15 21:51:13 +0000102 /// Generate the string component containing the location of the
103 /// declaration.
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000104 bool GenLoc(const Decl *D);
Ted Kremenek6f153952010-04-15 21:51:13 +0000105
Ted Kremenek896b70f2010-03-13 02:50:34 +0000106 /// String generation methods used both by the visitation methods
107 /// and from other clients that want to directly generate USRs. These
108 /// methods do not construct complete USRs (which incorporate the parents
109 /// of an AST element), but only the fragments concerning the AST element
110 /// itself.
111
Ted Kremenek896b70f2010-03-13 02:50:34 +0000112 /// Generate a USR for an Objective-C class.
113 void GenObjCClass(llvm::StringRef cls);
114 /// Generate a USR for an Objective-C class category.
115 void GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat);
116 /// Generate a USR fragment for an Objective-C instance variable. The
117 /// complete USR can be created by concatenating the USR for the
118 /// encompassing class with this USR fragment.
119 void GenObjCIvar(llvm::StringRef ivar);
120 /// Generate a USR fragment for an Objective-C method.
121 void GenObjCMethod(llvm::StringRef sel, bool isInstanceMethod);
122 /// Generate a USR fragment for an Objective-C property.
123 void GenObjCProperty(llvm::StringRef prop);
124 /// Generate a USR for an Objective-C protocol.
125 void GenObjCProtocol(llvm::StringRef prot);
Ted Kremenek8e672192010-05-07 01:04:32 +0000126
127 void VisitType(QualType T);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000128 void VisitTemplateParameterList(const TemplateParameterList *Params);
129 void VisitTemplateName(TemplateName Name);
130 void VisitTemplateArgument(const TemplateArgument &Arg);
131
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000132 /// Emit a Decl's name using NamedDecl::printName() and return true if
133 /// the decl had no name.
134 bool EmitDeclName(const NamedDecl *D);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000135};
136
Ted Kremenekc50277f2010-01-12 23:33:42 +0000137} // end anonymous namespace
138
Ted Kremenek896b70f2010-03-13 02:50:34 +0000139//===----------------------------------------------------------------------===//
140// Generating USRs from ASTS.
141//===----------------------------------------------------------------------===//
142
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000143bool USRGenerator::EmitDeclName(const NamedDecl *D) {
144 Out.flush();
145 const unsigned startSize = Buf.size();
146 D->printName(Out);
147 Out.flush();
148 const unsigned endSize = Buf.size();
149 return startSize == endSize;
150}
151
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000152static bool InAnonymousNamespace(const Decl *D) {
153 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(D->getDeclContext()))
154 return ND->isAnonymousNamespace();
155 return false;
156}
157
158static inline bool ShouldGenerateLocation(const NamedDecl *D) {
159 return D->getLinkage() != ExternalLinkage && !InAnonymousNamespace(D);
160}
161
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000162void USRGenerator::VisitDeclContext(DeclContext *DC) {
163 if (NamedDecl *D = dyn_cast<NamedDecl>(DC))
164 Visit(D);
165}
166
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000167void USRGenerator::VisitFieldDecl(FieldDecl *D) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000168 VisitDeclContext(D->getDeclContext());
169 Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@");
170 if (EmitDeclName(D)) {
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000171 // Bit fields can be anonymous.
172 IgnoreResults = true;
173 return;
174 }
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000175}
176
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000177void USRGenerator::VisitFunctionDecl(FunctionDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000178 if (ShouldGenerateLocation(D) && GenLoc(D))
179 return;
Ted Kremenekcf999102010-04-29 17:43:29 +0000180
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000181 VisitDeclContext(D->getDeclContext());
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000182 if (FunctionTemplateDecl *FunTmpl = D->getDescribedFunctionTemplate()) {
183 Out << "@FT@";
184 VisitTemplateParameterList(FunTmpl->getTemplateParameters());
185 } else
186 Out << "@F@";
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000187 D->printName(Out);
Ted Kremenek8e672192010-05-07 01:04:32 +0000188
189 ASTContext &Ctx = AU->getASTContext();
190 if (!Ctx.getLangOptions().CPlusPlus || D->isExternC())
191 return;
192
193 // Mangle in type information for the arguments.
194 for (FunctionDecl::param_iterator I = D->param_begin(), E = D->param_end();
195 I != E; ++I) {
196 Out << '#';
197 if (ParmVarDecl *PD = *I)
198 VisitType(PD->getType());
199 }
200 if (D->isVariadic())
201 Out << '.';
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000202 Out << '#';
203 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
204 if (MD->isStatic())
205 Out << 'S';
206 if (unsigned quals = MD->getTypeQualifiers())
207 Out << (char)('0' + quals);
208 }
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000209}
Ted Kremenekc50277f2010-01-12 23:33:42 +0000210
211void USRGenerator::VisitNamedDecl(NamedDecl *D) {
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000212 VisitDeclContext(D->getDeclContext());
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000213 Out << "@";
214
215 if (EmitDeclName(D)) {
216 // The string can be empty if the declaration has no name; e.g., it is
217 // the ParmDecl with no name for declaration of a function pointer type,
218 // e.g.: void (*f)(void *);
219 // In this case, don't generate a USR.
Ted Kremeneke74ef122010-04-16 21:31:52 +0000220 IgnoreResults = true;
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000221 }
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000222}
223
Ted Kremeneke542f772010-04-20 23:15:40 +0000224void USRGenerator::VisitVarDecl(VarDecl *D) {
225 // VarDecls can be declared 'extern' within a function or method body,
226 // but their enclosing DeclContext is the function, not the TU. We need
227 // to check the storage class to correctly generate the USR.
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000228 if (ShouldGenerateLocation(D) && GenLoc(D))
229 return;
230
231 VisitDeclContext(D->getDeclContext());
Ted Kremeneke542f772010-04-20 23:15:40 +0000232
Ted Kremenekcf999102010-04-29 17:43:29 +0000233 // Variables always have simple names.
234 llvm::StringRef s = D->getName();
235
Ted Kremeneke542f772010-04-20 23:15:40 +0000236 // The string can be empty if the declaration has no name; e.g., it is
237 // the ParmDecl with no name for declaration of a function pointer type, e.g.:
Eli Friedmana7e68452010-08-22 01:00:03 +0000238 // void (*f)(void *);
Ted Kremeneke542f772010-04-20 23:15:40 +0000239 // In this case, don't generate a USR.
240 if (s.empty())
241 IgnoreResults = true;
242 else
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000243 Out << '@' << s;
Ted Kremeneke542f772010-04-20 23:15:40 +0000244}
245
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000246void USRGenerator::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
247 GenLoc(D);
248 return;
249}
250
251void USRGenerator::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
252 GenLoc(D);
253 return;
254}
255
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000256void USRGenerator::VisitNamespaceDecl(NamespaceDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000257 if (D->isAnonymousNamespace()) {
258 Out << "@aN";
259 return;
260 }
261
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000262 VisitDeclContext(D->getDeclContext());
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000263 if (!IgnoreResults)
264 Out << "@N@" << D->getName();
Ted Kremenekc50277f2010-01-12 23:33:42 +0000265}
266
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000267void USRGenerator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
268 VisitFunctionDecl(D->getTemplatedDecl());
269}
270
Douglas Gregor39d6f072010-08-31 19:02:00 +0000271void USRGenerator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
272 VisitTagDecl(D->getTemplatedDecl());
273}
274
Douglas Gregor69319002010-08-31 23:48:11 +0000275void USRGenerator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
276 VisitDeclContext(D->getDeclContext());
277 if (!IgnoreResults)
278 Out << "@NA@" << D->getName();
279}
Douglas Gregor39d6f072010-08-31 19:02:00 +0000280
Ted Kremenekc50277f2010-01-12 23:33:42 +0000281void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) {
Ted Kremenek28a7f252010-08-24 23:13:41 +0000282 Decl *container = cast<Decl>(D->getDeclContext());
283
284 // The USR for a method declared in a class extension is based on
285 // the ObjCInterfaceDecl, not the ObjCCategoryDecl.
286 do {
287 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(container))
288 if (CD->IsClassExtension()) {
Ted Kremenekc52d0692010-09-21 04:45:46 +0000289 // ID can be null with invalid code.
290 if (ObjCInterfaceDecl *ID = CD->getClassInterface()) {
291 Visit(ID);
Ted Kremenek097727b2010-09-21 04:47:01 +0000292 break;
Ted Kremenekc52d0692010-09-21 04:45:46 +0000293 }
294 // Invalid code. Can't generate USR.
295 IgnoreResults = true;
296 return;
297 }
298
299 Visit(container);
Ted Kremenek28a7f252010-08-24 23:13:41 +0000300 }
301 while (false);
302
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000303 // Ideally we would use 'GenObjCMethod', but this is such a hot path
304 // for Objective-C code that we don't want to use
305 // DeclarationName::getAsString().
306 Out << (D->isInstanceMethod() ? "(im)" : "(cm)");
307 DeclarationName N(D->getSelector());
308 N.printName(Out);
Ted Kremenekc50277f2010-01-12 23:33:42 +0000309}
310
Ted Kremeneke74ef122010-04-16 21:31:52 +0000311void USRGenerator::VisitObjCClassDecl(ObjCClassDecl *D) {
312 // FIXME: @class declarations can refer to multiple classes. We need
313 // to be able to traverse these.
314 IgnoreResults = true;
315}
316
317void USRGenerator::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
318 // FIXME: @protocol declarations can refer to multiple protocols. We need
319 // to be able to traverse these.
320 IgnoreResults = true;
321}
322
Ted Kremenekc50277f2010-01-12 23:33:42 +0000323void USRGenerator::VisitObjCContainerDecl(ObjCContainerDecl *D) {
324 switch (D->getKind()) {
325 default:
326 assert(false && "Invalid ObjC container.");
327 case Decl::ObjCInterface:
328 case Decl::ObjCImplementation:
Ted Kremenek896b70f2010-03-13 02:50:34 +0000329 GenObjCClass(D->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000330 break;
331 case Decl::ObjCCategory: {
332 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
Ted Kremenekebfa3392010-03-19 20:39:03 +0000333 ObjCInterfaceDecl *ID = CD->getClassInterface();
334 if (!ID) {
335 // Handle invalid code where the @interface might not
336 // have been specified.
337 // FIXME: We should be able to generate this USR even if the
338 // @interface isn't available.
339 IgnoreResults = true;
340 return;
341 }
Ted Kremenek28a7f252010-08-24 23:13:41 +0000342 // Specially handle class extensions, which are anonymous categories.
343 // We want to mangle in the location to uniquely distinguish them.
344 if (CD->IsClassExtension()) {
345 Out << "objc(ext)" << ID->getName() << '@';
346 GenLoc(CD);
347 }
348 else
349 GenObjCCategory(ID->getName(), CD->getName());
350
Ted Kremenekc50277f2010-01-12 23:33:42 +0000351 break;
352 }
353 case Decl::ObjCCategoryImpl: {
354 ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
Ted Kremenekebfa3392010-03-19 20:39:03 +0000355 ObjCInterfaceDecl *ID = CD->getClassInterface();
356 if (!ID) {
357 // Handle invalid code where the @interface might not
358 // have been specified.
359 // FIXME: We should be able to generate this USR even if the
360 // @interface isn't available.
361 IgnoreResults = true;
362 return;
363 }
364 GenObjCCategory(ID->getName(), CD->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000365 break;
366 }
367 case Decl::ObjCProtocol:
Ted Kremenek896b70f2010-03-13 02:50:34 +0000368 GenObjCProtocol(cast<ObjCProtocolDecl>(D)->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000369 break;
370 }
371}
372
373void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
374 Visit(cast<Decl>(D->getDeclContext()));
Ted Kremenek896b70f2010-03-13 02:50:34 +0000375 GenObjCProperty(D->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000376}
377
Ted Kremeneke542f772010-04-20 23:15:40 +0000378void USRGenerator::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
379 if (ObjCPropertyDecl *PD = D->getPropertyDecl()) {
380 VisitObjCPropertyDecl(PD);
381 return;
382 }
383
384 IgnoreResults = true;
385}
386
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000387void USRGenerator::VisitTagDecl(TagDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000388 // Add the location of the tag decl to handle resolution across
389 // translation units.
390 if (ShouldGenerateLocation(D) && GenLoc(D))
391 return;
Ted Kremenek8e672192010-05-07 01:04:32 +0000392
Ted Kremenek6f153952010-04-15 21:51:13 +0000393 D = D->getCanonicalDecl();
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000394 VisitDeclContext(D->getDeclContext());
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000395
Douglas Gregor74dbe642010-08-31 19:31:58 +0000396 bool AlreadyStarted = false;
397 if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(D)) {
Douglas Gregor39d6f072010-08-31 19:02:00 +0000398 if (ClassTemplateDecl *ClassTmpl = CXXRecord->getDescribedClassTemplate()) {
Douglas Gregor74dbe642010-08-31 19:31:58 +0000399 AlreadyStarted = true;
Douglas Gregor39d6f072010-08-31 19:02:00 +0000400
401 switch (D->getTagKind()) {
Douglas Gregor74dbe642010-08-31 19:31:58 +0000402 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"); break;
Douglas Gregor39d6f072010-08-31 19:02:00 +0000406 }
407 VisitTemplateParameterList(ClassTmpl->getTemplateParameters());
Douglas Gregor74dbe642010-08-31 19:31:58 +0000408 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
409 = dyn_cast<ClassTemplatePartialSpecializationDecl>(CXXRecord)) {
410 AlreadyStarted = true;
411
412 switch (D->getTagKind()) {
413 case TTK_Struct: Out << "@SP"; break;
414 case TTK_Class: Out << "@CP"; break;
415 case TTK_Union: Out << "@UP"; break;
416 case TTK_Enum: llvm_unreachable("enum partial specialization"); break;
417 }
418 VisitTemplateParameterList(PartialSpec->getTemplateParameters());
Douglas Gregor39d6f072010-08-31 19:02:00 +0000419 }
Douglas Gregor74dbe642010-08-31 19:31:58 +0000420 }
Douglas Gregor39d6f072010-08-31 19:02:00 +0000421
Douglas Gregor74dbe642010-08-31 19:31:58 +0000422 if (!AlreadyStarted) {
Douglas Gregor39d6f072010-08-31 19:02:00 +0000423 switch (D->getTagKind()) {
424 case TTK_Struct: Out << "@S"; break;
425 case TTK_Class: Out << "@C"; break;
426 case TTK_Union: Out << "@U"; break;
427 case TTK_Enum: Out << "@E"; break;
428 }
Ted Kremeneke74ef122010-04-16 21:31:52 +0000429 }
Douglas Gregor39d6f072010-08-31 19:02:00 +0000430
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000431 Out << '@';
432 Out.flush();
433 assert(Buf.size() > 0);
434 const unsigned off = Buf.size() - 1;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000435
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000436 if (EmitDeclName(D)) {
437 if (const TypedefDecl *TD = D->getTypedefForAnonDecl()) {
438 Buf[off] = 'A';
Benjamin Kramer900fc632010-04-17 09:33:03 +0000439 Out << '@' << TD;
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000440 }
441 else
442 Buf[off] = 'a';
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000443 }
Douglas Gregor0ab1e9f2010-09-01 17:32:36 +0000444
445 // For a class template specialization, mangle the template arguments.
446 if (ClassTemplateSpecializationDecl *Spec
447 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
448 const TemplateArgumentList &Args = Spec->getTemplateInstantiationArgs();
449 Out << '>';
450 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
451 Out << '#';
452 VisitTemplateArgument(Args.get(I));
453 }
454 }
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000455}
456
Ted Kremenekc50277f2010-01-12 23:33:42 +0000457void USRGenerator::VisitTypedefDecl(TypedefDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000458 if (ShouldGenerateLocation(D) && GenLoc(D))
459 return;
Ted Kremenekc50277f2010-01-12 23:33:42 +0000460 DeclContext *DC = D->getDeclContext();
461 if (NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
Ted Kremenek896b70f2010-03-13 02:50:34 +0000462 Visit(DCN);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000463 Out << "@T@";
Ted Kremenek6f153952010-04-15 21:51:13 +0000464 Out << D->getName();
465}
466
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000467void USRGenerator::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
468 GenLoc(D);
469 return;
470}
471
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000472bool USRGenerator::GenLoc(const Decl *D) {
473 if (generatedLoc)
474 return IgnoreResults;
475 generatedLoc = true;
476
Ted Kremenek6f153952010-04-15 21:51:13 +0000477 const SourceManager &SM = AU->getSourceManager();
478 SourceLocation L = D->getLocStart();
479 if (L.isInvalid()) {
480 IgnoreResults = true;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000481 return true;
Ted Kremenek6f153952010-04-15 21:51:13 +0000482 }
483 L = SM.getInstantiationLoc(L);
484 const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(L);
485 const FileEntry *FE = SM.getFileEntryForID(Decomposed.first);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000486 if (FE) {
487 llvm::sys::Path P(FE->getName());
488 Out << P.getLast();
489 }
Ted Kremenek6f153952010-04-15 21:51:13 +0000490 else {
491 // This case really isn't interesting.
492 IgnoreResults = true;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000493 return true;
Ted Kremenek6f153952010-04-15 21:51:13 +0000494 }
Ted Kremenekf48b5312010-07-22 11:14:15 +0000495 // Use the offest into the FileID to represent the location. Using
496 // a line/column can cause us to look back at the original source file,
497 // which is expensive.
498 Out << '@' << Decomposed.second;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000499 return IgnoreResults;
Ted Kremenekc50277f2010-01-12 23:33:42 +0000500}
501
Ted Kremenek8e672192010-05-07 01:04:32 +0000502void USRGenerator::VisitType(QualType T) {
503 // This method mangles in USR information for types. It can possibly
504 // just reuse the naming-mangling logic used by codegen, although the
505 // requirements for USRs might not be the same.
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000506 ASTContext &Ctx = AU->getASTContext();
507
Ted Kremenek8e672192010-05-07 01:04:32 +0000508 do {
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000509 T = Ctx.getCanonicalType(T);
Ted Kremenek8e672192010-05-07 01:04:32 +0000510 Qualifiers Q = T.getQualifiers();
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000511 unsigned qVal = 0;
Ted Kremenek8e672192010-05-07 01:04:32 +0000512 if (Q.hasConst())
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000513 qVal |= 0x1;
Ted Kremenek8e672192010-05-07 01:04:32 +0000514 if (Q.hasVolatile())
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000515 qVal |= 0x2;
Ted Kremenek8e672192010-05-07 01:04:32 +0000516 if (Q.hasRestrict())
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000517 qVal |= 0x4;
518 if(qVal)
519 Out << ((char) ('0' + qVal));
Ted Kremenek8e672192010-05-07 01:04:32 +0000520
521 // Mangle in ObjC GC qualifiers?
522
Ted Kremenek8e672192010-05-07 01:04:32 +0000523 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
524 unsigned char c = '\0';
525 switch (BT->getKind()) {
526 case BuiltinType::Void:
527 c = 'v'; break;
528 case BuiltinType::Bool:
529 c = 'b'; break;
530 case BuiltinType::Char_U:
531 case BuiltinType::UChar:
532 c = 'c'; break;
533 case BuiltinType::Char16:
534 c = 'q'; break;
535 case BuiltinType::Char32:
536 c = 'w'; break;
537 case BuiltinType::UShort:
538 c = 's'; break;
539 case BuiltinType::UInt:
540 c = 'i'; break;
541 case BuiltinType::ULong:
542 c = 'l'; break;
543 case BuiltinType::ULongLong:
544 c = 'k'; break;
545 case BuiltinType::UInt128:
546 c = 'j'; break;
547 case BuiltinType::Char_S:
548 case BuiltinType::SChar:
549 c = 'C'; break;
550 case BuiltinType::WChar:
551 c = 'W'; break;
552 case BuiltinType::Short:
553 c = 'S'; break;
554 case BuiltinType::Int:
555 c = 'I'; break;
556 case BuiltinType::Long:
557 c = 'L'; break;
558 case BuiltinType::LongLong:
559 c = 'K'; break;
560 case BuiltinType::Int128:
561 c = 'J'; break;
562 case BuiltinType::Float:
563 c = 'f'; break;
564 case BuiltinType::Double:
565 c = 'd'; break;
566 case BuiltinType::LongDouble:
567 c = 'D'; break;
568 case BuiltinType::NullPtr:
569 c = 'n'; break;
570 case BuiltinType::Overload:
571 case BuiltinType::Dependent:
572 case BuiltinType::UndeducedAuto:
573 IgnoreResults = true;
574 return;
575 case BuiltinType::ObjCId:
576 c = 'o'; break;
577 case BuiltinType::ObjCClass:
578 c = 'O'; break;
579 case BuiltinType::ObjCSel:
580 c = 'e'; break;
581 }
582 Out << c;
583 return;
584 }
Douglas Gregor66b7fbf2010-09-20 20:37:39 +0000585
586 // If we have already seen this (non-built-in) type, use a substitution
587 // encoding.
588 llvm::DenseMap<const Type *, unsigned>::iterator Substitution
589 = TypeSubstitutions.find(T.getTypePtr());
590 if (Substitution != TypeSubstitutions.end()) {
591 Out << 'S' << Substitution->second << '_';
592 return;
593 } else {
594 // Record this as a substitution.
595 unsigned Number = TypeSubstitutions.size();
596 TypeSubstitutions[T.getTypePtr()] = Number;
597 }
598
599 if (const PointerType *PT = T->getAs<PointerType>()) {
600 Out << '*';
601 T = PT->getPointeeType();
602 continue;
603 }
604 if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
605 Out << '&';
606 T = RT->getPointeeType();
607 continue;
608 }
609 if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) {
610 Out << 'F';
611 VisitType(FT->getResultType());
612 for (FunctionProtoType::arg_type_iterator
613 I = FT->arg_type_begin(), E = FT->arg_type_end(); I!=E; ++I) {
614 VisitType(*I);
615 }
616 if (FT->isVariadic())
617 Out << '.';
618 return;
619 }
620 if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) {
621 Out << 'B';
622 T = BT->getPointeeType();
623 continue;
624 }
Ted Kremenek8e672192010-05-07 01:04:32 +0000625 if (const ComplexType *CT = T->getAs<ComplexType>()) {
626 Out << '<';
627 T = CT->getElementType();
628 continue;
629 }
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000630 if (const TagType *TT = T->getAs<TagType>()) {
631 Out << '$';
632 VisitTagDecl(TT->getDecl());
633 return;
634 }
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000635 if (const TemplateTypeParmType *TTP = T->getAs<TemplateTypeParmType>()) {
636 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
637 return;
638 }
639 if (const TemplateSpecializationType *Spec
640 = T->getAs<TemplateSpecializationType>()) {
641 Out << '>';
642 VisitTemplateName(Spec->getTemplateName());
643 Out << Spec->getNumArgs();
644 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
645 VisitTemplateArgument(Spec->getArg(I));
646 return;
647 }
648
Ted Kremenek8e672192010-05-07 01:04:32 +0000649 // Unhandled type.
650 Out << ' ';
651 break;
652 } while (true);
653}
654
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000655void USRGenerator::VisitTemplateParameterList(
656 const TemplateParameterList *Params) {
657 if (!Params)
658 return;
659 Out << '>' << Params->size();
660 for (TemplateParameterList::const_iterator P = Params->begin(),
661 PEnd = Params->end();
662 P != PEnd; ++P) {
663 Out << '#';
664 if (isa<TemplateTypeParmDecl>(*P)) {
665 Out << 'T';
666 continue;
667 }
668
669 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
670 Out << 'N';
671 VisitType(NTTP->getType());
672 continue;
673 }
674
675 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
676 Out << 't';
677 VisitTemplateParameterList(TTP->getTemplateParameters());
678 }
679}
680
681void USRGenerator::VisitTemplateName(TemplateName Name) {
682 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
683 if (TemplateTemplateParmDecl *TTP
684 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
685 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
686 return;
687 }
688
689 Visit(Template);
690 return;
691 }
692
693 // FIXME: Visit dependent template names.
694}
695
696void USRGenerator::VisitTemplateArgument(const TemplateArgument &Arg) {
697 switch (Arg.getKind()) {
698 case TemplateArgument::Null:
699 break;
700
701 case TemplateArgument::Declaration:
Douglas Gregor97475832010-10-05 18:37:06 +0000702 if (Decl *D = Arg.getAsDecl())
703 Visit(D);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000704 break;
705
706 case TemplateArgument::Template:
707 VisitTemplateName(Arg.getAsTemplate());
708 break;
709
710 case TemplateArgument::Expression:
711 // FIXME: Visit expressions.
712 break;
713
714 case TemplateArgument::Pack:
715 // FIXME: Variadic templates
716 break;
717
718 case TemplateArgument::Type:
719 VisitType(Arg.getAsType());
720 break;
721
722 case TemplateArgument::Integral:
723 Out << 'V';
724 VisitType(Arg.getIntegralType());
725 Out << *Arg.getAsIntegral();
726 break;
727 }
728}
729
Ted Kremenek896b70f2010-03-13 02:50:34 +0000730//===----------------------------------------------------------------------===//
731// General purpose USR generation methods.
732//===----------------------------------------------------------------------===//
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000733
Ted Kremenek896b70f2010-03-13 02:50:34 +0000734void USRGenerator::GenObjCClass(llvm::StringRef cls) {
735 Out << "objc(cs)" << cls;
736}
737
738void USRGenerator::GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat) {
Ted Kremeneke74ef122010-04-16 21:31:52 +0000739 Out << "objc(cy)" << cls << '@' << cat;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000740}
741
742void USRGenerator::GenObjCIvar(llvm::StringRef ivar) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000743 Out << '@' << ivar;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000744}
745
746void USRGenerator::GenObjCMethod(llvm::StringRef meth, bool isInstanceMethod) {
747 Out << (isInstanceMethod ? "(im)" : "(cm)") << meth;
748}
749
750void USRGenerator::GenObjCProperty(llvm::StringRef prop) {
751 Out << "(py)" << prop;
752}
753
754void USRGenerator::GenObjCProtocol(llvm::StringRef prot) {
755 Out << "objc(pl)" << prot;
756}
757
758//===----------------------------------------------------------------------===//
759// API hooks.
760//===----------------------------------------------------------------------===//
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000761
Benjamin Kramercfb51b62010-04-08 15:54:07 +0000762static inline llvm::StringRef extractUSRSuffix(llvm::StringRef s) {
763 return s.startswith("c:") ? s.substr(2) : "";
764}
765
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000766static CXString getDeclCursorUSR(const CXCursor &C) {
Ted Kremenek896b70f2010-03-13 02:50:34 +0000767 Decl *D = cxcursor::getCursorDecl(C);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000768
769 // Don't generate USRs for things with invalid locations.
770 if (!D || D->getLocStart().isInvalid())
Ted Kremenek1af0a2a2010-04-17 00:21:38 +0000771 return createCXString("");
Ted Kremenek896b70f2010-03-13 02:50:34 +0000772
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000773 // Check if the cursor has 'NoLinkage'.
Ted Kremeneke74ef122010-04-16 21:31:52 +0000774 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000775 switch (ND->getLinkage()) {
776 case ExternalLinkage:
777 // Generate USRs for all entities with external linkage.
778 break;
779 case NoLinkage:
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000780 case UniqueExternalLinkage:
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000781 // We allow enums, typedefs, and structs that have no linkage to
782 // have USRs that are anchored to the file they were defined in
783 // (e.g., the header). This is a little gross, but in principal
784 // enums/anonymous structs/etc. defined in a common header file
785 // are referred to across multiple translation units.
Ted Kremenek6f153952010-04-15 21:51:13 +0000786 if (isa<TagDecl>(ND) || isa<TypedefDecl>(ND) ||
Ted Kremenekcf999102010-04-29 17:43:29 +0000787 isa<EnumConstantDecl>(ND) || isa<FieldDecl>(ND) ||
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000788 isa<VarDecl>(ND) || isa<NamespaceDecl>(ND))
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000789 break;
790 // Fall-through.
791 case InternalLinkage:
Ted Kremenekcf999102010-04-29 17:43:29 +0000792 if (isa<FunctionDecl>(ND))
793 break;
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000794 }
795
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000796 USRGenerator UG(&C);
797 UG->Visit(D);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000798
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000799 if (UG->ignoreResults())
Ted Kremenekebfa3392010-03-19 20:39:03 +0000800 return createCXString("");
Ted Kremenek896b70f2010-03-13 02:50:34 +0000801
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000802#if 0
Ted Kremeneke542f772010-04-20 23:15:40 +0000803 // For development testing.
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000804 assert(UG.str().size() > 2);
805#endif
Ted Kremeneke542f772010-04-20 23:15:40 +0000806
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000807 // Return a copy of the string that must be disposed by the caller.
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000808 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000809}
810
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000811extern "C" {
812
813CXString clang_getCursorUSR(CXCursor C) {
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000814 const CXCursorKind &K = clang_getCursorKind(C);
815
816 if (clang_isDeclaration(K))
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000817 return getDeclCursorUSR(C);
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000818
819 if (K == CXCursor_MacroDefinition) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000820 USRGenerator UG(&C);
821 UG << "macro@"
822 << cxcursor::getCursorMacroDefinition(C)->getName()->getNameStart();
823 return createCXString(UG.str(), true);
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000824 }
825
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000826 return createCXString("");
827}
828
Ted Kremenek896b70f2010-03-13 02:50:34 +0000829CXString clang_constructUSR_ObjCIvar(const char *name, CXString classUSR) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000830 USRGenerator UG;
831 UG << extractUSRSuffix(clang_getCString(classUSR));
832 UG->GenObjCIvar(name);
833 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000834}
835
836CXString clang_constructUSR_ObjCMethod(const char *name,
837 unsigned isInstanceMethod,
838 CXString classUSR) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000839 USRGenerator UG;
840 UG << extractUSRSuffix(clang_getCString(classUSR));
841 UG->GenObjCMethod(name, isInstanceMethod);
842 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000843}
844
845CXString clang_constructUSR_ObjCClass(const char *name) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000846 USRGenerator UG;
847 UG->GenObjCClass(name);
848 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000849}
850
851CXString clang_constructUSR_ObjCProtocol(const char *name) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000852 USRGenerator UG;
853 UG->GenObjCProtocol(name);
854 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000855}
856
Ted Kremenek66ccaec2010-03-15 17:38:58 +0000857CXString clang_constructUSR_ObjCCategory(const char *class_name,
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000858 const char *category_name) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000859 USRGenerator UG;
860 UG->GenObjCCategory(class_name, category_name);
861 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000862}
863
864CXString clang_constructUSR_ObjCProperty(const char *property,
865 CXString classUSR) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000866 USRGenerator UG;
867 UG << extractUSRSuffix(clang_getCString(classUSR));
868 UG->GenObjCProperty(property);
869 return createCXString(UG.str(), true);
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000870}
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000871
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000872} // end extern "C"