clang 22.0.0git
Interp.h
Go to the documentation of this file.
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 "DynamicAllocator.h"
20#include "FixedPoint.h"
21#include "Floating.h"
22#include "Function.h"
24#include "InterpFrame.h"
25#include "InterpStack.h"
26#include "InterpState.h"
27#include "MemberPointer.h"
28#include "PrimType.h"
29#include "Program.h"
30#include "State.h"
32#include "clang/AST/Expr.h"
33#include "llvm/ADT/APFloat.h"
34#include "llvm/ADT/APSInt.h"
35#include <type_traits>
36
37namespace clang {
38namespace interp {
39
40using APSInt = llvm::APSInt;
41using FixedPointSemantics = llvm::FixedPointSemantics;
42
43/// Checks if the variable has externally defined storage.
44bool CheckExtern(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
45
46/// Checks if the array is offsetable.
47bool CheckArray(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
48
49/// Checks if a pointer is live and accessible.
50bool CheckLive(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
51 AccessKinds AK);
52
53/// Checks if a pointer is a dummy pointer.
54bool CheckDummy(InterpState &S, CodePtr OpPC, const Block *B, AccessKinds AK);
55
56/// Checks if a pointer is null.
57bool CheckNull(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
59
60/// Checks if a pointer is in range.
61bool CheckRange(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
62 AccessKinds AK);
63
64/// Checks if a field from which a pointer is going to be derived is valid.
65bool CheckRange(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
67
68/// Checks if Ptr is a one-past-the-end pointer.
69bool CheckSubobject(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
71
72/// Checks if the dowcast using the given offset is possible with the given
73/// pointer.
74bool CheckDowncast(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
75 uint32_t Offset);
76
77/// Checks if a pointer points to const storage.
78bool CheckConst(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
79
80/// Checks if the Descriptor is of a constexpr or const global variable.
81bool CheckConstant(InterpState &S, CodePtr OpPC, const Descriptor *Desc);
82
83/// Checks if a pointer points to a mutable field.
84bool CheckMutable(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
85
86/// Checks if a value can be loaded from a block.
87bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
88 AccessKinds AK = AK_Read);
89bool CheckFinalLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
90
91bool DiagnoseUninitialized(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
92 AccessKinds AK);
93bool DiagnoseUninitialized(InterpState &S, CodePtr OpPC, bool Extern,
94 const Descriptor *Desc, AccessKinds AK);
95
96/// Checks a direct load of a primitive value from a global or local variable.
97bool CheckGlobalLoad(InterpState &S, CodePtr OpPC, const Block *B);
98bool CheckLocalLoad(InterpState &S, CodePtr OpPC, const Block *B);
99
100/// Checks if a value can be stored in a block.
101bool CheckStore(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
102
103/// Checks if a value can be initialized.
104bool CheckInit(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
105
106/// Checks the 'this' pointer.
107bool CheckThis(InterpState &S, CodePtr OpPC, const Pointer &This);
108
109/// Checks if dynamic memory allocation is available in the current
110/// language mode.
112
113/// Diagnose mismatched new[]/delete or new/delete[] pairs.
115 DynamicAllocator::Form AllocForm,
116 DynamicAllocator::Form DeleteForm, const Descriptor *D,
117 const Expr *NewExpr);
118
119/// Check the source of the pointer passed to delete/delete[] has actually
120/// been heap allocated by us.
121bool CheckDeleteSource(InterpState &S, CodePtr OpPC, const Expr *Source,
122 const Pointer &Ptr);
123
124bool CheckActive(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
125 AccessKinds AK);
126
127/// Sets the given integral value to the pointer, which is of
128/// a std::{weak,partial,strong}_ordering type.
130 const Pointer &Ptr, const APSInt &IntValue);
131
132/// Copy the contents of Src into Dest.
133bool DoMemcpy(InterpState &S, CodePtr OpPC, const Pointer &Src, Pointer &Dest);
134
135bool CallVar(InterpState &S, CodePtr OpPC, const Function *Func,
136 uint32_t VarArgSize);
137bool Call(InterpState &S, CodePtr OpPC, const Function *Func,
138 uint32_t VarArgSize);
139bool CallVirt(InterpState &S, CodePtr OpPC, const Function *Func,
140 uint32_t VarArgSize);
141bool CallBI(InterpState &S, CodePtr OpPC, const CallExpr *CE,
142 uint32_t BuiltinID);
143bool CallPtr(InterpState &S, CodePtr OpPC, uint32_t ArgSize,
144 const CallExpr *CE);
145bool CheckLiteralType(InterpState &S, CodePtr OpPC, const Type *T);
146bool InvalidShuffleVectorIndex(InterpState &S, CodePtr OpPC, uint32_t Index);
147bool CheckBitCast(InterpState &S, CodePtr OpPC, bool HasIndeterminateBits,
148 bool TargetIsUCharOrByte);
149bool CheckBCPResult(InterpState &S, const Pointer &Ptr);
150bool CheckDestructor(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
151
152template <typename T>
153static bool handleOverflow(InterpState &S, CodePtr OpPC, const T &SrcValue) {
154 const Expr *E = S.Current->getExpr(OpPC);
155 S.CCEDiag(E, diag::note_constexpr_overflow) << SrcValue << E->getType();
156 return S.noteUndefinedBehavior();
157}
158bool handleFixedPointOverflow(InterpState &S, CodePtr OpPC,
159 const FixedPoint &FP);
160
161bool isConstexprUnknown(const Pointer &P);
162
163inline bool CheckArraySize(InterpState &S, CodePtr OpPC, uint64_t NumElems);
164
165enum class ShiftDir { Left, Right };
166
167/// Checks if the shift operation is legal.
168template <ShiftDir Dir, typename LT, typename RT>
169bool CheckShift(InterpState &S, CodePtr OpPC, const LT &LHS, const RT &RHS,
170 unsigned Bits) {
171 if (RHS.isNegative()) {
172 const SourceInfo &Loc = S.Current->getSource(OpPC);
173 S.CCEDiag(Loc, diag::note_constexpr_negative_shift) << RHS.toAPSInt();
174 if (!S.noteUndefinedBehavior())
175 return false;
176 }
177
178 // C++11 [expr.shift]p1: Shift width must be less than the bit width of
179 // the shifted type.
180 if (Bits > 1 && RHS >= Bits) {
181 const Expr *E = S.Current->getExpr(OpPC);
182 const APSInt Val = RHS.toAPSInt();
183 QualType Ty = E->getType();
184 S.CCEDiag(E, diag::note_constexpr_large_shift) << Val << Ty << Bits;
185 if (!S.noteUndefinedBehavior())
186 return false;
187 }
188
189 if constexpr (Dir == ShiftDir::Left) {
190 if (LHS.isSigned() && !S.getLangOpts().CPlusPlus20) {
191 // C++11 [expr.shift]p2: A signed left shift must have a non-negative
192 // operand, and must not overflow the corresponding unsigned type.
193 if (LHS.isNegative()) {
194 const Expr *E = S.Current->getExpr(OpPC);
195 S.CCEDiag(E, diag::note_constexpr_lshift_of_negative) << LHS.toAPSInt();
196 if (!S.noteUndefinedBehavior())
197 return false;
198 } else if (LHS.toUnsigned().countLeadingZeros() <
199 static_cast<unsigned>(RHS)) {
200 const Expr *E = S.Current->getExpr(OpPC);
201 S.CCEDiag(E, diag::note_constexpr_lshift_discards);
202 if (!S.noteUndefinedBehavior())
203 return false;
204 }
205 }
206 }
207
208 // C++2a [expr.shift]p2: [P0907R4]:
209 // E1 << E2 is the unique value congruent to
210 // E1 x 2^E2 module 2^N.
211 return true;
212}
213
214/// Checks if Div/Rem operation on LHS and RHS is valid.
215template <typename T>
216bool CheckDivRem(InterpState &S, CodePtr OpPC, const T &LHS, const T &RHS) {
217 if (RHS.isZero()) {
218 const auto *Op = cast<BinaryOperator>(S.Current->getExpr(OpPC));
219 if constexpr (std::is_same_v<T, Floating>) {
220 S.CCEDiag(Op, diag::note_expr_divide_by_zero)
221 << Op->getRHS()->getSourceRange();
222 return true;
223 }
224
225 S.FFDiag(Op, diag::note_expr_divide_by_zero)
226 << Op->getRHS()->getSourceRange();
227 return false;
228 }
229
230 if constexpr (!std::is_same_v<T, FixedPoint>) {
231 if (LHS.isSigned() && LHS.isMin() && RHS.isNegative() && RHS.isMinusOne()) {
232 APSInt LHSInt = LHS.toAPSInt();
233 SmallString<32> Trunc;
234 (-LHSInt.extend(LHSInt.getBitWidth() + 1)).toString(Trunc, 10);
235 const SourceInfo &Loc = S.Current->getSource(OpPC);
236 const Expr *E = S.Current->getExpr(OpPC);
237 S.CCEDiag(Loc, diag::note_constexpr_overflow) << Trunc << E->getType();
238 return false;
239 }
240 }
241 return true;
242}
243
244template <typename SizeT>
245bool CheckArraySize(InterpState &S, CodePtr OpPC, SizeT *NumElements,
246 unsigned ElemSize, bool IsNoThrow) {
247 // FIXME: Both the SizeT::from() as well as the
248 // NumElements.toAPSInt() in this function are rather expensive.
249
250 // Can't be too many elements if the bitwidth of NumElements is lower than
251 // that of Descriptor::MaxArrayElemBytes.
252 if ((NumElements->bitWidth() - NumElements->isSigned()) <
253 (sizeof(Descriptor::MaxArrayElemBytes) * 8))
254 return true;
255
256 // FIXME: GH63562
257 // APValue stores array extents as unsigned,
258 // so anything that is greater that unsigned would overflow when
259 // constructing the array, we catch this here.
260 SizeT MaxElements = SizeT::from(Descriptor::MaxArrayElemBytes / ElemSize);
261 assert(MaxElements.isPositive());
262 if (NumElements->toAPSInt().getActiveBits() >
264 *NumElements > MaxElements) {
265 if (!IsNoThrow) {
266 const SourceInfo &Loc = S.Current->getSource(OpPC);
267
268 if (NumElements->isSigned() && NumElements->isNegative()) {
269 S.FFDiag(Loc, diag::note_constexpr_new_negative)
270 << NumElements->toDiagnosticString(S.getASTContext());
271 } else {
272 S.FFDiag(Loc, diag::note_constexpr_new_too_large)
273 << NumElements->toDiagnosticString(S.getASTContext());
274 }
275 }
276 return false;
277 }
278 return true;
279}
280
281/// Checks if the result of a floating-point operation is valid
282/// in the current context.
283bool CheckFloatResult(InterpState &S, CodePtr OpPC, const Floating &Result,
284 APFloat::opStatus Status, FPOptions FPO);
285
286/// Checks why the given DeclRefExpr is invalid.
287bool CheckDeclRef(InterpState &S, CodePtr OpPC, const DeclRefExpr *DR);
288
289/// Interpreter entry point.
290bool Interpret(InterpState &S);
291
292/// Interpret a builtin function.
293bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const CallExpr *Call,
294 uint32_t BuiltinID);
295
296/// Interpret an offsetof operation.
297bool InterpretOffsetOf(InterpState &S, CodePtr OpPC, const OffsetOfExpr *E,
298 ArrayRef<int64_t> ArrayIndices, int64_t &Result);
299
300inline bool Invalid(InterpState &S, CodePtr OpPC);
301
302enum class ArithOp { Add, Sub };
303
304//===----------------------------------------------------------------------===//
305// Returning values
306//===----------------------------------------------------------------------===//
307
308void cleanupAfterFunctionCall(InterpState &S, CodePtr OpPC,
309 const Function *Func);
310
311template <PrimType Name, class T = typename PrimConv<Name>::T>
312bool Ret(InterpState &S, CodePtr &PC) {
313 const T &Ret = S.Stk.pop<T>();
314
315 assert(S.Current);
316 assert(S.Current->getFrameOffset() == S.Stk.size() && "Invalid frame");
319
320 if (InterpFrame *Caller = S.Current->Caller) {
321 PC = S.Current->getRetPC();
323 S.Current = Caller;
324 S.Stk.push<T>(Ret);
325 } else {
327 S.Current = nullptr;
328 // The topmost frame should come from an EvalEmitter,
329 // which has its own implementation of the Ret<> instruction.
330 }
331 return true;
332}
333
334inline bool RetVoid(InterpState &S, CodePtr &PC) {
335 assert(S.Current->getFrameOffset() == S.Stk.size() && "Invalid frame");
336
339
340 if (InterpFrame *Caller = S.Current->Caller) {
341 PC = S.Current->getRetPC();
343 S.Current = Caller;
344 } else {
346 S.Current = nullptr;
347 }
348 return true;
349}
350
351//===----------------------------------------------------------------------===//
352// Add, Sub, Mul
353//===----------------------------------------------------------------------===//
354
355template <typename T, bool (*OpFW)(T, T, unsigned, T *),
356 template <typename U> class OpAP>
357bool AddSubMulHelper(InterpState &S, CodePtr OpPC, unsigned Bits, const T &LHS,
358 const T &RHS) {
359 // Fast path - add the numbers with fixed width.
360 T Result;
361 if constexpr (needsAlloc<T>())
362 Result = S.allocAP<T>(LHS.bitWidth());
363
364 if (!OpFW(LHS, RHS, Bits, &Result)) {
365 S.Stk.push<T>(Result);
366 return true;
367 }
368 // If for some reason evaluation continues, use the truncated results.
369 S.Stk.push<T>(Result);
370
371 // Short-circuit fixed-points here since the error handling is easier.
372 if constexpr (std::is_same_v<T, FixedPoint>)
373 return handleFixedPointOverflow(S, OpPC, Result);
374
375 // Slow path - compute the result using another bit of precision.
376 APSInt Value = OpAP<APSInt>()(LHS.toAPSInt(Bits), RHS.toAPSInt(Bits));
377
378 // Report undefined behaviour, stopping if required.
380 const Expr *E = S.Current->getExpr(OpPC);
381 QualType Type = E->getType();
382 SmallString<32> Trunc;
383 Value.trunc(Result.bitWidth())
384 .toString(Trunc, 10, Result.isSigned(), /*formatAsCLiteral=*/false,
385 /*UpperCase=*/true, /*InsertSeparators=*/true);
386 S.report(E->getExprLoc(), diag::warn_integer_constant_overflow)
387 << Trunc << Type << E->getSourceRange();
388 }
389
390 if (!handleOverflow(S, OpPC, Value)) {
391 S.Stk.pop<T>();
392 return false;
393 }
394 return true;
395}
396
397template <PrimType Name, class T = typename PrimConv<Name>::T>
398bool Add(InterpState &S, CodePtr OpPC) {
399 const T &RHS = S.Stk.pop<T>();
400 const T &LHS = S.Stk.pop<T>();
401 const unsigned Bits = RHS.bitWidth() + 1;
402
403 return AddSubMulHelper<T, T::add, std::plus>(S, OpPC, Bits, LHS, RHS);
404}
405
406static inline llvm::RoundingMode getRoundingMode(FPOptions FPO) {
407 auto RM = FPO.getRoundingMode();
408 if (RM == llvm::RoundingMode::Dynamic)
409 return llvm::RoundingMode::NearestTiesToEven;
410 return RM;
411}
412
413inline bool Addf(InterpState &S, CodePtr OpPC, uint32_t FPOI) {
414 const Floating &RHS = S.Stk.pop<Floating>();
415 const Floating &LHS = S.Stk.pop<Floating>();
416
418 Floating Result = S.allocFloat(LHS.getSemantics());
419 auto Status = Floating::add(LHS, RHS, getRoundingMode(FPO), &Result);
421 return CheckFloatResult(S, OpPC, Result, Status, FPO);
422}
423
424template <PrimType Name, class T = typename PrimConv<Name>::T>
425bool Sub(InterpState &S, CodePtr OpPC) {
426 const T &RHS = S.Stk.pop<T>();
427 const T &LHS = S.Stk.pop<T>();
428 const unsigned Bits = RHS.bitWidth() + 1;
429
430 return AddSubMulHelper<T, T::sub, std::minus>(S, OpPC, Bits, LHS, RHS);
431}
432
433inline bool Subf(InterpState &S, CodePtr OpPC, uint32_t FPOI) {
434 const Floating &RHS = S.Stk.pop<Floating>();
435 const Floating &LHS = S.Stk.pop<Floating>();
436
438 Floating Result = S.allocFloat(LHS.getSemantics());
439 auto Status = Floating::sub(LHS, RHS, getRoundingMode(FPO), &Result);
441 return CheckFloatResult(S, OpPC, Result, Status, FPO);
442}
443
444template <PrimType Name, class T = typename PrimConv<Name>::T>
445bool Mul(InterpState &S, CodePtr OpPC) {
446 const T &RHS = S.Stk.pop<T>();
447 const T &LHS = S.Stk.pop<T>();
448 const unsigned Bits = RHS.bitWidth() * 2;
449
450 return AddSubMulHelper<T, T::mul, std::multiplies>(S, OpPC, Bits, LHS, RHS);
451}
452
453inline bool Mulf(InterpState &S, CodePtr OpPC, uint32_t FPOI) {
454 const Floating &RHS = S.Stk.pop<Floating>();
455 const Floating &LHS = S.Stk.pop<Floating>();
456
458 Floating Result = S.allocFloat(LHS.getSemantics());
459
460 auto Status = Floating::mul(LHS, RHS, getRoundingMode(FPO), &Result);
461
463 return CheckFloatResult(S, OpPC, Result, Status, FPO);
464}
465
466template <PrimType Name, class T = typename PrimConv<Name>::T>
467inline bool Mulc(InterpState &S, CodePtr OpPC) {
468 const Pointer &RHS = S.Stk.pop<Pointer>();
469 const Pointer &LHS = S.Stk.pop<Pointer>();
470 const Pointer &Result = S.Stk.peek<Pointer>();
471
472 if constexpr (std::is_same_v<T, Floating>) {
473 APFloat A = LHS.elem<Floating>(0).getAPFloat();
474 APFloat B = LHS.elem<Floating>(1).getAPFloat();
475 APFloat C = RHS.elem<Floating>(0).getAPFloat();
476 APFloat D = RHS.elem<Floating>(1).getAPFloat();
477
478 APFloat ResR(A.getSemantics());
479 APFloat ResI(A.getSemantics());
480 HandleComplexComplexMul(A, B, C, D, ResR, ResI);
481
482 // Copy into the result.
483 Floating RA = S.allocFloat(A.getSemantics());
484 RA.copy(ResR);
485 Result.elem<Floating>(0) = RA; // Floating(ResR);
486
487 Floating RI = S.allocFloat(A.getSemantics());
488 RI.copy(ResI);
489 Result.elem<Floating>(1) = RI; // Floating(ResI);
490 Result.initializeAllElements();
491 } else {
492 // Integer element type.
493 const T &LHSR = LHS.elem<T>(0);
494 const T &LHSI = LHS.elem<T>(1);
495 const T &RHSR = RHS.elem<T>(0);
496 const T &RHSI = RHS.elem<T>(1);
497 unsigned Bits = LHSR.bitWidth();
498
499 // real(Result) = (real(LHS) * real(RHS)) - (imag(LHS) * imag(RHS))
500 T A;
501 if (T::mul(LHSR, RHSR, Bits, &A))
502 return false;
503 T B;
504 if (T::mul(LHSI, RHSI, Bits, &B))
505 return false;
506 if (T::sub(A, B, Bits, &Result.elem<T>(0)))
507 return false;
508
509 // imag(Result) = (real(LHS) * imag(RHS)) + (imag(LHS) * real(RHS))
510 if (T::mul(LHSR, RHSI, Bits, &A))
511 return false;
512 if (T::mul(LHSI, RHSR, Bits, &B))
513 return false;
514 if (T::add(A, B, Bits, &Result.elem<T>(1)))
515 return false;
516 Result.initialize();
517 Result.initializeAllElements();
518 }
519
520 return true;
521}
522
523template <PrimType Name, class T = typename PrimConv<Name>::T>
524inline bool Divc(InterpState &S, CodePtr OpPC) {
525 const Pointer &RHS = S.Stk.pop<Pointer>();
526 const Pointer &LHS = S.Stk.pop<Pointer>();
527 const Pointer &Result = S.Stk.peek<Pointer>();
528
529 if constexpr (std::is_same_v<T, Floating>) {
530 APFloat A = LHS.elem<Floating>(0).getAPFloat();
531 APFloat B = LHS.elem<Floating>(1).getAPFloat();
532 APFloat C = RHS.elem<Floating>(0).getAPFloat();
533 APFloat D = RHS.elem<Floating>(1).getAPFloat();
534
535 APFloat ResR(A.getSemantics());
536 APFloat ResI(A.getSemantics());
537 HandleComplexComplexDiv(A, B, C, D, ResR, ResI);
538
539 // Copy into the result.
540 Floating RA = S.allocFloat(A.getSemantics());
541 RA.copy(ResR);
542 Result.elem<Floating>(0) = RA; // Floating(ResR);
543
544 Floating RI = S.allocFloat(A.getSemantics());
545 RI.copy(ResI);
546 Result.elem<Floating>(1) = RI; // Floating(ResI);
547
548 Result.initializeAllElements();
549 } else {
550 // Integer element type.
551 const T &LHSR = LHS.elem<T>(0);
552 const T &LHSI = LHS.elem<T>(1);
553 const T &RHSR = RHS.elem<T>(0);
554 const T &RHSI = RHS.elem<T>(1);
555 unsigned Bits = LHSR.bitWidth();
556 const T Zero = T::from(0, Bits);
557
560 const SourceInfo &E = S.Current->getSource(OpPC);
561 S.FFDiag(E, diag::note_expr_divide_by_zero);
562 return false;
563 }
564
565 // Den = real(RHS)² + imag(RHS)²
566 T A, B;
567 if (T::mul(RHSR, RHSR, Bits, &A) || T::mul(RHSI, RHSI, Bits, &B)) {
568 // Ignore overflow here, because that's what the current interpeter does.
569 }
570 T Den;
571 if (T::add(A, B, Bits, &Den))
572 return false;
573
575 const SourceInfo &E = S.Current->getSource(OpPC);
576 S.FFDiag(E, diag::note_expr_divide_by_zero);
577 return false;
578 }
579
580 // real(Result) = ((real(LHS) * real(RHS)) + (imag(LHS) * imag(RHS))) / Den
581 T &ResultR = Result.elem<T>(0);
582 T &ResultI = Result.elem<T>(1);
583
584 if (T::mul(LHSR, RHSR, Bits, &A) || T::mul(LHSI, RHSI, Bits, &B))
585 return false;
586 if (T::add(A, B, Bits, &ResultR))
587 return false;
588 if (T::div(ResultR, Den, Bits, &ResultR))
589 return false;
590
591 // imag(Result) = ((imag(LHS) * real(RHS)) - (real(LHS) * imag(RHS))) / Den
592 if (T::mul(LHSI, RHSR, Bits, &A) || T::mul(LHSR, RHSI, Bits, &B))
593 return false;
594 if (T::sub(A, B, Bits, &ResultI))
595 return false;
596 if (T::div(ResultI, Den, Bits, &ResultI))
597 return false;
598 Result.initializeAllElements();
599 }
600
601 return true;
602}
603
604/// 1) Pops the RHS from the stack.
605/// 2) Pops the LHS from the stack.
606/// 3) Pushes 'LHS & RHS' on the stack
607template <PrimType Name, class T = typename PrimConv<Name>::T>
608bool BitAnd(InterpState &S, CodePtr OpPC) {
609 const T &RHS = S.Stk.pop<T>();
610 const T &LHS = S.Stk.pop<T>();
611 unsigned Bits = RHS.bitWidth();
612
613 T Result;
614 if constexpr (needsAlloc<T>())
615 Result = S.allocAP<T>(Bits);
616
617 if (!T::bitAnd(LHS, RHS, Bits, &Result)) {
618 S.Stk.push<T>(Result);
619 return true;
620 }
621 return false;
622}
623
624/// 1) Pops the RHS from the stack.
625/// 2) Pops the LHS from the stack.
626/// 3) Pushes 'LHS | RHS' on the stack
627template <PrimType Name, class T = typename PrimConv<Name>::T>
628bool BitOr(InterpState &S, CodePtr OpPC) {
629 const T &RHS = S.Stk.pop<T>();
630 const T &LHS = S.Stk.pop<T>();
631 unsigned Bits = RHS.bitWidth();
632
633 T Result;
634 if constexpr (needsAlloc<T>())
635 Result = S.allocAP<T>(Bits);
636
637 if (!T::bitOr(LHS, RHS, Bits, &Result)) {
638 S.Stk.push<T>(Result);
639 return true;
640 }
641 return false;
642}
643
644/// 1) Pops the RHS from the stack.
645/// 2) Pops the LHS from the stack.
646/// 3) Pushes 'LHS ^ RHS' on the stack
647template <PrimType Name, class T = typename PrimConv<Name>::T>
648bool BitXor(InterpState &S, CodePtr OpPC) {
649 const T &RHS = S.Stk.pop<T>();
650 const T &LHS = S.Stk.pop<T>();
651
652 unsigned Bits = RHS.bitWidth();
653
654 T Result;
655 if constexpr (needsAlloc<T>())
656 Result = S.allocAP<T>(Bits);
657
658 if (!T::bitXor(LHS, RHS, Bits, &Result)) {
659 S.Stk.push<T>(Result);
660 return true;
661 }
662 return false;
663}
664
665/// 1) Pops the RHS from the stack.
666/// 2) Pops the LHS from the stack.
667/// 3) Pushes 'LHS % RHS' on the stack (the remainder of dividing LHS by RHS).
668template <PrimType Name, class T = typename PrimConv<Name>::T>
669bool Rem(InterpState &S, CodePtr OpPC) {
670 const T &RHS = S.Stk.pop<T>();
671 const T &LHS = S.Stk.pop<T>();
672 const unsigned Bits = RHS.bitWidth() * 2;
673
674 if (!CheckDivRem(S, OpPC, LHS, RHS))
675 return false;
676
677 T Result;
678 if constexpr (needsAlloc<T>())
679 Result = S.allocAP<T>(LHS.bitWidth());
680
681 if (!T::rem(LHS, RHS, Bits, &Result)) {
682 S.Stk.push<T>(Result);
683 return true;
684 }
685 return false;
686}
687
688/// 1) Pops the RHS from the stack.
689/// 2) Pops the LHS from the stack.
690/// 3) Pushes 'LHS / RHS' on the stack
691template <PrimType Name, class T = typename PrimConv<Name>::T>
692bool Div(InterpState &S, CodePtr OpPC) {
693 const T &RHS = S.Stk.pop<T>();
694 const T &LHS = S.Stk.pop<T>();
695 const unsigned Bits = RHS.bitWidth() * 2;
696
697 if (!CheckDivRem(S, OpPC, LHS, RHS))
698 return false;
699
700 T Result;
701 if constexpr (needsAlloc<T>())
702 Result = S.allocAP<T>(LHS.bitWidth());
703
704 if (!T::div(LHS, RHS, Bits, &Result)) {
705 S.Stk.push<T>(Result);
706 return true;
707 }
708
709 if constexpr (std::is_same_v<T, FixedPoint>) {
710 if (handleFixedPointOverflow(S, OpPC, Result)) {
711 S.Stk.push<T>(Result);
712 return true;
713 }
714 }
715 return false;
716}
717
718inline bool Divf(InterpState &S, CodePtr OpPC, uint32_t FPOI) {
719 const Floating &RHS = S.Stk.pop<Floating>();
720 const Floating &LHS = S.Stk.pop<Floating>();
721
722 if (!CheckDivRem(S, OpPC, LHS, RHS))
723 return false;
724
726
727 Floating Result = S.allocFloat(LHS.getSemantics());
728 auto Status = Floating::div(LHS, RHS, getRoundingMode(FPO), &Result);
729
731 return CheckFloatResult(S, OpPC, Result, Status, FPO);
732}
733
734//===----------------------------------------------------------------------===//
735// Inv
736//===----------------------------------------------------------------------===//
737
738inline bool Inv(InterpState &S, CodePtr OpPC) {
739 const auto &Val = S.Stk.pop<Boolean>();
740 S.Stk.push<Boolean>(!Val);
741 return true;
742}
743
744//===----------------------------------------------------------------------===//
745// Neg
746//===----------------------------------------------------------------------===//
747
748template <PrimType Name, class T = typename PrimConv<Name>::T>
749bool Neg(InterpState &S, CodePtr OpPC) {
750 const T &Value = S.Stk.pop<T>();
751
752 if constexpr (std::is_same_v<T, Floating>) {
753 T Result = S.allocFloat(Value.getSemantics());
754
755 if (!T::neg(Value, &Result)) {
756 S.Stk.push<T>(Result);
757 return true;
758 }
759 return false;
760 } else {
761 T Result;
762 if constexpr (needsAlloc<T>())
763 Result = S.allocAP<T>(Value.bitWidth());
764
765 if (!T::neg(Value, &Result)) {
766 S.Stk.push<T>(Result);
767 return true;
768 }
769
770 assert(isIntegralType(Name) &&
771 "don't expect other types to fail at constexpr negation");
772 S.Stk.push<T>(Result);
773
774 APSInt NegatedValue = -Value.toAPSInt(Value.bitWidth() + 1);
776 const Expr *E = S.Current->getExpr(OpPC);
777 QualType Type = E->getType();
778 SmallString<32> Trunc;
779 NegatedValue.trunc(Result.bitWidth())
780 .toString(Trunc, 10, Result.isSigned(), /*formatAsCLiteral=*/false,
781 /*UpperCase=*/true, /*InsertSeparators=*/true);
782 S.report(E->getExprLoc(), diag::warn_integer_constant_overflow)
783 << Trunc << Type << E->getSourceRange();
784 return true;
785 }
786
787 return handleOverflow(S, OpPC, NegatedValue);
788 }
789}
790
791enum class PushVal : bool {
794};
795enum class IncDecOp {
798};
799
800template <typename T, IncDecOp Op, PushVal DoPush>
801bool IncDecHelper(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
802 bool CanOverflow) {
803 assert(!Ptr.isDummy());
804
805 if (!S.inConstantContext()) {
806 if (isConstexprUnknown(Ptr))
807 return false;
808 }
809
810 if constexpr (std::is_same_v<T, Boolean>) {
811 if (!S.getLangOpts().CPlusPlus14)
812 return Invalid(S, OpPC);
813 }
814
815 const T &Value = Ptr.deref<T>();
816 T Result;
817 if constexpr (needsAlloc<T>())
818 Result = S.allocAP<T>(Value.bitWidth());
819
820 if constexpr (DoPush == PushVal::Yes)
821 S.Stk.push<T>(Value);
822
823 if constexpr (Op == IncDecOp::Inc) {
824 if (!T::increment(Value, &Result) || !CanOverflow) {
825 Ptr.deref<T>() = Result;
826 return true;
827 }
828 } else {
829 if (!T::decrement(Value, &Result) || !CanOverflow) {
830 Ptr.deref<T>() = Result;
831 return true;
832 }
833 }
834 assert(CanOverflow);
835
836 // Something went wrong with the previous operation. Compute the
837 // result with another bit of precision.
838 unsigned Bits = Value.bitWidth() + 1;
839 APSInt APResult;
840 if constexpr (Op == IncDecOp::Inc)
841 APResult = ++Value.toAPSInt(Bits);
842 else
843 APResult = --Value.toAPSInt(Bits);
844
845 // Report undefined behaviour, stopping if required.
847 const Expr *E = S.Current->getExpr(OpPC);
848 QualType Type = E->getType();
849 SmallString<32> Trunc;
850 APResult.trunc(Result.bitWidth())
851 .toString(Trunc, 10, Result.isSigned(), /*formatAsCLiteral=*/false,
852 /*UpperCase=*/true, /*InsertSeparators=*/true);
853 S.report(E->getExprLoc(), diag::warn_integer_constant_overflow)
854 << Trunc << Type << E->getSourceRange();
855 return true;
856 }
857 return handleOverflow(S, OpPC, APResult);
858}
859
860/// 1) Pops a pointer from the stack
861/// 2) Load the value from the pointer
862/// 3) Writes the value increased by one back to the pointer
863/// 4) Pushes the original (pre-inc) value on the stack.
864template <PrimType Name, class T = typename PrimConv<Name>::T>
865bool Inc(InterpState &S, CodePtr OpPC, bool CanOverflow) {
866 const Pointer &Ptr = S.Stk.pop<Pointer>();
867 if (!CheckLoad(S, OpPC, Ptr, AK_Increment))
868 return false;
869
871 CanOverflow);
872}
873
874/// 1) Pops a pointer from the stack
875/// 2) Load the value from the pointer
876/// 3) Writes the value increased by one back to the pointer
877template <PrimType Name, class T = typename PrimConv<Name>::T>
878bool IncPop(InterpState &S, CodePtr OpPC, bool CanOverflow) {
879 const Pointer &Ptr = S.Stk.pop<Pointer>();
880 if (!CheckLoad(S, OpPC, Ptr, AK_Increment))
881 return false;
882
883 return IncDecHelper<T, IncDecOp::Inc, PushVal::No>(S, OpPC, Ptr, CanOverflow);
884}
885
886template <PrimType Name, class T = typename PrimConv<Name>::T>
887bool PreInc(InterpState &S, CodePtr OpPC, bool CanOverflow) {
888 const Pointer &Ptr = S.Stk.peek<Pointer>();
889 if (!CheckLoad(S, OpPC, Ptr, AK_Increment))
890 return false;
891
892 return IncDecHelper<T, IncDecOp::Inc, PushVal::No>(S, OpPC, Ptr, CanOverflow);
893}
894
895/// 1) Pops a pointer from the stack
896/// 2) Load the value from the pointer
897/// 3) Writes the value decreased by one back to the pointer
898/// 4) Pushes the original (pre-dec) value on the stack.
899template <PrimType Name, class T = typename PrimConv<Name>::T>
900bool Dec(InterpState &S, CodePtr OpPC, bool CanOverflow) {
901 const Pointer &Ptr = S.Stk.pop<Pointer>();
902 if (!CheckLoad(S, OpPC, Ptr, AK_Decrement))
903 return false;
904
906 CanOverflow);
907}
908
909/// 1) Pops a pointer from the stack
910/// 2) Load the value from the pointer
911/// 3) Writes the value decreased by one back to the pointer
912template <PrimType Name, class T = typename PrimConv<Name>::T>
913bool DecPop(InterpState &S, CodePtr OpPC, bool CanOverflow) {
914 const Pointer &Ptr = S.Stk.pop<Pointer>();
915 if (!CheckLoad(S, OpPC, Ptr, AK_Decrement))
916 return false;
917
918 return IncDecHelper<T, IncDecOp::Dec, PushVal::No>(S, OpPC, Ptr, CanOverflow);
919}
920
921template <PrimType Name, class T = typename PrimConv<Name>::T>
922bool PreDec(InterpState &S, CodePtr OpPC, bool CanOverflow) {
923 const Pointer &Ptr = S.Stk.peek<Pointer>();
924 if (!CheckLoad(S, OpPC, Ptr, AK_Decrement))
925 return false;
926 return IncDecHelper<T, IncDecOp::Dec, PushVal::No>(S, OpPC, Ptr, CanOverflow);
927}
928
929template <IncDecOp Op, PushVal DoPush>
931 uint32_t FPOI) {
932 Floating Value = Ptr.deref<Floating>();
933 Floating Result = S.allocFloat(Value.getSemantics());
934
935 if constexpr (DoPush == PushVal::Yes)
937
939 llvm::APFloat::opStatus Status;
940 if constexpr (Op == IncDecOp::Inc)
942 else
944
945 Ptr.deref<Floating>() = Result;
946
947 return CheckFloatResult(S, OpPC, Result, Status, FPO);
948}
949
950inline bool Incf(InterpState &S, CodePtr OpPC, uint32_t FPOI) {
951 const Pointer &Ptr = S.Stk.pop<Pointer>();
952 if (!CheckLoad(S, OpPC, Ptr, AK_Increment))
953 return false;
954
955 return IncDecFloatHelper<IncDecOp::Inc, PushVal::Yes>(S, OpPC, Ptr, FPOI);
956}
957
958inline bool IncfPop(InterpState &S, CodePtr OpPC, uint32_t FPOI) {
959 const Pointer &Ptr = S.Stk.pop<Pointer>();
960 if (!CheckLoad(S, OpPC, Ptr, AK_Increment))
961 return false;
962
963 return IncDecFloatHelper<IncDecOp::Inc, PushVal::No>(S, OpPC, Ptr, FPOI);
964}
965
966inline bool Decf(InterpState &S, CodePtr OpPC, uint32_t FPOI) {
967 const Pointer &Ptr = S.Stk.pop<Pointer>();
968 if (!CheckLoad(S, OpPC, Ptr, AK_Decrement))
969 return false;
970
971 return IncDecFloatHelper<IncDecOp::Dec, PushVal::Yes>(S, OpPC, Ptr, FPOI);
972}
973
974inline bool DecfPop(InterpState &S, CodePtr OpPC, uint32_t FPOI) {
975 const Pointer &Ptr = S.Stk.pop<Pointer>();
976 if (!CheckLoad(S, OpPC, Ptr, AK_Decrement))
977 return false;
978
979 return IncDecFloatHelper<IncDecOp::Dec, PushVal::No>(S, OpPC, Ptr, FPOI);
980}
981
982/// 1) Pops the value from the stack.
983/// 2) Pushes the bitwise complemented value on the stack (~V).
984template <PrimType Name, class T = typename PrimConv<Name>::T>
985bool Comp(InterpState &S, CodePtr OpPC) {
986 const T &Val = S.Stk.pop<T>();
987
988 T Result;
989 if constexpr (needsAlloc<T>())
990 Result = S.allocAP<T>(Val.bitWidth());
991
992 if (!T::comp(Val, &Result)) {
993 S.Stk.push<T>(Result);
994 return true;
995 }
996 return false;
997}
998
999//===----------------------------------------------------------------------===//
1000// EQ, NE, GT, GE, LT, LE
1001//===----------------------------------------------------------------------===//
1002
1003using CompareFn = llvm::function_ref<bool(ComparisonCategoryResult)>;
1004
1005template <typename T>
1007 assert((!std::is_same_v<T, MemberPointer>) &&
1008 "Non-equality comparisons on member pointer types should already be "
1009 "rejected in Sema.");
1010 using BoolT = PrimConv<PT_Bool>::T;
1011 const T &RHS = S.Stk.pop<T>();
1012 const T &LHS = S.Stk.pop<T>();
1013 S.Stk.push<BoolT>(BoolT::from(Fn(LHS.compare(RHS))));
1014 return true;
1015}
1016
1017template <typename T>
1019 return CmpHelper<T>(S, OpPC, Fn);
1020}
1021
1022template <>
1024 using BoolT = PrimConv<PT_Bool>::T;
1025 const Pointer &RHS = S.Stk.pop<Pointer>();
1026 const Pointer &LHS = S.Stk.pop<Pointer>();
1027
1028 // Function pointers cannot be compared in an ordered way.
1029 if (LHS.isFunctionPointer() || RHS.isFunctionPointer() ||
1030 LHS.isTypeidPointer() || RHS.isTypeidPointer()) {
1031 const SourceInfo &Loc = S.Current->getSource(OpPC);
1032 S.FFDiag(Loc, diag::note_constexpr_pointer_comparison_unspecified)
1033 << LHS.toDiagnosticString(S.getASTContext())
1035 return false;
1036 }
1037
1038 if (!Pointer::hasSameBase(LHS, RHS)) {
1039 const SourceInfo &Loc = S.Current->getSource(OpPC);
1040 S.FFDiag(Loc, diag::note_constexpr_pointer_comparison_unspecified)
1041 << LHS.toDiagnosticString(S.getASTContext())
1043 return false;
1044 }
1045
1046 // Diagnose comparisons between fields with different access specifiers.
1047 if (std::optional<std::pair<Pointer, Pointer>> Split =
1048 Pointer::computeSplitPoint(LHS, RHS)) {
1049 const FieldDecl *LF = Split->first.getField();
1050 const FieldDecl *RF = Split->second.getField();
1051 if (LF && RF && !LF->getParent()->isUnion() &&
1052 LF->getAccess() != RF->getAccess()) {
1053 S.CCEDiag(S.Current->getSource(OpPC),
1054 diag::note_constexpr_pointer_comparison_differing_access)
1055 << LF << LF->getAccess() << RF << RF->getAccess() << LF->getParent();
1056 }
1057 }
1058
1059 unsigned VL = LHS.getByteOffset();
1060 unsigned VR = RHS.getByteOffset();
1061 S.Stk.push<BoolT>(BoolT::from(Fn(Compare(VL, VR))));
1062 return true;
1063}
1064
1065static inline bool IsOpaqueConstantCall(const CallExpr *E) {
1066 unsigned Builtin = E->getBuiltinCallee();
1067 return (Builtin == Builtin::BI__builtin___CFStringMakeConstantString ||
1068 Builtin == Builtin::BI__builtin___NSStringMakeConstantString ||
1069 Builtin == Builtin::BI__builtin_ptrauth_sign_constant ||
1070 Builtin == Builtin::BI__builtin_function_start);
1071}
1072
1074 const Pointer &RHS);
1075
1076template <>
1078 using BoolT = PrimConv<PT_Bool>::T;
1079 const Pointer &RHS = S.Stk.pop<Pointer>();
1080 const Pointer &LHS = S.Stk.pop<Pointer>();
1081
1082 if (LHS.isZero() && RHS.isZero()) {
1083 S.Stk.push<BoolT>(BoolT::from(Fn(ComparisonCategoryResult::Equal)));
1084 return true;
1085 }
1086
1087 // Reject comparisons to weak pointers.
1088 for (const auto &P : {LHS, RHS}) {
1089 if (P.isZero())
1090 continue;
1091 if (P.isWeak()) {
1092 const SourceInfo &Loc = S.Current->getSource(OpPC);
1093 S.FFDiag(Loc, diag::note_constexpr_pointer_weak_comparison)
1094 << P.toDiagnosticString(S.getASTContext());
1095 return false;
1096 }
1097 }
1098
1099 if (!S.inConstantContext()) {
1100 if (isConstexprUnknown(LHS) || isConstexprUnknown(RHS))
1101 return false;
1102 }
1103
1104 if (LHS.isFunctionPointer() && RHS.isFunctionPointer()) {
1105 S.Stk.push<BoolT>(BoolT::from(Fn(Compare(LHS.getIntegerRepresentation(),
1106 RHS.getIntegerRepresentation()))));
1107 return true;
1108 }
1109
1110 // FIXME: The source check here isn't entirely correct.
1111 if (LHS.pointsToStringLiteral() && RHS.pointsToStringLiteral() &&
1112 LHS.getFieldDesc()->asExpr() != RHS.getFieldDesc()->asExpr()) {
1114 const SourceInfo &Loc = S.Current->getSource(OpPC);
1115 S.FFDiag(Loc, diag::note_constexpr_literal_comparison)
1116 << LHS.toDiagnosticString(S.getASTContext())
1118 return false;
1119 }
1120 }
1121
1122 if (Pointer::hasSameBase(LHS, RHS)) {
1123 size_t A = LHS.computeOffsetForComparison();
1124 size_t B = RHS.computeOffsetForComparison();
1125 S.Stk.push<BoolT>(BoolT::from(Fn(Compare(A, B))));
1126 return true;
1127 }
1128
1129 // Otherwise we need to do a bunch of extra checks before returning Unordered.
1130 if (LHS.isOnePastEnd() && !RHS.isOnePastEnd() && !RHS.isZero() &&
1131 RHS.getOffset() == 0) {
1132 const SourceInfo &Loc = S.Current->getSource(OpPC);
1133 S.FFDiag(Loc, diag::note_constexpr_pointer_comparison_past_end)
1134 << LHS.toDiagnosticString(S.getASTContext());
1135 return false;
1136 }
1137 if (RHS.isOnePastEnd() && !LHS.isOnePastEnd() && !LHS.isZero() &&
1138 LHS.getOffset() == 0) {
1139 const SourceInfo &Loc = S.Current->getSource(OpPC);
1140 S.FFDiag(Loc, diag::note_constexpr_pointer_comparison_past_end)
1142 return false;
1143 }
1144
1145 bool BothNonNull = !LHS.isZero() && !RHS.isZero();
1146 // Reject comparisons to literals.
1147 for (const auto &P : {LHS, RHS}) {
1148 if (P.isZero())
1149 continue;
1150 if (BothNonNull && P.pointsToLiteral()) {
1151 const Expr *E = P.getDeclDesc()->asExpr();
1152 if (isa<StringLiteral>(E)) {
1153 const SourceInfo &Loc = S.Current->getSource(OpPC);
1154 S.FFDiag(Loc, diag::note_constexpr_literal_comparison);
1155 return false;
1156 }
1157 if (const auto *CE = dyn_cast<CallExpr>(E);
1158 CE && IsOpaqueConstantCall(CE)) {
1159 const SourceInfo &Loc = S.Current->getSource(OpPC);
1160 S.FFDiag(Loc, diag::note_constexpr_opaque_call_comparison)
1161 << P.toDiagnosticString(S.getASTContext());
1162 return false;
1163 }
1164 } else if (BothNonNull && P.isIntegralPointer()) {
1165 const SourceInfo &Loc = S.Current->getSource(OpPC);
1166 S.FFDiag(Loc, diag::note_constexpr_pointer_constant_comparison)
1167 << LHS.toDiagnosticString(S.getASTContext())
1169 return false;
1170 }
1171 }
1172
1173 if (LHS.isUnknownSizeArray() && RHS.isUnknownSizeArray()) {
1174 const SourceInfo &Loc = S.Current->getSource(OpPC);
1175 S.FFDiag(Loc, diag::note_constexpr_pointer_comparison_zero_sized)
1176 << LHS.toDiagnosticString(S.getASTContext())
1178 return false;
1179 }
1180
1181 S.Stk.push<BoolT>(BoolT::from(Fn(ComparisonCategoryResult::Unordered)));
1182 return true;
1183}
1184
1185template <>
1187 CompareFn Fn) {
1188 const auto &RHS = S.Stk.pop<MemberPointer>();
1189 const auto &LHS = S.Stk.pop<MemberPointer>();
1190
1191 // If either operand is a pointer to a weak function, the comparison is not
1192 // constant.
1193 for (const auto &MP : {LHS, RHS}) {
1194 if (MP.isWeak()) {
1195 const SourceInfo &Loc = S.Current->getSource(OpPC);
1196 S.FFDiag(Loc, diag::note_constexpr_mem_pointer_weak_comparison)
1197 << MP.getMemberFunction();
1198 return false;
1199 }
1200 }
1201
1202 // C++11 [expr.eq]p2:
1203 // If both operands are null, they compare equal. Otherwise if only one is
1204 // null, they compare unequal.
1205 if (LHS.isZero() && RHS.isZero()) {
1207 return true;
1208 }
1209 if (LHS.isZero() || RHS.isZero()) {
1211 return true;
1212 }
1213
1214 // We cannot compare against virtual declarations at compile time.
1215 for (const auto &MP : {LHS, RHS}) {
1216 if (const CXXMethodDecl *MD = MP.getMemberFunction();
1217 MD && MD->isVirtual()) {
1218 const SourceInfo &Loc = S.Current->getSource(OpPC);
1219 S.CCEDiag(Loc, diag::note_constexpr_compare_virtual_mem_ptr) << MD;
1220 }
1221 }
1222
1223 S.Stk.push<Boolean>(Boolean::from(Fn(LHS.compare(RHS))));
1224 return true;
1225}
1226
1227template <PrimType Name, class T = typename PrimConv<Name>::T>
1228bool EQ(InterpState &S, CodePtr OpPC) {
1229 return CmpHelperEQ<T>(S, OpPC, [](ComparisonCategoryResult R) {
1231 });
1232}
1233
1234template <PrimType Name, class T = typename PrimConv<Name>::T>
1235bool CMP3(InterpState &S, CodePtr OpPC, const ComparisonCategoryInfo *CmpInfo) {
1236 const T &RHS = S.Stk.pop<T>();
1237 const T &LHS = S.Stk.pop<T>();
1238 const Pointer &P = S.Stk.peek<Pointer>();
1239
1240 ComparisonCategoryResult CmpResult = LHS.compare(RHS);
1241 if constexpr (std::is_same_v<T, Pointer>) {
1242 if (CmpResult == ComparisonCategoryResult::Unordered) {
1243 const SourceInfo &Loc = S.Current->getSource(OpPC);
1244 S.FFDiag(Loc, diag::note_constexpr_pointer_comparison_unspecified)
1245 << LHS.toDiagnosticString(S.getASTContext())
1246 << RHS.toDiagnosticString(S.getASTContext());
1247 return false;
1248 }
1249 }
1250
1251 assert(CmpInfo);
1252 const auto *CmpValueInfo =
1253 CmpInfo->getValueInfo(CmpInfo->makeWeakResult(CmpResult));
1254 assert(CmpValueInfo);
1255 assert(CmpValueInfo->hasValidIntValue());
1256 return SetThreeWayComparisonField(S, OpPC, P, CmpValueInfo->getIntValue());
1257}
1258
1259template <PrimType Name, class T = typename PrimConv<Name>::T>
1260bool NE(InterpState &S, CodePtr OpPC) {
1261 return CmpHelperEQ<T>(S, OpPC, [](ComparisonCategoryResult R) {
1263 });
1264}
1265
1266template <PrimType Name, class T = typename PrimConv<Name>::T>
1267bool LT(InterpState &S, CodePtr OpPC) {
1268 return CmpHelper<T>(S, OpPC, [](ComparisonCategoryResult R) {
1270 });
1271}
1272
1273template <PrimType Name, class T = typename PrimConv<Name>::T>
1274bool LE(InterpState &S, CodePtr OpPC) {
1275 return CmpHelper<T>(S, OpPC, [](ComparisonCategoryResult R) {
1276 return R == ComparisonCategoryResult::Less ||
1278 });
1279}
1280
1281template <PrimType Name, class T = typename PrimConv<Name>::T>
1282bool GT(InterpState &S, CodePtr OpPC) {
1283 return CmpHelper<T>(S, OpPC, [](ComparisonCategoryResult R) {
1285 });
1286}
1287
1288template <PrimType Name, class T = typename PrimConv<Name>::T>
1289bool GE(InterpState &S, CodePtr OpPC) {
1290 return CmpHelper<T>(S, OpPC, [](ComparisonCategoryResult R) {
1293 });
1294}
1295
1296//===----------------------------------------------------------------------===//
1297// Dup, Pop, Test
1298//===----------------------------------------------------------------------===//
1299
1300template <PrimType Name, class T = typename PrimConv<Name>::T>
1301bool Dup(InterpState &S, CodePtr OpPC) {
1302 S.Stk.push<T>(S.Stk.peek<T>());
1303 return true;
1304}
1305
1306template <PrimType Name, class T = typename PrimConv<Name>::T>
1307bool Pop(InterpState &S, CodePtr OpPC) {
1308 S.Stk.discard<T>();
1309 return true;
1310}
1311
1312/// [Value1, Value2] -> [Value2, Value1]
1313template <PrimType TopName, PrimType BottomName>
1314bool Flip(InterpState &S, CodePtr OpPC) {
1315 using TopT = typename PrimConv<TopName>::T;
1316 using BottomT = typename PrimConv<BottomName>::T;
1317
1318 const auto &Top = S.Stk.pop<TopT>();
1319 const auto &Bottom = S.Stk.pop<BottomT>();
1320
1321 S.Stk.push<TopT>(Top);
1322 S.Stk.push<BottomT>(Bottom);
1323
1324 return true;
1325}
1326
1327//===----------------------------------------------------------------------===//
1328// Const
1329//===----------------------------------------------------------------------===//
1330
1331template <PrimType Name, class T = typename PrimConv<Name>::T>
1332bool Const(InterpState &S, CodePtr OpPC, const T &Arg) {
1333 if constexpr (needsAlloc<T>()) {
1334 T Result = S.allocAP<T>(Arg.bitWidth());
1335 Result.copy(Arg.toAPSInt());
1336 S.Stk.push<T>(Result);
1337 return true;
1338 }
1339 S.Stk.push<T>(Arg);
1340 return true;
1341}
1342
1343inline bool ConstFloat(InterpState &S, CodePtr OpPC, const Floating &F) {
1345 Result.copy(F.getAPFloat());
1346 S.Stk.push<Floating>(Result);
1347 return true;
1348}
1349
1350//===----------------------------------------------------------------------===//
1351// Get/Set Local/Param/Global/This
1352//===----------------------------------------------------------------------===//
1353
1354template <PrimType Name, class T = typename PrimConv<Name>::T>
1355bool GetLocal(InterpState &S, CodePtr OpPC, uint32_t I) {
1356 const Block *B = S.Current->getLocalBlock(I);
1357 if (!CheckLocalLoad(S, OpPC, B))
1358 return false;
1359 S.Stk.push<T>(B->deref<T>());
1360 return true;
1361}
1362
1363bool EndLifetime(InterpState &S, CodePtr OpPC);
1364bool EndLifetimePop(InterpState &S, CodePtr OpPC);
1365bool StartLifetime(InterpState &S, CodePtr OpPC);
1366
1367/// 1) Pops the value from the stack.
1368/// 2) Writes the value to the local variable with the
1369/// given offset.
1370template <PrimType Name, class T = typename PrimConv<Name>::T>
1371bool SetLocal(InterpState &S, CodePtr OpPC, uint32_t I) {
1372 S.Current->setLocal<T>(I, S.Stk.pop<T>());
1373 return true;
1374}
1375
1376template <PrimType Name, class T = typename PrimConv<Name>::T>
1377bool GetParam(InterpState &S, CodePtr OpPC, uint32_t I) {
1379 return false;
1380 }
1381 S.Stk.push<T>(S.Current->getParam<T>(I));
1382 return true;
1383}
1384
1385template <PrimType Name, class T = typename PrimConv<Name>::T>
1386bool SetParam(InterpState &S, CodePtr OpPC, uint32_t I) {
1387 S.Current->setParam<T>(I, S.Stk.pop<T>());
1388 return true;
1389}
1390
1391/// 1) Peeks a pointer on the stack
1392/// 2) Pushes the value of the pointer's field on the stack
1393template <PrimType Name, class T = typename PrimConv<Name>::T>
1394bool GetField(InterpState &S, CodePtr OpPC, uint32_t I) {
1395 const Pointer &Obj = S.Stk.peek<Pointer>();
1396 if (!CheckNull(S, OpPC, Obj, CSK_Field))
1397 return false;
1398 if (!CheckRange(S, OpPC, Obj, CSK_Field))
1399 return false;
1400 const Pointer &Field = Obj.atField(I);
1401 if (!CheckLoad(S, OpPC, Field))
1402 return false;
1403 S.Stk.push<T>(Field.deref<T>());
1404 return true;
1405}
1406
1407template <PrimType Name, class T = typename PrimConv<Name>::T>
1408bool SetField(InterpState &S, CodePtr OpPC, uint32_t I) {
1409 const T &Value = S.Stk.pop<T>();
1410 const Pointer &Obj = S.Stk.peek<Pointer>();
1411 if (!CheckNull(S, OpPC, Obj, CSK_Field))
1412 return false;
1413 if (!CheckRange(S, OpPC, Obj, CSK_Field))
1414 return false;
1415 const Pointer &Field = Obj.atField(I);
1416 if (!CheckStore(S, OpPC, Field))
1417 return false;
1418 Field.initialize();
1419 Field.deref<T>() = Value;
1420 return true;
1421}
1422
1423/// 1) Pops a pointer from the stack
1424/// 2) Pushes the value of the pointer's field on the stack
1425template <PrimType Name, class T = typename PrimConv<Name>::T>
1426bool GetFieldPop(InterpState &S, CodePtr OpPC, uint32_t I) {
1427 const Pointer &Obj = S.Stk.pop<Pointer>();
1428 if (!CheckNull(S, OpPC, Obj, CSK_Field))
1429 return false;
1430 if (!CheckRange(S, OpPC, Obj, CSK_Field))
1431 return false;
1432 const Pointer &Field = Obj.atField(I);
1433 if (!CheckLoad(S, OpPC, Field))
1434 return false;
1435 S.Stk.push<T>(Field.deref<T>());
1436 return true;
1437}
1438
1439template <PrimType Name, class T = typename PrimConv<Name>::T>
1440bool GetThisField(InterpState &S, CodePtr OpPC, uint32_t I) {
1442 return false;
1443 const Pointer &This = S.Current->getThis();
1444 if (!CheckThis(S, OpPC, This))
1445 return false;
1446 const Pointer &Field = This.atField(I);
1447 if (!CheckLoad(S, OpPC, Field))
1448 return false;
1449 S.Stk.push<T>(Field.deref<T>());
1450 return true;
1451}
1452
1453template <PrimType Name, class T = typename PrimConv<Name>::T>
1454bool SetThisField(InterpState &S, CodePtr OpPC, uint32_t I) {
1456 return false;
1457 const T &Value = S.Stk.pop<T>();
1458 const Pointer &This = S.Current->getThis();
1459 if (!CheckThis(S, OpPC, This))
1460 return false;
1461 const Pointer &Field = This.atField(I);
1462 if (!CheckStore(S, OpPC, Field))
1463 return false;
1464 Field.deref<T>() = Value;
1465 return true;
1466}
1467
1468template <PrimType Name, class T = typename PrimConv<Name>::T>
1469bool GetGlobal(InterpState &S, CodePtr OpPC, uint32_t I) {
1470 const Block *B = S.P.getGlobal(I);
1471
1472 if (!CheckGlobalLoad(S, OpPC, B))
1473 return false;
1474
1475 S.Stk.push<T>(B->deref<T>());
1476 return true;
1477}
1478
1479/// Same as GetGlobal, but without the checks.
1480template <PrimType Name, class T = typename PrimConv<Name>::T>
1481bool GetGlobalUnchecked(InterpState &S, CodePtr OpPC, uint32_t I) {
1482 const Block *B = S.P.getGlobal(I);
1483 const auto &Desc =
1484 *reinterpret_cast<const GlobalInlineDescriptor *>(B->rawData());
1485 if (Desc.InitState != GlobalInitState::Initialized)
1486 return DiagnoseUninitialized(S, OpPC, B->isExtern(), B->getDescriptor(),
1487 AK_Read);
1488
1489 S.Stk.push<T>(B->deref<T>());
1490 return true;
1491}
1492
1493template <PrimType Name, class T = typename PrimConv<Name>::T>
1494bool SetGlobal(InterpState &S, CodePtr OpPC, uint32_t I) {
1495 // TODO: emit warning.
1496 return false;
1497}
1498
1499template <PrimType Name, class T = typename PrimConv<Name>::T>
1500bool InitGlobal(InterpState &S, CodePtr OpPC, uint32_t I) {
1501 const Pointer &P = S.P.getGlobal(I);
1502
1503 P.deref<T>() = S.Stk.pop<T>();
1504
1505 if constexpr (std::is_same_v<T, Floating>) {
1506 auto &Val = P.deref<Floating>();
1507 if (!Val.singleWord()) {
1508 uint64_t *NewMemory = new (S.P) uint64_t[Val.numWords()];
1509 Val.take(NewMemory);
1510 }
1511
1512 } else if constexpr (needsAlloc<T>()) {
1513 auto &Val = P.deref<T>();
1514 if (!Val.singleWord()) {
1515 uint64_t *NewMemory = new (S.P) uint64_t[Val.numWords()];
1516 Val.take(NewMemory);
1517 }
1518 }
1519
1520 P.initialize();
1521 return true;
1522}
1523
1524/// 1) Converts the value on top of the stack to an APValue
1525/// 2) Sets that APValue on \Temp
1526/// 3) Initializes global with index \I with that
1527template <PrimType Name, class T = typename PrimConv<Name>::T>
1528bool InitGlobalTemp(InterpState &S, CodePtr OpPC, uint32_t I,
1529 const LifetimeExtendedTemporaryDecl *Temp) {
1531 return false;
1532 assert(Temp);
1533
1534 const Pointer &Ptr = S.P.getGlobal(I);
1535 assert(Ptr.getDeclDesc()->asExpr());
1536 S.SeenGlobalTemporaries.push_back(
1537 std::make_pair(Ptr.getDeclDesc()->asExpr(), Temp));
1538
1539 Ptr.deref<T>() = S.Stk.pop<T>();
1540 Ptr.initialize();
1541 return true;
1542}
1543
1544/// 1) Converts the value on top of the stack to an APValue
1545/// 2) Sets that APValue on \Temp
1546/// 3) Initialized global with index \I with that
1548 const LifetimeExtendedTemporaryDecl *Temp) {
1550 return false;
1551 assert(Temp);
1552
1553 const Pointer &Ptr = S.Stk.peek<Pointer>();
1554 S.SeenGlobalTemporaries.push_back(
1555 std::make_pair(Ptr.getDeclDesc()->asExpr(), Temp));
1556 return true;
1557}
1558
1559template <PrimType Name, class T = typename PrimConv<Name>::T>
1560bool InitThisField(InterpState &S, CodePtr OpPC, uint32_t I) {
1562 return false;
1563 const Pointer &This = S.Current->getThis();
1564 if (!CheckThis(S, OpPC, This))
1565 return false;
1566 const Pointer &Field = This.atField(I);
1567 assert(Field.canBeInitialized());
1568 Field.deref<T>() = S.Stk.pop<T>();
1569 Field.initialize();
1570 return true;
1571}
1572
1573template <PrimType Name, class T = typename PrimConv<Name>::T>
1574bool InitThisFieldActivate(InterpState &S, CodePtr OpPC, uint32_t I) {
1576 return false;
1577 const Pointer &This = S.Current->getThis();
1578 if (!CheckThis(S, OpPC, This))
1579 return false;
1580 const Pointer &Field = This.atField(I);
1581 assert(Field.canBeInitialized());
1582 Field.deref<T>() = S.Stk.pop<T>();
1583 Field.activate();
1584 Field.initialize();
1585 return true;
1586}
1587
1588// FIXME: The Field pointer here is too much IMO and we could instead just
1589// pass an Offset + BitWidth pair.
1590template <PrimType Name, class T = typename PrimConv<Name>::T>
1591bool InitThisBitField(InterpState &S, CodePtr OpPC, const Record::Field *F,
1592 uint32_t FieldOffset) {
1593 assert(F->isBitField());
1595 return false;
1596 const Pointer &This = S.Current->getThis();
1597 if (!CheckThis(S, OpPC, This))
1598 return false;
1599 const Pointer &Field = This.atField(FieldOffset);
1600 assert(Field.canBeInitialized());
1601 const auto &Value = S.Stk.pop<T>();
1602 Field.deref<T>() = Value.truncate(F->Decl->getBitWidthValue());
1603 Field.initialize();
1604 return true;
1605}
1606
1607template <PrimType Name, class T = typename PrimConv<Name>::T>
1609 const Record::Field *F, uint32_t FieldOffset) {
1610 assert(F->isBitField());
1612 return false;
1613 const Pointer &This = S.Current->getThis();
1614 if (!CheckThis(S, OpPC, This))
1615 return false;
1616 const Pointer &Field = This.atField(FieldOffset);
1617 assert(Field.canBeInitialized());
1618 const auto &Value = S.Stk.pop<T>();
1619 Field.deref<T>() = Value.truncate(F->Decl->getBitWidthValue());
1620 Field.initialize();
1621 Field.activate();
1622 return true;
1623}
1624
1625/// 1) Pops the value from the stack
1626/// 2) Peeks a pointer from the stack
1627/// 3) Pushes the value to field I of the pointer on the stack
1628template <PrimType Name, class T = typename PrimConv<Name>::T>
1629bool InitField(InterpState &S, CodePtr OpPC, uint32_t I) {
1630 const T &Value = S.Stk.pop<T>();
1631 const Pointer &Ptr = S.Stk.peek<Pointer>();
1632 if (!CheckRange(S, OpPC, Ptr, CSK_Field))
1633 return false;
1634 if (!CheckArray(S, OpPC, Ptr))
1635 return false;
1636
1637 const Pointer &Field = Ptr.atField(I);
1638 Field.deref<T>() = Value;
1639 Field.initialize();
1640 return true;
1641}
1642
1643template <PrimType Name, class T = typename PrimConv<Name>::T>
1644bool InitFieldActivate(InterpState &S, CodePtr OpPC, uint32_t I) {
1645 const T &Value = S.Stk.pop<T>();
1646 const Pointer &Ptr = S.Stk.peek<Pointer>();
1647 if (!CheckRange(S, OpPC, Ptr, CSK_Field))
1648 return false;
1649 if (!CheckArray(S, OpPC, Ptr))
1650 return false;
1651
1652 const Pointer &Field = Ptr.atField(I);
1653 Field.deref<T>() = Value;
1654 Field.activate();
1655 Field.initialize();
1656 return true;
1657}
1658
1659template <PrimType Name, class T = typename PrimConv<Name>::T>
1660bool InitBitField(InterpState &S, CodePtr OpPC, const Record::Field *F) {
1661 assert(F->isBitField());
1662 const T &Value = S.Stk.pop<T>();
1663 const Pointer &Ptr = S.Stk.peek<Pointer>();
1664 if (!CheckRange(S, OpPC, Ptr, CSK_Field))
1665 return false;
1666 if (!CheckArray(S, OpPC, Ptr))
1667 return false;
1668
1669 const Pointer &Field = Ptr.atField(F->Offset);
1670
1671 if constexpr (needsAlloc<T>()) {
1672 T Result = S.allocAP<T>(Value.bitWidth());
1673 if (T::isSigned())
1674 Result.copy(Value.toAPSInt()
1675 .trunc(F->Decl->getBitWidthValue())
1676 .sextOrTrunc(Value.bitWidth()));
1677 else
1678 Result.copy(Value.toAPSInt()
1679 .trunc(F->Decl->getBitWidthValue())
1680 .zextOrTrunc(Value.bitWidth()));
1681
1682 Field.deref<T>() = Result;
1683 } else {
1684 Field.deref<T>() = Value.truncate(F->Decl->getBitWidthValue());
1685 }
1686 Field.initialize();
1687 return true;
1688}
1689
1690template <PrimType Name, class T = typename PrimConv<Name>::T>
1692 const Record::Field *F) {
1693 assert(F->isBitField());
1694 const T &Value = S.Stk.pop<T>();
1695 const Pointer &Ptr = S.Stk.peek<Pointer>();
1696 if (!CheckRange(S, OpPC, Ptr, CSK_Field))
1697 return false;
1698 if (!CheckArray(S, OpPC, Ptr))
1699 return false;
1700
1701 const Pointer &Field = Ptr.atField(F->Offset);
1702
1703 if constexpr (needsAlloc<T>()) {
1704 T Result = S.allocAP<T>(Value.bitWidth());
1705 if (T::isSigned())
1706 Result.copy(Value.toAPSInt()
1707 .trunc(F->Decl->getBitWidthValue())
1708 .sextOrTrunc(Value.bitWidth()));
1709 else
1710 Result.copy(Value.toAPSInt()
1711 .trunc(F->Decl->getBitWidthValue())
1712 .zextOrTrunc(Value.bitWidth()));
1713
1714 Field.deref<T>() = Result;
1715 } else {
1716 Field.deref<T>() = Value.truncate(F->Decl->getBitWidthValue());
1717 }
1718 Field.activate();
1719 Field.initialize();
1720 return true;
1721}
1722
1723//===----------------------------------------------------------------------===//
1724// GetPtr Local/Param/Global/Field/This
1725//===----------------------------------------------------------------------===//
1726
1727inline bool GetPtrLocal(InterpState &S, CodePtr OpPC, uint32_t I) {
1729 return true;
1730}
1731
1732inline bool GetPtrParam(InterpState &S, CodePtr OpPC, uint32_t I) {
1734 return false;
1735 }
1737 return true;
1738}
1739
1740inline bool GetPtrGlobal(InterpState &S, CodePtr OpPC, uint32_t I) {
1741 S.Stk.push<Pointer>(S.P.getPtrGlobal(I));
1742 return true;
1743}
1744
1745/// 1) Peeks a Pointer
1746/// 2) Pushes Pointer.atField(Off) on the stack
1747bool GetPtrField(InterpState &S, CodePtr OpPC, uint32_t Off);
1748bool GetPtrFieldPop(InterpState &S, CodePtr OpPC, uint32_t Off);
1749
1750inline bool GetPtrThisField(InterpState &S, CodePtr OpPC, uint32_t Off) {
1752 return false;
1753 const Pointer &This = S.Current->getThis();
1754 if (!CheckThis(S, OpPC, This))
1755 return false;
1756 S.Stk.push<Pointer>(This.atField(Off));
1757 return true;
1758}
1759
1760inline bool GetPtrDerivedPop(InterpState &S, CodePtr OpPC, uint32_t Off,
1761 bool NullOK, const Type *TargetType) {
1762 const Pointer &Ptr = S.Stk.pop<Pointer>();
1763 if (!NullOK && !CheckNull(S, OpPC, Ptr, CSK_Derived))
1764 return false;
1765
1766 if (!Ptr.isBlockPointer()) {
1767 // FIXME: We don't have the necessary information in integral pointers.
1768 // The Descriptor only has a record, but that does of course not include
1769 // the potential derived classes of said record.
1770 S.Stk.push<Pointer>(Ptr);
1771 return true;
1772 }
1773
1774 if (!CheckSubobject(S, OpPC, Ptr, CSK_Derived))
1775 return false;
1776 if (!CheckDowncast(S, OpPC, Ptr, Off))
1777 return false;
1778
1779 const Record *TargetRecord = Ptr.atFieldSub(Off).getRecord();
1780 assert(TargetRecord);
1781
1782 if (TargetRecord->getDecl()->getCanonicalDecl() !=
1783 TargetType->getAsCXXRecordDecl()->getCanonicalDecl()) {
1784 QualType MostDerivedType = Ptr.getDeclDesc()->getType();
1785 S.CCEDiag(S.Current->getSource(OpPC), diag::note_constexpr_invalid_downcast)
1786 << MostDerivedType << QualType(TargetType, 0);
1787 return false;
1788 }
1789
1790 S.Stk.push<Pointer>(Ptr.atFieldSub(Off));
1791 return true;
1792}
1793
1794inline bool GetPtrBase(InterpState &S, CodePtr OpPC, uint32_t Off) {
1795 const Pointer &Ptr = S.Stk.peek<Pointer>();
1796 if (!CheckNull(S, OpPC, Ptr, CSK_Base))
1797 return false;
1798
1799 if (!Ptr.isBlockPointer()) {
1800 if (!Ptr.isIntegralPointer())
1801 return false;
1802 S.Stk.push<Pointer>(Ptr.asIntPointer().baseCast(S.getASTContext(), Off));
1803 return true;
1804 }
1805
1806 if (!CheckSubobject(S, OpPC, Ptr, CSK_Base))
1807 return false;
1808 const Pointer &Result = Ptr.atField(Off);
1809 if (Result.isPastEnd() || !Result.isBaseClass())
1810 return false;
1811 S.Stk.push<Pointer>(Result);
1812 return true;
1813}
1814
1815inline bool GetPtrBasePop(InterpState &S, CodePtr OpPC, uint32_t Off,
1816 bool NullOK) {
1817 const Pointer &Ptr = S.Stk.pop<Pointer>();
1818
1819 if (!NullOK && !CheckNull(S, OpPC, Ptr, CSK_Base))
1820 return false;
1821
1822 if (!Ptr.isBlockPointer()) {
1823 if (!Ptr.isIntegralPointer())
1824 return false;
1825 S.Stk.push<Pointer>(Ptr.asIntPointer().baseCast(S.getASTContext(), Off));
1826 return true;
1827 }
1828
1829 if (!CheckSubobject(S, OpPC, Ptr, CSK_Base))
1830 return false;
1831 const Pointer &Result = Ptr.atField(Off);
1832 if (Result.isPastEnd() || !Result.isBaseClass())
1833 return false;
1834 S.Stk.push<Pointer>(Result);
1835 return true;
1836}
1837
1838inline bool GetMemberPtrBasePop(InterpState &S, CodePtr OpPC, int32_t Off) {
1839 const auto &Ptr = S.Stk.pop<MemberPointer>();
1840 S.Stk.push<MemberPointer>(Ptr.atInstanceBase(Off));
1841 return true;
1842}
1843
1844inline bool GetPtrThisBase(InterpState &S, CodePtr OpPC, uint32_t Off) {
1846 return false;
1847 const Pointer &This = S.Current->getThis();
1848 if (!CheckThis(S, OpPC, This))
1849 return false;
1850 S.Stk.push<Pointer>(This.atField(Off));
1851 return true;
1852}
1853
1854inline bool FinishInitPop(InterpState &S, CodePtr OpPC) {
1855 const Pointer &Ptr = S.Stk.pop<Pointer>();
1856 if (Ptr.canBeInitialized())
1857 Ptr.initialize();
1858 return true;
1859}
1860
1861inline bool FinishInit(InterpState &S, CodePtr OpPC) {
1862 const Pointer &Ptr = S.Stk.peek<Pointer>();
1863 if (Ptr.canBeInitialized())
1864 Ptr.initialize();
1865 return true;
1866}
1867
1869 const Pointer &Ptr = S.Stk.peek<Pointer>();
1870 if (Ptr.canBeInitialized()) {
1871 Ptr.initialize();
1872 Ptr.activate();
1873 }
1874 return true;
1875}
1876
1878 const Pointer &Ptr = S.Stk.pop<Pointer>();
1879 if (Ptr.canBeInitialized()) {
1880 Ptr.initialize();
1881 Ptr.activate();
1882 }
1883 return true;
1884}
1885
1886bool FinishInitGlobal(InterpState &S, CodePtr OpPC);
1887
1888inline bool Dump(InterpState &S, CodePtr OpPC) {
1889 S.Stk.dump();
1890 return true;
1891}
1892
1893inline bool CheckNull(InterpState &S, CodePtr OpPC) {
1894 const auto &Ptr = S.Stk.peek<Pointer>();
1895 if (Ptr.isZero()) {
1896 S.FFDiag(S.Current->getSource(OpPC),
1897 diag::note_constexpr_dereferencing_null);
1898 return S.noteUndefinedBehavior();
1899 }
1900 return true;
1901}
1902
1903inline bool VirtBaseHelper(InterpState &S, CodePtr OpPC, const RecordDecl *Decl,
1904 const Pointer &Ptr) {
1905 Pointer Base = Ptr;
1906 while (Base.isBaseClass())
1907 Base = Base.getBase();
1908
1909 const Record::Base *VirtBase = Base.getRecord()->getVirtualBase(Decl);
1910 S.Stk.push<Pointer>(Base.atField(VirtBase->Offset));
1911 return true;
1912}
1913
1915 const RecordDecl *D) {
1916 assert(D);
1917 const Pointer &Ptr = S.Stk.pop<Pointer>();
1918 if (!CheckNull(S, OpPC, Ptr, CSK_Base))
1919 return false;
1920 return VirtBaseHelper(S, OpPC, D, Ptr);
1921}
1922
1924 const RecordDecl *D) {
1925 assert(D);
1927 return false;
1928 const Pointer &This = S.Current->getThis();
1929 if (!CheckThis(S, OpPC, This))
1930 return false;
1931 return VirtBaseHelper(S, OpPC, D, S.Current->getThis());
1932}
1933
1934//===----------------------------------------------------------------------===//
1935// Load, Store, Init
1936//===----------------------------------------------------------------------===//
1937
1938template <PrimType Name, class T = typename PrimConv<Name>::T>
1939bool Load(InterpState &S, CodePtr OpPC) {
1940 const Pointer &Ptr = S.Stk.peek<Pointer>();
1941 if (!CheckLoad(S, OpPC, Ptr))
1942 return false;
1943 if (!Ptr.isBlockPointer())
1944 return false;
1945 S.Stk.push<T>(Ptr.deref<T>());
1946 return true;
1947}
1948
1949template <PrimType Name, class T = typename PrimConv<Name>::T>
1951 const Pointer &Ptr = S.Stk.pop<Pointer>();
1952 if (!CheckLoad(S, OpPC, Ptr))
1953 return false;
1954 if (!Ptr.isBlockPointer())
1955 return false;
1956 S.Stk.push<T>(Ptr.deref<T>());
1957 return true;
1958}
1959
1960template <PrimType Name, class T = typename PrimConv<Name>::T>
1961bool Store(InterpState &S, CodePtr OpPC) {
1962 const T &Value = S.Stk.pop<T>();
1963 const Pointer &Ptr = S.Stk.peek<Pointer>();
1964 if (!CheckStore(S, OpPC, Ptr))
1965 return false;
1966 if (Ptr.canBeInitialized())
1967 Ptr.initialize();
1968 Ptr.deref<T>() = Value;
1969 return true;
1970}
1971
1972template <PrimType Name, class T = typename PrimConv<Name>::T>
1974 const T &Value = S.Stk.pop<T>();
1975 const Pointer &Ptr = S.Stk.pop<Pointer>();
1976 if (!CheckStore(S, OpPC, Ptr))
1977 return false;
1978 if (Ptr.canBeInitialized())
1979 Ptr.initialize();
1980 Ptr.deref<T>() = Value;
1981 return true;
1982}
1983
1984static inline bool Activate(InterpState &S, CodePtr OpPC) {
1985 const Pointer &Ptr = S.Stk.peek<Pointer>();
1986 if (Ptr.canBeInitialized())
1987 Ptr.activate();
1988 return true;
1989}
1990
1991static inline bool ActivateThisField(InterpState &S, CodePtr OpPC, uint32_t I) {
1993 return false;
1994
1995 const Pointer &Ptr = S.Current->getThis();
1996 assert(Ptr.atField(I).canBeInitialized());
1997 Ptr.atField(I).activate();
1998 return true;
1999}
2000
2001template <PrimType Name, class T = typename PrimConv<Name>::T>
2003 const T &Value = S.Stk.pop<T>();
2004 const Pointer &Ptr = S.Stk.peek<Pointer>();
2005
2006 if (Ptr.canBeInitialized()) {
2007 Ptr.initialize();
2008 Ptr.activate();
2009 }
2010
2011 if (!CheckStore(S, OpPC, Ptr))
2012 return false;
2013 Ptr.deref<T>() = Value;
2014 return true;
2015}
2016
2017template <PrimType Name, class T = typename PrimConv<Name>::T>
2019 const T &Value = S.Stk.pop<T>();
2020 const Pointer &Ptr = S.Stk.pop<Pointer>();
2021
2022 if (Ptr.canBeInitialized()) {
2023 Ptr.initialize();
2024 Ptr.activate();
2025 }
2026 if (!CheckStore(S, OpPC, Ptr))
2027 return false;
2028 Ptr.deref<T>() = Value;
2029 return true;
2030}
2031
2032template <PrimType Name, class T = typename PrimConv<Name>::T>
2034 const T &Value = S.Stk.pop<T>();
2035 const Pointer &Ptr = S.Stk.peek<Pointer>();
2036 if (!CheckStore(S, OpPC, Ptr))
2037 return false;
2038 if (Ptr.canBeInitialized())
2039 Ptr.initialize();
2040 if (const auto *FD = Ptr.getField())
2041 Ptr.deref<T>() = Value.truncate(FD->getBitWidthValue());
2042 else
2043 Ptr.deref<T>() = Value;
2044 return true;
2045}
2046
2047template <PrimType Name, class T = typename PrimConv<Name>::T>
2049 const T &Value = S.Stk.pop<T>();
2050 const Pointer &Ptr = S.Stk.pop<Pointer>();
2051 if (!CheckStore(S, OpPC, Ptr))
2052 return false;
2053 if (Ptr.canBeInitialized())
2054 Ptr.initialize();
2055 if (const auto *FD = Ptr.getField())
2056 Ptr.deref<T>() = Value.truncate(FD->getBitWidthValue());
2057 else
2058 Ptr.deref<T>() = Value;
2059 return true;
2060}
2061
2062template <PrimType Name, class T = typename PrimConv<Name>::T>
2064 const T &Value = S.Stk.pop<T>();
2065 const Pointer &Ptr = S.Stk.peek<Pointer>();
2066 if (Ptr.canBeInitialized()) {
2067 Ptr.initialize();
2068 Ptr.activate();
2069 }
2070 if (!CheckStore(S, OpPC, Ptr))
2071 return false;
2072 if (const auto *FD = Ptr.getField())
2073 Ptr.deref<T>() = Value.truncate(FD->getBitWidthValue());
2074 else
2075 Ptr.deref<T>() = Value;
2076 return true;
2077}
2078
2079template <PrimType Name, class T = typename PrimConv<Name>::T>
2081 const T &Value = S.Stk.pop<T>();
2082 const Pointer &Ptr = S.Stk.pop<Pointer>();
2083
2084 if (Ptr.canBeInitialized()) {
2085 Ptr.initialize();
2086 Ptr.activate();
2087 }
2088 if (!CheckStore(S, OpPC, Ptr))
2089 return false;
2090 if (const auto *FD = Ptr.getField())
2091 Ptr.deref<T>() = Value.truncate(FD->getBitWidthValue());
2092 else
2093 Ptr.deref<T>() = Value;
2094 return true;
2095}
2096
2097template <PrimType Name, class T = typename PrimConv<Name>::T>
2098bool Init(InterpState &S, CodePtr OpPC) {
2099 const T &Value = S.Stk.pop<T>();
2100 const Pointer &Ptr = S.Stk.peek<Pointer>();
2101 if (!CheckInit(S, OpPC, Ptr))
2102 return false;
2103 Ptr.initialize();
2104 new (&Ptr.deref<T>()) T(Value);
2105 return true;
2106}
2107
2108template <PrimType Name, class T = typename PrimConv<Name>::T>
2110 const T &Value = S.Stk.pop<T>();
2111 const Pointer &Ptr = S.Stk.pop<Pointer>();
2112 if (!CheckInit(S, OpPC, Ptr))
2113 return false;
2114 Ptr.initialize();
2115 new (&Ptr.deref<T>()) T(Value);
2116 return true;
2117}
2118
2119/// 1) Pops the value from the stack
2120/// 2) Peeks a pointer and gets its index \Idx
2121/// 3) Sets the value on the pointer, leaving the pointer on the stack.
2122template <PrimType Name, class T = typename PrimConv<Name>::T>
2123bool InitElem(InterpState &S, CodePtr OpPC, uint32_t Idx) {
2124 const T &Value = S.Stk.pop<T>();
2125 const Pointer &Ptr = S.Stk.peek<Pointer>();
2126
2127 if (Ptr.isUnknownSizeArray())
2128 return false;
2129
2130 const Descriptor *Desc = Ptr.getFieldDesc();
2131 // In the unlikely event that we're initializing the first item of
2132 // a non-array, skip the atIndex().
2133 if (Idx == 0 && !Desc->isArray()) {
2134 Ptr.initialize();
2135 new (&Ptr.deref<T>()) T(Value);
2136 return true;
2137 }
2138
2139 if (!CheckLive(S, OpPC, Ptr, AK_Assign))
2140 return false;
2141 if (Idx >= Desc->getNumElems()) {
2142 // CheckRange.
2143 if (S.getLangOpts().CPlusPlus) {
2144 const SourceInfo &Loc = S.Current->getSource(OpPC);
2145 S.FFDiag(Loc, diag::note_constexpr_access_past_end)
2146 << AK_Assign << S.Current->getRange(OpPC);
2147 }
2148 return false;
2149 }
2150 Ptr.initializeElement(Idx);
2151 new (&Ptr.elem<T>(Idx)) T(Value);
2152 return true;
2153}
2154
2155/// The same as InitElem, but pops the pointer as well.
2156template <PrimType Name, class T = typename PrimConv<Name>::T>
2157bool InitElemPop(InterpState &S, CodePtr OpPC, uint32_t Idx) {
2158 const T &Value = S.Stk.pop<T>();
2159 const Pointer &Ptr = S.Stk.pop<Pointer>();
2160
2161 if (Ptr.isUnknownSizeArray())
2162 return false;
2163
2164 const Descriptor *Desc = Ptr.getFieldDesc();
2165 // In the unlikely event that we're initializing the first item of
2166 // a non-array, skip the atIndex().
2167 if (Idx == 0 && !Desc->isArray()) {
2168 Ptr.initialize();
2169 new (&Ptr.deref<T>()) T(Value);
2170 return true;
2171 }
2172
2173 if (!CheckLive(S, OpPC, Ptr, AK_Assign))
2174 return false;
2175 if (Idx >= Desc->getNumElems()) {
2176 // CheckRange.
2177 if (S.getLangOpts().CPlusPlus) {
2178 const SourceInfo &Loc = S.Current->getSource(OpPC);
2179 S.FFDiag(Loc, diag::note_constexpr_access_past_end)
2180 << AK_Assign << S.Current->getRange(OpPC);
2181 }
2182 return false;
2183 }
2184 Ptr.initializeElement(Idx);
2185 new (&Ptr.elem<T>(Idx)) T(Value);
2186 return true;
2187}
2188
2189inline bool Memcpy(InterpState &S, CodePtr OpPC) {
2190 const Pointer &Src = S.Stk.pop<Pointer>();
2191 Pointer &Dest = S.Stk.peek<Pointer>();
2192
2193 if (!CheckLoad(S, OpPC, Src))
2194 return false;
2195
2196 return DoMemcpy(S, OpPC, Src, Dest);
2197}
2198
2199inline bool ToMemberPtr(InterpState &S, CodePtr OpPC) {
2200 const auto &Member = S.Stk.pop<MemberPointer>();
2201 const auto &Base = S.Stk.pop<Pointer>();
2202
2203 S.Stk.push<MemberPointer>(Member.takeInstance(Base));
2204 return true;
2205}
2206
2207inline bool CastMemberPtrPtr(InterpState &S, CodePtr OpPC) {
2208 const auto &MP = S.Stk.pop<MemberPointer>();
2209
2210 if (std::optional<Pointer> Ptr = MP.toPointer(S.Ctx)) {
2211 S.Stk.push<Pointer>(*Ptr);
2212 return true;
2213 }
2214 return Invalid(S, OpPC);
2215}
2216
2217//===----------------------------------------------------------------------===//
2218// AddOffset, SubOffset
2219//===----------------------------------------------------------------------===//
2220
2221template <class T, ArithOp Op>
2222std::optional<Pointer> OffsetHelper(InterpState &S, CodePtr OpPC,
2223 const T &Offset, const Pointer &Ptr,
2224 bool IsPointerArith = false) {
2225 // A zero offset does not change the pointer.
2226 if (Offset.isZero())
2227 return Ptr;
2228
2229 if (IsPointerArith && !CheckNull(S, OpPC, Ptr, CSK_ArrayIndex)) {
2230 // The CheckNull will have emitted a note already, but we only
2231 // abort in C++, since this is fine in C.
2232 if (S.getLangOpts().CPlusPlus)
2233 return std::nullopt;
2234 }
2235
2236 // Arrays of unknown bounds cannot have pointers into them.
2237 if (!CheckArray(S, OpPC, Ptr))
2238 return std::nullopt;
2239
2240 // This is much simpler for integral pointers, so handle them first.
2241 if (Ptr.isIntegralPointer()) {
2242 uint64_t V = Ptr.getIntegerRepresentation();
2243 uint64_t O = static_cast<uint64_t>(Offset) * Ptr.elemSize();
2244 if constexpr (Op == ArithOp::Add)
2245 return Pointer(V + O, Ptr.asIntPointer().Desc);
2246 else
2247 return Pointer(V - O, Ptr.asIntPointer().Desc);
2248 } else if (Ptr.isFunctionPointer()) {
2249 uint64_t O = static_cast<uint64_t>(Offset);
2250 uint64_t N;
2251 if constexpr (Op == ArithOp::Add)
2252 N = Ptr.getByteOffset() + O;
2253 else
2254 N = Ptr.getByteOffset() - O;
2255
2256 if (N > 1)
2257 S.CCEDiag(S.Current->getSource(OpPC), diag::note_constexpr_array_index)
2258 << N << /*non-array*/ true << 0;
2259 return Pointer(Ptr.asFunctionPointer().getFunction(), N);
2260 }
2261
2262 assert(Ptr.isBlockPointer());
2263
2264 uint64_t MaxIndex = static_cast<uint64_t>(Ptr.getNumElems());
2265 uint64_t Index;
2266 if (Ptr.isOnePastEnd())
2267 Index = MaxIndex;
2268 else
2269 Index = Ptr.getIndex();
2270
2271 bool Invalid = false;
2272 // Helper to report an invalid offset, computed as APSInt.
2273 auto DiagInvalidOffset = [&]() -> void {
2274 const unsigned Bits = Offset.bitWidth();
2275 APSInt APOffset(Offset.toAPSInt().extend(Bits + 2), /*IsUnsigend=*/false);
2276 APSInt APIndex(APInt(Bits + 2, Index, /*IsSigned=*/true),
2277 /*IsUnsigned=*/false);
2278 APSInt NewIndex =
2279 (Op == ArithOp::Add) ? (APIndex + APOffset) : (APIndex - APOffset);
2280 S.CCEDiag(S.Current->getSource(OpPC), diag::note_constexpr_array_index)
2281 << NewIndex << /*array*/ static_cast<int>(!Ptr.inArray()) << MaxIndex;
2282 Invalid = true;
2283 };
2284
2285 if (Ptr.isBlockPointer()) {
2286 uint64_t IOffset = static_cast<uint64_t>(Offset);
2287 uint64_t MaxOffset = MaxIndex - Index;
2288
2289 if constexpr (Op == ArithOp::Add) {
2290 // If the new offset would be negative, bail out.
2291 if (Offset.isNegative() && (Offset.isMin() || -IOffset > Index))
2292 DiagInvalidOffset();
2293
2294 // If the new offset would be out of bounds, bail out.
2295 if (Offset.isPositive() && IOffset > MaxOffset)
2296 DiagInvalidOffset();
2297 } else {
2298 // If the new offset would be negative, bail out.
2299 if (Offset.isPositive() && Index < IOffset)
2300 DiagInvalidOffset();
2301
2302 // If the new offset would be out of bounds, bail out.
2303 if (Offset.isNegative() && (Offset.isMin() || -IOffset > MaxOffset))
2304 DiagInvalidOffset();
2305 }
2306 }
2307
2308 if (Invalid && S.getLangOpts().CPlusPlus)
2309 return std::nullopt;
2310
2311 // Offset is valid - compute it on unsigned.
2312 int64_t WideIndex = static_cast<int64_t>(Index);
2313 int64_t WideOffset = static_cast<int64_t>(Offset);
2314 int64_t Result;
2315 if constexpr (Op == ArithOp::Add)
2316 Result = WideIndex + WideOffset;
2317 else
2318 Result = WideIndex - WideOffset;
2319
2320 // When the pointer is one-past-end, going back to index 0 is the only
2321 // useful thing we can do. Any other index has been diagnosed before and
2322 // we don't get here.
2323 if (Result == 0 && Ptr.isOnePastEnd()) {
2324 if (Ptr.getFieldDesc()->isArray())
2325 return Ptr.atIndex(0);
2326 return Pointer(Ptr.asBlockPointer().Pointee, Ptr.asBlockPointer().Base);
2327 }
2328
2329 return Ptr.atIndex(static_cast<uint64_t>(Result));
2330}
2331
2332template <PrimType Name, class T = typename PrimConv<Name>::T>
2334 const T &Offset = S.Stk.pop<T>();
2335 Pointer Ptr = S.Stk.pop<Pointer>();
2336 if (Ptr.isBlockPointer())
2337 Ptr = Ptr.expand();
2338
2339 if (std::optional<Pointer> Result = OffsetHelper<T, ArithOp::Add>(
2340 S, OpPC, Offset, Ptr, /*IsPointerArith=*/true)) {
2341 S.Stk.push<Pointer>(*Result);
2342 return true;
2343 }
2344 return false;
2345}
2346
2347template <PrimType Name, class T = typename PrimConv<Name>::T>
2349 const T &Offset = S.Stk.pop<T>();
2350 const Pointer &Ptr = S.Stk.pop<Pointer>();
2351
2352 if (std::optional<Pointer> Result = OffsetHelper<T, ArithOp::Sub>(
2353 S, OpPC, Offset, Ptr, /*IsPointerArith=*/true)) {
2354 S.Stk.push<Pointer>(*Result);
2355 return true;
2356 }
2357 return false;
2358}
2359
2360template <ArithOp Op>
2361static inline bool IncDecPtrHelper(InterpState &S, CodePtr OpPC,
2362 const Pointer &Ptr) {
2363 if (Ptr.isDummy())
2364 return false;
2365
2366 using OneT = Integral<8, false>;
2367
2368 const Pointer &P = Ptr.deref<Pointer>();
2369 if (!CheckNull(S, OpPC, P, CSK_ArrayIndex))
2370 return false;
2371
2372 // Get the current value on the stack.
2373 S.Stk.push<Pointer>(P);
2374
2375 // Now the current Ptr again and a constant 1.
2376 OneT One = OneT::from(1);
2377 if (std::optional<Pointer> Result =
2378 OffsetHelper<OneT, Op>(S, OpPC, One, P, /*IsPointerArith=*/true)) {
2379 // Store the new value.
2380 Ptr.deref<Pointer>() = *Result;
2381 return true;
2382 }
2383 return false;
2384}
2385
2386static inline bool IncPtr(InterpState &S, CodePtr OpPC) {
2387 const Pointer &Ptr = S.Stk.pop<Pointer>();
2388
2389 if (!Ptr.isInitialized())
2390 return DiagnoseUninitialized(S, OpPC, Ptr, AK_Increment);
2391
2392 return IncDecPtrHelper<ArithOp::Add>(S, OpPC, Ptr);
2393}
2394
2395static inline bool DecPtr(InterpState &S, CodePtr OpPC) {
2396 const Pointer &Ptr = S.Stk.pop<Pointer>();
2397
2398 if (!Ptr.isInitialized())
2399 return DiagnoseUninitialized(S, OpPC, Ptr, AK_Decrement);
2400
2401 return IncDecPtrHelper<ArithOp::Sub>(S, OpPC, Ptr);
2402}
2403
2404/// 1) Pops a Pointer from the stack.
2405/// 2) Pops another Pointer from the stack.
2406/// 3) Pushes the difference of the indices of the two pointers on the stack.
2407template <PrimType Name, class T = typename PrimConv<Name>::T>
2408inline bool SubPtr(InterpState &S, CodePtr OpPC) {
2409 const Pointer &LHS = S.Stk.pop<Pointer>();
2410 const Pointer &RHS = S.Stk.pop<Pointer>();
2411
2412 if (!Pointer::hasSameBase(LHS, RHS) && S.getLangOpts().CPlusPlus) {
2413 S.FFDiag(S.Current->getSource(OpPC),
2414 diag::note_constexpr_pointer_arith_unspecified)
2416 << RHS.toDiagnosticString(S.getASTContext());
2417 return false;
2418 }
2419
2420 if (LHS == RHS) {
2421 S.Stk.push<T>();
2422 return true;
2423 }
2424
2425 for (const Pointer &P : {LHS, RHS}) {
2426 if (P.isZeroSizeArray()) {
2427 QualType PtrT = P.getType();
2428 while (auto *AT = dyn_cast<ArrayType>(PtrT))
2429 PtrT = AT->getElementType();
2430
2432 PtrT, APInt::getZero(1), nullptr, ArraySizeModifier::Normal, 0);
2433 S.FFDiag(S.Current->getSource(OpPC),
2434 diag::note_constexpr_pointer_subtraction_zero_size)
2435 << ArrayTy;
2436
2437 return false;
2438 }
2439 }
2440
2441 int64_t A64 =
2442 LHS.isBlockPointer()
2443 ? (LHS.isElementPastEnd() ? LHS.getNumElems() : LHS.getIndex())
2445
2446 int64_t B64 =
2447 RHS.isBlockPointer()
2448 ? (RHS.isElementPastEnd() ? RHS.getNumElems() : RHS.getIndex())
2449 : RHS.getIntegerRepresentation();
2450
2451 int64_t R64 = A64 - B64;
2452 if (static_cast<int64_t>(T::from(R64)) != R64)
2453 return handleOverflow(S, OpPC, R64);
2454
2455 S.Stk.push<T>(T::from(R64));
2456 return true;
2457}
2458
2459//===----------------------------------------------------------------------===//
2460// Destroy
2461//===----------------------------------------------------------------------===//
2462
2463inline bool Destroy(InterpState &S, CodePtr OpPC, uint32_t I) {
2464 assert(S.Current->getFunction());
2465
2466 // FIXME: We iterate the scope once here and then again in the destroy() call
2467 // below.
2468 for (auto &Local : S.Current->getFunction()->getScope(I).locals_reverse()) {
2469 const Pointer &Ptr = S.Current->getLocalPointer(Local.Offset);
2470
2471 if (Ptr.getLifetime() == Lifetime::Ended) {
2472 // Try to use the declaration for better diagnostics
2473 if (const Decl *D = Ptr.getDeclDesc()->asDecl()) {
2474 auto *ND = cast<NamedDecl>(D);
2475 S.FFDiag(ND->getLocation(),
2476 diag::note_constexpr_destroy_out_of_lifetime)
2477 << ND->getNameAsString();
2478 } else {
2479 S.FFDiag(Ptr.getDeclDesc()->getLocation(),
2480 diag::note_constexpr_destroy_out_of_lifetime)
2482 }
2483 return false;
2484 }
2485 }
2486
2487 S.Current->destroy(I);
2488 return true;
2489}
2490
2491inline bool InitScope(InterpState &S, CodePtr OpPC, uint32_t I) {
2492 S.Current->initScope(I);
2493 return true;
2494}
2495
2496//===----------------------------------------------------------------------===//
2497// Cast, CastFP
2498//===----------------------------------------------------------------------===//
2499
2500template <PrimType TIn, PrimType TOut> bool Cast(InterpState &S, CodePtr OpPC) {
2501 using T = typename PrimConv<TIn>::T;
2502 using U = typename PrimConv<TOut>::T;
2503 S.Stk.push<U>(U::from(S.Stk.pop<T>()));
2504 return true;
2505}
2506
2507/// 1) Pops a Floating from the stack.
2508/// 2) Pushes a new floating on the stack that uses the given semantics.
2509inline bool CastFP(InterpState &S, CodePtr OpPC, const llvm::fltSemantics *Sem,
2510 llvm::RoundingMode RM) {
2511 Floating F = S.Stk.pop<Floating>();
2512 Floating Result = S.allocFloat(*Sem);
2513 F.toSemantics(Sem, RM, &Result);
2514 S.Stk.push<Floating>(Result);
2515 return true;
2516}
2517
2518inline bool CastFixedPoint(InterpState &S, CodePtr OpPC, uint32_t FPS) {
2519 FixedPointSemantics TargetSemantics =
2520 FixedPointSemantics::getFromOpaqueInt(FPS);
2521 const auto &Source = S.Stk.pop<FixedPoint>();
2522
2523 bool Overflow;
2524 FixedPoint Result = Source.toSemantics(TargetSemantics, &Overflow);
2525
2526 if (Overflow && !handleFixedPointOverflow(S, OpPC, Result))
2527 return false;
2528
2530 return true;
2531}
2532
2533/// Like Cast(), but we cast to an arbitrary-bitwidth integral, so we need
2534/// to know what bitwidth the result should be.
2535template <PrimType Name, class T = typename PrimConv<Name>::T>
2536bool CastAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
2537 auto Result = S.allocAP<IntegralAP<false>>(BitWidth);
2538 // Copy data.
2539 {
2540 APInt Source = S.Stk.pop<T>().toAPSInt().extOrTrunc(BitWidth);
2541 Result.copy(Source);
2542 }
2544 return true;
2545}
2546
2547template <PrimType Name, class T = typename PrimConv<Name>::T>
2548bool CastAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
2549 auto Result = S.allocAP<IntegralAP<true>>(BitWidth);
2550 // Copy data.
2551 {
2552 APInt Source = S.Stk.pop<T>().toAPSInt().extOrTrunc(BitWidth);
2553 Result.copy(Source);
2554 }
2556 return true;
2557}
2558
2559template <PrimType Name, class T = typename PrimConv<Name>::T>
2561 const llvm::fltSemantics *Sem, uint32_t FPOI) {
2562 const T &From = S.Stk.pop<T>();
2563 APSInt FromAP = From.toAPSInt();
2564
2566 Floating Result = S.allocFloat(*Sem);
2567 auto Status =
2568 Floating::fromIntegral(FromAP, *Sem, getRoundingMode(FPO), &Result);
2569 S.Stk.push<Floating>(Result);
2570
2571 return CheckFloatResult(S, OpPC, Result, Status, FPO);
2572}
2573
2574template <PrimType Name, class T = typename PrimConv<Name>::T>
2575bool CastFloatingIntegral(InterpState &S, CodePtr OpPC, uint32_t FPOI) {
2576 const Floating &F = S.Stk.pop<Floating>();
2577
2578 if constexpr (std::is_same_v<T, Boolean>) {
2579 S.Stk.push<T>(T(F.isNonZero()));
2580 return true;
2581 } else {
2582 APSInt Result(std::max(8u, T::bitWidth()),
2583 /*IsUnsigned=*/!T::isSigned());
2584 auto Status = F.convertToInteger(Result);
2585
2586 // Float-to-Integral overflow check.
2587 if ((Status & APFloat::opStatus::opInvalidOp)) {
2588 const Expr *E = S.Current->getExpr(OpPC);
2589 QualType Type = E->getType();
2590
2591 S.CCEDiag(E, diag::note_constexpr_overflow) << F.getAPFloat() << Type;
2592 if (S.noteUndefinedBehavior()) {
2593 S.Stk.push<T>(T(Result));
2594 return true;
2595 }
2596 return false;
2597 }
2598
2600 S.Stk.push<T>(T(Result));
2601 return CheckFloatResult(S, OpPC, F, Status, FPO);
2602 }
2603}
2604
2605static inline bool CastFloatingIntegralAP(InterpState &S, CodePtr OpPC,
2606 uint32_t BitWidth, uint32_t FPOI) {
2607 const Floating &F = S.Stk.pop<Floating>();
2608
2609 APSInt Result(BitWidth, /*IsUnsigned=*/true);
2610 auto Status = F.convertToInteger(Result);
2611
2612 // Float-to-Integral overflow check.
2613 if ((Status & APFloat::opStatus::opInvalidOp) && F.isFinite())
2614 return handleOverflow(S, OpPC, F.getAPFloat());
2615
2617
2618 auto ResultAP = S.allocAP<IntegralAP<false>>(BitWidth);
2619 ResultAP.copy(Result);
2620
2621 S.Stk.push<IntegralAP<false>>(ResultAP);
2622
2623 return CheckFloatResult(S, OpPC, F, Status, FPO);
2624}
2625
2626static inline bool CastFloatingIntegralAPS(InterpState &S, CodePtr OpPC,
2627 uint32_t BitWidth, uint32_t FPOI) {
2628 const Floating &F = S.Stk.pop<Floating>();
2629
2630 APSInt Result(BitWidth, /*IsUnsigned=*/false);
2631 auto Status = F.convertToInteger(Result);
2632
2633 // Float-to-Integral overflow check.
2634 if ((Status & APFloat::opStatus::opInvalidOp) && F.isFinite())
2635 return handleOverflow(S, OpPC, F.getAPFloat());
2636
2638
2639 auto ResultAP = S.allocAP<IntegralAP<true>>(BitWidth);
2640 ResultAP.copy(Result);
2641
2642 S.Stk.push<IntegralAP<true>>(ResultAP);
2643
2644 return CheckFloatResult(S, OpPC, F, Status, FPO);
2645}
2646
2647bool CheckPointerToIntegralCast(InterpState &S, CodePtr OpPC,
2648 const Pointer &Ptr, unsigned BitWidth);
2649bool CastPointerIntegralAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth);
2650bool CastPointerIntegralAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth);
2651
2652template <PrimType Name, class T = typename PrimConv<Name>::T>
2654 const Pointer &Ptr = S.Stk.pop<Pointer>();
2655
2656 S.CCEDiag(S.Current->getSource(OpPC), diag::note_constexpr_invalid_cast)
2657 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
2658 << S.getLangOpts().CPlusPlus << S.Current->getRange(OpPC);
2659
2660 if (!CheckPointerToIntegralCast(S, OpPC, Ptr, T::bitWidth()))
2661 return Invalid(S, OpPC);
2662
2663 S.Stk.push<T>(T::from(Ptr.getIntegerRepresentation()));
2664 return true;
2665}
2666
2667template <PrimType Name, class T = typename PrimConv<Name>::T>
2668static inline bool CastIntegralFixedPoint(InterpState &S, CodePtr OpPC,
2669 uint32_t FPS) {
2670 const T &Int = S.Stk.pop<T>();
2671
2672 FixedPointSemantics Sem = FixedPointSemantics::getFromOpaqueInt(FPS);
2673
2674 bool Overflow;
2675 FixedPoint Result = FixedPoint::from(Int.toAPSInt(), Sem, &Overflow);
2676
2677 if (Overflow && !handleFixedPointOverflow(S, OpPC, Result))
2678 return false;
2679
2681 return true;
2682}
2683
2684static inline bool CastFloatingFixedPoint(InterpState &S, CodePtr OpPC,
2685 uint32_t FPS) {
2686 const auto &Float = S.Stk.pop<Floating>();
2687
2688 FixedPointSemantics Sem = FixedPointSemantics::getFromOpaqueInt(FPS);
2689
2690 bool Overflow;
2691 FixedPoint Result = FixedPoint::from(Float.getAPFloat(), Sem, &Overflow);
2692
2693 if (Overflow && !handleFixedPointOverflow(S, OpPC, Result))
2694 return false;
2695
2697 return true;
2698}
2699
2700static inline bool CastFixedPointFloating(InterpState &S, CodePtr OpPC,
2701 const llvm::fltSemantics *Sem) {
2702 const auto &Fixed = S.Stk.pop<FixedPoint>();
2703 Floating Result = S.allocFloat(*Sem);
2704 Result.copy(Fixed.toFloat(Sem));
2705 S.Stk.push<Floating>(Result);
2706 return true;
2707}
2708
2709template <PrimType Name, class T = typename PrimConv<Name>::T>
2710static inline bool CastFixedPointIntegral(InterpState &S, CodePtr OpPC) {
2711 const auto &Fixed = S.Stk.pop<FixedPoint>();
2712
2713 bool Overflow;
2714 APSInt Int = Fixed.toInt(T::bitWidth(), T::isSigned(), &Overflow);
2715
2716 if (Overflow && !handleOverflow(S, OpPC, Int))
2717 return false;
2718
2719 S.Stk.push<T>(Int);
2720 return true;
2721}
2722
2723static inline bool FnPtrCast(InterpState &S, CodePtr OpPC) {
2724 const SourceInfo &E = S.Current->getSource(OpPC);
2725 S.CCEDiag(E, diag::note_constexpr_invalid_cast)
2726 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
2727 << S.getLangOpts().CPlusPlus << S.Current->getRange(OpPC);
2728 return true;
2729}
2730
2731static inline bool PtrPtrCast(InterpState &S, CodePtr OpPC, bool SrcIsVoidPtr) {
2732 const auto &Ptr = S.Stk.peek<Pointer>();
2733
2734 if (SrcIsVoidPtr && S.getLangOpts().CPlusPlus) {
2735 bool HasValidResult = !Ptr.isZero();
2736
2737 if (HasValidResult) {
2738 if (S.getStdAllocatorCaller("allocate"))
2739 return true;
2740
2741 const auto &E = cast<CastExpr>(S.Current->getExpr(OpPC));
2742 if (S.getLangOpts().CPlusPlus26 &&
2743 S.getASTContext().hasSimilarType(Ptr.getType(),
2744 E->getType()->getPointeeType()))
2745 return true;
2746
2747 S.CCEDiag(E, diag::note_constexpr_invalid_void_star_cast)
2748 << E->getSubExpr()->getType() << S.getLangOpts().CPlusPlus26
2749 << Ptr.getType().getCanonicalType() << E->getType()->getPointeeType();
2750 } else if (!S.getLangOpts().CPlusPlus26) {
2751 const SourceInfo &E = S.Current->getSource(OpPC);
2752 S.CCEDiag(E, diag::note_constexpr_invalid_cast)
2753 << diag::ConstexprInvalidCastKind::CastFrom << "'void *'"
2754 << S.Current->getRange(OpPC);
2755 }
2756 } else {
2757 const SourceInfo &E = S.Current->getSource(OpPC);
2758 S.CCEDiag(E, diag::note_constexpr_invalid_cast)
2759 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
2760 << S.getLangOpts().CPlusPlus << S.Current->getRange(OpPC);
2761 }
2762
2763 return true;
2764}
2765
2766//===----------------------------------------------------------------------===//
2767// Zero, Nullptr
2768//===----------------------------------------------------------------------===//
2769
2770template <PrimType Name, class T = typename PrimConv<Name>::T>
2771bool Zero(InterpState &S, CodePtr OpPC) {
2772 S.Stk.push<T>(T::zero());
2773 return true;
2774}
2775
2776static inline bool ZeroIntAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
2777 auto Result = S.allocAP<IntegralAP<false>>(BitWidth);
2778 if (!Result.singleWord())
2779 std::memset(Result.Memory, 0, Result.numWords() * sizeof(uint64_t));
2781 return true;
2782}
2783
2784static inline bool ZeroIntAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
2785 auto Result = S.allocAP<IntegralAP<true>>(BitWidth);
2786 if (!Result.singleWord())
2787 std::memset(Result.Memory, 0, Result.numWords() * sizeof(uint64_t));
2789 return true;
2790}
2791
2792template <PrimType Name, class T = typename PrimConv<Name>::T>
2793inline bool Null(InterpState &S, CodePtr OpPC, uint64_t Value,
2794 const Descriptor *Desc) {
2795 // FIXME(perf): This is a somewhat often-used function and the value of a
2796 // null pointer is almost always 0.
2797 S.Stk.push<T>(Value, Desc);
2798 return true;
2799}
2800
2801template <PrimType Name, class T = typename PrimConv<Name>::T>
2802inline bool IsNonNull(InterpState &S, CodePtr OpPC) {
2803 const auto &P = S.Stk.pop<T>();
2804 if (P.isWeak())
2805 return false;
2806 S.Stk.push<Boolean>(Boolean::from(!P.isZero()));
2807 return true;
2808}
2809
2810//===----------------------------------------------------------------------===//
2811// This, ImplicitThis
2812//===----------------------------------------------------------------------===//
2813
2814inline bool This(InterpState &S, CodePtr OpPC) {
2815 // Cannot read 'this' in this mode.
2817 return false;
2818 }
2819
2820 const Pointer &This = S.Current->getThis();
2821 if (!CheckThis(S, OpPC, This))
2822 return false;
2823
2824 // Ensure the This pointer has been cast to the correct base.
2825 if (!This.isDummy()) {
2827 if (!This.isTypeidPointer()) {
2828 [[maybe_unused]] const Record *R = This.getRecord();
2829 if (!R)
2830 R = This.narrow().getRecord();
2831 assert(R);
2832 assert(R->getDecl() ==
2834 ->getParent());
2835 }
2836 }
2837
2838 S.Stk.push<Pointer>(This);
2839 return true;
2840}
2841
2842inline bool RVOPtr(InterpState &S, CodePtr OpPC) {
2843 assert(S.Current->getFunction()->hasRVO());
2845 return false;
2846 S.Stk.push<Pointer>(S.Current->getRVOPtr());
2847 return true;
2848}
2849
2850//===----------------------------------------------------------------------===//
2851// Shr, Shl
2852//===----------------------------------------------------------------------===//
2853
2854template <class LT, class RT, ShiftDir Dir>
2855inline bool DoShift(InterpState &S, CodePtr OpPC, LT &LHS, RT &RHS,
2856 LT *Result) {
2857 static_assert(!needsAlloc<LT>());
2858 const unsigned Bits = LHS.bitWidth();
2859
2860 // OpenCL 6.3j: shift values are effectively % word size of LHS.
2861 if (S.getLangOpts().OpenCL)
2862 RT::bitAnd(RHS, RT::from(LHS.bitWidth() - 1, RHS.bitWidth()),
2863 RHS.bitWidth(), &RHS);
2864
2865 if (RHS.isNegative()) {
2866 // During constant-folding, a negative shift is an opposite shift. Such a
2867 // shift is not a constant expression.
2868 const SourceInfo &Loc = S.Current->getSource(OpPC);
2869 S.CCEDiag(Loc, diag::note_constexpr_negative_shift) << RHS.toAPSInt();
2870 if (!S.noteUndefinedBehavior())
2871 return false;
2872 RHS = -RHS;
2873 return DoShift<LT, RT,
2875 S, OpPC, LHS, RHS, Result);
2876 }
2877
2878 if (!CheckShift<Dir>(S, OpPC, LHS, RHS, Bits))
2879 return false;
2880
2881 // Limit the shift amount to Bits - 1. If this happened,
2882 // it has already been diagnosed by CheckShift() above,
2883 // but we still need to handle it.
2884 // Note that we have to be extra careful here since we're doing the shift in
2885 // any case, but we need to adjust the shift amount or the way we do the shift
2886 // for the potential error cases.
2887 typename LT::AsUnsigned R;
2888 unsigned MaxShiftAmount = LHS.bitWidth() - 1;
2889 if constexpr (Dir == ShiftDir::Left) {
2890 if (Compare(RHS, RT::from(MaxShiftAmount, RHS.bitWidth())) ==
2892 if (LHS.isNegative())
2893 R = LT::AsUnsigned::zero(LHS.bitWidth());
2894 else {
2895 RHS = RT::from(LHS.countLeadingZeros(), RHS.bitWidth());
2896 LT::AsUnsigned::shiftLeft(LT::AsUnsigned::from(LHS),
2897 LT::AsUnsigned::from(RHS, Bits), Bits, &R);
2898 }
2899 } else if (LHS.isNegative()) {
2900 if (LHS.isMin()) {
2901 R = LT::AsUnsigned::zero(LHS.bitWidth());
2902 } else {
2903 // If the LHS is negative, perform the cast and invert the result.
2904 typename LT::AsUnsigned LHSU = LT::AsUnsigned::from(-LHS);
2905 LT::AsUnsigned::shiftLeft(LHSU, LT::AsUnsigned::from(RHS, Bits), Bits,
2906 &R);
2907 R = -R;
2908 }
2909 } else {
2910 // The good case, a simple left shift.
2911 LT::AsUnsigned::shiftLeft(LT::AsUnsigned::from(LHS),
2912 LT::AsUnsigned::from(RHS, Bits), Bits, &R);
2913 }
2914 S.Stk.push<LT>(LT::from(R));
2915 return true;
2916 }
2917
2918 // Right shift.
2919 if (Compare(RHS, RT::from(MaxShiftAmount, RHS.bitWidth())) ==
2921 R = LT::AsUnsigned::from(-1);
2922 } else {
2923 // Do the shift on potentially signed LT, then convert to unsigned type.
2924 LT A;
2925 LT::shiftRight(LHS, LT::from(RHS, Bits), Bits, &A);
2926 R = LT::AsUnsigned::from(A);
2927 }
2928
2929 S.Stk.push<LT>(LT::from(R));
2930 return true;
2931}
2932
2933/// A version of DoShift that works on IntegralAP.
2934template <class LT, class RT, ShiftDir Dir>
2935inline bool DoShiftAP(InterpState &S, CodePtr OpPC, const APSInt &LHS,
2936 APSInt RHS, LT *Result) {
2937 const unsigned Bits = LHS.getBitWidth();
2938
2939 // OpenCL 6.3j: shift values are effectively % word size of LHS.
2940 if (S.getLangOpts().OpenCL)
2941 RHS &=
2942 APSInt(llvm::APInt(RHS.getBitWidth(), static_cast<uint64_t>(Bits - 1)),
2943 RHS.isUnsigned());
2944
2945 if (RHS.isNegative()) {
2946 // During constant-folding, a negative shift is an opposite shift. Such a
2947 // shift is not a constant expression.
2948 const SourceInfo &Loc = S.Current->getSource(OpPC);
2949 S.CCEDiag(Loc, diag::note_constexpr_negative_shift) << RHS; //.toAPSInt();
2950 if (!S.noteUndefinedBehavior())
2951 return false;
2952 return DoShiftAP<LT, RT,
2954 S, OpPC, LHS, -RHS, Result);
2955 }
2956
2957 if (!CheckShift<Dir>(S, OpPC, static_cast<LT>(LHS), static_cast<RT>(RHS),
2958 Bits))
2959 return false;
2960
2961 unsigned SA = (unsigned)RHS.getLimitedValue(Bits - 1);
2962 if constexpr (Dir == ShiftDir::Left) {
2963 if constexpr (needsAlloc<LT>())
2964 Result->copy(LHS << SA);
2965 else
2966 *Result = LT(LHS << SA);
2967 } else {
2968 if constexpr (needsAlloc<LT>())
2969 Result->copy(LHS >> SA);
2970 else
2971 *Result = LT(LHS >> SA);
2972 }
2973
2974 S.Stk.push<LT>(*Result);
2975 return true;
2976}
2977
2978template <PrimType NameL, PrimType NameR>
2979inline bool Shr(InterpState &S, CodePtr OpPC) {
2980 using LT = typename PrimConv<NameL>::T;
2981 using RT = typename PrimConv<NameR>::T;
2982 auto RHS = S.Stk.pop<RT>();
2983 auto LHS = S.Stk.pop<LT>();
2984
2985 if constexpr (needsAlloc<LT>() || needsAlloc<RT>()) {
2986 LT Result;
2987 if constexpr (needsAlloc<LT>())
2988 Result = S.allocAP<LT>(LHS.bitWidth());
2989 return DoShiftAP<LT, RT, ShiftDir::Right>(S, OpPC, LHS.toAPSInt(),
2990 RHS.toAPSInt(), &Result);
2991 } else {
2992 LT Result;
2993 return DoShift<LT, RT, ShiftDir::Right>(S, OpPC, LHS, RHS, &Result);
2994 }
2995}
2996
2997template <PrimType NameL, PrimType NameR>
2998inline bool Shl(InterpState &S, CodePtr OpPC) {
2999 using LT = typename PrimConv<NameL>::T;
3000 using RT = typename PrimConv<NameR>::T;
3001 auto RHS = S.Stk.pop<RT>();
3002 auto LHS = S.Stk.pop<LT>();
3003
3004 if constexpr (needsAlloc<LT>() || needsAlloc<RT>()) {
3005 LT Result;
3006 if constexpr (needsAlloc<LT>())
3007 Result = S.allocAP<LT>(LHS.bitWidth());
3008 return DoShiftAP<LT, RT, ShiftDir::Left>(S, OpPC, LHS.toAPSInt(),
3009 RHS.toAPSInt(), &Result);
3010 } else {
3011 LT Result;
3012 return DoShift<LT, RT, ShiftDir::Left>(S, OpPC, LHS, RHS, &Result);
3013 }
3014}
3015
3016static inline bool ShiftFixedPoint(InterpState &S, CodePtr OpPC, bool Left) {
3017 const auto &RHS = S.Stk.pop<FixedPoint>();
3018 const auto &LHS = S.Stk.pop<FixedPoint>();
3019 llvm::FixedPointSemantics LHSSema = LHS.getSemantics();
3020
3021 unsigned ShiftBitWidth =
3022 LHSSema.getWidth() - (unsigned)LHSSema.hasUnsignedPadding() - 1;
3023
3024 // Embedded-C 4.1.6.2.2:
3025 // The right operand must be nonnegative and less than the total number
3026 // of (nonpadding) bits of the fixed-point operand ...
3027 if (RHS.isNegative()) {
3028 S.CCEDiag(S.Current->getLocation(OpPC), diag::note_constexpr_negative_shift)
3029 << RHS.toAPSInt();
3030 } else if (static_cast<unsigned>(RHS.toAPSInt().getLimitedValue(
3031 ShiftBitWidth)) != RHS.toAPSInt()) {
3032 const Expr *E = S.Current->getExpr(OpPC);
3033 S.CCEDiag(E, diag::note_constexpr_large_shift)
3034 << RHS.toAPSInt() << E->getType() << ShiftBitWidth;
3035 }
3036
3038 if (Left) {
3039 if (FixedPoint::shiftLeft(LHS, RHS, ShiftBitWidth, &Result) &&
3041 return false;
3042 } else {
3043 if (FixedPoint::shiftRight(LHS, RHS, ShiftBitWidth, &Result) &&
3045 return false;
3046 }
3047
3049 return true;
3050}
3051
3052//===----------------------------------------------------------------------===//
3053// NoRet
3054//===----------------------------------------------------------------------===//
3055
3056inline bool NoRet(InterpState &S, CodePtr OpPC) {
3057 SourceLocation EndLoc = S.Current->getCallee()->getEndLoc();
3058 S.FFDiag(EndLoc, diag::note_constexpr_no_return);
3059 return false;
3060}
3061
3062//===----------------------------------------------------------------------===//
3063// NarrowPtr, ExpandPtr
3064//===----------------------------------------------------------------------===//
3065
3066inline bool NarrowPtr(InterpState &S, CodePtr OpPC) {
3067 const Pointer &Ptr = S.Stk.pop<Pointer>();
3068 S.Stk.push<Pointer>(Ptr.narrow());
3069 return true;
3070}
3071
3072inline bool ExpandPtr(InterpState &S, CodePtr OpPC) {
3073 const Pointer &Ptr = S.Stk.pop<Pointer>();
3074 if (Ptr.isBlockPointer())
3075 S.Stk.push<Pointer>(Ptr.expand());
3076 else
3077 S.Stk.push<Pointer>(Ptr);
3078 return true;
3079}
3080
3081// 1) Pops an integral value from the stack
3082// 2) Peeks a pointer
3083// 3) Pushes a new pointer that's a narrowed array
3084// element of the peeked pointer with the value
3085// from 1) added as offset.
3086//
3087// This leaves the original pointer on the stack and pushes a new one
3088// with the offset applied and narrowed.
3089template <PrimType Name, class T = typename PrimConv<Name>::T>
3090inline bool ArrayElemPtr(InterpState &S, CodePtr OpPC) {
3091 const T &Offset = S.Stk.pop<T>();
3092 const Pointer &Ptr = S.Stk.peek<Pointer>();
3093
3094 if (!Ptr.isZero() && !Offset.isZero()) {
3095 if (!CheckArray(S, OpPC, Ptr))
3096 return false;
3097 }
3098
3099 if (Offset.isZero()) {
3100 if (Ptr.getFieldDesc()->isArray() && Ptr.getIndex() == 0) {
3101 S.Stk.push<Pointer>(Ptr.atIndex(0).narrow());
3102 return true;
3103 }
3104 S.Stk.push<Pointer>(Ptr);
3105 return true;
3106 }
3107
3108 assert(!Offset.isZero());
3109
3110 if (std::optional<Pointer> Result =
3111 OffsetHelper<T, ArithOp::Add>(S, OpPC, Offset, Ptr)) {
3112 S.Stk.push<Pointer>(Result->narrow());
3113 return true;
3114 }
3115
3116 return false;
3117}
3118
3119template <PrimType Name, class T = typename PrimConv<Name>::T>
3120inline bool ArrayElemPtrPop(InterpState &S, CodePtr OpPC) {
3121 const T &Offset = S.Stk.pop<T>();
3122 const Pointer &Ptr = S.Stk.pop<Pointer>();
3123
3124 if (!Ptr.isZero() && !Offset.isZero()) {
3125 if (!CheckArray(S, OpPC, Ptr))
3126 return false;
3127 }
3128
3129 if (Offset.isZero()) {
3130 if (Ptr.getFieldDesc()->isArray() && Ptr.getIndex() == 0) {
3131 S.Stk.push<Pointer>(Ptr.atIndex(0).narrow());
3132 return true;
3133 }
3134 S.Stk.push<Pointer>(Ptr);
3135 return true;
3136 }
3137
3138 assert(!Offset.isZero());
3139
3140 if (std::optional<Pointer> Result =
3141 OffsetHelper<T, ArithOp::Add>(S, OpPC, Offset, Ptr)) {
3142 S.Stk.push<Pointer>(Result->narrow());
3143 return true;
3144 }
3145 return false;
3146}
3147
3148template <PrimType Name, class T = typename PrimConv<Name>::T>
3149inline bool ArrayElem(InterpState &S, CodePtr OpPC, uint32_t Index) {
3150 const Pointer &Ptr = S.Stk.peek<Pointer>();
3151
3152 if (!CheckLoad(S, OpPC, Ptr))
3153 return false;
3154
3155 assert(Ptr.atIndex(Index).getFieldDesc()->getPrimType() == Name);
3156 S.Stk.push<T>(Ptr.elem<T>(Index));
3157 return true;
3158}
3159
3160template <PrimType Name, class T = typename PrimConv<Name>::T>
3161inline bool ArrayElemPop(InterpState &S, CodePtr OpPC, uint32_t Index) {
3162 const Pointer &Ptr = S.Stk.pop<Pointer>();
3163
3164 if (!CheckLoad(S, OpPC, Ptr))
3165 return false;
3166
3167 assert(Ptr.atIndex(Index).getFieldDesc()->getPrimType() == Name);
3168 S.Stk.push<T>(Ptr.elem<T>(Index));
3169 return true;
3170}
3171
3172template <PrimType Name, class T = typename PrimConv<Name>::T>
3173inline bool CopyArray(InterpState &S, CodePtr OpPC, uint32_t SrcIndex,
3174 uint32_t DestIndex, uint32_t Size) {
3175 const auto &SrcPtr = S.Stk.pop<Pointer>();
3176 const auto &DestPtr = S.Stk.peek<Pointer>();
3177
3178 if (SrcPtr.isDummy() || DestPtr.isDummy())
3179 return false;
3180
3181 for (uint32_t I = 0; I != Size; ++I) {
3182 const Pointer &SP = SrcPtr.atIndex(SrcIndex + I);
3183
3184 if (!CheckLoad(S, OpPC, SP))
3185 return false;
3186
3187 const Pointer &DP = DestPtr.atIndex(DestIndex + I);
3188 DP.deref<T>() = SP.deref<T>();
3189 DP.initialize();
3190 }
3191 return true;
3192}
3193
3194/// Just takes a pointer and checks if it's an incomplete
3195/// array type.
3196inline bool ArrayDecay(InterpState &S, CodePtr OpPC) {
3197 const Pointer &Ptr = S.Stk.pop<Pointer>();
3198
3199 if (Ptr.isZero()) {
3200 S.Stk.push<Pointer>(Ptr);
3201 return true;
3202 }
3203
3204 if (!Ptr.isZeroSizeArray()) {
3205 if (!CheckRange(S, OpPC, Ptr, CSK_ArrayToPointer))
3206 return false;
3207 }
3208
3209 if (Ptr.isRoot() || !Ptr.isUnknownSizeArray()) {
3210 S.Stk.push<Pointer>(Ptr.atIndex(0));
3211 return true;
3212 }
3213
3214 const SourceInfo &E = S.Current->getSource(OpPC);
3215 S.FFDiag(E, diag::note_constexpr_unsupported_unsized_array);
3216
3217 return false;
3218}
3219
3220inline bool GetFnPtr(InterpState &S, CodePtr OpPC, const Function *Func) {
3221 assert(Func);
3222 S.Stk.push<Pointer>(Func);
3223 return true;
3224}
3225
3226template <PrimType Name, class T = typename PrimConv<Name>::T>
3227inline bool GetIntPtr(InterpState &S, CodePtr OpPC, const Descriptor *Desc) {
3228 const T &IntVal = S.Stk.pop<T>();
3229
3230 S.CCEDiag(S.Current->getSource(OpPC), diag::note_constexpr_invalid_cast)
3231 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
3232 << S.getLangOpts().CPlusPlus;
3233
3234 S.Stk.push<Pointer>(static_cast<uint64_t>(IntVal), Desc);
3235 return true;
3236}
3237
3238inline bool GetMemberPtr(InterpState &S, CodePtr OpPC, const ValueDecl *D) {
3239 S.Stk.push<MemberPointer>(D);
3240 return true;
3241}
3242
3243inline bool GetMemberPtrBase(InterpState &S, CodePtr OpPC) {
3244 const auto &MP = S.Stk.pop<MemberPointer>();
3245
3246 if (!MP.isBaseCastPossible())
3247 return false;
3248
3249 S.Stk.push<Pointer>(MP.getBase());
3250 return true;
3251}
3252
3253inline bool GetMemberPtrDecl(InterpState &S, CodePtr OpPC) {
3254 const auto &MP = S.Stk.pop<MemberPointer>();
3255
3256 const auto *FD = cast<FunctionDecl>(MP.getDecl());
3257 const auto *Func = S.getContext().getOrCreateFunction(FD);
3258
3259 S.Stk.push<Pointer>(Func);
3260 return true;
3261}
3262
3263/// Just emit a diagnostic. The expression that caused emission of this
3264/// op is not valid in a constant context.
3265inline bool Invalid(InterpState &S, CodePtr OpPC) {
3266 const SourceLocation &Loc = S.Current->getLocation(OpPC);
3267 S.FFDiag(Loc, diag::note_invalid_subexpr_in_const_expr)
3268 << S.Current->getRange(OpPC);
3269 return false;
3270}
3271
3272inline bool Unsupported(InterpState &S, CodePtr OpPC) {
3273 const SourceLocation &Loc = S.Current->getLocation(OpPC);
3274 S.FFDiag(Loc, diag::note_constexpr_stmt_expr_unsupported)
3275 << S.Current->getRange(OpPC);
3276 return false;
3277}
3278
3279inline bool StartSpeculation(InterpState &S, CodePtr OpPC) {
3280 ++S.SpeculationDepth;
3281 if (S.SpeculationDepth != 1)
3282 return true;
3283
3284 assert(S.PrevDiags == nullptr);
3286 S.getEvalStatus().Diag = nullptr;
3287 return true;
3288}
3289inline bool EndSpeculation(InterpState &S, CodePtr OpPC) {
3290 assert(S.SpeculationDepth != 0);
3291 --S.SpeculationDepth;
3292 if (S.SpeculationDepth == 0) {
3294 S.PrevDiags = nullptr;
3295 }
3296 return true;
3297}
3298
3299inline bool PushCC(InterpState &S, CodePtr OpPC, bool Value) {
3301 return true;
3302}
3303inline bool PopCC(InterpState &S, CodePtr OpPC) {
3304 S.ConstantContextOverride = std::nullopt;
3305 return true;
3306}
3307
3308/// Do nothing and just abort execution.
3309inline bool Error(InterpState &S, CodePtr OpPC) { return false; }
3310
3311inline bool SideEffect(InterpState &S, CodePtr OpPC) {
3312 return S.noteSideEffect();
3313}
3314
3315/// Same here, but only for casts.
3316inline bool InvalidCast(InterpState &S, CodePtr OpPC, CastKind Kind,
3317 bool Fatal) {
3318 const SourceLocation &Loc = S.Current->getLocation(OpPC);
3319
3320 if (Kind == CastKind::Reinterpret) {
3321 S.CCEDiag(Loc, diag::note_constexpr_invalid_cast)
3322 << static_cast<unsigned>(Kind) << S.Current->getRange(OpPC);
3323 return !Fatal;
3324 }
3325 if (Kind == CastKind::Volatile) {
3327 const auto *E = cast<CastExpr>(S.Current->getExpr(OpPC));
3328 if (S.getLangOpts().CPlusPlus)
3329 S.FFDiag(E, diag::note_constexpr_access_volatile_type)
3330 << AK_Read << E->getSubExpr()->getType();
3331 else
3332 S.FFDiag(E);
3333 }
3334
3335 return false;
3336 }
3337 if (Kind == CastKind::Dynamic) {
3338 assert(!S.getLangOpts().CPlusPlus20);
3339 S.CCEDiag(S.Current->getSource(OpPC), diag::note_constexpr_invalid_cast)
3340 << diag::ConstexprInvalidCastKind::Dynamic;
3341 return true;
3342 }
3343
3344 return false;
3345}
3346
3347inline bool InvalidDeclRef(InterpState &S, CodePtr OpPC, const DeclRefExpr *DR,
3348 bool InitializerFailed) {
3349 assert(DR);
3350
3351 if (InitializerFailed) {
3352 const SourceInfo &Loc = S.Current->getSource(OpPC);
3353 const auto *VD = cast<VarDecl>(DR->getDecl());
3354 S.FFDiag(Loc, diag::note_constexpr_var_init_non_constant, 1) << VD;
3355 S.Note(VD->getLocation(), diag::note_declared_at);
3356 return false;
3357 }
3358
3359 return CheckDeclRef(S, OpPC, DR);
3360}
3361
3363 if (S.inConstantContext()) {
3364 const SourceRange &ArgRange = S.Current->getRange(OpPC);
3365 const Expr *E = S.Current->getExpr(OpPC);
3366 S.CCEDiag(E, diag::note_constexpr_non_const_vectorelements) << ArgRange;
3367 }
3368 return false;
3369}
3370
3371inline bool CheckPseudoDtor(InterpState &S, CodePtr OpPC) {
3372 if (!S.getLangOpts().CPlusPlus20)
3373 S.CCEDiag(S.Current->getSource(OpPC),
3374 diag::note_constexpr_pseudo_destructor);
3375 return true;
3376}
3377
3378inline bool Assume(InterpState &S, CodePtr OpPC) {
3379 const auto Val = S.Stk.pop<Boolean>();
3380
3381 if (Val)
3382 return true;
3383
3384 // Else, diagnose.
3385 const SourceLocation &Loc = S.Current->getLocation(OpPC);
3386 S.CCEDiag(Loc, diag::note_constexpr_assumption_failed);
3387 return false;
3388}
3389
3390template <PrimType Name, class T = typename PrimConv<Name>::T>
3391inline bool OffsetOf(InterpState &S, CodePtr OpPC, const OffsetOfExpr *E) {
3392 llvm::SmallVector<int64_t> ArrayIndices;
3393 for (size_t I = 0; I != E->getNumExpressions(); ++I)
3394 ArrayIndices.emplace_back(S.Stk.pop<int64_t>());
3395
3396 int64_t Result;
3397 if (!InterpretOffsetOf(S, OpPC, E, ArrayIndices, Result))
3398 return false;
3399
3400 S.Stk.push<T>(T::from(Result));
3401
3402 return true;
3403}
3404
3405template <PrimType Name, class T = typename PrimConv<Name>::T>
3406inline bool CheckNonNullArg(InterpState &S, CodePtr OpPC) {
3407 const T &Arg = S.Stk.peek<T>();
3408 if (!Arg.isZero())
3409 return true;
3410
3411 const SourceLocation &Loc = S.Current->getLocation(OpPC);
3412 S.CCEDiag(Loc, diag::note_non_null_attribute_failed);
3413
3414 return false;
3415}
3416
3417void diagnoseEnumValue(InterpState &S, CodePtr OpPC, const EnumDecl *ED,
3418 const APSInt &Value);
3419
3420template <PrimType Name, class T = typename PrimConv<Name>::T>
3421inline bool CheckEnumValue(InterpState &S, CodePtr OpPC, const EnumDecl *ED) {
3422 assert(ED);
3423 assert(!ED->isFixed());
3424
3425 if (S.inConstantContext()) {
3426 const APSInt Val = S.Stk.peek<T>().toAPSInt();
3427 diagnoseEnumValue(S, OpPC, ED, Val);
3428 }
3429 return true;
3430}
3431
3432/// OldPtr -> Integer -> NewPtr.
3433template <PrimType TIn, PrimType TOut>
3434inline bool DecayPtr(InterpState &S, CodePtr OpPC) {
3435 static_assert(isPtrType(TIn) && isPtrType(TOut));
3436 using FromT = typename PrimConv<TIn>::T;
3437 using ToT = typename PrimConv<TOut>::T;
3438
3439 const FromT &OldPtr = S.Stk.pop<FromT>();
3440
3441 if constexpr (std::is_same_v<FromT, FunctionPointer> &&
3442 std::is_same_v<ToT, Pointer>) {
3443 S.Stk.push<Pointer>(OldPtr.getFunction(), OldPtr.getOffset());
3444 return true;
3445 } else if constexpr (std::is_same_v<FromT, Pointer> &&
3446 std::is_same_v<ToT, FunctionPointer>) {
3447 if (OldPtr.isFunctionPointer()) {
3448 S.Stk.push<FunctionPointer>(OldPtr.asFunctionPointer().getFunction(),
3449 OldPtr.getByteOffset());
3450 return true;
3451 }
3452 }
3453
3454 S.Stk.push<ToT>(ToT(OldPtr.getIntegerRepresentation(), nullptr));
3455 return true;
3456}
3457
3458inline bool CheckDecl(InterpState &S, CodePtr OpPC, const VarDecl *VD) {
3459 // An expression E is a core constant expression unless the evaluation of E
3460 // would evaluate one of the following: [C++23] - a control flow that passes
3461 // through a declaration of a variable with static or thread storage duration
3462 // unless that variable is usable in constant expressions.
3463 assert(VD->isLocalVarDecl() &&
3464 VD->isStaticLocal()); // Checked before emitting this.
3465
3466 if (VD == S.EvaluatingDecl)
3467 return true;
3468
3470 S.CCEDiag(VD->getLocation(), diag::note_constexpr_static_local)
3471 << (VD->getTSCSpec() == TSCS_unspecified ? 0 : 1) << VD;
3472 return false;
3473 }
3474 return true;
3475}
3476
3477inline bool Alloc(InterpState &S, CodePtr OpPC, const Descriptor *Desc) {
3478 assert(Desc);
3479
3480 if (!CheckDynamicMemoryAllocation(S, OpPC))
3481 return false;
3482
3483 DynamicAllocator &Allocator = S.getAllocator();
3484 Block *B = Allocator.allocate(Desc, S.Ctx.getEvalID(),
3486 assert(B);
3487 S.Stk.push<Pointer>(B);
3488 return true;
3489}
3490
3491template <PrimType Name, class SizeT = typename PrimConv<Name>::T>
3492inline bool AllocN(InterpState &S, CodePtr OpPC, PrimType T, const Expr *Source,
3493 bool IsNoThrow) {
3494 if (!CheckDynamicMemoryAllocation(S, OpPC))
3495 return false;
3496
3497 SizeT NumElements = S.Stk.pop<SizeT>();
3498 if (!CheckArraySize(S, OpPC, &NumElements, primSize(T), IsNoThrow)) {
3499 if (!IsNoThrow)
3500 return false;
3501
3502 // If this failed and is nothrow, just return a null ptr.
3503 S.Stk.push<Pointer>(0, nullptr);
3504 return true;
3505 }
3506 if (NumElements.isNegative()) {
3507 if (!IsNoThrow) {
3508 S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_new_negative)
3509 << NumElements.toDiagnosticString(S.getASTContext());
3510 return false;
3511 }
3512 S.Stk.push<Pointer>(0, nullptr);
3513 return true;
3514 }
3515
3516 if (!CheckArraySize(S, OpPC, static_cast<uint64_t>(NumElements)))
3517 return false;
3518
3519 DynamicAllocator &Allocator = S.getAllocator();
3520 Block *B =
3521 Allocator.allocate(Source, T, static_cast<size_t>(NumElements),
3523 assert(B);
3524 if (NumElements.isZero())
3525 S.Stk.push<Pointer>(B);
3526 else
3527 S.Stk.push<Pointer>(Pointer(B).atIndex(0));
3528 return true;
3529}
3530
3531template <PrimType Name, class SizeT = typename PrimConv<Name>::T>
3532inline bool AllocCN(InterpState &S, CodePtr OpPC, const Descriptor *ElementDesc,
3533 bool IsNoThrow) {
3534 if (!CheckDynamicMemoryAllocation(S, OpPC))
3535 return false;
3536
3537 SizeT NumElements = S.Stk.pop<SizeT>();
3538 if (!CheckArraySize(S, OpPC, &NumElements, ElementDesc->getSize(),
3539 IsNoThrow)) {
3540 if (!IsNoThrow)
3541 return false;
3542
3543 // If this failed and is nothrow, just return a null ptr.
3544 S.Stk.push<Pointer>(0, ElementDesc);
3545 return true;
3546 }
3547 assert(NumElements.isPositive());
3548
3549 if (!CheckArraySize(S, OpPC, static_cast<uint64_t>(NumElements)))
3550 return false;
3551
3552 DynamicAllocator &Allocator = S.getAllocator();
3553 Block *B =
3554 Allocator.allocate(ElementDesc, static_cast<size_t>(NumElements),
3556 assert(B);
3557 if (NumElements.isZero())
3558 S.Stk.push<Pointer>(B);
3559 else
3560 S.Stk.push<Pointer>(Pointer(B).atIndex(0));
3561
3562 return true;
3563}
3564
3565bool Free(InterpState &S, CodePtr OpPC, bool DeleteIsArrayForm,
3566 bool IsGlobalDelete);
3567
3568static inline bool IsConstantContext(InterpState &S, CodePtr OpPC) {
3570 return true;
3571}
3572
3573static inline bool CheckAllocations(InterpState &S, CodePtr OpPC) {
3575}
3576
3577/// Check if the initializer and storage types of a placement-new expression
3578/// match.
3579bool CheckNewTypeMismatch(InterpState &S, CodePtr OpPC, const Expr *E,
3580 std::optional<uint64_t> ArraySize = std::nullopt);
3581
3582template <PrimType Name, class T = typename PrimConv<Name>::T>
3584 const auto &Size = S.Stk.pop<T>();
3585 return CheckNewTypeMismatch(S, OpPC, E, static_cast<uint64_t>(Size));
3586}
3587bool InvalidNewDeleteExpr(InterpState &S, CodePtr OpPC, const Expr *E);
3588
3589template <PrimType Name, class T = typename PrimConv<Name>::T>
3590inline bool BitCastPrim(InterpState &S, CodePtr OpPC, bool TargetIsUCharOrByte,
3591 uint32_t ResultBitWidth, const llvm::fltSemantics *Sem,
3592 const Type *TargetType) {
3593 const Pointer &FromPtr = S.Stk.pop<Pointer>();
3594
3595 if (!CheckLoad(S, OpPC, FromPtr))
3596 return false;
3597
3598 if constexpr (std::is_same_v<T, Pointer>) {
3599 if (!TargetType->isNullPtrType()) {
3600 S.FFDiag(S.Current->getSource(OpPC),
3601 diag::note_constexpr_bit_cast_invalid_type)
3602 << /*IsToType=*/true << /*IsReference=*/false << 1 /*Pointer*/;
3603 return false;
3604 }
3605 // The only pointer type we can validly bitcast to is nullptr_t.
3606 S.Stk.push<Pointer>();
3607 return true;
3608 } else if constexpr (std::is_same_v<T, MemberPointer>) {
3609 S.FFDiag(S.Current->getSource(OpPC),
3610 diag::note_constexpr_bit_cast_invalid_type)
3611 << /*IsToType=*/true << /*IsReference=*/false << 2 /*MemberPointer*/;
3612 return false;
3613 } else {
3614
3615 size_t BuffSize = ResultBitWidth / 8;
3616 llvm::SmallVector<std::byte> Buff(BuffSize);
3617 bool HasIndeterminateBits = false;
3618
3619 Bits FullBitWidth(ResultBitWidth);
3620 Bits BitWidth = FullBitWidth;
3621
3622 if constexpr (std::is_same_v<T, Floating>) {
3623 assert(Sem);
3624 BitWidth = Bits(llvm::APFloatBase::getSizeInBits(*Sem));
3625 }
3626
3627 if (!DoBitCast(S, OpPC, FromPtr, Buff.data(), BitWidth, FullBitWidth,
3628 HasIndeterminateBits))
3629 return false;
3630
3631 if (!CheckBitCast(S, OpPC, HasIndeterminateBits, TargetIsUCharOrByte))
3632 return false;
3633
3634 if constexpr (std::is_same_v<T, Floating>) {
3635 assert(Sem);
3636 Floating Result = S.allocFloat(*Sem);
3637 Floating::bitcastFromMemory(Buff.data(), *Sem, &Result);
3638 S.Stk.push<Floating>(Result);
3639 } else if constexpr (needsAlloc<T>()) {
3640 T Result = S.allocAP<T>(ResultBitWidth);
3641 T::bitcastFromMemory(Buff.data(), ResultBitWidth, &Result);
3642 S.Stk.push<T>(Result);
3643 } else if constexpr (std::is_same_v<T, Boolean>) {
3644 // Only allow to cast single-byte integers to bool if they are either 0
3645 // or 1.
3646 assert(FullBitWidth.getQuantity() == 8);
3647 auto Val = static_cast<unsigned int>(Buff[0]);
3648 if (Val > 1) {
3649 S.FFDiag(S.Current->getSource(OpPC),
3650 diag::note_constexpr_bit_cast_unrepresentable_value)
3651 << S.getASTContext().BoolTy << Val;
3652 return false;
3653 }
3654 S.Stk.push<T>(T::bitcastFromMemory(Buff.data(), ResultBitWidth));
3655 } else {
3656 assert(!Sem);
3657 S.Stk.push<T>(T::bitcastFromMemory(Buff.data(), ResultBitWidth));
3658 }
3659 return true;
3660 }
3661}
3662
3663inline bool BitCast(InterpState &S, CodePtr OpPC) {
3664 const Pointer &FromPtr = S.Stk.pop<Pointer>();
3665 Pointer &ToPtr = S.Stk.peek<Pointer>();
3666
3667 if (!CheckLoad(S, OpPC, FromPtr))
3668 return false;
3669
3670 if (!DoBitCastPtr(S, OpPC, FromPtr, ToPtr))
3671 return false;
3672
3673 return true;
3674}
3675
3676/// Typeid support.
3677bool GetTypeid(InterpState &S, CodePtr OpPC, const Type *TypePtr,
3678 const Type *TypeInfoType);
3679bool GetTypeidPtr(InterpState &S, CodePtr OpPC, const Type *TypeInfoType);
3680bool DiagTypeid(InterpState &S, CodePtr OpPC);
3681
3682inline bool CheckDestruction(InterpState &S, CodePtr OpPC) {
3683 const auto &Ptr = S.Stk.peek<Pointer>();
3684 return CheckDestructor(S, OpPC, Ptr);
3685}
3686
3687inline bool CheckArraySize(InterpState &S, CodePtr OpPC, uint64_t NumElems) {
3688 uint64_t Limit = S.getLangOpts().ConstexprStepLimit;
3689 if (NumElems > Limit) {
3690 S.FFDiag(S.Current->getSource(OpPC),
3691 diag::note_constexpr_new_exceeds_limits)
3692 << NumElems << Limit;
3693 return false;
3694 }
3695 return true;
3696}
3697
3698//===----------------------------------------------------------------------===//
3699// Read opcode arguments
3700//===----------------------------------------------------------------------===//
3701
3702template <typename T> inline T ReadArg(InterpState &S, CodePtr &OpPC) {
3703 if constexpr (std::is_pointer<T>::value) {
3704 uint32_t ID = OpPC.read<uint32_t>();
3705 return reinterpret_cast<T>(S.P.getNativePointer(ID));
3706 } else {
3707 return OpPC.read<T>();
3708 }
3709}
3710
3711template <> inline Floating ReadArg<Floating>(InterpState &S, CodePtr &OpPC) {
3712 auto &Semantics =
3713 llvm::APFloatBase::EnumToSemantics(Floating::deserializeSemantics(*OpPC));
3714
3715 auto F = S.allocFloat(Semantics);
3716 Floating::deserialize(*OpPC, &F);
3717 OpPC += align(F.bytesToSerialize());
3718 return F;
3719}
3720
3721template <>
3722inline IntegralAP<false> ReadArg<IntegralAP<false>>(InterpState &S,
3723 CodePtr &OpPC) {
3724 uint32_t BitWidth = IntegralAP<false>::deserializeSize(*OpPC);
3725 auto Result = S.allocAP<IntegralAP<false>>(BitWidth);
3726 assert(Result.bitWidth() == BitWidth);
3727
3729 OpPC += align(Result.bytesToSerialize());
3730 return Result;
3731}
3732
3733template <>
3734inline IntegralAP<true> ReadArg<IntegralAP<true>>(InterpState &S,
3735 CodePtr &OpPC) {
3736 uint32_t BitWidth = IntegralAP<true>::deserializeSize(*OpPC);
3737 auto Result = S.allocAP<IntegralAP<true>>(BitWidth);
3738 assert(Result.bitWidth() == BitWidth);
3739
3740 IntegralAP<true>::deserialize(*OpPC, &Result);
3741 OpPC += align(Result.bytesToSerialize());
3742 return Result;
3743}
3744
3745template <>
3748 OpPC += align(FP.bytesToSerialize());
3749 return FP;
3750}
3751
3752} // namespace interp
3753} // namespace clang
3754
3755#endif
Defines the clang::ASTContext interface.
#define V(N, I)
void HandleComplexComplexDiv(APFloat A, APFloat B, APFloat C, APFloat D, APFloat &ResR, APFloat &ResI)
void HandleComplexComplexMul(APFloat A, APFloat B, APFloat C, APFloat D, APFloat &ResR, APFloat &ResI)
QualType getConstantArrayType(QualType EltTy, const llvm::APInt &ArySize, const Expr *SizeExpr, ArraySizeModifier ASM, unsigned IndexTypeQuals) const
Return the unique reference to the type for a constant array of the specified element type.
CanQualType BoolTy
bool hasSimilarType(QualType T1, QualType T2) const
Determine if two types are similar, according to the C++ rules.
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2129
bool isVirtual() const
Definition DeclCXX.h:2184
CXXRecordDecl * getCanonicalDecl() override
Retrieves the "canonical" declaration of the given declaration.
Definition DeclCXX.h:522
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:2877
unsigned getBuiltinCallee() const
getBuiltinCallee - If this is a call to a builtin, return the builtin ID of the callee.
Definition Expr.cpp:1588
const ValueInfo * getValueInfo(ComparisonCategoryResult ValueKind) const
ComparisonCategoryResult makeWeakResult(ComparisonCategoryResult Res) const
Converts the specified result kind into the correct result kind for this category.
static unsigned getMaxSizeBits(const ASTContext &Context)
Determine the maximum number of active bits that an array's size can require, which limits the maximu...
Definition Type.cpp:254
A reference to a declared variable, function, enum, etc.
Definition Expr.h:1270
ValueDecl * getDecl()
Definition Expr.h:1338
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
SourceLocation getEndLoc() const LLVM_READONLY
Definition DeclBase.h:435
SourceLocation getLocation() const
Definition DeclBase.h:439
AccessSpecifier getAccess() const
Definition DeclBase.h:507
Represents an enum.
Definition Decl.h:4004
bool isFixed() const
Returns true if this is an Objective-C, C++11, or Microsoft-style enumeration with a fixed underlying...
Definition Decl.h:4222
This represents one expression.
Definition Expr.h:112
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition Expr.cpp:273
QualType getType() const
Definition Expr.h:144
static FPOptions getFromOpaqueInt(storage_type Value)
RoundingMode getRoundingMode() const
Represents a member of a struct/union/class.
Definition Decl.h:3157
const RecordDecl * getParent() const
Returns the parent of this field declaration, which is the struct in which this field is defined.
Definition Decl.h:3393
Implicit declaration of a temporary that was materialized by a MaterializeTemporaryExpr and lifetime-...
Definition DeclCXX.h:3308
OffsetOfExpr - [C99 7.17] - This represents an expression of the form offsetof(record-type,...
Definition Expr.h:2527
unsigned getNumExpressions() const
Definition Expr.h:2598
A (possibly-)qualified type.
Definition TypeBase.h:937
Represents a struct/union/class.
Definition Decl.h:4309
Encodes a location in the source.
A trivial tuple used to represent a source range.
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition Stmt.cpp:334
TagDecl * getCanonicalDecl() override
Retrieves the "canonical" declaration of the given declaration.
Definition Decl.cpp:4840
bool isUnion() const
Definition Decl.h:3919
The base class of the type hierarchy.
Definition TypeBase.h:1833
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition Type.h:26
bool isNullPtrType() const
Definition TypeBase.h:8915
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition Decl.h:711
Represents a variable declaration or definition.
Definition Decl.h:925
bool isStaticLocal() const
Returns true if a variable with function scope is a static local variable.
Definition Decl.h:1207
ThreadStorageClassSpecifier getTSCSpec() const
Definition Decl.h:1176
bool isLocalVarDecl() const
Returns true for local variable declarations other than parameters.
Definition Decl.h:1252
bool isUsableInConstantExpressions(const ASTContext &C) const
Determine whether this variable's value can be used in a constant expression, according to the releva...
Definition Decl.cpp:2528
A memory block, either on the stack or in the heap.
Definition InterpBlock.h:44
bool isExtern() const
Checks if the block is extern.
Definition InterpBlock.h:77
const Descriptor * getDescriptor() const
Returns the block's descriptor.
Definition InterpBlock.h:73
std::byte * rawData()
Returns a pointer to the raw data, including metadata.
Wrapper around boolean types.
Definition Boolean.h:25
static Boolean from(T Value)
Definition Boolean.h:97
Pointer into the code segment.
Definition Source.h:30
std::enable_if_t<!std::is_pointer< T >::value, T > read()
Reads data and advances the pointer.
Definition Source.h:56
const Function * getOrCreateFunction(const FunctionDecl *FuncDecl)
Definition Context.cpp:447
unsigned getEvalID() const
Definition Context.h:141
Manages dynamic memory allocations done during bytecode interpretation.
Block * allocate(const Descriptor *D, unsigned EvalID, Form AllocForm)
Allocate ONE element of the given descriptor.
Wrapper around fixed point types.
Definition FixedPoint.h:23
llvm::FixedPointSemantics getSemantics() const
Definition FixedPoint.h:71
static bool shiftRight(const FixedPoint A, const FixedPoint B, unsigned OpBits, FixedPoint *R)
Definition FixedPoint.h:158
static FixedPoint deserialize(const std::byte *Buff)
Definition FixedPoint.h:108
static bool shiftLeft(const FixedPoint A, const FixedPoint B, unsigned OpBits, FixedPoint *R)
Definition FixedPoint.h:151
static FixedPoint from(const APSInt &I, llvm::FixedPointSemantics Sem, bool *Overflow)
Definition FixedPoint.h:40
size_t bytesToSerialize() const
Definition FixedPoint.h:94
If a Floating is constructed from Memory, it DOES NOT OWN THAT MEMORY.
Definition Floating.h:35
static APFloat::opStatus div(const Floating &A, const Floating &B, llvm::RoundingMode RM, Floating *R)
Definition Floating.h:286
static llvm::APFloatBase::Semantics deserializeSemantics(const std::byte *Buff)
Definition Floating.h:211
void copy(const APFloat &F)
Definition Floating.h:122
static APFloat::opStatus fromIntegral(APSInt Val, const llvm::fltSemantics &Sem, llvm::RoundingMode RM, Floating *Result)
Definition Floating.h:171
static APFloat::opStatus sub(const Floating &A, const Floating &B, llvm::RoundingMode RM, Floating *R)
Definition Floating.h:255
static APFloat::opStatus increment(const Floating &A, llvm::RoundingMode RM, Floating *R)
Definition Floating.h:245
static APFloat::opStatus add(const Floating &A, const Floating &B, llvm::RoundingMode RM, Floating *R)
Definition Floating.h:235
static void deserialize(const std::byte *Buff, Floating *Result)
Definition Floating.h:215
static APFloat::opStatus mul(const Floating &A, const Floating &B, llvm::RoundingMode RM, Floating *R)
Definition Floating.h:275
bool isNonZero() const
Definition Floating.h:144
void toSemantics(const llvm::fltSemantics *Sem, llvm::RoundingMode RM, Floating *Result) const
Definition Floating.h:76
const llvm::fltSemantics & getSemantics() const
Definition Floating.h:118
bool isFinite() const
Definition Floating.h:150
static APFloat::opStatus decrement(const Floating &A, llvm::RoundingMode RM, Floating *R)
Definition Floating.h:265
APFloat::opStatus convertToInteger(APSInt &Result) const
Definition Floating.h:70
static void bitcastFromMemory(const std::byte *Buff, const llvm::fltSemantics &Sem, Floating *Result)
Definition Floating.h:181
APFloat getAPFloat() const
Definition Floating.h:63
const Function * getFunction() const
Bytecode function.
Definition Function.h:86
Scope & getScope(unsigned Idx)
Returns a specific scope.
Definition Function.h:147
const FunctionDecl * getDecl() const
Returns the original FunctionDecl.
Definition Function.h:109
bool hasRVO() const
Checks if the first argument is a RVO pointer.
Definition Function.h:129
If an IntegralAP is constructed from Memory, it DOES NOT OWN THAT MEMORY.
Definition IntegralAP.h:36
static uint32_t deserializeSize(const std::byte *Buff)
Definition IntegralAP.h:332
static void deserialize(const std::byte *Buff, IntegralAP< Signed > *Result)
Definition IntegralAP.h:336
void copy(const APInt &V)
Definition IntegralAP.h:78
Wrapper around numeric types.
Definition Integral.h:66
Frame storing local variables.
Definition InterpFrame.h:26
static void free(InterpFrame *F)
Definition InterpFrame.h:48
const Expr * getExpr(CodePtr PC) const
InterpFrame * Caller
The frame of the previous function.
Definition InterpFrame.h:29
SourceInfo getSource(CodePtr PC) const
Map a location to a source.
CodePtr getRetPC() const
Returns the return address of the frame.
Block * getLocalBlock(unsigned Offset) const
SourceLocation getLocation(CodePtr PC) const
const Pointer & getThis() const
Returns the 'this' pointer.
const Function * getFunction() const
Returns the current function.
Definition InterpFrame.h:71
size_t getFrameOffset() const
Returns the offset on the stack at which the frame starts.
Definition InterpFrame.h:74
SourceRange getRange(CodePtr PC) const
void setLocal(unsigned Offset, const T &Value)
Mutates a local variable.
Definition InterpFrame.h:82
const T & getParam(unsigned Offset) const
Returns the value of an argument.
Definition InterpFrame.h:92
Pointer getLocalPointer(unsigned Offset) const
Returns a pointer to a local variables.
unsigned getDepth() const
void setParam(unsigned Offset, const T &Value)
Mutates a local copy of a parameter.
void destroy(unsigned Idx)
Invokes the destructors for a scope.
const Pointer & getRVOPtr() const
Returns the RVO pointer, if the Function has one.
Pointer getParamPointer(unsigned Offset)
Returns a pointer to an argument - lazily creates a block.
const FunctionDecl * getCallee() const override
Returns the caller.
void initScope(unsigned Idx)
T pop()
Returns the value from the top of the stack and removes it.
Definition InterpStack.h:39
void push(Tys &&...Args)
Constructs a value in place on the top of the stack.
Definition InterpStack.h:33
void dump() const
dump the stack contents to stderr.
size_t size() const
Returns the size of the stack in bytes.
Definition InterpStack.h:77
void discard()
Discards the top value from the stack.
Definition InterpStack.h:50
T & peek() const
Returns a reference to the value on the top of the stack.
Definition InterpStack.h:62
Interpreter context.
Definition InterpState.h:43
SmallVectorImpl< PartialDiagnosticAt > * PrevDiags
Things needed to do speculative execution.
Expr::EvalStatus & getEvalStatus() const override
Definition InterpState.h:67
Context & getContext() const
bool noteUndefinedBehavior() override
Definition InterpState.h:82
DynamicAllocator & getAllocator()
Context & Ctx
Interpreter Context.
Floating allocFloat(const llvm::fltSemantics &Sem)
ASTContext & getASTContext() const override
Definition InterpState.h:70
llvm::SmallVector< std::pair< const Expr *, const LifetimeExtendedTemporaryDecl * > > SeenGlobalTemporaries
InterpStack & Stk
Temporary stack.
bool maybeDiagnoseDanglingAllocations()
Diagnose any dynamic allocations that haven't been freed yet.
bool noteSideEffect() override
Definition InterpState.h:94
const VarDecl * EvaluatingDecl
Declaration we're initializing/evaluting, if any.
InterpFrame * Current
The current frame.
std::optional< bool > ConstantContextOverride
T allocAP(unsigned BitWidth)
const LangOptions & getLangOpts() const
Definition InterpState.h:71
StdAllocatorCaller getStdAllocatorCaller(StringRef Name) const
Program & P
Reference to the module containing all bytecode.
ComparisonCategoryResult compare(const MemberPointer &RHS) const
A pointer to a memory block, live or dead.
Definition Pointer.h:91
static bool hasSameBase(const Pointer &A, const Pointer &B)
Checks if two pointers are comparable.
Definition Pointer.cpp:626
Pointer narrow() const
Restricts the scope of an array element pointer.
Definition Pointer.h:188
bool isInitialized() const
Checks if an object was initialized.
Definition Pointer.cpp:432
bool isZeroSizeArray() const
Checks if the pointer is pointing to a zero-size array.
Definition Pointer.h:651
Pointer atIndex(uint64_t Idx) const
Offsets a pointer inside an array.
Definition Pointer.h:156
bool isDummy() const
Checks if the pointer points to a dummy value.
Definition Pointer.h:544
Pointer atFieldSub(unsigned Off) const
Subtract the given offset from the current Base and Offset of the pointer.
Definition Pointer.h:181
int64_t getIndex() const
Returns the index into an array.
Definition Pointer.h:609
Pointer atField(unsigned Off) const
Creates a pointer to a field.
Definition Pointer.h:173
T & deref() const
Dereferences the pointer, if it's live.
Definition Pointer.h:660
unsigned getNumElems() const
Returns the number of elements.
Definition Pointer.h:593
bool isUnknownSizeArray() const
Checks if the structure is an array of unknown size.
Definition Pointer.h:412
void activate() const
Activats a field.
Definition Pointer.cpp:568
static std::optional< std::pair< Pointer, Pointer > > computeSplitPoint(const Pointer &A, const Pointer &B)
Definition Pointer.cpp:678
bool isIntegralPointer() const
Definition Pointer.h:466
bool pointsToStringLiteral() const
Definition Pointer.cpp:666
bool inArray() const
Checks if the innermost field is an array.
Definition Pointer.h:394
T & elem(unsigned I) const
Dereferences the element at index I.
Definition Pointer.h:676
uint64_t getByteOffset() const
Returns the byte offset from the start.
Definition Pointer.h:582
bool isTypeidPointer() const
Definition Pointer.h:468
std::string toDiagnosticString(const ASTContext &Ctx) const
Converts the pointer to a string usable in diagnostics.
Definition Pointer.cpp:419
bool isZero() const
Checks if the pointer is null.
Definition Pointer.h:254
const IntPointer & asIntPointer() const
Definition Pointer.h:452
bool isRoot() const
Pointer points directly to a block.
Definition Pointer.h:434
const Descriptor * getDeclDesc() const
Accessor for information about the declaration site.
Definition Pointer.h:279
unsigned getOffset() const
Returns the offset into an array.
Definition Pointer.h:373
bool isOnePastEnd() const
Checks if the index is one past end.
Definition Pointer.h:626
uint64_t getIntegerRepresentation() const
Definition Pointer.h:143
Pointer expand() const
Expands a pointer to the containing array, undoing narrowing.
Definition Pointer.h:221
bool isElementPastEnd() const
Checks if the pointer is an out-of-bounds element pointer.
Definition Pointer.h:648
bool isBlockPointer() const
Definition Pointer.h:465
const FunctionPointer & asFunctionPointer() const
Definition Pointer.h:456
bool isFunctionPointer() const
Definition Pointer.h:467
const Descriptor * getFieldDesc() const
Accessors for information about the innermost field.
Definition Pointer.h:323
size_t elemSize() const
Returns the element size of the innermost field.
Definition Pointer.h:355
bool canBeInitialized() const
If this pointer has an InlineDescriptor we can use to initialize.
Definition Pointer.h:441
Lifetime getLifetime() const
Definition Pointer.h:721
size_t computeOffsetForComparison() const
Compute an integer that can be used to compare this pointer to another one.
Definition Pointer.cpp:360
const BlockPointer & asBlockPointer() const
Definition Pointer.h:448
void initialize() const
Initializes a field.
Definition Pointer.cpp:485
const Record * getRecord() const
Returns the record descriptor of a class.
Definition Pointer.h:471
Block * getGlobal(unsigned Idx)
Returns the value of a global.
Definition Program.h:71
const void * getNativePointer(unsigned Idx)
Returns the value of a marshalled native pointer.
Definition Program.cpp:30
Pointer getPtrGlobal(unsigned Idx) const
Returns a pointer to a global.
Definition Program.cpp:109
Structure/Class descriptor.
Definition Record.h:25
const RecordDecl * getDecl() const
Returns the underlying declaration.
Definition Record.h:53
llvm::iterator_range< LocalVectorTy::const_reverse_iterator > locals_reverse() const
Definition Function.h:55
Describes the statement/declaration an opcode was generated from.
Definition Source.h:73
bool checkingForUndefinedBehavior() const
Are we checking an expression for overflow?
Definition State.h:103
EvaluationMode EvalMode
Definition State.h:171
OptionalDiagnostic Note(SourceLocation Loc, diag::kind DiagId)
Add a note to a prior diagnostic.
Definition State.cpp:63
DiagnosticBuilder report(SourceLocation Loc, diag::kind DiagId)
Directly reports a diagnostic message.
Definition State.cpp:74
OptionalDiagnostic FFDiag(SourceLocation Loc, diag::kind DiagId=diag::note_invalid_subexpr_in_const_expr, unsigned ExtraNotes=0)
Diagnose that the evaluation could not be folded (FF => FoldFailure)
Definition State.cpp:21
OptionalDiagnostic CCEDiag(SourceLocation Loc, diag::kind DiagId=diag::note_invalid_subexpr_in_const_expr, unsigned ExtraNotes=0)
Diagnose that the evaluation does not produce a C++11 core constant expression.
Definition State.cpp:42
bool checkingPotentialConstantExpression() const
Are we checking whether the expression is a potential constant expression?
Definition State.h:99
#define bool
Definition gpuintrin.h:32
bool arePotentiallyOverlappingStringLiterals(const Pointer &LHS, const Pointer &RHS)
Definition Interp.cpp:2128
bool EndSpeculation(InterpState &S, CodePtr OpPC)
Definition Interp.h:3289
static bool ShiftFixedPoint(InterpState &S, CodePtr OpPC, bool Left)
Definition Interp.h:3016
bool GetPtrFieldPop(InterpState &S, CodePtr OpPC, uint32_t Off)
Definition Interp.cpp:1452
bool InitPop(InterpState &S, CodePtr OpPC)
Definition Interp.h:2109
bool Shr(InterpState &S, CodePtr OpPC)
Definition Interp.h:2979
bool InitGlobalTemp(InterpState &S, CodePtr OpPC, uint32_t I, const LifetimeExtendedTemporaryDecl *Temp)
1) Converts the value on top of the stack to an APValue 2) Sets that APValue on \Temp 3) Initializes ...
Definition Interp.h:1528
bool CheckDestruction(InterpState &S, CodePtr OpPC)
Definition Interp.h:3682
bool ArrayElemPop(InterpState &S, CodePtr OpPC, uint32_t Index)
Definition Interp.h:3161
bool CastPointerIntegralAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth)
Definition Interp.cpp:2058
bool PopCC(InterpState &S, CodePtr OpPC)
Definition Interp.h:3303
bool ArrayElem(InterpState &S, CodePtr OpPC, uint32_t Index)
Definition Interp.h:3149
bool GT(InterpState &S, CodePtr OpPC)
Definition Interp.h:1282
bool CastPointerIntegralAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth)
Definition Interp.cpp:2045
bool CheckInit(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Checks if a value can be initialized.
Definition Interp.cpp:909
static bool CastFloatingIntegralAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth, uint32_t FPOI)
Definition Interp.h:2605
bool GetMemberPtrBase(InterpState &S, CodePtr OpPC)
Definition Interp.h:3243
bool GetThisField(InterpState &S, CodePtr OpPC, uint32_t I)
Definition Interp.h:1440
bool PreInc(InterpState &S, CodePtr OpPC, bool CanOverflow)
Definition Interp.h:887
bool NarrowPtr(InterpState &S, CodePtr OpPC)
Definition Interp.h:3066
bool GetMemberPtrBasePop(InterpState &S, CodePtr OpPC, int32_t Off)
Definition Interp.h:1838
bool InitThisField(InterpState &S, CodePtr OpPC, uint32_t I)
Definition Interp.h:1560
bool BitCastPrim(InterpState &S, CodePtr OpPC, bool TargetIsUCharOrByte, uint32_t ResultBitWidth, const llvm::fltSemantics *Sem, const Type *TargetType)
Definition Interp.h:3590
Floating ReadArg< Floating >(InterpState &S, CodePtr &OpPC)
Definition Interp.h:3711
bool Incf(InterpState &S, CodePtr OpPC, uint32_t FPOI)
Definition Interp.h:950
bool SideEffect(InterpState &S, CodePtr OpPC)
Definition Interp.h:3311
static bool ZeroIntAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth)
Definition Interp.h:2784
bool DoShift(InterpState &S, CodePtr OpPC, LT &LHS, RT &RHS, LT *Result)
Definition Interp.h:2855
bool GetParam(InterpState &S, CodePtr OpPC, uint32_t I)
Definition Interp.h:1377
bool EndLifetimePop(InterpState &S, CodePtr OpPC)
Ends the lifetime of the pop'd pointer.
Definition Interp.cpp:1866
bool Sub(InterpState &S, CodePtr OpPC)
Definition Interp.h:425
bool GetTypeidPtr(InterpState &S, CodePtr OpPC, const Type *TypeInfoType)
Definition Interp.cpp:2094
bool Mulf(InterpState &S, CodePtr OpPC, uint32_t FPOI)
Definition Interp.h:453
bool InitElemPop(InterpState &S, CodePtr OpPC, uint32_t Idx)
The same as InitElem, but pops the pointer as well.
Definition Interp.h:2157
bool StoreBitField(InterpState &S, CodePtr OpPC)
Definition Interp.h:2033
bool LT(InterpState &S, CodePtr OpPC)
Definition Interp.h:1267
bool CheckDowncast(InterpState &S, CodePtr OpPC, const Pointer &Ptr, uint32_t Offset)
Checks if the dowcast using the given offset is possible with the given pointer.
Definition Interp.cpp:552
bool CheckNewDeleteForms(InterpState &S, CodePtr OpPC, DynamicAllocator::Form AllocForm, DynamicAllocator::Form DeleteForm, const Descriptor *D, const Expr *NewExpr)
Diagnose mismatched new[]/delete or new/delete[] pairs.
Definition Interp.cpp:1103
bool BitCast(InterpState &S, CodePtr OpPC)
Definition Interp.h:3663
bool LoadPop(InterpState &S, CodePtr OpPC)
Definition Interp.h:1950
bool Null(InterpState &S, CodePtr OpPC, uint64_t Value, const Descriptor *Desc)
Definition Interp.h:2793
bool CheckGlobalLoad(InterpState &S, CodePtr OpPC, const Block *B)
Checks a direct load of a primitive value from a global or local variable.
Definition Interp.cpp:738
static llvm::RoundingMode getRoundingMode(FPOptions FPO)
Definition Interp.h:406
static bool IncPtr(InterpState &S, CodePtr OpPC)
Definition Interp.h:2386
bool CheckDeclRef(InterpState &S, CodePtr OpPC, const DeclRefExpr *DR)
We aleady know the given DeclRefExpr is invalid for some reason, now figure out why and print appropr...
Definition Interp.cpp:1149
bool EndLifetime(InterpState &S, CodePtr OpPC)
Ends the lifetime of the peek'd pointer.
Definition Interp.cpp:1852
bool GetTypeid(InterpState &S, CodePtr OpPC, const Type *TypePtr, const Type *TypeInfoType)
Typeid support.
Definition Interp.cpp:2088
bool Dup(InterpState &S, CodePtr OpPC)
Definition Interp.h:1301
bool CheckThis(InterpState &S, CodePtr OpPC, const Pointer &This)
Checks the 'this' pointer.
Definition Interp.cpp:1030
bool SetField(InterpState &S, CodePtr OpPC, uint32_t I)
Definition Interp.h:1408
bool CheckNonNullArg(InterpState &S, CodePtr OpPC)
Definition Interp.h:3406
bool SetThreeWayComparisonField(InterpState &S, CodePtr OpPC, const Pointer &Ptr, const APSInt &IntValue)
Sets the given integral value to the pointer, which is of a std::{weak,partial,strong}...
static bool IncDecPtrHelper(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Definition Interp.h:2361
bool FinishInitActivate(InterpState &S, CodePtr OpPC)
Definition Interp.h:1868
bool GetPtrLocal(InterpState &S, CodePtr OpPC, uint32_t I)
Definition Interp.h:1727
bool Addf(InterpState &S, CodePtr OpPC, uint32_t FPOI)
Definition Interp.h:413
bool CheckDivRem(InterpState &S, CodePtr OpPC, const T &LHS, const T &RHS)
Checks if Div/Rem operation on LHS and RHS is valid.
Definition Interp.h:216
bool CheckConstant(InterpState &S, CodePtr OpPC, const Descriptor *Desc)
Checks if the Descriptor is of a constexpr or const global variable.
Definition Interp.cpp:448
static bool IsOpaqueConstantCall(const CallExpr *E)
Definition Interp.h:1065
bool CheckDecl(InterpState &S, CodePtr OpPC, const VarDecl *VD)
Definition Interp.h:3458
bool CheckPointerToIntegralCast(InterpState &S, CodePtr OpPC, const Pointer &Ptr, unsigned BitWidth)
Definition Interp.cpp:2025
bool AddSubMulHelper(InterpState &S, CodePtr OpPC, unsigned Bits, const T &LHS, const T &RHS)
Definition Interp.h:357
bool GetPtrField(InterpState &S, CodePtr OpPC, uint32_t Off)
1) Peeks a Pointer 2) Pushes Pointer.atField(Off) on the stack
Definition Interp.cpp:1447
bool StoreActivate(InterpState &S, CodePtr OpPC)
Definition Interp.h:2002
bool Div(InterpState &S, CodePtr OpPC)
1) Pops the RHS from the stack.
Definition Interp.h:692
bool CheckMutable(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Checks if a pointer points to a mutable field.
Definition Interp.cpp:594
bool GetFnPtr(InterpState &S, CodePtr OpPC, const Function *Func)
Definition Interp.h:3220
bool FinishInitActivatePop(InterpState &S, CodePtr OpPC)
Definition Interp.h:1877
bool GetGlobalUnchecked(InterpState &S, CodePtr OpPC, uint32_t I)
Same as GetGlobal, but without the checks.
Definition Interp.h:1481
bool SubPtr(InterpState &S, CodePtr OpPC)
1) Pops a Pointer from the stack.
Definition Interp.h:2408
bool CheckSubobject(InterpState &S, CodePtr OpPC, const Pointer &Ptr, CheckSubobjectKind CSK)
Checks if Ptr is a one-past-the-end pointer.
Definition Interp.cpp:541
bool GetPtrGlobal(InterpState &S, CodePtr OpPC, uint32_t I)
Definition Interp.h:1740
static bool Activate(InterpState &S, CodePtr OpPC)
Definition Interp.h:1984
bool handleFixedPointOverflow(InterpState &S, CodePtr OpPC, const FixedPoint &FP)
Definition Interp.cpp:2004
bool Mulc(InterpState &S, CodePtr OpPC)
Definition Interp.h:467
bool RetVoid(InterpState &S, CodePtr &PC)
Definition Interp.h:334
bool ArrayElemPtr(InterpState &S, CodePtr OpPC)
Definition Interp.h:3090
bool NE(InterpState &S, CodePtr OpPC)
Definition Interp.h:1260
bool NoRet(InterpState &S, CodePtr OpPC)
Definition Interp.h:3056
bool GetIntPtr(InterpState &S, CodePtr OpPC, const Descriptor *Desc)
Definition Interp.h:3227
llvm::FixedPointSemantics FixedPointSemantics
Definition Interp.h:41
bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr, AccessKinds AK)
Checks if a value can be loaded from a block.
Definition Interp.cpp:793
static bool FnPtrCast(InterpState &S, CodePtr OpPC)
Definition Interp.h:2723
static bool ZeroIntAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth)
Definition Interp.h:2776
bool Shl(InterpState &S, CodePtr OpPC)
Definition Interp.h:2998
bool RVOPtr(InterpState &S, CodePtr OpPC)
Definition Interp.h:2842
bool CastPointerIntegral(InterpState &S, CodePtr OpPC)
Definition Interp.h:2653
constexpr bool isPtrType(PrimType T)
Definition PrimType.h:85
bool DecfPop(InterpState &S, CodePtr OpPC, uint32_t FPOI)
Definition Interp.h:974
bool InterpretOffsetOf(InterpState &S, CodePtr OpPC, const OffsetOfExpr *E, ArrayRef< int64_t > ArrayIndices, int64_t &Result)
Interpret an offsetof operation.
bool SubOffset(InterpState &S, CodePtr OpPC)
Definition Interp.h:2348
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition PrimType.h:185
bool BitXor(InterpState &S, CodePtr OpPC)
1) Pops the RHS from the stack.
Definition Interp.h:648
bool CheckBCPResult(InterpState &S, const Pointer &Ptr)
Definition Interp.cpp:308
bool CheckRange(InterpState &S, CodePtr OpPC, const Pointer &Ptr, AccessKinds AK)
Checks if a pointer is in range.
Definition Interp.cpp:519
bool CastAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth)
Definition Interp.h:2548
bool ExpandPtr(InterpState &S, CodePtr OpPC)
Definition Interp.h:3072
bool Store(InterpState &S, CodePtr OpPC)
Definition Interp.h:1961
bool Divc(InterpState &S, CodePtr OpPC)
Definition Interp.h:524
bool DoBitCastPtr(InterpState &S, CodePtr OpPC, const Pointer &FromPtr, Pointer &ToPtr)
bool GetField(InterpState &S, CodePtr OpPC, uint32_t I)
1) Peeks a pointer on the stack 2) Pushes the value of the pointer's field on the stack
Definition Interp.h:1394
bool ArrayElemPtrPop(InterpState &S, CodePtr OpPC)
Definition Interp.h:3120
bool This(InterpState &S, CodePtr OpPC)
Definition Interp.h:2814
bool InitScope(InterpState &S, CodePtr OpPC, uint32_t I)
Definition Interp.h:2491
bool CheckDynamicMemoryAllocation(InterpState &S, CodePtr OpPC)
Checks if dynamic memory allocation is available in the current language mode.
Definition Interp.cpp:1094
bool InitField(InterpState &S, CodePtr OpPC, uint32_t I)
1) Pops the value from the stack 2) Peeks a pointer from the stack 3) Pushes the value to field I of ...
Definition Interp.h:1629
llvm::APFloat APFloat
Definition Floating.h:27
bool CmpHelperEQ(InterpState &S, CodePtr OpPC, CompareFn Fn)
Definition Interp.h:1018
T ReadArg(InterpState &S, CodePtr &OpPC)
Definition Interp.h:3702
bool CheckLive(InterpState &S, CodePtr OpPC, const Pointer &Ptr, AccessKinds AK)
Checks if a pointer is live and accessible.
Definition Interp.cpp:414
bool CastFloatingIntegral(InterpState &S, CodePtr OpPC, uint32_t FPOI)
Definition Interp.h:2575
bool ArrayDecay(InterpState &S, CodePtr OpPC)
Just takes a pointer and checks if it's an incomplete array type.
Definition Interp.h:3196
bool PushCC(InterpState &S, CodePtr OpPC, bool Value)
Definition Interp.h:3299
bool GetPtrDerivedPop(InterpState &S, CodePtr OpPC, uint32_t Off, bool NullOK, const Type *TargetType)
Definition Interp.h:1760
bool DiagTypeid(InterpState &S, CodePtr OpPC)
Definition Interp.cpp:2120
bool CheckFinalLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
This is not used by any of the opcodes directly.
Definition Interp.cpp:840
bool InitGlobalTempComp(InterpState &S, CodePtr OpPC, const LifetimeExtendedTemporaryDecl *Temp)
1) Converts the value on top of the stack to an APValue 2) Sets that APValue on \Temp 3) Initialized ...
Definition Interp.h:1547
bool CheckBitCast(InterpState &S, CodePtr OpPC, bool HasIndeterminateBits, bool TargetIsUCharOrByte)
Definition Interp.cpp:2071
bool GetLocal(InterpState &S, CodePtr OpPC, uint32_t I)
Definition Interp.h:1355
bool OffsetOf(InterpState &S, CodePtr OpPC, const OffsetOfExpr *E)
Definition Interp.h:3391
bool BitAnd(InterpState &S, CodePtr OpPC)
1) Pops the RHS from the stack.
Definition Interp.h:608
bool CheckShift(InterpState &S, CodePtr OpPC, const LT &LHS, const RT &RHS, unsigned Bits)
Checks if the shift operation is legal.
Definition Interp.h:169
static bool handleOverflow(InterpState &S, CodePtr OpPC, const T &SrcValue)
Definition Interp.h:153
llvm::APInt APInt
Definition FixedPoint.h:19
FixedPoint ReadArg< FixedPoint >(InterpState &S, CodePtr &OpPC)
Definition Interp.h:3746
static bool CastFloatingFixedPoint(InterpState &S, CodePtr OpPC, uint32_t FPS)
Definition Interp.h:2684
void diagnoseEnumValue(InterpState &S, CodePtr OpPC, const EnumDecl *ED, const APSInt &Value)
Definition Interp.cpp:1357
bool StartLifetime(InterpState &S, CodePtr OpPC)
Definition Interp.cpp:1821
bool LE(InterpState &S, CodePtr OpPC)
Definition Interp.h:1274
bool CheckNewTypeMismatchArray(InterpState &S, CodePtr OpPC, const Expr *E)
Definition Interp.h:3583
bool Zero(InterpState &S, CodePtr OpPC)
Definition Interp.h:2771
bool InitThisBitField(InterpState &S, CodePtr OpPC, const Record::Field *F, uint32_t FieldOffset)
Definition Interp.h:1591
bool Unsupported(InterpState &S, CodePtr OpPC)
Definition Interp.h:3272
bool InvalidDeclRef(InterpState &S, CodePtr OpPC, const DeclRefExpr *DR, bool InitializerFailed)
Definition Interp.h:3347
bool CheckStore(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Checks if a value can be stored in a block.
Definition Interp.cpp:871
bool DecPop(InterpState &S, CodePtr OpPC, bool CanOverflow)
1) Pops a pointer from the stack 2) Load the value from the pointer 3) Writes the value decreased by ...
Definition Interp.h:913
bool CheckNull(InterpState &S, CodePtr OpPC, const Pointer &Ptr, CheckSubobjectKind CSK)
Checks if a pointer is null.
Definition Interp.cpp:508
bool CheckDeleteSource(InterpState &S, CodePtr OpPC, const Expr *Source, const Pointer &Ptr)
Check the source of the pointer passed to delete/delete[] has actually been heap allocated by us.
Definition Interp.cpp:1121
bool CheckFloatResult(InterpState &S, CodePtr OpPC, const Floating &Result, APFloat::opStatus Status, FPOptions FPO)
Checks if the result of a floating-point operation is valid in the current context.
Definition Interp.cpp:1047
bool CastFP(InterpState &S, CodePtr OpPC, const llvm::fltSemantics *Sem, llvm::RoundingMode RM)
1) Pops a Floating from the stack.
Definition Interp.h:2509
ComparisonCategoryResult Compare(const T &X, const T &Y)
Helper to compare two comparable types.
Definition Primitives.h:25
bool IncDecHelper(InterpState &S, CodePtr OpPC, const Pointer &Ptr, bool CanOverflow)
Definition Interp.h:801
PrimType
Enumeration of the primitive types of the VM.
Definition PrimType.h:34
bool InitThisBitFieldActivate(InterpState &S, CodePtr OpPC, const Record::Field *F, uint32_t FieldOffset)
Definition Interp.h:1608
bool SetThisField(InterpState &S, CodePtr OpPC, uint32_t I)
Definition Interp.h:1454
bool StoreBitFieldPop(InterpState &S, CodePtr OpPC)
Definition Interp.h:2048
bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const CallExpr *Call, uint32_t BuiltinID)
Interpret a builtin function.
bool CallVar(InterpState &S, CodePtr OpPC, const Function *Func, uint32_t VarArgSize)
Definition Interp.cpp:1504
static bool DecPtr(InterpState &S, CodePtr OpPC)
Definition Interp.h:2395
constexpr bool needsAlloc()
Definition PrimType.h:125
static bool CheckAllocations(InterpState &S, CodePtr OpPC)
Definition Interp.h:3573
bool Alloc(InterpState &S, CodePtr OpPC, const Descriptor *Desc)
Definition Interp.h:3477
bool InvalidShuffleVectorIndex(InterpState &S, CodePtr OpPC, uint32_t Index)
Definition Interp.cpp:2017
bool ToMemberPtr(InterpState &S, CodePtr OpPC)
Definition Interp.h:2199
bool CheckDummy(InterpState &S, CodePtr OpPC, const Block *B, AccessKinds AK)
Checks if a pointer is a dummy pointer.
Definition Interp.cpp:1154
static bool CastIntegralFixedPoint(InterpState &S, CodePtr OpPC, uint32_t FPS)
Definition Interp.h:2668
bool Rem(InterpState &S, CodePtr OpPC)
1) Pops the RHS from the stack.
Definition Interp.h:669
bool VirtBaseHelper(InterpState &S, CodePtr OpPC, const RecordDecl *Decl, const Pointer &Ptr)
Definition Interp.h:1903
bool CheckNewTypeMismatch(InterpState &S, CodePtr OpPC, const Expr *E, std::optional< uint64_t > ArraySize)
Check if the initializer and storage types of a placement-new expression match.
Definition Interp.cpp:1879
bool GetMemberPtr(InterpState &S, CodePtr OpPC, const ValueDecl *D)
Definition Interp.h:3238
bool Dump(InterpState &S, CodePtr OpPC)
Definition Interp.h:1888
bool SizelessVectorElementSize(InterpState &S, CodePtr OpPC)
Definition Interp.h:3362
static bool PtrPtrCast(InterpState &S, CodePtr OpPC, bool SrcIsVoidPtr)
Definition Interp.h:2731
bool CheckLiteralType(InterpState &S, CodePtr OpPC, const Type *T)
Definition Interp.cpp:1381
bool IsNonNull(InterpState &S, CodePtr OpPC)
Definition Interp.h:2802
bool GetPtrThisField(InterpState &S, CodePtr OpPC, uint32_t Off)
Definition Interp.h:1750
bool ConstFloat(InterpState &S, CodePtr OpPC, const Floating &F)
Definition Interp.h:1343
bool CheckArray(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Checks if the array is offsetable.
Definition Interp.cpp:406
bool InitThisFieldActivate(InterpState &S, CodePtr OpPC, uint32_t I)
Definition Interp.h:1574
bool GetPtrBase(InterpState &S, CodePtr OpPC, uint32_t Off)
Definition Interp.h:1794
bool SetParam(InterpState &S, CodePtr OpPC, uint32_t I)
Definition Interp.h:1386
bool StoreActivatePop(InterpState &S, CodePtr OpPC)
Definition Interp.h:2018
bool GetMemberPtrDecl(InterpState &S, CodePtr OpPC)
Definition Interp.h:3253
bool Comp(InterpState &S, CodePtr OpPC)
1) Pops the value from the stack.
Definition Interp.h:985
static bool CastFixedPointFloating(InterpState &S, CodePtr OpPC, const llvm::fltSemantics *Sem)
Definition Interp.h:2700
bool Divf(InterpState &S, CodePtr OpPC, uint32_t FPOI)
Definition Interp.h:718
bool FinishInitGlobal(InterpState &S, CodePtr OpPC)
Definition Interp.cpp:2255
bool DecayPtr(InterpState &S, CodePtr OpPC)
OldPtr -> Integer -> NewPtr.
Definition Interp.h:3434
static bool ActivateThisField(InterpState &S, CodePtr OpPC, uint32_t I)
Definition Interp.h:1991
bool CheckActive(InterpState &S, CodePtr OpPC, const Pointer &Ptr, AccessKinds AK)
Definition Interp.cpp:328
bool GetPtrVirtBasePop(InterpState &S, CodePtr OpPC, const RecordDecl *D)
Definition Interp.h:1914
bool StorePop(InterpState &S, CodePtr OpPC)
Definition Interp.h:1973
void cleanupAfterFunctionCall(InterpState &S, CodePtr OpPC, const Function *Func)
Definition Interp.cpp:261
bool SetLocal(InterpState &S, CodePtr OpPC, uint32_t I)
1) Pops the value from the stack.
Definition Interp.h:1371
bool FinishInit(InterpState &S, CodePtr OpPC)
Definition Interp.h:1861
static bool CastFloatingIntegralAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth, uint32_t FPOI)
Definition Interp.h:2626
bool Mul(InterpState &S, CodePtr OpPC)
Definition Interp.h:445
bool InitElem(InterpState &S, CodePtr OpPC, uint32_t Idx)
1) Pops the value from the stack 2) Peeks a pointer and gets its index \Idx 3) Sets the value on the ...
Definition Interp.h:2123
bool Destroy(InterpState &S, CodePtr OpPC, uint32_t I)
Definition Interp.h:2463
bool Pop(InterpState &S, CodePtr OpPC)
Definition Interp.h:1307
size_t primSize(PrimType Type)
Returns the size of a primitive type in bytes.
Definition PrimType.cpp:23
bool InitBitField(InterpState &S, CodePtr OpPC, const Record::Field *F)
Definition Interp.h:1660
bool Dec(InterpState &S, CodePtr OpPC, bool CanOverflow)
1) Pops a pointer from the stack 2) Load the value from the pointer 3) Writes the value decreased by ...
Definition Interp.h:900
bool StoreBitFieldActivate(InterpState &S, CodePtr OpPC)
Definition Interp.h:2063
bool CheckPseudoDtor(InterpState &S, CodePtr OpPC)
Definition Interp.h:3371
bool Free(InterpState &S, CodePtr OpPC, bool DeleteIsArrayForm, bool IsGlobalDelete)
Definition Interp.cpp:1258
bool PreDec(InterpState &S, CodePtr OpPC, bool CanOverflow)
Definition Interp.h:922
bool InvalidNewDeleteExpr(InterpState &S, CodePtr OpPC, const Expr *E)
Definition Interp.cpp:1960
bool CheckArraySize(InterpState &S, CodePtr OpPC, uint64_t NumElems)
Definition Interp.h:3687
bool CallBI(InterpState &S, CodePtr OpPC, const CallExpr *CE, uint32_t BuiltinID)
Definition Interp.cpp:1737
bool CheckLocalLoad(InterpState &S, CodePtr OpPC, const Block *B)
Definition Interp.cpp:771
bool FinishInitPop(InterpState &S, CodePtr OpPC)
Definition Interp.h:1854
bool Neg(InterpState &S, CodePtr OpPC)
Definition Interp.h:749
bool StartSpeculation(InterpState &S, CodePtr OpPC)
Definition Interp.h:3279
bool CheckExtern(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Checks if the variable has externally defined storage.
Definition Interp.cpp:389
std::optional< Pointer > OffsetHelper(InterpState &S, CodePtr OpPC, const T &Offset, const Pointer &Ptr, bool IsPointerArith=false)
Definition Interp.h:2222
bool BitOr(InterpState &S, CodePtr OpPC)
1) Pops the RHS from the stack.
Definition Interp.h:628
llvm::function_ref< bool(ComparisonCategoryResult)> CompareFn
Definition Interp.h:1003
bool Inv(InterpState &S, CodePtr OpPC)
Definition Interp.h:738
bool Load(InterpState &S, CodePtr OpPC)
Definition Interp.h:1939
bool isConstexprUnknown(const Pointer &P)
Definition Interp.cpp:298
bool SetGlobal(InterpState &S, CodePtr OpPC, uint32_t I)
Definition Interp.h:1494
bool Cast(InterpState &S, CodePtr OpPC)
Definition Interp.h:2500
bool StoreBitFieldActivatePop(InterpState &S, CodePtr OpPC)
Definition Interp.h:2080
bool Inc(InterpState &S, CodePtr OpPC, bool CanOverflow)
1) Pops a pointer from the stack 2) Load the value from the pointer 3) Writes the value increased by ...
Definition Interp.h:865
bool EQ(InterpState &S, CodePtr OpPC)
Definition Interp.h:1228
bool IncfPop(InterpState &S, CodePtr OpPC, uint32_t FPOI)
Definition Interp.h:958
bool GetPtrBasePop(InterpState &S, CodePtr OpPC, uint32_t Off, bool NullOK)
Definition Interp.h:1815
bool GetFieldPop(InterpState &S, CodePtr OpPC, uint32_t I)
1) Pops a pointer from the stack 2) Pushes the value of the pointer's field on the stack
Definition Interp.h:1426
bool Add(InterpState &S, CodePtr OpPC)
Definition Interp.h:398
bool CmpHelperEQ< MemberPointer >(InterpState &S, CodePtr OpPC, CompareFn Fn)
Definition Interp.h:1186
bool AddOffset(InterpState &S, CodePtr OpPC)
Definition Interp.h:2333
bool Const(InterpState &S, CodePtr OpPC, const T &Arg)
Definition Interp.h:1332
bool DoMemcpy(InterpState &S, CodePtr OpPC, const Pointer &Src, Pointer &Dest)
Copy the contents of Src into Dest.
bool DiagnoseUninitialized(InterpState &S, CodePtr OpPC, const Pointer &Ptr, AccessKinds AK)
Definition Interp.cpp:662
bool IncPop(InterpState &S, CodePtr OpPC, bool CanOverflow)
1) Pops a pointer from the stack 2) Load the value from the pointer 3) Writes the value increased by ...
Definition Interp.h:878
bool Memcpy(InterpState &S, CodePtr OpPC)
Definition Interp.h:2189
bool GE(InterpState &S, CodePtr OpPC)
Definition Interp.h:1289
bool DoBitCast(InterpState &S, CodePtr OpPC, const Pointer &Ptr, std::byte *Buff, Bits BitWidth, Bits FullBitWidth, bool &HasIndeterminateBits)
bool CallPtr(InterpState &S, CodePtr OpPC, uint32_t ArgSize, const CallExpr *CE)
Definition Interp.cpp:1748
bool CmpHelperEQ< Pointer >(InterpState &S, CodePtr OpPC, CompareFn Fn)
Definition Interp.h:1077
static bool CastFixedPointIntegral(InterpState &S, CodePtr OpPC)
Definition Interp.h:2710
constexpr bool isIntegralType(PrimType T)
Definition PrimType.h:124
bool CallVirt(InterpState &S, CodePtr OpPC, const Function *Func, uint32_t VarArgSize)
Definition Interp.cpp:1639
bool CastIntegralFloating(InterpState &S, CodePtr OpPC, const llvm::fltSemantics *Sem, uint32_t FPOI)
Definition Interp.h:2560
bool CmpHelper(InterpState &S, CodePtr OpPC, CompareFn Fn)
Definition Interp.h:1006
bool CheckConst(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Checks if a pointer points to const storage.
Definition Interp.cpp:572
bool CastFixedPoint(InterpState &S, CodePtr OpPC, uint32_t FPS)
Definition Interp.h:2518
bool GetPtrParam(InterpState &S, CodePtr OpPC, uint32_t I)
Definition Interp.h:1732
bool AllocCN(InterpState &S, CodePtr OpPC, const Descriptor *ElementDesc, bool IsNoThrow)
Definition Interp.h:3532
bool GetGlobal(InterpState &S, CodePtr OpPC, uint32_t I)
Definition Interp.h:1469
bool Interpret(InterpState &S)
Interpreter entry point.
Definition Interp.cpp:2271
bool Subf(InterpState &S, CodePtr OpPC, uint32_t FPOI)
Definition Interp.h:433
bool GetPtrThisVirtBase(InterpState &S, CodePtr OpPC, const RecordDecl *D)
Definition Interp.h:1923
llvm::APSInt APSInt
Definition FixedPoint.h:20
bool InitGlobal(InterpState &S, CodePtr OpPC, uint32_t I)
Definition Interp.h:1500
bool InvalidCast(InterpState &S, CodePtr OpPC, CastKind Kind, bool Fatal)
Same here, but only for casts.
Definition Interp.h:3316
bool DoShiftAP(InterpState &S, CodePtr OpPC, const APSInt &LHS, APSInt RHS, LT *Result)
A version of DoShift that works on IntegralAP.
Definition Interp.h:2935
bool CastMemberPtrPtr(InterpState &S, CodePtr OpPC)
Definition Interp.h:2207
bool Ret(InterpState &S, CodePtr &PC)
Definition Interp.h:312
bool InitFieldActivate(InterpState &S, CodePtr OpPC, uint32_t I)
Definition Interp.h:1644
bool CheckDestructor(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Definition Interp.cpp:1478
bool Flip(InterpState &S, CodePtr OpPC)
[Value1, Value2] -> [Value2, Value1]
Definition Interp.h:1314
bool CMP3(InterpState &S, CodePtr OpPC, const ComparisonCategoryInfo *CmpInfo)
Definition Interp.h:1235
bool InitBitFieldActivate(InterpState &S, CodePtr OpPC, const Record::Field *F)
Definition Interp.h:1691
bool CastAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth)
Like Cast(), but we cast to an arbitrary-bitwidth integral, so we need to know what bitwidth the resu...
Definition Interp.h:2536
bool Invalid(InterpState &S, CodePtr OpPC)
Just emit a diagnostic.
Definition Interp.h:3265
bool CmpHelper< Pointer >(InterpState &S, CodePtr OpPC, CompareFn Fn)
Definition Interp.h:1023
bool Decf(InterpState &S, CodePtr OpPC, uint32_t FPOI)
Definition Interp.h:966
bool Assume(InterpState &S, CodePtr OpPC)
Definition Interp.h:3378
bool GetPtrThisBase(InterpState &S, CodePtr OpPC, uint32_t Off)
Definition Interp.h:1844
bool IncDecFloatHelper(InterpState &S, CodePtr OpPC, const Pointer &Ptr, uint32_t FPOI)
Definition Interp.h:930
static bool IsConstantContext(InterpState &S, CodePtr OpPC)
Definition Interp.h:3568
bool AllocN(InterpState &S, CodePtr OpPC, PrimType T, const Expr *Source, bool IsNoThrow)
Definition Interp.h:3492
bool CheckEnumValue(InterpState &S, CodePtr OpPC, const EnumDecl *ED)
Definition Interp.h:3421
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
Definition Address.h:330
@ TSCS_unspecified
Definition Specifiers.h:236
ComparisonCategoryResult
An enumeration representing the possible results of a three-way comparison.
CheckSubobjectKind
The order of this enum is important for diagnostics.
Definition State.h:42
@ CSK_ArrayToPointer
Definition State.h:46
@ CSK_Derived
Definition State.h:44
@ CSK_Base
Definition State.h:43
@ CSK_ArrayIndex
Definition State.h:47
@ CSK_Field
Definition State.h:45
@ Result
The result type of a method or function.
Definition TypeBase.h:905
AccessKinds
Kinds of access we can perform on an object, for diagnostics.
Definition State.h:26
@ AK_Increment
Definition State.h:30
@ AK_Read
Definition State.h:27
@ AK_Assign
Definition State.h:29
@ AK_Decrement
Definition State.h:31
const FunctionProtoType * T
@ ConstantFold
Fold the expression to a constant.
Definition State.h:67
U cast(CodeGen::Address addr)
Definition Address.h:327
SmallVectorImpl< PartialDiagnosticAt > * Diag
Diag - If this is non-null, it will be filled in with a stack of notes indicating why evaluation fail...
Definition Expr.h:633
A quantity in bits.
size_t getQuantity() const
unsigned Base
Start of the current subfield.
Definition Pointer.h:39
Block * Pointee
The block the pointer is pointing to.
Definition Pointer.h:37
Describes a memory block created by an allocation site.
Definition Descriptor.h:122
unsigned getNumElems() const
Returns the number of elements stored in the block.
Definition Descriptor.h:249
unsigned getSize() const
Returns the size of the object without metadata.
Definition Descriptor.h:231
static constexpr unsigned MaxArrayElemBytes
Maximum number of bytes to be used for array elements.
Definition Descriptor.h:148
QualType getType() const
const Decl * asDecl() const
Definition Descriptor.h:210
SourceLocation getLocation() const
PrimType getPrimType() const
Definition Descriptor.h:236
const Expr * asExpr() const
Definition Descriptor.h:211
bool isArray() const
Checks if the descriptor is of an array.
Definition Descriptor.h:266
Descriptor used for global variables.
Definition Descriptor.h:51
const Descriptor * Desc
Definition Pointer.h:47
Mapping from primitive types to their representation.
Definition PrimType.h:134