blob: 0227b2c037e0469b1cb8ffa9c18fc57847c98ce7 [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"
Benjamin Kramer9895c6a2010-01-12 11:32:40 +000016#include "clang/AST/DeclVisitor.h"
Ted Kremenek6f153952010-04-15 21:51:13 +000017#include "clang/Frontend/ASTUnit.h"
Benjamin Kramerb846deb2010-04-12 19:45:50 +000018#include "clang/Lex/PreprocessingRecord.h"
Ted Kremenek87763822010-01-12 02:07:58 +000019#include "llvm/ADT/SmallString.h"
Benjamin Kramer9895c6a2010-01-12 11:32:40 +000020#include "llvm/Support/raw_ostream.h"
Ted Kremenek6f153952010-04-15 21:51:13 +000021
Benjamin Kramerb846deb2010-04-12 19:45:50 +000022using namespace clang;
Ted Kremenekee4db4f2010-02-17 00:41:08 +000023using namespace clang::cxstring;
24
Ted Kremenekc50277f2010-01-12 23:33:42 +000025//===----------------------------------------------------------------------===//
26// USR generation.
27//===----------------------------------------------------------------------===//
28
29namespace {
Ted Kremenek2fee4e62010-01-14 01:50:21 +000030class USRGenerator : public DeclVisitor<USRGenerator> {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +000031 llvm::SmallString<1024> Buf;
32 llvm::raw_svector_ostream Out;
Ted Kremenek3adca6d2010-01-18 22:02:49 +000033 bool IgnoreResults;
Ted Kremenek1865cfe2010-04-15 21:04:25 +000034 ASTUnit *AU;
Ted Kremenekcbd66f02010-05-06 23:38:28 +000035 bool generatedLoc;
Ted Kremenek2fee4e62010-01-14 01:50:21 +000036public:
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +000037 USRGenerator(const CXCursor *C = 0)
38 : Out(Buf),
39 IgnoreResults(false),
40 AU(C ? cxcursor::getCursorASTUnit(*C) : 0),
41 generatedLoc(false)
42 {
43 // Add the USR space prefix.
44 Out << "c:";
45 }
46
47 llvm::StringRef str() {
48 return Out.str();
49 }
50
51 USRGenerator* operator->() { return this; }
52
53 template <typename T>
54 llvm::raw_svector_ostream &operator<<(const T &x) {
55 Out << x;
56 return Out;
57 }
Ted Kremenek896b70f2010-03-13 02:50:34 +000058
Ted Kremenek3adca6d2010-01-18 22:02:49 +000059 bool ignoreResults() const { return IgnoreResults; }
Ted Kremenek896b70f2010-03-13 02:50:34 +000060
61 // Visitation methods from generating USRs from AST elements.
Ted Kremenek2fee4e62010-01-14 01:50:21 +000062 void VisitDeclContext(DeclContext *D);
Ted Kremenek3adca6d2010-01-18 22:02:49 +000063 void VisitFieldDecl(FieldDecl *D);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000064 void VisitFunctionDecl(FunctionDecl *D);
65 void VisitNamedDecl(NamedDecl *D);
66 void VisitNamespaceDecl(NamespaceDecl *D);
Ted Kremeneke74ef122010-04-16 21:31:52 +000067 void VisitObjCClassDecl(ObjCClassDecl *CD);
Ted Kremenek896b70f2010-03-13 02:50:34 +000068 void VisitObjCContainerDecl(ObjCContainerDecl *CD);
Ted Kremeneke74ef122010-04-16 21:31:52 +000069 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *P);
Ted Kremeneke542f772010-04-20 23:15:40 +000070 void VisitObjCMethodDecl(ObjCMethodDecl *MD);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000071 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Ted Kremeneke542f772010-04-20 23:15:40 +000072 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Ted Kremenekb82b3be2010-01-18 22:42:20 +000073 void VisitTagDecl(TagDecl *D);
Ted Kremenek2fee4e62010-01-14 01:50:21 +000074 void VisitTypedefDecl(TypedefDecl *D);
Ted Kremeneke542f772010-04-20 23:15:40 +000075 void VisitVarDecl(VarDecl *D);
Ted Kremenek8e672192010-05-07 01:04:32 +000076 void VisitLinkageSpecDecl(LinkageSpecDecl *D) {
77 IgnoreResults = true;
78 return;
79 }
Ted Kremenek896b70f2010-03-13 02:50:34 +000080
Ted Kremenek6f153952010-04-15 21:51:13 +000081 /// Generate the string component containing the location of the
82 /// declaration.
Ted Kremenekcbd66f02010-05-06 23:38:28 +000083 bool GenLoc(const Decl *D);
Ted Kremenek6f153952010-04-15 21:51:13 +000084
Ted Kremenek896b70f2010-03-13 02:50:34 +000085 /// String generation methods used both by the visitation methods
86 /// and from other clients that want to directly generate USRs. These
87 /// methods do not construct complete USRs (which incorporate the parents
88 /// of an AST element), but only the fragments concerning the AST element
89 /// itself.
90
Ted Kremenek896b70f2010-03-13 02:50:34 +000091 /// Generate a USR for an Objective-C class.
92 void GenObjCClass(llvm::StringRef cls);
93 /// Generate a USR for an Objective-C class category.
94 void GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat);
95 /// Generate a USR fragment for an Objective-C instance variable. The
96 /// complete USR can be created by concatenating the USR for the
97 /// encompassing class with this USR fragment.
98 void GenObjCIvar(llvm::StringRef ivar);
99 /// Generate a USR fragment for an Objective-C method.
100 void GenObjCMethod(llvm::StringRef sel, bool isInstanceMethod);
101 /// Generate a USR fragment for an Objective-C property.
102 void GenObjCProperty(llvm::StringRef prop);
103 /// Generate a USR for an Objective-C protocol.
104 void GenObjCProtocol(llvm::StringRef prot);
Ted Kremenek8e672192010-05-07 01:04:32 +0000105
106 void VisitType(QualType T);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000107
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000108 /// Emit a Decl's name using NamedDecl::printName() and return true if
109 /// the decl had no name.
110 bool EmitDeclName(const NamedDecl *D);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000111};
112
Ted Kremenekc50277f2010-01-12 23:33:42 +0000113} // end anonymous namespace
114
Ted Kremenek896b70f2010-03-13 02:50:34 +0000115//===----------------------------------------------------------------------===//
116// Generating USRs from ASTS.
117//===----------------------------------------------------------------------===//
118
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000119bool USRGenerator::EmitDeclName(const NamedDecl *D) {
120 Out.flush();
121 const unsigned startSize = Buf.size();
122 D->printName(Out);
123 Out.flush();
124 const unsigned endSize = Buf.size();
125 return startSize == endSize;
126}
127
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000128static bool InAnonymousNamespace(const Decl *D) {
129 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(D->getDeclContext()))
130 return ND->isAnonymousNamespace();
131 return false;
132}
133
134static inline bool ShouldGenerateLocation(const NamedDecl *D) {
135 return D->getLinkage() != ExternalLinkage && !InAnonymousNamespace(D);
136}
137
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000138void USRGenerator::VisitDeclContext(DeclContext *DC) {
139 if (NamedDecl *D = dyn_cast<NamedDecl>(DC))
140 Visit(D);
141}
142
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000143void USRGenerator::VisitFieldDecl(FieldDecl *D) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000144 VisitDeclContext(D->getDeclContext());
145 Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@");
146 if (EmitDeclName(D)) {
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000147 // Bit fields can be anonymous.
148 IgnoreResults = true;
149 return;
150 }
Ted Kremenek3adca6d2010-01-18 22:02:49 +0000151}
152
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000153void USRGenerator::VisitFunctionDecl(FunctionDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000154 if (ShouldGenerateLocation(D) && GenLoc(D))
155 return;
Ted Kremenekcf999102010-04-29 17:43:29 +0000156
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000157 VisitDeclContext(D->getDeclContext());
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000158 Out << "@F@";
159 D->printName(Out);
Ted Kremenek8e672192010-05-07 01:04:32 +0000160
161 ASTContext &Ctx = AU->getASTContext();
162 if (!Ctx.getLangOptions().CPlusPlus || D->isExternC())
163 return;
164
165 // Mangle in type information for the arguments.
166 for (FunctionDecl::param_iterator I = D->param_begin(), E = D->param_end();
167 I != E; ++I) {
168 Out << '#';
169 if (ParmVarDecl *PD = *I)
170 VisitType(PD->getType());
171 }
172 if (D->isVariadic())
173 Out << '.';
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000174 Out << '#';
175 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
176 if (MD->isStatic())
177 Out << 'S';
178 if (unsigned quals = MD->getTypeQualifiers())
179 Out << (char)('0' + quals);
180 }
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000181}
Ted Kremenekc50277f2010-01-12 23:33:42 +0000182
183void USRGenerator::VisitNamedDecl(NamedDecl *D) {
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000184 VisitDeclContext(D->getDeclContext());
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000185 Out << "@";
186
187 if (EmitDeclName(D)) {
188 // The string can be empty if the declaration has no name; e.g., it is
189 // the ParmDecl with no name for declaration of a function pointer type,
190 // e.g.: void (*f)(void *);
191 // In this case, don't generate a USR.
Ted Kremeneke74ef122010-04-16 21:31:52 +0000192 IgnoreResults = true;
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000193 }
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000194}
195
Ted Kremeneke542f772010-04-20 23:15:40 +0000196void USRGenerator::VisitVarDecl(VarDecl *D) {
197 // VarDecls can be declared 'extern' within a function or method body,
198 // but their enclosing DeclContext is the function, not the TU. We need
199 // to check the storage class to correctly generate the USR.
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000200 if (ShouldGenerateLocation(D) && GenLoc(D))
201 return;
202
203 VisitDeclContext(D->getDeclContext());
Ted Kremeneke542f772010-04-20 23:15:40 +0000204
Ted Kremenekcf999102010-04-29 17:43:29 +0000205 // Variables always have simple names.
206 llvm::StringRef s = D->getName();
207
Ted Kremeneke542f772010-04-20 23:15:40 +0000208 // The string can be empty if the declaration has no name; e.g., it is
209 // the ParmDecl with no name for declaration of a function pointer type, e.g.:
Eli Friedmana7e68452010-08-22 01:00:03 +0000210 // void (*f)(void *);
Ted Kremeneke542f772010-04-20 23:15:40 +0000211 // In this case, don't generate a USR.
212 if (s.empty())
213 IgnoreResults = true;
214 else
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000215 Out << '@' << s;
Ted Kremeneke542f772010-04-20 23:15:40 +0000216}
217
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000218void USRGenerator::VisitNamespaceDecl(NamespaceDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000219 if (D->isAnonymousNamespace()) {
220 Out << "@aN";
221 return;
222 }
223
Ted Kremenek2fee4e62010-01-14 01:50:21 +0000224 VisitDeclContext(D->getDeclContext());
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000225 if (!IgnoreResults)
226 Out << "@N@" << D->getName();
Ted Kremenekc50277f2010-01-12 23:33:42 +0000227}
228
Ted Kremenekc50277f2010-01-12 23:33:42 +0000229void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) {
Ted Kremenek28a7f252010-08-24 23:13:41 +0000230 Decl *container = cast<Decl>(D->getDeclContext());
231
232 // The USR for a method declared in a class extension is based on
233 // the ObjCInterfaceDecl, not the ObjCCategoryDecl.
234 do {
235 if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(container))
236 if (CD->IsClassExtension()) {
237 Visit(CD->getClassInterface());
238 break;
239 }
240 Visit(cast<Decl>(D->getDeclContext()));
241 }
242 while (false);
243
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000244 // Ideally we would use 'GenObjCMethod', but this is such a hot path
245 // for Objective-C code that we don't want to use
246 // DeclarationName::getAsString().
247 Out << (D->isInstanceMethod() ? "(im)" : "(cm)");
248 DeclarationName N(D->getSelector());
249 N.printName(Out);
Ted Kremenekc50277f2010-01-12 23:33:42 +0000250}
251
Ted Kremeneke74ef122010-04-16 21:31:52 +0000252void USRGenerator::VisitObjCClassDecl(ObjCClassDecl *D) {
253 // FIXME: @class declarations can refer to multiple classes. We need
254 // to be able to traverse these.
255 IgnoreResults = true;
256}
257
258void USRGenerator::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
259 // FIXME: @protocol declarations can refer to multiple protocols. We need
260 // to be able to traverse these.
261 IgnoreResults = true;
262}
263
Ted Kremenekc50277f2010-01-12 23:33:42 +0000264void USRGenerator::VisitObjCContainerDecl(ObjCContainerDecl *D) {
265 switch (D->getKind()) {
266 default:
267 assert(false && "Invalid ObjC container.");
268 case Decl::ObjCInterface:
269 case Decl::ObjCImplementation:
Ted Kremenek896b70f2010-03-13 02:50:34 +0000270 GenObjCClass(D->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000271 break;
272 case Decl::ObjCCategory: {
273 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
Ted Kremenekebfa3392010-03-19 20:39:03 +0000274 ObjCInterfaceDecl *ID = CD->getClassInterface();
275 if (!ID) {
276 // Handle invalid code where the @interface might not
277 // have been specified.
278 // FIXME: We should be able to generate this USR even if the
279 // @interface isn't available.
280 IgnoreResults = true;
281 return;
282 }
Ted Kremenek28a7f252010-08-24 23:13:41 +0000283 // Specially handle class extensions, which are anonymous categories.
284 // We want to mangle in the location to uniquely distinguish them.
285 if (CD->IsClassExtension()) {
286 Out << "objc(ext)" << ID->getName() << '@';
287 GenLoc(CD);
288 }
289 else
290 GenObjCCategory(ID->getName(), CD->getName());
291
Ted Kremenekc50277f2010-01-12 23:33:42 +0000292 break;
293 }
294 case Decl::ObjCCategoryImpl: {
295 ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
Ted Kremenekebfa3392010-03-19 20:39:03 +0000296 ObjCInterfaceDecl *ID = CD->getClassInterface();
297 if (!ID) {
298 // Handle invalid code where the @interface might not
299 // have been specified.
300 // FIXME: We should be able to generate this USR even if the
301 // @interface isn't available.
302 IgnoreResults = true;
303 return;
304 }
305 GenObjCCategory(ID->getName(), CD->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000306 break;
307 }
308 case Decl::ObjCProtocol:
Ted Kremenek896b70f2010-03-13 02:50:34 +0000309 GenObjCProtocol(cast<ObjCProtocolDecl>(D)->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000310 break;
311 }
312}
313
314void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
315 Visit(cast<Decl>(D->getDeclContext()));
Ted Kremenek896b70f2010-03-13 02:50:34 +0000316 GenObjCProperty(D->getName());
Ted Kremenekc50277f2010-01-12 23:33:42 +0000317}
318
Ted Kremeneke542f772010-04-20 23:15:40 +0000319void USRGenerator::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
320 if (ObjCPropertyDecl *PD = D->getPropertyDecl()) {
321 VisitObjCPropertyDecl(PD);
322 return;
323 }
324
325 IgnoreResults = true;
326}
327
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000328void USRGenerator::VisitTagDecl(TagDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000329 // Add the location of the tag decl to handle resolution across
330 // translation units.
331 if (ShouldGenerateLocation(D) && GenLoc(D))
332 return;
Ted Kremenek8e672192010-05-07 01:04:32 +0000333
Ted Kremenek6f153952010-04-15 21:51:13 +0000334 D = D->getCanonicalDecl();
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000335 VisitDeclContext(D->getDeclContext());
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000336
Ted Kremenekb82b3be2010-01-18 22:42:20 +0000337 switch (D->getTagKind()) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000338 case TTK_Struct: Out << "@S"; break;
339 case TTK_Class: Out << "@C"; break;
340 case TTK_Union: Out << "@U"; break;
341 case TTK_Enum: Out << "@E"; break;
Ted Kremeneke74ef122010-04-16 21:31:52 +0000342 }
343
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000344 Out << '@';
345 Out.flush();
346 assert(Buf.size() > 0);
347 const unsigned off = Buf.size() - 1;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000348
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000349 if (EmitDeclName(D)) {
350 if (const TypedefDecl *TD = D->getTypedefForAnonDecl()) {
351 Buf[off] = 'A';
Benjamin Kramer900fc632010-04-17 09:33:03 +0000352 Out << '@' << TD;
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000353 }
354 else
355 Buf[off] = 'a';
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000356 }
Ted Kremenekc5b48b32010-01-15 23:34:31 +0000357}
358
Ted Kremenekc50277f2010-01-12 23:33:42 +0000359void USRGenerator::VisitTypedefDecl(TypedefDecl *D) {
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000360 if (ShouldGenerateLocation(D) && GenLoc(D))
361 return;
Ted Kremenekc50277f2010-01-12 23:33:42 +0000362 DeclContext *DC = D->getDeclContext();
363 if (NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
Ted Kremenek896b70f2010-03-13 02:50:34 +0000364 Visit(DCN);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000365 Out << "@T@";
Ted Kremenek6f153952010-04-15 21:51:13 +0000366 Out << D->getName();
367}
368
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000369bool USRGenerator::GenLoc(const Decl *D) {
370 if (generatedLoc)
371 return IgnoreResults;
372 generatedLoc = true;
373
Ted Kremenek6f153952010-04-15 21:51:13 +0000374 const SourceManager &SM = AU->getSourceManager();
375 SourceLocation L = D->getLocStart();
376 if (L.isInvalid()) {
377 IgnoreResults = true;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000378 return true;
Ted Kremenek6f153952010-04-15 21:51:13 +0000379 }
380 L = SM.getInstantiationLoc(L);
381 const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(L);
382 const FileEntry *FE = SM.getFileEntryForID(Decomposed.first);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000383 if (FE) {
384 llvm::sys::Path P(FE->getName());
385 Out << P.getLast();
386 }
Ted Kremenek6f153952010-04-15 21:51:13 +0000387 else {
388 // This case really isn't interesting.
389 IgnoreResults = true;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000390 return true;
Ted Kremenek6f153952010-04-15 21:51:13 +0000391 }
Ted Kremenekf48b5312010-07-22 11:14:15 +0000392 // Use the offest into the FileID to represent the location. Using
393 // a line/column can cause us to look back at the original source file,
394 // which is expensive.
395 Out << '@' << Decomposed.second;
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000396 return IgnoreResults;
Ted Kremenekc50277f2010-01-12 23:33:42 +0000397}
398
Ted Kremenek8e672192010-05-07 01:04:32 +0000399void USRGenerator::VisitType(QualType T) {
400 // This method mangles in USR information for types. It can possibly
401 // just reuse the naming-mangling logic used by codegen, although the
402 // requirements for USRs might not be the same.
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000403 ASTContext &Ctx = AU->getASTContext();
404
Ted Kremenek8e672192010-05-07 01:04:32 +0000405 do {
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000406 T = Ctx.getCanonicalType(T);
Ted Kremenek8e672192010-05-07 01:04:32 +0000407 Qualifiers Q = T.getQualifiers();
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000408 unsigned qVal = 0;
Ted Kremenek8e672192010-05-07 01:04:32 +0000409 if (Q.hasConst())
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000410 qVal |= 0x1;
Ted Kremenek8e672192010-05-07 01:04:32 +0000411 if (Q.hasVolatile())
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000412 qVal |= 0x2;
Ted Kremenek8e672192010-05-07 01:04:32 +0000413 if (Q.hasRestrict())
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000414 qVal |= 0x4;
415 if(qVal)
416 Out << ((char) ('0' + qVal));
Ted Kremenek8e672192010-05-07 01:04:32 +0000417
418 // Mangle in ObjC GC qualifiers?
419
420 if (const PointerType *PT = T->getAs<PointerType>()) {
421 Out << '*';
422 T = PT->getPointeeType();
423 continue;
424 }
425 if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
426 Out << '&';
427 T = RT->getPointeeType();
428 continue;
429 }
430 if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) {
431 Out << 'F';
432 VisitType(FT->getResultType());
433 for (FunctionProtoType::arg_type_iterator
434 I = FT->arg_type_begin(), E = FT->arg_type_end(); I!=E; ++I) {
435 VisitType(*I);
436 }
437 if (FT->isVariadic())
438 Out << '.';
439 return;
440 }
441 if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) {
442 Out << 'B';
443 T = BT->getPointeeType();
444 continue;
445 }
446 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
447 unsigned char c = '\0';
448 switch (BT->getKind()) {
449 case BuiltinType::Void:
450 c = 'v'; break;
451 case BuiltinType::Bool:
452 c = 'b'; break;
453 case BuiltinType::Char_U:
454 case BuiltinType::UChar:
455 c = 'c'; break;
456 case BuiltinType::Char16:
457 c = 'q'; break;
458 case BuiltinType::Char32:
459 c = 'w'; break;
460 case BuiltinType::UShort:
461 c = 's'; break;
462 case BuiltinType::UInt:
463 c = 'i'; break;
464 case BuiltinType::ULong:
465 c = 'l'; break;
466 case BuiltinType::ULongLong:
467 c = 'k'; break;
468 case BuiltinType::UInt128:
469 c = 'j'; break;
470 case BuiltinType::Char_S:
471 case BuiltinType::SChar:
472 c = 'C'; break;
473 case BuiltinType::WChar:
474 c = 'W'; break;
475 case BuiltinType::Short:
476 c = 'S'; break;
477 case BuiltinType::Int:
478 c = 'I'; break;
479 case BuiltinType::Long:
480 c = 'L'; break;
481 case BuiltinType::LongLong:
482 c = 'K'; break;
483 case BuiltinType::Int128:
484 c = 'J'; break;
485 case BuiltinType::Float:
486 c = 'f'; break;
487 case BuiltinType::Double:
488 c = 'd'; break;
489 case BuiltinType::LongDouble:
490 c = 'D'; break;
491 case BuiltinType::NullPtr:
492 c = 'n'; break;
493 case BuiltinType::Overload:
494 case BuiltinType::Dependent:
495 case BuiltinType::UndeducedAuto:
496 IgnoreResults = true;
497 return;
498 case BuiltinType::ObjCId:
499 c = 'o'; break;
500 case BuiltinType::ObjCClass:
501 c = 'O'; break;
502 case BuiltinType::ObjCSel:
503 c = 'e'; break;
504 }
505 Out << c;
506 return;
507 }
508 if (const ComplexType *CT = T->getAs<ComplexType>()) {
509 Out << '<';
510 T = CT->getElementType();
511 continue;
512 }
Ted Kremenek2ea5baf2010-05-07 20:39:40 +0000513 if (const TagType *TT = T->getAs<TagType>()) {
514 Out << '$';
515 VisitTagDecl(TT->getDecl());
516 return;
517 }
Ted Kremenek8e672192010-05-07 01:04:32 +0000518
519 // Unhandled type.
520 Out << ' ';
521 break;
522 } while (true);
523}
524
Ted Kremenek896b70f2010-03-13 02:50:34 +0000525//===----------------------------------------------------------------------===//
526// General purpose USR generation methods.
527//===----------------------------------------------------------------------===//
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000528
Ted Kremenek896b70f2010-03-13 02:50:34 +0000529void USRGenerator::GenObjCClass(llvm::StringRef cls) {
530 Out << "objc(cs)" << cls;
531}
532
533void USRGenerator::GenObjCCategory(llvm::StringRef cls, llvm::StringRef cat) {
Ted Kremeneke74ef122010-04-16 21:31:52 +0000534 Out << "objc(cy)" << cls << '@' << cat;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000535}
536
537void USRGenerator::GenObjCIvar(llvm::StringRef ivar) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000538 Out << '@' << ivar;
Ted Kremenek896b70f2010-03-13 02:50:34 +0000539}
540
541void USRGenerator::GenObjCMethod(llvm::StringRef meth, bool isInstanceMethod) {
542 Out << (isInstanceMethod ? "(im)" : "(cm)") << meth;
543}
544
545void USRGenerator::GenObjCProperty(llvm::StringRef prop) {
546 Out << "(py)" << prop;
547}
548
549void USRGenerator::GenObjCProtocol(llvm::StringRef prot) {
550 Out << "objc(pl)" << prot;
551}
552
553//===----------------------------------------------------------------------===//
554// API hooks.
555//===----------------------------------------------------------------------===//
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000556
Benjamin Kramercfb51b62010-04-08 15:54:07 +0000557static inline llvm::StringRef extractUSRSuffix(llvm::StringRef s) {
558 return s.startswith("c:") ? s.substr(2) : "";
559}
560
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000561static CXString getDeclCursorUSR(const CXCursor &C) {
Ted Kremenek896b70f2010-03-13 02:50:34 +0000562 Decl *D = cxcursor::getCursorDecl(C);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000563
564 // Don't generate USRs for things with invalid locations.
565 if (!D || D->getLocStart().isInvalid())
Ted Kremenek1af0a2a2010-04-17 00:21:38 +0000566 return createCXString("");
Ted Kremenek896b70f2010-03-13 02:50:34 +0000567
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000568 // Check if the cursor has 'NoLinkage'.
Ted Kremeneke74ef122010-04-16 21:31:52 +0000569 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000570 switch (ND->getLinkage()) {
571 case ExternalLinkage:
572 // Generate USRs for all entities with external linkage.
573 break;
574 case NoLinkage:
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000575 case UniqueExternalLinkage:
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000576 // We allow enums, typedefs, and structs that have no linkage to
577 // have USRs that are anchored to the file they were defined in
578 // (e.g., the header). This is a little gross, but in principal
579 // enums/anonymous structs/etc. defined in a common header file
580 // are referred to across multiple translation units.
Ted Kremenek6f153952010-04-15 21:51:13 +0000581 if (isa<TagDecl>(ND) || isa<TypedefDecl>(ND) ||
Ted Kremenekcf999102010-04-29 17:43:29 +0000582 isa<EnumConstantDecl>(ND) || isa<FieldDecl>(ND) ||
Ted Kremenekcbd66f02010-05-06 23:38:28 +0000583 isa<VarDecl>(ND) || isa<NamespaceDecl>(ND))
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000584 break;
585 // Fall-through.
586 case InternalLinkage:
Ted Kremenekcf999102010-04-29 17:43:29 +0000587 if (isa<FunctionDecl>(ND))
588 break;
Ted Kremenek1865cfe2010-04-15 21:04:25 +0000589 }
590
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000591 USRGenerator UG(&C);
592 UG->Visit(D);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000593
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000594 if (UG->ignoreResults())
Ted Kremenekebfa3392010-03-19 20:39:03 +0000595 return createCXString("");
Ted Kremenek896b70f2010-03-13 02:50:34 +0000596
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000597#if 0
Ted Kremeneke542f772010-04-20 23:15:40 +0000598 // For development testing.
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000599 assert(UG.str().size() > 2);
600#endif
Ted Kremeneke542f772010-04-20 23:15:40 +0000601
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000602 // Return a copy of the string that must be disposed by the caller.
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000603 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000604}
605
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000606extern "C" {
607
608CXString clang_getCursorUSR(CXCursor C) {
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000609 const CXCursorKind &K = clang_getCursorKind(C);
610
611 if (clang_isDeclaration(K))
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000612 return getDeclCursorUSR(C);
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000613
614 if (K == CXCursor_MacroDefinition) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000615 USRGenerator UG(&C);
616 UG << "macro@"
617 << cxcursor::getCursorMacroDefinition(C)->getName()->getNameStart();
618 return createCXString(UG.str(), true);
Ted Kremenekfa8231d2010-04-11 22:20:34 +0000619 }
620
Ted Kremenekc3ef91d2010-04-11 22:20:26 +0000621 return createCXString("");
622}
623
Ted Kremenek896b70f2010-03-13 02:50:34 +0000624CXString clang_constructUSR_ObjCIvar(const char *name, CXString classUSR) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000625 USRGenerator UG;
626 UG << extractUSRSuffix(clang_getCString(classUSR));
627 UG->GenObjCIvar(name);
628 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000629}
630
631CXString clang_constructUSR_ObjCMethod(const char *name,
632 unsigned isInstanceMethod,
633 CXString classUSR) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000634 USRGenerator UG;
635 UG << extractUSRSuffix(clang_getCString(classUSR));
636 UG->GenObjCMethod(name, isInstanceMethod);
637 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000638}
639
640CXString clang_constructUSR_ObjCClass(const char *name) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000641 USRGenerator UG;
642 UG->GenObjCClass(name);
643 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000644}
645
646CXString clang_constructUSR_ObjCProtocol(const char *name) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000647 USRGenerator UG;
648 UG->GenObjCProtocol(name);
649 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000650}
651
Ted Kremenek66ccaec2010-03-15 17:38:58 +0000652CXString clang_constructUSR_ObjCCategory(const char *class_name,
Ted Kremenek0c0fb412010-03-25 02:00:36 +0000653 const char *category_name) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000654 USRGenerator UG;
655 UG->GenObjCCategory(class_name, category_name);
656 return createCXString(UG.str(), true);
Ted Kremenek896b70f2010-03-13 02:50:34 +0000657}
658
659CXString clang_constructUSR_ObjCProperty(const char *property,
660 CXString classUSR) {
Ted Kremenek3ebd8dc2010-05-07 20:07:23 +0000661 USRGenerator UG;
662 UG << extractUSRSuffix(clang_getCString(classUSR));
663 UG->GenObjCProperty(property);
664 return createCXString(UG.str(), true);
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000665}
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000666
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000667} // end extern "C"