clang 22.0.0git
Interp.h
Go to the documentation of this file.
1//===--- Interp.h - Interpreter for the constexpr VM ------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Definition of the interpreter state and entry point.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_AST_INTERP_INTERP_H
14#define LLVM_CLANG_AST_INTERP_INTERP_H
15
16#include "../ExprConstShared.h"
17#include "BitcastBuffer.h"
18#include "Boolean.h"
19#include "DynamicAllocator.h"
20#include "FixedPoint.h"
21#include "Floating.h"
22#include "Function.h"
24#include "InterpFrame.h"
25#include "InterpStack.h"
26#include "InterpState.h"
27#include "MemberPointer.h"
28#include "PrimType.h"
29#include "Program.h"
30#include "State.h"
32#include "clang/AST/Expr.h"
33#include "llvm/ADT/APFloat.h"
34#include "llvm/ADT/APSInt.h"
35#include <type_traits>
36
37namespace clang {
38namespace interp {
39
40using APSInt = llvm::APSInt;
41using FixedPointSemantics = llvm::FixedPointSemantics;
42
43/// Checks if the variable has externally defined storage.
44bool CheckExtern(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
45
46/// Checks if the array is offsetable.
47bool CheckArray(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
48
49/// Checks if a pointer is live and accessible.
50bool CheckLive(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
51 AccessKinds AK);
52
53/// Checks if a pointer is a dummy pointer.
54bool CheckDummy(InterpState &S, CodePtr OpPC, const Block *B, AccessKinds AK);
55
56/// Checks if a pointer is null.
57bool CheckNull(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
59
60/// Checks if a pointer is in range.
61bool CheckRange(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
62 AccessKinds AK);
63
64/// Checks if a field from which a pointer is going to be derived is valid.
65bool CheckRange(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
67
68/// Checks if Ptr is a one-past-the-end pointer.
69bool CheckSubobject(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
71
72/// Checks if the dowcast using the given offset is possible with the given
73/// pointer.
74bool CheckDowncast(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
75 uint32_t Offset);
76
77/// Checks if a pointer points to const storage.
78bool CheckConst(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
79
80/// Checks if the Descriptor is of a constexpr or const global variable.
81bool CheckConstant(InterpState &S, CodePtr OpPC, const Descriptor *Desc);
82
83/// Checks if a pointer points to a mutable field.
84bool CheckMutable(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
85
86/// Checks if a value can be loaded from a block.
87bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
88 AccessKinds AK = AK_Read);
89bool CheckFinalLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
90
91bool DiagnoseUninitialized(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
92 AccessKinds AK);
93bool DiagnoseUninitialized(InterpState &S, CodePtr OpPC, bool Extern,
94 const Descriptor *Desc, AccessKinds AK);
95
96/// Checks a direct load of a primitive value from a global or local variable.
97bool CheckGlobalLoad(InterpState &S, CodePtr OpPC, const Block *B);
98bool CheckLocalLoad(InterpState &S, CodePtr OpPC, const Block *B);
99
100/// Checks if a value can be stored in a block.
101bool CheckStore(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
102
103/// Checks if a value can be initialized.
104bool CheckInit(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
105
106/// Checks the 'this' pointer.
107bool CheckThis(InterpState &S, CodePtr OpPC, const Pointer &This);
108
109/// Checks if dynamic memory allocation is available in the current
110/// language mode.
112
113/// Diagnose mismatched new[]/delete or new/delete[] pairs.
115 DynamicAllocator::Form AllocForm,
116 DynamicAllocator::Form DeleteForm, const Descriptor *D,
117 const Expr *NewExpr);
118
119/// Check the source of the pointer passed to delete/delete[] has actually
120/// been heap allocated by us.
121bool CheckDeleteSource(InterpState &S, CodePtr OpPC, const Expr *Source,
122 const Pointer &Ptr);
123
124bool CheckActive(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
125 AccessKinds AK);
126
127/// Sets the given integral value to the pointer, which is of
128/// a std::{weak,partial,strong}_ordering type.
130 const Pointer &Ptr, const APSInt &IntValue);
131
132/// Copy the contents of Src into Dest.
133bool DoMemcpy(InterpState &S, CodePtr OpPC, const Pointer &Src, Pointer &Dest);
134
135bool CallVar(InterpState &S, CodePtr OpPC, const Function *Func,
136 uint32_t VarArgSize);
137bool Call(InterpState &S, CodePtr OpPC, const Function *Func,
138 uint32_t VarArgSize);
139bool CallVirt(InterpState &S, CodePtr OpPC, const Function *Func,
140 uint32_t VarArgSize);
141bool CallBI(InterpState &S, CodePtr OpPC, const CallExpr *CE,
142 uint32_t BuiltinID);
143bool CallPtr(InterpState &S, CodePtr OpPC, uint32_t ArgSize,
144 const CallExpr *CE);
145bool CheckLiteralType(InterpState &S, CodePtr OpPC, const Type *T);
146bool InvalidShuffleVectorIndex(InterpState &S, CodePtr OpPC, uint32_t Index);
147bool CheckBitCast(InterpState &S, CodePtr OpPC, bool HasIndeterminateBits,
148 bool TargetIsUCharOrByte);
149bool CheckBCPResult(InterpState &S, const Pointer &Ptr);
150bool CheckDestructor(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
151
152template <typename T>
153static bool handleOverflow(InterpState &S, CodePtr OpPC, const T &SrcValue) {
154 const Expr *E = S.Current->getExpr(OpPC);
155 S.CCEDiag(E, diag::note_constexpr_overflow) << SrcValue << E->getType();
156 return S.noteUndefinedBehavior();
157}
158bool handleFixedPointOverflow(InterpState &S, CodePtr OpPC,
159 const FixedPoint &FP);
160
161bool isConstexprUnknown(const Pointer &P);
162
163inline bool CheckArraySize(InterpState &S, CodePtr OpPC, uint64_t NumElems);
164
165enum class ShiftDir { Left, Right };
166
167/// Checks if the shift operation is legal.
168template <ShiftDir Dir, typename LT, typename RT>
169bool CheckShift(InterpState &S, CodePtr OpPC, const LT &LHS, const RT &RHS,
170 unsigned Bits) {
171 if (RHS.isNegative()) {
172 const SourceInfo &Loc = S.Current->getSource(OpPC);
173 S.CCEDiag(Loc, diag::note_constexpr_negative_shift) << RHS.toAPSInt();
174 if (!S.noteUndefinedBehavior())
175 return false;
176 }
177
178 // C++11 [expr.shift]p1: Shift width must be less than the bit width of
179 // the shifted type.
180 if (Bits > 1 && RHS >= Bits) {
181 const Expr *E = S.Current->getExpr(OpPC);
182 const APSInt Val = RHS.toAPSInt();
183 QualType Ty = E->getType();
184 S.CCEDiag(E, diag::note_constexpr_large_shift) << Val << Ty << Bits;
185 if (!S.noteUndefinedBehavior())
186 return false;
187 }
188
189 if constexpr (Dir == ShiftDir::Left) {
190 if (LHS.isSigned() && !S.getLangOpts().CPlusPlus20) {
191 // C++11 [expr.shift]p2: A signed left shift must have a non-negative
192 // operand, and must not overflow the corresponding unsigned type.
193 if (LHS.isNegative()) {
194 const Expr *E = S.Current->getExpr(OpPC);
195 S.CCEDiag(E, diag::note_constexpr_lshift_of_negative) << LHS.toAPSInt();
196 if (!S.noteUndefinedBehavior())
197 return false;
198 } else if (LHS.toUnsigned().countLeadingZeros() <
199 static_cast<unsigned>(RHS)) {
200 const Expr *E = S.Current->getExpr(OpPC);
201 S.CCEDiag(E, diag::note_constexpr_lshift_discards);
202 if (!S.noteUndefinedBehavior())
203 return false;
204 }
205 }
206 }
207
208 // C++2a [expr.shift]p2: [P0907R4]:
209 // E1 << E2 is the unique value congruent to
210 // E1 x 2^E2 module 2^N.
211 return true;
212}
213
214/// Checks if Div/Rem operation on LHS and RHS is valid.
215template <typename T>
216bool CheckDivRem(InterpState &S, CodePtr OpPC, const T &LHS, const T &RHS) {
217 if (RHS.isZero()) {
218 const auto *Op = cast<BinaryOperator>(S.Current->getExpr(OpPC));
219 if constexpr (std::is_same_v<T, Floating>) {
220 S.CCEDiag(Op, diag::note_expr_divide_by_zero)
221 << Op->getRHS()->getSourceRange();
222 return true;
223 }
224
225 S.FFDiag(Op, diag::note_expr_divide_by_zero)
226 << Op->getRHS()->getSourceRange();
227 return false;
228 }
229
230 if constexpr (!std::is_same_v<T, FixedPoint>) {
231 if (LHS.isSigned() && LHS.isMin() && RHS.isNegative() && RHS.isMinusOne()) {
232 APSInt LHSInt = LHS.toAPSInt();
233 SmallString<32> Trunc;
234 (-LHSInt.extend(LHSInt.getBitWidth() + 1)).toString(Trunc, 10);
235 const SourceInfo &Loc = S.Current->getSource(OpPC);
236 const Expr *E = S.Current->getExpr(OpPC);
237 S.CCEDiag(Loc, diag::note_constexpr_overflow) << Trunc << E->getType();
238 return false;
239 }
240 }
241 return true;
242}
243
244template <typename SizeT>
245bool CheckArraySize(InterpState &S, CodePtr OpPC, SizeT *NumElements,
246 unsigned ElemSize, bool IsNoThrow) {
247 // FIXME: Both the SizeT::from() as well as the
248 // NumElements.toAPSInt() in this function are rather expensive.
249
250 // Can't be too many elements if the bitwidth of NumElements is lower than
251 // that of Descriptor::MaxArrayElemBytes.
252 if ((NumElements->bitWidth() - NumElements->isSigned()) <
253 (sizeof(Descriptor::MaxArrayElemBytes) * 8))
254 return true;
255
256 // FIXME: GH63562
257 // APValue stores array extents as unsigned,
258 // so anything that is greater that unsigned would overflow when
259 // constructing the array, we catch this here.
260 SizeT MaxElements = SizeT::from(Descriptor::MaxArrayElemBytes / ElemSize);
261 assert(MaxElements.isPositive());
262 if (NumElements->toAPSInt().getActiveBits() >
264 *NumElements > MaxElements) {
265 if (!IsNoThrow) {
266 const SourceInfo &Loc = S.Current->getSource(OpPC);
267
268 if (NumElements->isSigned() && NumElements->isNegative()) {
269 S.FFDiag(Loc, diag::note_constexpr_new_negative)
270 << NumElements->toDiagnosticString(S.getASTContext());
271 } else {
272 S.FFDiag(Loc, diag::note_constexpr_new_too_large)
273 << NumElements->toDiagnosticString(S.getASTContext());
274 }
275 }
276 return false;
277 }
278 return true;
279}
280
281/// Checks if the result of a floating-point operation is valid
282/// in the current context.
283bool CheckFloatResult(InterpState &S, CodePtr OpPC, const Floating &Result,
284 APFloat::opStatus Status, FPOptions FPO);
285
286/// Checks why the given DeclRefExpr is invalid.
287bool CheckDeclRef(InterpState &S, CodePtr OpPC, const DeclRefExpr *DR);
288
289/// Interpreter entry point.
290bool Interpret(InterpState &S);
291
292/// Interpret a builtin function.
293bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const CallExpr *Call,
294 uint32_t BuiltinID);
295
296/// Interpret an offsetof operation.
297bool InterpretOffsetOf(InterpState &S, CodePtr OpPC, const OffsetOfExpr *E,
298 ArrayRef<int64_t> ArrayIndices, int64_t &Result);
299
300inline bool Invalid(InterpState &S, CodePtr OpPC);
301
302enum class ArithOp { Add, Sub };
303
304//===----------------------------------------------------------------------===//
305// Returning values
306//===----------------------------------------------------------------------===//
307
308void cleanupAfterFunctionCall(InterpState &S, CodePtr OpPC,
309 const Function *Func);
310
311template <PrimType Name, class T = typename PrimConv<Name>::T>
312bool Ret(InterpState &S, CodePtr &PC) {
313 const T &Ret = S.Stk.pop<T>();
314
315 assert(S.Current);
316 assert(S.Current->getFrameOffset() == S.Stk.size() && "Invalid frame");
317 if (!S.checkingPotentialConstantExpression() || S.Current->Caller)
318 cleanupAfterFunctionCall(S, PC, S.Current->getFunction());
319
320 if (InterpFrame *Caller = S.Current->Caller) {
321 PC = S.Current->getRetPC();
322 InterpFrame::free(S.Current);
323 S.Current = Caller;
324 S.Stk.push<T>(Ret);
325 } else {
326 InterpFrame::free(S.Current);
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
337 if (!S.checkingPotentialConstantExpression() || S.Current->Caller)
338 cleanupAfterFunctionCall(S, PC, S.Current->getFunction());
339
340 if (InterpFrame *Caller = S.Current->Caller) {
341 PC = S.Current->getRetPC();
342 InterpFrame::free(S.Current);
343 S.Current = Caller;
344 } else {
345 InterpFrame::free(S.Current);
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.
379 if (S.checkingForUndefinedBehavior()) {
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);
420 S.Stk.push<Floating>(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);
440 S.Stk.push<Floating>(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
462 S.Stk.push<Floating>(Result);
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
730 S.Stk.push<Floating>(Result);
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);
775 if (S.checkingForUndefinedBehavior()) {
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 {
792 No,
793 Yes,
794};
795enum class IncDecOp {
796 Inc,
797 Dec,
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.
846 if (S.checkingForUndefinedBehavior()) {
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
870 return IncDecHelper<T, IncDecOp::Inc, PushVal::Yes>(S, OpPC, Ptr,
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
905 return IncDecHelper<T, IncDecOp::Dec, PushVal::Yes>(S, OpPC, Ptr,
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)
936 S.Stk.push<Floating>(Value);
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
1073bool arePotentiallyOverlappingStringLiterals(const Pointer &LHS,
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) {
1344 Floating Result = S.allocFloat(F.getSemantics());
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) {
1378 if (S.checkingPotentialConstantExpression()) {
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) {
1441 if (S.checkingPotentialConstantExpression())
1442 return false;
1443 const Pointer &This = S.Current->getThis();
1444 if (!CheckThis(S, OpPC, This))
1445 return false;
1446 const Pointer &Field = This.atField(I);
1447 if (!CheckLoad(S, OpPC, Field))
1448 return false;
1449 S.Stk.push<T>(Field.deref<T>());
1450 return true;
1451}
1452
1453template <PrimType Name, class T = typename PrimConv<Name>::T>
1454bool SetThisField(InterpState &S, CodePtr OpPC, uint32_t I) {
1455 if (S.checkingPotentialConstantExpression())
1456 return false;
1457 const T &Value = S.Stk.pop<T>();
1458 const Pointer &This = S.Current->getThis();
1459 if (!CheckThis(S, OpPC, This))
1460 return false;
1461 const Pointer &Field = This.atField(I);
1462 if (!CheckStore(S, OpPC, Field))
1463 return false;
1464 Field.deref<T>() = Value;
1465 return true;
1466}
1467
1468template <PrimType Name, class T = typename PrimConv<Name>::T>
1469bool GetGlobal(InterpState &S, CodePtr OpPC, uint32_t I) {
1470 const Block *B = S.P.getGlobal(I);
1471
1472 if (!CheckGlobalLoad(S, OpPC, B))
1473 return false;
1474
1475 S.Stk.push<T>(B->deref<T>());
1476 return true;
1477}
1478
1479/// Same as GetGlobal, but without the checks.
1480template <PrimType Name, class T = typename PrimConv<Name>::T>
1481bool GetGlobalUnchecked(InterpState &S, CodePtr OpPC, uint32_t I) {
1482 const Block *B = S.P.getGlobal(I);
1483 const auto &Desc =
1484 *reinterpret_cast<const GlobalInlineDescriptor *>(B->rawData());
1485 if (Desc.InitState != GlobalInitState::Initialized)
1486 return DiagnoseUninitialized(S, OpPC, B->isExtern(), B->getDescriptor(),
1487 AK_Read);
1488
1489 S.Stk.push<T>(B->deref<T>());
1490 return true;
1491}
1492
1493template <PrimType Name, class T = typename PrimConv<Name>::T>
1494bool SetGlobal(InterpState &S, CodePtr OpPC, uint32_t I) {
1495 // TODO: emit warning.
1496 return false;
1497}
1498
1499template <PrimType Name, class T = typename PrimConv<Name>::T>
1500bool InitGlobal(InterpState &S, CodePtr OpPC, uint32_t I) {
1501 const Pointer &P = S.P.getGlobal(I);
1502
1503 P.deref<T>() = S.Stk.pop<T>();
1504
1505 if constexpr (std::is_same_v<T, Floating>) {
1506 auto &Val = P.deref<Floating>();
1507 if (!Val.singleWord()) {
1508 uint64_t *NewMemory = new (S.P) uint64_t[Val.numWords()];
1509 Val.take(NewMemory);
1510 }
1511
1512 } else if constexpr (needsAlloc<T>()) {
1513 auto &Val = P.deref<T>();
1514 if (!Val.singleWord()) {
1515 uint64_t *NewMemory = new (S.P) uint64_t[Val.numWords()];
1516 Val.take(NewMemory);
1517 }
1518 }
1519
1520 P.initialize();
1521 return true;
1522}
1523
1524/// 1) Converts the value on top of the stack to an APValue
1525/// 2) Sets that APValue on \Temp
1526/// 3) Initializes global with index \I with that
1527template <PrimType Name, class T = typename PrimConv<Name>::T>
1528bool InitGlobalTemp(InterpState &S, CodePtr OpPC, uint32_t I,
1529 const LifetimeExtendedTemporaryDecl *Temp) {
1530 const Pointer &Ptr = S.P.getGlobal(I);
1531
1532 const T Value = S.Stk.peek<T>();
1533 APValue APV = Value.toAPValue(S.getASTContext());
1534 APValue *Cached = Temp->getOrCreateValue(true);
1535 *Cached = APV;
1536
1537 assert(Ptr.getDeclDesc()->asExpr());
1538
1539 S.SeenGlobalTemporaries.push_back(
1540 std::make_pair(Ptr.getDeclDesc()->asExpr(), Temp));
1541
1542 Ptr.deref<T>() = S.Stk.pop<T>();
1543 Ptr.initialize();
1544 return true;
1545}
1546
1547/// 1) Converts the value on top of the stack to an APValue
1548/// 2) Sets that APValue on \Temp
1549/// 3) Initialized global with index \I with that
1551 const LifetimeExtendedTemporaryDecl *Temp) {
1552 assert(Temp);
1553 const Pointer &P = S.Stk.peek<Pointer>();
1554 APValue *Cached = Temp->getOrCreateValue(true);
1555
1556 S.SeenGlobalTemporaries.push_back(
1557 std::make_pair(P.getDeclDesc()->asExpr(), Temp));
1558
1559 if (std::optional<APValue> APV =
1560 P.toRValue(S.getASTContext(), Temp->getTemporaryExpr()->getType())) {
1561 *Cached = *APV;
1562 return true;
1563 }
1564
1565 return false;
1566}
1567
1568template <PrimType Name, class T = typename PrimConv<Name>::T>
1569bool InitThisField(InterpState &S, CodePtr OpPC, uint32_t I) {
1570 if (S.checkingPotentialConstantExpression() && S.Current->getDepth() == 0)
1571 return false;
1572 const Pointer &This = S.Current->getThis();
1573 if (!CheckThis(S, OpPC, This))
1574 return false;
1575 const Pointer &Field = This.atField(I);
1576 assert(Field.canBeInitialized());
1577 Field.deref<T>() = S.Stk.pop<T>();
1578 Field.initialize();
1579 return true;
1580}
1581
1582template <PrimType Name, class T = typename PrimConv<Name>::T>
1583bool InitThisFieldActivate(InterpState &S, CodePtr OpPC, uint32_t I) {
1584 if (S.checkingPotentialConstantExpression() && S.Current->getDepth() == 0)
1585 return false;
1586 const Pointer &This = S.Current->getThis();
1587 if (!CheckThis(S, OpPC, This))
1588 return false;
1589 const Pointer &Field = This.atField(I);
1590 assert(Field.canBeInitialized());
1591 Field.deref<T>() = S.Stk.pop<T>();
1592 Field.activate();
1593 Field.initialize();
1594 return true;
1595}
1596
1597// FIXME: The Field pointer here is too much IMO and we could instead just
1598// pass an Offset + BitWidth pair.
1599template <PrimType Name, class T = typename PrimConv<Name>::T>
1600bool InitThisBitField(InterpState &S, CodePtr OpPC, const Record::Field *F,
1601 uint32_t FieldOffset) {
1602 assert(F->isBitField());
1603 if (S.checkingPotentialConstantExpression() && S.Current->getDepth() == 0)
1604 return false;
1605 const Pointer &This = S.Current->getThis();
1606 if (!CheckThis(S, OpPC, This))
1607 return false;
1608 const Pointer &Field = This.atField(FieldOffset);
1609 assert(Field.canBeInitialized());
1610 const auto &Value = S.Stk.pop<T>();
1611 Field.deref<T>() = Value.truncate(F->Decl->getBitWidthValue());
1612 Field.initialize();
1613 return true;
1614}
1615
1616template <PrimType Name, class T = typename PrimConv<Name>::T>
1618 const Record::Field *F, uint32_t FieldOffset) {
1619 assert(F->isBitField());
1620 if (S.checkingPotentialConstantExpression() && S.Current->getDepth() == 0)
1621 return false;
1622 const Pointer &This = S.Current->getThis();
1623 if (!CheckThis(S, OpPC, This))
1624 return false;
1625 const Pointer &Field = This.atField(FieldOffset);
1626 assert(Field.canBeInitialized());
1627 const auto &Value = S.Stk.pop<T>();
1628 Field.deref<T>() = Value.truncate(F->Decl->getBitWidthValue());
1629 Field.initialize();
1630 Field.activate();
1631 return true;
1632}
1633
1634/// 1) Pops the value from the stack
1635/// 2) Peeks a pointer from the stack
1636/// 3) Pushes the value to field I of the pointer on the stack
1637template <PrimType Name, class T = typename PrimConv<Name>::T>
1638bool InitField(InterpState &S, CodePtr OpPC, uint32_t I) {
1639 const T &Value = S.Stk.pop<T>();
1640 const Pointer &Ptr = S.Stk.peek<Pointer>();
1641 if (!CheckRange(S, OpPC, Ptr, CSK_Field))
1642 return false;
1643 if (!CheckArray(S, OpPC, Ptr))
1644 return false;
1645
1646 const Pointer &Field = Ptr.atField(I);
1647 Field.deref<T>() = Value;
1648 Field.initialize();
1649 return true;
1650}
1651
1652template <PrimType Name, class T = typename PrimConv<Name>::T>
1653bool InitFieldActivate(InterpState &S, CodePtr OpPC, uint32_t I) {
1654 const T &Value = S.Stk.pop<T>();
1655 const Pointer &Ptr = S.Stk.peek<Pointer>();
1656 if (!CheckRange(S, OpPC, Ptr, CSK_Field))
1657 return false;
1658 if (!CheckArray(S, OpPC, Ptr))
1659 return false;
1660
1661 const Pointer &Field = Ptr.atField(I);
1662 Field.deref<T>() = Value;
1663 Field.activate();
1664 Field.initialize();
1665 return true;
1666}
1667
1668template <PrimType Name, class T = typename PrimConv<Name>::T>
1669bool InitBitField(InterpState &S, CodePtr OpPC, const Record::Field *F) {
1670 assert(F->isBitField());
1671 const T &Value = S.Stk.pop<T>();
1672 const Pointer &Ptr = S.Stk.peek<Pointer>();
1673 if (!CheckRange(S, OpPC, Ptr, CSK_Field))
1674 return false;
1675 if (!CheckArray(S, OpPC, Ptr))
1676 return false;
1677
1678 const Pointer &Field = Ptr.atField(F->Offset);
1679
1680 if constexpr (needsAlloc<T>()) {
1681 T Result = S.allocAP<T>(Value.bitWidth());
1682 if (T::isSigned())
1683 Result.copy(Value.toAPSInt()
1684 .trunc(F->Decl->getBitWidthValue())
1685 .sextOrTrunc(Value.bitWidth()));
1686 else
1687 Result.copy(Value.toAPSInt()
1688 .trunc(F->Decl->getBitWidthValue())
1689 .zextOrTrunc(Value.bitWidth()));
1690
1691 Field.deref<T>() = Result;
1692 } else {
1693 Field.deref<T>() = Value.truncate(F->Decl->getBitWidthValue());
1694 }
1695 Field.initialize();
1696 return true;
1697}
1698
1699template <PrimType Name, class T = typename PrimConv<Name>::T>
1701 const Record::Field *F) {
1702 assert(F->isBitField());
1703 const T &Value = S.Stk.pop<T>();
1704 const Pointer &Ptr = S.Stk.peek<Pointer>();
1705 if (!CheckRange(S, OpPC, Ptr, CSK_Field))
1706 return false;
1707 if (!CheckArray(S, OpPC, Ptr))
1708 return false;
1709
1710 const Pointer &Field = Ptr.atField(F->Offset);
1711
1712 if constexpr (needsAlloc<T>()) {
1713 T Result = S.allocAP<T>(Value.bitWidth());
1714 if (T::isSigned())
1715 Result.copy(Value.toAPSInt()
1716 .trunc(F->Decl->getBitWidthValue())
1717 .sextOrTrunc(Value.bitWidth()));
1718 else
1719 Result.copy(Value.toAPSInt()
1720 .trunc(F->Decl->getBitWidthValue())
1721 .zextOrTrunc(Value.bitWidth()));
1722
1723 Field.deref<T>() = Result;
1724 } else {
1725 Field.deref<T>() = Value.truncate(F->Decl->getBitWidthValue());
1726 }
1727 Field.activate();
1728 Field.initialize();
1729 return true;
1730}
1731
1732//===----------------------------------------------------------------------===//
1733// GetPtr Local/Param/Global/Field/This
1734//===----------------------------------------------------------------------===//
1735
1736inline bool GetPtrLocal(InterpState &S, CodePtr OpPC, uint32_t I) {
1737 S.Stk.push<Pointer>(S.Current->getLocalPointer(I));
1738 return true;
1739}
1740
1741inline bool GetPtrParam(InterpState &S, CodePtr OpPC, uint32_t I) {
1742 if (S.checkingPotentialConstantExpression()) {
1743 return false;
1744 }
1745 S.Stk.push<Pointer>(S.Current->getParamPointer(I));
1746 return true;
1747}
1748
1749inline bool GetPtrGlobal(InterpState &S, CodePtr OpPC, uint32_t I) {
1750 S.Stk.push<Pointer>(S.P.getPtrGlobal(I));
1751 return true;
1752}
1753
1754/// 1) Peeks a Pointer
1755/// 2) Pushes Pointer.atField(Off) on the stack
1756bool GetPtrField(InterpState &S, CodePtr OpPC, uint32_t Off);
1757bool GetPtrFieldPop(InterpState &S, CodePtr OpPC, uint32_t Off);
1758
1759inline bool GetPtrThisField(InterpState &S, CodePtr OpPC, uint32_t Off) {
1760 if (S.checkingPotentialConstantExpression() && S.Current->getDepth() == 0)
1761 return false;
1762 const Pointer &This = S.Current->getThis();
1763 if (!CheckThis(S, OpPC, This))
1764 return false;
1765 S.Stk.push<Pointer>(This.atField(Off));
1766 return true;
1767}
1768
1769inline bool GetPtrDerivedPop(InterpState &S, CodePtr OpPC, uint32_t Off,
1770 bool NullOK, const Type *TargetType) {
1771 const Pointer &Ptr = S.Stk.pop<Pointer>();
1772 if (!NullOK && !CheckNull(S, OpPC, Ptr, CSK_Derived))
1773 return false;
1774
1775 if (!Ptr.isBlockPointer()) {
1776 // FIXME: We don't have the necessary information in integral pointers.
1777 // The Descriptor only has a record, but that does of course not include
1778 // the potential derived classes of said record.
1779 S.Stk.push<Pointer>(Ptr);
1780 return true;
1781 }
1782
1783 if (!CheckSubobject(S, OpPC, Ptr, CSK_Derived))
1784 return false;
1785 if (!CheckDowncast(S, OpPC, Ptr, Off))
1786 return false;
1787
1788 const Record *TargetRecord = Ptr.atFieldSub(Off).getRecord();
1789 assert(TargetRecord);
1790
1791 if (TargetRecord->getDecl()->getCanonicalDecl() !=
1792 TargetType->getAsCXXRecordDecl()->getCanonicalDecl()) {
1793 QualType MostDerivedType = Ptr.getDeclDesc()->getType();
1794 S.CCEDiag(S.Current->getSource(OpPC), diag::note_constexpr_invalid_downcast)
1795 << MostDerivedType << QualType(TargetType, 0);
1796 return false;
1797 }
1798
1799 S.Stk.push<Pointer>(Ptr.atFieldSub(Off));
1800 return true;
1801}
1802
1803inline bool GetPtrBase(InterpState &S, CodePtr OpPC, uint32_t Off) {
1804 const Pointer &Ptr = S.Stk.peek<Pointer>();
1805 if (!CheckNull(S, OpPC, Ptr, CSK_Base))
1806 return false;
1807
1808 if (!Ptr.isBlockPointer()) {
1809 if (!Ptr.isIntegralPointer())
1810 return false;
1811 S.Stk.push<Pointer>(Ptr.asIntPointer().baseCast(S.getASTContext(), Off));
1812 return true;
1813 }
1814
1815 if (!CheckSubobject(S, OpPC, Ptr, CSK_Base))
1816 return false;
1817 const Pointer &Result = Ptr.atField(Off);
1818 if (Result.isPastEnd() || !Result.isBaseClass())
1819 return false;
1820 S.Stk.push<Pointer>(Result);
1821 return true;
1822}
1823
1824inline bool GetPtrBasePop(InterpState &S, CodePtr OpPC, uint32_t Off,
1825 bool NullOK) {
1826 const Pointer &Ptr = S.Stk.pop<Pointer>();
1827
1828 if (!NullOK && !CheckNull(S, OpPC, Ptr, CSK_Base))
1829 return false;
1830
1831 if (!Ptr.isBlockPointer()) {
1832 if (!Ptr.isIntegralPointer())
1833 return false;
1834 S.Stk.push<Pointer>(Ptr.asIntPointer().baseCast(S.getASTContext(), Off));
1835 return true;
1836 }
1837
1838 if (!CheckSubobject(S, OpPC, Ptr, CSK_Base))
1839 return false;
1840 const Pointer &Result = Ptr.atField(Off);
1841 if (Result.isPastEnd() || !Result.isBaseClass())
1842 return false;
1843 S.Stk.push<Pointer>(Result);
1844 return true;
1845}
1846
1847inline bool GetMemberPtrBasePop(InterpState &S, CodePtr OpPC, int32_t Off) {
1848 const auto &Ptr = S.Stk.pop<MemberPointer>();
1849 S.Stk.push<MemberPointer>(Ptr.atInstanceBase(Off));
1850 return true;
1851}
1852
1853inline bool GetPtrThisBase(InterpState &S, CodePtr OpPC, uint32_t Off) {
1854 if (S.checkingPotentialConstantExpression())
1855 return false;
1856 const Pointer &This = S.Current->getThis();
1857 if (!CheckThis(S, OpPC, This))
1858 return false;
1859 S.Stk.push<Pointer>(This.atField(Off));
1860 return true;
1861}
1862
1863inline bool FinishInitPop(InterpState &S, CodePtr OpPC) {
1864 const Pointer &Ptr = S.Stk.pop<Pointer>();
1865 if (Ptr.canBeInitialized())
1866 Ptr.initialize();
1867 return true;
1868}
1869
1870inline bool FinishInit(InterpState &S, CodePtr OpPC) {
1871 const Pointer &Ptr = S.Stk.peek<Pointer>();
1872 if (Ptr.canBeInitialized())
1873 Ptr.initialize();
1874 return true;
1875}
1876
1878 const Pointer &Ptr = S.Stk.peek<Pointer>();
1879 if (Ptr.canBeInitialized()) {
1880 Ptr.initialize();
1881 Ptr.activate();
1882 }
1883 return true;
1884}
1885
1887 const Pointer &Ptr = S.Stk.pop<Pointer>();
1888 if (Ptr.canBeInitialized()) {
1889 Ptr.initialize();
1890 Ptr.activate();
1891 }
1892 return true;
1893}
1894
1895bool FinishInitGlobal(InterpState &S, CodePtr OpPC);
1896
1897inline bool Dump(InterpState &S, CodePtr OpPC) {
1898 S.Stk.dump();
1899 return true;
1900}
1901
1902inline bool CheckNull(InterpState &S, CodePtr OpPC) {
1903 const auto &Ptr = S.Stk.peek<Pointer>();
1904 if (Ptr.isZero()) {
1905 S.FFDiag(S.Current->getSource(OpPC),
1906 diag::note_constexpr_dereferencing_null);
1907 return S.noteUndefinedBehavior();
1908 }
1909 return true;
1910}
1911
1912inline bool VirtBaseHelper(InterpState &S, CodePtr OpPC, const RecordDecl *Decl,
1913 const Pointer &Ptr) {
1914 Pointer Base = Ptr;
1915 while (Base.isBaseClass())
1916 Base = Base.getBase();
1917
1918 const Record::Base *VirtBase = Base.getRecord()->getVirtualBase(Decl);
1919 S.Stk.push<Pointer>(Base.atField(VirtBase->Offset));
1920 return true;
1921}
1922
1924 const RecordDecl *D) {
1925 assert(D);
1926 const Pointer &Ptr = S.Stk.pop<Pointer>();
1927 if (!CheckNull(S, OpPC, Ptr, CSK_Base))
1928 return false;
1929 return VirtBaseHelper(S, OpPC, D, Ptr);
1930}
1931
1933 const RecordDecl *D) {
1934 assert(D);
1935 if (S.checkingPotentialConstantExpression())
1936 return false;
1937 const Pointer &This = S.Current->getThis();
1938 if (!CheckThis(S, OpPC, This))
1939 return false;
1940 return VirtBaseHelper(S, OpPC, D, S.Current->getThis());
1941}
1942
1943//===----------------------------------------------------------------------===//
1944// Load, Store, Init
1945//===----------------------------------------------------------------------===//
1946
1947template <PrimType Name, class T = typename PrimConv<Name>::T>
1948bool Load(InterpState &S, CodePtr OpPC) {
1949 const Pointer &Ptr = S.Stk.peek<Pointer>();
1950 if (!CheckLoad(S, OpPC, Ptr))
1951 return false;
1952 if (!Ptr.isBlockPointer())
1953 return false;
1954 S.Stk.push<T>(Ptr.deref<T>());
1955 return true;
1956}
1957
1958template <PrimType Name, class T = typename PrimConv<Name>::T>
1960 const Pointer &Ptr = S.Stk.pop<Pointer>();
1961 if (!CheckLoad(S, OpPC, Ptr))
1962 return false;
1963 if (!Ptr.isBlockPointer())
1964 return false;
1965 S.Stk.push<T>(Ptr.deref<T>());
1966 return true;
1967}
1968
1969template <PrimType Name, class T = typename PrimConv<Name>::T>
1970bool Store(InterpState &S, CodePtr OpPC) {
1971 const T &Value = S.Stk.pop<T>();
1972 const Pointer &Ptr = S.Stk.peek<Pointer>();
1973 if (!CheckStore(S, OpPC, Ptr))
1974 return false;
1975 if (Ptr.canBeInitialized())
1976 Ptr.initialize();
1977 Ptr.deref<T>() = Value;
1978 return true;
1979}
1980
1981template <PrimType Name, class T = typename PrimConv<Name>::T>
1983 const T &Value = S.Stk.pop<T>();
1984 const Pointer &Ptr = S.Stk.pop<Pointer>();
1985 if (!CheckStore(S, OpPC, Ptr))
1986 return false;
1987 if (Ptr.canBeInitialized())
1988 Ptr.initialize();
1989 Ptr.deref<T>() = Value;
1990 return true;
1991}
1992
1993static inline bool Activate(InterpState &S, CodePtr OpPC) {
1994 const Pointer &Ptr = S.Stk.peek<Pointer>();
1995 if (Ptr.canBeInitialized())
1996 Ptr.activate();
1997 return true;
1998}
1999
2000static inline bool ActivateThisField(InterpState &S, CodePtr OpPC, uint32_t I) {
2001 if (S.checkingPotentialConstantExpression())
2002 return false;
2003
2004 const Pointer &Ptr = S.Current->getThis();
2005 assert(Ptr.atField(I).canBeInitialized());
2006 Ptr.atField(I).activate();
2007 return true;
2008}
2009
2010template <PrimType Name, class T = typename PrimConv<Name>::T>
2012 const T &Value = S.Stk.pop<T>();
2013 const Pointer &Ptr = S.Stk.peek<Pointer>();
2014
2015 if (Ptr.canBeInitialized()) {
2016 Ptr.initialize();
2017 Ptr.activate();
2018 }
2019
2020 if (!CheckStore(S, OpPC, Ptr))
2021 return false;
2022 Ptr.deref<T>() = Value;
2023 return true;
2024}
2025
2026template <PrimType Name, class T = typename PrimConv<Name>::T>
2028 const T &Value = S.Stk.pop<T>();
2029 const Pointer &Ptr = S.Stk.pop<Pointer>();
2030
2031 if (Ptr.canBeInitialized()) {
2032 Ptr.initialize();
2033 Ptr.activate();
2034 }
2035 if (!CheckStore(S, OpPC, Ptr))
2036 return false;
2037 Ptr.deref<T>() = Value;
2038 return true;
2039}
2040
2041template <PrimType Name, class T = typename PrimConv<Name>::T>
2043 const T &Value = S.Stk.pop<T>();
2044 const Pointer &Ptr = S.Stk.peek<Pointer>();
2045 if (!CheckStore(S, OpPC, Ptr))
2046 return false;
2047 if (Ptr.canBeInitialized())
2048 Ptr.initialize();
2049 if (const auto *FD = Ptr.getField())
2050 Ptr.deref<T>() = Value.truncate(FD->getBitWidthValue());
2051 else
2052 Ptr.deref<T>() = Value;
2053 return true;
2054}
2055
2056template <PrimType Name, class T = typename PrimConv<Name>::T>
2058 const T &Value = S.Stk.pop<T>();
2059 const Pointer &Ptr = S.Stk.pop<Pointer>();
2060 if (!CheckStore(S, OpPC, Ptr))
2061 return false;
2062 if (Ptr.canBeInitialized())
2063 Ptr.initialize();
2064 if (const auto *FD = Ptr.getField())
2065 Ptr.deref<T>() = Value.truncate(FD->getBitWidthValue());
2066 else
2067 Ptr.deref<T>() = Value;
2068 return true;
2069}
2070
2071template <PrimType Name, class T = typename PrimConv<Name>::T>
2073 const T &Value = S.Stk.pop<T>();
2074 const Pointer &Ptr = S.Stk.peek<Pointer>();
2075 if (Ptr.canBeInitialized()) {
2076 Ptr.initialize();
2077 Ptr.activate();
2078 }
2079 if (!CheckStore(S, OpPC, Ptr))
2080 return false;
2081 if (const auto *FD = Ptr.getField())
2082 Ptr.deref<T>() = Value.truncate(FD->getBitWidthValue());
2083 else
2084 Ptr.deref<T>() = Value;
2085 return true;
2086}
2087
2088template <PrimType Name, class T = typename PrimConv<Name>::T>
2090 const T &Value = S.Stk.pop<T>();
2091 const Pointer &Ptr = S.Stk.pop<Pointer>();
2092
2093 if (Ptr.canBeInitialized()) {
2094 Ptr.initialize();
2095 Ptr.activate();
2096 }
2097 if (!CheckStore(S, OpPC, Ptr))
2098 return false;
2099 if (const auto *FD = Ptr.getField())
2100 Ptr.deref<T>() = Value.truncate(FD->getBitWidthValue());
2101 else
2102 Ptr.deref<T>() = Value;
2103 return true;
2104}
2105
2106template <PrimType Name, class T = typename PrimConv<Name>::T>
2107bool Init(InterpState &S, CodePtr OpPC) {
2108 const T &Value = S.Stk.pop<T>();
2109 const Pointer &Ptr = S.Stk.peek<Pointer>();
2110 if (!CheckInit(S, OpPC, Ptr))
2111 return false;
2112 Ptr.initialize();
2113 new (&Ptr.deref<T>()) T(Value);
2114 return true;
2115}
2116
2117template <PrimType Name, class T = typename PrimConv<Name>::T>
2119 const T &Value = S.Stk.pop<T>();
2120 const Pointer &Ptr = S.Stk.pop<Pointer>();
2121 if (!CheckInit(S, OpPC, Ptr))
2122 return false;
2123 Ptr.initialize();
2124 new (&Ptr.deref<T>()) T(Value);
2125 return true;
2126}
2127
2128/// 1) Pops the value from the stack
2129/// 2) Peeks a pointer and gets its index \Idx
2130/// 3) Sets the value on the pointer, leaving the pointer on the stack.
2131template <PrimType Name, class T = typename PrimConv<Name>::T>
2132bool InitElem(InterpState &S, CodePtr OpPC, uint32_t Idx) {
2133 const T &Value = S.Stk.pop<T>();
2134 const Pointer &Ptr = S.Stk.peek<Pointer>();
2135
2136 if (Ptr.isUnknownSizeArray())
2137 return false;
2138
2139 // In the unlikely event that we're initializing the first item of
2140 // a non-array, skip the atIndex().
2141 if (Idx == 0 && !Ptr.getFieldDesc()->isArray()) {
2142 Ptr.initialize();
2143 new (&Ptr.deref<T>()) T(Value);
2144 return true;
2145 }
2146
2147 const Pointer &ElemPtr = Ptr.atIndex(Idx);
2148 if (!CheckInit(S, OpPC, ElemPtr))
2149 return false;
2150 ElemPtr.initialize();
2151 new (&ElemPtr.deref<T>()) T(Value);
2152 return true;
2153}
2154
2155/// The same as InitElem, but pops the pointer as well.
2156template <PrimType Name, class T = typename PrimConv<Name>::T>
2157bool InitElemPop(InterpState &S, CodePtr OpPC, uint32_t Idx) {
2158 const T &Value = S.Stk.pop<T>();
2159 const Pointer &Ptr = S.Stk.pop<Pointer>();
2160 if (Ptr.isUnknownSizeArray())
2161 return false;
2162
2163 // In the unlikely event that we're initializing the first item of
2164 // a non-array, skip the atIndex().
2165 if (Idx == 0 && !Ptr.getFieldDesc()->isArray()) {
2166 Ptr.initialize();
2167 new (&Ptr.deref<T>()) T(Value);
2168 return true;
2169 }
2170
2171 const Pointer &ElemPtr = Ptr.atIndex(Idx);
2172 if (!CheckInit(S, OpPC, ElemPtr))
2173 return false;
2174 ElemPtr.initialize();
2175 new (&ElemPtr.deref<T>()) T(Value);
2176 return true;
2177}
2178
2179inline bool Memcpy(InterpState &S, CodePtr OpPC) {
2180 const Pointer &Src = S.Stk.pop<Pointer>();
2181 Pointer &Dest = S.Stk.peek<Pointer>();
2182
2183 if (!CheckLoad(S, OpPC, Src))
2184 return false;
2185
2186 return DoMemcpy(S, OpPC, Src, Dest);
2187}
2188
2189inline bool ToMemberPtr(InterpState &S, CodePtr OpPC) {
2190 const auto &Member = S.Stk.pop<MemberPointer>();
2191 const auto &Base = S.Stk.pop<Pointer>();
2192
2193 S.Stk.push<MemberPointer>(Member.takeInstance(Base));
2194 return true;
2195}
2196
2197inline bool CastMemberPtrPtr(InterpState &S, CodePtr OpPC) {
2198 const auto &MP = S.Stk.pop<MemberPointer>();
2199
2200 if (std::optional<Pointer> Ptr = MP.toPointer(S.Ctx)) {
2201 S.Stk.push<Pointer>(*Ptr);
2202 return true;
2203 }
2204 return Invalid(S, OpPC);
2205}
2206
2207//===----------------------------------------------------------------------===//
2208// AddOffset, SubOffset
2209//===----------------------------------------------------------------------===//
2210
2211template <class T, ArithOp Op>
2212std::optional<Pointer> OffsetHelper(InterpState &S, CodePtr OpPC,
2213 const T &Offset, const Pointer &Ptr,
2214 bool IsPointerArith = false) {
2215 // A zero offset does not change the pointer.
2216 if (Offset.isZero())
2217 return Ptr;
2218
2219 if (IsPointerArith && !CheckNull(S, OpPC, Ptr, CSK_ArrayIndex)) {
2220 // The CheckNull will have emitted a note already, but we only
2221 // abort in C++, since this is fine in C.
2222 if (S.getLangOpts().CPlusPlus)
2223 return std::nullopt;
2224 }
2225
2226 // Arrays of unknown bounds cannot have pointers into them.
2227 if (!CheckArray(S, OpPC, Ptr))
2228 return std::nullopt;
2229
2230 // This is much simpler for integral pointers, so handle them first.
2231 if (Ptr.isIntegralPointer()) {
2232 uint64_t V = Ptr.getIntegerRepresentation();
2233 uint64_t O = static_cast<uint64_t>(Offset) * Ptr.elemSize();
2234 if constexpr (Op == ArithOp::Add)
2235 return Pointer(V + O, Ptr.asIntPointer().Desc);
2236 else
2237 return Pointer(V - O, Ptr.asIntPointer().Desc);
2238 } else if (Ptr.isFunctionPointer()) {
2239 uint64_t O = static_cast<uint64_t>(Offset);
2240 uint64_t N;
2241 if constexpr (Op == ArithOp::Add)
2242 N = Ptr.getByteOffset() + O;
2243 else
2244 N = Ptr.getByteOffset() - O;
2245
2246 if (N > 1)
2247 S.CCEDiag(S.Current->getSource(OpPC), diag::note_constexpr_array_index)
2248 << N << /*non-array*/ true << 0;
2249 return Pointer(Ptr.asFunctionPointer().getFunction(), N);
2250 }
2251
2252 assert(Ptr.isBlockPointer());
2253
2254 uint64_t MaxIndex = static_cast<uint64_t>(Ptr.getNumElems());
2255 uint64_t Index;
2256 if (Ptr.isOnePastEnd())
2257 Index = MaxIndex;
2258 else
2259 Index = Ptr.getIndex();
2260
2261 bool Invalid = false;
2262 // Helper to report an invalid offset, computed as APSInt.
2263 auto DiagInvalidOffset = [&]() -> void {
2264 const unsigned Bits = Offset.bitWidth();
2265 APSInt APOffset(Offset.toAPSInt().extend(Bits + 2), /*IsUnsigend=*/false);
2266 APSInt APIndex(APInt(Bits + 2, Index, /*IsSigned=*/true),
2267 /*IsUnsigned=*/false);
2268 APSInt NewIndex =
2269 (Op == ArithOp::Add) ? (APIndex + APOffset) : (APIndex - APOffset);
2270 S.CCEDiag(S.Current->getSource(OpPC), diag::note_constexpr_array_index)
2271 << NewIndex << /*array*/ static_cast<int>(!Ptr.inArray()) << MaxIndex;
2272 Invalid = true;
2273 };
2274
2275 if (Ptr.isBlockPointer()) {
2276 uint64_t IOffset = static_cast<uint64_t>(Offset);
2277 uint64_t MaxOffset = MaxIndex - Index;
2278
2279 if constexpr (Op == ArithOp::Add) {
2280 // If the new offset would be negative, bail out.
2281 if (Offset.isNegative() && (Offset.isMin() || -IOffset > Index))
2282 DiagInvalidOffset();
2283
2284 // If the new offset would be out of bounds, bail out.
2285 if (Offset.isPositive() && IOffset > MaxOffset)
2286 DiagInvalidOffset();
2287 } else {
2288 // If the new offset would be negative, bail out.
2289 if (Offset.isPositive() && Index < IOffset)
2290 DiagInvalidOffset();
2291
2292 // If the new offset would be out of bounds, bail out.
2293 if (Offset.isNegative() && (Offset.isMin() || -IOffset > MaxOffset))
2294 DiagInvalidOffset();
2295 }
2296 }
2297
2298 if (Invalid && S.getLangOpts().CPlusPlus)
2299 return std::nullopt;
2300
2301 // Offset is valid - compute it on unsigned.
2302 int64_t WideIndex = static_cast<int64_t>(Index);
2303 int64_t WideOffset = static_cast<int64_t>(Offset);
2304 int64_t Result;
2305 if constexpr (Op == ArithOp::Add)
2306 Result = WideIndex + WideOffset;
2307 else
2308 Result = WideIndex - WideOffset;
2309
2310 // When the pointer is one-past-end, going back to index 0 is the only
2311 // useful thing we can do. Any other index has been diagnosed before and
2312 // we don't get here.
2313 if (Result == 0 && Ptr.isOnePastEnd()) {
2314 if (Ptr.getFieldDesc()->isArray())
2315 return Ptr.atIndex(0);
2316 return Pointer(Ptr.asBlockPointer().Pointee, Ptr.asBlockPointer().Base);
2317 }
2318
2319 return Ptr.atIndex(static_cast<uint64_t>(Result));
2320}
2321
2322template <PrimType Name, class T = typename PrimConv<Name>::T>
2324 const T &Offset = S.Stk.pop<T>();
2325 Pointer Ptr = S.Stk.pop<Pointer>();
2326 if (Ptr.isBlockPointer())
2327 Ptr = Ptr.expand();
2328
2329 if (std::optional<Pointer> Result = OffsetHelper<T, ArithOp::Add>(
2330 S, OpPC, Offset, Ptr, /*IsPointerArith=*/true)) {
2331 S.Stk.push<Pointer>(*Result);
2332 return true;
2333 }
2334 return false;
2335}
2336
2337template <PrimType Name, class T = typename PrimConv<Name>::T>
2339 const T &Offset = S.Stk.pop<T>();
2340 const Pointer &Ptr = S.Stk.pop<Pointer>();
2341
2342 if (std::optional<Pointer> Result = OffsetHelper<T, ArithOp::Sub>(
2343 S, OpPC, Offset, Ptr, /*IsPointerArith=*/true)) {
2344 S.Stk.push<Pointer>(*Result);
2345 return true;
2346 }
2347 return false;
2348}
2349
2350template <ArithOp Op>
2351static inline bool IncDecPtrHelper(InterpState &S, CodePtr OpPC,
2352 const Pointer &Ptr) {
2353 if (Ptr.isDummy())
2354 return false;
2355
2356 using OneT = Integral<8, false>;
2357
2358 const Pointer &P = Ptr.deref<Pointer>();
2359 if (!CheckNull(S, OpPC, P, CSK_ArrayIndex))
2360 return false;
2361
2362 // Get the current value on the stack.
2363 S.Stk.push<Pointer>(P);
2364
2365 // Now the current Ptr again and a constant 1.
2366 OneT One = OneT::from(1);
2367 if (std::optional<Pointer> Result =
2368 OffsetHelper<OneT, Op>(S, OpPC, One, P, /*IsPointerArith=*/true)) {
2369 // Store the new value.
2370 Ptr.deref<Pointer>() = *Result;
2371 return true;
2372 }
2373 return false;
2374}
2375
2376static inline bool IncPtr(InterpState &S, CodePtr OpPC) {
2377 const Pointer &Ptr = S.Stk.pop<Pointer>();
2378
2379 if (!Ptr.isInitialized())
2380 return DiagnoseUninitialized(S, OpPC, Ptr, AK_Increment);
2381
2382 return IncDecPtrHelper<ArithOp::Add>(S, OpPC, Ptr);
2383}
2384
2385static inline bool DecPtr(InterpState &S, CodePtr OpPC) {
2386 const Pointer &Ptr = S.Stk.pop<Pointer>();
2387
2388 if (!Ptr.isInitialized())
2389 return DiagnoseUninitialized(S, OpPC, Ptr, AK_Decrement);
2390
2391 return IncDecPtrHelper<ArithOp::Sub>(S, OpPC, Ptr);
2392}
2393
2394/// 1) Pops a Pointer from the stack.
2395/// 2) Pops another Pointer from the stack.
2396/// 3) Pushes the difference of the indices of the two pointers on the stack.
2397template <PrimType Name, class T = typename PrimConv<Name>::T>
2398inline bool SubPtr(InterpState &S, CodePtr OpPC) {
2399 const Pointer &LHS = S.Stk.pop<Pointer>();
2400 const Pointer &RHS = S.Stk.pop<Pointer>();
2401
2402 if (!Pointer::hasSameBase(LHS, RHS) && S.getLangOpts().CPlusPlus) {
2403 S.FFDiag(S.Current->getSource(OpPC),
2404 diag::note_constexpr_pointer_arith_unspecified)
2406 << RHS.toDiagnosticString(S.getASTContext());
2407 return false;
2408 }
2409
2410 if (LHS == RHS) {
2411 S.Stk.push<T>();
2412 return true;
2413 }
2414
2415 for (const Pointer &P : {LHS, RHS}) {
2416 if (P.isZeroSizeArray()) {
2417 QualType PtrT = P.getType();
2418 while (auto *AT = dyn_cast<ArrayType>(PtrT))
2419 PtrT = AT->getElementType();
2420
2422 PtrT, APInt::getZero(1), nullptr, ArraySizeModifier::Normal, 0);
2423 S.FFDiag(S.Current->getSource(OpPC),
2424 diag::note_constexpr_pointer_subtraction_zero_size)
2425 << ArrayTy;
2426
2427 return false;
2428 }
2429 }
2430
2431 int64_t A64 =
2432 LHS.isBlockPointer()
2433 ? (LHS.isElementPastEnd() ? LHS.getNumElems() : LHS.getIndex())
2435
2436 int64_t B64 =
2437 RHS.isBlockPointer()
2438 ? (RHS.isElementPastEnd() ? RHS.getNumElems() : RHS.getIndex())
2439 : RHS.getIntegerRepresentation();
2440
2441 int64_t R64 = A64 - B64;
2442 if (static_cast<int64_t>(T::from(R64)) != R64)
2443 return handleOverflow(S, OpPC, R64);
2444
2445 S.Stk.push<T>(T::from(R64));
2446 return true;
2447}
2448
2449//===----------------------------------------------------------------------===//
2450// Destroy
2451//===----------------------------------------------------------------------===//
2452
2453inline bool Destroy(InterpState &S, CodePtr OpPC, uint32_t I) {
2454 assert(S.Current->getFunction());
2455
2456 // FIXME: We iterate the scope once here and then again in the destroy() call
2457 // below.
2458 for (auto &Local : S.Current->getFunction()->getScope(I).locals_reverse()) {
2459 const Pointer &Ptr = S.Current->getLocalPointer(Local.Offset);
2460
2461 if (Ptr.getLifetime() == Lifetime::Ended) {
2462 // Try to use the declaration for better diagnostics
2463 if (const Decl *D = Ptr.getDeclDesc()->asDecl()) {
2464 auto *ND = cast<NamedDecl>(D);
2465 S.FFDiag(ND->getLocation(),
2466 diag::note_constexpr_destroy_out_of_lifetime)
2467 << ND->getNameAsString();
2468 } else {
2469 S.FFDiag(Ptr.getDeclDesc()->getLocation(),
2470 diag::note_constexpr_destroy_out_of_lifetime)
2472 }
2473 return false;
2474 }
2475 }
2476
2477 S.Current->destroy(I);
2478 return true;
2479}
2480
2481inline bool InitScope(InterpState &S, CodePtr OpPC, uint32_t I) {
2482 S.Current->initScope(I);
2483 return true;
2484}
2485
2486//===----------------------------------------------------------------------===//
2487// Cast, CastFP
2488//===----------------------------------------------------------------------===//
2489
2490template <PrimType TIn, PrimType TOut> bool Cast(InterpState &S, CodePtr OpPC) {
2491 using T = typename PrimConv<TIn>::T;
2492 using U = typename PrimConv<TOut>::T;
2493 S.Stk.push<U>(U::from(S.Stk.pop<T>()));
2494 return true;
2495}
2496
2497/// 1) Pops a Floating from the stack.
2498/// 2) Pushes a new floating on the stack that uses the given semantics.
2499inline bool CastFP(InterpState &S, CodePtr OpPC, const llvm::fltSemantics *Sem,
2500 llvm::RoundingMode RM) {
2501 Floating F = S.Stk.pop<Floating>();
2502 Floating Result = S.allocFloat(*Sem);
2503 F.toSemantics(Sem, RM, &Result);
2504 S.Stk.push<Floating>(Result);
2505 return true;
2506}
2507
2508inline bool CastFixedPoint(InterpState &S, CodePtr OpPC, uint32_t FPS) {
2509 FixedPointSemantics TargetSemantics =
2510 FixedPointSemantics::getFromOpaqueInt(FPS);
2511 const auto &Source = S.Stk.pop<FixedPoint>();
2512
2513 bool Overflow;
2514 FixedPoint Result = Source.toSemantics(TargetSemantics, &Overflow);
2515
2516 if (Overflow && !handleFixedPointOverflow(S, OpPC, Result))
2517 return false;
2518
2519 S.Stk.push<FixedPoint>(Result);
2520 return true;
2521}
2522
2523/// Like Cast(), but we cast to an arbitrary-bitwidth integral, so we need
2524/// to know what bitwidth the result should be.
2525template <PrimType Name, class T = typename PrimConv<Name>::T>
2526bool CastAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
2527 auto Result = S.allocAP<IntegralAP<false>>(BitWidth);
2528 // Copy data.
2529 {
2530 APInt Source = S.Stk.pop<T>().toAPSInt().extOrTrunc(BitWidth);
2531 Result.copy(Source);
2532 }
2533 S.Stk.push<IntegralAP<false>>(Result);
2534 return true;
2535}
2536
2537template <PrimType Name, class T = typename PrimConv<Name>::T>
2538bool CastAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
2539 auto Result = S.allocAP<IntegralAP<true>>(BitWidth);
2540 // Copy data.
2541 {
2542 APInt Source = S.Stk.pop<T>().toAPSInt().extOrTrunc(BitWidth);
2543 Result.copy(Source);
2544 }
2545 S.Stk.push<IntegralAP<true>>(Result);
2546 return true;
2547}
2548
2549template <PrimType Name, class T = typename PrimConv<Name>::T>
2551 const llvm::fltSemantics *Sem, uint32_t FPOI) {
2552 const T &From = S.Stk.pop<T>();
2553 APSInt FromAP = From.toAPSInt();
2554
2556 Floating Result = S.allocFloat(*Sem);
2557 auto Status =
2558 Floating::fromIntegral(FromAP, *Sem, getRoundingMode(FPO), &Result);
2559 S.Stk.push<Floating>(Result);
2560
2561 return CheckFloatResult(S, OpPC, Result, Status, FPO);
2562}
2563
2564template <PrimType Name, class T = typename PrimConv<Name>::T>
2565bool CastFloatingIntegral(InterpState &S, CodePtr OpPC, uint32_t FPOI) {
2566 const Floating &F = S.Stk.pop<Floating>();
2567
2568 if constexpr (std::is_same_v<T, Boolean>) {
2569 S.Stk.push<T>(T(F.isNonZero()));
2570 return true;
2571 } else {
2572 APSInt Result(std::max(8u, T::bitWidth()),
2573 /*IsUnsigned=*/!T::isSigned());
2574 auto Status = F.convertToInteger(Result);
2575
2576 // Float-to-Integral overflow check.
2577 if ((Status & APFloat::opStatus::opInvalidOp)) {
2578 const Expr *E = S.Current->getExpr(OpPC);
2579 QualType Type = E->getType();
2580
2581 S.CCEDiag(E, diag::note_constexpr_overflow) << F.getAPFloat() << Type;
2582 if (S.noteUndefinedBehavior()) {
2583 S.Stk.push<T>(T(Result));
2584 return true;
2585 }
2586 return false;
2587 }
2588
2590 S.Stk.push<T>(T(Result));
2591 return CheckFloatResult(S, OpPC, F, Status, FPO);
2592 }
2593}
2594
2595static inline bool CastFloatingIntegralAP(InterpState &S, CodePtr OpPC,
2596 uint32_t BitWidth, uint32_t FPOI) {
2597 const Floating &F = S.Stk.pop<Floating>();
2598
2599 APSInt Result(BitWidth, /*IsUnsigned=*/true);
2600 auto Status = F.convertToInteger(Result);
2601
2602 // Float-to-Integral overflow check.
2603 if ((Status & APFloat::opStatus::opInvalidOp) && F.isFinite())
2604 return handleOverflow(S, OpPC, F.getAPFloat());
2605
2607
2608 auto ResultAP = S.allocAP<IntegralAP<false>>(BitWidth);
2609 ResultAP.copy(Result);
2610
2611 S.Stk.push<IntegralAP<false>>(ResultAP);
2612
2613 return CheckFloatResult(S, OpPC, F, Status, FPO);
2614}
2615
2616static inline bool CastFloatingIntegralAPS(InterpState &S, CodePtr OpPC,
2617 uint32_t BitWidth, uint32_t FPOI) {
2618 const Floating &F = S.Stk.pop<Floating>();
2619
2620 APSInt Result(BitWidth, /*IsUnsigned=*/false);
2621 auto Status = F.convertToInteger(Result);
2622
2623 // Float-to-Integral overflow check.
2624 if ((Status & APFloat::opStatus::opInvalidOp) && F.isFinite())
2625 return handleOverflow(S, OpPC, F.getAPFloat());
2626
2628
2629 auto ResultAP = S.allocAP<IntegralAP<true>>(BitWidth);
2630 ResultAP.copy(Result);
2631
2632 S.Stk.push<IntegralAP<true>>(ResultAP);
2633
2634 return CheckFloatResult(S, OpPC, F, Status, FPO);
2635}
2636
2637bool CheckPointerToIntegralCast(InterpState &S, CodePtr OpPC,
2638 const Pointer &Ptr, unsigned BitWidth);
2639bool CastPointerIntegralAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth);
2640bool CastPointerIntegralAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth);
2641
2642template <PrimType Name, class T = typename PrimConv<Name>::T>
2644 const Pointer &Ptr = S.Stk.pop<Pointer>();
2645
2646 S.CCEDiag(S.Current->getSource(OpPC), diag::note_constexpr_invalid_cast)
2647 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
2648 << S.getLangOpts().CPlusPlus << S.Current->getRange(OpPC);
2649
2650 if (!CheckPointerToIntegralCast(S, OpPC, Ptr, T::bitWidth()))
2651 return Invalid(S, OpPC);
2652
2653 S.Stk.push<T>(T::from(Ptr.getIntegerRepresentation()));
2654 return true;
2655}
2656
2657template <PrimType Name, class T = typename PrimConv<Name>::T>
2658static inline bool CastIntegralFixedPoint(InterpState &S, CodePtr OpPC,
2659 uint32_t FPS) {
2660 const T &Int = S.Stk.pop<T>();
2661
2662 FixedPointSemantics Sem = FixedPointSemantics::getFromOpaqueInt(FPS);
2663
2664 bool Overflow;
2665 FixedPoint Result = FixedPoint::from(Int.toAPSInt(), Sem, &Overflow);
2666
2667 if (Overflow && !handleFixedPointOverflow(S, OpPC, Result))
2668 return false;
2669
2670 S.Stk.push<FixedPoint>(Result);
2671 return true;
2672}
2673
2674static inline bool CastFloatingFixedPoint(InterpState &S, CodePtr OpPC,
2675 uint32_t FPS) {
2676 const auto &Float = S.Stk.pop<Floating>();
2677
2678 FixedPointSemantics Sem = FixedPointSemantics::getFromOpaqueInt(FPS);
2679
2680 bool Overflow;
2681 FixedPoint Result = FixedPoint::from(Float.getAPFloat(), Sem, &Overflow);
2682
2683 if (Overflow && !handleFixedPointOverflow(S, OpPC, Result))
2684 return false;
2685
2686 S.Stk.push<FixedPoint>(Result);
2687 return true;
2688}
2689
2690static inline bool CastFixedPointFloating(InterpState &S, CodePtr OpPC,
2691 const llvm::fltSemantics *Sem) {
2692 const auto &Fixed = S.Stk.pop<FixedPoint>();
2693 Floating Result = S.allocFloat(*Sem);
2694 Result.copy(Fixed.toFloat(Sem));
2695 S.Stk.push<Floating>(Result);
2696 return true;
2697}
2698
2699template <PrimType Name, class T = typename PrimConv<Name>::T>
2700static inline bool CastFixedPointIntegral(InterpState &S, CodePtr OpPC) {
2701 const auto &Fixed = S.Stk.pop<FixedPoint>();
2702
2703 bool Overflow;
2704 APSInt Int = Fixed.toInt(T::bitWidth(), T::isSigned(), &Overflow);
2705
2706 if (Overflow && !handleOverflow(S, OpPC, Int))
2707 return false;
2708
2709 S.Stk.push<T>(Int);
2710 return true;
2711}
2712
2713static inline bool FnPtrCast(InterpState &S, CodePtr OpPC) {
2714 const SourceInfo &E = S.Current->getSource(OpPC);
2715 S.CCEDiag(E, diag::note_constexpr_invalid_cast)
2716 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
2717 << S.getLangOpts().CPlusPlus << S.Current->getRange(OpPC);
2718 return true;
2719}
2720
2721static inline bool PtrPtrCast(InterpState &S, CodePtr OpPC, bool SrcIsVoidPtr) {
2722 const auto &Ptr = S.Stk.peek<Pointer>();
2723
2724 if (SrcIsVoidPtr && S.getLangOpts().CPlusPlus) {
2725 bool HasValidResult = !Ptr.isZero();
2726
2727 if (HasValidResult) {
2728 if (S.getStdAllocatorCaller("allocate"))
2729 return true;
2730
2731 const auto &E = cast<CastExpr>(S.Current->getExpr(OpPC));
2732 if (S.getLangOpts().CPlusPlus26 &&
2733 S.getASTContext().hasSimilarType(Ptr.getType(),
2734 E->getType()->getPointeeType()))
2735 return true;
2736
2737 S.CCEDiag(E, diag::note_constexpr_invalid_void_star_cast)
2738 << E->getSubExpr()->getType() << S.getLangOpts().CPlusPlus26
2739 << Ptr.getType().getCanonicalType() << E->getType()->getPointeeType();
2740 } else if (!S.getLangOpts().CPlusPlus26) {
2741 const SourceInfo &E = S.Current->getSource(OpPC);
2742 S.CCEDiag(E, diag::note_constexpr_invalid_cast)
2743 << diag::ConstexprInvalidCastKind::CastFrom << "'void *'"
2744 << S.Current->getRange(OpPC);
2745 }
2746 } else {
2747 const SourceInfo &E = S.Current->getSource(OpPC);
2748 S.CCEDiag(E, diag::note_constexpr_invalid_cast)
2749 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
2750 << S.getLangOpts().CPlusPlus << S.Current->getRange(OpPC);
2751 }
2752
2753 return true;
2754}
2755
2756//===----------------------------------------------------------------------===//
2757// Zero, Nullptr
2758//===----------------------------------------------------------------------===//
2759
2760template <PrimType Name, class T = typename PrimConv<Name>::T>
2761bool Zero(InterpState &S, CodePtr OpPC) {
2762 S.Stk.push<T>(T::zero());
2763 return true;
2764}
2765
2766static inline bool ZeroIntAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
2767 auto Result = S.allocAP<IntegralAP<false>>(BitWidth);
2768 if (!Result.singleWord())
2769 std::memset(Result.Memory, 0, Result.numWords() * sizeof(uint64_t));
2770 S.Stk.push<IntegralAP<false>>(Result);
2771 return true;
2772}
2773
2774static inline bool ZeroIntAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
2775 auto Result = S.allocAP<IntegralAP<true>>(BitWidth);
2776 if (!Result.singleWord())
2777 std::memset(Result.Memory, 0, Result.numWords() * sizeof(uint64_t));
2778 S.Stk.push<IntegralAP<true>>(Result);
2779 return true;
2780}
2781
2782template <PrimType Name, class T = typename PrimConv<Name>::T>
2783inline bool Null(InterpState &S, CodePtr OpPC, uint64_t Value,
2784 const Descriptor *Desc) {
2785 // FIXME(perf): This is a somewhat often-used function and the value of a
2786 // null pointer is almost always 0.
2787 S.Stk.push<T>(Value, Desc);
2788 return true;
2789}
2790
2791template <PrimType Name, class T = typename PrimConv<Name>::T>
2792inline bool IsNonNull(InterpState &S, CodePtr OpPC) {
2793 const auto &P = S.Stk.pop<T>();
2794 if (P.isWeak())
2795 return false;
2796 S.Stk.push<Boolean>(Boolean::from(!P.isZero()));
2797 return true;
2798}
2799
2800//===----------------------------------------------------------------------===//
2801// This, ImplicitThis
2802//===----------------------------------------------------------------------===//
2803
2804inline bool This(InterpState &S, CodePtr OpPC) {
2805 // Cannot read 'this' in this mode.
2806 if (S.checkingPotentialConstantExpression()) {
2807 return false;
2808 }
2809
2810 const Pointer &This = S.Current->getThis();
2811 if (!CheckThis(S, OpPC, This))
2812 return false;
2813
2814 // Ensure the This pointer has been cast to the correct base.
2815 if (!This.isDummy()) {
2816 assert(isa<CXXMethodDecl>(S.Current->getFunction()->getDecl()));
2817 if (!This.isTypeidPointer()) {
2818 [[maybe_unused]] const Record *R = This.getRecord();
2819 if (!R)
2820 R = This.narrow().getRecord();
2821 assert(R);
2822 assert(R->getDecl() ==
2823 cast<CXXMethodDecl>(S.Current->getFunction()->getDecl())
2824 ->getParent());
2825 }
2826 }
2827
2828 S.Stk.push<Pointer>(This);
2829 return true;
2830}
2831
2832inline bool RVOPtr(InterpState &S, CodePtr OpPC) {
2833 assert(S.Current->getFunction()->hasRVO());
2834 if (S.checkingPotentialConstantExpression())
2835 return false;
2836 S.Stk.push<Pointer>(S.Current->getRVOPtr());
2837 return true;
2838}
2839
2840//===----------------------------------------------------------------------===//
2841// Shr, Shl
2842//===----------------------------------------------------------------------===//
2843
2844template <class LT, class RT, ShiftDir Dir>
2845inline bool DoShift(InterpState &S, CodePtr OpPC, LT &LHS, RT &RHS,
2846 LT *Result) {
2847 static_assert(!needsAlloc<LT>());
2848 const unsigned Bits = LHS.bitWidth();
2849
2850 // OpenCL 6.3j: shift values are effectively % word size of LHS.
2851 if (S.getLangOpts().OpenCL)
2852 RT::bitAnd(RHS, RT::from(LHS.bitWidth() - 1, RHS.bitWidth()),
2853 RHS.bitWidth(), &RHS);
2854
2855 if (RHS.isNegative()) {
2856 // During constant-folding, a negative shift is an opposite shift. Such a
2857 // shift is not a constant expression.
2858 const SourceInfo &Loc = S.Current->getSource(OpPC);
2859 S.CCEDiag(Loc, diag::note_constexpr_negative_shift) << RHS.toAPSInt();
2860 if (!S.noteUndefinedBehavior())
2861 return false;
2862 RHS = -RHS;
2863 return DoShift<LT, RT,
2865 S, OpPC, LHS, RHS, Result);
2866 }
2867
2868 if (!CheckShift<Dir>(S, OpPC, LHS, RHS, Bits))
2869 return false;
2870
2871 // Limit the shift amount to Bits - 1. If this happened,
2872 // it has already been diagnosed by CheckShift() above,
2873 // but we still need to handle it.
2874 // Note that we have to be extra careful here since we're doing the shift in
2875 // any case, but we need to adjust the shift amount or the way we do the shift
2876 // for the potential error cases.
2877 typename LT::AsUnsigned R;
2878 unsigned MaxShiftAmount = LHS.bitWidth() - 1;
2879 if constexpr (Dir == ShiftDir::Left) {
2880 if (Compare(RHS, RT::from(MaxShiftAmount, RHS.bitWidth())) ==
2882 if (LHS.isNegative())
2883 R = LT::AsUnsigned::zero(LHS.bitWidth());
2884 else {
2885 RHS = RT::from(LHS.countLeadingZeros(), RHS.bitWidth());
2886 LT::AsUnsigned::shiftLeft(LT::AsUnsigned::from(LHS),
2887 LT::AsUnsigned::from(RHS, Bits), Bits, &R);
2888 }
2889 } else if (LHS.isNegative()) {
2890 if (LHS.isMin()) {
2891 R = LT::AsUnsigned::zero(LHS.bitWidth());
2892 } else {
2893 // If the LHS is negative, perform the cast and invert the result.
2894 typename LT::AsUnsigned LHSU = LT::AsUnsigned::from(-LHS);
2895 LT::AsUnsigned::shiftLeft(LHSU, LT::AsUnsigned::from(RHS, Bits), Bits,
2896 &R);
2897 R = -R;
2898 }
2899 } else {
2900 // The good case, a simple left shift.
2901 LT::AsUnsigned::shiftLeft(LT::AsUnsigned::from(LHS),
2902 LT::AsUnsigned::from(RHS, Bits), Bits, &R);
2903 }
2904 S.Stk.push<LT>(LT::from(R));
2905 return true;
2906 }
2907
2908 // Right shift.
2909 if (Compare(RHS, RT::from(MaxShiftAmount, RHS.bitWidth())) ==
2911 R = LT::AsUnsigned::from(-1);
2912 } else {
2913 // Do the shift on potentially signed LT, then convert to unsigned type.
2914 LT A;
2915 LT::shiftRight(LHS, LT::from(RHS, Bits), Bits, &A);
2916 R = LT::AsUnsigned::from(A);
2917 }
2918
2919 S.Stk.push<LT>(LT::from(R));
2920 return true;
2921}
2922
2923/// A version of DoShift that works on IntegralAP.
2924template <class LT, class RT, ShiftDir Dir>
2925inline bool DoShiftAP(InterpState &S, CodePtr OpPC, const APSInt &LHS,
2926 APSInt RHS, LT *Result) {
2927 const unsigned Bits = LHS.getBitWidth();
2928
2929 // OpenCL 6.3j: shift values are effectively % word size of LHS.
2930 if (S.getLangOpts().OpenCL)
2931 RHS &=
2932 APSInt(llvm::APInt(RHS.getBitWidth(), static_cast<uint64_t>(Bits - 1)),
2933 RHS.isUnsigned());
2934
2935 if (RHS.isNegative()) {
2936 // During constant-folding, a negative shift is an opposite shift. Such a
2937 // shift is not a constant expression.
2938 const SourceInfo &Loc = S.Current->getSource(OpPC);
2939 S.CCEDiag(Loc, diag::note_constexpr_negative_shift) << RHS; //.toAPSInt();
2940 if (!S.noteUndefinedBehavior())
2941 return false;
2942 return DoShiftAP<LT, RT,
2944 S, OpPC, LHS, -RHS, Result);
2945 }
2946
2947 if (!CheckShift<Dir>(S, OpPC, static_cast<LT>(LHS), static_cast<RT>(RHS),
2948 Bits))
2949 return false;
2950
2951 unsigned SA = (unsigned)RHS.getLimitedValue(Bits - 1);
2952 if constexpr (Dir == ShiftDir::Left) {
2953 if constexpr (needsAlloc<LT>())
2954 Result->copy(LHS << SA);
2955 else
2956 *Result = LT(LHS << SA);
2957 } else {
2958 if constexpr (needsAlloc<LT>())
2959 Result->copy(LHS >> SA);
2960 else
2961 *Result = LT(LHS >> SA);
2962 }
2963
2964 S.Stk.push<LT>(*Result);
2965 return true;
2966}
2967
2968template <PrimType NameL, PrimType NameR>
2969inline bool Shr(InterpState &S, CodePtr OpPC) {
2970 using LT = typename PrimConv<NameL>::T;
2971 using RT = typename PrimConv<NameR>::T;
2972 auto RHS = S.Stk.pop<RT>();
2973 auto LHS = S.Stk.pop<LT>();
2974
2975 if constexpr (needsAlloc<LT>() || needsAlloc<RT>()) {
2976 LT Result;
2977 if constexpr (needsAlloc<LT>())
2978 Result = S.allocAP<LT>(LHS.bitWidth());
2979 return DoShiftAP<LT, RT, ShiftDir::Right>(S, OpPC, LHS.toAPSInt(),
2980 RHS.toAPSInt(), &Result);
2981 } else {
2982 LT Result;
2983 return DoShift<LT, RT, ShiftDir::Right>(S, OpPC, LHS, RHS, &Result);
2984 }
2985}
2986
2987template <PrimType NameL, PrimType NameR>
2988inline bool Shl(InterpState &S, CodePtr OpPC) {
2989 using LT = typename PrimConv<NameL>::T;
2990 using RT = typename PrimConv<NameR>::T;
2991 auto RHS = S.Stk.pop<RT>();
2992 auto LHS = S.Stk.pop<LT>();
2993
2994 if constexpr (needsAlloc<LT>() || needsAlloc<RT>()) {
2995 LT Result;
2996 if constexpr (needsAlloc<LT>())
2997 Result = S.allocAP<LT>(LHS.bitWidth());
2998 return DoShiftAP<LT, RT, ShiftDir::Left>(S, OpPC, LHS.toAPSInt(),
2999 RHS.toAPSInt(), &Result);
3000 } else {
3001 LT Result;
3002 return DoShift<LT, RT, ShiftDir::Left>(S, OpPC, LHS, RHS, &Result);
3003 }
3004}
3005
3006static inline bool ShiftFixedPoint(InterpState &S, CodePtr OpPC, bool Left) {
3007 const auto &RHS = S.Stk.pop<FixedPoint>();
3008 const auto &LHS = S.Stk.pop<FixedPoint>();
3009 llvm::FixedPointSemantics LHSSema = LHS.getSemantics();
3010
3011 unsigned ShiftBitWidth =
3012 LHSSema.getWidth() - (unsigned)LHSSema.hasUnsignedPadding() - 1;
3013
3014 // Embedded-C 4.1.6.2.2:
3015 // The right operand must be nonnegative and less than the total number
3016 // of (nonpadding) bits of the fixed-point operand ...
3017 if (RHS.isNegative()) {
3018 S.CCEDiag(S.Current->getLocation(OpPC), diag::note_constexpr_negative_shift)
3019 << RHS.toAPSInt();
3020 } else if (static_cast<unsigned>(RHS.toAPSInt().getLimitedValue(
3021 ShiftBitWidth)) != RHS.toAPSInt()) {
3022 const Expr *E = S.Current->getExpr(OpPC);
3023 S.CCEDiag(E, diag::note_constexpr_large_shift)
3024 << RHS.toAPSInt() << E->getType() << ShiftBitWidth;
3025 }
3026
3028 if (Left) {
3029 if (FixedPoint::shiftLeft(LHS, RHS, ShiftBitWidth, &Result) &&
3031 return false;
3032 } else {
3033 if (FixedPoint::shiftRight(LHS, RHS, ShiftBitWidth, &Result) &&
3035 return false;
3036 }
3037
3038 S.Stk.push<FixedPoint>(Result);
3039 return true;
3040}
3041
3042//===----------------------------------------------------------------------===//
3043// NoRet
3044//===----------------------------------------------------------------------===//
3045
3046inline bool NoRet(InterpState &S, CodePtr OpPC) {
3047 SourceLocation EndLoc = S.Current->getCallee()->getEndLoc();
3048 S.FFDiag(EndLoc, diag::note_constexpr_no_return);
3049 return false;
3050}
3051
3052//===----------------------------------------------------------------------===//
3053// NarrowPtr, ExpandPtr
3054//===----------------------------------------------------------------------===//
3055
3056inline bool NarrowPtr(InterpState &S, CodePtr OpPC) {
3057 const Pointer &Ptr = S.Stk.pop<Pointer>();
3058 S.Stk.push<Pointer>(Ptr.narrow());
3059 return true;
3060}
3061
3062inline bool ExpandPtr(InterpState &S, CodePtr OpPC) {
3063 const Pointer &Ptr = S.Stk.pop<Pointer>();
3064 if (Ptr.isBlockPointer())
3065 S.Stk.push<Pointer>(Ptr.expand());
3066 else
3067 S.Stk.push<Pointer>(Ptr);
3068 return true;
3069}
3070
3071// 1) Pops an integral value from the stack
3072// 2) Peeks a pointer
3073// 3) Pushes a new pointer that's a narrowed array
3074// element of the peeked pointer with the value
3075// from 1) added as offset.
3076//
3077// This leaves the original pointer on the stack and pushes a new one
3078// with the offset applied and narrowed.
3079template <PrimType Name, class T = typename PrimConv<Name>::T>
3080inline bool ArrayElemPtr(InterpState &S, CodePtr OpPC) {
3081 const T &Offset = S.Stk.pop<T>();
3082 const Pointer &Ptr = S.Stk.peek<Pointer>();
3083
3084 if (!Ptr.isZero() && !Offset.isZero()) {
3085 if (!CheckArray(S, OpPC, Ptr))
3086 return false;
3087 }
3088
3089 if (Offset.isZero()) {
3090 if (Ptr.getFieldDesc()->isArray() && Ptr.getIndex() == 0) {
3091 S.Stk.push<Pointer>(Ptr.atIndex(0).narrow());
3092 return true;
3093 }
3094 S.Stk.push<Pointer>(Ptr);
3095 return true;
3096 }
3097
3098 assert(!Offset.isZero());
3099
3100 if (std::optional<Pointer> Result =
3101 OffsetHelper<T, ArithOp::Add>(S, OpPC, Offset, Ptr)) {
3102 S.Stk.push<Pointer>(Result->narrow());
3103 return true;
3104 }
3105
3106 return false;
3107}
3108
3109template <PrimType Name, class T = typename PrimConv<Name>::T>
3110inline bool ArrayElemPtrPop(InterpState &S, CodePtr OpPC) {
3111 const T &Offset = S.Stk.pop<T>();
3112 const Pointer &Ptr = S.Stk.pop<Pointer>();
3113
3114 if (!Ptr.isZero() && !Offset.isZero()) {
3115 if (!CheckArray(S, OpPC, Ptr))
3116 return false;
3117 }
3118
3119 if (Offset.isZero()) {
3120 if (Ptr.getFieldDesc()->isArray() && Ptr.getIndex() == 0) {
3121 S.Stk.push<Pointer>(Ptr.atIndex(0).narrow());
3122 return true;
3123 }
3124 S.Stk.push<Pointer>(Ptr);
3125 return true;
3126 }
3127
3128 assert(!Offset.isZero());
3129
3130 if (std::optional<Pointer> Result =
3131 OffsetHelper<T, ArithOp::Add>(S, OpPC, Offset, Ptr)) {
3132 S.Stk.push<Pointer>(Result->narrow());
3133 return true;
3134 }
3135 return false;
3136}
3137
3138template <PrimType Name, class T = typename PrimConv<Name>::T>
3139inline bool ArrayElem(InterpState &S, CodePtr OpPC, uint32_t Index) {
3140 const Pointer &Ptr = S.Stk.peek<Pointer>();
3141
3142 if (!CheckLoad(S, OpPC, Ptr))
3143 return false;
3144
3145 assert(Ptr.atIndex(Index).getFieldDesc()->getPrimType() == Name);
3146 S.Stk.push<T>(Ptr.elem<T>(Index));
3147 return true;
3148}
3149
3150template <PrimType Name, class T = typename PrimConv<Name>::T>
3151inline bool ArrayElemPop(InterpState &S, CodePtr OpPC, uint32_t Index) {
3152 const Pointer &Ptr = S.Stk.pop<Pointer>();
3153
3154 if (!CheckLoad(S, OpPC, Ptr))
3155 return false;
3156
3157 assert(Ptr.atIndex(Index).getFieldDesc()->getPrimType() == Name);
3158 S.Stk.push<T>(Ptr.elem<T>(Index));
3159 return true;
3160}
3161
3162template <PrimType Name, class T = typename PrimConv<Name>::T>
3163inline bool CopyArray(InterpState &S, CodePtr OpPC, uint32_t SrcIndex,
3164 uint32_t DestIndex, uint32_t Size) {
3165 const auto &SrcPtr = S.Stk.pop<Pointer>();
3166 const auto &DestPtr = S.Stk.peek<Pointer>();
3167
3168 for (uint32_t I = 0; I != Size; ++I) {
3169 const Pointer &SP = SrcPtr.atIndex(SrcIndex + I);
3170
3171 if (!CheckLoad(S, OpPC, SP))
3172 return false;
3173
3174 const Pointer &DP = DestPtr.atIndex(DestIndex + I);
3175 DP.deref<T>() = SP.deref<T>();
3176 DP.initialize();
3177 }
3178 return true;
3179}
3180
3181/// Just takes a pointer and checks if it's an incomplete
3182/// array type.
3183inline bool ArrayDecay(InterpState &S, CodePtr OpPC) {
3184 const Pointer &Ptr = S.Stk.pop<Pointer>();
3185
3186 if (Ptr.isZero()) {
3187 S.Stk.push<Pointer>(Ptr);
3188 return true;
3189 }
3190
3191 if (!Ptr.isZeroSizeArray()) {
3192 if (!CheckRange(S, OpPC, Ptr, CSK_ArrayToPointer))
3193 return false;
3194 }
3195
3196 if (Ptr.isRoot() || !Ptr.isUnknownSizeArray()) {
3197 S.Stk.push<Pointer>(Ptr.atIndex(0));
3198 return true;
3199 }
3200
3201 const SourceInfo &E = S.Current->getSource(OpPC);
3202 S.FFDiag(E, diag::note_constexpr_unsupported_unsized_array);
3203
3204 return false;
3205}
3206
3207inline bool GetFnPtr(InterpState &S, CodePtr OpPC, const Function *Func) {
3208 assert(Func);
3209 S.Stk.push<Pointer>(Func);
3210 return true;
3211}
3212
3213template <PrimType Name, class T = typename PrimConv<Name>::T>
3214inline bool GetIntPtr(InterpState &S, CodePtr OpPC, const Descriptor *Desc) {
3215 const T &IntVal = S.Stk.pop<T>();
3216
3217 S.CCEDiag(S.Current->getSource(OpPC), diag::note_constexpr_invalid_cast)
3218 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
3219 << S.getLangOpts().CPlusPlus;
3220
3221 S.Stk.push<Pointer>(static_cast<uint64_t>(IntVal), Desc);
3222 return true;
3223}
3224
3225inline bool GetMemberPtr(InterpState &S, CodePtr OpPC, const ValueDecl *D) {
3226 S.Stk.push<MemberPointer>(D);
3227 return true;
3228}
3229
3230inline bool GetMemberPtrBase(InterpState &S, CodePtr OpPC) {
3231 const auto &MP = S.Stk.pop<MemberPointer>();
3232
3233 if (!MP.isBaseCastPossible())
3234 return false;
3235
3236 S.Stk.push<Pointer>(MP.getBase());
3237 return true;
3238}
3239
3240inline bool GetMemberPtrDecl(InterpState &S, CodePtr OpPC) {
3241 const auto &MP = S.Stk.pop<MemberPointer>();
3242
3243 const auto *FD = cast<FunctionDecl>(MP.getDecl());
3244 const auto *Func = S.getContext().getOrCreateFunction(FD);
3245
3246 S.Stk.push<Pointer>(Func);
3247 return true;
3248}
3249
3250/// Just emit a diagnostic. The expression that caused emission of this
3251/// op is not valid in a constant context.
3252inline bool Invalid(InterpState &S, CodePtr OpPC) {
3253 const SourceLocation &Loc = S.Current->getLocation(OpPC);
3254 S.FFDiag(Loc, diag::note_invalid_subexpr_in_const_expr)
3255 << S.Current->getRange(OpPC);
3256 return false;
3257}
3258
3259inline bool Unsupported(InterpState &S, CodePtr OpPC) {
3260 const SourceLocation &Loc = S.Current->getLocation(OpPC);
3261 S.FFDiag(Loc, diag::note_constexpr_stmt_expr_unsupported)
3262 << S.Current->getRange(OpPC);
3263 return false;
3264}
3265
3266inline bool StartSpeculation(InterpState &S, CodePtr OpPC) {
3267 ++S.SpeculationDepth;
3268 if (S.SpeculationDepth != 1)
3269 return true;
3270
3271 assert(S.PrevDiags == nullptr);
3272 S.PrevDiags = S.getEvalStatus().Diag;
3273 S.getEvalStatus().Diag = nullptr;
3274 return true;
3275}
3276inline bool EndSpeculation(InterpState &S, CodePtr OpPC) {
3277 assert(S.SpeculationDepth != 0);
3278 --S.SpeculationDepth;
3279 if (S.SpeculationDepth == 0) {
3280 S.getEvalStatus().Diag = S.PrevDiags;
3281 S.PrevDiags = nullptr;
3282 }
3283 return true;
3284}
3285
3286inline bool PushCC(InterpState &S, CodePtr OpPC, bool Value) {
3287 S.ConstantContextOverride = Value;
3288 return true;
3289}
3290inline bool PopCC(InterpState &S, CodePtr OpPC) {
3291 S.ConstantContextOverride = std::nullopt;
3292 return true;
3293}
3294
3295/// Do nothing and just abort execution.
3296inline bool Error(InterpState &S, CodePtr OpPC) { return false; }
3297
3298inline bool SideEffect(InterpState &S, CodePtr OpPC) {
3299 return S.noteSideEffect();
3300}
3301
3302/// Same here, but only for casts.
3303inline bool InvalidCast(InterpState &S, CodePtr OpPC, CastKind Kind,
3304 bool Fatal) {
3305 const SourceLocation &Loc = S.Current->getLocation(OpPC);
3306
3307 if (Kind == CastKind::Reinterpret) {
3308 S.CCEDiag(Loc, diag::note_constexpr_invalid_cast)
3309 << static_cast<unsigned>(Kind) << S.Current->getRange(OpPC);
3310 return !Fatal;
3311 }
3312 if (Kind == CastKind::Volatile) {
3313 if (!S.checkingPotentialConstantExpression()) {
3314 const auto *E = cast<CastExpr>(S.Current->getExpr(OpPC));
3315 if (S.getLangOpts().CPlusPlus)
3316 S.FFDiag(E, diag::note_constexpr_access_volatile_type)
3317 << AK_Read << E->getSubExpr()->getType();
3318 else
3319 S.FFDiag(E);
3320 }
3321
3322 return false;
3323 }
3324 if (Kind == CastKind::Dynamic) {
3325 assert(!S.getLangOpts().CPlusPlus20);
3326 S.CCEDiag(S.Current->getSource(OpPC), diag::note_constexpr_invalid_cast)
3327 << diag::ConstexprInvalidCastKind::Dynamic;
3328 return true;
3329 }
3330
3331 return false;
3332}
3333
3334inline bool InvalidDeclRef(InterpState &S, CodePtr OpPC, const DeclRefExpr *DR,
3335 bool InitializerFailed) {
3336 assert(DR);
3337
3338 if (InitializerFailed) {
3339 const SourceInfo &Loc = S.Current->getSource(OpPC);
3340 const auto *VD = cast<VarDecl>(DR->getDecl());
3341 S.FFDiag(Loc, diag::note_constexpr_var_init_non_constant, 1) << VD;
3342 S.Note(VD->getLocation(), diag::note_declared_at);
3343 return false;
3344 }
3345
3346 return CheckDeclRef(S, OpPC, DR);
3347}
3348
3350 if (S.inConstantContext()) {
3351 const SourceRange &ArgRange = S.Current->getRange(OpPC);
3352 const Expr *E = S.Current->getExpr(OpPC);
3353 S.CCEDiag(E, diag::note_constexpr_non_const_vectorelements) << ArgRange;
3354 }
3355 return false;
3356}
3357
3358inline bool CheckPseudoDtor(InterpState &S, CodePtr OpPC) {
3359 if (!S.getLangOpts().CPlusPlus20)
3360 S.CCEDiag(S.Current->getSource(OpPC),
3361 diag::note_constexpr_pseudo_destructor);
3362 return true;
3363}
3364
3365inline bool Assume(InterpState &S, CodePtr OpPC) {
3366 const auto Val = S.Stk.pop<Boolean>();
3367
3368 if (Val)
3369 return true;
3370
3371 // Else, diagnose.
3372 const SourceLocation &Loc = S.Current->getLocation(OpPC);
3373 S.CCEDiag(Loc, diag::note_constexpr_assumption_failed);
3374 return false;
3375}
3376
3377template <PrimType Name, class T = typename PrimConv<Name>::T>
3378inline bool OffsetOf(InterpState &S, CodePtr OpPC, const OffsetOfExpr *E) {
3379 llvm::SmallVector<int64_t> ArrayIndices;
3380 for (size_t I = 0; I != E->getNumExpressions(); ++I)
3381 ArrayIndices.emplace_back(S.Stk.pop<int64_t>());
3382
3383 int64_t Result;
3384 if (!InterpretOffsetOf(S, OpPC, E, ArrayIndices, Result))
3385 return false;
3386
3387 S.Stk.push<T>(T::from(Result));
3388
3389 return true;
3390}
3391
3392template <PrimType Name, class T = typename PrimConv<Name>::T>
3393inline bool CheckNonNullArg(InterpState &S, CodePtr OpPC) {
3394 const T &Arg = S.Stk.peek<T>();
3395 if (!Arg.isZero())
3396 return true;
3397
3398 const SourceLocation &Loc = S.Current->getLocation(OpPC);
3399 S.CCEDiag(Loc, diag::note_non_null_attribute_failed);
3400
3401 return false;
3402}
3403
3404void diagnoseEnumValue(InterpState &S, CodePtr OpPC, const EnumDecl *ED,
3405 const APSInt &Value);
3406
3407template <PrimType Name, class T = typename PrimConv<Name>::T>
3408inline bool CheckEnumValue(InterpState &S, CodePtr OpPC, const EnumDecl *ED) {
3409 assert(ED);
3410 assert(!ED->isFixed());
3411
3412 if (S.inConstantContext()) {
3413 const APSInt Val = S.Stk.peek<T>().toAPSInt();
3414 diagnoseEnumValue(S, OpPC, ED, Val);
3415 }
3416 return true;
3417}
3418
3419/// OldPtr -> Integer -> NewPtr.
3420template <PrimType TIn, PrimType TOut>
3421inline bool DecayPtr(InterpState &S, CodePtr OpPC) {
3422 static_assert(isPtrType(TIn) && isPtrType(TOut));
3423 using FromT = typename PrimConv<TIn>::T;
3424 using ToT = typename PrimConv<TOut>::T;
3425
3426 const FromT &OldPtr = S.Stk.pop<FromT>();
3427
3428 if constexpr (std::is_same_v<FromT, FunctionPointer> &&
3429 std::is_same_v<ToT, Pointer>) {
3430 S.Stk.push<Pointer>(OldPtr.getFunction(), OldPtr.getOffset());
3431 return true;
3432 } else if constexpr (std::is_same_v<FromT, Pointer> &&
3433 std::is_same_v<ToT, FunctionPointer>) {
3434 if (OldPtr.isFunctionPointer()) {
3435 S.Stk.push<FunctionPointer>(OldPtr.asFunctionPointer().getFunction(),
3436 OldPtr.getByteOffset());
3437 return true;
3438 }
3439 }
3440
3441 S.Stk.push<ToT>(ToT(OldPtr.getIntegerRepresentation(), nullptr));
3442 return true;
3443}
3444
3445inline bool CheckDecl(InterpState &S, CodePtr OpPC, const VarDecl *VD) {
3446 // An expression E is a core constant expression unless the evaluation of E
3447 // would evaluate one of the following: [C++23] - a control flow that passes
3448 // through a declaration of a variable with static or thread storage duration
3449 // unless that variable is usable in constant expressions.
3450 assert(VD->isLocalVarDecl() &&
3451 VD->isStaticLocal()); // Checked before emitting this.
3452
3453 if (VD == S.EvaluatingDecl)
3454 return true;
3455
3457 S.CCEDiag(VD->getLocation(), diag::note_constexpr_static_local)
3458 << (VD->getTSCSpec() == TSCS_unspecified ? 0 : 1) << VD;
3459 return false;
3460 }
3461 return true;
3462}
3463
3464inline bool Alloc(InterpState &S, CodePtr OpPC, const Descriptor *Desc) {
3465 assert(Desc);
3466
3467 if (!CheckDynamicMemoryAllocation(S, OpPC))
3468 return false;
3469
3470 DynamicAllocator &Allocator = S.getAllocator();
3471 Block *B = Allocator.allocate(Desc, S.Ctx.getEvalID(),
3473 assert(B);
3474 S.Stk.push<Pointer>(B);
3475 return true;
3476}
3477
3478template <PrimType Name, class SizeT = typename PrimConv<Name>::T>
3479inline bool AllocN(InterpState &S, CodePtr OpPC, PrimType T, const Expr *Source,
3480 bool IsNoThrow) {
3481 if (!CheckDynamicMemoryAllocation(S, OpPC))
3482 return false;
3483
3484 SizeT NumElements = S.Stk.pop<SizeT>();
3485 if (!CheckArraySize(S, OpPC, &NumElements, primSize(T), IsNoThrow)) {
3486 if (!IsNoThrow)
3487 return false;
3488
3489 // If this failed and is nothrow, just return a null ptr.
3490 S.Stk.push<Pointer>(0, nullptr);
3491 return true;
3492 }
3493 if (NumElements.isNegative()) {
3494 if (!IsNoThrow) {
3495 S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_new_negative)
3496 << NumElements.toDiagnosticString(S.getASTContext());
3497 return false;
3498 }
3499 S.Stk.push<Pointer>(0, nullptr);
3500 return true;
3501 }
3502
3503 if (!CheckArraySize(S, OpPC, static_cast<uint64_t>(NumElements)))
3504 return false;
3505
3506 DynamicAllocator &Allocator = S.getAllocator();
3507 Block *B =
3508 Allocator.allocate(Source, T, static_cast<size_t>(NumElements),
3509 S.Ctx.getEvalID(), DynamicAllocator::Form::Array);
3510 assert(B);
3511 if (NumElements.isZero())
3512 S.Stk.push<Pointer>(B);
3513 else
3514 S.Stk.push<Pointer>(Pointer(B).atIndex(0));
3515 return true;
3516}
3517
3518template <PrimType Name, class SizeT = typename PrimConv<Name>::T>
3519inline bool AllocCN(InterpState &S, CodePtr OpPC, const Descriptor *ElementDesc,
3520 bool IsNoThrow) {
3521 if (!CheckDynamicMemoryAllocation(S, OpPC))
3522 return false;
3523
3524 SizeT NumElements = S.Stk.pop<SizeT>();
3525 if (!CheckArraySize(S, OpPC, &NumElements, ElementDesc->getSize(),
3526 IsNoThrow)) {
3527 if (!IsNoThrow)
3528 return false;
3529
3530 // If this failed and is nothrow, just return a null ptr.
3531 S.Stk.push<Pointer>(0, ElementDesc);
3532 return true;
3533 }
3534 assert(NumElements.isPositive());
3535
3536 if (!CheckArraySize(S, OpPC, static_cast<uint64_t>(NumElements)))
3537 return false;
3538
3539 DynamicAllocator &Allocator = S.getAllocator();
3540 Block *B =
3541 Allocator.allocate(ElementDesc, static_cast<size_t>(NumElements),
3542 S.Ctx.getEvalID(), DynamicAllocator::Form::Array);
3543 assert(B);
3544 if (NumElements.isZero())
3545 S.Stk.push<Pointer>(B);
3546 else
3547 S.Stk.push<Pointer>(Pointer(B).atIndex(0));
3548
3549 return true;
3550}
3551
3552bool Free(InterpState &S, CodePtr OpPC, bool DeleteIsArrayForm,
3553 bool IsGlobalDelete);
3554
3555static inline bool IsConstantContext(InterpState &S, CodePtr OpPC) {
3556 S.Stk.push<Boolean>(Boolean::from(S.inConstantContext()));
3557 return true;
3558}
3559
3560static inline bool CheckAllocations(InterpState &S, CodePtr OpPC) {
3561 return S.maybeDiagnoseDanglingAllocations();
3562}
3563
3564/// Check if the initializer and storage types of a placement-new expression
3565/// match.
3566bool CheckNewTypeMismatch(InterpState &S, CodePtr OpPC, const Expr *E,
3567 std::optional<uint64_t> ArraySize = std::nullopt);
3568
3569template <PrimType Name, class T = typename PrimConv<Name>::T>
3571 const auto &Size = S.Stk.pop<T>();
3572 return CheckNewTypeMismatch(S, OpPC, E, static_cast<uint64_t>(Size));
3573}
3574bool InvalidNewDeleteExpr(InterpState &S, CodePtr OpPC, const Expr *E);
3575
3576template <PrimType Name, class T = typename PrimConv<Name>::T>
3577inline bool BitCastPrim(InterpState &S, CodePtr OpPC, bool TargetIsUCharOrByte,
3578 uint32_t ResultBitWidth,
3579 const llvm::fltSemantics *Sem) {
3580 const Pointer &FromPtr = S.Stk.pop<Pointer>();
3581
3582 if (!CheckLoad(S, OpPC, FromPtr))
3583 return false;
3584
3585 if constexpr (std::is_same_v<T, Pointer>) {
3586 // The only pointer type we can validly bitcast to is nullptr_t.
3587 S.Stk.push<Pointer>();
3588 return true;
3589 } else {
3590
3591 size_t BuffSize = ResultBitWidth / 8;
3592 llvm::SmallVector<std::byte> Buff(BuffSize);
3593 bool HasIndeterminateBits = false;
3594
3595 Bits FullBitWidth(ResultBitWidth);
3596 Bits BitWidth = FullBitWidth;
3597
3598 if constexpr (std::is_same_v<T, Floating>) {
3599 assert(Sem);
3600 BitWidth = Bits(llvm::APFloatBase::getSizeInBits(*Sem));
3601 }
3602
3603 if (!DoBitCast(S, OpPC, FromPtr, Buff.data(), BitWidth, FullBitWidth,
3604 HasIndeterminateBits))
3605 return false;
3606
3607 if (!CheckBitCast(S, OpPC, HasIndeterminateBits, TargetIsUCharOrByte))
3608 return false;
3609
3610 if constexpr (std::is_same_v<T, Floating>) {
3611 assert(Sem);
3612 Floating Result = S.allocFloat(*Sem);
3613 Floating::bitcastFromMemory(Buff.data(), *Sem, &Result);
3614 S.Stk.push<Floating>(Result);
3615 } else if constexpr (needsAlloc<T>()) {
3616 T Result = S.allocAP<T>(ResultBitWidth);
3617 T::bitcastFromMemory(Buff.data(), ResultBitWidth, &Result);
3618 S.Stk.push<T>(Result);
3619 } else if constexpr (std::is_same_v<T, Boolean>) {
3620 // Only allow to cast single-byte integers to bool if they are either 0
3621 // or 1.
3622 assert(FullBitWidth.getQuantity() == 8);
3623 auto Val = static_cast<unsigned int>(Buff[0]);
3624 if (Val > 1) {
3625 S.FFDiag(S.Current->getSource(OpPC),
3626 diag::note_constexpr_bit_cast_unrepresentable_value)
3627 << S.getASTContext().BoolTy << Val;
3628 return false;
3629 }
3630 S.Stk.push<T>(T::bitcastFromMemory(Buff.data(), ResultBitWidth));
3631 } else {
3632 assert(!Sem);
3633 S.Stk.push<T>(T::bitcastFromMemory(Buff.data(), ResultBitWidth));
3634 }
3635 return true;
3636 }
3637}
3638
3639inline bool BitCast(InterpState &S, CodePtr OpPC) {
3640 const Pointer &FromPtr = S.Stk.pop<Pointer>();
3641 Pointer &ToPtr = S.Stk.peek<Pointer>();
3642
3643 if (!CheckLoad(S, OpPC, FromPtr))
3644 return false;
3645
3646 if (!DoBitCastPtr(S, OpPC, FromPtr, ToPtr))
3647 return false;
3648
3649 return true;
3650}
3651
3652/// Typeid support.
3653bool GetTypeid(InterpState &S, CodePtr OpPC, const Type *TypePtr,
3654 const Type *TypeInfoType);
3655bool GetTypeidPtr(InterpState &S, CodePtr OpPC, const Type *TypeInfoType);
3656bool DiagTypeid(InterpState &S, CodePtr OpPC);
3657
3658inline bool CheckDestruction(InterpState &S, CodePtr OpPC) {
3659 const auto &Ptr = S.Stk.peek<Pointer>();
3660 return CheckDestructor(S, OpPC, Ptr);
3661}
3662
3663inline bool CheckArraySize(InterpState &S, CodePtr OpPC, uint64_t NumElems) {
3664 uint64_t Limit = S.getLangOpts().ConstexprStepLimit;
3665 if (NumElems > Limit) {
3666 S.FFDiag(S.Current->getSource(OpPC),
3667 diag::note_constexpr_new_exceeds_limits)
3668 << NumElems << Limit;
3669 return false;
3670 }
3671 return true;
3672}
3673
3674//===----------------------------------------------------------------------===//
3675// Read opcode arguments
3676//===----------------------------------------------------------------------===//
3677
3678template <typename T> inline T ReadArg(InterpState &S, CodePtr &OpPC) {
3679 if constexpr (std::is_pointer<T>::value) {
3680 uint32_t ID = OpPC.read<uint32_t>();
3681 return reinterpret_cast<T>(S.P.getNativePointer(ID));
3682 } else {
3683 return OpPC.read<T>();
3684 }
3685}
3686
3687template <> inline Floating ReadArg<Floating>(InterpState &S, CodePtr &OpPC) {
3688 auto &Semantics =
3689 llvm::APFloatBase::EnumToSemantics(Floating::deserializeSemantics(*OpPC));
3690
3691 auto F = S.allocFloat(Semantics);
3692 Floating::deserialize(*OpPC, &F);
3693 OpPC += align(F.bytesToSerialize());
3694 return F;
3695}
3696
3697template <>
3698inline IntegralAP<false> ReadArg<IntegralAP<false>>(InterpState &S,
3699 CodePtr &OpPC) {
3700 uint32_t BitWidth = IntegralAP<false>::deserializeSize(*OpPC);
3701 auto Result = S.allocAP<IntegralAP<false>>(BitWidth);
3702 assert(Result.bitWidth() == BitWidth);
3703
3705 OpPC += align(Result.bytesToSerialize());
3706 return Result;
3707}
3708
3709template <>
3710inline IntegralAP<true> ReadArg<IntegralAP<true>>(InterpState &S,
3711 CodePtr &OpPC) {
3712 uint32_t BitWidth = IntegralAP<true>::deserializeSize(*OpPC);
3713 auto Result = S.allocAP<IntegralAP<true>>(BitWidth);
3714 assert(Result.bitWidth() == BitWidth);
3715
3717 OpPC += align(Result.bytesToSerialize());
3718 return Result;
3719}
3720
3721template <>
3724 OpPC += align(FP.bytesToSerialize());
3725 return FP;
3726}
3727
3728} // namespace interp
3729} // namespace clang
3730
3731#endif
Defines the clang::ASTContext interface.
#define V(N, I)
Definition: ASTContext.h:3597
ASTImporterLookupTable & LT
StringRef P
const Decl * D
Expr * E
llvm::APSInt APSInt
Definition: Compiler.cpp:23
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)
static std::string toString(const clang::SanitizerSet &Sanitizers)
Produce a string containing comma-separated names of sanitizers in Sanitizers set.
SourceLocation Loc
Definition: SemaObjC.cpp:754
a trap message and trap category.
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition: APValue.h:122
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
Definition: ASTContext.h:1223
bool hasSimilarType(QualType T1, QualType T2) const
Determine if two types are similar, according to the C++ rules.
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:2129
bool isVirtual() const
Definition: DeclCXX.h:2184
CXXRecordDecl * getCanonicalDecl() override
Retrieves the "canonical" declaration of the given declaration.
Definition: DeclCXX.h:522
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2879
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
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition: DeclBase.h:2109
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:1272
ValueDecl * getDecl()
Definition: Expr.h:1340
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
SourceLocation 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)
Definition: LangOptions.h:880
RoundingMode getRoundingMode() const
Definition: LangOptions.h:850
Represents a member of a struct/union/class.
Definition: Decl.h:3157
const RecordDecl * getParent() const
Returns the parent of this field declaration, which is the struct in which this field is defined.
Definition: Decl.h:3393
Implicit declaration of a temporary that was materialized by a MaterializeTemporaryExpr and lifetime-...
Definition: DeclCXX.h:3302
APValue * getOrCreateValue(bool MayCreate) const
Get the storage for the constant value of a materialized temporary of static storage duration.
Definition: DeclCXX.cpp:3324
Expr * getTemporaryExpr()
Retrieve the expression to which the temporary materialization conversion was applied.
Definition: DeclCXX.h:3348
OffsetOfExpr - [C99 7.17] - This represents an expression of the form offsetof(record-type,...
Definition: Expr.h:2529
A (possibly-)qualified type.
Definition: TypeBase.h:937
Represents a struct/union/class.
Definition: Decl.h:4309
SemaDiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID, bool DeferHint=false)
Emit a diagnostic.
Definition: SemaBase.cpp:61
ASTContext & getASTContext() const
Definition: Sema.h:918
const LangOptions & getLangOpts() const
Definition: Sema.h:911
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
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition: Type.cpp:752
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.
Definition: InterpBlock.h:111
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
Manages dynamic memory allocations done during bytecode interpretation.
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
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
Interpreter context.
Definition: InterpState.h:43
ComparisonCategoryResult compare(const MemberPointer &RHS) const
A pointer to a memory block, live or dead.
Definition: Pointer.h:90
static bool hasSameBase(const Pointer &A, const Pointer &B)
Checks if two pointers are comparable.
Definition: Pointer.cpp:618
Pointer narrow() const
Restricts the scope of an array element pointer.
Definition: Pointer.h:187
bool isInitialized() const
Checks if an object was initialized.
Definition: Pointer.cpp:432
bool isZeroSizeArray() const
Checks if the pointer is pointing to a zero-size array.
Definition: Pointer.h:650
Pointer atIndex(uint64_t Idx) const
Offsets a pointer inside an array.
Definition: Pointer.h:155
bool isDummy() const
Checks if the pointer points to a dummy value.
Definition: Pointer.h:543
Pointer atFieldSub(unsigned Off) const
Subtract the given offset from the current Base and Offset of the pointer.
Definition: Pointer.h:180
int64_t getIndex() const
Returns the index into an array.
Definition: Pointer.h:608
Pointer atField(unsigned Off) const
Creates a pointer to a field.
Definition: Pointer.h:172
T & deref() const
Dereferences the pointer, if it's live.
Definition: Pointer.h:659
unsigned getNumElems() const
Returns the number of elements.
Definition: Pointer.h:592
bool isUnknownSizeArray() const
Checks if the structure is an array of unknown size.
Definition: Pointer.h:411
void activate() const
Activats a field.
Definition: Pointer.cpp:560
static std::optional< std::pair< Pointer, Pointer > > computeSplitPoint(const Pointer &A, const Pointer &B)
Definition: Pointer.cpp:670
bool isIntegralPointer() const
Definition: Pointer.h:465
bool pointsToStringLiteral() const
Definition: Pointer.cpp:658
bool inArray() const
Checks if the innermost field is an array.
Definition: Pointer.h:393
T & elem(unsigned I) const
Dereferences the element at index I.
Definition: Pointer.h:675
uint64_t getByteOffset() const
Returns the byte offset from the start.
Definition: Pointer.h:581
bool isTypeidPointer() const
Definition: Pointer.h:467
std::string toDiagnosticString(const ASTContext &Ctx) const
Converts the pointer to a string usable in diagnostics.
Definition: Pointer.cpp:419
bool isZero() const
Checks if the pointer is null.
Definition: Pointer.h:253
ComparisonCategoryResult compare(const Pointer &Other) const
Compare two pointers.
Definition: Pointer.h:743
const IntPointer & asIntPointer() const
Definition: Pointer.h:451
bool isRoot() const
Pointer points directly to a block.
Definition: Pointer.h:433
const Descriptor * getDeclDesc() const
Accessor for information about the declaration site.
Definition: Pointer.h:278
unsigned getOffset() const
Returns the offset into an array.
Definition: Pointer.h:372
bool isOnePastEnd() const
Checks if the index is one past end.
Definition: Pointer.h:625
uint64_t getIntegerRepresentation() const
Definition: Pointer.h:142
Pointer expand() const
Expands a pointer to the containing array, undoing narrowing.
Definition: Pointer.h:220
bool isElementPastEnd() const
Checks if the pointer is an out-of-bounds element pointer.
Definition: Pointer.h:647
bool isBlockPointer() const
Definition: Pointer.h:464
const FunctionPointer & asFunctionPointer() const
Definition: Pointer.h:455
bool isFunctionPointer() const
Definition: Pointer.h:466
const Descriptor * getFieldDesc() const
Accessors for information about the innermost field.
Definition: Pointer.h:322
size_t elemSize() const
Returns the element size of the innermost field.
Definition: Pointer.h:354
bool canBeInitialized() const
If this pointer has an InlineDescriptor we can use to initialize.
Definition: Pointer.h:440
Lifetime getLifetime() const
Definition: Pointer.h:718
size_t computeOffsetForComparison() const
Compute an integer that can be used to compare this pointer to another one.
Definition: Pointer.cpp:360
const BlockPointer & asBlockPointer() const
Definition: Pointer.h:447
void initialize() const
Initializes a field.
Definition: Pointer.cpp:483
const Record * getRecord() const
Returns the record descriptor of a class.
Definition: Pointer.h:470
Structure/Class descriptor.
Definition: Record.h:25
const RecordDecl * getDecl() const
Returns the underlying declaration.
Definition: Record.h:53
Describes the statement/declaration an opcode was generated from.
Definition: Source.h:73
#define bool
Definition: gpuintrin.h:32
bool arePotentiallyOverlappingStringLiterals(const Pointer &LHS, const Pointer &RHS)
Definition: Interp.cpp:2119
bool EndSpeculation(InterpState &S, CodePtr OpPC)
Definition: Interp.h:3276
static bool ShiftFixedPoint(InterpState &S, CodePtr OpPC, bool Left)
Definition: Interp.h:3006
bool GetPtrFieldPop(InterpState &S, CodePtr OpPC, uint32_t Off)
Definition: Interp.cpp:1453
bool InitPop(InterpState &S, CodePtr OpPC)
Definition: Interp.h:2118
bool Shr(InterpState &S, CodePtr OpPC)
Definition: Interp.h:2969
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
llvm::APFloat APFloat
Definition: Floating.h:27
bool CheckDestruction(InterpState &S, CodePtr OpPC)
Definition: Interp.h:3658
bool ArrayElemPop(InterpState &S, CodePtr OpPC, uint32_t Index)
Definition: Interp.h:3151
bool CastPointerIntegralAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth)
Definition: Interp.cpp:2049
bool PopCC(InterpState &S, CodePtr OpPC)
Definition: Interp.h:3290
bool ArrayElem(InterpState &S, CodePtr OpPC, uint32_t Index)
Definition: Interp.h:3139
bool GT(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1282
bool CastPointerIntegralAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth)
Definition: Interp.cpp:2036
bool CheckInit(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Checks if a value can be initialized.
Definition: Interp.cpp:910
static bool CastFloatingIntegralAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth, uint32_t FPOI)
Definition: Interp.h:2595
bool GetMemberPtrBase(InterpState &S, CodePtr OpPC)
Definition: Interp.h:3230
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:3056
llvm::APInt APInt
Definition: FixedPoint.h:19
bool GetMemberPtrBasePop(InterpState &S, CodePtr OpPC, int32_t Off)
Definition: Interp.h:1847
bool InitThisField(InterpState &S, CodePtr OpPC, uint32_t I)
Definition: Interp.h:1569
Floating ReadArg< Floating >(InterpState &S, CodePtr &OpPC)
Definition: Interp.h:3687
bool Incf(InterpState &S, CodePtr OpPC, uint32_t FPOI)
Definition: Interp.h:950
bool SideEffect(InterpState &S, CodePtr OpPC)
Definition: Interp.h:3298
static bool ZeroIntAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth)
Definition: Interp.h:2774
bool DoShift(InterpState &S, CodePtr OpPC, LT &LHS, RT &RHS, LT *Result)
Definition: Interp.h:2845
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:1857
bool GetTypeidPtr(InterpState &S, CodePtr OpPC, const Type *TypeInfoType)
Definition: Interp.cpp:2085
bool Mulf(InterpState &S, CodePtr OpPC, uint32_t FPOI)
Definition: Interp.h:453
bool InitElemPop(InterpState &S, CodePtr OpPC, uint32_t Idx)
The same as InitElem, but pops the pointer as well.
Definition: Interp.h:2157
bool StoreBitField(InterpState &S, CodePtr OpPC)
Definition: Interp.h:2042
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:1104
bool BitCast(InterpState &S, CodePtr OpPC)
Definition: Interp.h:3639
bool LoadPop(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1959
bool Null(InterpState &S, CodePtr OpPC, uint64_t Value, const Descriptor *Desc)
Definition: Interp.h:2783
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:2376
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:1150
bool EndLifetime(InterpState &S, CodePtr OpPC)
Ends the lifetime of the peek'd pointer.
Definition: Interp.cpp:1843
bool GetTypeid(InterpState &S, CodePtr OpPC, const Type *TypePtr, const Type *TypeInfoType)
Typeid support.
Definition: Interp.cpp:2079
bool Dup(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1301
bool CheckThis(InterpState &S, CodePtr OpPC, const Pointer &This)
Checks the 'this' pointer.
Definition: Interp.cpp:1031
bool SetField(InterpState &S, CodePtr OpPC, uint32_t I)
Definition: Interp.h:1408
bool CheckNonNullArg(InterpState &S, CodePtr OpPC)
Definition: Interp.h:3393
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}_ordering type.
static bool IncDecPtrHelper(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Definition: Interp.h:2351
bool FinishInitActivate(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1877
bool GetPtrLocal(InterpState &S, CodePtr OpPC, uint32_t I)
Definition: Interp.h:1736
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:3445
bool CheckPointerToIntegralCast(InterpState &S, CodePtr OpPC, const Pointer &Ptr, unsigned BitWidth)
Definition: Interp.cpp:2016
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:1448
bool StoreActivate(InterpState &S, CodePtr OpPC)
Definition: Interp.h:2011
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:3207
bool FinishInitActivatePop(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1886
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:2398
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:1749
static bool Activate(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1993
bool handleFixedPointOverflow(InterpState &S, CodePtr OpPC, const FixedPoint &FP)
Definition: Interp.cpp:1995
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:3080
bool NE(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1260
bool NoRet(InterpState &S, CodePtr OpPC)
Definition: Interp.h:3046
bool GetIntPtr(InterpState &S, CodePtr OpPC, const Descriptor *Desc)
Definition: Interp.h:3214
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:2713
static bool ZeroIntAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth)
Definition: Interp.h:2766
bool BitCastPrim(InterpState &S, CodePtr OpPC, bool TargetIsUCharOrByte, uint32_t ResultBitWidth, const llvm::fltSemantics *Sem)
Definition: Interp.h:3577
bool Shl(InterpState &S, CodePtr OpPC)
Definition: Interp.h:2988
bool RVOPtr(InterpState &S, CodePtr OpPC)
Definition: Interp.h:2832
llvm::FixedPointSemantics FixedPointSemantics
Definition: Interp.h:41
bool CastPointerIntegral(InterpState &S, CodePtr OpPC)
Definition: Interp.h:2643
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:2338
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:2538
bool ExpandPtr(InterpState &S, CodePtr OpPC)
Definition: Interp.h:3062
bool Store(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1970
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:3110
bool InitScope(InterpState &S, CodePtr OpPC, uint32_t I)
Definition: Interp.h:2481
bool CheckDynamicMemoryAllocation(InterpState &S, CodePtr OpPC)
Checks if dynamic memory allocation is available in the current language mode.
Definition: Interp.cpp:1095
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:1638
bool CmpHelperEQ(InterpState &S, CodePtr OpPC, CompareFn Fn)
Definition: Interp.h:1018
llvm::function_ref< bool(ComparisonCategoryResult)> CompareFn
Definition: Interp.h:1003
T ReadArg(InterpState &S, CodePtr &OpPC)
Definition: Interp.h:3678
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:2565
bool ArrayDecay(InterpState &S, CodePtr OpPC)
Just takes a pointer and checks if it's an incomplete array type.
Definition: Interp.h:3183
bool PushCC(InterpState &S, CodePtr OpPC, bool Value)
Definition: Interp.h:3286
bool GetPtrDerivedPop(InterpState &S, CodePtr OpPC, uint32_t Off, bool NullOK, const Type *TargetType)
Definition: Interp.h:1769
bool DiagTypeid(InterpState &S, CodePtr OpPC)
Definition: Interp.cpp:2111
bool CheckFinalLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
This is not used by any of the opcodes directly.
Definition: Interp.cpp:841
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:1550
bool CheckBitCast(InterpState &S, CodePtr OpPC, bool HasIndeterminateBits, bool TargetIsUCharOrByte)
Definition: Interp.cpp:2062
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:3378
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
FixedPoint ReadArg< FixedPoint >(InterpState &S, CodePtr &OpPC)
Definition: Interp.h:3722
static bool CastFloatingFixedPoint(InterpState &S, CodePtr OpPC, uint32_t FPS)
Definition: Interp.h:2674
void diagnoseEnumValue(InterpState &S, CodePtr OpPC, const EnumDecl *ED, const APSInt &Value)
Definition: Interp.cpp:1358
bool StartLifetime(InterpState &S, CodePtr OpPC)
Definition: Interp.cpp:1812
bool LE(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1274
bool CheckNewTypeMismatchArray(InterpState &S, CodePtr OpPC, const Expr *E)
Definition: Interp.h:3570
bool InitThisBitField(InterpState &S, CodePtr OpPC, const Record::Field *F, uint32_t FieldOffset)
Definition: Interp.h:1600
bool Unsupported(InterpState &S, CodePtr OpPC)
Definition: Interp.h:3259
bool InvalidDeclRef(InterpState &S, CodePtr OpPC, const DeclRefExpr *DR, bool InitializerFailed)
Definition: Interp.h:3334
bool CheckStore(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Checks if a value can be stored in a block.
Definition: Interp.cpp:872
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:1122
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:1048
bool CastFP(InterpState &S, CodePtr OpPC, const llvm::fltSemantics *Sem, llvm::RoundingMode RM)
1) Pops a Floating from the stack.
Definition: Interp.h:2499
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:1617
bool SetThisField(InterpState &S, CodePtr OpPC, uint32_t I)
Definition: Interp.h:1454
bool StoreBitFieldPop(InterpState &S, CodePtr OpPC)
Definition: Interp.h:2057
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:1502
static bool DecPtr(InterpState &S, CodePtr OpPC)
Definition: Interp.h:2385
static bool CheckAllocations(InterpState &S, CodePtr OpPC)
Definition: Interp.h:3560
bool Alloc(InterpState &S, CodePtr OpPC, const Descriptor *Desc)
Definition: Interp.h:3464
bool InvalidShuffleVectorIndex(InterpState &S, CodePtr OpPC, uint32_t Index)
Definition: Interp.cpp:2008
bool ToMemberPtr(InterpState &S, CodePtr OpPC)
Definition: Interp.h:2189
bool CheckDummy(InterpState &S, CodePtr OpPC, const Block *B, AccessKinds AK)
Checks if a pointer is a dummy pointer.
Definition: Interp.cpp:1155
static bool CastIntegralFixedPoint(InterpState &S, CodePtr OpPC, uint32_t FPS)
Definition: Interp.h:2658
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:1912
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:1870
bool GetMemberPtr(InterpState &S, CodePtr OpPC, const ValueDecl *D)
Definition: Interp.h:3225
bool Dump(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1897
bool SizelessVectorElementSize(InterpState &S, CodePtr OpPC)
Definition: Interp.h:3349
static bool PtrPtrCast(InterpState &S, CodePtr OpPC, bool SrcIsVoidPtr)
Definition: Interp.h:2721
bool CheckLiteralType(InterpState &S, CodePtr OpPC, const Type *T)
Definition: Interp.cpp:1382
bool IsNonNull(InterpState &S, CodePtr OpPC)
Definition: Interp.h:2792
bool GetPtrThisField(InterpState &S, CodePtr OpPC, uint32_t Off)
Definition: Interp.h:1759
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:1583
bool GetPtrBase(InterpState &S, CodePtr OpPC, uint32_t Off)
Definition: Interp.h:1803
bool SetParam(InterpState &S, CodePtr OpPC, uint32_t I)
Definition: Interp.h:1386
bool StoreActivatePop(InterpState &S, CodePtr OpPC)
Definition: Interp.h:2027
bool GetMemberPtrDecl(InterpState &S, CodePtr OpPC)
Definition: Interp.h:3240
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:2690
bool Divf(InterpState &S, CodePtr OpPC, uint32_t FPOI)
Definition: Interp.h:718
bool FinishInitGlobal(InterpState &S, CodePtr OpPC)
Definition: Interp.cpp:2246
bool DecayPtr(InterpState &S, CodePtr OpPC)
OldPtr -> Integer -> NewPtr.
Definition: Interp.h:3421
static bool ActivateThisField(InterpState &S, CodePtr OpPC, uint32_t I)
Definition: Interp.h:2000
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:1923
bool StorePop(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1982
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:1870
static bool CastFloatingIntegralAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth, uint32_t FPOI)
Definition: Interp.h:2616
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:2132
bool Destroy(InterpState &S, CodePtr OpPC, uint32_t I)
Definition: Interp.h:2453
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:1669
bool StoreBitFieldActivate(InterpState &S, CodePtr OpPC)
Definition: Interp.h:2072
bool CheckPseudoDtor(InterpState &S, CodePtr OpPC)
Definition: Interp.h:3358
bool Free(InterpState &S, CodePtr OpPC, bool DeleteIsArrayForm, bool IsGlobalDelete)
Definition: Interp.cpp:1259
bool PreDec(InterpState &S, CodePtr OpPC, bool CanOverflow)
Definition: Interp.h:922
bool InvalidNewDeleteExpr(InterpState &S, CodePtr OpPC, const Expr *E)
Definition: Interp.cpp:1951
bool CheckArraySize(InterpState &S, CodePtr OpPC, uint64_t NumElems)
Definition: Interp.h:3663
bool CallBI(InterpState &S, CodePtr OpPC, const CallExpr *CE, uint32_t BuiltinID)
Definition: Interp.cpp:1735
bool CheckLocalLoad(InterpState &S, CodePtr OpPC, const Block *B)
Definition: Interp.cpp:771
bool FinishInitPop(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1863
bool Neg(InterpState &S, CodePtr OpPC)
Definition: Interp.h:749
llvm::APSInt APSInt
Definition: FixedPoint.h:20
bool StartSpeculation(InterpState &S, CodePtr OpPC)
Definition: Interp.h:3266
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:2212
bool BitOr(InterpState &S, CodePtr OpPC)
1) Pops the RHS from the stack.
Definition: Interp.h:628
bool Inv(InterpState &S, CodePtr OpPC)
Definition: Interp.h:738
bool Load(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1948
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:2490
bool StoreBitFieldActivatePop(InterpState &S, CodePtr OpPC)
Definition: Interp.h:2089
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:1824
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 CmpHelperEQ< MemberPointer >(InterpState &S, CodePtr OpPC, CompareFn Fn)
Definition: Interp.h:1186
bool AddOffset(InterpState &S, CodePtr OpPC)
Definition: Interp.h:2323
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:2179
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:1746
bool CmpHelperEQ< Pointer >(InterpState &S, CodePtr OpPC, CompareFn Fn)
Definition: Interp.h:1077
static bool CastFixedPointIntegral(InterpState &S, CodePtr OpPC)
Definition: Interp.h:2700
constexpr bool isIntegralType(PrimType T)
Definition: PrimType.h:124
bool CallVirt(InterpState &S, CodePtr OpPC, const Function *Func, uint32_t VarArgSize)
Definition: Interp.cpp:1637
bool CastIntegralFloating(InterpState &S, CodePtr OpPC, const llvm::fltSemantics *Sem, uint32_t FPOI)
Definition: Interp.h:2550
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:2508
bool GetPtrParam(InterpState &S, CodePtr OpPC, uint32_t I)
Definition: Interp.h:1741
bool AllocCN(InterpState &S, CodePtr OpPC, const Descriptor *ElementDesc, bool IsNoThrow)
Definition: Interp.h:3519
bool GetGlobal(InterpState &S, CodePtr OpPC, uint32_t I)
Definition: Interp.h:1469
bool Interpret(InterpState &S)
Interpreter entry point.
Definition: Interp.cpp:2262
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:1932
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:3303
bool DoShiftAP(InterpState &S, CodePtr OpPC, const APSInt &LHS, APSInt RHS, LT *Result)
A version of DoShift that works on IntegralAP.
Definition: Interp.h:2925
bool CastMemberPtrPtr(InterpState &S, CodePtr OpPC)
Definition: Interp.h:2197
bool Ret(InterpState &S, CodePtr &PC)
Definition: Interp.h:312
bool InitFieldActivate(InterpState &S, CodePtr OpPC, uint32_t I)
Definition: Interp.h:1653
bool CheckDestructor(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Definition: Interp.cpp:1479
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:1700
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:2526
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:3365
bool GetPtrThisBase(InterpState &S, CodePtr OpPC, uint32_t Off)
Definition: Interp.h:1853
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:3555
bool AllocN(InterpState &S, CodePtr OpPC, PrimType T, const Expr *Source, bool IsNoThrow)
Definition: Interp.h:3479
bool CheckEnumValue(InterpState &S, CodePtr OpPC, const EnumDecl *ED)
Definition: Interp.h:3408
The JSON file list parser is used to communicate input to InstallAPI.
@ 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.
AccessKinds
Kinds of access we can perform on an object, for diagnostics.
Definition: State.h:26
@ AK_Increment
Definition: State.h:30
@ AK_Read
Definition: State.h:27
@ AK_Decrement
Definition: State.h:31
const FunctionProtoType * T
A quantity in bits.
Definition: BitcastBuffer.h:24
size_t getQuantity() const
Definition: BitcastBuffer.h:29
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
Definition: Descriptor.cpp:371
const Decl * asDecl() const
Definition: Descriptor.h:210
SourceLocation getLocation() const
Definition: Descriptor.cpp:438
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
IntPointer baseCast(const ASTContext &ASTCtx, unsigned BaseOffset) const
Definition: Pointer.cpp:908
const Descriptor * Desc
Definition: Pointer.h:47
Mapping from primitive types to their representation.
Definition: PrimType.h:134