clang 24.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/DeclCXX.h"
18#include "clang/AST/DeclObjC.h"
19#include "clang/Basic/ABI.h"
20#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
28namespace clang {
29
35
42
43enum class KernelReferenceKind : unsigned {
44 Kernel = 0,
45 Stub = 1,
46};
47
48/// GlobalDecl - represents a global declaration. This can either be a
49/// CXXConstructorDecl and the constructor type (Base, Complete).
50/// a CXXDestructorDecl and the destructor type (Base, Complete),
51/// a FunctionDecl and the kernel reference type (Kernel, Stub), or
52/// a VarDecl, a FunctionDecl or a BlockDecl.
53///
54/// When a new type of GlobalDecl is added, the following places should
55/// be updated to convert a Decl* to a GlobalDecl:
56/// PredefinedExpr::ComputeName() in lib/AST/Expr.cpp.
57/// getParentOfLocalEntity() in lib/AST/ItaniumMangle.cpp
58/// ASTNameGenerator::Implementation::writeFuncOrVarName in lib/AST/Mangle.cpp
59///
61 llvm::PointerIntPair<const Decl *, 3> Value;
62 unsigned MultiVersionIndex = 0;
63
64 CLANG_ABI static bool hasCUDAGlobalAttr(const Decl *D);
65
66 /// Whether \p D is a kernel referred to through a KernelReferenceKind,
67 /// either directly or as a function template.
68 CLANG_ABI static bool isKernelReference(const Decl *D);
69
70 void Init(const Decl *D) {
71 assert(!isa<CXXConstructorDecl>(D) && "Use other ctor with ctor decls!");
72 assert(!isa<CXXDestructorDecl>(D) && "Use other ctor with dtor decls!");
73 assert(!hasCUDAGlobalAttr(D) && "Use other ctor with GPU kernels!");
74
75 Value.setPointer(D);
76 }
77
78public:
79 GlobalDecl() = default;
80 GlobalDecl(const VarDecl *D) { Init(D);}
81 GlobalDecl(const FunctionDecl *D, unsigned MVIndex = 0)
82 : MultiVersionIndex(MVIndex) {
83 if (D->isReferenceableKernel()) {
84 Value.setPointerAndInt(D, unsigned(getDefaultKernelReference(D)));
85 return;
86 }
87 Init(D);
88 }
90 : Value(D, unsigned(Kind)) {
91 assert(D->isReferenceableKernel() && "Decl is not a GPU kernel!");
92 }
93 GlobalDecl(const NamedDecl *D) { Init(D); }
94 GlobalDecl(const BlockDecl *D) { Init(D); }
95 GlobalDecl(const CapturedDecl *D) { Init(D); }
96 GlobalDecl(const ObjCMethodDecl *D) { Init(D); }
104 : Value(D, unsigned(StubKind)) {}
105
107 GlobalDecl CanonGD;
108 CanonGD.Value.setPointer(Value.getPointer()->getCanonicalDecl());
109 CanonGD.Value.setInt(Value.getInt());
110 CanonGD.MultiVersionIndex = MultiVersionIndex;
111
112 return CanonGD;
113 }
114
115 const Decl *getDecl() const { return Value.getPointer(); }
116
118 assert(isa<CXXConstructorDecl>(getDecl()) && "Decl is not a ctor!");
119 return static_cast<CXXCtorType>(Value.getInt());
120 }
121
123 assert(isa<CXXDestructorDecl>(getDecl()) && "Decl is not a dtor!");
124 return static_cast<CXXDtorType>(Value.getInt());
125 }
126
128 assert(isa<VarDecl>(getDecl()) &&
129 cast<VarDecl>(getDecl())->hasGlobalStorage() &&
130 "Decl is not a global variable!");
131 return static_cast<DynamicInitKind>(Value.getInt());
132 }
133
134 unsigned getMultiVersionIndex() const {
135 assert(isa<FunctionDecl>(getDecl()) && !hasCUDAGlobalAttr(getDecl()) &&
138 "Decl is not a plain FunctionDecl!");
139 return MultiVersionIndex;
140 }
141
143 assert(isKernelReference(getDecl()) && "Decl is not a GPU kernel!");
144
145 return static_cast<KernelReferenceKind>(Value.getInt());
146 }
147
148 friend bool operator==(const GlobalDecl &LHS, const GlobalDecl &RHS) {
149 return LHS.Value == RHS.Value &&
150 LHS.MultiVersionIndex == RHS.MultiVersionIndex;
151 }
152
153 bool operator!=(const GlobalDecl &Other) const {
154 return !(*this == Other);
155 }
156
157 void *getAsOpaquePtr() const { return Value.getOpaqueValue(); }
158
159 explicit operator bool() const { return getAsOpaquePtr(); }
160
161 static GlobalDecl getFromOpaquePtr(void *P) {
162 GlobalDecl GD;
163 GD.Value.setFromOpaqueValue(P);
164 return GD;
165 }
166
167 CLANG_ABI static KernelReferenceKind
169
171 GlobalDecl Result(*this);
172 Result.Value.setPointer(D);
173 return Result;
174 }
175
178 GlobalDecl Result(*this);
179 Result.Value.setInt(Type);
180 return Result;
181 }
182
185 GlobalDecl Result(*this);
186 Result.Value.setInt(Type);
187 return Result;
188 }
189
191 assert(isa<FunctionDecl>(getDecl()) && !hasCUDAGlobalAttr(getDecl()) &&
194 "Decl is not a plain FunctionDecl!");
195 GlobalDecl Result(*this);
196 Result.MultiVersionIndex = Index;
197 return Result;
198 }
199
201 assert(isa<FunctionDecl>(getDecl()) &&
202 cast<FunctionDecl>(getDecl())->isReferenceableKernel() &&
203 "Decl is not a GPU kernel!");
204 GlobalDecl Result(*this);
205 Result.Value.setInt(unsigned(Kind));
206 return Result;
207 }
208};
209
210} // namespace clang
211
212namespace llvm {
213
214 template<> struct DenseMapInfo<clang::GlobalDecl> {
215 static unsigned getHashValue(clang::GlobalDecl GD) {
216 return DenseMapInfo<void*>::getHashValue(GD.getAsOpaquePtr());
217 }
218
219 static bool isEqual(clang::GlobalDecl LHS,
220 clang::GlobalDecl RHS) {
221 return LHS == RHS;
222 }
223 };
224
225} // namespace llvm
226
227#endif // LLVM_CLANG_AST_GLOBALDECL_H
Enums/classes describing ABI related information about constructors, destructors and thunks.
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
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:4806
Represents a C++ constructor within a class.
Definition DeclCXX.h:2637
Represents a C++ destructor within a class.
Definition DeclCXX.h:2902
Represents the body of a CapturedStmt, and serves as its DeclContext.
Definition Decl.h:5078
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
Represents a function declaration or definition.
Definition Decl.h:2058
bool isReferenceableKernel() const
Definition Decl.cpp:5709
Declaration of a template function.
GlobalDecl - represents a global declaration.
Definition GlobalDecl.h:60
GlobalDecl(const CapturedDecl *D)
Definition GlobalDecl.h:95
GlobalDecl getWithMultiVersionIndex(unsigned Index)
Definition GlobalDecl.h:190
GlobalDecl getWithCtorType(CXXCtorType Type)
Definition GlobalDecl.h:176
GlobalDecl(const FunctionDecl *D, KernelReferenceKind Kind)
Definition GlobalDecl.h:89
CXXCtorType getCtorType() const
Definition GlobalDecl.h:117
GlobalDecl(const FunctionDecl *D, unsigned MVIndex=0)
Definition GlobalDecl.h:81
GlobalDecl getWithKernelReferenceKind(KernelReferenceKind Kind)
Definition GlobalDecl.h:200
static CLANG_ABI KernelReferenceKind getDefaultKernelReference(const FunctionDecl *D)
GlobalDecl getCanonicalDecl() const
Definition GlobalDecl.h:106
friend bool operator==(const GlobalDecl &LHS, const GlobalDecl &RHS)
Definition GlobalDecl.h:148
GlobalDecl(const VarDecl *D)
Definition GlobalDecl.h:80
KernelReferenceKind getKernelReferenceKind() const
Definition GlobalDecl.h:142
static GlobalDecl getFromOpaquePtr(void *P)
Definition GlobalDecl.h:161
GlobalDecl getWithDecl(const Decl *D)
Definition GlobalDecl.h:170
unsigned getMultiVersionIndex() const
Definition GlobalDecl.h:134
void * getAsOpaquePtr() const
Definition GlobalDecl.h:157
GlobalDecl(const CXXConstructorDecl *D, CXXCtorType Type)
Definition GlobalDecl.h:101
DynamicInitKind getDynamicInitKind() const
Definition GlobalDecl.h:127
GlobalDecl getWithDtorType(CXXDtorType Type)
Definition GlobalDecl.h:183
bool operator!=(const GlobalDecl &Other) const
Definition GlobalDecl.h:153
GlobalDecl(const VarDecl *D, DynamicInitKind StubKind)
Definition GlobalDecl.h:103
CXXDtorType getDtorType() const
Definition GlobalDecl.h:122
const Decl * getDecl() const
Definition GlobalDecl.h:115
GlobalDecl(const BlockDecl *D)
Definition GlobalDecl.h:94
GlobalDecl()=default
GlobalDecl(const CXXDestructorDecl *D, CXXDtorType Type)
Definition GlobalDecl.h:102
GlobalDecl(const ObjCMethodDecl *D)
Definition GlobalDecl.h:96
GlobalDecl(const NamedDecl *D)
Definition GlobalDecl.h:93
This represents a decl that may have a name.
Definition Decl.h:274
This represents 'pragma omp declare mapper ...' directive.
Definition DeclOpenMP.h:349
This represents 'pragma omp declare reduction ...' directive.
Definition DeclOpenMP.h:239
ObjCMethodDecl - Represents an instance or class method declaration.
Definition DeclObjC.h:140
The base class of the type hierarchy.
Definition TypeBase.h:1879
Represents a variable declaration or definition.
Definition Decl.h:932
Top level wrappers for InstallAPI frontend operations.
CXXCtorType
C++ constructor types.
Definition ABI.h:24
bool isa(CodeGen::Address addr)
Definition Address.h:330
@ Result
The result type of a method or function.
Definition TypeBase.h:906
DynamicInitKind
Definition GlobalDecl.h:36
CXXDtorType
C++ destructor types.
Definition ABI.h:34
U cast(CodeGen::Address addr)
Definition Address.h:327
KernelReferenceKind
Definition GlobalDecl.h:43
@ Other
Other implicit parameter.
Definition Decl.h:1774
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30
static bool isEqual(clang::GlobalDecl LHS, clang::GlobalDecl RHS)
Definition GlobalDecl.h:219
static unsigned getHashValue(clang::GlobalDecl GD)
Definition GlobalDecl.h:215