Anders Carlsson | 764d0c2 | 2009-11-13 04:25:07 +0000 | [diff] [blame] | 1 | //===--- GlobalDecl.h - Global declaration holder ---------------*- C++ -*-===// |
| 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 | // A GlobalDecl can hold either a regular variable/function or a C++ ctor/dtor |
| 11 | // together with its type. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef CLANG_CODEGEN_GLOBALDECL_H |
| 16 | #define CLANG_CODEGEN_GLOBALDECL_H |
| 17 | |
Anders Carlsson | b2bcf1c | 2010-02-06 02:44:09 +0000 | [diff] [blame] | 18 | #include "CGCXX.h" |
| 19 | #include "clang/AST/DeclCXX.h" |
| 20 | #include "clang/AST/DeclObjC.h" |
| 21 | |
Anders Carlsson | 764d0c2 | 2009-11-13 04:25:07 +0000 | [diff] [blame] | 22 | namespace clang { |
| 23 | |
| 24 | namespace CodeGen { |
| 25 | |
| 26 | /// GlobalDecl - represents a global declaration. This can either be a |
| 27 | /// CXXConstructorDecl and the constructor type (Base, Complete). |
| 28 | /// a CXXDestructorDecl and the destructor type (Base, Complete) or |
| 29 | /// a VarDecl, a FunctionDecl or a BlockDecl. |
| 30 | class GlobalDecl { |
| 31 | llvm::PointerIntPair<const Decl*, 2> Value; |
| 32 | |
| 33 | void Init(const Decl *D) { |
| 34 | assert(!isa<CXXConstructorDecl>(D) && "Use other ctor with ctor decls!"); |
| 35 | assert(!isa<CXXDestructorDecl>(D) && "Use other ctor with dtor decls!"); |
| 36 | |
| 37 | Value.setPointer(D); |
| 38 | } |
Anders Carlsson | dc5daec | 2010-06-22 16:00:14 +0000 | [diff] [blame] | 39 | |
Anders Carlsson | 764d0c2 | 2009-11-13 04:25:07 +0000 | [diff] [blame] | 40 | public: |
| 41 | GlobalDecl() {} |
| 42 | |
| 43 | GlobalDecl(const VarDecl *D) { Init(D);} |
| 44 | GlobalDecl(const FunctionDecl *D) { Init(D); } |
| 45 | GlobalDecl(const BlockDecl *D) { Init(D); } |
| 46 | GlobalDecl(const ObjCMethodDecl *D) { Init(D); } |
| 47 | |
| 48 | GlobalDecl(const CXXConstructorDecl *D, CXXCtorType Type) |
| 49 | : Value(D, Type) {} |
| 50 | GlobalDecl(const CXXDestructorDecl *D, CXXDtorType Type) |
| 51 | : Value(D, Type) {} |
| 52 | |
Anders Carlsson | dc5daec | 2010-06-22 16:00:14 +0000 | [diff] [blame] | 53 | GlobalDecl getCanonicalDecl() const { |
| 54 | GlobalDecl CanonGD; |
| 55 | CanonGD.Value.setPointer(Value.getPointer()->getCanonicalDecl()); |
| 56 | CanonGD.Value.setInt(Value.getInt()); |
| 57 | |
| 58 | return CanonGD; |
| 59 | } |
| 60 | |
Anders Carlsson | 764d0c2 | 2009-11-13 04:25:07 +0000 | [diff] [blame] | 61 | const Decl *getDecl() const { return Value.getPointer(); } |
| 62 | |
| 63 | CXXCtorType getCtorType() const { |
| 64 | assert(isa<CXXConstructorDecl>(getDecl()) && "Decl is not a ctor!"); |
| 65 | return static_cast<CXXCtorType>(Value.getInt()); |
| 66 | } |
| 67 | |
| 68 | CXXDtorType getDtorType() const { |
| 69 | assert(isa<CXXDestructorDecl>(getDecl()) && "Decl is not a dtor!"); |
| 70 | return static_cast<CXXDtorType>(Value.getInt()); |
| 71 | } |
| 72 | |
| 73 | friend bool operator==(const GlobalDecl &LHS, const GlobalDecl &RHS) { |
| 74 | return LHS.Value == RHS.Value; |
| 75 | } |
| 76 | |
| 77 | void *getAsOpaquePtr() const { return Value.getOpaqueValue(); } |
| 78 | |
| 79 | static GlobalDecl getFromOpaquePtr(void *P) { |
| 80 | GlobalDecl GD; |
| 81 | GD.Value.setFromOpaqueValue(P); |
| 82 | return GD; |
| 83 | } |
| 84 | }; |
| 85 | |
| 86 | } // end namespace CodeGen |
| 87 | } // end namespace clang |
| 88 | |
| 89 | namespace llvm { |
| 90 | template<class> struct DenseMapInfo; |
| 91 | |
| 92 | template<> struct DenseMapInfo<clang::CodeGen::GlobalDecl> { |
| 93 | static inline clang::CodeGen::GlobalDecl getEmptyKey() { |
| 94 | return clang::CodeGen::GlobalDecl(); |
| 95 | } |
| 96 | |
| 97 | static inline clang::CodeGen::GlobalDecl getTombstoneKey() { |
| 98 | return clang::CodeGen::GlobalDecl:: |
| 99 | getFromOpaquePtr(reinterpret_cast<void*>(-1)); |
| 100 | } |
| 101 | |
| 102 | static unsigned getHashValue(clang::CodeGen::GlobalDecl GD) { |
| 103 | return DenseMapInfo<void*>::getHashValue(GD.getAsOpaquePtr()); |
| 104 | } |
| 105 | |
| 106 | static bool isEqual(clang::CodeGen::GlobalDecl LHS, |
| 107 | clang::CodeGen::GlobalDecl RHS) { |
| 108 | return LHS == RHS; |
| 109 | } |
| 110 | |
Anders Carlsson | 764d0c2 | 2009-11-13 04:25:07 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
Chris Lattner | 06159e8 | 2009-12-15 07:26:51 +0000 | [diff] [blame] | 113 | // GlobalDecl isn't *technically* a POD type. However, its copy constructor, |
| 114 | // copy assignment operator, and destructor are all trivial. |
| 115 | template <> |
| 116 | struct isPodLike<clang::CodeGen::GlobalDecl> { |
| 117 | static const bool value = true; |
| 118 | }; |
| 119 | } // end namespace llvm |
Anders Carlsson | 764d0c2 | 2009-11-13 04:25:07 +0000 | [diff] [blame] | 120 | |
| 121 | #endif |