blob: 554165754ace93234d1e865f6cca6695c473ad54 [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()) {
289 Visit(CD->getClassInterface());
290 break;
291 }
292 Visit(cast<Decl>(D->getDeclContext()));
293 }
294 while (false);
295
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000296 // Ideally we would use 'GenObjCMethod', but this is such a hot path
297 // for Objective-C code that we don't want to use
298 // DeclarationName::getAsString().
299 Out << (D->isInstanceMethod() ? "(im)" : "(cm)");
300 DeclarationName N(D->getSelector());
301 N.printName(Out);
Ted Kremenekc50277f2010-01-12 23:33:42 +0000302}
303
Ted Kremeneke74ef122010-04-16 21:31:52 +0000304void USRGenerator::VisitObjCClassDecl(ObjCClassDecl *D) {
305 // FIXME: @class declarations can refer to multiple classes. We need
306 // to be able to traverse these.
307 IgnoreResults = true;
308}
309
310void USRGenerator::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
311 // FIXME: @protocol declarations can refer to multiple protocols. We need
312 // to be able to traverse these.
313 IgnoreResults = true;
314}
315
Ted Kremenekc50277f2010-01-12 23:33:42 +0000316void USRGenerator::VisitObjCContainerDecl(ObjCContainerDecl *D) {
317 switch (D->getKind()) {
318 default:
319 assert(false && "Invalid ObjC container.");
320 case Decl::ObjCInterface:
321 case Decl::ObjCImplementation:
Ted Kremenek896b70f2010-03-13 02:50:34 +0000322 GenObjCClass(D->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000323 break;
324 case Decl::ObjCCategory: {
325 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
Ted Kremenekebfa3392010-03-19 20:39:03 +0000326 ObjCInterfaceDecl *ID = CD->getClassInterface();
327 if (!ID) {
328 // Handle invalid code where the @interface might not
329 // have been specified.
330 // FIXME: We should be able to generate this USR even if the
331 // @interface isn't available.
332 IgnoreResults = true;
333 return;
334 }
Ted Kremenek28a7f252010-08-24 23:13:41 +0000335 // Specially handle class extensions, which are anonymous categories.
336 // We want to mangle in the location to uniquely distinguish them.
337 if (CD->IsClassExtension()) {
338 Out << "objc(ext)" << ID->getName() << '@';
339 GenLoc(CD);
340 }
341 else
342 GenObjCCategory(ID->getName(), CD->getName());
343
Ted Kremenekc50277f2010-01-12 23:33:42 +0000344 break;
345 }
346 case Decl::ObjCCategoryImpl: {
347 ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
Ted Kremenekebfa3392010-03-19 20:39:03 +0000348 ObjCInterfaceDecl *ID = CD->getClassInterface();
349 if (!ID) {
350 // Handle invalid code where the @interface might not
351 // have been specified.
352 // FIXME: We should be able to generate this USR even if the
353 // @interface isn't available.
354 IgnoreResults = true;
355 return;
356 }
357 GenObjCCategory(ID->getName(), CD->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000358 break;
359 }
360 case Decl::ObjCProtocol:
Ted Kremenek896b70f2010-03-13 02:50:34 +0000361 GenObjCProtocol(cast<ObjCProtocolDecl>(D)->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000362 break;
363 }
364}
365
366void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
367 Visit(cast<Decl>(D->getDeclContext()));
Ted Kremenek896b70f2010-03-13 02:50:34 +0000368 GenObjCProperty(D->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000369}
370
Ted Kremeneke542f772010-04-20 23:15:40 +0000371void USRGenerator::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
372 if (ObjCPropertyDecl *PD = D->getPropertyDecl()) {
373 VisitObjCPropertyDecl(PD);
374 return;
375 }
376
377 IgnoreResults = true;
378}
379
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000380void USRGenerator::VisitTagDecl(TagDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000381 // Add the location of the tag decl to handle resolution across
382 // translation units.
383 if (ShouldGenerateLocation(D) && GenLoc(D))
384 return;
Ted Kremenek8e672192010-05-07 01:04:32 +0000385
Ted Kremenek6f153952010-04-15 21:51:13 +0000386 D = D->getCanonicalDecl();
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000387 VisitDeclContext(D->getDeclContext());
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000388
Douglas Gregor74dbe642010-08-31 19:31:58 +0000389 bool AlreadyStarted = false;
390 if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(D)) {
Douglas Gregor39d6f072010-08-31 19:02:00 +0000391 if (ClassTemplateDecl *ClassTmpl = CXXRecord->getDescribedClassTemplate()) {
Douglas Gregor74dbe642010-08-31 19:31:58 +0000392 AlreadyStarted = true;
Douglas Gregor39d6f072010-08-31 19:02:00 +0000393
394 switch (D->getTagKind()) {
Douglas Gregor74dbe642010-08-31 19:31:58 +0000395 case TTK_Struct: Out << "@ST"; break;
396 case TTK_Class: Out << "@CT"; break;
397 case TTK_Union: Out << "@UT"; break;
398 case TTK_Enum: llvm_unreachable("enum template"); break;
Douglas Gregor39d6f072010-08-31 19:02:00 +0000399 }
400 VisitTemplateParameterList(ClassTmpl->getTemplateParameters());
Douglas Gregor74dbe642010-08-31 19:31:58 +0000401 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
402 = dyn_cast<ClassTemplatePartialSpecializationDecl>(CXXRecord)) {
403 AlreadyStarted = true;
404
405 switch (D->getTagKind()) {
406 case TTK_Struct: Out << "@SP"; break;
407 case TTK_Class: Out << "@CP"; break;
408 case TTK_Union: Out << "@UP"; break;
409 case TTK_Enum: llvm_unreachable("enum partial specialization"); break;
410 }
411 VisitTemplateParameterList(PartialSpec->getTemplateParameters());
Douglas Gregor39d6f072010-08-31 19:02:00 +0000412 }
Douglas Gregor74dbe642010-08-31 19:31:58 +0000413 }
Douglas Gregor39d6f072010-08-31 19:02:00 +0000414
Douglas Gregor74dbe642010-08-31 19:31:58 +0000415 if (!AlreadyStarted) {
Douglas Gregor39d6f072010-08-31 19:02:00 +0000416 switch (D->getTagKind()) {
417 case TTK_Struct: Out << "@S"; break;
418 case TTK_Class: Out << "@C"; break;
419 case TTK_Union: Out << "@U"; break;
420 case TTK_Enum: Out << "@E"; break;
421 }
Ted Kremeneke74ef122010-04-16 21:31:52 +0000422 }
Douglas Gregor39d6f072010-08-31 19:02:00 +0000423
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000424 Out << '@';
425 Out.flush();
426 assert(Buf.size() > 0);
427 const unsigned off = Buf.size() - 1;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000428
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000429 if (EmitDeclName(D)) {
430 if (const TypedefDecl *TD = D->getTypedefForAnonDecl()) {
431 Buf[off] = 'A';
Benjamin Kramer900fc632010-04-17 09:33:03 +0000432 Out << '@' << TD;
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000433 }
434 else
435 Buf[off] = 'a';
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000436 }
Douglas Gregor0ab1e9f2010-09-01 17:32:36 +0000437
438 // For a class template specialization, mangle the template arguments.
439 if (ClassTemplateSpecializationDecl *Spec
440 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
441 const TemplateArgumentList &Args = Spec->getTemplateInstantiationArgs();
442 Out << '>';
443 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
444 Out << '#';
445 VisitTemplateArgument(Args.get(I));
446 }
447 }
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000448}
449
Ted Kremenekc50277f2010-01-12 23:33:42 +0000450void USRGenerator::VisitTypedefDecl(TypedefDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000451 if (ShouldGenerateLocation(D) && GenLoc(D))
452 return;
Ted Kremenekc50277f2010-01-12 23:33:42 +0000453 DeclContext *DC = D->getDeclContext();
454 if (NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
Ted Kremenek896b70f2010-03-13 02:50:34 +0000455 Visit(DCN);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000456 Out << "@T@";
Ted Kremenek6f153952010-04-15 21:51:13 +0000457 Out << D->getName();
458}
459
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000460void USRGenerator::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
461 GenLoc(D);
462 return;
463}
464
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000465bool USRGenerator::GenLoc(const Decl *D) {
466 if (generatedLoc)
467 return IgnoreResults;
468 generatedLoc = true;
469
Ted Kremenek6f153952010-04-15 21:51:13 +0000470 const SourceManager &SM = AU->getSourceManager();
471 SourceLocation L = D->getLocStart();
472 if (L.isInvalid()) {
473 IgnoreResults = true;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000474 return true;
Ted Kremenek6f153952010-04-15 21:51:13 +0000475 }
476 L = SM.getInstantiationLoc(L);
477 const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(L);
478 const FileEntry *FE = SM.getFileEntryForID(Decomposed.first);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000479 if (FE) {
480 llvm::sys::Path P(FE->getName());
481 Out << P.getLast();
482 }
Ted Kremenek6f153952010-04-15 21:51:13 +0000483 else {
484 // This case really isn't interesting.
485 IgnoreResults = true;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000486 return true;
Ted Kremenek6f153952010-04-15 21:51:13 +0000487 }
Ted Kremenekf48b5312010-07-22 11:14:15 +0000488 // Use the offest into the FileID to represent the location. Using
489 // a line/column can cause us to look back at the original source file,
490 // which is expensive.
491 Out << '@' << Decomposed.second;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000492 return IgnoreResults;
Ted Kremenekc50277f2010-01-12 23:33:42 +0000493}
494
Ted Kremenek8e672192010-05-07 01:04:32 +0000495void USRGenerator::VisitType(QualType T) {
496 // This method mangles in USR information for types. It can possibly
497 // just reuse the naming-mangling logic used by codegen, although the
498 // requirements for USRs might not be the same.
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000499 ASTContext &Ctx = AU->getASTContext();
500
Ted Kremenek8e672192010-05-07 01:04:32 +0000501 do {
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000502 T = Ctx.getCanonicalType(T);
Ted Kremenek8e672192010-05-07 01:04:32 +0000503 Qualifiers Q = T.getQualifiers();
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000504 unsigned qVal = 0;
Ted Kremenek8e672192010-05-07 01:04:32 +0000505 if (Q.hasConst())
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000506 qVal |= 0x1;
Ted Kremenek8e672192010-05-07 01:04:32 +0000507 if (Q.hasVolatile())
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000508 qVal |= 0x2;
Ted Kremenek8e672192010-05-07 01:04:32 +0000509 if (Q.hasRestrict())
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000510 qVal |= 0x4;
511 if(qVal)
512 Out << ((char) ('0' + qVal));
Ted Kremenek8e672192010-05-07 01:04:32 +0000513
514 // Mangle in ObjC GC qualifiers?
515
Ted Kremenek8e672192010-05-07 01:04:32 +0000516 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
517 unsigned char c = '\0';
518 switch (BT->getKind()) {
519 case BuiltinType::Void:
520 c = 'v'; break;
521 case BuiltinType::Bool:
522 c = 'b'; break;
523 case BuiltinType::Char_U:
524 case BuiltinType::UChar:
525 c = 'c'; break;
526 case BuiltinType::Char16:
527 c = 'q'; break;
528 case BuiltinType::Char32:
529 c = 'w'; break;
530 case BuiltinType::UShort:
531 c = 's'; break;
532 case BuiltinType::UInt:
533 c = 'i'; break;
534 case BuiltinType::ULong:
535 c = 'l'; break;
536 case BuiltinType::ULongLong:
537 c = 'k'; break;
538 case BuiltinType::UInt128:
539 c = 'j'; break;
540 case BuiltinType::Char_S:
541 case BuiltinType::SChar:
542 c = 'C'; break;
543 case BuiltinType::WChar:
544 c = 'W'; break;
545 case BuiltinType::Short:
546 c = 'S'; break;
547 case BuiltinType::Int:
548 c = 'I'; break;
549 case BuiltinType::Long:
550 c = 'L'; break;
551 case BuiltinType::LongLong:
552 c = 'K'; break;
553 case BuiltinType::Int128:
554 c = 'J'; break;
555 case BuiltinType::Float:
556 c = 'f'; break;
557 case BuiltinType::Double:
558 c = 'd'; break;
559 case BuiltinType::LongDouble:
560 c = 'D'; break;
561 case BuiltinType::NullPtr:
562 c = 'n'; break;
563 case BuiltinType::Overload:
564 case BuiltinType::Dependent:
565 case BuiltinType::UndeducedAuto:
566 IgnoreResults = true;
567 return;
568 case BuiltinType::ObjCId:
569 c = 'o'; break;
570 case BuiltinType::ObjCClass:
571 c = 'O'; break;
572 case BuiltinType::ObjCSel:
573 c = 'e'; break;
574 }
575 Out << c;
576 return;
577 }
Douglas Gregor66b7fbf2010-09-20 20:37:39 +0000578
579 // If we have already seen this (non-built-in) type, use a substitution
580 // encoding.
581 llvm::DenseMap<const Type *, unsigned>::iterator Substitution
582 = TypeSubstitutions.find(T.getTypePtr());
583 if (Substitution != TypeSubstitutions.end()) {
584 Out << 'S' << Substitution->second << '_';
585 return;
586 } else {
587 // Record this as a substitution.
588 unsigned Number = TypeSubstitutions.size();
589 TypeSubstitutions[T.getTypePtr()] = Number;
590 }
591
592 if (const PointerType *PT = T->getAs<PointerType>()) {
593 Out << '*';
594 T = PT->getPointeeType();
595 continue;
596 }
597 if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
598 Out << '&';
599 T = RT->getPointeeType();
600 continue;
601 }
602 if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) {
603 Out << 'F';
604 VisitType(FT->getResultType());
605 for (FunctionProtoType::arg_type_iterator
606 I = FT->arg_type_begin(), E = FT->arg_type_end(); I!=E; ++I) {
607 VisitType(*I);
608 }
609 if (FT->isVariadic())
610 Out << '.';
611 return;
612 }
613 if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) {
614 Out << 'B';
615 T = BT->getPointeeType();
616 continue;
617 }
Ted Kremenek8e672192010-05-07 01:04:32 +0000618 if (const ComplexType *CT = T->getAs<ComplexType>()) {
619 Out << '<';
620 T = CT->getElementType();
621 continue;
622 }
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000623 if (const TagType *TT = T->getAs<TagType>()) {
624 Out << '$';
625 VisitTagDecl(TT->getDecl());
626 return;
627 }
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000628 if (const TemplateTypeParmType *TTP = T->getAs<TemplateTypeParmType>()) {
629 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
630 return;
631 }
632 if (const TemplateSpecializationType *Spec
633 = T->getAs<TemplateSpecializationType>()) {
634 Out << '>';
635 VisitTemplateName(Spec->getTemplateName());
636 Out << Spec->getNumArgs();
637 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
638 VisitTemplateArgument(Spec->getArg(I));
639 return;
640 }
641
Ted Kremenek8e672192010-05-07 01:04:32 +0000642 // Unhandled type.
643 Out << ' ';
644 break;
645 } while (true);
646}
647
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000648void USRGenerator::VisitTemplateParameterList(
649 const TemplateParameterList *Params) {
650 if (!Params)
651 return;
652 Out << '>' << Params->size();
653 for (TemplateParameterList::const_iterator P = Params->begin(),
654 PEnd = Params->end();
655 P != PEnd; ++P) {
656 Out << '#';
657 if (isa<TemplateTypeParmDecl>(*P)) {
658 Out << 'T';
659 continue;
660 }
661
662 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
663 Out << 'N';
664 VisitType(NTTP->getType());
665 continue;
666 }
667
668 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
669 Out << 't';
670 VisitTemplateParameterList(TTP->getTemplateParameters());
671 }
672}
673
674void USRGenerator::VisitTemplateName(TemplateName Name) {
675 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
676 if (TemplateTemplateParmDecl *TTP
677 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
678 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
679 return;
680 }
681
682 Visit(Template);
683 return;
684 }
685
686 // FIXME: Visit dependent template names.
687}
688
689void USRGenerator::VisitTemplateArgument(const TemplateArgument &Arg) {
690 switch (Arg.getKind()) {
691 case TemplateArgument::Null:
692 break;
693
694 case TemplateArgument::Declaration:
695 Visit(Arg.getAsDecl());
696 break;
697
698 case TemplateArgument::Template:
699 VisitTemplateName(Arg.getAsTemplate());
700 break;
701
702 case TemplateArgument::Expression:
703 // FIXME: Visit expressions.
704 break;
705
706 case TemplateArgument::Pack:
707 // FIXME: Variadic templates
708 break;
709
710 case TemplateArgument::Type:
711 VisitType(Arg.getAsType());
712 break;
713
714 case TemplateArgument::Integral:
715 Out << 'V';
716 VisitType(Arg.getIntegralType());
717 Out << *Arg.getAsIntegral();
718 break;
719 }
720}
721
Ted Kremenek896b70f2010-03-13 02:50:34 +0000722//===----------------------------------------------------------------------===//
723// General purpose USR generation methods.
724//===----------------------------------------------------------------------===//
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000725
Ted Kremenek896b70f2010-03-13 02:50:34 +0000726void USRGenerator::GenObjCClass(llvm::StringRef cls) {
727 Out << "objc(cs)" << cls;
728}
729
730void USRGenerator::GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat) {
Ted Kremeneke74ef122010-04-16 21:31:52 +0000731 Out << "objc(cy)" << cls << '@' << cat;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000732}
733
734void USRGenerator::GenObjCIvar(llvm::StringRef ivar) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000735 Out << '@' << ivar;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000736}
737
738void USRGenerator::GenObjCMethod(llvm::StringRef meth, bool isInstanceMethod) {
739 Out << (isInstanceMethod ? "(im)" : "(cm)") << meth;
740}
741
742void USRGenerator::GenObjCProperty(llvm::StringRef prop) {
743 Out << "(py)" << prop;
744}
745
746void USRGenerator::GenObjCProtocol(llvm::StringRef prot) {
747 Out << "objc(pl)" << prot;
748}
749
750//===----------------------------------------------------------------------===//
751// API hooks.
752//===----------------------------------------------------------------------===//
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000753
Benjamin Kramercfb51b62010-04-08 15:54:07 +0000754static inline llvm::StringRef extractUSRSuffix(llvm::StringRef s) {
755 return s.startswith("c:") ? s.substr(2) : "";
756}
757
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000758static CXString getDeclCursorUSR(const CXCursor &C) {
Ted Kremenek896b70f2010-03-13 02:50:34 +0000759 Decl *D = cxcursor::getCursorDecl(C);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000760
761 // Don't generate USRs for things with invalid locations.
762 if (!D || D->getLocStart().isInvalid())
Ted Kremenek1af0a2a2010-04-17 00:21:38 +0000763 return createCXString("");
Ted Kremenek896b70f2010-03-13 02:50:34 +0000764
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000765 // Check if the cursor has 'NoLinkage'.
Ted Kremeneke74ef122010-04-16 21:31:52 +0000766 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000767 switch (ND->getLinkage()) {
768 case ExternalLinkage:
769 // Generate USRs for all entities with external linkage.
770 break;
771 case NoLinkage:
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000772 case UniqueExternalLinkage:
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000773 // We allow enums, typedefs, and structs that have no linkage to
774 // have USRs that are anchored to the file they were defined in
775 // (e.g., the header). This is a little gross, but in principal
776 // enums/anonymous structs/etc. defined in a common header file
777 // are referred to across multiple translation units.
Ted Kremenek6f153952010-04-15 21:51:13 +0000778 if (isa<TagDecl>(ND) || isa<TypedefDecl>(ND) ||
Ted Kremenekcf999102010-04-29 17:43:29 +0000779 isa<EnumConstantDecl>(ND) || isa<FieldDecl>(ND) ||
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000780 isa<VarDecl>(ND) || isa<NamespaceDecl>(ND))
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000781 break;
782 // Fall-through.
783 case InternalLinkage:
Ted Kremenekcf999102010-04-29 17:43:29 +0000784 if (isa<FunctionDecl>(ND))
785 break;
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000786 }
787
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000788 USRGenerator UG(&C);
789 UG->Visit(D);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000790
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000791 if (UG->ignoreResults())
Ted Kremenekebfa3392010-03-19 20:39:03 +0000792 return createCXString("");
Ted Kremenek896b70f2010-03-13 02:50:34 +0000793
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000794#if 0
Ted Kremeneke542f772010-04-20 23:15:40 +0000795 // For development testing.
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000796 assert(UG.str().size() > 2);
797#endif
Ted Kremeneke542f772010-04-20 23:15:40 +0000798
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000799 // Return a copy of the string that must be disposed by the caller.
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000800 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000801}
802
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000803extern "C" {
804
805CXString clang_getCursorUSR(CXCursor C) {
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000806 const CXCursorKind &K = clang_getCursorKind(C);
807
808 if (clang_isDeclaration(K))
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000809 return getDeclCursorUSR(C);
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000810
811 if (K == CXCursor_MacroDefinition) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000812 USRGenerator UG(&C);
813 UG << "macro@"
814 << cxcursor::getCursorMacroDefinition(C)->getName()->getNameStart();
815 return createCXString(UG.str(), true);
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000816 }
817
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000818 return createCXString("");
819}
820
Ted Kremenek896b70f2010-03-13 02:50:34 +0000821CXString clang_constructUSR_ObjCIvar(const char *name, CXString classUSR) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000822 USRGenerator UG;
823 UG << extractUSRSuffix(clang_getCString(classUSR));
824 UG->GenObjCIvar(name);
825 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000826}
827
828CXString clang_constructUSR_ObjCMethod(const char *name,
829 unsigned isInstanceMethod,
830 CXString classUSR) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000831 USRGenerator UG;
832 UG << extractUSRSuffix(clang_getCString(classUSR));
833 UG->GenObjCMethod(name, isInstanceMethod);
834 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000835}
836
837CXString clang_constructUSR_ObjCClass(const char *name) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000838 USRGenerator UG;
839 UG->GenObjCClass(name);
840 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000841}
842
843CXString clang_constructUSR_ObjCProtocol(const char *name) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000844 USRGenerator UG;
845 UG->GenObjCProtocol(name);
846 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000847}
848
Ted Kremenek66ccaec2010-03-15 17:38:58 +0000849CXString clang_constructUSR_ObjCCategory(const char *class_name,
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000850 const char *category_name) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000851 USRGenerator UG;
852 UG->GenObjCCategory(class_name, category_name);
853 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000854}
855
856CXString clang_constructUSR_ObjCProperty(const char *property,
857 CXString classUSR) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000858 USRGenerator UG;
859 UG << extractUSRSuffix(clang_getCString(classUSR));
860 UG->GenObjCProperty(property);
861 return createCXString(UG.str(), true);
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000862}
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000863
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000864} // end extern "C"