blob: a60d1d3e189884be33587b8dd4e613b3f3f07c58 [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;
Ted Kremenek2fee4e62010-01-14 01:50:21 +000037public:
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +000038 USRGenerator(const CXCursor *C = 0)
39 : Out(Buf),
40 IgnoreResults(false),
41 AU(C ? cxcursor::getCursorASTUnit(*C) : 0),
42 generatedLoc(false)
43 {
44 // Add the USR space prefix.
45 Out << "c:";
46 }
47
48 llvm::StringRef str() {
49 return Out.str();
50 }
51
52 USRGenerator* operator->() { return this; }
53
54 template <typename T>
55 llvm::raw_svector_ostream &operator<<(const T &x) {
56 Out << x;
57 return Out;
58 }
Ted Kremenek896b70f2010-03-13 02:50:34 +000059
Ted Kremenek3adca6d2010-01-18 22:02:49 +000060 bool ignoreResults() const { return IgnoreResults; }
Ted Kremenek896b70f2010-03-13 02:50:34 +000061
62 // Visitation methods from generating USRs from AST elements.
Ted Kremenek2fee4e62010-01-14 01:50:21 +000063 void VisitDeclContext(DeclContext *D);
Ted Kremenek3adca6d2010-01-18 22:02:49 +000064 void VisitFieldDecl(FieldDecl *D);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000065 void VisitFunctionDecl(FunctionDecl *D);
66 void VisitNamedDecl(NamedDecl *D);
67 void VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor69319002010-08-31 23:48:11 +000068 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +000069 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Douglas Gregor39d6f072010-08-31 19:02:00 +000070 void VisitClassTemplateDecl(ClassTemplateDecl *D);
Ted Kremeneke74ef122010-04-16 21:31:52 +000071 void VisitObjCClassDecl(ObjCClassDecl *CD);
Ted Kremenek896b70f2010-03-13 02:50:34 +000072 void VisitObjCContainerDecl(ObjCContainerDecl *CD);
Ted Kremeneke74ef122010-04-16 21:31:52 +000073 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *P);
Ted Kremeneke542f772010-04-20 23:15:40 +000074 void VisitObjCMethodDecl(ObjCMethodDecl *MD);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000075 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Ted Kremeneke542f772010-04-20 23:15:40 +000076 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Ted Kremenekb82b3be2010-01-18 22:42:20 +000077 void VisitTagDecl(TagDecl *D);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000078 void VisitTypedefDecl(TypedefDecl *D);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +000079 void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Ted Kremeneke542f772010-04-20 23:15:40 +000080 void VisitVarDecl(VarDecl *D);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +000081 void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
82 void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
Ted Kremenek8e672192010-05-07 01:04:32 +000083 void VisitLinkageSpecDecl(LinkageSpecDecl *D) {
84 IgnoreResults = true;
Ted Kremenek8e672192010-05-07 01:04:32 +000085 }
Douglas Gregor0a35bce2010-09-01 03:07:18 +000086 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
87 IgnoreResults = true;
88 }
89
Ted Kremenek6f153952010-04-15 21:51:13 +000090 /// Generate the string component containing the location of the
91 /// declaration.
Ted Kremenekcbd66f02010-05-06 23:38:28 +000092 bool GenLoc(const Decl *D);
Ted Kremenek6f153952010-04-15 21:51:13 +000093
Ted Kremenek896b70f2010-03-13 02:50:34 +000094 /// String generation methods used both by the visitation methods
95 /// and from other clients that want to directly generate USRs. These
96 /// methods do not construct complete USRs (which incorporate the parents
97 /// of an AST element), but only the fragments concerning the AST element
98 /// itself.
99
Ted Kremenek896b70f2010-03-13 02:50:34 +0000100 /// Generate a USR for an Objective-C class.
101 void GenObjCClass(llvm::StringRef cls);
102 /// Generate a USR for an Objective-C class category.
103 void GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat);
104 /// Generate a USR fragment for an Objective-C instance variable. The
105 /// complete USR can be created by concatenating the USR for the
106 /// encompassing class with this USR fragment.
107 void GenObjCIvar(llvm::StringRef ivar);
108 /// Generate a USR fragment for an Objective-C method.
109 void GenObjCMethod(llvm::StringRef sel, bool isInstanceMethod);
110 /// Generate a USR fragment for an Objective-C property.
111 void GenObjCProperty(llvm::StringRef prop);
112 /// Generate a USR for an Objective-C protocol.
113 void GenObjCProtocol(llvm::StringRef prot);
Ted Kremenek8e672192010-05-07 01:04:32 +0000114
115 void VisitType(QualType T);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000116 void VisitTemplateParameterList(const TemplateParameterList *Params);
117 void VisitTemplateName(TemplateName Name);
118 void VisitTemplateArgument(const TemplateArgument &Arg);
119
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000120 /// Emit a Decl's name using NamedDecl::printName() and return true if
121 /// the decl had no name.
122 bool EmitDeclName(const NamedDecl *D);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000123};
124
Ted Kremenekc50277f2010-01-12 23:33:42 +0000125} // end anonymous namespace
126
Ted Kremenek896b70f2010-03-13 02:50:34 +0000127//===----------------------------------------------------------------------===//
128// Generating USRs from ASTS.
129//===----------------------------------------------------------------------===//
130
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000131bool USRGenerator::EmitDeclName(const NamedDecl *D) {
132 Out.flush();
133 const unsigned startSize = Buf.size();
134 D->printName(Out);
135 Out.flush();
136 const unsigned endSize = Buf.size();
137 return startSize == endSize;
138}
139
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000140static bool InAnonymousNamespace(const Decl *D) {
141 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(D->getDeclContext()))
142 return ND->isAnonymousNamespace();
143 return false;
144}
145
146static inline bool ShouldGenerateLocation(const NamedDecl *D) {
147 return D->getLinkage() != ExternalLinkage && !InAnonymousNamespace(D);
148}
149
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000150void USRGenerator::VisitDeclContext(DeclContext *DC) {
151 if (NamedDecl *D = dyn_cast<NamedDecl>(DC))
152 Visit(D);
153}
154
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000155void USRGenerator::VisitFieldDecl(FieldDecl *D) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000156 VisitDeclContext(D->getDeclContext());
157 Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@");
158 if (EmitDeclName(D)) {
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000159 // Bit fields can be anonymous.
160 IgnoreResults = true;
161 return;
162 }
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000163}
164
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000165void USRGenerator::VisitFunctionDecl(FunctionDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000166 if (ShouldGenerateLocation(D) && GenLoc(D))
167 return;
Ted Kremenekcf999102010-04-29 17:43:29 +0000168
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000169 VisitDeclContext(D->getDeclContext());
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000170 if (FunctionTemplateDecl *FunTmpl = D->getDescribedFunctionTemplate()) {
171 Out << "@FT@";
172 VisitTemplateParameterList(FunTmpl->getTemplateParameters());
173 } else
174 Out << "@F@";
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000175 D->printName(Out);
Ted Kremenek8e672192010-05-07 01:04:32 +0000176
177 ASTContext &Ctx = AU->getASTContext();
178 if (!Ctx.getLangOptions().CPlusPlus || D->isExternC())
179 return;
180
181 // Mangle in type information for the arguments.
182 for (FunctionDecl::param_iterator I = D->param_begin(), E = D->param_end();
183 I != E; ++I) {
184 Out << '#';
185 if (ParmVarDecl *PD = *I)
186 VisitType(PD->getType());
187 }
188 if (D->isVariadic())
189 Out << '.';
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000190 Out << '#';
191 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
192 if (MD->isStatic())
193 Out << 'S';
194 if (unsigned quals = MD->getTypeQualifiers())
195 Out << (char)('0' + quals);
196 }
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000197}
Ted Kremenekc50277f2010-01-12 23:33:42 +0000198
199void USRGenerator::VisitNamedDecl(NamedDecl *D) {
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000200 VisitDeclContext(D->getDeclContext());
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000201 Out << "@";
202
203 if (EmitDeclName(D)) {
204 // The string can be empty if the declaration has no name; e.g., it is
205 // the ParmDecl with no name for declaration of a function pointer type,
206 // e.g.: void (*f)(void *);
207 // In this case, don't generate a USR.
Ted Kremeneke74ef122010-04-16 21:31:52 +0000208 IgnoreResults = true;
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000209 }
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000210}
211
Ted Kremeneke542f772010-04-20 23:15:40 +0000212void USRGenerator::VisitVarDecl(VarDecl *D) {
213 // VarDecls can be declared 'extern' within a function or method body,
214 // but their enclosing DeclContext is the function, not the TU. We need
215 // to check the storage class to correctly generate the USR.
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000216 if (ShouldGenerateLocation(D) && GenLoc(D))
217 return;
218
219 VisitDeclContext(D->getDeclContext());
Ted Kremeneke542f772010-04-20 23:15:40 +0000220
Ted Kremenekcf999102010-04-29 17:43:29 +0000221 // Variables always have simple names.
222 llvm::StringRef s = D->getName();
223
Ted Kremeneke542f772010-04-20 23:15:40 +0000224 // The string can be empty if the declaration has no name; e.g., it is
225 // the ParmDecl with no name for declaration of a function pointer type, e.g.:
Eli Friedmana7e68452010-08-22 01:00:03 +0000226 // void (*f)(void *);
Ted Kremeneke542f772010-04-20 23:15:40 +0000227 // In this case, don't generate a USR.
228 if (s.empty())
229 IgnoreResults = true;
230 else
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000231 Out << '@' << s;
Ted Kremeneke542f772010-04-20 23:15:40 +0000232}
233
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000234void USRGenerator::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
235 GenLoc(D);
236 return;
237}
238
239void USRGenerator::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
240 GenLoc(D);
241 return;
242}
243
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000244void USRGenerator::VisitNamespaceDecl(NamespaceDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000245 if (D->isAnonymousNamespace()) {
246 Out << "@aN";
247 return;
248 }
249
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000250 VisitDeclContext(D->getDeclContext());
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000251 if (!IgnoreResults)
252 Out << "@N@" << D->getName();
Ted Kremenekc50277f2010-01-12 23:33:42 +0000253}
254
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000255void USRGenerator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
256 VisitFunctionDecl(D->getTemplatedDecl());
257}
258
Douglas Gregor39d6f072010-08-31 19:02:00 +0000259void USRGenerator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
260 VisitTagDecl(D->getTemplatedDecl());
261}
262
Douglas Gregor69319002010-08-31 23:48:11 +0000263void USRGenerator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
264 VisitDeclContext(D->getDeclContext());
265 if (!IgnoreResults)
266 Out << "@NA@" << D->getName();
267}
Douglas Gregor39d6f072010-08-31 19:02:00 +0000268
Ted Kremenekc50277f2010-01-12 23:33:42 +0000269void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) {
Ted Kremenek28a7f252010-08-24 23:13:41 +0000270 Decl *container = cast<Decl>(D->getDeclContext());
271
272 // The USR for a method declared in a class extension is based on
273 // the ObjCInterfaceDecl, not the ObjCCategoryDecl.
274 do {
275 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(container))
276 if (CD->IsClassExtension()) {
277 Visit(CD->getClassInterface());
278 break;
279 }
280 Visit(cast<Decl>(D->getDeclContext()));
281 }
282 while (false);
283
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000284 // Ideally we would use 'GenObjCMethod', but this is such a hot path
285 // for Objective-C code that we don't want to use
286 // DeclarationName::getAsString().
287 Out << (D->isInstanceMethod() ? "(im)" : "(cm)");
288 DeclarationName N(D->getSelector());
289 N.printName(Out);
Ted Kremenekc50277f2010-01-12 23:33:42 +0000290}
291
Ted Kremeneke74ef122010-04-16 21:31:52 +0000292void USRGenerator::VisitObjCClassDecl(ObjCClassDecl *D) {
293 // FIXME: @class declarations can refer to multiple classes. We need
294 // to be able to traverse these.
295 IgnoreResults = true;
296}
297
298void USRGenerator::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
299 // FIXME: @protocol declarations can refer to multiple protocols. We need
300 // to be able to traverse these.
301 IgnoreResults = true;
302}
303
Ted Kremenekc50277f2010-01-12 23:33:42 +0000304void USRGenerator::VisitObjCContainerDecl(ObjCContainerDecl *D) {
305 switch (D->getKind()) {
306 default:
307 assert(false && "Invalid ObjC container.");
308 case Decl::ObjCInterface:
309 case Decl::ObjCImplementation:
Ted Kremenek896b70f2010-03-13 02:50:34 +0000310 GenObjCClass(D->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000311 break;
312 case Decl::ObjCCategory: {
313 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
Ted Kremenekebfa3392010-03-19 20:39:03 +0000314 ObjCInterfaceDecl *ID = CD->getClassInterface();
315 if (!ID) {
316 // Handle invalid code where the @interface might not
317 // have been specified.
318 // FIXME: We should be able to generate this USR even if the
319 // @interface isn't available.
320 IgnoreResults = true;
321 return;
322 }
Ted Kremenek28a7f252010-08-24 23:13:41 +0000323 // Specially handle class extensions, which are anonymous categories.
324 // We want to mangle in the location to uniquely distinguish them.
325 if (CD->IsClassExtension()) {
326 Out << "objc(ext)" << ID->getName() << '@';
327 GenLoc(CD);
328 }
329 else
330 GenObjCCategory(ID->getName(), CD->getName());
331
Ted Kremenekc50277f2010-01-12 23:33:42 +0000332 break;
333 }
334 case Decl::ObjCCategoryImpl: {
335 ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
Ted Kremenekebfa3392010-03-19 20:39:03 +0000336 ObjCInterfaceDecl *ID = CD->getClassInterface();
337 if (!ID) {
338 // Handle invalid code where the @interface might not
339 // have been specified.
340 // FIXME: We should be able to generate this USR even if the
341 // @interface isn't available.
342 IgnoreResults = true;
343 return;
344 }
345 GenObjCCategory(ID->getName(), CD->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000346 break;
347 }
348 case Decl::ObjCProtocol:
Ted Kremenek896b70f2010-03-13 02:50:34 +0000349 GenObjCProtocol(cast<ObjCProtocolDecl>(D)->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000350 break;
351 }
352}
353
354void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
355 Visit(cast<Decl>(D->getDeclContext()));
Ted Kremenek896b70f2010-03-13 02:50:34 +0000356 GenObjCProperty(D->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000357}
358
Ted Kremeneke542f772010-04-20 23:15:40 +0000359void USRGenerator::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
360 if (ObjCPropertyDecl *PD = D->getPropertyDecl()) {
361 VisitObjCPropertyDecl(PD);
362 return;
363 }
364
365 IgnoreResults = true;
366}
367
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000368void USRGenerator::VisitTagDecl(TagDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000369 // Add the location of the tag decl to handle resolution across
370 // translation units.
371 if (ShouldGenerateLocation(D) && GenLoc(D))
372 return;
Ted Kremenek8e672192010-05-07 01:04:32 +0000373
Ted Kremenek6f153952010-04-15 21:51:13 +0000374 D = D->getCanonicalDecl();
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000375 VisitDeclContext(D->getDeclContext());
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000376
Douglas Gregor74dbe642010-08-31 19:31:58 +0000377 bool AlreadyStarted = false;
378 if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(D)) {
Douglas Gregor39d6f072010-08-31 19:02:00 +0000379 if (ClassTemplateDecl *ClassTmpl = CXXRecord->getDescribedClassTemplate()) {
Douglas Gregor74dbe642010-08-31 19:31:58 +0000380 AlreadyStarted = true;
Douglas Gregor39d6f072010-08-31 19:02:00 +0000381
382 switch (D->getTagKind()) {
Douglas Gregor74dbe642010-08-31 19:31:58 +0000383 case TTK_Struct: Out << "@ST"; break;
384 case TTK_Class: Out << "@CT"; break;
385 case TTK_Union: Out << "@UT"; break;
386 case TTK_Enum: llvm_unreachable("enum template"); break;
Douglas Gregor39d6f072010-08-31 19:02:00 +0000387 }
388 VisitTemplateParameterList(ClassTmpl->getTemplateParameters());
Douglas Gregor74dbe642010-08-31 19:31:58 +0000389 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
390 = dyn_cast<ClassTemplatePartialSpecializationDecl>(CXXRecord)) {
391 AlreadyStarted = true;
392
393 switch (D->getTagKind()) {
394 case TTK_Struct: Out << "@SP"; break;
395 case TTK_Class: Out << "@CP"; break;
396 case TTK_Union: Out << "@UP"; break;
397 case TTK_Enum: llvm_unreachable("enum partial specialization"); break;
398 }
399 VisitTemplateParameterList(PartialSpec->getTemplateParameters());
Douglas Gregor39d6f072010-08-31 19:02:00 +0000400 }
Douglas Gregor74dbe642010-08-31 19:31:58 +0000401 }
Douglas Gregor39d6f072010-08-31 19:02:00 +0000402
Douglas Gregor74dbe642010-08-31 19:31:58 +0000403 if (!AlreadyStarted) {
Douglas Gregor39d6f072010-08-31 19:02:00 +0000404 switch (D->getTagKind()) {
405 case TTK_Struct: Out << "@S"; break;
406 case TTK_Class: Out << "@C"; break;
407 case TTK_Union: Out << "@U"; break;
408 case TTK_Enum: Out << "@E"; break;
409 }
Ted Kremeneke74ef122010-04-16 21:31:52 +0000410 }
Douglas Gregor39d6f072010-08-31 19:02:00 +0000411
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000412 Out << '@';
413 Out.flush();
414 assert(Buf.size() > 0);
415 const unsigned off = Buf.size() - 1;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000416
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000417 if (EmitDeclName(D)) {
418 if (const TypedefDecl *TD = D->getTypedefForAnonDecl()) {
419 Buf[off] = 'A';
Benjamin Kramer900fc632010-04-17 09:33:03 +0000420 Out << '@' << TD;
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000421 }
422 else
423 Buf[off] = 'a';
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000424 }
Douglas Gregor0ab1e9f2010-09-01 17:32:36 +0000425
426 // For a class template specialization, mangle the template arguments.
427 if (ClassTemplateSpecializationDecl *Spec
428 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
429 const TemplateArgumentList &Args = Spec->getTemplateInstantiationArgs();
430 Out << '>';
431 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
432 Out << '#';
433 VisitTemplateArgument(Args.get(I));
434 }
435 }
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000436}
437
Ted Kremenekc50277f2010-01-12 23:33:42 +0000438void USRGenerator::VisitTypedefDecl(TypedefDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000439 if (ShouldGenerateLocation(D) && GenLoc(D))
440 return;
Ted Kremenekc50277f2010-01-12 23:33:42 +0000441 DeclContext *DC = D->getDeclContext();
442 if (NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
Ted Kremenek896b70f2010-03-13 02:50:34 +0000443 Visit(DCN);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000444 Out << "@T@";
Ted Kremenek6f153952010-04-15 21:51:13 +0000445 Out << D->getName();
446}
447
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000448void USRGenerator::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
449 GenLoc(D);
450 return;
451}
452
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000453bool USRGenerator::GenLoc(const Decl *D) {
454 if (generatedLoc)
455 return IgnoreResults;
456 generatedLoc = true;
457
Ted Kremenek6f153952010-04-15 21:51:13 +0000458 const SourceManager &SM = AU->getSourceManager();
459 SourceLocation L = D->getLocStart();
460 if (L.isInvalid()) {
461 IgnoreResults = true;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000462 return true;
Ted Kremenek6f153952010-04-15 21:51:13 +0000463 }
464 L = SM.getInstantiationLoc(L);
465 const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(L);
466 const FileEntry *FE = SM.getFileEntryForID(Decomposed.first);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000467 if (FE) {
468 llvm::sys::Path P(FE->getName());
469 Out << P.getLast();
470 }
Ted Kremenek6f153952010-04-15 21:51:13 +0000471 else {
472 // This case really isn't interesting.
473 IgnoreResults = true;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000474 return true;
Ted Kremenek6f153952010-04-15 21:51:13 +0000475 }
Ted Kremenekf48b5312010-07-22 11:14:15 +0000476 // Use the offest into the FileID to represent the location. Using
477 // a line/column can cause us to look back at the original source file,
478 // which is expensive.
479 Out << '@' << Decomposed.second;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000480 return IgnoreResults;
Ted Kremenekc50277f2010-01-12 23:33:42 +0000481}
482
Ted Kremenek8e672192010-05-07 01:04:32 +0000483void USRGenerator::VisitType(QualType T) {
484 // This method mangles in USR information for types. It can possibly
485 // just reuse the naming-mangling logic used by codegen, although the
486 // requirements for USRs might not be the same.
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000487 ASTContext &Ctx = AU->getASTContext();
488
Ted Kremenek8e672192010-05-07 01:04:32 +0000489 do {
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000490 T = Ctx.getCanonicalType(T);
Ted Kremenek8e672192010-05-07 01:04:32 +0000491 Qualifiers Q = T.getQualifiers();
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000492 unsigned qVal = 0;
Ted Kremenek8e672192010-05-07 01:04:32 +0000493 if (Q.hasConst())
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000494 qVal |= 0x1;
Ted Kremenek8e672192010-05-07 01:04:32 +0000495 if (Q.hasVolatile())
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000496 qVal |= 0x2;
Ted Kremenek8e672192010-05-07 01:04:32 +0000497 if (Q.hasRestrict())
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000498 qVal |= 0x4;
499 if(qVal)
500 Out << ((char) ('0' + qVal));
Ted Kremenek8e672192010-05-07 01:04:32 +0000501
502 // Mangle in ObjC GC qualifiers?
503
504 if (const PointerType *PT = T->getAs<PointerType>()) {
505 Out << '*';
506 T = PT->getPointeeType();
507 continue;
508 }
509 if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
510 Out << '&';
511 T = RT->getPointeeType();
512 continue;
513 }
514 if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) {
515 Out << 'F';
516 VisitType(FT->getResultType());
517 for (FunctionProtoType::arg_type_iterator
518 I = FT->arg_type_begin(), E = FT->arg_type_end(); I!=E; ++I) {
519 VisitType(*I);
520 }
521 if (FT->isVariadic())
522 Out << '.';
523 return;
524 }
525 if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) {
526 Out << 'B';
527 T = BT->getPointeeType();
528 continue;
529 }
530 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
531 unsigned char c = '\0';
532 switch (BT->getKind()) {
533 case BuiltinType::Void:
534 c = 'v'; break;
535 case BuiltinType::Bool:
536 c = 'b'; break;
537 case BuiltinType::Char_U:
538 case BuiltinType::UChar:
539 c = 'c'; break;
540 case BuiltinType::Char16:
541 c = 'q'; break;
542 case BuiltinType::Char32:
543 c = 'w'; break;
544 case BuiltinType::UShort:
545 c = 's'; break;
546 case BuiltinType::UInt:
547 c = 'i'; break;
548 case BuiltinType::ULong:
549 c = 'l'; break;
550 case BuiltinType::ULongLong:
551 c = 'k'; break;
552 case BuiltinType::UInt128:
553 c = 'j'; break;
554 case BuiltinType::Char_S:
555 case BuiltinType::SChar:
556 c = 'C'; break;
557 case BuiltinType::WChar:
558 c = 'W'; break;
559 case BuiltinType::Short:
560 c = 'S'; break;
561 case BuiltinType::Int:
562 c = 'I'; break;
563 case BuiltinType::Long:
564 c = 'L'; break;
565 case BuiltinType::LongLong:
566 c = 'K'; break;
567 case BuiltinType::Int128:
568 c = 'J'; break;
569 case BuiltinType::Float:
570 c = 'f'; break;
571 case BuiltinType::Double:
572 c = 'd'; break;
573 case BuiltinType::LongDouble:
574 c = 'D'; break;
575 case BuiltinType::NullPtr:
576 c = 'n'; break;
577 case BuiltinType::Overload:
578 case BuiltinType::Dependent:
579 case BuiltinType::UndeducedAuto:
580 IgnoreResults = true;
581 return;
582 case BuiltinType::ObjCId:
583 c = 'o'; break;
584 case BuiltinType::ObjCClass:
585 c = 'O'; break;
586 case BuiltinType::ObjCSel:
587 c = 'e'; break;
588 }
589 Out << c;
590 return;
591 }
592 if (const ComplexType *CT = T->getAs<ComplexType>()) {
593 Out << '<';
594 T = CT->getElementType();
595 continue;
596 }
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000597 if (const TagType *TT = T->getAs<TagType>()) {
598 Out << '$';
599 VisitTagDecl(TT->getDecl());
600 return;
601 }
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000602 if (const TemplateTypeParmType *TTP = T->getAs<TemplateTypeParmType>()) {
603 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
604 return;
605 }
606 if (const TemplateSpecializationType *Spec
607 = T->getAs<TemplateSpecializationType>()) {
608 Out << '>';
609 VisitTemplateName(Spec->getTemplateName());
610 Out << Spec->getNumArgs();
611 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
612 VisitTemplateArgument(Spec->getArg(I));
613 return;
614 }
615
Ted Kremenek8e672192010-05-07 01:04:32 +0000616 // Unhandled type.
617 Out << ' ';
618 break;
619 } while (true);
620}
621
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000622void USRGenerator::VisitTemplateParameterList(
623 const TemplateParameterList *Params) {
624 if (!Params)
625 return;
626 Out << '>' << Params->size();
627 for (TemplateParameterList::const_iterator P = Params->begin(),
628 PEnd = Params->end();
629 P != PEnd; ++P) {
630 Out << '#';
631 if (isa<TemplateTypeParmDecl>(*P)) {
632 Out << 'T';
633 continue;
634 }
635
636 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
637 Out << 'N';
638 VisitType(NTTP->getType());
639 continue;
640 }
641
642 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
643 Out << 't';
644 VisitTemplateParameterList(TTP->getTemplateParameters());
645 }
646}
647
648void USRGenerator::VisitTemplateName(TemplateName Name) {
649 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
650 if (TemplateTemplateParmDecl *TTP
651 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
652 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
653 return;
654 }
655
656 Visit(Template);
657 return;
658 }
659
660 // FIXME: Visit dependent template names.
661}
662
663void USRGenerator::VisitTemplateArgument(const TemplateArgument &Arg) {
664 switch (Arg.getKind()) {
665 case TemplateArgument::Null:
666 break;
667
668 case TemplateArgument::Declaration:
669 Visit(Arg.getAsDecl());
670 break;
671
672 case TemplateArgument::Template:
673 VisitTemplateName(Arg.getAsTemplate());
674 break;
675
676 case TemplateArgument::Expression:
677 // FIXME: Visit expressions.
678 break;
679
680 case TemplateArgument::Pack:
681 // FIXME: Variadic templates
682 break;
683
684 case TemplateArgument::Type:
685 VisitType(Arg.getAsType());
686 break;
687
688 case TemplateArgument::Integral:
689 Out << 'V';
690 VisitType(Arg.getIntegralType());
691 Out << *Arg.getAsIntegral();
692 break;
693 }
694}
695
Ted Kremenek896b70f2010-03-13 02:50:34 +0000696//===----------------------------------------------------------------------===//
697// General purpose USR generation methods.
698//===----------------------------------------------------------------------===//
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000699
Ted Kremenek896b70f2010-03-13 02:50:34 +0000700void USRGenerator::GenObjCClass(llvm::StringRef cls) {
701 Out << "objc(cs)" << cls;
702}
703
704void USRGenerator::GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat) {
Ted Kremeneke74ef122010-04-16 21:31:52 +0000705 Out << "objc(cy)" << cls << '@' << cat;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000706}
707
708void USRGenerator::GenObjCIvar(llvm::StringRef ivar) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000709 Out << '@' << ivar;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000710}
711
712void USRGenerator::GenObjCMethod(llvm::StringRef meth, bool isInstanceMethod) {
713 Out << (isInstanceMethod ? "(im)" : "(cm)") << meth;
714}
715
716void USRGenerator::GenObjCProperty(llvm::StringRef prop) {
717 Out << "(py)" << prop;
718}
719
720void USRGenerator::GenObjCProtocol(llvm::StringRef prot) {
721 Out << "objc(pl)" << prot;
722}
723
724//===----------------------------------------------------------------------===//
725// API hooks.
726//===----------------------------------------------------------------------===//
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000727
Benjamin Kramercfb51b62010-04-08 15:54:07 +0000728static inline llvm::StringRef extractUSRSuffix(llvm::StringRef s) {
729 return s.startswith("c:") ? s.substr(2) : "";
730}
731
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000732static CXString getDeclCursorUSR(const CXCursor &C) {
Ted Kremenek896b70f2010-03-13 02:50:34 +0000733 Decl *D = cxcursor::getCursorDecl(C);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000734
735 // Don't generate USRs for things with invalid locations.
736 if (!D || D->getLocStart().isInvalid())
Ted Kremenek1af0a2a2010-04-17 00:21:38 +0000737 return createCXString("");
Ted Kremenek896b70f2010-03-13 02:50:34 +0000738
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000739 // Check if the cursor has 'NoLinkage'.
Ted Kremeneke74ef122010-04-16 21:31:52 +0000740 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000741 switch (ND->getLinkage()) {
742 case ExternalLinkage:
743 // Generate USRs for all entities with external linkage.
744 break;
745 case NoLinkage:
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000746 case UniqueExternalLinkage:
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000747 // We allow enums, typedefs, and structs that have no linkage to
748 // have USRs that are anchored to the file they were defined in
749 // (e.g., the header). This is a little gross, but in principal
750 // enums/anonymous structs/etc. defined in a common header file
751 // are referred to across multiple translation units.
Ted Kremenek6f153952010-04-15 21:51:13 +0000752 if (isa<TagDecl>(ND) || isa<TypedefDecl>(ND) ||
Ted Kremenekcf999102010-04-29 17:43:29 +0000753 isa<EnumConstantDecl>(ND) || isa<FieldDecl>(ND) ||
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000754 isa<VarDecl>(ND) || isa<NamespaceDecl>(ND))
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000755 break;
756 // Fall-through.
757 case InternalLinkage:
Ted Kremenekcf999102010-04-29 17:43:29 +0000758 if (isa<FunctionDecl>(ND))
759 break;
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000760 }
761
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000762 USRGenerator UG(&C);
763 UG->Visit(D);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000764
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000765 if (UG->ignoreResults())
Ted Kremenekebfa3392010-03-19 20:39:03 +0000766 return createCXString("");
Ted Kremenek896b70f2010-03-13 02:50:34 +0000767
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000768#if 0
Ted Kremeneke542f772010-04-20 23:15:40 +0000769 // For development testing.
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000770 assert(UG.str().size() > 2);
771#endif
Ted Kremeneke542f772010-04-20 23:15:40 +0000772
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000773 // Return a copy of the string that must be disposed by the caller.
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000774 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000775}
776
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000777extern "C" {
778
779CXString clang_getCursorUSR(CXCursor C) {
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000780 const CXCursorKind &K = clang_getCursorKind(C);
781
782 if (clang_isDeclaration(K))
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000783 return getDeclCursorUSR(C);
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000784
785 if (K == CXCursor_MacroDefinition) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000786 USRGenerator UG(&C);
787 UG << "macro@"
788 << cxcursor::getCursorMacroDefinition(C)->getName()->getNameStart();
789 return createCXString(UG.str(), true);
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000790 }
791
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000792 return createCXString("");
793}
794
Ted Kremenek896b70f2010-03-13 02:50:34 +0000795CXString clang_constructUSR_ObjCIvar(const char *name, CXString classUSR) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000796 USRGenerator UG;
797 UG << extractUSRSuffix(clang_getCString(classUSR));
798 UG->GenObjCIvar(name);
799 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000800}
801
802CXString clang_constructUSR_ObjCMethod(const char *name,
803 unsigned isInstanceMethod,
804 CXString classUSR) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000805 USRGenerator UG;
806 UG << extractUSRSuffix(clang_getCString(classUSR));
807 UG->GenObjCMethod(name, isInstanceMethod);
808 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000809}
810
811CXString clang_constructUSR_ObjCClass(const char *name) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000812 USRGenerator UG;
813 UG->GenObjCClass(name);
814 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000815}
816
817CXString clang_constructUSR_ObjCProtocol(const char *name) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000818 USRGenerator UG;
819 UG->GenObjCProtocol(name);
820 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000821}
822
Ted Kremenek66ccaec2010-03-15 17:38:58 +0000823CXString clang_constructUSR_ObjCCategory(const char *class_name,
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000824 const char *category_name) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000825 USRGenerator UG;
826 UG->GenObjCCategory(class_name, category_name);
827 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000828}
829
830CXString clang_constructUSR_ObjCProperty(const char *property,
831 CXString classUSR) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000832 USRGenerator UG;
833 UG << extractUSRSuffix(clang_getCString(classUSR));
834 UG->GenObjCProperty(property);
835 return createCXString(UG.str(), true);
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000836}
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000837
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000838} // end extern "C"