blob: 549c65058d8606f9d5f0e7686773057ad1431cfb [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);
77 void VisitFunctionDecl(FunctionDecl *D);
78 void VisitNamedDecl(NamedDecl *D);
79 void VisitNamespaceDecl(NamespaceDecl *D);
80 void VisitObjCContainerDecl(ObjCContainerDecl *CD);
81 void VisitObjCMethodDecl(ObjCMethodDecl *MD);
82 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
83 void VisitRecordDecl(RecordDecl *D);
84 void VisitTypedefDecl(TypedefDecl *D);
85};
Ted Kremenekcb674f92010-01-12 23:33:42 +000086} // end anonymous namespace
87
Ted Kremeneke1b55252010-01-14 01:50:21 +000088void USRGenerator::VisitBlockDecl(BlockDecl *D) {
89 VisitDeclContext(D->getDeclContext());
90 // FIXME: Better support for anonymous blocks.
91 Out << "@B^anon";
92}
93
94void USRGenerator::VisitDeclContext(DeclContext *DC) {
95 if (NamedDecl *D = dyn_cast<NamedDecl>(DC))
96 Visit(D);
97}
98
99void USRGenerator::VisitFunctionDecl(FunctionDecl *D) {
100 VisitDeclContext(D->getDeclContext());
101 Out << "@F^" << D->getNameAsString();
102}
Ted Kremenekcb674f92010-01-12 23:33:42 +0000103
104void USRGenerator::VisitNamedDecl(NamedDecl *D) {
Ted Kremeneke1b55252010-01-14 01:50:21 +0000105 VisitDeclContext(D->getDeclContext());
Ted Kremenekcb674f92010-01-12 23:33:42 +0000106 const std::string &s = D->getNameAsString();
107 assert(!s.empty());
Ted Kremeneke1b55252010-01-14 01:50:21 +0000108 Out << "@^" << s;
109}
110
111void USRGenerator::VisitNamespaceDecl(NamespaceDecl *D) {
112 VisitDeclContext(D->getDeclContext());
113 Out << "@N^" << D->getNameAsString();
Ted Kremenekcb674f92010-01-12 23:33:42 +0000114}
115
116void USRGenerator::VisitRecordDecl(RecordDecl *D) {
Ted Kremeneke1b55252010-01-14 01:50:21 +0000117 VisitDeclContext(D->getDeclContext());
118 Out << "@S^";
119 // FIXME: Better support for anonymous structures.
Ted Kremenekcb674f92010-01-12 23:33:42 +0000120 const std::string &s = D->getNameAsString();
121 if (s.empty())
122 Out << "^anon";
123 else
124 Out << s;
125}
126
127void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) {
128 Visit(cast<Decl>(D->getDeclContext()));
129 Out << (D->isInstanceMethod() ? "(im)" : "(cm)");
130 Out << DeclarationName(D->getSelector()).getAsString();
131}
132
133void USRGenerator::VisitObjCContainerDecl(ObjCContainerDecl *D) {
134 switch (D->getKind()) {
135 default:
136 assert(false && "Invalid ObjC container.");
137 case Decl::ObjCInterface:
138 case Decl::ObjCImplementation:
139 Out << "objc(cs)" << D->getName();
140 break;
141 case Decl::ObjCCategory: {
142 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
143 Out << "objc(cy)" << CD->getClassInterface()->getName()
144 << '_' << CD->getName();
145 break;
146 }
147 case Decl::ObjCCategoryImpl: {
148 ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
149 Out << "objc(cy)" << CD->getClassInterface()->getName()
150 << '_' << CD->getName();
151 break;
152 }
153 case Decl::ObjCProtocol:
154 Out << "objc(pl)" << cast<ObjCProtocolDecl>(D)->getName();
155 break;
156 }
157}
158
159void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
160 Visit(cast<Decl>(D->getDeclContext()));
161 Out << "(py)" << D->getName();
162}
163
164void USRGenerator::VisitTypedefDecl(TypedefDecl *D) {
165 DeclContext *DC = D->getDeclContext();
166 if (NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
167 Visit(DCN);
168 Out << "typedef@" << D->getName();
169}
170
Benjamin Kramer59617be2010-01-12 11:32:40 +0000171extern "C" {
172
Ted Kremeneke5f86be2010-01-11 23:56:39 +0000173/// clang_getDeclaration() maps from a CXEntity to the matching CXDecl (if any)
174/// in a specified translation unit.
175CXDecl clang_getDeclaration(CXEntity CE, CXTranslationUnit TU) {
176 return (CXDecl) GetEntity(CE).getDecl(GetASTContext(TU));
177}
178
179
180CXEntity clang_getEntityFromDecl(CXIndex CIdx, CXDecl CE) {
181 if (Decl *D = (Decl *) CE)
182 return MakeEntity(CIdx, Entity::get(D, GetProgram(CIdx)));
183 return NullCXEntity();
184}
Ted Kremenekcb674f92010-01-12 23:33:42 +0000185
Ted Kremenek5631d2d2010-01-12 02:07:58 +0000186// FIXME: This is a skeleton implementation. It will be overhauled.
Ted Kremenekcb674f92010-01-12 23:33:42 +0000187CXString clang_getDeclUSR(CXDecl D) {
188 assert(D && "Null CXDecl passed to clang_getDeclUSR()");
Ted Kremenek5631d2d2010-01-12 02:07:58 +0000189 llvm::SmallString<1024> StrBuf;
190 {
191 llvm::raw_svector_ostream Out(StrBuf);
192 USRGenerator UG(Out);
Ted Kremenekcb674f92010-01-12 23:33:42 +0000193 UG.Visit(static_cast<Decl*>(D));
Ted Kremenek5631d2d2010-01-12 02:07:58 +0000194 }
195
196 if (StrBuf.empty())
197 return CIndexer::createCXString(NULL);
198
199 // Return a copy of the string that must be disposed by the caller.
200 return CIndexer::createCXString(StrBuf.c_str(), true);
Ted Kremenekcb674f92010-01-12 23:33:42 +0000201}
Ted Kremenek9cd9f6d2010-01-05 22:06:45 +0000202
Ted Kremenek9cd9f6d2010-01-05 22:06:45 +0000203} // end extern "C"