Ted Kremenek | 378e93c | 2010-08-27 21:57:20 +0000 | [diff] [blame] | 1 | //===- CIndexCXX.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 libclang support for C++ cursors. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CIndexer.h" |
| 15 | #include "CXCursor.h" |
| 16 | #include "CXType.h" |
| 17 | #include "clang/AST/DeclCXX.h" |
Douglas Gregor | f11309e | 2010-08-31 22:12:17 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclTemplate.h" |
Ted Kremenek | 378e93c | 2010-08-27 21:57:20 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace clang; |
Douglas Gregor | d3f48bd | 2010-09-02 00:07:54 +0000 | [diff] [blame] | 21 | using namespace clang::cxcursor; |
Ted Kremenek | 378e93c | 2010-08-27 21:57:20 +0000 | [diff] [blame] | 22 | |
Ted Kremenek | 378e93c | 2010-08-27 21:57:20 +0000 | [diff] [blame] | 23 | unsigned clang_isVirtualBase(CXCursor C) { |
| 24 | if (C.kind != CXCursor_CXXBaseSpecifier) |
| 25 | return 0; |
| 26 | |
Dmitri Gribenko | ba2f746 | 2013-01-11 21:01:49 +0000 | [diff] [blame] | 27 | const CXXBaseSpecifier *B = getCursorCXXBaseSpecifier(C); |
Ted Kremenek | 378e93c | 2010-08-27 21:57:20 +0000 | [diff] [blame] | 28 | return B->isVirtual(); |
| 29 | } |
| 30 | |
| 31 | enum CX_CXXAccessSpecifier clang_getCXXAccessSpecifier(CXCursor C) { |
Argyrios Kyrtzidis | 12afd70 | 2011-09-30 17:58:23 +0000 | [diff] [blame] | 32 | AccessSpecifier spec = AS_none; |
| 33 | |
Argyrios Kyrtzidis | 1ab09cc | 2013-04-11 17:02:10 +0000 | [diff] [blame] | 34 | if (C.kind == CXCursor_CXXAccessSpecifier || clang_isDeclaration(C.kind)) |
Argyrios Kyrtzidis | 12afd70 | 2011-09-30 17:58:23 +0000 | [diff] [blame] | 35 | spec = getCursorDecl(C)->getAccess(); |
| 36 | else if (C.kind == CXCursor_CXXBaseSpecifier) |
| 37 | spec = getCursorCXXBaseSpecifier(C)->getAccessSpecifier(); |
| 38 | else |
Ted Kremenek | 378e93c | 2010-08-27 21:57:20 +0000 | [diff] [blame] | 39 | return CX_CXXInvalidAccessSpecifier; |
| 40 | |
Argyrios Kyrtzidis | 12afd70 | 2011-09-30 17:58:23 +0000 | [diff] [blame] | 41 | switch (spec) { |
Ted Kremenek | 378e93c | 2010-08-27 21:57:20 +0000 | [diff] [blame] | 42 | case AS_public: return CX_CXXPublic; |
| 43 | case AS_protected: return CX_CXXProtected; |
| 44 | case AS_private: return CX_CXXPrivate; |
| 45 | case AS_none: return CX_CXXInvalidAccessSpecifier; |
| 46 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 47 | |
| 48 | llvm_unreachable("Invalid AccessSpecifier!"); |
Ted Kremenek | 378e93c | 2010-08-27 21:57:20 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Douglas Gregor | f11309e | 2010-08-31 22:12:17 +0000 | [diff] [blame] | 51 | enum CXCursorKind clang_getTemplateCursorKind(CXCursor C) { |
| 52 | using namespace clang::cxcursor; |
| 53 | |
| 54 | switch (C.kind) { |
| 55 | case CXCursor_ClassTemplate: |
| 56 | case CXCursor_FunctionTemplate: |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 57 | if (const TemplateDecl *Template |
Douglas Gregor | f11309e | 2010-08-31 22:12:17 +0000 | [diff] [blame] | 58 | = dyn_cast_or_null<TemplateDecl>(getCursorDecl(C))) |
Dmitri Gribenko | 2c173b4 | 2013-01-11 19:28:44 +0000 | [diff] [blame] | 59 | return MakeCXCursor(Template->getTemplatedDecl(), getCursorTU(C)).kind; |
Douglas Gregor | f11309e | 2010-08-31 22:12:17 +0000 | [diff] [blame] | 60 | break; |
| 61 | |
| 62 | case CXCursor_ClassTemplatePartialSpecialization: |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 63 | if (const ClassTemplateSpecializationDecl *PartialSpec |
Joao Matos | e9a3ed4 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 64 | = dyn_cast_or_null<ClassTemplatePartialSpecializationDecl>( |
| 65 | getCursorDecl(C))) { |
| 66 | switch (PartialSpec->getTagKind()) { |
| 67 | case TTK_Interface: |
| 68 | case TTK_Struct: return CXCursor_StructDecl; |
| 69 | case TTK_Class: return CXCursor_ClassDecl; |
| 70 | case TTK_Union: return CXCursor_UnionDecl; |
| 71 | case TTK_Enum: return CXCursor_NoDeclFound; |
| 72 | } |
Douglas Gregor | f11309e | 2010-08-31 22:12:17 +0000 | [diff] [blame] | 73 | } |
| 74 | break; |
| 75 | |
| 76 | default: |
| 77 | break; |
| 78 | } |
| 79 | |
| 80 | return CXCursor_NoDeclFound; |
| 81 | } |
| 82 | |
Douglas Gregor | d3f48bd | 2010-09-02 00:07:54 +0000 | [diff] [blame] | 83 | CXCursor clang_getSpecializedCursorTemplate(CXCursor C) { |
| 84 | if (!clang_isDeclaration(C.kind)) |
| 85 | return clang_getNullCursor(); |
| 86 | |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 87 | const Decl *D = getCursorDecl(C); |
Douglas Gregor | d3f48bd | 2010-09-02 00:07:54 +0000 | [diff] [blame] | 88 | if (!D) |
| 89 | return clang_getNullCursor(); |
Craig Topper | 69186e7 | 2014-06-08 08:38:04 +0000 | [diff] [blame] | 90 | |
| 91 | Decl *Template = nullptr; |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 92 | if (const CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(D)) { |
| 93 | if (const ClassTemplatePartialSpecializationDecl *PartialSpec |
Douglas Gregor | d3f48bd | 2010-09-02 00:07:54 +0000 | [diff] [blame] | 94 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(CXXRecord)) |
| 95 | Template = PartialSpec->getSpecializedTemplate(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 96 | else if (const ClassTemplateSpecializationDecl *ClassSpec |
Douglas Gregor | d3f48bd | 2010-09-02 00:07:54 +0000 | [diff] [blame] | 97 | = dyn_cast<ClassTemplateSpecializationDecl>(CXXRecord)) { |
| 98 | llvm::PointerUnion<ClassTemplateDecl *, |
| 99 | ClassTemplatePartialSpecializationDecl *> Result |
| 100 | = ClassSpec->getSpecializedTemplateOrPartial(); |
| 101 | if (Result.is<ClassTemplateDecl *>()) |
| 102 | Template = Result.get<ClassTemplateDecl *>(); |
| 103 | else |
| 104 | Template = Result.get<ClassTemplatePartialSpecializationDecl *>(); |
| 105 | |
| 106 | } else |
| 107 | Template = CXXRecord->getInstantiatedFromMemberClass(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 108 | } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | d3f48bd | 2010-09-02 00:07:54 +0000 | [diff] [blame] | 109 | Template = Function->getPrimaryTemplate(); |
| 110 | if (!Template) |
| 111 | Template = Function->getInstantiatedFromMemberFunction(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 112 | } else if (const VarDecl *Var = dyn_cast<VarDecl>(D)) { |
Douglas Gregor | d3f48bd | 2010-09-02 00:07:54 +0000 | [diff] [blame] | 113 | if (Var->isStaticDataMember()) |
| 114 | Template = Var->getInstantiatedFromStaticDataMember(); |
Dmitri Gribenko | d15bb30 | 2013-01-23 17:25:27 +0000 | [diff] [blame] | 115 | } else if (const RedeclarableTemplateDecl *Tmpl |
Douglas Gregor | d3f48bd | 2010-09-02 00:07:54 +0000 | [diff] [blame] | 116 | = dyn_cast<RedeclarableTemplateDecl>(D)) |
| 117 | Template = Tmpl->getInstantiatedFromMemberTemplate(); |
| 118 | |
| 119 | if (!Template) |
| 120 | return clang_getNullCursor(); |
| 121 | |
Dmitri Gribenko | 2c173b4 | 2013-01-11 19:28:44 +0000 | [diff] [blame] | 122 | return MakeCXCursor(Template, getCursorTU(C)); |
Douglas Gregor | d3f48bd | 2010-09-02 00:07:54 +0000 | [diff] [blame] | 123 | } |