1 //===- GlobalDecl.h - Global declaration holder -----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // A GlobalDecl can hold either a regular variable/function or a C++ ctor/dtor
11 // together with its type.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_AST_GLOBALDECL_H
16 #define LLVM_CLANG_AST_GLOBALDECL_H
18 #include "clang/AST/DeclCXX.h"
19 #include "clang/AST/DeclObjC.h"
20 #include "clang/AST/DeclOpenMP.h"
21 #include "clang/Basic/ABI.h"
22 #include "clang/Basic/LLVM.h"
23 #include "llvm/ADT/DenseMapInfo.h"
24 #include "llvm/ADT/PointerIntPair.h"
25 #include "llvm/Support/Casting.h"
26 #include "llvm/Support/type_traits.h"
31 /// GlobalDecl - represents a global declaration. This can either be a
32 /// CXXConstructorDecl and the constructor type (Base, Complete).
33 /// a CXXDestructorDecl and the destructor type (Base, Complete) or
34 /// a VarDecl, a FunctionDecl or a BlockDecl.
36 llvm::PointerIntPair<const Decl *, 2> Value;
38 void Init(const Decl *D) {
39 assert(!isa<CXXConstructorDecl>(D) && "Use other ctor with ctor decls!");
40 assert(!isa<CXXDestructorDecl>(D) && "Use other ctor with dtor decls!");
46 GlobalDecl() = default;
47 GlobalDecl(const VarDecl *D) { Init(D);}
48 GlobalDecl(const FunctionDecl *D) { Init(D); }
49 GlobalDecl(const BlockDecl *D) { Init(D); }
50 GlobalDecl(const CapturedDecl *D) { Init(D); }
51 GlobalDecl(const ObjCMethodDecl *D) { Init(D); }
52 GlobalDecl(const OMPDeclareReductionDecl *D) { Init(D); }
53 GlobalDecl(const CXXConstructorDecl *D, CXXCtorType Type) : Value(D, Type) {}
54 GlobalDecl(const CXXDestructorDecl *D, CXXDtorType Type) : Value(D, Type) {}
56 GlobalDecl getCanonicalDecl() const {
58 CanonGD.Value.setPointer(Value.getPointer()->getCanonicalDecl());
59 CanonGD.Value.setInt(Value.getInt());
64 const Decl *getDecl() const { return Value.getPointer(); }
66 CXXCtorType getCtorType() const {
67 assert(isa<CXXConstructorDecl>(getDecl()) && "Decl is not a ctor!");
68 return static_cast<CXXCtorType>(Value.getInt());
71 CXXDtorType getDtorType() const {
72 assert(isa<CXXDestructorDecl>(getDecl()) && "Decl is not a dtor!");
73 return static_cast<CXXDtorType>(Value.getInt());
76 friend bool operator==(const GlobalDecl &LHS, const GlobalDecl &RHS) {
77 return LHS.Value == RHS.Value;
80 void *getAsOpaquePtr() const { return Value.getOpaqueValue(); }
82 static GlobalDecl getFromOpaquePtr(void *P) {
84 GD.Value.setFromOpaqueValue(P);
88 GlobalDecl getWithDecl(const Decl *D) {
89 GlobalDecl Result(*this);
90 Result.Value.setPointer(D);
99 template<> struct DenseMapInfo<clang::GlobalDecl> {
100 static inline clang::GlobalDecl getEmptyKey() {
101 return clang::GlobalDecl();
104 static inline clang::GlobalDecl getTombstoneKey() {
105 return clang::GlobalDecl::
106 getFromOpaquePtr(reinterpret_cast<void*>(-1));
109 static unsigned getHashValue(clang::GlobalDecl GD) {
110 return DenseMapInfo<void*>::getHashValue(GD.getAsOpaquePtr());
113 static bool isEqual(clang::GlobalDecl LHS,
114 clang::GlobalDecl RHS) {
119 // GlobalDecl isn't *technically* a POD type. However, its copy constructor,
120 // copy assignment operator, and destructor are all trivial.
122 struct isPodLike<clang::GlobalDecl> {
123 static const bool value = true;
128 #endif // LLVM_CLANG_AST_GLOBALDECL_H