1//===--- Interp.h - Interpreter for the constexpr VM ------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Definition of the interpreter state and entry point.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_AST_INTERP_INTERP_H
14#define LLVM_CLANG_AST_INTERP_INTERP_H
15
16#include "../ExprConstShared.h"
17#include "BitcastBuffer.h"
18#include "Boolean.h"
19#include "Char.h"
20#include "DynamicAllocator.h"
21#include "FixedPoint.h"
22#include "Floating.h"
23#include "Function.h"
24#include "InterpBuiltinBitCast.h"
25#include "InterpFrame.h"
26#include "InterpHelpers.h"
27#include "InterpStack.h"
28#include "InterpState.h"
29#include "MemberPointer.h"
30#include "PrimType.h"
31#include "Program.h"
32#include "Reflect.h"
33#include "State.h"
34#include "clang/AST/ASTContext.h"
35#include "clang/AST/Expr.h"
36#include "llvm/ADT/APFloat.h"
37#include "llvm/ADT/APSInt.h"
38#include "llvm/Support/Compiler.h"
39#include <type_traits>
40
41// preserve_none causes problems when asan is enabled on both AArch64 and other
42// platforms. Disable it until all the bugs are fixed here.
43//
44// See https://github.com/llvm/llvm-project/issues/177519 for AArch64.
45#if !defined(__aarch64__) && !defined(__i386__) && \
46 !__has_feature(address_sanitizer) && \
47 __has_cpp_attribute(clang::preserve_none)
48#define PRESERVE_NONE [[clang::preserve_none]]
49#else
50#define PRESERVE_NONE
51#endif
52
53namespace clang {
54namespace interp {
55
56using APSInt = llvm::APSInt;
57using FixedPointSemantics = llvm::FixedPointSemantics;
58
59/// Checks if a pointer is null.
60bool CheckNull(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
61 CheckSubobjectKind CSK);
62
63/// Checks if Ptr is a one-past-the-end pointer.
64bool CheckSubobject(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
65 CheckSubobjectKind CSK);
66
67/// Checks if a pointer points to const storage.
68bool CheckConst(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
69
70/// Checks if the Descriptor is of a constexpr or const global variable.
71bool CheckConstant(InterpState &S, CodePtr OpPC, const Descriptor *Desc,
72 AccessKinds AK = AK_Read);
73
74bool CheckFinalLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
75
76bool diagnoseUninitialized(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
77 AccessKinds AK);
78
79bool diagnoseArrayIndex(InterpState &S, CodePtr OpPC, const APSInt &Index,
80 std::optional<uint64_t> NumElems = std::nullopt,
81 bool IsArray = true);
82
83/// Checks a direct load of a primitive value from a global or local variable.
84bool CheckGlobalLoad(InterpState &S, CodePtr OpPC, const Block *B);
85bool CheckLocalLoad(InterpState &S, CodePtr OpPC, const Block *B);
86
87/// Checks if a value can be stored in a block.
88bool CheckStore(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
89 AccessKinds AK = AK_Assign, bool WillBeActivated = false);
90
91/// Checks if a value can be initialized.
92bool CheckInit(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
93
94/// Checks the 'this' pointer.
95bool CheckThis(InterpState &S, CodePtr OpPC);
96
97/// Checks if dynamic memory allocation is available in the current
98/// language mode.
99bool CheckDynamicMemoryAllocation(InterpState &S, CodePtr OpPC);
100
101/// Check the source of the pointer passed to delete/delete[] has actually
102/// been heap allocated by us.
103bool CheckDeleteSource(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
104
105/// Sets the given integral value to the pointer, which is of
106/// a std::{weak,partial,strong}_ordering type.
107bool SetThreeWayComparisonField(InterpState &S, CodePtr OpPC,
108 const Pointer &Ptr, const APSInt &IntValue);
109
110bool CallVar(InterpState &S, CodePtr OpPC, const Function *Func,
111 uint32_t VarArgSize);
112bool Call(InterpState &S, CodePtr OpPC, const Function *Func,
113 uint32_t VarArgSize);
114bool CallVirt(InterpState &S, CodePtr OpPC, const Function *Func,
115 uint32_t VarArgSize);
116bool CallBI(InterpState &S, CodePtr OpPC, const CallExpr *CE,
117 uint32_t BuiltinID);
118bool CallPtr(InterpState &S, CodePtr OpPC, uint32_t ArgSize,
119 const CallExpr *CE);
120bool CheckLiteralType(InterpState &S, CodePtr OpPC, const Type *T);
121bool InvalidShuffleVectorIndex(InterpState &S, CodePtr OpPC, uint32_t Index);
122bool CheckBitCast(InterpState &S, CodePtr OpPC, bool HasIndeterminateBits,
123 bool TargetIsUCharOrByte);
124bool CheckBCPResult(InterpState &S, const Pointer &Ptr);
125bool checkDestructor(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
126bool CheckFunctionDecl(InterpState &S, CodePtr OpPC, const FunctionDecl *FD);
127bool CheckBitCast(InterpState &S, CodePtr OpPC, const Type *TargetType,
128 bool SrcIsVoidPtr);
129bool handleReference(InterpState &S, CodePtr OpPC, Block *B);
130bool InvalidCast(InterpState &S, CodePtr OpPC, CastKind Kind, bool Fatal);
131
132bool handleFixedPointOverflow(InterpState &S, CodePtr OpPC,
133 const FixedPoint &FP);
134
135bool Destroy(InterpState &S, CodePtr OpPC, uint32_t I);
136bool DynamicCast(InterpState &S, CodePtr OpPC, const Type *DestType,
137 bool IsReferenceCast);
138bool CastFloatingIntegralAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth,
139 uint32_t FPOI);
140bool CastFloatingIntegralAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth,
141 uint32_t FPOI);
142
143enum class ShiftDir { Left, Right };
144
145enum class ShiftFailure {
146 NegativeCount,
147 TooLarge,
148 NegativeLeftOperand,
149 DiscardsBits,
150};
151
152LLVM_ATTRIBUTE_NOINLINE bool diagnoseShiftFailure(InterpState &S, CodePtr OpPC,
153 ShiftFailure Failure,
154 const APSInt *Value = nullptr,
155 unsigned Bits = 0);
156
157/// Checks if the shift operation is legal.
158template <ShiftDir Dir, typename LT, typename RT>
159bool CheckShift(InterpState &S, CodePtr OpPC, const LT &LHS, const RT &RHS,
160 unsigned Bits) {
161 if (RHS.isNegative()) {
162 const APSInt Value = RHS.toAPSInt();
163 if (!diagnoseShiftFailure(S, OpPC, Failure: ShiftFailure::NegativeCount, Value: &Value))
164 return false;
165 }
166
167 // C++11 [expr.shift]p1: Shift width must be less than the bit width of
168 // the shifted type.
169 if (Bits > 1 && RHS >= Bits) {
170 const APSInt Value = RHS.toAPSInt();
171 if (!diagnoseShiftFailure(S, OpPC, Failure: ShiftFailure::TooLarge, Value: &Value, Bits))
172 return false;
173 }
174
175 if constexpr (Dir == ShiftDir::Left) {
176 if (LHS.isSigned() && !S.getLangOpts().CPlusPlus20) {
177 // C++11 [expr.shift]p2: A signed left shift must have a non-negative
178 // operand, and must not overflow the corresponding unsigned type.
179 if (LHS.isNegative()) {
180 const APSInt Value = LHS.toAPSInt();
181 if (!diagnoseShiftFailure(S, OpPC, Failure: ShiftFailure::NegativeLeftOperand,
182 Value: &Value))
183 return false;
184 } else if (LHS.toUnsigned().countLeadingZeros() <
185 static_cast<unsigned>(RHS)) {
186 if (!diagnoseShiftFailure(S, OpPC, Failure: ShiftFailure::DiscardsBits))
187 return false;
188 }
189 }
190 }
191
192 // C++2a [expr.shift]p2: [P0907R4]:
193 // E1 << E2 is the unique value congruent to
194 // E1 x 2^E2 module 2^N.
195 return true;
196}
197
198/// Checks if Div/Rem operation on LHS and RHS is valid.
199template <typename T>
200bool CheckDivRem(InterpState &S, CodePtr OpPC, const T &LHS, const T &RHS) {
201
202 if constexpr (isIntegralOrPointer<T>()) {
203 if (!LHS.isNumber() || !RHS.isNumber())
204 return false;
205 }
206
207 if (RHS.isZero()) {
208 const auto *Op = cast<BinaryOperator>(Val: S.Current->getExpr(PC: OpPC));
209 if constexpr (std::is_same_v<T, Floating>) {
210 S.CCEDiag(E: Op, DiagId: diag::note_expr_divide_by_zero)
211 << Op->getRHS()->getSourceRange();
212 return true;
213 }
214
215 S.FFDiag(E: Op, DiagId: diag::note_expr_divide_by_zero)
216 << Op->getRHS()->getSourceRange();
217 return false;
218 }
219
220 if constexpr (!std::is_same_v<T, FixedPoint>) {
221 if (LHS.isSigned() && LHS.isMin() && RHS.isNegative() && RHS.isMinusOne()) {
222 APSInt LHSInt = LHS.toAPSInt();
223 SmallString<32> Trunc;
224 (-LHSInt.extend(width: LHSInt.getBitWidth() + 1)).toString(Str&: Trunc, Radix: 10);
225 const SourceInfo &Loc = S.Current->getSource(PC: OpPC);
226 const Expr *E = S.Current->getExpr(PC: OpPC);
227 S.CCEDiag(SI: Loc, DiagId: diag::note_constexpr_overflow) << Trunc << E->getType();
228 return false;
229 }
230 }
231 return true;
232}
233
234/// Checks if the result of a floating-point operation is valid
235/// in the current context.
236/// Notes:
237/// - CheckFloatStatus is the same as
238/// checkFloatingPointResultForConstantFolding in
239/// clang/lib/AST/ExprConstant.cpp.
240/// - CheckFloatResult will also check if the result is NaN, in addition to
241/// CheckFloatStatus's checks.
242// FIXME: P3899R3 (adopted by WG21 in June 2026) likely makes this interface
243// obsolete.
244// https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2026/p3899r3.html
245// Also see the comment:
246// https://github.com/llvm/llvm-project/pull/213750/changes/2fea01449764e23b84ce6790bc7121d369546192#r3708712572
247bool CheckFloatResult(InterpState &S, CodePtr OpPC, const Floating &Result,
248 APFloat::opStatus Status, FPOptions FPO);
249
250/// Check if the given floating-point evaluation status is allowed for
251/// compile-time constant folding during translation (as opposed to mandatory
252/// constant expression evaluation).
253bool CheckFloatStatus(InterpState &S, CodePtr OpPC, APFloat::opStatus Status,
254 FPOptions FPO);
255
256/// Checks why the given DeclRefExpr is invalid.
257bool CheckDeclRef(InterpState &S, CodePtr OpPC, const DeclRefExpr *DR);
258bool InvalidDeclRef(InterpState &S, CodePtr OpPC, const DeclRefExpr *DR,
259 bool InitializerFailed);
260
261/// DerivedToBaseMemberPointer
262bool CastMemberPtrBasePop(InterpState &S, int32_t Off,
263 const RecordDecl *BaseDecl);
264/// BaseToDerivedMemberPointer
265bool CastMemberPtrDerivedPop(InterpState &S, int32_t Off,
266 const RecordDecl *BaseDecl);
267enum class ArithOp { Add, Sub };
268
269//===----------------------------------------------------------------------===//
270// Returning values
271//===----------------------------------------------------------------------===//
272
273void cleanupAfterFunctionCall(InterpState &S, const Function *Func);
274
275template <PrimType Name, class T = typename PrimConv<Name>::T>
276PRESERVE_NONE bool Ret(InterpState &S) {
277 const T &Ret = S.Stk.pop<T>();
278
279 assert(S.Current);
280#ifndef NDEBUG
281 assert(S.Current->getFrameOffset() == S.Stk.size() && "Invalid frame");
282#endif
283
284 // This only happens via Context::Run().
285 if (S.Current->isBottomFrame())
286 return true;
287
288 cleanupAfterFunctionCall(S, Func: S.Current->getFunction());
289 S.PC = S.Current->getRetPC();
290 S.Stk.push<T>(Ret);
291 return true;
292}
293
294PRESERVE_NONE inline bool RetVoid(InterpState &S) {
295 assert(S.Current);
296#ifndef NDEBUG
297 assert(S.Current->getFrameOffset() == S.Stk.size() && "Invalid frame");
298#endif
299
300 // This only happens via Context::Run().
301 if (S.Current->isBottomFrame())
302 return true;
303
304 cleanupAfterFunctionCall(S, Func: S.Current->getFunction());
305 S.PC = S.Current->getRetPC();
306 return true;
307}
308
309//===----------------------------------------------------------------------===//
310// Add, Sub, Mul
311//===----------------------------------------------------------------------===//
312
313template <typename T, bool (*OpFW)(T, T, unsigned, T *),
314 template <typename U> class OpAP>
315bool AddSubMulHelper(InterpState &S, CodePtr OpPC, unsigned Bits, const T &LHS,
316 const T &RHS) {
317 // Should've been handled before.
318 if constexpr (isIntegralOrPointer<T>()) {
319 assert(LHS.isNumber() && RHS.isNumber());
320 }
321
322 // Fast path - add the numbers with fixed width.
323 T Result;
324 if constexpr (needsAlloc<T>())
325 Result = S.allocAP<T>(LHS.bitWidth());
326
327 if (!OpFW(LHS, RHS, Bits, &Result)) {
328 S.Stk.push<T>(Result);
329 return true;
330 }
331 // If for some reason evaluation continues, use the truncated results.
332 S.Stk.push<T>(Result);
333
334 // Short-circuit fixed-points here since the error handling is easier.
335 if constexpr (std::is_same_v<T, FixedPoint>)
336 return handleFixedPointOverflow(S, OpPC, Result);
337
338 // If wrapping is enabled, the new value is fine.
339 if (S.Current->getExpr(PC: OpPC)->getType().isWrapType())
340 return true;
341
342 // Slow path - compute the result using another bit of precision.
343 APSInt Value = OpAP<APSInt>()(LHS.toAPSInt(Bits), RHS.toAPSInt(Bits));
344
345 // Report undefined behaviour, stopping if required.
346 if (S.checkingForUndefinedBehavior()) {
347 const Expr *E = S.Current->getExpr(PC: OpPC);
348 QualType Type = E->getType();
349 SmallString<32> Trunc;
350 Value.trunc(width: Result.bitWidth())
351 .toString(Trunc, 10, Result.isSigned(), /*formatAsCLiteral=*/false,
352 /*UpperCase=*/true, /*InsertSeparators=*/true);
353 S.report(Loc: E->getExprLoc(), DiagId: diag::warn_integer_constant_overflow)
354 << Trunc << Type << E->getSourceRange();
355 }
356
357 if (!handleOverflow(S, OpPC, SrcValue: Value)) {
358 S.Stk.pop<T>();
359 return false;
360 }
361 return true;
362}
363
364// Add or subtract an integer-thats-actually-a-pointer and one real integer.
365template <typename T, template <typename U> class Op>
366static bool AddSubNonNumber(InterpState &S, CodePtr OpPC, T LHS, T RHS) {
367 assert(!LHS.isNumber() || !RHS.isNumber());
368
369 typename T::ReprT Number;
370 const void *Ptr;
371 typename T::ReprT Offset;
372 IntegralKind Kind;
373 if (LHS.isNumber()) {
374 if (RHS.getKind() == IntegralKind::AddrLabelDiff)
375 return Invalid(S, OpPC);
376
377 Number = static_cast<typename T::ReprT>(LHS);
378 Ptr = RHS.getPtr();
379 Offset = RHS.getOffset();
380 Kind = RHS.getKind();
381 } else {
382 assert(RHS.isNumber());
383 if (LHS.getKind() == IntegralKind::AddrLabelDiff)
384 return Invalid(S, OpPC);
385
386 Number = static_cast<typename T::ReprT>(RHS);
387 Ptr = LHS.getPtr();
388 Offset = LHS.getOffset();
389 Kind = LHS.getKind();
390 }
391
392 S.Stk.push<T>(Kind, Ptr, Op<int32_t>()(Offset, Number));
393 return true;
394}
395
396template <PrimType Name, class T = typename PrimConv<Name>::T>
397bool Add(InterpState &S, CodePtr OpPC) {
398 const T &RHS = S.Stk.pop<T>();
399 const T &LHS = S.Stk.pop<T>();
400 const unsigned Bits = RHS.bitWidth() + 1;
401
402 if constexpr (isIntegralOrPointer<T>()) {
403 if (LHS.isNumber() != RHS.isNumber())
404 return AddSubNonNumber<T, std::plus>(S, OpPC, LHS, RHS);
405 else if (LHS.isNumber() && RHS.isNumber())
406 ; // Fall through to proper addition below.
407 else
408 return false; // Reject everything else.
409 }
410
411 return AddSubMulHelper<T, T::add, std::plus>(S, OpPC, Bits, LHS, RHS);
412}
413
414inline bool Addf(InterpState &S, CodePtr OpPC, uint32_t FPOI) {
415 const Floating &RHS = S.Stk.pop<Floating>();
416 const Floating &LHS = S.Stk.pop<Floating>();
417
418 FPOptions FPO = FPOptions::getFromOpaqueInt(Value: FPOI);
419 Floating Result = S.allocFloat(Sem: LHS.getSemantics());
420 auto Status = Floating::add(A: LHS, B: RHS, RM: getRoundingMode(FPO), R: &Result);
421 S.Stk.push<Floating>(Args&: Result);
422 return CheckFloatResult(S, OpPC, Result, Status, FPO);
423}
424
425template <PrimType Name, class T = typename PrimConv<Name>::T>
426bool Sub(InterpState &S, CodePtr OpPC) {
427 const T &RHS = S.Stk.pop<T>();
428 const T &LHS = S.Stk.pop<T>();
429 const unsigned Bits = RHS.bitWidth() + 1;
430
431 if constexpr (isIntegralOrPointer<T>()) {
432 // Handle (int)&&a - (int)&&b.
433 // Both operands should be integrals that point to labels and the result is
434 // a AddrLabelDiff integral.
435 if (LHS.getKind() == IntegralKind::LabelAddress ||
436 RHS.getKind() == IntegralKind::LabelAddress) {
437 const auto *A = LHS.getKind() == IntegralKind::LabelAddress
438 ? reinterpret_cast<const Expr *>(LHS.getPtr())
439 : nullptr;
440 const auto *B = RHS.getKind() == IntegralKind::LabelAddress
441 ? reinterpret_cast<const Expr *>(RHS.getPtr())
442 : nullptr;
443 if (!isa_and_nonnull<AddrLabelExpr>(A) ||
444 !isa_and_nonnull<AddrLabelExpr>(B))
445 return false;
446 const auto *LHSAddrExpr = cast<AddrLabelExpr>(A);
447 const auto *RHSAddrExpr = cast<AddrLabelExpr>(B);
448
449 if (LHSAddrExpr->getLabel()->getDeclContext() !=
450 RHSAddrExpr->getLabel()->getDeclContext())
451 return Invalid(S, OpPC);
452
453 S.Stk.push<T>(LHSAddrExpr, RHSAddrExpr);
454 return true;
455 }
456
457 if (!LHS.isNumber() && RHS.isNumber())
458 return AddSubNonNumber<T, std::minus>(S, OpPC, LHS, RHS);
459 else if (LHS.isNumber() && RHS.isNumber())
460 ; // Fall through to proper addition below.
461 else
462 return false; // Reject everything else.
463 }
464
465 return AddSubMulHelper<T, T::sub, std::minus>(S, OpPC, Bits, LHS, RHS);
466}
467
468inline bool Subf(InterpState &S, CodePtr OpPC, uint32_t FPOI) {
469 const Floating &RHS = S.Stk.pop<Floating>();
470 const Floating &LHS = S.Stk.pop<Floating>();
471
472 FPOptions FPO = FPOptions::getFromOpaqueInt(Value: FPOI);
473 Floating Result = S.allocFloat(Sem: LHS.getSemantics());
474 auto Status = Floating::sub(A: LHS, B: RHS, RM: getRoundingMode(FPO), R: &Result);
475 S.Stk.push<Floating>(Args&: Result);
476 return CheckFloatResult(S, OpPC, Result, Status, FPO);
477}
478
479template <PrimType Name, class T = typename PrimConv<Name>::T>
480bool Mul(InterpState &S, CodePtr OpPC) {
481 const T &RHS = S.Stk.pop<T>();
482 const T &LHS = S.Stk.pop<T>();
483 const unsigned Bits = RHS.bitWidth() * 2;
484
485 if constexpr (isIntegralOrPointer<T>()) {
486 if (!LHS.isNumber() || !RHS.isNumber())
487 return Invalid(S, OpPC);
488 }
489
490 return AddSubMulHelper<T, T::mul, std::multiplies>(S, OpPC, Bits, LHS, RHS);
491}
492
493inline bool Mulf(InterpState &S, CodePtr OpPC, uint32_t FPOI) {
494 const Floating &RHS = S.Stk.pop<Floating>();
495 const Floating &LHS = S.Stk.pop<Floating>();
496
497 FPOptions FPO = FPOptions::getFromOpaqueInt(Value: FPOI);
498 Floating Result = S.allocFloat(Sem: LHS.getSemantics());
499
500 auto Status = Floating::mul(A: LHS, B: RHS, RM: getRoundingMode(FPO), R: &Result);
501
502 S.Stk.push<Floating>(Args&: Result);
503 return CheckFloatResult(S, OpPC, Result, Status, FPO);
504}
505
506template <PrimType Name, class T = typename PrimConv<Name>::T>
507inline bool Mulc(InterpState &S) {
508 const Pointer &RHS = S.Stk.pop<Pointer>();
509 const Pointer &LHS = S.Stk.pop<Pointer>();
510 const Pointer &Result = S.Stk.peek<Pointer>();
511
512 if constexpr (std::is_same_v<T, Floating>) {
513 APFloat A = LHS.elem<Floating>(I: 0).getAPFloat();
514 APFloat B = LHS.elem<Floating>(I: 1).getAPFloat();
515 APFloat C = RHS.elem<Floating>(I: 0).getAPFloat();
516 APFloat D = RHS.elem<Floating>(I: 1).getAPFloat();
517
518 APFloat ResR(A.getSemantics());
519 APFloat ResI(A.getSemantics());
520 HandleComplexComplexMul(A, B, C, D, ResR, ResI);
521
522 // Copy into the result.
523 Floating RA = S.allocFloat(Sem: A.getSemantics());
524 RA.copy(F: ResR);
525 Result.elem<Floating>(I: 0) = RA; // Floating(ResR);
526
527 Floating RI = S.allocFloat(Sem: A.getSemantics());
528 RI.copy(F: ResI);
529 Result.elem<Floating>(I: 1) = RI; // Floating(ResI);
530 Result.initializeAllElements();
531 } else {
532 // Integer element type.
533 const T &LHSR = LHS.elem<T>(0);
534 const T &LHSI = LHS.elem<T>(1);
535 const T &RHSR = RHS.elem<T>(0);
536 const T &RHSI = RHS.elem<T>(1);
537 unsigned Bits = LHSR.bitWidth();
538
539 // We only handle actual numbers here.
540 if (!LHSR.isNumber() || !LHSI.isNumber() || !RHSR.isNumber() ||
541 !RHSI.isNumber())
542 return false;
543
544 // real(Result) = (real(LHS) * real(RHS)) - (imag(LHS) * imag(RHS))
545 T A;
546 if constexpr (needsAlloc<T>())
547 A = S.allocAP<T>(Bits);
548 if (T::mul(LHSR, RHSR, Bits, &A))
549 return false;
550
551 T B;
552 if constexpr (needsAlloc<T>())
553 B = S.allocAP<T>(Bits);
554 if (T::mul(LHSI, RHSI, Bits, &B))
555 return false;
556
557 if constexpr (needsAlloc<T>())
558 Result.elem<T>(0) = S.allocAP<T>(Bits);
559 if (T::sub(A, B, Bits, &Result.elem<T>(0)))
560 return false;
561
562 // imag(Result) = (real(LHS) * imag(RHS)) + (imag(LHS) * real(RHS))
563 if (T::mul(LHSR, RHSI, Bits, &A))
564 return false;
565 if (T::mul(LHSI, RHSR, Bits, &B))
566 return false;
567
568 if constexpr (needsAlloc<T>())
569 Result.elem<T>(1) = S.allocAP<T>(Bits);
570 if (T::add(A, B, Bits, &Result.elem<T>(1)))
571 return false;
572 Result.initialize();
573 Result.initializeAllElements();
574 }
575
576 return true;
577}
578
579template <PrimType Name, class T = typename PrimConv<Name>::T>
580inline bool Divc(InterpState &S, CodePtr OpPC) {
581 const Pointer &RHS = S.Stk.pop<Pointer>();
582 const Pointer &LHS = S.Stk.pop<Pointer>();
583 const Pointer &Result = S.Stk.peek<Pointer>();
584
585 if constexpr (std::is_same_v<T, Floating>) {
586 APFloat A = LHS.elem<Floating>(I: 0).getAPFloat();
587 APFloat B = LHS.elem<Floating>(I: 1).getAPFloat();
588 APFloat C = RHS.elem<Floating>(I: 0).getAPFloat();
589 APFloat D = RHS.elem<Floating>(I: 1).getAPFloat();
590
591 APFloat ResR(A.getSemantics());
592 APFloat ResI(A.getSemantics());
593 HandleComplexComplexDiv(A, B, C, D, ResR, ResI);
594
595 // Copy into the result.
596 Floating RA = S.allocFloat(Sem: A.getSemantics());
597 RA.copy(F: ResR);
598 Result.elem<Floating>(I: 0) = RA; // Floating(ResR);
599
600 Floating RI = S.allocFloat(Sem: A.getSemantics());
601 RI.copy(F: ResI);
602 Result.elem<Floating>(I: 1) = RI; // Floating(ResI);
603
604 Result.initializeAllElements();
605 } else {
606 // Integer element type.
607 const T &LHSR = LHS.elem<T>(0);
608 const T &LHSI = LHS.elem<T>(1);
609 const T &RHSR = RHS.elem<T>(0);
610 const T &RHSI = RHS.elem<T>(1);
611 unsigned Bits = LHSR.bitWidth();
612
613 if (RHSR.isZero() && RHSI.isZero()) {
614 const SourceInfo &E = S.Current->getSource(PC: OpPC);
615 S.FFDiag(SI: E, DiagId: diag::note_expr_divide_by_zero);
616 return false;
617 }
618
619 // Den = real(RHS)² + imag(RHS)²
620 T A, B;
621 if constexpr (needsAlloc<T>()) {
622 A = S.allocAP<T>(Bits);
623 B = S.allocAP<T>(Bits);
624 }
625
626 if (T::mul(RHSR, RHSR, Bits, &A) || T::mul(RHSI, RHSI, Bits, &B)) {
627 // Ignore overflow here, because that's what the current interpeter does.
628 }
629 T Den;
630 if constexpr (needsAlloc<T>())
631 Den = S.allocAP<T>(Bits);
632
633 if (T::add(A, B, Bits, &Den))
634 return false;
635
636 if (Den.isZero()) {
637 const SourceInfo &E = S.Current->getSource(PC: OpPC);
638 S.FFDiag(SI: E, DiagId: diag::note_expr_divide_by_zero);
639 return false;
640 }
641
642 // real(Result) = ((real(LHS) * real(RHS)) + (imag(LHS) * imag(RHS))) / Den
643 T &ResultR = Result.elem<T>(0);
644 T &ResultI = Result.elem<T>(1);
645 if constexpr (needsAlloc<T>()) {
646 ResultR = S.allocAP<T>(Bits);
647 ResultI = S.allocAP<T>(Bits);
648 }
649 if (T::mul(LHSR, RHSR, Bits, &A) || T::mul(LHSI, RHSI, Bits, &B))
650 return false;
651 if (T::add(A, B, Bits, &ResultR))
652 return false;
653 if (T::div(ResultR, Den, Bits, &ResultR))
654 return false;
655
656 // imag(Result) = ((imag(LHS) * real(RHS)) - (real(LHS) * imag(RHS))) / Den
657 if (T::mul(LHSI, RHSR, Bits, &A) || T::mul(LHSR, RHSI, Bits, &B))
658 return false;
659 if (T::sub(A, B, Bits, &ResultI))
660 return false;
661 if (T::div(ResultI, Den, Bits, &ResultI))
662 return false;
663 Result.initializeAllElements();
664 }
665
666 return true;
667}
668
669/// 1) Pops the RHS from the stack.
670/// 2) Pops the LHS from the stack.
671/// 3) Pushes 'LHS & RHS' on the stack
672template <PrimType Name, class T = typename PrimConv<Name>::T>
673bool BitAnd(InterpState &S) {
674 const T &RHS = S.Stk.pop<T>();
675 const T &LHS = S.Stk.pop<T>();
676 unsigned Bits = RHS.bitWidth();
677
678 if constexpr (isIntegralOrPointer<T>()) {
679 if (!LHS.isNumber() || !RHS.isNumber())
680 return false;
681 }
682
683 T Result;
684 if constexpr (needsAlloc<T>())
685 Result = S.allocAP<T>(Bits);
686
687 if (!T::bitAnd(LHS, RHS, Bits, &Result)) {
688 S.Stk.push<T>(Result);
689 return true;
690 }
691 return false;
692}
693
694/// 1) Pops the RHS from the stack.
695/// 2) Pops the LHS from the stack.
696/// 3) Pushes 'LHS | RHS' on the stack
697template <PrimType Name, class T = typename PrimConv<Name>::T>
698bool BitOr(InterpState &S) {
699 const T &RHS = S.Stk.pop<T>();
700 const T &LHS = S.Stk.pop<T>();
701 unsigned Bits = RHS.bitWidth();
702
703 if constexpr (isIntegralOrPointer<T>()) {
704 if (!LHS.isNumber() || !RHS.isNumber())
705 return false;
706 }
707
708 T Result;
709 if constexpr (needsAlloc<T>())
710 Result = S.allocAP<T>(Bits);
711
712 if (!T::bitOr(LHS, RHS, Bits, &Result)) {
713 S.Stk.push<T>(Result);
714 return true;
715 }
716 return false;
717}
718
719/// 1) Pops the RHS from the stack.
720/// 2) Pops the LHS from the stack.
721/// 3) Pushes 'LHS ^ RHS' on the stack
722template <PrimType Name, class T = typename PrimConv<Name>::T>
723bool BitXor(InterpState &S) {
724 const T &RHS = S.Stk.pop<T>();
725 const T &LHS = S.Stk.pop<T>();
726 unsigned Bits = RHS.bitWidth();
727
728 if constexpr (isIntegralOrPointer<T>()) {
729 if (!LHS.isNumber() || !RHS.isNumber())
730 return false;
731 }
732
733 T Result;
734 if constexpr (needsAlloc<T>())
735 Result = S.allocAP<T>(Bits);
736
737 if (!T::bitXor(LHS, RHS, Bits, &Result)) {
738 S.Stk.push<T>(Result);
739 return true;
740 }
741 return false;
742}
743
744/// 1) Pops the RHS from the stack.
745/// 2) Pops the LHS from the stack.
746/// 3) Pushes 'LHS % RHS' on the stack (the remainder of dividing LHS by RHS).
747template <PrimType Name, class T = typename PrimConv<Name>::T>
748bool Rem(InterpState &S, CodePtr OpPC) {
749 const T &RHS = S.Stk.pop<T>();
750 const T &LHS = S.Stk.pop<T>();
751 const unsigned Bits = RHS.bitWidth() * 2;
752
753 if (!CheckDivRem(S, OpPC, LHS, RHS))
754 return false;
755
756 T Result;
757 if constexpr (needsAlloc<T>())
758 Result = S.allocAP<T>(LHS.bitWidth());
759
760 if (!T::rem(LHS, RHS, Bits, &Result)) {
761 S.Stk.push<T>(Result);
762 return true;
763 }
764 return false;
765}
766
767/// 1) Pops the RHS from the stack.
768/// 2) Pops the LHS from the stack.
769/// 3) Pushes 'LHS / RHS' on the stack
770template <PrimType Name, class T = typename PrimConv<Name>::T>
771bool Div(InterpState &S, CodePtr OpPC) {
772 const T &RHS = S.Stk.pop<T>();
773 const T &LHS = S.Stk.pop<T>();
774 const unsigned Bits = RHS.bitWidth() * 2;
775
776 if (!CheckDivRem(S, OpPC, LHS, RHS))
777 return false;
778
779 T Result;
780 if constexpr (needsAlloc<T>())
781 Result = S.allocAP<T>(LHS.bitWidth());
782
783 if (!T::div(LHS, RHS, Bits, &Result)) {
784 S.Stk.push<T>(Result);
785 return true;
786 }
787
788 if constexpr (std::is_same_v<T, FixedPoint>) {
789 if (handleFixedPointOverflow(S, OpPC, Result)) {
790 S.Stk.push<T>(Result);
791 return true;
792 }
793 }
794 return false;
795}
796
797inline bool Divf(InterpState &S, CodePtr OpPC, uint32_t FPOI) {
798 const Floating &RHS = S.Stk.pop<Floating>();
799 const Floating &LHS = S.Stk.pop<Floating>();
800
801 if (!CheckDivRem(S, OpPC, LHS, RHS))
802 return false;
803
804 FPOptions FPO = FPOptions::getFromOpaqueInt(Value: FPOI);
805
806 Floating Result = S.allocFloat(Sem: LHS.getSemantics());
807 auto Status = Floating::div(A: LHS, B: RHS, RM: getRoundingMode(FPO), R: &Result);
808
809 S.Stk.push<Floating>(Args&: Result);
810 return CheckFloatResult(S, OpPC, Result, Status, FPO);
811}
812
813//===----------------------------------------------------------------------===//
814// Inv
815//===----------------------------------------------------------------------===//
816
817inline bool Inv(InterpState &S) {
818 const auto &Val = S.Stk.pop<Boolean>();
819 S.Stk.push<Boolean>(Args: !Val);
820 return true;
821}
822
823//===----------------------------------------------------------------------===//
824// Neg
825//===----------------------------------------------------------------------===//
826
827template <PrimType Name, class T = typename PrimConv<Name>::T>
828bool Neg(InterpState &S, CodePtr OpPC) {
829 const T &Value = S.Stk.pop<T>();
830
831 if constexpr (std::is_same_v<T, Floating>) {
832 T Result = S.allocFloat(Sem: Value.getSemantics());
833
834 if (!T::neg(Value, &Result)) {
835 S.Stk.push<T>(Result);
836 return true;
837 }
838 return false;
839 } else {
840 T Result;
841 if constexpr (needsAlloc<T>())
842 Result = S.allocAP<T>(Value.bitWidth());
843
844 if (!T::neg(Value, &Result)) {
845 S.Stk.push<T>(Result);
846 return true;
847 }
848
849 assert((isIntegerType(Name) || Name == PT_FixedPoint) &&
850 "don't expect other types to fail at constexpr negation");
851 S.Stk.push<T>(Result);
852
853 if (S.Current->getExpr(PC: OpPC)->getType().isWrapType())
854 return true;
855
856 APSInt NegatedValue = -Value.toAPSInt(Value.bitWidth() + 1);
857 if (S.checkingForUndefinedBehavior()) {
858 const Expr *E = S.Current->getExpr(PC: OpPC);
859 QualType Type = E->getType();
860 SmallString<32> Trunc;
861 NegatedValue.trunc(width: Result.bitWidth())
862 .toString(Trunc, 10, Result.isSigned(), /*formatAsCLiteral=*/false,
863 /*UpperCase=*/true, /*InsertSeparators=*/true);
864 S.report(Loc: E->getExprLoc(), DiagId: diag::warn_integer_constant_overflow)
865 << Trunc << Type << E->getSourceRange();
866 return true;
867 }
868
869 return handleOverflow(S, OpPC, SrcValue: NegatedValue);
870 }
871}
872
873enum class PushVal : bool {
874 No,
875 Yes,
876};
877enum class IncDecOp {
878 Inc,
879 Dec,
880};
881
882template <typename T, IncDecOp Op, PushVal DoPush>
883bool IncDecHelper(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
884 bool CanOverflow, UnsignedOrNone BitWidth = std::nullopt) {
885 assert(!Ptr.isDummy());
886
887 if constexpr (std::is_same_v<T, Boolean>) {
888 if (!S.getLangOpts().CPlusPlus14)
889 return Invalid(S, OpPC);
890 }
891
892 const T &Value = Ptr.deref<T>();
893
894 // Can't inc/dec non-numbers.
895 if constexpr (isIntegralOrPointer<T>()) {
896 if (!Value.isNumber())
897 return false;
898 }
899
900 T Result;
901 if constexpr (needsAlloc<T>())
902 Result = S.allocAP<T>(Value.bitWidth());
903
904 if constexpr (DoPush == PushVal::Yes)
905 S.Stk.push<T>(Value);
906
907 if constexpr (Op == IncDecOp::Inc) {
908 if (!T::increment(Value, &Result) || !CanOverflow) {
909 if (BitWidth)
910 Ptr.deref<T>() = Result.truncate(*BitWidth);
911 else
912 Ptr.deref<T>() = Result;
913 return true;
914 }
915 } else {
916 if (!T::decrement(Value, &Result) || !CanOverflow) {
917 if (BitWidth)
918 Ptr.deref<T>() = Result.truncate(*BitWidth);
919 else
920 Ptr.deref<T>() = Result;
921 return true;
922 }
923 }
924 assert(CanOverflow);
925
926 if (S.Current->getExpr(PC: OpPC)->getType().isWrapType()) {
927 Ptr.deref<T>() = Result;
928 return true;
929 }
930
931 // Something went wrong with the previous operation. Compute the
932 // result with another bit of precision.
933 unsigned Bits = Value.bitWidth() + 1;
934 APSInt APResult;
935 if constexpr (Op == IncDecOp::Inc)
936 APResult = ++Value.toAPSInt(Bits);
937 else
938 APResult = --Value.toAPSInt(Bits);
939
940 // Report undefined behaviour, stopping if required.
941 if (S.checkingForUndefinedBehavior()) {
942 const Expr *E = S.Current->getExpr(PC: OpPC);
943 QualType Type = E->getType();
944 SmallString<32> Trunc;
945 APResult.trunc(width: Result.bitWidth())
946 .toString(Trunc, 10, Result.isSigned(), /*formatAsCLiteral=*/false,
947 /*UpperCase=*/true, /*InsertSeparators=*/true);
948 S.report(Loc: E->getExprLoc(), DiagId: diag::warn_integer_constant_overflow)
949 << Trunc << Type << E->getSourceRange();
950 return true;
951 }
952 return handleOverflow(S, OpPC, SrcValue: APResult);
953}
954
955/// 1) Pops a pointer from the stack
956/// 2) Load the value from the pointer
957/// 3) Writes the value increased by one back to the pointer
958/// 4) Pushes the original (pre-inc) value on the stack.
959template <PrimType Name, class T = typename PrimConv<Name>::T>
960bool Inc(InterpState &S, CodePtr OpPC, bool CanOverflow) {
961 const Pointer &Ptr = S.Stk.pop<Pointer>();
962 if (!CheckLoad(S, OpPC, Ptr, AK: AK_Increment))
963 return false;
964 if (!CheckConst(S, OpPC, Ptr))
965 return false;
966
967 return IncDecHelper<T, IncDecOp::Inc, PushVal::Yes>(S, OpPC, Ptr,
968 CanOverflow);
969}
970
971template <PrimType Name, class T = typename PrimConv<Name>::T>
972bool IncBitfield(InterpState &S, CodePtr OpPC, bool CanOverflow,
973 unsigned BitWidth) {
974 const Pointer &Ptr = S.Stk.pop<Pointer>();
975 if (!CheckLoad(S, OpPC, Ptr, AK: AK_Increment))
976 return false;
977 if (!CheckConst(S, OpPC, Ptr))
978 return false;
979
980 return IncDecHelper<T, IncDecOp::Inc, PushVal::Yes>(S, OpPC, Ptr, CanOverflow,
981 BitWidth);
982}
983
984/// 1) Pops a pointer from the stack
985/// 2) Load the value from the pointer
986/// 3) Writes the value increased by one back to the pointer
987template <PrimType Name, class T = typename PrimConv<Name>::T>
988bool IncPop(InterpState &S, CodePtr OpPC, bool CanOverflow) {
989 const Pointer &Ptr = S.Stk.pop<Pointer>();
990 if (!CheckLoad(S, OpPC, Ptr, AK: AK_Increment))
991 return false;
992 if (!CheckConst(S, OpPC, Ptr))
993 return false;
994
995 return IncDecHelper<T, IncDecOp::Inc, PushVal::No>(S, OpPC, Ptr, CanOverflow);
996}
997
998template <PrimType Name, class T = typename PrimConv<Name>::T>
999bool IncPopBitfield(InterpState &S, CodePtr OpPC, bool CanOverflow,
1000 uint32_t BitWidth) {
1001 const Pointer &Ptr = S.Stk.pop<Pointer>();
1002 if (!CheckLoad(S, OpPC, Ptr, AK: AK_Increment))
1003 return false;
1004 if (!CheckConst(S, OpPC, Ptr))
1005 return false;
1006
1007 return IncDecHelper<T, IncDecOp::Inc, PushVal::No>(S, OpPC, Ptr, CanOverflow,
1008 BitWidth);
1009}
1010
1011template <PrimType Name, class T = typename PrimConv<Name>::T>
1012bool PreInc(InterpState &S, CodePtr OpPC, bool CanOverflow) {
1013 const Pointer &Ptr = S.Stk.peek<Pointer>();
1014 if (!CheckLoad(S, OpPC, Ptr, AK: AK_Increment))
1015 return false;
1016 if (!CheckConst(S, OpPC, Ptr))
1017 return false;
1018
1019 return IncDecHelper<T, IncDecOp::Inc, PushVal::No>(S, OpPC, Ptr, CanOverflow);
1020}
1021
1022template <PrimType Name, class T = typename PrimConv<Name>::T>
1023bool PreIncBitfield(InterpState &S, CodePtr OpPC, bool CanOverflow,
1024 uint32_t BitWidth) {
1025 const Pointer &Ptr = S.Stk.peek<Pointer>();
1026 if (!CheckLoad(S, OpPC, Ptr, AK: AK_Increment))
1027 return false;
1028 if (!CheckConst(S, OpPC, Ptr))
1029 return false;
1030
1031 return IncDecHelper<T, IncDecOp::Inc, PushVal::No>(S, OpPC, Ptr, CanOverflow,
1032 BitWidth);
1033}
1034
1035/// 1) Pops a pointer from the stack
1036/// 2) Load the value from the pointer
1037/// 3) Writes the value decreased by one back to the pointer
1038/// 4) Pushes the original (pre-dec) value on the stack.
1039template <PrimType Name, class T = typename PrimConv<Name>::T>
1040bool Dec(InterpState &S, CodePtr OpPC, bool CanOverflow) {
1041 const Pointer &Ptr = S.Stk.pop<Pointer>();
1042 if (!CheckLoad(S, OpPC, Ptr, AK: AK_Decrement))
1043 return false;
1044 if (!CheckConst(S, OpPC, Ptr))
1045 return false;
1046
1047 return IncDecHelper<T, IncDecOp::Dec, PushVal::Yes>(S, OpPC, Ptr,
1048 CanOverflow);
1049}
1050template <PrimType Name, class T = typename PrimConv<Name>::T>
1051bool DecBitfield(InterpState &S, CodePtr OpPC, bool CanOverflow,
1052 uint32_t BitWidth) {
1053 const Pointer &Ptr = S.Stk.pop<Pointer>();
1054 if (!CheckLoad(S, OpPC, Ptr, AK: AK_Decrement))
1055 return false;
1056 if (!CheckConst(S, OpPC, Ptr))
1057 return false;
1058
1059 return IncDecHelper<T, IncDecOp::Dec, PushVal::Yes>(S, OpPC, Ptr, CanOverflow,
1060 BitWidth);
1061}
1062
1063/// 1) Pops a pointer from the stack
1064/// 2) Load the value from the pointer
1065/// 3) Writes the value decreased by one back to the pointer
1066template <PrimType Name, class T = typename PrimConv<Name>::T>
1067bool DecPop(InterpState &S, CodePtr OpPC, bool CanOverflow) {
1068 const Pointer &Ptr = S.Stk.pop<Pointer>();
1069 if (!CheckLoad(S, OpPC, Ptr, AK: AK_Decrement))
1070 return false;
1071 if (!CheckConst(S, OpPC, Ptr))
1072 return false;
1073
1074 return IncDecHelper<T, IncDecOp::Dec, PushVal::No>(S, OpPC, Ptr, CanOverflow);
1075}
1076
1077template <PrimType Name, class T = typename PrimConv<Name>::T>
1078bool DecPopBitfield(InterpState &S, CodePtr OpPC, bool CanOverflow,
1079 uint32_t BitWidth) {
1080 const Pointer &Ptr = S.Stk.pop<Pointer>();
1081 if (!CheckLoad(S, OpPC, Ptr, AK: AK_Decrement))
1082 return false;
1083 if (!CheckConst(S, OpPC, Ptr))
1084 return false;
1085
1086 return IncDecHelper<T, IncDecOp::Dec, PushVal::No>(S, OpPC, Ptr, CanOverflow,
1087 BitWidth);
1088}
1089
1090template <PrimType Name, class T = typename PrimConv<Name>::T>
1091bool PreDec(InterpState &S, CodePtr OpPC, bool CanOverflow) {
1092 const Pointer &Ptr = S.Stk.peek<Pointer>();
1093 if (!CheckLoad(S, OpPC, Ptr, AK: AK_Decrement))
1094 return false;
1095 if (!CheckConst(S, OpPC, Ptr))
1096 return false;
1097 return IncDecHelper<T, IncDecOp::Dec, PushVal::No>(S, OpPC, Ptr, CanOverflow);
1098}
1099
1100template <PrimType Name, class T = typename PrimConv<Name>::T>
1101bool PreDecBitfield(InterpState &S, CodePtr OpPC, bool CanOverflow,
1102 uint32_t BitWidth) {
1103 const Pointer &Ptr = S.Stk.peek<Pointer>();
1104 if (!CheckLoad(S, OpPC, Ptr, AK: AK_Decrement))
1105 return false;
1106 if (!CheckConst(S, OpPC, Ptr))
1107 return false;
1108 return IncDecHelper<T, IncDecOp::Dec, PushVal::No>(S, OpPC, Ptr, CanOverflow,
1109 BitWidth);
1110}
1111
1112template <IncDecOp Op, PushVal DoPush>
1113bool IncDecFloatHelper(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
1114 uint32_t FPOI) {
1115 Floating Value = Ptr.deref<Floating>();
1116 Floating Result = S.allocFloat(Sem: Value.getSemantics());
1117
1118 if constexpr (DoPush == PushVal::Yes)
1119 S.Stk.push<Floating>(Args&: Value);
1120
1121 FPOptions FPO = FPOptions::getFromOpaqueInt(Value: FPOI);
1122 llvm::APFloat::opStatus Status;
1123 if constexpr (Op == IncDecOp::Inc)
1124 Status = Floating::increment(A: Value, RM: getRoundingMode(FPO), R: &Result);
1125 else
1126 Status = Floating::decrement(A: Value, RM: getRoundingMode(FPO), R: &Result);
1127
1128 Ptr.deref<Floating>() = Result;
1129
1130 return CheckFloatResult(S, OpPC, Result, Status, FPO);
1131}
1132
1133inline bool Incf(InterpState &S, CodePtr OpPC, uint32_t FPOI) {
1134 const Pointer &Ptr = S.Stk.pop<Pointer>();
1135 if (!CheckLoad(S, OpPC, Ptr, AK: AK_Increment))
1136 return false;
1137 if (!CheckConst(S, OpPC, Ptr))
1138 return false;
1139
1140 return IncDecFloatHelper<IncDecOp::Inc, PushVal::Yes>(S, OpPC, Ptr, FPOI);
1141}
1142
1143inline bool IncfPop(InterpState &S, CodePtr OpPC, uint32_t FPOI) {
1144 const Pointer &Ptr = S.Stk.pop<Pointer>();
1145 if (!CheckLoad(S, OpPC, Ptr, AK: AK_Increment))
1146 return false;
1147 if (!CheckConst(S, OpPC, Ptr))
1148 return false;
1149
1150 return IncDecFloatHelper<IncDecOp::Inc, PushVal::No>(S, OpPC, Ptr, FPOI);
1151}
1152
1153inline bool Decf(InterpState &S, CodePtr OpPC, uint32_t FPOI) {
1154 const Pointer &Ptr = S.Stk.pop<Pointer>();
1155 if (!CheckLoad(S, OpPC, Ptr, AK: AK_Decrement))
1156 return false;
1157 if (!CheckConst(S, OpPC, Ptr))
1158 return false;
1159
1160 return IncDecFloatHelper<IncDecOp::Dec, PushVal::Yes>(S, OpPC, Ptr, FPOI);
1161}
1162
1163inline bool DecfPop(InterpState &S, CodePtr OpPC, uint32_t FPOI) {
1164 const Pointer &Ptr = S.Stk.pop<Pointer>();
1165 if (!CheckLoad(S, OpPC, Ptr, AK: AK_Decrement))
1166 return false;
1167 if (!CheckConst(S, OpPC, Ptr))
1168 return false;
1169
1170 return IncDecFloatHelper<IncDecOp::Dec, PushVal::No>(S, OpPC, Ptr, FPOI);
1171}
1172
1173/// 1) Pops the value from the stack.
1174/// 2) Pushes the bitwise complemented value on the stack (~V).
1175template <PrimType Name, class T = typename PrimConv<Name>::T>
1176bool Comp(InterpState &S) {
1177 const T &Val = S.Stk.pop<T>();
1178
1179 T Result;
1180 if constexpr (needsAlloc<T>())
1181 Result = S.allocAP<T>(Val.bitWidth());
1182
1183 if (!T::comp(Val, &Result)) {
1184 S.Stk.push<T>(Result);
1185 return true;
1186 }
1187 return false;
1188}
1189
1190//===----------------------------------------------------------------------===//
1191// EQ, NE, GT, GE, LT, LE
1192//===----------------------------------------------------------------------===//
1193
1194using CompareFn = llvm::function_ref<bool(ComparisonCategoryResult)>;
1195
1196template <typename T>
1197bool CmpHelper(InterpState &S, CodePtr OpPC, CompareFn Fn) {
1198 assert((!std::is_same_v<T, MemberPointer>) &&
1199 "Non-equality comparisons on member pointer types should already be "
1200 "rejected in Sema.");
1201 using BoolT = PrimConv<PT_Bool>::T;
1202 const T &RHS = S.Stk.pop<T>();
1203 const T &LHS = S.Stk.pop<T>();
1204
1205 if constexpr (isIntegralOrPointer<T>()) {
1206 if (!LHS.isNumber() || !RHS.isNumber())
1207 return Invalid(S, OpPC);
1208 }
1209
1210 S.Stk.push<BoolT>(BoolT::from(Fn(LHS.compare(RHS))));
1211 return true;
1212}
1213
1214template <typename T>
1215bool CmpHelperEQ(InterpState &S, CodePtr OpPC, CompareFn Fn) {
1216 return CmpHelper<T>(S, OpPC, Fn);
1217}
1218
1219template <>
1220inline bool CmpHelper<Pointer>(InterpState &S, CodePtr OpPC, CompareFn Fn) {
1221 using BoolT = PrimConv<PT_Bool>::T;
1222 const Pointer &RHS = S.Stk.pop<Pointer>();
1223 const Pointer &LHS = S.Stk.pop<Pointer>();
1224
1225 // Function pointers cannot be compared in an ordered way.
1226 if (LHS.isFunctionPointer() || RHS.isFunctionPointer() ||
1227 LHS.isTypeidPointer() || RHS.isTypeidPointer()) {
1228 const SourceInfo &Loc = S.Current->getSource(PC: OpPC);
1229 S.FFDiag(SI: Loc, DiagId: diag::note_constexpr_pointer_comparison_unspecified)
1230 << LHS.toDiagnosticString(Ctx: S.getASTContext())
1231 << RHS.toDiagnosticString(Ctx: S.getASTContext());
1232 return false;
1233 }
1234
1235 if (LHS == RHS) {
1236 S.Stk.push<BoolT>(Args: BoolT::from(Value: Fn(ComparisonCategoryResult::Equal)));
1237 return true;
1238 }
1239
1240 if (!Pointer::hasSameBase(A: LHS, B: RHS)) {
1241 const SourceInfo &Loc = S.Current->getSource(PC: OpPC);
1242 S.FFDiag(SI: Loc, DiagId: diag::note_constexpr_pointer_comparison_unspecified)
1243 << LHS.toDiagnosticString(Ctx: S.getASTContext())
1244 << RHS.toDiagnosticString(Ctx: S.getASTContext());
1245 return false;
1246 }
1247
1248 // Diagnose comparisons between fields with different access specifiers,
1249 // comparisons between bases and bases+fields.
1250 if (std::optional<std::pair<PtrView, PtrView>> Split =
1251 Pointer::computeSplitPoint(A: LHS, B: RHS)) {
1252 const FieldDecl *LF = Split->first.getField();
1253 const FieldDecl *RF = Split->second.getField();
1254 if (!LF && !RF)
1255 S.CCEDiag(SI: S.Current->getSource(PC: OpPC),
1256 DiagId: diag::note_constexpr_pointer_comparison_base_classes);
1257 else if (!LF)
1258 S.CCEDiag(SI: S.Current->getSource(PC: OpPC),
1259 DiagId: diag::note_constexpr_pointer_comparison_base_field)
1260 << Split->first.getRecord()->getDecl() << RF->getParent() << RF;
1261 else if (!RF)
1262 S.CCEDiag(SI: S.Current->getSource(PC: OpPC),
1263 DiagId: diag::note_constexpr_pointer_comparison_base_field)
1264 << Split->second.getRecord()->getDecl() << LF->getParent() << LF;
1265 else if (!LF->getParent()->isUnion() &&
1266 LF->getAccess() != RF->getAccess()) {
1267 S.CCEDiag(SI: S.Current->getSource(PC: OpPC),
1268 DiagId: diag::note_constexpr_pointer_comparison_differing_access)
1269 << LF << LF->getAccess() << RF << RF->getAccess() << LF->getParent();
1270 }
1271 }
1272
1273 std::optional<size_t> VL = LHS.computeOffsetForComparison(ASTCtx: S.getASTContext());
1274 std::optional<size_t> VR = RHS.computeOffsetForComparison(ASTCtx: S.getASTContext());
1275 if (!VL || !VR)
1276 return Invalid(S, OpPC);
1277 S.Stk.push<BoolT>(Args: BoolT::from(Value: Fn(Compare(X: *VL, Y: *VR))));
1278 return true;
1279}
1280
1281bool arePotentiallyOverlappingStringLiterals(const Pointer &LHS,
1282 const Pointer &RHS);
1283
1284template <>
1285inline bool CmpHelperEQ<Pointer>(InterpState &S, CodePtr OpPC, CompareFn Fn) {
1286 using BoolT = PrimConv<PT_Bool>::T;
1287 const Pointer &RHS = S.Stk.pop<Pointer>();
1288 const Pointer &LHS = S.Stk.pop<Pointer>();
1289
1290 if (LHS.isZero() && RHS.isZero()) {
1291 S.Stk.push<BoolT>(Args: BoolT::from(Value: Fn(ComparisonCategoryResult::Equal)));
1292 return true;
1293 }
1294
1295 // Reject comparisons to weak pointers.
1296 for (const auto &P : {LHS, RHS}) {
1297 if (P.isZero())
1298 continue;
1299 if (P.isWeak()) {
1300 const SourceInfo &Loc = S.Current->getSource(PC: OpPC);
1301 S.FFDiag(SI: Loc, DiagId: diag::note_constexpr_pointer_weak_comparison)
1302 << P.toDiagnosticString(Ctx: S.getASTContext());
1303 return false;
1304 }
1305 }
1306
1307 // p == nullptr or nullptr == p.
1308 if (RHS.isZero() || LHS.isZero()) {
1309 S.Stk.push<BoolT>(Args: BoolT::from(Value: Fn(ComparisonCategoryResult::Unordered)));
1310 return true;
1311 }
1312
1313 assert(!LHS.isZero());
1314 assert(!RHS.isZero());
1315
1316 if (!S.inConstantContext()) {
1317 if (LHS.isConstexprUnknown() || RHS.isConstexprUnknown())
1318 return false;
1319 }
1320
1321 if (LHS.isFunctionPointer() && RHS.isFunctionPointer()) {
1322 S.Stk.push<BoolT>(Args: BoolT::from(Value: Fn(Compare(X: LHS.getIntegerRepresentation(),
1323 Y: RHS.getIntegerRepresentation()))));
1324 return true;
1325 }
1326
1327 if (Pointer::hasSameBase(A: LHS, B: RHS)) {
1328 std::optional<size_t> A = LHS.computeOffsetForComparison(ASTCtx: S.getASTContext());
1329 std::optional<size_t> B = RHS.computeOffsetForComparison(ASTCtx: S.getASTContext());
1330 if (!A || !B)
1331 return false;
1332
1333 S.Stk.push<BoolT>(Args: BoolT::from(Value: Fn(Compare(X: *A, Y: *B))));
1334 return true;
1335 }
1336 // Otherwise we need to do a bunch of extra checks before returning Unordered.
1337
1338 if (LHS.isStringPointer() && RHS.isStringPointer() &&
1339 arePotentiallyOverlappingStringLiterals(LHS, RHS)) {
1340 S.FFDiag(SI: S.Current->getSource(PC: OpPC),
1341 DiagId: diag::note_constexpr_literal_comparison)
1342 << LHS.toDiagnosticString(Ctx: S.getASTContext())
1343 << RHS.toDiagnosticString(Ctx: S.getASTContext());
1344 return false;
1345 }
1346
1347 if (LHS.isOnePastEnd() && !RHS.isOnePastEnd()) {
1348 const SourceInfo &Loc = S.Current->getSource(PC: OpPC);
1349 S.FFDiag(SI: Loc, DiagId: diag::note_constexpr_pointer_comparison_past_end)
1350 << LHS.toDiagnosticString(Ctx: S.getASTContext());
1351 return false;
1352 }
1353 if (RHS.isOnePastEnd() && !LHS.isOnePastEnd()) {
1354 const SourceInfo &Loc = S.Current->getSource(PC: OpPC);
1355 S.FFDiag(SI: Loc, DiagId: diag::note_constexpr_pointer_comparison_past_end)
1356 << RHS.toDiagnosticString(Ctx: S.getASTContext());
1357 return false;
1358 }
1359
1360 // Reject comparisons to literals.
1361 for (const auto &P : {LHS, RHS}) {
1362 if (P.isZero())
1363 continue;
1364 if (P.pointsToLiteral()) {
1365 const Expr *E = P.getRootExpr();
1366 if (isa<StringLiteral>(Val: E)) {
1367 const SourceInfo &Loc = S.Current->getSource(PC: OpPC);
1368 S.FFDiag(SI: Loc, DiagId: diag::note_constexpr_literal_comparison);
1369 return false;
1370 }
1371 if (const auto *CE = dyn_cast<CallExpr>(Val: E);
1372 CE && isOpaqueConstantCall(E: CE)) {
1373 const SourceInfo &Loc = S.Current->getSource(PC: OpPC);
1374 S.FFDiag(SI: Loc, DiagId: diag::note_constexpr_opaque_call_comparison)
1375 << P.toDiagnosticString(Ctx: S.getASTContext());
1376 return false;
1377 }
1378 } else if (P.isIntegralPointer()) {
1379 const SourceInfo &Loc = S.Current->getSource(PC: OpPC);
1380 S.FFDiag(SI: Loc, DiagId: diag::note_constexpr_pointer_constant_comparison)
1381 << LHS.toDiagnosticString(Ctx: S.getASTContext())
1382 << RHS.toDiagnosticString(Ctx: S.getASTContext());
1383 return false;
1384 }
1385 }
1386
1387 if (LHS.isUnknownSizeArray() && RHS.isUnknownSizeArray()) {
1388 const SourceInfo &Loc = S.Current->getSource(PC: OpPC);
1389 S.FFDiag(SI: Loc, DiagId: diag::note_constexpr_pointer_comparison_zero_sized)
1390 << LHS.toDiagnosticString(Ctx: S.getASTContext())
1391 << RHS.toDiagnosticString(Ctx: S.getASTContext());
1392 return false;
1393 }
1394
1395 if (LHS.isConstexprUnknown() || RHS.isConstexprUnknown()) {
1396 if (!S.checkingPotentialConstantExpression())
1397 S.FFDiag(SI: S.Current->getSource(PC: OpPC),
1398 DiagId: diag::note_constexpr_pointer_comparison_unspecified)
1399 << LHS.toDiagnosticString(Ctx: S.getASTContext())
1400 << RHS.toDiagnosticString(Ctx: S.getASTContext());
1401 return false;
1402 }
1403
1404 S.Stk.push<BoolT>(Args: BoolT::from(Value: Fn(ComparisonCategoryResult::Unordered)));
1405 return true;
1406}
1407
1408template <>
1409inline bool CmpHelperEQ<MemberPointer>(InterpState &S, CodePtr OpPC,
1410 CompareFn Fn) {
1411 const auto &RHS = S.Stk.pop<MemberPointer>();
1412 const auto &LHS = S.Stk.pop<MemberPointer>();
1413
1414 // If either operand is a pointer to a weak function, the comparison is not
1415 // constant.
1416 for (const auto &MP : {LHS, RHS}) {
1417 if (MP.isWeak()) {
1418 const SourceInfo &Loc = S.Current->getSource(PC: OpPC);
1419 S.FFDiag(SI: Loc, DiagId: diag::note_constexpr_mem_pointer_weak_comparison)
1420 << MP.getMemberFunction();
1421 return false;
1422 }
1423 }
1424
1425 // C++11 [expr.eq]p2:
1426 // If both operands are null, they compare equal. Otherwise if only one is
1427 // null, they compare unequal.
1428 if (LHS.isZero() && RHS.isZero()) {
1429 S.Stk.push<Boolean>(Args: Fn(ComparisonCategoryResult::Equal));
1430 return true;
1431 }
1432 if (LHS.isZero() || RHS.isZero()) {
1433 S.Stk.push<Boolean>(Args: Fn(ComparisonCategoryResult::Unordered));
1434 return true;
1435 }
1436
1437 // We cannot compare against virtual declarations at compile time.
1438 for (const auto &MP : {LHS, RHS}) {
1439 if (const CXXMethodDecl *MD = MP.getMemberFunction();
1440 MD && MD->isVirtual()) {
1441 const SourceInfo &Loc = S.Current->getSource(PC: OpPC);
1442 S.CCEDiag(SI: Loc, DiagId: diag::note_constexpr_compare_virtual_mem_ptr) << MD;
1443 }
1444 }
1445
1446 S.Stk.push<Boolean>(Args: Boolean::from(Value: Fn(LHS.compare(RHS))));
1447 return true;
1448}
1449
1450template <PrimType Name, class T = typename PrimConv<Name>::T>
1451bool EQ(InterpState &S, CodePtr OpPC) {
1452 return CmpHelperEQ<T>(S, OpPC, [](ComparisonCategoryResult R) {
1453 return R == ComparisonCategoryResult::Equal;
1454 });
1455}
1456
1457template <PrimType Name, class T = typename PrimConv<Name>::T>
1458bool CMP3(InterpState &S, CodePtr OpPC, const ComparisonCategoryInfo *CmpInfo) {
1459 const T &RHS = S.Stk.pop<T>();
1460 const T &LHS = S.Stk.pop<T>();
1461 const Pointer &P = S.Stk.peek<Pointer>();
1462
1463 ComparisonCategoryResult CmpResult;
1464 if constexpr (std::is_same_v<T, Pointer>) {
1465 if (!Pointer::hasSameBase(A: LHS, B: RHS)) {
1466 S.FFDiag(SI: S.Current->getSource(PC: OpPC),
1467 DiagId: diag::note_constexpr_pointer_comparison_unspecified)
1468 << LHS.toDiagnosticString(S.getASTContext())
1469 << RHS.toDiagnosticString(S.getASTContext());
1470 return false;
1471 }
1472 std::optional<size_t> LHSOffset =
1473 LHS.computeLayoutOffset(S.getASTContext());
1474 std::optional<size_t> RHSOffset =
1475 RHS.computeLayoutOffset(S.getASTContext());
1476 if (!LHSOffset || !RHSOffset)
1477 return false;
1478
1479 if (LHSOffset < RHSOffset)
1480 CmpResult = ComparisonCategoryResult::Less;
1481 else if (LHSOffset > RHSOffset)
1482 CmpResult = ComparisonCategoryResult::Greater;
1483 else
1484 CmpResult = ComparisonCategoryResult::Equal;
1485 } else {
1486 CmpResult = LHS.compare(RHS);
1487 }
1488
1489 assert(CmpInfo);
1490 const auto *CmpValueInfo =
1491 CmpInfo->getValueInfo(ValueKind: CmpInfo->makeWeakResult(Res: CmpResult));
1492 assert(CmpValueInfo);
1493 assert(CmpValueInfo->hasValidIntValue());
1494 return SetThreeWayComparisonField(S, OpPC, Ptr: P, IntValue: CmpValueInfo->getIntValue());
1495}
1496
1497template <PrimType Name, class T = typename PrimConv<Name>::T>
1498bool NE(InterpState &S, CodePtr OpPC) {
1499 return CmpHelperEQ<T>(S, OpPC, [](ComparisonCategoryResult R) {
1500 return R != ComparisonCategoryResult::Equal;
1501 });
1502}
1503
1504template <PrimType Name, class T = typename PrimConv<Name>::T>
1505bool LT(InterpState &S, CodePtr OpPC) {
1506 return CmpHelper<T>(S, OpPC, [](ComparisonCategoryResult R) {
1507 return R == ComparisonCategoryResult::Less;
1508 });
1509}
1510
1511template <PrimType Name, class T = typename PrimConv<Name>::T>
1512bool LE(InterpState &S, CodePtr OpPC) {
1513 return CmpHelper<T>(S, OpPC, [](ComparisonCategoryResult R) {
1514 return R == ComparisonCategoryResult::Less ||
1515 R == ComparisonCategoryResult::Equal;
1516 });
1517}
1518
1519template <PrimType Name, class T = typename PrimConv<Name>::T>
1520bool GT(InterpState &S, CodePtr OpPC) {
1521 return CmpHelper<T>(S, OpPC, [](ComparisonCategoryResult R) {
1522 return R == ComparisonCategoryResult::Greater;
1523 });
1524}
1525
1526template <PrimType Name, class T = typename PrimConv<Name>::T>
1527bool GE(InterpState &S, CodePtr OpPC) {
1528 return CmpHelper<T>(S, OpPC, [](ComparisonCategoryResult R) {
1529 return R == ComparisonCategoryResult::Greater ||
1530 R == ComparisonCategoryResult::Equal;
1531 });
1532}
1533
1534//===----------------------------------------------------------------------===//
1535// Dup, Pop, Test
1536//===----------------------------------------------------------------------===//
1537
1538template <PrimType Name, class T = typename PrimConv<Name>::T>
1539bool Dup(InterpState &S) {
1540 S.Stk.push<T>(S.Stk.peek<T>());
1541 return true;
1542}
1543
1544template <PrimType Name, class T = typename PrimConv<Name>::T>
1545bool Pop(InterpState &S) {
1546 S.Stk.discard<T>();
1547 return true;
1548}
1549
1550/// [Value1, Value2] -> [Value2, Value1]
1551template <PrimType TopName, PrimType BottomName> bool Flip(InterpState &S) {
1552 using TopT = typename PrimConv<TopName>::T;
1553 using BottomT = typename PrimConv<BottomName>::T;
1554
1555 const auto &Top = S.Stk.pop<TopT>();
1556 const auto &Bottom = S.Stk.pop<BottomT>();
1557
1558 S.Stk.push<TopT>(Top);
1559 S.Stk.push<BottomT>(Bottom);
1560
1561 return true;
1562}
1563
1564//===----------------------------------------------------------------------===//
1565// Const
1566//===----------------------------------------------------------------------===//
1567
1568template <PrimType Name, class T = typename PrimConv<Name>::T>
1569bool Const(InterpState &S, const T &Arg) {
1570 if constexpr (needsAlloc<T>()) {
1571 T Result = S.allocAP<T>(Arg.bitWidth());
1572 Result.copy(Arg.toAPSInt());
1573 S.Stk.push<T>(Result);
1574 return true;
1575 }
1576
1577 if constexpr (std::is_same_v<T, uint16_t>) {
1578 S.Stk.push<Integral<16, false>>(Integral<16, false>::from(Arg));
1579 } else if constexpr (std::is_same_v<T, int16_t>) {
1580 S.Stk.push<Integral<16, true>>(Integral<16, true>::from(Arg));
1581 } else if constexpr (std::is_same_v<T, uint32_t>) {
1582 S.Stk.push<Integral<32, false>>(Integral<32, false>::from(Arg));
1583 } else if constexpr (std::is_same_v<T, int32_t>) {
1584 S.Stk.push<Integral<32, true>>(Integral<32, true>::from(Arg));
1585 } else if constexpr (std::is_same_v<T, uint64_t>) {
1586 S.Stk.push<Integral<64, false>>(Integral<64, false>::from(Arg));
1587 } else if constexpr (std::is_same_v<T, int64_t>) {
1588 S.Stk.push<Integral<64, true>>(Integral<64, true>::from(Arg));
1589 } else {
1590 // Bool.
1591 S.Stk.push<T>(Arg);
1592 }
1593
1594 return true;
1595}
1596
1597inline bool ConstFloat(InterpState &S, const Floating &F) {
1598 Floating Result = S.allocFloat(Sem: F.getSemantics());
1599 Result.copy(F: F.getAPFloat());
1600 S.Stk.push<Floating>(Args&: Result);
1601 return true;
1602}
1603
1604//===----------------------------------------------------------------------===//
1605// Get/Set Local/Param/Global/This
1606//===----------------------------------------------------------------------===//
1607
1608template <PrimType Name, class T = typename PrimConv<Name>::T>
1609bool GetLocal(InterpState &S, CodePtr OpPC, uint32_t I) {
1610 const Block *B = S.Current->getLocalBlock(Offset: I);
1611 if (!CheckLocalLoad(S, OpPC, B))
1612 return false;
1613 S.Stk.push<T>(B->deref<T>());
1614 return true;
1615}
1616
1617bool EndLifetime(InterpState &S, CodePtr OpPC);
1618bool PseudoDtor(InterpState &S, CodePtr OpPC);
1619bool StartThisLifetime(InterpState &S);
1620bool StartThisLifetime1(InterpState &S);
1621bool MarkDestroyed(InterpState &S, CodePtr OpPC);
1622bool DefaultInit(InterpState &S, CodePtr OpPC, const CXXConstructorDecl *Ctor);
1623
1624/// 1) Pops the value from the stack.
1625/// 2) Writes the value to the local variable with the
1626/// given offset.
1627template <PrimType Name, class T = typename PrimConv<Name>::T>
1628bool SetLocal(InterpState &S, uint32_t I) {
1629 S.Current->setLocal<T>(I, S.Stk.pop<T>());
1630 return true;
1631}
1632
1633template <PrimType Name, class T = typename PrimConv<Name>::T>
1634bool GetParam(InterpState &S, uint32_t Index) {
1635 if (S.checkingPotentialConstantExpression()) {
1636 return false;
1637 }
1638 S.Stk.push<T>(S.Current->getParam<T>(Index));
1639 return true;
1640}
1641
1642template <PrimType Name, class T = typename PrimConv<Name>::T>
1643bool SetParam(InterpState &S, uint32_t I) {
1644 S.Current->setParam<T>(I, S.Stk.pop<T>());
1645 return true;
1646}
1647
1648/// 1) Peeks a pointer on the stack
1649/// 2) Pushes the value of the pointer's field on the stack
1650template <PrimType Name, class T = typename PrimConv<Name>::T>
1651bool GetField(InterpState &S, CodePtr OpPC, uint32_t I) {
1652 const Pointer &Obj = S.Stk.peek<Pointer>();
1653 if (!CheckNull(S, OpPC, Ptr: Obj, CSK: CSK_Field))
1654 return false;
1655 if (!CheckRange(S, OpPC, Ptr: Obj, CSK: CSK_Field))
1656 return false;
1657
1658 if (!Obj.isBlockPointer())
1659 return false;
1660
1661 // FIXME(postswitch): The isUnknownSizeArray() check here is only needed
1662 // to keep an invalid sample producing the same diagnostics as the current
1663 // interpreter.
1664 if (!Obj.getFieldDesc()->isRecord() && !Obj.isUnknownSizeArray())
1665 return false;
1666
1667 PtrView FieldPtr = Obj.view().atField(Offset: I);
1668 if (!CheckLoad(S, OpPC, Ptr: FieldPtr))
1669 return false;
1670 S.Stk.push<T>(FieldPtr.deref<T>());
1671 return true;
1672}
1673
1674/// 1) Pops a pointer from the stack
1675/// 2) Pushes the value of the pointer's field on the stack
1676template <PrimType Name, class T = typename PrimConv<Name>::T>
1677bool GetFieldPop(InterpState &S, CodePtr OpPC, uint32_t I) {
1678 const Pointer &Obj = S.Stk.pop<Pointer>();
1679 if (!CheckNull(S, OpPC, Ptr: Obj, CSK: CSK_Field))
1680 return false;
1681 if (!CheckRange(S, OpPC, Ptr: Obj, CSK: CSK_Field))
1682 return false;
1683
1684 if (!Obj.isBlockPointer())
1685 return false;
1686
1687 // FIXME(postswitch): The isUnknownSizeArray() check here is only needed
1688 // to keep an invalid sample producing the same diagnostics as the current
1689 // interpreter.
1690 if (!Obj.getFieldDesc()->isRecord() && !Obj.isUnknownSizeArray())
1691 return false;
1692
1693 PtrView FieldPtr = Obj.view().atField(Offset: I);
1694 if (!CheckLoad(S, OpPC, Ptr: FieldPtr))
1695 return false;
1696 S.Stk.push<T>(FieldPtr.deref<T>());
1697 return true;
1698}
1699
1700template <PrimType Name, class T = typename PrimConv<Name>::T>
1701bool GetThisField(InterpState &S, CodePtr OpPC, uint32_t I) {
1702 if (S.checkingPotentialConstantExpression())
1703 return false;
1704 if (!CheckThis(S, OpPC))
1705 return false;
1706 const Pointer &This = S.Current->getThis();
1707
1708 if (!This.isBlockPointer())
1709 return false;
1710
1711 PtrView FieldPtr = This.view().atField(Offset: I);
1712 if (!CheckLoad(S, OpPC, Ptr: FieldPtr))
1713 return false;
1714 S.Stk.push<T>(FieldPtr.deref<T>());
1715 return true;
1716}
1717
1718template <PrimType Name, class T = typename PrimConv<Name>::T>
1719bool GetGlobal(InterpState &S, CodePtr OpPC, uint32_t I) {
1720 const Block *B = S.P.getGlobal(Idx: I);
1721
1722 if (!CheckGlobalLoad(S, OpPC, B))
1723 return false;
1724
1725 S.Stk.push<T>(B->deref<T>());
1726 return true;
1727}
1728
1729/// Same as GetGlobal, but without the checks.
1730template <PrimType Name, class T = typename PrimConv<Name>::T>
1731bool GetGlobalUnchecked(InterpState &S, CodePtr OpPC, uint32_t I) {
1732 const Block *B = S.P.getGlobal(Idx: I);
1733 const auto &Desc = B->getBlockDesc<GlobalInlineDescriptor>();
1734 if (Desc.InitState != GlobalInitState::Initialized)
1735 return diagnoseUninitialized(S, OpPC, Extern: B->isExtern(), B);
1736
1737 S.Stk.push<T>(B->deref<T>());
1738 return true;
1739}
1740
1741template <PrimType Name, class T = typename PrimConv<Name>::T>
1742bool SetGlobal(InterpState &S, CodePtr OpPC, uint32_t I) {
1743 // TODO: emit warning.
1744 return false;
1745}
1746
1747template <PrimType Name, class T = typename PrimConv<Name>::T>
1748bool InitGlobal(InterpState &S, uint32_t I) {
1749 const Pointer &P = S.P.getGlobal(Idx: I);
1750
1751 P.deref<T>() = S.Stk.pop<T>();
1752
1753 if constexpr (std::is_same_v<T, Floating>) {
1754 auto &Val = P.deref<Floating>();
1755 if (!Val.singleWord()) {
1756 uint64_t *NewMemory = new (S.P) uint64_t[Val.numWords()];
1757 Val.take(NewMemory);
1758 }
1759
1760 } else if constexpr (std::is_same_v<T, MemberPointer>) {
1761 auto &Val = P.deref<MemberPointer>();
1762 unsigned PathLength = Val.getPathLength();
1763 auto *NewPath = new (S.P) const CXXRecordDecl *[PathLength];
1764 for (unsigned I = 0; I != PathLength; ++I) {
1765 NewPath[I] = Val.getPathEntry(Index: I);
1766 }
1767 Val.takePath(NewPath);
1768 } else if constexpr (std::is_same_v<T, Pointer>) {
1769 auto &Val = P.deref<Pointer>();
1770 if (Val.isOpaquePointer() && Val.asOpaquePointer().PathLength != 0) {
1771 const OpaquePointer &OP = Val.asOpaquePointer();
1772 auto *NewPath = new (S.P) PointerPathEntry[OP.PathLength];
1773 std::memcpy(dest: NewPath, src: OP.Path, n: OP.PathLength * sizeof(PointerPathEntry));
1774 Val = Pointer(OP.withPath(Path: NewPath, PathLength: OP.PathLength,
1775 FieldTy: OP.getFieldType().getTypePtr(),
1776 PastEnd: OP.isOnePastEnd()),
1777 Val.getByteOffset());
1778 }
1779
1780 } else if constexpr (needsAlloc<T>()) {
1781 auto &Val = P.deref<T>();
1782 if (!Val.singleWord()) {
1783 uint64_t *NewMemory = new (S.P) uint64_t[Val.numWords()];
1784 Val.take(NewMemory);
1785 }
1786 }
1787
1788 P.initialize();
1789 return true;
1790}
1791
1792/// 1) Converts the value on top of the stack to an APValue
1793/// 2) Sets that APValue on \Temp
1794/// 3) Initializes global with index \I with that
1795template <PrimType Name, class T = typename PrimConv<Name>::T>
1796bool InitGlobalTemp(InterpState &S, uint32_t I,
1797 const LifetimeExtendedTemporaryDecl *Temp) {
1798 if (S.EvalMode == EvaluationMode::ConstantFold)
1799 return false;
1800 assert(Temp);
1801
1802 const Pointer &Ptr = S.P.getGlobal(Idx: I);
1803 assert(Ptr.getRootExpr());
1804 S.SeenGlobalTemporaries.push_back(Elt: std::make_pair(x: Ptr.getRootExpr(), y&: Temp));
1805
1806 Ptr.deref<T>() = S.Stk.pop<T>();
1807 Ptr.initialize();
1808 return true;
1809}
1810
1811/// 1) Converts the value on top of the stack to an APValue
1812/// 2) Sets that APValue on \Temp
1813/// 3) Initialized global with index \I with that
1814inline bool InitGlobalTempComp(InterpState &S,
1815 const LifetimeExtendedTemporaryDecl *Temp) {
1816 if (S.EvalMode == EvaluationMode::ConstantFold)
1817 return false;
1818 assert(Temp);
1819
1820 const Pointer &Ptr = S.Stk.peek<Pointer>();
1821 S.SeenGlobalTemporaries.push_back(Elt: std::make_pair(x: Ptr.getRootExpr(), y&: Temp));
1822 return true;
1823}
1824
1825template <PrimType Name, class T = typename PrimConv<Name>::T>
1826bool InitThisField(InterpState &S, CodePtr OpPC, uint32_t I) {
1827 if (S.checkingPotentialConstantExpression() && S.Current->isBottomFrame())
1828 return false;
1829 if (!CheckThis(S, OpPC))
1830 return false;
1831 const Pointer &This = S.Current->getThis();
1832 if (!This.isDereferencable())
1833 return false;
1834
1835 const Pointer &Field = This.atField(Off: I);
1836 Field.deref<T>() = S.Stk.pop<T>();
1837 Field.initialize();
1838 return true;
1839}
1840
1841template <PrimType Name, class T = typename PrimConv<Name>::T>
1842bool InitThisFieldActivate(InterpState &S, CodePtr OpPC, uint32_t I) {
1843 if (S.checkingPotentialConstantExpression() && S.Current->isBottomFrame())
1844 return false;
1845 if (!CheckThis(S, OpPC))
1846 return false;
1847 const Pointer &This = S.Current->getThis();
1848 if (!This.isDereferencable())
1849 return false;
1850
1851 const Pointer &Field = This.atField(Off: I);
1852 Field.deref<T>() = S.Stk.pop<T>();
1853 Field.activate();
1854 Field.initialize();
1855 return true;
1856}
1857
1858template <PrimType Name, class T = typename PrimConv<Name>::T>
1859bool InitThisBitField(InterpState &S, CodePtr OpPC, uint32_t FieldOffset,
1860 uint32_t FieldBitWidth) {
1861 if (S.checkingPotentialConstantExpression() && S.Current->isBottomFrame())
1862 return false;
1863 if (!CheckThis(S, OpPC))
1864 return false;
1865 const Pointer &This = S.Current->getThis();
1866 if (!This.isDereferencable())
1867 return false;
1868
1869 const Pointer &Field = This.atField(Off: FieldOffset);
1870 const auto &Value = S.Stk.pop<T>();
1871
1872 if constexpr (isIntegralOrPointer<T>()) {
1873 if (!Value.isNumber())
1874 return false;
1875 }
1876
1877 Field.deref<T>() = Value.truncate(FieldBitWidth);
1878 Field.initialize();
1879 return true;
1880}
1881
1882template <PrimType Name, class T = typename PrimConv<Name>::T>
1883bool InitThisBitFieldActivate(InterpState &S, CodePtr OpPC,
1884 uint32_t FieldOffset, uint32_t FieldBitWidth) {
1885 if (S.checkingPotentialConstantExpression() && S.Current->isBottomFrame())
1886 return false;
1887 if (!CheckThis(S, OpPC))
1888 return false;
1889 const Pointer &This = S.Current->getThis();
1890 if (!This.isDereferencable())
1891 return false;
1892
1893 const Pointer &Field = This.atField(Off: FieldOffset);
1894 const auto &Value = S.Stk.pop<T>();
1895
1896 if constexpr (isIntegralOrPointer<T>()) {
1897 if (!Value.isNumber())
1898 return false;
1899 }
1900
1901 Field.deref<T>() = Value.truncate(FieldBitWidth);
1902 Field.initialize();
1903 Field.activate();
1904 return true;
1905}
1906
1907/// 1) Pops the value from the stack
1908/// 2) Peeks a pointer from the stack
1909/// 3) Pushes the value to field I of the pointer on the stack
1910template <PrimType Name, class T = typename PrimConv<Name>::T>
1911bool InitField(InterpState &S, CodePtr OpPC, uint32_t I) {
1912 const T &Value = S.Stk.pop<T>();
1913 const Pointer &Ptr = S.Stk.peek<Pointer>();
1914 if (!Ptr.isDereferencable())
1915 return false;
1916
1917 if (!CheckRange(S, OpPC, Ptr, CSK: CSK_Field))
1918 return false;
1919 if (!CheckArray(S, OpPC, Ptr))
1920 return false;
1921
1922 const Pointer &Field = Ptr.atField(Off: I);
1923 Field.deref<T>() = Value;
1924 Field.initialize();
1925 return true;
1926}
1927
1928template <PrimType Name, class T = typename PrimConv<Name>::T>
1929bool InitFieldActivate(InterpState &S, CodePtr OpPC, uint32_t I) {
1930 const T &Value = S.Stk.pop<T>();
1931 const Pointer &Ptr = S.Stk.peek<Pointer>();
1932 if (!Ptr.isDereferencable())
1933 return false;
1934 if (!CheckRange(S, OpPC, Ptr, CSK: CSK_Field))
1935 return false;
1936 if (!CheckArray(S, OpPC, Ptr))
1937 return false;
1938
1939 const Pointer &Field = Ptr.atField(Off: I);
1940 Field.deref<T>() = Value;
1941 Field.activate();
1942 Field.initialize();
1943 return true;
1944}
1945
1946template <PrimType Name, class T = typename PrimConv<Name>::T>
1947bool InitBitField(InterpState &S, CodePtr OpPC, uint32_t FieldOffset,
1948 uint32_t FieldBitWidth) {
1949 const T &Value = S.Stk.pop<T>();
1950 const Pointer &Ptr = S.Stk.peek<Pointer>();
1951 if (!Ptr.isDereferencable())
1952 return false;
1953
1954 if constexpr (isIntegralOrPointer<T>()) {
1955 if (!Value.isNumber())
1956 return false;
1957 }
1958 if (!CheckRange(S, OpPC, Ptr, CSK: CSK_Field))
1959 return false;
1960 if (!CheckArray(S, OpPC, Ptr))
1961 return false;
1962
1963 const Pointer &Field = Ptr.atField(Off: FieldOffset);
1964
1965 unsigned BitWidth = std::min(FieldBitWidth, Value.bitWidth());
1966 if constexpr (needsAlloc<T>()) {
1967 T Result = S.allocAP<T>(Value.bitWidth());
1968 if constexpr (T::isSigned())
1969 Result.copy(
1970 Value.toAPSInt().trunc(BitWidth).sextOrTrunc(Value.bitWidth()));
1971 else
1972 Result.copy(
1973 Value.toAPSInt().trunc(BitWidth).zextOrTrunc(Value.bitWidth()));
1974
1975 Field.deref<T>() = Result;
1976 } else {
1977 Field.deref<T>() = Value.truncate(FieldBitWidth);
1978 }
1979 Field.initialize();
1980 return true;
1981}
1982
1983template <PrimType Name, class T = typename PrimConv<Name>::T>
1984bool InitBitFieldActivate(InterpState &S, CodePtr OpPC, uint32_t FieldOffset,
1985 uint32_t FieldBitWidth) {
1986 const T &Value = S.Stk.pop<T>();
1987 const Pointer &Ptr = S.Stk.peek<Pointer>();
1988 if (!Ptr.isDereferencable())
1989 return false;
1990
1991 if constexpr (isIntegralOrPointer<T>()) {
1992 if (!Value.isNumber())
1993 return false;
1994 }
1995 if (!CheckRange(S, OpPC, Ptr, CSK: CSK_Field))
1996 return false;
1997 if (!CheckArray(S, OpPC, Ptr))
1998 return false;
1999
2000 const Pointer &Field = Ptr.atField(Off: FieldOffset);
2001
2002 unsigned BitWidth = std::min(FieldBitWidth, Value.bitWidth());
2003 if constexpr (needsAlloc<T>()) {
2004 T Result = S.allocAP<T>(Value.bitWidth());
2005 if constexpr (T::isSigned())
2006 Result.copy(
2007 Value.toAPSInt().trunc(BitWidth).sextOrTrunc(Value.bitWidth()));
2008 else
2009 Result.copy(
2010 Value.toAPSInt().trunc(BitWidth).zextOrTrunc(Value.bitWidth()));
2011
2012 Field.deref<T>() = Result;
2013 } else {
2014 Field.deref<T>() = Value.truncate(FieldBitWidth);
2015 }
2016 Field.activate();
2017 Field.initialize();
2018 return true;
2019}
2020
2021//===----------------------------------------------------------------------===//
2022// GetPtr Local/Param/Global/Field/This
2023//===----------------------------------------------------------------------===//
2024
2025inline bool GetPtrLocal(InterpState &S, uint32_t I) {
2026 S.Stk.push<Pointer>(Args: S.Current->getLocalPointer(Offset: I));
2027 return true;
2028}
2029
2030inline bool GetRefLocal(InterpState &S, CodePtr OpPC, uint32_t I) {
2031 Block *LocalBlock = S.Current->getLocalBlock(Offset: I);
2032 return handleReference(S, OpPC, B: LocalBlock);
2033}
2034
2035inline bool GetRefGlobal(InterpState &S, CodePtr OpPC, uint32_t I) {
2036 Block *B = S.P.getGlobal(Idx: I);
2037
2038 // If we're currently evaluating this variable, use that in-flight value.
2039 // It will otherwise be diagnosed as non-initialized reference and we will
2040 // complain about a missing initializer.
2041 if (S.EvaluatingDecl && B->getDescriptor()->asVarDecl() == S.EvaluatingDecl) {
2042 S.Stk.push<Pointer>(Args&: B);
2043 return true;
2044 }
2045
2046 if (B->isConstexprUnknown()) {
2047 S.Stk.push<Pointer>(Args&: B);
2048 return true;
2049 }
2050
2051 const auto &Desc = B->getBlockDesc<GlobalInlineDescriptor>();
2052 if (Desc.InitState != GlobalInitState::Initialized)
2053 return diagnoseUninitialized(S, OpPC, Extern: B->isExtern(), B);
2054
2055 S.Stk.push<Pointer>(Args&: B->deref<Pointer>());
2056 return true;
2057}
2058
2059inline bool CheckRefInit(InterpState &S, CodePtr OpPC) {
2060 const Pointer &Ptr = S.Stk.peek<Pointer>();
2061 return CheckRange(S, OpPC, Ptr, AK: AK_Read);
2062}
2063
2064inline bool GetPtrParam(InterpState &S, uint32_t Index) {
2065 if (S.Current->isBottomFrame())
2066 return false;
2067 S.Stk.push<Pointer>(Args: S.Current->getParamPointer(Offset: Index));
2068 return true;
2069}
2070
2071inline bool GetPtrGlobal(InterpState &S, uint32_t I) {
2072 S.Stk.push<Pointer>(Args: S.P.getPtrGlobal(Idx: I));
2073 return true;
2074}
2075
2076/// 1) Peeks a Pointer
2077/// 2) Pushes Pointer.atField(Off) on the stack
2078bool GetPtrField(InterpState &S, CodePtr OpPC, uint32_t Off);
2079bool GetPtrFieldPop(InterpState &S, CodePtr OpPC, uint32_t Off);
2080
2081bool GetPtrBase(InterpState &S, CodePtr OpPC, uint32_t Off);
2082bool GetPtrBasePop(InterpState &S, CodePtr OpPC, uint32_t Off, bool NullOK);
2083
2084bool GetPtrDerivedPop(InterpState &S, CodePtr OpPC, bool NullOK,
2085 const Type *TargetType, const Type *FinalType);
2086
2087inline bool GetPtrThisField(InterpState &S, CodePtr OpPC, uint32_t Off) {
2088 if (S.checkingPotentialConstantExpression() && S.Current->isBottomFrame())
2089 return false;
2090 if (!CheckThis(S, OpPC))
2091 return false;
2092 const Pointer &This = S.Current->getThis();
2093 if (!This.isBlockPointer())
2094 return false;
2095 S.Stk.push<Pointer>(Args: This.atField(Off));
2096 return true;
2097}
2098
2099inline bool GetPtrThisBase(InterpState &S, CodePtr OpPC, uint32_t Off) {
2100 if (S.checkingPotentialConstantExpression() && S.Current->isBottomFrame())
2101 return false;
2102 if (!CheckThis(S, OpPC))
2103 return false;
2104 const Pointer &This = S.Current->getThis();
2105 S.Stk.push<Pointer>(Args: This.atField(Off));
2106 return true;
2107}
2108
2109inline bool FinishInitPop(InterpState &S) {
2110 const Pointer &Ptr = S.Stk.pop<Pointer>();
2111 Ptr.initialize();
2112 return true;
2113}
2114
2115inline bool FinishInit(InterpState &S) {
2116 const Pointer &Ptr = S.Stk.peek<Pointer>();
2117 Ptr.initialize();
2118 return true;
2119}
2120
2121inline bool FinishInitActivate(InterpState &S) {
2122 const Pointer &Ptr = S.Stk.peek<Pointer>();
2123 Ptr.initialize();
2124 Ptr.activate();
2125 return true;
2126}
2127
2128inline bool FinishInitActivatePop(InterpState &S) {
2129 const Pointer &Ptr = S.Stk.pop<Pointer>();
2130 Ptr.initialize();
2131 Ptr.activate();
2132 return true;
2133}
2134
2135bool FinishInitGlobal(InterpState &S);
2136
2137inline bool Dump(InterpState &S) {
2138 S.Stk.dump();
2139 return true;
2140}
2141
2142inline bool CheckNull(InterpState &S, CodePtr OpPC) {
2143 const auto &Ptr = S.Stk.peek<Pointer>();
2144 if (Ptr.isZero()) {
2145 S.FFDiag(SI: S.Current->getSource(PC: OpPC),
2146 DiagId: diag::note_constexpr_dereferencing_null);
2147 return S.noteUndefinedBehavior();
2148 }
2149 return true;
2150}
2151
2152bool virtBaseHelper(InterpState &S, const CXXRecordDecl *Decl,
2153 const Pointer &Ptr);
2154
2155inline bool GetPtrVirtBasePop(InterpState &S, CodePtr OpPC,
2156 const CXXRecordDecl *D) {
2157 assert(D);
2158 const Pointer &Ptr = S.Stk.pop<Pointer>();
2159 if (!CheckNull(S, OpPC, Ptr, CSK: CSK_Base))
2160 return false;
2161 return virtBaseHelper(S, Decl: D, Ptr);
2162}
2163
2164inline bool GetPtrVirtBase(InterpState &S, CodePtr OpPC,
2165 const CXXRecordDecl *D) {
2166 assert(D);
2167 const Pointer &Ptr = S.Stk.peek<Pointer>();
2168 if (!CheckNull(S, OpPC, Ptr, CSK: CSK_Base))
2169 return false;
2170 return virtBaseHelper(S, Decl: D, Ptr);
2171}
2172
2173inline bool GetPtrThisVirtBase(InterpState &S, CodePtr OpPC,
2174 const CXXRecordDecl *D) {
2175 assert(D);
2176 if (S.checkingPotentialConstantExpression())
2177 return false;
2178 if (!CheckThis(S, OpPC))
2179 return false;
2180 const Pointer &This = S.Current->getThis();
2181 return virtBaseHelper(S, Decl: D, Ptr: This);
2182}
2183
2184//===----------------------------------------------------------------------===//
2185// Load, Store, Init
2186//===----------------------------------------------------------------------===//
2187
2188template <PrimType Name, class T = typename PrimConv<Name>::T>
2189bool Load(InterpState &S, CodePtr OpPC) {
2190 const Pointer &Ptr = S.Stk.peek<Pointer>();
2191 if (!CheckLoad(S, OpPC, Ptr))
2192 return false;
2193 if (!Ptr.canDeref(T: Name))
2194 return false;
2195 S.Stk.push<T>(Ptr.load<T>());
2196 return true;
2197}
2198
2199template <PrimType Name, class T = typename PrimConv<Name>::T>
2200bool LoadPop(InterpState &S, CodePtr OpPC) {
2201 const Pointer &Ptr = S.Stk.pop<Pointer>();
2202 if (!CheckLoad(S, OpPC, Ptr))
2203 return false;
2204 if (!Ptr.canDeref(T: Name))
2205 return false;
2206 S.Stk.push<T>(Ptr.load<T>());
2207 return true;
2208}
2209
2210/// Like LoadPop above, but if any of the checks fail, we
2211/// turn the pointer into an opaque pointer of appropriate type.
2212inline bool LoadPopL(InterpState &S, CodePtr OpPC) {
2213 const Pointer &Ptr = S.Stk.pop<Pointer>();
2214 auto *P = S.getEvalStatus().Diag;
2215 S.getEvalStatus().Diag = nullptr;
2216
2217 bool Failed = false;
2218 if (!CheckLoad(S, OpPC, Ptr))
2219 Failed = true;
2220 if (!Ptr.isBlockPointer())
2221 Failed = true;
2222 if (!Failed && !Ptr.canDeref(T: PT_Ptr))
2223 Failed = true;
2224 S.getEvalStatus().Diag = P;
2225
2226 if (Failed) {
2227 if (Ptr.isOpaquePointer()) {
2228 const OpaquePointer &OP = Ptr.asOpaquePointer();
2229
2230 if (!Ptr.asOpaquePointer().Base.getType()->isPointerType())
2231 return false;
2232
2233 QualType T = Ptr.getType();
2234 S.Stk.push<Pointer>(Args: OP.withFieldType(FieldTy: T.getTypePtr()),
2235 Args: Ptr.getByteOffset());
2236 return true;
2237 }
2238
2239 // Convert the block pointer to an opaque pointer.
2240 if (!Ptr.isBlockPointer())
2241 return false;
2242 // FIXME: I *think* we need more information here than just the base.
2243 S.Stk.push<Pointer>(Args: Ptr.getDeclDesc()->asValueDecl());
2244 } else {
2245 S.Stk.push<Pointer>(Args&: Ptr.deref<Pointer>());
2246 }
2247 return true;
2248}
2249
2250template <PrimType Name, class T = typename PrimConv<Name>::T>
2251bool Store(InterpState &S, CodePtr OpPC) {
2252 const T &Value = S.Stk.pop<T>();
2253 const Pointer &Ptr = S.Stk.peek<Pointer>();
2254 if (!CheckStore(S, OpPC, Ptr))
2255 return false;
2256 if (!Ptr.canDeref(T: Name))
2257 return false;
2258 Ptr.initialize();
2259 Ptr.deref<T>() = Value;
2260 return true;
2261}
2262
2263template <PrimType Name, class T = typename PrimConv<Name>::T>
2264bool StorePop(InterpState &S, CodePtr OpPC) {
2265 const T &Value = S.Stk.pop<T>();
2266 const Pointer &Ptr = S.Stk.pop<Pointer>();
2267 if (!CheckStore(S, OpPC, Ptr))
2268 return false;
2269 if (!Ptr.canDeref(T: Name))
2270 return false;
2271 Ptr.initialize();
2272 Ptr.deref<T>() = Value;
2273 return true;
2274}
2275
2276static inline bool Activate(InterpState &S) {
2277 const Pointer &Ptr = S.Stk.peek<Pointer>();
2278 Ptr.activate();
2279 return true;
2280}
2281
2282static inline bool ActivateThisField(InterpState &S, uint32_t I) {
2283 if (S.checkingPotentialConstantExpression())
2284 return false;
2285 if (!S.Current->hasThisPointer())
2286 return false;
2287
2288 const Pointer &Ptr = S.Current->getThis();
2289 Ptr.atField(Off: I).activate();
2290 return true;
2291}
2292
2293template <PrimType Name, class T = typename PrimConv<Name>::T>
2294bool StoreActivate(InterpState &S, CodePtr OpPC) {
2295 const T &Value = S.Stk.pop<T>();
2296 const Pointer &Ptr = S.Stk.peek<Pointer>();
2297
2298 if (!CheckStore(S, OpPC, Ptr, AK: AK_Assign, /*WillBeActivated=*/WillBeActivated: true))
2299 return false;
2300 Ptr.initialize();
2301 Ptr.activate();
2302 Ptr.deref<T>() = Value;
2303 return true;
2304}
2305
2306template <PrimType Name, class T = typename PrimConv<Name>::T>
2307bool StoreActivatePop(InterpState &S, CodePtr OpPC) {
2308 const T &Value = S.Stk.pop<T>();
2309 const Pointer &Ptr = S.Stk.pop<Pointer>();
2310
2311 if (!CheckStore(S, OpPC, Ptr, AK: AK_Assign, /*WillBeActivated=*/WillBeActivated: true))
2312 return false;
2313 Ptr.initialize();
2314 Ptr.activate();
2315 Ptr.deref<T>() = Value;
2316 return true;
2317}
2318
2319template <PrimType Name, class T = typename PrimConv<Name>::T>
2320bool StoreBitField(InterpState &S, CodePtr OpPC) {
2321 const T &Value = S.Stk.pop<T>();
2322 const Pointer &Ptr = S.Stk.peek<Pointer>();
2323
2324 if (!CheckStore(S, OpPC, Ptr))
2325 return false;
2326 Ptr.initialize();
2327 if (const auto *FD = Ptr.getField())
2328 Ptr.deref<T>() = Value.truncate(FD->getBitWidthValue());
2329 else
2330 Ptr.deref<T>() = Value;
2331 return true;
2332}
2333
2334template <PrimType Name, class T = typename PrimConv<Name>::T>
2335bool StoreBitFieldPop(InterpState &S, CodePtr OpPC) {
2336 const T &Value = S.Stk.pop<T>();
2337 const Pointer &Ptr = S.Stk.pop<Pointer>();
2338 if (!CheckStore(S, OpPC, Ptr))
2339 return false;
2340 Ptr.initialize();
2341 if (const auto *FD = Ptr.getField())
2342 Ptr.deref<T>() = Value.truncate(FD->getBitWidthValue());
2343 else
2344 Ptr.deref<T>() = Value;
2345 return true;
2346}
2347
2348template <PrimType Name, class T = typename PrimConv<Name>::T>
2349bool StoreBitFieldActivate(InterpState &S, CodePtr OpPC) {
2350 const T &Value = S.Stk.pop<T>();
2351 const Pointer &Ptr = S.Stk.peek<Pointer>();
2352
2353 if (!CheckStore(S, OpPC, Ptr, AK: AK_Assign, /*WillBeActivated=*/WillBeActivated: true))
2354 return false;
2355 Ptr.initialize();
2356 Ptr.activate();
2357 if (const auto *FD = Ptr.getField())
2358 Ptr.deref<T>() = Value.truncate(FD->getBitWidthValue());
2359 else
2360 Ptr.deref<T>() = Value;
2361 return true;
2362}
2363
2364template <PrimType Name, class T = typename PrimConv<Name>::T>
2365bool StoreBitFieldActivatePop(InterpState &S, CodePtr OpPC) {
2366 const T &Value = S.Stk.pop<T>();
2367 const Pointer &Ptr = S.Stk.pop<Pointer>();
2368
2369 if (!CheckStore(S, OpPC, Ptr, AK: AK_Assign, /*WillBeActivated=*/WillBeActivated: true))
2370 return false;
2371 Ptr.initialize();
2372 Ptr.activate();
2373 if (const auto *FD = Ptr.getField())
2374 Ptr.deref<T>() = Value.truncate(FD->getBitWidthValue());
2375 else
2376 Ptr.deref<T>() = Value;
2377 return true;
2378}
2379
2380template <PrimType Name, class T = typename PrimConv<Name>::T>
2381bool Init(InterpState &S, CodePtr OpPC) {
2382 const T &Value = S.Stk.pop<T>();
2383 const Pointer &Ptr = S.Stk.peek<Pointer>();
2384 if (!CheckInit(S, OpPC, Ptr))
2385 return false;
2386 Ptr.initialize();
2387 new (&Ptr.deref<T>()) T(Value);
2388 return true;
2389}
2390
2391template <PrimType Name, class T = typename PrimConv<Name>::T>
2392bool InitPop(InterpState &S, CodePtr OpPC) {
2393 const T &Value = S.Stk.pop<T>();
2394 const Pointer &Ptr = S.Stk.pop<Pointer>();
2395 if (!CheckInit(S, OpPC, Ptr))
2396 return false;
2397 Ptr.initialize();
2398 new (&Ptr.deref<T>()) T(Value);
2399 return true;
2400}
2401
2402/// 1) Pops the value from the stack
2403/// 2) Peeks a pointer and gets its index \Idx
2404/// 3) Sets the value on the pointer, leaving the pointer on the stack.
2405template <PrimType Name, class T = typename PrimConv<Name>::T>
2406bool InitElem(InterpState &S, CodePtr OpPC, uint32_t Idx) {
2407 const T &Value = S.Stk.pop<T>();
2408 const Pointer &Ptr = S.Stk.peek<Pointer>();
2409
2410 if (Ptr.isConstexprUnknown())
2411 return false;
2412
2413 const Descriptor *Desc = Ptr.getFieldDesc();
2414 if (Desc->isUnknownSizeArray())
2415 return false;
2416
2417 // In the unlikely event that we're initializing the first item of
2418 // a non-array, skip the atIndex().
2419 if (Idx == 0 && !Desc->isArray()) {
2420 Ptr.initialize();
2421 new (&Ptr.deref<T>()) T(Value);
2422 return true;
2423 }
2424
2425 if (!CheckLive(S, OpPC, Ptr, AK: AK_Assign))
2426 return false;
2427 if (Idx >= Desc->getNumElems()) {
2428 // CheckRange.
2429 if (S.getLangOpts().CPlusPlus) {
2430 const SourceInfo &Loc = S.Current->getSource(PC: OpPC);
2431 S.FFDiag(SI: Loc, DiagId: diag::note_constexpr_access_past_end)
2432 << AK_Assign << S.Current->getRange(PC: OpPC);
2433 }
2434 return false;
2435 }
2436 Ptr.initializeElement(Index: Idx);
2437 new (&Ptr.elem<T>(Idx)) T(Value);
2438 return true;
2439}
2440
2441/// The same as InitElem, but pops the pointer as well.
2442template <PrimType Name, class T = typename PrimConv<Name>::T>
2443bool InitElemPop(InterpState &S, CodePtr OpPC, uint32_t Idx) {
2444 const T &Value = S.Stk.pop<T>();
2445 const Pointer &Ptr = S.Stk.pop<Pointer>();
2446
2447 if (Ptr.isConstexprUnknown())
2448 return false;
2449
2450 const Descriptor *Desc = Ptr.getFieldDesc();
2451 if (Desc->isUnknownSizeArray())
2452 return false;
2453
2454 // In the unlikely event that we're initializing the first item of
2455 // a non-array, skip the atIndex().
2456 if (Idx == 0 && !Desc->isArray()) {
2457 Ptr.initialize();
2458 new (&Ptr.deref<T>()) T(Value);
2459 return true;
2460 }
2461
2462 if (!CheckLive(S, OpPC, Ptr, AK: AK_Assign))
2463 return false;
2464 if (Idx >= Desc->getNumElems()) {
2465 // CheckRange.
2466 if (S.getLangOpts().CPlusPlus) {
2467 const SourceInfo &Loc = S.Current->getSource(PC: OpPC);
2468 S.FFDiag(SI: Loc, DiagId: diag::note_constexpr_access_past_end)
2469 << AK_Assign << S.Current->getRange(PC: OpPC);
2470 }
2471 return false;
2472 }
2473 Ptr.initializeElement(Index: Idx);
2474 new (&Ptr.elem<T>(Idx)) T(Value);
2475 return true;
2476}
2477
2478inline bool ToMemberPtr(InterpState &S) {
2479 const auto &Member = S.Stk.pop<MemberPointer>();
2480 const auto &Base = S.Stk.pop<Pointer>();
2481
2482 S.Stk.push<MemberPointer>(Args: Member.takeInstance(Instance: Base));
2483 return true;
2484}
2485
2486inline bool CastMemberPtrPtr(InterpState &S, CodePtr OpPC) {
2487 const auto &MP = S.Stk.pop<MemberPointer>();
2488
2489 if (std::optional<Pointer> Ptr = MP.toPointer(Ctx: S.Ctx)) {
2490 S.Stk.push<Pointer>(Args&: *Ptr);
2491 return true;
2492 }
2493 return Invalid(S, OpPC);
2494}
2495
2496bool Memcpy(InterpState &S, CodePtr OpPC);
2497bool TrivialCopy(InterpState &S, CodePtr OpPC, bool Activate,
2498 const Function *Func);
2499
2500//===----------------------------------------------------------------------===//
2501// AddOffset, SubOffset
2502//===----------------------------------------------------------------------===//
2503
2504template <class T, ArithOp Op>
2505std::optional<Pointer> OffsetHelper(InterpState &S, CodePtr OpPC,
2506 const T &Offset, const Pointer &Ptr) {
2507 // This is much simpler for integral pointers, so handle them first.
2508 if (Ptr.isIntegralPointer()) {
2509 uint64_t V = Ptr.getIntegerRepresentation();
2510 QualType ElemType = Ptr.asIntPointer().getPointeeType();
2511 uint64_t ElemSize =
2512 (ElemType.isNull() || ElemType->isVoidType())
2513 ? 1u
2514 : S.getASTContext().getTypeSizeInChars(T: ElemType).getQuantity();
2515 uint64_t O = static_cast<uint64_t>(Offset) * ElemSize;
2516 if constexpr (Op == ArithOp::Add) {
2517 return Pointer(V + O, Ptr.asIntPointer().getType());
2518 } else
2519 return Pointer(V - O, Ptr.asIntPointer().getType());
2520 } else if (Ptr.isFunctionPointer()) {
2521 uint64_t O = static_cast<uint64_t>(Offset);
2522 uint64_t N;
2523 if constexpr (Op == ArithOp::Add)
2524 N = Ptr.getByteOffset() + O;
2525 else
2526 N = Ptr.getByteOffset() - O;
2527
2528 if (N > 1)
2529 diagnoseArrayIndex(S, OpPC, Index: APSInt::getUnsigned(X: N), NumElems: 0, /*IsArray=*/IsArray: false);
2530 return Pointer(Ptr.asFunctionPointer().Func, N);
2531 } else if (Ptr.isStringPointer()) {
2532 int64_t NewOffset;
2533 if constexpr (Op == ArithOp::Add)
2534 NewOffset = Ptr.getByteOffset() + static_cast<int64_t>(Offset);
2535 else
2536 NewOffset = Ptr.getByteOffset() - static_cast<int64_t>(Offset);
2537 if (NewOffset < 0 ||
2538 NewOffset > (Ptr.asStringPointer().getLiteral()->getLength() + 1)) {
2539 diagnoseArrayIndex(S, OpPC, Index: APSInt::get(X: NewOffset),
2540 NumElems: (Ptr.asStringPointer().getLiteral()->getLength() + 1));
2541 return std::nullopt;
2542 }
2543 return Pointer(Ptr.asStringPointer(), NewOffset);
2544 } else if (!Ptr.isBlockPointer()) {
2545 return std::nullopt;
2546 }
2547
2548 assert(Ptr.isBlockPointer());
2549 assert(!Ptr.isZero());
2550 PtrView P = Ptr.view();
2551
2552 // Arrays of unknown bounds cannot have pointers into them.
2553 if (!CheckArray(S, OpPC, Ptr))
2554 return std::nullopt;
2555
2556 uint64_t MaxIndex = static_cast<uint64_t>(P.getNumElems());
2557 uint64_t Index;
2558 if (P.isOnePastEnd())
2559 Index = MaxIndex;
2560 else
2561 Index = P.getIndex();
2562
2563 bool Invalid = false;
2564 // Helper to report an invalid offset, computed as APSInt.
2565 auto DiagInvalidOffset = [&]() -> void {
2566 const unsigned Bits = Offset.bitWidth();
2567 APSInt APOffset(Offset.toAPSInt().extend(Bits + 2), /*IsUnsigend=*/false);
2568 APSInt APIndex(APInt(Bits + 2, Index, /*IsSigned=*/true),
2569 /*IsUnsigned=*/false);
2570 APSInt NewIndex =
2571 (Op == ArithOp::Add) ? (APIndex + APOffset) : (APIndex - APOffset);
2572 diagnoseArrayIndex(S, OpPC, Index: NewIndex, NumElems: MaxIndex, IsArray: P.inArray());
2573 Invalid = true;
2574 };
2575
2576 uint64_t IOffset = static_cast<uint64_t>(Offset);
2577 uint64_t MaxOffset = MaxIndex - Index;
2578
2579 if constexpr (Op == ArithOp::Add) {
2580 // If the new offset would be negative, bail out.
2581 if (Offset.isNegative() && (Offset.isMin() || -IOffset > Index))
2582 DiagInvalidOffset();
2583
2584 // If the new offset would be out of bounds, bail out.
2585 if (Offset.isPositive() && IOffset > MaxOffset)
2586 DiagInvalidOffset();
2587 } else {
2588 // If the new offset would be negative, bail out.
2589 if (Offset.isPositive() && Index < IOffset)
2590 DiagInvalidOffset();
2591
2592 // If the new offset would be out of bounds, bail out.
2593 if (Offset.isNegative() && (Offset.isMin() || -IOffset > MaxOffset))
2594 DiagInvalidOffset();
2595 }
2596
2597 if (Invalid && (S.getLangOpts().CPlusPlus || P.inArray()))
2598 return std::nullopt;
2599
2600 // Offset is valid - compute it on unsigned.
2601 int64_t WideIndex = static_cast<int64_t>(Index);
2602 int64_t WideOffset = static_cast<int64_t>(Offset);
2603 int64_t Result;
2604 if constexpr (Op == ArithOp::Add)
2605 Result = WideIndex + WideOffset;
2606 else
2607 Result = WideIndex - WideOffset;
2608
2609 // When the pointer is one-past-end, going back to index 0 is the only
2610 // useful thing we can do. Any other index has been diagnosed before and
2611 // we don't get here.
2612 if (Result == 0 && P.isOnePastEnd()) {
2613 if (P.getFieldDesc()->isArray())
2614 return Pointer(P.atIndex(Idx: 0));
2615 return Pointer(P.Pointee, P.Base);
2616 }
2617
2618 return Pointer(P.atIndex(Idx: static_cast<uint64_t>(Result)));
2619}
2620
2621std::optional<Pointer> addSubOffsetOpaque(InterpState &S, CodePtr OpPC,
2622 const Pointer &Ptr, APSInt &&Offset,
2623 ArithOp Op);
2624
2625template <PrimType Name, class T = typename PrimConv<Name>::T>
2626bool AddOffset(InterpState &S, CodePtr OpPC) {
2627 const T &Offset = S.Stk.pop<T>();
2628 const Pointer &Ptr = S.Stk.pop<Pointer>().expand();
2629
2630 // Adding zero to a non-array doesn't change anything.
2631 if (Offset.isZero() && !Ptr.inArray()) {
2632 S.Stk.push<Pointer>(Args: Ptr);
2633 return true;
2634 }
2635
2636 if (Ptr.isOpaquePointer()) {
2637 if (std::optional<Pointer> Result =
2638 addSubOffsetOpaque(S, OpPC, Ptr, Offset.toAPSInt(), ArithOp::Add)) {
2639 S.Stk.push<Pointer>(Args&: *Result);
2640 return true;
2641 }
2642 return false;
2643 }
2644
2645 if (!CheckNull(S, OpPC, Ptr, CSK: CSK_ArrayIndex) && S.getLangOpts().CPlusPlus)
2646 return false;
2647
2648 if (std::optional<Pointer> Result =
2649 OffsetHelper<T, ArithOp::Add>(S, OpPC, Offset, Ptr)) {
2650 S.Stk.push<Pointer>(Args: Result->narrow());
2651 return true;
2652 }
2653 return false;
2654}
2655
2656template <PrimType Name, class T = typename PrimConv<Name>::T>
2657bool SubOffset(InterpState &S, CodePtr OpPC) {
2658 const T &Offset = S.Stk.pop<T>();
2659 const Pointer &Ptr = S.Stk.pop<Pointer>().expand();
2660
2661 // Subtracting zero from a non-array doesn't change anything.
2662 if (Offset.isZero() && !Ptr.inArray()) {
2663 S.Stk.push<Pointer>(Args: Ptr);
2664 return true;
2665 }
2666
2667 if (Ptr.isOpaquePointer()) {
2668 if (std::optional<Pointer> Result =
2669 addSubOffsetOpaque(S, OpPC, Ptr, Offset.toAPSInt(), ArithOp::Sub)) {
2670 S.Stk.push<Pointer>(Args&: *Result);
2671 return true;
2672 }
2673 return false;
2674 }
2675
2676 if (!CheckNull(S, OpPC, Ptr, CSK: CSK_ArrayIndex) && S.getLangOpts().CPlusPlus)
2677 return false;
2678
2679 if (std::optional<Pointer> Result =
2680 OffsetHelper<T, ArithOp::Sub>(S, OpPC, Offset, Ptr)) {
2681 S.Stk.push<Pointer>(Args: Result->narrow());
2682 return true;
2683 }
2684 return false;
2685}
2686
2687inline bool GetOpaquePtr(InterpState &S, DeclOrExpr DOE,
2688 bool ConstexprUnknown) {
2689 S.Stk.push<Pointer>(Args&: DOE, Args&: ConstexprUnknown);
2690 return true;
2691}
2692
2693template <ArithOp Op>
2694static inline bool IncDecPtrHelper(InterpState &S, CodePtr OpPC,
2695 const Pointer &Ptr) {
2696 if (!Ptr.isDereferencable())
2697 return false;
2698
2699 using OneT = Char<false>;
2700
2701 const Pointer &P = Ptr.deref<Pointer>();
2702 if (!CheckNull(S, OpPC, Ptr: P, CSK: CSK_ArrayIndex))
2703 return false;
2704
2705 // Get the current value on the stack.
2706 S.Stk.push<Pointer>(Args: P);
2707
2708 if (P.isOpaquePointer()) {
2709 if (std::optional<Pointer> Result =
2710 addSubOffsetOpaque(S, OpPC, Ptr: P, Offset: APSInt(APInt(1, 1), true), Op)) {
2711 Ptr.deref<Pointer>() = *Result;
2712 return true;
2713 }
2714 return false;
2715 }
2716
2717 // Now the current Ptr again and a constant 1.
2718 OneT One = OneT::from(t: 1);
2719 if (std::optional<Pointer> Result = OffsetHelper<OneT, Op>(S, OpPC, One, P)) {
2720 // Store the new value.
2721 Ptr.deref<Pointer>() = Result->narrow();
2722 return true;
2723 }
2724 return false;
2725}
2726
2727static inline bool IncPtr(InterpState &S, CodePtr OpPC) {
2728 const Pointer &Ptr = S.Stk.pop<Pointer>();
2729
2730 if (!Ptr.isInitialized())
2731 return diagnoseUninitialized(S, OpPC, Ptr, AK: AK_Increment);
2732
2733 return IncDecPtrHelper<ArithOp::Add>(S, OpPC, Ptr);
2734}
2735
2736static inline bool DecPtr(InterpState &S, CodePtr OpPC) {
2737 const Pointer &Ptr = S.Stk.pop<Pointer>();
2738
2739 if (!Ptr.isInitialized())
2740 return diagnoseUninitialized(S, OpPC, Ptr, AK: AK_Decrement);
2741
2742 return IncDecPtrHelper<ArithOp::Sub>(S, OpPC, Ptr);
2743}
2744
2745/// 1) Pops a Pointer from the stack.
2746/// 2) Pops another Pointer from the stack.
2747/// 3) Pushes the difference of the indices of the two pointers on the stack.
2748template <PrimType Name, class T = typename PrimConv<Name>::T>
2749inline bool SubPtr(InterpState &S, CodePtr OpPC, uint32_t ElemSize) {
2750 const Pointer &LHS = S.Stk.pop<Pointer>().expand();
2751 const Pointer &RHS = S.Stk.pop<Pointer>().expand();
2752
2753 if (LHS.pointsToLabel() || RHS.pointsToLabel()) {
2754 if constexpr (isIntegralOrPointer<T>()) {
2755 const AddrLabelExpr *LHSAddrExpr = LHS.getPointedToLabel();
2756 const AddrLabelExpr *RHSAddrExpr = RHS.getPointedToLabel();
2757 if (!LHSAddrExpr || !RHSAddrExpr) {
2758 S.FFDiag(SI: S.Current->getSource(PC: OpPC),
2759 DiagId: diag::note_constexpr_pointer_arith_unspecified)
2760 << LHS.toDiagnosticString(Ctx: S.getASTContext())
2761 << RHS.toDiagnosticString(Ctx: S.getASTContext());
2762 return false;
2763 }
2764
2765 if (LHSAddrExpr->getLabel()->getDeclContext() !=
2766 RHSAddrExpr->getLabel()->getDeclContext())
2767 return Invalid(S, OpPC);
2768
2769 S.Stk.push<T>(LHSAddrExpr, RHSAddrExpr);
2770 return true;
2771 }
2772 // Can't represent an address-label-diff in these types.
2773 return false;
2774 }
2775
2776 if (!Pointer::hasSameBase(A: LHS, B: RHS)) {
2777 S.FFDiag(SI: S.Current->getSource(PC: OpPC),
2778 DiagId: diag::note_constexpr_pointer_arith_unspecified)
2779 << LHS.toDiagnosticString(Ctx: S.getASTContext())
2780 << RHS.toDiagnosticString(Ctx: S.getASTContext());
2781 return false;
2782 }
2783
2784 if (ElemSize == 0) {
2785 QualType PtrT = S.getASTContext().getBaseElementType(QT: LHS.getType());
2786 QualType ArrayTy = S.getASTContext().getConstantArrayType(
2787 EltTy: PtrT, ArySize: APInt::getZero(numBits: 1), SizeExpr: nullptr, ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0);
2788 S.FFDiag(SI: S.Current->getSource(PC: OpPC),
2789 DiagId: diag::note_constexpr_pointer_subtraction_zero_size)
2790 << ArrayTy;
2791
2792 return false;
2793 }
2794
2795 if (LHS == RHS) {
2796 S.Stk.push<T>();
2797 return true;
2798 }
2799
2800 // C++11 [expr.add]p6:
2801 // Unless both pointers point to elements of the same array object, or
2802 // one past the last element of the array object, the behavior is
2803 // undefined.
2804 if (LHS.isBlockPointer() && !Pointer::elemsOfSameArray(A: LHS, B: RHS))
2805 S.CCEDiag(SI: S.Current->getSource(PC: OpPC),
2806 DiagId: diag::note_constexpr_pointer_subtraction_not_same_array);
2807
2808 std::optional<size_t> VL = LHS.computeLayoutOffset(ASTCtx: S.getASTContext());
2809 if (!VL)
2810 return false;
2811 std::optional<size_t> VR = RHS.computeLayoutOffset(ASTCtx: S.getASTContext());
2812 if (!VR)
2813 return false;
2814
2815 // We allow (VL - VR) / Elemsize to have non-zero remainder. This happens for
2816 // invalid expressions where LHS and RHS are of different types.
2817 int64_t R64 =
2818 (static_cast<int64_t>(*VL) - static_cast<int64_t>(*VR)) / ElemSize;
2819 if (static_cast<int64_t>(T::from(R64)) != R64)
2820 return handleOverflow(S, OpPC, SrcValue: R64);
2821
2822 S.Stk.push<T>(T::from(R64));
2823 return true;
2824}
2825
2826inline bool EnableLocal(InterpState &S, uint32_t I) {
2827 assert(!S.Current->isLocalEnabled(I));
2828 S.Current->enableLocal(Offset: I);
2829 return true;
2830}
2831
2832inline bool GetLocalEnabled(InterpState &S, uint32_t I) {
2833 assert(S.Current);
2834 S.Stk.push<bool>(Args: S.Current->isLocalEnabled(Offset: I));
2835 return true;
2836}
2837
2838//===----------------------------------------------------------------------===//
2839// Cast, CastFP
2840//===----------------------------------------------------------------------===//
2841
2842template <PrimType TIn, PrimType TOut> bool Cast(InterpState &S, CodePtr OpPC) {
2843 using T = typename PrimConv<TIn>::T;
2844 using U = typename PrimConv<TOut>::T;
2845
2846 auto In = S.Stk.pop<T>();
2847
2848 if constexpr (isIntegralOrPointer<T>()) {
2849 if (In.getKind() != IntegralKind::Number &&
2850 In.getKind() != IntegralKind::AddrLabelDiff) {
2851 if (!CheckIntegralAddressCast(S, OpPC, U::bitWidth()))
2852 return Invalid(S, OpPC);
2853 } else if (In.getKind() == IntegralKind::AddrLabelDiff) {
2854 // Allow casts of address-of-label differences if they are no-ops
2855 // or narrowing, if the result is at least 32 bits wide.
2856 // (The narrowing case isn't actually guaranteed to
2857 // be constant-evaluatable except in some narrow cases which are hard
2858 // to detect here. We let it through on the assumption the user knows
2859 // what they are doing.)
2860 if (!(U::bitWidth() >= 32 && U::bitWidth() <= In.bitWidth()))
2861 return false;
2862 }
2863 }
2864
2865 S.Stk.push<U>(U::from(In));
2866 return true;
2867}
2868
2869/// 1) Pops a Floating from the stack.
2870/// 2) Pushes a new floating on the stack that uses the given semantics.
2871inline bool CastFP(InterpState &S, const llvm::fltSemantics *Sem,
2872 llvm::RoundingMode RM) {
2873 Floating F = S.Stk.pop<Floating>();
2874 Floating Result = S.allocFloat(Sem: *Sem);
2875 F.toSemantics(Sem, RM, Result: &Result);
2876 S.Stk.push<Floating>(Args&: Result);
2877 return true;
2878}
2879
2880inline bool CastFixedPoint(InterpState &S, CodePtr OpPC, uint32_t FPS) {
2881 FixedPointSemantics TargetSemantics =
2882 FixedPointSemantics::getFromOpaqueInt(FPS);
2883 const auto &Source = S.Stk.pop<FixedPoint>();
2884
2885 bool Overflow;
2886 FixedPoint Result = Source.toSemantics(Sem: TargetSemantics, Overflow: &Overflow);
2887
2888 if (Overflow && !handleFixedPointOverflow(S, OpPC, FP: Result))
2889 return false;
2890
2891 S.Stk.push<FixedPoint>(Args&: Result);
2892 return true;
2893}
2894
2895/// Like Cast(), but we cast to an arbitrary-bitwidth integral, so we need
2896/// to know what bitwidth the result should be.
2897template <PrimType Name, class T = typename PrimConv<Name>::T>
2898bool CastAP(InterpState &S, uint32_t BitWidth) {
2899 T Source = S.Stk.pop<T>();
2900
2901 if constexpr (isIntegralOrPointer<T>()) {
2902 if (!Source.isNumber())
2903 return false;
2904 }
2905
2906 auto Result = S.allocAP<IntegralAP<false>>(BitWidth);
2907 // Copy data.
2908 {
2909 APInt SourceInt = Source.toAPSInt().extOrTrunc(BitWidth);
2910 Result.copy(V: SourceInt);
2911 }
2912 S.Stk.push<IntegralAP<false>>(Args&: Result);
2913 return true;
2914}
2915
2916template <PrimType Name, class T = typename PrimConv<Name>::T>
2917bool CastAPS(InterpState &S, uint32_t BitWidth) {
2918 T Source = S.Stk.pop<T>();
2919
2920 if constexpr (isIntegralOrPointer<T>()) {
2921 if (!Source.isNumber())
2922 return false;
2923 }
2924
2925 auto Result = S.allocAP<IntegralAP<true>>(BitWidth);
2926 // Copy data.
2927 {
2928 APInt SourceInt = Source.toAPSInt().extOrTrunc(BitWidth);
2929 Result.copy(V: SourceInt);
2930 }
2931 S.Stk.push<IntegralAP<true>>(Args&: Result);
2932 return true;
2933}
2934
2935// Cast an AP integer to Sint64 for use as an offsetof array index, failing
2936// constant evaluation if the value is negative or too large to fit in Sint64
2937// (i.e. truncation would change the value).
2938template <PrimType Name, class T = typename PrimConv<Name>::T>
2939bool CastAPToOffsetIndex(InterpState &S, CodePtr OpPC) {
2940 T Val = S.Stk.pop<T>();
2941 if (Val.isNegative() || Val.getActiveBits() > 63)
2942 return Invalid(S, OpPC);
2943 S.Stk.push<Integral<64, true>>(
2944 Args: Integral<64, true>::from(V: static_cast<uint64_t>(Val)));
2945 return true;
2946}
2947
2948template <PrimType Name, class T = typename PrimConv<Name>::T>
2949bool CastIntegralFloating(InterpState &S, CodePtr OpPC,
2950 const llvm::fltSemantics *Sem, uint32_t FPOI) {
2951 const T &From = S.Stk.pop<T>();
2952
2953 if constexpr (isIntegralOrPointer<T>()) {
2954 if (!From.isNumber())
2955 return false;
2956 }
2957
2958 APSInt FromAP = From.toAPSInt();
2959
2960 FPOptions FPO = FPOptions::getFromOpaqueInt(Value: FPOI);
2961 Floating Result = S.allocFloat(Sem: *Sem);
2962 auto Status =
2963 Floating::fromIntegral(Val: FromAP, Sem: *Sem, RM: getRoundingMode(FPO), Result: &Result);
2964 S.Stk.push<Floating>(Args&: Result);
2965
2966 return CheckFloatResult(S, OpPC, Result, Status, FPO);
2967}
2968
2969template <PrimType Name, class T = typename PrimConv<Name>::T>
2970bool CastFloatingIntegral(InterpState &S, CodePtr OpPC, uint32_t FPOI) {
2971 const Floating &F = S.Stk.pop<Floating>();
2972
2973 if constexpr (std::is_same_v<T, Boolean>) {
2974 S.Stk.push<T>(T(F.isNonZero()));
2975 return true;
2976 } else {
2977 APSInt Result(std::max(8u, T::bitWidth()),
2978 /*IsUnsigned=*/!T::isSigned());
2979 auto Status = F.convertToInteger(Result);
2980
2981 // Float-to-Integral overflow check.
2982 if ((Status & APFloat::opStatus::opInvalidOp)) {
2983 const Expr *E = S.Current->getExpr(PC: OpPC);
2984 QualType Type = E->getType();
2985
2986 S.CCEDiag(E, DiagId: diag::note_constexpr_overflow) << F.getAPFloat() << Type;
2987 if (S.noteUndefinedBehavior()) {
2988 S.Stk.push<T>(T(Result));
2989 return true;
2990 }
2991 return false;
2992 }
2993
2994 FPOptions FPO = FPOptions::getFromOpaqueInt(Value: FPOI);
2995 S.Stk.push<T>(T(Result));
2996 return CheckFloatResult(S, OpPC, Result: F, Status, FPO);
2997 }
2998}
2999
3000inline bool AddrOf(InterpState &S, CodePtr OpPC) {
3001 const Pointer Ptr = S.Stk.pop<Pointer>();
3002
3003 if (Ptr.isOpaquePointer()) {
3004 const OpaquePointer &OP = Ptr.asOpaquePointer();
3005 QualType T = QualType(OP.FieldType.getPointer(), 0);
3006
3007 T = S.getASTContext().getPointerType(T);
3008 S.Stk.push<Pointer>(Args: OP.withFieldType(FieldTy: T.getTypePtr(), PastEnd: OP.isOnePastEnd()));
3009 } else {
3010 S.Stk.push<Pointer>(Args: Ptr);
3011 }
3012
3013 return true;
3014}
3015
3016bool CheckPointerToIntegralCast(InterpState &S, CodePtr OpPC,
3017 const Pointer &Ptr, unsigned BitWidth);
3018bool CheckIntegralAddressCast(InterpState &S, CodePtr OpPC, unsigned BitWidth);
3019bool CastPointerIntegralAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth);
3020bool CastPointerIntegralAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth);
3021
3022template <PrimType Name, class T = typename PrimConv<Name>::T>
3023bool CastPointerIntegral(InterpState &S, CodePtr OpPC) {
3024 const Pointer &Ptr = S.Stk.pop<Pointer>();
3025 if (!CheckPointerToIntegralCast(S, OpPC, Ptr, T::bitWidth()))
3026 return Invalid(S, OpPC);
3027
3028 if constexpr (std::is_same_v<T, Boolean>) {
3029 S.Stk.push<T>(T::from(Ptr.getIntegerRepresentation()));
3030 } else if constexpr (isIntegralOrPointer<T>()) {
3031 if (Ptr.isBlockPointer()) {
3032 S.Stk.push<T>(IntegralKind::BlockAddress, Ptr.block(), /*Offset=*/0);
3033 } else if (Ptr.isOpaquePointer()) {
3034 if (const Expr *BaseExpr = Ptr.asOpaquePointer().getBaseExpr()) {
3035 IntegralKind Kind = IntegralKind::ExprAddress;
3036 if (isa<AddrLabelExpr>(Val: BaseExpr))
3037 Kind = IntegralKind::LabelAddress;
3038 S.Stk.push<T>(Kind, BaseExpr, 0);
3039 } else {
3040 S.Stk.push<T>(IntegralKind::Address,
3041 Ptr.asOpaquePointer().Base.asVarDecl(), 0);
3042 }
3043
3044 } else if (Ptr.isFunctionPointer()) {
3045 const void *FuncDecl = Ptr.asFunctionPointer().Func->getDecl();
3046 S.Stk.push<T>(IntegralKind::FunctionAddress, FuncDecl, /*Offset=*/0);
3047 } else if (Ptr.isStringPointer()) {
3048 S.Stk.push<T>(IntegralKind::ExprAddress,
3049 (const void *)Ptr.asStringPointer().getLiteral(), 0);
3050 } else {
3051 S.Stk.push<T>(T::from(Ptr.getIntegerRepresentation()));
3052 }
3053 } else {
3054 S.Stk.push<T>(T::from(Ptr.getIntegerRepresentation()));
3055 }
3056 return true;
3057}
3058
3059template <PrimType Name, class T = typename PrimConv<Name>::T>
3060static inline bool CastIntegralFixedPoint(InterpState &S, CodePtr OpPC,
3061 uint32_t FPS) {
3062 const T &Int = S.Stk.pop<T>();
3063
3064 FixedPointSemantics Sem = FixedPointSemantics::getFromOpaqueInt(FPS);
3065
3066 bool Overflow;
3067 FixedPoint Result = FixedPoint::from(Int.toAPSInt(), Sem, &Overflow);
3068
3069 if (Overflow && !handleFixedPointOverflow(S, OpPC, FP: Result))
3070 return false;
3071
3072 S.Stk.push<FixedPoint>(Args&: Result);
3073 return true;
3074}
3075
3076static inline bool CastFloatingFixedPoint(InterpState &S, CodePtr OpPC,
3077 uint32_t FPS) {
3078 const auto &Float = S.Stk.pop<Floating>();
3079
3080 FixedPointSemantics Sem = FixedPointSemantics::getFromOpaqueInt(FPS);
3081
3082 bool Overflow;
3083 FixedPoint Result = FixedPoint::from(I: Float.getAPFloat(), Sem, Overflow: &Overflow);
3084
3085 if (Overflow && !handleFixedPointOverflow(S, OpPC, FP: Result))
3086 return false;
3087
3088 S.Stk.push<FixedPoint>(Args&: Result);
3089 return true;
3090}
3091
3092static inline bool CastFixedPointFloating(InterpState &S,
3093 const llvm::fltSemantics *Sem) {
3094 const auto &Fixed = S.Stk.pop<FixedPoint>();
3095 Floating Result = S.allocFloat(Sem: *Sem);
3096 Result.copy(F: Fixed.toFloat(Sem));
3097 S.Stk.push<Floating>(Args&: Result);
3098 return true;
3099}
3100
3101template <PrimType Name, class T = typename PrimConv<Name>::T>
3102static inline bool CastFixedPointIntegral(InterpState &S, CodePtr OpPC) {
3103 const auto &Fixed = S.Stk.pop<FixedPoint>();
3104
3105 bool Overflow;
3106 APSInt Int = Fixed.toInt(BitWidth: T::bitWidth(), Signed: T::isSigned(), Overflow: &Overflow);
3107
3108 if (Overflow && !handleOverflow(S, OpPC, SrcValue: Int))
3109 return false;
3110
3111 S.Stk.push<T>(Int);
3112 return true;
3113}
3114
3115static inline bool FnPtrCast(InterpState &S, CodePtr OpPC) {
3116 const SourceInfo &E = S.Current->getSource(PC: OpPC);
3117 S.CCEDiag(SI: E, DiagId: diag::note_constexpr_invalid_cast)
3118 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
3119 << S.getLangOpts().CPlusPlus << S.Current->getRange(PC: OpPC);
3120 return true;
3121}
3122
3123bool PtrPtrCast(InterpState &S, CodePtr OpPC, bool SrcIsVoidPtr,
3124 const Type *TargetType);
3125
3126//===----------------------------------------------------------------------===//
3127// Zero, Nullptr
3128//===----------------------------------------------------------------------===//
3129
3130template <PrimType Name, class T = typename PrimConv<Name>::T>
3131bool Zero(InterpState &S) {
3132 S.Stk.push<T>(T::zero());
3133 return true;
3134}
3135
3136static inline bool ZeroIntAP(InterpState &S, uint32_t BitWidth) {
3137 auto Result = S.allocAP<IntegralAP<false>>(BitWidth);
3138 if (!Result.singleWord())
3139 std::memset(s: Result.Memory, c: 0, n: Result.numWords() * sizeof(uint64_t));
3140 S.Stk.push<IntegralAP<false>>(Args&: Result);
3141 return true;
3142}
3143
3144static inline bool ZeroIntAPS(InterpState &S, uint32_t BitWidth) {
3145 auto Result = S.allocAP<IntegralAP<true>>(BitWidth);
3146 if (!Result.singleWord())
3147 std::memset(s: Result.Memory, c: 0, n: Result.numWords() * sizeof(uint64_t));
3148 S.Stk.push<IntegralAP<true>>(Args&: Result);
3149 return true;
3150}
3151
3152template <PrimType Name, class T = typename PrimConv<Name>::T>
3153inline bool Null(InterpState &S, uint64_t Value, const Type *Ty) {
3154 // FIXME(perf): This is a somewhat often-used function and the value of a
3155 // null pointer is almost always 0.
3156 if constexpr (std::is_same_v<T, Pointer>)
3157 S.Stk.push<T>(Value, Ty, /*Offset=*/0, /*IsNull=*/true);
3158 else
3159 S.Stk.push<T>(Value, Ty);
3160 return true;
3161}
3162
3163inline bool CastAddressSpace(InterpState &S, CodePtr OpPC, uint64_t Value,
3164 const Type *Ty) {
3165 const Pointer Ptr = S.Stk.pop<Pointer>();
3166 if (Ptr.isZero())
3167 S.Stk.push<Pointer>(Args&: Value, Args&: Ty);
3168 else
3169 S.Stk.push<Pointer>(Args: Ptr);
3170 return true;
3171}
3172
3173template <PrimType Name, class T = typename PrimConv<Name>::T>
3174inline bool IsNonNull(InterpState &S) {
3175 const auto &P = S.Stk.pop<T>();
3176 if (P.isWeak())
3177 return false;
3178 S.Stk.push<Boolean>(Boolean::from(!P.isZero()));
3179 return true;
3180}
3181
3182//===----------------------------------------------------------------------===//
3183// This, ImplicitThis
3184//===----------------------------------------------------------------------===//
3185
3186inline bool This(InterpState &S, CodePtr OpPC) {
3187 // Cannot read 'this' in this mode.
3188 if (S.checkingPotentialConstantExpression())
3189 return false;
3190 if (!CheckThis(S, OpPC))
3191 return false;
3192 const Pointer &This = S.Current->getThis();
3193
3194 // Ensure the This pointer has been cast to the correct base.
3195 if (!This.isDummy()) {
3196 assert(isa<CXXMethodDecl>(S.Current->getFunction()->getDecl()));
3197 if (!This.isTypeidPointer()) {
3198 [[maybe_unused]] const Record *R = This.getRecord();
3199 if (!R)
3200 R = This.narrow().getRecord();
3201 if (!R)
3202 return false;
3203 assert(R->getDecl() ==
3204 cast<CXXMethodDecl>(S.Current->getFunction()->getDecl())
3205 ->getParent());
3206 }
3207 }
3208
3209 S.Stk.push<Pointer>(Args: This);
3210 return true;
3211}
3212
3213inline bool RVOPtr(InterpState &S) {
3214 assert(S.Current->getFunction()->hasRVO());
3215 if (S.checkingPotentialConstantExpression())
3216 return false;
3217 S.Stk.push<Pointer>(Args: S.Current->getRVOPtr());
3218 return true;
3219}
3220
3221//===----------------------------------------------------------------------===//
3222// Shr, Shl
3223//===----------------------------------------------------------------------===//
3224
3225template <class LT, class RT, ShiftDir Dir>
3226inline bool DoShift(InterpState &S, CodePtr OpPC, LT &LHS, RT &RHS,
3227 LT *Result) {
3228 static_assert(!needsAlloc<LT>());
3229 const unsigned Bits = LHS.bitWidth();
3230
3231 // OpenCL 6.3j: shift values are effectively % word size of LHS.
3232 if (S.getLangOpts().OpenCL)
3233 RT::bitAnd(RHS, RT::from(LHS.bitWidth() - 1, RHS.bitWidth()),
3234 RHS.bitWidth(), &RHS);
3235
3236 if (RHS.isNegative()) {
3237 // During constant-folding, a negative shift is an opposite shift. Such a
3238 // shift is not a constant expression.
3239 const SourceInfo &Loc = S.Current->getSource(PC: OpPC);
3240 S.CCEDiag(SI: Loc, DiagId: diag::note_constexpr_negative_shift) << RHS.toAPSInt();
3241 if (!S.noteUndefinedBehavior())
3242 return false;
3243
3244 RHS = RHS.isMin() ? RT(APSInt::getMaxValue(numBits: RHS.bitWidth(), Unsigned: false)) : -RHS;
3245
3246 return DoShift<LT, RT,
3247 Dir == ShiftDir::Left ? ShiftDir::Right : ShiftDir::Left>(
3248 S, OpPC, LHS, RHS, Result);
3249 }
3250
3251 if (!CheckShift<Dir>(S, OpPC, LHS, RHS, Bits))
3252 return false;
3253
3254 // Limit the shift amount to Bits - 1. If this happened,
3255 // it has already been diagnosed by CheckShift() above,
3256 // but we still need to handle it.
3257 // Note that we have to be extra careful here since we're doing the shift in
3258 // any case, but we need to adjust the shift amount or the way we do the shift
3259 // for the potential error cases.
3260 typename LT::AsUnsigned R;
3261 unsigned MaxShiftAmount = LHS.bitWidth() - 1;
3262 if constexpr (Dir == ShiftDir::Left) {
3263 if (Compare(RHS, RT::from(MaxShiftAmount, RHS.bitWidth())) ==
3264 ComparisonCategoryResult::Greater) {
3265 if (LHS.isNegative())
3266 R = LT::AsUnsigned::zero(LHS.bitWidth());
3267 else {
3268 RHS = RT::from(LHS.countLeadingZeros(), RHS.bitWidth());
3269 LT::AsUnsigned::shiftLeft(LT::AsUnsigned::from(LHS),
3270 LT::AsUnsigned::from(RHS, Bits), Bits, &R);
3271 }
3272 } else if (LHS.isNegative()) {
3273 if (LHS.isMin()) {
3274 R = LT::AsUnsigned::zero(LHS.bitWidth());
3275 } else {
3276 // If the LHS is negative, perform the cast and invert the result.
3277 typename LT::AsUnsigned LHSU = LT::AsUnsigned::from(-LHS);
3278 LT::AsUnsigned::shiftLeft(LHSU, LT::AsUnsigned::from(RHS, Bits), Bits,
3279 &R);
3280 R = -R;
3281 }
3282 } else {
3283 // The good case, a simple left shift.
3284 LT::AsUnsigned::shiftLeft(LT::AsUnsigned::from(LHS),
3285 LT::AsUnsigned::from(RHS, Bits), Bits, &R);
3286 }
3287 S.Stk.push<LT>(LT::from(R));
3288 return true;
3289 }
3290
3291 // Right shift.
3292 if (Compare(RHS, RT::from(MaxShiftAmount, RHS.bitWidth())) ==
3293 ComparisonCategoryResult::Greater) {
3294 R = LT::AsUnsigned::from(0);
3295 } else {
3296 // Do the shift on potentially signed LT, then convert to unsigned type.
3297 LT A;
3298 LT::shiftRight(LHS, LT::from(RHS, Bits), Bits, &A);
3299 R = LT::AsUnsigned::from(A);
3300 }
3301
3302 S.Stk.push<LT>(LT::from(R));
3303 return true;
3304}
3305
3306/// A version of DoShift that works on IntegralAP.
3307template <class LT, class RT, ShiftDir Dir>
3308inline bool DoShiftAP(InterpState &S, CodePtr OpPC, const APSInt &LHS,
3309 APSInt RHS, LT *Result) {
3310 const unsigned Bits = LHS.getBitWidth();
3311
3312 // OpenCL 6.3j: shift values are effectively % word size of LHS.
3313 if (S.getLangOpts().OpenCL)
3314 RHS &=
3315 APSInt(llvm::APInt(RHS.getBitWidth(), static_cast<uint64_t>(Bits - 1)),
3316 RHS.isUnsigned());
3317
3318 if (RHS.isNegative()) {
3319 // During constant-folding, a negative shift is an opposite shift. Such a
3320 // shift is not a constant expression.
3321 const SourceInfo &Loc = S.Current->getSource(PC: OpPC);
3322 S.CCEDiag(SI: Loc, DiagId: diag::note_constexpr_negative_shift) << RHS; //.toAPSInt();
3323 if (!S.noteUndefinedBehavior())
3324 return false;
3325 return DoShiftAP<LT, RT,
3326 Dir == ShiftDir::Left ? ShiftDir::Right : ShiftDir::Left>(
3327 S, OpPC, LHS, -(RHS.extend(width: RHS.getBitWidth() + 1)), Result);
3328 }
3329
3330 if (!CheckShift<Dir>(S, OpPC, static_cast<LT>(LHS), static_cast<RT>(RHS),
3331 Bits))
3332 return false;
3333
3334 unsigned SA = (unsigned)RHS.getLimitedValue(Limit: Bits - 1);
3335 if constexpr (Dir == ShiftDir::Left) {
3336 if constexpr (needsAlloc<LT>())
3337 Result->copy(LHS << SA);
3338 else
3339 *Result = LT(LHS << SA);
3340 } else {
3341 if constexpr (needsAlloc<LT>())
3342 Result->copy(LHS >> SA);
3343 else
3344 *Result = LT(LHS >> SA);
3345 }
3346
3347 S.Stk.push<LT>(*Result);
3348 return true;
3349}
3350
3351template <PrimType NameL, PrimType NameR>
3352inline bool Shr(InterpState &S, CodePtr OpPC) {
3353 using LT = typename PrimConv<NameL>::T;
3354 using RT = typename PrimConv<NameR>::T;
3355 auto RHS = S.Stk.pop<RT>();
3356 auto LHS = S.Stk.pop<LT>();
3357
3358 if constexpr (needsAlloc<LT>() || needsAlloc<RT>()) {
3359 LT Result;
3360 if constexpr (needsAlloc<LT>())
3361 Result = S.allocAP<LT>(LHS.bitWidth());
3362 return DoShiftAP<LT, RT, ShiftDir::Right>(S, OpPC, LHS.toAPSInt(),
3363 RHS.toAPSInt(), &Result);
3364 } else {
3365 LT Result;
3366 return DoShift<LT, RT, ShiftDir::Right>(S, OpPC, LHS, RHS, &Result);
3367 }
3368}
3369
3370template <PrimType NameL, PrimType NameR>
3371inline bool Shl(InterpState &S, CodePtr OpPC) {
3372 using LT = typename PrimConv<NameL>::T;
3373 using RT = typename PrimConv<NameR>::T;
3374 auto RHS = S.Stk.pop<RT>();
3375 auto LHS = S.Stk.pop<LT>();
3376
3377 if constexpr (needsAlloc<LT>() || needsAlloc<RT>()) {
3378 LT Result;
3379 if constexpr (needsAlloc<LT>())
3380 Result = S.allocAP<LT>(LHS.bitWidth());
3381 return DoShiftAP<LT, RT, ShiftDir::Left>(S, OpPC, LHS.toAPSInt(),
3382 RHS.toAPSInt(), &Result);
3383 } else {
3384 LT Result;
3385 return DoShift<LT, RT, ShiftDir::Left>(S, OpPC, LHS, RHS, &Result);
3386 }
3387}
3388
3389static inline bool ShiftFixedPoint(InterpState &S, CodePtr OpPC, bool Left) {
3390 const auto &RHS = S.Stk.pop<FixedPoint>();
3391 const auto &LHS = S.Stk.pop<FixedPoint>();
3392 llvm::FixedPointSemantics LHSSema = LHS.getSemantics();
3393
3394 unsigned ShiftBitWidth =
3395 LHSSema.getWidth() - (unsigned)LHSSema.hasUnsignedPadding() - 1;
3396
3397 // Embedded-C 4.1.6.2.2:
3398 // The right operand must be nonnegative and less than the total number
3399 // of (nonpadding) bits of the fixed-point operand ...
3400 if (RHS.isNegative()) {
3401 S.CCEDiag(Loc: S.Current->getLocation(PC: OpPC), DiagId: diag::note_constexpr_negative_shift)
3402 << RHS.toAPSInt();
3403 } else if (static_cast<unsigned>(RHS.toAPSInt().getLimitedValue(
3404 Limit: ShiftBitWidth)) != RHS.toAPSInt()) {
3405 const Expr *E = S.Current->getExpr(PC: OpPC);
3406 S.CCEDiag(E, DiagId: diag::note_constexpr_large_shift)
3407 << RHS.toAPSInt() << E->getType() << ShiftBitWidth;
3408 }
3409
3410 FixedPoint Result;
3411 if (Left) {
3412 if (FixedPoint::shiftLeft(A: LHS, B: RHS, OpBits: ShiftBitWidth, R: &Result) &&
3413 !handleFixedPointOverflow(S, OpPC, FP: Result))
3414 return false;
3415 } else {
3416 if (FixedPoint::shiftRight(A: LHS, B: RHS, OpBits: ShiftBitWidth, R: &Result) &&
3417 !handleFixedPointOverflow(S, OpPC, FP: Result))
3418 return false;
3419 }
3420
3421 S.Stk.push<FixedPoint>(Args&: Result);
3422 return true;
3423}
3424
3425//===----------------------------------------------------------------------===//
3426// NoRet
3427//===----------------------------------------------------------------------===//
3428PRESERVE_NONE inline bool NoRet(InterpState &S) {
3429 SourceLocation EndLoc = S.Current->getCallee()->getEndLoc();
3430 S.FFDiag(Loc: EndLoc, DiagId: diag::note_constexpr_no_return);
3431 return false;
3432}
3433
3434//===----------------------------------------------------------------------===//
3435// NarrowPtr, ExpandPtr
3436//===----------------------------------------------------------------------===//
3437
3438inline bool NarrowPtr(InterpState &S) {
3439 const Pointer &Ptr = S.Stk.pop<Pointer>();
3440 S.Stk.push<Pointer>(Args: Ptr.narrow());
3441 return true;
3442}
3443
3444inline bool ExpandPtr(InterpState &S) {
3445 const Pointer &Ptr = S.Stk.pop<Pointer>();
3446 if (Ptr.isBlockPointer())
3447 S.Stk.push<Pointer>(Args: Ptr.expand());
3448 else
3449 S.Stk.push<Pointer>(Args: Ptr);
3450 return true;
3451}
3452
3453// Implementation for ArrayElemPtr and ArrayElemPtrPop ops.
3454template <typename T>
3455inline bool arrayElemPtr(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
3456 const T &Offset) {
3457 if (Ptr.isOpaquePointer()) {
3458 if (S.inConstantContext() && !Offset.isZero() &&
3459 !CheckArray(S, OpPC, Ptr)) {
3460 return false;
3461 }
3462 return arrayElemPtrOpaque(S, OpPC, Ptr, Offset.toAPSInt());
3463 }
3464
3465 if (Offset.isZero()) {
3466 if (const Descriptor *Desc = Ptr.getFieldDesc();
3467 Desc && Desc->isArray() && Ptr.getIndex() == 0) {
3468 S.Stk.push<Pointer>(Args: Ptr.atIndex(Idx: 0).narrow());
3469 return true;
3470 }
3471 S.Stk.push<Pointer>(Args: Ptr.narrow());
3472 return true;
3473 }
3474
3475 assert(!Offset.isZero());
3476
3477 if (std::optional<Pointer> Result =
3478 OffsetHelper<T, ArithOp::Add>(S, OpPC, Offset, Ptr)) {
3479 S.Stk.push<Pointer>(Args: Result->narrow());
3480 return true;
3481 }
3482 return false;
3483}
3484
3485// 1) Pops an integral value from the stack
3486// 2) Peeks a pointer
3487// 3) Pushes a new pointer that's a narrowed array
3488// element of the peeked pointer with the value
3489// from 1) added as offset.
3490//
3491// This leaves the original pointer on the stack and pushes a new one
3492// with the offset applied and narrowed.
3493template <PrimType Name, class T = typename PrimConv<Name>::T>
3494inline bool ArrayElemPtr(InterpState &S, CodePtr OpPC) {
3495 const T &Offset = S.Stk.pop<T>();
3496 const Pointer &Ptr = S.Stk.peek<Pointer>();
3497
3498 return arrayElemPtr<T>(S, OpPC, Ptr, Offset);
3499}
3500
3501template <PrimType Name, class T = typename PrimConv<Name>::T>
3502inline bool ArrayElemPtrPop(InterpState &S, CodePtr OpPC) {
3503 const T &Offset = S.Stk.pop<T>();
3504 const Pointer &Ptr = S.Stk.pop<Pointer>();
3505
3506 return arrayElemPtr<T>(S, OpPC, Ptr, Offset);
3507}
3508
3509template <PrimType Name, class T = typename PrimConv<Name>::T>
3510inline bool ArrayElem(InterpState &S, CodePtr OpPC, uint32_t Index) {
3511 const Pointer &Ptr = S.Stk.peek<Pointer>();
3512
3513 if (!CheckLoad(S, OpPC, Ptr))
3514 return false;
3515
3516 assert(Ptr.atIndex(Index).getFieldDesc()->getPrimType() == Name);
3517 S.Stk.push<T>(Ptr.elem<T>(Index));
3518 return true;
3519}
3520
3521template <PrimType Name, class T = typename PrimConv<Name>::T>
3522inline bool ArrayElemPop(InterpState &S, CodePtr OpPC, uint32_t Index) {
3523 const Pointer &Ptr = S.Stk.pop<Pointer>();
3524
3525 if (!CheckLoad(S, OpPC, Ptr))
3526 return false;
3527
3528 assert(Ptr.atIndex(Index).getFieldDesc()->getPrimType() == Name);
3529 S.Stk.push<T>(Ptr.elem<T>(Index));
3530 return true;
3531}
3532
3533template <PrimType Name, class T = typename PrimConv<Name>::T>
3534inline bool CopyArray(InterpState &S, CodePtr OpPC, uint32_t SrcIndex,
3535 uint32_t DestIndex, uint32_t Size) {
3536 const auto &SrcPtr = S.Stk.pop<Pointer>();
3537 const auto &DestPtr = S.Stk.peek<Pointer>();
3538
3539 if (SrcPtr.isDummy() || DestPtr.isDummy())
3540 return false;
3541
3542 if (!SrcPtr.isBlockPointer() || !DestPtr.isBlockPointer())
3543 return false;
3544
3545 const Descriptor *SrcDesc = SrcPtr.getFieldDesc();
3546 const Descriptor *DestDesc = DestPtr.getFieldDesc();
3547 if (!SrcDesc->isPrimitiveArray() || !DestDesc->isPrimitiveArray() ||
3548 SrcDesc->getPrimType() != Name || DestDesc->getPrimType() != Name)
3549 return false;
3550
3551 for (uint32_t I = 0; I != Size; ++I) {
3552 PtrView SP = SrcPtr.view().atIndex(Idx: SrcIndex + I);
3553
3554 if (!CheckLoad(S, OpPC, Ptr: SP))
3555 return false;
3556
3557 DestPtr.elem<T>(DestIndex + I) = SrcPtr.elem<T>(SrcIndex + I);
3558 DestPtr.initializeElement(Index: DestIndex + I);
3559 }
3560 return true;
3561}
3562
3563/// Just takes a pointer and checks if it's an incomplete
3564/// array type.
3565inline bool ArrayDecay(InterpState &S, CodePtr OpPC) {
3566 const Pointer &Ptr = S.Stk.pop<Pointer>();
3567
3568 if (Ptr.isZero()) {
3569 S.Stk.push<Pointer>(Args: Ptr);
3570 return true;
3571 }
3572
3573 if (!Ptr.isZeroSizeArray()) {
3574 if (!CheckRange(S, OpPC, Ptr, CSK: CSK_ArrayToPointer))
3575 return false;
3576 }
3577
3578 if (Ptr.isRoot() || !Ptr.isUnknownSizeArray() || !S.inConstantContext()) {
3579 if (Ptr.isBlockPointer()) {
3580 if (!Ptr.isArrayRoot())
3581 return false;
3582 S.Stk.push<Pointer>(Args: Ptr.atIndex(Idx: 0).narrow());
3583 return true;
3584 }
3585
3586 if (Ptr.isStringPointer()) {
3587 S.Stk.push<Pointer>(Args: Ptr.asStringPointer().decay());
3588 return true;
3589 }
3590
3591 if (!Ptr.isOpaquePointer()) {
3592 S.Stk.push<Pointer>(Args: Ptr);
3593 return true;
3594 }
3595
3596 const OpaquePointer &OP = Ptr.asOpaquePointer();
3597 if (!OP.getFieldType()->isArrayType()) {
3598 S.Stk.push<Pointer>(Args: Ptr);
3599 return true;
3600 }
3601
3602 if (OP.isUnknownSizeArray() && OP.PathLength != 0) {
3603 S.FFDiag(SI: S.Current->getSource(PC: OpPC),
3604 DiagId: diag::note_constexpr_unsupported_unsized_array);
3605 S.Stk.push<Pointer>(Args: Ptr);
3606 return true;
3607 }
3608
3609 return arrayElemPtrOpaque(S, OpPC, Ptr,
3610 Index: APSInt(APInt::getZero(numBits: 1), /*IsUnsigned=*/true),
3611 /*AllowReplace=*/AllowReplace: false);
3612 }
3613
3614 S.FFDiag(SI: S.Current->getSource(PC: OpPC),
3615 DiagId: diag::note_constexpr_unsupported_unsized_array);
3616 return false;
3617}
3618
3619inline bool GetFnPtr(InterpState &S, const Function *Func) {
3620 assert(Func);
3621 S.Stk.push<Pointer>(Args&: Func);
3622 return true;
3623}
3624
3625template <PrimType Name, class T = typename PrimConv<Name>::T>
3626inline bool GetIntPtr(InterpState &S, CodePtr OpPC, const Type *Ty) {
3627 const T &IntVal = S.Stk.pop<T>();
3628
3629 S.CCEDiag(SI: S.Current->getSource(PC: OpPC), DiagId: diag::note_constexpr_invalid_cast)
3630 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
3631 << S.getLangOpts().CPlusPlus;
3632
3633 if constexpr (isIntegralOrPointer<T>()) {
3634 if (IntVal.getKind() == IntegralKind::Address) {
3635 if (IntVal.getOffset() != 0)
3636 return Invalid(S, OpPC);
3637 const VarDecl *VD = (const VarDecl *)IntVal.getPtr();
3638 unsigned GlobalIndex = *S.P.getOrCreateGlobal(VD);
3639 S.Stk.push<Pointer>(Args: S.P.getGlobal(Idx: GlobalIndex));
3640 } else if (IntVal.getKind() == IntegralKind::BlockAddress) {
3641 if (IntVal.getOffset() != 0)
3642 return Invalid(S, OpPC);
3643
3644 const Block *B = (const Block *)IntVal.getPtr();
3645 S.Stk.push<Pointer>(Args: const_cast<Block *>(B));
3646 } else if (IntVal.getKind() == IntegralKind::FunctionAddress) {
3647 const Function *F =
3648 S.P.getFunction(F: (const FunctionDecl *)IntVal.getPtr());
3649 S.Stk.push<Pointer>(F, IntVal.getOffset());
3650 } else {
3651 uint64_t NullValue =
3652 S.getASTContext().getTargetNullPointerValue(QT: QualType(Ty, 0));
3653 S.Stk.push<Pointer>(Args: static_cast<uint64_t>(IntVal), Args&: Ty, Args: 0,
3654 Args: static_cast<uint64_t>(IntVal) == NullValue);
3655 }
3656 } else {
3657 S.Stk.push<Pointer>(Args: static_cast<uint64_t>(IntVal), Args&: Ty);
3658 }
3659
3660 return true;
3661}
3662
3663inline bool GetStringPtr(InterpState &S, const Expr *Base) {
3664 S.Stk.push<Pointer>(Args&: Base, Args: S.newStringID());
3665 return true;
3666}
3667
3668bool GetMemberPtr(InterpState &S, const ValueDecl *D);
3669bool GetMemberPtrBase(InterpState &S);
3670bool GetMemberPtrDecl(InterpState &S);
3671bool CopyMemberPtrPath(InterpState &S, const RecordDecl *Entry, bool IsDerived);
3672
3673/// Just emit a diagnostic. The expression that caused emission of this
3674/// op is not valid in a constant context.
3675
3676inline bool Unsupported(InterpState &S, CodePtr OpPC) {
3677 const SourceLocation &Loc = S.Current->getLocation(PC: OpPC);
3678 S.FFDiag(Loc, DiagId: diag::note_constexpr_stmt_expr_unsupported)
3679 << S.Current->getRange(PC: OpPC);
3680 return false;
3681}
3682
3683inline bool InvalidTargetLabel(InterpState &S, CodePtr OpPC, bool IsContinue) {
3684 S.FFDiag(Loc: S.Current->getLocation(PC: OpPC),
3685 DiagId: diag::note_break_continue_label_outside_context)
3686 << S.Current->getRange(PC: OpPC) << IsContinue;
3687 return false;
3688}
3689
3690inline bool PushIgnoreDiags(InterpState &S) {
3691 ++S.DiagIgnoreDepth;
3692 if (S.DiagIgnoreDepth != 1)
3693 return true;
3694 assert(S.PrevDiags == nullptr);
3695 S.PrevDiags = S.getEvalStatus().Diag;
3696 S.PrevDiagsEmitted = S.getEvalStatus().DiagEmitted;
3697 S.getEvalStatus().Diag = nullptr;
3698 assert(!S.diagnosing());
3699 return true;
3700}
3701
3702inline bool PopIgnoreDiags(InterpState &S) {
3703 assert(S.DiagIgnoreDepth != 0);
3704 --S.DiagIgnoreDepth;
3705 if (S.DiagIgnoreDepth == 0) {
3706 S.getEvalStatus().Diag = S.PrevDiags;
3707 S.getEvalStatus().DiagEmitted = S.PrevDiagsEmitted;
3708 S.PrevDiags = nullptr;
3709 }
3710 return true;
3711}
3712
3713inline bool StartSpeculation(InterpState &S) {
3714#ifndef NDEBUG
3715 ++S.SpeculationDepth;
3716#endif
3717 return true;
3718}
3719
3720inline bool StartInit(InterpState &S) {
3721 const Pointer &Ptr = S.Stk.peek<Pointer>();
3722 S.InitializingPtrs.push_back(Elt: Ptr.view());
3723 return true;
3724}
3725
3726inline bool StartFieldInit(InterpState &S, uint32_t FieldOffset) {
3727 const Pointer &Ptr = S.Stk.peek<Pointer>();
3728 S.InitializingPtrs.push_back(Elt: Ptr.view().atField(Offset: FieldOffset));
3729 return true;
3730}
3731
3732inline bool EndInit(InterpState &S) {
3733 S.InitializingPtrs.pop_back();
3734 return true;
3735}
3736
3737// This is special-cased in the tablegen opcode emitter.
3738// Its dispatch function will NOT call InterpNext
3739// and instead simply return true.
3740PRESERVE_NONE inline bool EndSpeculation(InterpState &S) {
3741#ifndef NDEBUG
3742 assert(S.SpeculationDepth != 0);
3743 --S.SpeculationDepth;
3744#endif
3745 return true;
3746}
3747
3748inline bool PushCC(InterpState &S, bool Value) {
3749 S.ConstantContextOverride = Value;
3750 return true;
3751}
3752inline bool PopCC(InterpState &S) {
3753 S.ConstantContextOverride = std::nullopt;
3754 return true;
3755}
3756
3757inline bool PushMSVCCE(InterpState &S) {
3758 // This is a per-frame property.
3759 ++S.Current->MSVCConstexprAllowed;
3760 return true;
3761}
3762
3763inline bool PopMSVCCE(InterpState &S) {
3764 assert(S.Current->MSVCConstexprAllowed >= 1);
3765 // This is a per-frame property.
3766 --S.Current->MSVCConstexprAllowed;
3767 return true;
3768}
3769
3770/// Do nothing and just abort execution.
3771inline bool Error(InterpState &S) { return false; }
3772
3773inline bool SideEffect(InterpState &S) { return S.noteSideEffect(); }
3774
3775/// Abort without a diagnostic if we're checking for a potential constant
3776/// expression and this is not the bottom frame. This is used in constructors to
3777/// allow evaluating their initializers but abort if we encounter anything in
3778/// their body.
3779inline bool CtorCheck(InterpState &S) {
3780 if (S.checkingPotentialConstantExpression() && !S.Current->isBottomFrame())
3781 return false;
3782 return true;
3783}
3784
3785inline bool InvalidStore(InterpState &S, CodePtr OpPC, const Type *T) {
3786 if (S.getLangOpts().CPlusPlus) {
3787 QualType VolatileType = QualType(T, 0).withVolatile();
3788 S.FFDiag(SI: S.Current->getSource(PC: OpPC),
3789 DiagId: diag::note_constexpr_access_volatile_type)
3790 << AK_Assign << VolatileType;
3791 } else {
3792 S.FFDiag(SI: S.Current->getSource(PC: OpPC));
3793 }
3794 return false;
3795}
3796
3797inline bool InvalidAccessIndexAttr(InterpState &S, CodePtr OpPC,
3798 const ValueDecl *ECD, const Type *EnumTy) {
3799 const Expr *E = S.Current->getExpr(PC: OpPC);
3800 S.FFDiag(E, DiagId: diag::note_constexpr_bpf_preserve_ai_enumerator)
3801 << ECD << QualType(EnumTy, 0) << E->getSourceRange();
3802 return false;
3803}
3804
3805inline bool SizelessVectorElementSize(InterpState &S, CodePtr OpPC) {
3806 if (S.inConstantContext()) {
3807 const SourceRange &ArgRange = S.Current->getRange(PC: OpPC);
3808 const Expr *E = S.Current->getExpr(PC: OpPC);
3809 S.CCEDiag(E, DiagId: diag::note_constexpr_non_const_vectorelements) << ArgRange;
3810 }
3811 return false;
3812}
3813
3814inline bool CheckPseudoDtor(InterpState &S, CodePtr OpPC) {
3815 if (!S.getLangOpts().CPlusPlus20)
3816 S.CCEDiag(SI: S.Current->getSource(PC: OpPC),
3817 DiagId: diag::note_constexpr_pseudo_destructor);
3818 return true;
3819}
3820
3821inline bool Assume(InterpState &S, CodePtr OpPC) {
3822 const auto Val = S.Stk.pop<Boolean>();
3823
3824 if (Val)
3825 return true;
3826
3827 // Else, diagnose.
3828 const SourceLocation &Loc = S.Current->getLocation(PC: OpPC);
3829 S.CCEDiag(Loc, DiagId: diag::note_constexpr_assumption_failed);
3830 return false;
3831}
3832
3833template <PrimType Name, class T = typename PrimConv<Name>::T>
3834inline bool OffsetOf(InterpState &S, CodePtr OpPC, const OffsetOfExpr *E) {
3835 llvm::SmallVector<int64_t> ArrayIndices;
3836 for (size_t I = 0; I != E->getNumExpressions(); ++I)
3837 ArrayIndices.emplace_back(
3838 Args: static_cast<int64_t>(S.Stk.pop<Integral<64, true>>()));
3839
3840 int64_t Result;
3841 if (!InterpretOffsetOf(S, OpPC, E, ArrayIndices, Result))
3842 return false;
3843
3844 S.Stk.push<T>(T::from(Result));
3845
3846 return true;
3847}
3848
3849template <PrimType Name, class T = typename PrimConv<Name>::T>
3850inline bool CheckNonNullArg(InterpState &S, CodePtr OpPC) {
3851 const T &Arg = S.Stk.peek<T>();
3852 if (!Arg.isZero())
3853 return true;
3854
3855 const SourceLocation &Loc = S.Current->getLocation(PC: OpPC);
3856 S.CCEDiag(Loc, DiagId: diag::note_non_null_attribute_failed);
3857
3858 return false;
3859}
3860
3861void diagnoseEnumValue(InterpState &S, CodePtr OpPC, const EnumDecl *ED,
3862 const APSInt &Value);
3863
3864template <PrimType Name, class T = typename PrimConv<Name>::T>
3865inline bool CheckEnumValue(InterpState &S, CodePtr OpPC, const EnumDecl *ED) {
3866 assert(ED);
3867 assert(!ED->isFixed());
3868
3869 if (S.inConstantContext()) {
3870 const APSInt Val = S.Stk.peek<T>().toAPSInt();
3871 diagnoseEnumValue(S, OpPC, ED, Value: Val);
3872 }
3873 return true;
3874}
3875
3876/// OldPtr -> Integer -> NewPtr.
3877template <PrimType TIn, PrimType TOut> inline bool DecayPtr(InterpState &S) {
3878 static_assert(isPtrType(T: TIn) && isPtrType(T: TOut));
3879 using FromT = typename PrimConv<TIn>::T;
3880 using ToT = typename PrimConv<TOut>::T;
3881
3882 const FromT &OldPtr = S.Stk.pop<FromT>();
3883
3884 if constexpr (std::is_same_v<FromT, FunctionPointer> &&
3885 std::is_same_v<ToT, Pointer>) {
3886 S.Stk.push<Pointer>(OldPtr.getFunction(), OldPtr.getOffset());
3887 return true;
3888 } else if constexpr (std::is_same_v<FromT, Pointer> &&
3889 std::is_same_v<ToT, FunctionPointer>) {
3890 if (OldPtr.isFunctionPointer()) {
3891 S.Stk.push<FunctionPointer>(OldPtr.asFunctionPointer().getFunction(),
3892 OldPtr.getByteOffset());
3893 return true;
3894 }
3895 }
3896
3897 S.Stk.push<ToT>(ToT(OldPtr.getIntegerRepresentation(), nullptr));
3898 return true;
3899}
3900
3901inline bool CheckDecl(InterpState &S, const VarDecl *VD) {
3902 // An expression E is a core constant expression unless the evaluation of E
3903 // would evaluate one of the following: [C++23] - a control flow that passes
3904 // through a declaration of a variable with static or thread storage duration
3905 // unless that variable is usable in constant expressions.
3906 assert(VD->isLocalVarDecl() &&
3907 VD->isStaticLocal()); // Checked before emitting this.
3908
3909 if (VD == S.EvaluatingDecl)
3910 return true;
3911
3912 if (!VD->isUsableInConstantExpressions(C: S.getASTContext())) {
3913 S.CCEDiag(Loc: VD->getLocation(), DiagId: diag::note_constexpr_static_local)
3914 << (VD->getTSCSpec() == TSCS_unspecified ? 0 : 1) << VD;
3915 return false;
3916 }
3917 return true;
3918}
3919
3920/// Check if the destination array we're initializing can hold the \p NumElems
3921/// elements.
3922inline bool CheckArrayDestSize(InterpState &S, CodePtr OpPC, size_t NumElems) {
3923 if (!CheckArraySize(S, OpPC, NumElems))
3924 return false;
3925
3926 const Pointer &Ptr = S.Stk.peek<Pointer>();
3927 if (!Ptr.isUnknownSizeArray() && NumElems > Ptr.getNumElems()) {
3928 S.FFDiag(SI: S.Current->getSource(PC: OpPC), DiagId: diag::note_constexpr_new_too_small)
3929 << Ptr.getNumElems() << NumElems;
3930 return false;
3931 }
3932
3933 return true;
3934}
3935
3936inline bool Alloc(InterpState &S, CodePtr OpPC, const Descriptor *Desc) {
3937 assert(Desc);
3938
3939 if (!CheckDynamicMemoryAllocation(S, OpPC))
3940 return false;
3941
3942 DynamicAllocator &Allocator = S.getAllocator();
3943 Block *B =
3944 Allocator.allocate(D: Desc, EvalID: S.EvalID, AllocForm: DynamicAllocator::Form::NonArray);
3945 assert(B);
3946 S.Stk.push<Pointer>(Args&: B);
3947 return true;
3948}
3949
3950template <PrimType Name, class SizeT = typename PrimConv<Name>::T>
3951inline bool AllocN(InterpState &S, CodePtr OpPC, PrimType T, const Expr *Source,
3952 bool IsNoThrow) {
3953 if (!CheckDynamicMemoryAllocation(S, OpPC))
3954 return false;
3955
3956 SizeT NumElements = S.Stk.pop<SizeT>();
3957 if (!CheckArraySize(S, OpPC, NumElements, primSize(Type: T), IsNoThrow)) {
3958 if (!IsNoThrow)
3959 return false;
3960
3961 // If this failed and is nothrow, just return a null ptr.
3962 S.Stk.push<Pointer>();
3963 return true;
3964 }
3965 if (NumElements.isNegative()) {
3966 if (!IsNoThrow) {
3967 S.FFDiag(SI: S.Current->getSource(PC: OpPC), DiagId: diag::note_constexpr_new_negative)
3968 << NumElements.toDiagnosticString(S.getASTContext());
3969 return false;
3970 }
3971 S.Stk.push<Pointer>();
3972 return true;
3973 }
3974
3975 if (!CheckArraySize(S, OpPC, NumElems: static_cast<uint64_t>(NumElements)))
3976 return false;
3977
3978 DynamicAllocator &Allocator = S.getAllocator();
3979 Block *B = Allocator.allocate(Source, T, NumElements: static_cast<size_t>(NumElements),
3980 EvalID: S.EvalID, AllocForm: DynamicAllocator::Form::Array);
3981 assert(B);
3982 if (NumElements.isZero())
3983 S.Stk.push<Pointer>(Args&: B);
3984 else
3985 S.Stk.push<Pointer>(Args: Pointer(B).atIndex(Idx: 0));
3986 return true;
3987}
3988
3989template <PrimType Name, class SizeT = typename PrimConv<Name>::T>
3990inline bool AllocCN(InterpState &S, CodePtr OpPC, const Descriptor *ElementDesc,
3991 bool IsNoThrow) {
3992 if (!CheckDynamicMemoryAllocation(S, OpPC))
3993 return false;
3994
3995 if (!ElementDesc)
3996 return false;
3997
3998 SizeT NumElements = S.Stk.pop<SizeT>();
3999 if (!CheckArraySize(S, OpPC, NumElements, ElementDesc->getSize(),
4000 IsNoThrow)) {
4001 if (!IsNoThrow)
4002 return false;
4003
4004 // If this failed and is nothrow, just return a null ptr.
4005 S.Stk.push<Pointer>(Args: 0, Args: ElementDesc->getType().getTypePtr(), Args: 0,
4006 /*IsNull=*/Args: true);
4007 return true;
4008 }
4009 if (NumElements.isNegative()) {
4010 if (!IsNoThrow) {
4011 S.FFDiag(SI: S.Current->getSource(PC: OpPC), DiagId: diag::note_constexpr_new_negative)
4012 << NumElements.toDiagnosticString(S.getASTContext());
4013 return false;
4014 }
4015 S.Stk.push<Pointer>();
4016 return true;
4017 }
4018
4019 if (!CheckArraySize(S, OpPC, NumElems: static_cast<uint64_t>(NumElements)))
4020 return false;
4021
4022 DynamicAllocator &Allocator = S.getAllocator();
4023 Block *B = Allocator.allocate(D: ElementDesc, NumElements: static_cast<size_t>(NumElements),
4024 EvalID: S.EvalID, AllocForm: DynamicAllocator::Form::Array);
4025 assert(B);
4026 if (NumElements.isZero())
4027 S.Stk.push<Pointer>(Args&: B);
4028 else
4029 S.Stk.push<Pointer>(Args: Pointer(B).atIndex(Idx: 0));
4030
4031 return true;
4032}
4033
4034bool Free(InterpState &S, CodePtr OpPC, bool DeleteIsArrayForm,
4035 bool IsGlobalDelete);
4036
4037static inline bool IsConstantContext(InterpState &S) {
4038 S.Stk.push<Boolean>(Args: Boolean::from(Value: S.inConstantContext()));
4039 return true;
4040}
4041
4042static inline bool CheckAllocations(InterpState &S) {
4043 return S.maybeDiagnoseDanglingAllocations();
4044}
4045
4046/// Check if the initializer and storage types of a placement-new expression
4047/// match.
4048bool CheckNewTypeMismatch(InterpState &S, CodePtr OpPC, const Expr *E,
4049 std::optional<uint64_t> ArraySize = std::nullopt);
4050
4051template <PrimType Name, class T = typename PrimConv<Name>::T>
4052bool CheckNewTypeMismatchArray(InterpState &S, CodePtr OpPC, const Expr *E) {
4053 const auto &Size = S.Stk.pop<T>();
4054 return CheckNewTypeMismatch(S, OpPC, E, ArraySize: static_cast<uint64_t>(Size));
4055}
4056bool InvalidNewDeleteExpr(InterpState &S, CodePtr OpPC, const Expr *E);
4057bool CheckPlacementNew(InterpState &S, CodePtr OpPC, const Expr *E);
4058
4059template <PrimType Name, class T = typename PrimConv<Name>::T>
4060inline bool BitCastPrim(InterpState &S, CodePtr OpPC, bool TargetIsUCharOrByte,
4061 uint32_t ResultBitWidth, const llvm::fltSemantics *Sem,
4062 const Type *TargetType) {
4063 const Pointer &FromPtr = S.Stk.pop<Pointer>();
4064
4065 if (!CheckLoad(S, OpPC, Ptr: FromPtr))
4066 return false;
4067
4068 if constexpr (std::is_same_v<T, Pointer>) {
4069 if (!TargetType->isNullPtrType()) {
4070 S.FFDiag(SI: S.Current->getSource(PC: OpPC),
4071 DiagId: diag::note_constexpr_bit_cast_invalid_type)
4072 << /*IsToType=*/true << /*IsReference=*/false << 1 /*Pointer*/;
4073 return false;
4074 }
4075 // The only pointer type we can validly bitcast to is nullptr_t.
4076 S.Stk.push<Pointer>();
4077 return true;
4078 } else if constexpr (std::is_same_v<T, MemberPointer>) {
4079 S.FFDiag(SI: S.Current->getSource(PC: OpPC),
4080 DiagId: diag::note_constexpr_bit_cast_invalid_type)
4081 << /*IsToType=*/true << /*IsReference=*/false << 2 /*MemberPointer*/;
4082 return false;
4083 } else {
4084
4085 size_t BuffSize = ResultBitWidth / 8;
4086 llvm::SmallVector<std::byte> Buff(BuffSize);
4087 bool HasIndeterminateBits = false;
4088
4089 Bits FullBitWidth(ResultBitWidth);
4090
4091 // FIXME: Since we support bitcasts of bitfields, we should also support
4092 // this.
4093 if (!FullBitWidth.isFullByte())
4094 return false;
4095
4096 Bits BitWidth = FullBitWidth;
4097
4098 if constexpr (std::is_same_v<T, Floating>) {
4099 assert(Sem);
4100 BitWidth = Bits(llvm::APFloatBase::getSizeInBits(Sem: *Sem));
4101 }
4102
4103 if (!DoBitCast(S, OpPC, Ptr: FromPtr, Buff: Buff.data(), BitWidth, FullBitWidth,
4104 HasIndeterminateBits))
4105 return false;
4106
4107 if (!CheckBitCast(S, OpPC, HasIndeterminateBits, TargetIsUCharOrByte))
4108 return false;
4109
4110 if constexpr (std::is_same_v<T, Floating>) {
4111 assert(Sem);
4112 Floating Result = S.allocFloat(Sem: *Sem);
4113 Floating::bitcastFromMemory(Buff: Buff.data(), Sem: *Sem, Result: &Result);
4114 S.Stk.push<Floating>(Args&: Result);
4115 } else if constexpr (needsAlloc<T>()) {
4116 T Result = S.allocAP<T>(ResultBitWidth);
4117 T::bitcastFromMemory(Buff.data(), ResultBitWidth, &Result);
4118 S.Stk.push<T>(Result);
4119 } else if constexpr (std::is_same_v<T, Boolean>) {
4120 // Only allow to cast single-byte integers to bool if they are either 0
4121 // or 1.
4122 assert(FullBitWidth.getQuantity() == 8);
4123 auto Val = static_cast<unsigned int>(Buff[0]);
4124 if (Val > 1) {
4125 S.FFDiag(SI: S.Current->getSource(PC: OpPC),
4126 DiagId: diag::note_constexpr_bit_cast_unrepresentable_value)
4127 << S.getASTContext().BoolTy << Val;
4128 return false;
4129 }
4130 S.Stk.push<T>(T::bitcastFromMemory(Buff.data(), ResultBitWidth));
4131 } else {
4132 assert(!Sem);
4133 S.Stk.push<T>(T::bitcastFromMemory(Buff.data(), ResultBitWidth));
4134 }
4135 return true;
4136 }
4137}
4138
4139inline bool BitCast(InterpState &S, CodePtr OpPC) {
4140 Pointer FromPtr = S.Stk.pop<Pointer>();
4141 Pointer &ToPtr = S.Stk.peek<Pointer>();
4142
4143 // FIXME: Could allow reading from string pointers?
4144 if (!FromPtr.isBlockPointer() || !ToPtr.isBlockPointer())
4145 return false;
4146
4147 const Descriptor *D = FromPtr.getFieldDesc();
4148 if (D->isPrimitiveArray() && FromPtr.isArrayRoot())
4149 FromPtr = FromPtr.atIndex(Idx: 0);
4150
4151 if (!CheckLoad(S, OpPC, Ptr: FromPtr))
4152 return false;
4153
4154 if (!DoBitCastPtr(S, OpPC, FromPtr, ToPtr))
4155 return false;
4156
4157 return true;
4158}
4159
4160/// Typeid support.
4161bool GetTypeid(InterpState &S, const Type *TypePtr, const Type *TypeInfoType);
4162bool GetTypeidPtr(InterpState &S, CodePtr OpPC, const Type *TypeInfoType);
4163bool DiagTypeid(InterpState &S, CodePtr OpPC);
4164
4165inline bool CheckDestruction(InterpState &S, CodePtr OpPC) {
4166 const auto &Ptr = S.Stk.peek<Pointer>();
4167 return checkDestructor(S, OpPC, Ptr);
4168}
4169
4170inline bool ReflectValue(InterpState &S, CodePtr OpPC, ReflectionKind Kind,
4171 const void *Operand) {
4172 S.Stk.push<Reflect>(Args&: Kind, Args&: Operand);
4173 return true;
4174}
4175
4176inline bool IsBaseClass(InterpState &S) {
4177 S.Stk.push<bool>(Args: S.Stk.peek<Pointer>().isBaseClass());
4178 return true;
4179}
4180
4181//===----------------------------------------------------------------------===//
4182// Read opcode arguments
4183//===----------------------------------------------------------------------===//
4184
4185template <typename T> inline T ReadArg(InterpState &S, CodePtr &OpPC) {
4186 if constexpr (std::is_pointer<T>::value)
4187 return reinterpret_cast<T>(OpPC.read<uintptr_t>());
4188 else
4189 return OpPC.read<T>();
4190}
4191
4192template <> inline Floating ReadArg<Floating>(InterpState &S, CodePtr &OpPC) {
4193 auto &Semantics =
4194 llvm::APFloatBase::EnumToSemantics(S: Floating::deserializeSemantics(Buff: *OpPC));
4195
4196 auto F = S.allocFloat(Sem: Semantics);
4197 Floating::deserialize(Buff: *OpPC, Result: &F);
4198 OpPC += align(Size: F.bytesToSerialize());
4199 return F;
4200}
4201
4202template <>
4203inline IntegralAP<false> ReadArg<IntegralAP<false>>(InterpState &S,
4204 CodePtr &OpPC) {
4205 uint32_t BitWidth = IntegralAP<false>::deserializeSize(Buff: *OpPC);
4206 auto Result = S.allocAP<IntegralAP<false>>(BitWidth);
4207 assert(Result.bitWidth() == BitWidth);
4208
4209 IntegralAP<false>::deserialize(Buff: *OpPC, Result: &Result);
4210 OpPC += align(Size: Result.bytesToSerialize());
4211 return Result;
4212}
4213
4214template <>
4215inline IntegralAP<true> ReadArg<IntegralAP<true>>(InterpState &S,
4216 CodePtr &OpPC) {
4217 uint32_t BitWidth = IntegralAP<true>::deserializeSize(Buff: *OpPC);
4218 auto Result = S.allocAP<IntegralAP<true>>(BitWidth);
4219 assert(Result.bitWidth() == BitWidth);
4220
4221 IntegralAP<true>::deserialize(Buff: *OpPC, Result: &Result);
4222 OpPC += align(Size: Result.bytesToSerialize());
4223 return Result;
4224}
4225
4226template <>
4227inline FixedPoint ReadArg<FixedPoint>(InterpState &S, CodePtr &OpPC) {
4228 FixedPoint FP = FixedPoint::deserialize(Buff: *OpPC);
4229 OpPC += align(Size: FP.bytesToSerialize());
4230 return FP;
4231}
4232
4233} // namespace interp
4234} // namespace clang
4235
4236#endif
4237