Coverage Report

Created: 2022-01-22 13:19

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/AST/GlobalDecl.h
Line
Count
Source (jump to first uncovered line)
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/AST/DeclTemplate.h"
22
#include "clang/Basic/ABI.h"
23
#include "clang/Basic/LLVM.h"
24
#include "llvm/ADT/DenseMapInfo.h"
25
#include "llvm/ADT/PointerIntPair.h"
26
#include "llvm/Support/Casting.h"
27
#include "llvm/Support/type_traits.h"
28
#include <cassert>
29
30
namespace clang {
31
32
enum class DynamicInitKind : unsigned {
33
  NoStub = 0,
34
  Initializer,
35
  AtExit,
36
  GlobalArrayDestructor
37
};
38
39
enum class KernelReferenceKind : unsigned {
40
  Kernel = 0,
41
  Stub = 1,
42
};
43
44
/// GlobalDecl - represents a global declaration. This can either be a
45
/// CXXConstructorDecl and the constructor type (Base, Complete).
46
/// a CXXDestructorDecl and the destructor type (Base, Complete),
47
/// a FunctionDecl and the kernel reference type (Kernel, Stub), or
48
/// a VarDecl, a FunctionDecl or a BlockDecl.
49
///
50
/// When a new type of GlobalDecl is added, the following places should
51
/// be updated to convert a Decl* to a GlobalDecl:
52
/// PredefinedExpr::ComputeName() in lib/AST/Expr.cpp.
53
/// getParentOfLocalEntity() in lib/AST/ItaniumMangle.cpp
54
/// ASTNameGenerator::Implementation::writeFuncOrVarName in lib/AST/Mangle.cpp
55
///
56
class GlobalDecl {
57
  llvm::PointerIntPair<const Decl *, 3> Value;
58
  unsigned MultiVersionIndex = 0;
59
60
29.6M
  void Init(const Decl *D) {
61
29.6M
    assert(!isa<CXXConstructorDecl>(D) && "Use other ctor with ctor decls!");
62
0
    assert(!isa<CXXDestructorDecl>(D) && "Use other ctor with dtor decls!");
63
0
    assert(!D->hasAttr<CUDAGlobalAttr>() && "Use other ctor with GPU kernels!");
64
65
0
    Value.setPointer(D);
66
29.6M
  }
67
68
public:
69
36.0M
  GlobalDecl() = default;
70
1.78M
  GlobalDecl(const VarDecl *D) { Init(D);}
71
  GlobalDecl(const FunctionDecl *D, unsigned MVIndex = 0)
72
23.1M
      : MultiVersionIndex(MVIndex) {
73
23.1M
    if (!D->hasAttr<CUDAGlobalAttr>()) {
74
23.1M
      Init(D);
75
23.1M
      return;
76
23.1M
    }
77
247
    Value.setPointerAndInt(D, unsigned(getDefaultKernelReference(D)));
78
247
  }
79
  GlobalDecl(const FunctionDecl *D, KernelReferenceKind Kind)
80
120
      : Value(D, unsigned(Kind)) {
81
120
    assert(D->hasAttr<CUDAGlobalAttr>() && "Decl is not a GPU kernel!");
82
120
  }
83
4.61M
  GlobalDecl(const NamedDecl *D) { Init(D); }
84
4.48k
  GlobalDecl(const BlockDecl *D) { Init(D); }
85
74.7k
  GlobalDecl(const CapturedDecl *D) { Init(D); }
86
21.4k
  GlobalDecl(const ObjCMethodDecl *D) { Init(D); }
87
10
  GlobalDecl(const OMPDeclareReductionDecl *D) { Init(D); }
88
0
  GlobalDecl(const OMPDeclareMapperDecl *D) { Init(D); }
89
464k
  GlobalDecl(const CXXConstructorDecl *D, CXXCtorType Type) : Value(D, Type) {}
90
122k
  GlobalDecl(const CXXDestructorDecl *D, CXXDtorType Type) : Value(D, Type) {}
91
  GlobalDecl(const VarDecl *D, DynamicInitKind StubKind)
92
6.37k
      : Value(D, unsigned(StubKind)) {}
93
94
4.89M
  GlobalDecl getCanonicalDecl() const {
95
4.89M
    GlobalDecl CanonGD;
96
4.89M
    CanonGD.Value.setPointer(Value.getPointer()->getCanonicalDecl());
97
4.89M
    CanonGD.Value.setInt(Value.getInt());
98
4.89M
    CanonGD.MultiVersionIndex = MultiVersionIndex;
99
100
4.89M
    return CanonGD;
101
4.89M
  }
102
103
77.0M
  const Decl *getDecl() const { return Value.getPointer(); }
104
105
403k
  CXXCtorType getCtorType() const {
106
403k
    assert(isa<CXXConstructorDecl>(getDecl()) && "Decl is not a ctor!");
107
0
    return static_cast<CXXCtorType>(Value.getInt());
108
403k
  }
109
110
285k
  CXXDtorType getDtorType() const {
111
285k
    assert(isa<CXXDestructorDecl>(getDecl()) && "Decl is not a dtor!");
112
0
    return static_cast<CXXDtorType>(Value.getInt());
113
285k
  }
114
115
1.58k
  DynamicInitKind getDynamicInitKind() const {
116
1.58k
    assert(isa<VarDecl>(getDecl()) &&
117
1.58k
           cast<VarDecl>(getDecl())->hasGlobalStorage() &&
118
1.58k
           "Decl is not a global variable!");
119
0
    return static_cast<DynamicInitKind>(Value.getInt());
120
1.58k
  }
121
122
376
  unsigned getMultiVersionIndex() const {
123
376
    assert(isa<FunctionDecl>(
124
376
               getDecl()) &&
125
376
               !cast<FunctionDecl>(getDecl())->hasAttr<CUDAGlobalAttr>() &&
126
376
           !isa<CXXConstructorDecl>(getDecl()) &&
127
376
           !isa<CXXDestructorDecl>(getDecl()) &&
128
376
           "Decl is not a plain FunctionDecl!");
129
0
    return MultiVersionIndex;
130
376
  }
131
132
362
  KernelReferenceKind getKernelReferenceKind() const {
133
362
    assert(((isa<FunctionDecl>(getDecl()) &&
134
362
             cast<FunctionDecl>(getDecl())->hasAttr<CUDAGlobalAttr>()) ||
135
362
            (isa<FunctionTemplateDecl>(getDecl()) &&
136
362
             cast<FunctionTemplateDecl>(getDecl())
137
362
                 ->getTemplatedDecl()
138
362
                 ->hasAttr<CUDAGlobalAttr>())) &&
139
362
           "Decl is not a GPU kernel!");
140
0
    return static_cast<KernelReferenceKind>(Value.getInt());
141
362
  }
142
143
94.8M
  friend bool operator==(const GlobalDecl &LHS, const GlobalDecl &RHS) {
144
94.8M
    return LHS.Value == RHS.Value &&
145
94.8M
           
LHS.MultiVersionIndex == RHS.MultiVersionIndex20.1M
;
146
94.8M
  }
147
148
15.4M
  void *getAsOpaquePtr() const { return Value.getOpaqueValue(); }
149
150
4.52M
  explicit operator bool() const { return getAsOpaquePtr(); }
151
152
11.0M
  static GlobalDecl getFromOpaquePtr(void *P) {
153
11.0M
    GlobalDecl GD;
154
11.0M
    GD.Value.setFromOpaqueValue(P);
155
11.0M
    return GD;
156
11.0M
  }
157
158
247
  static KernelReferenceKind getDefaultKernelReference(const FunctionDecl *D) {
159
247
    return D->getLangOpts().CUDAIsDevice ? 
KernelReferenceKind::Kernel91
160
247
                                         : 
KernelReferenceKind::Stub156
;
161
247
  }
162
163
4.54M
  GlobalDecl getWithDecl(const Decl *D) {
164
4.54M
    GlobalDecl Result(*this);
165
4.54M
    Result.Value.setPointer(D);
166
4.54M
    return Result;
167
4.54M
  }
168
169
17.8k
  GlobalDecl getWithCtorType(CXXCtorType Type) {
170
17.8k
    assert(isa<CXXConstructorDecl>(getDecl()));
171
0
    GlobalDecl Result(*this);
172
17.8k
    Result.Value.setInt(Type);
173
17.8k
    return Result;
174
17.8k
  }
175
176
7.71k
  GlobalDecl getWithDtorType(CXXDtorType Type) {
177
7.71k
    assert(isa<CXXDestructorDecl>(getDecl()));
178
0
    GlobalDecl Result(*this);
179
7.71k
    Result.Value.setInt(Type);
180
7.71k
    return Result;
181
7.71k
  }
182
183
232
  GlobalDecl getWithMultiVersionIndex(unsigned Index) {
184
232
    assert(isa<FunctionDecl>(getDecl()) &&
185
232
           !cast<FunctionDecl>(getDecl())->hasAttr<CUDAGlobalAttr>() &&
186
232
           !isa<CXXConstructorDecl>(getDecl()) &&
187
232
           !isa<CXXDestructorDecl>(getDecl()) &&
188
232
           "Decl is not a plain FunctionDecl!");
189
0
    GlobalDecl Result(*this);
190
232
    Result.MultiVersionIndex = Index;
191
232
    return Result;
192
232
  }
193
194
113
  GlobalDecl getWithKernelReferenceKind(KernelReferenceKind Kind) {
195
113
    assert(isa<FunctionDecl>(getDecl()) &&
196
113
           cast<FunctionDecl>(getDecl())->hasAttr<CUDAGlobalAttr>() &&
197
113
           "Decl is not a GPU kernel!");
198
0
    GlobalDecl Result(*this);
199
113
    Result.Value.setInt(unsigned(Kind));
200
113
    return Result;
201
113
  }
202
};
203
204
} // namespace clang
205
206
namespace llvm {
207
208
  template<> struct DenseMapInfo<clang::GlobalDecl> {
209
13.9M
    static inline clang::GlobalDecl getEmptyKey() {
210
13.9M
      return clang::GlobalDecl();
211
13.9M
    }
212
213
11.0M
    static inline clang::GlobalDecl getTombstoneKey() {
214
11.0M
      return clang::GlobalDecl::
215
11.0M
        getFromOpaquePtr(reinterpret_cast<void*>(-1));
216
11.0M
    }
217
218
10.9M
    static unsigned getHashValue(clang::GlobalDecl GD) {
219
10.9M
      return DenseMapInfo<void*>::getHashValue(GD.getAsOpaquePtr());
220
10.9M
    }
221
222
    static bool isEqual(clang::GlobalDecl LHS,
223
94.8M
                        clang::GlobalDecl RHS) {
224
94.8M
      return LHS == RHS;
225
94.8M
    }
226
  };
227
228
} // namespace llvm
229
230
#endif // LLVM_CLANG_AST_GLOBALDECL_H