clang 22.0.0git
InitMap.h
Go to the documentation of this file.
1//===----------------------- InitMap.h --------------------------*- 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#ifndef LLVM_CLANG_AST_INTERP_INIT_MAP_H
10#define LLVM_CLANG_AST_INTERP_INIT_MAP_H
11
12#include <cassert>
13#include <climits>
14#include <memory>
15#include <cstdint>
16
17namespace clang {
18namespace interp {
19
20/// Bitfield tracking the initialisation status of elements of primitive arrays.
21struct InitMap final {
22private:
23 /// Type packing bits.
24 using T = uint64_t;
25 /// Bits stored in a single field.
26 static constexpr uint64_t PER_FIELD = sizeof(T) * CHAR_BIT;
27 /// Number of fields not initialized.
28 unsigned UninitFields;
29 std::unique_ptr<T[]> Data;
30
31public:
32 /// Initializes the map with no fields set.
33 explicit InitMap(unsigned N);
34
35private:
36 friend class Pointer;
37
38 /// Returns a pointer to storage.
39 T *data() { return Data.get(); }
40 const T *data() const { return Data.get(); }
41
42 /// Initializes an element. Returns true when object if fully initialized.
43 bool initializeElement(unsigned I);
44
45 /// Checks if an element was initialized.
46 bool isElementInitialized(unsigned I) const;
47
48 static constexpr size_t numFields(unsigned N) {
49 return ((N + PER_FIELD - 1) / PER_FIELD) * 2;
50 }
51};
52
53/// A pointer-sized struct we use to allocate into data storage.
54/// An InitMapPtr is either backed by an actual InitMap, or it
55/// hold information about the absence of the InitMap.
56struct InitMapPtr final {
57 /// V's value before an initmap has been created.
58 static constexpr intptr_t NoInitMapValue = 0;
59 /// V's value after the initmap has been destroyed because
60 /// all its elements have already been initialized.
61 static constexpr intptr_t AllInitializedValue = 1;
63
64 explicit InitMapPtr() = default;
65 bool hasInitMap() const {
66 return V != NoInitMapValue && V != AllInitializedValue;
67 }
68 /// Are all elements in the array already initialized?
69 bool allInitialized() const { return V == AllInitializedValue; }
70
71 void setInitMap(const InitMap *IM) {
72 assert(IM != nullptr);
73 V = reinterpret_cast<uintptr_t>(IM);
74 assert(hasInitMap());
75 }
76
78 if (hasInitMap())
79 delete (operator->)();
81 }
82
83 /// Access the underlying InitMap directly.
85 assert(hasInitMap());
86 return reinterpret_cast<InitMap *>(V);
87 }
88
89 /// Delete the InitMap if one exists.
91 if (hasInitMap())
92 delete (operator->)();
94 };
95};
96static_assert(sizeof(InitMapPtr) == sizeof(void *));
97
98} // namespace interp
99} // namespace clang
100
101#endif
#define CHAR_BIT
Definition limits.h:71
The JSON file list parser is used to communicate input to InstallAPI.
const FunctionProtoType * T
__INTPTR_TYPE__ intptr_t
A signed integer type with the property that any valid pointer to void can be converted to this type,...
__UINTPTR_TYPE__ uintptr_t
An unsigned integer type with the property that any valid pointer to void can be converted to this ty...
void deleteInitMap()
Delete the InitMap if one exists.
Definition InitMap.h:90
bool hasInitMap() const
Definition InitMap.h:65
bool allInitialized() const
Are all elements in the array already initialized?
Definition InitMap.h:69
void setInitMap(const InitMap *IM)
Definition InitMap.h:71
static constexpr intptr_t AllInitializedValue
V's value after the initmap has been destroyed because all its elements have already been initialized...
Definition InitMap.h:61
InitMap * operator->()
Access the underlying InitMap directly.
Definition InitMap.h:84
static constexpr intptr_t NoInitMapValue
V's value before an initmap has been created.
Definition InitMap.h:58
Bitfield tracking the initialisation status of elements of primitive arrays.
Definition InitMap.h:21
friend class Pointer
Definition InitMap.h:36
InitMap(unsigned N)
Initializes the map with no fields set.
Definition InitMap.cpp:13