Ted Kremenek | 1b6869a | 2010-01-05 22:06:45 +0000 | [diff] [blame] | 1 | //===- 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 Kramer | 9895c6a | 2010-01-12 11:32:40 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclVisitor.h" |
Ted Kremenek | 8776382 | 2010-01-12 02:07:58 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallString.h" |
Benjamin Kramer | 9895c6a | 2010-01-12 11:32:40 +0000 | [diff] [blame] | 17 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | 1b6869a | 2010-01-05 22:06:45 +0000 | [diff] [blame] | 18 | |
| 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 Kremenek | 3172383 | 2010-01-11 23:56:39 +0000 | [diff] [blame] | 34 | |
| 35 | static inline Entity GetEntity(const CXEntity &E) { |
| 36 | return Entity::getFromOpaquePtr(E.data); |
| 37 | } |
| 38 | |
| 39 | static inline ASTUnit *GetTranslationUnit(CXTranslationUnit TU) { |
| 40 | return (ASTUnit*) TU; |
Ted Kremenek | 1b6869a | 2010-01-05 22:06:45 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Ted Kremenek | 3172383 | 2010-01-11 23:56:39 +0000 | [diff] [blame] | 43 | static inline ASTContext &GetASTContext(CXTranslationUnit TU) { |
| 44 | return GetTranslationUnit(TU)->getASTContext(); |
| 45 | } |
| 46 | |
| 47 | static inline CXEntity NullCXEntity() { |
| 48 | CXEntity CE; |
| 49 | CE.index = NULL; |
| 50 | CE.data = NULL; |
| 51 | return CE; |
| 52 | } |
| 53 | |
| 54 | static 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 | |
| 61 | static inline Program &GetProgram(CXIndex CIdx) { |
| 62 | return ((CIndexer*) CIdx)->getProgram(); |
| 63 | } |
Benjamin Kramer | 9895c6a | 2010-01-12 11:32:40 +0000 | [diff] [blame] | 64 | |
| 65 | extern "C" { |
| 66 | |
Ted Kremenek | 3172383 | 2010-01-11 23:56:39 +0000 | [diff] [blame] | 67 | /// clang_getDeclaration() maps from a CXEntity to the matching CXDecl (if any) |
| 68 | /// in a specified translation unit. |
| 69 | CXDecl clang_getDeclaration(CXEntity CE, CXTranslationUnit TU) { |
| 70 | return (CXDecl) GetEntity(CE).getDecl(GetASTContext(TU)); |
| 71 | } |
| 72 | |
| 73 | |
| 74 | CXEntity 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 Kremenek | 8776382 | 2010-01-12 02:07:58 +0000 | [diff] [blame] | 80 | //===----------------------------------------------------------------------===// |
| 81 | // USR generation. |
| 82 | //===----------------------------------------------------------------------===// |
| 83 | |
| 84 | namespace { |
| 85 | class USRGenerator : public DeclVisitor<USRGenerator> { |
| 86 | llvm::raw_ostream &Out; |
| 87 | public: |
| 88 | USRGenerator(llvm::raw_ostream &out) : Out(out) {} |
| 89 | |
Ted Kremenek | e9cde11 | 2010-01-12 19:35:53 +0000 | [diff] [blame] | 90 | void VisitNamedDecl(NamedDecl *D); |
Ted Kremenek | 8776382 | 2010-01-12 02:07:58 +0000 | [diff] [blame] | 91 | void VisitObjCContainerDecl(ObjCContainerDecl *CD); |
| 92 | void VisitObjCMethodDecl(ObjCMethodDecl *MD); |
| 93 | void VisitObjCPropertyDecl(ObjCPropertyDecl *D); |
| 94 | }; |
| 95 | } // end anonymous namespace |
| 96 | |
Ted Kremenek | e9cde11 | 2010-01-12 19:35:53 +0000 | [diff] [blame] | 97 | |
| 98 | void 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 Kremenek | 8776382 | 2010-01-12 02:07:58 +0000 | [diff] [blame] | 110 | void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) { |
| 111 | Visit(cast<Decl>(D->getDeclContext())); |
Ted Kremenek | e9cde11 | 2010-01-12 19:35:53 +0000 | [diff] [blame] | 112 | Out << (D->isInstanceMethod() ? "(im)" : "(cm)"); |
Ted Kremenek | 8776382 | 2010-01-12 02:07:58 +0000 | [diff] [blame] | 113 | Out << DeclarationName(D->getSelector()); |
| 114 | } |
| 115 | |
| 116 | void 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 Kremenek | e9cde11 | 2010-01-12 19:35:53 +0000 | [diff] [blame] | 122 | Out << "objc(cs)" << D->getName(); |
Ted Kremenek | 8776382 | 2010-01-12 02:07:58 +0000 | [diff] [blame] | 123 | break; |
| 124 | case Decl::ObjCCategory: { |
| 125 | ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D); |
Ted Kremenek | e9cde11 | 2010-01-12 19:35:53 +0000 | [diff] [blame] | 126 | Out << "objc(cy)" << CD->getClassInterface()->getName() |
Ted Kremenek | 8776382 | 2010-01-12 02:07:58 +0000 | [diff] [blame] | 127 | << '_' << CD->getName(); |
| 128 | break; |
| 129 | } |
| 130 | case Decl::ObjCCategoryImpl: { |
| 131 | ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D); |
Ted Kremenek | e9cde11 | 2010-01-12 19:35:53 +0000 | [diff] [blame] | 132 | Out << "objc(cy)" << CD->getClassInterface()->getName() |
Ted Kremenek | 8776382 | 2010-01-12 02:07:58 +0000 | [diff] [blame] | 133 | << '_' << CD->getName(); |
| 134 | break; |
| 135 | } |
| 136 | case Decl::ObjCProtocol: |
Ted Kremenek | e9cde11 | 2010-01-12 19:35:53 +0000 | [diff] [blame] | 137 | Out << "objc(pl)" << cast<ObjCProtocolDecl>(D)->getName(); |
Ted Kremenek | 8776382 | 2010-01-12 02:07:58 +0000 | [diff] [blame] | 138 | break; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | void USRGenerator::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { |
| 143 | Visit(cast<Decl>(D->getDeclContext())); |
Ted Kremenek | e9cde11 | 2010-01-12 19:35:53 +0000 | [diff] [blame] | 144 | Out << "(py)" << D->getName(); |
Ted Kremenek | 8776382 | 2010-01-12 02:07:58 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | // FIXME: This is a skeleton implementation. It will be overhauled. |
| 148 | CXString 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 Kremenek | 1b6869a | 2010-01-05 22:06:45 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Ted Kremenek | 1b6869a | 2010-01-05 22:06:45 +0000 | [diff] [blame] | 173 | } // end extern "C" |