diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp index ed5493c315dd7..4daab0702f147 100644 --- a/clang/lib/AST/ByteCode/Compiler.cpp +++ b/clang/lib/AST/ByteCode/Compiler.cpp @@ -4798,8 +4798,7 @@ VarCreationState Compiler::visitDecl(const VarDecl *VD, if (!R && Context::shouldBeGloballyIndexed(VD)) { if (auto GlobalIndex = P.getGlobal(VD)) { Block *GlobalBlock = P.getGlobal(*GlobalIndex); - GlobalInlineDescriptor &GD = - *reinterpret_cast(GlobalBlock->rawData()); + auto &GD = GlobalBlock->getBlockDesc(); GD.InitState = GlobalInitState::InitializerFailed; GlobalBlock->invokeDtor(); @@ -4860,8 +4859,7 @@ bool Compiler::visitDeclAndReturn(const VarDecl *VD, const Expr *Init, auto GlobalIndex = P.getGlobal(VD); assert(GlobalIndex); Block *GlobalBlock = P.getGlobal(*GlobalIndex); - GlobalInlineDescriptor &GD = - *reinterpret_cast(GlobalBlock->rawData()); + auto &GD = GlobalBlock->getBlockDesc(); GD.InitState = GlobalInitState::InitializerFailed; GlobalBlock->invokeDtor(); diff --git a/clang/lib/AST/ByteCode/EvalEmitter.cpp b/clang/lib/AST/ByteCode/EvalEmitter.cpp index a2e01efc8ffd9..2ed5147a15491 100644 --- a/clang/lib/AST/ByteCode/EvalEmitter.cpp +++ b/clang/lib/AST/ByteCode/EvalEmitter.cpp @@ -110,7 +110,7 @@ Scope::Local EvalEmitter::createLocal(Descriptor *D) { B->invokeCtor(); // Initialize local variable inline descriptor. - InlineDescriptor &Desc = *reinterpret_cast(B->rawData()); + auto &Desc = B->getBlockDesc(); Desc.Desc = D; Desc.Offset = sizeof(InlineDescriptor); Desc.IsActive = false; @@ -304,7 +304,7 @@ bool EvalEmitter::emitSetLocal(uint32_t I, SourceInfo Info) { Block *B = getLocal(I); *reinterpret_cast(B->data()) = S.Stk.pop(); - InlineDescriptor &Desc = *reinterpret_cast(B->rawData()); + auto &Desc = B->getBlockDesc(); Desc.IsInitialized = true; return true; @@ -327,8 +327,7 @@ bool EvalEmitter::emitGetLocalEnabled(uint32_t I, SourceInfo Info) { return true; Block *B = getLocal(I); - const InlineDescriptor &Desc = - *reinterpret_cast(B->rawData()); + const auto &Desc = B->getBlockDesc(); S.Stk.push(Desc.IsActive); return true; @@ -344,7 +343,7 @@ bool EvalEmitter::emitEnableLocal(uint32_t I, SourceInfo Info) { // probably use a different struct than InlineDescriptor for the block-level // inline descriptor of local varaibles. Block *B = getLocal(I); - InlineDescriptor &Desc = *reinterpret_cast(B->rawData()); + auto &Desc = B->getBlockDesc(); Desc.IsActive = true; return true; } diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp index 80ef656dc6285..889ac1e1a9a7e 100644 --- a/clang/lib/AST/ByteCode/Interp.cpp +++ b/clang/lib/AST/ByteCode/Interp.cpp @@ -736,8 +736,7 @@ static bool CheckWeak(InterpState &S, CodePtr OpPC, const Block *B) { // For example, since those can't be members of structs, they also can't // be mutable. bool CheckGlobalLoad(InterpState &S, CodePtr OpPC, const Block *B) { - const auto &Desc = - *reinterpret_cast(B->rawData()); + const auto &Desc = B->getBlockDesc(); if (!B->isAccessible()) { if (!CheckExtern(S, OpPC, Pointer(const_cast(B)))) return false; diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h index d8b8b209fa927..427f694319b6c 100644 --- a/clang/lib/AST/ByteCode/Interp.h +++ b/clang/lib/AST/ByteCode/Interp.h @@ -1455,8 +1455,7 @@ bool GetGlobal(InterpState &S, CodePtr OpPC, uint32_t I) { template ::T> bool GetGlobalUnchecked(InterpState &S, CodePtr OpPC, uint32_t I) { const Block *B = S.P.getGlobal(I); - const auto &Desc = - *reinterpret_cast(B->rawData()); + const auto &Desc = B->getBlockDesc(); if (Desc.InitState != GlobalInitState::Initialized) return DiagnoseUninitialized(S, OpPC, B->isExtern(), B->getDescriptor(), AK_Read); diff --git a/clang/lib/AST/ByteCode/InterpBlock.h b/clang/lib/AST/ByteCode/InterpBlock.h index 73fdc8d85da11..57f9e7ec3714d 100644 --- a/clang/lib/AST/ByteCode/InterpBlock.h +++ b/clang/lib/AST/ByteCode/InterpBlock.h @@ -122,6 +122,14 @@ class Block final { } template T &deref() { return *reinterpret_cast(data()); } + template T &getBlockDesc() { + assert(sizeof(T) == getDescriptor()->getMetadataSize()); + return *reinterpret_cast(rawData()); + } + template const T &getBlockDesc() const { + return const_cast(this)->getBlockDesc(); + } + /// Invokes the constructor. void invokeCtor() { assert(!IsInitialized); diff --git a/clang/lib/AST/ByteCode/Pointer.cpp b/clang/lib/AST/ByteCode/Pointer.cpp index 00e74db5655d6..90f41bed39440 100644 --- a/clang/lib/AST/ByteCode/Pointer.cpp +++ b/clang/lib/AST/ByteCode/Pointer.cpp @@ -444,8 +444,7 @@ bool Pointer::isInitialized() const { if (isRoot() && BS.Base == sizeof(GlobalInlineDescriptor) && Offset == BS.Base) { - const GlobalInlineDescriptor &GD = - *reinterpret_cast(block()->rawData()); + const auto &GD = block()->getBlockDesc(); return GD.InitState == GlobalInitState::Initialized; } @@ -473,8 +472,7 @@ bool Pointer::isElementInitialized(unsigned Index) const { if (isRoot() && BS.Base == sizeof(GlobalInlineDescriptor) && Offset == BS.Base) { - const GlobalInlineDescriptor &GD = - *reinterpret_cast(block()->rawData()); + const auto &GD = block()->getBlockDesc(); return GD.InitState == GlobalInitState::Initialized; } @@ -499,8 +497,7 @@ void Pointer::initialize() const { if (isRoot() && BS.Base == sizeof(GlobalInlineDescriptor) && Offset == BS.Base) { - GlobalInlineDescriptor &GD = *reinterpret_cast( - asBlockPointer().Pointee->rawData()); + auto &GD = BS.Pointee->getBlockDesc(); GD.InitState = GlobalInitState::Initialized; return; } @@ -565,8 +562,7 @@ bool Pointer::allElementsInitialized() const { if (isRoot() && BS.Base == sizeof(GlobalInlineDescriptor) && Offset == BS.Base) { - const GlobalInlineDescriptor &GD = - *reinterpret_cast(block()->rawData()); + const auto &GD = block()->getBlockDesc(); return GD.InitState == GlobalInitState::Initialized; }