clang 20.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 "Boolean.h"
18#include "DynamicAllocator.h"
19#include "Floating.h"
20#include "Function.h"
21#include "FunctionPointer.h"
22#include "InterpFrame.h"
23#include "InterpStack.h"
24#include "InterpState.h"
25#include "MemberPointer.h"
26#include "Opcode.h"
27#include "PrimType.h"
28#include "Program.h"
29#include "State.h"
31#include "clang/AST/Expr.h"
32#include "llvm/ADT/APFloat.h"
33#include "llvm/ADT/APSInt.h"
34#include <type_traits>
35
36namespace clang {
37namespace interp {
38
39using APSInt = llvm::APSInt;
40
41/// Convert a value to an APValue.
42template <typename T>
43bool ReturnValue(const InterpState &S, const T &V, APValue &R) {
44 R = V.toAPValue(S.getCtx());
45 return true;
46}
47
48/// Checks if the variable has externally defined storage.
49bool CheckExtern(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
50
51/// Checks if the array is offsetable.
52bool CheckArray(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
53
54/// Checks if a pointer is live and accessible.
55bool CheckLive(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
56 AccessKinds AK);
57
58/// Checks if a pointer is a dummy pointer.
59bool CheckDummy(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
60 AccessKinds AK);
61
62/// Checks if a pointer is null.
63bool CheckNull(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
65
66/// Checks if a pointer is in range.
67bool CheckRange(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
68 AccessKinds AK);
69
70/// Checks if a field from which a pointer is going to be derived is valid.
71bool CheckRange(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
73
74/// Checks if Ptr is a one-past-the-end pointer.
75bool CheckSubobject(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
77
78/// Checks if the dowcast using the given offset is possible with the given
79/// pointer.
80bool CheckDowncast(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
81 uint32_t Offset);
82
83/// Checks if a pointer points to const storage.
84bool CheckConst(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
85
86/// Checks if the Descriptor is of a constexpr or const global variable.
87bool CheckConstant(InterpState &S, CodePtr OpPC, const Descriptor *Desc);
88
89/// Checks if a pointer points to a mutable field.
90bool CheckMutable(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
91
92/// Checks if a value can be loaded from a block.
93bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
94 AccessKinds AK = AK_Read);
95bool CheckFinalLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
96
97bool CheckInitialized(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
98 AccessKinds AK);
99/// Check if a global variable is initialized.
100bool CheckGlobalInitialized(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
101
102/// Checks if a value can be stored in a block.
103bool CheckStore(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
104
105/// Checks if a method can be invoked on an object.
106bool CheckInvoke(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
107
108/// Checks if a value can be initialized.
109bool CheckInit(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
110
111/// Checks if a method can be called.
112bool CheckCallable(InterpState &S, CodePtr OpPC, const Function *F);
113
114/// Checks if calling the currently active function would exceed
115/// the allowed call depth.
116bool CheckCallDepth(InterpState &S, CodePtr OpPC);
117
118/// Checks the 'this' pointer.
119bool CheckThis(InterpState &S, CodePtr OpPC, const Pointer &This);
120
121/// Checks if a method is pure virtual.
122bool CheckPure(InterpState &S, CodePtr OpPC, const CXXMethodDecl *MD);
123
124/// Checks if all the arguments annotated as 'nonnull' are in fact not null.
125bool CheckNonNullArgs(InterpState &S, CodePtr OpPC, const Function *F,
126 const CallExpr *CE, unsigned ArgSize);
127
128/// Checks if dynamic memory allocation is available in the current
129/// language mode.
130bool CheckDynamicMemoryAllocation(InterpState &S, CodePtr OpPC);
131
132/// Diagnose mismatched new[]/delete or new/delete[] pairs.
133bool CheckNewDeleteForms(InterpState &S, CodePtr OpPC, bool NewWasArray,
134 bool DeleteIsArray, const Descriptor *D,
135 const Expr *NewExpr);
136
137/// Check the source of the pointer passed to delete/delete[] has actually
138/// been heap allocated by us.
139bool CheckDeleteSource(InterpState &S, CodePtr OpPC, const Expr *Source,
140 const Pointer &Ptr);
141
142/// Sets the given integral value to the pointer, which is of
143/// a std::{weak,partial,strong}_ordering type.
144bool SetThreeWayComparisonField(InterpState &S, CodePtr OpPC,
145 const Pointer &Ptr, const APSInt &IntValue);
146
147/// Copy the contents of Src into Dest.
148bool DoMemcpy(InterpState &S, CodePtr OpPC, const Pointer &Src, Pointer &Dest);
149
150/// Checks if the shift operation is legal.
151template <typename LT, typename RT>
152bool CheckShift(InterpState &S, CodePtr OpPC, const LT &LHS, const RT &RHS,
153 unsigned Bits) {
154 if (RHS.isNegative()) {
155 const SourceInfo &Loc = S.Current->getSource(OpPC);
156 S.CCEDiag(Loc, diag::note_constexpr_negative_shift) << RHS.toAPSInt();
157 if (!S.noteUndefinedBehavior())
158 return false;
159 }
160
161 // C++11 [expr.shift]p1: Shift width must be less than the bit width of
162 // the shifted type.
163 if (Bits > 1 && RHS >= RT::from(Bits, RHS.bitWidth())) {
164 const Expr *E = S.Current->getExpr(OpPC);
165 const APSInt Val = RHS.toAPSInt();
166 QualType Ty = E->getType();
167 S.CCEDiag(E, diag::note_constexpr_large_shift) << Val << Ty << Bits;
168 if (!S.noteUndefinedBehavior())
169 return false;
170 }
171
172 if (LHS.isSigned() && !S.getLangOpts().CPlusPlus20) {
173 const Expr *E = S.Current->getExpr(OpPC);
174 // C++11 [expr.shift]p2: A signed left shift must have a non-negative
175 // operand, and must not overflow the corresponding unsigned type.
176 if (LHS.isNegative()) {
177 S.CCEDiag(E, diag::note_constexpr_lshift_of_negative) << LHS.toAPSInt();
178 if (!S.noteUndefinedBehavior())
179 return false;
180 } else if (LHS.toUnsigned().countLeadingZeros() <
181 static_cast<unsigned>(RHS)) {
182 S.CCEDiag(E, diag::note_constexpr_lshift_discards);
183 if (!S.noteUndefinedBehavior())
184 return false;
185 }
186 }
187
188 // C++2a [expr.shift]p2: [P0907R4]:
189 // E1 << E2 is the unique value congruent to
190 // E1 x 2^E2 module 2^N.
191 return true;
192}
193
194/// Checks if Div/Rem operation on LHS and RHS is valid.
195template <typename T>
196bool CheckDivRem(InterpState &S, CodePtr OpPC, const T &LHS, const T &RHS) {
197 if (RHS.isZero()) {
198 const auto *Op = cast<BinaryOperator>(S.Current->getExpr(OpPC));
199 if constexpr (std::is_same_v<T, Floating>) {
200 S.CCEDiag(Op, diag::note_expr_divide_by_zero)
201 << Op->getRHS()->getSourceRange();
202 return true;
203 }
204
205 S.FFDiag(Op, diag::note_expr_divide_by_zero)
206 << Op->getRHS()->getSourceRange();
207 return false;
208 }
209
210 if (LHS.isSigned() && LHS.isMin() && RHS.isNegative() && RHS.isMinusOne()) {
211 APSInt LHSInt = LHS.toAPSInt();
212 SmallString<32> Trunc;
213 (-LHSInt.extend(LHSInt.getBitWidth() + 1)).toString(Trunc, 10);
214 const SourceInfo &Loc = S.Current->getSource(OpPC);
215 const Expr *E = S.Current->getExpr(OpPC);
216 S.CCEDiag(Loc, diag::note_constexpr_overflow) << Trunc << E->getType();
217 return false;
218 }
219 return true;
220}
221
222template <typename SizeT>
223bool CheckArraySize(InterpState &S, CodePtr OpPC, SizeT *NumElements,
224 unsigned ElemSize, bool IsNoThrow) {
225 // FIXME: Both the SizeT::from() as well as the
226 // NumElements.toAPSInt() in this function are rather expensive.
227
228 // FIXME: GH63562
229 // APValue stores array extents as unsigned,
230 // so anything that is greater that unsigned would overflow when
231 // constructing the array, we catch this here.
232 SizeT MaxElements = SizeT::from(Descriptor::MaxArrayElemBytes / ElemSize);
233 if (NumElements->toAPSInt().getActiveBits() >
235 *NumElements > MaxElements) {
236 if (!IsNoThrow) {
237 const SourceInfo &Loc = S.Current->getSource(OpPC);
238 S.FFDiag(Loc, diag::note_constexpr_new_too_large)
239 << NumElements->toDiagnosticString(S.getCtx());
240 }
241 return false;
242 }
243 return true;
244}
245
246/// Checks if the result of a floating-point operation is valid
247/// in the current context.
248bool CheckFloatResult(InterpState &S, CodePtr OpPC, const Floating &Result,
249 APFloat::opStatus Status);
250
251/// Checks why the given DeclRefExpr is invalid.
252bool CheckDeclRef(InterpState &S, CodePtr OpPC, const DeclRefExpr *DR);
253
254/// Interpreter entry point.
255bool Interpret(InterpState &S, APValue &Result);
256
257/// Interpret a builtin function.
258bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F,
259 const CallExpr *Call);
260
261/// Interpret an offsetof operation.
262bool InterpretOffsetOf(InterpState &S, CodePtr OpPC, const OffsetOfExpr *E,
263 llvm::ArrayRef<int64_t> ArrayIndices, int64_t &Result);
264
265inline bool Invalid(InterpState &S, CodePtr OpPC);
266
267enum class ArithOp { Add, Sub };
268
269//===----------------------------------------------------------------------===//
270// Returning values
271//===----------------------------------------------------------------------===//
272
273void cleanupAfterFunctionCall(InterpState &S, CodePtr OpPC);
274
275template <PrimType Name, class T = typename PrimConv<Name>::T>
277 const T &Ret = S.Stk.pop<T>();
278
279 // Make sure returned pointers are live. We might be trying to return a
280 // pointer or reference to a local variable.
281 // Just return false, since a diagnostic has already been emitted in Sema.
282 if constexpr (std::is_same_v<T, Pointer>) {
283 // FIXME: We could be calling isLive() here, but the emitted diagnostics
284 // seem a little weird, at least if the returned expression is of
285 // pointer type.
286 // Null pointers are considered live here.
287 if (!Ret.isZero() && !Ret.isLive())
288 return false;
289 }
290
291 assert(S.Current);
292 assert(S.Current->getFrameOffset() == S.Stk.size() && "Invalid frame");
293 if (!S.checkingPotentialConstantExpression() || S.Current->Caller)
295
296 if (InterpFrame *Caller = S.Current->Caller) {
297 PC = S.Current->getRetPC();
298 delete S.Current;
299 S.Current = Caller;
300 S.Stk.push<T>(Ret);
301 } else {
302 delete S.Current;
303 S.Current = nullptr;
304 if (!ReturnValue<T>(S, Ret, Result))
305 return false;
306 }
307 return true;
308}
309
310inline bool RetVoid(InterpState &S, CodePtr &PC, APValue &Result) {
311 assert(S.Current->getFrameOffset() == S.Stk.size() && "Invalid frame");
312
313 if (!S.checkingPotentialConstantExpression() || S.Current->Caller)
315
316 if (InterpFrame *Caller = S.Current->Caller) {
317 PC = S.Current->getRetPC();
318 delete S.Current;
319 S.Current = Caller;
320 } else {
321 delete S.Current;
322 S.Current = nullptr;
323 }
324 return true;
325}
326
327//===----------------------------------------------------------------------===//
328// Add, Sub, Mul
329//===----------------------------------------------------------------------===//
330
331template <typename T, bool (*OpFW)(T, T, unsigned, T *),
332 template <typename U> class OpAP>
333bool AddSubMulHelper(InterpState &S, CodePtr OpPC, unsigned Bits, const T &LHS,
334 const T &RHS) {
335 // Fast path - add the numbers with fixed width.
336 T Result;
337 if (!OpFW(LHS, RHS, Bits, &Result)) {
338 S.Stk.push<T>(Result);
339 return true;
340 }
341
342 // If for some reason evaluation continues, use the truncated results.
343 S.Stk.push<T>(Result);
344
345 // Slow path - compute the result using another bit of precision.
346 APSInt Value = OpAP<APSInt>()(LHS.toAPSInt(Bits), RHS.toAPSInt(Bits));
347
348 // Report undefined behaviour, stopping if required.
349 const Expr *E = S.Current->getExpr(OpPC);
350 QualType Type = E->getType();
351 if (S.checkingForUndefinedBehavior()) {
352 SmallString<32> Trunc;
353 Value.trunc(Result.bitWidth())
354 .toString(Trunc, 10, Result.isSigned(), /*formatAsCLiteral=*/false,
355 /*UpperCase=*/true, /*InsertSeparators=*/true);
356 auto Loc = E->getExprLoc();
357 S.report(Loc, diag::warn_integer_constant_overflow)
358 << Trunc << Type << E->getSourceRange();
359 }
360
361 S.CCEDiag(E, diag::note_constexpr_overflow) << Value << Type;
362
363 if (!S.noteUndefinedBehavior()) {
364 S.Stk.pop<T>();
365 return false;
366 }
367
368 return true;
369}
370
371template <PrimType Name, class T = typename PrimConv<Name>::T>
372bool Add(InterpState &S, CodePtr OpPC) {
373 const T &RHS = S.Stk.pop<T>();
374 const T &LHS = S.Stk.pop<T>();
375 const unsigned Bits = RHS.bitWidth() + 1;
376 return AddSubMulHelper<T, T::add, std::plus>(S, OpPC, Bits, LHS, RHS);
377}
378
379inline bool Addf(InterpState &S, CodePtr OpPC, llvm::RoundingMode RM) {
380 const Floating &RHS = S.Stk.pop<Floating>();
381 const Floating &LHS = S.Stk.pop<Floating>();
382
384 auto Status = Floating::add(LHS, RHS, RM, &Result);
385 S.Stk.push<Floating>(Result);
386 return CheckFloatResult(S, OpPC, Result, Status);
387}
388
389template <PrimType Name, class T = typename PrimConv<Name>::T>
390bool Sub(InterpState &S, CodePtr OpPC) {
391 const T &RHS = S.Stk.pop<T>();
392 const T &LHS = S.Stk.pop<T>();
393 const unsigned Bits = RHS.bitWidth() + 1;
394 return AddSubMulHelper<T, T::sub, std::minus>(S, OpPC, Bits, LHS, RHS);
395}
396
397inline bool Subf(InterpState &S, CodePtr OpPC, llvm::RoundingMode RM) {
398 const Floating &RHS = S.Stk.pop<Floating>();
399 const Floating &LHS = S.Stk.pop<Floating>();
400
402 auto Status = Floating::sub(LHS, RHS, RM, &Result);
403 S.Stk.push<Floating>(Result);
404 return CheckFloatResult(S, OpPC, Result, Status);
405}
406
407template <PrimType Name, class T = typename PrimConv<Name>::T>
408bool Mul(InterpState &S, CodePtr OpPC) {
409 const T &RHS = S.Stk.pop<T>();
410 const T &LHS = S.Stk.pop<T>();
411 const unsigned Bits = RHS.bitWidth() * 2;
412 return AddSubMulHelper<T, T::mul, std::multiplies>(S, OpPC, Bits, LHS, RHS);
413}
414
415inline bool Mulf(InterpState &S, CodePtr OpPC, llvm::RoundingMode RM) {
416 const Floating &RHS = S.Stk.pop<Floating>();
417 const Floating &LHS = S.Stk.pop<Floating>();
418
420 auto Status = Floating::mul(LHS, RHS, RM, &Result);
421 S.Stk.push<Floating>(Result);
422 return CheckFloatResult(S, OpPC, Result, Status);
423}
424
425template <PrimType Name, class T = typename PrimConv<Name>::T>
426inline bool Mulc(InterpState &S, CodePtr OpPC) {
427 const Pointer &RHS = S.Stk.pop<Pointer>();
428 const Pointer &LHS = S.Stk.pop<Pointer>();
429 const Pointer &Result = S.Stk.peek<Pointer>();
430
431 if constexpr (std::is_same_v<T, Floating>) {
432 APFloat A = LHS.atIndex(0).deref<Floating>().getAPFloat();
433 APFloat B = LHS.atIndex(1).deref<Floating>().getAPFloat();
434 APFloat C = RHS.atIndex(0).deref<Floating>().getAPFloat();
435 APFloat D = RHS.atIndex(1).deref<Floating>().getAPFloat();
436
437 APFloat ResR(A.getSemantics());
438 APFloat ResI(A.getSemantics());
439 HandleComplexComplexMul(A, B, C, D, ResR, ResI);
440
441 // Copy into the result.
442 Result.atIndex(0).deref<Floating>() = Floating(ResR);
443 Result.atIndex(0).initialize();
444 Result.atIndex(1).deref<Floating>() = Floating(ResI);
445 Result.atIndex(1).initialize();
446 Result.initialize();
447 } else {
448 // Integer element type.
449 const T &LHSR = LHS.atIndex(0).deref<T>();
450 const T &LHSI = LHS.atIndex(1).deref<T>();
451 const T &RHSR = RHS.atIndex(0).deref<T>();
452 const T &RHSI = RHS.atIndex(1).deref<T>();
453 unsigned Bits = LHSR.bitWidth();
454
455 // real(Result) = (real(LHS) * real(RHS)) - (imag(LHS) * imag(RHS))
456 T A;
457 if (T::mul(LHSR, RHSR, Bits, &A))
458 return false;
459 T B;
460 if (T::mul(LHSI, RHSI, Bits, &B))
461 return false;
462 if (T::sub(A, B, Bits, &Result.atIndex(0).deref<T>()))
463 return false;
464 Result.atIndex(0).initialize();
465
466 // imag(Result) = (real(LHS) * imag(RHS)) + (imag(LHS) * real(RHS))
467 if (T::mul(LHSR, RHSI, Bits, &A))
468 return false;
469 if (T::mul(LHSI, RHSR, Bits, &B))
470 return false;
471 if (T::add(A, B, Bits, &Result.atIndex(1).deref<T>()))
472 return false;
473 Result.atIndex(1).initialize();
474 Result.initialize();
475 }
476
477 return true;
478}
479
480template <PrimType Name, class T = typename PrimConv<Name>::T>
481inline bool Divc(InterpState &S, CodePtr OpPC) {
482 const Pointer &RHS = S.Stk.pop<Pointer>();
483 const Pointer &LHS = S.Stk.pop<Pointer>();
484 const Pointer &Result = S.Stk.peek<Pointer>();
485
486 if constexpr (std::is_same_v<T, Floating>) {
487 APFloat A = LHS.atIndex(0).deref<Floating>().getAPFloat();
488 APFloat B = LHS.atIndex(1).deref<Floating>().getAPFloat();
489 APFloat C = RHS.atIndex(0).deref<Floating>().getAPFloat();
490 APFloat D = RHS.atIndex(1).deref<Floating>().getAPFloat();
491
492 APFloat ResR(A.getSemantics());
493 APFloat ResI(A.getSemantics());
494 HandleComplexComplexDiv(A, B, C, D, ResR, ResI);
495
496 // Copy into the result.
497 Result.atIndex(0).deref<Floating>() = Floating(ResR);
498 Result.atIndex(0).initialize();
499 Result.atIndex(1).deref<Floating>() = Floating(ResI);
500 Result.atIndex(1).initialize();
501 Result.initialize();
502 } else {
503 // Integer element type.
504 const T &LHSR = LHS.atIndex(0).deref<T>();
505 const T &LHSI = LHS.atIndex(1).deref<T>();
506 const T &RHSR = RHS.atIndex(0).deref<T>();
507 const T &RHSI = RHS.atIndex(1).deref<T>();
508 unsigned Bits = LHSR.bitWidth();
509 const T Zero = T::from(0, Bits);
510
513 const SourceInfo &E = S.Current->getSource(OpPC);
514 S.FFDiag(E, diag::note_expr_divide_by_zero);
515 return false;
516 }
517
518 // Den = real(RHS)² + imag(RHS)²
519 T A, B;
520 if (T::mul(RHSR, RHSR, Bits, &A) || T::mul(RHSI, RHSI, Bits, &B))
521 return false;
522 T Den;
523 if (T::add(A, B, Bits, &Den))
524 return false;
525
526 // real(Result) = ((real(LHS) * real(RHS)) + (imag(LHS) * imag(RHS))) / Den
527 T &ResultR = Result.atIndex(0).deref<T>();
528 T &ResultI = Result.atIndex(1).deref<T>();
529
530 if (T::mul(LHSR, RHSR, Bits, &A) || T::mul(LHSI, RHSI, Bits, &B))
531 return false;
532 if (T::add(A, B, Bits, &ResultR))
533 return false;
534 if (T::div(ResultR, Den, Bits, &ResultR))
535 return false;
536 Result.atIndex(0).initialize();
537
538 // imag(Result) = ((imag(LHS) * real(RHS)) - (real(LHS) * imag(RHS))) / Den
539 if (T::mul(LHSI, RHSR, Bits, &A) || T::mul(LHSR, RHSI, Bits, &B))
540 return false;
541 if (T::sub(A, B, Bits, &ResultI))
542 return false;
543 if (T::div(ResultI, Den, Bits, &ResultI))
544 return false;
545 Result.atIndex(1).initialize();
546 Result.initialize();
547 }
548
549 return true;
550}
551
552/// 1) Pops the RHS from the stack.
553/// 2) Pops the LHS from the stack.
554/// 3) Pushes 'LHS & RHS' on the stack
555template <PrimType Name, class T = typename PrimConv<Name>::T>
556bool BitAnd(InterpState &S, CodePtr OpPC) {
557 const T &RHS = S.Stk.pop<T>();
558 const T &LHS = S.Stk.pop<T>();
559
560 unsigned Bits = RHS.bitWidth();
561 T Result;
562 if (!T::bitAnd(LHS, RHS, Bits, &Result)) {
563 S.Stk.push<T>(Result);
564 return true;
565 }
566 return false;
567}
568
569/// 1) Pops the RHS from the stack.
570/// 2) Pops the LHS from the stack.
571/// 3) Pushes 'LHS | RHS' on the stack
572template <PrimType Name, class T = typename PrimConv<Name>::T>
573bool BitOr(InterpState &S, CodePtr OpPC) {
574 const T &RHS = S.Stk.pop<T>();
575 const T &LHS = S.Stk.pop<T>();
576
577 unsigned Bits = RHS.bitWidth();
578 T Result;
579 if (!T::bitOr(LHS, RHS, Bits, &Result)) {
580 S.Stk.push<T>(Result);
581 return true;
582 }
583 return false;
584}
585
586/// 1) Pops the RHS from the stack.
587/// 2) Pops the LHS from the stack.
588/// 3) Pushes 'LHS ^ RHS' on the stack
589template <PrimType Name, class T = typename PrimConv<Name>::T>
590bool BitXor(InterpState &S, CodePtr OpPC) {
591 const T &RHS = S.Stk.pop<T>();
592 const T &LHS = S.Stk.pop<T>();
593
594 unsigned Bits = RHS.bitWidth();
595 T Result;
596 if (!T::bitXor(LHS, RHS, Bits, &Result)) {
597 S.Stk.push<T>(Result);
598 return true;
599 }
600 return false;
601}
602
603/// 1) Pops the RHS from the stack.
604/// 2) Pops the LHS from the stack.
605/// 3) Pushes 'LHS % RHS' on the stack (the remainder of dividing LHS by RHS).
606template <PrimType Name, class T = typename PrimConv<Name>::T>
607bool Rem(InterpState &S, CodePtr OpPC) {
608 const T &RHS = S.Stk.pop<T>();
609 const T &LHS = S.Stk.pop<T>();
610
611 if (!CheckDivRem(S, OpPC, LHS, RHS))
612 return false;
613
614 const unsigned Bits = RHS.bitWidth() * 2;
615 T Result;
616 if (!T::rem(LHS, RHS, Bits, &Result)) {
617 S.Stk.push<T>(Result);
618 return true;
619 }
620 return false;
621}
622
623/// 1) Pops the RHS from the stack.
624/// 2) Pops the LHS from the stack.
625/// 3) Pushes 'LHS / RHS' on the stack
626template <PrimType Name, class T = typename PrimConv<Name>::T>
627bool Div(InterpState &S, CodePtr OpPC) {
628 const T &RHS = S.Stk.pop<T>();
629 const T &LHS = S.Stk.pop<T>();
630
631 if (!CheckDivRem(S, OpPC, LHS, RHS))
632 return false;
633
634 const unsigned Bits = RHS.bitWidth() * 2;
635 T Result;
636 if (!T::div(LHS, RHS, Bits, &Result)) {
637 S.Stk.push<T>(Result);
638 return true;
639 }
640 return false;
641}
642
643inline bool Divf(InterpState &S, CodePtr OpPC, llvm::RoundingMode RM) {
644 const Floating &RHS = S.Stk.pop<Floating>();
645 const Floating &LHS = S.Stk.pop<Floating>();
646
647 if (!CheckDivRem(S, OpPC, LHS, RHS))
648 return false;
649
651 auto Status = Floating::div(LHS, RHS, RM, &Result);
652 S.Stk.push<Floating>(Result);
653 return CheckFloatResult(S, OpPC, Result, Status);
654}
655
656//===----------------------------------------------------------------------===//
657// Inv
658//===----------------------------------------------------------------------===//
659
660inline bool Inv(InterpState &S, CodePtr OpPC) {
661 const auto &Val = S.Stk.pop<Boolean>();
662 S.Stk.push<Boolean>(!Val);
663 return true;
664}
665
666//===----------------------------------------------------------------------===//
667// Neg
668//===----------------------------------------------------------------------===//
669
670template <PrimType Name, class T = typename PrimConv<Name>::T>
671bool Neg(InterpState &S, CodePtr OpPC) {
672 const T &Value = S.Stk.pop<T>();
673 T Result;
674
675 if (!T::neg(Value, &Result)) {
676 S.Stk.push<T>(Result);
677 return true;
678 }
679
680 assert(isIntegralType(Name) &&
681 "don't expect other types to fail at constexpr negation");
682 S.Stk.push<T>(Result);
683
684 APSInt NegatedValue = -Value.toAPSInt(Value.bitWidth() + 1);
685 const Expr *E = S.Current->getExpr(OpPC);
686 QualType Type = E->getType();
687
688 if (S.checkingForUndefinedBehavior()) {
689 SmallString<32> Trunc;
690 NegatedValue.trunc(Result.bitWidth())
691 .toString(Trunc, 10, Result.isSigned(), /*formatAsCLiteral=*/false,
692 /*UpperCase=*/true, /*InsertSeparators=*/true);
693 auto Loc = E->getExprLoc();
694 S.report(Loc, diag::warn_integer_constant_overflow)
695 << Trunc << Type << E->getSourceRange();
696 return true;
697 }
698
699 S.CCEDiag(E, diag::note_constexpr_overflow) << NegatedValue << Type;
700 return S.noteUndefinedBehavior();
701}
702
703enum class PushVal : bool {
704 No,
705 Yes,
706};
707enum class IncDecOp {
708 Inc,
709 Dec,
710};
711
712template <typename T, IncDecOp Op, PushVal DoPush>
713bool IncDecHelper(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
714 assert(!Ptr.isDummy());
715
716 if constexpr (std::is_same_v<T, Boolean>) {
717 if (!S.getLangOpts().CPlusPlus14)
718 return Invalid(S, OpPC);
719 }
720
721 const T &Value = Ptr.deref<T>();
722 T Result;
723
724 if constexpr (DoPush == PushVal::Yes)
725 S.Stk.push<T>(Value);
726
727 if constexpr (Op == IncDecOp::Inc) {
728 if (!T::increment(Value, &Result)) {
729 Ptr.deref<T>() = Result;
730 return true;
731 }
732 } else {
733 if (!T::decrement(Value, &Result)) {
734 Ptr.deref<T>() = Result;
735 return true;
736 }
737 }
738
739 // Something went wrong with the previous operation. Compute the
740 // result with another bit of precision.
741 unsigned Bits = Value.bitWidth() + 1;
742 APSInt APResult;
743 if constexpr (Op == IncDecOp::Inc)
744 APResult = ++Value.toAPSInt(Bits);
745 else
746 APResult = --Value.toAPSInt(Bits);
747
748 // Report undefined behaviour, stopping if required.
749 const Expr *E = S.Current->getExpr(OpPC);
750 QualType Type = E->getType();
751 if (S.checkingForUndefinedBehavior()) {
752 SmallString<32> Trunc;
753 APResult.trunc(Result.bitWidth())
754 .toString(Trunc, 10, Result.isSigned(), /*formatAsCLiteral=*/false,
755 /*UpperCase=*/true, /*InsertSeparators=*/true);
756 auto Loc = E->getExprLoc();
757 S.report(Loc, diag::warn_integer_constant_overflow)
758 << Trunc << Type << E->getSourceRange();
759 return true;
760 }
761
762 S.CCEDiag(E, diag::note_constexpr_overflow) << APResult << Type;
763 return S.noteUndefinedBehavior();
764}
765
766/// 1) Pops a pointer from the stack
767/// 2) Load the value from the pointer
768/// 3) Writes the value increased by one back to the pointer
769/// 4) Pushes the original (pre-inc) value on the stack.
770template <PrimType Name, class T = typename PrimConv<Name>::T>
771bool Inc(InterpState &S, CodePtr OpPC) {
772 const Pointer &Ptr = S.Stk.pop<Pointer>();
773 if (!CheckLoad(S, OpPC, Ptr, AK_Increment))
774 return false;
775
776 return IncDecHelper<T, IncDecOp::Inc, PushVal::Yes>(S, OpPC, Ptr);
777}
778
779/// 1) Pops a pointer from the stack
780/// 2) Load the value from the pointer
781/// 3) Writes the value increased by one back to the pointer
782template <PrimType Name, class T = typename PrimConv<Name>::T>
783bool IncPop(InterpState &S, CodePtr OpPC) {
784 const Pointer &Ptr = S.Stk.pop<Pointer>();
785 if (!CheckLoad(S, OpPC, Ptr, AK_Increment))
786 return false;
787
788 return IncDecHelper<T, IncDecOp::Inc, PushVal::No>(S, OpPC, Ptr);
789}
790
791/// 1) Pops a pointer from the stack
792/// 2) Load the value from the pointer
793/// 3) Writes the value decreased by one back to the pointer
794/// 4) Pushes the original (pre-dec) value on the stack.
795template <PrimType Name, class T = typename PrimConv<Name>::T>
796bool Dec(InterpState &S, CodePtr OpPC) {
797 const Pointer &Ptr = S.Stk.pop<Pointer>();
798 if (!CheckLoad(S, OpPC, Ptr, AK_Decrement))
799 return false;
800
801 return IncDecHelper<T, IncDecOp::Dec, PushVal::Yes>(S, OpPC, Ptr);
802}
803
804/// 1) Pops a pointer from the stack
805/// 2) Load the value from the pointer
806/// 3) Writes the value decreased by one back to the pointer
807template <PrimType Name, class T = typename PrimConv<Name>::T>
808bool DecPop(InterpState &S, CodePtr OpPC) {
809 const Pointer &Ptr = S.Stk.pop<Pointer>();
810 if (!CheckLoad(S, OpPC, Ptr, AK_Decrement))
811 return false;
812
813 return IncDecHelper<T, IncDecOp::Dec, PushVal::No>(S, OpPC, Ptr);
814}
815
816template <IncDecOp Op, PushVal DoPush>
818 llvm::RoundingMode RM) {
819 Floating Value = Ptr.deref<Floating>();
821
822 if constexpr (DoPush == PushVal::Yes)
823 S.Stk.push<Floating>(Value);
824
825 llvm::APFloat::opStatus Status;
826 if constexpr (Op == IncDecOp::Inc)
827 Status = Floating::increment(Value, RM, &Result);
828 else
829 Status = Floating::decrement(Value, RM, &Result);
830
831 Ptr.deref<Floating>() = Result;
832
833 return CheckFloatResult(S, OpPC, Result, Status);
834}
835
836inline bool Incf(InterpState &S, CodePtr OpPC, llvm::RoundingMode RM) {
837 const Pointer &Ptr = S.Stk.pop<Pointer>();
838 if (!CheckLoad(S, OpPC, Ptr, AK_Increment))
839 return false;
840
841 return IncDecFloatHelper<IncDecOp::Inc, PushVal::Yes>(S, OpPC, Ptr, RM);
842}
843
844inline bool IncfPop(InterpState &S, CodePtr OpPC, llvm::RoundingMode RM) {
845 const Pointer &Ptr = S.Stk.pop<Pointer>();
846 if (!CheckLoad(S, OpPC, Ptr, AK_Increment))
847 return false;
848
849 return IncDecFloatHelper<IncDecOp::Inc, PushVal::No>(S, OpPC, Ptr, RM);
850}
851
852inline bool Decf(InterpState &S, CodePtr OpPC, llvm::RoundingMode RM) {
853 const Pointer &Ptr = S.Stk.pop<Pointer>();
854 if (!CheckLoad(S, OpPC, Ptr, AK_Decrement))
855 return false;
856
857 return IncDecFloatHelper<IncDecOp::Dec, PushVal::Yes>(S, OpPC, Ptr, RM);
858}
859
860inline bool DecfPop(InterpState &S, CodePtr OpPC, llvm::RoundingMode RM) {
861 const Pointer &Ptr = S.Stk.pop<Pointer>();
862 if (!CheckLoad(S, OpPC, Ptr, AK_Decrement))
863 return false;
864
865 return IncDecFloatHelper<IncDecOp::Dec, PushVal::No>(S, OpPC, Ptr, RM);
866}
867
868/// 1) Pops the value from the stack.
869/// 2) Pushes the bitwise complemented value on the stack (~V).
870template <PrimType Name, class T = typename PrimConv<Name>::T>
871bool Comp(InterpState &S, CodePtr OpPC) {
872 const T &Val = S.Stk.pop<T>();
873 T Result;
874 if (!T::comp(Val, &Result)) {
875 S.Stk.push<T>(Result);
876 return true;
877 }
878
879 return false;
880}
881
882//===----------------------------------------------------------------------===//
883// EQ, NE, GT, GE, LT, LE
884//===----------------------------------------------------------------------===//
885
886using CompareFn = llvm::function_ref<bool(ComparisonCategoryResult)>;
887
888template <typename T>
890 assert((!std::is_same_v<T, MemberPointer>) &&
891 "Non-equality comparisons on member pointer types should already be "
892 "rejected in Sema.");
893 using BoolT = PrimConv<PT_Bool>::T;
894 const T &RHS = S.Stk.pop<T>();
895 const T &LHS = S.Stk.pop<T>();
896 S.Stk.push<BoolT>(BoolT::from(Fn(LHS.compare(RHS))));
897 return true;
898}
899
900template <typename T>
902 return CmpHelper<T>(S, OpPC, Fn);
903}
904
905/// Function pointers cannot be compared in an ordered way.
906template <>
908 CompareFn Fn) {
909 const auto &RHS = S.Stk.pop<FunctionPointer>();
910 const auto &LHS = S.Stk.pop<FunctionPointer>();
911
912 const SourceInfo &Loc = S.Current->getSource(OpPC);
913 S.FFDiag(Loc, diag::note_constexpr_pointer_comparison_unspecified)
914 << LHS.toDiagnosticString(S.getCtx())
915 << RHS.toDiagnosticString(S.getCtx());
916 return false;
917}
918
919template <>
921 CompareFn Fn) {
922 const auto &RHS = S.Stk.pop<FunctionPointer>();
923 const auto &LHS = S.Stk.pop<FunctionPointer>();
924
925 // We cannot compare against weak declarations at compile time.
926 for (const auto &FP : {LHS, RHS}) {
927 if (FP.isWeak()) {
928 const SourceInfo &Loc = S.Current->getSource(OpPC);
929 S.FFDiag(Loc, diag::note_constexpr_pointer_weak_comparison)
930 << FP.toDiagnosticString(S.getCtx());
931 return false;
932 }
933 }
934
935 S.Stk.push<Boolean>(Boolean::from(Fn(LHS.compare(RHS))));
936 return true;
937}
938
939template <>
941 using BoolT = PrimConv<PT_Bool>::T;
942 const Pointer &RHS = S.Stk.pop<Pointer>();
943 const Pointer &LHS = S.Stk.pop<Pointer>();
944
945 if (!Pointer::hasSameBase(LHS, RHS)) {
946 const SourceInfo &Loc = S.Current->getSource(OpPC);
947 S.FFDiag(Loc, diag::note_constexpr_pointer_comparison_unspecified)
948 << LHS.toDiagnosticString(S.getCtx())
949 << RHS.toDiagnosticString(S.getCtx());
950 return false;
951 } else {
952 unsigned VL = LHS.getByteOffset();
953 unsigned VR = RHS.getByteOffset();
954 S.Stk.push<BoolT>(BoolT::from(Fn(Compare(VL, VR))));
955 return true;
956 }
957}
958
959template <>
961 using BoolT = PrimConv<PT_Bool>::T;
962 const Pointer &RHS = S.Stk.pop<Pointer>();
963 const Pointer &LHS = S.Stk.pop<Pointer>();
964
965 if (LHS.isZero() && RHS.isZero()) {
966 S.Stk.push<BoolT>(BoolT::from(Fn(ComparisonCategoryResult::Equal)));
967 return true;
968 }
969
970 // Reject comparisons to weak pointers.
971 for (const auto &P : {LHS, RHS}) {
972 if (P.isZero())
973 continue;
974 if (P.isWeak()) {
975 const SourceInfo &Loc = S.Current->getSource(OpPC);
976 S.FFDiag(Loc, diag::note_constexpr_pointer_weak_comparison)
977 << P.toDiagnosticString(S.getCtx());
978 return false;
979 }
980 }
981
982 if (!Pointer::hasSameBase(LHS, RHS)) {
983 if (LHS.isOnePastEnd() && !RHS.isOnePastEnd() && !RHS.isZero() &&
984 RHS.getOffset() == 0) {
985 const SourceInfo &Loc = S.Current->getSource(OpPC);
986 S.FFDiag(Loc, diag::note_constexpr_pointer_comparison_past_end)
987 << LHS.toDiagnosticString(S.getCtx());
988 return false;
989 } else if (RHS.isOnePastEnd() && !LHS.isOnePastEnd() && !LHS.isZero() &&
990 LHS.getOffset() == 0) {
991 const SourceInfo &Loc = S.Current->getSource(OpPC);
992 S.FFDiag(Loc, diag::note_constexpr_pointer_comparison_past_end)
993 << RHS.toDiagnosticString(S.getCtx());
994 return false;
995 }
996
997 S.Stk.push<BoolT>(BoolT::from(Fn(ComparisonCategoryResult::Unordered)));
998 return true;
999 } else {
1000 unsigned VL = LHS.getByteOffset();
1001 unsigned VR = RHS.getByteOffset();
1002
1003 // In our Pointer class, a pointer to an array and a pointer to the first
1004 // element in the same array are NOT equal. They have the same Base value,
1005 // but a different Offset. This is a pretty rare case, so we fix this here
1006 // by comparing pointers to the first elements.
1007 if (!LHS.isZero() && LHS.isArrayRoot())
1008 VL = LHS.atIndex(0).getByteOffset();
1009 if (!RHS.isZero() && RHS.isArrayRoot())
1010 VR = RHS.atIndex(0).getByteOffset();
1011
1012 S.Stk.push<BoolT>(BoolT::from(Fn(Compare(VL, VR))));
1013 return true;
1014 }
1015}
1016
1017template <>
1019 CompareFn Fn) {
1020 const auto &RHS = S.Stk.pop<MemberPointer>();
1021 const auto &LHS = S.Stk.pop<MemberPointer>();
1022
1023 // If either operand is a pointer to a weak function, the comparison is not
1024 // constant.
1025 for (const auto &MP : {LHS, RHS}) {
1026 if (const CXXMethodDecl *MD = MP.getMemberFunction(); MD && MD->isWeak()) {
1027 const SourceInfo &Loc = S.Current->getSource(OpPC);
1028 S.FFDiag(Loc, diag::note_constexpr_mem_pointer_weak_comparison) << MD;
1029 return false;
1030 }
1031 }
1032
1033 // C++11 [expr.eq]p2:
1034 // If both operands are null, they compare equal. Otherwise if only one is
1035 // null, they compare unequal.
1036 if (LHS.isZero() && RHS.isZero()) {
1038 return true;
1039 }
1040 if (LHS.isZero() || RHS.isZero()) {
1042 return true;
1043 }
1044
1045 // We cannot compare against virtual declarations at compile time.
1046 for (const auto &MP : {LHS, RHS}) {
1047 if (const CXXMethodDecl *MD = MP.getMemberFunction();
1048 MD && MD->isVirtual()) {
1049 const SourceInfo &Loc = S.Current->getSource(OpPC);
1050 S.CCEDiag(Loc, diag::note_constexpr_compare_virtual_mem_ptr) << MD;
1051 }
1052 }
1053
1054 S.Stk.push<Boolean>(Boolean::from(Fn(LHS.compare(RHS))));
1055 return true;
1056}
1057
1058template <PrimType Name, class T = typename PrimConv<Name>::T>
1059bool EQ(InterpState &S, CodePtr OpPC) {
1060 return CmpHelperEQ<T>(S, OpPC, [](ComparisonCategoryResult R) {
1062 });
1063}
1064
1065template <PrimType Name, class T = typename PrimConv<Name>::T>
1066bool CMP3(InterpState &S, CodePtr OpPC, const ComparisonCategoryInfo *CmpInfo) {
1067 const T &RHS = S.Stk.pop<T>();
1068 const T &LHS = S.Stk.pop<T>();
1069 const Pointer &P = S.Stk.peek<Pointer>();
1070
1071 ComparisonCategoryResult CmpResult = LHS.compare(RHS);
1072 if (CmpResult == ComparisonCategoryResult::Unordered) {
1073 // This should only happen with pointers.
1074 const SourceInfo &Loc = S.Current->getSource(OpPC);
1075 S.FFDiag(Loc, diag::note_constexpr_pointer_comparison_unspecified)
1076 << LHS.toDiagnosticString(S.getCtx())
1077 << RHS.toDiagnosticString(S.getCtx());
1078 return false;
1079 }
1080
1081 assert(CmpInfo);
1082 const auto *CmpValueInfo =
1083 CmpInfo->getValueInfo(CmpInfo->makeWeakResult(CmpResult));
1084 assert(CmpValueInfo);
1085 assert(CmpValueInfo->hasValidIntValue());
1086 return SetThreeWayComparisonField(S, OpPC, P, CmpValueInfo->getIntValue());
1087}
1088
1089template <PrimType Name, class T = typename PrimConv<Name>::T>
1090bool NE(InterpState &S, CodePtr OpPC) {
1091 return CmpHelperEQ<T>(S, OpPC, [](ComparisonCategoryResult R) {
1093 });
1094}
1095
1096template <PrimType Name, class T = typename PrimConv<Name>::T>
1097bool LT(InterpState &S, CodePtr OpPC) {
1098 return CmpHelper<T>(S, OpPC, [](ComparisonCategoryResult R) {
1100 });
1101}
1102
1103template <PrimType Name, class T = typename PrimConv<Name>::T>
1104bool LE(InterpState &S, CodePtr OpPC) {
1105 return CmpHelper<T>(S, OpPC, [](ComparisonCategoryResult R) {
1106 return R == ComparisonCategoryResult::Less ||
1108 });
1109}
1110
1111template <PrimType Name, class T = typename PrimConv<Name>::T>
1112bool GT(InterpState &S, CodePtr OpPC) {
1113 return CmpHelper<T>(S, OpPC, [](ComparisonCategoryResult R) {
1115 });
1116}
1117
1118template <PrimType Name, class T = typename PrimConv<Name>::T>
1119bool GE(InterpState &S, CodePtr OpPC) {
1120 return CmpHelper<T>(S, OpPC, [](ComparisonCategoryResult R) {
1123 });
1124}
1125
1126//===----------------------------------------------------------------------===//
1127// InRange
1128//===----------------------------------------------------------------------===//
1129
1130template <PrimType Name, class T = typename PrimConv<Name>::T>
1132 const T RHS = S.Stk.pop<T>();
1133 const T LHS = S.Stk.pop<T>();
1134 const T Value = S.Stk.pop<T>();
1135
1136 S.Stk.push<bool>(LHS <= Value && Value <= RHS);
1137 return true;
1138}
1139
1140//===----------------------------------------------------------------------===//
1141// Dup, Pop, Test
1142//===----------------------------------------------------------------------===//
1143
1144template <PrimType Name, class T = typename PrimConv<Name>::T>
1145bool Dup(InterpState &S, CodePtr OpPC) {
1146 S.Stk.push<T>(S.Stk.peek<T>());
1147 return true;
1148}
1149
1150template <PrimType Name, class T = typename PrimConv<Name>::T>
1151bool Pop(InterpState &S, CodePtr OpPC) {
1152 S.Stk.pop<T>();
1153 return true;
1154}
1155
1156/// [Value1, Value2] -> [Value2, Value1]
1157template <PrimType TopName, PrimType BottomName>
1158bool Flip(InterpState &S, CodePtr OpPC) {
1159 using TopT = typename PrimConv<TopName>::T;
1160 using BottomT = typename PrimConv<BottomName>::T;
1161
1162 const auto &Top = S.Stk.pop<TopT>();
1163 const auto &Bottom = S.Stk.pop<BottomT>();
1164
1165 S.Stk.push<TopT>(Top);
1166 S.Stk.push<BottomT>(Bottom);
1167
1168 return true;
1169}
1170
1171//===----------------------------------------------------------------------===//
1172// Const
1173//===----------------------------------------------------------------------===//
1174
1175template <PrimType Name, class T = typename PrimConv<Name>::T>
1176bool Const(InterpState &S, CodePtr OpPC, const T &Arg) {
1177 S.Stk.push<T>(Arg);
1178 return true;
1179}
1180
1181//===----------------------------------------------------------------------===//
1182// Get/Set Local/Param/Global/This
1183//===----------------------------------------------------------------------===//
1184
1185template <PrimType Name, class T = typename PrimConv<Name>::T>
1186bool GetLocal(InterpState &S, CodePtr OpPC, uint32_t I) {
1187 const Pointer &Ptr = S.Current->getLocalPointer(I);
1188 if (!CheckLoad(S, OpPC, Ptr))
1189 return false;
1190 S.Stk.push<T>(Ptr.deref<T>());
1191 return true;
1192}
1193
1194/// 1) Pops the value from the stack.
1195/// 2) Writes the value to the local variable with the
1196/// given offset.
1197template <PrimType Name, class T = typename PrimConv<Name>::T>
1198bool SetLocal(InterpState &S, CodePtr OpPC, uint32_t I) {
1199 S.Current->setLocal<T>(I, S.Stk.pop<T>());
1200 return true;
1201}
1202
1203template <PrimType Name, class T = typename PrimConv<Name>::T>
1204bool GetParam(InterpState &S, CodePtr OpPC, uint32_t I) {
1205 if (S.checkingPotentialConstantExpression()) {
1206 return false;
1207 }
1208 S.Stk.push<T>(S.Current->getParam<T>(I));
1209 return true;
1210}
1211
1212template <PrimType Name, class T = typename PrimConv<Name>::T>
1213bool SetParam(InterpState &S, CodePtr OpPC, uint32_t I) {
1214 S.Current->setParam<T>(I, S.Stk.pop<T>());
1215 return true;
1216}
1217
1218/// 1) Peeks a pointer on the stack
1219/// 2) Pushes the value of the pointer's field on the stack
1220template <PrimType Name, class T = typename PrimConv<Name>::T>
1221bool GetField(InterpState &S, CodePtr OpPC, uint32_t I) {
1222 const Pointer &Obj = S.Stk.peek<Pointer>();
1223 if (!CheckNull(S, OpPC, Obj, CSK_Field))
1224 return false;
1225 if (!CheckRange(S, OpPC, Obj, CSK_Field))
1226 return false;
1227 const Pointer &Field = Obj.atField(I);
1228 if (!CheckLoad(S, OpPC, Field))
1229 return false;
1230 S.Stk.push<T>(Field.deref<T>());
1231 return true;
1232}
1233
1234template <PrimType Name, class T = typename PrimConv<Name>::T>
1235bool SetField(InterpState &S, CodePtr OpPC, uint32_t I) {
1236 const T &Value = S.Stk.pop<T>();
1237 const Pointer &Obj = S.Stk.peek<Pointer>();
1238 if (!CheckNull(S, OpPC, Obj, CSK_Field))
1239 return false;
1240 if (!CheckRange(S, OpPC, Obj, CSK_Field))
1241 return false;
1242 const Pointer &Field = Obj.atField(I);
1243 if (!CheckStore(S, OpPC, Field))
1244 return false;
1245 Field.initialize();
1246 Field.deref<T>() = Value;
1247 return true;
1248}
1249
1250/// 1) Pops a pointer from the stack
1251/// 2) Pushes the value of the pointer's field on the stack
1252template <PrimType Name, class T = typename PrimConv<Name>::T>
1253bool GetFieldPop(InterpState &S, CodePtr OpPC, uint32_t I) {
1254 const Pointer &Obj = S.Stk.pop<Pointer>();
1255 if (!CheckNull(S, OpPC, Obj, CSK_Field))
1256 return false;
1257 if (!CheckRange(S, OpPC, Obj, CSK_Field))
1258 return false;
1259 const Pointer &Field = Obj.atField(I);
1260 if (!CheckLoad(S, OpPC, Field))
1261 return false;
1262 S.Stk.push<T>(Field.deref<T>());
1263 return true;
1264}
1265
1266template <PrimType Name, class T = typename PrimConv<Name>::T>
1267bool GetThisField(InterpState &S, CodePtr OpPC, uint32_t I) {
1268 if (S.checkingPotentialConstantExpression())
1269 return false;
1270 const Pointer &This = S.Current->getThis();
1271 if (!CheckThis(S, OpPC, This))
1272 return false;
1273 const Pointer &Field = This.atField(I);
1274 if (!CheckLoad(S, OpPC, Field))
1275 return false;
1276 S.Stk.push<T>(Field.deref<T>());
1277 return true;
1278}
1279
1280template <PrimType Name, class T = typename PrimConv<Name>::T>
1281bool SetThisField(InterpState &S, CodePtr OpPC, uint32_t I) {
1282 if (S.checkingPotentialConstantExpression())
1283 return false;
1284 const T &Value = S.Stk.pop<T>();
1285 const Pointer &This = S.Current->getThis();
1286 if (!CheckThis(S, OpPC, This))
1287 return false;
1288 const Pointer &Field = This.atField(I);
1289 if (!CheckStore(S, OpPC, Field))
1290 return false;
1291 Field.deref<T>() = Value;
1292 return true;
1293}
1294
1295template <PrimType Name, class T = typename PrimConv<Name>::T>
1296bool GetGlobal(InterpState &S, CodePtr OpPC, uint32_t I) {
1297 const Pointer &Ptr = S.P.getPtrGlobal(I);
1298 if (!CheckConstant(S, OpPC, Ptr.getFieldDesc()))
1299 return false;
1300 if (Ptr.isExtern())
1301 return false;
1302
1303 // If a global variable is uninitialized, that means the initializer we've
1304 // compiled for it wasn't a constant expression. Diagnose that.
1305 if (!CheckGlobalInitialized(S, OpPC, Ptr))
1306 return false;
1307
1308 S.Stk.push<T>(Ptr.deref<T>());
1309 return true;
1310}
1311
1312/// Same as GetGlobal, but without the checks.
1313template <PrimType Name, class T = typename PrimConv<Name>::T>
1314bool GetGlobalUnchecked(InterpState &S, CodePtr OpPC, uint32_t I) {
1315 const Pointer &Ptr = S.P.getPtrGlobal(I);
1316 if (!Ptr.isInitialized())
1317 return false;
1318 S.Stk.push<T>(Ptr.deref<T>());
1319 return true;
1320}
1321
1322template <PrimType Name, class T = typename PrimConv<Name>::T>
1323bool SetGlobal(InterpState &S, CodePtr OpPC, uint32_t I) {
1324 // TODO: emit warning.
1325 return false;
1326}
1327
1328template <PrimType Name, class T = typename PrimConv<Name>::T>
1329bool InitGlobal(InterpState &S, CodePtr OpPC, uint32_t I) {
1330 const Pointer &P = S.P.getGlobal(I);
1331 P.deref<T>() = S.Stk.pop<T>();
1332 P.initialize();
1333 return true;
1334}
1335
1336/// 1) Converts the value on top of the stack to an APValue
1337/// 2) Sets that APValue on \Temp
1338/// 3) Initializes global with index \I with that
1339template <PrimType Name, class T = typename PrimConv<Name>::T>
1340bool InitGlobalTemp(InterpState &S, CodePtr OpPC, uint32_t I,
1341 const LifetimeExtendedTemporaryDecl *Temp) {
1342 const Pointer &Ptr = S.P.getGlobal(I);
1343
1344 const T Value = S.Stk.peek<T>();
1345 APValue APV = Value.toAPValue(S.getCtx());
1346 APValue *Cached = Temp->getOrCreateValue(true);
1347 *Cached = APV;
1348
1349 assert(Ptr.getDeclDesc()->asExpr());
1350
1351 S.SeenGlobalTemporaries.push_back(
1352 std::make_pair(Ptr.getDeclDesc()->asExpr(), Temp));
1353
1354 Ptr.deref<T>() = S.Stk.pop<T>();
1355 Ptr.initialize();
1356 return true;
1357}
1358
1359/// 1) Converts the value on top of the stack to an APValue
1360/// 2) Sets that APValue on \Temp
1361/// 3) Initialized global with index \I with that
1363 const LifetimeExtendedTemporaryDecl *Temp) {
1364 assert(Temp);
1365 const Pointer &P = S.Stk.peek<Pointer>();
1366 APValue *Cached = Temp->getOrCreateValue(true);
1367
1368 S.SeenGlobalTemporaries.push_back(
1369 std::make_pair(P.getDeclDesc()->asExpr(), Temp));
1370
1371 if (std::optional<APValue> APV =
1372 P.toRValue(S.getCtx(), Temp->getTemporaryExpr()->getType())) {
1373 *Cached = *APV;
1374 return true;
1375 }
1376
1377 return false;
1378}
1379
1380template <PrimType Name, class T = typename PrimConv<Name>::T>
1381bool InitThisField(InterpState &S, CodePtr OpPC, uint32_t I) {
1382 if (S.checkingPotentialConstantExpression())
1383 return false;
1384 const Pointer &This = S.Current->getThis();
1385 if (!CheckThis(S, OpPC, This))
1386 return false;
1387 const Pointer &Field = This.atField(I);
1388 Field.deref<T>() = S.Stk.pop<T>();
1389 Field.activate();
1390 Field.initialize();
1391 return true;
1392}
1393
1394// FIXME: The Field pointer here is too much IMO and we could instead just
1395// pass an Offset + BitWidth pair.
1396template <PrimType Name, class T = typename PrimConv<Name>::T>
1397bool InitThisBitField(InterpState &S, CodePtr OpPC, const Record::Field *F,
1398 uint32_t FieldOffset) {
1399 assert(F->isBitField());
1400 if (S.checkingPotentialConstantExpression())
1401 return false;
1402 const Pointer &This = S.Current->getThis();
1403 if (!CheckThis(S, OpPC, This))
1404 return false;
1405 const Pointer &Field = This.atField(FieldOffset);
1406 const auto &Value = S.Stk.pop<T>();
1407 Field.deref<T>() = Value.truncate(F->Decl->getBitWidthValue(S.getCtx()));
1408 Field.initialize();
1409 return true;
1410}
1411
1412/// 1) Pops the value from the stack
1413/// 2) Peeks a pointer from the stack
1414/// 3) Pushes the value to field I of the pointer on the stack
1415template <PrimType Name, class T = typename PrimConv<Name>::T>
1416bool InitField(InterpState &S, CodePtr OpPC, uint32_t I) {
1417 const T &Value = S.Stk.pop<T>();
1418 const Pointer &Field = S.Stk.peek<Pointer>().atField(I);
1419 Field.deref<T>() = Value;
1420 Field.activate();
1421 Field.initialize();
1422 return true;
1423}
1424
1425template <PrimType Name, class T = typename PrimConv<Name>::T>
1426bool InitBitField(InterpState &S, CodePtr OpPC, const Record::Field *F) {
1427 assert(F->isBitField());
1428 const T &Value = S.Stk.pop<T>();
1429 const Pointer &Field = S.Stk.peek<Pointer>().atField(F->Offset);
1430 Field.deref<T>() = Value.truncate(F->Decl->getBitWidthValue(S.getCtx()));
1431 Field.activate();
1432 Field.initialize();
1433 return true;
1434}
1435
1436//===----------------------------------------------------------------------===//
1437// GetPtr Local/Param/Global/Field/This
1438//===----------------------------------------------------------------------===//
1439
1440inline bool GetPtrLocal(InterpState &S, CodePtr OpPC, uint32_t I) {
1441 S.Stk.push<Pointer>(S.Current->getLocalPointer(I));
1442 return true;
1443}
1444
1445inline bool GetPtrParam(InterpState &S, CodePtr OpPC, uint32_t I) {
1446 if (S.checkingPotentialConstantExpression()) {
1447 return false;
1448 }
1449 S.Stk.push<Pointer>(S.Current->getParamPointer(I));
1450 return true;
1451}
1452
1453inline bool GetPtrGlobal(InterpState &S, CodePtr OpPC, uint32_t I) {
1454 S.Stk.push<Pointer>(S.P.getPtrGlobal(I));
1455 return true;
1456}
1457
1458/// 1) Peeks a Pointer
1459/// 2) Pushes Pointer.atField(Off) on the stack
1460inline bool GetPtrField(InterpState &S, CodePtr OpPC, uint32_t Off) {
1461 const Pointer &Ptr = S.Stk.peek<Pointer>();
1462
1463 if (S.getLangOpts().CPlusPlus && S.inConstantContext() &&
1464 !CheckNull(S, OpPC, Ptr, CSK_Field))
1465 return false;
1466
1467 if (!CheckExtern(S, OpPC, Ptr))
1468 return false;
1469 if (!CheckRange(S, OpPC, Ptr, CSK_Field))
1470 return false;
1471 if (!CheckArray(S, OpPC, Ptr))
1472 return false;
1473 if (!CheckSubobject(S, OpPC, Ptr, CSK_Field))
1474 return false;
1475
1476 if (Ptr.isBlockPointer() && Off > Ptr.block()->getSize())
1477 return false;
1478
1479 if (Ptr.isIntegralPointer()) {
1480 S.Stk.push<Pointer>(Ptr.asIntPointer().atOffset(S.getCtx(), Off));
1481 return true;
1482 }
1483
1484 S.Stk.push<Pointer>(Ptr.atField(Off));
1485 return true;
1486}
1487
1488inline bool GetPtrFieldPop(InterpState &S, CodePtr OpPC, uint32_t Off) {
1489 const Pointer &Ptr = S.Stk.pop<Pointer>();
1490
1491 if (S.getLangOpts().CPlusPlus && S.inConstantContext() &&
1492 !CheckNull(S, OpPC, Ptr, CSK_Field))
1493 return false;
1494
1495 if (!CheckExtern(S, OpPC, Ptr))
1496 return false;
1497 if (!CheckRange(S, OpPC, Ptr, CSK_Field))
1498 return false;
1499 if (!CheckArray(S, OpPC, Ptr))
1500 return false;
1501 if (!CheckSubobject(S, OpPC, Ptr, CSK_Field))
1502 return false;
1503
1504 if (Ptr.isBlockPointer() && Off > Ptr.block()->getSize())
1505 return false;
1506
1507 if (Ptr.isIntegralPointer()) {
1508 S.Stk.push<Pointer>(Ptr.asIntPointer().atOffset(S.getCtx(), Off));
1509 return true;
1510 }
1511
1512 S.Stk.push<Pointer>(Ptr.atField(Off));
1513 return true;
1514}
1515
1516inline bool GetPtrThisField(InterpState &S, CodePtr OpPC, uint32_t Off) {
1517 if (S.checkingPotentialConstantExpression())
1518 return false;
1519 const Pointer &This = S.Current->getThis();
1520 if (!CheckThis(S, OpPC, This))
1521 return false;
1522 S.Stk.push<Pointer>(This.atField(Off));
1523 return true;
1524}
1525
1526inline bool GetPtrActiveField(InterpState &S, CodePtr OpPC, uint32_t Off) {
1527 const Pointer &Ptr = S.Stk.pop<Pointer>();
1528 if (!CheckNull(S, OpPC, Ptr, CSK_Field))
1529 return false;
1530 if (!CheckRange(S, OpPC, Ptr, CSK_Field))
1531 return false;
1532 Pointer Field = Ptr.atField(Off);
1533 Ptr.deactivate();
1534 Field.activate();
1535 S.Stk.push<Pointer>(std::move(Field));
1536 return true;
1537}
1538
1539inline bool GetPtrActiveThisField(InterpState &S, CodePtr OpPC, uint32_t Off) {
1540 if (S.checkingPotentialConstantExpression())
1541 return false;
1542 const Pointer &This = S.Current->getThis();
1543 if (!CheckThis(S, OpPC, This))
1544 return false;
1545 Pointer Field = This.atField(Off);
1546 This.deactivate();
1547 Field.activate();
1548 S.Stk.push<Pointer>(std::move(Field));
1549 return true;
1550}
1551
1552inline bool GetPtrDerivedPop(InterpState &S, CodePtr OpPC, uint32_t Off) {
1553 const Pointer &Ptr = S.Stk.pop<Pointer>();
1554 if (!CheckNull(S, OpPC, Ptr, CSK_Derived))
1555 return false;
1556 if (!CheckSubobject(S, OpPC, Ptr, CSK_Derived))
1557 return false;
1558 if (!CheckDowncast(S, OpPC, Ptr, Off))
1559 return false;
1560
1561 S.Stk.push<Pointer>(Ptr.atFieldSub(Off));
1562 return true;
1563}
1564
1565inline bool GetPtrBase(InterpState &S, CodePtr OpPC, uint32_t Off) {
1566 const Pointer &Ptr = S.Stk.peek<Pointer>();
1567 if (!CheckNull(S, OpPC, Ptr, CSK_Base))
1568 return false;
1569 if (!CheckSubobject(S, OpPC, Ptr, CSK_Base))
1570 return false;
1571 const Pointer &Result = Ptr.atField(Off);
1572 if (Result.isPastEnd() || !Result.isBaseClass())
1573 return false;
1574 S.Stk.push<Pointer>(Result);
1575 return true;
1576}
1577
1578inline bool GetPtrBasePop(InterpState &S, CodePtr OpPC, uint32_t Off) {
1579 const Pointer &Ptr = S.Stk.pop<Pointer>();
1580 if (!CheckNull(S, OpPC, Ptr, CSK_Base))
1581 return false;
1582 if (!CheckSubobject(S, OpPC, Ptr, CSK_Base))
1583 return false;
1584 const Pointer &Result = Ptr.atField(Off);
1585 if (Result.isPastEnd() || !Result.isBaseClass())
1586 return false;
1587 S.Stk.push<Pointer>(Result);
1588 return true;
1589}
1590
1591inline bool GetMemberPtrBasePop(InterpState &S, CodePtr OpPC, int32_t Off) {
1592 const auto &Ptr = S.Stk.pop<MemberPointer>();
1593 S.Stk.push<MemberPointer>(Ptr.atInstanceBase(Off));
1594 return true;
1595}
1596
1597inline bool GetPtrThisBase(InterpState &S, CodePtr OpPC, uint32_t Off) {
1598 if (S.checkingPotentialConstantExpression())
1599 return false;
1600 const Pointer &This = S.Current->getThis();
1601 if (!CheckThis(S, OpPC, This))
1602 return false;
1603 S.Stk.push<Pointer>(This.atField(Off));
1604 return true;
1605}
1606
1607inline bool FinishInitPop(InterpState &S, CodePtr OpPC) {
1608 const Pointer &Ptr = S.Stk.pop<Pointer>();
1609 if (Ptr.canBeInitialized()) {
1610 Ptr.initialize();
1611 Ptr.activate();
1612 }
1613 return true;
1614}
1615
1616inline bool FinishInit(InterpState &S, CodePtr OpPC) {
1617 const Pointer &Ptr = S.Stk.peek<Pointer>();
1618 if (Ptr.canBeInitialized()) {
1619 Ptr.initialize();
1620 Ptr.activate();
1621 }
1622 return true;
1623}
1624
1625inline bool Dump(InterpState &S, CodePtr OpPC) {
1626 S.Stk.dump();
1627 return true;
1628}
1629
1630inline bool VirtBaseHelper(InterpState &S, CodePtr OpPC, const RecordDecl *Decl,
1631 const Pointer &Ptr) {
1632 Pointer Base = Ptr;
1633 while (Base.isBaseClass())
1634 Base = Base.getBase();
1635
1636 const Record::Base *VirtBase = Base.getRecord()->getVirtualBase(Decl);
1637 S.Stk.push<Pointer>(Base.atField(VirtBase->Offset));
1638 return true;
1639}
1640
1642 const RecordDecl *D) {
1643 assert(D);
1644 const Pointer &Ptr = S.Stk.pop<Pointer>();
1645 if (!CheckNull(S, OpPC, Ptr, CSK_Base))
1646 return false;
1647 return VirtBaseHelper(S, OpPC, D, Ptr);
1648}
1649
1651 const RecordDecl *D) {
1652 assert(D);
1653 if (S.checkingPotentialConstantExpression())
1654 return false;
1655 const Pointer &This = S.Current->getThis();
1656 if (!CheckThis(S, OpPC, This))
1657 return false;
1658 return VirtBaseHelper(S, OpPC, D, S.Current->getThis());
1659}
1660
1661//===----------------------------------------------------------------------===//
1662// Load, Store, Init
1663//===----------------------------------------------------------------------===//
1664
1665template <PrimType Name, class T = typename PrimConv<Name>::T>
1666bool Load(InterpState &S, CodePtr OpPC) {
1667 const Pointer &Ptr = S.Stk.peek<Pointer>();
1668 if (!CheckLoad(S, OpPC, Ptr))
1669 return false;
1670 if (!Ptr.isBlockPointer())
1671 return false;
1672 S.Stk.push<T>(Ptr.deref<T>());
1673 return true;
1674}
1675
1676template <PrimType Name, class T = typename PrimConv<Name>::T>
1678 const Pointer &Ptr = S.Stk.pop<Pointer>();
1679 if (!CheckLoad(S, OpPC, Ptr))
1680 return false;
1681 if (!Ptr.isBlockPointer())
1682 return false;
1683 S.Stk.push<T>(Ptr.deref<T>());
1684 return true;
1685}
1686
1687template <PrimType Name, class T = typename PrimConv<Name>::T>
1688bool Store(InterpState &S, CodePtr OpPC) {
1689 const T &Value = S.Stk.pop<T>();
1690 const Pointer &Ptr = S.Stk.peek<Pointer>();
1691 if (!CheckStore(S, OpPC, Ptr))
1692 return false;
1693 if (Ptr.canBeInitialized()) {
1694 Ptr.initialize();
1695 Ptr.activate();
1696 }
1697 Ptr.deref<T>() = Value;
1698 return true;
1699}
1700
1701template <PrimType Name, class T = typename PrimConv<Name>::T>
1703 const T &Value = S.Stk.pop<T>();
1704 const Pointer &Ptr = S.Stk.pop<Pointer>();
1705 if (!CheckStore(S, OpPC, Ptr))
1706 return false;
1707 if (Ptr.canBeInitialized()) {
1708 Ptr.initialize();
1709 Ptr.activate();
1710 }
1711 Ptr.deref<T>() = Value;
1712 return true;
1713}
1714
1715template <PrimType Name, class T = typename PrimConv<Name>::T>
1717 const T &Value = S.Stk.pop<T>();
1718 const Pointer &Ptr = S.Stk.peek<Pointer>();
1719 if (!CheckStore(S, OpPC, Ptr))
1720 return false;
1721 if (Ptr.canBeInitialized())
1722 Ptr.initialize();
1723 if (const auto *FD = Ptr.getField())
1724 Ptr.deref<T>() = Value.truncate(FD->getBitWidthValue(S.getCtx()));
1725 else
1726 Ptr.deref<T>() = Value;
1727 return true;
1728}
1729
1730template <PrimType Name, class T = typename PrimConv<Name>::T>
1732 const T &Value = S.Stk.pop<T>();
1733 const Pointer &Ptr = S.Stk.pop<Pointer>();
1734 if (!CheckStore(S, OpPC, Ptr))
1735 return false;
1736 if (Ptr.canBeInitialized())
1737 Ptr.initialize();
1738 if (const auto *FD = Ptr.getField())
1739 Ptr.deref<T>() = Value.truncate(FD->getBitWidthValue(S.getCtx()));
1740 else
1741 Ptr.deref<T>() = Value;
1742 return true;
1743}
1744
1745template <PrimType Name, class T = typename PrimConv<Name>::T>
1746bool Init(InterpState &S, CodePtr OpPC) {
1747 const T &Value = S.Stk.pop<T>();
1748 const Pointer &Ptr = S.Stk.peek<Pointer>();
1749 if (!CheckInit(S, OpPC, Ptr)) {
1750 assert(false);
1751 return false;
1752 }
1753 Ptr.initialize();
1754 new (&Ptr.deref<T>()) T(Value);
1755 return true;
1756}
1757
1758template <PrimType Name, class T = typename PrimConv<Name>::T>
1760 const T &Value = S.Stk.pop<T>();
1761 const Pointer &Ptr = S.Stk.pop<Pointer>();
1762 if (!CheckInit(S, OpPC, Ptr))
1763 return false;
1764 Ptr.initialize();
1765 new (&Ptr.deref<T>()) T(Value);
1766 return true;
1767}
1768
1769/// 1) Pops the value from the stack
1770/// 2) Peeks a pointer and gets its index \Idx
1771/// 3) Sets the value on the pointer, leaving the pointer on the stack.
1772template <PrimType Name, class T = typename PrimConv<Name>::T>
1773bool InitElem(InterpState &S, CodePtr OpPC, uint32_t Idx) {
1774 const T &Value = S.Stk.pop<T>();
1775 const Pointer &Ptr = S.Stk.peek<Pointer>().atIndex(Idx);
1776 if (Ptr.isUnknownSizeArray())
1777 return false;
1778 if (!CheckInit(S, OpPC, Ptr))
1779 return false;
1780 Ptr.initialize();
1781 new (&Ptr.deref<T>()) T(Value);
1782 return true;
1783}
1784
1785/// The same as InitElem, but pops the pointer as well.
1786template <PrimType Name, class T = typename PrimConv<Name>::T>
1787bool InitElemPop(InterpState &S, CodePtr OpPC, uint32_t Idx) {
1788 const T &Value = S.Stk.pop<T>();
1789 const Pointer &Ptr = S.Stk.pop<Pointer>().atIndex(Idx);
1790 if (Ptr.isUnknownSizeArray())
1791 return false;
1792 if (!CheckInit(S, OpPC, Ptr))
1793 return false;
1794 Ptr.initialize();
1795 new (&Ptr.deref<T>()) T(Value);
1796 return true;
1797}
1798
1799inline bool Memcpy(InterpState &S, CodePtr OpPC) {
1800 const Pointer &Src = S.Stk.pop<Pointer>();
1801 Pointer &Dest = S.Stk.peek<Pointer>();
1802
1803 if (!CheckLoad(S, OpPC, Src))
1804 return false;
1805
1806 return DoMemcpy(S, OpPC, Src, Dest);
1807}
1808
1809inline bool ToMemberPtr(InterpState &S, CodePtr OpPC) {
1810 const auto &Member = S.Stk.pop<MemberPointer>();
1811 const auto &Base = S.Stk.pop<Pointer>();
1812
1813 S.Stk.push<MemberPointer>(Member.takeInstance(Base));
1814 return true;
1815}
1816
1817inline bool CastMemberPtrPtr(InterpState &S, CodePtr OpPC) {
1818 const auto &MP = S.Stk.pop<MemberPointer>();
1819
1820 if (std::optional<Pointer> Ptr = MP.toPointer(S.Ctx)) {
1821 S.Stk.push<Pointer>(*Ptr);
1822 return true;
1823 }
1824 return false;
1825}
1826
1827//===----------------------------------------------------------------------===//
1828// AddOffset, SubOffset
1829//===----------------------------------------------------------------------===//
1830
1831template <class T, ArithOp Op>
1832bool OffsetHelper(InterpState &S, CodePtr OpPC, const T &Offset,
1833 const Pointer &Ptr) {
1834 // A zero offset does not change the pointer.
1835 if (Offset.isZero()) {
1836 S.Stk.push<Pointer>(Ptr);
1837 return true;
1838 }
1839
1840 if (!CheckNull(S, OpPC, Ptr, CSK_ArrayIndex)) {
1841 // The CheckNull will have emitted a note already, but we only
1842 // abort in C++, since this is fine in C.
1843 if (S.getLangOpts().CPlusPlus)
1844 return false;
1845 }
1846
1847 // Arrays of unknown bounds cannot have pointers into them.
1848 if (!CheckArray(S, OpPC, Ptr))
1849 return false;
1850
1851 // This is much simpler for integral pointers, so handle them first.
1852 if (Ptr.isIntegralPointer()) {
1853 uint64_t V = Ptr.getIntegerRepresentation();
1854 uint64_t O = static_cast<uint64_t>(Offset) * Ptr.elemSize();
1855 if constexpr (Op == ArithOp::Add)
1856 S.Stk.push<Pointer>(V + O, Ptr.asIntPointer().Desc);
1857 else
1858 S.Stk.push<Pointer>(V - O, Ptr.asIntPointer().Desc);
1859 return true;
1860 }
1861
1862 uint64_t MaxIndex = static_cast<uint64_t>(Ptr.getNumElems());
1863 uint64_t Index;
1864 if (Ptr.isOnePastEnd())
1865 Index = MaxIndex;
1866 else
1867 Index = Ptr.getIndex();
1868
1869 bool Invalid = false;
1870 // Helper to report an invalid offset, computed as APSInt.
1871 auto DiagInvalidOffset = [&]() -> void {
1872 const unsigned Bits = Offset.bitWidth();
1873 APSInt APOffset(Offset.toAPSInt().extend(Bits + 2), /*IsUnsigend=*/false);
1874 APSInt APIndex(APInt(Bits + 2, Index, /*IsSigned=*/true),
1875 /*IsUnsigned=*/false);
1876 APSInt NewIndex =
1877 (Op == ArithOp::Add) ? (APIndex + APOffset) : (APIndex - APOffset);
1878 S.CCEDiag(S.Current->getSource(OpPC), diag::note_constexpr_array_index)
1879 << NewIndex << /*array*/ static_cast<int>(!Ptr.inArray()) << MaxIndex;
1880 Invalid = true;
1881 };
1882
1883 if (Ptr.isBlockPointer()) {
1884 uint64_t IOffset = static_cast<uint64_t>(Offset);
1885 uint64_t MaxOffset = MaxIndex - Index;
1886
1887 if constexpr (Op == ArithOp::Add) {
1888 // If the new offset would be negative, bail out.
1889 if (Offset.isNegative() && (Offset.isMin() || -IOffset > Index))
1890 DiagInvalidOffset();
1891
1892 // If the new offset would be out of bounds, bail out.
1893 if (Offset.isPositive() && IOffset > MaxOffset)
1894 DiagInvalidOffset();
1895 } else {
1896 // If the new offset would be negative, bail out.
1897 if (Offset.isPositive() && Index < IOffset)
1898 DiagInvalidOffset();
1899
1900 // If the new offset would be out of bounds, bail out.
1901 if (Offset.isNegative() && (Offset.isMin() || -IOffset > MaxOffset))
1902 DiagInvalidOffset();
1903 }
1904 }
1905
1906 if (Invalid && S.getLangOpts().CPlusPlus)
1907 return false;
1908
1909 // Offset is valid - compute it on unsigned.
1910 int64_t WideIndex = static_cast<int64_t>(Index);
1911 int64_t WideOffset = static_cast<int64_t>(Offset);
1912 int64_t Result;
1913 if constexpr (Op == ArithOp::Add)
1914 Result = WideIndex + WideOffset;
1915 else
1916 Result = WideIndex - WideOffset;
1917
1918 // When the pointer is one-past-end, going back to index 0 is the only
1919 // useful thing we can do. Any other index has been diagnosed before and
1920 // we don't get here.
1921 if (Result == 0 && Ptr.isOnePastEnd()) {
1922 S.Stk.push<Pointer>(Ptr.asBlockPointer().Pointee,
1923 Ptr.asBlockPointer().Base);
1924 return true;
1925 }
1926
1927 S.Stk.push<Pointer>(Ptr.atIndex(static_cast<uint64_t>(Result)));
1928 return true;
1929}
1930
1931template <PrimType Name, class T = typename PrimConv<Name>::T>
1933 const T &Offset = S.Stk.pop<T>();
1934 const Pointer &Ptr = S.Stk.pop<Pointer>();
1935 return OffsetHelper<T, ArithOp::Add>(S, OpPC, Offset, Ptr);
1936}
1937
1938template <PrimType Name, class T = typename PrimConv<Name>::T>
1940 const T &Offset = S.Stk.pop<T>();
1941 const Pointer &Ptr = S.Stk.pop<Pointer>();
1942 return OffsetHelper<T, ArithOp::Sub>(S, OpPC, Offset, Ptr);
1943}
1944
1945template <ArithOp Op>
1946static inline bool IncDecPtrHelper(InterpState &S, CodePtr OpPC,
1947 const Pointer &Ptr) {
1948 if (Ptr.isDummy())
1949 return false;
1950
1951 using OneT = Integral<8, false>;
1952
1953 const Pointer &P = Ptr.deref<Pointer>();
1954 if (!CheckNull(S, OpPC, P, CSK_ArrayIndex))
1955 return false;
1956
1957 // Get the current value on the stack.
1958 S.Stk.push<Pointer>(P);
1959
1960 // Now the current Ptr again and a constant 1.
1961 OneT One = OneT::from(1);
1962 if (!OffsetHelper<OneT, Op>(S, OpPC, One, P))
1963 return false;
1964
1965 // Store the new value.
1966 Ptr.deref<Pointer>() = S.Stk.pop<Pointer>();
1967 return true;
1968}
1969
1970static inline bool IncPtr(InterpState &S, CodePtr OpPC) {
1971 const Pointer &Ptr = S.Stk.pop<Pointer>();
1972
1973 if (!CheckInitialized(S, OpPC, Ptr, AK_Increment))
1974 return false;
1975
1976 return IncDecPtrHelper<ArithOp::Add>(S, OpPC, Ptr);
1977}
1978
1979static inline bool DecPtr(InterpState &S, CodePtr OpPC) {
1980 const Pointer &Ptr = S.Stk.pop<Pointer>();
1981
1982 if (!CheckInitialized(S, OpPC, Ptr, AK_Decrement))
1983 return false;
1984
1985 return IncDecPtrHelper<ArithOp::Sub>(S, OpPC, Ptr);
1986}
1987
1988/// 1) Pops a Pointer from the stack.
1989/// 2) Pops another Pointer from the stack.
1990/// 3) Pushes the different of the indices of the two pointers on the stack.
1991template <PrimType Name, class T = typename PrimConv<Name>::T>
1992inline bool SubPtr(InterpState &S, CodePtr OpPC) {
1993 const Pointer &LHS = S.Stk.pop<Pointer>();
1994 const Pointer &RHS = S.Stk.pop<Pointer>();
1995
1996 for (const Pointer &P : {LHS, RHS}) {
1997 if (P.isZeroSizeArray()) {
1998 QualType PtrT = P.getType();
1999 while (auto *AT = dyn_cast<ArrayType>(PtrT))
2000 PtrT = AT->getElementType();
2001
2002 QualType ArrayTy = S.getCtx().getConstantArrayType(
2003 PtrT, APInt::getZero(1), nullptr, ArraySizeModifier::Normal, 0);
2004 S.FFDiag(S.Current->getSource(OpPC),
2005 diag::note_constexpr_pointer_subtraction_zero_size)
2006 << ArrayTy;
2007
2008 return false;
2009 }
2010 }
2011
2012 if (RHS.isZero()) {
2013 S.Stk.push<T>(T::from(LHS.getIndex()));
2014 return true;
2015 }
2016
2017 if (!Pointer::hasSameBase(LHS, RHS) && S.getLangOpts().CPlusPlus) {
2018 // TODO: Diagnose.
2019 return false;
2020 }
2021
2022 if (LHS.isZero() && RHS.isZero()) {
2023 S.Stk.push<T>();
2024 return true;
2025 }
2026
2027 T A = LHS.isElementPastEnd() ? T::from(LHS.getNumElems())
2028 : T::from(LHS.getIndex());
2029 T B = RHS.isElementPastEnd() ? T::from(RHS.getNumElems())
2030 : T::from(RHS.getIndex());
2031 return AddSubMulHelper<T, T::sub, std::minus>(S, OpPC, A.bitWidth(), A, B);
2032}
2033
2034//===----------------------------------------------------------------------===//
2035// Destroy
2036//===----------------------------------------------------------------------===//
2037
2038inline bool Destroy(InterpState &S, CodePtr OpPC, uint32_t I) {
2039 S.Current->destroy(I);
2040 return true;
2041}
2042
2043inline bool InitScope(InterpState &S, CodePtr OpPC, uint32_t I) {
2044 S.Current->initScope(I);
2045 return true;
2046}
2047
2048//===----------------------------------------------------------------------===//
2049// Cast, CastFP
2050//===----------------------------------------------------------------------===//
2051
2052template <PrimType TIn, PrimType TOut> bool Cast(InterpState &S, CodePtr OpPC) {
2053 using T = typename PrimConv<TIn>::T;
2054 using U = typename PrimConv<TOut>::T;
2055 S.Stk.push<U>(U::from(S.Stk.pop<T>()));
2056 return true;
2057}
2058
2059/// 1) Pops a Floating from the stack.
2060/// 2) Pushes a new floating on the stack that uses the given semantics.
2061inline bool CastFP(InterpState &S, CodePtr OpPC, const llvm::fltSemantics *Sem,
2062 llvm::RoundingMode RM) {
2063 Floating F = S.Stk.pop<Floating>();
2064 Floating Result = F.toSemantics(Sem, RM);
2065 S.Stk.push<Floating>(Result);
2066 return true;
2067}
2068
2069/// Like Cast(), but we cast to an arbitrary-bitwidth integral, so we need
2070/// to know what bitwidth the result should be.
2071template <PrimType Name, class T = typename PrimConv<Name>::T>
2072bool CastAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
2073 S.Stk.push<IntegralAP<false>>(
2074 IntegralAP<false>::from(S.Stk.pop<T>(), BitWidth));
2075 return true;
2076}
2077
2078template <PrimType Name, class T = typename PrimConv<Name>::T>
2079bool CastAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
2080 S.Stk.push<IntegralAP<true>>(
2081 IntegralAP<true>::from(S.Stk.pop<T>(), BitWidth));
2082 return true;
2083}
2084
2085template <PrimType Name, class T = typename PrimConv<Name>::T>
2087 const llvm::fltSemantics *Sem,
2088 llvm::RoundingMode RM) {
2089 const T &From = S.Stk.pop<T>();
2090 APSInt FromAP = From.toAPSInt();
2092
2093 auto Status = Floating::fromIntegral(FromAP, *Sem, RM, Result);
2094 S.Stk.push<Floating>(Result);
2095
2096 return CheckFloatResult(S, OpPC, Result, Status);
2097}
2098
2099template <PrimType Name, class T = typename PrimConv<Name>::T>
2101 const Floating &F = S.Stk.pop<Floating>();
2102
2103 if constexpr (std::is_same_v<T, Boolean>) {
2104 S.Stk.push<T>(T(F.isNonZero()));
2105 return true;
2106 } else {
2107 APSInt Result(std::max(8u, T::bitWidth()),
2108 /*IsUnsigned=*/!T::isSigned());
2109 auto Status = F.convertToInteger(Result);
2110
2111 // Float-to-Integral overflow check.
2112 if ((Status & APFloat::opStatus::opInvalidOp)) {
2113 const Expr *E = S.Current->getExpr(OpPC);
2114 QualType Type = E->getType();
2115
2116 S.CCEDiag(E, diag::note_constexpr_overflow) << F.getAPFloat() << Type;
2117 if (S.noteUndefinedBehavior()) {
2118 S.Stk.push<T>(T(Result));
2119 return true;
2120 }
2121 return false;
2122 }
2123
2124 S.Stk.push<T>(T(Result));
2125 return CheckFloatResult(S, OpPC, F, Status);
2126 }
2127}
2128
2129static inline bool CastFloatingIntegralAP(InterpState &S, CodePtr OpPC,
2130 uint32_t BitWidth) {
2131 const Floating &F = S.Stk.pop<Floating>();
2132
2133 APSInt Result(BitWidth, /*IsUnsigned=*/true);
2134 auto Status = F.convertToInteger(Result);
2135
2136 // Float-to-Integral overflow check.
2137 if ((Status & APFloat::opStatus::opInvalidOp) && F.isFinite()) {
2138 const Expr *E = S.Current->getExpr(OpPC);
2139 QualType Type = E->getType();
2140
2141 S.CCEDiag(E, diag::note_constexpr_overflow) << F.getAPFloat() << Type;
2142 return S.noteUndefinedBehavior();
2143 }
2144
2146 return CheckFloatResult(S, OpPC, F, Status);
2147}
2148
2149static inline bool CastFloatingIntegralAPS(InterpState &S, CodePtr OpPC,
2150 uint32_t BitWidth) {
2151 const Floating &F = S.Stk.pop<Floating>();
2152
2153 APSInt Result(BitWidth, /*IsUnsigned=*/false);
2154 auto Status = F.convertToInteger(Result);
2155
2156 // Float-to-Integral overflow check.
2157 if ((Status & APFloat::opStatus::opInvalidOp) && F.isFinite()) {
2158 const Expr *E = S.Current->getExpr(OpPC);
2159 QualType Type = E->getType();
2160
2161 S.CCEDiag(E, diag::note_constexpr_overflow) << F.getAPFloat() << Type;
2162 return S.noteUndefinedBehavior();
2163 }
2164
2166 return CheckFloatResult(S, OpPC, F, Status);
2167}
2168
2169template <PrimType Name, class T = typename PrimConv<Name>::T>
2171 const Pointer &Ptr = S.Stk.pop<Pointer>();
2172
2173 if (Ptr.isDummy())
2174 return false;
2175
2176 const SourceInfo &E = S.Current->getSource(OpPC);
2177 S.CCEDiag(E, diag::note_constexpr_invalid_cast)
2178 << 2 << S.getLangOpts().CPlusPlus << S.Current->getRange(OpPC);
2179
2180 S.Stk.push<T>(T::from(Ptr.getIntegerRepresentation()));
2181 return true;
2182}
2183
2184static inline bool CastPointerIntegralAP(InterpState &S, CodePtr OpPC,
2185 uint32_t BitWidth) {
2186 const Pointer &Ptr = S.Stk.pop<Pointer>();
2187
2188 if (Ptr.isDummy())
2189 return false;
2190
2191 const SourceInfo &E = S.Current->getSource(OpPC);
2192 S.CCEDiag(E, diag::note_constexpr_invalid_cast)
2193 << 2 << S.getLangOpts().CPlusPlus << S.Current->getRange(OpPC);
2194
2195 S.Stk.push<IntegralAP<false>>(
2197 return true;
2198}
2199
2200static inline bool CastPointerIntegralAPS(InterpState &S, CodePtr OpPC,
2201 uint32_t BitWidth) {
2202 const Pointer &Ptr = S.Stk.pop<Pointer>();
2203
2204 if (Ptr.isDummy())
2205 return false;
2206
2207 const SourceInfo &E = S.Current->getSource(OpPC);
2208 S.CCEDiag(E, diag::note_constexpr_invalid_cast)
2209 << 2 << S.getLangOpts().CPlusPlus << S.Current->getRange(OpPC);
2210
2211 S.Stk.push<IntegralAP<true>>(
2213 return true;
2214}
2215
2216static inline bool PtrPtrCast(InterpState &S, CodePtr OpPC, bool SrcIsVoidPtr) {
2217 const auto &Ptr = S.Stk.peek<Pointer>();
2218
2219 if (SrcIsVoidPtr && S.getLangOpts().CPlusPlus) {
2220 bool HasValidResult = !Ptr.isZero();
2221
2222 if (HasValidResult) {
2223 // FIXME: note_constexpr_invalid_void_star_cast
2224 } else if (!S.getLangOpts().CPlusPlus26) {
2225 const SourceInfo &E = S.Current->getSource(OpPC);
2226 S.CCEDiag(E, diag::note_constexpr_invalid_cast)
2227 << 3 << "'void *'" << S.Current->getRange(OpPC);
2228 }
2229 } else {
2230 const SourceInfo &E = S.Current->getSource(OpPC);
2231 S.CCEDiag(E, diag::note_constexpr_invalid_cast)
2232 << 2 << S.getLangOpts().CPlusPlus << S.Current->getRange(OpPC);
2233 }
2234
2235 return true;
2236}
2237
2238//===----------------------------------------------------------------------===//
2239// Zero, Nullptr
2240//===----------------------------------------------------------------------===//
2241
2242template <PrimType Name, class T = typename PrimConv<Name>::T>
2243bool Zero(InterpState &S, CodePtr OpPC) {
2244 S.Stk.push<T>(T::zero());
2245 return true;
2246}
2247
2248static inline bool ZeroIntAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
2249 S.Stk.push<IntegralAP<false>>(IntegralAP<false>::zero(BitWidth));
2250 return true;
2251}
2252
2253static inline bool ZeroIntAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
2254 S.Stk.push<IntegralAP<true>>(IntegralAP<true>::zero(BitWidth));
2255 return true;
2256}
2257
2258template <PrimType Name, class T = typename PrimConv<Name>::T>
2259inline bool Null(InterpState &S, CodePtr OpPC, const Descriptor *Desc) {
2260 // Note: Desc can be null.
2261 S.Stk.push<T>(0, Desc);
2262 return true;
2263}
2264
2265//===----------------------------------------------------------------------===//
2266// This, ImplicitThis
2267//===----------------------------------------------------------------------===//
2268
2269inline bool This(InterpState &S, CodePtr OpPC) {
2270 // Cannot read 'this' in this mode.
2271 if (S.checkingPotentialConstantExpression()) {
2272 return false;
2273 }
2274
2275 const Pointer &This = S.Current->getThis();
2276 if (!CheckThis(S, OpPC, This))
2277 return false;
2278
2279 // Ensure the This pointer has been cast to the correct base.
2280 if (!This.isDummy()) {
2281 assert(isa<CXXMethodDecl>(S.Current->getFunction()->getDecl()));
2282 assert(This.getRecord());
2283 assert(
2284 This.getRecord()->getDecl() ==
2285 cast<CXXMethodDecl>(S.Current->getFunction()->getDecl())->getParent());
2286 }
2287
2288 S.Stk.push<Pointer>(This);
2289 return true;
2290}
2291
2292inline bool RVOPtr(InterpState &S, CodePtr OpPC) {
2293 assert(S.Current->getFunction()->hasRVO());
2294 if (S.checkingPotentialConstantExpression())
2295 return false;
2296 S.Stk.push<Pointer>(S.Current->getRVOPtr());
2297 return true;
2298}
2299
2300//===----------------------------------------------------------------------===//
2301// Shr, Shl
2302//===----------------------------------------------------------------------===//
2303enum class ShiftDir { Left, Right };
2304
2305template <class LT, class RT, ShiftDir Dir>
2306inline bool DoShift(InterpState &S, CodePtr OpPC, LT &LHS, RT &RHS) {
2307 const unsigned Bits = LHS.bitWidth();
2308
2309 // OpenCL 6.3j: shift values are effectively % word size of LHS.
2310 if (S.getLangOpts().OpenCL)
2311 RT::bitAnd(RHS, RT::from(LHS.bitWidth() - 1, RHS.bitWidth()),
2312 RHS.bitWidth(), &RHS);
2313
2314 if (RHS.isNegative()) {
2315 // During constant-folding, a negative shift is an opposite shift. Such a
2316 // shift is not a constant expression.
2317 const SourceInfo &Loc = S.Current->getSource(OpPC);
2318 S.CCEDiag(Loc, diag::note_constexpr_negative_shift) << RHS.toAPSInt();
2319 if (!S.noteUndefinedBehavior())
2320 return false;
2321 RHS = -RHS;
2322 return DoShift<LT, RT,
2324 S, OpPC, LHS, RHS);
2325 }
2326
2327 if constexpr (Dir == ShiftDir::Left) {
2328 if (LHS.isNegative() && !S.getLangOpts().CPlusPlus20) {
2329 // C++11 [expr.shift]p2: A signed left shift must have a non-negative
2330 // operand, and must not overflow the corresponding unsigned type.
2331 // C++2a [expr.shift]p2: E1 << E2 is the unique value congruent to
2332 // E1 x 2^E2 module 2^N.
2333 const SourceInfo &Loc = S.Current->getSource(OpPC);
2334 S.CCEDiag(Loc, diag::note_constexpr_lshift_of_negative) << LHS.toAPSInt();
2335 if (!S.noteUndefinedBehavior())
2336 return false;
2337 }
2338 }
2339
2340 if (!CheckShift(S, OpPC, LHS, RHS, Bits))
2341 return false;
2342
2343 // Limit the shift amount to Bits - 1. If this happened,
2344 // it has already been diagnosed by CheckShift() above,
2345 // but we still need to handle it.
2346 typename LT::AsUnsigned R;
2347 if constexpr (Dir == ShiftDir::Left) {
2348 if (RHS > RT::from(Bits - 1, RHS.bitWidth()))
2349 LT::AsUnsigned::shiftLeft(LT::AsUnsigned::from(LHS),
2350 LT::AsUnsigned::from(Bits - 1), Bits, &R);
2351 else
2352 LT::AsUnsigned::shiftLeft(LT::AsUnsigned::from(LHS),
2353 LT::AsUnsigned::from(RHS, Bits), Bits, &R);
2354 } else {
2355 if (RHS > RT::from(Bits - 1, RHS.bitWidth()))
2356 LT::AsUnsigned::shiftRight(LT::AsUnsigned::from(LHS),
2357 LT::AsUnsigned::from(Bits - 1), Bits, &R);
2358 else
2359 LT::AsUnsigned::shiftRight(LT::AsUnsigned::from(LHS),
2360 LT::AsUnsigned::from(RHS, Bits), Bits, &R);
2361 }
2362
2363 // We did the shift above as unsigned. Restore the sign bit if we need to.
2364 if constexpr (Dir == ShiftDir::Right) {
2365 if (LHS.isSigned() && LHS.isNegative()) {
2366 typename LT::AsUnsigned SignBit;
2367 LT::AsUnsigned::shiftLeft(LT::AsUnsigned::from(1, Bits),
2368 LT::AsUnsigned::from(Bits - 1, Bits), Bits,
2369 &SignBit);
2370 LT::AsUnsigned::bitOr(R, SignBit, Bits, &R);
2371 }
2372 }
2373
2374 S.Stk.push<LT>(LT::from(R));
2375 return true;
2376}
2377
2378template <PrimType NameL, PrimType NameR>
2379inline bool Shr(InterpState &S, CodePtr OpPC) {
2380 using LT = typename PrimConv<NameL>::T;
2381 using RT = typename PrimConv<NameR>::T;
2382 auto RHS = S.Stk.pop<RT>();
2383 auto LHS = S.Stk.pop<LT>();
2384
2385 return DoShift<LT, RT, ShiftDir::Right>(S, OpPC, LHS, RHS);
2386}
2387
2388template <PrimType NameL, PrimType NameR>
2389inline bool Shl(InterpState &S, CodePtr OpPC) {
2390 using LT = typename PrimConv<NameL>::T;
2391 using RT = typename PrimConv<NameR>::T;
2392 auto RHS = S.Stk.pop<RT>();
2393 auto LHS = S.Stk.pop<LT>();
2394
2395 return DoShift<LT, RT, ShiftDir::Left>(S, OpPC, LHS, RHS);
2396}
2397
2398//===----------------------------------------------------------------------===//
2399// NoRet
2400//===----------------------------------------------------------------------===//
2401
2402inline bool NoRet(InterpState &S, CodePtr OpPC) {
2403 SourceLocation EndLoc = S.Current->getCallee()->getEndLoc();
2404 S.FFDiag(EndLoc, diag::note_constexpr_no_return);
2405 return false;
2406}
2407
2408//===----------------------------------------------------------------------===//
2409// NarrowPtr, ExpandPtr
2410//===----------------------------------------------------------------------===//
2411
2412inline bool NarrowPtr(InterpState &S, CodePtr OpPC) {
2413 const Pointer &Ptr = S.Stk.pop<Pointer>();
2414 S.Stk.push<Pointer>(Ptr.narrow());
2415 return true;
2416}
2417
2418inline bool ExpandPtr(InterpState &S, CodePtr OpPC) {
2419 const Pointer &Ptr = S.Stk.pop<Pointer>();
2420 S.Stk.push<Pointer>(Ptr.expand());
2421 return true;
2422}
2423
2424// 1) Pops an integral value from the stack
2425// 2) Peeks a pointer
2426// 3) Pushes a new pointer that's a narrowed array
2427// element of the peeked pointer with the value
2428// from 1) added as offset.
2429//
2430// This leaves the original pointer on the stack and pushes a new one
2431// with the offset applied and narrowed.
2432template <PrimType Name, class T = typename PrimConv<Name>::T>
2433inline bool ArrayElemPtr(InterpState &S, CodePtr OpPC) {
2434 const T &Offset = S.Stk.pop<T>();
2435 const Pointer &Ptr = S.Stk.peek<Pointer>();
2436
2437 if (!Ptr.isZero() && !Offset.isZero()) {
2438 if (!CheckArray(S, OpPC, Ptr))
2439 return false;
2440 }
2441
2442 if (!OffsetHelper<T, ArithOp::Add>(S, OpPC, Offset, Ptr))
2443 return false;
2444
2445 return NarrowPtr(S, OpPC);
2446}
2447
2448template <PrimType Name, class T = typename PrimConv<Name>::T>
2449inline bool ArrayElemPtrPop(InterpState &S, CodePtr OpPC) {
2450 const T &Offset = S.Stk.pop<T>();
2451 const Pointer &Ptr = S.Stk.pop<Pointer>();
2452
2453 if (!Ptr.isZero() && !Offset.isZero()) {
2454 if (!CheckArray(S, OpPC, Ptr))
2455 return false;
2456 }
2457
2458 if (!OffsetHelper<T, ArithOp::Add>(S, OpPC, Offset, Ptr))
2459 return false;
2460
2461 return NarrowPtr(S, OpPC);
2462}
2463
2464template <PrimType Name, class T = typename PrimConv<Name>::T>
2465inline bool ArrayElem(InterpState &S, CodePtr OpPC, uint32_t Index) {
2466 const Pointer &Ptr = S.Stk.peek<Pointer>();
2467
2468 if (!CheckLoad(S, OpPC, Ptr))
2469 return false;
2470
2471 S.Stk.push<T>(Ptr.atIndex(Index).deref<T>());
2472 return true;
2473}
2474
2475template <PrimType Name, class T = typename PrimConv<Name>::T>
2476inline bool ArrayElemPop(InterpState &S, CodePtr OpPC, uint32_t Index) {
2477 const Pointer &Ptr = S.Stk.pop<Pointer>();
2478
2479 if (!CheckLoad(S, OpPC, Ptr))
2480 return false;
2481
2482 S.Stk.push<T>(Ptr.atIndex(Index).deref<T>());
2483 return true;
2484}
2485
2486template <PrimType Name, class T = typename PrimConv<Name>::T>
2487inline bool CopyArray(InterpState &S, CodePtr OpPC, uint32_t SrcIndex,
2488 uint32_t DestIndex, uint32_t Size) {
2489 const auto &SrcPtr = S.Stk.pop<Pointer>();
2490 const auto &DestPtr = S.Stk.peek<Pointer>();
2491
2492 for (uint32_t I = 0; I != Size; ++I) {
2493 const Pointer &SP = SrcPtr.atIndex(SrcIndex + I);
2494
2495 if (!CheckLoad(S, OpPC, SP))
2496 return false;
2497
2498 const Pointer &DP = DestPtr.atIndex(DestIndex + I);
2499 DP.deref<T>() = SP.deref<T>();
2500 DP.initialize();
2501 }
2502 return true;
2503}
2504
2505/// Just takes a pointer and checks if it's an incomplete
2506/// array type.
2507inline bool ArrayDecay(InterpState &S, CodePtr OpPC) {
2508 const Pointer &Ptr = S.Stk.pop<Pointer>();
2509
2510 if (Ptr.isZero()) {
2511 S.Stk.push<Pointer>(Ptr);
2512 return true;
2513 }
2514
2515 if (!CheckRange(S, OpPC, Ptr, CSK_ArrayToPointer))
2516 return false;
2517
2518 if (Ptr.isRoot() || !Ptr.isUnknownSizeArray() || Ptr.isDummy()) {
2519 S.Stk.push<Pointer>(Ptr.atIndex(0));
2520 return true;
2521 }
2522
2523 const SourceInfo &E = S.Current->getSource(OpPC);
2524 S.FFDiag(E, diag::note_constexpr_unsupported_unsized_array);
2525
2526 return false;
2527}
2528
2529inline bool CallVar(InterpState &S, CodePtr OpPC, const Function *Func,
2530 uint32_t VarArgSize) {
2531 if (Func->hasThisPointer()) {
2532 size_t ArgSize = Func->getArgSize() + VarArgSize;
2533 size_t ThisOffset = ArgSize - (Func->hasRVO() ? primSize(PT_Ptr) : 0);
2534 const Pointer &ThisPtr = S.Stk.peek<Pointer>(ThisOffset);
2535
2536 // If the current function is a lambda static invoker and
2537 // the function we're about to call is a lambda call operator,
2538 // skip the CheckInvoke, since the ThisPtr is a null pointer
2539 // anyway.
2540 if (!(S.Current->getFunction() &&
2541 S.Current->getFunction()->isLambdaStaticInvoker() &&
2542 Func->isLambdaCallOperator())) {
2543 if (!CheckInvoke(S, OpPC, ThisPtr))
2544 return false;
2545 }
2546
2547 if (S.checkingPotentialConstantExpression())
2548 return false;
2549 }
2550
2551 if (!CheckCallable(S, OpPC, Func))
2552 return false;
2553
2554 if (!CheckCallDepth(S, OpPC))
2555 return false;
2556
2557 auto NewFrame = std::make_unique<InterpFrame>(S, Func, OpPC, VarArgSize);
2558 InterpFrame *FrameBefore = S.Current;
2559 S.Current = NewFrame.get();
2560
2561 APValue CallResult;
2562 // Note that we cannot assert(CallResult.hasValue()) here since
2563 // Ret() above only sets the APValue if the curent frame doesn't
2564 // have a caller set.
2565 if (Interpret(S, CallResult)) {
2566 NewFrame.release(); // Frame was delete'd already.
2567 assert(S.Current == FrameBefore);
2568 return true;
2569 }
2570
2571 // Interpreting the function failed somehow. Reset to
2572 // previous state.
2573 S.Current = FrameBefore;
2574 return false;
2575
2576 return false;
2577}
2578
2579inline bool Call(InterpState &S, CodePtr OpPC, const Function *Func,
2580 uint32_t VarArgSize) {
2581 if (Func->hasThisPointer()) {
2582 size_t ArgSize = Func->getArgSize() + VarArgSize;
2583 size_t ThisOffset = ArgSize - (Func->hasRVO() ? primSize(PT_Ptr) : 0);
2584
2585 const Pointer &ThisPtr = S.Stk.peek<Pointer>(ThisOffset);
2586
2587 // If the current function is a lambda static invoker and
2588 // the function we're about to call is a lambda call operator,
2589 // skip the CheckInvoke, since the ThisPtr is a null pointer
2590 // anyway.
2591 if (!(S.Current->getFunction() &&
2592 S.Current->getFunction()->isLambdaStaticInvoker() &&
2593 Func->isLambdaCallOperator())) {
2594 if (!CheckInvoke(S, OpPC, ThisPtr))
2595 return false;
2596 }
2597 }
2598
2599 if (!CheckCallable(S, OpPC, Func))
2600 return false;
2601
2602 if (Func->hasThisPointer() && S.checkingPotentialConstantExpression())
2603 return false;
2604
2605 if (!CheckCallDepth(S, OpPC))
2606 return false;
2607
2608 auto NewFrame = std::make_unique<InterpFrame>(S, Func, OpPC, VarArgSize);
2609 InterpFrame *FrameBefore = S.Current;
2610 S.Current = NewFrame.get();
2611
2612 APValue CallResult;
2613 // Note that we cannot assert(CallResult.hasValue()) here since
2614 // Ret() above only sets the APValue if the curent frame doesn't
2615 // have a caller set.
2616 if (Interpret(S, CallResult)) {
2617 NewFrame.release(); // Frame was delete'd already.
2618 assert(S.Current == FrameBefore);
2619 return true;
2620 }
2621
2622 // Interpreting the function failed somehow. Reset to
2623 // previous state.
2624 S.Current = FrameBefore;
2625 return false;
2626}
2627
2628inline bool CallVirt(InterpState &S, CodePtr OpPC, const Function *Func,
2629 uint32_t VarArgSize) {
2630 assert(Func->hasThisPointer());
2631 assert(Func->isVirtual());
2632 size_t ArgSize = Func->getArgSize() + VarArgSize;
2633 size_t ThisOffset = ArgSize - (Func->hasRVO() ? primSize(PT_Ptr) : 0);
2634 Pointer &ThisPtr = S.Stk.peek<Pointer>(ThisOffset);
2635
2636 const CXXRecordDecl *DynamicDecl = nullptr;
2637 {
2638 Pointer TypePtr = ThisPtr;
2639 while (TypePtr.isBaseClass())
2640 TypePtr = TypePtr.getBase();
2641
2642 QualType DynamicType = TypePtr.getType();
2643 if (DynamicType->isPointerType() || DynamicType->isReferenceType())
2644 DynamicDecl = DynamicType->getPointeeCXXRecordDecl();
2645 else
2646 DynamicDecl = DynamicType->getAsCXXRecordDecl();
2647 }
2648 assert(DynamicDecl);
2649
2650 const auto *StaticDecl = cast<CXXRecordDecl>(Func->getParentDecl());
2651 const auto *InitialFunction = cast<CXXMethodDecl>(Func->getDecl());
2652 const CXXMethodDecl *Overrider = S.getContext().getOverridingFunction(
2653 DynamicDecl, StaticDecl, InitialFunction);
2654
2655 if (Overrider != InitialFunction) {
2656 // DR1872: An instantiated virtual constexpr function can't be called in a
2657 // constant expression (prior to C++20). We can still constant-fold such a
2658 // call.
2659 if (!S.getLangOpts().CPlusPlus20 && Overrider->isVirtual()) {
2660 const Expr *E = S.Current->getExpr(OpPC);
2661 S.CCEDiag(E, diag::note_constexpr_virtual_call) << E->getSourceRange();
2662 }
2663
2664 Func = S.getContext().getOrCreateFunction(Overrider);
2665
2666 const CXXRecordDecl *ThisFieldDecl =
2667 ThisPtr.getFieldDesc()->getType()->getAsCXXRecordDecl();
2668 if (Func->getParentDecl()->isDerivedFrom(ThisFieldDecl)) {
2669 // If the function we call is further DOWN the hierarchy than the
2670 // FieldDesc of our pointer, just go up the hierarchy of this field
2671 // the furthest we can go.
2672 while (ThisPtr.isBaseClass())
2673 ThisPtr = ThisPtr.getBase();
2674 }
2675 }
2676
2677 if (!Call(S, OpPC, Func, VarArgSize))
2678 return false;
2679
2680 // Covariant return types. The return type of Overrider is a pointer
2681 // or reference to a class type.
2682 if (Overrider != InitialFunction &&
2683 Overrider->getReturnType()->isPointerOrReferenceType() &&
2684 InitialFunction->getReturnType()->isPointerOrReferenceType()) {
2685 QualType OverriderPointeeType =
2686 Overrider->getReturnType()->getPointeeType();
2687 QualType InitialPointeeType =
2688 InitialFunction->getReturnType()->getPointeeType();
2689 // We've called Overrider above, but calling code expects us to return what
2690 // InitialFunction returned. According to the rules for covariant return
2691 // types, what InitialFunction returns needs to be a base class of what
2692 // Overrider returns. So, we need to do an upcast here.
2693 unsigned Offset = S.getContext().collectBaseOffset(
2694 InitialPointeeType->getAsRecordDecl(),
2695 OverriderPointeeType->getAsRecordDecl());
2696 return GetPtrBasePop(S, OpPC, Offset);
2697 }
2698
2699 return true;
2700}
2701
2702inline bool CallBI(InterpState &S, CodePtr &PC, const Function *Func,
2703 const CallExpr *CE) {
2704 auto NewFrame = std::make_unique<InterpFrame>(S, Func, PC);
2705
2706 InterpFrame *FrameBefore = S.Current;
2707 S.Current = NewFrame.get();
2708
2709 if (InterpretBuiltin(S, PC, Func, CE)) {
2710 NewFrame.release();
2711 return true;
2712 }
2713 S.Current = FrameBefore;
2714 return false;
2715}
2716
2717inline bool CallPtr(InterpState &S, CodePtr OpPC, uint32_t ArgSize,
2718 const CallExpr *CE) {
2719 const FunctionPointer &FuncPtr = S.Stk.pop<FunctionPointer>();
2720
2721 const Function *F = FuncPtr.getFunction();
2722 if (!F) {
2723 const auto *E = cast<CallExpr>(S.Current->getExpr(OpPC));
2724 S.FFDiag(E, diag::note_constexpr_null_callee)
2725 << const_cast<Expr *>(E->getCallee()) << E->getSourceRange();
2726 return false;
2727 }
2728
2729 if (!FuncPtr.isValid() || !F->getDecl())
2730 return Invalid(S, OpPC);
2731
2732 assert(F);
2733
2734 // This happens when the call expression has been cast to
2735 // something else, but we don't support that.
2736 if (S.Ctx.classify(F->getDecl()->getReturnType()) !=
2737 S.Ctx.classify(CE->getType()))
2738 return false;
2739
2740 // Check argument nullability state.
2741 if (F->hasNonNullAttr()) {
2742 if (!CheckNonNullArgs(S, OpPC, F, CE, ArgSize))
2743 return false;
2744 }
2745
2746 assert(ArgSize >= F->getWrittenArgSize());
2747 uint32_t VarArgSize = ArgSize - F->getWrittenArgSize();
2748
2749 // We need to do this explicitly here since we don't have the necessary
2750 // information to do it automatically.
2751 if (F->isThisPointerExplicit())
2752 VarArgSize -= align(primSize(PT_Ptr));
2753
2754 if (F->isVirtual())
2755 return CallVirt(S, OpPC, F, VarArgSize);
2756
2757 return Call(S, OpPC, F, VarArgSize);
2758}
2759
2760inline bool GetFnPtr(InterpState &S, CodePtr OpPC, const Function *Func) {
2761 assert(Func);
2762 S.Stk.push<FunctionPointer>(Func);
2763 return true;
2764}
2765
2766template <PrimType Name, class T = typename PrimConv<Name>::T>
2767inline bool GetIntPtr(InterpState &S, CodePtr OpPC, const Descriptor *Desc) {
2768 const T &IntVal = S.Stk.pop<T>();
2769
2770 S.Stk.push<Pointer>(static_cast<uint64_t>(IntVal), Desc);
2771 return true;
2772}
2773
2774inline bool GetMemberPtr(InterpState &S, CodePtr OpPC, const Decl *D) {
2775 S.Stk.push<MemberPointer>(D);
2776 return true;
2777}
2778
2779inline bool GetMemberPtrBase(InterpState &S, CodePtr OpPC) {
2780 const auto &MP = S.Stk.pop<MemberPointer>();
2781
2782 S.Stk.push<Pointer>(MP.getBase());
2783 return true;
2784}
2785
2786inline bool GetMemberPtrDecl(InterpState &S, CodePtr OpPC) {
2787 const auto &MP = S.Stk.pop<MemberPointer>();
2788
2789 const auto *FD = cast<FunctionDecl>(MP.getDecl());
2790 const auto *Func = S.getContext().getOrCreateFunction(FD);
2791
2792 S.Stk.push<FunctionPointer>(Func);
2793 return true;
2794}
2795
2796/// Just emit a diagnostic. The expression that caused emission of this
2797/// op is not valid in a constant context.
2798inline bool Invalid(InterpState &S, CodePtr OpPC) {
2799 const SourceLocation &Loc = S.Current->getLocation(OpPC);
2800 S.FFDiag(Loc, diag::note_invalid_subexpr_in_const_expr)
2801 << S.Current->getRange(OpPC);
2802 return false;
2803}
2804
2805inline bool Unsupported(InterpState &S, CodePtr OpPC) {
2806 const SourceLocation &Loc = S.Current->getLocation(OpPC);
2807 S.FFDiag(Loc, diag::note_constexpr_stmt_expr_unsupported)
2808 << S.Current->getRange(OpPC);
2809 return false;
2810}
2811
2812/// Do nothing and just abort execution.
2813inline bool Error(InterpState &S, CodePtr OpPC) { return false; }
2814
2815/// Same here, but only for casts.
2816inline bool InvalidCast(InterpState &S, CodePtr OpPC, CastKind Kind,
2817 bool Fatal) {
2818 const SourceLocation &Loc = S.Current->getLocation(OpPC);
2819
2820 // FIXME: Support diagnosing other invalid cast kinds.
2821 if (Kind == CastKind::Reinterpret) {
2822 S.CCEDiag(Loc, diag::note_constexpr_invalid_cast)
2823 << static_cast<unsigned>(Kind) << S.Current->getRange(OpPC);
2824 return !Fatal;
2825 }
2826 return false;
2827}
2828
2830 const DeclRefExpr *DR) {
2831 assert(DR);
2832 return CheckDeclRef(S, OpPC, DR);
2833}
2834
2836 if (S.inConstantContext()) {
2837 const SourceRange &ArgRange = S.Current->getRange(OpPC);
2838 const Expr *E = S.Current->getExpr(OpPC);
2839 S.CCEDiag(E, diag::note_constexpr_non_const_vectorelements) << ArgRange;
2840 }
2841 return false;
2842}
2843
2844inline bool Assume(InterpState &S, CodePtr OpPC) {
2845 const auto Val = S.Stk.pop<Boolean>();
2846
2847 if (Val)
2848 return true;
2849
2850 // Else, diagnose.
2851 const SourceLocation &Loc = S.Current->getLocation(OpPC);
2852 S.CCEDiag(Loc, diag::note_constexpr_assumption_failed);
2853 return false;
2854}
2855
2856template <PrimType Name, class T = typename PrimConv<Name>::T>
2857inline bool OffsetOf(InterpState &S, CodePtr OpPC, const OffsetOfExpr *E) {
2858 llvm::SmallVector<int64_t> ArrayIndices;
2859 for (size_t I = 0; I != E->getNumExpressions(); ++I)
2860 ArrayIndices.emplace_back(S.Stk.pop<int64_t>());
2861
2862 int64_t Result;
2863 if (!InterpretOffsetOf(S, OpPC, E, ArrayIndices, Result))
2864 return false;
2865
2866 S.Stk.push<T>(T::from(Result));
2867
2868 return true;
2869}
2870
2871template <PrimType Name, class T = typename PrimConv<Name>::T>
2872inline bool CheckNonNullArg(InterpState &S, CodePtr OpPC) {
2873 const T &Arg = S.Stk.peek<T>();
2874 if (!Arg.isZero())
2875 return true;
2876
2877 const SourceLocation &Loc = S.Current->getLocation(OpPC);
2878 S.CCEDiag(Loc, diag::note_non_null_attribute_failed);
2879
2880 return false;
2881}
2882
2883void diagnoseEnumValue(InterpState &S, CodePtr OpPC, const EnumDecl *ED,
2884 const APSInt &Value);
2885
2886template <PrimType Name, class T = typename PrimConv<Name>::T>
2887inline bool CheckEnumValue(InterpState &S, CodePtr OpPC, const EnumDecl *ED) {
2888 assert(ED);
2889 assert(!ED->isFixed());
2890 const APSInt Val = S.Stk.peek<T>().toAPSInt();
2891
2892 if (S.inConstantContext())
2893 diagnoseEnumValue(S, OpPC, ED, Val);
2894 return true;
2895}
2896
2897/// OldPtr -> Integer -> NewPtr.
2898template <PrimType TIn, PrimType TOut>
2899inline bool DecayPtr(InterpState &S, CodePtr OpPC) {
2900 static_assert(isPtrType(TIn) && isPtrType(TOut));
2901 using FromT = typename PrimConv<TIn>::T;
2902 using ToT = typename PrimConv<TOut>::T;
2903
2904 const FromT &OldPtr = S.Stk.pop<FromT>();
2905
2906 if constexpr (std::is_same_v<FromT, FunctionPointer> &&
2907 std::is_same_v<ToT, Pointer>) {
2908 S.Stk.push<Pointer>(OldPtr.getFunction());
2909 return true;
2910 }
2911
2912 S.Stk.push<ToT>(ToT(OldPtr.getIntegerRepresentation(), nullptr));
2913 return true;
2914}
2915
2916inline bool CheckDecl(InterpState &S, CodePtr OpPC, const VarDecl *VD) {
2917 // An expression E is a core constant expression unless the evaluation of E
2918 // would evaluate one of the following: [C++23] - a control flow that passes
2919 // through a declaration of a variable with static or thread storage duration
2920 // unless that variable is usable in constant expressions.
2921 assert(VD->isLocalVarDecl() &&
2922 VD->isStaticLocal()); // Checked before emitting this.
2923
2924 if (VD == S.EvaluatingDecl)
2925 return true;
2926
2927 if (!VD->isUsableInConstantExpressions(S.getCtx())) {
2928 S.CCEDiag(VD->getLocation(), diag::note_constexpr_static_local)
2929 << (VD->getTSCSpec() == TSCS_unspecified ? 0 : 1) << VD;
2930 return false;
2931 }
2932 return true;
2933}
2934
2935inline bool Alloc(InterpState &S, CodePtr OpPC, const Descriptor *Desc) {
2936 assert(Desc);
2937
2938 if (!CheckDynamicMemoryAllocation(S, OpPC))
2939 return false;
2940
2941 DynamicAllocator &Allocator = S.getAllocator();
2942 Block *B = Allocator.allocate(Desc, S.Ctx.getEvalID());
2943 assert(B);
2944
2945 S.Stk.push<Pointer>(B, sizeof(InlineDescriptor));
2946
2947 return true;
2948}
2949
2950template <PrimType Name, class SizeT = typename PrimConv<Name>::T>
2951inline bool AllocN(InterpState &S, CodePtr OpPC, PrimType T, const Expr *Source,
2952 bool IsNoThrow) {
2953 if (!CheckDynamicMemoryAllocation(S, OpPC))
2954 return false;
2955
2956 SizeT NumElements = S.Stk.pop<SizeT>();
2957 if (!CheckArraySize(S, OpPC, &NumElements, primSize(T), IsNoThrow)) {
2958 if (!IsNoThrow)
2959 return false;
2960
2961 // If this failed and is nothrow, just return a null ptr.
2962 S.Stk.push<Pointer>(0, nullptr);
2963 return true;
2964 }
2965
2966 DynamicAllocator &Allocator = S.getAllocator();
2967 Block *B = Allocator.allocate(Source, T, static_cast<size_t>(NumElements),
2968 S.Ctx.getEvalID());
2969 assert(B);
2970 S.Stk.push<Pointer>(B, sizeof(InlineDescriptor));
2971
2972 return true;
2973}
2974
2975template <PrimType Name, class SizeT = typename PrimConv<Name>::T>
2976inline bool AllocCN(InterpState &S, CodePtr OpPC, const Descriptor *ElementDesc,
2977 bool IsNoThrow) {
2978 if (!CheckDynamicMemoryAllocation(S, OpPC))
2979 return false;
2980
2981 SizeT NumElements = S.Stk.pop<SizeT>();
2982 if (!CheckArraySize(S, OpPC, &NumElements, ElementDesc->getSize(),
2983 IsNoThrow)) {
2984 if (!IsNoThrow)
2985 return false;
2986
2987 // If this failed and is nothrow, just return a null ptr.
2988 S.Stk.push<Pointer>(0, ElementDesc);
2989 return true;
2990 }
2991
2992 DynamicAllocator &Allocator = S.getAllocator();
2993 Block *B = Allocator.allocate(ElementDesc, static_cast<size_t>(NumElements),
2994 S.Ctx.getEvalID());
2995 assert(B);
2996
2997 S.Stk.push<Pointer>(B, sizeof(InlineDescriptor));
2998
2999 return true;
3000}
3001
3002bool RunDestructors(InterpState &S, CodePtr OpPC, const Block *B);
3003static inline bool Free(InterpState &S, CodePtr OpPC, bool DeleteIsArrayForm) {
3004 if (!CheckDynamicMemoryAllocation(S, OpPC))
3005 return false;
3006
3007 const Expr *Source = nullptr;
3008 const Block *BlockToDelete = nullptr;
3009 {
3010 // Extra scope for this so the block doesn't have this pointer
3011 // pointing to it when we destroy it.
3012 const Pointer &Ptr = S.Stk.pop<Pointer>();
3013
3014 // Deleteing nullptr is always fine.
3015 if (Ptr.isZero())
3016 return true;
3017
3018 if (!Ptr.isRoot() || Ptr.isOnePastEnd() || Ptr.isArrayElement()) {
3019 const SourceInfo &Loc = S.Current->getSource(OpPC);
3020 S.FFDiag(Loc, diag::note_constexpr_delete_subobject)
3021 << Ptr.toDiagnosticString(S.getCtx()) << Ptr.isOnePastEnd();
3022 return false;
3023 }
3024
3025 Source = Ptr.getDeclDesc()->asExpr();
3026 BlockToDelete = Ptr.block();
3027
3028 if (!CheckDeleteSource(S, OpPC, Source, Ptr))
3029 return false;
3030 }
3031 assert(Source);
3032 assert(BlockToDelete);
3033
3034 // Invoke destructors before deallocating the memory.
3035 if (!RunDestructors(S, OpPC, BlockToDelete))
3036 return false;
3037
3038 DynamicAllocator &Allocator = S.getAllocator();
3039 bool WasArrayAlloc = Allocator.isArrayAllocation(Source);
3040 const Descriptor *BlockDesc = BlockToDelete->getDescriptor();
3041
3042 if (!Allocator.deallocate(Source, BlockToDelete, S)) {
3043 // Nothing has been deallocated, this must be a double-delete.
3044 const SourceInfo &Loc = S.Current->getSource(OpPC);
3045 S.FFDiag(Loc, diag::note_constexpr_double_delete);
3046 return false;
3047 }
3048 return CheckNewDeleteForms(S, OpPC, WasArrayAlloc, DeleteIsArrayForm,
3049 BlockDesc, Source);
3050}
3051
3052inline bool CheckLiteralType(InterpState &S, CodePtr OpPC, const Type *T) {
3053 assert(T);
3054 assert(!S.getLangOpts().CPlusPlus23);
3055
3056 // C++1y: A constant initializer for an object o [...] may also invoke
3057 // constexpr constructors for o and its subobjects even if those objects
3058 // are of non-literal class types.
3059 //
3060 // C++11 missed this detail for aggregates, so classes like this:
3061 // struct foo_t { union { int i; volatile int j; } u; };
3062 // are not (obviously) initializable like so:
3063 // __attribute__((__require_constant_initialization__))
3064 // static const foo_t x = {{0}};
3065 // because "i" is a subobject with non-literal initialization (due to the
3066 // volatile member of the union). See:
3067 // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1677
3068 // Therefore, we use the C++1y behavior.
3069
3070 if (S.EvaluatingDecl)
3071 return true;
3072
3073 if (S.Current->getFunction() && S.Current->getFunction()->isConstructor() &&
3074 S.Current->getThis().getDeclDesc()->asDecl() == S.EvaluatingDecl)
3075 return true;
3076
3077 const Expr *E = S.Current->getExpr(OpPC);
3078 if (S.getLangOpts().CPlusPlus11)
3079 S.FFDiag(E, diag::note_constexpr_nonliteral) << E->getType();
3080 else
3081 S.FFDiag(E, diag::note_invalid_subexpr_in_const_expr);
3082 return false;
3083}
3084
3085//===----------------------------------------------------------------------===//
3086// Read opcode arguments
3087//===----------------------------------------------------------------------===//
3088
3089template <typename T> inline T ReadArg(InterpState &S, CodePtr &OpPC) {
3090 if constexpr (std::is_pointer<T>::value) {
3091 uint32_t ID = OpPC.read<uint32_t>();
3092 return reinterpret_cast<T>(S.P.getNativePointer(ID));
3093 } else {
3094 return OpPC.read<T>();
3095 }
3096}
3097
3098template <> inline Floating ReadArg<Floating>(InterpState &S, CodePtr &OpPC) {
3100 OpPC += align(F.bytesToSerialize());
3101 return F;
3102}
3103
3104template <>
3105inline IntegralAP<false> ReadArg<IntegralAP<false>>(InterpState &S,
3106 CodePtr &OpPC) {
3107 IntegralAP<false> I = IntegralAP<false>::deserialize(*OpPC);
3108 OpPC += align(I.bytesToSerialize());
3109 return I;
3110}
3111
3112template <>
3113inline IntegralAP<true> ReadArg<IntegralAP<true>>(InterpState &S,
3114 CodePtr &OpPC) {
3115 IntegralAP<true> I = IntegralAP<true>::deserialize(*OpPC);
3116 OpPC += align(I.bytesToSerialize());
3117 return I;
3118}
3119
3120} // namespace interp
3121} // namespace clang
3122
3123#endif
Defines the clang::ASTContext interface.
#define V(N, I)
Definition: ASTContext.h:3341
ASTImporterLookupTable & LT
StringRef P
const Decl * D
Expr * E
llvm::APSInt APSInt
Definition: Compiler.cpp:22
void HandleComplexComplexDiv(APFloat A, APFloat B, APFloat C, APFloat D, APFloat &ResR, APFloat &ResI)
void HandleComplexComplexMul(APFloat A, APFloat B, APFloat C, APFloat D, APFloat &ResR, APFloat &ResI)
static std::string toString(const clang::SanitizerSet &Sanitizers)
Produce a string containing comma-separated names of sanitizers in Sanitizers set.
SourceLocation Loc
Definition: SemaObjC.cpp:759
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition: APValue.h:122
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:2064
bool isVirtual() const
Definition: DeclCXX.h:2119
Represents a C++ struct/union/class.
Definition: DeclCXX.h:258
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2830
const ValueInfo * getValueInfo(ComparisonCategoryResult ValueKind) const
ComparisonCategoryResult makeWeakResult(ComparisonCategoryResult Res) const
Converts the specified result kind into the correct result kind for this category.
static unsigned getMaxSizeBits(const ASTContext &Context)
Determine the maximum number of active bits that an array's size can require, which limits the maximu...
Definition: Type.cpp:218
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:1265
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
SourceLocation getLocation() const
Definition: DeclBase.h:446
Represents an enum.
Definition: Decl.h:3844
bool isFixed() const
Returns true if this is an Objective-C, C++11, or Microsoft-style enumeration with a fixed underlying...
Definition: Decl.h:4058
This represents one expression.
Definition: Expr.h:110
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition: Expr.cpp:277
QualType getType() const
Definition: Expr.h:142
QualType getReturnType() const
Definition: Decl.h:2717
Implicit declaration of a temporary that was materialized by a MaterializeTemporaryExpr and lifetime-...
Definition: DeclCXX.h:3233
APValue * getOrCreateValue(bool MayCreate) const
Get the storage for the constant value of a materialized temporary of static storage duration.
Definition: DeclCXX.cpp:3094
Expr * getTemporaryExpr()
Retrieve the expression to which the temporary materialization conversion was applied.
Definition: DeclCXX.h:3279
OffsetOfExpr - [C99 7.17] - This represents an expression of the form offsetof(record-type,...
Definition: Expr.h:2475
A (possibly-)qualified type.
Definition: Type.h:941
Represents a struct/union/class.
Definition: Decl.h:4145
const LangOptions & getLangOpts() const
Definition: Sema.h:553
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:326
The base class of the type hierarchy.
Definition: Type.h:1829
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition: Type.cpp:705
bool isPointerOrReferenceType() const
Definition: Type.h:8007
RecordDecl * getAsRecordDecl() const
Retrieves the RecordDecl this type refers to.
Definition: Type.cpp:1886
bool isWeak() const
Determine whether this symbol is weakly-imported, or declared with the weak or weak-ref attr.
Definition: Decl.cpp:5364
Represents a variable declaration or definition.
Definition: Decl.h:879
bool isStaticLocal() const
Returns true if a variable with function scope is a static local variable.
Definition: Decl.h:1156
ThreadStorageClassSpecifier getTSCSpec() const
Definition: Decl.h:1125
bool isLocalVarDecl() const
Returns true for local variable declarations other than parameters.
Definition: Decl.h:1201
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:2493
A memory block, either on the stack or in the heap.
Definition: InterpBlock.h:49
unsigned getSize() const
Returns the size of the block.
Definition: InterpBlock.h:78
const Descriptor * getDescriptor() const
Returns the block's descriptor.
Definition: InterpBlock.h:67
Wrapper around boolean types.
Definition: Boolean.h:25
static Boolean from(T Value)
Definition: Boolean.h:94
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:60
Manages dynamic memory allocations done during bytecode interpretation.
static APFloat::opStatus div(const Floating &A, const Floating &B, llvm::RoundingMode RM, Floating *R)
Definition: Floating.h:200
static Floating deserialize(const std::byte *Buff)
Definition: Floating.h:153
static APFloat::opStatus sub(const Floating &A, const Floating &B, llvm::RoundingMode RM, Floating *R)
Definition: Floating.h:181
const APFloat & getAPFloat() const
Definition: Floating.h:40
static APFloat::opStatus increment(const Floating &A, llvm::RoundingMode RM, Floating *R)
Definition: Floating.h:174
static APFloat::opStatus fromIntegral(APSInt Val, const llvm::fltSemantics &Sem, llvm::RoundingMode RM, Floating &Result)
Definition: Floating.h:119
Floating toSemantics(const llvm::fltSemantics *Sem, llvm::RoundingMode RM) const
Definition: Floating.h:55
size_t bytesToSerialize() const
Definition: Floating.h:139
static APFloat::opStatus add(const Floating &A, const Floating &B, llvm::RoundingMode RM, Floating *R)
Definition: Floating.h:168
static APFloat::opStatus mul(const Floating &A, const Floating &B, llvm::RoundingMode RM, Floating *R)
Definition: Floating.h:194
bool isNonZero() const
Definition: Floating.h:92
bool isFinite() const
Definition: Floating.h:98
static APFloat::opStatus decrement(const Floating &A, llvm::RoundingMode RM, Floating *R)
Definition: Floating.h:187
APFloat::opStatus convertToInteger(APSInt &Result) const
Definition: Floating.h:50
const Function * getFunction() const
ComparisonCategoryResult compare(const FunctionPointer &RHS) const
std::string toDiagnosticString(const ASTContext &Ctx) const
Bytecode function.
Definition: Function.h:81
static IntegralAP< Signed > deserialize(const std::byte *Buff)
Definition: IntegralAP.h:284
static IntegralAP zero(int32_t BitWidth)
Definition: IntegralAP.h:120
static IntegralAP from(T Value, unsigned NumBits=0)
Definition: IntegralAP.h:96
Wrapper around numeric types.
Definition: Integral.h:66
Frame storing local variables.
Definition: InterpFrame.h:26
Interpreter context.
Definition: InterpState.h:36
ComparisonCategoryResult compare(const MemberPointer &RHS) const
Definition: MemberPointer.h:97
A pointer to a memory block, live or dead.
Definition: Pointer.h:82
static bool hasSameBase(const Pointer &A, const Pointer &B)
Checks if two pointers are comparable.
Definition: Pointer.cpp:435
Pointer narrow() const
Restricts the scope of an array element pointer.
Definition: Pointer.h:183
void deactivate() const
Deactivates an entire strurcutre.
Definition: Pointer.cpp:431
bool isInitialized() const
Checks if an object was initialized.
Definition: Pointer.cpp:311
Pointer atIndex(uint64_t Idx) const
Offsets a pointer inside an array.
Definition: Pointer.h:149
bool isDummy() const
Checks if the pointer points to a dummy value.
Definition: Pointer.h:527
Pointer atFieldSub(unsigned Off) const
Subtract the given offset from the current Base and Offset of the pointer.
Definition: Pointer.h:176
bool isExtern() const
Checks if the storage is extern.
Definition: Pointer.h:474
int64_t getIndex() const
Returns the index into an array.
Definition: Pointer.h:572
Pointer atField(unsigned Off) const
Creates a pointer to a field.
Definition: Pointer.h:168
T & deref() const
Dereferences the pointer, if it's live.
Definition: Pointer.h:623
unsigned getNumElems() const
Returns the number of elements.
Definition: Pointer.h:563
bool isUnknownSizeArray() const
Checks if the structure is an array of unknown size.
Definition: Pointer.h:416
void activate() const
Activats a field.
Definition: Pointer.cpp:395
bool isIntegralPointer() const
Definition: Pointer.h:457
QualType getType() const
Returns the type of the innermost field.
Definition: Pointer.h:338
bool isArrayElement() const
Checks if the pointer points to an array.
Definition: Pointer.h:422
bool isArrayRoot() const
Whether this array refers to an array, but not to the first element.
Definition: Pointer.h:393
bool inArray() const
Checks if the innermost field is an array.
Definition: Pointer.h:398
Pointer getBase() const
Returns a pointer to the object of which this pointer is a field.
Definition: Pointer.h:308
std::string toDiagnosticString(const ASTContext &Ctx) const
Converts the pointer to a string usable in diagnostics.
Definition: Pointer.cpp:301
bool isZero() const
Checks if the pointer is null.
Definition: Pointer.h:260
ComparisonCategoryResult compare(const Pointer &Other) const
Compare two pointers.
Definition: Pointer.h:666
const IntPointer & asIntPointer() const
Definition: Pointer.h:447
bool isRoot() const
Pointer points directly to a block.
Definition: Pointer.h:428
const Descriptor * getDeclDesc() const
Accessor for information about the declaration site.
Definition: Pointer.h:283
unsigned getOffset() const
Returns the offset into an array.
Definition: Pointer.h:375
bool isOnePastEnd() const
Checks if the index is one past end.
Definition: Pointer.h:590
uint64_t getIntegerRepresentation() const
Definition: Pointer.h:136
Pointer expand() const
Expands a pointer to the containing array, undoing narrowing.
Definition: Pointer.h:225
bool isElementPastEnd() const
Checks if the pointer is an out-of-bounds element pointer.
Definition: Pointer.h:613
bool isBlockPointer() const
Definition: Pointer.h:456
const Block * block() const
Definition: Pointer.h:569
const Descriptor * getFieldDesc() const
Accessors for information about the innermost field.
Definition: Pointer.h:328
bool isBaseClass() const
Checks if a structure is a base class.
Definition: Pointer.h:522
size_t elemSize() const
Returns the element size of the innermost field.
Definition: Pointer.h:357
bool canBeInitialized() const
If this pointer has an InlineDescriptor we can use to initialize.
Definition: Pointer.h:436
const BlockPointer & asBlockPointer() const
Definition: Pointer.h:443
void initialize() const
Initializes a field.
Definition: Pointer.cpp:347
unsigned getByteOffset() const
Returns the byte offset from the start.
Definition: Pointer.h:554
Describes the statement/declaration an opcode was generated from.
Definition: Source.h:77
bool CheckShift(InterpState &S, CodePtr OpPC, const LT &LHS, const RT &RHS, unsigned Bits)
Checks if the shift operation is legal.
Definition: Interp.h:152
bool GetPtrFieldPop(InterpState &S, CodePtr OpPC, uint32_t Off)
Definition: Interp.h:1488
bool InitPop(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1759
bool Shr(InterpState &S, CodePtr OpPC)
Definition: Interp.h:2379
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:1340
llvm::APFloat APFloat
Definition: Floating.h:23
bool IncPop(InterpState &S, CodePtr OpPC)
1) Pops a pointer from the stack 2) Load the value from the pointer 3) Writes the value increased by ...
Definition: Interp.h:783
bool ArrayElemPop(InterpState &S, CodePtr OpPC, uint32_t Index)
Definition: Interp.h:2476
bool GetPtrBasePop(InterpState &S, CodePtr OpPC, uint32_t Off)
Definition: Interp.h:1578
bool Subf(InterpState &S, CodePtr OpPC, llvm::RoundingMode RM)
Definition: Interp.h:397
bool ArrayElem(InterpState &S, CodePtr OpPC, uint32_t Index)
Definition: Interp.h:2465
bool GT(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1112
bool CheckInit(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Checks if a value can be initialized.
Definition: Interp.cpp:615
bool GetMemberPtrBase(InterpState &S, CodePtr OpPC)
Definition: Interp.h:2779
bool DecPop(InterpState &S, CodePtr OpPC)
1) Pops a pointer from the stack 2) Load the value from the pointer 3) Writes the value decreased by ...
Definition: Interp.h:808
bool GetThisField(InterpState &S, CodePtr OpPC, uint32_t I)
Definition: Interp.h:1267
bool NarrowPtr(InterpState &S, CodePtr OpPC)
Definition: Interp.h:2412
bool RunDestructors(InterpState &S, CodePtr OpPC, const Block *B)
Definition: Interp.cpp:913
llvm::APInt APInt
Definition: Integral.h:29
bool GetMemberPtrBasePop(InterpState &S, CodePtr OpPC, int32_t Off)
Definition: Interp.h:1591
bool InitThisField(InterpState &S, CodePtr OpPC, uint32_t I)
Definition: Interp.h:1381
bool Interpret(InterpState &S, APValue &Result)
Interpreter entry point.
Definition: Interp.cpp:963
Floating ReadArg< Floating >(InterpState &S, CodePtr &OpPC)
Definition: Interp.h:3098
static bool ZeroIntAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth)
Definition: Interp.h:2253
bool Incf(InterpState &S, CodePtr OpPC, llvm::RoundingMode RM)
Definition: Interp.h:836
bool GetParam(InterpState &S, CodePtr OpPC, uint32_t I)
Definition: Interp.h:1204
bool InitElemPop(InterpState &S, CodePtr OpPC, uint32_t Idx)
The same as InitElem, but pops the pointer as well.
Definition: Interp.h:1787
bool StoreBitField(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1716
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:406
bool LoadPop(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1677
bool DecfPop(InterpState &S, CodePtr OpPC, llvm::RoundingMode RM)
Definition: Interp.h:860
static bool CastFloatingIntegralAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth)
Definition: Interp.h:2149
static bool IncPtr(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1970
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:833
bool Dup(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1145
bool CheckCallDepth(InterpState &S, CodePtr OpPC)
Checks if calling the currently active function would exceed the allowed call depth.
Definition: Interp.cpp:694
bool CheckThis(InterpState &S, CodePtr OpPC, const Pointer &This)
Checks the 'this' pointer.
Definition: Interp.cpp:705
bool SetField(InterpState &S, CodePtr OpPC, uint32_t I)
Definition: Interp.h:1235
bool CheckNonNullArg(InterpState &S, CodePtr OpPC)
Definition: Interp.h:2872
bool InterpretOffsetOf(InterpState &S, CodePtr OpPC, const OffsetOfExpr *E, llvm::ArrayRef< int64_t > ArrayIndices, int64_t &Result)
Interpret an offsetof operation.
bool SetThreeWayComparisonField(InterpState &S, CodePtr OpPC, const Pointer &Ptr, const APSInt &IntValue)
Sets the given integral value to the pointer, which is of a std::{weak,partial,strong}_ordering type.
static bool IncDecPtrHelper(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Definition: Interp.h:1946
bool GetPtrLocal(InterpState &S, CodePtr OpPC, uint32_t I)
Definition: Interp.h:1440
static bool CastFloatingIntegralAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth)
Definition: Interp.h:2129
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:196
bool CheckConstant(InterpState &S, CodePtr OpPC, const Descriptor *Desc)
Checks if the Descriptor is of a constexpr or const global variable.
Definition: Interp.cpp:323
bool CheckDecl(InterpState &S, CodePtr OpPC, const VarDecl *VD)
Definition: Interp.h:2916
bool AddSubMulHelper(InterpState &S, CodePtr OpPC, unsigned Bits, const T &LHS, const T &RHS)
Definition: Interp.h:333
bool GetPtrField(InterpState &S, CodePtr OpPC, uint32_t Off)
1) Peeks a Pointer 2) Pushes Pointer.atField(Off) on the stack
Definition: Interp.h:1460
bool Div(InterpState &S, CodePtr OpPC)
1) Pops the RHS from the stack.
Definition: Interp.h:627
bool CheckMutable(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Checks if a pointer points to a mutable field.
Definition: Interp.cpp:454
bool GetFnPtr(InterpState &S, CodePtr OpPC, const Function *Func)
Definition: Interp.h:2760
bool GetGlobalUnchecked(InterpState &S, CodePtr OpPC, uint32_t I)
Same as GetGlobal, but without the checks.
Definition: Interp.h:1314
bool GetPtrActiveThisField(InterpState &S, CodePtr OpPC, uint32_t Off)
Definition: Interp.h:1539
bool SubPtr(InterpState &S, CodePtr OpPC)
1) Pops a Pointer from the stack.
Definition: Interp.h:1992
bool CheckSubobject(InterpState &S, CodePtr OpPC, const Pointer &Ptr, CheckSubobjectKind CSK)
Checks if Ptr is a one-past-the-end pointer.
Definition: Interp.cpp:395
static bool CastPointerIntegralAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth)
Definition: Interp.h:2200
bool GetPtrGlobal(InterpState &S, CodePtr OpPC, uint32_t I)
Definition: Interp.h:1453
bool Mulc(InterpState &S, CodePtr OpPC)
Definition: Interp.h:426
bool ArrayElemPtr(InterpState &S, CodePtr OpPC)
Definition: Interp.h:2433
bool NE(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1090
bool NoRet(InterpState &S, CodePtr OpPC)
Definition: Interp.h:2402
bool GetIntPtr(InterpState &S, CodePtr OpPC, const Descriptor *Desc)
Definition: Interp.h:2767
bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr, AccessKinds AK)
Checks if a value can be loaded from a block.
Definition: Interp.cpp:536
static bool ZeroIntAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth)
Definition: Interp.h:2248
bool Addf(InterpState &S, CodePtr OpPC, llvm::RoundingMode RM)
Definition: Interp.h:379
bool Shl(InterpState &S, CodePtr OpPC)
Definition: Interp.h:2389
bool RVOPtr(InterpState &S, CodePtr OpPC)
Definition: Interp.h:2292
bool CheckInitialized(InterpState &S, CodePtr OpPC, const Pointer &Ptr, AccessKinds AK)
Definition: Interp.cpp:493
bool CastPointerIntegral(InterpState &S, CodePtr OpPC)
Definition: Interp.h:2170
constexpr bool isPtrType(PrimType T)
Definition: PrimType.h:51
bool SubOffset(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1939
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition: PrimType.h:126
bool BitXor(InterpState &S, CodePtr OpPC)
1) Pops the RHS from the stack.
Definition: Interp.h:590
bool CheckRange(InterpState &S, CodePtr OpPC, const Pointer &Ptr, AccessKinds AK)
Checks if a pointer is in range.
Definition: Interp.cpp:375
bool Ret(InterpState &S, CodePtr &PC, APValue &Result)
Definition: Interp.h:276
bool CastAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth)
Definition: Interp.h:2079
bool ExpandPtr(InterpState &S, CodePtr OpPC)
Definition: Interp.h:2418
bool CheckPure(InterpState &S, CodePtr OpPC, const CXXMethodDecl *MD)
Checks if a method is pure virtual.
Definition: Interp.cpp:723
bool Store(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1688
bool Divc(InterpState &S, CodePtr OpPC)
Definition: Interp.h:481
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:1221
bool ArrayElemPtrPop(InterpState &S, CodePtr OpPC)
Definition: Interp.h:2449
bool This(InterpState &S, CodePtr OpPC)
Definition: Interp.h:2269
bool InitScope(InterpState &S, CodePtr OpPC, uint32_t I)
Definition: Interp.h:2043
bool Mulf(InterpState &S, CodePtr OpPC, llvm::RoundingMode RM)
Definition: Interp.h:415
bool CheckDynamicMemoryAllocation(InterpState &S, CodePtr OpPC)
Checks if dynamic memory allocation is available in the current language mode.
Definition: Interp.cpp:779
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:1416
bool IncDecFloatHelper(InterpState &S, CodePtr OpPC, const Pointer &Ptr, llvm::RoundingMode RM)
Definition: Interp.h:817
bool CopyArray(InterpState &S, CodePtr OpPC, uint32_t SrcIndex, uint32_t DestIndex, uint32_t Size)
Definition: Interp.h:2487
bool CmpHelperEQ(InterpState &S, CodePtr OpPC, CompareFn Fn)
Definition: Interp.h:901
llvm::function_ref< bool(ComparisonCategoryResult)> CompareFn
Definition: Interp.h:886
T ReadArg(InterpState &S, CodePtr &OpPC)
Definition: Interp.h:3089
bool CheckLive(InterpState &S, CodePtr OpPC, const Pointer &Ptr, AccessKinds AK)
Checks if a pointer is live and accessible.
Definition: Interp.cpp:293
bool ArrayDecay(InterpState &S, CodePtr OpPC)
Just takes a pointer and checks if it's an incomplete array type.
Definition: Interp.h:2507
bool CheckFinalLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
This is not used by any of the opcodes directly.
Definition: Interp.cpp:564
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:1362
bool IncDecHelper(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Definition: Interp.h:713
bool CastFloatingIntegral(InterpState &S, CodePtr OpPC)
Definition: Interp.h:2100
bool GetLocal(InterpState &S, CodePtr OpPC, uint32_t I)
Definition: Interp.h:1186
bool OffsetOf(InterpState &S, CodePtr OpPC, const OffsetOfExpr *E)
Definition: Interp.h:2857
bool BitAnd(InterpState &S, CodePtr OpPC)
1) Pops the RHS from the stack.
Definition: Interp.h:556
void diagnoseEnumValue(InterpState &S, CodePtr OpPC, const EnumDecl *ED, const APSInt &Value)
Definition: Interp.cpp:938
bool CastIntegralFloating(InterpState &S, CodePtr OpPC, const llvm::fltSemantics *Sem, llvm::RoundingMode RM)
Definition: Interp.h:2086
bool LE(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1104
PrimType
Enumeration of the primitive types of the VM.
Definition: PrimType.h:33
bool Zero(InterpState &S, CodePtr OpPC)
Definition: Interp.h:2243
bool InitThisBitField(InterpState &S, CodePtr OpPC, const Record::Field *F, uint32_t FieldOffset)
Definition: Interp.h:1397
bool Unsupported(InterpState &S, CodePtr OpPC)
Definition: Interp.h:2805
bool CheckStore(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Checks if a value can be stored in a block.
Definition: Interp.cpp:587
bool CheckNull(InterpState &S, CodePtr OpPC, const Pointer &Ptr, CheckSubobjectKind CSK)
Checks if a pointer is null.
Definition: Interp.cpp:364
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:814
bool CastFP(InterpState &S, CodePtr OpPC, const llvm::fltSemantics *Sem, llvm::RoundingMode RM)
1) Pops a Floating from the stack.
Definition: Interp.h:2061
ComparisonCategoryResult Compare(const T &X, const T &Y)
Helper to compare two comparable types.
Definition: Primitives.h:25
bool SetThisField(InterpState &S, CodePtr OpPC, uint32_t I)
Definition: Interp.h:1281
bool StoreBitFieldPop(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1731
bool CallVar(InterpState &S, CodePtr OpPC, const Function *Func, uint32_t VarArgSize)
Definition: Interp.h:2529
static bool DecPtr(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1979
bool Alloc(InterpState &S, CodePtr OpPC, const Descriptor *Desc)
Definition: Interp.h:2935
bool ToMemberPtr(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1809
bool Rem(InterpState &S, CodePtr OpPC)
1) Pops the RHS from the stack.
Definition: Interp.h:607
bool VirtBaseHelper(InterpState &S, CodePtr OpPC, const RecordDecl *Decl, const Pointer &Ptr)
Definition: Interp.h:1630
bool IncfPop(InterpState &S, CodePtr OpPC, llvm::RoundingMode RM)
Definition: Interp.h:844
bool Dump(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1625
bool SizelessVectorElementSize(InterpState &S, CodePtr OpPC)
Definition: Interp.h:2835
bool Null(InterpState &S, CodePtr OpPC, const Descriptor *Desc)
Definition: Interp.h:2259
static bool CastPointerIntegralAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth)
Definition: Interp.h:2184
static bool PtrPtrCast(InterpState &S, CodePtr OpPC, bool SrcIsVoidPtr)
Definition: Interp.h:2216
bool CheckLiteralType(InterpState &S, CodePtr OpPC, const Type *T)
Definition: Interp.h:3052
bool GetPtrThisField(InterpState &S, CodePtr OpPC, uint32_t Off)
Definition: Interp.h:1516
bool GetPtrActiveField(InterpState &S, CodePtr OpPC, uint32_t Off)
Definition: Interp.h:1526
bool CheckArray(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Checks if the array is offsetable.
Definition: Interp.cpp:285
bool Decf(InterpState &S, CodePtr OpPC, llvm::RoundingMode RM)
Definition: Interp.h:852
bool CheckGlobalInitialized(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Check if a global variable is initialized.
Definition: Interp.cpp:519
bool GetPtrDerivedPop(InterpState &S, CodePtr OpPC, uint32_t Off)
Definition: Interp.h:1552
bool CheckNonNullArgs(InterpState &S, CodePtr OpPC, const Function *F, const CallExpr *CE, unsigned ArgSize)
Checks if all the arguments annotated as 'nonnull' are in fact not null.
Definition: Interp.cpp:859
bool GetPtrBase(InterpState &S, CodePtr OpPC, uint32_t Off)
Definition: Interp.h:1565
bool SetParam(InterpState &S, CodePtr OpPC, uint32_t I)
Definition: Interp.h:1213
bool CheckDummy(InterpState &S, CodePtr OpPC, const Pointer &Ptr, AccessKinds AK)
Checks if a pointer is a dummy pointer.
Definition: Interp.cpp:838
bool GetMemberPtrDecl(InterpState &S, CodePtr OpPC)
Definition: Interp.h:2786
bool CmpHelper< FunctionPointer >(InterpState &S, CodePtr OpPC, CompareFn Fn)
Function pointers cannot be compared in an ordered way.
Definition: Interp.h:907
bool Comp(InterpState &S, CodePtr OpPC)
1) Pops the value from the stack.
Definition: Interp.h:871
bool DecayPtr(InterpState &S, CodePtr OpPC)
OldPtr -> Integer -> NewPtr.
Definition: Interp.h:2899
bool GetPtrVirtBasePop(InterpState &S, CodePtr OpPC, const RecordDecl *D)
Definition: Interp.h:1641
bool StorePop(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1702
void cleanupAfterFunctionCall(InterpState &S, CodePtr OpPC)
Definition: Interp.cpp:219
bool CallBI(InterpState &S, CodePtr &PC, const Function *Func, const CallExpr *CE)
Definition: Interp.h:2702
bool SetLocal(InterpState &S, CodePtr OpPC, uint32_t I)
1) Pops the value from the stack.
Definition: Interp.h:1198
bool FinishInit(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1616
bool Mul(InterpState &S, CodePtr OpPC)
Definition: Interp.h:408
bool DoShift(InterpState &S, CodePtr OpPC, LT &LHS, RT &RHS)
Definition: Interp.h:2306
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:1773
bool Destroy(InterpState &S, CodePtr OpPC, uint32_t I)
Definition: Interp.h:2038
bool Pop(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1151
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:1426
bool GetMemberPtr(InterpState &S, CodePtr OpPC, const Decl *D)
Definition: Interp.h:2774
bool InvalidDeclRef(InterpState &S, CodePtr OpPC, const DeclRefExpr *DR)
Definition: Interp.h:2829
bool FinishInitPop(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1607
bool ReturnValue(const InterpState &S, const T &V, APValue &R)
Convert a value to an APValue.
Definition: Interp.h:43
bool InRange(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1131
bool CmpHelperEQ< FunctionPointer >(InterpState &S, CodePtr OpPC, CompareFn Fn)
Definition: Interp.h:920
bool Neg(InterpState &S, CodePtr OpPC)
Definition: Interp.h:671
bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F, const CallExpr *Call)
Interpret a builtin function.
llvm::APSInt APSInt
Definition: Floating.h:24
bool CheckExtern(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Checks if the variable has externally defined storage.
Definition: Interp.cpp:270
bool Divf(InterpState &S, CodePtr OpPC, llvm::RoundingMode RM)
Definition: Interp.h:643
bool BitOr(InterpState &S, CodePtr OpPC)
1) Pops the RHS from the stack.
Definition: Interp.h:573
bool Inv(InterpState &S, CodePtr OpPC)
Definition: Interp.h:660
bool Load(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1666
bool SetGlobal(InterpState &S, CodePtr OpPC, uint32_t I)
Definition: Interp.h:1323
bool Cast(InterpState &S, CodePtr OpPC)
Definition: Interp.h:2052
bool EQ(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1059
bool CheckNewDeleteForms(InterpState &S, CodePtr OpPC, bool NewWasArray, bool DeleteIsArray, const Descriptor *D, const Expr *NewExpr)
Diagnose mismatched new[]/delete or new/delete[] pairs.
Definition: Interp.cpp:788
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:1253
bool CmpHelperEQ< MemberPointer >(InterpState &S, CodePtr OpPC, CompareFn Fn)
Definition: Interp.h:1018
bool CheckCallable(InterpState &S, CodePtr OpPC, const Function *F)
Checks if a method can be called.
Definition: Interp.cpp:623
bool AddOffset(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1932
bool Const(InterpState &S, CodePtr OpPC, const T &Arg)
Definition: Interp.h:1176
bool DoMemcpy(InterpState &S, CodePtr OpPC, const Pointer &Src, Pointer &Dest)
Copy the contents of Src into Dest.
bool Memcpy(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1799
bool GE(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1119
bool CheckArraySize(InterpState &S, CodePtr OpPC, SizeT *NumElements, unsigned ElemSize, bool IsNoThrow)
Definition: Interp.h:223
bool CallPtr(InterpState &S, CodePtr OpPC, uint32_t ArgSize, const CallExpr *CE)
Definition: Interp.h:2717
bool CmpHelperEQ< Pointer >(InterpState &S, CodePtr OpPC, CompareFn Fn)
Definition: Interp.h:960
constexpr bool isIntegralType(PrimType T)
Definition: PrimType.h:72
bool CallVirt(InterpState &S, CodePtr OpPC, const Function *Func, uint32_t VarArgSize)
Definition: Interp.h:2628
bool CmpHelper(InterpState &S, CodePtr OpPC, CompareFn Fn)
Definition: Interp.h:889
bool CheckConst(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Checks if a pointer points to const storage.
Definition: Interp.cpp:426
bool GetPtrParam(InterpState &S, CodePtr OpPC, uint32_t I)
Definition: Interp.h:1445
bool AllocCN(InterpState &S, CodePtr OpPC, const Descriptor *ElementDesc, bool IsNoThrow)
Definition: Interp.h:2976
bool GetGlobal(InterpState &S, CodePtr OpPC, uint32_t I)
Definition: Interp.h:1296
bool GetPtrThisVirtBase(InterpState &S, CodePtr OpPC, const RecordDecl *D)
Definition: Interp.h:1650
bool InitGlobal(InterpState &S, CodePtr OpPC, uint32_t I)
Definition: Interp.h:1329
bool InvalidCast(InterpState &S, CodePtr OpPC, CastKind Kind, bool Fatal)
Same here, but only for casts.
Definition: Interp.h:2816
bool CastMemberPtrPtr(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1817
bool RetVoid(InterpState &S, CodePtr &PC, APValue &Result)
Definition: Interp.h:310
bool Flip(InterpState &S, CodePtr OpPC)
[Value1, Value2] -> [Value2, Value1]
Definition: Interp.h:1158
bool CMP3(InterpState &S, CodePtr OpPC, const ComparisonCategoryInfo *CmpInfo)
Definition: Interp.h:1066
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:2072
bool CmpHelper< Pointer >(InterpState &S, CodePtr OpPC, CompareFn Fn)
Definition: Interp.h:940
bool Assume(InterpState &S, CodePtr OpPC)
Definition: Interp.h:2844
bool CheckInvoke(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Checks if a method can be invoked on an object.
Definition: Interp.cpp:603
bool GetPtrThisBase(InterpState &S, CodePtr OpPC, uint32_t Off)
Definition: Interp.h:1597
static bool Free(InterpState &S, CodePtr OpPC, bool DeleteIsArrayForm)
Definition: Interp.h:3003
bool CheckFloatResult(InterpState &S, CodePtr OpPC, const Floating &Result, APFloat::opStatus Status)
Checks if the result of a floating-point operation is valid in the current context.
Definition: Interp.cpp:732
bool OffsetHelper(InterpState &S, CodePtr OpPC, const T &Offset, const Pointer &Ptr)
Definition: Interp.h:1832
bool AllocN(InterpState &S, CodePtr OpPC, PrimType T, const Expr *Source, bool IsNoThrow)
Definition: Interp.h:2951
bool CheckEnumValue(InterpState &S, CodePtr OpPC, const EnumDecl *ED)
Definition: Interp.h:2887
The JSON file list parser is used to communicate input to InstallAPI.
@ TSCS_unspecified
Definition: Specifiers.h:236
ComparisonCategoryResult
An enumeration representing the possible results of a three-way comparison.
CheckSubobjectKind
The order of this enum is important for diagnostics.
Definition: State.h:40
@ CSK_ArrayToPointer
Definition: State.h:44
@ CSK_Derived
Definition: State.h:42
@ CSK_Base
Definition: State.h:41
@ CSK_ArrayIndex
Definition: State.h:45
@ CSK_Field
Definition: State.h:43
@ Result
The result type of a method or function.
AccessKinds
Kinds of access we can perform on an object, for diagnostics.
Definition: State.h:26
@ AK_Increment
Definition: State.h:30
@ AK_Read
Definition: State.h:27
@ AK_Decrement
Definition: State.h:31
const FunctionProtoType * T
#define bool
Definition: stdbool.h:24
unsigned Base
Start of the current subfield.
Definition: Pointer.h:41
Block * Pointee
The block the pointer is pointing to.
Definition: Pointer.h:39
Describes a memory block created by an allocation site.
Definition: Descriptor.h:111
unsigned getSize() const
Returns the size of the object without metadata.
Definition: Descriptor.h:219
static constexpr unsigned MaxArrayElemBytes
Maximum number of bytes to be used for array elements.
Definition: Descriptor.h:136
const Expr * asExpr() const
Definition: Descriptor.h:199
Inline descriptor embedded in structures and arrays.
Definition: Descriptor.h:69
IntPointer atOffset(const ASTContext &ASTCtx, unsigned Offset) const
Definition: Pointer.cpp:633
const Descriptor * Desc
Definition: Pointer.h:45
Mapping from primitive types to their representation.
Definition: PrimType.h:75