Skip to content

Commit 93aa412

Browse files
committed
[clang][Interp][NFC] Refector OffsetHelper
There was a FIXME comment for this. Stop getting the values in OffsetHelper and let the caller do that instead, so we can control whether the value(s) are removed from the stack at all. Also use ArithOp instead of the unclear boolean for Add.
1 parent c4c5e79 commit 93aa412

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

clang/lib/AST/Interp/Interp.h

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,11 +1082,9 @@ bool InitElemPop(InterpState &S, CodePtr OpPC, uint32_t Idx) {
10821082
// AddOffset, SubOffset
10831083
//===----------------------------------------------------------------------===//
10841084

1085-
template <class T, bool Add> bool OffsetHelper(InterpState &S, CodePtr OpPC) {
1086-
// Fetch the pointer and the offset.
1087-
const T &Offset = S.Stk.pop<T>();
1088-
const Pointer &Ptr = S.Stk.pop<Pointer>();
1089-
1085+
template <class T, ArithOp Op>
1086+
bool OffsetHelper(InterpState &S, CodePtr OpPC, const T &Offset,
1087+
const Pointer &Ptr) {
10901088
if (!CheckRange(S, OpPC, Ptr, CSK_ArrayToPointer))
10911089
return false;
10921090

@@ -1113,7 +1111,8 @@ template <class T, bool Add> bool OffsetHelper(InterpState &S, CodePtr OpPC) {
11131111
const unsigned Bits = Offset.bitWidth();
11141112
APSInt APOffset(Offset.toAPSInt().extend(Bits + 2), false);
11151113
APSInt APIndex(Index.toAPSInt().extend(Bits + 2), false);
1116-
APSInt NewIndex = Add ? (APIndex + APOffset) : (APIndex - APOffset);
1114+
APSInt NewIndex =
1115+
(Op == ArithOp::Add) ? (APIndex + APOffset) : (APIndex - APOffset);
11171116
S.CCEDiag(S.Current->getSource(OpPC), diag::note_constexpr_array_index)
11181117
<< NewIndex
11191118
<< /*array*/ static_cast<int>(!Ptr.inArray())
@@ -1122,7 +1121,7 @@ template <class T, bool Add> bool OffsetHelper(InterpState &S, CodePtr OpPC) {
11221121
};
11231122

11241123
unsigned MaxOffset = MaxIndex - Ptr.getIndex();
1125-
if constexpr (Add) {
1124+
if constexpr (Op == ArithOp::Add) {
11261125
// If the new offset would be negative, bail out.
11271126
if (Offset.isNegative() && (Offset.isMin() || -Offset > Index))
11281127
return InvalidOffset();
@@ -1144,7 +1143,7 @@ template <class T, bool Add> bool OffsetHelper(InterpState &S, CodePtr OpPC) {
11441143
int64_t WideIndex = static_cast<int64_t>(Index);
11451144
int64_t WideOffset = static_cast<int64_t>(Offset);
11461145
int64_t Result;
1147-
if constexpr (Add)
1146+
if constexpr (Op == ArithOp::Add)
11481147
Result = WideIndex + WideOffset;
11491148
else
11501149
Result = WideIndex - WideOffset;
@@ -1155,12 +1154,16 @@ template <class T, bool Add> bool OffsetHelper(InterpState &S, CodePtr OpPC) {
11551154

11561155
template <PrimType Name, class T = typename PrimConv<Name>::T>
11571156
bool AddOffset(InterpState &S, CodePtr OpPC) {
1158-
return OffsetHelper<T, true>(S, OpPC);
1157+
const T &Offset = S.Stk.pop<T>();
1158+
const Pointer &Ptr = S.Stk.pop<Pointer>();
1159+
return OffsetHelper<T, ArithOp::Add>(S, OpPC, Offset, Ptr);
11591160
}
11601161

11611162
template <PrimType Name, class T = typename PrimConv<Name>::T>
11621163
bool SubOffset(InterpState &S, CodePtr OpPC) {
1163-
return OffsetHelper<T, false>(S, OpPC);
1164+
const T &Offset = S.Stk.pop<T>();
1165+
const Pointer &Ptr = S.Stk.pop<Pointer>();
1166+
return OffsetHelper<T, ArithOp::Sub>(S, OpPC, Offset, Ptr);
11641167
}
11651168

11661169
template <ArithOp Op>
@@ -1172,10 +1175,9 @@ static inline bool IncDecPtrHelper(InterpState &S, CodePtr OpPC) {
11721175
S.Stk.push<Pointer>(Ptr.deref<Pointer>());
11731176

11741177
// Now the current Ptr again and a constant 1.
1175-
// FIXME: We shouldn't have to push these two on the stack.
1176-
S.Stk.push<Pointer>(Ptr.deref<Pointer>());
1177-
S.Stk.push<OneT>(OneT::from(1));
1178-
if (!OffsetHelper<OneT, Op == ArithOp::Add>(S, OpPC))
1178+
Pointer P = Ptr.deref<Pointer>();
1179+
OneT One = OneT::from(1);
1180+
if (!OffsetHelper<OneT, Op>(S, OpPC, One, P))
11791181
return false;
11801182

11811183
// Store the new value.

0 commit comments

Comments
 (0)