blob: 5766b21d86747984ba08adac2de4f32abb839520 [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
90 void VisitObjCContainerDecl(ObjCContainerDecl *CD);
91 void VisitObjCMethodDecl(ObjCMethodDecl *MD);
92 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
93};
94} // end anonymous namespace
95
96void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) {
97 Visit(cast<Decl>(D->getDeclContext()));
98 Out << (D->isInstanceMethod() ? "_IM_" : "_CM_");
99 Out << DeclarationName(D->getSelector());
100}
101
102void USRGenerator::VisitObjCContainerDecl(ObjCContainerDecl *D) {
103 switch (D->getKind()) {
104 default:
105 assert(false && "Invalid ObjC container.");
106 case Decl::ObjCInterface:
107 case Decl::ObjCImplementation:
108 Out << "objc_class_" << D->getName();
109 break;
110 case Decl::ObjCCategory: {
111 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
112 Out << "objc_cat_" << CD->getClassInterface()->getName()
113 << '_' << CD->getName();
114 break;
115 }
116 case Decl::ObjCCategoryImpl: {
117 ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
118 Out << "objc_cat_" << CD->getClassInterface()->getName()
119 << '_' << CD->getName();
120 break;
121 }
122 case Decl::ObjCProtocol:
123 Out << "objc_prot_" << cast<ObjCProtocolDecl>(D)->getName();
124 break;
125 }
126}
127
128void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
129 Visit(cast<Decl>(D->getDeclContext()));
130 Out << "_prop_" << D->getName();
131}
132
133// FIXME: This is a skeleton implementation. It will be overhauled.
134CXString clang_getUSR(CXEntity CE) {
135 const Entity &E = GetEntity(CE);
136
137 // FIXME: Support cross-translation unit CXEntities.
138 if (!E.isInternalToTU())
139 return CIndexer::createCXString(NULL);
140
141 Decl *D = E.getInternalDecl();
142 if (!D)
143 return CIndexer::createCXString(NULL);
144
145 llvm::SmallString<1024> StrBuf;
146 {
147 llvm::raw_svector_ostream Out(StrBuf);
148 USRGenerator UG(Out);
149 UG.Visit(D);
150 }
151
152 if (StrBuf.empty())
153 return CIndexer::createCXString(NULL);
154
155 // Return a copy of the string that must be disposed by the caller.
156 return CIndexer::createCXString(StrBuf.c_str(), true);
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000157}
158
Ted Kremenek1b6869a2010-01-05 22:06:45 +0000159} // end extern "C"