blob: 8f3dacfad218ac593eaded7489435fb7542a83d2 [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 }
Douglas Gregor7e242562010-09-01 19:52:22 +000089 void VisitUsingDecl(UsingDecl *D) {
90 IgnoreResults = true;
91 }
92 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
93 IgnoreResults = true;
94 }
95 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
96 IgnoreResults = true;
97 }
Douglas Gregor0a35bce2010-09-01 03:07:18 +000098
Ted Kremenek6f153952010-04-15 21:51:13 +000099 /// Generate the string component containing the location of the
100 /// declaration.
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000101 bool GenLoc(const Decl *D);
Ted Kremenek6f153952010-04-15 21:51:13 +0000102
Ted Kremenek896b70f2010-03-13 02:50:34 +0000103 /// String generation methods used both by the visitation methods
104 /// and from other clients that want to directly generate USRs. These
105 /// methods do not construct complete USRs (which incorporate the parents
106 /// of an AST element), but only the fragments concerning the AST element
107 /// itself.
108
Ted Kremenek896b70f2010-03-13 02:50:34 +0000109 /// Generate a USR for an Objective-C class.
110 void GenObjCClass(llvm::StringRef cls);
111 /// Generate a USR for an Objective-C class category.
112 void GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat);
113 /// Generate a USR fragment for an Objective-C instance variable. The
114 /// complete USR can be created by concatenating the USR for the
115 /// encompassing class with this USR fragment.
116 void GenObjCIvar(llvm::StringRef ivar);
117 /// Generate a USR fragment for an Objective-C method.
118 void GenObjCMethod(llvm::StringRef sel, bool isInstanceMethod);
119 /// Generate a USR fragment for an Objective-C property.
120 void GenObjCProperty(llvm::StringRef prop);
121 /// Generate a USR for an Objective-C protocol.
122 void GenObjCProtocol(llvm::StringRef prot);
Ted Kremenek8e672192010-05-07 01:04:32 +0000123
124 void VisitType(QualType T);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000125 void VisitTemplateParameterList(const TemplateParameterList *Params);
126 void VisitTemplateName(TemplateName Name);
127 void VisitTemplateArgument(const TemplateArgument &Arg);
128
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000129 /// Emit a Decl's name using NamedDecl::printName() and return true if
130 /// the decl had no name.
131 bool EmitDeclName(const NamedDecl *D);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000132};
133
Ted Kremenekc50277f2010-01-12 23:33:42 +0000134} // end anonymous namespace
135
Ted Kremenek896b70f2010-03-13 02:50:34 +0000136//===----------------------------------------------------------------------===//
137// Generating USRs from ASTS.
138//===----------------------------------------------------------------------===//
139
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000140bool USRGenerator::EmitDeclName(const NamedDecl *D) {
141 Out.flush();
142 const unsigned startSize = Buf.size();
143 D->printName(Out);
144 Out.flush();
145 const unsigned endSize = Buf.size();
146 return startSize == endSize;
147}
148
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000149static bool InAnonymousNamespace(const Decl *D) {
150 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(D->getDeclContext()))
151 return ND->isAnonymousNamespace();
152 return false;
153}
154
155static inline bool ShouldGenerateLocation(const NamedDecl *D) {
156 return D->getLinkage() != ExternalLinkage && !InAnonymousNamespace(D);
157}
158
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000159void USRGenerator::VisitDeclContext(DeclContext *DC) {
160 if (NamedDecl *D = dyn_cast<NamedDecl>(DC))
161 Visit(D);
162}
163
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000164void USRGenerator::VisitFieldDecl(FieldDecl *D) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000165 VisitDeclContext(D->getDeclContext());
166 Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@");
167 if (EmitDeclName(D)) {
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000168 // Bit fields can be anonymous.
169 IgnoreResults = true;
170 return;
171 }
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000172}
173
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000174void USRGenerator::VisitFunctionDecl(FunctionDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000175 if (ShouldGenerateLocation(D) && GenLoc(D))
176 return;
Ted Kremenekcf999102010-04-29 17:43:29 +0000177
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000178 VisitDeclContext(D->getDeclContext());
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000179 if (FunctionTemplateDecl *FunTmpl = D->getDescribedFunctionTemplate()) {
180 Out << "@FT@";
181 VisitTemplateParameterList(FunTmpl->getTemplateParameters());
182 } else
183 Out << "@F@";
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000184 D->printName(Out);
Ted Kremenek8e672192010-05-07 01:04:32 +0000185
186 ASTContext &Ctx = AU->getASTContext();
187 if (!Ctx.getLangOptions().CPlusPlus || D->isExternC())
188 return;
189
190 // Mangle in type information for the arguments.
191 for (FunctionDecl::param_iterator I = D->param_begin(), E = D->param_end();
192 I != E; ++I) {
193 Out << '#';
194 if (ParmVarDecl *PD = *I)
195 VisitType(PD->getType());
196 }
197 if (D->isVariadic())
198 Out << '.';
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000199 Out << '#';
200 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
201 if (MD->isStatic())
202 Out << 'S';
203 if (unsigned quals = MD->getTypeQualifiers())
204 Out << (char)('0' + quals);
205 }
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000206}
Ted Kremenekc50277f2010-01-12 23:33:42 +0000207
208void USRGenerator::VisitNamedDecl(NamedDecl *D) {
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000209 VisitDeclContext(D->getDeclContext());
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000210 Out << "@";
211
212 if (EmitDeclName(D)) {
213 // The string can be empty if the declaration has no name; e.g., it is
214 // the ParmDecl with no name for declaration of a function pointer type,
215 // e.g.: void (*f)(void *);
216 // In this case, don't generate a USR.
Ted Kremeneke74ef122010-04-16 21:31:52 +0000217 IgnoreResults = true;
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000218 }
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000219}
220
Ted Kremeneke542f772010-04-20 23:15:40 +0000221void USRGenerator::VisitVarDecl(VarDecl *D) {
222 // VarDecls can be declared 'extern' within a function or method body,
223 // but their enclosing DeclContext is the function, not the TU. We need
224 // to check the storage class to correctly generate the USR.
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000225 if (ShouldGenerateLocation(D) && GenLoc(D))
226 return;
227
228 VisitDeclContext(D->getDeclContext());
Ted Kremeneke542f772010-04-20 23:15:40 +0000229
Ted Kremenekcf999102010-04-29 17:43:29 +0000230 // Variables always have simple names.
231 llvm::StringRef s = D->getName();
232
Ted Kremeneke542f772010-04-20 23:15:40 +0000233 // The string can be empty if the declaration has no name; e.g., it is
234 // the ParmDecl with no name for declaration of a function pointer type, e.g.:
Eli Friedmana7e68452010-08-22 01:00:03 +0000235 // void (*f)(void *);
Ted Kremeneke542f772010-04-20 23:15:40 +0000236 // In this case, don't generate a USR.
237 if (s.empty())
238 IgnoreResults = true;
239 else
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000240 Out << '@' << s;
Ted Kremeneke542f772010-04-20 23:15:40 +0000241}
242
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000243void USRGenerator::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
244 GenLoc(D);
245 return;
246}
247
248void USRGenerator::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
249 GenLoc(D);
250 return;
251}
252
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000253void USRGenerator::VisitNamespaceDecl(NamespaceDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000254 if (D->isAnonymousNamespace()) {
255 Out << "@aN";
256 return;
257 }
258
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000259 VisitDeclContext(D->getDeclContext());
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000260 if (!IgnoreResults)
261 Out << "@N@" << D->getName();
Ted Kremenekc50277f2010-01-12 23:33:42 +0000262}
263
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000264void USRGenerator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
265 VisitFunctionDecl(D->getTemplatedDecl());
266}
267
Douglas Gregor39d6f072010-08-31 19:02:00 +0000268void USRGenerator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
269 VisitTagDecl(D->getTemplatedDecl());
270}
271
Douglas Gregor69319002010-08-31 23:48:11 +0000272void USRGenerator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
273 VisitDeclContext(D->getDeclContext());
274 if (!IgnoreResults)
275 Out << "@NA@" << D->getName();
276}
Douglas Gregor39d6f072010-08-31 19:02:00 +0000277
Ted Kremenekc50277f2010-01-12 23:33:42 +0000278void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) {
Ted Kremenek28a7f252010-08-24 23:13:41 +0000279 Decl *container = cast<Decl>(D->getDeclContext());
280
281 // The USR for a method declared in a class extension is based on
282 // the ObjCInterfaceDecl, not the ObjCCategoryDecl.
283 do {
284 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(container))
285 if (CD->IsClassExtension()) {
286 Visit(CD->getClassInterface());
287 break;
288 }
289 Visit(cast<Decl>(D->getDeclContext()));
290 }
291 while (false);
292
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000293 // Ideally we would use 'GenObjCMethod', but this is such a hot path
294 // for Objective-C code that we don't want to use
295 // DeclarationName::getAsString().
296 Out << (D->isInstanceMethod() ? "(im)" : "(cm)");
297 DeclarationName N(D->getSelector());
298 N.printName(Out);
Ted Kremenekc50277f2010-01-12 23:33:42 +0000299}
300
Ted Kremeneke74ef122010-04-16 21:31:52 +0000301void USRGenerator::VisitObjCClassDecl(ObjCClassDecl *D) {
302 // FIXME: @class declarations can refer to multiple classes. We need
303 // to be able to traverse these.
304 IgnoreResults = true;
305}
306
307void USRGenerator::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
308 // FIXME: @protocol declarations can refer to multiple protocols. We need
309 // to be able to traverse these.
310 IgnoreResults = true;
311}
312
Ted Kremenekc50277f2010-01-12 23:33:42 +0000313void USRGenerator::VisitObjCContainerDecl(ObjCContainerDecl *D) {
314 switch (D->getKind()) {
315 default:
316 assert(false && "Invalid ObjC container.");
317 case Decl::ObjCInterface:
318 case Decl::ObjCImplementation:
Ted Kremenek896b70f2010-03-13 02:50:34 +0000319 GenObjCClass(D->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000320 break;
321 case Decl::ObjCCategory: {
322 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
Ted Kremenekebfa3392010-03-19 20:39:03 +0000323 ObjCInterfaceDecl *ID = CD->getClassInterface();
324 if (!ID) {
325 // Handle invalid code where the @interface might not
326 // have been specified.
327 // FIXME: We should be able to generate this USR even if the
328 // @interface isn't available.
329 IgnoreResults = true;
330 return;
331 }
Ted Kremenek28a7f252010-08-24 23:13:41 +0000332 // Specially handle class extensions, which are anonymous categories.
333 // We want to mangle in the location to uniquely distinguish them.
334 if (CD->IsClassExtension()) {
335 Out << "objc(ext)" << ID->getName() << '@';
336 GenLoc(CD);
337 }
338 else
339 GenObjCCategory(ID->getName(), CD->getName());
340
Ted Kremenekc50277f2010-01-12 23:33:42 +0000341 break;
342 }
343 case Decl::ObjCCategoryImpl: {
344 ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
Ted Kremenekebfa3392010-03-19 20:39:03 +0000345 ObjCInterfaceDecl *ID = CD->getClassInterface();
346 if (!ID) {
347 // Handle invalid code where the @interface might not
348 // have been specified.
349 // FIXME: We should be able to generate this USR even if the
350 // @interface isn't available.
351 IgnoreResults = true;
352 return;
353 }
354 GenObjCCategory(ID->getName(), CD->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000355 break;
356 }
357 case Decl::ObjCProtocol:
Ted Kremenek896b70f2010-03-13 02:50:34 +0000358 GenObjCProtocol(cast<ObjCProtocolDecl>(D)->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000359 break;
360 }
361}
362
363void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
364 Visit(cast<Decl>(D->getDeclContext()));
Ted Kremenek896b70f2010-03-13 02:50:34 +0000365 GenObjCProperty(D->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000366}
367
Ted Kremeneke542f772010-04-20 23:15:40 +0000368void USRGenerator::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
369 if (ObjCPropertyDecl *PD = D->getPropertyDecl()) {
370 VisitObjCPropertyDecl(PD);
371 return;
372 }
373
374 IgnoreResults = true;
375}
376
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000377void USRGenerator::VisitTagDecl(TagDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000378 // Add the location of the tag decl to handle resolution across
379 // translation units.
380 if (ShouldGenerateLocation(D) && GenLoc(D))
381 return;
Ted Kremenek8e672192010-05-07 01:04:32 +0000382
Ted Kremenek6f153952010-04-15 21:51:13 +0000383 D = D->getCanonicalDecl();
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000384 VisitDeclContext(D->getDeclContext());
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000385
Douglas Gregor74dbe642010-08-31 19:31:58 +0000386 bool AlreadyStarted = false;
387 if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(D)) {
Douglas Gregor39d6f072010-08-31 19:02:00 +0000388 if (ClassTemplateDecl *ClassTmpl = CXXRecord->getDescribedClassTemplate()) {
Douglas Gregor74dbe642010-08-31 19:31:58 +0000389 AlreadyStarted = true;
Douglas Gregor39d6f072010-08-31 19:02:00 +0000390
391 switch (D->getTagKind()) {
Douglas Gregor74dbe642010-08-31 19:31:58 +0000392 case TTK_Struct: Out << "@ST"; break;
393 case TTK_Class: Out << "@CT"; break;
394 case TTK_Union: Out << "@UT"; break;
395 case TTK_Enum: llvm_unreachable("enum template"); break;
Douglas Gregor39d6f072010-08-31 19:02:00 +0000396 }
397 VisitTemplateParameterList(ClassTmpl->getTemplateParameters());
Douglas Gregor74dbe642010-08-31 19:31:58 +0000398 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
399 = dyn_cast<ClassTemplatePartialSpecializationDecl>(CXXRecord)) {
400 AlreadyStarted = true;
401
402 switch (D->getTagKind()) {
403 case TTK_Struct: Out << "@SP"; break;
404 case TTK_Class: Out << "@CP"; break;
405 case TTK_Union: Out << "@UP"; break;
406 case TTK_Enum: llvm_unreachable("enum partial specialization"); break;
407 }
408 VisitTemplateParameterList(PartialSpec->getTemplateParameters());
Douglas Gregor39d6f072010-08-31 19:02:00 +0000409 }
Douglas Gregor74dbe642010-08-31 19:31:58 +0000410 }
Douglas Gregor39d6f072010-08-31 19:02:00 +0000411
Douglas Gregor74dbe642010-08-31 19:31:58 +0000412 if (!AlreadyStarted) {
Douglas Gregor39d6f072010-08-31 19:02:00 +0000413 switch (D->getTagKind()) {
414 case TTK_Struct: Out << "@S"; break;
415 case TTK_Class: Out << "@C"; break;
416 case TTK_Union: Out << "@U"; break;
417 case TTK_Enum: Out << "@E"; break;
418 }
Ted Kremeneke74ef122010-04-16 21:31:52 +0000419 }
Douglas Gregor39d6f072010-08-31 19:02:00 +0000420
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000421 Out << '@';
422 Out.flush();
423 assert(Buf.size() > 0);
424 const unsigned off = Buf.size() - 1;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000425
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000426 if (EmitDeclName(D)) {
427 if (const TypedefDecl *TD = D->getTypedefForAnonDecl()) {
428 Buf[off] = 'A';
Benjamin Kramer900fc632010-04-17 09:33:03 +0000429 Out << '@' << TD;
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000430 }
431 else
432 Buf[off] = 'a';
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000433 }
Douglas Gregor0ab1e9f2010-09-01 17:32:36 +0000434
435 // For a class template specialization, mangle the template arguments.
436 if (ClassTemplateSpecializationDecl *Spec
437 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
438 const TemplateArgumentList &Args = Spec->getTemplateInstantiationArgs();
439 Out << '>';
440 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
441 Out << '#';
442 VisitTemplateArgument(Args.get(I));
443 }
444 }
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000445}
446
Ted Kremenekc50277f2010-01-12 23:33:42 +0000447void USRGenerator::VisitTypedefDecl(TypedefDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000448 if (ShouldGenerateLocation(D) && GenLoc(D))
449 return;
Ted Kremenekc50277f2010-01-12 23:33:42 +0000450 DeclContext *DC = D->getDeclContext();
451 if (NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
Ted Kremenek896b70f2010-03-13 02:50:34 +0000452 Visit(DCN);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000453 Out << "@T@";
Ted Kremenek6f153952010-04-15 21:51:13 +0000454 Out << D->getName();
455}
456
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000457void USRGenerator::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
458 GenLoc(D);
459 return;
460}
461
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000462bool USRGenerator::GenLoc(const Decl *D) {
463 if (generatedLoc)
464 return IgnoreResults;
465 generatedLoc = true;
466
Ted Kremenek6f153952010-04-15 21:51:13 +0000467 const SourceManager &SM = AU->getSourceManager();
468 SourceLocation L = D->getLocStart();
469 if (L.isInvalid()) {
470 IgnoreResults = true;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000471 return true;
Ted Kremenek6f153952010-04-15 21:51:13 +0000472 }
473 L = SM.getInstantiationLoc(L);
474 const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(L);
475 const FileEntry *FE = SM.getFileEntryForID(Decomposed.first);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000476 if (FE) {
477 llvm::sys::Path P(FE->getName());
478 Out << P.getLast();
479 }
Ted Kremenek6f153952010-04-15 21:51:13 +0000480 else {
481 // This case really isn't interesting.
482 IgnoreResults = true;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000483 return true;
Ted Kremenek6f153952010-04-15 21:51:13 +0000484 }
Ted Kremenekf48b5312010-07-22 11:14:15 +0000485 // Use the offest into the FileID to represent the location. Using
486 // a line/column can cause us to look back at the original source file,
487 // which is expensive.
488 Out << '@' << Decomposed.second;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000489 return IgnoreResults;
Ted Kremenekc50277f2010-01-12 23:33:42 +0000490}
491
Ted Kremenek8e672192010-05-07 01:04:32 +0000492void USRGenerator::VisitType(QualType T) {
493 // This method mangles in USR information for types. It can possibly
494 // just reuse the naming-mangling logic used by codegen, although the
495 // requirements for USRs might not be the same.
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000496 ASTContext &Ctx = AU->getASTContext();
497
Ted Kremenek8e672192010-05-07 01:04:32 +0000498 do {
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000499 T = Ctx.getCanonicalType(T);
Ted Kremenek8e672192010-05-07 01:04:32 +0000500 Qualifiers Q = T.getQualifiers();
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000501 unsigned qVal = 0;
Ted Kremenek8e672192010-05-07 01:04:32 +0000502 if (Q.hasConst())
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000503 qVal |= 0x1;
Ted Kremenek8e672192010-05-07 01:04:32 +0000504 if (Q.hasVolatile())
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000505 qVal |= 0x2;
Ted Kremenek8e672192010-05-07 01:04:32 +0000506 if (Q.hasRestrict())
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000507 qVal |= 0x4;
508 if(qVal)
509 Out << ((char) ('0' + qVal));
Ted Kremenek8e672192010-05-07 01:04:32 +0000510
511 // Mangle in ObjC GC qualifiers?
512
513 if (const PointerType *PT = T->getAs<PointerType>()) {
514 Out << '*';
515 T = PT->getPointeeType();
516 continue;
517 }
518 if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
519 Out << '&';
520 T = RT->getPointeeType();
521 continue;
522 }
523 if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) {
524 Out << 'F';
525 VisitType(FT->getResultType());
526 for (FunctionProtoType::arg_type_iterator
527 I = FT->arg_type_begin(), E = FT->arg_type_end(); I!=E; ++I) {
528 VisitType(*I);
529 }
530 if (FT->isVariadic())
531 Out << '.';
532 return;
533 }
534 if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) {
535 Out << 'B';
536 T = BT->getPointeeType();
537 continue;
538 }
539 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
540 unsigned char c = '\0';
541 switch (BT->getKind()) {
542 case BuiltinType::Void:
543 c = 'v'; break;
544 case BuiltinType::Bool:
545 c = 'b'; break;
546 case BuiltinType::Char_U:
547 case BuiltinType::UChar:
548 c = 'c'; break;
549 case BuiltinType::Char16:
550 c = 'q'; break;
551 case BuiltinType::Char32:
552 c = 'w'; break;
553 case BuiltinType::UShort:
554 c = 's'; break;
555 case BuiltinType::UInt:
556 c = 'i'; break;
557 case BuiltinType::ULong:
558 c = 'l'; break;
559 case BuiltinType::ULongLong:
560 c = 'k'; break;
561 case BuiltinType::UInt128:
562 c = 'j'; break;
563 case BuiltinType::Char_S:
564 case BuiltinType::SChar:
565 c = 'C'; break;
566 case BuiltinType::WChar:
567 c = 'W'; break;
568 case BuiltinType::Short:
569 c = 'S'; break;
570 case BuiltinType::Int:
571 c = 'I'; break;
572 case BuiltinType::Long:
573 c = 'L'; break;
574 case BuiltinType::LongLong:
575 c = 'K'; break;
576 case BuiltinType::Int128:
577 c = 'J'; break;
578 case BuiltinType::Float:
579 c = 'f'; break;
580 case BuiltinType::Double:
581 c = 'd'; break;
582 case BuiltinType::LongDouble:
583 c = 'D'; break;
584 case BuiltinType::NullPtr:
585 c = 'n'; break;
586 case BuiltinType::Overload:
587 case BuiltinType::Dependent:
588 case BuiltinType::UndeducedAuto:
589 IgnoreResults = true;
590 return;
591 case BuiltinType::ObjCId:
592 c = 'o'; break;
593 case BuiltinType::ObjCClass:
594 c = 'O'; break;
595 case BuiltinType::ObjCSel:
596 c = 'e'; break;
597 }
598 Out << c;
599 return;
600 }
601 if (const ComplexType *CT = T->getAs<ComplexType>()) {
602 Out << '<';
603 T = CT->getElementType();
604 continue;
605 }
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000606 if (const TagType *TT = T->getAs<TagType>()) {
607 Out << '$';
608 VisitTagDecl(TT->getDecl());
609 return;
610 }
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000611 if (const TemplateTypeParmType *TTP = T->getAs<TemplateTypeParmType>()) {
612 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
613 return;
614 }
615 if (const TemplateSpecializationType *Spec
616 = T->getAs<TemplateSpecializationType>()) {
617 Out << '>';
618 VisitTemplateName(Spec->getTemplateName());
619 Out << Spec->getNumArgs();
620 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
621 VisitTemplateArgument(Spec->getArg(I));
622 return;
623 }
624
Ted Kremenek8e672192010-05-07 01:04:32 +0000625 // Unhandled type.
626 Out << ' ';
627 break;
628 } while (true);
629}
630
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000631void USRGenerator::VisitTemplateParameterList(
632 const TemplateParameterList *Params) {
633 if (!Params)
634 return;
635 Out << '>' << Params->size();
636 for (TemplateParameterList::const_iterator P = Params->begin(),
637 PEnd = Params->end();
638 P != PEnd; ++P) {
639 Out << '#';
640 if (isa<TemplateTypeParmDecl>(*P)) {
641 Out << 'T';
642 continue;
643 }
644
645 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
646 Out << 'N';
647 VisitType(NTTP->getType());
648 continue;
649 }
650
651 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
652 Out << 't';
653 VisitTemplateParameterList(TTP->getTemplateParameters());
654 }
655}
656
657void USRGenerator::VisitTemplateName(TemplateName Name) {
658 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
659 if (TemplateTemplateParmDecl *TTP
660 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
661 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
662 return;
663 }
664
665 Visit(Template);
666 return;
667 }
668
669 // FIXME: Visit dependent template names.
670}
671
672void USRGenerator::VisitTemplateArgument(const TemplateArgument &Arg) {
673 switch (Arg.getKind()) {
674 case TemplateArgument::Null:
675 break;
676
677 case TemplateArgument::Declaration:
678 Visit(Arg.getAsDecl());
679 break;
680
681 case TemplateArgument::Template:
682 VisitTemplateName(Arg.getAsTemplate());
683 break;
684
685 case TemplateArgument::Expression:
686 // FIXME: Visit expressions.
687 break;
688
689 case TemplateArgument::Pack:
690 // FIXME: Variadic templates
691 break;
692
693 case TemplateArgument::Type:
694 VisitType(Arg.getAsType());
695 break;
696
697 case TemplateArgument::Integral:
698 Out << 'V';
699 VisitType(Arg.getIntegralType());
700 Out << *Arg.getAsIntegral();
701 break;
702 }
703}
704
Ted Kremenek896b70f2010-03-13 02:50:34 +0000705//===----------------------------------------------------------------------===//
706// General purpose USR generation methods.
707//===----------------------------------------------------------------------===//
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000708
Ted Kremenek896b70f2010-03-13 02:50:34 +0000709void USRGenerator::GenObjCClass(llvm::StringRef cls) {
710 Out << "objc(cs)" << cls;
711}
712
713void USRGenerator::GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat) {
Ted Kremeneke74ef122010-04-16 21:31:52 +0000714 Out << "objc(cy)" << cls << '@' << cat;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000715}
716
717void USRGenerator::GenObjCIvar(llvm::StringRef ivar) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000718 Out << '@' << ivar;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000719}
720
721void USRGenerator::GenObjCMethod(llvm::StringRef meth, bool isInstanceMethod) {
722 Out << (isInstanceMethod ? "(im)" : "(cm)") << meth;
723}
724
725void USRGenerator::GenObjCProperty(llvm::StringRef prop) {
726 Out << "(py)" << prop;
727}
728
729void USRGenerator::GenObjCProtocol(llvm::StringRef prot) {
730 Out << "objc(pl)" << prot;
731}
732
733//===----------------------------------------------------------------------===//
734// API hooks.
735//===----------------------------------------------------------------------===//
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000736
Benjamin Kramercfb51b62010-04-08 15:54:07 +0000737static inline llvm::StringRef extractUSRSuffix(llvm::StringRef s) {
738 return s.startswith("c:") ? s.substr(2) : "";
739}
740
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000741static CXString getDeclCursorUSR(const CXCursor &C) {
Ted Kremenek896b70f2010-03-13 02:50:34 +0000742 Decl *D = cxcursor::getCursorDecl(C);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000743
744 // Don't generate USRs for things with invalid locations.
745 if (!D || D->getLocStart().isInvalid())
Ted Kremenek1af0a2a2010-04-17 00:21:38 +0000746 return createCXString("");
Ted Kremenek896b70f2010-03-13 02:50:34 +0000747
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000748 // Check if the cursor has 'NoLinkage'.
Ted Kremeneke74ef122010-04-16 21:31:52 +0000749 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000750 switch (ND->getLinkage()) {
751 case ExternalLinkage:
752 // Generate USRs for all entities with external linkage.
753 break;
754 case NoLinkage:
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000755 case UniqueExternalLinkage:
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000756 // We allow enums, typedefs, and structs that have no linkage to
757 // have USRs that are anchored to the file they were defined in
758 // (e.g., the header). This is a little gross, but in principal
759 // enums/anonymous structs/etc. defined in a common header file
760 // are referred to across multiple translation units.
Ted Kremenek6f153952010-04-15 21:51:13 +0000761 if (isa<TagDecl>(ND) || isa<TypedefDecl>(ND) ||
Ted Kremenekcf999102010-04-29 17:43:29 +0000762 isa<EnumConstantDecl>(ND) || isa<FieldDecl>(ND) ||
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000763 isa<VarDecl>(ND) || isa<NamespaceDecl>(ND))
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000764 break;
765 // Fall-through.
766 case InternalLinkage:
Ted Kremenekcf999102010-04-29 17:43:29 +0000767 if (isa<FunctionDecl>(ND))
768 break;
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000769 }
770
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000771 USRGenerator UG(&C);
772 UG->Visit(D);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000773
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000774 if (UG->ignoreResults())
Ted Kremenekebfa3392010-03-19 20:39:03 +0000775 return createCXString("");
Ted Kremenek896b70f2010-03-13 02:50:34 +0000776
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000777#if 0
Ted Kremeneke542f772010-04-20 23:15:40 +0000778 // For development testing.
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000779 assert(UG.str().size() > 2);
780#endif
Ted Kremeneke542f772010-04-20 23:15:40 +0000781
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000782 // Return a copy of the string that must be disposed by the caller.
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000783 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000784}
785
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000786extern "C" {
787
788CXString clang_getCursorUSR(CXCursor C) {
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000789 const CXCursorKind &K = clang_getCursorKind(C);
790
791 if (clang_isDeclaration(K))
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000792 return getDeclCursorUSR(C);
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000793
794 if (K == CXCursor_MacroDefinition) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000795 USRGenerator UG(&C);
796 UG << "macro@"
797 << cxcursor::getCursorMacroDefinition(C)->getName()->getNameStart();
798 return createCXString(UG.str(), true);
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000799 }
800
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000801 return createCXString("");
802}
803
Ted Kremenek896b70f2010-03-13 02:50:34 +0000804CXString clang_constructUSR_ObjCIvar(const char *name, CXString classUSR) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000805 USRGenerator UG;
806 UG << extractUSRSuffix(clang_getCString(classUSR));
807 UG->GenObjCIvar(name);
808 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000809}
810
811CXString clang_constructUSR_ObjCMethod(const char *name,
812 unsigned isInstanceMethod,
813 CXString classUSR) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000814 USRGenerator UG;
815 UG << extractUSRSuffix(clang_getCString(classUSR));
816 UG->GenObjCMethod(name, isInstanceMethod);
817 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000818}
819
820CXString clang_constructUSR_ObjCClass(const char *name) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000821 USRGenerator UG;
822 UG->GenObjCClass(name);
823 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000824}
825
826CXString clang_constructUSR_ObjCProtocol(const char *name) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000827 USRGenerator UG;
828 UG->GenObjCProtocol(name);
829 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000830}
831
Ted Kremenek66ccaec2010-03-15 17:38:58 +0000832CXString clang_constructUSR_ObjCCategory(const char *class_name,
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000833 const char *category_name) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000834 USRGenerator UG;
835 UG->GenObjCCategory(class_name, category_name);
836 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000837}
838
839CXString clang_constructUSR_ObjCProperty(const char *property,
840 CXString classUSR) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000841 USRGenerator UG;
842 UG << extractUSRSuffix(clang_getCString(classUSR));
843 UG->GenObjCProperty(property);
844 return createCXString(UG.str(), true);
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000845}
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000846
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000847} // end extern "C"