blob: b52b9da92124d341fbdb861d8da266e33ddd5d07 [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);
Ted Kremenek6b1d5502010-01-15 23:34:31 +000085 void VisitTagDeclCommon(TagDecl *D);
Ted Kremeneke1b55252010-01-14 01:50:21 +000086 void VisitTypedefDecl(TypedefDecl *D);
87};
Ted Kremenekcb674f92010-01-12 23:33:42 +000088} // end anonymous namespace
89
Ted Kremeneke1b55252010-01-14 01:50:21 +000090void USRGenerator::VisitBlockDecl(BlockDecl *D) {
91 VisitDeclContext(D->getDeclContext());
92 // FIXME: Better support for anonymous blocks.
93 Out << "@B^anon";
94}
95
96void USRGenerator::VisitDeclContext(DeclContext *DC) {
97 if (NamedDecl *D = dyn_cast<NamedDecl>(DC))
98 Visit(D);
99}
100
Ted Kremenek8433d1d2010-01-15 20:04:31 +0000101void USRGenerator::VisitEnumDecl(EnumDecl *D) {
102 VisitDeclContext(D->getDeclContext());
103 Out << "@E^";
Ted Kremenek6b1d5502010-01-15 23:34:31 +0000104 VisitTagDeclCommon(D);
Ted Kremenek8433d1d2010-01-15 20:04:31 +0000105}
106
Ted Kremeneke1b55252010-01-14 01:50:21 +0000107void USRGenerator::VisitFunctionDecl(FunctionDecl *D) {
108 VisitDeclContext(D->getDeclContext());
109 Out << "@F^" << D->getNameAsString();
110}
Ted Kremenekcb674f92010-01-12 23:33:42 +0000111
112void USRGenerator::VisitNamedDecl(NamedDecl *D) {
Ted Kremeneke1b55252010-01-14 01:50:21 +0000113 VisitDeclContext(D->getDeclContext());
Ted Kremenekcb674f92010-01-12 23:33:42 +0000114 const std::string &s = D->getNameAsString();
115 assert(!s.empty());
Ted Kremeneke1b55252010-01-14 01:50:21 +0000116 Out << "@^" << s;
117}
118
119void USRGenerator::VisitNamespaceDecl(NamespaceDecl *D) {
120 VisitDeclContext(D->getDeclContext());
121 Out << "@N^" << D->getNameAsString();
Ted Kremenekcb674f92010-01-12 23:33:42 +0000122}
123
124void USRGenerator::VisitRecordDecl(RecordDecl *D) {
Ted Kremeneke1b55252010-01-14 01:50:21 +0000125 VisitDeclContext(D->getDeclContext());
126 Out << "@S^";
Ted Kremenek6b1d5502010-01-15 23:34:31 +0000127 VisitTagDeclCommon(D);
Ted Kremenekcb674f92010-01-12 23:33:42 +0000128}
129
130void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) {
131 Visit(cast<Decl>(D->getDeclContext()));
132 Out << (D->isInstanceMethod() ? "(im)" : "(cm)");
133 Out << DeclarationName(D->getSelector()).getAsString();
134}
135
136void USRGenerator::VisitObjCContainerDecl(ObjCContainerDecl *D) {
137 switch (D->getKind()) {
138 default:
139 assert(false && "Invalid ObjC container.");
140 case Decl::ObjCInterface:
141 case Decl::ObjCImplementation:
142 Out << "objc(cs)" << D->getName();
143 break;
144 case Decl::ObjCCategory: {
145 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
146 Out << "objc(cy)" << CD->getClassInterface()->getName()
147 << '_' << CD->getName();
148 break;
149 }
150 case Decl::ObjCCategoryImpl: {
151 ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
152 Out << "objc(cy)" << CD->getClassInterface()->getName()
153 << '_' << CD->getName();
154 break;
155 }
156 case Decl::ObjCProtocol:
157 Out << "objc(pl)" << cast<ObjCProtocolDecl>(D)->getName();
158 break;
159 }
160}
161
162void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
163 Visit(cast<Decl>(D->getDeclContext()));
164 Out << "(py)" << D->getName();
165}
166
Ted Kremenek6b1d5502010-01-15 23:34:31 +0000167void USRGenerator::VisitTagDeclCommon(TagDecl *D) {
168 // FIXME: Better support for anonymous structures and enums.
169 const std::string &s = D->getNameAsString();
170 if (s.empty()) {
171 if (TypedefDecl *TD = D->getTypedefForAnonDecl())
172 Out << "^anontd^" << TD->getNameAsString();
173 else
174 Out << "^anon";
175 }
176 else
177 Out << s;
178}
179
Ted Kremenekcb674f92010-01-12 23:33:42 +0000180void USRGenerator::VisitTypedefDecl(TypedefDecl *D) {
181 DeclContext *DC = D->getDeclContext();
182 if (NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
183 Visit(DCN);
184 Out << "typedef@" << D->getName();
185}
186
Benjamin Kramer59617be2010-01-12 11:32:40 +0000187extern "C" {
188
Ted Kremeneke5f86be2010-01-11 23:56:39 +0000189/// clang_getDeclaration() maps from a CXEntity to the matching CXDecl (if any)
190/// in a specified translation unit.
191CXDecl clang_getDeclaration(CXEntity CE, CXTranslationUnit TU) {
192 return (CXDecl) GetEntity(CE).getDecl(GetASTContext(TU));
193}
194
195
196CXEntity clang_getEntityFromDecl(CXIndex CIdx, CXDecl CE) {
197 if (Decl *D = (Decl *) CE)
198 return MakeEntity(CIdx, Entity::get(D, GetProgram(CIdx)));
199 return NullCXEntity();
200}
Ted Kremenekcb674f92010-01-12 23:33:42 +0000201
Ted Kremenek5631d2d2010-01-12 02:07:58 +0000202// FIXME: This is a skeleton implementation. It will be overhauled.
Ted Kremenekcb674f92010-01-12 23:33:42 +0000203CXString clang_getDeclUSR(CXDecl D) {
204 assert(D && "Null CXDecl passed to clang_getDeclUSR()");
Ted Kremenek5631d2d2010-01-12 02:07:58 +0000205 llvm::SmallString<1024> StrBuf;
206 {
207 llvm::raw_svector_ostream Out(StrBuf);
208 USRGenerator UG(Out);
Ted Kremenekcb674f92010-01-12 23:33:42 +0000209 UG.Visit(static_cast<Decl*>(D));
Ted Kremenek5631d2d2010-01-12 02:07:58 +0000210 }
211
212 if (StrBuf.empty())
213 return CIndexer::createCXString(NULL);
214
215 // Return a copy of the string that must be disposed by the caller.
216 return CIndexer::createCXString(StrBuf.c_str(), true);
Ted Kremenekcb674f92010-01-12 23:33:42 +0000217}
Ted Kremenek9cd9f6d2010-01-05 22:06:45 +0000218
Ted Kremenek9cd9f6d2010-01-05 22:06:45 +0000219} // end extern "C"