clang  14.0.0git
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/Attr.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"
27 #include <cassert>
28 
29 namespace clang {
30 
31 enum class DynamicInitKind : unsigned {
32  NoStub = 0,
34  AtExit,
36 };
37 
39  Kernel = 0,
40  Stub = 1,
41 };
42 
43 /// GlobalDecl - represents a global declaration. This can either be a
44 /// CXXConstructorDecl and the constructor type (Base, Complete).
45 /// a CXXDestructorDecl and the destructor type (Base, Complete),
46 /// a FunctionDecl and the kernel reference type (Kernel, Stub), or
47 /// a VarDecl, a FunctionDecl or a BlockDecl.
48 ///
49 /// When a new type of GlobalDecl is added, the following places should
50 /// be updated to convert a Decl* to a GlobalDecl:
51 /// PredefinedExpr::ComputeName() in lib/AST/Expr.cpp.
52 /// getParentOfLocalEntity() in lib/AST/ItaniumMangle.cpp
53 /// ASTNameGenerator::Implementation::writeFuncOrVarName in lib/AST/Mangle.cpp
54 ///
55 class GlobalDecl {
56  llvm::PointerIntPair<const Decl *, 3> Value;
57  unsigned MultiVersionIndex = 0;
58 
59  void Init(const Decl *D) {
60  assert(!isa<CXXConstructorDecl>(D) && "Use other ctor with ctor decls!");
61  assert(!isa<CXXDestructorDecl>(D) && "Use other ctor with dtor decls!");
62  assert(!D->hasAttr<CUDAGlobalAttr>() && "Use other ctor with GPU kernels!");
63 
64  Value.setPointer(D);
65  }
66 
67 public:
68  GlobalDecl() = default;
69  GlobalDecl(const VarDecl *D) { Init(D);}
70  GlobalDecl(const FunctionDecl *D, unsigned MVIndex = 0)
71  : MultiVersionIndex(MVIndex) {
72  if (!D->hasAttr<CUDAGlobalAttr>()) {
73  Init(D);
74  return;
75  }
76  Value.setPointerAndInt(D, unsigned(getDefaultKernelReference(D)));
77  }
79  : Value(D, unsigned(Kind)) {
80  assert(D->hasAttr<CUDAGlobalAttr>() && "Decl is not a GPU kernel!");
81  }
82  GlobalDecl(const NamedDecl *D) { Init(D); }
83  GlobalDecl(const BlockDecl *D) { Init(D); }
84  GlobalDecl(const CapturedDecl *D) { Init(D); }
85  GlobalDecl(const ObjCMethodDecl *D) { Init(D); }
86  GlobalDecl(const OMPDeclareReductionDecl *D) { Init(D); }
87  GlobalDecl(const OMPDeclareMapperDecl *D) { Init(D); }
90  GlobalDecl(const VarDecl *D, DynamicInitKind StubKind)
91  : Value(D, unsigned(StubKind)) {}
92 
94  GlobalDecl CanonGD;
95  CanonGD.Value.setPointer(Value.getPointer()->getCanonicalDecl());
96  CanonGD.Value.setInt(Value.getInt());
97  CanonGD.MultiVersionIndex = MultiVersionIndex;
98 
99  return CanonGD;
100  }
101 
102  const Decl *getDecl() const { return Value.getPointer(); }
103 
105  assert(isa<CXXConstructorDecl>(getDecl()) && "Decl is not a ctor!");
106  return static_cast<CXXCtorType>(Value.getInt());
107  }
108 
110  assert(isa<CXXDestructorDecl>(getDecl()) && "Decl is not a dtor!");
111  return static_cast<CXXDtorType>(Value.getInt());
112  }
113 
115  assert(isa<VarDecl>(getDecl()) &&
116  cast<VarDecl>(getDecl())->hasGlobalStorage() &&
117  "Decl is not a global variable!");
118  return static_cast<DynamicInitKind>(Value.getInt());
119  }
120 
121  unsigned getMultiVersionIndex() const {
122  assert(isa<FunctionDecl>(
123  getDecl()) &&
124  !cast<FunctionDecl>(getDecl())->hasAttr<CUDAGlobalAttr>() &&
125  !isa<CXXConstructorDecl>(getDecl()) &&
126  !isa<CXXDestructorDecl>(getDecl()) &&
127  "Decl is not a plain FunctionDecl!");
128  return MultiVersionIndex;
129  }
130 
132  assert(isa<FunctionDecl>(getDecl()) &&
133  cast<FunctionDecl>(getDecl())->hasAttr<CUDAGlobalAttr>() &&
134  "Decl is not a GPU kernel!");
135  return static_cast<KernelReferenceKind>(Value.getInt());
136  }
137 
138  friend bool operator==(const GlobalDecl &LHS, const GlobalDecl &RHS) {
139  return LHS.Value == RHS.Value &&
140  LHS.MultiVersionIndex == RHS.MultiVersionIndex;
141  }
142 
143  void *getAsOpaquePtr() const { return Value.getOpaqueValue(); }
144 
145  explicit operator bool() const { return getAsOpaquePtr(); }
146 
147  static GlobalDecl getFromOpaquePtr(void *P) {
148  GlobalDecl GD;
149  GD.Value.setFromOpaqueValue(P);
150  return GD;
151  }
152 
154  return D->getLangOpts().CUDAIsDevice ? KernelReferenceKind::Kernel
156  }
157 
159  GlobalDecl Result(*this);
160  Result.Value.setPointer(D);
161  return Result;
162  }
163 
165  assert(isa<CXXConstructorDecl>(getDecl()));
166  GlobalDecl Result(*this);
167  Result.Value.setInt(Type);
168  return Result;
169  }
170 
172  assert(isa<CXXDestructorDecl>(getDecl()));
173  GlobalDecl Result(*this);
174  Result.Value.setInt(Type);
175  return Result;
176  }
177 
179  assert(isa<FunctionDecl>(getDecl()) &&
180  !cast<FunctionDecl>(getDecl())->hasAttr<CUDAGlobalAttr>() &&
181  !isa<CXXConstructorDecl>(getDecl()) &&
182  !isa<CXXDestructorDecl>(getDecl()) &&
183  "Decl is not a plain FunctionDecl!");
184  GlobalDecl Result(*this);
185  Result.MultiVersionIndex = Index;
186  return Result;
187  }
188 
190  assert(isa<FunctionDecl>(getDecl()) &&
191  cast<FunctionDecl>(getDecl())->hasAttr<CUDAGlobalAttr>() &&
192  "Decl is not a GPU kernel!");
193  GlobalDecl Result(*this);
194  Result.Value.setInt(unsigned(Kind));
195  return Result;
196  }
197 };
198 
199 } // namespace clang
200 
201 namespace llvm {
202 
203  template<> struct DenseMapInfo<clang::GlobalDecl> {
204  static inline clang::GlobalDecl getEmptyKey() {
205  return clang::GlobalDecl();
206  }
207 
209  return clang::GlobalDecl::
210  getFromOpaquePtr(reinterpret_cast<void*>(-1));
211  }
212 
213  static unsigned getHashValue(clang::GlobalDecl GD) {
215  }
216 
217  static bool isEqual(clang::GlobalDecl LHS,
218  clang::GlobalDecl RHS) {
219  return LHS == RHS;
220  }
221  };
222 
223 } // namespace llvm
224 
225 #endif // LLVM_CLANG_AST_GLOBALDECL_H
clang::GlobalDecl::getDtorType
CXXDtorType getDtorType() const
Definition: GlobalDecl.h:109
llvm
Definition: Dominators.h:30
clang::CXXConstructorDecl
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2401
clang::GlobalDecl::GlobalDecl
GlobalDecl(const ObjCMethodDecl *D)
Definition: GlobalDecl.h:85
clang::GlobalDecl::GlobalDecl
GlobalDecl()=default
clang::GlobalDecl::GlobalDecl
GlobalDecl(const FunctionDecl *D, KernelReferenceKind Kind)
Definition: GlobalDecl.h:78
clang::Decl::hasAttr
bool hasAttr() const
Definition: DeclBase.h:547
clang::DynamicInitKind::NoStub
@ NoStub
clang::GlobalDecl::getWithCtorType
GlobalDecl getWithCtorType(CXXCtorType Type)
Definition: GlobalDecl.h:164
clang::GlobalDecl::getMultiVersionIndex
unsigned getMultiVersionIndex() const
Definition: GlobalDecl.h:121
clang::NamedDecl
This represents a decl that may have a name.
Definition: Decl.h:249
clang::GlobalDecl::GlobalDecl
GlobalDecl(const FunctionDecl *D, unsigned MVIndex=0)
Definition: GlobalDecl.h:70
clang::GlobalDecl::getWithKernelReferenceKind
GlobalDecl getWithKernelReferenceKind(KernelReferenceKind Kind)
Definition: GlobalDecl.h:189
Attr.h
clang::DynamicInitKind::AtExit
@ AtExit
clang::DynamicInitKind::Initializer
@ Initializer
DeclCXX.h
DeclOpenMP.h
clang::GlobalDecl::getWithMultiVersionIndex
GlobalDecl getWithMultiVersionIndex(unsigned Index)
Definition: GlobalDecl.h:178
clang::Decl::getLangOpts
const LangOptions & getLangOpts() const LLVM_READONLY
Helper to get the language options from the ASTContext.
Definition: DeclBase.cpp:420
clang::KernelReferenceKind
KernelReferenceKind
Definition: GlobalDecl.h:38
clang::CapturedDecl
Represents the body of a CapturedStmt, and serves as its DeclContext.
Definition: Decl.h:4342
llvm::DenseMapInfo< clang::GlobalDecl >::getTombstoneKey
static clang::GlobalDecl getTombstoneKey()
Definition: GlobalDecl.h:208
clang::Type
The base class of the type hierarchy.
Definition: Type.h:1490
DeclObjC.h
ABI.h
clang::GlobalDecl
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:55
clang::GlobalDecl::GlobalDecl
GlobalDecl(const CXXDestructorDecl *D, CXXDtorType Type)
Definition: GlobalDecl.h:89
clang::DynamicInitKind
DynamicInitKind
Definition: GlobalDecl.h:31
llvm::DenseMapInfo
Definition: TypeOrdering.h:37
clang::GlobalDecl::getWithDtorType
GlobalDecl getWithDtorType(CXXDtorType Type)
Definition: GlobalDecl.h:171
clang::CXXCtorType
CXXCtorType
C++ constructor types.
Definition: ABI.h:24
clang::GlobalDecl::getDefaultKernelReference
static KernelReferenceKind getDefaultKernelReference(const FunctionDecl *D)
Definition: GlobalDecl.h:153
clang::GlobalDecl::getCanonicalDecl
GlobalDecl getCanonicalDecl() const
Definition: GlobalDecl.h:93
clang::OMPDeclareReductionDecl
This represents '#pragma omp declare reduction ...' directive.
Definition: DeclOpenMP.h:171
llvm::DenseMapInfo< clang::GlobalDecl >::isEqual
static bool isEqual(clang::GlobalDecl LHS, clang::GlobalDecl RHS)
Definition: GlobalDecl.h:217
llvm::DenseMapInfo< clang::GlobalDecl >::getEmptyKey
static clang::GlobalDecl getEmptyKey()
Definition: GlobalDecl.h:204
clang::BlockDecl
Represents a block literal declaration, which is like an unnamed FunctionDecl.
Definition: Decl.h:4153
clang::CXXDtorType
CXXDtorType
C++ destructor types.
Definition: ABI.h:33
clang::GlobalDecl::GlobalDecl
GlobalDecl(const OMPDeclareMapperDecl *D)
Definition: GlobalDecl.h:87
clang::GlobalDecl::GlobalDecl
GlobalDecl(const BlockDecl *D)
Definition: GlobalDecl.h:83
clang::GlobalDecl::GlobalDecl
GlobalDecl(const NamedDecl *D)
Definition: GlobalDecl.h:82
clang::CXXDestructorDecl
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2668
bool
#define bool
Definition: stdbool.h:15
clang::VarDecl
Represents a variable declaration or definition.
Definition: Decl.h:876
clang::OMPDeclareMapperDecl
This represents '#pragma omp declare mapper ...' directive.
Definition: DeclOpenMP.h:286
clang::KernelReferenceKind::Kernel
@ Kernel
clang::DynamicInitKind::GlobalArrayDestructor
@ GlobalArrayDestructor
clang::GlobalDecl::getAsOpaquePtr
void * getAsOpaquePtr() const
Definition: GlobalDecl.h:143
clang::GlobalDecl::GlobalDecl
GlobalDecl(const VarDecl *D)
Definition: GlobalDecl.h:69
clang::GlobalDecl::getFromOpaquePtr
static GlobalDecl getFromOpaquePtr(void *P)
Definition: GlobalDecl.h:147
P
StringRef P
Definition: ASTMatchersInternal.cpp:563
clang::GlobalDecl::getWithDecl
GlobalDecl getWithDecl(const Decl *D)
Definition: GlobalDecl.h:158
Value
Value
Definition: UninitializedValues.cpp:102
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:89
LLVM.h
clang::GlobalDecl::operator==
friend bool operator==(const GlobalDecl &LHS, const GlobalDecl &RHS)
Definition: GlobalDecl.h:138
clang::GlobalDecl::GlobalDecl
GlobalDecl(const CXXConstructorDecl *D, CXXCtorType Type)
Definition: GlobalDecl.h:88
llvm::DenseMapInfo< clang::GlobalDecl >::getHashValue
static unsigned getHashValue(clang::GlobalDecl GD)
Definition: GlobalDecl.h:213
clang::ObjCPropertyAttribute::Kind
Kind
Definition: DeclObjCCommon.h:22
clang::ObjCMethodDecl
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:139
clang::GlobalDecl::GlobalDecl
GlobalDecl(const VarDecl *D, DynamicInitKind StubKind)
Definition: GlobalDecl.h:90
clang
Definition: CalledOnceCheck.h:17
unsigned
clang::GlobalDecl::GlobalDecl
GlobalDecl(const CapturedDecl *D)
Definition: GlobalDecl.h:84
clang::GlobalDecl::getCtorType
CXXCtorType getCtorType() const
Definition: GlobalDecl.h:104
clang::GlobalDecl::getDecl
const Decl * getDecl() const
Definition: GlobalDecl.h:102
clang::FunctionDecl
Represents a function declaration or definition.
Definition: Decl.h:1856
clang::KernelReferenceKind::Stub
@ Stub
clang::GlobalDecl::GlobalDecl
GlobalDecl(const OMPDeclareReductionDecl *D)
Definition: GlobalDecl.h:86
clang::GlobalDecl::getDynamicInitKind
DynamicInitKind getDynamicInitKind() const
Definition: GlobalDecl.h:114
clang::GlobalDecl::getKernelReferenceKind
KernelReferenceKind getKernelReferenceKind() const
Definition: GlobalDecl.h:131