clang  10.0.0svn
GlobalDecl.h
Go to the documentation of this file.
1 //===- GlobalDecl.h - Global declaration holder -----------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // A GlobalDecl can hold either a regular variable/function or a C++ ctor/dtor
10 // together with its type.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_AST_GLOBALDECL_H
15 #define LLVM_CLANG_AST_GLOBALDECL_H
16 
17 #include "clang/AST/DeclCXX.h"
18 #include "clang/AST/DeclObjC.h"
19 #include "clang/AST/DeclOpenMP.h"
20 #include "clang/Basic/ABI.h"
21 #include "clang/Basic/LLVM.h"
22 #include "llvm/ADT/DenseMapInfo.h"
23 #include "llvm/ADT/PointerIntPair.h"
24 #include "llvm/Support/Casting.h"
25 #include "llvm/Support/type_traits.h"
26 #include <cassert>
27 
28 namespace clang {
29 
30 enum class DynamicInitKind : unsigned {
31  NoStub = 0,
33  AtExit,
34 };
35 
36 /// GlobalDecl - represents a global declaration. This can either be a
37 /// CXXConstructorDecl and the constructor type (Base, Complete).
38 /// a CXXDestructorDecl and the destructor type (Base, Complete) or
39 /// a VarDecl, a FunctionDecl or a BlockDecl.
40 class GlobalDecl {
41  llvm::PointerIntPair<const Decl *, 2> Value;
42  unsigned MultiVersionIndex = 0;
43 
44  void Init(const Decl *D) {
45  assert(!isa<CXXConstructorDecl>(D) && "Use other ctor with ctor decls!");
46  assert(!isa<CXXDestructorDecl>(D) && "Use other ctor with dtor decls!");
47 
48  Value.setPointer(D);
49  }
50 
51 public:
52  GlobalDecl() = default;
53  GlobalDecl(const VarDecl *D) { Init(D);}
54  GlobalDecl(const FunctionDecl *D, unsigned MVIndex = 0)
55  : MultiVersionIndex(MVIndex) {
56  Init(D);
57  }
58  GlobalDecl(const BlockDecl *D) { Init(D); }
59  GlobalDecl(const CapturedDecl *D) { Init(D); }
60  GlobalDecl(const ObjCMethodDecl *D) { Init(D); }
61  GlobalDecl(const OMPDeclareReductionDecl *D) { Init(D); }
62  GlobalDecl(const OMPDeclareMapperDecl *D) { Init(D); }
63  GlobalDecl(const CXXConstructorDecl *D, CXXCtorType Type) : Value(D, Type) {}
64  GlobalDecl(const CXXDestructorDecl *D, CXXDtorType Type) : Value(D, Type) {}
65  GlobalDecl(const VarDecl *D, DynamicInitKind StubKind)
66  : Value(D, unsigned(StubKind)) {}
67 
69  GlobalDecl CanonGD;
70  CanonGD.Value.setPointer(Value.getPointer()->getCanonicalDecl());
71  CanonGD.Value.setInt(Value.getInt());
72  CanonGD.MultiVersionIndex = MultiVersionIndex;
73 
74  return CanonGD;
75  }
76 
77  const Decl *getDecl() const { return Value.getPointer(); }
78 
80  assert(isa<CXXConstructorDecl>(getDecl()) && "Decl is not a ctor!");
81  return static_cast<CXXCtorType>(Value.getInt());
82  }
83 
85  assert(isa<CXXDestructorDecl>(getDecl()) && "Decl is not a dtor!");
86  return static_cast<CXXDtorType>(Value.getInt());
87  }
88 
90  assert(isa<VarDecl>(getDecl()) &&
91  cast<VarDecl>(getDecl())->hasGlobalStorage() &&
92  "Decl is not a global variable!");
93  return static_cast<DynamicInitKind>(Value.getInt());
94  }
95 
96  unsigned getMultiVersionIndex() const {
97  assert(isa<FunctionDecl>(getDecl()) &&
98  !isa<CXXConstructorDecl>(getDecl()) &&
99  !isa<CXXDestructorDecl>(getDecl()) &&
100  "Decl is not a plain FunctionDecl!");
101  return MultiVersionIndex;
102  }
103 
104  friend bool operator==(const GlobalDecl &LHS, const GlobalDecl &RHS) {
105  return LHS.Value == RHS.Value &&
106  LHS.MultiVersionIndex == RHS.MultiVersionIndex;
107  }
108 
109  void *getAsOpaquePtr() const { return Value.getOpaqueValue(); }
110 
111  static GlobalDecl getFromOpaquePtr(void *P) {
112  GlobalDecl GD;
113  GD.Value.setFromOpaqueValue(P);
114  return GD;
115  }
116 
118  GlobalDecl Result(*this);
119  Result.Value.setPointer(D);
120  return Result;
121  }
122 
124  assert(isa<CXXConstructorDecl>(getDecl()));
125  GlobalDecl Result(*this);
126  Result.Value.setInt(Type);
127  return Result;
128  }
129 
131  assert(isa<CXXDestructorDecl>(getDecl()));
132  GlobalDecl Result(*this);
133  Result.Value.setInt(Type);
134  return Result;
135  }
136 
138  assert(isa<FunctionDecl>(getDecl()) &&
139  !isa<CXXConstructorDecl>(getDecl()) &&
140  !isa<CXXDestructorDecl>(getDecl()) &&
141  "Decl is not a plain FunctionDecl!");
142  GlobalDecl Result(*this);
143  Result.MultiVersionIndex = Index;
144  return Result;
145  }
146 };
147 
148 } // namespace clang
149 
150 namespace llvm {
151 
152  template<> struct DenseMapInfo<clang::GlobalDecl> {
153  static inline clang::GlobalDecl getEmptyKey() {
154  return clang::GlobalDecl();
155  }
156 
158  return clang::GlobalDecl::
159  getFromOpaquePtr(reinterpret_cast<void*>(-1));
160  }
161 
162  static unsigned getHashValue(clang::GlobalDecl GD) {
164  }
165 
166  static bool isEqual(clang::GlobalDecl LHS,
167  clang::GlobalDecl RHS) {
168  return LHS == RHS;
169  }
170  };
171 
172 } // namespace llvm
173 
174 #endif // LLVM_CLANG_AST_GLOBALDECL_H
Represents a function declaration or definition.
Definition: Decl.h:1756
GlobalDecl getWithDecl(const Decl *D)
Definition: GlobalDecl.h:117
GlobalDecl(const VarDecl *D)
Definition: GlobalDecl.h:53
GlobalDecl getWithMultiVersionIndex(unsigned Index)
Definition: GlobalDecl.h:137
CXXDtorType getDtorType() const
Definition: GlobalDecl.h:84
Specialize PointerLikeTypeTraits to allow LazyGenerationalUpdatePtr to be placed into a PointerUnion...
Definition: Dominators.h:30
static clang::GlobalDecl getEmptyKey()
Definition: GlobalDecl.h:153
GlobalDecl(const VarDecl *D, DynamicInitKind StubKind)
Definition: GlobalDecl.h:65
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:88
StringRef P
GlobalDecl getCanonicalDecl() const
Definition: GlobalDecl.h:68
The base class of the type hierarchy.
Definition: Type.h:1436
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2528
DynamicInitKind
Definition: GlobalDecl.h:30
static GlobalDecl getFromOpaquePtr(void *P)
Definition: GlobalDecl.h:111
GlobalDecl getWithCtorType(CXXCtorType Type)
Definition: GlobalDecl.h:123
Represents a variable declaration or definition.
Definition: Decl.h:812
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:138
static bool isEqual(clang::GlobalDecl LHS, clang::GlobalDecl RHS)
Definition: GlobalDecl.h:166
void * getAsOpaquePtr() const
Definition: GlobalDecl.h:109
static unsigned getHashValue(clang::GlobalDecl GD)
Definition: GlobalDecl.h:162
GlobalDecl(const CXXDestructorDecl *D, CXXDtorType Type)
Definition: GlobalDecl.h:64
GlobalDecl(const CapturedDecl *D)
Definition: GlobalDecl.h:59
GlobalDecl(const CXXConstructorDecl *D, CXXCtorType Type)
Definition: GlobalDecl.h:63
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
CXXCtorType getCtorType() const
Definition: GlobalDecl.h:79
Enums/classes describing ABI related information about constructors, destructors and thunks...
GlobalDecl(const OMPDeclareMapperDecl *D)
Definition: GlobalDecl.h:62
GlobalDecl(const OMPDeclareReductionDecl *D)
Definition: GlobalDecl.h:61
Represents the body of a CapturedStmt, and serves as its DeclContext.
Definition: Decl.h:4116
GlobalDecl(const FunctionDecl *D, unsigned MVIndex=0)
Definition: GlobalDecl.h:54
GlobalDecl getWithDtorType(CXXDtorType Type)
Definition: GlobalDecl.h:130
CXXDtorType
C++ destructor types.
Definition: ABI.h:33
Represents a block literal declaration, which is like an unnamed FunctionDecl.
Definition: Decl.h:3923
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2792
The result type of a method or function.
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:40
static clang::GlobalDecl getTombstoneKey()
Definition: GlobalDecl.h:157
DynamicInitKind getDynamicInitKind() const
Definition: GlobalDecl.h:89
This represents &#39;#pragma omp declare reduction ...&#39; directive.
Definition: DeclOpenMP.h:102
const Decl * getDecl() const
Definition: GlobalDecl.h:77
GlobalDecl(const ObjCMethodDecl *D)
Definition: GlobalDecl.h:60
This file defines OpenMP nodes for declarative directives.
CXXCtorType
C++ constructor types.
Definition: ABI.h:24
Dataflow Directional Tag Classes.
unsigned getMultiVersionIndex() const
Definition: GlobalDecl.h:96
GlobalDecl(const BlockDecl *D)
Definition: GlobalDecl.h:58
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
This represents &#39;#pragma omp declare mapper ...&#39; directive.
Definition: DeclOpenMP.h:217
friend bool operator==(const GlobalDecl &LHS, const GlobalDecl &RHS)
Definition: GlobalDecl.h:104