blob: edf3406356918327836df53e2e2a76ff431ce5b3 [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"
Ted Kremenek5631d2d2010-01-12 02:07:58 +000015#include "llvm/ADT/SmallString.h"
16#include "llvm/Support/raw_ostream.h";
17#include "clang/AST/DeclVisitor.h";
Ted Kremenek9cd9f6d2010-01-05 22:06:45 +000018
19extern "C" {
20
21// Some notes on CXEntity:
22//
23// - Since the 'ordinary' namespace includes functions, data, typedefs,
24// ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one
25// entity for 2 different types). For example:
26//
27// module1.m: @interface Foo @end Foo *x;
28// module2.m: void Foo(int);
29//
30// - Since the unique name spans translation units, static data/functions
31// within a CXTranslationUnit are *not* currently represented by entities.
32// As a result, there will be no entity for the following:
33//
34// module.m: static void Foo() { }
35//
Ted Kremeneke5f86be2010-01-11 23:56:39 +000036
37static inline Entity GetEntity(const CXEntity &E) {
38 return Entity::getFromOpaquePtr(E.data);
39}
40
41static inline ASTUnit *GetTranslationUnit(CXTranslationUnit TU) {
42 return (ASTUnit*) TU;
Ted Kremenek9cd9f6d2010-01-05 22:06:45 +000043}
44
Ted Kremeneke5f86be2010-01-11 23:56:39 +000045static inline ASTContext &GetASTContext(CXTranslationUnit TU) {
46 return GetTranslationUnit(TU)->getASTContext();
47}
48
49static inline CXEntity NullCXEntity() {
50 CXEntity CE;
51 CE.index = NULL;
52 CE.data = NULL;
53 return CE;
54}
55
56static inline CXEntity MakeEntity(CXIndex CIdx, const Entity &E) {
57 CXEntity CE;
58 CE.index = CIdx;
59 CE.data = E.getAsOpaquePtr();
60 return CE;
61}
62
63static inline Program &GetProgram(CXIndex CIdx) {
64 return ((CIndexer*) CIdx)->getProgram();
65}
66
67/// 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 Kremenek5631d2d2010-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 Kremenek9cd9f6d2010-01-05 22:06:45 +0000157}
158
Ted Kremenek9cd9f6d2010-01-05 22:06:45 +0000159} // end extern "C"