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 | |
| 90 | void VisitObjCContainerDecl(ObjCContainerDecl *CD); |
| 91 | void VisitObjCMethodDecl(ObjCMethodDecl *MD); |
| 92 | void VisitObjCPropertyDecl(ObjCPropertyDecl *D); |
| 93 | }; |
| 94 | } // end anonymous namespace |
| 95 | |
| 96 | void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) { |
| 97 | Visit(cast<Decl>(D->getDeclContext())); |
| 98 | Out << (D->isInstanceMethod() ? "_IM_" : "_CM_"); |
| 99 | Out << DeclarationName(D->getSelector()); |
| 100 | } |
| 101 | |
| 102 | void 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 | |
| 128 | void 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. |
| 134 | CXString 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 Kremenek | 1b6869a | 2010-01-05 22:06:45 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Ted Kremenek | 1b6869a | 2010-01-05 22:06:45 +0000 | [diff] [blame] | 159 | } // end extern "C" |