Thanks to visit codestin.com
Credit goes to clang.llvm.org

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);
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 if (!CheckThis(S, OpPC))
1444 return false;
1445 const Pointer &This = S.Current->getThis();
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 if (!CheckThis(S, OpPC))
1458 return false;
1459 const T &Value = S.Stk.pop<T>();
1460 const Pointer &This = S.Current->getThis();
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 if (!CheckThis(S, OpPC))
1564 return false;
1565 const Pointer &This = S.Current->getThis();
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 if (!CheckThis(S, OpPC))
1578 return false;
1579 const Pointer &This = S.Current->getThis();
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 if (!CheckThis(S, OpPC))
1597 return false;
1598 const Pointer &This = S.Current->getThis();
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 if (!CheckThis(S, OpPC))
1614 return false;
1615 const Pointer &This = S.Current->getThis();
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 if (!CheckThis(S, OpPC))
1754 return false;
1755 const Pointer &This = S.Current->getThis();
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 if (!CheckThis(S, OpPC))
1848 return false;
1849 const Pointer &This = S.Current->getThis();
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 if (!CheckThis(S, OpPC))
1929 return false;
1930 const Pointer &This = S.Current->getThis();
1931 return VirtBaseHelper(S, OpPC, D, This);
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 if (!S.Current->hasThisPointer())
1995 return false;
1996
1997 const Pointer &Ptr = S.Current->getThis();
1998 assert(Ptr.atField(I).canBeInitialized());
1999 Ptr.atField(I).activate();
2000 return true;
2001}
2002
2003template <PrimType Name, class T = typename PrimConv<Name>::T>
2005 const T &Value = S.Stk.pop<T>();
2006 const Pointer &Ptr = S.Stk.peek<Pointer>();
2007
2008 if (Ptr.canBeInitialized()) {
2009 Ptr.initialize();
2010 Ptr.activate();
2011 }
2012
2013 if (!CheckStore(S, OpPC, Ptr))
2014 return false;
2015 Ptr.deref<T>() = Value;
2016 return true;
2017}
2018
2019template <PrimType Name, class T = typename PrimConv<Name>::T>
2021 const T &Value = S.Stk.pop<T>();
2022 const Pointer &Ptr = S.Stk.pop<Pointer>();
2023
2024 if (Ptr.canBeInitialized()) {
2025 Ptr.initialize();
2026 Ptr.activate();
2027 }
2028 if (!CheckStore(S, OpPC, Ptr))
2029 return false;
2030 Ptr.deref<T>() = Value;
2031 return true;
2032}
2033
2034template <PrimType Name, class T = typename PrimConv<Name>::T>
2036 const T &Value = S.Stk.pop<T>();
2037 const Pointer &Ptr = S.Stk.peek<Pointer>();
2038 if (!CheckStore(S, OpPC, Ptr))
2039 return false;
2040 if (Ptr.canBeInitialized())
2041 Ptr.initialize();
2042 if (const auto *FD = Ptr.getField())
2043 Ptr.deref<T>() = Value.truncate(FD->getBitWidthValue());
2044 else
2045 Ptr.deref<T>() = Value;
2046 return true;
2047}
2048
2049template <PrimType Name, class T = typename PrimConv<Name>::T>
2051 const T &Value = S.Stk.pop<T>();
2052 const Pointer &Ptr = S.Stk.pop<Pointer>();
2053 if (!CheckStore(S, OpPC, Ptr))
2054 return false;
2055 if (Ptr.canBeInitialized())
2056 Ptr.initialize();
2057 if (const auto *FD = Ptr.getField())
2058 Ptr.deref<T>() = Value.truncate(FD->getBitWidthValue());
2059 else
2060 Ptr.deref<T>() = Value;
2061 return true;
2062}
2063
2064template <PrimType Name, class T = typename PrimConv<Name>::T>
2066 const T &Value = S.Stk.pop<T>();
2067 const Pointer &Ptr = S.Stk.peek<Pointer>();
2068 if (Ptr.canBeInitialized()) {
2069 Ptr.initialize();
2070 Ptr.activate();
2071 }
2072 if (!CheckStore(S, OpPC, Ptr))
2073 return false;
2074 if (const auto *FD = Ptr.getField())
2075 Ptr.deref<T>() = Value.truncate(FD->getBitWidthValue());
2076 else
2077 Ptr.deref<T>() = Value;
2078 return true;
2079}
2080
2081template <PrimType Name, class T = typename PrimConv<Name>::T>
2083 const T &Value = S.Stk.pop<T>();
2084 const Pointer &Ptr = S.Stk.pop<Pointer>();
2085
2086 if (Ptr.canBeInitialized()) {
2087 Ptr.initialize();
2088 Ptr.activate();
2089 }
2090 if (!CheckStore(S, OpPC, Ptr))
2091 return false;
2092 if (const auto *FD = Ptr.getField())
2093 Ptr.deref<T>() = Value.truncate(FD->getBitWidthValue());
2094 else
2095 Ptr.deref<T>() = Value;
2096 return true;
2097}
2098
2099template <PrimType Name, class T = typename PrimConv<Name>::T>
2100bool Init(InterpState &S, CodePtr OpPC) {
2101 const T &Value = S.Stk.pop<T>();
2102 const Pointer &Ptr = S.Stk.peek<Pointer>();
2103 if (!CheckInit(S, OpPC, Ptr))
2104 return false;
2105 Ptr.initialize();
2106 new (&Ptr.deref<T>()) T(Value);
2107 return true;
2108}
2109
2110template <PrimType Name, class T = typename PrimConv<Name>::T>
2112 const T &Value = S.Stk.pop<T>();
2113 const Pointer &Ptr = S.Stk.pop<Pointer>();
2114 if (!CheckInit(S, OpPC, Ptr))
2115 return false;
2116 Ptr.initialize();
2117 new (&Ptr.deref<T>()) T(Value);
2118 return true;
2119}
2120
2121/// 1) Pops the value from the stack
2122/// 2) Peeks a pointer and gets its index \Idx
2123/// 3) Sets the value on the pointer, leaving the pointer on the stack.
2124template <PrimType Name, class T = typename PrimConv<Name>::T>
2125bool InitElem(InterpState &S, CodePtr OpPC, uint32_t Idx) {
2126 const T &Value = S.Stk.pop<T>();
2127 const Pointer &Ptr = S.Stk.peek<Pointer>();
2128
2129 const Descriptor *Desc = Ptr.getFieldDesc();
2130 if (Desc->isUnknownSizeArray())
2131 return false;
2132
2133 // In the unlikely event that we're initializing the first item of
2134 // a non-array, skip the atIndex().
2135 if (Idx == 0 && !Desc->isArray()) {
2136 Ptr.initialize();
2137 new (&Ptr.deref<T>()) T(Value);
2138 return true;
2139 }
2140
2141 if (!CheckLive(S, OpPC, Ptr, AK_Assign))
2142 return false;
2143 if (Idx >= Desc->getNumElems()) {
2144 // CheckRange.
2145 if (S.getLangOpts().CPlusPlus) {
2146 const SourceInfo &Loc = S.Current->getSource(OpPC);
2147 S.FFDiag(Loc, diag::note_constexpr_access_past_end)
2148 << AK_Assign << S.Current->getRange(OpPC);
2149 }
2150 return false;
2151 }
2152 Ptr.initializeElement(Idx);
2153 new (&Ptr.elem<T>(Idx)) T(Value);
2154 return true;
2155}
2156
2157/// The same as InitElem, but pops the pointer as well.
2158template <PrimType Name, class T = typename PrimConv<Name>::T>
2159bool InitElemPop(InterpState &S, CodePtr OpPC, uint32_t Idx) {
2160 const T &Value = S.Stk.pop<T>();
2161 const Pointer &Ptr = S.Stk.pop<Pointer>();
2162
2163 const Descriptor *Desc = Ptr.getFieldDesc();
2164 if (Desc->isUnknownSizeArray())
2165 return false;
2166
2167 // In the unlikely event that we're initializing the first item of
2168 // a non-array, skip the atIndex().
2169 if (Idx == 0 && !Desc->isArray()) {
2170 Ptr.initialize();
2171 new (&Ptr.deref<T>()) T(Value);
2172 return true;
2173 }
2174
2175 if (!CheckLive(S, OpPC, Ptr, AK_Assign))
2176 return false;
2177 if (Idx >= Desc->getNumElems()) {
2178 // CheckRange.
2179 if (S.getLangOpts().CPlusPlus) {
2180 const SourceInfo &Loc = S.Current->getSource(OpPC);
2181 S.FFDiag(Loc, diag::note_constexpr_access_past_end)
2182 << AK_Assign << S.Current->getRange(OpPC);
2183 }
2184 return false;
2185 }
2186 Ptr.initializeElement(Idx);
2187 new (&Ptr.elem<T>(Idx)) T(Value);
2188 return true;
2189}
2190
2191inline bool Memcpy(InterpState &S, CodePtr OpPC) {
2192 const Pointer &Src = S.Stk.pop<Pointer>();
2193 Pointer &Dest = S.Stk.peek<Pointer>();
2194
2195 if (!CheckLoad(S, OpPC, Src))
2196 return false;
2197
2198 return DoMemcpy(S, OpPC, Src, Dest);
2199}
2200
2201inline bool ToMemberPtr(InterpState &S, CodePtr OpPC) {
2202 const auto &Member = S.Stk.pop<MemberPointer>();
2203 const auto &Base = S.Stk.pop<Pointer>();
2204
2205 S.Stk.push<MemberPointer>(Member.takeInstance(Base));
2206 return true;
2207}
2208
2209inline bool CastMemberPtrPtr(InterpState &S, CodePtr OpPC) {
2210 const auto &MP = S.Stk.pop<MemberPointer>();
2211
2212 if (std::optional<Pointer> Ptr = MP.toPointer(S.Ctx)) {
2213 S.Stk.push<Pointer>(*Ptr);
2214 return true;
2215 }
2216 return Invalid(S, OpPC);
2217}
2218
2219//===----------------------------------------------------------------------===//
2220// AddOffset, SubOffset
2221//===----------------------------------------------------------------------===//
2222
2223template <class T, ArithOp Op>
2224std::optional<Pointer> OffsetHelper(InterpState &S, CodePtr OpPC,
2225 const T &Offset, const Pointer &Ptr,
2226 bool IsPointerArith = false) {
2227 // A zero offset does not change the pointer.
2228 if (Offset.isZero())
2229 return Ptr;
2230
2231 if (IsPointerArith && !CheckNull(S, OpPC, Ptr, CSK_ArrayIndex)) {
2232 // The CheckNull will have emitted a note already, but we only
2233 // abort in C++, since this is fine in C.
2234 if (S.getLangOpts().CPlusPlus)
2235 return std::nullopt;
2236 }
2237
2238 // Arrays of unknown bounds cannot have pointers into them.
2239 if (!CheckArray(S, OpPC, Ptr))
2240 return std::nullopt;
2241
2242 // This is much simpler for integral pointers, so handle them first.
2243 if (Ptr.isIntegralPointer()) {
2244 uint64_t V = Ptr.getIntegerRepresentation();
2245 uint64_t O = static_cast<uint64_t>(Offset) * Ptr.elemSize();
2246 if constexpr (Op == ArithOp::Add)
2247 return Pointer(V + O, Ptr.asIntPointer().Desc);
2248 else
2249 return Pointer(V - O, Ptr.asIntPointer().Desc);
2250 } else if (Ptr.isFunctionPointer()) {
2251 uint64_t O = static_cast<uint64_t>(Offset);
2252 uint64_t N;
2253 if constexpr (Op == ArithOp::Add)
2254 N = Ptr.getByteOffset() + O;
2255 else
2256 N = Ptr.getByteOffset() - O;
2257
2258 if (N > 1)
2259 S.CCEDiag(S.Current->getSource(OpPC), diag::note_constexpr_array_index)
2260 << N << /*non-array*/ true << 0;
2261 return Pointer(Ptr.asFunctionPointer().getFunction(), N);
2262 }
2263
2264 assert(Ptr.isBlockPointer());
2265
2266 uint64_t MaxIndex = static_cast<uint64_t>(Ptr.getNumElems());
2267 uint64_t Index;
2268 if (Ptr.isOnePastEnd())
2269 Index = MaxIndex;
2270 else
2271 Index = Ptr.getIndex();
2272
2273 bool Invalid = false;
2274 // Helper to report an invalid offset, computed as APSInt.
2275 auto DiagInvalidOffset = [&]() -> void {
2276 const unsigned Bits = Offset.bitWidth();
2277 APSInt APOffset(Offset.toAPSInt().extend(Bits + 2), /*IsUnsigend=*/false);
2278 APSInt APIndex(APInt(Bits + 2, Index, /*IsSigned=*/true),
2279 /*IsUnsigned=*/false);
2280 APSInt NewIndex =
2281 (Op == ArithOp::Add) ? (APIndex + APOffset) : (APIndex - APOffset);
2282 S.CCEDiag(S.Current->getSource(OpPC), diag::note_constexpr_array_index)
2283 << NewIndex << /*array*/ static_cast<int>(!Ptr.inArray()) << MaxIndex;
2284 Invalid = true;
2285 };
2286
2287 if (Ptr.isBlockPointer()) {
2288 uint64_t IOffset = static_cast<uint64_t>(Offset);
2289 uint64_t MaxOffset = MaxIndex - Index;
2290
2291 if constexpr (Op == ArithOp::Add) {
2292 // If the new offset would be negative, bail out.
2293 if (Offset.isNegative() && (Offset.isMin() || -IOffset > Index))
2294 DiagInvalidOffset();
2295
2296 // If the new offset would be out of bounds, bail out.
2297 if (Offset.isPositive() && IOffset > MaxOffset)
2298 DiagInvalidOffset();
2299 } else {
2300 // If the new offset would be negative, bail out.
2301 if (Offset.isPositive() && Index < IOffset)
2302 DiagInvalidOffset();
2303
2304 // If the new offset would be out of bounds, bail out.
2305 if (Offset.isNegative() && (Offset.isMin() || -IOffset > MaxOffset))
2306 DiagInvalidOffset();
2307 }
2308 }
2309
2310 if (Invalid && S.getLangOpts().CPlusPlus)
2311 return std::nullopt;
2312
2313 // Offset is valid - compute it on unsigned.
2314 int64_t WideIndex = static_cast<int64_t>(Index);
2315 int64_t WideOffset = static_cast<int64_t>(Offset);
2316 int64_t Result;
2317 if constexpr (Op == ArithOp::Add)
2318 Result = WideIndex + WideOffset;
2319 else
2320 Result = WideIndex - WideOffset;
2321
2322 // When the pointer is one-past-end, going back to index 0 is the only
2323 // useful thing we can do. Any other index has been diagnosed before and
2324 // we don't get here.
2325 if (Result == 0 && Ptr.isOnePastEnd()) {
2326 if (Ptr.getFieldDesc()->isArray())
2327 return Ptr.atIndex(0);
2328 return Pointer(Ptr.asBlockPointer().Pointee, Ptr.asBlockPointer().Base);
2329 }
2330
2331 return Ptr.atIndex(static_cast<uint64_t>(Result));
2332}
2333
2334template <PrimType Name, class T = typename PrimConv<Name>::T>
2336 const T &Offset = S.Stk.pop<T>();
2337 Pointer Ptr = S.Stk.pop<Pointer>();
2338 if (Ptr.isBlockPointer())
2339 Ptr = Ptr.expand();
2340
2341 if (std::optional<Pointer> Result = OffsetHelper<T, ArithOp::Add>(
2342 S, OpPC, Offset, Ptr, /*IsPointerArith=*/true)) {
2343 S.Stk.push<Pointer>(*Result);
2344 return true;
2345 }
2346 return false;
2347}
2348
2349template <PrimType Name, class T = typename PrimConv<Name>::T>
2351 const T &Offset = S.Stk.pop<T>();
2352 const Pointer &Ptr = S.Stk.pop<Pointer>();
2353
2354 if (std::optional<Pointer> Result = OffsetHelper<T, ArithOp::Sub>(
2355 S, OpPC, Offset, Ptr, /*IsPointerArith=*/true)) {
2356 S.Stk.push<Pointer>(*Result);
2357 return true;
2358 }
2359 return false;
2360}
2361
2362template <ArithOp Op>
2363static inline bool IncDecPtrHelper(InterpState &S, CodePtr OpPC,
2364 const Pointer &Ptr) {
2365 if (Ptr.isDummy())
2366 return false;
2367
2368 using OneT = Integral<8, false>;
2369
2370 const Pointer &P = Ptr.deref<Pointer>();
2371 if (!CheckNull(S, OpPC, P, CSK_ArrayIndex))
2372 return false;
2373
2374 // Get the current value on the stack.
2375 S.Stk.push<Pointer>(P);
2376
2377 // Now the current Ptr again and a constant 1.
2378 OneT One = OneT::from(1);
2379 if (std::optional<Pointer> Result =
2380 OffsetHelper<OneT, Op>(S, OpPC, One, P, /*IsPointerArith=*/true)) {
2381 // Store the new value.
2382 Ptr.deref<Pointer>() = *Result;
2383 return true;
2384 }
2385 return false;
2386}
2387
2388static inline bool IncPtr(InterpState &S, CodePtr OpPC) {
2389 const Pointer &Ptr = S.Stk.pop<Pointer>();
2390
2391 if (!Ptr.isInitialized())
2392 return DiagnoseUninitialized(S, OpPC, Ptr, AK_Increment);
2393
2394 return IncDecPtrHelper<ArithOp::Add>(S, OpPC, Ptr);
2395}
2396
2397static inline bool DecPtr(InterpState &S, CodePtr OpPC) {
2398 const Pointer &Ptr = S.Stk.pop<Pointer>();
2399
2400 if (!Ptr.isInitialized())
2401 return DiagnoseUninitialized(S, OpPC, Ptr, AK_Decrement);
2402
2403 return IncDecPtrHelper<ArithOp::Sub>(S, OpPC, Ptr);
2404}
2405
2406/// 1) Pops a Pointer from the stack.
2407/// 2) Pops another Pointer from the stack.
2408/// 3) Pushes the difference of the indices of the two pointers on the stack.
2409template <PrimType Name, class T = typename PrimConv<Name>::T>
2410inline bool SubPtr(InterpState &S, CodePtr OpPC) {
2411 const Pointer &LHS = S.Stk.pop<Pointer>();
2412 const Pointer &RHS = S.Stk.pop<Pointer>();
2413
2414 if (!Pointer::hasSameBase(LHS, RHS) && S.getLangOpts().CPlusPlus) {
2415 S.FFDiag(S.Current->getSource(OpPC),
2416 diag::note_constexpr_pointer_arith_unspecified)
2418 << RHS.toDiagnosticString(S.getASTContext());
2419 return false;
2420 }
2421
2422 if (LHS == RHS) {
2423 S.Stk.push<T>();
2424 return true;
2425 }
2426
2427 for (const Pointer &P : {LHS, RHS}) {
2428 if (P.isZeroSizeArray()) {
2429 QualType PtrT = P.getType();
2430 while (auto *AT = dyn_cast<ArrayType>(PtrT))
2431 PtrT = AT->getElementType();
2432
2434 PtrT, APInt::getZero(1), nullptr, ArraySizeModifier::Normal, 0);
2435 S.FFDiag(S.Current->getSource(OpPC),
2436 diag::note_constexpr_pointer_subtraction_zero_size)
2437 << ArrayTy;
2438
2439 return false;
2440 }
2441 }
2442
2443 int64_t A64 =
2444 LHS.isBlockPointer()
2445 ? (LHS.isElementPastEnd() ? LHS.getNumElems() : LHS.getIndex())
2447
2448 int64_t B64 =
2449 RHS.isBlockPointer()
2450 ? (RHS.isElementPastEnd() ? RHS.getNumElems() : RHS.getIndex())
2451 : RHS.getIntegerRepresentation();
2452
2453 int64_t R64 = A64 - B64;
2454 if (static_cast<int64_t>(T::from(R64)) != R64)
2455 return handleOverflow(S, OpPC, R64);
2456
2457 S.Stk.push<T>(T::from(R64));
2458 return true;
2459}
2460
2461//===----------------------------------------------------------------------===//
2462// Destroy
2463//===----------------------------------------------------------------------===//
2464
2465inline bool Destroy(InterpState &S, CodePtr OpPC, uint32_t I) {
2466 assert(S.Current->getFunction());
2467
2468 // FIXME: We iterate the scope once here and then again in the destroy() call
2469 // below.
2470 for (auto &Local : S.Current->getFunction()->getScope(I).locals_reverse()) {
2471 const Pointer &Ptr = S.Current->getLocalPointer(Local.Offset);
2472
2473 if (Ptr.getLifetime() == Lifetime::Ended) {
2474 // Try to use the declaration for better diagnostics
2475 if (const Decl *D = Ptr.getDeclDesc()->asDecl()) {
2476 auto *ND = cast<NamedDecl>(D);
2477 S.FFDiag(ND->getLocation(),
2478 diag::note_constexpr_destroy_out_of_lifetime)
2479 << ND->getNameAsString();
2480 } else {
2481 S.FFDiag(Ptr.getDeclDesc()->getLocation(),
2482 diag::note_constexpr_destroy_out_of_lifetime)
2484 }
2485 return false;
2486 }
2487 }
2488
2489 S.Current->destroy(I);
2490 return true;
2491}
2492
2493inline bool InitScope(InterpState &S, CodePtr OpPC, uint32_t I) {
2494 S.Current->initScope(I);
2495 return true;
2496}
2497
2498//===----------------------------------------------------------------------===//
2499// Cast, CastFP
2500//===----------------------------------------------------------------------===//
2501
2502template <PrimType TIn, PrimType TOut> bool Cast(InterpState &S, CodePtr OpPC) {
2503 using T = typename PrimConv<TIn>::T;
2504 using U = typename PrimConv<TOut>::T;
2505 S.Stk.push<U>(U::from(S.Stk.pop<T>()));
2506 return true;
2507}
2508
2509/// 1) Pops a Floating from the stack.
2510/// 2) Pushes a new floating on the stack that uses the given semantics.
2511inline bool CastFP(InterpState &S, CodePtr OpPC, const llvm::fltSemantics *Sem,
2512 llvm::RoundingMode RM) {
2513 Floating F = S.Stk.pop<Floating>();
2514 Floating Result = S.allocFloat(*Sem);
2515 F.toSemantics(Sem, RM, &Result);
2516 S.Stk.push<Floating>(Result);
2517 return true;
2518}
2519
2520inline bool CastFixedPoint(InterpState &S, CodePtr OpPC, uint32_t FPS) {
2521 FixedPointSemantics TargetSemantics =
2522 FixedPointSemantics::getFromOpaqueInt(FPS);
2523 const auto &Source = S.Stk.pop<FixedPoint>();
2524
2525 bool Overflow;
2526 FixedPoint Result = Source.toSemantics(TargetSemantics, &Overflow);
2527
2528 if (Overflow && !handleFixedPointOverflow(S, OpPC, Result))
2529 return false;
2530
2532 return true;
2533}
2534
2535/// Like Cast(), but we cast to an arbitrary-bitwidth integral, so we need
2536/// to know what bitwidth the result should be.
2537template <PrimType Name, class T = typename PrimConv<Name>::T>
2538bool CastAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
2539 auto Result = S.allocAP<IntegralAP<false>>(BitWidth);
2540 // Copy data.
2541 {
2542 APInt Source = S.Stk.pop<T>().toAPSInt().extOrTrunc(BitWidth);
2543 Result.copy(Source);
2544 }
2546 return true;
2547}
2548
2549template <PrimType Name, class T = typename PrimConv<Name>::T>
2550bool CastAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
2551 auto Result = S.allocAP<IntegralAP<true>>(BitWidth);
2552 // Copy data.
2553 {
2554 APInt Source = S.Stk.pop<T>().toAPSInt().extOrTrunc(BitWidth);
2555 Result.copy(Source);
2556 }
2558 return true;
2559}
2560
2561template <PrimType Name, class T = typename PrimConv<Name>::T>
2563 const llvm::fltSemantics *Sem, uint32_t FPOI) {
2564 const T &From = S.Stk.pop<T>();
2565 APSInt FromAP = From.toAPSInt();
2566
2568 Floating Result = S.allocFloat(*Sem);
2569 auto Status =
2570 Floating::fromIntegral(FromAP, *Sem, getRoundingMode(FPO), &Result);
2571 S.Stk.push<Floating>(Result);
2572
2573 return CheckFloatResult(S, OpPC, Result, Status, FPO);
2574}
2575
2576template <PrimType Name, class T = typename PrimConv<Name>::T>
2577bool CastFloatingIntegral(InterpState &S, CodePtr OpPC, uint32_t FPOI) {
2578 const Floating &F = S.Stk.pop<Floating>();
2579
2580 if constexpr (std::is_same_v<T, Boolean>) {
2581 S.Stk.push<T>(T(F.isNonZero()));
2582 return true;
2583 } else {
2584 APSInt Result(std::max(8u, T::bitWidth()),
2585 /*IsUnsigned=*/!T::isSigned());
2586 auto Status = F.convertToInteger(Result);
2587
2588 // Float-to-Integral overflow check.
2589 if ((Status & APFloat::opStatus::opInvalidOp)) {
2590 const Expr *E = S.Current->getExpr(OpPC);
2591 QualType Type = E->getType();
2592
2593 S.CCEDiag(E, diag::note_constexpr_overflow) << F.getAPFloat() << Type;
2594 if (S.noteUndefinedBehavior()) {
2595 S.Stk.push<T>(T(Result));
2596 return true;
2597 }
2598 return false;
2599 }
2600
2602 S.Stk.push<T>(T(Result));
2603 return CheckFloatResult(S, OpPC, F, Status, FPO);
2604 }
2605}
2606
2607static inline bool CastFloatingIntegralAP(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=*/true);
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<false>>(BitWidth);
2621 ResultAP.copy(Result);
2622
2623 S.Stk.push<IntegralAP<false>>(ResultAP);
2624
2625 return CheckFloatResult(S, OpPC, F, Status, FPO);
2626}
2627
2628static inline bool CastFloatingIntegralAPS(InterpState &S, CodePtr OpPC,
2629 uint32_t BitWidth, uint32_t FPOI) {
2630 const Floating &F = S.Stk.pop<Floating>();
2631
2632 APSInt Result(BitWidth, /*IsUnsigned=*/false);
2633 auto Status = F.convertToInteger(Result);
2634
2635 // Float-to-Integral overflow check.
2636 if ((Status & APFloat::opStatus::opInvalidOp) && F.isFinite())
2637 return handleOverflow(S, OpPC, F.getAPFloat());
2638
2640
2641 auto ResultAP = S.allocAP<IntegralAP<true>>(BitWidth);
2642 ResultAP.copy(Result);
2643
2644 S.Stk.push<IntegralAP<true>>(ResultAP);
2645
2646 return CheckFloatResult(S, OpPC, F, Status, FPO);
2647}
2648
2649bool CheckPointerToIntegralCast(InterpState &S, CodePtr OpPC,
2650 const Pointer &Ptr, unsigned BitWidth);
2651bool CastPointerIntegralAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth);
2652bool CastPointerIntegralAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth);
2653
2654template <PrimType Name, class T = typename PrimConv<Name>::T>
2656 const Pointer &Ptr = S.Stk.pop<Pointer>();
2657
2658 S.CCEDiag(S.Current->getSource(OpPC), diag::note_constexpr_invalid_cast)
2659 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
2660 << S.getLangOpts().CPlusPlus << S.Current->getRange(OpPC);
2661
2662 if (!CheckPointerToIntegralCast(S, OpPC, Ptr, T::bitWidth()))
2663 return Invalid(S, OpPC);
2664
2665 S.Stk.push<T>(T::from(Ptr.getIntegerRepresentation()));
2666 return true;
2667}
2668
2669template <PrimType Name, class T = typename PrimConv<Name>::T>
2670static inline bool CastIntegralFixedPoint(InterpState &S, CodePtr OpPC,
2671 uint32_t FPS) {
2672 const T &Int = S.Stk.pop<T>();
2673
2674 FixedPointSemantics Sem = FixedPointSemantics::getFromOpaqueInt(FPS);
2675
2676 bool Overflow;
2677 FixedPoint Result = FixedPoint::from(Int.toAPSInt(), Sem, &Overflow);
2678
2679 if (Overflow && !handleFixedPointOverflow(S, OpPC, Result))
2680 return false;
2681
2683 return true;
2684}
2685
2686static inline bool CastFloatingFixedPoint(InterpState &S, CodePtr OpPC,
2687 uint32_t FPS) {
2688 const auto &Float = S.Stk.pop<Floating>();
2689
2690 FixedPointSemantics Sem = FixedPointSemantics::getFromOpaqueInt(FPS);
2691
2692 bool Overflow;
2693 FixedPoint Result = FixedPoint::from(Float.getAPFloat(), Sem, &Overflow);
2694
2695 if (Overflow && !handleFixedPointOverflow(S, OpPC, Result))
2696 return false;
2697
2699 return true;
2700}
2701
2702static inline bool CastFixedPointFloating(InterpState &S, CodePtr OpPC,
2703 const llvm::fltSemantics *Sem) {
2704 const auto &Fixed = S.Stk.pop<FixedPoint>();
2705 Floating Result = S.allocFloat(*Sem);
2706 Result.copy(Fixed.toFloat(Sem));
2707 S.Stk.push<Floating>(Result);
2708 return true;
2709}
2710
2711template <PrimType Name, class T = typename PrimConv<Name>::T>
2712static inline bool CastFixedPointIntegral(InterpState &S, CodePtr OpPC) {
2713 const auto &Fixed = S.Stk.pop<FixedPoint>();
2714
2715 bool Overflow;
2716 APSInt Int = Fixed.toInt(T::bitWidth(), T::isSigned(), &Overflow);
2717
2718 if (Overflow && !handleOverflow(S, OpPC, Int))
2719 return false;
2720
2721 S.Stk.push<T>(Int);
2722 return true;
2723}
2724
2725static inline bool FnPtrCast(InterpState &S, CodePtr OpPC) {
2726 const SourceInfo &E = S.Current->getSource(OpPC);
2727 S.CCEDiag(E, diag::note_constexpr_invalid_cast)
2728 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
2729 << S.getLangOpts().CPlusPlus << S.Current->getRange(OpPC);
2730 return true;
2731}
2732
2733static inline bool PtrPtrCast(InterpState &S, CodePtr OpPC, bool SrcIsVoidPtr) {
2734 const auto &Ptr = S.Stk.peek<Pointer>();
2735
2736 if (SrcIsVoidPtr && S.getLangOpts().CPlusPlus) {
2737 bool HasValidResult = !Ptr.isZero();
2738
2739 if (HasValidResult) {
2740 if (S.getStdAllocatorCaller("allocate"))
2741 return true;
2742
2743 const auto &E = cast<CastExpr>(S.Current->getExpr(OpPC));
2744 if (S.getLangOpts().CPlusPlus26 &&
2745 S.getASTContext().hasSimilarType(Ptr.getType(),
2746 E->getType()->getPointeeType()))
2747 return true;
2748
2749 S.CCEDiag(E, diag::note_constexpr_invalid_void_star_cast)
2750 << E->getSubExpr()->getType() << S.getLangOpts().CPlusPlus26
2751 << Ptr.getType().getCanonicalType() << E->getType()->getPointeeType();
2752 } else if (!S.getLangOpts().CPlusPlus26) {
2753 const SourceInfo &E = S.Current->getSource(OpPC);
2754 S.CCEDiag(E, diag::note_constexpr_invalid_cast)
2755 << diag::ConstexprInvalidCastKind::CastFrom << "'void *'"
2756 << S.Current->getRange(OpPC);
2757 }
2758 } else {
2759 const SourceInfo &E = S.Current->getSource(OpPC);
2760 S.CCEDiag(E, diag::note_constexpr_invalid_cast)
2761 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
2762 << S.getLangOpts().CPlusPlus << S.Current->getRange(OpPC);
2763 }
2764
2765 return true;
2766}
2767
2768//===----------------------------------------------------------------------===//
2769// Zero, Nullptr
2770//===----------------------------------------------------------------------===//
2771
2772template <PrimType Name, class T = typename PrimConv<Name>::T>
2773bool Zero(InterpState &S, CodePtr OpPC) {
2774 S.Stk.push<T>(T::zero());
2775 return true;
2776}
2777
2778static inline bool ZeroIntAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
2779 auto Result = S.allocAP<IntegralAP<false>>(BitWidth);
2780 if (!Result.singleWord())
2781 std::memset(Result.Memory, 0, Result.numWords() * sizeof(uint64_t));
2783 return true;
2784}
2785
2786static inline bool ZeroIntAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
2787 auto Result = S.allocAP<IntegralAP<true>>(BitWidth);
2788 if (!Result.singleWord())
2789 std::memset(Result.Memory, 0, Result.numWords() * sizeof(uint64_t));
2791 return true;
2792}
2793
2794template <PrimType Name, class T = typename PrimConv<Name>::T>
2795inline bool Null(InterpState &S, CodePtr OpPC, uint64_t Value,
2796 const Descriptor *Desc) {
2797 // FIXME(perf): This is a somewhat often-used function and the value of a
2798 // null pointer is almost always 0.
2799 S.Stk.push<T>(Value, Desc);
2800 return true;
2801}
2802
2803template <PrimType Name, class T = typename PrimConv<Name>::T>
2804inline bool IsNonNull(InterpState &S, CodePtr OpPC) {
2805 const auto &P = S.Stk.pop<T>();
2806 if (P.isWeak())
2807 return false;
2808 S.Stk.push<Boolean>(Boolean::from(!P.isZero()));
2809 return true;
2810}
2811
2812//===----------------------------------------------------------------------===//
2813// This, ImplicitThis
2814//===----------------------------------------------------------------------===//
2815
2816inline bool This(InterpState &S, CodePtr OpPC) {
2817 // Cannot read 'this' in this mode.
2819 return false;
2820 if (!CheckThis(S, OpPC))
2821 return false;
2822 const Pointer &This = S.Current->getThis();
2823
2824 // Ensure the This pointer has been cast to the correct base.
2825 if (!This.isDummy()) {
2827 if (!This.isTypeidPointer()) {
2828 [[maybe_unused]] const Record *R = This.getRecord();
2829 if (!R)
2830 R = This.narrow().getRecord();
2831 assert(R);
2832 assert(R->getDecl() ==
2834 ->getParent());
2835 }
2836 }
2837
2838 S.Stk.push<Pointer>(This);
2839 return true;
2840}
2841
2842inline bool RVOPtr(InterpState &S, CodePtr OpPC) {
2843 assert(S.Current->getFunction()->hasRVO());
2845 return false;
2846 S.Stk.push<Pointer>(S.Current->getRVOPtr());
2847 return true;
2848}
2849
2850//===----------------------------------------------------------------------===//
2851// Shr, Shl
2852//===----------------------------------------------------------------------===//
2853
2854template <class LT, class RT, ShiftDir Dir>
2855inline bool DoShift(InterpState &S, CodePtr OpPC, LT &LHS, RT &RHS,
2856 LT *Result) {
2857 static_assert(!needsAlloc<LT>());
2858 const unsigned Bits = LHS.bitWidth();
2859
2860 // OpenCL 6.3j: shift values are effectively % word size of LHS.
2861 if (S.getLangOpts().OpenCL)
2862 RT::bitAnd(RHS, RT::from(LHS.bitWidth() - 1, RHS.bitWidth()),
2863 RHS.bitWidth(), &RHS);
2864
2865 if (RHS.isNegative()) {
2866 // During constant-folding, a negative shift is an opposite shift. Such a
2867 // shift is not a constant expression.
2868 const SourceInfo &Loc = S.Current->getSource(OpPC);
2869 S.CCEDiag(Loc, diag::note_constexpr_negative_shift) << RHS.toAPSInt();
2870 if (!S.noteUndefinedBehavior())
2871 return false;
2872 RHS = -RHS;
2873 return DoShift<LT, RT,
2875 S, OpPC, LHS, RHS, Result);
2876 }
2877
2878 if (!CheckShift<Dir>(S, OpPC, LHS, RHS, Bits))
2879 return false;
2880
2881 // Limit the shift amount to Bits - 1. If this happened,
2882 // it has already been diagnosed by CheckShift() above,
2883 // but we still need to handle it.
2884 // Note that we have to be extra careful here since we're doing the shift in
2885 // any case, but we need to adjust the shift amount or the way we do the shift
2886 // for the potential error cases.
2887 typename LT::AsUnsigned R;
2888 unsigned MaxShiftAmount = LHS.bitWidth() - 1;
2889 if constexpr (Dir == ShiftDir::Left) {
2890 if (Compare(RHS, RT::from(MaxShiftAmount, RHS.bitWidth())) ==
2892 if (LHS.isNegative())
2893 R = LT::AsUnsigned::zero(LHS.bitWidth());
2894 else {
2895 RHS = RT::from(LHS.countLeadingZeros(), RHS.bitWidth());
2896 LT::AsUnsigned::shiftLeft(LT::AsUnsigned::from(LHS),
2897 LT::AsUnsigned::from(RHS, Bits), Bits, &R);
2898 }
2899 } else if (LHS.isNegative()) {
2900 if (LHS.isMin()) {
2901 R = LT::AsUnsigned::zero(LHS.bitWidth());
2902 } else {
2903 // If the LHS is negative, perform the cast and invert the result.
2904 typename LT::AsUnsigned LHSU = LT::AsUnsigned::from(-LHS);
2905 LT::AsUnsigned::shiftLeft(LHSU, LT::AsUnsigned::from(RHS, Bits), Bits,
2906 &R);
2907 R = -R;
2908 }
2909 } else {
2910 // The good case, a simple left shift.
2911 LT::AsUnsigned::shiftLeft(LT::AsUnsigned::from(LHS),
2912 LT::AsUnsigned::from(RHS, Bits), Bits, &R);
2913 }
2914 S.Stk.push<LT>(LT::from(R));
2915 return true;
2916 }
2917
2918 // Right shift.
2919 if (Compare(RHS, RT::from(MaxShiftAmount, RHS.bitWidth())) ==
2921 R = LT::AsUnsigned::from(-1);
2922 } else {
2923 // Do the shift on potentially signed LT, then convert to unsigned type.
2924 LT A;
2925 LT::shiftRight(LHS, LT::from(RHS, Bits), Bits, &A);
2926 R = LT::AsUnsigned::from(A);
2927 }
2928
2929 S.Stk.push<LT>(LT::from(R));
2930 return true;
2931}
2932
2933/// A version of DoShift that works on IntegralAP.
2934template <class LT, class RT, ShiftDir Dir>
2935inline bool DoShiftAP(InterpState &S, CodePtr OpPC, const APSInt &LHS,
2936 APSInt RHS, LT *Result) {
2937 const unsigned Bits = LHS.getBitWidth();
2938
2939 // OpenCL 6.3j: shift values are effectively % word size of LHS.
2940 if (S.getLangOpts().OpenCL)
2941 RHS &=
2942 APSInt(llvm::APInt(RHS.getBitWidth(), static_cast<uint64_t>(Bits - 1)),
2943 RHS.isUnsigned());
2944
2945 if (RHS.isNegative()) {
2946 // During constant-folding, a negative shift is an opposite shift. Such a
2947 // shift is not a constant expression.
2948 const SourceInfo &Loc = S.Current->getSource(OpPC);
2949 S.CCEDiag(Loc, diag::note_constexpr_negative_shift) << RHS; //.toAPSInt();
2950 if (!S.noteUndefinedBehavior())
2951 return false;
2952 return DoShiftAP<LT, RT,
2954 S, OpPC, LHS, -RHS, Result);
2955 }
2956
2957 if (!CheckShift<Dir>(S, OpPC, static_cast<LT>(LHS), static_cast<RT>(RHS),
2958 Bits))
2959 return false;
2960
2961 unsigned SA = (unsigned)RHS.getLimitedValue(Bits - 1);
2962 if constexpr (Dir == ShiftDir::Left) {
2963 if constexpr (needsAlloc<LT>())
2964 Result->copy(LHS << SA);
2965 else
2966 *Result = LT(LHS << SA);
2967 } else {
2968 if constexpr (needsAlloc<LT>())
2969 Result->copy(LHS >> SA);
2970 else
2971 *Result = LT(LHS >> SA);
2972 }
2973
2974 S.Stk.push<LT>(*Result);
2975 return true;
2976}
2977
2978template <PrimType NameL, PrimType NameR>
2979inline bool Shr(InterpState &S, CodePtr OpPC) {
2980 using LT = typename PrimConv<NameL>::T;
2981 using RT = typename PrimConv<NameR>::T;
2982 auto RHS = S.Stk.pop<RT>();
2983 auto LHS = S.Stk.pop<LT>();
2984
2985 if constexpr (needsAlloc<LT>() || needsAlloc<RT>()) {
2986 LT Result;
2987 if constexpr (needsAlloc<LT>())
2988 Result = S.allocAP<LT>(LHS.bitWidth());
2989 return DoShiftAP<LT, RT, ShiftDir::Right>(S, OpPC, LHS.toAPSInt(),
2990 RHS.toAPSInt(), &Result);
2991 } else {
2992 LT Result;
2993 return DoShift<LT, RT, ShiftDir::Right>(S, OpPC, LHS, RHS, &Result);
2994 }
2995}
2996
2997template <PrimType NameL, PrimType NameR>
2998inline bool Shl(InterpState &S, CodePtr OpPC) {
2999 using LT = typename PrimConv<NameL>::T;
3000 using RT = typename PrimConv<NameR>::T;
3001 auto RHS = S.Stk.pop<RT>();
3002 auto LHS = S.Stk.pop<LT>();
3003
3004 if constexpr (needsAlloc<LT>() || needsAlloc<RT>()) {
3005 LT Result;
3006 if constexpr (needsAlloc<LT>())
3007 Result = S.allocAP<LT>(LHS.bitWidth());
3008 return DoShiftAP<LT, RT, ShiftDir::Left>(S, OpPC, LHS.toAPSInt(),
3009 RHS.toAPSInt(), &Result);
3010 } else {
3011 LT Result;
3012 return DoShift<LT, RT, ShiftDir::Left>(S, OpPC, LHS, RHS, &Result);
3013 }
3014}
3015
3016static inline bool ShiftFixedPoint(InterpState &S, CodePtr OpPC, bool Left) {
3017 const auto &RHS = S.Stk.pop<FixedPoint>();
3018 const auto &LHS = S.Stk.pop<FixedPoint>();
3019 llvm::FixedPointSemantics LHSSema = LHS.getSemantics();
3020
3021 unsigned ShiftBitWidth =
3022 LHSSema.getWidth() - (unsigned)LHSSema.hasUnsignedPadding() - 1;
3023
3024 // Embedded-C 4.1.6.2.2:
3025 // The right operand must be nonnegative and less than the total number
3026 // of (nonpadding) bits of the fixed-point operand ...
3027 if (RHS.isNegative()) {
3028 S.CCEDiag(S.Current->getLocation(OpPC), diag::note_constexpr_negative_shift)
3029 << RHS.toAPSInt();
3030 } else if (static_cast<unsigned>(RHS.toAPSInt().getLimitedValue(
3031 ShiftBitWidth)) != RHS.toAPSInt()) {
3032 const Expr *E = S.Current->getExpr(OpPC);
3033 S.CCEDiag(E, diag::note_constexpr_large_shift)
3034 << RHS.toAPSInt() << E->getType() << ShiftBitWidth;
3035 }
3036
3038 if (Left) {
3039 if (FixedPoint::shiftLeft(LHS, RHS, ShiftBitWidth, &Result) &&
3041 return false;
3042 } else {
3043 if (FixedPoint::shiftRight(LHS, RHS, ShiftBitWidth, &Result) &&
3045 return false;
3046 }
3047
3049 return true;
3050}
3051
3052//===----------------------------------------------------------------------===//
3053// NoRet
3054//===----------------------------------------------------------------------===//
3055
3056inline bool NoRet(InterpState &S, CodePtr OpPC) {
3057 SourceLocation EndLoc = S.Current->getCallee()->getEndLoc();
3058 S.FFDiag(EndLoc, diag::note_constexpr_no_return);
3059 return false;
3060}
3061
3062//===----------------------------------------------------------------------===//
3063// NarrowPtr, ExpandPtr
3064//===----------------------------------------------------------------------===//
3065
3066inline bool NarrowPtr(InterpState &S, CodePtr OpPC) {
3067 const Pointer &Ptr = S.Stk.pop<Pointer>();
3068 S.Stk.push<Pointer>(Ptr.narrow());
3069 return true;
3070}
3071
3072inline bool ExpandPtr(InterpState &S, CodePtr OpPC) {
3073 const Pointer &Ptr = S.Stk.pop<Pointer>();
3074 if (Ptr.isBlockPointer())
3075 S.Stk.push<Pointer>(Ptr.expand());
3076 else
3077 S.Stk.push<Pointer>(Ptr);
3078 return true;
3079}
3080
3081// 1) Pops an integral value from the stack
3082// 2) Peeks a pointer
3083// 3) Pushes a new pointer that's a narrowed array
3084// element of the peeked pointer with the value
3085// from 1) added as offset.
3086//
3087// This leaves the original pointer on the stack and pushes a new one
3088// with the offset applied and narrowed.
3089template <PrimType Name, class T = typename PrimConv<Name>::T>
3090inline bool ArrayElemPtr(InterpState &S, CodePtr OpPC) {
3091 const T &Offset = S.Stk.pop<T>();
3092 const Pointer &Ptr = S.Stk.peek<Pointer>();
3093
3094 if (!Ptr.isZero() && !Offset.isZero()) {
3095 if (!CheckArray(S, OpPC, Ptr))
3096 return false;
3097 }
3098
3099 if (Offset.isZero()) {
3100 if (Ptr.getFieldDesc()->isArray() && Ptr.getIndex() == 0) {
3101 S.Stk.push<Pointer>(Ptr.atIndex(0).narrow());
3102 return true;
3103 }
3104 S.Stk.push<Pointer>(Ptr);
3105 return true;
3106 }
3107
3108 assert(!Offset.isZero());
3109
3110 if (std::optional<Pointer> Result =
3111 OffsetHelper<T, ArithOp::Add>(S, OpPC, Offset, Ptr)) {
3112 S.Stk.push<Pointer>(Result->narrow());
3113 return true;
3114 }
3115
3116 return false;
3117}
3118
3119template <PrimType Name, class T = typename PrimConv<Name>::T>
3120inline bool ArrayElemPtrPop(InterpState &S, CodePtr OpPC) {
3121 const T &Offset = S.Stk.pop<T>();
3122 const Pointer &Ptr = S.Stk.pop<Pointer>();
3123
3124 if (!Ptr.isZero() && !Offset.isZero()) {
3125 if (!CheckArray(S, OpPC, Ptr))
3126 return false;
3127 }
3128
3129 if (Offset.isZero()) {
3130 if (Ptr.getFieldDesc()->isArray() && Ptr.getIndex() == 0) {
3131 S.Stk.push<Pointer>(Ptr.atIndex(0).narrow());
3132 return true;
3133 }
3134 S.Stk.push<Pointer>(Ptr);
3135 return true;
3136 }
3137
3138 assert(!Offset.isZero());
3139
3140 if (std::optional<Pointer> Result =
3141 OffsetHelper<T, ArithOp::Add>(S, OpPC, Offset, Ptr)) {
3142 S.Stk.push<Pointer>(Result->narrow());
3143 return true;
3144 }
3145 return false;
3146}
3147
3148template <PrimType Name, class T = typename PrimConv<Name>::T>
3149inline bool ArrayElem(InterpState &S, CodePtr OpPC, uint32_t Index) {
3150 const Pointer &Ptr = S.Stk.peek<Pointer>();
3151
3152 if (!CheckLoad(S, OpPC, Ptr))
3153 return false;
3154
3155 assert(Ptr.atIndex(Index).getFieldDesc()->getPrimType() == Name);
3156 S.Stk.push<T>(Ptr.elem<T>(Index));
3157 return true;
3158}
3159
3160template <PrimType Name, class T = typename PrimConv<Name>::T>
3161inline bool ArrayElemPop(InterpState &S, CodePtr OpPC, uint32_t Index) {
3162 const Pointer &Ptr = S.Stk.pop<Pointer>();
3163
3164 if (!CheckLoad(S, OpPC, Ptr))
3165 return false;
3166
3167 assert(Ptr.atIndex(Index).getFieldDesc()->getPrimType() == Name);
3168 S.Stk.push<T>(Ptr.elem<T>(Index));
3169 return true;
3170}
3171
3172template <PrimType Name, class T = typename PrimConv<Name>::T>
3173inline bool CopyArray(InterpState &S, CodePtr OpPC, uint32_t SrcIndex,
3174 uint32_t DestIndex, uint32_t Size) {
3175 const auto &SrcPtr = S.Stk.pop<Pointer>();
3176 const auto &DestPtr = S.Stk.peek<Pointer>();
3177
3178 if (SrcPtr.isDummy() || DestPtr.isDummy())
3179 return false;
3180
3181 for (uint32_t I = 0; I != Size; ++I) {
3182 const Pointer &SP = SrcPtr.atIndex(SrcIndex + I);
3183
3184 if (!CheckLoad(S, OpPC, SP))
3185 return false;
3186
3187 const Pointer &DP = DestPtr.atIndex(DestIndex + I);
3188 DP.deref<T>() = SP.deref<T>();
3189 DP.initialize();
3190 }
3191 return true;
3192}
3193
3194/// Just takes a pointer and checks if it's an incomplete
3195/// array type.
3196inline bool ArrayDecay(InterpState &S, CodePtr OpPC) {
3197 const Pointer &Ptr = S.Stk.pop<Pointer>();
3198
3199 if (Ptr.isZero()) {
3200 S.Stk.push<Pointer>(Ptr);
3201 return true;
3202 }
3203
3204 if (!Ptr.isZeroSizeArray()) {
3205 if (!CheckRange(S, OpPC, Ptr, CSK_ArrayToPointer))
3206 return false;
3207 }
3208
3209 if (Ptr.isRoot() || !Ptr.isUnknownSizeArray()) {
3210 S.Stk.push<Pointer>(Ptr.atIndex(0));
3211 return true;
3212 }
3213
3214 const SourceInfo &E = S.Current->getSource(OpPC);
3215 S.FFDiag(E, diag::note_constexpr_unsupported_unsized_array);
3216
3217 return false;
3218}
3219
3220inline bool GetFnPtr(InterpState &S, CodePtr OpPC, const Function *Func) {
3221 assert(Func);
3222 S.Stk.push<Pointer>(Func);
3223 return true;
3224}
3225
3226template <PrimType Name, class T = typename PrimConv<Name>::T>
3227inline bool GetIntPtr(InterpState &S, CodePtr OpPC, const Descriptor *Desc) {
3228 const T &IntVal = S.Stk.pop<T>();
3229
3230 S.CCEDiag(S.Current->getSource(OpPC), diag::note_constexpr_invalid_cast)
3231 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
3232 << S.getLangOpts().CPlusPlus;
3233
3234 S.Stk.push<Pointer>(static_cast<uint64_t>(IntVal), Desc);
3235 return true;
3236}
3237
3238inline bool GetMemberPtr(InterpState &S, CodePtr OpPC, const ValueDecl *D) {
3239 S.Stk.push<MemberPointer>(D);
3240 return true;
3241}
3242
3243inline bool GetMemberPtrBase(InterpState &S, CodePtr OpPC) {
3244 const auto &MP = S.Stk.pop<MemberPointer>();
3245
3246 if (!MP.isBaseCastPossible())
3247 return false;
3248
3249 S.Stk.push<Pointer>(MP.getBase());
3250 return true;
3251}
3252
3253inline bool GetMemberPtrDecl(InterpState &S, CodePtr OpPC) {
3254 const auto &MP = S.Stk.pop<MemberPointer>();
3255
3256 const auto *FD = cast<FunctionDecl>(MP.getDecl());
3257 const auto *Func = S.getContext().getOrCreateFunction(FD);
3258
3259 S.Stk.push<Pointer>(Func);
3260 return true;
3261}
3262
3263/// Just emit a diagnostic. The expression that caused emission of this
3264/// op is not valid in a constant context.
3265inline bool Invalid(InterpState &S, CodePtr OpPC) {
3266 const SourceLocation &Loc = S.Current->getLocation(OpPC);
3267 S.FFDiag(Loc, diag::note_invalid_subexpr_in_const_expr)
3268 << S.Current->getRange(OpPC);
3269 return false;
3270}
3271
3272inline bool Unsupported(InterpState &S, CodePtr OpPC) {
3273 const SourceLocation &Loc = S.Current->getLocation(OpPC);
3274 S.FFDiag(Loc, diag::note_constexpr_stmt_expr_unsupported)
3275 << S.Current->getRange(OpPC);
3276 return false;
3277}
3278
3279inline bool StartSpeculation(InterpState &S, CodePtr OpPC) {
3280 ++S.SpeculationDepth;
3281 if (S.SpeculationDepth != 1)
3282 return true;
3283
3284 assert(S.PrevDiags == nullptr);
3286 S.getEvalStatus().Diag = nullptr;
3287 return true;
3288}
3289inline bool EndSpeculation(InterpState &S, CodePtr OpPC) {
3290 assert(S.SpeculationDepth != 0);
3291 --S.SpeculationDepth;
3292 if (S.SpeculationDepth == 0) {
3294 S.PrevDiags = nullptr;
3295 }
3296 return true;
3297}
3298
3299inline bool PushCC(InterpState &S, CodePtr OpPC, bool Value) {
3301 return true;
3302}
3303inline bool PopCC(InterpState &S, CodePtr OpPC) {
3304 S.ConstantContextOverride = std::nullopt;
3305 return true;
3306}
3307
3308/// Do nothing and just abort execution.
3309inline bool Error(InterpState &S, CodePtr OpPC) { return false; }
3310
3311inline bool SideEffect(InterpState &S, CodePtr OpPC) {
3312 return S.noteSideEffect();
3313}
3314
3315/// Same here, but only for casts.
3316inline bool InvalidCast(InterpState &S, CodePtr OpPC, CastKind Kind,
3317 bool Fatal) {
3318 const SourceLocation &Loc = S.Current->getLocation(OpPC);
3319
3320 if (Kind == CastKind::Reinterpret) {
3321 S.CCEDiag(Loc, diag::note_constexpr_invalid_cast)
3322 << static_cast<unsigned>(Kind) << S.Current->getRange(OpPC);
3323 return !Fatal;
3324 }
3325 if (Kind == CastKind::Volatile) {
3327 const auto *E = cast<CastExpr>(S.Current->getExpr(OpPC));
3328 if (S.getLangOpts().CPlusPlus)
3329 S.FFDiag(E, diag::note_constexpr_access_volatile_type)
3330 << AK_Read << E->getSubExpr()->getType();
3331 else
3332 S.FFDiag(E);
3333 }
3334
3335 return false;
3336 }
3337 if (Kind == CastKind::Dynamic) {
3338 assert(!S.getLangOpts().CPlusPlus20);
3339 S.CCEDiag(S.Current->getSource(OpPC), diag::note_constexpr_invalid_cast)
3340 << diag::ConstexprInvalidCastKind::Dynamic;
3341 return true;
3342 }
3343
3344 return false;
3345}
3346
3347inline bool InvalidDeclRef(InterpState &S, CodePtr OpPC, const DeclRefExpr *DR,
3348 bool InitializerFailed) {
3349 assert(DR);
3350
3351 if (InitializerFailed) {
3352 const SourceInfo &Loc = S.Current->getSource(OpPC);
3353 const auto *VD = cast<VarDecl>(DR->getDecl());
3354 S.FFDiag(Loc, diag::note_constexpr_var_init_non_constant, 1) << VD;
3355 S.Note(VD->getLocation(), diag::note_declared_at);
3356 return false;
3357 }
3358
3359 return CheckDeclRef(S, OpPC, DR);
3360}
3361
3363 if (S.inConstantContext()) {
3364 const SourceRange &ArgRange = S.Current->getRange(OpPC);
3365 const Expr *E = S.Current->getExpr(OpPC);
3366 S.CCEDiag(E, diag::note_constexpr_non_const_vectorelements) << ArgRange;
3367 }
3368 return false;
3369}
3370
3371inline bool CheckPseudoDtor(InterpState &S, CodePtr OpPC) {
3372 if (!S.getLangOpts().CPlusPlus20)
3373 S.CCEDiag(S.Current->getSource(OpPC),
3374 diag::note_constexpr_pseudo_destructor);
3375 return true;
3376}
3377
3378inline bool Assume(InterpState &S, CodePtr OpPC) {
3379 const auto Val = S.Stk.pop<Boolean>();
3380
3381 if (Val)
3382 return true;
3383
3384 // Else, diagnose.
3385 const SourceLocation &Loc = S.Current->getLocation(OpPC);
3386 S.CCEDiag(Loc, diag::note_constexpr_assumption_failed);
3387 return false;
3388}
3389
3390template <PrimType Name, class T = typename PrimConv<Name>::T>
3391inline bool OffsetOf(InterpState &S, CodePtr OpPC, const OffsetOfExpr *E) {
3392 llvm::SmallVector<int64_t> ArrayIndices;
3393 for (size_t I = 0; I != E->getNumExpressions(); ++I)
3394 ArrayIndices.emplace_back(S.Stk.pop<int64_t>());
3395
3396 int64_t Result;
3397 if (!InterpretOffsetOf(S, OpPC, E, ArrayIndices, Result))
3398 return false;
3399
3400 S.Stk.push<T>(T::from(Result));
3401
3402 return true;
3403}
3404
3405template <PrimType Name, class T = typename PrimConv<Name>::T>
3406inline bool CheckNonNullArg(InterpState &S, CodePtr OpPC) {
3407 const T &Arg = S.Stk.peek<T>();
3408 if (!Arg.isZero())
3409 return true;
3410
3411 const SourceLocation &Loc = S.Current->getLocation(OpPC);
3412 S.CCEDiag(Loc, diag::note_non_null_attribute_failed);
3413
3414 return false;
3415}
3416
3417void diagnoseEnumValue(InterpState &S, CodePtr OpPC, const EnumDecl *ED,
3418 const APSInt &Value);
3419
3420template <PrimType Name, class T = typename PrimConv<Name>::T>
3421inline bool CheckEnumValue(InterpState &S, CodePtr OpPC, const EnumDecl *ED) {
3422 assert(ED);
3423 assert(!ED->isFixed());
3424
3425 if (S.inConstantContext()) {
3426 const APSInt Val = S.Stk.peek<T>().toAPSInt();
3427 diagnoseEnumValue(S, OpPC, ED, Val);
3428 }
3429 return true;
3430}
3431
3432/// OldPtr -> Integer -> NewPtr.
3433template <PrimType TIn, PrimType TOut>
3434inline bool DecayPtr(InterpState &S, CodePtr OpPC) {
3435 static_assert(isPtrType(TIn) && isPtrType(TOut));
3436 using FromT = typename PrimConv<TIn>::T;
3437 using ToT = typename PrimConv<TOut>::T;
3438
3439 const FromT &OldPtr = S.Stk.pop<FromT>();
3440
3441 if constexpr (std::is_same_v<FromT, FunctionPointer> &&
3442 std::is_same_v<ToT, Pointer>) {
3443 S.Stk.push<Pointer>(OldPtr.getFunction(), OldPtr.getOffset());
3444 return true;
3445 } else if constexpr (std::is_same_v<FromT, Pointer> &&
3446 std::is_same_v<ToT, FunctionPointer>) {
3447 if (OldPtr.isFunctionPointer()) {
3448 S.Stk.push<FunctionPointer>(OldPtr.asFunctionPointer().getFunction(),
3449 OldPtr.getByteOffset());
3450 return true;
3451 }
3452 }
3453
3454 S.Stk.push<ToT>(ToT(OldPtr.getIntegerRepresentation(), nullptr));
3455 return true;
3456}
3457
3458inline bool CheckDecl(InterpState &S, CodePtr OpPC, const VarDecl *VD) {
3459 // An expression E is a core constant expression unless the evaluation of E
3460 // would evaluate one of the following: [C++23] - a control flow that passes
3461 // through a declaration of a variable with static or thread storage duration
3462 // unless that variable is usable in constant expressions.
3463 assert(VD->isLocalVarDecl() &&
3464 VD->isStaticLocal()); // Checked before emitting this.
3465
3466 if (VD == S.EvaluatingDecl)
3467 return true;
3468
3470 S.CCEDiag(VD->getLocation(), diag::note_constexpr_static_local)
3471 << (VD->getTSCSpec() == TSCS_unspecified ? 0 : 1) << VD;
3472 return false;
3473 }
3474 return true;
3475}
3476
3477inline bool Alloc(InterpState &S, CodePtr OpPC, const Descriptor *Desc) {
3478 assert(Desc);
3479
3480 if (!CheckDynamicMemoryAllocation(S, OpPC))
3481 return false;
3482
3483 DynamicAllocator &Allocator = S.getAllocator();
3484 Block *B = Allocator.allocate(Desc, S.Ctx.getEvalID(),
3486 assert(B);
3487 S.Stk.push<Pointer>(B);
3488 return true;
3489}
3490
3491template <PrimType Name, class SizeT = typename PrimConv<Name>::T>
3492inline bool AllocN(InterpState &S, CodePtr OpPC, PrimType T, const Expr *Source,
3493 bool IsNoThrow) {
3494 if (!CheckDynamicMemoryAllocation(S, OpPC))
3495 return false;
3496
3497 SizeT NumElements = S.Stk.pop<SizeT>();
3498 if (!CheckArraySize(S, OpPC, &NumElements, primSize(T), IsNoThrow)) {
3499 if (!IsNoThrow)
3500 return false;
3501
3502 // If this failed and is nothrow, just return a null ptr.
3503 S.Stk.push<Pointer>(0, nullptr);
3504 return true;
3505 }
3506 if (NumElements.isNegative()) {
3507 if (!IsNoThrow) {
3508 S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_new_negative)
3509 << NumElements.toDiagnosticString(S.getASTContext());
3510 return false;
3511 }
3512 S.Stk.push<Pointer>(0, nullptr);
3513 return true;
3514 }
3515
3516 if (!CheckArraySize(S, OpPC, static_cast<uint64_t>(NumElements)))
3517 return false;
3518
3519 DynamicAllocator &Allocator = S.getAllocator();
3520 Block *B =
3521 Allocator.allocate(Source, T, static_cast<size_t>(NumElements),
3523 assert(B);
3524 if (NumElements.isZero())
3525 S.Stk.push<Pointer>(B);
3526 else
3527 S.Stk.push<Pointer>(Pointer(B).atIndex(0));
3528 return true;
3529}
3530
3531template <PrimType Name, class SizeT = typename PrimConv<Name>::T>
3532inline bool AllocCN(InterpState &S, CodePtr OpPC, const Descriptor *ElementDesc,
3533 bool IsNoThrow) {
3534 if (!CheckDynamicMemoryAllocation(S, OpPC))
3535 return false;
3536
3537 if (!ElementDesc)
3538 return false;
3539
3540 SizeT NumElements = S.Stk.pop<SizeT>();
3541 if (!CheckArraySize(S, OpPC, &NumElements, ElementDesc->getSize(),
3542 IsNoThrow)) {
3543 if (!IsNoThrow)
3544 return false;
3545
3546 // If this failed and is nothrow, just return a null ptr.
3547 S.Stk.push<Pointer>(0, ElementDesc);
3548 return true;
3549 }
3550 assert(NumElements.isPositive());
3551
3552 if (!CheckArraySize(S, OpPC, static_cast<uint64_t>(NumElements)))
3553 return false;
3554
3555 DynamicAllocator &Allocator = S.getAllocator();
3556 Block *B =
3557 Allocator.allocate(ElementDesc, static_cast<size_t>(NumElements),
3559 assert(B);
3560 if (NumElements.isZero())
3561 S.Stk.push<Pointer>(B);
3562 else
3563 S.Stk.push<Pointer>(Pointer(B).atIndex(0));
3564
3565 return true;
3566}
3567
3568bool Free(InterpState &S, CodePtr OpPC, bool DeleteIsArrayForm,
3569 bool IsGlobalDelete);
3570
3571static inline bool IsConstantContext(InterpState &S, CodePtr OpPC) {
3573 return true;
3574}
3575
3576static inline bool CheckAllocations(InterpState &S, CodePtr OpPC) {
3578}
3579
3580/// Check if the initializer and storage types of a placement-new expression
3581/// match.
3582bool CheckNewTypeMismatch(InterpState &S, CodePtr OpPC, const Expr *E,
3583 std::optional<uint64_t> ArraySize = std::nullopt);
3584
3585template <PrimType Name, class T = typename PrimConv<Name>::T>
3587 const auto &Size = S.Stk.pop<T>();
3588 return CheckNewTypeMismatch(S, OpPC, E, static_cast<uint64_t>(Size));
3589}
3590bool InvalidNewDeleteExpr(InterpState &S, CodePtr OpPC, const Expr *E);
3591
3592template <PrimType Name, class T = typename PrimConv<Name>::T>
3593inline bool BitCastPrim(InterpState &S, CodePtr OpPC, bool TargetIsUCharOrByte,
3594 uint32_t ResultBitWidth, const llvm::fltSemantics *Sem,
3595 const Type *TargetType) {
3596 const Pointer &FromPtr = S.Stk.pop<Pointer>();
3597
3598 if (!CheckLoad(S, OpPC, FromPtr))
3599 return false;
3600
3601 if constexpr (std::is_same_v<T, Pointer>) {
3602 if (!TargetType->isNullPtrType()) {
3603 S.FFDiag(S.Current->getSource(OpPC),
3604 diag::note_constexpr_bit_cast_invalid_type)
3605 << /*IsToType=*/true << /*IsReference=*/false << 1 /*Pointer*/;
3606 return false;
3607 }
3608 // The only pointer type we can validly bitcast to is nullptr_t.
3609 S.Stk.push<Pointer>();
3610 return true;
3611 } else if constexpr (std::is_same_v<T, MemberPointer>) {
3612 S.FFDiag(S.Current->getSource(OpPC),
3613 diag::note_constexpr_bit_cast_invalid_type)
3614 << /*IsToType=*/true << /*IsReference=*/false << 2 /*MemberPointer*/;
3615 return false;
3616 } else {
3617
3618 size_t BuffSize = ResultBitWidth / 8;
3619 llvm::SmallVector<std::byte> Buff(BuffSize);
3620 bool HasIndeterminateBits = false;
3621
3622 Bits FullBitWidth(ResultBitWidth);
3623 Bits BitWidth = FullBitWidth;
3624
3625 if constexpr (std::is_same_v<T, Floating>) {
3626 assert(Sem);
3627 BitWidth = Bits(llvm::APFloatBase::getSizeInBits(*Sem));
3628 }
3629
3630 if (!DoBitCast(S, OpPC, FromPtr, Buff.data(), BitWidth, FullBitWidth,
3631 HasIndeterminateBits))
3632 return false;
3633
3634 if (!CheckBitCast(S, OpPC, HasIndeterminateBits, TargetIsUCharOrByte))
3635 return false;
3636
3637 if constexpr (std::is_same_v<T, Floating>) {
3638 assert(Sem);
3639 Floating Result = S.allocFloat(*Sem);
3640 Floating::bitcastFromMemory(Buff.data(), *Sem, &Result);
3641 S.Stk.push<Floating>(Result);
3642 } else if constexpr (needsAlloc<T>()) {
3643 T Result = S.allocAP<T>(ResultBitWidth);
3644 T::bitcastFromMemory(Buff.data(), ResultBitWidth, &Result);
3645 S.Stk.push<T>(Result);
3646 } else if constexpr (std::is_same_v<T, Boolean>) {
3647 // Only allow to cast single-byte integers to bool if they are either 0
3648 // or 1.
3649 assert(FullBitWidth.getQuantity() == 8);
3650 auto Val = static_cast<unsigned int>(Buff[0]);
3651 if (Val > 1) {
3652 S.FFDiag(S.Current->getSource(OpPC),
3653 diag::note_constexpr_bit_cast_unrepresentable_value)
3654 << S.getASTContext().BoolTy << Val;
3655 return false;
3656 }
3657 S.Stk.push<T>(T::bitcastFromMemory(Buff.data(), ResultBitWidth));
3658 } else {
3659 assert(!Sem);
3660 S.Stk.push<T>(T::bitcastFromMemory(Buff.data(), ResultBitWidth));
3661 }
3662 return true;
3663 }
3664}
3665
3666inline bool BitCast(InterpState &S, CodePtr OpPC) {
3667 const Pointer &FromPtr = S.Stk.pop<Pointer>();
3668 Pointer &ToPtr = S.Stk.peek<Pointer>();
3669
3670 if (!CheckLoad(S, OpPC, FromPtr))
3671 return false;
3672
3673 if (!DoBitCastPtr(S, OpPC, FromPtr, ToPtr))
3674 return false;
3675
3676 return true;
3677}
3678
3679/// Typeid support.
3680bool GetTypeid(InterpState &S, CodePtr OpPC, const Type *TypePtr,
3681 const Type *TypeInfoType);
3682bool GetTypeidPtr(InterpState &S, CodePtr OpPC, const Type *TypeInfoType);
3683bool DiagTypeid(InterpState &S, CodePtr OpPC);
3684
3685inline bool CheckDestruction(InterpState &S, CodePtr OpPC) {
3686 const auto &Ptr = S.Stk.peek<Pointer>();
3687 return CheckDestructor(S, OpPC, Ptr);
3688}
3689
3690inline bool CheckArraySize(InterpState &S, CodePtr OpPC, uint64_t NumElems) {
3691 uint64_t Limit = S.getLangOpts().ConstexprStepLimit;
3692 if (NumElems > Limit) {
3693 S.FFDiag(S.Current->getSource(OpPC),
3694 diag::note_constexpr_new_exceeds_limits)
3695 << NumElems << Limit;
3696 return false;
3697 }
3698 return true;
3699}
3700
3701//===----------------------------------------------------------------------===//
3702// Read opcode arguments
3703//===----------------------------------------------------------------------===//
3704
3705template <typename T> inline T ReadArg(InterpState &S, CodePtr &OpPC) {
3706 if constexpr (std::is_pointer<T>::value) {
3707 uint32_t ID = OpPC.read<uint32_t>();
3708 return reinterpret_cast<T>(S.P.getNativePointer(ID));
3709 } else {
3710 return OpPC.read<T>();
3711 }
3712}
3713
3714template <> inline Floating ReadArg<Floating>(InterpState &S, CodePtr &OpPC) {
3715 auto &Semantics =
3716 llvm::APFloatBase::EnumToSemantics(Floating::deserializeSemantics(*OpPC));
3717
3718 auto F = S.allocFloat(Semantics);
3719 Floating::deserialize(*OpPC, &F);
3720 OpPC += align(F.bytesToSerialize());
3721 return F;
3722}
3723
3724template <>
3725inline IntegralAP<false> ReadArg<IntegralAP<false>>(InterpState &S,
3726 CodePtr &OpPC) {
3727 uint32_t BitWidth = IntegralAP<false>::deserializeSize(*OpPC);
3728 auto Result = S.allocAP<IntegralAP<false>>(BitWidth);
3729 assert(Result.bitWidth() == BitWidth);
3730
3732 OpPC += align(Result.bytesToSerialize());
3733 return Result;
3734}
3735
3736template <>
3737inline IntegralAP<true> ReadArg<IntegralAP<true>>(InterpState &S,
3738 CodePtr &OpPC) {
3739 uint32_t BitWidth = IntegralAP<true>::deserializeSize(*OpPC);
3740 auto Result = S.allocAP<IntegralAP<true>>(BitWidth);
3741 assert(Result.bitWidth() == BitWidth);
3742
3743 IntegralAP<true>::deserialize(*OpPC, &Result);
3744 OpPC += align(Result.bytesToSerialize());
3745 return Result;
3746}
3747
3748template <>
3751 OpPC += align(FP.bytesToSerialize());
3752 return FP;
3753}
3754
3755} // namespace interp
3756} // namespace clang
3757
3758#endif
Defines the clang::ASTContext interface.
#define V(N, I)
void HandleComplexComplexDiv(APFloat A, APFloat B, APFloat C, APFloat D, APFloat &ResR, APFloat &ResI)
void HandleComplexComplexMul(APFloat A, APFloat B, APFloat C, APFloat D, APFloat &ResR, APFloat &ResI)
QualType getConstantArrayType(QualType EltTy, const llvm::APInt &ArySize, const Expr *SizeExpr, ArraySizeModifier ASM, unsigned IndexTypeQuals) const
Return the unique reference to the type for a constant array of the specified element type.
CanQualType BoolTy
bool hasSimilarType(QualType T1, QualType T2) const
Determine if two types are similar, according to the C++ rules.
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2129
bool isVirtual() const
Definition DeclCXX.h:2184
CXXRecordDecl * getCanonicalDecl() override
Retrieves the "canonical" declaration of the given declaration.
Definition DeclCXX.h:522
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:2877
unsigned getBuiltinCallee() const
getBuiltinCallee - If this is a call to a builtin, return the builtin ID of the callee.
Definition Expr.cpp:1588
const ValueInfo * getValueInfo(ComparisonCategoryResult ValueKind) const
ComparisonCategoryResult makeWeakResult(ComparisonCategoryResult Res) const
Converts the specified result kind into the correct result kind for this category.
static unsigned getMaxSizeBits(const ASTContext &Context)
Determine the maximum number of active bits that an array's size can require, which limits the maximu...
Definition Type.cpp:254
A reference to a declared variable, function, enum, etc.
Definition Expr.h:1270
ValueDecl * getDecl()
Definition Expr.h:1338
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
SourceLocation getEndLoc() const LLVM_READONLY
Definition DeclBase.h:435
SourceLocation getLocation() const
Definition DeclBase.h:439
AccessSpecifier getAccess() const
Definition DeclBase.h:507
Represents an enum.
Definition Decl.h:4004
bool isFixed() const
Returns true if this is an Objective-C, C++11, or Microsoft-style enumeration with a fixed underlying...
Definition Decl.h:4222
This represents one expression.
Definition Expr.h:112
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition Expr.cpp:273
QualType getType() const
Definition Expr.h:144
static FPOptions getFromOpaqueInt(storage_type Value)
RoundingMode getRoundingMode() const
Represents a member of a struct/union/class.
Definition Decl.h:3157
const RecordDecl * getParent() const
Returns the parent of this field declaration, which is the struct in which this field is defined.
Definition Decl.h:3393
Implicit declaration of a temporary that was materialized by a MaterializeTemporaryExpr and lifetime-...
Definition DeclCXX.h:3308
OffsetOfExpr - [C99 7.17] - This represents an expression of the form offsetof(record-type,...
Definition Expr.h:2527
unsigned getNumExpressions() const
Definition Expr.h:2598
A (possibly-)qualified type.
Definition TypeBase.h:937
Represents a struct/union/class.
Definition Decl.h:4309
Encodes a location in the source.
A trivial tuple used to represent a source range.
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition Stmt.cpp:334
TagDecl * getCanonicalDecl() override
Retrieves the "canonical" declaration of the given declaration.
Definition Decl.cpp:4840
bool isUnion() const
Definition Decl.h:3919
The base class of the type hierarchy.
Definition TypeBase.h:1833
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition Type.h:26
bool isNullPtrType() const
Definition TypeBase.h:8915
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition Decl.h:711
Represents a variable declaration or definition.
Definition Decl.h:925
bool isStaticLocal() const
Returns true if a variable with function scope is a static local variable.
Definition Decl.h:1207
ThreadStorageClassSpecifier getTSCSpec() const
Definition Decl.h:1176
bool isLocalVarDecl() const
Returns true for local variable declarations other than parameters.
Definition Decl.h:1252
bool isUsableInConstantExpressions(const ASTContext &C) const
Determine whether this variable's value can be used in a constant expression, according to the releva...
Definition Decl.cpp:2528
A memory block, either on the stack or in the heap.
Definition InterpBlock.h:44
bool isExtern() const
Checks if the block is extern.
Definition InterpBlock.h:77
const Descriptor * getDescriptor() const
Returns the block's descriptor.
Definition InterpBlock.h:73
std::byte * rawData()
Returns a pointer to the raw data, including metadata.
Wrapper around boolean types.
Definition Boolean.h:25
static Boolean from(T Value)
Definition Boolean.h:97
Pointer into the code segment.
Definition Source.h:30
std::enable_if_t<!std::is_pointer< T >::value, T > read()
Reads data and advances the pointer.
Definition Source.h:56
const Function * getOrCreateFunction(const FunctionDecl *FuncDecl)
Definition Context.cpp:451
unsigned getEvalID() const
Definition Context.h:141
Manages dynamic memory allocations done during bytecode interpretation.
Block * allocate(const Descriptor *D, unsigned EvalID, Form AllocForm)
Allocate ONE element of the given descriptor.
Wrapper around fixed point types.
Definition FixedPoint.h:23
llvm::FixedPointSemantics getSemantics() const
Definition FixedPoint.h:71
static bool shiftRight(const FixedPoint A, const FixedPoint B, unsigned OpBits, FixedPoint *R)
Definition FixedPoint.h:158
static FixedPoint deserialize(const std::byte *Buff)
Definition FixedPoint.h:108
static bool shiftLeft(const FixedPoint A, const FixedPoint B, unsigned OpBits, FixedPoint *R)
Definition FixedPoint.h:151
static FixedPoint from(const APSInt &I, llvm::FixedPointSemantics Sem, bool *Overflow)
Definition FixedPoint.h:40
size_t bytesToSerialize() const
Definition FixedPoint.h:94
If a Floating is constructed from Memory, it DOES NOT OWN THAT MEMORY.
Definition Floating.h:35
static APFloat::opStatus div(const Floating &A, const Floating &B, llvm::RoundingMode RM, Floating *R)
Definition Floating.h:286
static llvm::APFloatBase::Semantics deserializeSemantics(const std::byte *Buff)
Definition Floating.h:211
void copy(const APFloat &F)
Definition Floating.h:122
static APFloat::opStatus fromIntegral(APSInt Val, const llvm::fltSemantics &Sem, llvm::RoundingMode RM, Floating *Result)
Definition Floating.h:171
static APFloat::opStatus sub(const Floating &A, const Floating &B, llvm::RoundingMode RM, Floating *R)
Definition Floating.h:255
static APFloat::opStatus increment(const Floating &A, llvm::RoundingMode RM, Floating *R)
Definition Floating.h:245
static APFloat::opStatus add(const Floating &A, const Floating &B, llvm::RoundingMode RM, Floating *R)
Definition Floating.h:235
static void deserialize(const std::byte *Buff, Floating *Result)
Definition Floating.h:215
static APFloat::opStatus mul(const Floating &A, const Floating &B, llvm::RoundingMode RM, Floating *R)
Definition Floating.h:275
bool isNonZero() const
Definition Floating.h:144
void toSemantics(const llvm::fltSemantics *Sem, llvm::RoundingMode RM, Floating *Result) const
Definition Floating.h:76
const llvm::fltSemantics & getSemantics() const
Definition Floating.h:118
bool isFinite() const
Definition Floating.h:150
static APFloat::opStatus decrement(const Floating &A, llvm::RoundingMode RM, Floating *R)
Definition Floating.h:265
APFloat::opStatus convertToInteger(APSInt &Result) const
Definition Floating.h:70
static void bitcastFromMemory(const std::byte *Buff, const llvm::fltSemantics &Sem, Floating *Result)
Definition Floating.h:181
APFloat getAPFloat() const
Definition Floating.h:63
const Function * getFunction() const
Bytecode function.
Definition Function.h:86
Scope & getScope(unsigned Idx)
Returns a specific scope.
Definition Function.h:147
const FunctionDecl * getDecl() const
Returns the original FunctionDecl.
Definition Function.h:109
bool hasRVO() const
Checks if the first argument is a RVO pointer.
Definition Function.h:129
If an IntegralAP is constructed from Memory, it DOES NOT OWN THAT MEMORY.
Definition IntegralAP.h:36
static uint32_t deserializeSize(const std::byte *Buff)
Definition IntegralAP.h:332
static void deserialize(const std::byte *Buff, IntegralAP< Signed > *Result)
Definition IntegralAP.h:336
void copy(const APInt &V)
Definition IntegralAP.h:78
Wrapper around numeric types.
Definition Integral.h:66
Frame storing local variables.
Definition InterpFrame.h:26
static void free(InterpFrame *F)
Definition InterpFrame.h:48
const Expr * getExpr(CodePtr PC) const
InterpFrame * Caller
The frame of the previous function.
Definition InterpFrame.h:29
SourceInfo getSource(CodePtr PC) const
Map a location to a source.
CodePtr getRetPC() const
Returns the return address of the frame.
Block * getLocalBlock(unsigned Offset) const
SourceLocation getLocation(CodePtr PC) const
const Pointer & getThis() const
Returns the 'this' pointer.
const Function * getFunction() const
Returns the current function.
Definition InterpFrame.h:71
size_t getFrameOffset() const
Returns the offset on the stack at which the frame starts.
Definition InterpFrame.h:74
SourceRange getRange(CodePtr PC) const
void setLocal(unsigned Offset, const T &Value)
Mutates a local variable.
Definition InterpFrame.h:82
const T & getParam(unsigned Offset) const
Returns the value of an argument.
Definition InterpFrame.h:92
Pointer getLocalPointer(unsigned Offset) const
Returns a pointer to a local variables.
unsigned getDepth() const
void setParam(unsigned Offset, const T &Value)
Mutates a local copy of a parameter.
void destroy(unsigned Idx)
Invokes the destructors for a scope.
const Pointer & getRVOPtr() const
Returns the RVO pointer, if the Function has one.
Pointer getParamPointer(unsigned Offset)
Returns a pointer to an argument - lazily creates a block.
const FunctionDecl * getCallee() const override
Returns the caller.
void initScope(unsigned Idx)
T pop()
Returns the value from the top of the stack and removes it.
Definition InterpStack.h:39
void push(Tys &&...Args)
Constructs a value in place on the top of the stack.
Definition InterpStack.h:33
void dump() const
dump the stack contents to stderr.
size_t size() const
Returns the size of the stack in bytes.
Definition InterpStack.h:77
void discard()
Discards the top value from the stack.
Definition InterpStack.h:50
T & peek() const
Returns a reference to the value on the top of the stack.
Definition InterpStack.h:62
Interpreter context.
Definition InterpState.h:43
SmallVectorImpl< PartialDiagnosticAt > * PrevDiags
Things needed to do speculative execution.
Expr::EvalStatus & getEvalStatus() const override
Definition InterpState.h:67
Context & getContext() const
bool noteUndefinedBehavior() override
Definition InterpState.h:82
DynamicAllocator & getAllocator()
Context & Ctx
Interpreter Context.
Floating allocFloat(const llvm::fltSemantics &Sem)
ASTContext & getASTContext() const override
Definition InterpState.h:70
llvm::SmallVector< std::pair< const Expr *, const LifetimeExtendedTemporaryDecl * > > SeenGlobalTemporaries
InterpStack & Stk
Temporary stack.
bool maybeDiagnoseDanglingAllocations()
Diagnose any dynamic allocations that haven't been freed yet.
bool noteSideEffect() override
Definition InterpState.h:94
const VarDecl * EvaluatingDecl
Declaration we're initializing/evaluting, if any.
InterpFrame * Current
The current frame.
std::optional< bool > ConstantContextOverride
T allocAP(unsigned BitWidth)
const LangOptions & getLangOpts() const
Definition InterpState.h:71
StdAllocatorCaller getStdAllocatorCaller(StringRef Name) const
Program & P
Reference to the module containing all bytecode.
ComparisonCategoryResult compare(const MemberPointer &RHS) const
A pointer to a memory block, live or dead.
Definition Pointer.h:91
static bool hasSameBase(const Pointer &A, const Pointer &B)
Checks if two pointers are comparable.
Definition Pointer.cpp:626
Pointer narrow() const
Restricts the scope of an array element pointer.
Definition Pointer.h:188
bool isInitialized() const
Checks if an object was initialized.
Definition Pointer.cpp:432
bool isZeroSizeArray() const
Checks if the pointer is pointing to a zero-size array.
Definition Pointer.h:651
Pointer atIndex(uint64_t Idx) const
Offsets a pointer inside an array.
Definition Pointer.h:156
bool isDummy() const
Checks if the pointer points to a dummy value.
Definition Pointer.h:544
Pointer atFieldSub(unsigned Off) const
Subtract the given offset from the current Base and Offset of the pointer.
Definition Pointer.h:181
int64_t getIndex() const
Returns the index into an array.
Definition Pointer.h:609
Pointer atField(unsigned Off) const
Creates a pointer to a field.
Definition Pointer.h:173
T & deref() const
Dereferences the pointer, if it's live.
Definition Pointer.h:660
unsigned getNumElems() const
Returns the number of elements.
Definition Pointer.h:593
bool isUnknownSizeArray() const
Checks if the structure is an array of unknown size.
Definition Pointer.h:412
void activate() const
Activats a field.
Definition Pointer.cpp:568
static std::optional< std::pair< Pointer, Pointer > > computeSplitPoint(const Pointer &A, const Pointer &B)
Definition Pointer.cpp:678
bool isIntegralPointer() const
Definition Pointer.h:466
bool pointsToStringLiteral() const
Definition Pointer.cpp:666
bool inArray() const
Checks if the innermost field is an array.
Definition Pointer.h:394
T & elem(unsigned I) const
Dereferences the element at index I.
Definition Pointer.h:676
uint64_t getByteOffset() const
Returns the byte offset from the start.
Definition Pointer.h:582
bool isTypeidPointer() const
Definition Pointer.h:468
std::string toDiagnosticString(const ASTContext &Ctx) const
Converts the pointer to a string usable in diagnostics.
Definition Pointer.cpp:419
bool isZero() const
Checks if the pointer is null.
Definition Pointer.h:254
const IntPointer & asIntPointer() const
Definition Pointer.h:452
bool isRoot() const
Pointer points directly to a block.
Definition Pointer.h:434
const Descriptor * getDeclDesc() const
Accessor for information about the declaration site.
Definition Pointer.h:279
unsigned getOffset() const
Returns the offset into an array.
Definition Pointer.h:373
bool isOnePastEnd() const
Checks if the index is one past end.
Definition Pointer.h:626
uint64_t getIntegerRepresentation() const
Definition Pointer.h:143
Pointer expand() const
Expands a pointer to the containing array, undoing narrowing.
Definition Pointer.h:221
bool isElementPastEnd() const
Checks if the pointer is an out-of-bounds element pointer.
Definition Pointer.h:648
bool isBlockPointer() const
Definition Pointer.h:465
const FunctionPointer & asFunctionPointer() const
Definition Pointer.h:456
bool isFunctionPointer() const
Definition Pointer.h:467
const Descriptor * getFieldDesc() const
Accessors for information about the innermost field.
Definition Pointer.h:323
size_t elemSize() const
Returns the element size of the innermost field.
Definition Pointer.h:355
bool canBeInitialized() const
If this pointer has an InlineDescriptor we can use to initialize.
Definition Pointer.h:441
Lifetime getLifetime() const
Definition Pointer.h:721
size_t computeOffsetForComparison() const
Compute an integer that can be used to compare this pointer to another one.
Definition Pointer.cpp:360
const BlockPointer & asBlockPointer() const
Definition Pointer.h:448
void initialize() const
Initializes a field.
Definition Pointer.cpp:485
const Record * getRecord() const
Returns the record descriptor of a class.
Definition Pointer.h:471
Block * getGlobal(unsigned Idx)
Returns the value of a global.
Definition Program.h:71
const void * getNativePointer(unsigned Idx)
Returns the value of a marshalled native pointer.
Definition Program.cpp:30
Pointer getPtrGlobal(unsigned Idx) const
Returns a pointer to a global.
Definition Program.cpp:109
Structure/Class descriptor.
Definition Record.h:25
const RecordDecl * getDecl() const
Returns the underlying declaration.
Definition Record.h:53
llvm::iterator_range< LocalVectorTy::const_reverse_iterator > locals_reverse() const
Definition Function.h:55
Describes the statement/declaration an opcode was generated from.
Definition Source.h:73
bool checkingForUndefinedBehavior() const
Are we checking an expression for overflow?
Definition State.h:103
EvaluationMode EvalMode
Definition State.h:171
OptionalDiagnostic Note(SourceLocation Loc, diag::kind DiagId)
Add a note to a prior diagnostic.
Definition State.cpp:63
DiagnosticBuilder report(SourceLocation Loc, diag::kind DiagId)
Directly reports a diagnostic message.
Definition State.cpp:74
OptionalDiagnostic FFDiag(SourceLocation Loc, diag::kind DiagId=diag::note_invalid_subexpr_in_const_expr, unsigned ExtraNotes=0)
Diagnose that the evaluation could not be folded (FF => FoldFailure)
Definition State.cpp:21
OptionalDiagnostic CCEDiag(SourceLocation Loc, diag::kind DiagId=diag::note_invalid_subexpr_in_const_expr, unsigned ExtraNotes=0)
Diagnose that the evaluation does not produce a C++11 core constant expression.
Definition State.cpp:42
bool checkingPotentialConstantExpression() const
Are we checking whether the expression is a potential constant expression?
Definition State.h:99
#define bool
Definition gpuintrin.h:32
bool arePotentiallyOverlappingStringLiterals(const Pointer &LHS, const Pointer &RHS)
Definition Interp.cpp:2128
bool EndSpeculation(InterpState &S, CodePtr OpPC)
Definition Interp.h:3289
static bool ShiftFixedPoint(InterpState &S, CodePtr OpPC, bool Left)
Definition Interp.h:3016
bool GetPtrFieldPop(InterpState &S, CodePtr OpPC, uint32_t Off)
Definition Interp.cpp:1452
bool InitPop(InterpState &S, CodePtr OpPC)
Definition Interp.h:2111
bool Shr(InterpState &S, CodePtr OpPC)
Definition Interp.h:2979
bool InitGlobalTemp(InterpState &S, CodePtr OpPC, uint32_t I, const LifetimeExtendedTemporaryDecl *Temp)
1) Converts the value on top of the stack to an APValue 2) Sets that APValue on \Temp 3) Initializes ...
Definition Interp.h:1528
bool CheckDestruction(InterpState &S, CodePtr OpPC)
Definition Interp.h:3685
bool ArrayElemPop(InterpState &S, CodePtr OpPC, uint32_t Index)
Definition Interp.h:3161
bool CastPointerIntegralAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth)
Definition Interp.cpp:2058
bool PopCC(InterpState &S, CodePtr OpPC)
Definition Interp.h:3303
bool ArrayElem(InterpState &S, CodePtr OpPC, uint32_t Index)
Definition Interp.h:3149
bool GT(InterpState &S, CodePtr OpPC)
Definition Interp.h:1282
bool CastPointerIntegralAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth)
Definition Interp.cpp:2045
bool CheckInit(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Checks if a value can be initialized.
Definition Interp.cpp:909
static bool CastFloatingIntegralAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth, uint32_t FPOI)
Definition Interp.h:2607
bool GetMemberPtrBase(InterpState &S, CodePtr OpPC)
Definition Interp.h:3243
bool GetThisField(InterpState &S, CodePtr OpPC, uint32_t I)
Definition Interp.h:1440
bool PreInc(InterpState &S, CodePtr OpPC, bool CanOverflow)
Definition Interp.h:887
bool NarrowPtr(InterpState &S, CodePtr OpPC)
Definition Interp.h:3066
bool GetMemberPtrBasePop(InterpState &S, CodePtr OpPC, int32_t Off)
Definition Interp.h:1838
bool InitThisField(InterpState &S, CodePtr OpPC, uint32_t I)
Definition Interp.h:1560
bool BitCastPrim(InterpState &S, CodePtr OpPC, bool TargetIsUCharOrByte, uint32_t ResultBitWidth, const llvm::fltSemantics *Sem, const Type *TargetType)
Definition Interp.h:3593
Floating ReadArg< Floating >(InterpState &S, CodePtr &OpPC)
Definition Interp.h:3714
bool Incf(InterpState &S, CodePtr OpPC, uint32_t FPOI)
Definition Interp.h:950
bool SideEffect(InterpState &S, CodePtr OpPC)
Definition Interp.h:3311
static bool ZeroIntAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth)
Definition Interp.h:2786
bool DoShift(InterpState &S, CodePtr OpPC, LT &LHS, RT &RHS, LT *Result)
Definition Interp.h:2855
bool GetParam(InterpState &S, CodePtr OpPC, uint32_t I)
Definition Interp.h:1377
bool EndLifetimePop(InterpState &S, CodePtr OpPC)
Ends the lifetime of the pop'd pointer.
Definition Interp.cpp:1866
bool Sub(InterpState &S, CodePtr OpPC)
Definition Interp.h:425
bool GetTypeidPtr(InterpState &S, CodePtr OpPC, const Type *TypeInfoType)
Definition Interp.cpp:2094
bool Mulf(InterpState &S, CodePtr OpPC, uint32_t FPOI)
Definition Interp.h:453
bool InitElemPop(InterpState &S, CodePtr OpPC, uint32_t Idx)
The same as InitElem, but pops the pointer as well.
Definition Interp.h:2159
bool StoreBitField(InterpState &S, CodePtr OpPC)
Definition Interp.h:2035
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:3666
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:2795
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:2388
bool CheckDeclRef(InterpState &S, CodePtr OpPC, const DeclRefExpr *DR)
We aleady know the given DeclRefExpr is invalid for some reason, now figure out why and print appropr...
Definition Interp.cpp:1149
bool EndLifetime(InterpState &S, CodePtr OpPC)
Ends the lifetime of the peek'd pointer.
Definition Interp.cpp:1852
bool GetTypeid(InterpState &S, CodePtr OpPC, const Type *TypePtr, const Type *TypeInfoType)
Typeid support.
Definition Interp.cpp:2088
bool Dup(InterpState &S, CodePtr OpPC)
Definition Interp.h:1301
bool SetField(InterpState &S, CodePtr OpPC, uint32_t I)
Definition Interp.h:1408
bool CheckNonNullArg(InterpState &S, CodePtr OpPC)
Definition Interp.h:3406
bool SetThreeWayComparisonField(InterpState &S, CodePtr OpPC, const Pointer &Ptr, const APSInt &IntValue)
Sets the given integral value to the pointer, which is of a std::{weak,partial,strong}...
static bool IncDecPtrHelper(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Definition Interp.h:2363
bool FinishInitActivate(InterpState &S, CodePtr OpPC)
Definition Interp.h:1868
bool GetPtrLocal(InterpState &S, CodePtr OpPC, uint32_t I)
Definition Interp.h:1727
bool Addf(InterpState &S, CodePtr OpPC, uint32_t FPOI)
Definition Interp.h:413
bool CheckDivRem(InterpState &S, CodePtr OpPC, const T &LHS, const T &RHS)
Checks if Div/Rem operation on LHS and RHS is valid.
Definition Interp.h:216
bool CheckConstant(InterpState &S, CodePtr OpPC, const Descriptor *Desc)
Checks if the Descriptor is of a constexpr or const global variable.
Definition Interp.cpp:448
static bool IsOpaqueConstantCall(const CallExpr *E)
Definition Interp.h:1065
bool CheckDecl(InterpState &S, CodePtr OpPC, const VarDecl *VD)
Definition Interp.h:3458
bool CheckPointerToIntegralCast(InterpState &S, CodePtr OpPC, const Pointer &Ptr, unsigned BitWidth)
Definition Interp.cpp:2025
bool AddSubMulHelper(InterpState &S, CodePtr OpPC, unsigned Bits, const T &LHS, const T &RHS)
Definition Interp.h:357
bool GetPtrField(InterpState &S, CodePtr OpPC, uint32_t Off)
1) Peeks a Pointer 2) Pushes Pointer.atField(Off) on the stack
Definition Interp.cpp:1447
bool StoreActivate(InterpState &S, CodePtr OpPC)
Definition Interp.h:2004
bool Div(InterpState &S, CodePtr OpPC)
1) Pops the RHS from the stack.
Definition Interp.h:692
bool CheckMutable(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Checks if a pointer points to a mutable field.
Definition Interp.cpp:594
bool GetFnPtr(InterpState &S, CodePtr OpPC, const Function *Func)
Definition Interp.h:3220
bool FinishInitActivatePop(InterpState &S, CodePtr OpPC)
Definition Interp.h:1877
bool GetGlobalUnchecked(InterpState &S, CodePtr OpPC, uint32_t I)
Same as GetGlobal, but without the checks.
Definition Interp.h:1481
bool SubPtr(InterpState &S, CodePtr OpPC)
1) Pops a Pointer from the stack.
Definition Interp.h:2410
bool CheckSubobject(InterpState &S, CodePtr OpPC, const Pointer &Ptr, CheckSubobjectKind CSK)
Checks if Ptr is a one-past-the-end pointer.
Definition Interp.cpp:541
bool GetPtrGlobal(InterpState &S, CodePtr OpPC, uint32_t I)
Definition Interp.h:1740
static bool Activate(InterpState &S, CodePtr OpPC)
Definition Interp.h:1984
bool handleFixedPointOverflow(InterpState &S, CodePtr OpPC, const FixedPoint &FP)
Definition Interp.cpp:2004
bool Mulc(InterpState &S, CodePtr OpPC)
Definition Interp.h:467
bool RetVoid(InterpState &S, CodePtr &PC)
Definition Interp.h:334
bool ArrayElemPtr(InterpState &S, CodePtr OpPC)
Definition Interp.h:3090
bool NE(InterpState &S, CodePtr OpPC)
Definition Interp.h:1260
bool NoRet(InterpState &S, CodePtr OpPC)
Definition Interp.h:3056
bool GetIntPtr(InterpState &S, CodePtr OpPC, const Descriptor *Desc)
Definition Interp.h:3227
llvm::FixedPointSemantics FixedPointSemantics
Definition Interp.h:41
bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr, AccessKinds AK)
Checks if a value can be loaded from a block.
Definition Interp.cpp:793
static bool FnPtrCast(InterpState &S, CodePtr OpPC)
Definition Interp.h:2725
static bool ZeroIntAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth)
Definition Interp.h:2778
bool Shl(InterpState &S, CodePtr OpPC)
Definition Interp.h:2998
bool RVOPtr(InterpState &S, CodePtr OpPC)
Definition Interp.h:2842
bool CastPointerIntegral(InterpState &S, CodePtr OpPC)
Definition Interp.h:2655
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:2350
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:2550
bool ExpandPtr(InterpState &S, CodePtr OpPC)
Definition Interp.h:3072
bool Store(InterpState &S, CodePtr OpPC)
Definition Interp.h:1961
bool Divc(InterpState &S, CodePtr OpPC)
Definition Interp.h:524
bool DoBitCastPtr(InterpState &S, CodePtr OpPC, const Pointer &FromPtr, Pointer &ToPtr)
bool GetField(InterpState &S, CodePtr OpPC, uint32_t I)
1) Peeks a pointer on the stack 2) Pushes the value of the pointer's field on the stack
Definition Interp.h:1394
bool ArrayElemPtrPop(InterpState &S, CodePtr OpPC)
Definition Interp.h:3120
bool This(InterpState &S, CodePtr OpPC)
Definition Interp.h:2816
bool InitScope(InterpState &S, CodePtr OpPC, uint32_t I)
Definition Interp.h:2493
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:3705
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:2577
bool ArrayDecay(InterpState &S, CodePtr OpPC)
Just takes a pointer and checks if it's an incomplete array type.
Definition Interp.h:3196
bool PushCC(InterpState &S, CodePtr OpPC, bool Value)
Definition Interp.h:3299
bool GetPtrDerivedPop(InterpState &S, CodePtr OpPC, uint32_t Off, bool NullOK, const Type *TargetType)
Definition Interp.h:1760
bool DiagTypeid(InterpState &S, CodePtr OpPC)
Definition Interp.cpp:2120
bool CheckFinalLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
This is not used by any of the opcodes directly.
Definition Interp.cpp:840
bool InitGlobalTempComp(InterpState &S, CodePtr OpPC, const LifetimeExtendedTemporaryDecl *Temp)
1) Converts the value on top of the stack to an APValue 2) Sets that APValue on \Temp 3) Initialized ...
Definition Interp.h:1547
bool CheckBitCast(InterpState &S, CodePtr OpPC, bool HasIndeterminateBits, bool TargetIsUCharOrByte)
Definition Interp.cpp:2071
bool GetLocal(InterpState &S, CodePtr OpPC, uint32_t I)
Definition Interp.h:1355
bool OffsetOf(InterpState &S, CodePtr OpPC, const OffsetOfExpr *E)
Definition Interp.h:3391
bool BitAnd(InterpState &S, CodePtr OpPC)
1) Pops the RHS from the stack.
Definition Interp.h:608
bool CheckShift(InterpState &S, CodePtr OpPC, const LT &LHS, const RT &RHS, unsigned Bits)
Checks if the shift operation is legal.
Definition Interp.h:169
static bool handleOverflow(InterpState &S, CodePtr OpPC, const T &SrcValue)
Definition Interp.h:153
llvm::APInt APInt
Definition FixedPoint.h:19
FixedPoint ReadArg< FixedPoint >(InterpState &S, CodePtr &OpPC)
Definition Interp.h:3749
static bool CastFloatingFixedPoint(InterpState &S, CodePtr OpPC, uint32_t FPS)
Definition Interp.h:2686
void diagnoseEnumValue(InterpState &S, CodePtr OpPC, const EnumDecl *ED, const APSInt &Value)
Definition Interp.cpp:1357
bool StartLifetime(InterpState &S, CodePtr OpPC)
Definition Interp.cpp:1821
bool LE(InterpState &S, CodePtr OpPC)
Definition Interp.h:1274
bool CheckNewTypeMismatchArray(InterpState &S, CodePtr OpPC, const Expr *E)
Definition Interp.h:3586
bool Zero(InterpState &S, CodePtr OpPC)
Definition Interp.h:2773
bool InitThisBitField(InterpState &S, CodePtr OpPC, const Record::Field *F, uint32_t FieldOffset)
Definition Interp.h:1591
bool Unsupported(InterpState &S, CodePtr OpPC)
Definition Interp.h:3272
bool InvalidDeclRef(InterpState &S, CodePtr OpPC, const DeclRefExpr *DR, bool InitializerFailed)
Definition Interp.h:3347
bool CheckStore(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Checks if a value can be stored in a block.
Definition Interp.cpp:871
bool DecPop(InterpState &S, CodePtr OpPC, bool CanOverflow)
1) Pops a pointer from the stack 2) Load the value from the pointer 3) Writes the value decreased by ...
Definition Interp.h:913
bool CheckNull(InterpState &S, CodePtr OpPC, const Pointer &Ptr, CheckSubobjectKind CSK)
Checks if a pointer is null.
Definition Interp.cpp:508
bool CheckDeleteSource(InterpState &S, CodePtr OpPC, const Expr *Source, const Pointer &Ptr)
Check the source of the pointer passed to delete/delete[] has actually been heap allocated by us.
Definition Interp.cpp:1121
bool CheckFloatResult(InterpState &S, CodePtr OpPC, const Floating &Result, APFloat::opStatus Status, FPOptions FPO)
Checks if the result of a floating-point operation is valid in the current context.
Definition Interp.cpp:1047
bool CastFP(InterpState &S, CodePtr OpPC, const llvm::fltSemantics *Sem, llvm::RoundingMode RM)
1) Pops a Floating from the stack.
Definition Interp.h:2511
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:2050
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:2397
constexpr bool needsAlloc()
Definition PrimType.h:125
static bool CheckAllocations(InterpState &S, CodePtr OpPC)
Definition Interp.h:3576
bool Alloc(InterpState &S, CodePtr OpPC, const Descriptor *Desc)
Definition Interp.h:3477
bool InvalidShuffleVectorIndex(InterpState &S, CodePtr OpPC, uint32_t Index)
Definition Interp.cpp:2017
bool ToMemberPtr(InterpState &S, CodePtr OpPC)
Definition Interp.h:2201
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:2670
bool Rem(InterpState &S, CodePtr OpPC)
1) Pops the RHS from the stack.
Definition Interp.h:669
bool VirtBaseHelper(InterpState &S, CodePtr OpPC, const RecordDecl *Decl, const Pointer &Ptr)
Definition Interp.h:1903
bool CheckNewTypeMismatch(InterpState &S, CodePtr OpPC, const Expr *E, std::optional< uint64_t > ArraySize)
Check if the initializer and storage types of a placement-new expression match.
Definition Interp.cpp:1879
bool GetMemberPtr(InterpState &S, CodePtr OpPC, const ValueDecl *D)
Definition Interp.h:3238
bool Dump(InterpState &S, CodePtr OpPC)
Definition Interp.h:1888
bool SizelessVectorElementSize(InterpState &S, CodePtr OpPC)
Definition Interp.h:3362
static bool PtrPtrCast(InterpState &S, CodePtr OpPC, bool SrcIsVoidPtr)
Definition Interp.h:2733
bool CheckLiteralType(InterpState &S, CodePtr OpPC, const Type *T)
Definition Interp.cpp:1381
bool IsNonNull(InterpState &S, CodePtr OpPC)
Definition Interp.h:2804
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:2020
bool GetMemberPtrDecl(InterpState &S, CodePtr OpPC)
Definition Interp.h:3253
bool Comp(InterpState &S, CodePtr OpPC)
1) Pops the value from the stack.
Definition Interp.h:985
static bool CastFixedPointFloating(InterpState &S, CodePtr OpPC, const llvm::fltSemantics *Sem)
Definition Interp.h:2702
bool Divf(InterpState &S, CodePtr OpPC, uint32_t FPOI)
Definition Interp.h:718
bool CheckThis(InterpState &S, CodePtr OpPC)
Checks the 'this' pointer.
Definition Interp.cpp:1030
bool FinishInitGlobal(InterpState &S, CodePtr OpPC)
Definition Interp.cpp:2255
bool DecayPtr(InterpState &S, CodePtr OpPC)
OldPtr -> Integer -> NewPtr.
Definition Interp.h:3434
static bool ActivateThisField(InterpState &S, CodePtr OpPC, uint32_t I)
Definition Interp.h:1991
bool CheckActive(InterpState &S, CodePtr OpPC, const Pointer &Ptr, AccessKinds AK)
Definition Interp.cpp:328
bool GetPtrVirtBasePop(InterpState &S, CodePtr OpPC, const RecordDecl *D)
Definition Interp.h:1914
bool StorePop(InterpState &S, CodePtr OpPC)
Definition Interp.h:1973
void cleanupAfterFunctionCall(InterpState &S, CodePtr OpPC, const Function *Func)
Definition Interp.cpp:261
bool SetLocal(InterpState &S, CodePtr OpPC, uint32_t I)
1) Pops the value from the stack.
Definition Interp.h:1371
bool FinishInit(InterpState &S, CodePtr OpPC)
Definition Interp.h:1861
static bool CastFloatingIntegralAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth, uint32_t FPOI)
Definition Interp.h:2628
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:2125
bool Destroy(InterpState &S, CodePtr OpPC, uint32_t I)
Definition Interp.h:2465
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:2065
bool CheckPseudoDtor(InterpState &S, CodePtr OpPC)
Definition Interp.h:3371
bool Free(InterpState &S, CodePtr OpPC, bool DeleteIsArrayForm, bool IsGlobalDelete)
Definition Interp.cpp:1258
bool PreDec(InterpState &S, CodePtr OpPC, bool CanOverflow)
Definition Interp.h:922
bool InvalidNewDeleteExpr(InterpState &S, CodePtr OpPC, const Expr *E)
Definition Interp.cpp:1960
bool CheckArraySize(InterpState &S, CodePtr OpPC, uint64_t NumElems)
Definition Interp.h:3690
bool CallBI(InterpState &S, CodePtr OpPC, const CallExpr *CE, uint32_t BuiltinID)
Definition Interp.cpp:1737
bool CheckLocalLoad(InterpState &S, CodePtr OpPC, const Block *B)
Definition Interp.cpp:771
bool FinishInitPop(InterpState &S, CodePtr OpPC)
Definition Interp.h:1854
bool Neg(InterpState &S, CodePtr OpPC)
Definition Interp.h:749
bool StartSpeculation(InterpState &S, CodePtr OpPC)
Definition Interp.h:3279
bool CheckExtern(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Checks if the variable has externally defined storage.
Definition Interp.cpp:389
std::optional< Pointer > OffsetHelper(InterpState &S, CodePtr OpPC, const T &Offset, const Pointer &Ptr, bool IsPointerArith=false)
Definition Interp.h:2224
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:2502
bool StoreBitFieldActivatePop(InterpState &S, CodePtr OpPC)
Definition Interp.h:2082
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:2335
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:2191
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:2712
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:2562
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:2520
bool GetPtrParam(InterpState &S, CodePtr OpPC, uint32_t I)
Definition Interp.h:1732
bool AllocCN(InterpState &S, CodePtr OpPC, const Descriptor *ElementDesc, bool IsNoThrow)
Definition Interp.h:3532
bool GetGlobal(InterpState &S, CodePtr OpPC, uint32_t I)
Definition Interp.h:1469
bool Interpret(InterpState &S)
Interpreter entry point.
Definition Interp.cpp:2271
bool Subf(InterpState &S, CodePtr OpPC, uint32_t FPOI)
Definition Interp.h:433
bool GetPtrThisVirtBase(InterpState &S, CodePtr OpPC, const RecordDecl *D)
Definition Interp.h:1923
llvm::APSInt APSInt
Definition FixedPoint.h:20
bool InitGlobal(InterpState &S, CodePtr OpPC, uint32_t I)
Definition Interp.h:1500
bool InvalidCast(InterpState &S, CodePtr OpPC, CastKind Kind, bool Fatal)
Same here, but only for casts.
Definition Interp.h:3316
bool DoShiftAP(InterpState &S, CodePtr OpPC, const APSInt &LHS, APSInt RHS, LT *Result)
A version of DoShift that works on IntegralAP.
Definition Interp.h:2935
bool CastMemberPtrPtr(InterpState &S, CodePtr OpPC)
Definition Interp.h:2209
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:2538
bool Invalid(InterpState &S, CodePtr OpPC)
Just emit a diagnostic.
Definition Interp.h:3265
bool CmpHelper< Pointer >(InterpState &S, CodePtr OpPC, CompareFn Fn)
Definition Interp.h:1023
bool Decf(InterpState &S, CodePtr OpPC, uint32_t FPOI)
Definition Interp.h:966
bool Assume(InterpState &S, CodePtr OpPC)
Definition Interp.h:3378
bool GetPtrThisBase(InterpState &S, CodePtr OpPC, uint32_t Off)
Definition Interp.h:1844
bool IncDecFloatHelper(InterpState &S, CodePtr OpPC, const Pointer &Ptr, uint32_t FPOI)
Definition Interp.h:930
static bool IsConstantContext(InterpState &S, CodePtr OpPC)
Definition Interp.h:3571
bool AllocN(InterpState &S, CodePtr OpPC, PrimType T, const Expr *Source, bool IsNoThrow)
Definition Interp.h:3492
bool CheckEnumValue(InterpState &S, CodePtr OpPC, const EnumDecl *ED)
Definition Interp.h:3421
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
Definition Address.h:330
@ TSCS_unspecified
Definition Specifiers.h:236
ComparisonCategoryResult
An enumeration representing the possible results of a three-way comparison.
CheckSubobjectKind
The order of this enum is important for diagnostics.
Definition State.h:42
@ CSK_ArrayToPointer
Definition State.h:46
@ CSK_Derived
Definition State.h:44
@ CSK_Base
Definition State.h:43
@ CSK_ArrayIndex
Definition State.h:47
@ CSK_Field
Definition State.h:45
@ Result
The result type of a method or function.
Definition TypeBase.h:905
AccessKinds
Kinds of access we can perform on an object, for diagnostics.
Definition State.h:26
@ AK_Increment
Definition State.h:30
@ AK_Read
Definition State.h:27
@ AK_Assign
Definition State.h:29
@ AK_Decrement
Definition State.h:31
const FunctionProtoType * T
@ ConstantFold
Fold the expression to a constant.
Definition State.h:67
U cast(CodeGen::Address addr)
Definition Address.h:327
SmallVectorImpl< PartialDiagnosticAt > * Diag
Diag - If this is non-null, it will be filled in with a stack of notes indicating why evaluation fail...
Definition Expr.h:633
A quantity in bits.
size_t getQuantity() const
unsigned Base
Start of the current subfield.
Definition Pointer.h:39
Block * Pointee
The block the pointer is pointing to.
Definition Pointer.h:37
Describes a memory block created by an allocation site.
Definition Descriptor.h:122
unsigned 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