@@ -1548,17 +1548,16 @@ bool OffsetHelper(InterpState &S, CodePtr OpPC, const T &Offset,
1548
1548
if (!CheckArray (S, OpPC, Ptr))
1549
1549
return false ;
1550
1550
1551
- // Get a version of the index comparable to the type.
1552
- T Index = T::from (Ptr.getIndex (), Offset.bitWidth ());
1553
- // Compute the largest index into the array.
1554
- T MaxIndex = T::from (Ptr.getNumElems (), Offset.bitWidth ());
1551
+ uint64_t Index = Ptr.getIndex ();
1552
+ uint64_t MaxIndex = static_cast <uint64_t >(Ptr.getNumElems ());
1555
1553
1556
1554
bool Invalid = false ;
1557
1555
// Helper to report an invalid offset, computed as APSInt.
1558
1556
auto DiagInvalidOffset = [&]() -> void {
1559
1557
const unsigned Bits = Offset.bitWidth ();
1560
- APSInt APOffset (Offset.toAPSInt ().extend (Bits + 2 ), false );
1561
- APSInt APIndex (Index.toAPSInt ().extend (Bits + 2 ), false );
1558
+ APSInt APOffset (Offset.toAPSInt ().extend (Bits + 2 ), /* IsUnsigend=*/ false );
1559
+ APSInt APIndex (APInt (Bits + 2 , Index, /* IsSigned=*/ true ),
1560
+ /* IsUnsigned=*/ false );
1562
1561
APSInt NewIndex =
1563
1562
(Op == ArithOp::Add) ? (APIndex + APOffset) : (APIndex - APOffset);
1564
1563
S.CCEDiag (S.Current ->getSource (OpPC), diag::note_constexpr_array_index)
@@ -1569,22 +1568,24 @@ bool OffsetHelper(InterpState &S, CodePtr OpPC, const T &Offset,
1569
1568
};
1570
1569
1571
1570
if (Ptr.isBlockPointer ()) {
1572
- T MaxOffset = T::from (MaxIndex - Index, Offset.bitWidth ());
1571
+ uint64_t IOffset = static_cast <uint64_t >(Offset);
1572
+ uint64_t MaxOffset = MaxIndex - Index;
1573
+
1573
1574
if constexpr (Op == ArithOp::Add) {
1574
1575
// If the new offset would be negative, bail out.
1575
- if (Offset.isNegative () && (Offset.isMin () || -Offset > Index))
1576
+ if (Offset.isNegative () && (Offset.isMin () || -IOffset > Index))
1576
1577
DiagInvalidOffset ();
1577
1578
1578
1579
// If the new offset would be out of bounds, bail out.
1579
- if (Offset.isPositive () && Offset > MaxOffset)
1580
+ if (Offset.isPositive () && IOffset > MaxOffset)
1580
1581
DiagInvalidOffset ();
1581
1582
} else {
1582
1583
// If the new offset would be negative, bail out.
1583
- if (Offset.isPositive () && Index < Offset )
1584
+ if (Offset.isPositive () && Index < IOffset )
1584
1585
DiagInvalidOffset ();
1585
1586
1586
1587
// If the new offset would be out of bounds, bail out.
1587
- if (Offset.isNegative () && (Offset.isMin () || -Offset > MaxOffset))
1588
+ if (Offset.isNegative () && (Offset.isMin () || -IOffset > MaxOffset))
1588
1589
DiagInvalidOffset ();
1589
1590
}
1590
1591
}
@@ -1601,7 +1602,7 @@ bool OffsetHelper(InterpState &S, CodePtr OpPC, const T &Offset,
1601
1602
else
1602
1603
Result = WideIndex - WideOffset;
1603
1604
1604
- S.Stk .push <Pointer>(Ptr.atIndex (static_cast <unsigned >(Result)));
1605
+ S.Stk .push <Pointer>(Ptr.atIndex (static_cast <uint64_t >(Result)));
1605
1606
return true ;
1606
1607
}
1607
1608
0 commit comments