blob: 12157f4b1d204dbb913bce8d92ac1494964e684a [file] [log] [blame]
Ted Kremenek9cd9f6d2010-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"
Benjamin Kramer59617be2010-01-12 11:32:40 +000015#include "clang/AST/DeclVisitor.h"
Ted Kremenek5631d2d2010-01-12 02:07:58 +000016#include "llvm/ADT/SmallString.h"
Benjamin Kramer59617be2010-01-12 11:32:40 +000017#include "llvm/Support/raw_ostream.h"
Ted Kremenek9cd9f6d2010-01-05 22:06:45 +000018
19// Some notes on CXEntity:
20//
21// - Since the 'ordinary' namespace includes functions, data, typedefs,
22// ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one
23// entity for 2 different types). For example:
24//
25// module1.m: @interface Foo @end Foo *x;
26// module2.m: void Foo(int);
27//
28// - Since the unique name spans translation units, static data/functions
29// within a CXTranslationUnit are *not* currently represented by entities.
30// As a result, there will be no entity for the following:
31//
32// module.m: static void Foo() { }
33//
Ted Kremeneke5f86be2010-01-11 23:56:39 +000034
35static inline Entity GetEntity(const CXEntity &E) {
36 return Entity::getFromOpaquePtr(E.data);
37}
38
39static inline ASTUnit *GetTranslationUnit(CXTranslationUnit TU) {
40 return (ASTUnit*) TU;
Ted Kremenek9cd9f6d2010-01-05 22:06:45 +000041}
42
Ted Kremeneke5f86be2010-01-11 23:56:39 +000043static inline ASTContext &GetASTContext(CXTranslationUnit TU) {
44 return GetTranslationUnit(TU)->getASTContext();
45}
46
47static inline CXEntity NullCXEntity() {
48 CXEntity CE;
49 CE.index = NULL;
50 CE.data = NULL;
51 return CE;
52}
53
54static inline CXEntity MakeEntity(CXIndex CIdx, const Entity &E) {
55 CXEntity CE;
56 CE.index = CIdx;
57 CE.data = E.getAsOpaquePtr();
58 return CE;
59}
60
61static inline Program &GetProgram(CXIndex CIdx) {
62 return ((CIndexer*) CIdx)->getProgram();
63}
Benjamin Kramer59617be2010-01-12 11:32:40 +000064
Ted Kremenekcb674f92010-01-12 23:33:42 +000065//===----------------------------------------------------------------------===//
66// USR generation.
67//===----------------------------------------------------------------------===//
68
69namespace {
Ted Kremeneke1b55252010-01-14 01:50:21 +000070class USRGenerator : public DeclVisitor<USRGenerator> {
71 llvm::raw_ostream &Out;
72public:
73 USRGenerator(llvm::raw_ostream &out) : Out(out) {}
74
75 void VisitBlockDecl(BlockDecl *D);
76 void VisitDeclContext(DeclContext *D);
Ted Kremenek8433d1d2010-01-15 20:04:31 +000077 void VisitEnumDecl(EnumDecl *D);
Ted Kremeneke1b55252010-01-14 01:50:21 +000078 void VisitFunctionDecl(FunctionDecl *D);
79 void VisitNamedDecl(NamedDecl *D);
80 void VisitNamespaceDecl(NamespaceDecl *D);
81 void VisitObjCContainerDecl(ObjCContainerDecl *CD);
82 void VisitObjCMethodDecl(ObjCMethodDecl *MD);
83 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
84 void VisitRecordDecl(RecordDecl *D);
85 void VisitTypedefDecl(TypedefDecl *D);
86};
Ted Kremenekcb674f92010-01-12 23:33:42 +000087} // end anonymous namespace
88
Ted Kremeneke1b55252010-01-14 01:50:21 +000089void USRGenerator::VisitBlockDecl(BlockDecl *D) {
90 VisitDeclContext(D->getDeclContext());
91 // FIXME: Better support for anonymous blocks.
92 Out << "@B^anon";
93}
94
95void USRGenerator::VisitDeclContext(DeclContext *DC) {
96 if (NamedDecl *D = dyn_cast<NamedDecl>(DC))
97 Visit(D);
98}
99
Ted Kremenek8433d1d2010-01-15 20:04:31 +0000100void USRGenerator::VisitEnumDecl(EnumDecl *D) {
101 VisitDeclContext(D->getDeclContext());
102 Out << "@E^";
103 const std::string &s = D->getNameAsString();
104 if (s.empty())
105 Out << "anon";
106 else
107 Out << s;
108}
109
Ted Kremeneke1b55252010-01-14 01:50:21 +0000110void USRGenerator::VisitFunctionDecl(FunctionDecl *D) {
111 VisitDeclContext(D->getDeclContext());
112 Out << "@F^" << D->getNameAsString();
113}
Ted Kremenekcb674f92010-01-12 23:33:42 +0000114
115void USRGenerator::VisitNamedDecl(NamedDecl *D) {
Ted Kremeneke1b55252010-01-14 01:50:21 +0000116 VisitDeclContext(D->getDeclContext());
Ted Kremenekcb674f92010-01-12 23:33:42 +0000117 const std::string &s = D->getNameAsString();
118 assert(!s.empty());
Ted Kremeneke1b55252010-01-14 01:50:21 +0000119 Out << "@^" << s;
120}
121
122void USRGenerator::VisitNamespaceDecl(NamespaceDecl *D) {
123 VisitDeclContext(D->getDeclContext());
124 Out << "@N^" << D->getNameAsString();
Ted Kremenekcb674f92010-01-12 23:33:42 +0000125}
126
127void USRGenerator::VisitRecordDecl(RecordDecl *D) {
Ted Kremeneke1b55252010-01-14 01:50:21 +0000128 VisitDeclContext(D->getDeclContext());
129 Out << "@S^";
130 // FIXME: Better support for anonymous structures.
Ted Kremenekcb674f92010-01-12 23:33:42 +0000131 const std::string &s = D->getNameAsString();
Ted Kremeneka7f7b172010-01-15 23:08:25 +0000132 if (s.empty()) {
133 if (TypedefDecl *TD = D->getTypedefForAnonDecl())
134 Out << "^anontd^" << TD->getNameAsString();
135 else
136 Out << "^anon";
137 }
Ted Kremenekcb674f92010-01-12 23:33:42 +0000138 else
139 Out << s;
140}
141
142void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) {
143 Visit(cast<Decl>(D->getDeclContext()));
144 Out << (D->isInstanceMethod() ? "(im)" : "(cm)");
145 Out << DeclarationName(D->getSelector()).getAsString();
146}
147
148void USRGenerator::VisitObjCContainerDecl(ObjCContainerDecl *D) {
149 switch (D->getKind()) {
150 default:
151 assert(false && "Invalid ObjC container.");
152 case Decl::ObjCInterface:
153 case Decl::ObjCImplementation:
154 Out << "objc(cs)" << D->getName();
155 break;
156 case Decl::ObjCCategory: {
157 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
158 Out << "objc(cy)" << CD->getClassInterface()->getName()
159 << '_' << CD->getName();
160 break;
161 }
162 case Decl::ObjCCategoryImpl: {
163 ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
164 Out << "objc(cy)" << CD->getClassInterface()->getName()
165 << '_' << CD->getName();
166 break;
167 }
168 case Decl::ObjCProtocol:
169 Out << "objc(pl)" << cast<ObjCProtocolDecl>(D)->getName();
170 break;
171 }
172}
173
174void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
175 Visit(cast<Decl>(D->getDeclContext()));
176 Out << "(py)" << D->getName();
177}
178
179void USRGenerator::VisitTypedefDecl(TypedefDecl *D) {
180 DeclContext *DC = D->getDeclContext();
181 if (NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
182 Visit(DCN);
183 Out << "typedef@" << D->getName();
184}
185
Benjamin Kramer59617be2010-01-12 11:32:40 +0000186extern "C" {
187
Ted Kremeneke5f86be2010-01-11 23:56:39 +0000188/// clang_getDeclaration() maps from a CXEntity to the matching CXDecl (if any)
189/// in a specified translation unit.
190CXDecl clang_getDeclaration(CXEntity CE, CXTranslationUnit TU) {
191 return (CXDecl) GetEntity(CE).getDecl(GetASTContext(TU));
192}
193
194
195CXEntity clang_getEntityFromDecl(CXIndex CIdx, CXDecl CE) {
196 if (Decl *D = (Decl *) CE)
197 return MakeEntity(CIdx, Entity::get(D, GetProgram(CIdx)));
198 return NullCXEntity();
199}
Ted Kremenekcb674f92010-01-12 23:33:42 +0000200
Ted Kremenek5631d2d2010-01-12 02:07:58 +0000201// FIXME: This is a skeleton implementation. It will be overhauled.
Ted Kremenekcb674f92010-01-12 23:33:42 +0000202CXString clang_getDeclUSR(CXDecl D) {
203 assert(D && "Null CXDecl passed to clang_getDeclUSR()");
Ted Kremenek5631d2d2010-01-12 02:07:58 +0000204 llvm::SmallString<1024> StrBuf;
205 {
206 llvm::raw_svector_ostream Out(StrBuf);
207 USRGenerator UG(Out);
Ted Kremenekcb674f92010-01-12 23:33:42 +0000208 UG.Visit(static_cast<Decl*>(D));
Ted Kremenek5631d2d2010-01-12 02:07:58 +0000209 }
210
211 if (StrBuf.empty())
212 return CIndexer::createCXString(NULL);
213
214 // Return a copy of the string that must be disposed by the caller.
215 return CIndexer::createCXString(StrBuf.c_str(), true);
Ted Kremenekcb674f92010-01-12 23:33:42 +0000216}
Ted Kremenek9cd9f6d2010-01-05 22:06:45 +0000217
Ted Kremenek9cd9f6d2010-01-05 22:06:45 +0000218} // end extern "C"