| 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 |
|
| 17 |
namespace clang { |
| 18 |
namespace interp { |
| 19 |
|
| 20 |
/// Bitfield tracking the initialisation status of elements of primitive arrays. |
| 21 |
struct InitMap final { |
| 22 |
private: |
| 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 |
|
| 31 |
public: |
| 32 |
/// Initializes the map with no fields set. |
| 33 |
explicit InitMap(unsigned N); |
| 34 |
|
| 35 |
private: |
| 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. |
| 56 |
struct 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; |
| 62 |
uintptr_t V = 0; |
| 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 |
|
| 77 |
void noteAllInitialized() { |
| 78 |
if (hasInitMap()) |
| 79 |
delete (operator->)(); |
| 80 |
V = AllInitializedValue; |
| 81 |
} |
| 82 |
|
| 83 |
/// Access the underlying InitMap directly. |
| 84 |
InitMap *operator->() { |
| 85 |
assert(hasInitMap()); |
| 86 |
return reinterpret_cast<InitMap *>(V); |
| 87 |
} |
| 88 |
|
| 89 |
/// Delete the InitMap if one exists. |
| 90 |
void deleteInitMap() { |
| 91 |
if (hasInitMap()) |
| 92 |
delete (operator->)(); |
| 93 |
V = NoInitMapValue; |
| 94 |
}; |
| 95 |
}; |
| 96 |
static_assert(sizeof(InitMapPtr) == sizeof(void *)); |
| 97 |
|
| 98 |
} // namespace interp |
| 99 |
} // namespace clang |
| 100 |
|
| 101 |
#endif |