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