clang 22.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"
23#include "clang/Basic/ABI.h"
24#include "clang/Basic/LLVM.h"
25#include "llvm/ADT/DenseMapInfo.h"
26#include "llvm/ADT/PointerIntPair.h"
27#include "llvm/Support/Casting.h"
28#include "llvm/Support/type_traits.h"
29#include <cassert>
30
31namespace clang {
32
33enum class DynamicInitKind : unsigned {
34 NoStub = 0,
36 AtExit,
38};
39
40enum class KernelReferenceKind : unsigned {
41 Kernel = 0,
42 Stub = 1,
43};
44
45/// GlobalDecl - represents a global declaration. This can either be a
46/// CXXConstructorDecl and the constructor type (Base, Complete).
47/// a CXXDestructorDecl and the destructor type (Base, Complete),
48/// a FunctionDecl and the kernel reference type (Kernel, Stub), or
49/// a VarDecl, a FunctionDecl or a BlockDecl.
50///
51/// When a new type of GlobalDecl is added, the following places should
52/// be updated to convert a Decl* to a GlobalDecl:
53/// PredefinedExpr::ComputeName() in lib/AST/Expr.cpp.
54/// getParentOfLocalEntity() in lib/AST/ItaniumMangle.cpp
55/// ASTNameGenerator::Implementation::writeFuncOrVarName in lib/AST/Mangle.cpp
56///
58 llvm::PointerIntPair<const Decl *, 3> Value;
59 unsigned MultiVersionIndex = 0;
60
61 void Init(const Decl *D) {
62 assert(!isa<CXXConstructorDecl>(D) && "Use other ctor with ctor decls!");
63 assert(!isa<CXXDestructorDecl>(D) && "Use other ctor with dtor decls!");
64 assert(!D->hasAttr<CUDAGlobalAttr>() && "Use other ctor with GPU kernels!");
65
66 Value.setPointer(D);
67 }
68
69public:
70 GlobalDecl() = default;
71 GlobalDecl(const VarDecl *D) { Init(D);}
72 GlobalDecl(const FunctionDecl *D, unsigned MVIndex = 0)
73 : MultiVersionIndex(MVIndex) {
74 if (D->isReferenceableKernel()) {
75 Value.setPointerAndInt(D, unsigned(getDefaultKernelReference(D)));
76 return;
77 }
78 Init(D);
79 }
81 : Value(D, unsigned(Kind)) {
82 assert(D->isReferenceableKernel() && "Decl is not a GPU kernel!");
83 }
84 GlobalDecl(const NamedDecl *D) { Init(D); }
85 GlobalDecl(const BlockDecl *D) { Init(D); }
95 : Value(D, unsigned(StubKind)) {}
96
98 GlobalDecl CanonGD;
99 CanonGD.Value.setPointer(Value.getPointer()->getCanonicalDecl());
100 CanonGD.Value.setInt(Value.getInt());
101 CanonGD.MultiVersionIndex = MultiVersionIndex;
102
103 return CanonGD;
104 }
105
106 const Decl *getDecl() const { return Value.getPointer(); }
107
109 assert(isa<CXXConstructorDecl>(getDecl()) && "Decl is not a ctor!");
110 return static_cast<CXXCtorType>(Value.getInt());
111 }
112
114 assert(isa<CXXDestructorDecl>(getDecl()) && "Decl is not a dtor!");
115 return static_cast<CXXDtorType>(Value.getInt());
116 }
117
119 assert(isa<VarDecl>(getDecl()) &&
120 cast<VarDecl>(getDecl())->hasGlobalStorage() &&
121 "Decl is not a global variable!");
122 return static_cast<DynamicInitKind>(Value.getInt());
123 }
124
125 unsigned getMultiVersionIndex() const {
126 assert(isa<FunctionDecl>(
127 getDecl()) &&
128 !cast<FunctionDecl>(getDecl())->hasAttr<CUDAGlobalAttr>() &&
129 !isa<CXXConstructorDecl>(getDecl()) &&
130 !isa<CXXDestructorDecl>(getDecl()) &&
131 "Decl is not a plain FunctionDecl!");
132 return MultiVersionIndex;
133 }
134
136 assert(((isa<FunctionDecl>(getDecl()) &&
137 cast<FunctionDecl>(getDecl())->isReferenceableKernel()) ||
138 (isa<FunctionTemplateDecl>(getDecl()) &&
139 cast<FunctionTemplateDecl>(getDecl())
140 ->getTemplatedDecl()
141 ->hasAttr<CUDAGlobalAttr>())) &&
142 "Decl is not a GPU kernel!");
143
144 return static_cast<KernelReferenceKind>(Value.getInt());
145 }
146
147 friend bool operator==(const GlobalDecl &LHS, const GlobalDecl &RHS) {
148 return LHS.Value == RHS.Value &&
149 LHS.MultiVersionIndex == RHS.MultiVersionIndex;
150 }
151
152 bool operator!=(const GlobalDecl &Other) const {
153 return !(*this == Other);
154 }
155
156 void *getAsOpaquePtr() const { return Value.getOpaqueValue(); }
157
158 explicit operator bool() const { return getAsOpaquePtr(); }
159
161 GlobalDecl GD;
162 GD.Value.setFromOpaqueValue(P);
163 return GD;
164 }
165
167 return (D->hasAttr<DeviceKernelAttr>() || D->getLangOpts().CUDAIsDevice)
170 }
171
173 GlobalDecl Result(*this);
174 Result.Value.setPointer(D);
175 return Result;
176 }
177
179 assert(isa<CXXConstructorDecl>(getDecl()));
180 GlobalDecl Result(*this);
181 Result.Value.setInt(Type);
182 return Result;
183 }
184
186 assert(isa<CXXDestructorDecl>(getDecl()));
187 GlobalDecl Result(*this);
188 Result.Value.setInt(Type);
189 return Result;
190 }
191
193 assert(isa<FunctionDecl>(getDecl()) &&
194 !cast<FunctionDecl>(getDecl())->hasAttr<CUDAGlobalAttr>() &&
195 !isa<CXXConstructorDecl>(getDecl()) &&
196 !isa<CXXDestructorDecl>(getDecl()) &&
197 "Decl is not a plain FunctionDecl!");
198 GlobalDecl Result(*this);
199 Result.MultiVersionIndex = Index;
200 return Result;
201 }
202
204 assert(isa<FunctionDecl>(getDecl()) &&
205 cast<FunctionDecl>(getDecl())->isReferenceableKernel() &&
206 "Decl is not a GPU kernel!");
207 GlobalDecl Result(*this);
208 Result.Value.setInt(unsigned(Kind));
209 return Result;
210 }
211};
212
213} // namespace clang
214
215namespace llvm {
216
217 template<> struct DenseMapInfo<clang::GlobalDecl> {
219 return clang::GlobalDecl();
220 }
221
224 getFromOpaquePtr(reinterpret_cast<void*>(-1));
225 }
226
227 static unsigned getHashValue(clang::GlobalDecl GD) {
228 return DenseMapInfo<void*>::getHashValue(GD.getAsOpaquePtr());
229 }
230
231 static bool isEqual(clang::GlobalDecl LHS,
232 clang::GlobalDecl RHS) {
233 return LHS == RHS;
234 }
235 };
236
237} // namespace llvm
238
239#endif // LLVM_CLANG_AST_GLOBALDECL_H
Enums/classes describing ABI related information about constructors, destructors and thunks.
StringRef P
const Decl * D
enum clang::sema::@1840::IndirectLocalPathEntry::EntryKind Kind
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
This file defines OpenACC nodes for declarative directives.
This file defines OpenMP nodes for declarative directives.
Defines the C++ template declaration subclasses.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Represents a block literal declaration, which is like an unnamed FunctionDecl.
Definition: Decl.h:4634
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2604
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2869
Represents the body of a CapturedStmt, and serves as its DeclContext.
Definition: Decl.h:4906
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
Represents a function declaration or definition.
Definition: Decl.h:1999
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:57
GlobalDecl(const OMPDeclareReductionDecl *D)
Definition: GlobalDecl.h:88
GlobalDecl(const CapturedDecl *D)
Definition: GlobalDecl.h:86
GlobalDecl(const OpenACCDeclareDecl *D)
Definition: GlobalDecl.h:91
GlobalDecl getWithMultiVersionIndex(unsigned Index)
Definition: GlobalDecl.h:192
GlobalDecl getWithCtorType(CXXCtorType Type)
Definition: GlobalDecl.h:178
GlobalDecl(const FunctionDecl *D, KernelReferenceKind Kind)
Definition: GlobalDecl.h:80
CXXCtorType getCtorType() const
Definition: GlobalDecl.h:108
GlobalDecl(const FunctionDecl *D, unsigned MVIndex=0)
Definition: GlobalDecl.h:72
GlobalDecl(const OpenACCRoutineDecl *D)
Definition: GlobalDecl.h:90
GlobalDecl getWithKernelReferenceKind(KernelReferenceKind Kind)
Definition: GlobalDecl.h:203
GlobalDecl getCanonicalDecl() const
Definition: GlobalDecl.h:97
friend bool operator==(const GlobalDecl &LHS, const GlobalDecl &RHS)
Definition: GlobalDecl.h:147
GlobalDecl(const VarDecl *D)
Definition: GlobalDecl.h:71
KernelReferenceKind getKernelReferenceKind() const
Definition: GlobalDecl.h:135
static GlobalDecl getFromOpaquePtr(void *P)
Definition: GlobalDecl.h:160
GlobalDecl getWithDecl(const Decl *D)
Definition: GlobalDecl.h:172
unsigned getMultiVersionIndex() const
Definition: GlobalDecl.h:125
void * getAsOpaquePtr() const
Definition: GlobalDecl.h:156
GlobalDecl(const CXXConstructorDecl *D, CXXCtorType Type)
Definition: GlobalDecl.h:92
DynamicInitKind getDynamicInitKind() const
Definition: GlobalDecl.h:118
GlobalDecl getWithDtorType(CXXDtorType Type)
Definition: GlobalDecl.h:185
GlobalDecl(const OMPDeclareMapperDecl *D)
Definition: GlobalDecl.h:89
bool operator!=(const GlobalDecl &Other) const
Definition: GlobalDecl.h:152
GlobalDecl(const VarDecl *D, DynamicInitKind StubKind)
Definition: GlobalDecl.h:94
static KernelReferenceKind getDefaultKernelReference(const FunctionDecl *D)
Definition: GlobalDecl.h:166
CXXDtorType getDtorType() const
Definition: GlobalDecl.h:113
const Decl * getDecl() const
Definition: GlobalDecl.h:106
GlobalDecl(const BlockDecl *D)
Definition: GlobalDecl.h:85
GlobalDecl()=default
GlobalDecl(const CXXDestructorDecl *D, CXXDtorType Type)
Definition: GlobalDecl.h:93
GlobalDecl(const ObjCMethodDecl *D)
Definition: GlobalDecl.h:87
GlobalDecl(const NamedDecl *D)
Definition: GlobalDecl.h:84
This represents a decl that may have a name.
Definition: Decl.h:273
This represents '#pragma omp declare mapper ...' directive.
Definition: DeclOpenMP.h:287
This represents '#pragma omp declare reduction ...' directive.
Definition: DeclOpenMP.h:177
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:140
The base class of the type hierarchy.
Definition: TypeBase.h:1833
Represents a variable declaration or definition.
Definition: Decl.h:925
#define bool
Definition: gpuintrin.h:32
The JSON file list parser is used to communicate input to InstallAPI.
CXXCtorType
C++ constructor types.
Definition: ABI.h:24
@ Result
The result type of a method or function.
DynamicInitKind
Definition: GlobalDecl.h:33
CXXDtorType
C++ destructor types.
Definition: ABI.h:33
KernelReferenceKind
Definition: GlobalDecl.h:40
@ Other
Other implicit parameter.
Diagnostic wrappers for TextAPI types for error reporting.
Definition: Dominators.h:30
static bool isEqual(clang::GlobalDecl LHS, clang::GlobalDecl RHS)
Definition: GlobalDecl.h:231
static clang::GlobalDecl getTombstoneKey()
Definition: GlobalDecl.h:222
static unsigned getHashValue(clang::GlobalDecl GD)
Definition: GlobalDecl.h:227
static clang::GlobalDecl getEmptyKey()
Definition: GlobalDecl.h:218