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 // In the unlikely event that we're initializing the first item of
2131 // a non-array, skip the atIndex().
2132 if (Idx == 0 && !Ptr.getFieldDesc()->isArray()) {
2133 Ptr.initialize();
2134 new (&Ptr.deref<T>()) T(Value);
2135 return true;
2136 }
2137
2138 const Pointer &ElemPtr = Ptr.atIndex(Idx);
2139 if (!CheckInit(S, OpPC, ElemPtr))
2140 return false;
2141 ElemPtr.initialize();
2142 new (&ElemPtr.deref<T>()) T(Value);
2143 return true;
2144}
2145
2146/// The same as InitElem, but pops the pointer as well.
2147template <PrimType Name, class T = typename PrimConv<Name>::T>
2148bool InitElemPop(InterpState &S, CodePtr OpPC, uint32_t Idx) {
2149 const T &Value = S.Stk.pop<T>();
2150 const Pointer &Ptr = S.Stk.pop<Pointer>();
2151 if (Ptr.isUnknownSizeArray())
2152 return false;
2153
2154 // In the unlikely event that we're initializing the first item of
2155 // a non-array, skip the atIndex().
2156 if (Idx == 0 && !Ptr.getFieldDesc()->isArray()) {
2157 Ptr.initialize();
2158 new (&Ptr.deref<T>()) T(Value);
2159 return true;
2160 }
2161
2162 const Pointer &ElemPtr = Ptr.atIndex(Idx);
2163 if (!CheckInit(S, OpPC, ElemPtr))
2164 return false;
2165 ElemPtr.initialize();
2166 new (&ElemPtr.deref<T>()) T(Value);
2167 return true;
2168}
2169
2170inline bool Memcpy(InterpState &S, CodePtr OpPC) {
2171 const Pointer &Src = S.Stk.pop<Pointer>();
2172 Pointer &Dest = S.Stk.peek<Pointer>();
2173
2174 if (!CheckLoad(S, OpPC, Src))
2175 return false;
2176
2177 return DoMemcpy(S, OpPC, Src, Dest);
2178}
2179
2180inline bool ToMemberPtr(InterpState &S, CodePtr OpPC) {
2181 const auto &Member = S.Stk.pop<MemberPointer>();
2182 const auto &Base = S.Stk.pop<Pointer>();
2183
2184 S.Stk.push<MemberPointer>(Member.takeInstance(Base));
2185 return true;
2186}
2187
2188inline bool CastMemberPtrPtr(InterpState &S, CodePtr OpPC) {
2189 const auto &MP = S.Stk.pop<MemberPointer>();
2190
2191 if (std::optional<Pointer> Ptr = MP.toPointer(S.Ctx)) {
2192 S.Stk.push<Pointer>(*Ptr);
2193 return true;
2194 }
2195 return Invalid(S, OpPC);
2196}
2197
2198//===----------------------------------------------------------------------===//
2199// AddOffset, SubOffset
2200//===----------------------------------------------------------------------===//
2201
2202template <class T, ArithOp Op>
2203std::optional<Pointer> OffsetHelper(InterpState &S, CodePtr OpPC,
2204 const T &Offset, const Pointer &Ptr,
2205 bool IsPointerArith = false) {
2206 // A zero offset does not change the pointer.
2207 if (Offset.isZero())
2208 return Ptr;
2209
2210 if (IsPointerArith && !CheckNull(S, OpPC, Ptr, CSK_ArrayIndex)) {
2211 // The CheckNull will have emitted a note already, but we only
2212 // abort in C++, since this is fine in C.
2213 if (S.getLangOpts().CPlusPlus)
2214 return std::nullopt;
2215 }
2216
2217 // Arrays of unknown bounds cannot have pointers into them.
2218 if (!CheckArray(S, OpPC, Ptr))
2219 return std::nullopt;
2220
2221 // This is much simpler for integral pointers, so handle them first.
2222 if (Ptr.isIntegralPointer()) {
2223 uint64_t V = Ptr.getIntegerRepresentation();
2224 uint64_t O = static_cast<uint64_t>(Offset) * Ptr.elemSize();
2225 if constexpr (Op == ArithOp::Add)
2226 return Pointer(V + O, Ptr.asIntPointer().Desc);
2227 else
2228 return Pointer(V - O, Ptr.asIntPointer().Desc);
2229 } else if (Ptr.isFunctionPointer()) {
2230 uint64_t O = static_cast<uint64_t>(Offset);
2231 uint64_t N;
2232 if constexpr (Op == ArithOp::Add)
2233 N = Ptr.getByteOffset() + O;
2234 else
2235 N = Ptr.getByteOffset() - O;
2236
2237 if (N > 1)
2238 S.CCEDiag(S.Current->getSource(OpPC), diag::note_constexpr_array_index)
2239 << N << /*non-array*/ true << 0;
2240 return Pointer(Ptr.asFunctionPointer().getFunction(), N);
2241 }
2242
2243 assert(Ptr.isBlockPointer());
2244
2245 uint64_t MaxIndex = static_cast<uint64_t>(Ptr.getNumElems());
2246 uint64_t Index;
2247 if (Ptr.isOnePastEnd())
2248 Index = MaxIndex;
2249 else
2250 Index = Ptr.getIndex();
2251
2252 bool Invalid = false;
2253 // Helper to report an invalid offset, computed as APSInt.
2254 auto DiagInvalidOffset = [&]() -> void {
2255 const unsigned Bits = Offset.bitWidth();
2256 APSInt APOffset(Offset.toAPSInt().extend(Bits + 2), /*IsUnsigend=*/false);
2257 APSInt APIndex(APInt(Bits + 2, Index, /*IsSigned=*/true),
2258 /*IsUnsigned=*/false);
2259 APSInt NewIndex =
2260 (Op == ArithOp::Add) ? (APIndex + APOffset) : (APIndex - APOffset);
2261 S.CCEDiag(S.Current->getSource(OpPC), diag::note_constexpr_array_index)
2262 << NewIndex << /*array*/ static_cast<int>(!Ptr.inArray()) << MaxIndex;
2263 Invalid = true;
2264 };
2265
2266 if (Ptr.isBlockPointer()) {
2267 uint64_t IOffset = static_cast<uint64_t>(Offset);
2268 uint64_t MaxOffset = MaxIndex - Index;
2269
2270 if constexpr (Op == ArithOp::Add) {
2271 // If the new offset would be negative, bail out.
2272 if (Offset.isNegative() && (Offset.isMin() || -IOffset > Index))
2273 DiagInvalidOffset();
2274
2275 // If the new offset would be out of bounds, bail out.
2276 if (Offset.isPositive() && IOffset > MaxOffset)
2277 DiagInvalidOffset();
2278 } else {
2279 // If the new offset would be negative, bail out.
2280 if (Offset.isPositive() && Index < IOffset)
2281 DiagInvalidOffset();
2282
2283 // If the new offset would be out of bounds, bail out.
2284 if (Offset.isNegative() && (Offset.isMin() || -IOffset > MaxOffset))
2285 DiagInvalidOffset();
2286 }
2287 }
2288
2289 if (Invalid && S.getLangOpts().CPlusPlus)
2290 return std::nullopt;
2291
2292 // Offset is valid - compute it on unsigned.
2293 int64_t WideIndex = static_cast<int64_t>(Index);
2294 int64_t WideOffset = static_cast<int64_t>(Offset);
2295 int64_t Result;
2296 if constexpr (Op == ArithOp::Add)
2297 Result = WideIndex + WideOffset;
2298 else
2299 Result = WideIndex - WideOffset;
2300
2301 // When the pointer is one-past-end, going back to index 0 is the only
2302 // useful thing we can do. Any other index has been diagnosed before and
2303 // we don't get here.
2304 if (Result == 0 && Ptr.isOnePastEnd()) {
2305 if (Ptr.getFieldDesc()->isArray())
2306 return Ptr.atIndex(0);
2307 return Pointer(Ptr.asBlockPointer().Pointee, Ptr.asBlockPointer().Base);
2308 }
2309
2310 return Ptr.atIndex(static_cast<uint64_t>(Result));
2311}
2312
2313template <PrimType Name, class T = typename PrimConv<Name>::T>
2315 const T &Offset = S.Stk.pop<T>();
2316 Pointer Ptr = S.Stk.pop<Pointer>();
2317 if (Ptr.isBlockPointer())
2318 Ptr = Ptr.expand();
2319
2320 if (std::optional<Pointer> Result = OffsetHelper<T, ArithOp::Add>(
2321 S, OpPC, Offset, Ptr, /*IsPointerArith=*/true)) {
2322 S.Stk.push<Pointer>(*Result);
2323 return true;
2324 }
2325 return false;
2326}
2327
2328template <PrimType Name, class T = typename PrimConv<Name>::T>
2330 const T &Offset = S.Stk.pop<T>();
2331 const Pointer &Ptr = S.Stk.pop<Pointer>();
2332
2333 if (std::optional<Pointer> Result = OffsetHelper<T, ArithOp::Sub>(
2334 S, OpPC, Offset, Ptr, /*IsPointerArith=*/true)) {
2335 S.Stk.push<Pointer>(*Result);
2336 return true;
2337 }
2338 return false;
2339}
2340
2341template <ArithOp Op>
2342static inline bool IncDecPtrHelper(InterpState &S, CodePtr OpPC,
2343 const Pointer &Ptr) {
2344 if (Ptr.isDummy())
2345 return false;
2346
2347 using OneT = Integral<8, false>;
2348
2349 const Pointer &P = Ptr.deref<Pointer>();
2350 if (!CheckNull(S, OpPC, P, CSK_ArrayIndex))
2351 return false;
2352
2353 // Get the current value on the stack.
2354 S.Stk.push<Pointer>(P);
2355
2356 // Now the current Ptr again and a constant 1.
2357 OneT One = OneT::from(1);
2358 if (std::optional<Pointer> Result =
2359 OffsetHelper<OneT, Op>(S, OpPC, One, P, /*IsPointerArith=*/true)) {
2360 // Store the new value.
2361 Ptr.deref<Pointer>() = *Result;
2362 return true;
2363 }
2364 return false;
2365}
2366
2367static inline bool IncPtr(InterpState &S, CodePtr OpPC) {
2368 const Pointer &Ptr = S.Stk.pop<Pointer>();
2369
2370 if (!Ptr.isInitialized())
2371 return DiagnoseUninitialized(S, OpPC, Ptr, AK_Increment);
2372
2373 return IncDecPtrHelper<ArithOp::Add>(S, OpPC, Ptr);
2374}
2375
2376static inline bool DecPtr(InterpState &S, CodePtr OpPC) {
2377 const Pointer &Ptr = S.Stk.pop<Pointer>();
2378
2379 if (!Ptr.isInitialized())
2380 return DiagnoseUninitialized(S, OpPC, Ptr, AK_Decrement);
2381
2382 return IncDecPtrHelper<ArithOp::Sub>(S, OpPC, Ptr);
2383}
2384
2385/// 1) Pops a Pointer from the stack.
2386/// 2) Pops another Pointer from the stack.
2387/// 3) Pushes the difference of the indices of the two pointers on the stack.
2388template <PrimType Name, class T = typename PrimConv<Name>::T>
2389inline bool SubPtr(InterpState &S, CodePtr OpPC) {
2390 const Pointer &LHS = S.Stk.pop<Pointer>();
2391 const Pointer &RHS = S.Stk.pop<Pointer>();
2392
2393 if (!Pointer::hasSameBase(LHS, RHS) && S.getLangOpts().CPlusPlus) {
2394 S.FFDiag(S.Current->getSource(OpPC),
2395 diag::note_constexpr_pointer_arith_unspecified)
2397 << RHS.toDiagnosticString(S.getASTContext());
2398 return false;
2399 }
2400
2401 if (LHS == RHS) {
2402 S.Stk.push<T>();
2403 return true;
2404 }
2405
2406 for (const Pointer &P : {LHS, RHS}) {
2407 if (P.isZeroSizeArray()) {
2408 QualType PtrT = P.getType();
2409 while (auto *AT = dyn_cast<ArrayType>(PtrT))
2410 PtrT = AT->getElementType();
2411
2413 PtrT, APInt::getZero(1), nullptr, ArraySizeModifier::Normal, 0);
2414 S.FFDiag(S.Current->getSource(OpPC),
2415 diag::note_constexpr_pointer_subtraction_zero_size)
2416 << ArrayTy;
2417
2418 return false;
2419 }
2420 }
2421
2422 int64_t A64 =
2423 LHS.isBlockPointer()
2424 ? (LHS.isElementPastEnd() ? LHS.getNumElems() : LHS.getIndex())
2426
2427 int64_t B64 =
2428 RHS.isBlockPointer()
2429 ? (RHS.isElementPastEnd() ? RHS.getNumElems() : RHS.getIndex())
2430 : RHS.getIntegerRepresentation();
2431
2432 int64_t R64 = A64 - B64;
2433 if (static_cast<int64_t>(T::from(R64)) != R64)
2434 return handleOverflow(S, OpPC, R64);
2435
2436 S.Stk.push<T>(T::from(R64));
2437 return true;
2438}
2439
2440//===----------------------------------------------------------------------===//
2441// Destroy
2442//===----------------------------------------------------------------------===//
2443
2444inline bool Destroy(InterpState &S, CodePtr OpPC, uint32_t I) {
2445 assert(S.Current->getFunction());
2446
2447 // FIXME: We iterate the scope once here and then again in the destroy() call
2448 // below.
2449 for (auto &Local : S.Current->getFunction()->getScope(I).locals_reverse()) {
2450 const Pointer &Ptr = S.Current->getLocalPointer(Local.Offset);
2451
2452 if (Ptr.getLifetime() == Lifetime::Ended) {
2453 // Try to use the declaration for better diagnostics
2454 if (const Decl *D = Ptr.getDeclDesc()->asDecl()) {
2455 auto *ND = cast<NamedDecl>(D);
2456 S.FFDiag(ND->getLocation(),
2457 diag::note_constexpr_destroy_out_of_lifetime)
2458 << ND->getNameAsString();
2459 } else {
2460 S.FFDiag(Ptr.getDeclDesc()->getLocation(),
2461 diag::note_constexpr_destroy_out_of_lifetime)
2463 }
2464 return false;
2465 }
2466 }
2467
2468 S.Current->destroy(I);
2469 return true;
2470}
2471
2472inline bool InitScope(InterpState &S, CodePtr OpPC, uint32_t I) {
2473 S.Current->initScope(I);
2474 return true;
2475}
2476
2477//===----------------------------------------------------------------------===//
2478// Cast, CastFP
2479//===----------------------------------------------------------------------===//
2480
2481template <PrimType TIn, PrimType TOut> bool Cast(InterpState &S, CodePtr OpPC) {
2482 using T = typename PrimConv<TIn>::T;
2483 using U = typename PrimConv<TOut>::T;
2484 S.Stk.push<U>(U::from(S.Stk.pop<T>()));
2485 return true;
2486}
2487
2488/// 1) Pops a Floating from the stack.
2489/// 2) Pushes a new floating on the stack that uses the given semantics.
2490inline bool CastFP(InterpState &S, CodePtr OpPC, const llvm::fltSemantics *Sem,
2491 llvm::RoundingMode RM) {
2492 Floating F = S.Stk.pop<Floating>();
2493 Floating Result = S.allocFloat(*Sem);
2494 F.toSemantics(Sem, RM, &Result);
2495 S.Stk.push<Floating>(Result);
2496 return true;
2497}
2498
2499inline bool CastFixedPoint(InterpState &S, CodePtr OpPC, uint32_t FPS) {
2500 FixedPointSemantics TargetSemantics =
2501 FixedPointSemantics::getFromOpaqueInt(FPS);
2502 const auto &Source = S.Stk.pop<FixedPoint>();
2503
2504 bool Overflow;
2505 FixedPoint Result = Source.toSemantics(TargetSemantics, &Overflow);
2506
2507 if (Overflow && !handleFixedPointOverflow(S, OpPC, Result))
2508 return false;
2509
2511 return true;
2512}
2513
2514/// Like Cast(), but we cast to an arbitrary-bitwidth integral, so we need
2515/// to know what bitwidth the result should be.
2516template <PrimType Name, class T = typename PrimConv<Name>::T>
2517bool CastAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
2518 auto Result = S.allocAP<IntegralAP<false>>(BitWidth);
2519 // Copy data.
2520 {
2521 APInt Source = S.Stk.pop<T>().toAPSInt().extOrTrunc(BitWidth);
2522 Result.copy(Source);
2523 }
2525 return true;
2526}
2527
2528template <PrimType Name, class T = typename PrimConv<Name>::T>
2529bool CastAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
2530 auto Result = S.allocAP<IntegralAP<true>>(BitWidth);
2531 // Copy data.
2532 {
2533 APInt Source = S.Stk.pop<T>().toAPSInt().extOrTrunc(BitWidth);
2534 Result.copy(Source);
2535 }
2537 return true;
2538}
2539
2540template <PrimType Name, class T = typename PrimConv<Name>::T>
2542 const llvm::fltSemantics *Sem, uint32_t FPOI) {
2543 const T &From = S.Stk.pop<T>();
2544 APSInt FromAP = From.toAPSInt();
2545
2547 Floating Result = S.allocFloat(*Sem);
2548 auto Status =
2549 Floating::fromIntegral(FromAP, *Sem, getRoundingMode(FPO), &Result);
2550 S.Stk.push<Floating>(Result);
2551
2552 return CheckFloatResult(S, OpPC, Result, Status, FPO);
2553}
2554
2555template <PrimType Name, class T = typename PrimConv<Name>::T>
2556bool CastFloatingIntegral(InterpState &S, CodePtr OpPC, uint32_t FPOI) {
2557 const Floating &F = S.Stk.pop<Floating>();
2558
2559 if constexpr (std::is_same_v<T, Boolean>) {
2560 S.Stk.push<T>(T(F.isNonZero()));
2561 return true;
2562 } else {
2563 APSInt Result(std::max(8u, T::bitWidth()),
2564 /*IsUnsigned=*/!T::isSigned());
2565 auto Status = F.convertToInteger(Result);
2566
2567 // Float-to-Integral overflow check.
2568 if ((Status & APFloat::opStatus::opInvalidOp)) {
2569 const Expr *E = S.Current->getExpr(OpPC);
2570 QualType Type = E->getType();
2571
2572 S.CCEDiag(E, diag::note_constexpr_overflow) << F.getAPFloat() << Type;
2573 if (S.noteUndefinedBehavior()) {
2574 S.Stk.push<T>(T(Result));
2575 return true;
2576 }
2577 return false;
2578 }
2579
2581 S.Stk.push<T>(T(Result));
2582 return CheckFloatResult(S, OpPC, F, Status, FPO);
2583 }
2584}
2585
2586static inline bool CastFloatingIntegralAP(InterpState &S, CodePtr OpPC,
2587 uint32_t BitWidth, uint32_t FPOI) {
2588 const Floating &F = S.Stk.pop<Floating>();
2589
2590 APSInt Result(BitWidth, /*IsUnsigned=*/true);
2591 auto Status = F.convertToInteger(Result);
2592
2593 // Float-to-Integral overflow check.
2594 if ((Status & APFloat::opStatus::opInvalidOp) && F.isFinite())
2595 return handleOverflow(S, OpPC, F.getAPFloat());
2596
2598
2599 auto ResultAP = S.allocAP<IntegralAP<false>>(BitWidth);
2600 ResultAP.copy(Result);
2601
2602 S.Stk.push<IntegralAP<false>>(ResultAP);
2603
2604 return CheckFloatResult(S, OpPC, F, Status, FPO);
2605}
2606
2607static inline bool CastFloatingIntegralAPS(InterpState &S, CodePtr OpPC,
2608 uint32_t BitWidth, uint32_t FPOI) {
2609 const Floating &F = S.Stk.pop<Floating>();
2610
2611 APSInt Result(BitWidth, /*IsUnsigned=*/false);
2612 auto Status = F.convertToInteger(Result);
2613
2614 // Float-to-Integral overflow check.
2615 if ((Status & APFloat::opStatus::opInvalidOp) && F.isFinite())
2616 return handleOverflow(S, OpPC, F.getAPFloat());
2617
2619
2620 auto ResultAP = S.allocAP<IntegralAP<true>>(BitWidth);
2621 ResultAP.copy(Result);
2622
2623 S.Stk.push<IntegralAP<true>>(ResultAP);
2624
2625 return CheckFloatResult(S, OpPC, F, Status, FPO);
2626}
2627
2628bool CheckPointerToIntegralCast(InterpState &S, CodePtr OpPC,
2629 const Pointer &Ptr, unsigned BitWidth);
2630bool CastPointerIntegralAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth);
2631bool CastPointerIntegralAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth);
2632
2633template <PrimType Name, class T = typename PrimConv<Name>::T>
2635 const Pointer &Ptr = S.Stk.pop<Pointer>();
2636
2637 S.CCEDiag(S.Current->getSource(OpPC), diag::note_constexpr_invalid_cast)
2638 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
2639 << S.getLangOpts().CPlusPlus << S.Current->getRange(OpPC);
2640
2641 if (!CheckPointerToIntegralCast(S, OpPC, Ptr, T::bitWidth()))
2642 return Invalid(S, OpPC);
2643
2644 S.Stk.push<T>(T::from(Ptr.getIntegerRepresentation()));
2645 return true;
2646}
2647
2648template <PrimType Name, class T = typename PrimConv<Name>::T>
2649static inline bool CastIntegralFixedPoint(InterpState &S, CodePtr OpPC,
2650 uint32_t FPS) {
2651 const T &Int = S.Stk.pop<T>();
2652
2653 FixedPointSemantics Sem = FixedPointSemantics::getFromOpaqueInt(FPS);
2654
2655 bool Overflow;
2656 FixedPoint Result = FixedPoint::from(Int.toAPSInt(), Sem, &Overflow);
2657
2658 if (Overflow && !handleFixedPointOverflow(S, OpPC, Result))
2659 return false;
2660
2662 return true;
2663}
2664
2665static inline bool CastFloatingFixedPoint(InterpState &S, CodePtr OpPC,
2666 uint32_t FPS) {
2667 const auto &Float = S.Stk.pop<Floating>();
2668
2669 FixedPointSemantics Sem = FixedPointSemantics::getFromOpaqueInt(FPS);
2670
2671 bool Overflow;
2672 FixedPoint Result = FixedPoint::from(Float.getAPFloat(), Sem, &Overflow);
2673
2674 if (Overflow && !handleFixedPointOverflow(S, OpPC, Result))
2675 return false;
2676
2678 return true;
2679}
2680
2681static inline bool CastFixedPointFloating(InterpState &S, CodePtr OpPC,
2682 const llvm::fltSemantics *Sem) {
2683 const auto &Fixed = S.Stk.pop<FixedPoint>();
2684 Floating Result = S.allocFloat(*Sem);
2685 Result.copy(Fixed.toFloat(Sem));
2686 S.Stk.push<Floating>(Result);
2687 return true;
2688}
2689
2690template <PrimType Name, class T = typename PrimConv<Name>::T>
2691static inline bool CastFixedPointIntegral(InterpState &S, CodePtr OpPC) {
2692 const auto &Fixed = S.Stk.pop<FixedPoint>();
2693
2694 bool Overflow;
2695 APSInt Int = Fixed.toInt(T::bitWidth(), T::isSigned(), &Overflow);
2696
2697 if (Overflow && !handleOverflow(S, OpPC, Int))
2698 return false;
2699
2700 S.Stk.push<T>(Int);
2701 return true;
2702}
2703
2704static inline bool FnPtrCast(InterpState &S, CodePtr OpPC) {
2705 const SourceInfo &E = S.Current->getSource(OpPC);
2706 S.CCEDiag(E, diag::note_constexpr_invalid_cast)
2707 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
2708 << S.getLangOpts().CPlusPlus << S.Current->getRange(OpPC);
2709 return true;
2710}
2711
2712static inline bool PtrPtrCast(InterpState &S, CodePtr OpPC, bool SrcIsVoidPtr) {
2713 const auto &Ptr = S.Stk.peek<Pointer>();
2714
2715 if (SrcIsVoidPtr && S.getLangOpts().CPlusPlus) {
2716 bool HasValidResult = !Ptr.isZero();
2717
2718 if (HasValidResult) {
2719 if (S.getStdAllocatorCaller("allocate"))
2720 return true;
2721
2722 const auto &E = cast<CastExpr>(S.Current->getExpr(OpPC));
2723 if (S.getLangOpts().CPlusPlus26 &&
2724 S.getASTContext().hasSimilarType(Ptr.getType(),
2725 E->getType()->getPointeeType()))
2726 return true;
2727
2728 S.CCEDiag(E, diag::note_constexpr_invalid_void_star_cast)
2729 << E->getSubExpr()->getType() << S.getLangOpts().CPlusPlus26
2730 << Ptr.getType().getCanonicalType() << E->getType()->getPointeeType();
2731 } else if (!S.getLangOpts().CPlusPlus26) {
2732 const SourceInfo &E = S.Current->getSource(OpPC);
2733 S.CCEDiag(E, diag::note_constexpr_invalid_cast)
2734 << diag::ConstexprInvalidCastKind::CastFrom << "'void *'"
2735 << S.Current->getRange(OpPC);
2736 }
2737 } else {
2738 const SourceInfo &E = S.Current->getSource(OpPC);
2739 S.CCEDiag(E, diag::note_constexpr_invalid_cast)
2740 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
2741 << S.getLangOpts().CPlusPlus << S.Current->getRange(OpPC);
2742 }
2743
2744 return true;
2745}
2746
2747//===----------------------------------------------------------------------===//
2748// Zero, Nullptr
2749//===----------------------------------------------------------------------===//
2750
2751template <PrimType Name, class T = typename PrimConv<Name>::T>
2752bool Zero(InterpState &S, CodePtr OpPC) {
2753 S.Stk.push<T>(T::zero());
2754 return true;
2755}
2756
2757static inline bool ZeroIntAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
2758 auto Result = S.allocAP<IntegralAP<false>>(BitWidth);
2759 if (!Result.singleWord())
2760 std::memset(Result.Memory, 0, Result.numWords() * sizeof(uint64_t));
2762 return true;
2763}
2764
2765static inline bool ZeroIntAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
2766 auto Result = S.allocAP<IntegralAP<true>>(BitWidth);
2767 if (!Result.singleWord())
2768 std::memset(Result.Memory, 0, Result.numWords() * sizeof(uint64_t));
2770 return true;
2771}
2772
2773template <PrimType Name, class T = typename PrimConv<Name>::T>
2774inline bool Null(InterpState &S, CodePtr OpPC, uint64_t Value,
2775 const Descriptor *Desc) {
2776 // FIXME(perf): This is a somewhat often-used function and the value of a
2777 // null pointer is almost always 0.
2778 S.Stk.push<T>(Value, Desc);
2779 return true;
2780}
2781
2782template <PrimType Name, class T = typename PrimConv<Name>::T>
2783inline bool IsNonNull(InterpState &S, CodePtr OpPC) {
2784 const auto &P = S.Stk.pop<T>();
2785 if (P.isWeak())
2786 return false;
2787 S.Stk.push<Boolean>(Boolean::from(!P.isZero()));
2788 return true;
2789}
2790
2791//===----------------------------------------------------------------------===//
2792// This, ImplicitThis
2793//===----------------------------------------------------------------------===//
2794
2795inline bool This(InterpState &S, CodePtr OpPC) {
2796 // Cannot read 'this' in this mode.
2798 return false;
2799 }
2800
2801 const Pointer &This = S.Current->getThis();
2802 if (!CheckThis(S, OpPC, This))
2803 return false;
2804
2805 // Ensure the This pointer has been cast to the correct base.
2806 if (!This.isDummy()) {
2808 if (!This.isTypeidPointer()) {
2809 [[maybe_unused]] const Record *R = This.getRecord();
2810 if (!R)
2811 R = This.narrow().getRecord();
2812 assert(R);
2813 assert(R->getDecl() ==
2815 ->getParent());
2816 }
2817 }
2818
2819 S.Stk.push<Pointer>(This);
2820 return true;
2821}
2822
2823inline bool RVOPtr(InterpState &S, CodePtr OpPC) {
2824 assert(S.Current->getFunction()->hasRVO());
2826 return false;
2827 S.Stk.push<Pointer>(S.Current->getRVOPtr());
2828 return true;
2829}
2830
2831//===----------------------------------------------------------------------===//
2832// Shr, Shl
2833//===----------------------------------------------------------------------===//
2834
2835template <class LT, class RT, ShiftDir Dir>
2836inline bool DoShift(InterpState &S, CodePtr OpPC, LT &LHS, RT &RHS,
2837 LT *Result) {
2838 static_assert(!needsAlloc<LT>());
2839 const unsigned Bits = LHS.bitWidth();
2840
2841 // OpenCL 6.3j: shift values are effectively % word size of LHS.
2842 if (S.getLangOpts().OpenCL)
2843 RT::bitAnd(RHS, RT::from(LHS.bitWidth() - 1, RHS.bitWidth()),
2844 RHS.bitWidth(), &RHS);
2845
2846 if (RHS.isNegative()) {
2847 // During constant-folding, a negative shift is an opposite shift. Such a
2848 // shift is not a constant expression.
2849 const SourceInfo &Loc = S.Current->getSource(OpPC);
2850 S.CCEDiag(Loc, diag::note_constexpr_negative_shift) << RHS.toAPSInt();
2851 if (!S.noteUndefinedBehavior())
2852 return false;
2853 RHS = -RHS;
2854 return DoShift<LT, RT,
2856 S, OpPC, LHS, RHS, Result);
2857 }
2858
2859 if (!CheckShift<Dir>(S, OpPC, LHS, RHS, Bits))
2860 return false;
2861
2862 // Limit the shift amount to Bits - 1. If this happened,
2863 // it has already been diagnosed by CheckShift() above,
2864 // but we still need to handle it.
2865 // Note that we have to be extra careful here since we're doing the shift in
2866 // any case, but we need to adjust the shift amount or the way we do the shift
2867 // for the potential error cases.
2868 typename LT::AsUnsigned R;
2869 unsigned MaxShiftAmount = LHS.bitWidth() - 1;
2870 if constexpr (Dir == ShiftDir::Left) {
2871 if (Compare(RHS, RT::from(MaxShiftAmount, RHS.bitWidth())) ==
2873 if (LHS.isNegative())
2874 R = LT::AsUnsigned::zero(LHS.bitWidth());
2875 else {
2876 RHS = RT::from(LHS.countLeadingZeros(), RHS.bitWidth());
2877 LT::AsUnsigned::shiftLeft(LT::AsUnsigned::from(LHS),
2878 LT::AsUnsigned::from(RHS, Bits), Bits, &R);
2879 }
2880 } else if (LHS.isNegative()) {
2881 if (LHS.isMin()) {
2882 R = LT::AsUnsigned::zero(LHS.bitWidth());
2883 } else {
2884 // If the LHS is negative, perform the cast and invert the result.
2885 typename LT::AsUnsigned LHSU = LT::AsUnsigned::from(-LHS);
2886 LT::AsUnsigned::shiftLeft(LHSU, LT::AsUnsigned::from(RHS, Bits), Bits,
2887 &R);
2888 R = -R;
2889 }
2890 } else {
2891 // The good case, a simple left shift.
2892 LT::AsUnsigned::shiftLeft(LT::AsUnsigned::from(LHS),
2893 LT::AsUnsigned::from(RHS, Bits), Bits, &R);
2894 }
2895 S.Stk.push<LT>(LT::from(R));
2896 return true;
2897 }
2898
2899 // Right shift.
2900 if (Compare(RHS, RT::from(MaxShiftAmount, RHS.bitWidth())) ==
2902 R = LT::AsUnsigned::from(-1);
2903 } else {
2904 // Do the shift on potentially signed LT, then convert to unsigned type.
2905 LT A;
2906 LT::shiftRight(LHS, LT::from(RHS, Bits), Bits, &A);
2907 R = LT::AsUnsigned::from(A);
2908 }
2909
2910 S.Stk.push<LT>(LT::from(R));
2911 return true;
2912}
2913
2914/// A version of DoShift that works on IntegralAP.
2915template <class LT, class RT, ShiftDir Dir>
2916inline bool DoShiftAP(InterpState &S, CodePtr OpPC, const APSInt &LHS,
2917 APSInt RHS, LT *Result) {
2918 const unsigned Bits = LHS.getBitWidth();
2919
2920 // OpenCL 6.3j: shift values are effectively % word size of LHS.
2921 if (S.getLangOpts().OpenCL)
2922 RHS &=
2923 APSInt(llvm::APInt(RHS.getBitWidth(), static_cast<uint64_t>(Bits - 1)),
2924 RHS.isUnsigned());
2925
2926 if (RHS.isNegative()) {
2927 // During constant-folding, a negative shift is an opposite shift. Such a
2928 // shift is not a constant expression.
2929 const SourceInfo &Loc = S.Current->getSource(OpPC);
2930 S.CCEDiag(Loc, diag::note_constexpr_negative_shift) << RHS; //.toAPSInt();
2931 if (!S.noteUndefinedBehavior())
2932 return false;
2933 return DoShiftAP<LT, RT,
2935 S, OpPC, LHS, -RHS, Result);
2936 }
2937
2938 if (!CheckShift<Dir>(S, OpPC, static_cast<LT>(LHS), static_cast<RT>(RHS),
2939 Bits))
2940 return false;
2941
2942 unsigned SA = (unsigned)RHS.getLimitedValue(Bits - 1);
2943 if constexpr (Dir == ShiftDir::Left) {
2944 if constexpr (needsAlloc<LT>())
2945 Result->copy(LHS << SA);
2946 else
2947 *Result = LT(LHS << SA);
2948 } else {
2949 if constexpr (needsAlloc<LT>())
2950 Result->copy(LHS >> SA);
2951 else
2952 *Result = LT(LHS >> SA);
2953 }
2954
2955 S.Stk.push<LT>(*Result);
2956 return true;
2957}
2958
2959template <PrimType NameL, PrimType NameR>
2960inline bool Shr(InterpState &S, CodePtr OpPC) {
2961 using LT = typename PrimConv<NameL>::T;
2962 using RT = typename PrimConv<NameR>::T;
2963 auto RHS = S.Stk.pop<RT>();
2964 auto LHS = S.Stk.pop<LT>();
2965
2966 if constexpr (needsAlloc<LT>() || needsAlloc<RT>()) {
2967 LT Result;
2968 if constexpr (needsAlloc<LT>())
2969 Result = S.allocAP<LT>(LHS.bitWidth());
2970 return DoShiftAP<LT, RT, ShiftDir::Right>(S, OpPC, LHS.toAPSInt(),
2971 RHS.toAPSInt(), &Result);
2972 } else {
2973 LT Result;
2974 return DoShift<LT, RT, ShiftDir::Right>(S, OpPC, LHS, RHS, &Result);
2975 }
2976}
2977
2978template <PrimType NameL, PrimType NameR>
2979inline bool Shl(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::Left>(S, OpPC, LHS.toAPSInt(),
2990 RHS.toAPSInt(), &Result);
2991 } else {
2992 LT Result;
2993 return DoShift<LT, RT, ShiftDir::Left>(S, OpPC, LHS, RHS, &Result);
2994 }
2995}
2996
2997static inline bool ShiftFixedPoint(InterpState &S, CodePtr OpPC, bool Left) {
2998 const auto &RHS = S.Stk.pop<FixedPoint>();
2999 const auto &LHS = S.Stk.pop<FixedPoint>();
3000 llvm::FixedPointSemantics LHSSema = LHS.getSemantics();
3001
3002 unsigned ShiftBitWidth =
3003 LHSSema.getWidth() - (unsigned)LHSSema.hasUnsignedPadding() - 1;
3004
3005 // Embedded-C 4.1.6.2.2:
3006 // The right operand must be nonnegative and less than the total number
3007 // of (nonpadding) bits of the fixed-point operand ...
3008 if (RHS.isNegative()) {
3009 S.CCEDiag(S.Current->getLocation(OpPC), diag::note_constexpr_negative_shift)
3010 << RHS.toAPSInt();
3011 } else if (static_cast<unsigned>(RHS.toAPSInt().getLimitedValue(
3012 ShiftBitWidth)) != RHS.toAPSInt()) {
3013 const Expr *E = S.Current->getExpr(OpPC);
3014 S.CCEDiag(E, diag::note_constexpr_large_shift)
3015 << RHS.toAPSInt() << E->getType() << ShiftBitWidth;
3016 }
3017
3019 if (Left) {
3020 if (FixedPoint::shiftLeft(LHS, RHS, ShiftBitWidth, &Result) &&
3022 return false;
3023 } else {
3024 if (FixedPoint::shiftRight(LHS, RHS, ShiftBitWidth, &Result) &&
3026 return false;
3027 }
3028
3030 return true;
3031}
3032
3033//===----------------------------------------------------------------------===//
3034// NoRet
3035//===----------------------------------------------------------------------===//
3036
3037inline bool NoRet(InterpState &S, CodePtr OpPC) {
3038 SourceLocation EndLoc = S.Current->getCallee()->getEndLoc();
3039 S.FFDiag(EndLoc, diag::note_constexpr_no_return);
3040 return false;
3041}
3042
3043//===----------------------------------------------------------------------===//
3044// NarrowPtr, ExpandPtr
3045//===----------------------------------------------------------------------===//
3046
3047inline bool NarrowPtr(InterpState &S, CodePtr OpPC) {
3048 const Pointer &Ptr = S.Stk.pop<Pointer>();
3049 S.Stk.push<Pointer>(Ptr.narrow());
3050 return true;
3051}
3052
3053inline bool ExpandPtr(InterpState &S, CodePtr OpPC) {
3054 const Pointer &Ptr = S.Stk.pop<Pointer>();
3055 if (Ptr.isBlockPointer())
3056 S.Stk.push<Pointer>(Ptr.expand());
3057 else
3058 S.Stk.push<Pointer>(Ptr);
3059 return true;
3060}
3061
3062// 1) Pops an integral value from the stack
3063// 2) Peeks a pointer
3064// 3) Pushes a new pointer that's a narrowed array
3065// element of the peeked pointer with the value
3066// from 1) added as offset.
3067//
3068// This leaves the original pointer on the stack and pushes a new one
3069// with the offset applied and narrowed.
3070template <PrimType Name, class T = typename PrimConv<Name>::T>
3071inline bool ArrayElemPtr(InterpState &S, CodePtr OpPC) {
3072 const T &Offset = S.Stk.pop<T>();
3073 const Pointer &Ptr = S.Stk.peek<Pointer>();
3074
3075 if (!Ptr.isZero() && !Offset.isZero()) {
3076 if (!CheckArray(S, OpPC, Ptr))
3077 return false;
3078 }
3079
3080 if (Offset.isZero()) {
3081 if (Ptr.getFieldDesc()->isArray() && Ptr.getIndex() == 0) {
3082 S.Stk.push<Pointer>(Ptr.atIndex(0).narrow());
3083 return true;
3084 }
3085 S.Stk.push<Pointer>(Ptr);
3086 return true;
3087 }
3088
3089 assert(!Offset.isZero());
3090
3091 if (std::optional<Pointer> Result =
3092 OffsetHelper<T, ArithOp::Add>(S, OpPC, Offset, Ptr)) {
3093 S.Stk.push<Pointer>(Result->narrow());
3094 return true;
3095 }
3096
3097 return false;
3098}
3099
3100template <PrimType Name, class T = typename PrimConv<Name>::T>
3101inline bool ArrayElemPtrPop(InterpState &S, CodePtr OpPC) {
3102 const T &Offset = S.Stk.pop<T>();
3103 const Pointer &Ptr = S.Stk.pop<Pointer>();
3104
3105 if (!Ptr.isZero() && !Offset.isZero()) {
3106 if (!CheckArray(S, OpPC, Ptr))
3107 return false;
3108 }
3109
3110 if (Offset.isZero()) {
3111 if (Ptr.getFieldDesc()->isArray() && Ptr.getIndex() == 0) {
3112 S.Stk.push<Pointer>(Ptr.atIndex(0).narrow());
3113 return true;
3114 }
3115 S.Stk.push<Pointer>(Ptr);
3116 return true;
3117 }
3118
3119 assert(!Offset.isZero());
3120
3121 if (std::optional<Pointer> Result =
3122 OffsetHelper<T, ArithOp::Add>(S, OpPC, Offset, Ptr)) {
3123 S.Stk.push<Pointer>(Result->narrow());
3124 return true;
3125 }
3126 return false;
3127}
3128
3129template <PrimType Name, class T = typename PrimConv<Name>::T>
3130inline bool ArrayElem(InterpState &S, CodePtr OpPC, uint32_t Index) {
3131 const Pointer &Ptr = S.Stk.peek<Pointer>();
3132
3133 if (!CheckLoad(S, OpPC, Ptr))
3134 return false;
3135
3136 assert(Ptr.atIndex(Index).getFieldDesc()->getPrimType() == Name);
3137 S.Stk.push<T>(Ptr.elem<T>(Index));
3138 return true;
3139}
3140
3141template <PrimType Name, class T = typename PrimConv<Name>::T>
3142inline bool ArrayElemPop(InterpState &S, CodePtr OpPC, uint32_t Index) {
3143 const Pointer &Ptr = S.Stk.pop<Pointer>();
3144
3145 if (!CheckLoad(S, OpPC, Ptr))
3146 return false;
3147
3148 assert(Ptr.atIndex(Index).getFieldDesc()->getPrimType() == Name);
3149 S.Stk.push<T>(Ptr.elem<T>(Index));
3150 return true;
3151}
3152
3153template <PrimType Name, class T = typename PrimConv<Name>::T>
3154inline bool CopyArray(InterpState &S, CodePtr OpPC, uint32_t SrcIndex,
3155 uint32_t DestIndex, uint32_t Size) {
3156 const auto &SrcPtr = S.Stk.pop<Pointer>();
3157 const auto &DestPtr = S.Stk.peek<Pointer>();
3158
3159 for (uint32_t I = 0; I != Size; ++I) {
3160 const Pointer &SP = SrcPtr.atIndex(SrcIndex + I);
3161
3162 if (!CheckLoad(S, OpPC, SP))
3163 return false;
3164
3165 const Pointer &DP = DestPtr.atIndex(DestIndex + I);
3166 DP.deref<T>() = SP.deref<T>();
3167 DP.initialize();
3168 }
3169 return true;
3170}
3171
3172/// Just takes a pointer and checks if it's an incomplete
3173/// array type.
3174inline bool ArrayDecay(InterpState &S, CodePtr OpPC) {
3175 const Pointer &Ptr = S.Stk.pop<Pointer>();
3176
3177 if (Ptr.isZero()) {
3178 S.Stk.push<Pointer>(Ptr);
3179 return true;
3180 }
3181
3182 if (!Ptr.isZeroSizeArray()) {
3183 if (!CheckRange(S, OpPC, Ptr, CSK_ArrayToPointer))
3184 return false;
3185 }
3186
3187 if (Ptr.isRoot() || !Ptr.isUnknownSizeArray()) {
3188 S.Stk.push<Pointer>(Ptr.atIndex(0));
3189 return true;
3190 }
3191
3192 const SourceInfo &E = S.Current->getSource(OpPC);
3193 S.FFDiag(E, diag::note_constexpr_unsupported_unsized_array);
3194
3195 return false;
3196}
3197
3198inline bool GetFnPtr(InterpState &S, CodePtr OpPC, const Function *Func) {
3199 assert(Func);
3200 S.Stk.push<Pointer>(Func);
3201 return true;
3202}
3203
3204template <PrimType Name, class T = typename PrimConv<Name>::T>
3205inline bool GetIntPtr(InterpState &S, CodePtr OpPC, const Descriptor *Desc) {
3206 const T &IntVal = S.Stk.pop<T>();
3207
3208 S.CCEDiag(S.Current->getSource(OpPC), diag::note_constexpr_invalid_cast)
3209 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
3210 << S.getLangOpts().CPlusPlus;
3211
3212 S.Stk.push<Pointer>(static_cast<uint64_t>(IntVal), Desc);
3213 return true;
3214}
3215
3216inline bool GetMemberPtr(InterpState &S, CodePtr OpPC, const ValueDecl *D) {
3217 S.Stk.push<MemberPointer>(D);
3218 return true;
3219}
3220
3221inline bool GetMemberPtrBase(InterpState &S, CodePtr OpPC) {
3222 const auto &MP = S.Stk.pop<MemberPointer>();
3223
3224 if (!MP.isBaseCastPossible())
3225 return false;
3226
3227 S.Stk.push<Pointer>(MP.getBase());
3228 return true;
3229}
3230
3231inline bool GetMemberPtrDecl(InterpState &S, CodePtr OpPC) {
3232 const auto &MP = S.Stk.pop<MemberPointer>();
3233
3234 const auto *FD = cast<FunctionDecl>(MP.getDecl());
3235 const auto *Func = S.getContext().getOrCreateFunction(FD);
3236
3237 S.Stk.push<Pointer>(Func);
3238 return true;
3239}
3240
3241/// Just emit a diagnostic. The expression that caused emission of this
3242/// op is not valid in a constant context.
3243inline bool Invalid(InterpState &S, CodePtr OpPC) {
3244 const SourceLocation &Loc = S.Current->getLocation(OpPC);
3245 S.FFDiag(Loc, diag::note_invalid_subexpr_in_const_expr)
3246 << S.Current->getRange(OpPC);
3247 return false;
3248}
3249
3250inline bool Unsupported(InterpState &S, CodePtr OpPC) {
3251 const SourceLocation &Loc = S.Current->getLocation(OpPC);
3252 S.FFDiag(Loc, diag::note_constexpr_stmt_expr_unsupported)
3253 << S.Current->getRange(OpPC);
3254 return false;
3255}
3256
3257inline bool StartSpeculation(InterpState &S, CodePtr OpPC) {
3258 ++S.SpeculationDepth;
3259 if (S.SpeculationDepth != 1)
3260 return true;
3261
3262 assert(S.PrevDiags == nullptr);
3264 S.getEvalStatus().Diag = nullptr;
3265 return true;
3266}
3267inline bool EndSpeculation(InterpState &S, CodePtr OpPC) {
3268 assert(S.SpeculationDepth != 0);
3269 --S.SpeculationDepth;
3270 if (S.SpeculationDepth == 0) {
3272 S.PrevDiags = nullptr;
3273 }
3274 return true;
3275}
3276
3277inline bool PushCC(InterpState &S, CodePtr OpPC, bool Value) {
3279 return true;
3280}
3281inline bool PopCC(InterpState &S, CodePtr OpPC) {
3282 S.ConstantContextOverride = std::nullopt;
3283 return true;
3284}
3285
3286/// Do nothing and just abort execution.
3287inline bool Error(InterpState &S, CodePtr OpPC) { return false; }
3288
3289inline bool SideEffect(InterpState &S, CodePtr OpPC) {
3290 return S.noteSideEffect();
3291}
3292
3293/// Same here, but only for casts.
3294inline bool InvalidCast(InterpState &S, CodePtr OpPC, CastKind Kind,
3295 bool Fatal) {
3296 const SourceLocation &Loc = S.Current->getLocation(OpPC);
3297
3298 if (Kind == CastKind::Reinterpret) {
3299 S.CCEDiag(Loc, diag::note_constexpr_invalid_cast)
3300 << static_cast<unsigned>(Kind) << S.Current->getRange(OpPC);
3301 return !Fatal;
3302 }
3303 if (Kind == CastKind::Volatile) {
3305 const auto *E = cast<CastExpr>(S.Current->getExpr(OpPC));
3306 if (S.getLangOpts().CPlusPlus)
3307 S.FFDiag(E, diag::note_constexpr_access_volatile_type)
3308 << AK_Read << E->getSubExpr()->getType();
3309 else
3310 S.FFDiag(E);
3311 }
3312
3313 return false;
3314 }
3315 if (Kind == CastKind::Dynamic) {
3316 assert(!S.getLangOpts().CPlusPlus20);
3317 S.CCEDiag(S.Current->getSource(OpPC), diag::note_constexpr_invalid_cast)
3318 << diag::ConstexprInvalidCastKind::Dynamic;
3319 return true;
3320 }
3321
3322 return false;
3323}
3324
3325inline bool InvalidDeclRef(InterpState &S, CodePtr OpPC, const DeclRefExpr *DR,
3326 bool InitializerFailed) {
3327 assert(DR);
3328
3329 if (InitializerFailed) {
3330 const SourceInfo &Loc = S.Current->getSource(OpPC);
3331 const auto *VD = cast<VarDecl>(DR->getDecl());
3332 S.FFDiag(Loc, diag::note_constexpr_var_init_non_constant, 1) << VD;
3333 S.Note(VD->getLocation(), diag::note_declared_at);
3334 return false;
3335 }
3336
3337 return CheckDeclRef(S, OpPC, DR);
3338}
3339
3341 if (S.inConstantContext()) {
3342 const SourceRange &ArgRange = S.Current->getRange(OpPC);
3343 const Expr *E = S.Current->getExpr(OpPC);
3344 S.CCEDiag(E, diag::note_constexpr_non_const_vectorelements) << ArgRange;
3345 }
3346 return false;
3347}
3348
3349inline bool CheckPseudoDtor(InterpState &S, CodePtr OpPC) {
3350 if (!S.getLangOpts().CPlusPlus20)
3351 S.CCEDiag(S.Current->getSource(OpPC),
3352 diag::note_constexpr_pseudo_destructor);
3353 return true;
3354}
3355
3356inline bool Assume(InterpState &S, CodePtr OpPC) {
3357 const auto Val = S.Stk.pop<Boolean>();
3358
3359 if (Val)
3360 return true;
3361
3362 // Else, diagnose.
3363 const SourceLocation &Loc = S.Current->getLocation(OpPC);
3364 S.CCEDiag(Loc, diag::note_constexpr_assumption_failed);
3365 return false;
3366}
3367
3368template <PrimType Name, class T = typename PrimConv<Name>::T>
3369inline bool OffsetOf(InterpState &S, CodePtr OpPC, const OffsetOfExpr *E) {
3370 llvm::SmallVector<int64_t> ArrayIndices;
3371 for (size_t I = 0; I != E->getNumExpressions(); ++I)
3372 ArrayIndices.emplace_back(S.Stk.pop<int64_t>());
3373
3374 int64_t Result;
3375 if (!InterpretOffsetOf(S, OpPC, E, ArrayIndices, Result))
3376 return false;
3377
3378 S.Stk.push<T>(T::from(Result));
3379
3380 return true;
3381}
3382
3383template <PrimType Name, class T = typename PrimConv<Name>::T>
3384inline bool CheckNonNullArg(InterpState &S, CodePtr OpPC) {
3385 const T &Arg = S.Stk.peek<T>();
3386 if (!Arg.isZero())
3387 return true;
3388
3389 const SourceLocation &Loc = S.Current->getLocation(OpPC);
3390 S.CCEDiag(Loc, diag::note_non_null_attribute_failed);
3391
3392 return false;
3393}
3394
3395void diagnoseEnumValue(InterpState &S, CodePtr OpPC, const EnumDecl *ED,
3396 const APSInt &Value);
3397
3398template <PrimType Name, class T = typename PrimConv<Name>::T>
3399inline bool CheckEnumValue(InterpState &S, CodePtr OpPC, const EnumDecl *ED) {
3400 assert(ED);
3401 assert(!ED->isFixed());
3402
3403 if (S.inConstantContext()) {
3404 const APSInt Val = S.Stk.peek<T>().toAPSInt();
3405 diagnoseEnumValue(S, OpPC, ED, Val);
3406 }
3407 return true;
3408}
3409
3410/// OldPtr -> Integer -> NewPtr.
3411template <PrimType TIn, PrimType TOut>
3412inline bool DecayPtr(InterpState &S, CodePtr OpPC) {
3413 static_assert(isPtrType(TIn) && isPtrType(TOut));
3414 using FromT = typename PrimConv<TIn>::T;
3415 using ToT = typename PrimConv<TOut>::T;
3416
3417 const FromT &OldPtr = S.Stk.pop<FromT>();
3418
3419 if constexpr (std::is_same_v<FromT, FunctionPointer> &&
3420 std::is_same_v<ToT, Pointer>) {
3421 S.Stk.push<Pointer>(OldPtr.getFunction(), OldPtr.getOffset());
3422 return true;
3423 } else if constexpr (std::is_same_v<FromT, Pointer> &&
3424 std::is_same_v<ToT, FunctionPointer>) {
3425 if (OldPtr.isFunctionPointer()) {
3426 S.Stk.push<FunctionPointer>(OldPtr.asFunctionPointer().getFunction(),
3427 OldPtr.getByteOffset());
3428 return true;
3429 }
3430 }
3431
3432 S.Stk.push<ToT>(ToT(OldPtr.getIntegerRepresentation(), nullptr));
3433 return true;
3434}
3435
3436inline bool CheckDecl(InterpState &S, CodePtr OpPC, const VarDecl *VD) {
3437 // An expression E is a core constant expression unless the evaluation of E
3438 // would evaluate one of the following: [C++23] - a control flow that passes
3439 // through a declaration of a variable with static or thread storage duration
3440 // unless that variable is usable in constant expressions.
3441 assert(VD->isLocalVarDecl() &&
3442 VD->isStaticLocal()); // Checked before emitting this.
3443
3444 if (VD == S.EvaluatingDecl)
3445 return true;
3446
3448 S.CCEDiag(VD->getLocation(), diag::note_constexpr_static_local)
3449 << (VD->getTSCSpec() == TSCS_unspecified ? 0 : 1) << VD;
3450 return false;
3451 }
3452 return true;
3453}
3454
3455inline bool Alloc(InterpState &S, CodePtr OpPC, const Descriptor *Desc) {
3456 assert(Desc);
3457
3458 if (!CheckDynamicMemoryAllocation(S, OpPC))
3459 return false;
3460
3461 DynamicAllocator &Allocator = S.getAllocator();
3462 Block *B = Allocator.allocate(Desc, S.Ctx.getEvalID(),
3464 assert(B);
3465 S.Stk.push<Pointer>(B);
3466 return true;
3467}
3468
3469template <PrimType Name, class SizeT = typename PrimConv<Name>::T>
3470inline bool AllocN(InterpState &S, CodePtr OpPC, PrimType T, const Expr *Source,
3471 bool IsNoThrow) {
3472 if (!CheckDynamicMemoryAllocation(S, OpPC))
3473 return false;
3474
3475 SizeT NumElements = S.Stk.pop<SizeT>();
3476 if (!CheckArraySize(S, OpPC, &NumElements, primSize(T), IsNoThrow)) {
3477 if (!IsNoThrow)
3478 return false;
3479
3480 // If this failed and is nothrow, just return a null ptr.
3481 S.Stk.push<Pointer>(0, nullptr);
3482 return true;
3483 }
3484 if (NumElements.isNegative()) {
3485 if (!IsNoThrow) {
3486 S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_new_negative)
3487 << NumElements.toDiagnosticString(S.getASTContext());
3488 return false;
3489 }
3490 S.Stk.push<Pointer>(0, nullptr);
3491 return true;
3492 }
3493
3494 if (!CheckArraySize(S, OpPC, static_cast<uint64_t>(NumElements)))
3495 return false;
3496
3497 DynamicAllocator &Allocator = S.getAllocator();
3498 Block *B =
3499 Allocator.allocate(Source, T, static_cast<size_t>(NumElements),
3501 assert(B);
3502 if (NumElements.isZero())
3503 S.Stk.push<Pointer>(B);
3504 else
3505 S.Stk.push<Pointer>(Pointer(B).atIndex(0));
3506 return true;
3507}
3508
3509template <PrimType Name, class SizeT = typename PrimConv<Name>::T>
3510inline bool AllocCN(InterpState &S, CodePtr OpPC, const Descriptor *ElementDesc,
3511 bool IsNoThrow) {
3512 if (!CheckDynamicMemoryAllocation(S, OpPC))
3513 return false;
3514
3515 SizeT NumElements = S.Stk.pop<SizeT>();
3516 if (!CheckArraySize(S, OpPC, &NumElements, ElementDesc->getSize(),
3517 IsNoThrow)) {
3518 if (!IsNoThrow)
3519 return false;
3520
3521 // If this failed and is nothrow, just return a null ptr.
3522 S.Stk.push<Pointer>(0, ElementDesc);
3523 return true;
3524 }
3525 assert(NumElements.isPositive());
3526
3527 if (!CheckArraySize(S, OpPC, static_cast<uint64_t>(NumElements)))
3528 return false;
3529
3530 DynamicAllocator &Allocator = S.getAllocator();
3531 Block *B =
3532 Allocator.allocate(ElementDesc, static_cast<size_t>(NumElements),
3534 assert(B);
3535 if (NumElements.isZero())
3536 S.Stk.push<Pointer>(B);
3537 else
3538 S.Stk.push<Pointer>(Pointer(B).atIndex(0));
3539
3540 return true;
3541}
3542
3543bool Free(InterpState &S, CodePtr OpPC, bool DeleteIsArrayForm,
3544 bool IsGlobalDelete);
3545
3546static inline bool IsConstantContext(InterpState &S, CodePtr OpPC) {
3548 return true;
3549}
3550
3551static inline bool CheckAllocations(InterpState &S, CodePtr OpPC) {
3553}
3554
3555/// Check if the initializer and storage types of a placement-new expression
3556/// match.
3557bool CheckNewTypeMismatch(InterpState &S, CodePtr OpPC, const Expr *E,
3558 std::optional<uint64_t> ArraySize = std::nullopt);
3559
3560template <PrimType Name, class T = typename PrimConv<Name>::T>
3562 const auto &Size = S.Stk.pop<T>();
3563 return CheckNewTypeMismatch(S, OpPC, E, static_cast<uint64_t>(Size));
3564}
3565bool InvalidNewDeleteExpr(InterpState &S, CodePtr OpPC, const Expr *E);
3566
3567template <PrimType Name, class T = typename PrimConv<Name>::T>
3568inline bool BitCastPrim(InterpState &S, CodePtr OpPC, bool TargetIsUCharOrByte,
3569 uint32_t ResultBitWidth,
3570 const llvm::fltSemantics *Sem) {
3571 const Pointer &FromPtr = S.Stk.pop<Pointer>();
3572
3573 if (!CheckLoad(S, OpPC, FromPtr))
3574 return false;
3575
3576 if constexpr (std::is_same_v<T, Pointer>) {
3577 // The only pointer type we can validly bitcast to is nullptr_t.
3578 S.Stk.push<Pointer>();
3579 return true;
3580 } else {
3581
3582 size_t BuffSize = ResultBitWidth / 8;
3583 llvm::SmallVector<std::byte> Buff(BuffSize);
3584 bool HasIndeterminateBits = false;
3585
3586 Bits FullBitWidth(ResultBitWidth);
3587 Bits BitWidth = FullBitWidth;
3588
3589 if constexpr (std::is_same_v<T, Floating>) {
3590 assert(Sem);
3591 BitWidth = Bits(llvm::APFloatBase::getSizeInBits(*Sem));
3592 }
3593
3594 if (!DoBitCast(S, OpPC, FromPtr, Buff.data(), BitWidth, FullBitWidth,
3595 HasIndeterminateBits))
3596 return false;
3597
3598 if (!CheckBitCast(S, OpPC, HasIndeterminateBits, TargetIsUCharOrByte))
3599 return false;
3600
3601 if constexpr (std::is_same_v<T, Floating>) {
3602 assert(Sem);
3603 Floating Result = S.allocFloat(*Sem);
3604 Floating::bitcastFromMemory(Buff.data(), *Sem, &Result);
3605 S.Stk.push<Floating>(Result);
3606 } else if constexpr (needsAlloc<T>()) {
3607 T Result = S.allocAP<T>(ResultBitWidth);
3608 T::bitcastFromMemory(Buff.data(), ResultBitWidth, &Result);
3609 S.Stk.push<T>(Result);
3610 } else if constexpr (std::is_same_v<T, Boolean>) {
3611 // Only allow to cast single-byte integers to bool if they are either 0
3612 // or 1.
3613 assert(FullBitWidth.getQuantity() == 8);
3614 auto Val = static_cast<unsigned int>(Buff[0]);
3615 if (Val > 1) {
3616 S.FFDiag(S.Current->getSource(OpPC),
3617 diag::note_constexpr_bit_cast_unrepresentable_value)
3618 << S.getASTContext().BoolTy << Val;
3619 return false;
3620 }
3621 S.Stk.push<T>(T::bitcastFromMemory(Buff.data(), ResultBitWidth));
3622 } else {
3623 assert(!Sem);
3624 S.Stk.push<T>(T::bitcastFromMemory(Buff.data(), ResultBitWidth));
3625 }
3626 return true;
3627 }
3628}
3629
3630inline bool BitCast(InterpState &S, CodePtr OpPC) {
3631 const Pointer &FromPtr = S.Stk.pop<Pointer>();
3632 Pointer &ToPtr = S.Stk.peek<Pointer>();
3633
3634 if (!CheckLoad(S, OpPC, FromPtr))
3635 return false;
3636
3637 if (!DoBitCastPtr(S, OpPC, FromPtr, ToPtr))
3638 return false;
3639
3640 return true;
3641}
3642
3643/// Typeid support.
3644bool GetTypeid(InterpState &S, CodePtr OpPC, const Type *TypePtr,
3645 const Type *TypeInfoType);
3646bool GetTypeidPtr(InterpState &S, CodePtr OpPC, const Type *TypeInfoType);
3647bool DiagTypeid(InterpState &S, CodePtr OpPC);
3648
3649inline bool CheckDestruction(InterpState &S, CodePtr OpPC) {
3650 const auto &Ptr = S.Stk.peek<Pointer>();
3651 return CheckDestructor(S, OpPC, Ptr);
3652}
3653
3654inline bool CheckArraySize(InterpState &S, CodePtr OpPC, uint64_t NumElems) {
3655 uint64_t Limit = S.getLangOpts().ConstexprStepLimit;
3656 if (NumElems > Limit) {
3657 S.FFDiag(S.Current->getSource(OpPC),
3658 diag::note_constexpr_new_exceeds_limits)
3659 << NumElems << Limit;
3660 return false;
3661 }
3662 return true;
3663}
3664
3665//===----------------------------------------------------------------------===//
3666// Read opcode arguments
3667//===----------------------------------------------------------------------===//
3668
3669template <typename T> inline T ReadArg(InterpState &S, CodePtr &OpPC) {
3670 if constexpr (std::is_pointer<T>::value) {
3671 uint32_t ID = OpPC.read<uint32_t>();
3672 return reinterpret_cast<T>(S.P.getNativePointer(ID));
3673 } else {
3674 return OpPC.read<T>();
3675 }
3676}
3677
3678template <> inline Floating ReadArg<Floating>(InterpState &S, CodePtr &OpPC) {
3679 auto &Semantics =
3680 llvm::APFloatBase::EnumToSemantics(Floating::deserializeSemantics(*OpPC));
3681
3682 auto F = S.allocFloat(Semantics);
3683 Floating::deserialize(*OpPC, &F);
3684 OpPC += align(F.bytesToSerialize());
3685 return F;
3686}
3687
3688template <>
3689inline IntegralAP<false> ReadArg<IntegralAP<false>>(InterpState &S,
3690 CodePtr &OpPC) {
3691 uint32_t BitWidth = IntegralAP<false>::deserializeSize(*OpPC);
3692 auto Result = S.allocAP<IntegralAP<false>>(BitWidth);
3693 assert(Result.bitWidth() == BitWidth);
3694
3696 OpPC += align(Result.bytesToSerialize());
3697 return Result;
3698}
3699
3700template <>
3701inline IntegralAP<true> ReadArg<IntegralAP<true>>(InterpState &S,
3702 CodePtr &OpPC) {
3703 uint32_t BitWidth = IntegralAP<true>::deserializeSize(*OpPC);
3704 auto Result = S.allocAP<IntegralAP<true>>(BitWidth);
3705 assert(Result.bitWidth() == BitWidth);
3706
3707 IntegralAP<true>::deserialize(*OpPC, &Result);
3708 OpPC += align(Result.bytesToSerialize());
3709 return Result;
3710}
3711
3712template <>
3715 OpPC += align(FP.bytesToSerialize());
3716 return FP;
3717}
3718
3719} // namespace interp
3720} // namespace clang
3721
3722#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:2879
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:1272
ValueDecl * getDecl()
Definition Expr.h:1340
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:3302
OffsetOfExpr - [C99 7.17] - This represents an expression of the form offsetof(record-type,...
Definition Expr.h:2529
unsigned getNumExpressions() const
Definition Expr.h:2600
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
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:140
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:79
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:91
const VarDecl * EvaluatingDecl
Declaration we're initializing/evaluting, if any.
InterpFrame * Current
The current frame.
std::optional< bool > ConstantContextOverride
T allocAP(unsigned BitWidth)
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:90
static bool hasSameBase(const Pointer &A, const Pointer &B)
Checks if two pointers are comparable.
Definition Pointer.cpp:618
Pointer narrow() const
Restricts the scope of an array element pointer.
Definition Pointer.h:187
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:650
Pointer atIndex(uint64_t Idx) const
Offsets a pointer inside an array.
Definition Pointer.h:155
bool isDummy() const
Checks if the pointer points to a dummy value.
Definition Pointer.h:543
Pointer atFieldSub(unsigned Off) const
Subtract the given offset from the current Base and Offset of the pointer.
Definition Pointer.h:180
int64_t getIndex() const
Returns the index into an array.
Definition Pointer.h:608
Pointer atField(unsigned Off) const
Creates a pointer to a field.
Definition Pointer.h:172
T & deref() const
Dereferences the pointer, if it's live.
Definition Pointer.h:659
unsigned getNumElems() const
Returns the number of elements.
Definition Pointer.h:592
bool isUnknownSizeArray() const
Checks if the structure is an array of unknown size.
Definition Pointer.h:411
void activate() const
Activats a field.
Definition Pointer.cpp:560
static std::optional< std::pair< Pointer, Pointer > > computeSplitPoint(const Pointer &A, const Pointer &B)
Definition Pointer.cpp:670
bool isIntegralPointer() const
Definition Pointer.h:465
bool pointsToStringLiteral() const
Definition Pointer.cpp:658
bool inArray() const
Checks if the innermost field is an array.
Definition Pointer.h:393
T & elem(unsigned I) const
Dereferences the element at index I.
Definition Pointer.h:675
uint64_t getByteOffset() const
Returns the byte offset from the start.
Definition Pointer.h:581
bool isTypeidPointer() const
Definition Pointer.h:467
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:253
const IntPointer & asIntPointer() const
Definition Pointer.h:451
bool isRoot() const
Pointer points directly to a block.
Definition Pointer.h:433
const Descriptor * getDeclDesc() const
Accessor for information about the declaration site.
Definition Pointer.h:278
unsigned getOffset() const
Returns the offset into an array.
Definition Pointer.h:372
bool isOnePastEnd() const
Checks if the index is one past end.
Definition Pointer.h:625
uint64_t getIntegerRepresentation() const
Definition Pointer.h:142
Pointer expand() const
Expands a pointer to the containing array, undoing narrowing.
Definition Pointer.h:220
bool isElementPastEnd() const
Checks if the pointer is an out-of-bounds element pointer.
Definition Pointer.h:647
bool isBlockPointer() const
Definition Pointer.h:464
const FunctionPointer & asFunctionPointer() const
Definition Pointer.h:455
bool isFunctionPointer() const
Definition Pointer.h:466
const Descriptor * getFieldDesc() const
Accessors for information about the innermost field.
Definition Pointer.h:322
size_t elemSize() const
Returns the element size of the innermost field.
Definition Pointer.h:354
bool canBeInitialized() const
If this pointer has an InlineDescriptor we can use to initialize.
Definition Pointer.h:440
Lifetime getLifetime() const
Definition Pointer.h:718
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:447
void initialize() const
Initializes a field.
Definition Pointer.cpp:483
const Record * getRecord() const
Returns the record descriptor of a class.
Definition Pointer.h:470
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:173
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
const LangOptions & getLangOpts() const
Definition State.cpp:115
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:2121
bool EndSpeculation(InterpState &S, CodePtr OpPC)
Definition Interp.h:3267
static bool ShiftFixedPoint(InterpState &S, CodePtr OpPC, bool Left)
Definition Interp.h:2997
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:2960
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:3649
bool ArrayElemPop(InterpState &S, CodePtr OpPC, uint32_t Index)
Definition Interp.h:3142
bool CastPointerIntegralAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth)
Definition Interp.cpp:2051
bool PopCC(InterpState &S, CodePtr OpPC)
Definition Interp.h:3281
bool ArrayElem(InterpState &S, CodePtr OpPC, uint32_t Index)
Definition Interp.h:3130
bool GT(InterpState &S, CodePtr OpPC)
Definition Interp.h:1282
bool CastPointerIntegralAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth)
Definition Interp.cpp:2038
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:2586
bool GetMemberPtrBase(InterpState &S, CodePtr OpPC)
Definition Interp.h:3221
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:3047
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
Floating ReadArg< Floating >(InterpState &S, CodePtr &OpPC)
Definition Interp.h:3678
bool Incf(InterpState &S, CodePtr OpPC, uint32_t FPOI)
Definition Interp.h:950
bool SideEffect(InterpState &S, CodePtr OpPC)
Definition Interp.h:3289
static bool ZeroIntAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth)
Definition Interp.h:2765
bool DoShift(InterpState &S, CodePtr OpPC, LT &LHS, RT &RHS, LT *Result)
Definition Interp.h:2836
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:1859
bool Sub(InterpState &S, CodePtr OpPC)
Definition Interp.h:425
bool GetTypeidPtr(InterpState &S, CodePtr OpPC, const Type *TypeInfoType)
Definition Interp.cpp:2087
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:2148
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:3630
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:2774
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:2367
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:1845
bool GetTypeid(InterpState &S, CodePtr OpPC, const Type *TypePtr, const Type *TypeInfoType)
Typeid support.
Definition Interp.cpp:2081
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:3384
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:2342
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:3436
bool CheckPointerToIntegralCast(InterpState &S, CodePtr OpPC, const Pointer &Ptr, unsigned BitWidth)
Definition Interp.cpp:2018
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:3198
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:2389
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:1997
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:3071
bool NE(InterpState &S, CodePtr OpPC)
Definition Interp.h:1260
bool NoRet(InterpState &S, CodePtr OpPC)
Definition Interp.h:3037
bool GetIntPtr(InterpState &S, CodePtr OpPC, const Descriptor *Desc)
Definition Interp.h:3205
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:2704
static bool ZeroIntAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth)
Definition Interp.h:2757
bool BitCastPrim(InterpState &S, CodePtr OpPC, bool TargetIsUCharOrByte, uint32_t ResultBitWidth, const llvm::fltSemantics *Sem)
Definition Interp.h:3568
bool Shl(InterpState &S, CodePtr OpPC)
Definition Interp.h:2979
bool RVOPtr(InterpState &S, CodePtr OpPC)
Definition Interp.h:2823
bool CastPointerIntegral(InterpState &S, CodePtr OpPC)
Definition Interp.h:2634
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:2329
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:2529
bool ExpandPtr(InterpState &S, CodePtr OpPC)
Definition Interp.h:3053
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:3101
bool This(InterpState &S, CodePtr OpPC)
Definition Interp.h:2795
bool InitScope(InterpState &S, CodePtr OpPC, uint32_t I)
Definition Interp.h:2472
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:3669
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:2556
bool ArrayDecay(InterpState &S, CodePtr OpPC)
Just takes a pointer and checks if it's an incomplete array type.
Definition Interp.h:3174
bool PushCC(InterpState &S, CodePtr OpPC, bool Value)
Definition Interp.h:3277
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:2113
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:2064
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:3369
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:3713
static bool CastFloatingFixedPoint(InterpState &S, CodePtr OpPC, uint32_t FPS)
Definition Interp.h:2665
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:1814
bool LE(InterpState &S, CodePtr OpPC)
Definition Interp.h:1274
bool CheckNewTypeMismatchArray(InterpState &S, CodePtr OpPC, const Expr *E)
Definition Interp.h:3561
bool Zero(InterpState &S, CodePtr OpPC)
Definition Interp.h:2752
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:3250
bool InvalidDeclRef(InterpState &S, CodePtr OpPC, const DeclRefExpr *DR, bool InitializerFailed)
Definition Interp.h:3325
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:2490
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:2376
constexpr bool needsAlloc()
Definition PrimType.h:125
static bool CheckAllocations(InterpState &S, CodePtr OpPC)
Definition Interp.h:3551
bool Alloc(InterpState &S, CodePtr OpPC, const Descriptor *Desc)
Definition Interp.h:3455
bool InvalidShuffleVectorIndex(InterpState &S, CodePtr OpPC, uint32_t Index)
Definition Interp.cpp:2010
bool ToMemberPtr(InterpState &S, CodePtr OpPC)
Definition Interp.h:2180
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:2649
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:1872
bool GetMemberPtr(InterpState &S, CodePtr OpPC, const ValueDecl *D)
Definition Interp.h:3216
bool Dump(InterpState &S, CodePtr OpPC)
Definition Interp.h:1888
bool SizelessVectorElementSize(InterpState &S, CodePtr OpPC)
Definition Interp.h:3340
static bool PtrPtrCast(InterpState &S, CodePtr OpPC, bool SrcIsVoidPtr)
Definition Interp.h:2712
bool CheckLiteralType(InterpState &S, CodePtr OpPC, const Type *T)
Definition Interp.cpp:1381
bool IsNonNull(InterpState &S, CodePtr OpPC)
Definition Interp.h:2783
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:3231
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:2681
bool Divf(InterpState &S, CodePtr OpPC, uint32_t FPOI)
Definition Interp.h:718
bool FinishInitGlobal(InterpState &S, CodePtr OpPC)
Definition Interp.cpp:2248
bool DecayPtr(InterpState &S, CodePtr OpPC)
OldPtr -> Integer -> NewPtr.
Definition Interp.h:3412
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:2607
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:2444
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:3349
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:1953
bool CheckArraySize(InterpState &S, CodePtr OpPC, uint64_t NumElems)
Definition Interp.h:3654
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:3257
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:2203
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:2481
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:2314
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:2170
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:2691
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:2541
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:2499
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:3510
bool GetGlobal(InterpState &S, CodePtr OpPC, uint32_t I)
Definition Interp.h:1469
bool Interpret(InterpState &S)
Interpreter entry point.
Definition Interp.cpp:2264
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:3294
bool DoShiftAP(InterpState &S, CodePtr OpPC, const APSInt &LHS, APSInt RHS, LT *Result)
A version of DoShift that works on IntegralAP.
Definition Interp.h:2916
bool CastMemberPtrPtr(InterpState &S, CodePtr OpPC)
Definition Interp.h:2188
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:2517
bool Invalid(InterpState &S, CodePtr OpPC)
Just emit a diagnostic.
Definition Interp.h:3243
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:3356
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:3546
bool AllocN(InterpState &S, CodePtr OpPC, PrimType T, const Expr *Source, bool IsNoThrow)
Definition Interp.h:3470
bool CheckEnumValue(InterpState &S, CodePtr OpPC, const EnumDecl *ED)
Definition Interp.h:3399
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_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 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