Skip to content

Commit

Permalink
[clang][Interp][NFC] Add type checks to InterpStack::peek()
Browse files Browse the repository at this point in the history
  • Loading branch information
tbaederr committed Jun 30, 2023
1 parent 8554a55 commit c442912
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions clang/lib/AST/Interp/InterpStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class InterpStack final {
assert(ItemTypes.back() == toPrimType<T>());
ItemTypes.pop_back();
#endif
auto *Ptr = &peek<T>();
auto *Ptr = &peekInternal<T>();
auto Value = std::move(*Ptr);
Ptr->~T();
shrink(aligned_size<T>());
Expand All @@ -57,14 +57,18 @@ class InterpStack final {
assert(ItemTypes.back() == toPrimType<T>());
ItemTypes.pop_back();
#endif
auto *Ptr = &peek<T>();
auto *Ptr = &peekInternal<T>();
Ptr->~T();
shrink(aligned_size<T>());
}

/// Returns a reference to the value on the top of the stack.
template <typename T> T &peek() const {
return *reinterpret_cast<T *>(peekData(aligned_size<T>()));
#ifndef NDEBUG
assert(!ItemTypes.empty());
assert(ItemTypes.back() == toPrimType<T>());
#endif
return peekInternal<T>();
}

template <typename T> T &peek(size_t Offset) const {
Expand Down Expand Up @@ -92,6 +96,11 @@ class InterpStack final {
return ((sizeof(T) + PtrAlign - 1) / PtrAlign) * PtrAlign;
}

/// Like the public peek(), but without the debug type checks.
template <typename T> T &peekInternal() const {
return *reinterpret_cast<T *>(peekData(aligned_size<T>()));
}

/// Grows the stack to accommodate a value and returns a pointer to it.
void *grow(size_t Size);
/// Returns a pointer from the top of the stack.
Expand Down

0 comments on commit c442912

Please sign in to comment.