Coverage Report

Created: 2020-09-19 12:23

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/CodeGen/CGBlocks.h
Line
Count
Source (jump to first uncovered line)
1
//===-- CGBlocks.h - state for LLVM CodeGen for blocks ----------*- 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
// This is the internal state used for llvm translation for block literals.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#ifndef LLVM_CLANG_LIB_CODEGEN_CGBLOCKS_H
14
#define LLVM_CLANG_LIB_CODEGEN_CGBLOCKS_H
15
16
#include "CGBuilder.h"
17
#include "CGCall.h"
18
#include "CGValue.h"
19
#include "CodeGenFunction.h"
20
#include "CodeGenTypes.h"
21
#include "clang/AST/CharUnits.h"
22
#include "clang/AST/Expr.h"
23
#include "clang/AST/ExprCXX.h"
24
#include "clang/AST/ExprObjC.h"
25
#include "clang/AST/Type.h"
26
#include "clang/Basic/TargetInfo.h"
27
28
namespace llvm {
29
class Constant;
30
class Function;
31
class GlobalValue;
32
class DataLayout;
33
class FunctionType;
34
class PointerType;
35
class Value;
36
class LLVMContext;
37
}
38
39
namespace clang {
40
namespace CodeGen {
41
42
class CGBlockInfo;
43
44
// Flags stored in __block variables.
45
enum BlockByrefFlags {
46
  BLOCK_BYREF_HAS_COPY_DISPOSE         = (1   << 25), // compiler
47
  BLOCK_BYREF_LAYOUT_MASK              = (0xF << 28), // compiler
48
  BLOCK_BYREF_LAYOUT_EXTENDED          = (1   << 28),
49
  BLOCK_BYREF_LAYOUT_NON_OBJECT        = (2   << 28),
50
  BLOCK_BYREF_LAYOUT_STRONG            = (3   << 28),
51
  BLOCK_BYREF_LAYOUT_WEAK              = (4   << 28),
52
  BLOCK_BYREF_LAYOUT_UNRETAINED        = (5   << 28)
53
};
54
55
enum BlockLiteralFlags {
56
  BLOCK_IS_NOESCAPE      =  (1 << 23),
57
  BLOCK_HAS_COPY_DISPOSE =  (1 << 25),
58
  BLOCK_HAS_CXX_OBJ =       (1 << 26),
59
  BLOCK_IS_GLOBAL =         (1 << 28),
60
  BLOCK_USE_STRET =         (1 << 29),
61
  BLOCK_HAS_SIGNATURE  =    (1 << 30),
62
  BLOCK_HAS_EXTENDED_LAYOUT = (1u << 31)
63
};
64
class BlockFlags {
65
  uint32_t flags;
66
67
public:
68
326
  BlockFlags(uint32_t flags) : flags(flags) {}
69
903
  BlockFlags() : flags(0) {}
70
2.15k
  BlockFlags(BlockLiteralFlags flag) : flags(flag) {}
71
302
  BlockFlags(BlockByrefFlags flag) : flags(flag) {}
72
73
1.22k
  uint32_t getBitMask() const { return flags; }
74
0
  bool empty() const { return flags == 0; }
75
76
306
  friend BlockFlags operator|(BlockFlags l, BlockFlags r) {
77
306
    return BlockFlags(l.flags | r.flags);
78
306
  }
79
1.03k
  friend BlockFlags &operator|=(BlockFlags &l, BlockFlags r) {
80
1.03k
    l.flags |= r.flags;
81
1.03k
    return l;
82
1.03k
  }
83
40
  friend bool operator&(BlockFlags l, BlockFlags r) {
84
40
    return (l.flags & r.flags);
85
40
  }
86
100
  bool operator==(BlockFlags r) {
87
100
    return (flags == r.flags);
88
100
  }
89
};
90
306
inline BlockFlags operator|(BlockLiteralFlags l, BlockLiteralFlags r) {
91
306
  return BlockFlags(l) | BlockFlags(r);
92
306
}
93
94
enum BlockFieldFlag_t {
95
  BLOCK_FIELD_IS_OBJECT   = 0x03,  /* id, NSObject, __attribute__((NSObject)),
96
                                    block, ... */
97
  BLOCK_FIELD_IS_BLOCK    = 0x07,  /* a block variable */
98
99
  BLOCK_FIELD_IS_BYREF    = 0x08,  /* the on stack structure holding the __block
100
                                    variable */
101
  BLOCK_FIELD_IS_WEAK     = 0x10,  /* declared __weak, only used in byref copy
102
                                    helpers */
103
  BLOCK_FIELD_IS_ARC      = 0x40,  /* field has ARC-specific semantics */
104
  BLOCK_BYREF_CALLER      = 128,   /* called from __block (byref) copy/dispose
105
                                      support routines */
106
  BLOCK_BYREF_CURRENT_MAX = 256
107
};
108
109
class BlockFieldFlags {
110
  uint32_t flags;
111
112
36
  BlockFieldFlags(uint32_t flags) : flags(flags) {}
113
public:
114
10.8k
  BlockFieldFlags() : flags(0) {}
115
5.89k
  BlockFieldFlags(BlockFieldFlag_t flag) : flags(flag) {}
116
117
1.62k
  uint32_t getBitMask() const { return flags; }
118
0
  bool empty() const { return flags == 0; }
119
120
  /// Answers whether the flags indicate that this field is an object
121
  /// or block pointer that requires _Block_object_assign/dispose.
122
0
  bool isSpecialPointer() const { return flags & BLOCK_FIELD_IS_OBJECT; }
123
124
36
  friend BlockFieldFlags operator|(BlockFieldFlags l, BlockFieldFlags r) {
125
36
    return BlockFieldFlags(l.flags | r.flags);
126
36
  }
127
68
  friend BlockFieldFlags &operator|=(BlockFieldFlags &l, BlockFieldFlags r) {
128
68
    l.flags |= r.flags;
129
68
    return l;
130
68
  }
131
0
  friend bool operator&(BlockFieldFlags l, BlockFieldFlags r) {
132
0
    return (l.flags & r.flags);
133
0
  }
134
466
  bool operator==(BlockFieldFlags Other) const {
135
466
    return flags == Other.flags;
136
466
  }
137
};
138
0
inline BlockFieldFlags operator|(BlockFieldFlag_t l, BlockFieldFlag_t r) {
139
0
  return BlockFieldFlags(l) | BlockFieldFlags(r);
140
0
}
141
142
/// Information about the layout of a __block variable.
143
class BlockByrefInfo {
144
public:
145
  llvm::StructType *Type;
146
  unsigned FieldIndex;
147
  CharUnits ByrefAlignment;
148
  CharUnits FieldOffset;
149
};
150
151
/// CGBlockInfo - Information to generate a block literal.
152
class CGBlockInfo {
153
public:
154
  /// Name - The name of the block, kindof.
155
  StringRef Name;
156
157
  /// The field index of 'this' within the block, if there is one.
158
  unsigned CXXThisIndex;
159
160
  class Capture {
161
    uintptr_t Data;
162
    EHScopeStack::stable_iterator Cleanup;
163
    CharUnits::QuantityType Offset;
164
165
    /// Type of the capture field. Normally, this is identical to the type of
166
    /// the capture's VarDecl, but can be different if there is an enclosing
167
    /// lambda.
168
    QualType FieldType;
169
170
  public:
171
34.5k
    bool isIndex() const { return (Data & 1) != 0; }
172
17.2k
    bool isConstant() const { return !isIndex(); }
173
174
11.9k
    unsigned getIndex() const {
175
11.9k
      assert(isIndex());
176
11.9k
      return Data >> 1;
177
11.9k
    }
178
5.32k
    CharUnits getOffset() const {
179
5.32k
      assert(isIndex());
180
5.32k
      return CharUnits::fromQuantity(Offset);
181
5.32k
    }
182
0
    EHScopeStack::stable_iterator getCleanup() const {
183
0
      assert(isIndex());
184
0
      return Cleanup;
185
0
    }
186
0
    void setCleanup(EHScopeStack::stable_iterator cleanup) {
187
0
      assert(isIndex());
188
0
      Cleanup = cleanup;
189
0
    }
190
191
5
    llvm::Value *getConstant() const {
192
5
      assert(isConstant());
193
5
      return reinterpret_cast<llvm::Value*>(Data);
194
5
    }
195
196
12.5k
    QualType fieldType() const {
197
12.5k
      return FieldType;
198
12.5k
    }
199
200
    static Capture makeIndex(unsigned index, CharUnits offset,
201
2.23k
                             QualType FieldType) {
202
2.23k
      Capture v;
203
2.23k
      v.Data = (index << 1) | 1;
204
2.23k
      v.Offset = offset.getQuantity();
205
2.23k
      v.FieldType = FieldType;
206
2.23k
      return v;
207
2.23k
    }
208
209
5
    static Capture makeConstant(llvm::Value *value) {
210
5
      Capture v;
211
5
      v.Data = reinterpret_cast<uintptr_t>(value);
212
5
      return v;
213
5
    }
214
  };
215
216
  /// CanBeGlobal - True if the block can be global, i.e. it has
217
  /// no non-constant captures.
218
  bool CanBeGlobal : 1;
219
220
  /// True if the block has captures that would necessitate custom copy or
221
  /// dispose helper functions if the block were escaping.
222
  bool NeedsCopyDispose : 1;
223
224
  /// HasCXXObject - True if the block's custom copy/dispose functions
225
  /// need to be run even in GC mode.
226
  bool HasCXXObject : 1;
227
228
  /// UsesStret : True if the block uses an stret return.  Mutable
229
  /// because it gets set later in the block-creation process.
230
  mutable bool UsesStret : 1;
231
232
  /// HasCapturedVariableLayout : True if block has captured variables
233
  /// and their layout meta-data has been generated.
234
  bool HasCapturedVariableLayout : 1;
235
236
  /// Indicates whether an object of a non-external C++ class is captured. This
237
  /// bit is used to determine the linkage of the block copy/destroy helper
238
  /// functions.
239
  bool CapturesNonExternalType : 1;
240
241
  /// The mapping of allocated indexes within the block.
242
  llvm::DenseMap<const VarDecl*, Capture> Captures;
243
244
  Address LocalAddress;
245
  llvm::StructType *StructureType;
246
  const BlockDecl *Block;
247
  const BlockExpr *BlockExpression;
248
  CharUnits BlockSize;
249
  CharUnits BlockAlign;
250
  CharUnits CXXThisOffset;
251
252
  // Offset of the gap caused by block header having a smaller
253
  // alignment than the alignment of the block descriptor. This
254
  // is the gap offset before the first capturued field.
255
  CharUnits BlockHeaderForcedGapOffset;
256
  // Gap size caused by aligning first field after block header.
257
  // This could be zero if no forced alignment is required.
258
  CharUnits BlockHeaderForcedGapSize;
259
260
  /// The next block in the block-info chain.  Invalid if this block
261
  /// info is not part of the CGF's block-info chain, which is true
262
  /// if it corresponds to a global block or a block whose expression
263
  /// has been encountered.
264
  CGBlockInfo *NextBlockInfo;
265
266
18.3k
  const Capture &getCapture(const VarDecl *var) const {
267
18.3k
    return const_cast<CGBlockInfo*>(this)->getCapture(var);
268
18.3k
  }
269
18.3k
  Capture &getCapture(const VarDecl *var) {
270
18.3k
    llvm::DenseMap<const VarDecl*, Capture>::iterator
271
18.3k
      it = Captures.find(var);
272
18.3k
    assert(it != Captures.end() && "no entry for variable!");
273
18.3k
    return it->second;
274
18.3k
  }
275
276
6.95k
  const BlockDecl *getBlockDecl() const { return Block; }
277
6.62k
  const BlockExpr *getBlockExpr() const {
278
6.62k
    assert(BlockExpression);
279
6.62k
    assert(BlockExpression->getBlockDecl() == Block);
280
6.62k
    return BlockExpression;
281
6.62k
  }
282
283
  CGBlockInfo(const BlockDecl *blockDecl, StringRef Name);
284
285
  // Indicates whether the block needs a custom copy or dispose function.
286
2.28k
  bool needsCopyDisposeHelpers() const {
287
2.28k
    return NeedsCopyDispose && 
!Block->doesNotEscape()1.30k
;
288
2.28k
  }
289
};
290
291
}  // end namespace CodeGen
292
}  // end namespace clang
293
294
#endif