Debian Codegraph @

clang/lib/AST/ByteCode/Interp.cpp

raw
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"
19 #include "clang/AST/ASTContext.h"
20 #include "clang/AST/CXXInheritance.h"
21 #include "clang/AST/DeclObjC.h"
22 #include "clang/AST/Expr.h"
23 #include "clang/AST/ExprCXX.h"
24 #include "clang/Basic/DiagnosticSema.h"
25 #include "clang/Basic/TargetInfo.h"
26 #include "llvm/ADT/StringExtras.h"
27
28 using namespace clang;
29 using namespace clang::interp;
30
31 static bool RetValue(InterpState &S, CodePtr &Pt) {
32 llvm::report_fatal_error("Interpreter cannot return values");
33 }
34
35 //===----------------------------------------------------------------------===//
36 // Jmp, Jt, Jf
37 //===----------------------------------------------------------------------===//
38
39 static bool Jmp(InterpState &S, CodePtr &PC, int32_t Offset) {
40 PC += Offset;
41 return true;
42 }
43
44 static bool Jt(InterpState &S, CodePtr &PC, int32_t Offset) {
45 if (S.Stk.pop<bool>()) {
46 PC += Offset;
47 }
48 return true;
49 }
50
51 static bool Jf(InterpState &S, CodePtr &PC, int32_t Offset) {
52 if (!S.Stk.pop<bool>()) {
53 PC += Offset;
54 }
55 return true;
56 }
57
58 // https://github.com/llvm/llvm-project/issues/102513
59 #if defined(_MSC_VER) && !defined(__clang__) && !defined(NDEBUG)
60 #pragma optimize("", off)
61 #endif
62 // FIXME: We have the large switch over all opcodes here again, and in
63 // Interpret().
64 static bool BCP(InterpState &S, CodePtr &RealPC, int32_t Offset, PrimType PT) {
65 [[maybe_unused]] CodePtr PCBefore = RealPC;
66 size_t StackSizeBefore = S.Stk.size();
67
68 auto SpeculativeInterp = [&S, RealPC]() -> bool {
69 const InterpFrame *StartFrame = S.Current;
70 CodePtr PC = RealPC;
71
72 for (;;) {
73 auto Op = PC.read<Opcode>();
74 if (Op == OP_EndSpeculation)
75 return true;
76 CodePtr OpPC = PC;
77
78 switch (Op) {
79 #define GET_INTERP
80 #include "Opcodes.inc"
81 #undef GET_INTERP
82 }
83 }
84 llvm_unreachable("We didn't see an EndSpeculation op?");
85 };
86
87 if (SpeculativeInterp()) {
88 if (PT == PT_Ptr) {
89 const auto &Ptr = S.Stk.pop<Pointer>();
90 assert(S.Stk.size() == StackSizeBefore);
91 S.Stk.push<Integral<32, true>>(
92 Integral<32, true>::from(CheckBCPResult(S, Ptr)));
93 } else {
94 // Pop the result from the stack and return success.
95 TYPE_SWITCH(PT, S.Stk.pop<T>(););
96 assert(S.Stk.size() == StackSizeBefore);
97 S.Stk.push<Integral<32, true>>(Integral<32, true>::from(1));
98 }
99 } else {
100 if (!S.inConstantContext())
101 return Invalid(S, RealPC);
102
103 S.Stk.clearTo(StackSizeBefore);
104 S.Stk.push<Integral<32, true>>(Integral<32, true>::from(0));
105 }
106
107 // RealPC should not have been modified.
108 assert(*RealPC == *PCBefore);
109
110 // Jump to end label. This is a little tricker than just RealPC += Offset
111 // because our usual jump instructions don't have any arguments, to the offset
112 // we get is a little too much and we need to subtract the size of the
113 // bool and PrimType arguments again.
114 int32_t ParamSize = align(sizeof(PrimType));
115 assert(Offset >= ParamSize);
116 RealPC += Offset - ParamSize;
117
118 [[maybe_unused]] CodePtr PCCopy = RealPC;
119 assert(PCCopy.read<Opcode>() == OP_EndSpeculation);
120
121 return true;
122 }
123 // https://github.com/llvm/llvm-project/issues/102513
124 #if defined(_MSC_VER) && !defined(__clang__) && !defined(NDEBUG)
125 #pragma optimize("", on)
126 #endif
127
128 static void diagnoseMissingInitializer(InterpState &S, CodePtr OpPC,
129 const ValueDecl *VD) {
130 const SourceInfo &E = S.Current->getSource(OpPC);
131 S.FFDiag(E, diag::note_constexpr_var_init_unknown, 1) << VD;
132 S.Note(VD->getLocation(), diag::note_declared_at) << VD->getSourceRange();
133 }
134
135 static void diagnoseNonConstVariable(InterpState &S, CodePtr OpPC,
136 const ValueDecl *VD);
137 static bool diagnoseUnknownDecl(InterpState &S, CodePtr OpPC,
138 const ValueDecl *D) {
139 // This function tries pretty hard to produce a good diagnostic. Just skip
140 // that if nobody will see it anyway.
141 if (!S.diagnosing())
142 return false;
143
144 if (isa<ParmVarDecl>(D)) {
145 if (D->getType()->isReferenceType()) {
146 if (S.inConstantContext() && S.getLangOpts().CPlusPlus &&
147 !S.getLangOpts().CPlusPlus11) {
148 diagnoseNonConstVariable(S, OpPC, D);
149 return false;
150 }
151 }
152
153 const SourceInfo &Loc = S.Current->getSource(OpPC);
154 if (S.getLangOpts().CPlusPlus23 && D->getType()->isReferenceType()) {
155 S.FFDiag(Loc, diag::note_constexpr_access_unknown_variable, 1)
156 << AK_Read << D;
157 S.Note(D->getLocation(), diag::note_declared_at) << D->getSourceRange();
158 } else if (S.getLangOpts().CPlusPlus11) {
159 S.FFDiag(Loc, diag::note_constexpr_function_param_value_unknown, 1) << D;
160 S.Note(D->getLocation(), diag::note_declared_at) << D->getSourceRange();
161 } else {
162 S.FFDiag(Loc);
163 }
164 return false;
165 }
166
167 if (!D->getType().isConstQualified()) {
168 diagnoseNonConstVariable(S, OpPC, D);
169 } else if (const auto *VD = dyn_cast<VarDecl>(D)) {
170 if (!VD->getAnyInitializer()) {
171 diagnoseMissingInitializer(S, OpPC, VD);
172 } else {
173 const SourceInfo &Loc = S.Current->getSource(OpPC);
174 S.FFDiag(Loc, diag::note_constexpr_var_init_non_constant, 1) << VD;
175 S.Note(VD->getLocation(), diag::note_declared_at);
176 }
177 }
178
179 return false;
180 }
181
182 static void diagnoseNonConstVariable(InterpState &S, CodePtr OpPC,
183 const ValueDecl *VD) {
184 if (!S.diagnosing())
185 return;
186
187 const SourceInfo &Loc = S.Current->getSource(OpPC);
188 if (!S.getLangOpts().CPlusPlus) {
189 S.FFDiag(Loc);
190 return;
191 }
192
193 if (const auto *VarD = dyn_cast<VarDecl>(VD);
194 VarD && VarD->getType().isConstQualified() &&
195 !VarD->getAnyInitializer()) {
196 diagnoseMissingInitializer(S, OpPC, VD);
197 return;
198 }
199
200 // Rather random, but this is to match the diagnostic output of the current
201 // interpreter.
202 if (isa<ObjCIvarDecl>(VD))
203 return;
204
205 if (VD->getType()->isIntegralOrEnumerationType()) {
206 S.FFDiag(Loc, diag::note_constexpr_ltor_non_const_int, 1) << VD;
207 S.Note(VD->getLocation(), diag::note_declared_at);
208 return;
209 }
210
211 S.FFDiag(Loc,
212 S.getLangOpts().CPlusPlus11 ? diag::note_constexpr_ltor_non_constexpr
213 : diag::note_constexpr_ltor_non_integral,
214 1)
215 << VD << VD->getType();
216 S.Note(VD->getLocation(), diag::note_declared_at);
217 }
218
219 static bool CheckTemporary(InterpState &S, CodePtr OpPC, const Block *B,
220 AccessKinds AK) {
221 if (B->getDeclID()) {
222 if (!(B->isStatic() && B->isTemporary()))
223 return true;
224
225 const auto *MTE = dyn_cast_if_present<MaterializeTemporaryExpr>(
226 B->getDescriptor()->asExpr());
227 if (!MTE)
228 return true;
229
230 // FIXME(perf): Since we do this check on every Load from a static
231 // temporary, it might make sense to cache the value of the
232 // isUsableInConstantExpressions call.
233 if (B->getEvalID() != S.Ctx.getEvalID() &&
234 !MTE->isUsableInConstantExpressions(S.getASTContext())) {
235 const SourceInfo &E = S.Current->getSource(OpPC);
236 S.FFDiag(E, diag::note_constexpr_access_static_temporary, 1) << AK;
237 S.Note(B->getDescriptor()->getLocation(),
238 diag::note_constexpr_temporary_here);
239 return false;
240 }
241 }
242 return true;
243 }
244
245 static bool CheckGlobal(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
246 if (auto ID = Ptr.getDeclID()) {
247 if (!Ptr.isStatic())
248 return true;
249
250 if (S.P.getCurrentDecl() == ID)
251 return true;
252
253 S.FFDiag(S.Current->getLocation(OpPC), diag::note_constexpr_modify_global);
254 return false;
255 }
256 return true;
257 }
258
259 namespace clang {
260 namespace interp {
261 static void popArg(InterpState &S, const Expr *Arg) {
262 PrimType Ty = S.getContext().classify(Arg).value_or(PT_Ptr);
263 TYPE_SWITCH(Ty, S.Stk.discard<T>());
264 }
265
266 void cleanupAfterFunctionCall(InterpState &S, CodePtr OpPC,
267 const Function *Func) {
268 assert(S.Current);
269 assert(Func);
270
271 if (S.Current->Caller && Func->isVariadic()) {
272 // CallExpr we're look for is at the return PC of the current function, i.e.
273 // in the caller.
274 // This code path should be executed very rarely.
275 unsigned NumVarArgs;
276 const Expr *const *Args = nullptr;
277 unsigned NumArgs = 0;
278 const Expr *CallSite = S.Current->Caller->getExpr(S.Current->getRetPC());
279 if (const auto *CE = dyn_cast<CallExpr>(CallSite)) {
280 Args = CE->getArgs();
281 NumArgs = CE->getNumArgs();
282 } else if (const auto *CE = dyn_cast<CXXConstructExpr>(CallSite)) {
283 Args = CE->getArgs();
284 NumArgs = CE->getNumArgs();
285 } else
286 assert(false && "Can't get arguments from that expression type");
287
288 assert(NumArgs >= Func->getNumWrittenParams());
289 NumVarArgs = NumArgs - (Func->getNumWrittenParams() +
290 isa<CXXOperatorCallExpr>(CallSite));
291 for (unsigned I = 0; I != NumVarArgs; ++I) {
292 const Expr *A = Args[NumArgs - 1 - I];
293 popArg(S, A);
294 }
295 }
296
297 // And in any case, remove the fixed parameters (the non-variadic ones)
298 // at the end.
299 for (PrimType Ty : Func->args_reverse())
300 TYPE_SWITCH(Ty, S.Stk.discard<T>());
301 }
302
303 bool isConstexprUnknown(const Pointer &P) {
304 if (!P.isBlockPointer())
305 return false;
306
307 if (P.isDummy())
308 return isa_and_nonnull<ParmVarDecl>(P.getDeclDesc()->asValueDecl());
309
310 return P.getDeclDesc()->IsConstexprUnknown;
311 }
312
313 bool CheckBCPResult(InterpState &S, const Pointer &Ptr) {
314 if (Ptr.isDummy())
315 return false;
316 if (Ptr.isZero())
317 return true;
318 if (Ptr.isFunctionPointer())
319 return false;
320 if (Ptr.isIntegralPointer())
321 return true;
322 if (Ptr.isTypeidPointer())
323 return true;
324
325 if (Ptr.getType()->isAnyComplexType())
326 return true;
327
328 if (const Expr *Base = Ptr.getDeclDesc()->asExpr())
329 return isa<StringLiteral>(Base) && Ptr.getIndex() == 0;
330 return false;
331 }
332
333 bool CheckActive(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
334 AccessKinds AK, bool WillActivate) {
335 if (Ptr.isActive())
336 return true;
337
338 assert(Ptr.inUnion());
339
340 // Find the outermost union.
341 Pointer U = Ptr.getBase();
342 Pointer C = Ptr;
343 while (!U.isRoot() && !U.isActive()) {
344 // A little arbitrary, but this is what the current interpreter does.
345 // See the AnonymousUnion test in test/AST/ByteCode/unions.cpp.
346 // GCC's output is more similar to what we would get without
347 // this condition.
348 if (U.getRecord() && U.getRecord()->isAnonymousUnion())
349 break;
350
351 C = U;
352 U = U.getBase();
353 }
354 assert(C.isField());
355 assert(C.getBase() == U);
356
357 // Consider:
358 // union U {
359 // struct {
360 // int x;
361 // int y;
362 // } a;
363 // }
364 //
365 // When activating x, we will also activate a. If we now try to read
366 // from y, we will get to CheckActive, because y is not active. In that
367 // case, our U will be a (not a union). We return here and let later code
368 // handle this.
369 if (!U.getFieldDesc()->isUnion())
370 return true;
371
372 // When we will activate Ptr, check that none of the unions in its path have a
373 // non-trivial default constructor.
374 if (WillActivate) {
375 bool Fails = false;
376 Pointer It = Ptr;
377 while (!It.isRoot() && !It.isActive()) {
378 if (const Record *R = It.getRecord(); R && R->isUnion()) {
379 if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(R->getDecl());
380 CXXRD && !CXXRD->hasTrivialDefaultConstructor()) {
381 Fails = true;
382 break;
383 }
384 }
385 It = It.getBase();
386 }
387 if (!Fails)
388 return true;
389 }
390
391 // Get the inactive field descriptor.
392 assert(!C.isActive());
393 const FieldDecl *InactiveField = C.getField();
394 assert(InactiveField);
395
396 // Find the active field of the union.
397 const Record *R = U.getRecord();
398 assert(R && R->isUnion() && "Not a union");
399
400 const FieldDecl *ActiveField = nullptr;
401 for (const Record::Field &F : R->fields()) {
402 const Pointer &Field = U.atField(F.Offset);
403 if (Field.isActive()) {
404 ActiveField = Field.getField();
405 break;
406 }
407 }
408
409 const SourceInfo &Loc = S.Current->getSource(OpPC);
410 S.FFDiag(Loc, diag::note_constexpr_access_inactive_union_member)
411 << AK << InactiveField << !ActiveField << ActiveField;
412 return false;
413 }
414
415 bool CheckExtern(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
416 if (!Ptr.isExtern())
417 return true;
418
419 if (!Ptr.isPastEnd() &&
420 (Ptr.isInitialized() ||
421 (Ptr.getDeclDesc()->asVarDecl() == S.EvaluatingDecl)))
422 return true;
423
424 if (S.checkingPotentialConstantExpression() && S.getLangOpts().CPlusPlus &&
425 Ptr.isConst())
426 return false;
427
428 const auto *VD = Ptr.getDeclDesc()->asValueDecl();
429 diagnoseNonConstVariable(S, OpPC, VD);
430 return false;
431 }
432
433 bool CheckArray(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
434 if (!Ptr.isUnknownSizeArray())
435 return true;
436 const SourceInfo &E = S.Current->getSource(OpPC);
437 S.FFDiag(E, diag::note_constexpr_unsized_array_indexed);
438 return false;
439 }
440
441 bool CheckLive(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
442 AccessKinds AK) {
443 if (Ptr.isZero()) {
444 const auto &Src = S.Current->getSource(OpPC);
445
446 if (Ptr.isField())
447 S.FFDiag(Src, diag::note_constexpr_null_subobject) << CSK_Field;
448 else
449 S.FFDiag(Src, diag::note_constexpr_access_null) << AK;
450
451 return false;
452 }
453
454 if (!Ptr.isLive()) {
455 const auto &Src = S.Current->getSource(OpPC);
456
457 if (Ptr.isDynamic()) {
458 S.FFDiag(Src, diag::note_constexpr_access_deleted_object) << AK;
459 } else if (!S.checkingPotentialConstantExpression()) {
460 bool IsTemp = Ptr.isTemporary();
461 S.FFDiag(Src, diag::note_constexpr_lifetime_ended, 1) << AK << !IsTemp;
462
463 if (IsTemp)
464 S.Note(Ptr.getDeclLoc(), diag::note_constexpr_temporary_here);
465 else
466 S.Note(Ptr.getDeclLoc(), diag::note_declared_at);
467 }
468
469 return false;
470 }
471
472 return true;
473 }
474
475 bool CheckConstant(InterpState &S, CodePtr OpPC, const Descriptor *Desc) {
476 assert(Desc);
477
478 const auto *D = Desc->asVarDecl();
479 if (!D || D == S.EvaluatingDecl || D->isConstexpr())
480 return true;
481
482 // If we're evaluating the initializer for a constexpr variable in C23, we may
483 // only read other contexpr variables. Abort here since this one isn't
484 // constexpr.
485 if (const auto *VD = dyn_cast_if_present<VarDecl>(S.EvaluatingDecl);
486 VD && VD->isConstexpr() && S.getLangOpts().C23)
487 return Invalid(S, OpPC);
488
489 QualType T = D->getType();
490 bool IsConstant = T.isConstant(S.getASTContext());
491 if (T->isIntegralOrEnumerationType()) {
492 if (!IsConstant) {
493 diagnoseNonConstVariable(S, OpPC, D);
494 return false;
495 }
496 return true;
497 }
498
499 if (IsConstant) {
500 if (S.getLangOpts().CPlusPlus) {
501 S.CCEDiag(S.Current->getLocation(OpPC),
502 S.getLangOpts().CPlusPlus11
503 ? diag::note_constexpr_ltor_non_constexpr
504 : diag::note_constexpr_ltor_non_integral,
505 1)
506 << D << T;
507 S.Note(D->getLocation(), diag::note_declared_at);
508 } else {
509 S.CCEDiag(S.Current->getLocation(OpPC));
510 }
511 return true;
512 }
513
514 if (T->isPointerOrReferenceType()) {
515 if (!T->getPointeeType().isConstant(S.getASTContext()) ||
516 !S.getLangOpts().CPlusPlus11) {
517 diagnoseNonConstVariable(S, OpPC, D);
518 return false;
519 }
520 return true;
521 }
522
523 diagnoseNonConstVariable(S, OpPC, D);
524 return false;
525 }
526
527 static bool CheckConstant(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
528 if (!Ptr.isStatic() || !Ptr.isBlockPointer())
529 return true;
530 if (!Ptr.getDeclID())
531 return true;
532 return CheckConstant(S, OpPC, Ptr.getDeclDesc());
533 }
534
535 bool CheckNull(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
536 CheckSubobjectKind CSK) {
537 if (!Ptr.isZero())
538 return true;
539 const SourceInfo &Loc = S.Current->getSource(OpPC);
540 S.FFDiag(Loc, diag::note_constexpr_null_subobject)
541 << CSK << S.Current->getRange(OpPC);
542
543 return false;
544 }
545
546 bool CheckRange(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
547 AccessKinds AK) {
548 if (!Ptr.isOnePastEnd() && !Ptr.isZeroSizeArray())
549 return true;
550 if (S.getLangOpts().CPlusPlus) {
551 const SourceInfo &Loc = S.Current->getSource(OpPC);
552 S.FFDiag(Loc, diag::note_constexpr_access_past_end)
553 << AK << S.Current->getRange(OpPC);
554 }
555 return false;
556 }
557
558 bool CheckRange(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
559 CheckSubobjectKind CSK) {
560 if (!Ptr.isElementPastEnd() && !Ptr.isZeroSizeArray())
561 return true;
562 const SourceInfo &Loc = S.Current->getSource(OpPC);
563 S.FFDiag(Loc, diag::note_constexpr_past_end_subobject)
564 << CSK << S.Current->getRange(OpPC);
565 return false;
566 }
567
568 bool CheckSubobject(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
569 CheckSubobjectKind CSK) {
570 if (!Ptr.isOnePastEnd())
571 return true;
572
573 const SourceInfo &Loc = S.Current->getSource(OpPC);
574 S.FFDiag(Loc, diag::note_constexpr_past_end_subobject)
575 << CSK << S.Current->getRange(OpPC);
576 return false;
577 }
578
579 bool CheckDowncast(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
580 uint32_t Offset) {
581 uint32_t MinOffset = Ptr.getDeclDesc()->getMetadataSize();
582 uint32_t PtrOffset = Ptr.getByteOffset();
583
584 // We subtract Offset from PtrOffset. The result must be at least
585 // MinOffset.
586 if (Offset < PtrOffset && (PtrOffset - Offset) >= MinOffset)
587 return true;
588
589 const auto *E = cast<CastExpr>(S.Current->getExpr(OpPC));
590 QualType TargetQT = E->getType()->getPointeeType();
591 QualType MostDerivedQT = Ptr.getDeclPtr().getType();
592
593 S.CCEDiag(E, diag::note_constexpr_invalid_downcast)
594 << MostDerivedQT << TargetQT;
595
596 return false;
597 }
598
599 bool CheckConst(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
600 assert(Ptr.isLive() && "Pointer is not live");
601 if (!Ptr.isConst())
602 return true;
603
604 if (Ptr.isMutable() && !Ptr.isConstInMutable())
605 return true;
606
607 if (!Ptr.isBlockPointer())
608 return false;
609
610 // The This pointer is writable in constructors and destructors,
611 // even if isConst() returns true.
612 if (llvm::is_contained(S.InitializingBlocks, Ptr.block()))
613 return true;
614
615 const QualType Ty = Ptr.getType();
616 const SourceInfo &Loc = S.Current->getSource(OpPC);
617 S.FFDiag(Loc, diag::note_constexpr_modify_const_type) << Ty;
618 return false;
619 }
620
621 bool CheckMutable(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
622 assert(Ptr.isLive() && "Pointer is not live");
623 if (!Ptr.isMutable())
624 return true;
625
626 // In C++14 onwards, it is permitted to read a mutable member whose
627 // lifetime began within the evaluation.
628 if (S.getLangOpts().CPlusPlus14 &&
629 Ptr.block()->getEvalID() == S.Ctx.getEvalID()) {
630 // FIXME: This check is necessary because (of the way) we revisit
631 // variables in Compiler.cpp:visitDeclRef. Revisiting a so far
632 // unknown variable will get the same EvalID and we end up allowing
633 // reads from mutable members of it.
634 if (!S.inConstantContext() && isConstexprUnknown(Ptr))
635 return false;
636 return true;
637 }
638
639 const SourceInfo &Loc = S.Current->getSource(OpPC);
640 const FieldDecl *Field = Ptr.getField();
641 S.FFDiag(Loc, diag::note_constexpr_access_mutable, 1) << AK_Read << Field;
642 S.Note(Field->getLocation(), diag::note_declared_at);
643 return false;
644 }
645
646 static bool CheckVolatile(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
647 AccessKinds AK) {
648 assert(Ptr.isLive());
649
650 if (!Ptr.isVolatile())
651 return true;
652
653 if (!S.getLangOpts().CPlusPlus)
654 return Invalid(S, OpPC);
655
656 // Volatile object can be written-to and read if they are being constructed.
657 if (llvm::is_contained(S.InitializingBlocks, Ptr.block()))
658 return true;
659
660 // The reason why Ptr is volatile might be further up the hierarchy.
661 // Find that pointer.
662 Pointer P = Ptr;
663 while (!P.isRoot()) {
664 if (P.getType().isVolatileQualified())
665 break;
666 P = P.getBase();
667 }
668
669 const NamedDecl *ND = nullptr;
670 int DiagKind;
671 SourceLocation Loc;
672 if (const auto *F = P.getField()) {
673 DiagKind = 2;
674 Loc = F->getLocation();
675 ND = F;
676 } else if (auto *VD = P.getFieldDesc()->asValueDecl()) {
677 DiagKind = 1;
678 Loc = VD->getLocation();
679 ND = VD;
680 } else {
681 DiagKind = 0;
682 if (const auto *E = P.getFieldDesc()->asExpr())
683 Loc = E->getExprLoc();
684 }
685
686 S.FFDiag(S.Current->getLocation(OpPC),
687 diag::note_constexpr_access_volatile_obj, 1)
688 << AK << DiagKind << ND;
689 S.Note(Loc, diag::note_constexpr_volatile_here) << DiagKind;
690 return false;
691 }
692
693 bool DiagnoseUninitialized(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
694 AccessKinds AK) {
695 assert(Ptr.isLive());
696 assert(!Ptr.isInitialized());
697 return DiagnoseUninitialized(S, OpPC, Ptr.isExtern(), Ptr.getDeclDesc(), AK);
698 }
699
700 bool DiagnoseUninitialized(InterpState &S, CodePtr OpPC, bool Extern,
701 const Descriptor *Desc, AccessKinds AK) {
702 if (Extern && S.checkingPotentialConstantExpression())
703 return false;
704
705 if (const auto *VD = Desc->asVarDecl();
706 VD && (VD->isConstexpr() || VD->hasGlobalStorage())) {
707
708 if (VD == S.EvaluatingDecl &&
709 !(S.getLangOpts().CPlusPlus23 && VD->getType()->isReferenceType())) {
710 if (!S.getLangOpts().CPlusPlus14 &&
711 !VD->getType().isConstant(S.getASTContext())) {
712 // Diagnose as non-const read.
713 diagnoseNonConstVariable(S, OpPC, VD);
714 } else {
715 const SourceInfo &Loc = S.Current->getSource(OpPC);
716 // Diagnose as "read of object outside its lifetime".
717 S.FFDiag(Loc, diag::note_constexpr_access_uninit)
718 << AK << /*IsIndeterminate=*/false;
719 }
720 return false;
721 }
722
723 if (VD->getAnyInitializer()) {
724 const SourceInfo &Loc = S.Current->getSource(OpPC);
725 S.FFDiag(Loc, diag::note_constexpr_var_init_non_constant, 1) << VD;
726 S.Note(VD->getLocation(), diag::note_declared_at);
727 } else {
728 diagnoseMissingInitializer(S, OpPC, VD);
729 }
730 return false;
731 }
732
733 if (!S.checkingPotentialConstantExpression()) {
734 S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_access_uninit)
735 << AK << /*uninitialized=*/true << S.Current->getRange(OpPC);
736 }
737 return false;
738 }
739
740 static bool CheckLifetime(InterpState &S, CodePtr OpPC, Lifetime LT,
741 AccessKinds AK) {
742 if (LT == Lifetime::Started)
743 return true;
744
745 if (!S.checkingPotentialConstantExpression()) {
746 S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_access_uninit)
747 << AK << /*uninitialized=*/false << S.Current->getRange(OpPC);
748 }
749 return false;
750 }
751
752 static bool CheckWeak(InterpState &S, CodePtr OpPC, const Block *B) {
753 if (!B->isWeak())
754 return true;
755
756 const auto *VD = B->getDescriptor()->asVarDecl();
757 assert(VD);
758 S.FFDiag(S.Current->getLocation(OpPC), diag::note_constexpr_var_init_weak)
759 << VD;
760 S.Note(VD->getLocation(), diag::note_declared_at);
761
762 return false;
763 }
764
765 // The list of checks here is just the one from CheckLoad, but with the
766 // ones removed that are impossible on primitive global values.
767 // For example, since those can't be members of structs, they also can't
768 // be mutable.
769 bool CheckGlobalLoad(InterpState &S, CodePtr OpPC, const Block *B) {
770 const auto &Desc = B->getBlockDesc<GlobalInlineDescriptor>();
771 if (!B->isAccessible()) {
772 if (!CheckExtern(S, OpPC, Pointer(const_cast<Block *>(B))))
773 return false;
774 if (!CheckDummy(S, OpPC, B, AK_Read))
775 return false;
776 return CheckWeak(S, OpPC, B);
777 }
778
779 if (!CheckConstant(S, OpPC, B->getDescriptor()))
780 return false;
781 if (Desc.InitState != GlobalInitState::Initialized)
782 return DiagnoseUninitialized(S, OpPC, B->isExtern(), B->getDescriptor(),
783 AK_Read);
784 if (!CheckTemporary(S, OpPC, B, AK_Read))
785 return false;
786 if (B->getDescriptor()->IsVolatile) {
787 if (!S.getLangOpts().CPlusPlus)
788 return Invalid(S, OpPC);
789
790 const ValueDecl *D = B->getDescriptor()->asValueDecl();
791 S.FFDiag(S.Current->getLocation(OpPC),
792 diag::note_constexpr_access_volatile_obj, 1)
793 << AK_Read << 1 << D;
794 S.Note(D->getLocation(), diag::note_constexpr_volatile_here) << 1;
795 return false;
796 }
797 return true;
798 }
799
800 // Similarly, for local loads.
801 bool CheckLocalLoad(InterpState &S, CodePtr OpPC, const Block *B) {
802 assert(!B->isExtern());
803 const auto &Desc = *reinterpret_cast<const InlineDescriptor *>(B->rawData());
804 if (!CheckLifetime(S, OpPC, Desc.LifeState, AK_Read))
805 return false;
806 if (!Desc.IsInitialized)
807 return DiagnoseUninitialized(S, OpPC, /*Extern=*/false, B->getDescriptor(),
808 AK_Read);
809 if (B->getDescriptor()->IsVolatile) {
810 if (!S.getLangOpts().CPlusPlus)
811 return Invalid(S, OpPC);
812
813 const ValueDecl *D = B->getDescriptor()->asValueDecl();
814 S.FFDiag(S.Current->getLocation(OpPC),
815 diag::note_constexpr_access_volatile_obj, 1)
816 << AK_Read << 1 << D;
817 S.Note(D->getLocation(), diag::note_constexpr_volatile_here) << 1;
818 return false;
819 }
820 return true;
821 }
822
823 bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
824 AccessKinds AK) {
825 if (Ptr.isZero()) {
826 const auto &Src = S.Current->getSource(OpPC);
827
828 if (Ptr.isField())
829 S.FFDiag(Src, diag::note_constexpr_null_subobject) << CSK_Field;
830 else
831 S.FFDiag(Src, diag::note_constexpr_access_null) << AK;
832 return false;
833 }
834 // Block pointers are the only ones we can actually read from.
835 if (!Ptr.isBlockPointer())
836 return false;
837
838 if (!Ptr.block()->isAccessible()) {
839 if (!CheckLive(S, OpPC, Ptr, AK))
840 return false;
841 if (!CheckExtern(S, OpPC, Ptr))
842 return false;
843 if (!CheckDummy(S, OpPC, Ptr.block(), AK))
844 return false;
845 return CheckWeak(S, OpPC, Ptr.block());
846 }
847
848 if (!CheckConstant(S, OpPC, Ptr))
849 return false;
850 if (!CheckRange(S, OpPC, Ptr, AK))
851 return false;
852 if (!CheckActive(S, OpPC, Ptr, AK))
853 return false;
854 if (!CheckLifetime(S, OpPC, Ptr.getLifetime(), AK))
855 return false;
856 if (!Ptr.isInitialized())
857 return DiagnoseUninitialized(S, OpPC, Ptr, AK);
858 if (!CheckTemporary(S, OpPC, Ptr.block(), AK))
859 return false;
860
861 if (!CheckMutable(S, OpPC, Ptr))
862 return false;
863 if (!CheckVolatile(S, OpPC, Ptr, AK))
864 return false;
865 if (!Ptr.isConst() && !S.inConstantContext() && isConstexprUnknown(Ptr))
866 return false;
867 return true;
868 }
869
870 /// This is not used by any of the opcodes directly. It's used by
871 /// EvalEmitter to do the final lvalue-to-rvalue conversion.
872 bool CheckFinalLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
873 assert(!Ptr.isZero());
874 if (!Ptr.isBlockPointer())
875 return false;
876
877 if (!Ptr.block()->isAccessible()) {
878 if (!CheckLive(S, OpPC, Ptr, AK_Read))
879 return false;
880 if (!CheckExtern(S, OpPC, Ptr))
881 return false;
882 if (!CheckDummy(S, OpPC, Ptr.block(), AK_Read))
883 return false;
884 return CheckWeak(S, OpPC, Ptr.block());
885 }
886
887 if (!CheckConstant(S, OpPC, Ptr))
888 return false;
889
890 if (!CheckActive(S, OpPC, Ptr, AK_Read))
891 return false;
892 if (!CheckLifetime(S, OpPC, Ptr.getLifetime(), AK_Read))
893 return false;
894 if (!Ptr.isInitialized())
895 return DiagnoseUninitialized(S, OpPC, Ptr, AK_Read);
896 if (!CheckTemporary(S, OpPC, Ptr.block(), AK_Read))
897 return false;
898 if (!CheckMutable(S, OpPC, Ptr))
899 return false;
900 return true;
901 }
902
903 bool CheckStore(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
904 bool WillBeActivated) {
905 if (!Ptr.isBlockPointer() || Ptr.isZero())
906 return false;
907
908 if (!Ptr.block()->isAccessible()) {
909 if (!CheckLive(S, OpPC, Ptr, AK_Assign))
910 return false;
911 if (!CheckExtern(S, OpPC, Ptr))
912 return false;
913 return CheckDummy(S, OpPC, Ptr.block(), AK_Assign);
914 }
915 if (!CheckLifetime(S, OpPC, Ptr.getLifetime(), AK_Assign))
916 return false;
917 if (!CheckRange(S, OpPC, Ptr, AK_Assign))
918 return false;
919 if (!CheckActive(S, OpPC, Ptr, AK_Assign, WillBeActivated))
920 return false;
921 if (!CheckGlobal(S, OpPC, Ptr))
922 return false;
923 if (!CheckConst(S, OpPC, Ptr))
924 return false;
925 if (!CheckVolatile(S, OpPC, Ptr, AK_Assign))
926 return false;
927 if (!S.inConstantContext() && isConstexprUnknown(Ptr))
928 return false;
929 return true;
930 }
931
932 static bool CheckInvoke(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
933 if (!CheckLive(S, OpPC, Ptr, AK_MemberCall))
934 return false;
935 if (!Ptr.isDummy()) {
936 if (!CheckExtern(S, OpPC, Ptr))
937 return false;
938 if (!CheckRange(S, OpPC, Ptr, AK_MemberCall))
939 return false;
940 }
941 return true;
942 }
943
944 bool CheckInit(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
945 if (!CheckLive(S, OpPC, Ptr, AK_Assign))
946 return false;
947 if (!CheckRange(S, OpPC, Ptr, AK_Assign))
948 return false;
949 return true;
950 }
951
952 static bool diagnoseCallableDecl(InterpState &S, CodePtr OpPC,
953 const FunctionDecl *DiagDecl) {
954 // Bail out if the function declaration itself is invalid. We will
955 // have produced a relevant diagnostic while parsing it, so just
956 // note the problematic sub-expression.
957 if (DiagDecl->isInvalidDecl())
958 return Invalid(S, OpPC);
959
960 // Diagnose failed assertions specially.
961 if (S.Current->getLocation(OpPC).isMacroID() && DiagDecl->getIdentifier()) {
962 // FIXME: Instead of checking for an implementation-defined function,
963 // check and evaluate the assert() macro.
964 StringRef Name = DiagDecl->getName();
965 bool AssertFailed =
966 Name == "__assert_rtn" || Name == "__assert_fail" || Name == "_wassert";
967 if (AssertFailed) {
968 S.FFDiag(S.Current->getLocation(OpPC),
969 diag::note_constexpr_assert_failed);
970 return false;
971 }
972 }
973
974 if (!S.getLangOpts().CPlusPlus11) {
975 S.FFDiag(S.Current->getLocation(OpPC),
976 diag::note_invalid_subexpr_in_const_expr);
977 return false;
978 }
979
980 // Invalid decls have been diagnosed before.
981 if (DiagDecl->isInvalidDecl())
982 return false;
983
984 // If this function is not constexpr because it is an inherited
985 // non-constexpr constructor, diagnose that directly.
986 const auto *CD = dyn_cast<CXXConstructorDecl>(DiagDecl);
987 if (CD && CD->isInheritingConstructor()) {
988 const auto *Inherited = CD->getInheritedConstructor().getConstructor();
989 if (!Inherited->isConstexpr())
990 DiagDecl = CD = Inherited;
991 }
992
993 // Silently reject constructors of invalid classes. The invalid class
994 // has been rejected elsewhere before.
995 if (CD && CD->getParent()->isInvalidDecl())
996 return false;
997
998 // FIXME: If DiagDecl is an implicitly-declared special member function
999 // or an inheriting constructor, we should be much more explicit about why
1000 // it's not constexpr.
1001 if (CD && CD->isInheritingConstructor()) {
1002 S.FFDiag(S.Current->getLocation(OpPC), diag::note_constexpr_invalid_inhctor,
1003 1)
1004 << CD->getInheritedConstructor().getConstructor()->getParent();
1005 S.Note(DiagDecl->getLocation(), diag::note_declared_at);
1006 } else {
1007 // Don't emit anything if the function isn't defined and we're checking
1008 // for a constant expression. It might be defined at the point we're
1009 // actually calling it.
1010 bool IsExtern = DiagDecl->getStorageClass() == SC_Extern;
1011 bool IsDefined = DiagDecl->isDefined();
1012 if (!IsDefined && !IsExtern && DiagDecl->isConstexpr() &&
1013 S.checkingPotentialConstantExpression())
1014 return false;
1015
1016 // If the declaration is defined, declared 'constexpr' _and_ has a body,
1017 // the below diagnostic doesn't add anything useful.
1018 if (DiagDecl->isDefined() && DiagDecl->isConstexpr() && DiagDecl->hasBody())
1019 return false;
1020
1021 S.FFDiag(S.Current->getLocation(OpPC),
1022 diag::note_constexpr_invalid_function, 1)
1023 << DiagDecl->isConstexpr() << (bool)CD << DiagDecl;
1024
1025 if (DiagDecl->getDefinition())
1026 S.Note(DiagDecl->getDefinition()->getLocation(), diag::note_declared_at);
1027 else
1028 S.Note(DiagDecl->getLocation(), diag::note_declared_at);
1029 }
1030
1031 return false;
1032 }
1033
1034 static bool CheckCallable(InterpState &S, CodePtr OpPC, const Function *F) {
1035 if (F->isVirtual() && !S.getLangOpts().CPlusPlus20) {
1036 const SourceLocation &Loc = S.Current->getLocation(OpPC);
1037 S.CCEDiag(Loc, diag::note_constexpr_virtual_call);
1038 return false;
1039 }
1040
1041 if (S.checkingPotentialConstantExpression() && S.Current->getDepth() != 0)
1042 return false;
1043
1044 if (F->isValid() && F->hasBody() && F->isConstexpr())
1045 return true;
1046
1047 const FunctionDecl *DiagDecl = F->getDecl();
1048 const FunctionDecl *Definition = nullptr;
1049 DiagDecl->getBody(Definition);
1050
1051 if (!Definition && S.checkingPotentialConstantExpression() &&
1052 DiagDecl->isConstexpr()) {
1053 return false;
1054 }
1055
1056 // Implicitly constexpr.
1057 if (F->isLambdaStaticInvoker())
1058 return true;
1059
1060 return diagnoseCallableDecl(S, OpPC, DiagDecl);
1061 }
1062
1063 static bool CheckCallDepth(InterpState &S, CodePtr OpPC) {
1064 if ((S.Current->getDepth() + 1) > S.getLangOpts().ConstexprCallDepth) {
1065 S.FFDiag(S.Current->getSource(OpPC),
1066 diag::note_constexpr_depth_limit_exceeded)
1067 << S.getLangOpts().ConstexprCallDepth;
1068 return false;
1069 }
1070
1071 return true;
1072 }
1073
1074 bool CheckThis(InterpState &S, CodePtr OpPC) {
1075 if (S.Current->hasThisPointer())
1076 return true;
1077
1078 const Expr *E = S.Current->getExpr(OpPC);
1079 if (S.getLangOpts().CPlusPlus11) {
1080 bool IsImplicit = false;
1081 if (const auto *TE = dyn_cast<CXXThisExpr>(E))
1082 IsImplicit = TE->isImplicit();
1083 S.FFDiag(E, diag::note_constexpr_this) << IsImplicit;
1084 } else {
1085 S.FFDiag(E);
1086 }
1087
1088 return false;
1089 }
1090
1091 bool CheckFloatResult(InterpState &S, CodePtr OpPC, const Floating &Result,
1092 APFloat::opStatus Status, FPOptions FPO) {
1093 // [expr.pre]p4:
1094 // If during the evaluation of an expression, the result is not
1095 // mathematically defined [...], the behavior is undefined.
1096 // FIXME: C++ rules require us to not conform to IEEE 754 here.
1097 if (Result.isNan()) {
1098 const SourceInfo &E = S.Current->getSource(OpPC);
1099 S.CCEDiag(E, diag::note_constexpr_float_arithmetic)
1100 << /*NaN=*/true << S.Current->getRange(OpPC);
1101 return S.noteUndefinedBehavior();
1102 }
1103
1104 // In a constant context, assume that any dynamic rounding mode or FP
1105 // exception state matches the default floating-point environment.
1106 if (S.inConstantContext())
1107 return true;
1108
1109 if ((Status & APFloat::opInexact) &&
1110 FPO.getRoundingMode() == llvm::RoundingMode::Dynamic) {
1111 // Inexact result means that it depends on rounding mode. If the requested
1112 // mode is dynamic, the evaluation cannot be made in compile time.
1113 const SourceInfo &E = S.Current->getSource(OpPC);
1114 S.FFDiag(E, diag::note_constexpr_dynamic_rounding);
1115 return false;
1116 }
1117
1118 if ((Status != APFloat::opOK) &&
1119 (FPO.getRoundingMode() == llvm::RoundingMode::Dynamic ||
1120 FPO.getExceptionMode() != LangOptions::FPE_Ignore ||
1121 FPO.getAllowFEnvAccess())) {
1122 const SourceInfo &E = S.Current->getSource(OpPC);
1123 S.FFDiag(E, diag::note_constexpr_float_arithmetic_strict);
1124 return false;
1125 }
1126
1127 if ((Status & APFloat::opStatus::opInvalidOp) &&
1128 FPO.getExceptionMode() != LangOptions::FPE_Ignore) {
1129 const SourceInfo &E = S.Current->getSource(OpPC);
1130 // There is no usefully definable result.
1131 S.FFDiag(E);
1132 return false;
1133 }
1134
1135 return true;
1136 }
1137
1138 bool CheckDynamicMemoryAllocation(InterpState &S, CodePtr OpPC) {
1139 if (S.getLangOpts().CPlusPlus20)
1140 return true;
1141
1142 const SourceInfo &E = S.Current->getSource(OpPC);
1143 S.CCEDiag(E, diag::note_constexpr_new);
1144 return true;
1145 }
1146
1147 bool CheckNewDeleteForms(InterpState &S, CodePtr OpPC,
1148 DynamicAllocator::Form AllocForm,
1149 DynamicAllocator::Form DeleteForm, const Descriptor *D,
1150 const Expr *NewExpr) {
1151 if (AllocForm == DeleteForm)
1152 return true;
1153
1154 QualType TypeToDiagnose = D->getDataType(S.getASTContext());
1155
1156 const SourceInfo &E = S.Current->getSource(OpPC);
1157 S.FFDiag(E, diag::note_constexpr_new_delete_mismatch)
1158 << static_cast<int>(DeleteForm) << static_cast<int>(AllocForm)
1159 << TypeToDiagnose;
1160 S.Note(NewExpr->getExprLoc(), diag::note_constexpr_dynamic_alloc_here)
1161 << NewExpr->getSourceRange();
1162 return false;
1163 }
1164
1165 bool CheckDeleteSource(InterpState &S, CodePtr OpPC, const Expr *Source,
1166 const Pointer &Ptr) {
1167 // Regular new type(...) call.
1168 if (isa_and_nonnull<CXXNewExpr>(Source))
1169 return true;
1170 // operator new.
1171 if (const auto *CE = dyn_cast_if_present<CallExpr>(Source);
1172 CE && CE->getBuiltinCallee() == Builtin::BI__builtin_operator_new)
1173 return true;
1174 // std::allocator.allocate() call
1175 if (const auto *MCE = dyn_cast_if_present<CXXMemberCallExpr>(Source);
1176 MCE && MCE->getMethodDecl()->getIdentifier()->isStr("allocate"))
1177 return true;
1178
1179 // Whatever this is, we didn't heap allocate it.
1180 const SourceInfo &Loc = S.Current->getSource(OpPC);
1181 S.FFDiag(Loc, diag::note_constexpr_delete_not_heap_alloc)
1182 << Ptr.toDiagnosticString(S.getASTContext());
1183
1184 if (Ptr.isTemporary())
1185 S.Note(Ptr.getDeclLoc(), diag::note_constexpr_temporary_here);
1186 else
1187 S.Note(Ptr.getDeclLoc(), diag::note_declared_at);
1188 return false;
1189 }
1190
1191 /// We aleady know the given DeclRefExpr is invalid for some reason,
1192 /// now figure out why and print appropriate diagnostics.
1193 bool CheckDeclRef(InterpState &S, CodePtr OpPC, const DeclRefExpr *DR) {
1194 const ValueDecl *D = DR->getDecl();
1195 return diagnoseUnknownDecl(S, OpPC, D);
1196 }
1197
1198 bool CheckDummy(InterpState &S, CodePtr OpPC, const Block *B, AccessKinds AK) {
1199 if (!B->isDummy())
1200 return true;
1201
1202 const ValueDecl *D = B->getDescriptor()->asValueDecl();
1203 if (!D)
1204 return false;
1205
1206 if (AK == AK_Read || AK == AK_Increment || AK == AK_Decrement)
1207 return diagnoseUnknownDecl(S, OpPC, D);
1208
1209 if (AK == AK_Destroy || S.getLangOpts().CPlusPlus14) {
1210 const SourceInfo &E = S.Current->getSource(OpPC);
1211 S.FFDiag(E, diag::note_constexpr_modify_global);
1212 }
1213 return false;
1214 }
1215
1216 static bool CheckNonNullArgs(InterpState &S, CodePtr OpPC, const Function *F,
1217 const CallExpr *CE, unsigned ArgSize) {
1218 auto Args = ArrayRef(CE->getArgs(), CE->getNumArgs());
1219 auto NonNullArgs = collectNonNullArgs(F->getDecl(), Args);
1220 unsigned Offset = 0;
1221 unsigned Index = 0;
1222 for (const Expr *Arg : Args) {
1223 if (NonNullArgs[Index] && Arg->getType()->isPointerType()) {
1224 const Pointer &ArgPtr = S.Stk.peek<Pointer>(ArgSize - Offset);
1225 if (ArgPtr.isZero()) {
1226 const SourceLocation &Loc = S.Current->getLocation(OpPC);
1227 S.CCEDiag(Loc, diag::note_non_null_attribute_failed);
1228 return false;
1229 }
1230 }
1231
1232 Offset += align(primSize(S.Ctx.classify(Arg).value_or(PT_Ptr)));
1233 ++Index;
1234 }
1235 return true;
1236 }
1237
1238 static bool runRecordDestructor(InterpState &S, CodePtr OpPC,
1239 const Pointer &BasePtr,
1240 const Descriptor *Desc) {
1241 assert(Desc->isRecord());
1242 const Record *R = Desc->ElemRecord;
1243 assert(R);
1244
1245 if (S.Current->hasThisPointer() && S.Current->getFunction()->isDestructor() &&
1246 Pointer::pointToSameBlock(BasePtr, S.Current->getThis())) {
1247 const SourceInfo &Loc = S.Current->getSource(OpPC);
1248 S.FFDiag(Loc, diag::note_constexpr_double_destroy);
1249 return false;
1250 }
1251
1252 // Destructor of this record.
1253 const CXXDestructorDecl *Dtor = R->getDestructor();
1254 assert(Dtor);
1255 assert(!Dtor->isTrivial());
1256 const Function *DtorFunc = S.getContext().getOrCreateFunction(Dtor);
1257 if (!DtorFunc)
1258 return false;
1259
1260 S.Stk.push<Pointer>(BasePtr);
1261 return Call(S, OpPC, DtorFunc, 0);
1262 }
1263
1264 static bool RunDestructors(InterpState &S, CodePtr OpPC, const Block *B) {
1265 assert(B);
1266 const Descriptor *Desc = B->getDescriptor();
1267
1268 if (Desc->isPrimitive() || Desc->isPrimitiveArray())
1269 return true;
1270
1271 assert(Desc->isRecord() || Desc->isCompositeArray());
1272
1273 if (Desc->hasTrivialDtor())
1274 return true;
1275
1276 if (Desc->isCompositeArray()) {
1277 unsigned N = Desc->getNumElems();
1278 if (N == 0)
1279 return true;
1280 const Descriptor *ElemDesc = Desc->ElemDesc;
1281 assert(ElemDesc->isRecord());
1282
1283 Pointer RP(const_cast<Block *>(B));
1284 for (int I = static_cast<int>(N) - 1; I >= 0; --I) {
1285 if (!runRecordDestructor(S, OpPC, RP.atIndex(I).narrow(), ElemDesc))
1286 return false;
1287 }
1288 return true;
1289 }
1290
1291 assert(Desc->isRecord());
1292 return runRecordDestructor(S, OpPC, Pointer(const_cast<Block *>(B)), Desc);
1293 }
1294
1295 static bool hasVirtualDestructor(QualType T) {
1296 if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl())
1297 if (const CXXDestructorDecl *DD = RD->getDestructor())
1298 return DD->isVirtual();
1299 return false;
1300 }
1301
1302 bool Free(InterpState &S, CodePtr OpPC, bool DeleteIsArrayForm,
1303 bool IsGlobalDelete) {
1304 if (!CheckDynamicMemoryAllocation(S, OpPC))
1305 return false;
1306
1307 DynamicAllocator &Allocator = S.getAllocator();
1308
1309 const Expr *Source = nullptr;
1310 const Block *BlockToDelete = nullptr;
1311 {
1312 // Extra scope for this so the block doesn't have this pointer
1313 // pointing to it when we destroy it.
1314 Pointer Ptr = S.Stk.pop<Pointer>();
1315
1316 // Deleteing nullptr is always fine.
1317 if (Ptr.isZero())
1318 return true;
1319
1320 // Remove base casts.
1321 QualType InitialType = Ptr.getType();
1322 while (Ptr.isBaseClass())
1323 Ptr = Ptr.getBase();
1324
1325 Source = Ptr.getDeclDesc()->asExpr();
1326 BlockToDelete = Ptr.block();
1327
1328 // Check that new[]/delete[] or new/delete were used, not a mixture.
1329 const Descriptor *BlockDesc = BlockToDelete->getDescriptor();
1330 if (std::optional<DynamicAllocator::Form> AllocForm =
1331 Allocator.getAllocationForm(Source)) {
1332 DynamicAllocator::Form DeleteForm =
1333 DeleteIsArrayForm ? DynamicAllocator::Form::Array
1334 : DynamicAllocator::Form::NonArray;
1335 if (!CheckNewDeleteForms(S, OpPC, *AllocForm, DeleteForm, BlockDesc,
1336 Source))
1337 return false;
1338 }
1339
1340 // For the non-array case, the types must match if the static type
1341 // does not have a virtual destructor.
1342 if (!DeleteIsArrayForm && Ptr.getType() != InitialType &&
1343 !hasVirtualDestructor(InitialType)) {
1344 S.FFDiag(S.Current->getSource(OpPC),
1345 diag::note_constexpr_delete_base_nonvirt_dtor)
1346 << InitialType << Ptr.getType();
1347 return false;
1348 }
1349
1350 if (!Ptr.isRoot() || (Ptr.isOnePastEnd() && !Ptr.isZeroSizeArray()) ||
1351 (Ptr.isArrayElement() && Ptr.getIndex() != 0)) {
1352 const SourceInfo &Loc = S.Current->getSource(OpPC);
1353 S.FFDiag(Loc, diag::note_constexpr_delete_subobject)
1354 << Ptr.toDiagnosticString(S.getASTContext()) << Ptr.isOnePastEnd();
1355 return false;
1356 }
1357
1358 if (!CheckDeleteSource(S, OpPC, Source, Ptr))
1359 return false;
1360
1361 // For a class type with a virtual destructor, the selected operator delete
1362 // is the one looked up when building the destructor.
1363 if (!DeleteIsArrayForm && !IsGlobalDelete) {
1364 QualType AllocType = Ptr.getType();
1365 auto getVirtualOperatorDelete = [](QualType T) -> const FunctionDecl * {
1366 if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl())
1367 if (const CXXDestructorDecl *DD = RD->getDestructor())
1368 return DD->isVirtual() ? DD->getOperatorDelete() : nullptr;
1369 return nullptr;
1370 };
1371
1372 if (const FunctionDecl *VirtualDelete =
1373 getVirtualOperatorDelete(AllocType);
1374 VirtualDelete &&
1375 !VirtualDelete
1376 ->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) {
1377 S.FFDiag(S.Current->getSource(OpPC),
1378 diag::note_constexpr_new_non_replaceable)
1379 << isa<CXXMethodDecl>(VirtualDelete) << VirtualDelete;
1380 return false;
1381 }
1382 }
1383 }
1384 assert(Source);
1385 assert(BlockToDelete);
1386
1387 // Invoke destructors before deallocating the memory.
1388 if (!RunDestructors(S, OpPC, BlockToDelete))
1389 return false;
1390
1391 if (!Allocator.deallocate(Source, BlockToDelete, S)) {
1392 // Nothing has been deallocated, this must be a double-delete.
1393 const SourceInfo &Loc = S.Current->getSource(OpPC);
1394 S.FFDiag(Loc, diag::note_constexpr_double_delete);
1395 return false;
1396 }
1397
1398 return true;
1399 }
1400
1401 void diagnoseEnumValue(InterpState &S, CodePtr OpPC, const EnumDecl *ED,
1402 const APSInt &Value) {
1403 llvm::APInt Min;
1404 llvm::APInt Max;
1405 ED->getValueRange(Max, Min);
1406 --Max;
1407
1408 if (ED->getNumNegativeBits() &&
1409 (Max.slt(Value.getSExtValue()) || Min.sgt(Value.getSExtValue()))) {
1410 const SourceLocation &Loc = S.Current->getLocation(OpPC);
1411 S.CCEDiag(Loc, diag::note_constexpr_unscoped_enum_out_of_range)
1412 << llvm::toString(Value, 10) << Min.getSExtValue() << Max.getSExtValue()
1413 << ED;
1414 } else if (!ED->getNumNegativeBits() && Max.ult(Value.getZExtValue())) {
1415 const SourceLocation &Loc = S.Current->getLocation(OpPC);
1416 S.CCEDiag(Loc, diag::note_constexpr_unscoped_enum_out_of_range)
1417 << llvm::toString(Value, 10) << Min.getZExtValue() << Max.getZExtValue()
1418 << ED;
1419 }
1420 }
1421
1422 bool CheckLiteralType(InterpState &S, CodePtr OpPC, const Type *T) {
1423 assert(T);
1424 assert(!S.getLangOpts().CPlusPlus23);
1425
1426 // C++1y: A constant initializer for an object o [...] may also invoke
1427 // constexpr constructors for o and its subobjects even if those objects
1428 // are of non-literal class types.
1429 //
1430 // C++11 missed this detail for aggregates, so classes like this:
1431 // struct foo_t { union { int i; volatile int j; } u; };
1432 // are not (obviously) initializable like so:
1433 // __attribute__((__require_constant_initialization__))
1434 // static const foo_t x = {{0}};
1435 // because "i" is a subobject with non-literal initialization (due to the
1436 // volatile member of the union). See:
1437 // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1677
1438 // Therefore, we use the C++1y behavior.
1439
1440 if (!S.Current->isBottomFrame() &&
1441 S.Current->getFunction()->isConstructor() &&
1442 S.Current->getThis().getDeclDesc()->asDecl() == S.EvaluatingDecl) {
1443 return true;
1444 }
1445
1446 const Expr *E = S.Current->getExpr(OpPC);
1447 if (S.getLangOpts().CPlusPlus11)
1448 S.FFDiag(E, diag::note_constexpr_nonliteral) << E->getType();
1449 else
1450 S.FFDiag(E, diag::note_invalid_subexpr_in_const_expr);
1451 return false;
1452 }
1453
1454 static bool getField(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
1455 uint32_t Off) {
1456 if (S.getLangOpts().CPlusPlus && S.inConstantContext() &&
1457 !CheckNull(S, OpPC, Ptr, CSK_Field))
1458 return false;
1459
1460 if (!CheckRange(S, OpPC, Ptr, CSK_Field))
1461 return false;
1462 if (!CheckArray(S, OpPC, Ptr))
1463 return false;
1464 if (!CheckSubobject(S, OpPC, Ptr, CSK_Field))
1465 return false;
1466
1467 if (Ptr.isIntegralPointer()) {
1468 if (std::optional<IntPointer> IntPtr =
1469 Ptr.asIntPointer().atOffset(S.getASTContext(), Off)) {
1470 S.Stk.push<Pointer>(std::move(*IntPtr));
1471 return true;
1472 }
1473 return false;
1474 }
1475
1476 if (!Ptr.isBlockPointer()) {
1477 // FIXME: The only time we (seem to) get here is when trying to access a
1478 // field of a typeid pointer. In that case, we're supposed to diagnose e.g.
1479 // `typeid(int).name`, but we currently diagnose `&typeid(int)`.
1480 S.FFDiag(S.Current->getSource(OpPC),
1481 diag::note_constexpr_access_unreadable_object)
1482 << AK_Read << Ptr.toDiagnosticString(S.getASTContext());
1483 return false;
1484 }
1485
1486 // We can't get the field of something that's not a record.
1487 if (!Ptr.getFieldDesc()->isRecord())
1488 return false;
1489
1490 if ((Ptr.getByteOffset() + Off) >= Ptr.block()->getSize())
1491 return false;
1492
1493 S.Stk.push<Pointer>(Ptr.atField(Off));
1494 return true;
1495 }
1496
1497 bool GetPtrField(InterpState &S, CodePtr OpPC, uint32_t Off) {
1498 const auto &Ptr = S.Stk.peek<Pointer>();
1499 return getField(S, OpPC, Ptr, Off);
1500 }
1501
1502 bool GetPtrFieldPop(InterpState &S, CodePtr OpPC, uint32_t Off) {
1503 const auto &Ptr = S.Stk.pop<Pointer>();
1504 return getField(S, OpPC, Ptr, Off);
1505 }
1506
1507 static bool checkConstructor(InterpState &S, CodePtr OpPC, const Function *Func,
1508 const Pointer &ThisPtr) {
1509 assert(Func->isConstructor());
1510
1511 if (Func->getParentDecl()->isInvalidDecl())
1512 return false;
1513
1514 const Descriptor *D = ThisPtr.getFieldDesc();
1515 // FIXME: I think this case is not 100% correct. E.g. a pointer into a
1516 // subobject of a composite array.
1517 if (!D->ElemRecord)
1518 return true;
1519
1520 if (D->ElemRecord->getNumVirtualBases() == 0)
1521 return true;
1522
1523 S.FFDiag(S.Current->getLocation(OpPC), diag::note_constexpr_virtual_base)
1524 << Func->getParentDecl();
1525 return false;
1526 }
1527
1528 bool CheckDestructor(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
1529 if (!CheckLive(S, OpPC, Ptr, AK_Destroy))
1530 return false;
1531 if (!CheckTemporary(S, OpPC, Ptr.block(), AK_Destroy))
1532 return false;
1533 if (!CheckRange(S, OpPC, Ptr, AK_Destroy))
1534 return false;
1535
1536 // Can't call a dtor on a global variable.
1537 if (Ptr.block()->isStatic()) {
1538 const SourceInfo &E = S.Current->getSource(OpPC);
1539 S.FFDiag(E, diag::note_constexpr_modify_global);
1540 return false;
1541 }
1542 return CheckActive(S, OpPC, Ptr, AK_Destroy);
1543 }
1544
1545 /// Opcode. Check if the function decl can be called at compile time.
1546 bool CheckFunctionDecl(InterpState &S, CodePtr OpPC, const FunctionDecl *FD) {
1547 if (S.checkingPotentialConstantExpression() && S.Current->getDepth() != 0)
1548 return false;
1549
1550 const FunctionDecl *Definition = nullptr;
1551 const Stmt *Body = FD->getBody(Definition);
1552
1553 if (Definition && Body &&
1554 (Definition->isConstexpr() || Definition->hasAttr<MSConstexprAttr>()))
1555 return true;
1556
1557 return diagnoseCallableDecl(S, OpPC, FD);
1558 }
1559
1560 static void compileFunction(InterpState &S, const Function *Func) {
1561 const FunctionDecl *Definition = Func->getDecl()->getDefinition();
1562 if (!Definition)
1563 return;
1564
1565 Compiler<ByteCodeEmitter>(S.getContext(), S.P)
1566 .compileFunc(Definition, const_cast<Function *>(Func));
1567 }
1568
1569 bool CallVar(InterpState &S, CodePtr OpPC, const Function *Func,
1570 uint32_t VarArgSize) {
1571 if (Func->hasThisPointer()) {
1572 size_t ArgSize = Func->getArgSize() + VarArgSize;
1573 size_t ThisOffset = ArgSize - (Func->hasRVO() ? primSize(PT_Ptr) : 0);
1574 const Pointer &ThisPtr = S.Stk.peek<Pointer>(ThisOffset);
1575
1576 // If the current function is a lambda static invoker and
1577 // the function we're about to call is a lambda call operator,
1578 // skip the CheckInvoke, since the ThisPtr is a null pointer
1579 // anyway.
1580 if (!(S.Current->getFunction() &&
1581 S.Current->getFunction()->isLambdaStaticInvoker() &&
1582 Func->isLambdaCallOperator())) {
1583 if (!CheckInvoke(S, OpPC, ThisPtr))
1584 return false;
1585 }
1586
1587 if (S.checkingPotentialConstantExpression())
1588 return false;
1589 }
1590
1591 if (!Func->isFullyCompiled())
1592 compileFunction(S, Func);
1593
1594 if (!CheckCallable(S, OpPC, Func))
1595 return false;
1596
1597 if (!CheckCallDepth(S, OpPC))
1598 return false;
1599
1600 auto NewFrame = std::make_unique<InterpFrame>(S, Func, OpPC, VarArgSize);
1601 InterpFrame *FrameBefore = S.Current;
1602 S.Current = NewFrame.get();
1603
1604 // Note that we cannot assert(CallResult.hasValue()) here since
1605 // Ret() above only sets the APValue if the curent frame doesn't
1606 // have a caller set.
1607 if (Interpret(S)) {
1608 NewFrame.release(); // Frame was delete'd already.
1609 assert(S.Current == FrameBefore);
1610 return true;
1611 }
1612
1613 // Interpreting the function failed somehow. Reset to
1614 // previous state.
1615 S.Current = FrameBefore;
1616 return false;
1617 }
1618 bool Call(InterpState &S, CodePtr OpPC, const Function *Func,
1619 uint32_t VarArgSize) {
1620 assert(Func);
1621 auto cleanup = [&]() -> bool {
1622 cleanupAfterFunctionCall(S, OpPC, Func);
1623 return false;
1624 };
1625
1626 if (Func->hasThisPointer()) {
1627 size_t ArgSize = Func->getArgSize() + VarArgSize;
1628 size_t ThisOffset = ArgSize - (Func->hasRVO() ? primSize(PT_Ptr) : 0);
1629
1630 const Pointer &ThisPtr = S.Stk.peek<Pointer>(ThisOffset);
1631
1632 // C++23 [expr.const]p5.6
1633 // an invocation of a virtual function ([class.virtual]) for an object whose
1634 // dynamic type is constexpr-unknown;
1635 if (ThisPtr.isDummy() && Func->isVirtual())
1636 return false;
1637
1638 // If the current function is a lambda static invoker and
1639 // the function we're about to call is a lambda call operator,
1640 // skip the CheckInvoke, since the ThisPtr is a null pointer
1641 // anyway.
1642 if (S.Current->getFunction() &&
1643 S.Current->getFunction()->isLambdaStaticInvoker() &&
1644 Func->isLambdaCallOperator()) {
1645 assert(ThisPtr.isZero());
1646 } else {
1647 if (!CheckInvoke(S, OpPC, ThisPtr))
1648 return cleanup();
1649 if (!Func->isConstructor() && !Func->isDestructor() &&
1650 !CheckActive(S, OpPC, ThisPtr, AK_MemberCall))
1651 return false;
1652 }
1653
1654 if (Func->isConstructor() && !checkConstructor(S, OpPC, Func, ThisPtr))
1655 return false;
1656 if (Func->isDestructor() && !CheckDestructor(S, OpPC, ThisPtr))
1657 return false;
1658
1659 if (Func->isConstructor() || Func->isDestructor())
1660 S.InitializingBlocks.push_back(ThisPtr.block());
1661 }
1662
1663 if (!Func->isFullyCompiled())
1664 compileFunction(S, Func);
1665
1666 if (!CheckCallable(S, OpPC, Func))
1667 return cleanup();
1668
1669 // FIXME: The isConstructor() check here is not always right. The current
1670 // constant evaluator is somewhat inconsistent in when it allows a function
1671 // call when checking for a constant expression.
1672 if (Func->hasThisPointer() && S.checkingPotentialConstantExpression() &&
1673 !Func->isConstructor())
1674 return cleanup();
1675
1676 if (!CheckCallDepth(S, OpPC))
1677 return cleanup();
1678
1679 auto NewFrame = std::make_unique<InterpFrame>(S, Func, OpPC, VarArgSize);
1680 InterpFrame *FrameBefore = S.Current;
1681 S.Current = NewFrame.get();
1682
1683 InterpStateCCOverride CCOverride(S, Func->isImmediate());
1684 // Note that we cannot assert(CallResult.hasValue()) here since
1685 // Ret() above only sets the APValue if the curent frame doesn't
1686 // have a caller set.
1687 bool Success = Interpret(S);
1688 // Remove initializing block again.
1689 if (Func->isConstructor() || Func->isDestructor())
1690 S.InitializingBlocks.pop_back();
1691
1692 if (!Success) {
1693 // Interpreting the function failed somehow. Reset to
1694 // previous state.
1695 S.Current = FrameBefore;
1696 return false;
1697 }
1698
1699 NewFrame.release(); // Frame was delete'd already.
1700 assert(S.Current == FrameBefore);
1701 return true;
1702 }
1703
1704 static bool GetDynamicDecl(InterpState &S, CodePtr OpPC, Pointer TypePtr,
1705 const CXXRecordDecl *&DynamicDecl) {
1706 while (TypePtr.isBaseClass())
1707 TypePtr = TypePtr.getBase();
1708
1709 QualType DynamicType = TypePtr.getType();
1710 if (TypePtr.isStatic() || TypePtr.isConst()) {
1711 if (const VarDecl *VD = TypePtr.getDeclDesc()->asVarDecl();
1712 VD && !VD->isConstexpr()) {
1713 const Expr *E = S.Current->getExpr(OpPC);
1714 APValue V = TypePtr.toAPValue(S.getASTContext());
1715 QualType TT = S.getASTContext().getLValueReferenceType(DynamicType);
1716 S.FFDiag(E, diag::note_constexpr_polymorphic_unknown_dynamic_type)
1717 << AccessKinds::AK_MemberCall << V.getAsString(S.getASTContext(), TT);
1718 return false;
1719 }
1720 }
1721
1722 if (DynamicType->isPointerType() || DynamicType->isReferenceType()) {
1723 DynamicDecl = DynamicType->getPointeeCXXRecordDecl();
1724 } else if (DynamicType->isArrayType()) {
1725 const Type *ElemType = DynamicType->getPointeeOrArrayElementType();
1726 assert(ElemType);
1727 DynamicDecl = ElemType->getAsCXXRecordDecl();
1728 } else {
1729 DynamicDecl = DynamicType->getAsCXXRecordDecl();
1730 }
1731 return true;
1732 }
1733
1734 bool CallVirt(InterpState &S, CodePtr OpPC, const Function *Func,
1735 uint32_t VarArgSize) {
1736 assert(Func->hasThisPointer());
1737 assert(Func->isVirtual());
1738 size_t ArgSize = Func->getArgSize() + VarArgSize;
1739 size_t ThisOffset = ArgSize - (Func->hasRVO() ? primSize(PT_Ptr) : 0);
1740 Pointer &ThisPtr = S.Stk.peek<Pointer>(ThisOffset);
1741 const FunctionDecl *Callee = Func->getDecl();
1742
1743 const CXXRecordDecl *DynamicDecl = nullptr;
1744 if (!GetDynamicDecl(S, OpPC, ThisPtr, DynamicDecl))
1745 return false;
1746 assert(DynamicDecl);
1747
1748 const auto *StaticDecl = cast<CXXRecordDecl>(Func->getParentDecl());
1749 const auto *InitialFunction = cast<CXXMethodDecl>(Callee);
1750 const CXXMethodDecl *Overrider;
1751
1752 if (StaticDecl != DynamicDecl &&
1753 !llvm::is_contained(S.InitializingBlocks, ThisPtr.block())) {
1754 if (!DynamicDecl->isDerivedFrom(StaticDecl))
1755 return false;
1756 Overrider = S.getContext().getOverridingFunction(DynamicDecl, StaticDecl,
1757 InitialFunction);
1758
1759 } else {
1760 Overrider = InitialFunction;
1761 }
1762
1763 // C++2a [class.abstract]p6:
1764 // the effect of making a virtual call to a pure virtual function [...] is
1765 // undefined
1766 if (Overrider->isPureVirtual()) {
1767 S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_pure_virtual_call,
1768 1)
1769 << Callee;
1770 S.Note(Callee->getLocation(), diag::note_declared_at);
1771 return false;
1772 }
1773
1774 if (Overrider != InitialFunction) {
1775 // DR1872: An instantiated virtual constexpr function can't be called in a
1776 // constant expression (prior to C++20). We can still constant-fold such a
1777 // call.
1778 if (!S.getLangOpts().CPlusPlus20 && Overrider->isVirtual()) {
1779 const Expr *E = S.Current->getExpr(OpPC);
1780 S.CCEDiag(E, diag::note_constexpr_virtual_call) << E->getSourceRange();
1781 }
1782
1783 Func = S.getContext().getOrCreateFunction(Overrider);
1784
1785 const CXXRecordDecl *ThisFieldDecl =
1786 ThisPtr.getFieldDesc()->getType()->getAsCXXRecordDecl();
1787 if (Func->getParentDecl()->isDerivedFrom(ThisFieldDecl)) {
1788 // If the function we call is further DOWN the hierarchy than the
1789 // FieldDesc of our pointer, just go up the hierarchy of this field
1790 // the furthest we can go.
1791 while (ThisPtr.isBaseClass())
1792 ThisPtr = ThisPtr.getBase();
1793 }
1794 }
1795
1796 if (!Call(S, OpPC, Func, VarArgSize))
1797 return false;
1798
1799 // Covariant return types. The return type of Overrider is a pointer
1800 // or reference to a class type.
1801 if (Overrider != InitialFunction &&
1802 Overrider->getReturnType()->isPointerOrReferenceType() &&
1803 InitialFunction->getReturnType()->isPointerOrReferenceType()) {
1804 QualType OverriderPointeeType =
1805 Overrider->getReturnType()->getPointeeType();
1806 QualType InitialPointeeType =
1807 InitialFunction->getReturnType()->getPointeeType();
1808 // We've called Overrider above, but calling code expects us to return what
1809 // InitialFunction returned. According to the rules for covariant return
1810 // types, what InitialFunction returns needs to be a base class of what
1811 // Overrider returns. So, we need to do an upcast here.
1812 unsigned Offset = S.getContext().collectBaseOffset(
1813 InitialPointeeType->getAsRecordDecl(),
1814 OverriderPointeeType->getAsRecordDecl());
1815 return GetPtrBasePop(S, OpPC, Offset, /*IsNullOK=*/true);
1816 }
1817
1818 return true;
1819 }
1820
1821 bool CallBI(InterpState &S, CodePtr OpPC, const CallExpr *CE,
1822 uint32_t BuiltinID) {
1823 // A little arbitrary, but the current interpreter allows evaluation
1824 // of builtin functions in this mode, with some exceptions.
1825 if (BuiltinID == Builtin::BI__builtin_operator_new &&
1826 S.checkingPotentialConstantExpression())
1827 return false;
1828
1829 return InterpretBuiltin(S, OpPC, CE, BuiltinID);
1830 }
1831
1832 bool CallPtr(InterpState &S, CodePtr OpPC, uint32_t ArgSize,
1833 const CallExpr *CE) {
1834 const Pointer &Ptr = S.Stk.pop<Pointer>();
1835
1836 if (Ptr.isZero()) {
1837 S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_null_callee)
1838 << const_cast<Expr *>(CE->getCallee()) << CE->getSourceRange();
1839 return false;
1840 }
1841
1842 if (!Ptr.isFunctionPointer())
1843 return Invalid(S, OpPC);
1844
1845 const FunctionPointer &FuncPtr = Ptr.asFunctionPointer();
1846 const Function *F = FuncPtr.getFunction();
1847 assert(F);
1848 // Don't allow calling block pointers.
1849 if (!F->getDecl())
1850 return Invalid(S, OpPC);
1851
1852 // This happens when the call expression has been cast to
1853 // something else, but we don't support that.
1854 if (S.Ctx.classify(F->getDecl()->getReturnType()) !=
1855 S.Ctx.classify(CE->getCallReturnType(S.getASTContext())))
1856 return false;
1857
1858 // Check argument nullability state.
1859 if (F->hasNonNullAttr()) {
1860 if (!CheckNonNullArgs(S, OpPC, F, CE, ArgSize))
1861 return false;
1862 }
1863
1864 // Can happen when casting function pointers around.
1865 QualType CalleeType = CE->getCallee()->getType();
1866 if (CalleeType->isPointerType() &&
1867 !S.getASTContext().hasSameFunctionTypeIgnoringExceptionSpec(
1868 F->getDecl()->getType(), CalleeType->getPointeeType())) {
1869 return false;
1870 }
1871
1872 // We nedd to compile (and check) early for function pointer calls
1873 // because the Call/CallVirt below might access the instance pointer
1874 // but the Function's information about them is wrong.
1875 if (!F->isFullyCompiled())
1876 compileFunction(S, F);
1877
1878 if (!CheckCallable(S, OpPC, F))
1879 return false;
1880
1881 assert(ArgSize >= F->getWrittenArgSize());
1882 uint32_t VarArgSize = ArgSize - F->getWrittenArgSize();
1883
1884 // We need to do this explicitly here since we don't have the necessary
1885 // information to do it automatically.
1886 if (F->isThisPointerExplicit())
1887 VarArgSize -= align(primSize(PT_Ptr));
1888
1889 if (F->isVirtual())
1890 return CallVirt(S, OpPC, F, VarArgSize);
1891
1892 return Call(S, OpPC, F, VarArgSize);
1893 }
1894
1895 static void startLifetimeRecurse(const Pointer &Ptr) {
1896 if (const Record *R = Ptr.getRecord()) {
1897 Ptr.startLifetime();
1898 for (const Record::Field &Fi : R->fields())
1899 startLifetimeRecurse(Ptr.atField(Fi.Offset));
1900 return;
1901 }
1902
1903 if (const Descriptor *FieldDesc = Ptr.getFieldDesc();
1904 FieldDesc->isCompositeArray()) {
1905 assert(Ptr.getLifetime() == Lifetime::Started);
1906 for (unsigned I = 0; I != FieldDesc->getNumElems(); ++I)
1907 startLifetimeRecurse(Ptr.atIndex(I).narrow());
1908 return;
1909 }
1910
1911 Ptr.startLifetime();
1912 }
1913
1914 bool StartLifetime(InterpState &S, CodePtr OpPC) {
1915 const auto &Ptr = S.Stk.peek<Pointer>();
1916 if (Ptr.isBlockPointer() && !CheckDummy(S, OpPC, Ptr.block(), AK_Destroy))
1917 return false;
1918 startLifetimeRecurse(Ptr.narrow());
1919 return true;
1920 }
1921
1922 // FIXME: It might be better to the recursing as part of the generated code for
1923 // a destructor?
1924 static void endLifetimeRecurse(const Pointer &Ptr) {
1925 if (const Record *R = Ptr.getRecord()) {
1926 Ptr.endLifetime();
1927 for (const Record::Field &Fi : R->fields())
1928 endLifetimeRecurse(Ptr.atField(Fi.Offset));
1929 return;
1930 }
1931
1932 if (const Descriptor *FieldDesc = Ptr.getFieldDesc();
1933 FieldDesc->isCompositeArray()) {
1934 // No endLifetime() for array roots.
1935 assert(Ptr.getLifetime() == Lifetime::Started);
1936 for (unsigned I = 0; I != FieldDesc->getNumElems(); ++I)
1937 endLifetimeRecurse(Ptr.atIndex(I).narrow());
1938 return;
1939 }
1940
1941 Ptr.endLifetime();
1942 }
1943
1944 /// Ends the lifetime of the peek'd pointer.
1945 bool EndLifetime(InterpState &S, CodePtr OpPC) {
1946 const auto &Ptr = S.Stk.peek<Pointer>();
1947 if (Ptr.isBlockPointer() && !CheckDummy(S, OpPC, Ptr.block(), AK_Destroy))
1948 return false;
1949
1950 // FIXME: We need per-element lifetime information for primitive arrays.
1951 if (Ptr.isArrayElement())
1952 return true;
1953
1954 endLifetimeRecurse(Ptr.narrow());
1955 return true;
1956 }
1957
1958 /// Ends the lifetime of the pop'd pointer.
1959 bool EndLifetimePop(InterpState &S, CodePtr OpPC) {
1960 const auto &Ptr = S.Stk.pop<Pointer>();
1961 if (Ptr.isBlockPointer() && !CheckDummy(S, OpPC, Ptr.block(), AK_Destroy))
1962 return false;
1963
1964 // FIXME: We need per-element lifetime information for primitive arrays.
1965 if (Ptr.isArrayElement())
1966 return true;
1967
1968 endLifetimeRecurse(Ptr.narrow());
1969 return true;
1970 }
1971
1972 bool CheckNewTypeMismatch(InterpState &S, CodePtr OpPC, const Expr *E,
1973 std::optional<uint64_t> ArraySize) {
1974 const Pointer &Ptr = S.Stk.peek<Pointer>();
1975
1976 if (Ptr.inUnion() && Ptr.getBase().getRecord()->isUnion())
1977 Ptr.activate();
1978
1979 if (Ptr.isZero()) {
1980 S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_access_null)
1981 << AK_Construct;
1982 return false;
1983 }
1984
1985 if (!Ptr.isBlockPointer())
1986 return false;
1987
1988 startLifetimeRecurse(Ptr);
1989
1990 // Similar to CheckStore(), but with the additional CheckTemporary() call and
1991 // the AccessKinds are different.
1992 if (!Ptr.block()->isAccessible()) {
1993 if (!CheckExtern(S, OpPC, Ptr))
1994 return false;
1995 if (!CheckLive(S, OpPC, Ptr, AK_Construct))
1996 return false;
1997 return CheckDummy(S, OpPC, Ptr.block(), AK_Construct);
1998 }
1999 if (!CheckTemporary(S, OpPC, Ptr.block(), AK_Construct))
2000 return false;
2001
2002 // CheckLifetime for this and all base pointers.
2003 for (Pointer P = Ptr;;) {
2004 if (!CheckLifetime(S, OpPC, P.getLifetime(), AK_Construct))
2005 return false;
2006
2007 if (P.isRoot())
2008 break;
2009 P = P.getBase();
2010 }
2011
2012 if (!CheckRange(S, OpPC, Ptr, AK_Construct))
2013 return false;
2014 if (!CheckGlobal(S, OpPC, Ptr))
2015 return false;
2016 if (!CheckConst(S, OpPC, Ptr))
2017 return false;
2018 if (!S.inConstantContext() && isConstexprUnknown(Ptr))
2019 return false;
2020
2021 if (!InvalidNewDeleteExpr(S, OpPC, E))
2022 return false;
2023
2024 const auto *NewExpr = cast<CXXNewExpr>(E);
2025 QualType StorageType = Ptr.getFieldDesc()->getDataType(S.getASTContext());
2026 const ASTContext &ASTCtx = S.getASTContext();
2027 QualType AllocType;
2028 if (ArraySize) {
2029 AllocType = ASTCtx.getConstantArrayType(
2030 NewExpr->getAllocatedType(),
2031 APInt(64, static_cast<uint64_t>(*ArraySize), false), nullptr,
2032 ArraySizeModifier::Normal, 0);
2033 } else {
2034 AllocType = NewExpr->getAllocatedType();
2035 }
2036
2037 unsigned StorageSize = 1;
2038 unsigned AllocSize = 1;
2039 if (const auto *CAT = dyn_cast<ConstantArrayType>(AllocType))
2040 AllocSize = CAT->getZExtSize();
2041 if (const auto *CAT = dyn_cast<ConstantArrayType>(StorageType))
2042 StorageSize = CAT->getZExtSize();
2043
2044 if (AllocSize > StorageSize ||
2045 !ASTCtx.hasSimilarType(ASTCtx.getBaseElementType(AllocType),
2046 ASTCtx.getBaseElementType(StorageType))) {
2047 S.FFDiag(S.Current->getLocation(OpPC),
2048 diag::note_constexpr_placement_new_wrong_type)
2049 << StorageType << AllocType;
2050 return false;
2051 }
2052
2053 // Can't activate fields in a union, unless the direct base is the union.
2054 if (Ptr.inUnion() && !Ptr.isActive() && !Ptr.getBase().getRecord()->isUnion())
2055 return CheckActive(S, OpPC, Ptr, AK_Construct);
2056
2057 return true;
2058 }
2059
2060 bool InvalidNewDeleteExpr(InterpState &S, CodePtr OpPC, const Expr *E) {
2061 assert(E);
2062
2063 if (const auto *NewExpr = dyn_cast<CXXNewExpr>(E)) {
2064 const FunctionDecl *OperatorNew = NewExpr->getOperatorNew();
2065
2066 if (NewExpr->getNumPlacementArgs() > 0) {
2067 // This is allowed pre-C++26, but only an std function.
2068 if (S.getLangOpts().CPlusPlus26 || S.Current->isStdFunction())
2069 return true;
2070 S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_new_placement)
2071 << /*C++26 feature*/ 1 << E->getSourceRange();
2072 } else if (
2073 !OperatorNew
2074 ->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) {
2075 S.FFDiag(S.Current->getSource(OpPC),
2076 diag::note_constexpr_new_non_replaceable)
2077 << isa<CXXMethodDecl>(OperatorNew) << OperatorNew;
2078 return false;
2079 } else if (!S.getLangOpts().CPlusPlus26 &&
2080 NewExpr->getNumPlacementArgs() == 1 &&
2081 !OperatorNew->isReservedGlobalPlacementOperator()) {
2082 if (!S.getLangOpts().CPlusPlus26) {
2083 S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_new_placement)
2084 << /*Unsupported*/ 0 << E->getSourceRange();
2085 return false;
2086 }
2087 return true;
2088 }
2089 } else {
2090 const auto *DeleteExpr = cast<CXXDeleteExpr>(E);
2091 const FunctionDecl *OperatorDelete = DeleteExpr->getOperatorDelete();
2092 if (!OperatorDelete
2093 ->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) {
2094 S.FFDiag(S.Current->getSource(OpPC),
2095 diag::note_constexpr_new_non_replaceable)
2096 << isa<CXXMethodDecl>(OperatorDelete) << OperatorDelete;
2097 return false;
2098 }
2099 }
2100
2101 return false;
2102 }
2103
2104 bool handleFixedPointOverflow(InterpState &S, CodePtr OpPC,
2105 const FixedPoint &FP) {
2106 const Expr *E = S.Current->getExpr(OpPC);
2107 if (S.checkingForUndefinedBehavior()) {
2108 S.getASTContext().getDiagnostics().Report(
2109 E->getExprLoc(), diag::warn_fixedpoint_constant_overflow)
2110 << FP.toDiagnosticString(S.getASTContext()) << E->getType();
2111 }
2112 S.CCEDiag(E, diag::note_constexpr_overflow)
2113 << FP.toDiagnosticString(S.getASTContext()) << E->getType();
2114 return S.noteUndefinedBehavior();
2115 }
2116
2117 bool InvalidShuffleVectorIndex(InterpState &S, CodePtr OpPC, uint32_t Index) {
2118 const SourceInfo &Loc = S.Current->getSource(OpPC);
2119 S.FFDiag(Loc,
2120 diag::err_shufflevector_minus_one_is_undefined_behavior_constexpr)
2121 << Index;
2122 return false;
2123 }
2124
2125 bool CheckPointerToIntegralCast(InterpState &S, CodePtr OpPC,
2126 const Pointer &Ptr, unsigned BitWidth) {
2127 const SourceInfo &E = S.Current->getSource(OpPC);
2128 S.CCEDiag(E, diag::note_constexpr_invalid_cast)
2129 << 2 << S.getLangOpts().CPlusPlus << S.Current->getRange(OpPC);
2130
2131 if (Ptr.isDummy())
2132 return false;
2133 if (Ptr.isFunctionPointer())
2134 return true;
2135
2136 if (Ptr.isBlockPointer() && !Ptr.isZero()) {
2137 // Only allow based lvalue casts if they are lossless.
2138 if (S.getASTContext().getTargetInfo().getPointerWidth(LangAS::Default) !=
2139 BitWidth)
2140 return Invalid(S, OpPC);
2141 }
2142 return true;
2143 }
2144
2145 bool CastPointerIntegralAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
2146 const Pointer &Ptr = S.Stk.pop<Pointer>();
2147
2148 if (!CheckPointerToIntegralCast(S, OpPC, Ptr, BitWidth))
2149 return false;
2150
2151 auto Result = S.allocAP<IntegralAP<false>>(BitWidth);
2152 Result.copy(APInt(BitWidth, Ptr.getIntegerRepresentation()));
2153
2154 S.Stk.push<IntegralAP<false>>(Result);
2155 return true;
2156 }
2157
2158 bool CastPointerIntegralAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
2159 const Pointer &Ptr = S.Stk.pop<Pointer>();
2160
2161 if (!CheckPointerToIntegralCast(S, OpPC, Ptr, BitWidth))
2162 return false;
2163
2164 auto Result = S.allocAP<IntegralAP<true>>(BitWidth);
2165 Result.copy(APInt(BitWidth, Ptr.getIntegerRepresentation()));
2166
2167 S.Stk.push<IntegralAP<true>>(Result);
2168 return true;
2169 }
2170
2171 bool CheckBitCast(InterpState &S, CodePtr OpPC, bool HasIndeterminateBits,
2172 bool TargetIsUCharOrByte) {
2173 // This is always fine.
2174 if (!HasIndeterminateBits)
2175 return true;
2176
2177 // Indeterminate bits can only be bitcast to unsigned char or std::byte.
2178 if (TargetIsUCharOrByte)
2179 return true;
2180
2181 const Expr *E = S.Current->getExpr(OpPC);
2182 QualType ExprType = E->getType();
2183 S.FFDiag(E, diag::note_constexpr_bit_cast_indet_dest)
2184 << ExprType << S.getLangOpts().CharIsSigned << E->getSourceRange();
2185 return false;
2186 }
2187
2188 bool GetTypeid(InterpState &S, CodePtr OpPC, const Type *TypePtr,
2189 const Type *TypeInfoType) {
2190 S.Stk.push<Pointer>(TypePtr, TypeInfoType);
2191 return true;
2192 }
2193
2194 bool GetTypeidPtr(InterpState &S, CodePtr OpPC, const Type *TypeInfoType) {
2195 const auto &P = S.Stk.pop<Pointer>();
2196
2197 if (!P.isBlockPointer())
2198 return false;
2199
2200 // Pick the most-derived type.
2201 CanQualType T = P.getDeclPtr().getType()->getCanonicalTypeUnqualified();
2202 // ... unless we're currently constructing this object.
2203 // FIXME: We have a similar check to this in more places.
2204 if (S.Current->getFunction()) {
2205 for (const InterpFrame *Frame = S.Current; Frame; Frame = Frame->Caller) {
2206 if (const Function *Func = Frame->getFunction();
2207 Func && (Func->isConstructor() || Func->isDestructor()) &&
2208 P.block() == Frame->getThis().block()) {
2209 T = S.getContext().getASTContext().getCanonicalTagType(
2210 Func->getParentDecl());
2211 break;
2212 }
2213 }
2214 }
2215
2216 S.Stk.push<Pointer>(T->getTypePtr(), TypeInfoType);
2217 return true;
2218 }
2219
2220 bool DiagTypeid(InterpState &S, CodePtr OpPC) {
2221 const auto *E = cast<CXXTypeidExpr>(S.Current->getExpr(OpPC));
2222 S.CCEDiag(E, diag::note_constexpr_typeid_polymorphic)
2223 << E->getExprOperand()->getType()
2224 << E->getExprOperand()->getSourceRange();
2225 return false;
2226 }
2227
2228 bool arePotentiallyOverlappingStringLiterals(const Pointer &LHS,
2229 const Pointer &RHS) {
2230 unsigned LHSOffset = LHS.isOnePastEnd() ? LHS.getNumElems() : LHS.getIndex();
2231 unsigned RHSOffset = RHS.isOnePastEnd() ? RHS.getNumElems() : RHS.getIndex();
2232 unsigned LHSLength = (LHS.getNumElems() - 1) * LHS.elemSize();
2233 unsigned RHSLength = (RHS.getNumElems() - 1) * RHS.elemSize();
2234
2235 StringRef LHSStr((const char *)LHS.atIndex(0).getRawAddress(), LHSLength);
2236 StringRef RHSStr((const char *)RHS.atIndex(0).getRawAddress(), RHSLength);
2237 int32_t IndexDiff = RHSOffset - LHSOffset;
2238 if (IndexDiff < 0) {
2239 if (static_cast<int32_t>(LHSLength) < -IndexDiff)
2240 return false;
2241 LHSStr = LHSStr.drop_front(-IndexDiff);
2242 } else {
2243 if (static_cast<int32_t>(RHSLength) < IndexDiff)
2244 return false;
2245 RHSStr = RHSStr.drop_front(IndexDiff);
2246 }
2247
2248 unsigned ShorterCharWidth;
2249 StringRef Shorter;
2250 StringRef Longer;
2251 if (LHSLength < RHSLength) {
2252 ShorterCharWidth = LHS.elemSize();
2253 Shorter = LHSStr;
2254 Longer = RHSStr;
2255 } else {
2256 ShorterCharWidth = RHS.elemSize();
2257 Shorter = RHSStr;
2258 Longer = LHSStr;
2259 }
2260
2261 // The null terminator isn't included in the string data, so check for it
2262 // manually. If the longer string doesn't have a null terminator where the
2263 // shorter string ends, they aren't potentially overlapping.
2264 for (unsigned NullByte : llvm::seq(ShorterCharWidth)) {
2265 if (Shorter.size() + NullByte >= Longer.size())
2266 break;
2267 if (Longer[Shorter.size() + NullByte])
2268 return false;
2269 }
2270 return Shorter == Longer.take_front(Shorter.size());
2271 }
2272
2273 static void copyPrimitiveMemory(InterpState &S, const Pointer &Ptr,
2274 PrimType T) {
2275
2276 if (T == PT_IntAPS) {
2277 auto &Val = Ptr.deref<IntegralAP<true>>();
2278 if (!Val.singleWord()) {
2279 uint64_t *NewMemory = new (S.P) uint64_t[Val.numWords()];
2280 Val.take(NewMemory);
2281 }
2282 } else if (T == PT_IntAP) {
2283 auto &Val = Ptr.deref<IntegralAP<false>>();
2284 if (!Val.singleWord()) {
2285 uint64_t *NewMemory = new (S.P) uint64_t[Val.numWords()];
2286 Val.take(NewMemory);
2287 }
2288 } else if (T == PT_Float) {
2289 auto &Val = Ptr.deref<Floating>();
2290 if (!Val.singleWord()) {
2291 uint64_t *NewMemory = new (S.P) uint64_t[Val.numWords()];
2292 Val.take(NewMemory);
2293 }
2294 }
2295 }
2296
2297 template <typename T>
2298 static void copyPrimitiveMemory(InterpState &S, const Pointer &Ptr) {
2299 assert(needsAlloc<T>());
2300 auto &Val = Ptr.deref<T>();
2301 if (!Val.singleWord()) {
2302 uint64_t *NewMemory = new (S.P) uint64_t[Val.numWords()];
2303 Val.take(NewMemory);
2304 }
2305 }
2306
2307 static void finishGlobalRecurse(InterpState &S, const Pointer &Ptr) {
2308 if (const Record *R = Ptr.getRecord()) {
2309 for (const Record::Field &Fi : R->fields()) {
2310 if (Fi.Desc->isPrimitive()) {
2311 TYPE_SWITCH_ALLOC(Fi.Desc->getPrimType(), {
2312 copyPrimitiveMemory<T>(S, Ptr.atField(Fi.Offset));
2313 });
2314 copyPrimitiveMemory(S, Ptr.atField(Fi.Offset), Fi.Desc->getPrimType());
2315 } else
2316 finishGlobalRecurse(S, Ptr.atField(Fi.Offset));
2317 }
2318 return;
2319 }
2320
2321 if (const Descriptor *D = Ptr.getFieldDesc(); D && D->isArray()) {
2322 unsigned NumElems = D->getNumElems();
2323 if (NumElems == 0)
2324 return;
2325
2326 if (D->isPrimitiveArray()) {
2327 PrimType PT = D->getPrimType();
2328 if (!needsAlloc(PT))
2329 return;
2330 assert(NumElems >= 1);
2331 const Pointer EP = Ptr.atIndex(0);
2332 bool AllSingleWord = true;
2333 TYPE_SWITCH_ALLOC(PT, {
2334 if (!EP.deref<T>().singleWord()) {
2335 copyPrimitiveMemory<T>(S, EP);
2336 AllSingleWord = false;
2337 }
2338 });
2339 if (AllSingleWord)
2340 return;
2341 for (unsigned I = 1; I != D->getNumElems(); ++I) {
2342 const Pointer EP = Ptr.atIndex(I);
2343 copyPrimitiveMemory(S, EP, PT);
2344 }
2345 } else {
2346 assert(D->isCompositeArray());
2347 for (unsigned I = 0; I != D->getNumElems(); ++I) {
2348 const Pointer EP = Ptr.atIndex(I).narrow();
2349 finishGlobalRecurse(S, EP);
2350 }
2351 }
2352 }
2353 }
2354
2355 bool FinishInitGlobal(InterpState &S, CodePtr OpPC) {
2356 const Pointer &Ptr = S.Stk.pop<Pointer>();
2357
2358 finishGlobalRecurse(S, Ptr);
2359 if (Ptr.canBeInitialized()) {
2360 Ptr.initialize();
2361 Ptr.activate();
2362 }
2363
2364 return true;
2365 }
2366
2367 // https://github.com/llvm/llvm-project/issues/102513
2368 #if defined(_MSC_VER) && !defined(__clang__) && !defined(NDEBUG)
2369 #pragma optimize("", off)
2370 #endif
2371 bool Interpret(InterpState &S) {
2372 // The current stack frame when we started Interpret().
2373 // This is being used by the ops to determine wheter
2374 // to return from this function and thus terminate
2375 // interpretation.
2376 const InterpFrame *StartFrame = S.Current;
2377 assert(!S.Current->isRoot());
2378 CodePtr PC = S.Current->getPC();
2379
2380 // Empty program.
2381 if (!PC)
2382 return true;
2383
2384 for (;;) {
2385 auto Op = PC.read<Opcode>();
2386 CodePtr OpPC = PC;
2387
2388 switch (Op) {
2389 #define GET_INTERP
2390 #include "Opcodes.inc"
2391 #undef GET_INTERP
2392 }
2393 }
2394 }
2395 // https://github.com/llvm/llvm-project/issues/102513
2396 #if defined(_MSC_VER) && !defined(__clang__) && !defined(NDEBUG)
2397 #pragma optimize("", on)
2398 #endif
2399
2400 } // namespace interp
2401 } // namespace clang