clang 23.0.0git
Interp.cpp
Go to the documentation of this file.
1//===------- Interp.cpp - 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#include "Interp.h"
10#include "Compiler.h"
11#include "Function.h"
12#include "InterpFrame.h"
13#include "InterpShared.h"
14#include "InterpStack.h"
15#include "Opcode.h"
16#include "PrimType.h"
17#include "Program.h"
18#include "State.h"
21#include "clang/AST/DeclObjC.h"
22#include "clang/AST/Expr.h"
23#include "clang/AST/ExprCXX.h"
26#include "llvm/ADT/StringExtras.h"
27
28using namespace clang;
29using namespace clang::interp;
30
31#if __has_cpp_attribute(clang::musttail)
32#define MUSTTAIL [[clang::musttail]]
33#elif __has_cpp_attribute(msvc::musttail)
34#define MUSTTAIL [[msvc::musttail]]
35#elif __has_attribute(musttail)
36#define MUSTTAIL __attribute__((musttail))
37#endif
38
39// On MSVC, musttail does not guarantee tail calls in debug mode.
40// We disable it on MSVC generally since it doesn't seem to be able
41// to handle the way we use tailcalls.
42// PPC can't tail-call external calls, which is a problem for InterpNext.
43#if defined(_MSC_VER) || defined(__powerpc__) || !defined(MUSTTAIL) || \
44 defined(__i386__) || defined(__sparc__)
45#undef MUSTTAIL
46#define MUSTTAIL
47#define USE_TAILCALLS 0
48#else
49#define USE_TAILCALLS 1
50#endif
51
53 llvm::report_fatal_error("Interpreter cannot return values");
54}
55
56//===----------------------------------------------------------------------===//
57// Jmp, Jt, Jf
58//===----------------------------------------------------------------------===//
59
60static bool Jmp(InterpState &S, CodePtr OpPC, int32_t Offset) {
61 S.PC += Offset;
62 return S.noteStep(OpPC);
63}
64
65static bool Jt(InterpState &S, CodePtr OpPC, int32_t Offset) {
66 if (S.Stk.pop<bool>()) {
67 S.PC += Offset;
68 return S.noteStep(OpPC);
69 }
70 return true;
71}
72
73static bool Jf(InterpState &S, CodePtr OpPC, int32_t Offset) {
74 if (!S.Stk.pop<bool>()) {
75 S.PC += Offset;
76 return S.noteStep(OpPC);
77 }
78 return true;
79}
80
82 const ValueDecl *VD) {
83 const SourceInfo &E = S.Current->getSource(OpPC);
84 S.FFDiag(E, diag::note_constexpr_var_init_unknown, 1) << VD;
85 S.Note(VD->getLocation(), diag::note_declared_at) << VD->getSourceRange();
86}
87
88static void noteValueLocation(InterpState &S, const Block *B) {
89 const Descriptor *Desc = B->getDescriptor();
90
91 if (B->isDynamic())
92 S.Note(Desc->getLocation(), diag::note_constexpr_dynamic_alloc_here);
93 else if (B->isTemporary())
94 S.Note(Desc->getLocation(), diag::note_constexpr_temporary_here);
95 else
96 S.Note(Desc->getLocation(), diag::note_declared_at);
97}
98
100 const ValueDecl *VD,
101 AccessKinds AK = AK_Read);
103 const ValueDecl *D, AccessKinds AK = AK_Read) {
104 // This function tries pretty hard to produce a good diagnostic. Just skip
105 // that if nobody will see it anyway.
106 if (!S.diagnosing())
107 return false;
108
109 if (isa<ParmVarDecl>(D)) {
110 if (D->getType()->isReferenceType()) {
111 if (S.inConstantContext() && S.getLangOpts().CPlusPlus &&
112 !S.getLangOpts().CPlusPlus11) {
113 diagnoseNonConstVariable(S, OpPC, D);
114 return false;
115 }
116 }
117
118 const SourceInfo &Loc = S.Current->getSource(OpPC);
119 if (S.getLangOpts().CPlusPlus23 && D->getType()->isReferenceType()) {
120 S.FFDiag(Loc, diag::note_constexpr_access_unknown_variable, 1)
121 << AK_Read << D;
122 S.Note(D->getLocation(), diag::note_declared_at) << D->getSourceRange();
123 } else if (S.getLangOpts().CPlusPlus11) {
124 S.FFDiag(Loc, diag::note_constexpr_function_param_value_unknown, 1) << D;
125 S.Note(D->getLocation(), diag::note_declared_at) << D->getSourceRange();
126 } else {
127 S.FFDiag(Loc);
128 }
129 return false;
130 }
131
132 if (!D->getType().isConstQualified()) {
133 diagnoseNonConstVariable(S, OpPC, D, AK);
134 } else if (const auto *VD = dyn_cast<VarDecl>(D)) {
135 if (!VD->getAnyInitializer()) {
136 diagnoseMissingInitializer(S, OpPC, VD);
137 } else {
138 const SourceInfo &Loc = S.Current->getSource(OpPC);
139 S.FFDiag(Loc, diag::note_constexpr_var_init_non_constant, 1) << VD;
140 S.Note(VD->getLocation(), diag::note_declared_at);
141 }
142 }
143
144 return false;
145}
146
148 return AK == AK_Assign || AK == AK_Increment || AK == AK_Decrement ||
149 AK == AK_Construct || AK == AK_Destroy;
150}
151
153 const ValueDecl *VD, AccessKinds AK) {
154 if (!S.diagnosing())
155 return;
156
157 const SourceInfo &Loc = S.Current->getSource(OpPC);
158 if (!S.getLangOpts().CPlusPlus) {
159 S.FFDiag(Loc);
160 return;
161 }
162
163 if (const auto *VarD = dyn_cast<VarDecl>(VD);
164 VarD && VarD->getType().isConstQualified() &&
165 (VarD->isConstexpr() || !VarD->getType()->isArrayType()) &&
166 !VarD->getAnyInitializer()) {
167 diagnoseMissingInitializer(S, OpPC, VD);
168 return;
169 }
170
171 // Rather random, but this is to match the diagnostic output of the current
172 // interpreter.
173 if (isa<ObjCIvarDecl>(VD))
174 return;
175
177 if (isModification(AK)) {
178 S.FFDiag(Loc, diag::note_constexpr_modify_global);
179 } else {
180 S.FFDiag(Loc, diag::note_constexpr_ltor_non_const_int, 1) << VD;
181 S.Note(VD->getLocation(), diag::note_declared_at);
182 }
183 return;
184 }
185
186 S.FFDiag(Loc,
187 S.getLangOpts().CPlusPlus11 ? diag::note_constexpr_ltor_non_constexpr
188 : diag::note_constexpr_ltor_non_integral,
189 1)
190 << VD << VD->getType();
191 S.Note(VD->getLocation(), diag::note_declared_at);
192}
193
194static bool CheckTemporary(InterpState &S, CodePtr OpPC, const Block *B,
195 AccessKinds AK) {
196 if (B->getDeclID()) {
197 if (!(B->isStatic() && B->isTemporary()))
198 return true;
199
200 const auto *MTE = dyn_cast_if_present<MaterializeTemporaryExpr>(
201 B->getDescriptor()->asExpr());
202 if (!MTE)
203 return true;
204
205 // FIXME(perf): Since we do this check on every Load from a static
206 // temporary, it might make sense to cache the value of the
207 // isUsableInConstantExpressions call.
209 (B->getEvalID() != S.EvalID &&
210 !MTE->isUsableInConstantExpressions(S.getASTContext()))) {
211 const SourceInfo &E = S.Current->getSource(OpPC);
212 S.FFDiag(E, diag::note_constexpr_access_static_temporary, 1) << AK;
213 noteValueLocation(S, B);
214 return false;
215 }
216 }
217
218 return true;
219}
220
221static bool CheckGlobal(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
222 if (auto ID = Ptr.getDeclID()) {
223 if (!Ptr.isStatic())
224 return true;
225
226 if (S.P.getCurrentDecl() == ID)
227 return true;
228
229 S.FFDiag(S.Current->getLocation(OpPC), diag::note_constexpr_modify_global);
230 return false;
231 }
232 return true;
233}
234
235namespace clang {
236namespace interp {
237PRESERVE_NONE static bool BCP(InterpState &S, CodePtr OpPC, int32_t Offset,
238 PrimType PT);
239
241 const APSInt *Value, unsigned Bits) {
242 switch (Failure) {
244 assert(Value);
245 S.CCEDiag(S.Current->getSource(OpPC), diag::note_constexpr_negative_shift)
246 << *Value;
247 break;
249 assert(Value);
250 const Expr *E = S.Current->getExpr(OpPC);
251 S.CCEDiag(E, diag::note_constexpr_large_shift)
252 << *Value << E->getType() << Bits;
253 break;
254 }
256 assert(Value);
257 S.CCEDiag(S.Current->getExpr(OpPC), diag::note_constexpr_lshift_of_negative)
258 << *Value;
259 break;
261 S.CCEDiag(S.Current->getExpr(OpPC), diag::note_constexpr_lshift_discards);
262 break;
263 }
264 return S.noteUndefinedBehavior();
265}
266
268 assert(S.Current);
269 assert(Func);
270
271 // Pop variadic parameter values from the stack.
272 if (S.Current->Caller && Func->isVariadic()) {
273 unsigned VariadicArgSize =
275 unsigned TargetStackSize = S.Stk.size() - VariadicArgSize;
276 while (S.Stk.size() != TargetStackSize) {
277 S.Stk.discardSlow();
278 }
279 }
280
281 // And in any case, remove the fixed parameters (the non-variadic ones)
282 // at the end.
283 for (const Function::ParamDescriptor &PDesc : Func->args_reverse())
284 TYPE_SWITCH(PDesc.T, S.Stk.discard<T>());
285
286 if (Func->hasImplicitThisPointer())
287 S.Stk.discard<Pointer>();
288 if (Func->hasRVO())
289 S.Stk.discard<Pointer>();
290}
291
292bool isConstexprUnknown(const Block *B) {
293 if (B->isDummy())
294 return isa_and_nonnull<ParmVarDecl>(B->getDescriptor()->asValueDecl());
296}
297
299 if (!P.isBlockPointer() || P.isZero())
300 return false;
301 return isConstexprUnknown(P.block());
302}
303
304bool CheckBCPResult(InterpState &S, const Pointer &Ptr) {
305 if (Ptr.isDummy())
306 return false;
307 if (Ptr.isZero())
308 return true;
309 if (Ptr.isFunctionPointer())
310 return false;
311 if (Ptr.isIntegralPointer())
312 return true;
313 if (Ptr.isTypeidPointer())
314 return true;
315
316 if (Ptr.getType()->isAnyComplexType())
317 return true;
318
319 if (const Expr *Base = Ptr.getDeclDesc()->asExpr())
320 return isa<StringLiteral>(Base) && Ptr.getIndex() == 0;
321 return false;
322}
323
324bool CheckActive(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
325 AccessKinds AK, bool WillActivate) {
326 if (Ptr.isActive())
327 return true;
328
329 assert(Ptr.inUnion());
330
331 // Find the outermost union.
332 PtrView U = Ptr.view().getBase();
333 PtrView C = Ptr.view();
334 while (!U.isRoot() && !U.isActive()) {
335 // A little arbitrary, but this is what the current interpreter does.
336 // See the AnonymousUnion test in test/AST/ByteCode/unions.cpp.
337 // GCC's output is more similar to what we would get without
338 // this condition.
339 if (U.getRecord() && U.getRecord()->isAnonymousUnion())
340 break;
341
342 C = U;
343 U = U.getBase();
344 }
345 assert(C.isField());
346 assert(C.getBase() == U);
347
348 // Consider:
349 // union U {
350 // struct {
351 // int x;
352 // int y;
353 // } a;
354 // }
355 //
356 // When activating x, we will also activate a. If we now try to read
357 // from y, we will get to CheckActive, because y is not active. In that
358 // case, our U will be a (not a union). We return here and let later code
359 // handle this.
360 if (!U.getFieldDesc()->isUnion())
361 return true;
362
363 // When we will activate Ptr, check that none of the unions in its path have a
364 // non-trivial default constructor.
365 if (WillActivate) {
366 bool Fails = false;
367 PtrView It = Ptr.view();
368 while (!It.isRoot() && !It.isActive()) {
369 if (const Record *R = It.getRecord(); R && R->isUnion()) {
370 if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(R->getDecl());
371 CXXRD && !CXXRD->hasTrivialDefaultConstructor()) {
372 Fails = true;
373 break;
374 }
375 }
376 It = It.getBase();
377 }
378 if (!Fails)
379 return true;
380 }
381
382 // Get the inactive field descriptor.
383 assert(!C.isActive());
384 const FieldDecl *InactiveField = C.getField();
385 assert(InactiveField);
386
387 // Find the active field of the union.
388 const Record *R = U.getRecord();
389 assert(R && R->isUnion() && "Not a union");
390
391 const FieldDecl *ActiveField = nullptr;
392 for (const Record::Field &F : R->fields()) {
393 PtrView Field = U.atField(F.Offset);
394 if (Field.isActive()) {
395 ActiveField = Field.getField();
396 break;
397 }
398 }
399
400 S.FFDiag(S.Current->getSource(OpPC),
401 diag::note_constexpr_access_inactive_union_member)
402 << AK << InactiveField << !ActiveField << ActiveField;
403 return false;
404}
405
406bool CheckExtern(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
407 if (!Ptr.isExtern())
408 return true;
409
410 if (!Ptr.isPastEnd() &&
411 (Ptr.isInitialized() ||
412 (Ptr.getDeclDesc()->asVarDecl() == S.EvaluatingDecl)))
413 return true;
414
415 if (S.checkingPotentialConstantExpression() && S.getLangOpts().CPlusPlus &&
416 Ptr.isConst())
417 return false;
418
419 const auto *VD = Ptr.getDeclDesc()->asValueDecl();
421 diagnoseNonConstVariable(S, OpPC, VD);
422 return false;
423}
424
425bool CheckArray(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
426 if (!Ptr.isUnknownSizeArray())
427 return true;
428 const SourceInfo &E = S.Current->getSource(OpPC);
429 S.FFDiag(E, diag::note_constexpr_unsized_array_indexed);
430 return false;
431}
432
433bool CheckLive(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
434 AccessKinds AK) {
435 if (Ptr.isZero()) {
436 const auto &Src = S.Current->getSource(OpPC);
437
438 if (Ptr.isField())
439 S.FFDiag(Src, diag::note_constexpr_null_subobject) << CSK_Field;
440 else
441 S.FFDiag(Src, diag::note_constexpr_access_null) << AK;
442
443 return false;
444 }
445
446 if (!Ptr.isLive()) {
447 const auto &Src = S.Current->getSource(OpPC);
448
449 if (Ptr.isDynamic()) {
450 S.FFDiag(Src, diag::note_constexpr_access_deleted_object) << AK;
451 } else if (!S.checkingPotentialConstantExpression()) {
452 S.FFDiag(Src, diag::note_constexpr_access_uninit)
453 << AK << /*uninitialized=*/false << S.Current->getRange(OpPC);
454 noteValueLocation(S, Ptr.block());
455 }
456
457 return false;
458 }
459
460 return true;
461}
462
463bool CheckConstant(InterpState &S, CodePtr OpPC, const Descriptor *Desc,
464 AccessKinds AK) {
465 assert(Desc);
466
467 const auto *D = Desc->asVarDecl();
469 // If we're checking for a constant destructor for this variable, we can
470 // only read from it if it is constant.
471 if (D->getType().isConstQualified())
472 return true;
473 } else if (!D || D == S.EvaluatingDecl || D->isConstexpr())
474 return true;
475
476 // If we're evaluating the initializer for a constexpr variable in C23, we may
477 // only read other contexpr variables. Abort here since this one isn't
478 // constexpr.
479 if (const auto *VD = dyn_cast_if_present<VarDecl>(S.EvaluatingDecl);
480 VD && VD->isConstexpr() && S.getLangOpts().C23)
481 return Invalid(S, OpPC);
482
483 QualType T = D->getType();
484 bool IsConstant = T.isConstant(S.getASTContext());
485 if (T->isIntegralOrEnumerationType()) {
486 if (!IsConstant) {
487 diagnoseNonConstVariable(S, OpPC, D, AK);
488 return false;
489 }
490 return true;
491 }
492
493 if (IsConstant) {
494 if (S.getLangOpts().CPlusPlus) {
495 S.CCEDiag(S.Current->getLocation(OpPC),
496 S.getLangOpts().CPlusPlus11
497 ? diag::note_constexpr_ltor_non_constexpr
498 : diag::note_constexpr_ltor_non_integral,
499 1)
500 << D << T;
501 S.Note(D->getLocation(), diag::note_declared_at);
502 } else {
503 S.CCEDiag(S.Current->getLocation(OpPC));
504 }
505 return true;
506 }
507
508 if (T->isPointerOrReferenceType()) {
509 if (!T->getPointeeType().isConstant(S.getASTContext()) ||
510 !S.getLangOpts().CPlusPlus11) {
511 diagnoseNonConstVariable(S, OpPC, D, AK);
512 return false;
513 }
514 return true;
515 }
516
517 diagnoseNonConstVariable(S, OpPC, D, AK);
518 return false;
519}
520
521static bool CheckConstant(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
522 AccessKinds AK = AK_Read) {
524 return CheckConstant(S, OpPC, Ptr.getDeclDesc(), AK);
525
526 if (!Ptr.isStatic() || !Ptr.isBlockPointer())
527 return true;
528 if (!Ptr.getDeclID())
529 return true;
530 return CheckConstant(S, OpPC, Ptr.getDeclDesc(), AK);
531}
532
533bool CheckNull(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
534 CheckSubobjectKind CSK) {
535 if (!Ptr.isZero())
536 return true;
537 const SourceInfo &Loc = S.Current->getSource(OpPC);
538 S.FFDiag(Loc, diag::note_constexpr_null_subobject)
539 << CSK << S.Current->getRange(OpPC);
540
541 return false;
542}
543
545 if (!Ptr.isOnePastEnd() && !Ptr.isZeroSizeArray())
546 return true;
547 if (S.getLangOpts().CPlusPlus) {
548 const SourceInfo &Loc = S.Current->getSource(OpPC);
549 S.FFDiag(Loc, diag::note_constexpr_access_past_end)
550 << AK << S.Current->getRange(OpPC);
551 }
552 return false;
553}
554
555bool CheckRange(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
556 CheckSubobjectKind CSK) {
557 if (!Ptr.isElementPastEnd() && !Ptr.isZeroSizeArray())
558 return true;
559 const SourceInfo &Loc = S.Current->getSource(OpPC);
560 S.FFDiag(Loc, diag::note_constexpr_past_end_subobject)
561 << CSK << S.Current->getRange(OpPC);
562 return false;
563}
564
565bool CheckSubobject(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
566 CheckSubobjectKind CSK) {
567 if (!Ptr.isOnePastEnd())
568 return true;
569
570 const SourceInfo &Loc = S.Current->getSource(OpPC);
571 S.FFDiag(Loc, diag::note_constexpr_past_end_subobject)
572 << CSK << S.Current->getRange(OpPC);
573 return false;
574}
575
576bool CheckDowncast(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
577 uint32_t Offset) {
578 uint32_t MinOffset = Ptr.getDeclDesc()->getMetadataSize();
579 uint32_t PtrOffset = Ptr.getByteOffset();
580
581 // We subtract Offset from PtrOffset. The result must be at least
582 // MinOffset.
583 if (Offset < PtrOffset && (PtrOffset - Offset) >= MinOffset)
584 return true;
585
586 const auto *E = cast<CastExpr>(S.Current->getExpr(OpPC));
587 QualType ExprTy = E->getType();
588 if (ExprTy->isPointerOrReferenceType())
589 ExprTy = ExprTy->getPointeeType();
590
591 QualType TargetQT = ExprTy;
592 QualType MostDerivedQT = Ptr.getDeclPtr().getType();
593
594 if (MostDerivedQT->isPointerOrReferenceType())
595 MostDerivedQT = MostDerivedQT->getPointeeType();
596
597 S.CCEDiag(E, diag::note_constexpr_invalid_downcast)
598 << MostDerivedQT << TargetQT;
599
600 return false;
601}
602
603bool CheckConst(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
604 assert(Ptr.isLive() && "Pointer is not live");
605 if (!Ptr.isConst())
606 return true;
607
608 if (Ptr.isMutable() && !Ptr.isConstInMutable())
609 return true;
610
611 if (!Ptr.isBlockPointer())
612 return false;
613
614 // The This pointer is writable in constructors and destructors,
615 // even if isConst() returns true.
616 for (PtrView V : llvm::reverse(S.InitializingPtrs)) {
617 if (V.block() != Ptr.block())
618 continue;
619 if (!V.getFieldDesc()->IsConst) {
620 // If the pointer being initialized is not declared as const,
621 // Ptr is const because of a parent of V, but that is irrelevant
622 // since V is being initialized and NOT const.
623 // This is fine, so return true.
624 return true;
625 }
626
627 // We know that Ptr is const because of a parent field and we also
628 // know that V is explicitly marked const.
629 // But since V is in InitializingPtrs, the fact that it is const doesn't
630 // matter and it is writable.
631 // What we now need to check is whether there is a pointer between Ptr and V
632 // that is marked const but NOT in InitializingPtrs. If that is the case,
633 // Ptr is currently not writable.
634 bool FoundProblem = false;
635 for (PtrView P = Ptr.view(); P != V; P = P.getBase()) {
636 if (P.getFieldDesc()->IsConst) {
637 FoundProblem = true;
638 break;
639 }
640 }
641
642 // We couldn't find any pointer that's explicitly marked const, so
643 // Ptr is writable right now.
644 if (!FoundProblem)
645 return true;
646 // We only need to find the right block once.
647 break;
648 }
649
651 QualType Ty = Ptr.getType();
652 if (!Ptr.getFieldDesc()->IsConst)
653 Ty.addConst();
654 const SourceInfo &Loc = S.Current->getSource(OpPC);
655 S.FFDiag(Loc, diag::note_constexpr_modify_const_type) << Ty;
656 }
657 return false;
658}
659
661 assert(Ptr.isLive() && "Pointer is not live");
662 if (!Ptr.isMutable())
663 return true;
664
666 // Never allowed when checking for constant destruction.
667 // Diagnose below.
668 } else if (S.getLangOpts().CPlusPlus14 &&
670 // In C++14 onwards, it is permitted to read a mutable member whose
671 // lifetime began within the evaluation.
672 return true;
673 }
674
675 // Find the reason this pointer is mutable.
676 PtrView MutablePtr = Ptr;
677 while (!MutablePtr.isRoot() && MutablePtr.getBase().isMutable())
678 MutablePtr = MutablePtr.getBase();
679
680 const SourceInfo &Loc = S.Current->getSource(OpPC);
681 const FieldDecl *Field = MutablePtr.getField();
682 S.FFDiag(Loc, diag::note_constexpr_access_mutable, 1) << AK << Field;
683 S.Note(Field->getLocation(), diag::note_declared_at);
684 return false;
685}
686
687static bool CheckVolatile(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
688 AccessKinds AK) {
689 assert(Ptr.isLive());
690
691 if (!Ptr.isVolatile())
692 return true;
693
694 if (!S.getLangOpts().CPlusPlus)
695 return Invalid(S, OpPC);
696
697 // Volatile object can be written-to and read if they are being constructed.
698 if (S.initializingBlock(Ptr.block()))
699 return true;
700
701 // The reason why Ptr is volatile might be further up the hierarchy.
702 // Find that pointer.
703 Pointer P = Ptr;
704 while (!P.isRoot()) {
706 break;
707 P = P.getBase();
708 }
709
710 const NamedDecl *ND = nullptr;
711 int DiagKind;
712 SourceLocation Loc;
713 if (const auto *F = P.getField()) {
714 DiagKind = 2;
715 Loc = F->getLocation();
716 ND = F;
717 } else if (auto *VD = P.getFieldDesc()->asValueDecl()) {
718 DiagKind = 1;
719 Loc = VD->getLocation();
720 ND = VD;
721 } else {
722 DiagKind = 0;
723 if (const auto *E = P.getFieldDesc()->asExpr())
724 Loc = E->getExprLoc();
725 }
726
727 S.FFDiag(S.Current->getLocation(OpPC),
728 diag::note_constexpr_access_volatile_obj, 1)
729 << AK << DiagKind << ND;
730 S.Note(Loc, diag::note_constexpr_volatile_here) << DiagKind;
731 return false;
732}
733
735 AccessKinds AK) {
736 assert(Ptr.isLive());
737 assert(!Ptr.isInitialized());
738 return DiagnoseUninitialized(S, OpPC, Ptr.isExtern(), Ptr.block(), AK);
739}
740
741bool DiagnoseUninitialized(InterpState &S, CodePtr OpPC, bool Extern,
742 const Block *B, AccessKinds AK) {
744 // Extern and static member declarations might be initialized later.
745 if (Extern)
746 return false;
747
748 if (const VarDecl *VD = B->getDescriptor()->asVarDecl();
749 VD && VD->isStaticDataMember())
750 return false;
751 }
752
753 const Descriptor *Desc = B->getDescriptor();
754
755 if (const auto *VD = Desc->asVarDecl();
756 VD && (VD->isConstexpr() || VD->hasGlobalStorage())) {
757
758 if (VD == S.EvaluatingDecl &&
759 !(S.getLangOpts().CPlusPlus23 && VD->getType()->isReferenceType())) {
760 if (!S.getLangOpts().CPlusPlus14 &&
761 !VD->getType().isConstant(S.getASTContext())) {
762 // Diagnose as non-const read.
763 diagnoseNonConstVariable(S, OpPC, VD);
764 } else {
765 const SourceInfo &Loc = S.Current->getSource(OpPC);
766 // Diagnose as "read of object outside its lifetime".
767 S.FFDiag(Loc, diag::note_constexpr_access_uninit)
768 << AK << /*IsIndeterminate=*/false;
769 S.Note(VD->getLocation(), diag::note_declared_at);
770 }
771 return false;
772 }
773
774 if (VD->getAnyInitializer()) {
775 const SourceInfo &Loc = S.Current->getSource(OpPC);
776 S.FFDiag(Loc, diag::note_constexpr_var_init_non_constant, 1) << VD;
777 S.Note(VD->getLocation(), diag::note_declared_at);
778 } else {
779 diagnoseMissingInitializer(S, OpPC, VD);
780 }
781 return false;
782 }
783
785 S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_access_uninit)
786 << AK << /*uninitialized=*/true << S.Current->getRange(OpPC);
787 noteValueLocation(S, B);
788 }
789 return false;
790}
791
793 const Block *B, AccessKinds AK) {
794 if (LT == Lifetime::Started)
795 return true;
796
798 S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_access_uninit)
799 << AK << /*uninitialized=*/false << S.Current->getRange(OpPC);
800 noteValueLocation(S, B);
801 }
802 return false;
803}
804static bool CheckLifetime(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
805 AccessKinds AK) {
806 return CheckLifetime(S, OpPC, Ptr.getLifetime(), Ptr.block(), AK);
807}
808
809static bool CheckWeak(InterpState &S, CodePtr OpPC, const Block *B) {
810 if (!B->isWeak())
811 return true;
812
813 const auto *VD = B->getDescriptor()->asVarDecl();
814 assert(VD);
815 S.FFDiag(S.Current->getLocation(OpPC), diag::note_constexpr_var_init_weak)
816 << VD;
817 S.Note(VD->getLocation(), diag::note_declared_at);
818
819 return false;
820}
821
822// The list of checks here is just the one from CheckLoad, but with the
823// ones removed that are impossible on primitive global values.
824// For example, since those can't be members of structs, they also can't
825// be mutable.
826bool CheckGlobalLoad(InterpState &S, CodePtr OpPC, const Block *B) {
827 const auto &Desc = B->getBlockDesc<GlobalInlineDescriptor>();
828 if (!B->isAccessible()) {
829 if (!CheckExtern(S, OpPC, Pointer(const_cast<Block *>(B))))
830 return false;
831 if (!CheckDummy(S, OpPC, B, AK_Read))
832 return false;
833 return CheckWeak(S, OpPC, B);
834 }
835
836 if (!CheckConstant(S, OpPC, B->getDescriptor()))
837 return false;
838 if (Desc.InitState != GlobalInitState::Initialized)
839 return DiagnoseUninitialized(S, OpPC, B->isExtern(), B, AK_Read);
840 if (!CheckTemporary(S, OpPC, B, AK_Read))
841 return false;
842 if (B->getDescriptor()->IsVolatile) {
843 if (!S.getLangOpts().CPlusPlus)
844 return Invalid(S, OpPC);
845
846 const ValueDecl *D = B->getDescriptor()->asValueDecl();
847 S.FFDiag(S.Current->getLocation(OpPC),
848 diag::note_constexpr_access_volatile_obj, 1)
849 << AK_Read << 1 << D;
850 S.Note(D->getLocation(), diag::note_constexpr_volatile_here) << 1;
851 return false;
852 }
853 return true;
854}
855
856// Similarly, for local loads.
857bool CheckLocalLoad(InterpState &S, CodePtr OpPC, const Block *B) {
858 assert(!B->isExtern());
859 const auto &Desc = *reinterpret_cast<const InlineDescriptor *>(B->rawData());
860 if (!CheckLifetime(S, OpPC, Desc.LifeState, B, AK_Read))
861 return false;
862 if (!Desc.IsInitialized)
863 return DiagnoseUninitialized(S, OpPC, /*Extern=*/false, B, AK_Read);
864 if (B->getDescriptor()->IsVolatile) {
865 if (!S.getLangOpts().CPlusPlus)
866 return Invalid(S, OpPC);
867
868 const ValueDecl *D = B->getDescriptor()->asValueDecl();
869 S.FFDiag(S.Current->getLocation(OpPC),
870 diag::note_constexpr_access_volatile_obj, 1)
871 << AK_Read << 1 << D;
872 S.Note(D->getLocation(), diag::note_constexpr_volatile_here) << 1;
873 return false;
874 }
875 return true;
876}
877
878bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
879 AccessKinds AK) {
880 if (Ptr.isZero()) {
881 const auto &Src = S.Current->getSource(OpPC);
882
883 if (Ptr.isField())
884 S.FFDiag(Src, diag::note_constexpr_null_subobject) << CSK_Field;
885 else
886 S.FFDiag(Src, diag::note_constexpr_access_null) << AK;
887 return false;
888 }
889 // Block pointers are the only ones we can actually read from.
890 if (!Ptr.isBlockPointer())
891 return false;
892
893 if (!Ptr.block()->isAccessible()) {
894 if (!CheckLive(S, OpPC, Ptr, AK))
895 return false;
896 if (!CheckExtern(S, OpPC, Ptr))
897 return false;
898 if (!CheckDummy(S, OpPC, Ptr.block(), AK))
899 return false;
900 return CheckWeak(S, OpPC, Ptr.block());
901 }
902
903 if (!CheckConstant(S, OpPC, Ptr, AK))
904 return false;
905 if (!CheckRange(S, OpPC, Ptr, AK))
906 return false;
907 if (!CheckActive(S, OpPC, Ptr, AK))
908 return false;
909 if (!Ptr.isInitialized())
910 return DiagnoseUninitialized(S, OpPC, Ptr, AK);
911 if (!CheckLifetime(S, OpPC, Ptr, AK))
912 return false;
913 if (!CheckTemporary(S, OpPC, Ptr.block(), AK))
914 return false;
915
916 if (!CheckMutable(S, OpPC, Ptr))
917 return false;
918 if (!CheckVolatile(S, OpPC, Ptr, AK))
919 return false;
920 if (isConstexprUnknown(Ptr))
921 return false;
922
923 if (!Ptr.isArrayRoot()) {
924 // According to GCC info page:
925 //
926 // 6.28 Compound Literals
927 //
928 // As an optimization, G++ sometimes gives array compound literals
929 // longer lifetimes: when the array either appears outside a function or
930 // has a const-qualified type. If foo and its initializer had elements
931 // of type char *const rather than char *, or if foo were a global
932 // variable, the array would have static storage duration. But it is
933 // probably safest just to avoid the use of array compound literals in
934 // C++ code.
935 //
936 // Obey that rule by checking constness for converted array types.
937 const Descriptor *Desc = Ptr.getFieldDesc();
938 if (const auto *CLE =
939 dyn_cast_if_present<CompoundLiteralExpr>(Desc->asExpr())) {
940 if (QualType CLETy = CLE->getType();
941 CLETy->isArrayType() && !CLETy.isConstant(S.getASTContext())) {
942 S.FFDiag(S.Current->getLocation(OpPC),
943 diag::note_invalid_subexpr_in_const_expr)
944 << S.Current->getRange(OpPC);
945 S.Note(CLE->getExprLoc(), diag::note_declared_at);
946 return false;
947 }
948 }
949 }
950 return true;
951}
952
953/// This is not used by any of the opcodes directly. It's used by
954/// EvalEmitter to do the final lvalue-to-rvalue conversion.
955bool CheckFinalLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
956 assert(!Ptr.isZero());
957 if (!Ptr.isBlockPointer())
958 return false;
959
960 if (!Ptr.block()->isAccessible()) {
961 if (!CheckLive(S, OpPC, Ptr, AK_Read))
962 return false;
963 if (!CheckExtern(S, OpPC, Ptr))
964 return false;
965 if (!CheckDummy(S, OpPC, Ptr.block(), AK_Read))
966 return false;
967 return CheckWeak(S, OpPC, Ptr.block());
968 }
969
970 if (!CheckConstant(S, OpPC, Ptr))
971 return false;
972
973 if (!CheckActive(S, OpPC, Ptr, AK_Read))
974 return false;
975 if (!CheckLifetime(S, OpPC, Ptr, AK_Read))
976 return false;
977 if (!Ptr.isInitialized())
978 return DiagnoseUninitialized(S, OpPC, Ptr, AK_Read);
979 if (!CheckTemporary(S, OpPC, Ptr.block(), AK_Read))
980 return false;
981 if (!CheckMutable(S, OpPC, Ptr))
982 return false;
983 if (Ptr.isConstexprUnknown())
984 return false;
985 return true;
986}
987
988bool CheckStore(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
989 bool WillBeActivated) {
990 if (!Ptr.isBlockPointer() || Ptr.isZero())
991 return false;
992
993 if (!Ptr.block()->isAccessible()) {
994 if (!CheckLive(S, OpPC, Ptr, AK_Assign))
995 return false;
996 if (!CheckExtern(S, OpPC, Ptr))
997 return false;
998 return CheckDummy(S, OpPC, Ptr.block(), AK_Assign);
999 }
1000 if (!WillBeActivated && !CheckLifetime(S, OpPC, Ptr, AK_Assign))
1001 return false;
1002 if (!CheckRange(S, OpPC, Ptr, AK_Assign))
1003 return false;
1004 if (!CheckActive(S, OpPC, Ptr, AK_Assign, WillBeActivated))
1005 return false;
1006 if (!CheckGlobal(S, OpPC, Ptr))
1007 return false;
1008 if (!CheckConst(S, OpPC, Ptr))
1009 return false;
1010 if (!CheckVolatile(S, OpPC, Ptr, AK_Assign))
1011 return false;
1012 if (!CheckMutable(S, OpPC, Ptr, AK_Assign))
1013 return false;
1014 if (isConstexprUnknown(Ptr))
1015 return false;
1016 return true;
1017}
1018
1019static bool CheckInvoke(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
1020 bool IsCtor, bool IsDtor) {
1021 if (!Ptr.isDummy() && !isConstexprUnknown(Ptr)) {
1022 if (!CheckLive(S, OpPC, Ptr, AK_MemberCall))
1023 return false;
1024 if (!CheckRange(S, OpPC, Ptr, AK_MemberCall))
1025 return false;
1026 if (!(IsCtor || IsDtor) && !CheckLifetime(S, OpPC, Ptr, AK_MemberCall))
1027 return false;
1028 }
1029 return true;
1030}
1031
1032bool CheckInit(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
1033 if (!CheckLive(S, OpPC, Ptr, AK_Assign))
1034 return false;
1035 if (!CheckRange(S, OpPC, Ptr, AK_Assign))
1036 return false;
1037 return true;
1038}
1039
1041 const FunctionDecl *DiagDecl) {
1042 // Bail out if the function declaration itself is invalid. We will
1043 // have produced a relevant diagnostic while parsing it, so just
1044 // note the problematic sub-expression.
1045 if (DiagDecl->isInvalidDecl())
1046 return Invalid(S, OpPC);
1047
1048 // Diagnose failed assertions specially.
1049 if (S.Current->getLocation(OpPC).isMacroID() && DiagDecl->getIdentifier()) {
1050 // FIXME: Instead of checking for an implementation-defined function,
1051 // check and evaluate the assert() macro.
1052 StringRef Name = DiagDecl->getName();
1053 bool AssertFailed =
1054 Name == "__assert_rtn" || Name == "__assert_fail" || Name == "_wassert";
1055 if (AssertFailed) {
1056 S.FFDiag(S.Current->getLocation(OpPC),
1057 diag::note_constexpr_assert_failed);
1058 return false;
1059 }
1060 }
1061
1062 if (!S.getLangOpts().CPlusPlus11) {
1063 S.FFDiag(S.Current->getLocation(OpPC),
1064 diag::note_invalid_subexpr_in_const_expr);
1065 return false;
1066 }
1067
1068 // Invalid decls have been diagnosed before.
1069 if (DiagDecl->isInvalidDecl())
1070 return false;
1071
1072 // If this function is not constexpr because it is an inherited
1073 // non-constexpr constructor, diagnose that directly.
1074 const auto *CD = dyn_cast<CXXConstructorDecl>(DiagDecl);
1075 if (CD && CD->isInheritingConstructor()) {
1076 const auto *Inherited = CD->getInheritedConstructor().getConstructor();
1077 if (!Inherited->isConstexpr())
1078 DiagDecl = CD = Inherited;
1079 }
1080
1081 // Silently reject constructors of invalid classes. The invalid class
1082 // has been rejected elsewhere before.
1083 if (CD && CD->getParent()->isInvalidDecl())
1084 return false;
1085
1086 // FIXME: If DiagDecl is an implicitly-declared special member function
1087 // or an inheriting constructor, we should be much more explicit about why
1088 // it's not constexpr.
1089 if (CD && CD->isInheritingConstructor()) {
1090 S.FFDiag(S.Current->getLocation(OpPC), diag::note_constexpr_invalid_inhctor,
1091 1)
1092 << CD->getInheritedConstructor().getConstructor()->getParent();
1093 S.Note(DiagDecl->getLocation(), diag::note_declared_at);
1094 } else {
1095 // Don't emit anything if the function isn't defined and we're checking
1096 // for a constant expression. It might be defined at the point we're
1097 // actually calling it.
1098 bool IsExtern = DiagDecl->getStorageClass() == SC_Extern;
1099 bool IsDefined = DiagDecl->isDefined();
1100 if (!IsDefined && !IsExtern && DiagDecl->isConstexpr() &&
1102 return false;
1103
1104 // If the declaration is defined, declared 'constexpr' _and_ has a body,
1105 // the below diagnostic doesn't add anything useful.
1106 if (DiagDecl->isDefined() && DiagDecl->isConstexpr() && DiagDecl->hasBody())
1107 return false;
1108
1109 S.FFDiag(S.Current->getLocation(OpPC),
1110 diag::note_constexpr_invalid_function, 1)
1111 << DiagDecl->isConstexpr() << (bool)CD << DiagDecl;
1112
1113 if (DiagDecl->getDefinition())
1114 S.Note(DiagDecl->getDefinition()->getLocation(), diag::note_declared_at);
1115 else
1116 S.Note(DiagDecl->getLocation(), diag::note_declared_at);
1117 }
1118
1119 return false;
1120}
1121
1122static bool CheckCallable(InterpState &S, CodePtr OpPC, const Function *F) {
1123 if (F->isVirtual() && !S.getLangOpts().CPlusPlus20) {
1124 const SourceLocation &Loc = S.Current->getLocation(OpPC);
1125 S.CCEDiag(Loc, diag::note_constexpr_virtual_call);
1126 return false;
1127 }
1128
1129 if (F->isValid() && F->hasBody() &&
1131 F->getDecl()->hasAttr<MSConstexprAttr>())))
1132 return true;
1133
1134 const FunctionDecl *DiagDecl = F->getDecl();
1135 const FunctionDecl *Definition = nullptr;
1136 DiagDecl->getBody(Definition);
1137
1139 DiagDecl->isConstexpr()) {
1140 return false;
1141 }
1142
1143 // Implicitly constexpr.
1144 if (F->isLambdaStaticInvoker())
1145 return true;
1146
1147 return diagnoseCallableDecl(S, OpPC, DiagDecl);
1148}
1149
1150static bool CheckCallDepth(InterpState &S, CodePtr OpPC) {
1151 if ((S.Current->getDepth() + 1) > S.getLangOpts().ConstexprCallDepth) {
1152 S.FFDiag(S.Current->getSource(OpPC),
1153 diag::note_constexpr_depth_limit_exceeded)
1154 << S.getLangOpts().ConstexprCallDepth;
1155 return false;
1156 }
1157
1158 return true;
1159}
1160
1162 if (S.Current->hasThisPointer())
1163 return true;
1164
1165 const Expr *E = S.Current->getExpr(OpPC);
1166 if (S.getLangOpts().CPlusPlus11) {
1167 bool IsImplicit = false;
1168 if (const auto *TE = dyn_cast<CXXThisExpr>(E))
1169 IsImplicit = TE->isImplicit();
1170 S.FFDiag(E, diag::note_constexpr_this) << IsImplicit;
1171 } else {
1172 S.FFDiag(E);
1173 }
1174
1175 return false;
1176}
1177
1179 APFloat::opStatus Status, FPOptions FPO) {
1180 // [expr.pre]p4:
1181 // If during the evaluation of an expression, the result is not
1182 // mathematically defined [...], the behavior is undefined.
1183 // FIXME: C++ rules require us to not conform to IEEE 754 here.
1184 if (Result.isNan()) {
1185 const SourceInfo &E = S.Current->getSource(OpPC);
1186 S.CCEDiag(E, diag::note_constexpr_float_arithmetic)
1187 << /*NaN=*/true << S.Current->getRange(OpPC);
1188 return S.noteUndefinedBehavior();
1189 }
1190
1191 // In a constant context, assume that any dynamic rounding mode or FP
1192 // exception state matches the default floating-point environment.
1193 if (S.inConstantContext())
1194 return true;
1195
1196 if ((Status & APFloat::opInexact) &&
1197 FPO.getRoundingMode() == llvm::RoundingMode::Dynamic) {
1198 // Inexact result means that it depends on rounding mode. If the requested
1199 // mode is dynamic, the evaluation cannot be made in compile time.
1200 const SourceInfo &E = S.Current->getSource(OpPC);
1201 S.FFDiag(E, diag::note_constexpr_dynamic_rounding);
1202 return false;
1203 }
1204
1205 if ((Status != APFloat::opOK) &&
1206 (FPO.getRoundingMode() == llvm::RoundingMode::Dynamic ||
1208 FPO.getAllowFEnvAccess())) {
1209 const SourceInfo &E = S.Current->getSource(OpPC);
1210 S.FFDiag(E, diag::note_constexpr_float_arithmetic_strict);
1211 return false;
1212 }
1213
1214 if ((Status & APFloat::opStatus::opInvalidOp) &&
1216 const SourceInfo &E = S.Current->getSource(OpPC);
1217 // There is no usefully definable result.
1218 S.FFDiag(E);
1219 return false;
1220 }
1221
1222 return true;
1223}
1224
1226 if (S.getLangOpts().CPlusPlus20)
1227 return true;
1228
1229 const SourceInfo &E = S.Current->getSource(OpPC);
1230 S.CCEDiag(E, diag::note_constexpr_new);
1231 return true;
1232}
1233
1235 DynamicAllocator::Form AllocForm,
1236 DynamicAllocator::Form DeleteForm, const Descriptor *D,
1237 const Expr *NewExpr) {
1238 if (AllocForm == DeleteForm)
1239 return true;
1240
1241 QualType TypeToDiagnose = D->getDataType(S.getASTContext());
1242
1243 const SourceInfo &E = S.Current->getSource(OpPC);
1244 S.FFDiag(E, diag::note_constexpr_new_delete_mismatch)
1245 << static_cast<int>(DeleteForm) << static_cast<int>(AllocForm)
1246 << TypeToDiagnose;
1247 S.Note(NewExpr->getExprLoc(), diag::note_constexpr_dynamic_alloc_here)
1248 << NewExpr->getSourceRange();
1249 return false;
1250}
1251
1252bool CheckDeleteSource(InterpState &S, CodePtr OpPC, const Expr *Source,
1253 const Pointer &Ptr) {
1254 // Regular new type(...) call.
1255 if (isa_and_nonnull<CXXNewExpr>(Source))
1256 return true;
1257 // operator new.
1258 if (const auto *CE = dyn_cast_if_present<CallExpr>(Source);
1259 CE && CE->getBuiltinCallee() == Builtin::BI__builtin_operator_new)
1260 return true;
1261 // std::allocator.allocate() call
1262 if (const auto *MCE = dyn_cast_if_present<CXXMemberCallExpr>(Source);
1263 MCE && MCE->getMethodDecl()->getIdentifier()->isStr("allocate"))
1264 return true;
1265
1266 // Whatever this is, we didn't heap allocate it.
1267 const SourceInfo &Loc = S.Current->getSource(OpPC);
1268 S.FFDiag(Loc, diag::note_constexpr_delete_not_heap_alloc)
1270 noteValueLocation(S, Ptr.block());
1271 return false;
1272}
1273
1274/// We aleady know the given DeclRefExpr is invalid for some reason,
1275/// now figure out why and print appropriate diagnostics.
1276bool CheckDeclRef(InterpState &S, CodePtr OpPC, const DeclRefExpr *DR) {
1277 const ValueDecl *D = DR->getDecl();
1278 return diagnoseUnknownDecl(S, OpPC, D);
1279}
1280
1282 bool InitializerFailed) {
1283 assert(DR);
1284
1285 if (InitializerFailed) {
1286 const SourceInfo &Loc = S.Current->getSource(OpPC);
1287 const auto *VD = cast<VarDecl>(DR->getDecl());
1288 S.FFDiag(Loc, diag::note_constexpr_var_init_non_constant, 1) << VD;
1289 S.Note(VD->getLocation(), diag::note_declared_at);
1290 return false;
1291 }
1292
1293 return CheckDeclRef(S, OpPC, DR);
1294}
1295
1296bool CheckDummy(InterpState &S, CodePtr OpPC, const Block *B, AccessKinds AK) {
1297 if (!B->isDummy())
1298 return true;
1299
1300 const ValueDecl *D = B->getDescriptor()->asValueDecl();
1301 if (!D)
1302 return false;
1303
1304 if (AK == AK_Read || AK == AK_Increment || AK == AK_Decrement)
1305 return diagnoseUnknownDecl(S, OpPC, D, AK);
1306
1307 if (AK == AK_Destroy || S.getLangOpts().CPlusPlus14) {
1308 const SourceInfo &E = S.Current->getSource(OpPC);
1309 S.FFDiag(E, diag::note_constexpr_modify_global);
1310 }
1311 return false;
1312}
1313
1314static bool CheckNonNullArgs(InterpState &S, CodePtr OpPC, const Function *F,
1315 const CallExpr *CE, unsigned ArgSize) {
1316 auto Args = ArrayRef(CE->getArgs(), CE->getNumArgs());
1317 auto NonNullArgs = collectNonNullArgs(F->getDecl(), Args);
1318 unsigned Offset = 0;
1319 unsigned Index = 0;
1320 for (const Expr *Arg : Args) {
1321 if (NonNullArgs[Index] && Arg->getType()->isPointerType()) {
1322 const Pointer &ArgPtr = S.Stk.peek<Pointer>(ArgSize - Offset);
1323 if (ArgPtr.isZero()) {
1324 const SourceLocation &Loc = S.Current->getLocation(OpPC);
1325 S.CCEDiag(Loc, diag::note_non_null_attribute_failed);
1326 return false;
1327 }
1328 }
1329
1330 Offset += align(primSize(S.Ctx.classify(Arg).value_or(PT_Ptr)));
1331 ++Index;
1332 }
1333 return true;
1334}
1335
1337 const Pointer &BasePtr,
1338 const Descriptor *Desc) {
1339 assert(Desc->isRecord());
1340 const Record *R = Desc->ElemRecord;
1341 assert(R);
1342
1343 if (!S.Current->isBottomFrame() && S.Current->hasThisPointer() &&
1345 Pointer::pointToSameBlock(BasePtr, S.Current->getThis())) {
1346 const SourceInfo &Loc = S.Current->getSource(OpPC);
1347 S.FFDiag(Loc, diag::note_constexpr_double_destroy);
1348 return false;
1349 }
1350
1351 // Destructor of this record.
1352 const CXXDestructorDecl *Dtor = R->getDestructor();
1353 assert(Dtor);
1354 assert(!Dtor->isTrivial());
1355 const Function *DtorFunc = S.getContext().getOrCreateFunction(Dtor);
1356 if (!DtorFunc)
1357 return false;
1358
1359 S.Stk.push<Pointer>(BasePtr);
1360 return Call(S, OpPC, DtorFunc, 0);
1361}
1362
1363static bool RunDestructors(InterpState &S, CodePtr OpPC, const Block *B) {
1364 assert(B);
1365 const Descriptor *Desc = B->getDescriptor();
1366
1367 if (Desc->isPrimitive() || Desc->isPrimitiveArray())
1368 return true;
1369
1370 assert(Desc->isRecord() || Desc->isCompositeArray());
1371
1372 if (Desc->hasTrivialDtor())
1373 return true;
1374
1375 if (Desc->isCompositeArray()) {
1376 unsigned N = Desc->getNumElems();
1377 if (N == 0)
1378 return true;
1379 const Descriptor *ElemDesc = Desc->ElemDesc;
1380 assert(ElemDesc->isRecord());
1381
1382 Pointer RP(const_cast<Block *>(B));
1383 for (int I = static_cast<int>(N) - 1; I >= 0; --I) {
1384 if (!runRecordDestructor(S, OpPC, RP.atIndex(I).narrow(), ElemDesc))
1385 return false;
1386 }
1387 return true;
1388 }
1389
1390 assert(Desc->isRecord());
1391 return runRecordDestructor(S, OpPC, Pointer(const_cast<Block *>(B)), Desc);
1392}
1393
1395 if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl())
1396 if (const CXXDestructorDecl *DD = RD->getDestructor())
1397 return DD->isVirtual();
1398 return false;
1399}
1400
1401bool Free(InterpState &S, CodePtr OpPC, bool DeleteIsArrayForm,
1402 bool IsGlobalDelete) {
1403 if (!CheckDynamicMemoryAllocation(S, OpPC))
1404 return false;
1405
1406 DynamicAllocator &Allocator = S.getAllocator();
1407
1408 const Expr *Source = nullptr;
1409 const Block *BlockToDelete = nullptr;
1410 {
1411 // Extra scope for this so the block doesn't have this pointer
1412 // pointing to it when we destroy it.
1413 Pointer Ptr = S.Stk.pop<Pointer>();
1414
1415 // Deleteing nullptr is always fine.
1416 if (Ptr.isZero())
1417 return true;
1418
1419 if (!Ptr.isBlockPointer())
1420 return false;
1421
1422 // Remove base casts.
1423 QualType InitialType = Ptr.getType();
1424 Ptr = Ptr.expand().stripBaseCasts();
1425
1426 Source = Ptr.getDeclDesc()->asExpr();
1427 BlockToDelete = Ptr.block();
1428
1429 // Check that new[]/delete[] or new/delete were used, not a mixture.
1430 const Descriptor *BlockDesc = BlockToDelete->getDescriptor();
1431 if (std::optional<DynamicAllocator::Form> AllocForm =
1432 Allocator.getAllocationForm(Source)) {
1433 DynamicAllocator::Form DeleteForm =
1434 DeleteIsArrayForm ? DynamicAllocator::Form::Array
1436 if (!CheckNewDeleteForms(S, OpPC, *AllocForm, DeleteForm, BlockDesc,
1437 Source))
1438 return false;
1439 }
1440
1441 // For the non-array case, the types must match if the static type
1442 // does not have a virtual destructor.
1443 if (!DeleteIsArrayForm && Ptr.getType() != InitialType &&
1444 !hasVirtualDestructor(InitialType)) {
1445 S.FFDiag(S.Current->getSource(OpPC),
1446 diag::note_constexpr_delete_base_nonvirt_dtor)
1447 << InitialType << Ptr.getType();
1448 return false;
1449 }
1450
1451 if (!Ptr.isRoot() || (Ptr.isOnePastEnd() && !Ptr.isZeroSizeArray()) ||
1452 (Ptr.isArrayElement() && Ptr.getIndex() != 0)) {
1453 const SourceInfo &Loc = S.Current->getSource(OpPC);
1454 S.FFDiag(Loc, diag::note_constexpr_delete_subobject)
1455 << Ptr.toDiagnosticString(S.getASTContext()) << Ptr.isOnePastEnd();
1456 return false;
1457 }
1458
1459 if (!CheckDeleteSource(S, OpPC, Source, Ptr))
1460 return false;
1461
1462 // For a class type with a virtual destructor, the selected operator delete
1463 // is the one looked up when building the destructor.
1464 if (!DeleteIsArrayForm && !IsGlobalDelete) {
1465 QualType AllocType = Ptr.getType();
1466 auto getVirtualOperatorDelete = [](QualType T) -> const FunctionDecl * {
1467 if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl())
1468 if (const CXXDestructorDecl *DD = RD->getDestructor())
1469 return DD->isVirtual() ? DD->getOperatorDelete() : nullptr;
1470 return nullptr;
1471 };
1472
1473 if (const FunctionDecl *VirtualDelete =
1474 getVirtualOperatorDelete(AllocType);
1475 VirtualDelete &&
1476 !VirtualDelete
1478 S.FFDiag(S.Current->getSource(OpPC),
1479 diag::note_constexpr_new_non_replaceable)
1480 << isa<CXXMethodDecl>(VirtualDelete) << VirtualDelete;
1481 return false;
1482 }
1483 }
1484 }
1485 assert(Source);
1486 assert(BlockToDelete);
1487
1488 // Invoke destructors before deallocating the memory.
1489 if (!RunDestructors(S, OpPC, BlockToDelete))
1490 return false;
1491
1492 if (!Allocator.deallocate(Source, BlockToDelete)) {
1493 // Nothing has been deallocated, this must be a double-delete.
1494 const SourceInfo &Loc = S.Current->getSource(OpPC);
1495 S.FFDiag(Loc, diag::note_constexpr_double_delete);
1496 return false;
1497 }
1498
1499 return true;
1500}
1501
1503 const APSInt &Value) {
1504 llvm::APInt Min;
1505 llvm::APInt Max;
1506 ED->getValueRange(Max, Min);
1507 --Max;
1508
1509 if (ED->getNumNegativeBits() &&
1510 (Max.slt(Value.getSExtValue()) || Min.sgt(Value.getSExtValue()))) {
1511 const SourceLocation &Loc = S.Current->getLocation(OpPC);
1512 S.CCEDiag(Loc, diag::note_constexpr_unscoped_enum_out_of_range)
1513 << llvm::toString(Value, 10) << Min.getSExtValue() << Max.getSExtValue()
1514 << ED;
1515 } else if (!ED->getNumNegativeBits() && Max.ult(Value.getZExtValue())) {
1516 const SourceLocation &Loc = S.Current->getLocation(OpPC);
1517 S.CCEDiag(Loc, diag::note_constexpr_unscoped_enum_out_of_range)
1518 << llvm::toString(Value, 10) << Min.getZExtValue() << Max.getZExtValue()
1519 << ED;
1520 }
1521}
1522
1523bool CheckLiteralType(InterpState &S, CodePtr OpPC, const Type *T) {
1524 assert(T);
1525 assert(!S.getLangOpts().CPlusPlus23);
1526
1527 // C++1y: A constant initializer for an object o [...] may also invoke
1528 // constexpr constructors for o and its subobjects even if those objects
1529 // are of non-literal class types.
1530 //
1531 // C++11 missed this detail for aggregates, so classes like this:
1532 // struct foo_t { union { int i; volatile int j; } u; };
1533 // are not (obviously) initializable like so:
1534 // __attribute__((__require_constant_initialization__))
1535 // static const foo_t x = {{0}};
1536 // because "i" is a subobject with non-literal initialization (due to the
1537 // volatile member of the union). See:
1538 // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1677
1539 // Therefore, we use the C++1y behavior.
1540
1541 if (!S.Current->isBottomFrame() &&
1544 return true;
1545 }
1546
1547 const Expr *E = S.Current->getExpr(OpPC);
1548 if (S.getLangOpts().CPlusPlus11)
1549 S.FFDiag(E, diag::note_constexpr_nonliteral) << E->getType();
1550 else
1551 S.FFDiag(E, diag::note_invalid_subexpr_in_const_expr);
1552 return false;
1553}
1554
1555static bool getField(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
1556 uint32_t Off) {
1557 if (S.getLangOpts().CPlusPlus && S.inConstantContext() &&
1558 !CheckNull(S, OpPC, Ptr, CSK_Field))
1559 return false;
1560
1561 if (!CheckRange(S, OpPC, Ptr, CSK_Field))
1562 return false;
1563 if (!CheckArray(S, OpPC, Ptr))
1564 return false;
1565 if (!CheckSubobject(S, OpPC, Ptr, CSK_Field))
1566 return false;
1567
1568 if (Ptr.isIntegralPointer()) {
1569 if (std::optional<IntPointer> IntPtr =
1570 Ptr.asIntPointer().atOffset(S.Ctx, Off)) {
1571 S.Stk.push<Pointer>(std::move(*IntPtr));
1572 return true;
1573 }
1574 return false;
1575 }
1576
1577 if (!Ptr.isBlockPointer()) {
1578 // FIXME: The only time we (seem to) get here is when trying to access a
1579 // field of a typeid pointer. In that case, we're supposed to diagnose e.g.
1580 // `typeid(int).name`, but we currently diagnose `&typeid(int)`.
1581 S.FFDiag(S.Current->getSource(OpPC),
1582 diag::note_constexpr_access_unreadable_object)
1584 return false;
1585 }
1586
1587 // We can't get the field of something that's not a record.
1588 if (!Ptr.getFieldDesc()->isRecord())
1589 return false;
1590
1591 if ((Ptr.getByteOffset() + Off) >= Ptr.block()->getSize())
1592 return false;
1593
1594 S.Stk.push<Pointer>(Ptr.atField(Off));
1595 return true;
1596}
1597
1598bool GetPtrField(InterpState &S, CodePtr OpPC, uint32_t Off) {
1599 const auto &Ptr = S.Stk.peek<Pointer>();
1600 return getField(S, OpPC, Ptr, Off);
1601}
1602
1603bool GetPtrFieldPop(InterpState &S, CodePtr OpPC, uint32_t Off) {
1604 const auto &Ptr = S.Stk.pop<Pointer>();
1605 return getField(S, OpPC, Ptr, Off);
1606}
1607
1608static bool getBase(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
1609 uint32_t Off, bool NullOK) {
1610 if (!NullOK && !CheckNull(S, OpPC, Ptr, CSK_Base))
1611 return false;
1612
1613 if (!Ptr.isBlockPointer()) {
1614 if (!Ptr.isIntegralPointer())
1615 return false;
1616 S.Stk.push<Pointer>(Ptr.asIntPointer().baseCast(S.Ctx, Off));
1617 return true;
1618 }
1619
1620 if (!CheckSubobject(S, OpPC, Ptr, CSK_Base))
1621 return false;
1622
1623 // In case this isn't something we can get the base of at all,
1624 // just return the pointer itself so it can be diagnosed later.
1625 if (!Ptr.getFieldDesc()->isRecord()) {
1626 S.Stk.push<Pointer>(Ptr);
1627 return true;
1628 }
1629
1630 const Pointer &Result = Ptr.atField(Off);
1631 if (Result.isPastEnd() || !Result.isBaseClass())
1632 return false;
1633 S.Stk.push<Pointer>(Result);
1634 return true;
1635}
1636
1637bool GetPtrBase(InterpState &S, CodePtr OpPC, uint32_t Off) {
1638 const auto &Ptr = S.Stk.peek<Pointer>();
1639 return getBase(S, OpPC, Ptr.narrow(), Off, /*NullOK=*/true);
1640}
1641bool GetPtrBasePop(InterpState &S, CodePtr OpPC, uint32_t Off, bool NullOK) {
1642 const auto &Ptr = S.Stk.pop<Pointer>();
1643 return getBase(S, OpPC, Ptr.narrow(), Off, NullOK);
1644}
1645
1646bool GetPtrDerivedPop(InterpState &S, CodePtr OpPC, uint32_t Off, bool NullOK,
1647 const Type *TargetType) {
1648 const Pointer &Ptr = S.Stk.pop<Pointer>().narrow();
1649 if (!NullOK && !CheckNull(S, OpPC, Ptr, CSK_Derived))
1650 return false;
1651
1652 if (!Ptr.isBlockPointer()) {
1653 // FIXME: We don't have the necessary information in integral pointers.
1654 // The Descriptor only has a record, but that does of course not include
1655 // the potential derived classes of said record.
1656 S.Stk.push<Pointer>(Ptr);
1657 return true;
1658 }
1659
1660 if (!Ptr.getFieldDesc()->isRecord()) {
1661 S.Stk.push<Pointer>(Ptr);
1662 return true;
1663 }
1664
1665 if (!CheckSubobject(S, OpPC, Ptr, CSK_Derived))
1666 return false;
1667 if (!CheckDowncast(S, OpPC, Ptr, Off))
1668 return false;
1669
1670 const Record *TargetRecord = Ptr.atFieldSub(Off).getRecord();
1671 assert(TargetRecord);
1672
1673 if (TargetRecord->getDecl()->getCanonicalDecl() !=
1674 TargetType->getAsCXXRecordDecl()->getCanonicalDecl()) {
1675 QualType MostDerivedType = Ptr.getDeclDesc()->getType();
1676 S.CCEDiag(S.Current->getSource(OpPC), diag::note_constexpr_invalid_downcast)
1677 << MostDerivedType << QualType(TargetType, 0);
1678 return false;
1679 }
1680
1681 S.Stk.push<Pointer>(Ptr.atFieldSub(Off));
1682 return true;
1683}
1684
1685static bool checkConstructor(InterpState &S, CodePtr OpPC, const Function *Func,
1686 const Pointer &ThisPtr) {
1687 assert(Func->isConstructor());
1688
1689 if (Func->getParentDecl()->isInvalidDecl())
1690 return false;
1691
1692 const Descriptor *D = ThisPtr.getFieldDesc();
1693 // FIXME: I think this case is not 100% correct. E.g. a pointer into a
1694 // subobject of a composite array.
1695 if (!D->ElemRecord)
1696 return true;
1697
1698 if (D->ElemRecord->getNumVirtualBases() == 0)
1699 return true;
1700
1701 S.FFDiag(S.Current->getLocation(OpPC), diag::note_constexpr_virtual_base)
1702 << Func->getParentDecl();
1703 return false;
1704}
1705
1707 const Pointer &Ptr) {
1708 assert(Ptr.getLifetime() != Lifetime::Started);
1709 // Try to use the declaration for better diagnostics
1710 if (const Decl *D = Ptr.getDeclDesc()->asDecl()) {
1711 auto *ND = cast<NamedDecl>(D);
1712 S.FFDiag(ND->getLocation(), diag::note_constexpr_destroy_out_of_lifetime)
1713 << ND->getNameAsString();
1714 } else {
1715 S.FFDiag(Ptr.getDeclDesc()->getLocation(),
1716 diag::note_constexpr_destroy_out_of_lifetime)
1718 }
1719 return false;
1720}
1721
1722bool checkDestructor(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
1723 if (!CheckLive(S, OpPC, Ptr, AK_Destroy))
1724 return false;
1725 if (!CheckTemporary(S, OpPC, Ptr.block(), AK_Destroy))
1726 return false;
1727 if (!CheckRange(S, OpPC, Ptr, AK_Destroy))
1728 return false;
1729
1730 if (Ptr.getLifetime() == Lifetime::Destroyed)
1731 return diagnoseOutOfLifetimeDestroy(S, OpPC, Ptr);
1732 if (Ptr.getLifetime() == Lifetime::Ended)
1733 return CheckLifetime(S, OpPC, Ptr, AK_Destroy);
1734
1735 // We _can_ call the destructor on the global variable we're checking constant
1736 // destruction for.
1737 if (S.checkingConstantDestruction(Ptr))
1738 return true;
1739
1740 // Can't call a dtor on a global variable.
1741 if (Ptr.block()->isStatic()) {
1742 const SourceInfo &E = S.Current->getSource(OpPC);
1743 S.FFDiag(E, diag::note_constexpr_modify_global);
1744 return false;
1745 }
1746 return CheckActive(S, OpPC, Ptr, AK_Destroy);
1747}
1748
1749/// Opcode. Check if the function decl can be called at compile time.
1752 return false;
1753
1754 const FunctionDecl *Definition = nullptr;
1755 const Stmt *Body = FD->getBody(Definition);
1756
1757 if (Definition && Body &&
1758 (Definition->isConstexpr() || (S.Current->MSVCConstexprAllowed &&
1759 Definition->hasAttr<MSConstexprAttr>())))
1760 return true;
1761
1762 return diagnoseCallableDecl(S, OpPC, FD);
1763}
1764
1765bool CheckBitCast(InterpState &S, CodePtr OpPC, const Type *TargetType,
1766 bool SrcIsVoidPtr) {
1767 const auto &Ptr = S.Stk.peek<Pointer>();
1768 if (Ptr.isZero())
1769 return true;
1770 if (!Ptr.isBlockPointer())
1771 return true;
1772
1773 if (TargetType->isIntegerType())
1774 return true;
1775
1776 if (SrcIsVoidPtr && S.getLangOpts().CPlusPlus) {
1777 bool HasValidResult = !Ptr.isZero();
1778
1779 if (HasValidResult) {
1780 if (S.getStdAllocatorCaller("allocate"))
1781 return true;
1782
1783 const auto *E = cast<CastExpr>(S.Current->getExpr(OpPC));
1784 if (S.getLangOpts().CPlusPlus26 &&
1785 S.getASTContext().hasSimilarType(Ptr.getType(),
1786 QualType(TargetType, 0)))
1787 return true;
1788
1789 S.CCEDiag(E, diag::note_constexpr_invalid_void_star_cast)
1790 << E->getSubExpr()->getType() << S.getLangOpts().CPlusPlus26
1791 << Ptr.getType().getCanonicalType() << E->getType()->getPointeeType();
1792 } else if (!S.getLangOpts().CPlusPlus26) {
1793 const SourceInfo &E = S.Current->getSource(OpPC);
1794 S.CCEDiag(E, diag::note_constexpr_invalid_cast)
1795 << diag::ConstexprInvalidCastKind::CastFrom << "'void *'"
1796 << S.Current->getRange(OpPC);
1797 }
1798 }
1799
1800 QualType PtrType = Ptr.getType();
1801 if (PtrType->isRecordType() &&
1802 PtrType->getAsRecordDecl() != TargetType->getAsRecordDecl()) {
1803 S.CCEDiag(S.Current->getSource(OpPC), diag::note_constexpr_invalid_cast)
1804 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
1805 << S.getLangOpts().CPlusPlus << S.Current->getRange(OpPC);
1806 }
1807 return true;
1808}
1809
1810static void compileFunction(InterpState &S, const Function *Func) {
1811 const FunctionDecl *Definition;
1812 if (!Func->getDecl()->getBody(Definition))
1813 return;
1814 if (!Definition)
1815 return;
1816
1818 .compileFunc(Definition, const_cast<Function *>(Func));
1819}
1820
1822 uint32_t VarArgSize) {
1823 if (Func->hasThisPointer()) {
1824 size_t ArgSize = Func->getArgSize() + VarArgSize;
1825 size_t ThisOffset = ArgSize - (Func->hasRVO() ? primSize(PT_Ptr) : 0);
1826 const Pointer &ThisPtr = S.Stk.peek<Pointer>(ThisOffset);
1827
1828 // If the current function is a lambda static invoker and
1829 // the function we're about to call is a lambda call operator,
1830 // skip the CheckInvoke, since the ThisPtr is a null pointer
1831 // anyway.
1832 if (!(S.Current->getFunction() &&
1834 Func->isLambdaCallOperator())) {
1835 if (!CheckInvoke(S, OpPC, ThisPtr, Func->isConstructor(),
1836 Func->isDestructor()))
1837 return false;
1838 }
1839
1841 return false;
1842 }
1843
1844 if (!Func->isFullyCompiled())
1846
1847 if (!CheckCallable(S, OpPC, Func))
1848 return false;
1849
1850 if (!CheckCallDepth(S, OpPC))
1851 return false;
1852
1853 auto Memory = new char[InterpFrame::allocSize(Func)];
1854 auto NewFrame = new (Memory) InterpFrame(S, Func, S.PC, VarArgSize);
1855 InterpFrame *FrameBefore = S.Current;
1856 S.Current = NewFrame;
1857
1858 InterpStateCCOverride CCOverride(S, Func->isImmediate());
1859 if (Interpret(S)) {
1860 assert(S.Current == FrameBefore);
1861 return true;
1862 }
1863
1864 InterpFrame::free(NewFrame);
1865 // Interpreting the function failed somehow. Reset to
1866 // previous state.
1867 S.Current = FrameBefore;
1868 return false;
1869}
1870bool Call(InterpState &S, CodePtr OpPC, const Function *Func,
1871 uint32_t VarArgSize) {
1872
1873 // C doesn't have constexpr functions.
1874 if (!S.getLangOpts().CPlusPlus)
1875 return Invalid(S, OpPC);
1876
1877 assert(Func);
1878 auto cleanup = [&]() -> bool {
1880 return false;
1881 };
1882
1883 bool InstancePtrTracked = false;
1884 if (Func->hasThisPointer()) {
1885 size_t ArgSize = Func->getArgSize() + VarArgSize;
1886 size_t ThisOffset = ArgSize - (Func->hasRVO() ? primSize(PT_Ptr) : 0);
1887
1888 const Pointer &ThisPtr = S.Stk.peek<Pointer>(ThisOffset);
1889
1890 // C++23 [expr.const]p5.6
1891 // an invocation of a virtual function ([class.virtual]) for an object whose
1892 // dynamic type is constexpr-unknown;
1893 if (ThisPtr.isDummy() && Func->isVirtual())
1894 return false;
1895
1896 // If the current function is a lambda static invoker and
1897 // the function we're about to call is a lambda call operator,
1898 // skip the CheckInvoke, since the ThisPtr is a null pointer
1899 // anyway.
1900 if (S.Current->getFunction() &&
1902 Func->isLambdaCallOperator()) {
1903 assert(ThisPtr.isZero());
1904 } else {
1905 if (!CheckInvoke(S, OpPC, ThisPtr, Func->isConstructor(),
1906 Func->isDestructor()))
1907 return cleanup();
1908
1909 if (Func->isCopyOrMoveOperator() || Func->isCopyOrMoveConstructor()) {
1910 const Pointer &RVOPtr =
1911 S.Stk.peek<Pointer>(ThisOffset - align(sizeof(Pointer)));
1912 if (!CheckInvoke(S, OpPC, RVOPtr, /*IsCtor=*/true, /*IsDtor=*/false))
1913 return cleanup();
1914 }
1915
1916 if (!Func->isConstructor() && !Func->isDestructor() &&
1917 !CheckActive(S, OpPC, ThisPtr, AK_MemberCall))
1918 return false;
1919 }
1920
1921 if (Func->isConstructor() && !checkConstructor(S, OpPC, Func, ThisPtr))
1922 return false;
1923 if (Func->isDestructor() && !checkDestructor(S, OpPC, ThisPtr))
1924 return false;
1925
1926 InstancePtrTracked = (Func->isConstructor() || Func->isDestructor());
1927 if (InstancePtrTracked)
1928 S.InitializingPtrs.push_back(ThisPtr.view());
1929 }
1930
1931 if (!Func->isFullyCompiled())
1933
1934 if (!CheckCallable(S, OpPC, Func))
1935 return cleanup();
1936
1937 // Do not evaluate any function calls in checkingPotentialConstantExpression
1938 // mode. Constructors will be aborted later when their initializers are
1939 // evaluated.
1940 if (S.checkingPotentialConstantExpression() && !Func->isConstructor())
1941 return false;
1942
1943 if (!CheckCallDepth(S, OpPC))
1944 return cleanup();
1945
1946 auto Memory = new char[InterpFrame::allocSize(Func)];
1947 auto NewFrame = new (Memory) InterpFrame(S, Func, S.PC, VarArgSize);
1948 InterpFrame *FrameBefore = S.Current;
1949 S.Current = NewFrame;
1950
1951 InterpStateCCOverride CCOverride(S, Func->isImmediate());
1952 bool Success = Interpret(S);
1953 // Remove initializing block again.
1954 if (InstancePtrTracked)
1955 S.InitializingPtrs.pop_back();
1956
1957 if (!Success) {
1958 InterpFrame::free(NewFrame);
1959 // Interpreting the function failed somehow. Reset to
1960 // previous state.
1961 S.Current = FrameBefore;
1962 return false;
1963 }
1964
1965 assert(S.Current == FrameBefore);
1966 return true;
1967}
1968
1969static bool getDynamicDecl(InterpState &S, CodePtr OpPC, PtrView TypePtr,
1970 const CXXRecordDecl *&DynamicDecl) {
1971
1972 if (S.InitializingPtrs.empty()) {
1973 TypePtr = TypePtr.stripBaseCasts();
1974 } else {
1975 auto depth = [](PtrView V) -> unsigned {
1976 unsigned C = 1;
1977 while (!V.isRoot()) {
1978 ++C;
1979 V = V.getBase();
1980 }
1981 return C;
1982 };
1983 // Consider a 'normal' diamond hierarchy:
1984 // A A 3
1985 // | |
1986 // B C 2
1987 // \ /
1988 // \ /
1989 // D 1
1990 // When we use a pointer of D*, cast it to B's A* and
1991 // use it during the construction of C*, the expected
1992 // dynamic type is B.
1993 PtrView InitPtr = S.InitializingPtrs.back();
1994 assert(depth(TypePtr) >= depth(InitPtr));
1995 unsigned D = depth(TypePtr) - depth(InitPtr);
1996 for (unsigned I = 0; I != D; ++I)
1997 TypePtr = TypePtr.getBase();
1998 }
1999
2000 QualType DynamicType = TypePtr.getType();
2001 if (TypePtr.Pointee->isStatic() || TypePtr.isConst()) {
2002 if (const VarDecl *VD = Pointer(TypePtr).getRootVarDecl();
2003 VD && !VD->isConstexpr()) {
2004 const Expr *E = S.Current->getExpr(OpPC);
2005 APValue V = Pointer(TypePtr).toAPValue(S.getASTContext());
2007 S.FFDiag(E, diag::note_constexpr_polymorphic_unknown_dynamic_type)
2008 << AK_MemberCall << V.getAsString(S.getASTContext(), TT);
2009 return false;
2010 }
2011 }
2012
2013 if (DynamicType->isPointerType() || DynamicType->isReferenceType()) {
2014 DynamicDecl = DynamicType->getPointeeCXXRecordDecl();
2015 } else if (DynamicType->isArrayType()) {
2016 const Type *ElemType = DynamicType->getPointeeOrArrayElementType();
2017 assert(ElemType);
2018 DynamicDecl = ElemType->getAsCXXRecordDecl();
2019 } else {
2020 DynamicDecl = DynamicType->getAsCXXRecordDecl();
2021 }
2022 return DynamicDecl != nullptr;
2023}
2024
2026 UnsignedOrNone Offset = std::nullopt;
2027 bool Ambiguous = false;
2028
2029 bool valid() const { return !Ambiguous && Offset; }
2030
2031 void setOffset(unsigned O) {
2032 if (!Offset)
2033 Offset = O;
2034 else {
2035 Ambiguous = true;
2036 }
2037 }
2038
2040 Ambiguous |= C.Ambiguous;
2041 if (C.Offset) {
2042 if (!Offset)
2043 Offset = C.Offset;
2044 else
2045 Ambiguous = true;
2046 }
2047 }
2048};
2049
2050// Walk UP the type hierarchy, starting at the decl of R to find Needle.
2052 QualType Needle) {
2054
2055 if (Ctx.hasSimilarType(Needle, Ctx.getCanonicalTagType(R->getDecl())))
2056 Res.setOffset(0);
2057
2058 for (const Record::Base &B : R->bases()) {
2059 auto N = findRecordBase(Ctx, B.R, Needle);
2060 if (N.Offset)
2061 N.Offset = *N.Offset + B.Offset;
2062 Res.merge(N);
2063 }
2064
2065 return Res;
2066}
2067
2068bool DynamicCast(InterpState &S, CodePtr OpPC, const Type *DestTypePtr,
2069 bool IsReferenceCast) {
2070 const auto &Ptr = S.Stk.pop<Pointer>();
2071 QualType TargetType = QualType(DestTypePtr, 0);
2072
2073 if (Ptr.isConstexprUnknown()) {
2074 QualType T = Ptr.getType();
2075 const Expr *E = S.Current->getExpr(OpPC);
2076 APValue V = Ptr.toAPValue(S.getASTContext());
2078 S.FFDiag(E, diag::note_constexpr_polymorphic_unknown_dynamic_type)
2079 << AK_DynamicCast << V.getAsString(S.getASTContext(), TT);
2080 return false;
2081 }
2082
2083 if (!Ptr.isBlockPointer() || !Ptr.getRecord())
2084 return false;
2085
2086 if (!Ptr.isInitialized())
2087 return DiagnoseUninitialized(S, OpPC, Ptr, AK_Read);
2088
2089 // Our given pointer, limited by the base that's currently being initialized,
2090 // if any.
2091 PtrView LimitedPtr;
2092 if (S.InitializingPtrs.empty() ||
2093 S.InitializingPtrs.back().block() != Ptr.block()) {
2094 LimitedPtr = Ptr.stripBaseCasts().view();
2095 } else {
2096 LimitedPtr = S.InitializingPtrs.back();
2097 assert(LimitedPtr.block() == Ptr.block());
2098 }
2099 assert(LimitedPtr.getRecord());
2100
2101 // C++ [expr.dynamic.cast]p7:
2102 // If T is "pointer to cv void", then the result is a pointer to the most
2103 // derived object
2104 if (TargetType->isVoidType()) {
2105 S.Stk.push<Pointer>(LimitedPtr);
2106 return true;
2107 }
2108
2109 assert(!TargetType.isNull());
2110 assert(!TargetType->isVoidType());
2111 assert(TargetType->isRecordType());
2112
2113 // Helper lambdas.
2114 auto typesMatch = [&](QualType A, QualType B) -> bool {
2115 return S.getASTContext().hasSimilarType(A, B);
2116 };
2117 auto getRecord = [](PtrView P) -> const CXXRecordDecl * {
2118 assert(P.getRecord());
2119 return cast<CXXRecordDecl>(P.getRecord()->getDecl());
2120 };
2121
2122 auto baseIsPrivate = [&](PtrView P) -> bool {
2123 if (P.isRoot() || !P.isBaseClass())
2124 return false;
2125
2126 CXXBasePaths Paths;
2127 getRecord(P.getBase())->isDerivedFrom(getRecord(P), Paths);
2128 assert(std::distance(Paths.begin(), Paths.end()) == 1);
2129
2130 return Paths.front().Access == AS_private;
2131 };
2132
2133 enum {
2134 DiagPrivateBase = 0,
2135 DiagNoBase = 1,
2136 DiagAmbiguous = 2,
2137 DiagPrivateSibling = 3
2138 };
2139
2140 auto diag = [&](int DiagKind, QualType ResultType) -> bool {
2141 // Pointer casts return nullptr on failure.
2142 if (!IsReferenceCast) {
2143 S.Stk.push<Pointer>(0, DestTypePtr);
2144 return true;
2145 }
2147 S.FFDiag(S.Current->getSource(OpPC),
2148 diag::note_constexpr_dynamic_cast_to_reference_failed)
2149 << DiagKind << ResultType << DynamicType << TargetType;
2150 return false;
2151 };
2152
2153 // Check if Ptr's dynamic type is derived from our target type at all.
2154 // If it isn't, diagnose this as "operand does not have base class of type
2155 // [...]".
2156 {
2157 CXXBasePaths Paths;
2158 getRecord(LimitedPtr)
2159 ->isDerivedFrom(TargetType->getAsCXXRecordDecl(), Paths);
2160 if (std::distance(Paths.begin(), Paths.end()) == 0 &&
2161 !typesMatch(LimitedPtr.getType(), TargetType)) {
2162 return diag(DiagNoBase, TargetType);
2163 }
2164 }
2165
2166 // Current base is already private.
2167 if (baseIsPrivate(Ptr.view()))
2168 return diag(DiagPrivateBase, Ptr.getType());
2169
2170 std::optional<PtrView> Result;
2171 // First, check simple downcasts without ambiguities.
2172 for (PtrView Iter = Ptr.view();;) {
2173 if (Iter.isRoot() || !Iter.isBaseClass())
2174 break;
2175
2176 if (typesMatch(TargetType, Iter.getType())) {
2177 Result = Iter;
2178 break;
2179 }
2180 // Moving DOWN the type hierarchy.
2181 Iter = Iter.getBase();
2182 }
2183
2184 // Simply walking down the type hierarchy has produced a valid result, use
2185 // that.
2186 if (Result) {
2187 if (baseIsPrivate(*Result))
2188 return diag(DiagPrivateBase, Result->getType());
2189 S.Stk.push<Pointer>(*Result);
2190 return true;
2191 }
2192
2193 // Otherwise, we need to do a deep hierarchy check.
2194 bool Ambiguous = false;
2195 for (PtrView Iter = LimitedPtr;;) {
2196 // If we can move up the hierarchy from this level and reach the target type
2197 // unambiguously, we're fine.
2198 auto R = findRecordBase(S.getASTContext(), Iter.getRecord(), TargetType);
2199
2200 if (R.valid()) {
2201 Result = Iter.atField(*R.Offset);
2202 break;
2203 } else if (R.Ambiguous) {
2204 Ambiguous = true;
2205 break;
2206 }
2207
2208 // This moves us DOWN the type hierarchy.
2209 Iter = Iter.getBase();
2210 if (Iter.isRoot() || !Iter.isBaseClass())
2211 break;
2212 }
2213
2214 if (Ambiguous)
2215 return diag(DiagAmbiguous, TargetType);
2216
2217 if (Result) {
2218 // Might still be invalid due to resulting in a private base though.
2219 if (baseIsPrivate(*Result))
2220 return diag(DiagPrivateSibling, TargetType);
2221 S.Stk.push<Pointer>(*Result);
2222 return true;
2223 }
2224
2225 // We couldn't find the requested base.
2226 return diag(DiagNoBase, TargetType);
2227}
2228
2230 uint32_t VarArgSize) {
2231 assert(Func->hasThisPointer());
2232 assert(Func->isVirtual());
2233 size_t ArgSize = Func->getArgSize() + VarArgSize;
2234 size_t ThisOffset = ArgSize - (Func->hasRVO() ? primSize(PT_Ptr) : 0);
2235 Pointer &ThisPtr = S.Stk.peek<Pointer>(ThisOffset);
2236
2237 if (!ThisPtr.isBlockPointer())
2238 return false;
2239
2240 const FunctionDecl *Callee = Func->getDecl();
2241
2242 const CXXRecordDecl *DynamicDecl = nullptr;
2243 if (!getDynamicDecl(S, OpPC, ThisPtr.view(), DynamicDecl))
2244 return false;
2245 assert(DynamicDecl);
2246
2247 const auto *StaticDecl = cast<CXXRecordDecl>(Func->getParentDecl());
2248 const auto *InitialFunction = cast<CXXMethodDecl>(Callee);
2249 const CXXMethodDecl *Overrider;
2250
2251 if (StaticDecl != DynamicDecl) {
2252 if (!DynamicDecl->isDerivedFrom(StaticDecl))
2253 return false;
2254 Overrider = S.getContext().getOverridingFunction(DynamicDecl, StaticDecl,
2255 InitialFunction);
2256
2257 } else {
2258 Overrider = InitialFunction;
2259 }
2260
2261 // C++2a [class.abstract]p6:
2262 // the effect of making a virtual call to a pure virtual function [...] is
2263 // undefined
2264 if (Overrider->isPureVirtual()) {
2265 S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_pure_virtual_call,
2266 1)
2267 << Callee;
2268 S.Note(Callee->getLocation(), diag::note_declared_at);
2269 return false;
2270 }
2271
2272 if (Overrider != InitialFunction) {
2273 // DR1872: An instantiated virtual constexpr function can't be called in a
2274 // constant expression (prior to C++20). We can still constant-fold such a
2275 // call.
2276 if (!S.getLangOpts().CPlusPlus20 && Overrider->isVirtual()) {
2277 const Expr *E = S.Current->getExpr(OpPC);
2278 S.CCEDiag(E, diag::note_constexpr_virtual_call) << E->getSourceRange();
2279 }
2280
2281 Func = S.getContext().getOrCreateFunction(Overrider);
2282
2283 const CXXRecordDecl *ThisFieldDecl =
2284 ThisPtr.getFieldDesc()->getType()->getAsCXXRecordDecl();
2285 if (Func->getParentDecl()->isDerivedFrom(ThisFieldDecl)) {
2286 // If the function we call is further DOWN the hierarchy than the
2287 // FieldDesc of our pointer, just go up the hierarchy of this field
2288 // the furthest we can go.
2289 ThisPtr = ThisPtr.stripBaseCasts();
2290 }
2291 }
2292
2293 if (!Call(S, OpPC, Func, VarArgSize))
2294 return false;
2295
2296 // Covariant return types. The return type of Overrider is a pointer
2297 // or reference to a class type.
2298 if (Overrider != InitialFunction &&
2299 Overrider->getReturnType()->isPointerOrReferenceType() &&
2300 InitialFunction->getReturnType()->isPointerOrReferenceType()) {
2301 QualType OverriderPointeeType =
2302 Overrider->getReturnType()->getPointeeType();
2303 QualType InitialPointeeType =
2304 InitialFunction->getReturnType()->getPointeeType();
2305
2306 // Nothing to do if the types already match.
2307 if (S.getASTContext().hasSimilarType(InitialPointeeType,
2308 OverriderPointeeType))
2309 return true;
2310
2311 // We've called Overrider above, but calling code expects us to return what
2312 // InitialFunction returned. According to the rules for covariant return
2313 // types, what InitialFunction returns needs to be a base class of what
2314 // Overrider returns. So, we need to do an upcast here.
2315 unsigned Offset = S.getContext().collectBaseOffset(
2316 InitialPointeeType->getAsRecordDecl(),
2317 OverriderPointeeType->getAsRecordDecl());
2318 return GetPtrBasePop(S, OpPC, Offset, /*IsNullOK=*/true);
2319 }
2320
2321 return true;
2322}
2323
2324bool CallBI(InterpState &S, CodePtr OpPC, const CallExpr *CE,
2325 uint32_t BuiltinID) {
2326 // A little arbitrary, but the current interpreter allows evaluation
2327 // of builtin functions in this mode, with some exceptions.
2328 if (BuiltinID == Builtin::BI__builtin_operator_new &&
2330 return false;
2331
2332 return InterpretBuiltin(S, OpPC, CE, BuiltinID);
2333}
2334
2335bool CallPtr(InterpState &S, CodePtr OpPC, uint32_t ArgSize,
2336 const CallExpr *CE) {
2337 const Pointer &Ptr = S.Stk.pop<Pointer>();
2338
2339 if (Ptr.isZero()) {
2340 S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_null_callee)
2341 << const_cast<Expr *>(CE->getCallee()) << CE->getSourceRange();
2342 return false;
2343 }
2344
2345 if (!Ptr.isFunctionPointer())
2346 return Invalid(S, OpPC);
2347
2348 const Function *F = Ptr.asFunctionPointer().Func;
2349 assert(F);
2350 // Don't allow calling block pointers.
2351 if (!F->getDecl())
2352 return Invalid(S, OpPC);
2353
2354 // This happens when the call expression has been cast to
2355 // something else, but we don't support that.
2356 if (S.Ctx.classify(F->getDecl()->getReturnType()) !=
2358 return false;
2359
2360 // Check argument nullability state.
2361 if (F->hasNonNullAttr()) {
2362 if (!CheckNonNullArgs(S, OpPC, F, CE, ArgSize))
2363 return false;
2364 }
2365
2366 // Can happen when casting function pointers around.
2367 QualType CalleeType = CE->getCallee()->getType();
2368 if (CalleeType->isPointerType() &&
2370 F->getDecl()->getType(), CalleeType->getPointeeType())) {
2371 return false;
2372 }
2373
2374 // We nedd to compile (and check) early for function pointer calls
2375 // because the Call/CallVirt below might access the instance pointer
2376 // but the Function's information about them is wrong.
2377 if (!F->isFullyCompiled())
2378 compileFunction(S, F);
2379
2380 if (!CheckCallable(S, OpPC, F))
2381 return false;
2382
2383 assert(ArgSize >= F->getWrittenArgSize());
2384 uint32_t VarArgSize = ArgSize - F->getWrittenArgSize();
2385
2386 // We need to do this explicitly here since we don't have the necessary
2387 // information to do it automatically.
2388 if (F->hasExplicitThisPointer())
2389 VarArgSize -= align(primSize(PT_Ptr));
2390
2391 if (F->isVirtual())
2392 return CallVirt(S, OpPC, F, VarArgSize);
2393
2394 return Call(S, OpPC, F, VarArgSize);
2395}
2396
2398 if (const Record *R = Ptr.getRecord()) {
2399 Ptr.startLifetime();
2400
2401 for (const Record::Field &Fi : R->fields()) {
2402 PtrView FP = Ptr.atField(Fi.Offset);
2403 if (FP.getLifetime() != Lifetime::Started)
2405 }
2406 return;
2407 }
2408
2409 if (const Descriptor *FieldDesc = Ptr.getFieldDesc();
2410 FieldDesc->isCompositeArray()) {
2411 for (unsigned I = 0; I != FieldDesc->getNumElems(); ++I) {
2412 PtrView EP = Ptr.atIndex(I).narrow();
2413 if (EP.getLifetime() != Lifetime::Started)
2415 }
2416 return;
2417 }
2418
2419 Ptr.startLifetime();
2420}
2421
2424 return true;
2425
2426 const auto &Ptr = S.Current->getThis();
2427 if (!Ptr.isBlockPointer())
2428 return false;
2429 startLifetimeRecurse(Ptr.view());
2430 return true;
2431}
2432
2435 return true;
2436
2437 const auto &Ptr = S.Current->getThis();
2438 if (!Ptr.isBlockPointer())
2439 return false;
2440 Ptr.startLifetime();
2441 return true;
2442}
2443
2444// FIXME: It might be better to the recursing as part of the generated code for
2445// a destructor?
2447 if (const Record *R = Ptr.getRecord()) {
2448 Ptr.setLifeState(L);
2449 for (const Record::Field &Fi : R->fields())
2450 setLifeStateRecurse(Ptr.atField(Fi.Offset), L);
2451 return;
2452 }
2453
2454 if (const Descriptor *FieldDesc = Ptr.getFieldDesc();
2455 FieldDesc->isCompositeArray()) {
2456 // No endLifetime() for primitive array roots.
2457 if (Ptr.getFieldDesc()->isPrimitiveArray())
2458 assert(Ptr.getLifetime() == Lifetime::Started);
2459 for (unsigned I = 0; I != FieldDesc->getNumElems(); ++I)
2460 setLifeStateRecurse(Ptr.atIndex(I).narrow(), L);
2461 return;
2462 }
2463
2464 Ptr.setLifeState(L);
2465}
2466
2467/// Ends the lifetime of the peek'd pointer.
2469 const auto &Ptr = S.Stk.peek<Pointer>();
2470 if (Ptr.isBlockPointer() && !CheckDummy(S, OpPC, Ptr.block(), AK_Destroy))
2471 return false;
2472
2473 setLifeStateRecurse(Ptr.view().narrow(), Lifetime::Ended);
2474 return true;
2475}
2476
2477/// Ends the lifetime of the pop'd pointer.
2479 const auto &Ptr = S.Stk.pop<Pointer>();
2480 if (!checkDestructor(S, OpPC, Ptr))
2481 return false;
2482 setLifeStateRecurse(Ptr.view().narrow(), Lifetime::Ended);
2483 return true;
2484}
2485
2487 const auto &Ptr = S.Stk.peek<Pointer>();
2488 if (Ptr.isBlockPointer() && !CheckDummy(S, OpPC, Ptr.block(), AK_Destroy))
2489 return false;
2490
2491 setLifeStateRecurse(Ptr.view().narrow(), Lifetime::Destroyed);
2492 return true;
2493}
2494
2496 std::optional<uint64_t> ArraySize) {
2497 const Pointer &Ptr = S.Stk.peek<Pointer>();
2498
2499 auto directBaseIsUnion = [](const Pointer &Ptr) -> bool {
2500 if (Ptr.isArrayElement())
2501 return false;
2502 const Record *R = Ptr.getBase().getRecord();
2503 return R && R->isUnion();
2504 };
2505
2506 if (Ptr.inUnion() && directBaseIsUnion(Ptr))
2507 Ptr.activate();
2508
2509 if (Ptr.isZero()) {
2510 S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_access_null)
2511 << AK_Construct;
2512 return false;
2513 }
2514
2515 if (!Ptr.isBlockPointer())
2516 return false;
2517
2518 if (!CheckRange(S, OpPC, Ptr, AK_Construct))
2519 return false;
2520
2522
2523 // Similar to CheckStore(), but with the additional CheckTemporary() call and
2524 // the AccessKinds are different.
2525 if (!Ptr.block()->isAccessible()) {
2526 if (!CheckExtern(S, OpPC, Ptr))
2527 return false;
2528 if (!CheckLive(S, OpPC, Ptr, AK_Construct))
2529 return false;
2530 return CheckDummy(S, OpPC, Ptr.block(), AK_Construct);
2531 }
2532 if (!CheckTemporary(S, OpPC, Ptr.block(), AK_Construct))
2533 return false;
2534
2535 // CheckLifetime for this and all base pointers.
2536 for (PtrView P = Ptr.view();;) {
2537 if (!CheckLifetime(S, OpPC, P.getLifetime(), P.Pointee, AK_Construct))
2538 return false;
2539
2540 if (P.isRoot())
2541 break;
2542 P = P.getBase();
2543 }
2544
2545 if (!CheckRange(S, OpPC, Ptr, AK_Construct))
2546 return false;
2547 if (!CheckGlobal(S, OpPC, Ptr))
2548 return false;
2549 if (!CheckConst(S, OpPC, Ptr))
2550 return false;
2551 if (!S.inConstantContext() && isConstexprUnknown(Ptr))
2552 return false;
2553
2554 if (!InvalidNewDeleteExpr(S, OpPC, E))
2555 return false;
2556
2557 const auto *NewExpr = cast<CXXNewExpr>(E);
2558 QualType StorageType = Ptr.getFieldDesc()->getDataType(S.getASTContext());
2559 const ASTContext &ASTCtx = S.getASTContext();
2560 QualType AllocType;
2561 if (ArraySize) {
2562 AllocType = ASTCtx.getConstantArrayType(
2563 NewExpr->getAllocatedType(),
2564 APInt(64, static_cast<uint64_t>(*ArraySize), false), nullptr,
2566 } else {
2567 AllocType = NewExpr->getAllocatedType();
2568 }
2569
2570 unsigned StorageSize = 1;
2571 unsigned AllocSize = 1;
2572 if (const auto *CAT = dyn_cast<ConstantArrayType>(AllocType))
2573 AllocSize = CAT->getZExtSize();
2574 if (const auto *CAT = dyn_cast<ConstantArrayType>(StorageType))
2575 StorageSize = CAT->getZExtSize();
2576
2577 if (AllocSize > StorageSize ||
2578 !ASTCtx.hasSimilarType(ASTCtx.getBaseElementType(AllocType),
2579 ASTCtx.getBaseElementType(StorageType))) {
2580 S.FFDiag(S.Current->getLocation(OpPC),
2581 diag::note_constexpr_placement_new_wrong_type)
2582 << StorageType << AllocType;
2583 return false;
2584 }
2585
2586 // Can't activate fields in a union, unless the direct base is the union.
2587 if (Ptr.inUnion() && !Ptr.isActive() && !directBaseIsUnion(Ptr))
2588 return CheckActive(S, OpPC, Ptr, AK_Construct);
2589
2590 return true;
2591}
2592
2594 assert(E);
2595
2596 if (const auto *NewExpr = dyn_cast<CXXNewExpr>(E)) {
2597 const FunctionDecl *OperatorNew = NewExpr->getOperatorNew();
2598
2599 if (NewExpr->getNumPlacementArgs() > 0) {
2600 // This is allowed pre-C++26, but only an std function or if
2601 // [[msvc::constexpr]] was used.
2602 if (S.getLangOpts().CPlusPlus26 || S.Current->isStdFunction() ||
2604 return true;
2605
2606 S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_new_placement)
2607 << /*C++26 feature*/ 1 << E->getSourceRange();
2608 } else if (
2609 !OperatorNew
2610 ->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) {
2611 S.FFDiag(S.Current->getSource(OpPC),
2612 diag::note_constexpr_new_non_replaceable)
2613 << isa<CXXMethodDecl>(OperatorNew) << OperatorNew;
2614 return false;
2615 } else if (!S.getLangOpts().CPlusPlus26 &&
2616 NewExpr->getNumPlacementArgs() == 1 &&
2617 !OperatorNew->isReservedGlobalPlacementOperator()) {
2618 if (!S.getLangOpts().CPlusPlus26) {
2619 S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_new_placement)
2620 << /*Unsupported*/ 0 << E->getSourceRange();
2621 return false;
2622 }
2623 return true;
2624 }
2625 } else {
2626 const auto *DeleteExpr = cast<CXXDeleteExpr>(E);
2627 const FunctionDecl *OperatorDelete = DeleteExpr->getOperatorDelete();
2628 if (!OperatorDelete
2629 ->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) {
2630 S.FFDiag(S.Current->getSource(OpPC),
2631 diag::note_constexpr_new_non_replaceable)
2632 << isa<CXXMethodDecl>(OperatorDelete) << OperatorDelete;
2633 return false;
2634 }
2635 }
2636
2637 return false;
2638}
2639
2641 const FixedPoint &FP) {
2642 const Expr *E = S.Current->getExpr(OpPC);
2645 E->getExprLoc(), diag::warn_fixedpoint_constant_overflow)
2646 << FP.toDiagnosticString(S.getASTContext()) << E->getType();
2647 }
2648 S.CCEDiag(E, diag::note_constexpr_overflow)
2649 << FP.toDiagnosticString(S.getASTContext()) << E->getType();
2650 return S.noteUndefinedBehavior();
2651}
2652
2653bool InvalidShuffleVectorIndex(InterpState &S, CodePtr OpPC, uint32_t Index) {
2654 const SourceInfo &Loc = S.Current->getSource(OpPC);
2655 S.FFDiag(Loc,
2656 diag::err_shufflevector_minus_one_is_undefined_behavior_constexpr)
2657 << Index;
2658 return false;
2659}
2660
2662 const Pointer &Ptr, unsigned BitWidth) {
2663 SourceInfo E = S.Current->getSource(OpPC);
2664 S.CCEDiag(E, diag::note_constexpr_invalid_cast)
2665 << 2 << S.getLangOpts().CPlusPlus << S.Current->getRange(OpPC);
2666
2667 if (Ptr.isIntegralPointer())
2668 return true;
2669
2670 if (Ptr.isDummy()) {
2671 if (!CheckIntegralAddressCast(S, OpPC, BitWidth))
2672 return false;
2673 return Ptr.getIndex() == 0;
2674 }
2675
2676 if (!Ptr.isZero()) {
2677 // Only allow based lvalue casts if they are lossless.
2678 if (!CheckIntegralAddressCast(S, OpPC, BitWidth))
2679 return Invalid(S, OpPC);
2680 }
2681 return true;
2682}
2683
2684bool CheckIntegralAddressCast(InterpState &S, CodePtr OpPC, unsigned BitWidth) {
2686 BitWidth);
2687}
2688
2689bool CastPointerIntegralAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
2690 const Pointer &Ptr = S.Stk.pop<Pointer>();
2691
2692 if (!CheckPointerToIntegralCast(S, OpPC, Ptr, BitWidth))
2693 return false;
2694
2695 auto Result = S.allocAP<IntegralAP<false>>(BitWidth);
2696 Result.copy(APInt(BitWidth, Ptr.getIntegerRepresentation()));
2697
2699 return true;
2700}
2701
2702bool CastPointerIntegralAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
2703 const Pointer &Ptr = S.Stk.pop<Pointer>();
2704
2705 if (!CheckPointerToIntegralCast(S, OpPC, Ptr, BitWidth))
2706 return false;
2707
2708 auto Result = S.allocAP<IntegralAP<true>>(BitWidth);
2709 Result.copy(APInt(BitWidth, Ptr.getIntegerRepresentation()));
2710
2712 return true;
2713}
2714
2715bool CheckBitCast(InterpState &S, CodePtr OpPC, bool HasIndeterminateBits,
2716 bool TargetIsUCharOrByte) {
2717 // This is always fine.
2718 if (!HasIndeterminateBits)
2719 return true;
2720
2721 // Indeterminate bits can only be bitcast to unsigned char or std::byte.
2722 if (TargetIsUCharOrByte)
2723 return true;
2724
2725 const Expr *E = S.Current->getExpr(OpPC);
2726 QualType ExprType = E->getType();
2727 S.FFDiag(E, diag::note_constexpr_bit_cast_indet_dest)
2728 << ExprType << S.getLangOpts().CharIsSigned << E->getSourceRange();
2729 return false;
2730}
2731
2733 if (isConstexprUnknown(B)) {
2734 S.Stk.push<Pointer>(B);
2735 return true;
2736 }
2737
2738 const auto &ID = B->getBlockDesc<const InlineDescriptor>();
2739 if (!ID.IsInitialized) {
2741 S.FFDiag(S.Current->getSource(OpPC),
2742 diag::note_constexpr_use_uninit_reference);
2743 return false;
2744 }
2745
2746 assert(B->getDescriptor()->getPrimType() == PT_Ptr);
2747 S.Stk.push<Pointer>(B->deref<Pointer>());
2748 return true;
2749}
2750
2751bool GetTypeid(InterpState &S, const Type *TypePtr, const Type *TypeInfoType) {
2752 S.Stk.push<Pointer>(TypePtr, TypeInfoType);
2753 return true;
2754}
2755
2756bool GetTypeidPtr(InterpState &S, CodePtr OpPC, const Type *TypeInfoType) {
2757 const auto &P = S.Stk.pop<Pointer>();
2758
2759 if (!P.isBlockPointer())
2760 return false;
2761
2762 if (P.isConstexprUnknown()) {
2763 QualType DynamicType = P.getType();
2764 const Expr *E = S.Current->getExpr(OpPC);
2765 APValue V = P.toAPValue(S.getASTContext());
2767 S.FFDiag(E, diag::note_constexpr_polymorphic_unknown_dynamic_type)
2768 << AK_TypeId << V.getAsString(S.getASTContext(), TT);
2769 return false;
2770 }
2771
2772 // Pick the most-derived type.
2773 CanQualType T = P.stripBaseCasts().getType()->getCanonicalTypeUnqualified();
2774 // ... unless we're currently constructing this object.
2775 // FIXME: We have a similar check to this in more places.
2776 if (S.Current->getFunction()) {
2777 for (const InterpFrame *Frame = S.Current; Frame; Frame = Frame->Caller) {
2778 if (const Function *Func = Frame->getFunction();
2779 Func && (Func->isConstructor() || Func->isDestructor()) &&
2780 P.block() == Frame->getThis().block()) {
2782 Func->getParentDecl());
2783 break;
2784 }
2785 }
2786 }
2787
2788 S.Stk.push<Pointer>(T->getTypePtr(), TypeInfoType);
2789 return true;
2790}
2791
2793 const auto *E = cast<CXXTypeidExpr>(S.Current->getExpr(OpPC));
2794 S.CCEDiag(E, diag::note_constexpr_typeid_polymorphic)
2795 << E->getExprOperand()->getType()
2796 << E->getExprOperand()->getSourceRange();
2797 return false;
2798}
2799
2801 const Pointer &RHS) {
2802 if (!LHS.pointsToStringLiteral() || !RHS.pointsToStringLiteral())
2803 return false;
2804
2805 unsigned LHSOffset = LHS.isOnePastEnd() ? LHS.getNumElems() : LHS.getIndex();
2806 unsigned RHSOffset = RHS.isOnePastEnd() ? RHS.getNumElems() : RHS.getIndex();
2807 const auto *LHSLit = cast<StringLiteral>(LHS.getDeclDesc()->asExpr());
2808 const auto *RHSLit = cast<StringLiteral>(RHS.getDeclDesc()->asExpr());
2809
2810 StringRef LHSStr(LHSLit->getBytes());
2811 unsigned LHSLength = LHSStr.size();
2812 StringRef RHSStr(RHSLit->getBytes());
2813 unsigned RHSLength = RHSStr.size();
2814
2815 int32_t IndexDiff = RHSOffset - LHSOffset;
2816 if (IndexDiff < 0) {
2817 if (static_cast<int32_t>(LHSLength) < -IndexDiff)
2818 return false;
2819 LHSStr = LHSStr.drop_front(-IndexDiff);
2820 } else {
2821 if (static_cast<int32_t>(RHSLength) < IndexDiff)
2822 return false;
2823 RHSStr = RHSStr.drop_front(IndexDiff);
2824 }
2825
2826 unsigned ShorterCharWidth;
2827 StringRef Shorter;
2828 StringRef Longer;
2829 if (LHSLength < RHSLength) {
2830 ShorterCharWidth = LHS.getFieldDesc()->getElemDataSize();
2831 Shorter = LHSStr;
2832 Longer = RHSStr;
2833 } else {
2834 ShorterCharWidth = RHS.getFieldDesc()->getElemDataSize();
2835 Shorter = RHSStr;
2836 Longer = LHSStr;
2837 }
2838
2839 // The null terminator isn't included in the string data, so check for it
2840 // manually. If the longer string doesn't have a null terminator where the
2841 // shorter string ends, they aren't potentially overlapping.
2842 for (unsigned NullByte : llvm::seq(ShorterCharWidth)) {
2843 if (Shorter.size() + NullByte >= Longer.size())
2844 break;
2845 if (Longer[Shorter.size() + NullByte])
2846 return false;
2847 }
2848 return Shorter == Longer.take_front(Shorter.size());
2849}
2850
2851static void copyPrimitiveMemory(InterpState &S, const Pointer &Ptr,
2852 PrimType T) {
2853 if (T == PT_IntAPS) {
2854 auto &Val = Ptr.deref<IntegralAP<true>>();
2855 if (!Val.singleWord()) {
2856 uint64_t *NewMemory = new (S.P) uint64_t[Val.numWords()];
2857 Val.take(NewMemory);
2858 }
2859 } else if (T == PT_IntAP) {
2860 auto &Val = Ptr.deref<IntegralAP<false>>();
2861 if (!Val.singleWord()) {
2862 uint64_t *NewMemory = new (S.P) uint64_t[Val.numWords()];
2863 Val.take(NewMemory);
2864 }
2865 } else if (T == PT_Float) {
2866 auto &Val = Ptr.deref<Floating>();
2867 if (!Val.singleWord()) {
2868 uint64_t *NewMemory = new (S.P) uint64_t[Val.numWords()];
2869 Val.take(NewMemory);
2870 }
2871 } else if (T == PT_MemberPtr) {
2872 auto &Val = Ptr.deref<MemberPointer>();
2873 unsigned PathLength = Val.getPathLength();
2874 auto *NewPath = new (S.P) const CXXRecordDecl *[PathLength];
2875 std::copy_n(Val.path(), PathLength, NewPath);
2876 Val.takePath(NewPath);
2877 }
2878}
2879
2880template <typename T>
2881static void copyPrimitiveMemory(InterpState &S, const Pointer &Ptr) {
2882 assert(needsAlloc<T>());
2883 if constexpr (std::is_same_v<T, MemberPointer>) {
2884 auto &Val = Ptr.deref<MemberPointer>();
2885 unsigned PathLength = Val.getPathLength();
2886 auto *NewPath = new (S.P) const CXXRecordDecl *[PathLength];
2887 std::copy_n(Val.path(), PathLength, NewPath);
2888 Val.takePath(NewPath);
2889 } else {
2890 auto &Val = Ptr.deref<T>();
2891 if (!Val.singleWord()) {
2892 uint64_t *NewMemory = new (S.P) uint64_t[Val.numWords()];
2893 Val.take(NewMemory);
2894 }
2895 }
2896}
2897
2898static void finishGlobalRecurse(InterpState &S, const Pointer &Ptr) {
2899 if (const Record *R = Ptr.getRecord()) {
2900 for (const Record::Field &Fi : R->fields()) {
2901 if (Fi.Desc->isPrimitive()) {
2902 TYPE_SWITCH_ALLOC(Fi.Desc->getPrimType(), {
2903 copyPrimitiveMemory<T>(S, Ptr.atField(Fi.Offset));
2904 });
2905 } else {
2906 finishGlobalRecurse(S, Ptr.atField(Fi.Offset));
2907 }
2908 }
2909 return;
2910 }
2911
2912 if (const Descriptor *D = Ptr.getFieldDesc(); D && D->isArray()) {
2913 unsigned NumElems = D->getNumElems();
2914 if (NumElems == 0)
2915 return;
2916
2917 if (D->isPrimitiveArray()) {
2918 PrimType PT = D->getPrimType();
2919 if (!needsAlloc(PT))
2920 return;
2921 assert(NumElems >= 1);
2922 const Pointer EP = Ptr.atIndex(0);
2923 bool AllSingleWord = true;
2924 TYPE_SWITCH_ALLOC(PT, {
2925 if (!EP.deref<T>().singleWord()) {
2927 AllSingleWord = false;
2928 }
2929 });
2930 if (AllSingleWord)
2931 return;
2932 for (unsigned I = 1; I != D->getNumElems(); ++I) {
2933 const Pointer EP = Ptr.atIndex(I);
2934 copyPrimitiveMemory(S, EP, PT);
2935 }
2936 } else {
2937 assert(D->isCompositeArray());
2938 for (unsigned I = 0; I != D->getNumElems(); ++I) {
2939 const Pointer EP = Ptr.atIndex(I).narrow();
2940 finishGlobalRecurse(S, EP);
2941 }
2942 }
2943 }
2944}
2945
2947 const Pointer &Ptr = S.Stk.pop<Pointer>();
2948
2949 finishGlobalRecurse(S, Ptr);
2950 if (Ptr.canBeInitialized()) {
2951 Ptr.initialize();
2952 Ptr.activate();
2953 }
2954
2955 return true;
2956}
2957
2958bool InvalidCast(InterpState &S, CodePtr OpPC, CastKind Kind, bool Fatal) {
2959 const SourceLocation &Loc = S.Current->getLocation(OpPC);
2960
2961 switch (Kind) {
2963 S.CCEDiag(Loc, diag::note_constexpr_invalid_cast)
2964 << diag::ConstexprInvalidCastKind::Reinterpret
2965 << S.Current->getRange(OpPC);
2966 return !Fatal;
2968 S.CCEDiag(Loc, diag::note_constexpr_invalid_cast)
2969 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
2970 << S.getLangOpts().CPlusPlus << S.Current->getRange(OpPC);
2971 return !Fatal;
2972 case CastKind::Volatile:
2974 const auto *E = cast<CastExpr>(S.Current->getExpr(OpPC));
2975 if (S.getLangOpts().CPlusPlus)
2976 S.FFDiag(E, diag::note_constexpr_access_volatile_type)
2977 << AK_Read << E->getSubExpr()->getType();
2978 else
2979 S.FFDiag(E);
2980 }
2981
2982 return false;
2983 case CastKind::Dynamic:
2984 assert(!S.getLangOpts().CPlusPlus20);
2985 S.CCEDiag(Loc, diag::note_constexpr_invalid_cast)
2986 << diag::ConstexprInvalidCastKind::Dynamic;
2987 return true;
2988 }
2989 llvm_unreachable("Unhandled CastKind");
2990 return false;
2991}
2992
2993bool Destroy(InterpState &S, CodePtr OpPC, uint32_t I) {
2994 assert(S.Current->getFunction());
2995 // FIXME: We iterate the scope once here and then again in the destroy() call
2996 // below.
2997 for (auto &Local : S.Current->getFunction()->getScope(I).locals_reverse()) {
2998 if (!S.Current->getLocalBlock(Local.Offset)->isInitialized())
2999 continue;
3000 const Pointer &Ptr = S.Current->getLocalPointer(Local.Offset);
3001 if (Ptr.getLifetime() == Lifetime::Ended)
3002 return diagnoseOutOfLifetimeDestroy(S, OpPC, Ptr);
3003 }
3004
3005 S.Current->destroy(I);
3006 return true;
3007}
3008
3009// Perform a cast towards the class of the Decl (either up or down the
3010// hierarchy).
3012 const MemberPointer &MemberPtr,
3013 int32_t BaseOffset,
3014 const RecordDecl *BaseDecl) {
3015 const CXXRecordDecl *Expected;
3016 if (MemberPtr.getPathLength() >= 2)
3017 Expected = MemberPtr.getPathEntry(MemberPtr.getPathLength() - 2);
3018 else
3019 Expected = MemberPtr.getRecordDecl();
3020
3021 assert(Expected);
3022 if (Expected->getCanonicalDecl() != BaseDecl->getCanonicalDecl()) {
3023 // C++11 [expr.static.cast]p12: In a conversion from (D::*) to (B::*),
3024 // if B does not contain the original member and is not a base or
3025 // derived class of the class containing the original member, the result
3026 // of the cast is undefined.
3027 // C++11 [conv.mem]p2 does not cover this case for a cast from (B::*) to
3028 // (D::*). We consider that to be a language defect.
3029 return false;
3030 }
3031
3032 unsigned OldPathLength = MemberPtr.getPathLength();
3033 unsigned NewPathLength = OldPathLength - 1;
3034 bool IsDerivedMember = NewPathLength != 0;
3035 auto NewPath = S.allocMemberPointerPath(NewPathLength);
3036 std::copy_n(MemberPtr.path(), NewPathLength, NewPath);
3037
3038 S.Stk.push<MemberPointer>(MemberPtr.atInstanceBase(BaseOffset, NewPathLength,
3039 NewPath, IsDerivedMember));
3040 return true;
3041}
3042
3044 const MemberPointer &MemberPtr,
3045 int32_t BaseOffset,
3046 const RecordDecl *BaseDecl,
3047 bool IsDerivedMember) {
3048 unsigned OldPathLength = MemberPtr.getPathLength();
3049 unsigned NewPathLength = OldPathLength + 1;
3050
3051 auto NewPath = S.allocMemberPointerPath(NewPathLength);
3052 std::copy_n(MemberPtr.path(), OldPathLength, NewPath);
3053 NewPath[OldPathLength] = cast<CXXRecordDecl>(BaseDecl);
3054
3055 S.Stk.push<MemberPointer>(MemberPtr.atInstanceBase(BaseOffset, NewPathLength,
3056 NewPath, IsDerivedMember));
3057 return true;
3058}
3059
3060/// DerivedToBaseMemberPointer
3062 const RecordDecl *BaseDecl) {
3063 const auto &Ptr = S.Stk.pop<MemberPointer>();
3064
3065 if (!Ptr.isDerivedMember() && Ptr.hasPath())
3066 return castBackMemberPointer(S, Ptr, Off, BaseDecl);
3067
3068 bool IsDerivedMember = Ptr.isDerivedMember() || !Ptr.hasPath();
3069 return appendToMemberPointer(S, Ptr, Off, BaseDecl, IsDerivedMember);
3070}
3071
3072/// BaseToDerivedMemberPointer
3074 const RecordDecl *BaseDecl) {
3075 const auto &Ptr = S.Stk.pop<MemberPointer>();
3076
3077 if (!Ptr.isDerivedMember()) {
3078 // Simply append.
3079 return appendToMemberPointer(S, Ptr, Off, BaseDecl,
3080 /*IsDerivedMember=*/false);
3081 }
3082
3083 return castBackMemberPointer(S, Ptr, Off, BaseDecl);
3084}
3085
3087 S.Stk.push<MemberPointer>(D);
3088 return true;
3089}
3090
3092 const auto &MP = S.Stk.pop<MemberPointer>();
3093
3094 if (!MP.isBaseCastPossible())
3095 return false;
3096
3097 S.Stk.push<Pointer>(MP.getBase());
3098 return true;
3099}
3100
3102 const auto &MP = S.Stk.pop<MemberPointer>();
3103
3104 const ValueDecl *D = MP.getDecl();
3105 const auto *FD = dyn_cast_if_present<FunctionDecl>(D);
3106 if (!FD)
3107 return false;
3108
3109 const auto *Method = dyn_cast<CXXMethodDecl>(FD);
3110 if (!Method)
3111 return false;
3112
3113 const Pointer &Base = MP.getBase();
3114 // The method must be accessible via the base of the MemberPointer.
3115 const CXXRecordDecl *MethodParent = Method->getParent();
3116 if (!Base.getRecord() || Base.getRecord()->getDecl() != MethodParent)
3117 return false;
3118
3119 const auto *Func = S.getContext().getOrCreateFunction(FD);
3120 if (!Func)
3121 return false;
3122 S.Stk.push<Pointer>(Func);
3123 return true;
3124}
3125
3126/// Just append the given Entry to the MemberPointer's path.
3127/// This is used to re-inject APValues into the bytecode interpreter.
3129 bool IsDerived) {
3130 const auto &MemberPtr = S.Stk.pop<MemberPointer>();
3131
3132 unsigned OldPathLength = MemberPtr.getPathLength();
3133 unsigned NewPathLength = OldPathLength + 1;
3134
3135 auto NewPath = S.allocMemberPointerPath(NewPathLength);
3136 std::copy_n(MemberPtr.path(), OldPathLength, NewPath);
3137 NewPath[OldPathLength] = cast<CXXRecordDecl>(Entry);
3138
3140 MemberPtr.withPath(NewPathLength, NewPath, IsDerived));
3141 return true;
3142}
3143
3144template <bool Signed>
3145static bool floatAPCast(InterpState &S, CodePtr OpPC, const Floating &F,
3146 uint32_t BitWidth, uint32_t FPOI) {
3147 APSInt Result(BitWidth, /*IsUnsigned=*/!Signed);
3148 auto Status = F.convertToInteger(Result);
3149
3150 // Float-to-Integral overflow check.
3151 if ((Status & APFloat::opStatus::opInvalidOp) && F.isFinite() &&
3152 !handleOverflow(S, OpPC, F.getAPFloat()))
3153 return false;
3154
3156
3157 auto ResultAP = S.allocAP<IntegralAP<Signed>>(BitWidth);
3158 ResultAP.copy(Result);
3159
3160 S.Stk.push<IntegralAP<Signed>>(ResultAP);
3161
3162 return CheckFloatResult(S, OpPC, F, Status, FPO);
3163}
3164
3165bool CastFloatingIntegralAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth,
3166 uint32_t FPOI) {
3167 Floating F = S.Stk.pop<Floating>();
3168 return floatAPCast<false>(S, OpPC, F, BitWidth, FPOI);
3169}
3170
3171bool CastFloatingIntegralAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth,
3172 uint32_t FPOI) {
3173 Floating F = S.Stk.pop<Floating>();
3174 return floatAPCast<true>(S, OpPC, F, BitWidth, FPOI);
3175}
3176
3177// FIXME: Would be nice to generate this instead of hardcoding it here.
3178constexpr bool OpReturns(Opcode Op) {
3179 return Op == OP_RetVoid || Op == OP_RetValue || Op == OP_NoRet ||
3180 Op == OP_RetSint8 || Op == OP_RetUint8 || Op == OP_RetSint16 ||
3181 Op == OP_RetUint16 || Op == OP_RetSint32 || Op == OP_RetUint32 ||
3182 Op == OP_RetSint64 || Op == OP_RetUint64 || Op == OP_RetIntAP ||
3183 Op == OP_RetIntAPS || Op == OP_RetBool || Op == OP_RetFixedPoint ||
3184 Op == OP_RetPtr || Op == OP_RetMemberPtr || Op == OP_RetFloat ||
3185 Op == OP_EndSpeculation;
3186}
3187
3188#if USE_TAILCALLS
3189PRESERVE_NONE static bool InterpNext(InterpState &S);
3190#endif
3191
3192// The dispatcher functions read the opcode arguments from the
3193// bytecode and call the implementation function.
3194#define GET_INTERPFN_DISPATCHERS
3195#include "Opcodes.inc"
3196#undef GET_INTERPFN_DISPATCHERS
3197
3199// Array of the dispatcher functions defined above.
3201#define GET_INTERPFN_LIST
3202#include "Opcodes.inc"
3203#undef GET_INTERPFN_LIST
3204};
3205
3206#if USE_TAILCALLS
3207// Read the next opcode and call the dispatcher function.
3208PRESERVE_NONE static bool InterpNext(InterpState &S) {
3209 auto Op = S.PC.read<Opcode>();
3210 auto Fn = InterpFunctions[Op];
3211 MUSTTAIL return Fn(S);
3212}
3213#endif
3214
3216 // The current stack frame when we started Interpret().
3217 // This is being used by the ops to determine wheter
3218 // to return from this function and thus terminate
3219 // interpretation.
3220 assert(!S.Current->isRoot());
3221
3222 S.PC = S.Current->getFunction()->getCodeBegin();
3223
3224#if USE_TAILCALLS
3225 return InterpNext(S);
3226#else
3227 while (true) {
3228 auto Op = S.PC.read<Opcode>();
3229 auto Fn = InterpFunctions[Op];
3230
3231 if (!Fn(S))
3232 return false;
3233 if (OpReturns(Op))
3234 break;
3235 }
3236 return true;
3237#endif
3238}
3239
3240/// This is used to implement speculative execution via __builtin_constant_p
3241/// when we generate bytecode.
3242///
3243/// The setup here is that we use the same tailcall mechanism for speculative
3244/// evaluation that we use for the regular one.
3245/// Since each speculative execution ends with an EndSpeculation opcode,
3246/// that one does NOT call InterpNext() but simply returns true.
3247/// This way, we return back to this function when we see an EndSpeculation,
3248/// OR (of course), when we encounter an error and one of the opcodes
3249/// returns false.
3250PRESERVE_NONE static bool BCP(InterpState &S, CodePtr OpPC, int32_t Offset,
3251 PrimType PT) {
3252 // PC after reading the BCP opcode and both Offset/PT arguments.
3253 [[maybe_unused]] CodePtr PCBefore = S.PC;
3254 size_t StackSizeBefore = S.Stk.size();
3255
3256 // Speculation depth must be at least 1 here, since we must have
3257 // passed a StartSpeculation op before.
3258#ifndef NDEBUG
3259 [[maybe_unused]] unsigned DepthBefore = S.SpeculationDepth;
3260 assert(DepthBefore >= 1);
3261#endif
3262
3263 auto SpeculativeInterp = [&S]() -> bool {
3264 // Ignore diagnostics during speculative execution.
3265 PushIgnoreDiags(S);
3266 auto _ = llvm::scope_exit([&]() { PopIgnoreDiags(S); });
3267
3268#if USE_TAILCALLS
3269 auto Op = S.PC.read<Opcode>();
3270 auto Fn = InterpFunctions[Op];
3271 return Fn(S);
3272#else
3273 while (true) {
3274 auto Op = S.PC.read<Opcode>();
3275 auto Fn = InterpFunctions[Op];
3276
3277 if (!Fn(S))
3278 return false;
3279 if (OpReturns(Op))
3280 break;
3281 }
3282 return true;
3283#endif
3284 };
3285
3286 if (SpeculativeInterp()) {
3287 // Speculation must've ended naturally via a EndSpeculation opcode.
3288 assert(S.SpeculationDepth == DepthBefore - 1);
3289 if (PT == PT_Ptr) {
3290 const auto &Ptr = S.Stk.pop<Pointer>();
3291 assert(S.Stk.size() == StackSizeBefore);
3294 } else {
3295 // Pop the result from the stack and return success.
3296 TYPE_SWITCH(PT, S.Stk.discard<T>(););
3297 assert(S.Stk.size() == StackSizeBefore);
3299 }
3300 } else {
3301 // Jump to the end of the speculation, just after the actual EndSpeculation
3302 // op.
3303 S.PC = PCBefore + Offset - align(sizeof(Opcode));
3304
3305 // End the speculation manually since we didn't call EndSpeculation
3306 // naturally.
3307 EndSpeculation(S);
3308
3309 if (!S.inConstantContext())
3310 return Invalid(S, OpPC);
3311
3312 S.Stk.clearTo(StackSizeBefore);
3314 }
3315
3316 // We have already evaluated this speculation's EndSpeculation opcode.
3317 assert(S.SpeculationDepth == DepthBefore - 1);
3318
3319 return true;
3320}
3321
3322} // namespace interp
3323} // namespace clang
Defines the clang::ASTContext interface.
#define V(N, I)
Defines the clang::Expr interface and subclasses for C++ expressions.
static const FunctionDecl * getVirtualOperatorDelete(QualType T)
tooling::Replacements cleanup(const FormatStyle &Style, StringRef Code, ArrayRef< tooling::Range > Ranges, StringRef FileName="<stdin>")
Clean up any erroneous/redundant code in the given Ranges in Code.
static PRESERVE_NONE bool RetValue(InterpState &S)
Definition Interp.cpp:52
static bool CheckTemporary(InterpState &S, CodePtr OpPC, const Block *B, AccessKinds AK)
Definition Interp.cpp:194
static bool CheckGlobal(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Definition Interp.cpp:221
static bool Jf(InterpState &S, CodePtr OpPC, int32_t Offset)
Definition Interp.cpp:73
static bool Jmp(InterpState &S, CodePtr OpPC, int32_t Offset)
Definition Interp.cpp:60
static bool diagnoseUnknownDecl(InterpState &S, CodePtr OpPC, const ValueDecl *D, AccessKinds AK=AK_Read)
Definition Interp.cpp:102
static void diagnoseNonConstVariable(InterpState &S, CodePtr OpPC, const ValueDecl *VD, AccessKinds AK=AK_Read)
Definition Interp.cpp:152
static bool isModification(AccessKinds AK)
Definition Interp.cpp:147
#define MUSTTAIL
Definition Interp.cpp:46
static void noteValueLocation(InterpState &S, const Block *B)
Definition Interp.cpp:88
static void diagnoseMissingInitializer(InterpState &S, CodePtr OpPC, const ValueDecl *VD)
Definition Interp.cpp:81
static bool Jt(InterpState &S, CodePtr OpPC, int32_t Offset)
Definition Interp.cpp:65
#define PRESERVE_NONE
Definition Interp.h:50
static StringRef getIdentifier(const Token &Tok)
#define TYPE_SWITCH_ALLOC(Expr, B)
Definition PrimType.h:297
#define TYPE_SWITCH(Expr, B)
Definition PrimType.h:223
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition APValue.h:122
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:223
bool hasSameFunctionTypeIgnoringExceptionSpec(QualType T, QualType U) const
Determine whether two function types are the same, ignoring exception specifications in cases where t...
QualType getLValueReferenceType(QualType T, bool SpelledAsLValue=true) const
Return the uniqued reference to the type for an lvalue reference to the specified type.
QualType getConstantArrayType(QualType EltTy, const llvm::APInt &ArySize, const Expr *SizeExpr, ArraySizeModifier ASM, unsigned IndexTypeQuals) const
Return the unique reference to the type for a constant array of the specified element type.
QualType getBaseElementType(const ArrayType *VAT) const
Return the innermost element type of an array type.
bool hasSimilarType(QualType T1, QualType T2) const
Determine if two types are similar, according to the C++ rules.
DiagnosticsEngine & getDiagnostics() const
const TargetInfo & getTargetInfo() const
Definition ASTContext.h:924
CanQualType getCanonicalTagType(const TagDecl *TD) const
AccessSpecifier Access
The access along this inheritance path.
BasePaths - Represents the set of paths from a derived class to one of its (direct or indirect) bases...
CXXBasePath & front()
paths_iterator begin()
paths_iterator end()
Represents a C++ destructor within a class.
Definition DeclCXX.h:2898
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2145
bool isVirtual() const
Definition DeclCXX.h:2200
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
CXXRecordDecl * getCanonicalDecl() override
Retrieves the "canonical" declaration of the given declaration.
Definition DeclCXX.h:522
bool isDerivedFrom(const CXXRecordDecl *Base) const
Determine whether this class is derived from the class Base.
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:2949
Expr * getCallee()
Definition Expr.h:3096
unsigned getNumArgs() const
getNumArgs - Return the number of actual arguments to this call.
Definition Expr.h:3140
Expr ** getArgs()
Retrieve the call arguments.
Definition Expr.h:3143
QualType getCallReturnType(const ASTContext &Ctx) const
getCallReturnType - Get the return type of the call expr.
Definition Expr.cpp:1609
A reference to a declared variable, function, enum, etc.
Definition Expr.h:1276
ValueDecl * getDecl()
Definition Expr.h:1344
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
bool isInvalidDecl() const
Definition DeclBase.h:596
SourceLocation getLocation() const
Definition DeclBase.h:447
bool hasAttr() const
Definition DeclBase.h:585
virtual SourceRange getSourceRange() const LLVM_READONLY
Source range that this declaration covers.
Definition DeclBase.h:435
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Represents an enum.
Definition Decl.h:4046
unsigned getNumNegativeBits() const
Returns the width in bits required to store all the negative enumerators of this enum.
Definition Decl.h:4256
void getValueRange(llvm::APInt &Max, llvm::APInt &Min) const
Calculates the [Min,Max) values the enum can store based on the NumPositiveBits and NumNegativeBits.
Definition Decl.cpp:5186
This represents one expression.
Definition Expr.h:112
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition Expr.cpp:283
QualType getType() const
Definition Expr.h:144
LangOptions::FPExceptionModeKind getExceptionMode() const
static FPOptions getFromOpaqueInt(storage_type Value)
RoundingMode getRoundingMode() const
Represents a member of a struct/union/class.
Definition Decl.h:3195
Represents a function declaration or definition.
Definition Decl.h:2027
Stmt * getBody(const FunctionDecl *&Definition) const
Retrieve the body (definition) of the function.
Definition Decl.cpp:3256
QualType getReturnType() const
Definition Decl.h:2876
StorageClass getStorageClass() const
Returns the storage class as written in the source.
Definition Decl.h:2919
bool isConstexpr() const
Whether this is a (C++11) constexpr function or constexpr constructor.
Definition Decl.h:2497
bool isPureVirtual() const
Whether this virtual function is pure, i.e.
Definition Decl.h:2380
bool isUsableAsGlobalAllocationFunctionInConstantEvaluation(UnsignedOrNone *AlignmentParam=nullptr, bool *IsNothrow=nullptr) const
Determines whether this function is one of the replaceable global allocation functions described in i...
Definition Decl.cpp:3403
FunctionDecl * getDefinition()
Get the definition for this declaration.
Definition Decl.h:2309
bool hasBody(const FunctionDecl *&Definition) const
Returns true if the function has a body.
Definition Decl.cpp:3176
bool isDefined(const FunctionDecl *&Definition, bool CheckForPendingFriendDefinition=false) const
Returns true if the function has a definition that does not need to be instantiated.
Definition Decl.cpp:3223
@ FPE_Ignore
Assume that floating-point exceptions are masked.
This represents a decl that may have a name.
Definition Decl.h:274
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Definition Decl.h:295
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Definition Decl.h:301
A (possibly-)qualified type.
Definition TypeBase.h:937
bool isVolatileQualified() const
Determine whether this type is volatile-qualified.
Definition TypeBase.h:8531
void addConst()
Add the const type qualifier to this QualType.
Definition TypeBase.h:1171
bool isConstQualified() const
Determine whether this type is const-qualified.
Definition TypeBase.h:8520
Represents a struct/union/class.
Definition Decl.h:4360
Encodes a location in the source.
Stmt - This represents one statement.
Definition Stmt.h:86
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition Stmt.cpp:343
TagDecl * getCanonicalDecl() override
Retrieves the "canonical" declaration of the given declaration.
Definition Decl.cpp:4896
uint64_t getPointerWidth(LangAS AddrSpace) const
Return the width of pointers on this target, for the specified address space.
Definition TargetInfo.h:490
The base class of the type hierarchy.
Definition TypeBase.h:1875
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition Type.h:26
RecordDecl * getAsRecordDecl() const
Retrieves the RecordDecl this type refers to.
Definition Type.h:41
bool isArrayType() const
Definition TypeBase.h:8783
bool isPointerType() const
Definition TypeBase.h:8684
CanQualType getCanonicalTypeUnqualified() const
bool isIntegerType() const
isIntegerType() does not include complex integers (a GCC extension).
Definition TypeBase.h:9094
bool isReferenceType() const
Definition TypeBase.h:8708
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition Type.cpp:789
bool isIntegralOrEnumerationType() const
Determine whether this type is an integral or enumeration type.
Definition TypeBase.h:9172
bool isAnyComplexType() const
Definition TypeBase.h:8819
bool isPointerOrReferenceType() const
Definition TypeBase.h:8688
bool isRecordType() const
Definition TypeBase.h:8811
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition Decl.h:712
QualType getType() const
Definition Decl.h:723
Represents a variable declaration or definition.
Definition Decl.h:932
bool isConstexpr() const
Whether this variable is (C++11) constexpr.
Definition Decl.h:1591
bool isStaticDataMember() const
Determines whether this is a static data member.
Definition Decl.h:1304
A memory block, either on the stack or in the heap.
Definition InterpBlock.h:44
unsigned getSize() const
Returns the size of the block.
Definition InterpBlock.h:87
const T & deref() const
bool isExtern() const
Checks if the block is extern.
Definition InterpBlock.h:77
const Descriptor * getDescriptor() const
Returns the block's descriptor.
Definition InterpBlock.h:73
bool isStatic() const
Checks if the block has static storage duration.
Definition InterpBlock.h:79
bool isTemporary() const
Checks if the block is temporary.
Definition InterpBlock.h:81
std::byte * rawData()
Returns a pointer to the raw data, including metadata.
bool isInitialized() const
Returns whether the data of this block has been initialized via invoking the Ctor func.
Definition InterpBlock.h:92
bool isDynamic() const
Definition InterpBlock.h:83
UnsignedOrNone getDeclID() const
Returns the declaration ID.
Definition InterpBlock.h:89
bool isDummy() const
Definition InterpBlock.h:84
unsigned getEvalID() const
The Evaluation ID this block was created in.
Definition InterpBlock.h:94
bool isWeak() const
Definition InterpBlock.h:82
bool isAccessible() const
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:59
Compilation context for expressions.
Definition Compiler.h:112
unsigned collectBaseOffset(const RecordDecl *BaseDecl, const RecordDecl *DerivedDecl) const
Definition Context.cpp:745
const Function * getOrCreateFunction(const FunctionDecl *FuncDecl)
Definition Context.cpp:612
ASTContext & getASTContext() const
Returns the AST context.
Definition Context.h:107
OptPrimType classify(QualType T) const
Classifies a type.
Definition Context.cpp:464
const CXXMethodDecl * getOverridingFunction(const CXXRecordDecl *DynamicDecl, const CXXRecordDecl *StaticDecl, const CXXMethodDecl *InitialFunction) const
Definition Context.cpp:576
Manages dynamic memory allocations done during bytecode interpretation.
std::optional< Form > getAllocationForm(const Expr *Source) const
Checks whether the allocation done at the given source is an array allocation.
bool deallocate(const Expr *Source, const Block *BlockToDelete)
Deallocate the given source+block combination.
Wrapper around fixed point types.
Definition FixedPoint.h:23
std::string toDiagnosticString(const ASTContext &Ctx) const
Definition FixedPoint.h:81
If a Floating is constructed from Memory, it DOES NOT OWN THAT MEMORY.
Definition Floating.h:35
bool isFinite() const
Definition Floating.h:151
APFloat::opStatus convertToInteger(APSInt &Result) const
Definition Floating.h:71
APFloat getAPFloat() const
Definition Floating.h:64
Base class for stack frames, shared between VM and walker.
Definition Frame.h:25
Bytecode function.
Definition Function.h:99
bool hasExplicitThisPointer() const
Definition Function.h:227
Scope & getScope(unsigned Idx)
Returns a specific scope.
Definition Function.h:174
CodePtr getCodeBegin() const
Returns a pointer to the start of the code.
Definition Function.h:129
bool isDestructor() const
Checks if the function is a destructor.
Definition Function.h:197
bool isVirtual() const
Checks if the function is virtual.
Definition Function.h:184
bool hasNonNullAttr() const
Definition Function.h:158
bool isFullyCompiled() const
Checks if the function is fully done compiling.
Definition Function.h:224
bool isConstructor() const
Checks if the function is a constructor.
Definition Function.h:189
const FunctionDecl * getDecl() const
Returns the original FunctionDecl.
Definition Function.h:134
bool hasBody() const
Checks if the function already has a body attached.
Definition Function.h:235
bool isConstexpr() const
Definition Function.h:186
unsigned getWrittenArgSize() const
Definition Function.h:251
unsigned getArgSize() const
Returns the size of the argument stack.
Definition Function.h:126
bool isLambdaStaticInvoker() const
Returns whether this function is a lambda static invoker, which we generate custom byte code for.
Definition Function.h:205
bool isValid() const
Checks if the function is valid to call.
Definition Function.h:181
If an IntegralAP is constructed from Memory, it DOES NOT OWN THAT MEMORY.
Definition IntegralAP.h:36
void copy(const APInt &V)
Definition IntegralAP.h:78
Wrapper around numeric types.
Definition Integral.h:69
static std::enable_if_t<!std::is_same_v< ValT, IntegralKind >, Integral > from(ValT V, unsigned NumBits=0)
Definition Integral.h:320
Frame storing local variables.
Definition InterpFrame.h:27
static void free(InterpFrame *F)
Definition InterpFrame.h:62
const Expr * getExpr(CodePtr PC) const
InterpFrame * Caller
The frame of the previous function.
Definition InterpFrame.h:30
SourceInfo getSource(CodePtr PC) const
Map a location to a source.
Block * getLocalBlock(unsigned Offset) const
SourceLocation getLocation(CodePtr PC) const
const Pointer & getThis() const
Returns the 'this' pointer.
const Function * getFunction() const
Returns the current function.
Definition InterpFrame.h:93
SourceRange getRange(CodePtr PC) const
unsigned getArgSize() const
bool isRoot() const
Checks if the frame is a root frame - return should quit the interpreter.
Pointer getLocalPointer(unsigned Offset) const
Returns a pointer to a local variables.
unsigned getDepth() const
void destroy(unsigned Idx)
Invokes the destructors for a scope.
static size_t allocSize(const Function *F)
Returns the number of bytes needed to allocate an InterpFrame for the given function.
Definition InterpFrame.h:51
void clearTo(size_t NewSize)
T pop()
Returns the value from the top of the stack and removes it.
Definition InterpStack.h:39
void push(Tys &&...Args)
Constructs a value in place on the top of the stack.
Definition InterpStack.h:33
size_t size() const
Returns the size of the stack in bytes.
Definition InterpStack.h:78
void discard()
Discards the top value from the stack.
Definition InterpStack.h:50
T & peek() const
Returns a reference to the value on the top of the stack.
Definition InterpStack.h:63
Interpreter context.
Definition InterpState.h:43
bool lifetimeStartedInEvaluation(const Block *B) const
Context & getContext() const
Definition InterpState.h:79
bool initializingBlock(const Block *B) const
DynamicAllocator & getAllocator()
Definition InterpState.h:83
Context & Ctx
Interpreter Context.
bool noteStep(CodePtr OpPC)
Note that a step has been executed.
const unsigned EvalID
ID identifying this evaluation.
InterpStack & Stk
Temporary stack.
bool checkingConstantDestruction() const
Return if we're checking if a global variable has a constant destructor.
const VarDecl * EvaluatingDecl
Declaration we're initializing/evaluting, if any.
InterpFrame * Current
The current frame.
const CXXRecordDecl ** allocMemberPointerPath(unsigned Length)
llvm::SmallVector< PtrView > InitializingPtrs
List of blocks we're currently running either constructors or destructors for.
T allocAP(unsigned BitWidth)
StdAllocatorCaller getStdAllocatorCaller(StringRef Name) const
Program & P
Reference to the module containing all bytecode.
unsigned getPathLength() const
Return the length of the cast path.
PrimType value_or(PrimType PT) const
Definition PrimType.h:88
A pointer to a memory block, live or dead.
Definition Pointer.h:405
Pointer narrow() const
Restricts the scope of an array element pointer.
Definition Pointer.h:494
UnsignedOrNone getDeclID() const
Returns the declaration ID.
Definition Pointer.h:788
Pointer stripBaseCasts() const
Strip base casts from this Pointer.
Definition Pointer.h:998
bool isVolatile() const
Checks if an object or a subfield is volatile.
Definition Pointer.h:781
bool isInitialized() const
Checks if an object was initialized.
Definition Pointer.cpp:551
bool isStatic() const
Checks if the storage is static.
Definition Pointer.h:706
bool isDynamic() const
Checks if the storage has been dynamically allocated.
Definition Pointer.h:721
bool inUnion() const
Definition Pointer.h:623
bool isZeroSizeArray() const
Checks if the pointer is pointing to a zero-size array.
Definition Pointer.h:857
Pointer atIndex(uint64_t Idx) const
Offsets a pointer inside an array.
Definition Pointer.h:471
bool isDummy() const
Checks if the pointer points to a dummy value.
Definition Pointer.h:762
Pointer atFieldSub(unsigned Off) const
Subtract the given offset from the current Base and Offset of the pointer.
Definition Pointer.h:487
bool isExtern() const
Checks if the storage is extern.
Definition Pointer.h:700
int64_t getIndex() const
Returns the index into an array.
Definition Pointer.h:824
bool isActive() const
Checks if the object is active.
Definition Pointer.h:752
bool isConst() const
Checks if an object or a subfield is mutable.
Definition Pointer.h:769
Pointer atField(unsigned Off) const
Creates a pointer to a field.
Definition Pointer.h:481
T & deref() const
Dereferences the pointer, if it's live.
Definition Pointer.h:875
bool isMutable() const
Checks if the field is mutable.
Definition Pointer.h:732
bool isConstInMutable() const
Definition Pointer.h:774
unsigned getNumElems() const
Returns the number of elements.
Definition Pointer.h:808
bool isUnknownSizeArray() const
Checks if the structure is an array of unknown size.
Definition Pointer.h:636
bool isIntegralPointer() const
Definition Pointer.h:680
QualType getType() const
Returns the type of the innermost field.
Definition Pointer.h:574
bool isArrayElement() const
Checks if the pointer points to an array.
Definition Pointer.h:642
bool pointsToStringLiteral() const
Definition Pointer.cpp:834
void initialize() const
Initializes a field.
Definition Pointer.h:920
bool isArrayRoot() const
Whether this array refers to an array, but not to the first element.
Definition Pointer.h:615
bool isLive() const
Checks if the pointer is live.
Definition Pointer.h:522
Pointer getBase() const
Returns a pointer to the object of which this pointer is a field.
Definition Pointer.h:559
uint64_t getByteOffset() const
Returns the byte offset from the start.
Definition Pointer.h:797
bool isTypeidPointer() const
Definition Pointer.h:682
std::string toDiagnosticString(const ASTContext &Ctx) const
Converts the pointer to a string usable in diagnostics.
Definition Pointer.cpp:538
bool isZero() const
Checks if the pointer is null.
Definition Pointer.h:508
bool isConstexprUnknown() const
Definition Pointer.h:898
const IntPointer & asIntPointer() const
Definition Pointer.h:666
bool isRoot() const
Pointer points directly to a block.
Definition Pointer.h:649
const Descriptor * getDeclDesc() const
Accessor for information about the declaration site.
Definition Pointer.h:536
void activate() const
Activates a field.
Definition Pointer.h:951
static bool pointToSameBlock(const Pointer &A, const Pointer &B)
Checks if both given pointers point to the same block.
Definition Pointer.cpp:812
bool isOnePastEnd() const
Checks if the index is one past end.
Definition Pointer.h:832
uint64_t getIntegerRepresentation() const
Definition Pointer.h:453
bool isPastEnd() const
Checks if the pointer points past the end of the object.
Definition Pointer.h:846
const FieldDecl * getField() const
Returns the field information.
Definition Pointer.h:693
Pointer expand() const
Expands a pointer to the containing array, undoing narrowing.
Definition Pointer.h:501
bool isElementPastEnd() const
Checks if the pointer is an out-of-bounds element pointer.
Definition Pointer.h:854
void startLifetime() const
Start the lifetime of this pointer.
Definition Pointer.h:983
bool isBlockPointer() const
Definition Pointer.h:679
const FunctionPointer & asFunctionPointer() const
Definition Pointer.h:670
const Block * block() const
Definition Pointer.h:814
bool isFunctionPointer() const
Definition Pointer.h:681
Pointer getDeclPtr() const
Definition Pointer.h:590
const Descriptor * getFieldDesc() const
Accessors for information about the innermost field.
Definition Pointer.h:564
PtrView view() const
Definition Pointer.h:461
bool canBeInitialized() const
If this pointer has an InlineDescriptor we can use to initialize.
Definition Pointer.h:655
Lifetime getLifetime() const
Definition Pointer.h:957
bool isField() const
Checks if the item is a field in an object.
Definition Pointer.h:528
const Record * getRecord() const
Returns the record descriptor of a class.
Definition Pointer.h:685
UnsignedOrNone getCurrentDecl() const
Returns the current declaration ID.
Definition Program.h:162
Structure/Class descriptor.
Definition Record.h:25
const RecordDecl * getDecl() const
Returns the underlying declaration.
Definition Record.h:65
unsigned getNumVirtualBases() const
Definition Record.h:121
llvm::iterator_range< LocalVectorTy::const_reverse_iterator > locals_reverse() const
Definition Function.h:57
Describes the statement/declaration an opcode was generated from.
Definition Source.h:76
bool checkingForUndefinedBehavior() const
Are we checking an expression for overflow?
Definition State.h:123
OptionalDiagnostic Note(SourceLocation Loc, diag::kind DiagId)
Add a note to a prior diagnostic.
Definition State.cpp:66
OptionalDiagnostic FFDiag(SourceLocation Loc, diag::kind DiagId=diag::note_invalid_subexpr_in_const_expr, unsigned ExtraNotes=0)
Diagnose that the evaluation could not be folded (FF => FoldFailure)
Definition State.cpp:21
ASTContext & getASTContext() const
Definition State.h:92
bool noteUndefinedBehavior() const
Note that we hit something that was technically undefined behavior, but that we can evaluate past it ...
Definition State.h:112
OptionalDiagnostic CCEDiag(SourceLocation Loc, diag::kind DiagId=diag::note_invalid_subexpr_in_const_expr, unsigned ExtraNotes=0)
Diagnose that the evaluation does not produce a C++11 core constant expression.
Definition State.cpp:44
const LangOptions & getLangOpts() const
Definition State.h:93
bool checkingPotentialConstantExpression() const
Are we checking whether the expression is a potential constant expression?
Definition State.h:119
Defines the clang::TargetInfo interface.
bool arePotentiallyOverlappingStringLiterals(const Pointer &LHS, const Pointer &RHS)
Definition Interp.cpp:2800
bool GetPtrFieldPop(InterpState &S, CodePtr OpPC, uint32_t Off)
Definition Interp.cpp:1603
bool GetMemberPtrBase(InterpState &S)
Definition Interp.cpp:3091
bool PseudoDtor(InterpState &S, CodePtr OpPC)
Ends the lifetime of the pop'd pointer.
Definition Interp.cpp:2478
static bool CheckCallDepth(InterpState &S, CodePtr OpPC)
Definition Interp.cpp:1150
const InterpFn InterpFunctions[]
Definition Interp.cpp:3200
static bool diagnoseCallableDecl(InterpState &S, CodePtr OpPC, const FunctionDecl *DiagDecl)
Definition Interp.cpp:1040
constexpr bool OpReturns(Opcode Op)
Definition Interp.cpp:3178
bool GetTypeid(InterpState &S, const Type *TypePtr, const Type *TypeInfoType)
Typeid support.
Definition Interp.cpp:2751
bool CastPointerIntegralAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth)
Definition Interp.cpp:2702
static bool CheckVolatile(InterpState &S, CodePtr OpPC, const Pointer &Ptr, AccessKinds AK)
Definition Interp.cpp:687
bool CastPointerIntegralAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth)
Definition Interp.cpp:2689
bool CheckInit(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Checks if a value can be initialized.
Definition Interp.cpp:1032
bool CheckFunctionDecl(InterpState &S, CodePtr OpPC, const FunctionDecl *FD)
Opcode. Check if the function decl can be called at compile time.
Definition Interp.cpp:1750
bool handleOverflow(InterpState &S, CodePtr OpPC, const T &SrcValue)
static bool appendToMemberPointer(InterpState &S, const MemberPointer &MemberPtr, int32_t BaseOffset, const RecordDecl *BaseDecl, bool IsDerivedMember)
Definition Interp.cpp:3043
static bool CheckCallable(InterpState &S, CodePtr OpPC, const Function *F)
Definition Interp.cpp:1122
bool StartThisLifetime(InterpState &S)
Definition Interp.cpp:2422
void cleanupAfterFunctionCall(InterpState &S, const Function *Func)
Definition Interp.cpp:267
static bool runRecordDestructor(InterpState &S, CodePtr OpPC, const Pointer &BasePtr, const Descriptor *Desc)
Definition Interp.cpp:1336
bool CheckConstant(InterpState &S, CodePtr OpPC, const Descriptor *Desc, AccessKinds AK)
Checks if the Descriptor is of a constexpr or const global variable.
Definition Interp.cpp:463
bool GetTypeidPtr(InterpState &S, CodePtr OpPC, const Type *TypeInfoType)
Definition Interp.cpp:2756
bool LT(InterpState &S, CodePtr OpPC)
Definition Interp.h:1517
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:576
bool CheckNewDeleteForms(InterpState &S, CodePtr OpPC, DynamicAllocator::Form AllocForm, DynamicAllocator::Form DeleteForm, const Descriptor *D, const Expr *NewExpr)
Diagnose mismatched new[]/delete or new/delete[] pairs.
Definition Interp.cpp:1234
static void setLifeStateRecurse(PtrView Ptr, Lifetime L)
Definition Interp.cpp:2446
bool PushIgnoreDiags(InterpState &S)
Definition Interp.h:3622
bool CheckGlobalLoad(InterpState &S, CodePtr OpPC, const Block *B)
Checks a direct load of a primitive value from a global or local variable.
Definition Interp.cpp:826
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:1276
bool EndLifetime(InterpState &S, CodePtr OpPC)
Ends the lifetime of the peek'd pointer.
Definition Interp.cpp:2468
static bool getDynamicDecl(InterpState &S, CodePtr OpPC, PtrView TypePtr, const CXXRecordDecl *&DynamicDecl)
Definition Interp.cpp:1969
bool CastMemberPtrDerivedPop(InterpState &S, int32_t Off, const RecordDecl *BaseDecl)
BaseToDerivedMemberPointer.
Definition Interp.cpp:3073
static DynamicCastResult findRecordBase(const ASTContext &Ctx, const Record *R, QualType Needle)
Definition Interp.cpp:2051
static bool CheckWeak(InterpState &S, CodePtr OpPC, const Block *B)
Definition Interp.cpp:809
bool CheckPointerToIntegralCast(InterpState &S, CodePtr OpPC, const Pointer &Ptr, unsigned BitWidth)
Definition Interp.cpp:2661
static bool RunDestructors(InterpState &S, CodePtr OpPC, const Block *B)
Definition Interp.cpp:1363
bool GetPtrField(InterpState &S, CodePtr OpPC, uint32_t Off)
1) Peeks a Pointer 2) Pushes Pointer.atField(Off) on the stack
Definition Interp.cpp:1598
bool CheckActive(InterpState &S, CodePtr OpPC, const Pointer &Ptr, AccessKinds AK, bool WillActivate)
Definition Interp.cpp:324
static bool CheckNonNullArgs(InterpState &S, CodePtr OpPC, const Function *F, const CallExpr *CE, unsigned ArgSize)
Definition Interp.cpp:1314
static void finishGlobalRecurse(InterpState &S, const Pointer &Ptr)
Definition Interp.cpp:2898
bool CheckSubobject(InterpState &S, CodePtr OpPC, const Pointer &Ptr, CheckSubobjectKind CSK)
Checks if Ptr is a one-past-the-end pointer.
Definition Interp.cpp:565
bool handleFixedPointOverflow(InterpState &S, CodePtr OpPC, const FixedPoint &FP)
Definition Interp.cpp:2640
bool PopIgnoreDiags(InterpState &S)
Definition Interp.h:3634
bool GetMemberPtrDecl(InterpState &S)
Definition Interp.cpp:3101
bool handleReference(InterpState &S, CodePtr OpPC, Block *B)
Definition Interp.cpp:2732
bool CheckBitCast(InterpState &S, CodePtr OpPC, const Type *TargetType, bool SrcIsVoidPtr)
Definition Interp.cpp:1765
bool CopyMemberPtrPath(InterpState &S, const RecordDecl *Entry, bool IsDerived)
Just append the given Entry to the MemberPointer's path.
Definition Interp.cpp:3128
static bool getField(InterpState &S, CodePtr OpPC, const Pointer &Ptr, uint32_t Off)
Definition Interp.cpp:1555
static void startLifetimeRecurse(PtrView Ptr)
Definition Interp.cpp:2397
static bool hasVirtualDestructor(QualType T)
Definition Interp.cpp:1394
bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr, AccessKinds AK)
Checks if a value can be loaded from a block.
Definition Interp.cpp:878
static bool getBase(InterpState &S, CodePtr OpPC, const Pointer &Ptr, uint32_t Off, bool NullOK)
Definition Interp.cpp:1608
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition PrimType.h:201
bool CheckBCPResult(InterpState &S, const Pointer &Ptr)
Definition Interp.cpp:304
PRESERVE_NONE bool EndSpeculation(InterpState &S)
Definition Interp.h:3666
bool diagnoseShiftFailure(InterpState &S, CodePtr OpPC, ShiftFailure Failure, const APSInt *Value, unsigned Bits)
Definition Interp.cpp:240
bool CheckDynamicMemoryAllocation(InterpState &S, CodePtr OpPC)
Checks if dynamic memory allocation is available in the current language mode.
Definition Interp.cpp:1225
bool CheckLive(InterpState &S, CodePtr OpPC, const Pointer &Ptr, AccessKinds AK)
Checks if a pointer is live and accessible.
Definition Interp.cpp:433
bool GetPtrDerivedPop(InterpState &S, CodePtr OpPC, uint32_t Off, bool NullOK, const Type *TargetType)
Definition Interp.cpp:1646
bool DiagTypeid(InterpState &S, CodePtr OpPC)
Definition Interp.cpp:2792
bool CheckFinalLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
This is not used by any of the opcodes directly.
Definition Interp.cpp:955
static bool checkConstructor(InterpState &S, CodePtr OpPC, const Function *Func, const Pointer &ThisPtr)
Definition Interp.cpp:1685
llvm::APInt APInt
Definition FixedPoint.h:19
void diagnoseEnumValue(InterpState &S, CodePtr OpPC, const EnumDecl *ED, const APSInt &Value)
Definition Interp.cpp:1502
bool isConstexprUnknown(const Block *B)
Definition Interp.cpp:292
bool StartThisLifetime1(InterpState &S)
Definition Interp.cpp:2433
bool RVOPtr(InterpState &S)
Definition Interp.h:3177
bool InvalidDeclRef(InterpState &S, CodePtr OpPC, const DeclRefExpr *DR, bool InitializerFailed)
Definition Interp.cpp:1281
bool CheckNull(InterpState &S, CodePtr OpPC, const Pointer &Ptr, CheckSubobjectKind CSK)
Checks if a pointer is null.
Definition Interp.cpp:533
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:1252
bool CheckFloatResult(InterpState &S, CodePtr OpPC, const Floating &Result, APFloat::opStatus Status, FPOptions FPO)
Checks if the result of a floating-point operation is valid in the current context.
Definition Interp.cpp:1178
PrimType
Enumeration of the primitive types of the VM.
Definition PrimType.h:34
static bool CheckInvoke(InterpState &S, CodePtr OpPC, const Pointer &Ptr, bool IsCtor, bool IsDtor)
Definition Interp.cpp:1019
bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const CallExpr *Call, uint32_t BuiltinID)
Interpret a builtin function.
bool CallVar(InterpState &S, CodePtr OpPC, const Function *Func, uint32_t VarArgSize)
Definition Interp.cpp:1821
constexpr bool needsAlloc()
Definition PrimType.h:131
bool(*)(InterpState &) PRESERVE_NONE InterpFn
Definition Interp.cpp:3198
bool InvalidShuffleVectorIndex(InterpState &S, CodePtr OpPC, uint32_t Index)
Definition Interp.cpp:2653
bool CheckDummy(InterpState &S, CodePtr OpPC, const Block *B, AccessKinds AK)
Checks if a pointer is a dummy pointer.
Definition Interp.cpp:1296
static bool diagnoseOutOfLifetimeDestroy(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Definition Interp.cpp:1706
static bool floatAPCast(InterpState &S, CodePtr OpPC, const Floating &F, uint32_t BitWidth, uint32_t FPOI)
Definition Interp.cpp:3145
bool CheckNewTypeMismatch(InterpState &S, CodePtr OpPC, const Expr *E, std::optional< uint64_t > ArraySize)
Check if the initializer and storage types of a placement-new expression match.
Definition Interp.cpp:2495
bool checkDestructor(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Definition Interp.cpp:1722
bool CheckRange(InterpState &S, CodePtr OpPC, PtrView Ptr, AccessKinds AK)
Checks if a pointer is in range.
Definition Interp.cpp:544
bool CheckLiteralType(InterpState &S, CodePtr OpPC, const Type *T)
Definition Interp.cpp:1523
bool CheckArray(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Checks if the array is offsetable.
Definition Interp.cpp:425
bool GetPtrBase(InterpState &S, CodePtr OpPC, uint32_t Off)
Definition Interp.cpp:1637
static void compileFunction(InterpState &S, const Function *Func)
Definition Interp.cpp:1810
bool CheckThis(InterpState &S, CodePtr OpPC)
Checks the 'this' pointer.
Definition Interp.cpp:1161
bool CastFloatingIntegralAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth, uint32_t FPOI)
Definition Interp.cpp:3171
bool CheckIntegralAddressCast(InterpState &S, CodePtr OpPC, unsigned BitWidth)
Definition Interp.cpp:2684
static void copyPrimitiveMemory(InterpState &S, const Pointer &Ptr, PrimType T)
Definition Interp.cpp:2851
bool Destroy(InterpState &S, CodePtr OpPC, uint32_t I)
Definition Interp.cpp:2993
bool CheckMutable(InterpState &S, CodePtr OpPC, PtrView Ptr, AccessKinds AK)
Checks if a pointer points to a mutable field.
Definition Interp.cpp:660
size_t primSize(PrimType Type)
Returns the size of a primitive type in bytes.
Definition PrimType.cpp:24
bool Free(InterpState &S, CodePtr OpPC, bool DeleteIsArrayForm, bool IsGlobalDelete)
Definition Interp.cpp:1401
bool InvalidNewDeleteExpr(InterpState &S, CodePtr OpPC, const Expr *E)
Definition Interp.cpp:2593
bool CallBI(InterpState &S, CodePtr OpPC, const CallExpr *CE, uint32_t BuiltinID)
Definition Interp.cpp:2324
bool CheckLocalLoad(InterpState &S, CodePtr OpPC, const Block *B)
Definition Interp.cpp:857
bool CheckExtern(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Checks if the variable has externally defined storage.
Definition Interp.cpp:406
static bool CheckLifetime(InterpState &S, CodePtr OpPC, Lifetime LT, const Block *B, AccessKinds AK)
Definition Interp.cpp:792
bool CheckStore(InterpState &S, CodePtr OpPC, const Pointer &Ptr, bool WillBeActivated)
Checks if a value can be stored in a block.
Definition Interp.cpp:988
bool FinishInitGlobal(InterpState &S)
Definition Interp.cpp:2946
bool GetPtrBasePop(InterpState &S, CodePtr OpPC, uint32_t Off, bool NullOK)
Definition Interp.cpp:1641
bool DiagnoseUninitialized(InterpState &S, CodePtr OpPC, const Pointer &Ptr, AccessKinds AK)
Definition Interp.cpp:734
llvm::BitVector collectNonNullArgs(const FunctionDecl *F, ArrayRef< const Expr * > Args)
static bool castBackMemberPointer(InterpState &S, const MemberPointer &MemberPtr, int32_t BaseOffset, const RecordDecl *BaseDecl)
Definition Interp.cpp:3011
bool CallPtr(InterpState &S, CodePtr OpPC, uint32_t ArgSize, const CallExpr *CE)
Definition Interp.cpp:2335
bool CastFloatingIntegralAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth, uint32_t FPOI)
Definition Interp.cpp:3165
bool MarkDestroyed(InterpState &S, CodePtr OpPC)
Definition Interp.cpp:2486
bool CallVirt(InterpState &S, CodePtr OpPC, const Function *Func, uint32_t VarArgSize)
Definition Interp.cpp:2229
bool CheckConst(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Checks if a pointer points to const storage.
Definition Interp.cpp:603
bool Interpret(InterpState &S)
Interpreter entry point.
Definition Interp.cpp:3215
bool GetMemberPtr(InterpState &S, const ValueDecl *D)
Definition Interp.cpp:3086
static PRESERVE_NONE bool BCP(InterpState &S, CodePtr OpPC, int32_t Offset, PrimType PT)
This is used to implement speculative execution via __builtin_constant_p when we generate bytecode.
Definition Interp.cpp:3250
bool CastMemberPtrBasePop(InterpState &S, int32_t Off, const RecordDecl *BaseDecl)
DerivedToBaseMemberPointer.
Definition Interp.cpp:3061
llvm::APSInt APSInt
Definition FixedPoint.h:20
bool InvalidCast(InterpState &S, CodePtr OpPC, CastKind Kind, bool Fatal)
Definition Interp.cpp:2958
bool DynamicCast(InterpState &S, CodePtr OpPC, const Type *DestTypePtr, bool IsReferenceCast)
Definition Interp.cpp:2068
The JSON file list parser is used to communicate input to InstallAPI.
CanQual< Type > CanQualType
Represents a canonical, potentially-qualified type.
bool isa(CodeGen::Address addr)
Definition Address.h:330
@ Ambiguous
Name lookup results in an ambiguity; use getAmbiguityKind to figure out what kind of ambiguity we hav...
Definition Lookup.h:64
@ Success
Annotation was successful.
Definition Parser.h:65
@ AS_private
Definition Specifiers.h:127
@ SC_Extern
Definition Specifiers.h:252
CheckSubobjectKind
The order of this enum is important for diagnostics.
Definition State.h:44
@ CSK_Derived
Definition State.h:46
@ CSK_Base
Definition State.h:45
@ CSK_Field
Definition State.h:47
@ Result
The result type of a method or function.
Definition TypeBase.h:905
AccessKinds
Kinds of access we can perform on an object, for diagnostics.
Definition State.h:28
@ AK_TypeId
Definition State.h:36
@ AK_Construct
Definition State.h:37
@ AK_Increment
Definition State.h:32
@ AK_DynamicCast
Definition State.h:35
@ AK_Read
Definition State.h:29
@ AK_Assign
Definition State.h:31
@ AK_MemberCall
Definition State.h:34
@ AK_Destroy
Definition State.h:38
@ AK_Decrement
Definition State.h:33
OptionalUnsigned< unsigned > UnsignedOrNone
@ Off
Never emit colors regardless of the output stream.
U cast(CodeGen::Address addr)
Definition Address.h:327
__packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 int32_t
A quantity in bits.
Describes a memory block created by an allocation site.
Definition Descriptor.h:123
const bool IsConst
Flag indicating if the block is mutable.
Definition Descriptor.h:162
unsigned getNumElems() const
Returns the number of elements stored in the block.
Definition Descriptor.h:260
bool isPrimitive() const
Checks if the descriptor is of a primitive.
Definition Descriptor.h:274
bool hasTrivialDtor() const
Whether variables of this descriptor need their destructor called or not.
bool isCompositeArray() const
Checks if the descriptor is of an array of composites.
Definition Descriptor.h:267
const ValueDecl * asValueDecl() const
Definition Descriptor.h:216
QualType getType() const
const Decl * asDecl() const
Definition Descriptor.h:212
const Descriptor *const ElemDesc
Descriptor of the array element.
Definition Descriptor.h:156
unsigned getMetadataSize() const
Returns the size of the metadata.
Definition Descriptor.h:257
SourceLocation getLocation() const
QualType getDataType(const ASTContext &Ctx) const
unsigned getElemDataSize() const
Returns the element data size, i.e.
bool isPrimitiveArray() const
Checks if the descriptor is of an array of primitives.
Definition Descriptor.h:265
const VarDecl * asVarDecl() const
Definition Descriptor.h:220
PrimType getPrimType() const
Definition Descriptor.h:242
bool isRecord() const
Checks if the descriptor is of a record.
Definition Descriptor.h:279
const Record *const ElemRecord
Pointer to the record, if block contains records.
Definition Descriptor.h:154
const Expr * asExpr() const
Definition Descriptor.h:213
bool isArray() const
Checks if the descriptor is of an array.
Definition Descriptor.h:277
void merge(DynamicCastResult C)
Definition Interp.cpp:2039
Descriptor used for global variables.
Definition Descriptor.h:50
Inline descriptor embedded in structures and arrays.
Definition Descriptor.h:68
std::optional< IntPointer > atOffset(const Context &Ctx, unsigned Offset) const
Definition Pointer.cpp:1106
PtrView atField(unsigned Offset) const
Definition Pointer.h:264
const Record * getRecord() const
Definition Pointer.h:156
const Descriptor * getFieldDesc() const
Definition Pointer.h:81
const FieldDecl * getField() const
Definition Pointer.h:161
PtrView atIndex(unsigned Idx) const
Definition Pointer.h:200
void startLifetime() const
Definition Pointer.h:321
PtrView narrow() const
Definition Pointer.h:91
const Block * block() const
Definition Pointer.h:56
bool isMutable() const
Definition Pointer.h:50
QualType getType() const
Definition Pointer.h:269
bool isOnePastEnd() const
Definition Pointer.h:191
bool isConst() const
Definition Pointer.h:64
bool isRoot() const
Definition Pointer.h:60
void setLifeState(Lifetime L) const
Definition Pointer.cpp:630
Lifetime getLifetime() const
Definition Pointer.cpp:611
PtrView getBase() const
Definition Pointer.h:259
bool isActive() const
Definition Pointer.h:46
bool isZeroSizeArray() const
Definition Pointer.h:49
bool isLive() const
Definition Pointer.h:44
PtrView stripBaseCasts() const
Definition Pointer.h:144