Timm Baeder 2093d2eea1
[clang][bytecode] Refactor InitMapPtr (#172665)
As mentioned a few times in the past, the previous handling using a
`optional<pair<bool, std::shared_ptr<>>>` was confusing and nobody ever
remembered what the optional being unset meant or what the bool stood
for.

Add an `InitMapPtr` struct that wraps a `uintptr_t` that either holds a
pointer to a valid `InitMap` instance _or_ one of two special values.
The struct has meaningful accessors for the various special cases that
were confusing before.
2025-12-18 10:16:55 +01:00

32 lines
968 B
C++

//===----------------------- InitMap.cpp ------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "InitMap.h"
using namespace clang;
using namespace clang::interp;
InitMap::InitMap(unsigned N)
: UninitFields(N), Data(std::make_unique<T[]>(numFields(N))) {}
bool InitMap::initializeElement(unsigned I) {
unsigned Bucket = I / PER_FIELD;
T Mask = T(1) << (I % PER_FIELD);
if (!(data()[Bucket] & Mask)) {
data()[Bucket] |= Mask;
UninitFields -= 1;
}
return UninitFields == 0;
}
bool InitMap::isElementInitialized(unsigned I) const {
if (UninitFields == 0)
return true;
unsigned Bucket = I / PER_FIELD;
return data()[Bucket] & (T(1) << (I % PER_FIELD));
}