1 //===--- Interp.h - Interpreter for the constexpr VM ------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // Definition of the interpreter state and entry point.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_AST_INTERP_INTERP_H
14 #define LLVM_CLANG_AST_INTERP_INTERP_H
19 #include "InterpFrame.h"
20 #include "InterpStack.h"
21 #include "InterpState.h"
26 #include "clang/AST/ASTContext.h"
27 #include "clang/AST/ASTDiagnostic.h"
28 #include "clang/AST/CXXInheritance.h"
29 #include "clang/AST/Expr.h"
30 #include "llvm/ADT/APFloat.h"
31 #include "llvm/ADT/APSInt.h"
32 #include "llvm/Support/Endian.h"
37 using APInt = llvm::APInt;
38 using APSInt = llvm::APSInt;
40 /// Convers a value to an APValue.
41 template <typename T> bool ReturnValue(const T &V, APValue &R) {
46 /// Checks if the variable has externally defined storage.
47 bool CheckExtern(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
49 /// Checks if the array is offsetable.
50 bool CheckArray(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
52 /// Checks if a pointer is live and accesible.
53 bool CheckLive(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
55 /// Checks if a pointer is null.
56 bool CheckNull(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
57 CheckSubobjectKind CSK);
59 /// Checks if a pointer is in range.
60 bool CheckRange(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
63 /// Checks if a field from which a pointer is going to be derived is valid.
64 bool CheckRange(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
65 CheckSubobjectKind CSK);
67 /// Checks if a pointer points to const storage.
68 bool CheckConst(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
70 /// Checks if a pointer points to a mutable field.
71 bool CheckMutable(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
73 /// Checks if a value can be loaded from a block.
74 bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
76 /// Checks if a value can be stored in a block.
77 bool CheckStore(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
79 /// Checks if a method can be invoked on an object.
80 bool CheckInvoke(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
82 /// Checks if a value can be initialized.
83 bool CheckInit(InterpState &S, CodePtr OpPC, const Pointer &Ptr);
85 /// Checks if a method can be called.
86 bool CheckCallable(InterpState &S, CodePtr OpPC, Function *F);
88 /// Checks the 'this' pointer.
89 bool CheckThis(InterpState &S, CodePtr OpPC, const Pointer &This);
91 /// Checks if a method is pure virtual.
92 bool CheckPure(InterpState &S, CodePtr OpPC, const CXXMethodDecl *MD);
94 template <typename T> inline bool IsTrue(const T &V) { return !V.isZero(); }
96 //===----------------------------------------------------------------------===//
98 //===----------------------------------------------------------------------===//
100 template <typename T, bool (*OpFW)(T, T, unsigned, T *),
101 template <typename U> class OpAP>
102 bool AddSubMulHelper(InterpState &S, CodePtr OpPC, unsigned Bits, const T &LHS,
104 // Fast path - add the numbers with fixed width.
106 if (!OpFW(LHS, RHS, Bits, &Result)) {
107 S.Stk.push<T>(Result);
111 // If for some reason evaluation continues, use the truncated results.
112 S.Stk.push<T>(Result);
114 // Slow path - compute the result using another bit of precision.
115 APSInt Value = OpAP<APSInt>()(LHS.toAPSInt(Bits), RHS.toAPSInt(Bits));
117 // Report undefined behaviour, stopping if required.
118 const Expr *E = S.Current->getExpr(OpPC);
119 QualType Type = E->getType();
120 if (S.checkingForUndefinedBehavior()) {
121 auto Trunc = Value.trunc(Result.bitWidth()).toString(10);
122 auto Loc = E->getExprLoc();
123 S.report(Loc, diag::warn_integer_constant_overflow) << Trunc << Type;
126 S.CCEDiag(E, diag::note_constexpr_overflow) << Value << Type;
127 return S.noteUndefinedBehavior();
131 template <PrimType Name, class T = typename PrimConv<Name>::T>
132 bool Add(InterpState &S, CodePtr OpPC) {
133 const T &RHS = S.Stk.pop<T>();
134 const T &LHS = S.Stk.pop<T>();
135 const unsigned Bits = RHS.bitWidth() + 1;
136 return AddSubMulHelper<T, T::add, std::plus>(S, OpPC, Bits, LHS, RHS);
139 template <PrimType Name, class T = typename PrimConv<Name>::T>
140 bool Sub(InterpState &S, CodePtr OpPC) {
141 const T &RHS = S.Stk.pop<T>();
142 const T &LHS = S.Stk.pop<T>();
143 const unsigned Bits = RHS.bitWidth() + 1;
144 return AddSubMulHelper<T, T::sub, std::minus>(S, OpPC, Bits, LHS, RHS);
147 template <PrimType Name, class T = typename PrimConv<Name>::T>
148 bool Mul(InterpState &S, CodePtr OpPC) {
149 const T &RHS = S.Stk.pop<T>();
150 const T &LHS = S.Stk.pop<T>();
151 const unsigned Bits = RHS.bitWidth() * 2;
152 return AddSubMulHelper<T, T::mul, std::multiplies>(S, OpPC, Bits, LHS, RHS);
155 //===----------------------------------------------------------------------===//
156 // EQ, NE, GT, GE, LT, LE
157 //===----------------------------------------------------------------------===//
159 using CompareFn = llvm::function_ref<bool(ComparisonCategoryResult)>;
161 template <typename T>
162 bool CmpHelper(InterpState &S, CodePtr OpPC, CompareFn Fn) {
163 using BoolT = PrimConv<PT_Bool>::T;
164 const T &RHS = S.Stk.pop<T>();
165 const T &LHS = S.Stk.pop<T>();
166 S.Stk.push<BoolT>(BoolT::from(Fn(LHS.compare(RHS))));
170 template <typename T>
171 bool CmpHelperEQ(InterpState &S, CodePtr OpPC, CompareFn Fn) {
172 return CmpHelper<T>(S, OpPC, Fn);
176 inline bool CmpHelper<Pointer>(InterpState &S, CodePtr OpPC, CompareFn Fn) {
177 using BoolT = PrimConv<PT_Bool>::T;
178 const Pointer &RHS = S.Stk.pop<Pointer>();
179 const Pointer &LHS = S.Stk.pop<Pointer>();
181 if (!Pointer::hasSameBase(LHS, RHS)) {
182 const SourceInfo &Loc = S.Current->getSource(OpPC);
183 S.FFDiag(Loc, diag::note_invalid_subexpr_in_const_expr);
186 unsigned VL = LHS.getByteOffset();
187 unsigned VR = RHS.getByteOffset();
188 S.Stk.push<BoolT>(BoolT::from(Fn(Compare(VL, VR))));
194 inline bool CmpHelperEQ<Pointer>(InterpState &S, CodePtr OpPC, CompareFn Fn) {
195 using BoolT = PrimConv<PT_Bool>::T;
196 const Pointer &RHS = S.Stk.pop<Pointer>();
197 const Pointer &LHS = S.Stk.pop<Pointer>();
199 if (LHS.isZero() && RHS.isZero()) {
200 S.Stk.push<BoolT>(BoolT::from(Fn(ComparisonCategoryResult::Equal)));
204 if (!Pointer::hasSameBase(LHS, RHS)) {
205 S.Stk.push<BoolT>(BoolT::from(Fn(ComparisonCategoryResult::Unordered)));
208 unsigned VL = LHS.getByteOffset();
209 unsigned VR = RHS.getByteOffset();
210 S.Stk.push<BoolT>(BoolT::from(Fn(Compare(VL, VR))));
215 template <PrimType Name, class T = typename PrimConv<Name>::T>
216 bool EQ(InterpState &S, CodePtr OpPC) {
217 return CmpHelperEQ<T>(S, OpPC, [](ComparisonCategoryResult R) {
218 return R == ComparisonCategoryResult::Equal;
222 template <PrimType Name, class T = typename PrimConv<Name>::T>
223 bool NE(InterpState &S, CodePtr OpPC) {
224 return CmpHelperEQ<T>(S, OpPC, [](ComparisonCategoryResult R) {
225 return R != ComparisonCategoryResult::Equal;
229 template <PrimType Name, class T = typename PrimConv<Name>::T>
230 bool LT(InterpState &S, CodePtr OpPC) {
231 return CmpHelper<T>(S, OpPC, [](ComparisonCategoryResult R) {
232 return R == ComparisonCategoryResult::Less;
236 template <PrimType Name, class T = typename PrimConv<Name>::T>
237 bool LE(InterpState &S, CodePtr OpPC) {
238 return CmpHelper<T>(S, OpPC, [](ComparisonCategoryResult R) {
239 return R == ComparisonCategoryResult::Less ||
240 R == ComparisonCategoryResult::Equal;
244 template <PrimType Name, class T = typename PrimConv<Name>::T>
245 bool GT(InterpState &S, CodePtr OpPC) {
246 return CmpHelper<T>(S, OpPC, [](ComparisonCategoryResult R) {
247 return R == ComparisonCategoryResult::Greater;
251 template <PrimType Name, class T = typename PrimConv<Name>::T>
252 bool GE(InterpState &S, CodePtr OpPC) {
253 return CmpHelper<T>(S, OpPC, [](ComparisonCategoryResult R) {
254 return R == ComparisonCategoryResult::Greater ||
255 R == ComparisonCategoryResult::Equal;
259 //===----------------------------------------------------------------------===//
261 //===----------------------------------------------------------------------===//
263 template <PrimType Name, class T = typename PrimConv<Name>::T>
264 bool InRange(InterpState &S, CodePtr OpPC) {
265 const T RHS = S.Stk.pop<T>();
266 const T LHS = S.Stk.pop<T>();
267 const T Value = S.Stk.pop<T>();
269 S.Stk.push<bool>(LHS <= Value && Value <= RHS);
273 //===----------------------------------------------------------------------===//
275 //===----------------------------------------------------------------------===//
277 template <PrimType Name, class T = typename PrimConv<Name>::T>
278 bool Dup(InterpState &S, CodePtr OpPC) {
279 S.Stk.push<T>(S.Stk.peek<T>());
283 template <PrimType Name, class T = typename PrimConv<Name>::T>
284 bool Pop(InterpState &S, CodePtr OpPC) {
289 //===----------------------------------------------------------------------===//
291 //===----------------------------------------------------------------------===//
293 template <PrimType Name, class T = typename PrimConv<Name>::T>
294 bool Const(InterpState &S, CodePtr OpPC, const T &Arg) {
299 //===----------------------------------------------------------------------===//
300 // Get/Set Local/Param/Global/This
301 //===----------------------------------------------------------------------===//
303 template <PrimType Name, class T = typename PrimConv<Name>::T>
304 bool GetLocal(InterpState &S, CodePtr OpPC, uint32_t I) {
305 S.Stk.push<T>(S.Current->getLocal<T>(I));
309 template <PrimType Name, class T = typename PrimConv<Name>::T>
310 bool SetLocal(InterpState &S, CodePtr OpPC, uint32_t I) {
311 S.Current->setLocal<T>(I, S.Stk.pop<T>());
315 template <PrimType Name, class T = typename PrimConv<Name>::T>
316 bool GetParam(InterpState &S, CodePtr OpPC, uint32_t I) {
317 if (S.checkingPotentialConstantExpression()) {
320 S.Stk.push<T>(S.Current->getParam<T>(I));
324 template <PrimType Name, class T = typename PrimConv<Name>::T>
325 bool SetParam(InterpState &S, CodePtr OpPC, uint32_t I) {
326 S.Current->setParam<T>(I, S.Stk.pop<T>());
330 template <PrimType Name, class T = typename PrimConv<Name>::T>
331 bool GetField(InterpState &S, CodePtr OpPC, uint32_t I) {
332 const Pointer &Obj = S.Stk.peek<Pointer>();
333 if (!CheckNull(S, OpPC, Obj, CSK_Field))
335 if (!CheckRange(S, OpPC, Obj, CSK_Field))
337 const Pointer &Field = Obj.atField(I);
338 if (!CheckLoad(S, OpPC, Field))
340 S.Stk.push<T>(Field.deref<T>());
344 template <PrimType Name, class T = typename PrimConv<Name>::T>
345 bool SetField(InterpState &S, CodePtr OpPC, uint32_t I) {
346 const T &Value = S.Stk.pop<T>();
347 const Pointer &Obj = S.Stk.peek<Pointer>();
348 if (!CheckNull(S, OpPC, Obj, CSK_Field))
350 if (!CheckRange(S, OpPC, Obj, CSK_Field))
352 const Pointer &Field = Obj.atField(I);
353 if (!CheckStore(S, OpPC, Field))
355 Field.deref<T>() = Value;
359 template <PrimType Name, class T = typename PrimConv<Name>::T>
360 bool GetFieldPop(InterpState &S, CodePtr OpPC, uint32_t I) {
361 const Pointer &Obj = S.Stk.pop<Pointer>();
362 if (!CheckNull(S, OpPC, Obj, CSK_Field))
364 if (!CheckRange(S, OpPC, Obj, CSK_Field))
366 const Pointer &Field = Obj.atField(I);
367 if (!CheckLoad(S, OpPC, Field))
369 S.Stk.push<T>(Field.deref<T>());
373 template <PrimType Name, class T = typename PrimConv<Name>::T>
374 bool GetThisField(InterpState &S, CodePtr OpPC, uint32_t I) {
375 if (S.checkingPotentialConstantExpression())
377 const Pointer &This = S.Current->getThis();
378 if (!CheckThis(S, OpPC, This))
380 const Pointer &Field = This.atField(I);
381 if (!CheckLoad(S, OpPC, Field))
383 S.Stk.push<T>(Field.deref<T>());
387 template <PrimType Name, class T = typename PrimConv<Name>::T>
388 bool SetThisField(InterpState &S, CodePtr OpPC, uint32_t I) {
389 if (S.checkingPotentialConstantExpression())
391 const T &Value = S.Stk.pop<T>();
392 const Pointer &This = S.Current->getThis();
393 if (!CheckThis(S, OpPC, This))
395 const Pointer &Field = This.atField(I);
396 if (!CheckStore(S, OpPC, Field))
398 Field.deref<T>() = Value;
402 template <PrimType Name, class T = typename PrimConv<Name>::T>
403 bool GetGlobal(InterpState &S, CodePtr OpPC, uint32_t I) {
404 auto *B = S.P.getGlobal(I);
407 S.Stk.push<T>(B->deref<T>());
411 template <PrimType Name, class T = typename PrimConv<Name>::T>
412 bool SetGlobal(InterpState &S, CodePtr OpPC, uint32_t I) {
413 // TODO: emit warning.
417 template <PrimType Name, class T = typename PrimConv<Name>::T>
418 bool InitGlobal(InterpState &S, CodePtr OpPC, uint32_t I) {
419 S.P.getGlobal(I)->deref<T>() = S.Stk.pop<T>();
423 template <PrimType Name, class T = typename PrimConv<Name>::T>
424 bool InitThisField(InterpState &S, CodePtr OpPC, uint32_t I) {
425 if (S.checkingPotentialConstantExpression())
427 const Pointer &This = S.Current->getThis();
428 if (!CheckThis(S, OpPC, This))
430 const Pointer &Field = This.atField(I);
431 Field.deref<T>() = S.Stk.pop<T>();
436 template <PrimType Name, class T = typename PrimConv<Name>::T>
437 bool InitThisBitField(InterpState &S, CodePtr OpPC, const Record::Field *F) {
438 if (S.checkingPotentialConstantExpression())
440 const Pointer &This = S.Current->getThis();
441 if (!CheckThis(S, OpPC, This))
443 const Pointer &Field = This.atField(F->Offset);
444 const auto &Value = S.Stk.pop<T>();
445 Field.deref<T>() = Value.truncate(F->Decl->getBitWidthValue(S.getCtx()));
450 template <PrimType Name, class T = typename PrimConv<Name>::T>
451 bool InitThisFieldActive(InterpState &S, CodePtr OpPC, uint32_t I) {
452 if (S.checkingPotentialConstantExpression())
454 const Pointer &This = S.Current->getThis();
455 if (!CheckThis(S, OpPC, This))
457 const Pointer &Field = This.atField(I);
458 Field.deref<T>() = S.Stk.pop<T>();
464 template <PrimType Name, class T = typename PrimConv<Name>::T>
465 bool InitField(InterpState &S, CodePtr OpPC, uint32_t I) {
466 const T &Value = S.Stk.pop<T>();
467 const Pointer &Field = S.Stk.pop<Pointer>().atField(I);
468 Field.deref<T>() = Value;
474 template <PrimType Name, class T = typename PrimConv<Name>::T>
475 bool InitBitField(InterpState &S, CodePtr OpPC, const Record::Field *F) {
476 const T &Value = S.Stk.pop<T>();
477 const Pointer &Field = S.Stk.pop<Pointer>().atField(F->Offset);
478 Field.deref<T>() = Value.truncate(F->Decl->getBitWidthValue(S.getCtx()));
484 template <PrimType Name, class T = typename PrimConv<Name>::T>
485 bool InitFieldActive(InterpState &S, CodePtr OpPC, uint32_t I) {
486 const T &Value = S.Stk.pop<T>();
487 const Pointer &Ptr = S.Stk.pop<Pointer>();
488 const Pointer &Field = Ptr.atField(I);
489 Field.deref<T>() = Value;
495 //===----------------------------------------------------------------------===//
496 // GetPtr Local/Param/Global/Field/This
497 //===----------------------------------------------------------------------===//
499 inline bool GetPtrLocal(InterpState &S, CodePtr OpPC, uint32_t I) {
500 S.Stk.push<Pointer>(S.Current->getLocalPointer(I));
504 inline bool GetPtrParam(InterpState &S, CodePtr OpPC, uint32_t I) {
505 if (S.checkingPotentialConstantExpression()) {
508 S.Stk.push<Pointer>(S.Current->getParamPointer(I));
512 inline bool GetPtrGlobal(InterpState &S, CodePtr OpPC, uint32_t I) {
513 S.Stk.push<Pointer>(S.P.getPtrGlobal(I));
517 inline bool GetPtrField(InterpState &S, CodePtr OpPC, uint32_t Off) {
518 const Pointer &Ptr = S.Stk.pop<Pointer>();
519 if (!CheckNull(S, OpPC, Ptr, CSK_Field))
521 if (!CheckExtern(S, OpPC, Ptr))
523 if (!CheckRange(S, OpPC, Ptr, CSK_Field))
525 S.Stk.push<Pointer>(Ptr.atField(Off));
529 inline bool GetPtrThisField(InterpState &S, CodePtr OpPC, uint32_t Off) {
530 if (S.checkingPotentialConstantExpression())
532 const Pointer &This = S.Current->getThis();
533 if (!CheckThis(S, OpPC, This))
535 S.Stk.push<Pointer>(This.atField(Off));
539 inline bool GetPtrActiveField(InterpState &S, CodePtr OpPC, uint32_t Off) {
540 const Pointer &Ptr = S.Stk.pop<Pointer>();
541 if (!CheckNull(S, OpPC, Ptr, CSK_Field))
543 if (!CheckRange(S, OpPC, Ptr, CSK_Field))
545 Pointer Field = Ptr.atField(Off);
548 S.Stk.push<Pointer>(std::move(Field));
552 inline bool GetPtrActiveThisField(InterpState &S, CodePtr OpPC, uint32_t Off) {
553 if (S.checkingPotentialConstantExpression())
555 const Pointer &This = S.Current->getThis();
556 if (!CheckThis(S, OpPC, This))
558 Pointer Field = This.atField(Off);
561 S.Stk.push<Pointer>(std::move(Field));
565 inline bool GetPtrBase(InterpState &S, CodePtr OpPC, uint32_t Off) {
566 const Pointer &Ptr = S.Stk.pop<Pointer>();
567 if (!CheckNull(S, OpPC, Ptr, CSK_Base))
569 S.Stk.push<Pointer>(Ptr.atField(Off));
573 inline bool GetPtrThisBase(InterpState &S, CodePtr OpPC, uint32_t Off) {
574 if (S.checkingPotentialConstantExpression())
576 const Pointer &This = S.Current->getThis();
577 if (!CheckThis(S, OpPC, This))
579 S.Stk.push<Pointer>(This.atField(Off));
583 inline bool VirtBaseHelper(InterpState &S, CodePtr OpPC, const RecordDecl *Decl,
584 const Pointer &Ptr) {
586 while (Base.isBaseClass())
587 Base = Base.getBase();
589 auto *Field = Base.getRecord()->getVirtualBase(Decl);
590 S.Stk.push<Pointer>(Base.atField(Field->Offset));
594 inline bool GetPtrVirtBase(InterpState &S, CodePtr OpPC, const RecordDecl *D) {
595 const Pointer &Ptr = S.Stk.pop<Pointer>();
596 if (!CheckNull(S, OpPC, Ptr, CSK_Base))
598 return VirtBaseHelper(S, OpPC, D, Ptr);
601 inline bool GetPtrThisVirtBase(InterpState &S, CodePtr OpPC,
602 const RecordDecl *D) {
603 if (S.checkingPotentialConstantExpression())
605 const Pointer &This = S.Current->getThis();
606 if (!CheckThis(S, OpPC, This))
608 return VirtBaseHelper(S, OpPC, D, S.Current->getThis());
611 //===----------------------------------------------------------------------===//
613 //===----------------------------------------------------------------------===//
615 template <PrimType Name, class T = typename PrimConv<Name>::T>
616 bool Load(InterpState &S, CodePtr OpPC) {
617 const Pointer &Ptr = S.Stk.peek<Pointer>();
618 if (!CheckLoad(S, OpPC, Ptr))
620 S.Stk.push<T>(Ptr.deref<T>());
624 template <PrimType Name, class T = typename PrimConv<Name>::T>
625 bool LoadPop(InterpState &S, CodePtr OpPC) {
626 const Pointer &Ptr = S.Stk.pop<Pointer>();
627 if (!CheckLoad(S, OpPC, Ptr))
629 S.Stk.push<T>(Ptr.deref<T>());
633 template <PrimType Name, class T = typename PrimConv<Name>::T>
634 bool Store(InterpState &S, CodePtr OpPC) {
635 const T &Value = S.Stk.pop<T>();
636 const Pointer &Ptr = S.Stk.peek<Pointer>();
637 if (!CheckStore(S, OpPC, Ptr))
639 Ptr.deref<T>() = Value;
643 template <PrimType Name, class T = typename PrimConv<Name>::T>
644 bool StorePop(InterpState &S, CodePtr OpPC) {
645 const T &Value = S.Stk.pop<T>();
646 const Pointer &Ptr = S.Stk.pop<Pointer>();
647 if (!CheckStore(S, OpPC, Ptr))
649 Ptr.deref<T>() = Value;
653 template <PrimType Name, class T = typename PrimConv<Name>::T>
654 bool StoreBitField(InterpState &S, CodePtr OpPC) {
655 const T &Value = S.Stk.pop<T>();
656 const Pointer &Ptr = S.Stk.peek<Pointer>();
657 if (!CheckStore(S, OpPC, Ptr))
659 if (auto *FD = Ptr.getField()) {
660 Ptr.deref<T>() = Value.truncate(FD->getBitWidthValue(S.getCtx()));
662 Ptr.deref<T>() = Value;
667 template <PrimType Name, class T = typename PrimConv<Name>::T>
668 bool StoreBitFieldPop(InterpState &S, CodePtr OpPC) {
669 const T &Value = S.Stk.pop<T>();
670 const Pointer &Ptr = S.Stk.pop<Pointer>();
671 if (!CheckStore(S, OpPC, Ptr))
673 if (auto *FD = Ptr.getField()) {
674 Ptr.deref<T>() = Value.truncate(FD->getBitWidthValue(S.getCtx()));
676 Ptr.deref<T>() = Value;
681 template <PrimType Name, class T = typename PrimConv<Name>::T>
682 bool InitPop(InterpState &S, CodePtr OpPC) {
683 const T &Value = S.Stk.pop<T>();
684 const Pointer &Ptr = S.Stk.pop<Pointer>();
685 if (!CheckInit(S, OpPC, Ptr))
688 new (&Ptr.deref<T>()) T(Value);
692 template <PrimType Name, class T = typename PrimConv<Name>::T>
693 bool InitElem(InterpState &S, CodePtr OpPC, uint32_t Idx) {
694 const T &Value = S.Stk.pop<T>();
695 const Pointer &Ptr = S.Stk.peek<Pointer>().atIndex(Idx);
696 if (!CheckInit(S, OpPC, Ptr))
699 new (&Ptr.deref<T>()) T(Value);
703 template <PrimType Name, class T = typename PrimConv<Name>::T>
704 bool InitElemPop(InterpState &S, CodePtr OpPC, uint32_t Idx) {
705 const T &Value = S.Stk.pop<T>();
706 const Pointer &Ptr = S.Stk.pop<Pointer>().atIndex(Idx);
707 if (!CheckInit(S, OpPC, Ptr))
710 new (&Ptr.deref<T>()) T(Value);
714 //===----------------------------------------------------------------------===//
715 // AddOffset, SubOffset
716 //===----------------------------------------------------------------------===//
718 template <class T, bool Add> bool OffsetHelper(InterpState &S, CodePtr OpPC) {
719 // Fetch the pointer and the offset.
720 const T &Offset = S.Stk.pop<T>();
721 const Pointer &Ptr = S.Stk.pop<Pointer>();
722 if (!CheckNull(S, OpPC, Ptr, CSK_ArrayIndex))
724 if (!CheckRange(S, OpPC, Ptr, CSK_ArrayToPointer))
727 // Get a version of the index comparable to the type.
728 T Index = T::from(Ptr.getIndex(), Offset.bitWidth());
729 // A zero offset does not change the pointer, but in the case of an array
730 // it has to be adjusted to point to the first element instead of the array.
731 if (Offset.isZero()) {
732 S.Stk.push<Pointer>(Index.isZero() ? Ptr.atIndex(0) : Ptr);
735 // Arrays of unknown bounds cannot have pointers into them.
736 if (!CheckArray(S, OpPC, Ptr))
739 // Compute the largest index into the array.
740 unsigned MaxIndex = Ptr.getNumElems();
742 // Helper to report an invalid offset, computed as APSInt.
743 auto InvalidOffset = [&]() {
744 const unsigned Bits = Offset.bitWidth();
745 APSInt APOffset(Offset.toAPSInt().extend(Bits + 2), false);
746 APSInt APIndex(Index.toAPSInt().extend(Bits + 2), false);
747 APSInt NewIndex = Add ? (APIndex + APOffset) : (APIndex - APOffset);
748 S.CCEDiag(S.Current->getSource(OpPC), diag::note_constexpr_array_index)
750 << /*array*/ static_cast<int>(!Ptr.inArray())
751 << static_cast<unsigned>(MaxIndex);
755 // If the new offset would be negative, bail out.
756 if (Add && Offset.isNegative() && (Offset.isMin() || -Offset > Index))
757 return InvalidOffset();
758 if (!Add && Offset.isPositive() && Index < Offset)
759 return InvalidOffset();
761 // If the new offset would be out of bounds, bail out.
762 unsigned MaxOffset = MaxIndex - Ptr.getIndex();
763 if (Add && Offset.isPositive() && Offset > MaxOffset)
764 return InvalidOffset();
765 if (!Add && Offset.isNegative() && (Offset.isMin() || -Offset > MaxOffset))
766 return InvalidOffset();
768 // Offset is valid - compute it on unsigned.
769 int64_t WideIndex = static_cast<int64_t>(Index);
770 int64_t WideOffset = static_cast<int64_t>(Offset);
771 int64_t Result = Add ? (WideIndex + WideOffset) : (WideIndex - WideOffset);
772 S.Stk.push<Pointer>(Ptr.atIndex(static_cast<unsigned>(Result)));
776 template <PrimType Name, class T = typename PrimConv<Name>::T>
777 bool AddOffset(InterpState &S, CodePtr OpPC) {
778 return OffsetHelper<T, true>(S, OpPC);
781 template <PrimType Name, class T = typename PrimConv<Name>::T>
782 bool SubOffset(InterpState &S, CodePtr OpPC) {
783 return OffsetHelper<T, false>(S, OpPC);
787 //===----------------------------------------------------------------------===//
789 //===----------------------------------------------------------------------===//
791 inline bool Destroy(InterpState &S, CodePtr OpPC, uint32_t I) {
792 S.Current->destroy(I);
796 //===----------------------------------------------------------------------===//
798 //===----------------------------------------------------------------------===//
800 template <PrimType TIn, PrimType TOut> bool Cast(InterpState &S, CodePtr OpPC) {
801 using T = typename PrimConv<TIn>::T;
802 using U = typename PrimConv<TOut>::T;
803 S.Stk.push<U>(U::from(S.Stk.pop<T>()));
807 //===----------------------------------------------------------------------===//
809 //===----------------------------------------------------------------------===//
811 template <PrimType Name, class T = typename PrimConv<Name>::T>
812 bool Zero(InterpState &S, CodePtr OpPC) {
813 S.Stk.push<T>(T::zero());
817 template <PrimType Name, class T = typename PrimConv<Name>::T>
818 inline bool Null(InterpState &S, CodePtr OpPC) {
823 //===----------------------------------------------------------------------===//
824 // This, ImplicitThis
825 //===----------------------------------------------------------------------===//
827 inline bool This(InterpState &S, CodePtr OpPC) {
828 // Cannot read 'this' in this mode.
829 if (S.checkingPotentialConstantExpression()) {
833 const Pointer &This = S.Current->getThis();
834 if (!CheckThis(S, OpPC, This))
837 S.Stk.push<Pointer>(This);
841 //===----------------------------------------------------------------------===//
843 //===----------------------------------------------------------------------===//
845 template <PrimType TR, PrimType TL, class T = typename PrimConv<TR>::T>
846 unsigned Trunc(InterpState &S, CodePtr OpPC, unsigned Bits, const T &V) {
847 // C++11 [expr.shift]p1: Shift width must be less than the bit width of
849 if (Bits > 1 && V >= T::from(Bits, V.bitWidth())) {
850 const Expr *E = S.Current->getExpr(OpPC);
851 const APSInt Val = V.toAPSInt();
852 QualType Ty = E->getType();
853 S.CCEDiag(E, diag::note_constexpr_large_shift) << Val << Ty << Bits;
856 return static_cast<unsigned>(V);
860 template <PrimType TL, PrimType TR, typename T = typename PrimConv<TL>::T>
861 inline bool ShiftRight(InterpState &S, CodePtr OpPC, const T &V, unsigned RHS) {
862 if (RHS >= V.bitWidth()) {
863 S.Stk.push<T>(T::from(0, V.bitWidth()));
865 S.Stk.push<T>(T::from(V >> RHS, V.bitWidth()));
870 template <PrimType TL, PrimType TR, typename T = typename PrimConv<TL>::T>
871 inline bool ShiftLeft(InterpState &S, CodePtr OpPC, const T &V, unsigned RHS) {
872 if (V.isSigned() && !S.getLangOpts().CPlusPlus2a) {
873 // C++11 [expr.shift]p2: A signed left shift must have a non-negative
874 // operand, and must not overflow the corresponding unsigned type.
875 // C++2a [expr.shift]p2: E1 << E2 is the unique value congruent to
876 // E1 x 2^E2 module 2^N.
877 if (V.isNegative()) {
878 const Expr *E = S.Current->getExpr(OpPC);
879 S.CCEDiag(E, diag::note_constexpr_lshift_of_negative) << V.toAPSInt();
880 } else if (V.countLeadingZeros() < RHS) {
881 S.CCEDiag(S.Current->getExpr(OpPC), diag::note_constexpr_lshift_discards);
885 if (V.bitWidth() == 1) {
887 } else if (RHS >= V.bitWidth()) {
888 S.Stk.push<T>(T::from(0, V.bitWidth()));
890 S.Stk.push<T>(T::from(V.toUnsigned() << RHS, V.bitWidth()));
895 template <PrimType TL, PrimType TR>
896 inline bool Shr(InterpState &S, CodePtr OpPC) {
897 const auto &RHS = S.Stk.pop<typename PrimConv<TR>::T>();
898 const auto &LHS = S.Stk.pop<typename PrimConv<TL>::T>();
899 const unsigned Bits = LHS.bitWidth();
901 if (RHS.isSigned() && RHS.isNegative()) {
902 const SourceInfo &Loc = S.Current->getSource(OpPC);
903 S.CCEDiag(Loc, diag::note_constexpr_negative_shift) << RHS.toAPSInt();
904 return ShiftLeft<TL, TR>(S, OpPC, LHS, Trunc<TR, TL>(S, OpPC, Bits, -RHS));
906 return ShiftRight<TL, TR>(S, OpPC, LHS, Trunc<TR, TL>(S, OpPC, Bits, RHS));
910 template <PrimType TL, PrimType TR>
911 inline bool Shl(InterpState &S, CodePtr OpPC) {
912 const auto &RHS = S.Stk.pop<typename PrimConv<TR>::T>();
913 const auto &LHS = S.Stk.pop<typename PrimConv<TL>::T>();
914 const unsigned Bits = LHS.bitWidth();
916 if (RHS.isSigned() && RHS.isNegative()) {
917 const SourceInfo &Loc = S.Current->getSource(OpPC);
918 S.CCEDiag(Loc, diag::note_constexpr_negative_shift) << RHS.toAPSInt();
919 return ShiftRight<TL, TR>(S, OpPC, LHS, Trunc<TR, TL>(S, OpPC, Bits, -RHS));
921 return ShiftLeft<TL, TR>(S, OpPC, LHS, Trunc<TR, TL>(S, OpPC, Bits, RHS));
925 //===----------------------------------------------------------------------===//
927 //===----------------------------------------------------------------------===//
929 inline bool NoRet(InterpState &S, CodePtr OpPC) {
930 SourceLocation EndLoc = S.Current->getCallee()->getEndLoc();
931 S.FFDiag(EndLoc, diag::note_constexpr_no_return);
935 //===----------------------------------------------------------------------===//
936 // NarrowPtr, ExpandPtr
937 //===----------------------------------------------------------------------===//
939 inline bool NarrowPtr(InterpState &S, CodePtr OpPC) {
940 const Pointer &Ptr = S.Stk.pop<Pointer>();
941 S.Stk.push<Pointer>(Ptr.narrow());
945 inline bool ExpandPtr(InterpState &S, CodePtr OpPC) {
946 const Pointer &Ptr = S.Stk.pop<Pointer>();
947 S.Stk.push<Pointer>(Ptr.expand());
951 /// Interpreter entry point.
952 bool Interpret(InterpState &S, APValue &Result);
954 } // namespace interp