blob: 9ef851aeaa2d1cfe87cd67a680dcbcd3ed6fd226 [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;
85 return;
86 }
Ted Kremenek896b70f2010-03-13 02:50:34 +000087
Ted Kremenek6f153952010-04-15 21:51:13 +000088 /// Generate the string component containing the location of the
89 /// declaration.
Ted Kremenekcbd66f02010-05-06 23:38:28 +000090 bool GenLoc(const Decl *D);
Ted Kremenek6f153952010-04-15 21:51:13 +000091
Ted Kremenek896b70f2010-03-13 02:50:34 +000092 /// String generation methods used both by the visitation methods
93 /// and from other clients that want to directly generate USRs. These
94 /// methods do not construct complete USRs (which incorporate the parents
95 /// of an AST element), but only the fragments concerning the AST element
96 /// itself.
97
Ted Kremenek896b70f2010-03-13 02:50:34 +000098 /// Generate a USR for an Objective-C class.
99 void GenObjCClass(llvm::StringRef cls);
100 /// Generate a USR for an Objective-C class category.
101 void GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat);
102 /// Generate a USR fragment for an Objective-C instance variable. The
103 /// complete USR can be created by concatenating the USR for the
104 /// encompassing class with this USR fragment.
105 void GenObjCIvar(llvm::StringRef ivar);
106 /// Generate a USR fragment for an Objective-C method.
107 void GenObjCMethod(llvm::StringRef sel, bool isInstanceMethod);
108 /// Generate a USR fragment for an Objective-C property.
109 void GenObjCProperty(llvm::StringRef prop);
110 /// Generate a USR for an Objective-C protocol.
111 void GenObjCProtocol(llvm::StringRef prot);
Ted Kremenek8e672192010-05-07 01:04:32 +0000112
113 void VisitType(QualType T);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000114 void VisitTemplateParameterList(const TemplateParameterList *Params);
115 void VisitTemplateName(TemplateName Name);
116 void VisitTemplateArgument(const TemplateArgument &Arg);
117
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000118 /// Emit a Decl's name using NamedDecl::printName() and return true if
119 /// the decl had no name.
120 bool EmitDeclName(const NamedDecl *D);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000121};
122
Ted Kremenekc50277f2010-01-12 23:33:42 +0000123} // end anonymous namespace
124
Ted Kremenek896b70f2010-03-13 02:50:34 +0000125//===----------------------------------------------------------------------===//
126// Generating USRs from ASTS.
127//===----------------------------------------------------------------------===//
128
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000129bool USRGenerator::EmitDeclName(const NamedDecl *D) {
130 Out.flush();
131 const unsigned startSize = Buf.size();
132 D->printName(Out);
133 Out.flush();
134 const unsigned endSize = Buf.size();
135 return startSize == endSize;
136}
137
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000138static bool InAnonymousNamespace(const Decl *D) {
139 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(D->getDeclContext()))
140 return ND->isAnonymousNamespace();
141 return false;
142}
143
144static inline bool ShouldGenerateLocation(const NamedDecl *D) {
145 return D->getLinkage() != ExternalLinkage && !InAnonymousNamespace(D);
146}
147
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000148void USRGenerator::VisitDeclContext(DeclContext *DC) {
149 if (NamedDecl *D = dyn_cast<NamedDecl>(DC))
150 Visit(D);
151}
152
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000153void USRGenerator::VisitFieldDecl(FieldDecl *D) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000154 VisitDeclContext(D->getDeclContext());
155 Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@");
156 if (EmitDeclName(D)) {
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000157 // Bit fields can be anonymous.
158 IgnoreResults = true;
159 return;
160 }
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000161}
162
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000163void USRGenerator::VisitFunctionDecl(FunctionDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000164 if (ShouldGenerateLocation(D) && GenLoc(D))
165 return;
Ted Kremenekcf999102010-04-29 17:43:29 +0000166
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000167 VisitDeclContext(D->getDeclContext());
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000168 if (FunctionTemplateDecl *FunTmpl = D->getDescribedFunctionTemplate()) {
169 Out << "@FT@";
170 VisitTemplateParameterList(FunTmpl->getTemplateParameters());
171 } else
172 Out << "@F@";
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000173 D->printName(Out);
Ted Kremenek8e672192010-05-07 01:04:32 +0000174
175 ASTContext &Ctx = AU->getASTContext();
176 if (!Ctx.getLangOptions().CPlusPlus || D->isExternC())
177 return;
178
179 // Mangle in type information for the arguments.
180 for (FunctionDecl::param_iterator I = D->param_begin(), E = D->param_end();
181 I != E; ++I) {
182 Out << '#';
183 if (ParmVarDecl *PD = *I)
184 VisitType(PD->getType());
185 }
186 if (D->isVariadic())
187 Out << '.';
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000188 Out << '#';
189 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
190 if (MD->isStatic())
191 Out << 'S';
192 if (unsigned quals = MD->getTypeQualifiers())
193 Out << (char)('0' + quals);
194 }
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000195}
Ted Kremenekc50277f2010-01-12 23:33:42 +0000196
197void USRGenerator::VisitNamedDecl(NamedDecl *D) {
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000198 VisitDeclContext(D->getDeclContext());
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000199 Out << "@";
200
201 if (EmitDeclName(D)) {
202 // The string can be empty if the declaration has no name; e.g., it is
203 // the ParmDecl with no name for declaration of a function pointer type,
204 // e.g.: void (*f)(void *);
205 // In this case, don't generate a USR.
Ted Kremeneke74ef122010-04-16 21:31:52 +0000206 IgnoreResults = true;
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000207 }
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000208}
209
Ted Kremeneke542f772010-04-20 23:15:40 +0000210void USRGenerator::VisitVarDecl(VarDecl *D) {
211 // VarDecls can be declared 'extern' within a function or method body,
212 // but their enclosing DeclContext is the function, not the TU. We need
213 // to check the storage class to correctly generate the USR.
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000214 if (ShouldGenerateLocation(D) && GenLoc(D))
215 return;
216
217 VisitDeclContext(D->getDeclContext());
Ted Kremeneke542f772010-04-20 23:15:40 +0000218
Ted Kremenekcf999102010-04-29 17:43:29 +0000219 // Variables always have simple names.
220 llvm::StringRef s = D->getName();
221
Ted Kremeneke542f772010-04-20 23:15:40 +0000222 // The string can be empty if the declaration has no name; e.g., it is
223 // the ParmDecl with no name for declaration of a function pointer type, e.g.:
Eli Friedmana7e68452010-08-22 01:00:03 +0000224 // void (*f)(void *);
Ted Kremeneke542f772010-04-20 23:15:40 +0000225 // In this case, don't generate a USR.
226 if (s.empty())
227 IgnoreResults = true;
228 else
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000229 Out << '@' << s;
Ted Kremeneke542f772010-04-20 23:15:40 +0000230}
231
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000232void USRGenerator::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
233 GenLoc(D);
234 return;
235}
236
237void USRGenerator::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
238 GenLoc(D);
239 return;
240}
241
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000242void USRGenerator::VisitNamespaceDecl(NamespaceDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000243 if (D->isAnonymousNamespace()) {
244 Out << "@aN";
245 return;
246 }
247
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000248 VisitDeclContext(D->getDeclContext());
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000249 if (!IgnoreResults)
250 Out << "@N@" << D->getName();
Ted Kremenekc50277f2010-01-12 23:33:42 +0000251}
252
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000253void USRGenerator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
254 VisitFunctionDecl(D->getTemplatedDecl());
255}
256
Douglas Gregor39d6f072010-08-31 19:02:00 +0000257void USRGenerator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
258 VisitTagDecl(D->getTemplatedDecl());
259}
260
Douglas Gregor69319002010-08-31 23:48:11 +0000261void USRGenerator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
262 VisitDeclContext(D->getDeclContext());
263 if (!IgnoreResults)
264 Out << "@NA@" << D->getName();
265}
Douglas Gregor39d6f072010-08-31 19:02:00 +0000266
Ted Kremenekc50277f2010-01-12 23:33:42 +0000267void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) {
Ted Kremenek28a7f252010-08-24 23:13:41 +0000268 Decl *container = cast<Decl>(D->getDeclContext());
269
270 // The USR for a method declared in a class extension is based on
271 // the ObjCInterfaceDecl, not the ObjCCategoryDecl.
272 do {
273 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(container))
274 if (CD->IsClassExtension()) {
275 Visit(CD->getClassInterface());
276 break;
277 }
278 Visit(cast<Decl>(D->getDeclContext()));
279 }
280 while (false);
281
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000282 // Ideally we would use 'GenObjCMethod', but this is such a hot path
283 // for Objective-C code that we don't want to use
284 // DeclarationName::getAsString().
285 Out << (D->isInstanceMethod() ? "(im)" : "(cm)");
286 DeclarationName N(D->getSelector());
287 N.printName(Out);
Ted Kremenekc50277f2010-01-12 23:33:42 +0000288}
289
Ted Kremeneke74ef122010-04-16 21:31:52 +0000290void USRGenerator::VisitObjCClassDecl(ObjCClassDecl *D) {
291 // FIXME: @class declarations can refer to multiple classes. We need
292 // to be able to traverse these.
293 IgnoreResults = true;
294}
295
296void USRGenerator::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
297 // FIXME: @protocol declarations can refer to multiple protocols. We need
298 // to be able to traverse these.
299 IgnoreResults = true;
300}
301
Ted Kremenekc50277f2010-01-12 23:33:42 +0000302void USRGenerator::VisitObjCContainerDecl(ObjCContainerDecl *D) {
303 switch (D->getKind()) {
304 default:
305 assert(false && "Invalid ObjC container.");
306 case Decl::ObjCInterface:
307 case Decl::ObjCImplementation:
Ted Kremenek896b70f2010-03-13 02:50:34 +0000308 GenObjCClass(D->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000309 break;
310 case Decl::ObjCCategory: {
311 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
Ted Kremenekebfa3392010-03-19 20:39:03 +0000312 ObjCInterfaceDecl *ID = CD->getClassInterface();
313 if (!ID) {
314 // Handle invalid code where the @interface might not
315 // have been specified.
316 // FIXME: We should be able to generate this USR even if the
317 // @interface isn't available.
318 IgnoreResults = true;
319 return;
320 }
Ted Kremenek28a7f252010-08-24 23:13:41 +0000321 // Specially handle class extensions, which are anonymous categories.
322 // We want to mangle in the location to uniquely distinguish them.
323 if (CD->IsClassExtension()) {
324 Out << "objc(ext)" << ID->getName() << '@';
325 GenLoc(CD);
326 }
327 else
328 GenObjCCategory(ID->getName(), CD->getName());
329
Ted Kremenekc50277f2010-01-12 23:33:42 +0000330 break;
331 }
332 case Decl::ObjCCategoryImpl: {
333 ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
Ted Kremenekebfa3392010-03-19 20:39:03 +0000334 ObjCInterfaceDecl *ID = CD->getClassInterface();
335 if (!ID) {
336 // Handle invalid code where the @interface might not
337 // have been specified.
338 // FIXME: We should be able to generate this USR even if the
339 // @interface isn't available.
340 IgnoreResults = true;
341 return;
342 }
343 GenObjCCategory(ID->getName(), CD->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000344 break;
345 }
346 case Decl::ObjCProtocol:
Ted Kremenek896b70f2010-03-13 02:50:34 +0000347 GenObjCProtocol(cast<ObjCProtocolDecl>(D)->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000348 break;
349 }
350}
351
352void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
353 Visit(cast<Decl>(D->getDeclContext()));
Ted Kremenek896b70f2010-03-13 02:50:34 +0000354 GenObjCProperty(D->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000355}
356
Ted Kremeneke542f772010-04-20 23:15:40 +0000357void USRGenerator::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
358 if (ObjCPropertyDecl *PD = D->getPropertyDecl()) {
359 VisitObjCPropertyDecl(PD);
360 return;
361 }
362
363 IgnoreResults = true;
364}
365
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000366void USRGenerator::VisitTagDecl(TagDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000367 // Add the location of the tag decl to handle resolution across
368 // translation units.
369 if (ShouldGenerateLocation(D) && GenLoc(D))
370 return;
Ted Kremenek8e672192010-05-07 01:04:32 +0000371
Ted Kremenek6f153952010-04-15 21:51:13 +0000372 D = D->getCanonicalDecl();
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000373 VisitDeclContext(D->getDeclContext());
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000374
Douglas Gregor74dbe642010-08-31 19:31:58 +0000375 bool AlreadyStarted = false;
376 if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(D)) {
Douglas Gregor39d6f072010-08-31 19:02:00 +0000377 if (ClassTemplateDecl *ClassTmpl = CXXRecord->getDescribedClassTemplate()) {
Douglas Gregor74dbe642010-08-31 19:31:58 +0000378 AlreadyStarted = true;
Douglas Gregor39d6f072010-08-31 19:02:00 +0000379
380 switch (D->getTagKind()) {
Douglas Gregor74dbe642010-08-31 19:31:58 +0000381 case TTK_Struct: Out << "@ST"; break;
382 case TTK_Class: Out << "@CT"; break;
383 case TTK_Union: Out << "@UT"; break;
384 case TTK_Enum: llvm_unreachable("enum template"); break;
Douglas Gregor39d6f072010-08-31 19:02:00 +0000385 }
386 VisitTemplateParameterList(ClassTmpl->getTemplateParameters());
Douglas Gregor74dbe642010-08-31 19:31:58 +0000387 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
388 = dyn_cast<ClassTemplatePartialSpecializationDecl>(CXXRecord)) {
389 AlreadyStarted = true;
390
391 switch (D->getTagKind()) {
392 case TTK_Struct: Out << "@SP"; break;
393 case TTK_Class: Out << "@CP"; break;
394 case TTK_Union: Out << "@UP"; break;
395 case TTK_Enum: llvm_unreachable("enum partial specialization"); break;
396 }
397 VisitTemplateParameterList(PartialSpec->getTemplateParameters());
Douglas Gregor39d6f072010-08-31 19:02:00 +0000398 }
Douglas Gregor74dbe642010-08-31 19:31:58 +0000399 }
Douglas Gregor39d6f072010-08-31 19:02:00 +0000400
Douglas Gregor74dbe642010-08-31 19:31:58 +0000401 if (!AlreadyStarted) {
Douglas Gregor39d6f072010-08-31 19:02:00 +0000402 switch (D->getTagKind()) {
403 case TTK_Struct: Out << "@S"; break;
404 case TTK_Class: Out << "@C"; break;
405 case TTK_Union: Out << "@U"; break;
406 case TTK_Enum: Out << "@E"; break;
407 }
Ted Kremeneke74ef122010-04-16 21:31:52 +0000408 }
Douglas Gregor39d6f072010-08-31 19:02:00 +0000409
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000410 Out << '@';
411 Out.flush();
412 assert(Buf.size() > 0);
413 const unsigned off = Buf.size() - 1;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000414
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000415 if (EmitDeclName(D)) {
416 if (const TypedefDecl *TD = D->getTypedefForAnonDecl()) {
417 Buf[off] = 'A';
Benjamin Kramer900fc632010-04-17 09:33:03 +0000418 Out << '@' << TD;
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000419 }
420 else
421 Buf[off] = 'a';
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000422 }
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000423}
424
Ted Kremenekc50277f2010-01-12 23:33:42 +0000425void USRGenerator::VisitTypedefDecl(TypedefDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000426 if (ShouldGenerateLocation(D) && GenLoc(D))
427 return;
Ted Kremenekc50277f2010-01-12 23:33:42 +0000428 DeclContext *DC = D->getDeclContext();
429 if (NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
Ted Kremenek896b70f2010-03-13 02:50:34 +0000430 Visit(DCN);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000431 Out << "@T@";
Ted Kremenek6f153952010-04-15 21:51:13 +0000432 Out << D->getName();
433}
434
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000435void USRGenerator::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
436 GenLoc(D);
437 return;
438}
439
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000440bool USRGenerator::GenLoc(const Decl *D) {
441 if (generatedLoc)
442 return IgnoreResults;
443 generatedLoc = true;
444
Ted Kremenek6f153952010-04-15 21:51:13 +0000445 const SourceManager &SM = AU->getSourceManager();
446 SourceLocation L = D->getLocStart();
447 if (L.isInvalid()) {
448 IgnoreResults = true;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000449 return true;
Ted Kremenek6f153952010-04-15 21:51:13 +0000450 }
451 L = SM.getInstantiationLoc(L);
452 const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(L);
453 const FileEntry *FE = SM.getFileEntryForID(Decomposed.first);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000454 if (FE) {
455 llvm::sys::Path P(FE->getName());
456 Out << P.getLast();
457 }
Ted Kremenek6f153952010-04-15 21:51:13 +0000458 else {
459 // This case really isn't interesting.
460 IgnoreResults = true;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000461 return true;
Ted Kremenek6f153952010-04-15 21:51:13 +0000462 }
Ted Kremenekf48b5312010-07-22 11:14:15 +0000463 // Use the offest into the FileID to represent the location. Using
464 // a line/column can cause us to look back at the original source file,
465 // which is expensive.
466 Out << '@' << Decomposed.second;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000467 return IgnoreResults;
Ted Kremenekc50277f2010-01-12 23:33:42 +0000468}
469
Ted Kremenek8e672192010-05-07 01:04:32 +0000470void USRGenerator::VisitType(QualType T) {
471 // This method mangles in USR information for types. It can possibly
472 // just reuse the naming-mangling logic used by codegen, although the
473 // requirements for USRs might not be the same.
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000474 ASTContext &Ctx = AU->getASTContext();
475
Ted Kremenek8e672192010-05-07 01:04:32 +0000476 do {
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000477 T = Ctx.getCanonicalType(T);
Ted Kremenek8e672192010-05-07 01:04:32 +0000478 Qualifiers Q = T.getQualifiers();
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000479 unsigned qVal = 0;
Ted Kremenek8e672192010-05-07 01:04:32 +0000480 if (Q.hasConst())
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000481 qVal |= 0x1;
Ted Kremenek8e672192010-05-07 01:04:32 +0000482 if (Q.hasVolatile())
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000483 qVal |= 0x2;
Ted Kremenek8e672192010-05-07 01:04:32 +0000484 if (Q.hasRestrict())
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000485 qVal |= 0x4;
486 if(qVal)
487 Out << ((char) ('0' + qVal));
Ted Kremenek8e672192010-05-07 01:04:32 +0000488
489 // Mangle in ObjC GC qualifiers?
490
491 if (const PointerType *PT = T->getAs<PointerType>()) {
492 Out << '*';
493 T = PT->getPointeeType();
494 continue;
495 }
496 if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
497 Out << '&';
498 T = RT->getPointeeType();
499 continue;
500 }
501 if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) {
502 Out << 'F';
503 VisitType(FT->getResultType());
504 for (FunctionProtoType::arg_type_iterator
505 I = FT->arg_type_begin(), E = FT->arg_type_end(); I!=E; ++I) {
506 VisitType(*I);
507 }
508 if (FT->isVariadic())
509 Out << '.';
510 return;
511 }
512 if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) {
513 Out << 'B';
514 T = BT->getPointeeType();
515 continue;
516 }
517 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
518 unsigned char c = '\0';
519 switch (BT->getKind()) {
520 case BuiltinType::Void:
521 c = 'v'; break;
522 case BuiltinType::Bool:
523 c = 'b'; break;
524 case BuiltinType::Char_U:
525 case BuiltinType::UChar:
526 c = 'c'; break;
527 case BuiltinType::Char16:
528 c = 'q'; break;
529 case BuiltinType::Char32:
530 c = 'w'; break;
531 case BuiltinType::UShort:
532 c = 's'; break;
533 case BuiltinType::UInt:
534 c = 'i'; break;
535 case BuiltinType::ULong:
536 c = 'l'; break;
537 case BuiltinType::ULongLong:
538 c = 'k'; break;
539 case BuiltinType::UInt128:
540 c = 'j'; break;
541 case BuiltinType::Char_S:
542 case BuiltinType::SChar:
543 c = 'C'; break;
544 case BuiltinType::WChar:
545 c = 'W'; break;
546 case BuiltinType::Short:
547 c = 'S'; break;
548 case BuiltinType::Int:
549 c = 'I'; break;
550 case BuiltinType::Long:
551 c = 'L'; break;
552 case BuiltinType::LongLong:
553 c = 'K'; break;
554 case BuiltinType::Int128:
555 c = 'J'; break;
556 case BuiltinType::Float:
557 c = 'f'; break;
558 case BuiltinType::Double:
559 c = 'd'; break;
560 case BuiltinType::LongDouble:
561 c = 'D'; break;
562 case BuiltinType::NullPtr:
563 c = 'n'; break;
564 case BuiltinType::Overload:
565 case BuiltinType::Dependent:
566 case BuiltinType::UndeducedAuto:
567 IgnoreResults = true;
568 return;
569 case BuiltinType::ObjCId:
570 c = 'o'; break;
571 case BuiltinType::ObjCClass:
572 c = 'O'; break;
573 case BuiltinType::ObjCSel:
574 c = 'e'; break;
575 }
576 Out << c;
577 return;
578 }
579 if (const ComplexType *CT = T->getAs<ComplexType>()) {
580 Out << '<';
581 T = CT->getElementType();
582 continue;
583 }
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000584 if (const TagType *TT = T->getAs<TagType>()) {
585 Out << '$';
586 VisitTagDecl(TT->getDecl());
587 return;
588 }
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000589 if (const TemplateTypeParmType *TTP = T->getAs<TemplateTypeParmType>()) {
590 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
591 return;
592 }
593 if (const TemplateSpecializationType *Spec
594 = T->getAs<TemplateSpecializationType>()) {
595 Out << '>';
596 VisitTemplateName(Spec->getTemplateName());
597 Out << Spec->getNumArgs();
598 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
599 VisitTemplateArgument(Spec->getArg(I));
600 return;
601 }
602
Ted Kremenek8e672192010-05-07 01:04:32 +0000603 // Unhandled type.
604 Out << ' ';
605 break;
606 } while (true);
607}
608
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000609void USRGenerator::VisitTemplateParameterList(
610 const TemplateParameterList *Params) {
611 if (!Params)
612 return;
613 Out << '>' << Params->size();
614 for (TemplateParameterList::const_iterator P = Params->begin(),
615 PEnd = Params->end();
616 P != PEnd; ++P) {
617 Out << '#';
618 if (isa<TemplateTypeParmDecl>(*P)) {
619 Out << 'T';
620 continue;
621 }
622
623 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
624 Out << 'N';
625 VisitType(NTTP->getType());
626 continue;
627 }
628
629 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
630 Out << 't';
631 VisitTemplateParameterList(TTP->getTemplateParameters());
632 }
633}
634
635void USRGenerator::VisitTemplateName(TemplateName Name) {
636 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
637 if (TemplateTemplateParmDecl *TTP
638 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
639 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
640 return;
641 }
642
643 Visit(Template);
644 return;
645 }
646
647 // FIXME: Visit dependent template names.
648}
649
650void USRGenerator::VisitTemplateArgument(const TemplateArgument &Arg) {
651 switch (Arg.getKind()) {
652 case TemplateArgument::Null:
653 break;
654
655 case TemplateArgument::Declaration:
656 Visit(Arg.getAsDecl());
657 break;
658
659 case TemplateArgument::Template:
660 VisitTemplateName(Arg.getAsTemplate());
661 break;
662
663 case TemplateArgument::Expression:
664 // FIXME: Visit expressions.
665 break;
666
667 case TemplateArgument::Pack:
668 // FIXME: Variadic templates
669 break;
670
671 case TemplateArgument::Type:
672 VisitType(Arg.getAsType());
673 break;
674
675 case TemplateArgument::Integral:
676 Out << 'V';
677 VisitType(Arg.getIntegralType());
678 Out << *Arg.getAsIntegral();
679 break;
680 }
681}
682
Ted Kremenek896b70f2010-03-13 02:50:34 +0000683//===----------------------------------------------------------------------===//
684// General purpose USR generation methods.
685//===----------------------------------------------------------------------===//
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000686
Ted Kremenek896b70f2010-03-13 02:50:34 +0000687void USRGenerator::GenObjCClass(llvm::StringRef cls) {
688 Out << "objc(cs)" << cls;
689}
690
691void USRGenerator::GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat) {
Ted Kremeneke74ef122010-04-16 21:31:52 +0000692 Out << "objc(cy)" << cls << '@' << cat;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000693}
694
695void USRGenerator::GenObjCIvar(llvm::StringRef ivar) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000696 Out << '@' << ivar;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000697}
698
699void USRGenerator::GenObjCMethod(llvm::StringRef meth, bool isInstanceMethod) {
700 Out << (isInstanceMethod ? "(im)" : "(cm)") << meth;
701}
702
703void USRGenerator::GenObjCProperty(llvm::StringRef prop) {
704 Out << "(py)" << prop;
705}
706
707void USRGenerator::GenObjCProtocol(llvm::StringRef prot) {
708 Out << "objc(pl)" << prot;
709}
710
711//===----------------------------------------------------------------------===//
712// API hooks.
713//===----------------------------------------------------------------------===//
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000714
Benjamin Kramercfb51b62010-04-08 15:54:07 +0000715static inline llvm::StringRef extractUSRSuffix(llvm::StringRef s) {
716 return s.startswith("c:") ? s.substr(2) : "";
717}
718
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000719static CXString getDeclCursorUSR(const CXCursor &C) {
Ted Kremenek896b70f2010-03-13 02:50:34 +0000720 Decl *D = cxcursor::getCursorDecl(C);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000721
722 // Don't generate USRs for things with invalid locations.
723 if (!D || D->getLocStart().isInvalid())
Ted Kremenek1af0a2a2010-04-17 00:21:38 +0000724 return createCXString("");
Ted Kremenek896b70f2010-03-13 02:50:34 +0000725
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000726 // Check if the cursor has 'NoLinkage'.
Ted Kremeneke74ef122010-04-16 21:31:52 +0000727 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000728 switch (ND->getLinkage()) {
729 case ExternalLinkage:
730 // Generate USRs for all entities with external linkage.
731 break;
732 case NoLinkage:
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000733 case UniqueExternalLinkage:
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000734 // We allow enums, typedefs, and structs that have no linkage to
735 // have USRs that are anchored to the file they were defined in
736 // (e.g., the header). This is a little gross, but in principal
737 // enums/anonymous structs/etc. defined in a common header file
738 // are referred to across multiple translation units.
Ted Kremenek6f153952010-04-15 21:51:13 +0000739 if (isa<TagDecl>(ND) || isa<TypedefDecl>(ND) ||
Ted Kremenekcf999102010-04-29 17:43:29 +0000740 isa<EnumConstantDecl>(ND) || isa<FieldDecl>(ND) ||
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000741 isa<VarDecl>(ND) || isa<NamespaceDecl>(ND))
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000742 break;
743 // Fall-through.
744 case InternalLinkage:
Ted Kremenekcf999102010-04-29 17:43:29 +0000745 if (isa<FunctionDecl>(ND))
746 break;
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000747 }
748
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000749 USRGenerator UG(&C);
750 UG->Visit(D);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000751
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000752 if (UG->ignoreResults())
Ted Kremenekebfa3392010-03-19 20:39:03 +0000753 return createCXString("");
Ted Kremenek896b70f2010-03-13 02:50:34 +0000754
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000755#if 0
Ted Kremeneke542f772010-04-20 23:15:40 +0000756 // For development testing.
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000757 assert(UG.str().size() > 2);
758#endif
Ted Kremeneke542f772010-04-20 23:15:40 +0000759
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000760 // Return a copy of the string that must be disposed by the caller.
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000761 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000762}
763
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000764extern "C" {
765
766CXString clang_getCursorUSR(CXCursor C) {
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000767 const CXCursorKind &K = clang_getCursorKind(C);
768
769 if (clang_isDeclaration(K))
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000770 return getDeclCursorUSR(C);
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000771
772 if (K == CXCursor_MacroDefinition) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000773 USRGenerator UG(&C);
774 UG << "macro@"
775 << cxcursor::getCursorMacroDefinition(C)->getName()->getNameStart();
776 return createCXString(UG.str(), true);
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000777 }
778
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000779 return createCXString("");
780}
781
Ted Kremenek896b70f2010-03-13 02:50:34 +0000782CXString clang_constructUSR_ObjCIvar(const char *name, CXString classUSR) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000783 USRGenerator UG;
784 UG << extractUSRSuffix(clang_getCString(classUSR));
785 UG->GenObjCIvar(name);
786 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000787}
788
789CXString clang_constructUSR_ObjCMethod(const char *name,
790 unsigned isInstanceMethod,
791 CXString classUSR) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000792 USRGenerator UG;
793 UG << extractUSRSuffix(clang_getCString(classUSR));
794 UG->GenObjCMethod(name, isInstanceMethod);
795 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000796}
797
798CXString clang_constructUSR_ObjCClass(const char *name) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000799 USRGenerator UG;
800 UG->GenObjCClass(name);
801 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000802}
803
804CXString clang_constructUSR_ObjCProtocol(const char *name) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000805 USRGenerator UG;
806 UG->GenObjCProtocol(name);
807 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000808}
809
Ted Kremenek66ccaec2010-03-15 17:38:58 +0000810CXString clang_constructUSR_ObjCCategory(const char *class_name,
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000811 const char *category_name) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000812 USRGenerator UG;
813 UG->GenObjCCategory(class_name, category_name);
814 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000815}
816
817CXString clang_constructUSR_ObjCProperty(const char *property,
818 CXString classUSR) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000819 USRGenerator UG;
820 UG << extractUSRSuffix(clang_getCString(classUSR));
821 UG->GenObjCProperty(property);
822 return createCXString(UG.str(), true);
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000823}
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000824
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000825} // end extern "C"