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