blob: f9e1c23f7ae1e3a374b69399cb266f262b25c407 [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();
132 if (s.empty())
133 Out << "^anon";
134 else
135 Out << s;
136}
137
138void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) {
139 Visit(cast<Decl>(D->getDeclContext()));
140 Out << (D->isInstanceMethod() ? "(im)" : "(cm)");
141 Out << DeclarationName(D->getSelector()).getAsString();
142}
143
144void USRGenerator::VisitObjCContainerDecl(ObjCContainerDecl *D) {
145 switch (D->getKind()) {
146 default:
147 assert(false && "Invalid ObjC container.");
148 case Decl::ObjCInterface:
149 case Decl::ObjCImplementation:
150 Out << "objc(cs)" << D->getName();
151 break;
152 case Decl::ObjCCategory: {
153 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
154 Out << "objc(cy)" << CD->getClassInterface()->getName()
155 << '_' << CD->getName();
156 break;
157 }
158 case Decl::ObjCCategoryImpl: {
159 ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
160 Out << "objc(cy)" << CD->getClassInterface()->getName()
161 << '_' << CD->getName();
162 break;
163 }
164 case Decl::ObjCProtocol:
165 Out << "objc(pl)" << cast<ObjCProtocolDecl>(D)->getName();
166 break;
167 }
168}
169
170void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
171 Visit(cast<Decl>(D->getDeclContext()));
172 Out << "(py)" << D->getName();
173}
174
175void USRGenerator::VisitTypedefDecl(TypedefDecl *D) {
176 DeclContext *DC = D->getDeclContext();
177 if (NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
178 Visit(DCN);
179 Out << "typedef@" << D->getName();
180}
181
Benjamin Kramer59617be2010-01-12 11:32:40 +0000182extern "C" {
183
Ted Kremeneke5f86be2010-01-11 23:56:39 +0000184/// clang_getDeclaration() maps from a CXEntity to the matching CXDecl (if any)
185/// in a specified translation unit.
186CXDecl clang_getDeclaration(CXEntity CE, CXTranslationUnit TU) {
187 return (CXDecl) GetEntity(CE).getDecl(GetASTContext(TU));
188}
189
190
191CXEntity clang_getEntityFromDecl(CXIndex CIdx, CXDecl CE) {
192 if (Decl *D = (Decl *) CE)
193 return MakeEntity(CIdx, Entity::get(D, GetProgram(CIdx)));
194 return NullCXEntity();
195}
Ted Kremenekcb674f92010-01-12 23:33:42 +0000196
Ted Kremenek5631d2d2010-01-12 02:07:58 +0000197// FIXME: This is a skeleton implementation. It will be overhauled.
Ted Kremenekcb674f92010-01-12 23:33:42 +0000198CXString clang_getDeclUSR(CXDecl D) {
199 assert(D && "Null CXDecl passed to clang_getDeclUSR()");
Ted Kremenek5631d2d2010-01-12 02:07:58 +0000200 llvm::SmallString<1024> StrBuf;
201 {
202 llvm::raw_svector_ostream Out(StrBuf);
203 USRGenerator UG(Out);
Ted Kremenekcb674f92010-01-12 23:33:42 +0000204 UG.Visit(static_cast<Decl*>(D));
Ted Kremenek5631d2d2010-01-12 02:07:58 +0000205 }
206
207 if (StrBuf.empty())
208 return CIndexer::createCXString(NULL);
209
210 // Return a copy of the string that must be disposed by the caller.
211 return CIndexer::createCXString(StrBuf.c_str(), true);
Ted Kremenekcb674f92010-01-12 23:33:42 +0000212}
Ted Kremenek9cd9f6d2010-01-05 22:06:45 +0000213
Ted Kremenek9cd9f6d2010-01-05 22:06:45 +0000214} // end extern "C"