blob: b8a98d7c03b63bc62e67250a62e89c9180719c24 [file] [log] [blame]
Anders Carlsson764d0c22009-11-13 04:25:07 +00001//===--- 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 Carlssonb2bcf1c2010-02-06 02:44:09 +000018#include "CGCXX.h"
19#include "clang/AST/DeclCXX.h"
20#include "clang/AST/DeclObjC.h"
21
Anders Carlsson764d0c22009-11-13 04:25:07 +000022namespace clang {
23
24namespace 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.
30class 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 }
39
40public:
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
53 const Decl *getDecl() const { return Value.getPointer(); }
54
55 CXXCtorType getCtorType() const {
56 assert(isa<CXXConstructorDecl>(getDecl()) && "Decl is not a ctor!");
57 return static_cast<CXXCtorType>(Value.getInt());
58 }
59
60 CXXDtorType getDtorType() const {
61 assert(isa<CXXDestructorDecl>(getDecl()) && "Decl is not a dtor!");
62 return static_cast<CXXDtorType>(Value.getInt());
63 }
64
65 friend bool operator==(const GlobalDecl &LHS, const GlobalDecl &RHS) {
66 return LHS.Value == RHS.Value;
67 }
68
69 void *getAsOpaquePtr() const { return Value.getOpaqueValue(); }
70
71 static GlobalDecl getFromOpaquePtr(void *P) {
72 GlobalDecl GD;
73 GD.Value.setFromOpaqueValue(P);
74 return GD;
75 }
76};
77
78} // end namespace CodeGen
79} // end namespace clang
80
81namespace llvm {
82 template<class> struct DenseMapInfo;
83
84 template<> struct DenseMapInfo<clang::CodeGen::GlobalDecl> {
85 static inline clang::CodeGen::GlobalDecl getEmptyKey() {
86 return clang::CodeGen::GlobalDecl();
87 }
88
89 static inline clang::CodeGen::GlobalDecl getTombstoneKey() {
90 return clang::CodeGen::GlobalDecl::
91 getFromOpaquePtr(reinterpret_cast<void*>(-1));
92 }
93
94 static unsigned getHashValue(clang::CodeGen::GlobalDecl GD) {
95 return DenseMapInfo<void*>::getHashValue(GD.getAsOpaquePtr());
96 }
97
98 static bool isEqual(clang::CodeGen::GlobalDecl LHS,
99 clang::CodeGen::GlobalDecl RHS) {
100 return LHS == RHS;
101 }
102
Anders Carlsson764d0c22009-11-13 04:25:07 +0000103 };
104
Chris Lattner06159e82009-12-15 07:26:51 +0000105 // GlobalDecl isn't *technically* a POD type. However, its copy constructor,
106 // copy assignment operator, and destructor are all trivial.
107 template <>
108 struct isPodLike<clang::CodeGen::GlobalDecl> {
109 static const bool value = true;
110 };
111} // end namespace llvm
Anders Carlsson764d0c22009-11-13 04:25:07 +0000112
113#endif