blob: c96dad5cd0fe2e92325dfe25d7945d7c9eda8dd8 [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"
Ted Kremeneked122732010-11-16 01:56:27 +000016#include "CXString.h"
Douglas Gregorfe72e9c2010-08-31 17:01:39 +000017#include "clang/AST/DeclTemplate.h"
Benjamin Kramer9895c6a2010-01-12 11:32:40 +000018#include "clang/AST/DeclVisitor.h"
Ted Kremenek6f153952010-04-15 21:51:13 +000019#include "clang/Frontend/ASTUnit.h"
Benjamin Kramerb846deb2010-04-12 19:45:50 +000020#include "clang/Lex/PreprocessingRecord.h"
Ted Kremenek87763822010-01-12 02:07:58 +000021#include "llvm/ADT/SmallString.h"
Benjamin Kramer9895c6a2010-01-12 11:32:40 +000022#include "llvm/Support/raw_ostream.h"
Ted Kremenek6f153952010-04-15 21:51:13 +000023
Benjamin Kramerb846deb2010-04-12 19:45:50 +000024using namespace clang;
Ted Kremenekee4db4f2010-02-17 00:41:08 +000025using namespace clang::cxstring;
26
Ted Kremenekc50277f2010-01-12 23:33:42 +000027//===----------------------------------------------------------------------===//
28// USR generation.
29//===----------------------------------------------------------------------===//
30
31namespace {
Ted Kremenek2fee4e62010-01-14 01:50:21 +000032class USRGenerator : public DeclVisitor<USRGenerator> {
Ted Kremenekf0199432010-11-16 08:15:38 +000033 llvm::OwningPtr<llvm::SmallString<128> > OwnedBuf;
34 llvm::SmallVectorImpl<char> &Buf;
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +000035 llvm::raw_svector_ostream Out;
Ted Kremenek3adca6d2010-01-18 22:02:49 +000036 bool IgnoreResults;
Ted Kremenek1865cfe2010-04-15 21:04:25 +000037 ASTUnit *AU;
Ted Kremenekcbd66f02010-05-06 23:38:28 +000038 bool generatedLoc;
Douglas Gregor66b7fbf2010-09-20 20:37:39 +000039
40 llvm::DenseMap<const Type *, unsigned> TypeSubstitutions;
41
Ted Kremenek2fee4e62010-01-14 01:50:21 +000042public:
Ted Kremenekf0199432010-11-16 08:15:38 +000043 USRGenerator(const CXCursor *C = 0, llvm::SmallVectorImpl<char> *extBuf = 0)
44 : OwnedBuf(extBuf ? 0 : new llvm::SmallString<128>()),
45 Buf(extBuf ? *extBuf : *OwnedBuf.get()),
46 Out(Buf),
47 IgnoreResults(false),
48 AU(C ? cxcursor::getCursorASTUnit(*C) : 0),
49 generatedLoc(false)
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +000050 {
51 // Add the USR space prefix.
52 Out << "c:";
53 }
54
55 llvm::StringRef str() {
56 return Out.str();
57 }
58
59 USRGenerator* operator->() { return this; }
60
61 template <typename T>
62 llvm::raw_svector_ostream &operator<<(const T &x) {
63 Out << x;
64 return Out;
65 }
Ted Kremenek896b70f2010-03-13 02:50:34 +000066
Ted Kremenek3adca6d2010-01-18 22:02:49 +000067 bool ignoreResults() const { return IgnoreResults; }
Ted Kremenek896b70f2010-03-13 02:50:34 +000068
69 // Visitation methods from generating USRs from AST elements.
Ted Kremenek2fee4e62010-01-14 01:50:21 +000070 void VisitDeclContext(DeclContext *D);
Ted Kremenek3adca6d2010-01-18 22:02:49 +000071 void VisitFieldDecl(FieldDecl *D);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000072 void VisitFunctionDecl(FunctionDecl *D);
73 void VisitNamedDecl(NamedDecl *D);
74 void VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor69319002010-08-31 23:48:11 +000075 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +000076 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Douglas Gregor39d6f072010-08-31 19:02:00 +000077 void VisitClassTemplateDecl(ClassTemplateDecl *D);
Ted Kremeneke74ef122010-04-16 21:31:52 +000078 void VisitObjCClassDecl(ObjCClassDecl *CD);
Ted Kremenek896b70f2010-03-13 02:50:34 +000079 void VisitObjCContainerDecl(ObjCContainerDecl *CD);
Ted Kremeneke74ef122010-04-16 21:31:52 +000080 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *P);
Ted Kremeneke542f772010-04-20 23:15:40 +000081 void VisitObjCMethodDecl(ObjCMethodDecl *MD);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000082 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Ted Kremeneke542f772010-04-20 23:15:40 +000083 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Ted Kremenekb82b3be2010-01-18 22:42:20 +000084 void VisitTagDecl(TagDecl *D);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000085 void VisitTypedefDecl(TypedefDecl *D);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +000086 void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Ted Kremeneke542f772010-04-20 23:15:40 +000087 void VisitVarDecl(VarDecl *D);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +000088 void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
89 void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
Ted Kremenek8e672192010-05-07 01:04:32 +000090 void VisitLinkageSpecDecl(LinkageSpecDecl *D) {
91 IgnoreResults = true;
Ted Kremenek8e672192010-05-07 01:04:32 +000092 }
Douglas Gregor0a35bce2010-09-01 03:07:18 +000093 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
94 IgnoreResults = true;
95 }
Douglas Gregor7e242562010-09-01 19:52:22 +000096 void VisitUsingDecl(UsingDecl *D) {
97 IgnoreResults = true;
98 }
99 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
100 IgnoreResults = true;
101 }
102 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
103 IgnoreResults = true;
104 }
Douglas Gregor0a35bce2010-09-01 03:07:18 +0000105
Ted Kremenek6f153952010-04-15 21:51:13 +0000106 /// Generate the string component containing the location of the
107 /// declaration.
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000108 bool GenLoc(const Decl *D);
Ted Kremenek6f153952010-04-15 21:51:13 +0000109
Ted Kremenek896b70f2010-03-13 02:50:34 +0000110 /// String generation methods used both by the visitation methods
111 /// and from other clients that want to directly generate USRs. These
112 /// methods do not construct complete USRs (which incorporate the parents
113 /// of an AST element), but only the fragments concerning the AST element
114 /// itself.
115
Ted Kremenek896b70f2010-03-13 02:50:34 +0000116 /// Generate a USR for an Objective-C class.
117 void GenObjCClass(llvm::StringRef cls);
118 /// Generate a USR for an Objective-C class category.
119 void GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat);
120 /// Generate a USR fragment for an Objective-C instance variable. The
121 /// complete USR can be created by concatenating the USR for the
122 /// encompassing class with this USR fragment.
123 void GenObjCIvar(llvm::StringRef ivar);
124 /// Generate a USR fragment for an Objective-C method.
125 void GenObjCMethod(llvm::StringRef sel, bool isInstanceMethod);
126 /// Generate a USR fragment for an Objective-C property.
127 void GenObjCProperty(llvm::StringRef prop);
128 /// Generate a USR for an Objective-C protocol.
129 void GenObjCProtocol(llvm::StringRef prot);
Ted Kremenek8e672192010-05-07 01:04:32 +0000130
131 void VisitType(QualType T);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000132 void VisitTemplateParameterList(const TemplateParameterList *Params);
133 void VisitTemplateName(TemplateName Name);
134 void VisitTemplateArgument(const TemplateArgument &Arg);
135
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000136 /// Emit a Decl's name using NamedDecl::printName() and return true if
137 /// the decl had no name.
138 bool EmitDeclName(const NamedDecl *D);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000139};
140
Ted Kremenekc50277f2010-01-12 23:33:42 +0000141} // end anonymous namespace
142
Ted Kremenek896b70f2010-03-13 02:50:34 +0000143//===----------------------------------------------------------------------===//
144// Generating USRs from ASTS.
145//===----------------------------------------------------------------------===//
146
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000147bool USRGenerator::EmitDeclName(const NamedDecl *D) {
148 Out.flush();
149 const unsigned startSize = Buf.size();
150 D->printName(Out);
151 Out.flush();
152 const unsigned endSize = Buf.size();
153 return startSize == endSize;
154}
155
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000156static bool InAnonymousNamespace(const Decl *D) {
157 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(D->getDeclContext()))
158 return ND->isAnonymousNamespace();
159 return false;
160}
161
162static inline bool ShouldGenerateLocation(const NamedDecl *D) {
163 return D->getLinkage() != ExternalLinkage && !InAnonymousNamespace(D);
164}
165
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000166void USRGenerator::VisitDeclContext(DeclContext *DC) {
167 if (NamedDecl *D = dyn_cast<NamedDecl>(DC))
168 Visit(D);
169}
170
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000171void USRGenerator::VisitFieldDecl(FieldDecl *D) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000172 VisitDeclContext(D->getDeclContext());
173 Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@");
174 if (EmitDeclName(D)) {
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000175 // Bit fields can be anonymous.
176 IgnoreResults = true;
177 return;
178 }
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000179}
180
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000181void USRGenerator::VisitFunctionDecl(FunctionDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000182 if (ShouldGenerateLocation(D) && GenLoc(D))
183 return;
Ted Kremenekcf999102010-04-29 17:43:29 +0000184
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000185 VisitDeclContext(D->getDeclContext());
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000186 if (FunctionTemplateDecl *FunTmpl = D->getDescribedFunctionTemplate()) {
187 Out << "@FT@";
188 VisitTemplateParameterList(FunTmpl->getTemplateParameters());
189 } else
190 Out << "@F@";
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000191 D->printName(Out);
Ted Kremenek8e672192010-05-07 01:04:32 +0000192
193 ASTContext &Ctx = AU->getASTContext();
194 if (!Ctx.getLangOptions().CPlusPlus || D->isExternC())
195 return;
196
197 // Mangle in type information for the arguments.
198 for (FunctionDecl::param_iterator I = D->param_begin(), E = D->param_end();
199 I != E; ++I) {
200 Out << '#';
201 if (ParmVarDecl *PD = *I)
202 VisitType(PD->getType());
203 }
204 if (D->isVariadic())
205 Out << '.';
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000206 Out << '#';
207 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
208 if (MD->isStatic())
209 Out << 'S';
210 if (unsigned quals = MD->getTypeQualifiers())
211 Out << (char)('0' + quals);
212 }
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000213}
Ted Kremenekc50277f2010-01-12 23:33:42 +0000214
215void USRGenerator::VisitNamedDecl(NamedDecl *D) {
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000216 VisitDeclContext(D->getDeclContext());
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000217 Out << "@";
218
219 if (EmitDeclName(D)) {
220 // The string can be empty if the declaration has no name; e.g., it is
221 // the ParmDecl with no name for declaration of a function pointer type,
222 // e.g.: void (*f)(void *);
223 // In this case, don't generate a USR.
Ted Kremeneke74ef122010-04-16 21:31:52 +0000224 IgnoreResults = true;
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000225 }
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000226}
227
Ted Kremeneke542f772010-04-20 23:15:40 +0000228void USRGenerator::VisitVarDecl(VarDecl *D) {
229 // VarDecls can be declared 'extern' within a function or method body,
230 // but their enclosing DeclContext is the function, not the TU. We need
231 // to check the storage class to correctly generate the USR.
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000232 if (ShouldGenerateLocation(D) && GenLoc(D))
233 return;
234
235 VisitDeclContext(D->getDeclContext());
Ted Kremeneke542f772010-04-20 23:15:40 +0000236
Ted Kremenekcf999102010-04-29 17:43:29 +0000237 // Variables always have simple names.
238 llvm::StringRef s = D->getName();
239
Ted Kremeneke542f772010-04-20 23:15:40 +0000240 // The string can be empty if the declaration has no name; e.g., it is
241 // the ParmDecl with no name for declaration of a function pointer type, e.g.:
Eli Friedmana7e68452010-08-22 01:00:03 +0000242 // void (*f)(void *);
Ted Kremeneke542f772010-04-20 23:15:40 +0000243 // In this case, don't generate a USR.
244 if (s.empty())
245 IgnoreResults = true;
246 else
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000247 Out << '@' << s;
Ted Kremeneke542f772010-04-20 23:15:40 +0000248}
249
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000250void USRGenerator::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
251 GenLoc(D);
252 return;
253}
254
255void USRGenerator::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
256 GenLoc(D);
257 return;
258}
259
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000260void USRGenerator::VisitNamespaceDecl(NamespaceDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000261 if (D->isAnonymousNamespace()) {
262 Out << "@aN";
263 return;
264 }
265
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000266 VisitDeclContext(D->getDeclContext());
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000267 if (!IgnoreResults)
268 Out << "@N@" << D->getName();
Ted Kremenekc50277f2010-01-12 23:33:42 +0000269}
270
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000271void USRGenerator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
272 VisitFunctionDecl(D->getTemplatedDecl());
273}
274
Douglas Gregor39d6f072010-08-31 19:02:00 +0000275void USRGenerator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
276 VisitTagDecl(D->getTemplatedDecl());
277}
278
Douglas Gregor69319002010-08-31 23:48:11 +0000279void USRGenerator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
280 VisitDeclContext(D->getDeclContext());
281 if (!IgnoreResults)
282 Out << "@NA@" << D->getName();
283}
Douglas Gregor39d6f072010-08-31 19:02:00 +0000284
Ted Kremenekc50277f2010-01-12 23:33:42 +0000285void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) {
Ted Kremenek28a7f252010-08-24 23:13:41 +0000286 Decl *container = cast<Decl>(D->getDeclContext());
287
288 // The USR for a method declared in a class extension is based on
289 // the ObjCInterfaceDecl, not the ObjCCategoryDecl.
290 do {
291 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(container))
292 if (CD->IsClassExtension()) {
Ted Kremenekc52d0692010-09-21 04:45:46 +0000293 // ID can be null with invalid code.
294 if (ObjCInterfaceDecl *ID = CD->getClassInterface()) {
295 Visit(ID);
Ted Kremenek097727b2010-09-21 04:47:01 +0000296 break;
Ted Kremenekc52d0692010-09-21 04:45:46 +0000297 }
298 // Invalid code. Can't generate USR.
299 IgnoreResults = true;
300 return;
301 }
302
303 Visit(container);
Ted Kremenek28a7f252010-08-24 23:13:41 +0000304 }
305 while (false);
306
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000307 // Ideally we would use 'GenObjCMethod', but this is such a hot path
308 // for Objective-C code that we don't want to use
309 // DeclarationName::getAsString().
310 Out << (D->isInstanceMethod() ? "(im)" : "(cm)");
311 DeclarationName N(D->getSelector());
312 N.printName(Out);
Ted Kremenekc50277f2010-01-12 23:33:42 +0000313}
314
Ted Kremeneke74ef122010-04-16 21:31:52 +0000315void USRGenerator::VisitObjCClassDecl(ObjCClassDecl *D) {
316 // FIXME: @class declarations can refer to multiple classes. We need
317 // to be able to traverse these.
318 IgnoreResults = true;
319}
320
321void USRGenerator::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
322 // FIXME: @protocol declarations can refer to multiple protocols. We need
323 // to be able to traverse these.
324 IgnoreResults = true;
325}
326
Ted Kremenekc50277f2010-01-12 23:33:42 +0000327void USRGenerator::VisitObjCContainerDecl(ObjCContainerDecl *D) {
328 switch (D->getKind()) {
329 default:
330 assert(false && "Invalid ObjC container.");
331 case Decl::ObjCInterface:
332 case Decl::ObjCImplementation:
Ted Kremenek896b70f2010-03-13 02:50:34 +0000333 GenObjCClass(D->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000334 break;
335 case Decl::ObjCCategory: {
336 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
Ted Kremenekebfa3392010-03-19 20:39:03 +0000337 ObjCInterfaceDecl *ID = CD->getClassInterface();
338 if (!ID) {
339 // Handle invalid code where the @interface might not
340 // have been specified.
341 // FIXME: We should be able to generate this USR even if the
342 // @interface isn't available.
343 IgnoreResults = true;
344 return;
345 }
Ted Kremenek28a7f252010-08-24 23:13:41 +0000346 // Specially handle class extensions, which are anonymous categories.
347 // We want to mangle in the location to uniquely distinguish them.
348 if (CD->IsClassExtension()) {
349 Out << "objc(ext)" << ID->getName() << '@';
350 GenLoc(CD);
351 }
352 else
353 GenObjCCategory(ID->getName(), CD->getName());
354
Ted Kremenekc50277f2010-01-12 23:33:42 +0000355 break;
356 }
357 case Decl::ObjCCategoryImpl: {
358 ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
Ted Kremenekebfa3392010-03-19 20:39:03 +0000359 ObjCInterfaceDecl *ID = CD->getClassInterface();
360 if (!ID) {
361 // Handle invalid code where the @interface might not
362 // have been specified.
363 // FIXME: We should be able to generate this USR even if the
364 // @interface isn't available.
365 IgnoreResults = true;
366 return;
367 }
368 GenObjCCategory(ID->getName(), CD->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000369 break;
370 }
371 case Decl::ObjCProtocol:
Ted Kremenek896b70f2010-03-13 02:50:34 +0000372 GenObjCProtocol(cast<ObjCProtocolDecl>(D)->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000373 break;
374 }
375}
376
377void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
378 Visit(cast<Decl>(D->getDeclContext()));
Ted Kremenek896b70f2010-03-13 02:50:34 +0000379 GenObjCProperty(D->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000380}
381
Ted Kremeneke542f772010-04-20 23:15:40 +0000382void USRGenerator::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
383 if (ObjCPropertyDecl *PD = D->getPropertyDecl()) {
384 VisitObjCPropertyDecl(PD);
385 return;
386 }
387
388 IgnoreResults = true;
389}
390
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000391void USRGenerator::VisitTagDecl(TagDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000392 // Add the location of the tag decl to handle resolution across
393 // translation units.
394 if (ShouldGenerateLocation(D) && GenLoc(D))
395 return;
Ted Kremenek8e672192010-05-07 01:04:32 +0000396
Ted Kremenek6f153952010-04-15 21:51:13 +0000397 D = D->getCanonicalDecl();
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000398 VisitDeclContext(D->getDeclContext());
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000399
Douglas Gregor74dbe642010-08-31 19:31:58 +0000400 bool AlreadyStarted = false;
401 if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(D)) {
Douglas Gregor39d6f072010-08-31 19:02:00 +0000402 if (ClassTemplateDecl *ClassTmpl = CXXRecord->getDescribedClassTemplate()) {
Douglas Gregor74dbe642010-08-31 19:31:58 +0000403 AlreadyStarted = true;
Douglas Gregor39d6f072010-08-31 19:02:00 +0000404
405 switch (D->getTagKind()) {
Douglas Gregor74dbe642010-08-31 19:31:58 +0000406 case TTK_Struct: Out << "@ST"; break;
407 case TTK_Class: Out << "@CT"; break;
408 case TTK_Union: Out << "@UT"; break;
409 case TTK_Enum: llvm_unreachable("enum template"); break;
Douglas Gregor39d6f072010-08-31 19:02:00 +0000410 }
411 VisitTemplateParameterList(ClassTmpl->getTemplateParameters());
Douglas Gregor74dbe642010-08-31 19:31:58 +0000412 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
413 = dyn_cast<ClassTemplatePartialSpecializationDecl>(CXXRecord)) {
414 AlreadyStarted = true;
415
416 switch (D->getTagKind()) {
417 case TTK_Struct: Out << "@SP"; break;
418 case TTK_Class: Out << "@CP"; break;
419 case TTK_Union: Out << "@UP"; break;
420 case TTK_Enum: llvm_unreachable("enum partial specialization"); break;
421 }
422 VisitTemplateParameterList(PartialSpec->getTemplateParameters());
Douglas Gregor39d6f072010-08-31 19:02:00 +0000423 }
Douglas Gregor74dbe642010-08-31 19:31:58 +0000424 }
Douglas Gregor39d6f072010-08-31 19:02:00 +0000425
Douglas Gregor74dbe642010-08-31 19:31:58 +0000426 if (!AlreadyStarted) {
Douglas Gregor39d6f072010-08-31 19:02:00 +0000427 switch (D->getTagKind()) {
428 case TTK_Struct: Out << "@S"; break;
429 case TTK_Class: Out << "@C"; break;
430 case TTK_Union: Out << "@U"; break;
431 case TTK_Enum: Out << "@E"; break;
432 }
Ted Kremeneke74ef122010-04-16 21:31:52 +0000433 }
Douglas Gregor39d6f072010-08-31 19:02:00 +0000434
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000435 Out << '@';
436 Out.flush();
437 assert(Buf.size() > 0);
438 const unsigned off = Buf.size() - 1;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000439
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000440 if (EmitDeclName(D)) {
441 if (const TypedefDecl *TD = D->getTypedefForAnonDecl()) {
442 Buf[off] = 'A';
Benjamin Kramer900fc632010-04-17 09:33:03 +0000443 Out << '@' << TD;
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000444 }
445 else
446 Buf[off] = 'a';
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000447 }
Douglas Gregor0ab1e9f2010-09-01 17:32:36 +0000448
449 // For a class template specialization, mangle the template arguments.
450 if (ClassTemplateSpecializationDecl *Spec
451 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
452 const TemplateArgumentList &Args = Spec->getTemplateInstantiationArgs();
453 Out << '>';
454 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
455 Out << '#';
456 VisitTemplateArgument(Args.get(I));
457 }
458 }
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000459}
460
Ted Kremenekc50277f2010-01-12 23:33:42 +0000461void USRGenerator::VisitTypedefDecl(TypedefDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000462 if (ShouldGenerateLocation(D) && GenLoc(D))
463 return;
Ted Kremenekc50277f2010-01-12 23:33:42 +0000464 DeclContext *DC = D->getDeclContext();
465 if (NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
Ted Kremenek896b70f2010-03-13 02:50:34 +0000466 Visit(DCN);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000467 Out << "@T@";
Ted Kremenek6f153952010-04-15 21:51:13 +0000468 Out << D->getName();
469}
470
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000471void USRGenerator::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
472 GenLoc(D);
473 return;
474}
475
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000476bool USRGenerator::GenLoc(const Decl *D) {
477 if (generatedLoc)
478 return IgnoreResults;
479 generatedLoc = true;
480
Ted Kremenek6f153952010-04-15 21:51:13 +0000481 const SourceManager &SM = AU->getSourceManager();
482 SourceLocation L = D->getLocStart();
483 if (L.isInvalid()) {
484 IgnoreResults = true;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000485 return true;
Ted Kremenek6f153952010-04-15 21:51:13 +0000486 }
487 L = SM.getInstantiationLoc(L);
488 const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(L);
489 const FileEntry *FE = SM.getFileEntryForID(Decomposed.first);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000490 if (FE) {
Michael J. Spencer472ccff2010-12-18 00:19:12 +0000491 Out << llvm::sys::path::filename(FE->getName());
Ted Kremeneke74ef122010-04-16 21:31:52 +0000492 }
Ted Kremenek6f153952010-04-15 21:51:13 +0000493 else {
494 // This case really isn't interesting.
495 IgnoreResults = true;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000496 return true;
Ted Kremenek6f153952010-04-15 21:51:13 +0000497 }
Ted Kremenekf48b5312010-07-22 11:14:15 +0000498 // Use the offest into the FileID to represent the location. Using
499 // a line/column can cause us to look back at the original source file,
500 // which is expensive.
501 Out << '@' << Decomposed.second;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000502 return IgnoreResults;
Ted Kremenekc50277f2010-01-12 23:33:42 +0000503}
504
Ted Kremenek8e672192010-05-07 01:04:32 +0000505void USRGenerator::VisitType(QualType T) {
506 // This method mangles in USR information for types. It can possibly
507 // just reuse the naming-mangling logic used by codegen, although the
508 // requirements for USRs might not be the same.
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000509 ASTContext &Ctx = AU->getASTContext();
510
Ted Kremenek8e672192010-05-07 01:04:32 +0000511 do {
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000512 T = Ctx.getCanonicalType(T);
Ted Kremenek8e672192010-05-07 01:04:32 +0000513 Qualifiers Q = T.getQualifiers();
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000514 unsigned qVal = 0;
Ted Kremenek8e672192010-05-07 01:04:32 +0000515 if (Q.hasConst())
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000516 qVal |= 0x1;
Ted Kremenek8e672192010-05-07 01:04:32 +0000517 if (Q.hasVolatile())
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000518 qVal |= 0x2;
Ted Kremenek8e672192010-05-07 01:04:32 +0000519 if (Q.hasRestrict())
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000520 qVal |= 0x4;
521 if(qVal)
522 Out << ((char) ('0' + qVal));
Ted Kremenek8e672192010-05-07 01:04:32 +0000523
524 // Mangle in ObjC GC qualifiers?
525
Ted Kremenek8e672192010-05-07 01:04:32 +0000526 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
527 unsigned char c = '\0';
528 switch (BT->getKind()) {
529 case BuiltinType::Void:
530 c = 'v'; break;
531 case BuiltinType::Bool:
532 c = 'b'; break;
533 case BuiltinType::Char_U:
534 case BuiltinType::UChar:
535 c = 'c'; break;
536 case BuiltinType::Char16:
537 c = 'q'; break;
538 case BuiltinType::Char32:
539 c = 'w'; break;
540 case BuiltinType::UShort:
541 c = 's'; break;
542 case BuiltinType::UInt:
543 c = 'i'; break;
544 case BuiltinType::ULong:
545 c = 'l'; break;
546 case BuiltinType::ULongLong:
547 c = 'k'; break;
548 case BuiltinType::UInt128:
549 c = 'j'; break;
550 case BuiltinType::Char_S:
551 case BuiltinType::SChar:
552 c = 'C'; break;
Chris Lattner3f59c972010-12-25 23:25:43 +0000553 case BuiltinType::WChar_S:
554 case BuiltinType::WChar_U:
Ted Kremenek8e672192010-05-07 01:04:32 +0000555 c = 'W'; break;
556 case BuiltinType::Short:
557 c = 'S'; break;
558 case BuiltinType::Int:
559 c = 'I'; break;
560 case BuiltinType::Long:
561 c = 'L'; break;
562 case BuiltinType::LongLong:
563 c = 'K'; break;
564 case BuiltinType::Int128:
565 c = 'J'; break;
566 case BuiltinType::Float:
567 c = 'f'; break;
568 case BuiltinType::Double:
569 c = 'd'; break;
570 case BuiltinType::LongDouble:
571 c = 'D'; break;
572 case BuiltinType::NullPtr:
573 c = 'n'; break;
574 case BuiltinType::Overload:
575 case BuiltinType::Dependent:
576 case BuiltinType::UndeducedAuto:
577 IgnoreResults = true;
578 return;
579 case BuiltinType::ObjCId:
580 c = 'o'; break;
581 case BuiltinType::ObjCClass:
582 c = 'O'; break;
583 case BuiltinType::ObjCSel:
584 c = 'e'; break;
585 }
586 Out << c;
587 return;
588 }
Douglas Gregor66b7fbf2010-09-20 20:37:39 +0000589
590 // If we have already seen this (non-built-in) type, use a substitution
591 // encoding.
592 llvm::DenseMap<const Type *, unsigned>::iterator Substitution
593 = TypeSubstitutions.find(T.getTypePtr());
594 if (Substitution != TypeSubstitutions.end()) {
595 Out << 'S' << Substitution->second << '_';
596 return;
597 } else {
598 // Record this as a substitution.
599 unsigned Number = TypeSubstitutions.size();
600 TypeSubstitutions[T.getTypePtr()] = Number;
601 }
602
603 if (const PointerType *PT = T->getAs<PointerType>()) {
604 Out << '*';
605 T = PT->getPointeeType();
606 continue;
607 }
608 if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
609 Out << '&';
610 T = RT->getPointeeType();
611 continue;
612 }
613 if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) {
614 Out << 'F';
615 VisitType(FT->getResultType());
616 for (FunctionProtoType::arg_type_iterator
617 I = FT->arg_type_begin(), E = FT->arg_type_end(); I!=E; ++I) {
618 VisitType(*I);
619 }
620 if (FT->isVariadic())
621 Out << '.';
622 return;
623 }
624 if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) {
625 Out << 'B';
626 T = BT->getPointeeType();
627 continue;
628 }
Ted Kremenek8e672192010-05-07 01:04:32 +0000629 if (const ComplexType *CT = T->getAs<ComplexType>()) {
630 Out << '<';
631 T = CT->getElementType();
632 continue;
633 }
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000634 if (const TagType *TT = T->getAs<TagType>()) {
635 Out << '$';
636 VisitTagDecl(TT->getDecl());
637 return;
638 }
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000639 if (const TemplateTypeParmType *TTP = T->getAs<TemplateTypeParmType>()) {
640 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
641 return;
642 }
643 if (const TemplateSpecializationType *Spec
644 = T->getAs<TemplateSpecializationType>()) {
645 Out << '>';
646 VisitTemplateName(Spec->getTemplateName());
647 Out << Spec->getNumArgs();
648 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
649 VisitTemplateArgument(Spec->getArg(I));
650 return;
651 }
652
Ted Kremenek8e672192010-05-07 01:04:32 +0000653 // Unhandled type.
654 Out << ' ';
655 break;
656 } while (true);
657}
658
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000659void USRGenerator::VisitTemplateParameterList(
660 const TemplateParameterList *Params) {
661 if (!Params)
662 return;
663 Out << '>' << Params->size();
664 for (TemplateParameterList::const_iterator P = Params->begin(),
665 PEnd = Params->end();
666 P != PEnd; ++P) {
667 Out << '#';
668 if (isa<TemplateTypeParmDecl>(*P)) {
669 Out << 'T';
670 continue;
671 }
672
673 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
674 Out << 'N';
675 VisitType(NTTP->getType());
676 continue;
677 }
678
679 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
680 Out << 't';
681 VisitTemplateParameterList(TTP->getTemplateParameters());
682 }
683}
684
685void USRGenerator::VisitTemplateName(TemplateName Name) {
686 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
687 if (TemplateTemplateParmDecl *TTP
688 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
689 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
690 return;
691 }
692
693 Visit(Template);
694 return;
695 }
696
697 // FIXME: Visit dependent template names.
698}
699
700void USRGenerator::VisitTemplateArgument(const TemplateArgument &Arg) {
701 switch (Arg.getKind()) {
702 case TemplateArgument::Null:
703 break;
704
705 case TemplateArgument::Declaration:
Douglas Gregor97475832010-10-05 18:37:06 +0000706 if (Decl *D = Arg.getAsDecl())
707 Visit(D);
Douglas Gregorfe72e9c2010-08-31 17:01:39 +0000708 break;
709
710 case TemplateArgument::Template:
711 VisitTemplateName(Arg.getAsTemplate());
712 break;
713
714 case TemplateArgument::Expression:
715 // FIXME: Visit expressions.
716 break;
717
718 case TemplateArgument::Pack:
719 // FIXME: Variadic templates
720 break;
721
722 case TemplateArgument::Type:
723 VisitType(Arg.getAsType());
724 break;
725
726 case TemplateArgument::Integral:
727 Out << 'V';
728 VisitType(Arg.getIntegralType());
729 Out << *Arg.getAsIntegral();
730 break;
731 }
732}
733
Ted Kremenek896b70f2010-03-13 02:50:34 +0000734//===----------------------------------------------------------------------===//
735// General purpose USR generation methods.
736//===----------------------------------------------------------------------===//
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000737
Ted Kremenek896b70f2010-03-13 02:50:34 +0000738void USRGenerator::GenObjCClass(llvm::StringRef cls) {
739 Out << "objc(cs)" << cls;
740}
741
742void USRGenerator::GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat) {
Ted Kremeneke74ef122010-04-16 21:31:52 +0000743 Out << "objc(cy)" << cls << '@' << cat;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000744}
745
746void USRGenerator::GenObjCIvar(llvm::StringRef ivar) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000747 Out << '@' << ivar;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000748}
749
750void USRGenerator::GenObjCMethod(llvm::StringRef meth, bool isInstanceMethod) {
751 Out << (isInstanceMethod ? "(im)" : "(cm)") << meth;
752}
753
754void USRGenerator::GenObjCProperty(llvm::StringRef prop) {
755 Out << "(py)" << prop;
756}
757
758void USRGenerator::GenObjCProtocol(llvm::StringRef prot) {
759 Out << "objc(pl)" << prot;
760}
761
762//===----------------------------------------------------------------------===//
763// API hooks.
764//===----------------------------------------------------------------------===//
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000765
Benjamin Kramercfb51b62010-04-08 15:54:07 +0000766static inline llvm::StringRef extractUSRSuffix(llvm::StringRef s) {
767 return s.startswith("c:") ? s.substr(2) : "";
768}
769
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000770static CXString getDeclCursorUSR(const CXCursor &C) {
Ted Kremenek896b70f2010-03-13 02:50:34 +0000771 Decl *D = cxcursor::getCursorDecl(C);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000772
773 // Don't generate USRs for things with invalid locations.
774 if (!D || D->getLocStart().isInvalid())
Ted Kremenek1af0a2a2010-04-17 00:21:38 +0000775 return createCXString("");
Ted Kremenek896b70f2010-03-13 02:50:34 +0000776
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000777 // Check if the cursor has 'NoLinkage'.
Ted Kremeneke74ef122010-04-16 21:31:52 +0000778 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000779 switch (ND->getLinkage()) {
780 case ExternalLinkage:
781 // Generate USRs for all entities with external linkage.
782 break;
783 case NoLinkage:
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000784 case UniqueExternalLinkage:
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000785 // We allow enums, typedefs, and structs that have no linkage to
786 // have USRs that are anchored to the file they were defined in
787 // (e.g., the header). This is a little gross, but in principal
788 // enums/anonymous structs/etc. defined in a common header file
789 // are referred to across multiple translation units.
Ted Kremenek6f153952010-04-15 21:51:13 +0000790 if (isa<TagDecl>(ND) || isa<TypedefDecl>(ND) ||
Ted Kremenekcf999102010-04-29 17:43:29 +0000791 isa<EnumConstantDecl>(ND) || isa<FieldDecl>(ND) ||
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000792 isa<VarDecl>(ND) || isa<NamespaceDecl>(ND))
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000793 break;
794 // Fall-through.
795 case InternalLinkage:
Ted Kremenekcf999102010-04-29 17:43:29 +0000796 if (isa<FunctionDecl>(ND))
797 break;
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000798 }
799
Ted Kremenekf0199432010-11-16 08:15:38 +0000800 CXTranslationUnit TU = cxcursor::getCursorTU(C);
801 if (!TU)
Ted Kremenekebfa3392010-03-19 20:39:03 +0000802 return createCXString("");
Ted Kremenek896b70f2010-03-13 02:50:34 +0000803
Ted Kremenekf0199432010-11-16 08:15:38 +0000804 CXStringBuf *buf = cxstring::getCXStringBuf(TU);
805 if (!buf)
806 return createCXString("");
807
808 {
809 USRGenerator UG(&C, &buf->Data);
810 UG->Visit(D);
Ted Kremeneke542f772010-04-20 23:15:40 +0000811
Ted Kremenekf0199432010-11-16 08:15:38 +0000812 if (UG->ignoreResults()) {
813 disposeCXStringBuf(buf);
814 return createCXString("");
815 }
816 }
817 // Return the C-string, but don't make a copy since it is already in
818 // the string buffer.
819 buf->Data.push_back('\0');
820 return createCXString(buf);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000821}
822
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000823extern "C" {
824
825CXString clang_getCursorUSR(CXCursor C) {
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000826 const CXCursorKind &K = clang_getCursorKind(C);
827
828 if (clang_isDeclaration(K))
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000829 return getDeclCursorUSR(C);
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000830
831 if (K == CXCursor_MacroDefinition) {
Ted Kremenekf0199432010-11-16 08:15:38 +0000832 CXTranslationUnit TU = cxcursor::getCursorTU(C);
833 if (!TU)
834 return createCXString("");
835
836 CXStringBuf *buf = cxstring::getCXStringBuf(TU);
837 if (!buf)
838 return createCXString("");
839
840 {
841 USRGenerator UG(&C, &buf->Data);
842 UG << "macro@"
843 << cxcursor::getCursorMacroDefinition(C)->getName()->getNameStart();
844 }
845 buf->Data.push_back('\0');
846 return createCXString(buf);
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000847 }
848
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000849 return createCXString("");
850}
851
Ted Kremenek896b70f2010-03-13 02:50:34 +0000852CXString clang_constructUSR_ObjCIvar(const char *name, CXString classUSR) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000853 USRGenerator UG;
854 UG << extractUSRSuffix(clang_getCString(classUSR));
855 UG->GenObjCIvar(name);
856 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000857}
858
859CXString clang_constructUSR_ObjCMethod(const char *name,
860 unsigned isInstanceMethod,
861 CXString classUSR) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000862 USRGenerator UG;
863 UG << extractUSRSuffix(clang_getCString(classUSR));
864 UG->GenObjCMethod(name, isInstanceMethod);
865 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000866}
867
868CXString clang_constructUSR_ObjCClass(const char *name) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000869 USRGenerator UG;
870 UG->GenObjCClass(name);
871 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000872}
873
874CXString clang_constructUSR_ObjCProtocol(const char *name) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000875 USRGenerator UG;
876 UG->GenObjCProtocol(name);
877 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000878}
879
Ted Kremenek66ccaec2010-03-15 17:38:58 +0000880CXString clang_constructUSR_ObjCCategory(const char *class_name,
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000881 const char *category_name) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000882 USRGenerator UG;
883 UG->GenObjCCategory(class_name, category_name);
884 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000885}
886
887CXString clang_constructUSR_ObjCProperty(const char *property,
888 CXString classUSR) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000889 USRGenerator UG;
890 UG << extractUSRSuffix(clang_getCString(classUSR));
891 UG->GenObjCProperty(property);
892 return createCXString(UG.str(), true);
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000893}
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000894
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000895} // end extern "C"