blob: 70006bcf3a02c1fd580fa0c6fe10e31c68a58ba1 [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"
Benjamin Kramer9895c6a2010-01-12 11:32:40 +000015#include "clang/AST/DeclVisitor.h"
Ted Kremenek87763822010-01-12 02:07:58 +000016#include "llvm/ADT/SmallString.h"
Benjamin Kramer9895c6a2010-01-12 11:32:40 +000017#include "llvm/Support/raw_ostream.h"
Ted Kremenek1b6869a2010-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 Kremenek31723832010-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 Kremenek1b6869a2010-01-05 22:06:45 +000041}
42
Ted Kremenek31723832010-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 Kramer9895c6a2010-01-12 11:32:40 +000064
65extern "C" {
66
Ted Kremenek31723832010-01-11 23:56:39 +000067/// clang_getDeclaration() maps from a CXEntity to the matching CXDecl (if any)
68/// in a specified translation unit.
69CXDecl clang_getDeclaration(CXEntity CE, CXTranslationUnit TU) {
70 return (CXDecl) GetEntity(CE).getDecl(GetASTContext(TU));
71}
72
73
74CXEntity clang_getEntityFromDecl(CXIndex CIdx, CXDecl CE) {
75 if (Decl *D = (Decl *) CE)
76 return MakeEntity(CIdx, Entity::get(D, GetProgram(CIdx)));
77 return NullCXEntity();
78}
79
Ted Kremenek87763822010-01-12 02:07:58 +000080//===----------------------------------------------------------------------===//
81// USR generation.
82//===----------------------------------------------------------------------===//
83
84namespace {
85class USRGenerator : public DeclVisitor<USRGenerator> {
86 llvm::raw_ostream &Out;
87public:
88 USRGenerator(llvm::raw_ostream &out) : Out(out) {}
89
Ted Kremeneke9cde112010-01-12 19:35:53 +000090 void VisitNamedDecl(NamedDecl *D);
Ted Kremenek87763822010-01-12 02:07:58 +000091 void VisitObjCContainerDecl(ObjCContainerDecl *CD);
92 void VisitObjCMethodDecl(ObjCMethodDecl *MD);
93 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
94};
95} // end anonymous namespace
96
Ted Kremeneke9cde112010-01-12 19:35:53 +000097
98void USRGenerator::VisitNamedDecl(NamedDecl *D) {
99 DeclContext *DC = D->getDeclContext();
100 if (NamedDecl *DCN = dyn_cast<NamedDecl>(DC)) {
101 Visit(DCN);
102 Out << '_';
103 }
104 else {
105 Out << '_';
106 }
107 Out << D->getName();
108}
109
Ted Kremenek87763822010-01-12 02:07:58 +0000110void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) {
111 Visit(cast<Decl>(D->getDeclContext()));
Ted Kremeneke9cde112010-01-12 19:35:53 +0000112 Out << (D->isInstanceMethod() ? "(im)" : "(cm)");
Ted Kremenek87763822010-01-12 02:07:58 +0000113 Out << DeclarationName(D->getSelector());
114}
115
116void USRGenerator::VisitObjCContainerDecl(ObjCContainerDecl *D) {
117 switch (D->getKind()) {
118 default:
119 assert(false && "Invalid ObjC container.");
120 case Decl::ObjCInterface:
121 case Decl::ObjCImplementation:
Ted Kremeneke9cde112010-01-12 19:35:53 +0000122 Out << "objc(cs)" << D->getName();
Ted Kremenek87763822010-01-12 02:07:58 +0000123 break;
124 case Decl::ObjCCategory: {
125 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
Ted Kremeneke9cde112010-01-12 19:35:53 +0000126 Out << "objc(cy)" << CD->getClassInterface()->getName()
Ted Kremenek87763822010-01-12 02:07:58 +0000127 << '_' << CD->getName();
128 break;
129 }
130 case Decl::ObjCCategoryImpl: {
131 ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
Ted Kremeneke9cde112010-01-12 19:35:53 +0000132 Out << "objc(cy)" << CD->getClassInterface()->getName()
Ted Kremenek87763822010-01-12 02:07:58 +0000133 << '_' << CD->getName();
134 break;
135 }
136 case Decl::ObjCProtocol:
Ted Kremeneke9cde112010-01-12 19:35:53 +0000137 Out << "objc(pl)" << cast<ObjCProtocolDecl>(D)->getName();
Ted Kremenek87763822010-01-12 02:07:58 +0000138 break;
139 }
140}
141
142void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
143 Visit(cast<Decl>(D->getDeclContext()));
Ted Kremeneke9cde112010-01-12 19:35:53 +0000144 Out << "(py)" << D->getName();
Ted Kremenek87763822010-01-12 02:07:58 +0000145}
146
147// FIXME: This is a skeleton implementation. It will be overhauled.
148CXString clang_getUSR(CXEntity CE) {
149 const Entity &E = GetEntity(CE);
150
151 // FIXME: Support cross-translation unit CXEntities.
152 if (!E.isInternalToTU())
153 return CIndexer::createCXString(NULL);
154
155 Decl *D = E.getInternalDecl();
156 if (!D)
157 return CIndexer::createCXString(NULL);
158
159 llvm::SmallString<1024> StrBuf;
160 {
161 llvm::raw_svector_ostream Out(StrBuf);
162 USRGenerator UG(Out);
163 UG.Visit(D);
164 }
165
166 if (StrBuf.empty())
167 return CIndexer::createCXString(NULL);
168
169 // Return a copy of the string that must be disposed by the caller.
170 return CIndexer::createCXString(StrBuf.c_str(), true);
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000171}
172
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000173} // end extern "C"