aboutsummaryrefslogtreecommitdiff
path: root/tools/CIndex/CIndexUSRs.cpp
blob: edf3406356918327836df53e2e2a76ff431ce5b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//===- CIndexUSR.cpp - Clang-C Source Indexing Library --------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the generation and use of USRs from CXEntities.
//
//===----------------------------------------------------------------------===//

#include "CIndexer.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/raw_ostream.h";
#include "clang/AST/DeclVisitor.h";

extern "C" {

// Some notes on CXEntity:
//
// - Since the 'ordinary' namespace includes functions, data, typedefs,
// ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one
// entity for 2 different types). For example:
//
// module1.m: @interface Foo @end Foo *x;
// module2.m: void Foo(int);
//
// - Since the unique name spans translation units, static data/functions
// within a CXTranslationUnit are *not* currently represented by entities.
// As a result, there will be no entity for the following:
//
// module.m: static void Foo() { }
//
  
static inline Entity GetEntity(const CXEntity &E) {
  return Entity::getFromOpaquePtr(E.data);
}
  
static inline ASTUnit *GetTranslationUnit(CXTranslationUnit TU) {
  return (ASTUnit*) TU;
}

static inline ASTContext &GetASTContext(CXTranslationUnit TU) {
  return GetTranslationUnit(TU)->getASTContext();
}

static inline CXEntity NullCXEntity() {
  CXEntity CE;
  CE.index = NULL;
  CE.data = NULL;
  return CE;
}
  
static inline CXEntity MakeEntity(CXIndex CIdx, const Entity &E) {
  CXEntity CE;
  CE.index = CIdx;
  CE.data = E.getAsOpaquePtr();
  return CE;
}

static inline Program &GetProgram(CXIndex CIdx) {
  return ((CIndexer*) CIdx)->getProgram();
}
 
/// clang_getDeclaration() maps from a CXEntity to the matching CXDecl (if any)
///  in a specified translation unit.
CXDecl clang_getDeclaration(CXEntity CE, CXTranslationUnit TU) {
  return (CXDecl) GetEntity(CE).getDecl(GetASTContext(TU));
}

  
CXEntity clang_getEntityFromDecl(CXIndex CIdx, CXDecl CE) {
  if (Decl *D = (Decl *) CE)
    return MakeEntity(CIdx, Entity::get(D, GetProgram(CIdx)));
  return NullCXEntity();
}
  
//===----------------------------------------------------------------------===//
// USR generation.
//===----------------------------------------------------------------------===//

namespace {
class USRGenerator : public DeclVisitor<USRGenerator> {
  llvm::raw_ostream &Out;
public:
  USRGenerator(llvm::raw_ostream &out) : Out(out) {}

  void VisitObjCContainerDecl(ObjCContainerDecl *CD);  
  void VisitObjCMethodDecl(ObjCMethodDecl *MD);
  void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
};
} // end anonymous namespace

void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) {
  Visit(cast<Decl>(D->getDeclContext()));
  Out << (D->isInstanceMethod() ? "_IM_" : "_CM_");
  Out << DeclarationName(D->getSelector());
}
  
void USRGenerator::VisitObjCContainerDecl(ObjCContainerDecl *D) {
  switch (D->getKind()) {
    default:
      assert(false && "Invalid ObjC container.");
    case Decl::ObjCInterface:
    case Decl::ObjCImplementation:
      Out << "objc_class_" << D->getName();
      break;
    case Decl::ObjCCategory: {
      ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
      Out << "objc_cat_" << CD->getClassInterface()->getName()
          << '_' << CD->getName();
      break;
    }
    case Decl::ObjCCategoryImpl: {
      ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
      Out << "objc_cat_" << CD->getClassInterface()->getName()
          << '_' << CD->getName();
      break;
    }
    case Decl::ObjCProtocol:
      Out << "objc_prot_" << cast<ObjCProtocolDecl>(D)->getName();
      break;
  }
}
  
void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
  Visit(cast<Decl>(D->getDeclContext()));
  Out << "_prop_" << D->getName();
}
  
// FIXME: This is a skeleton implementation.  It will be overhauled.
CXString clang_getUSR(CXEntity CE) {
  const Entity &E = GetEntity(CE);
  
  // FIXME: Support cross-translation unit CXEntities.  
  if (!E.isInternalToTU())
    return CIndexer::createCXString(NULL);
  
  Decl *D = E.getInternalDecl();
  if (!D)
    return CIndexer::createCXString(NULL);

  llvm::SmallString<1024> StrBuf;
  {
    llvm::raw_svector_ostream Out(StrBuf);
    USRGenerator UG(Out);
    UG.Visit(D);
  }
  
  if (StrBuf.empty())
    return CIndexer::createCXString(NULL);

  // Return a copy of the string that must be disposed by the caller.
  return CIndexer::createCXString(StrBuf.c_str(), true);
}

} // end extern "C"