clang 20.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 "Function.h"
11#include "InterpFrame.h"
12#include "InterpShared.h"
13#include "InterpStack.h"
14#include "Opcode.h"
15#include "PrimType.h"
16#include "Program.h"
17#include "State.h"
20#include "clang/AST/DeclObjC.h"
21#include "clang/AST/Expr.h"
22#include "clang/AST/ExprCXX.h"
25#include "llvm/ADT/StringExtras.h"
26
27using namespace clang;
28using namespace clang::interp;
29
30static bool RetValue(InterpState &S, CodePtr &Pt) {
31 llvm::report_fatal_error("Interpreter cannot return values");
32}
33
34//===----------------------------------------------------------------------===//
35// Jmp, Jt, Jf
36//===----------------------------------------------------------------------===//
37
38static bool Jmp(InterpState &S, CodePtr &PC, int32_t Offset) {
39 PC += Offset;
40 return true;
41}
42
43static bool Jt(InterpState &S, CodePtr &PC, int32_t Offset) {
44 if (S.Stk.pop<bool>()) {
45 PC += Offset;
46 }
47 return true;
48}
49
50static bool Jf(InterpState &S, CodePtr &PC, int32_t Offset) {
51 if (!S.Stk.pop<bool>()) {
52 PC += Offset;
53 }
54 return true;
55}
56
58 const ValueDecl *VD) {
59 const SourceInfo &E = S.Current->getSource(OpPC);
60 S.FFDiag(E, diag::note_constexpr_var_init_unknown, 1) << VD;
61 S.Note(VD->getLocation(), diag::note_declared_at) << VD->getSourceRange();
62}
63
65 const ValueDecl *VD);
67 const ValueDecl *D) {
68 const SourceInfo &E = S.Current->getSource(OpPC);
69
70 if (isa<ParmVarDecl>(D)) {
71 if (S.getLangOpts().CPlusPlus11) {
72 S.FFDiag(E, diag::note_constexpr_function_param_value_unknown) << D;
73 S.Note(D->getLocation(), diag::note_declared_at) << D->getSourceRange();
74 } else {
75 S.FFDiag(E);
76 }
77 return false;
78 }
79
80 if (!D->getType().isConstQualified()) {
82 } else if (const auto *VD = dyn_cast<VarDecl>(D)) {
83 if (!VD->getAnyInitializer()) {
84 diagnoseMissingInitializer(S, OpPC, VD);
85 } else {
86 const SourceInfo &Loc = S.Current->getSource(OpPC);
87 S.FFDiag(Loc, diag::note_constexpr_var_init_non_constant, 1) << VD;
88 S.Note(VD->getLocation(), diag::note_declared_at);
89 }
90 }
91
92 return false;
93}
94
96 const ValueDecl *VD) {
97 const SourceInfo &Loc = S.Current->getSource(OpPC);
98 if (!S.getLangOpts().CPlusPlus) {
99 S.FFDiag(Loc);
100 return;
101 }
102
103 if (const auto *VarD = dyn_cast<VarDecl>(VD);
104 VarD && VarD->getType().isConstQualified() &&
105 !VarD->getAnyInitializer()) {
106 diagnoseMissingInitializer(S, OpPC, VD);
107 return;
108 }
109
110 // Rather random, but this is to match the diagnostic output of the current
111 // interpreter.
112 if (isa<ObjCIvarDecl>(VD))
113 return;
114
116 S.FFDiag(Loc, diag::note_constexpr_ltor_non_const_int, 1) << VD;
117 S.Note(VD->getLocation(), diag::note_declared_at);
118 return;
119 }
120
121 S.FFDiag(Loc,
122 S.getLangOpts().CPlusPlus11 ? diag::note_constexpr_ltor_non_constexpr
123 : diag::note_constexpr_ltor_non_integral,
124 1)
125 << VD << VD->getType();
126 S.Note(VD->getLocation(), diag::note_declared_at);
127}
128
129static bool CheckActive(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
130 AccessKinds AK) {
131 if (Ptr.isActive())
132 return true;
133
134 assert(Ptr.inUnion());
135 assert(Ptr.isField() && Ptr.getField());
136
137 Pointer U = Ptr.getBase();
138 Pointer C = Ptr;
139 while (!U.isRoot() && U.inUnion() && !U.isActive()) {
140 if (U.getField())
141 C = U;
142 U = U.getBase();
143 }
144 assert(C.isField());
145
146 // Get the inactive field descriptor.
147 const FieldDecl *InactiveField = C.getField();
148 assert(InactiveField);
149
150 // Consider:
151 // union U {
152 // struct {
153 // int x;
154 // int y;
155 // } a;
156 // }
157 //
158 // When activating x, we will also activate a. If we now try to read
159 // from y, we will get to CheckActive, because y is not active. In that
160 // case, our U will be a (not a union). We return here and let later code
161 // handle this.
162 if (!U.getFieldDesc()->isUnion())
163 return true;
164
165 // Find the active field of the union.
166 const Record *R = U.getRecord();
167 assert(R && R->isUnion() && "Not a union");
168
169 const FieldDecl *ActiveField = nullptr;
170 for (const Record::Field &F : R->fields()) {
171 const Pointer &Field = U.atField(F.Offset);
172 if (Field.isActive()) {
173 ActiveField = Field.getField();
174 break;
175 }
176 }
177
178 const SourceInfo &Loc = S.Current->getSource(OpPC);
179 S.FFDiag(Loc, diag::note_constexpr_access_inactive_union_member)
180 << AK << InactiveField << !ActiveField << ActiveField;
181 return false;
182}
183
184static bool CheckTemporary(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
185 AccessKinds AK) {
186 if (auto ID = Ptr.getDeclID()) {
187 if (!Ptr.isStaticTemporary())
188 return true;
189
190 const auto *MTE = dyn_cast_if_present<MaterializeTemporaryExpr>(
191 Ptr.getDeclDesc()->asExpr());
192 if (!MTE)
193 return true;
194
195 // FIXME(perf): Since we do this check on every Load from a static
196 // temporary, it might make sense to cache the value of the
197 // isUsableInConstantExpressions call.
198 if (!MTE->isUsableInConstantExpressions(S.getASTContext()) &&
199 Ptr.block()->getEvalID() != S.Ctx.getEvalID()) {
200 const SourceInfo &E = S.Current->getSource(OpPC);
201 S.FFDiag(E, diag::note_constexpr_access_static_temporary, 1) << AK;
202 S.Note(Ptr.getDeclLoc(), diag::note_constexpr_temporary_here);
203 return false;
204 }
205 }
206 return true;
207}
208
209static bool CheckGlobal(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
210 if (auto ID = Ptr.getDeclID()) {
211 if (!Ptr.isStatic())
212 return true;
213
214 if (S.P.getCurrentDecl() == ID)
215 return true;
216
217 S.FFDiag(S.Current->getLocation(OpPC), diag::note_constexpr_modify_global);
218 return false;
219 }
220 return true;
221}
222
223namespace clang {
224namespace interp {
225static void popArg(InterpState &S, const Expr *Arg) {
226 PrimType Ty = S.getContext().classify(Arg).value_or(PT_Ptr);
227 TYPE_SWITCH(Ty, S.Stk.discard<T>());
228}
229
231 const Function *Func) {
232 assert(S.Current);
233 assert(Func);
234
235 if (Func->isUnevaluatedBuiltin())
236 return;
237
238 // Some builtin functions require us to only look at the call site, since
239 // the classified parameter types do not match.
240 if (unsigned BID = Func->getBuiltinID();
242 const auto *CE =
243 cast<CallExpr>(S.Current->Caller->getExpr(S.Current->getRetPC()));
244 for (int32_t I = CE->getNumArgs() - 1; I >= 0; --I) {
245 const Expr *A = CE->getArg(I);
246 popArg(S, A);
247 }
248 return;
249 }
250
251 if (S.Current->Caller && Func->isVariadic()) {
252 // CallExpr we're look for is at the return PC of the current function, i.e.
253 // in the caller.
254 // This code path should be executed very rarely.
255 unsigned NumVarArgs;
256 const Expr *const *Args = nullptr;
257 unsigned NumArgs = 0;
258 const Expr *CallSite = S.Current->Caller->getExpr(S.Current->getRetPC());
259 if (const auto *CE = dyn_cast<CallExpr>(CallSite)) {
260 Args = CE->getArgs();
261 NumArgs = CE->getNumArgs();
262 } else if (const auto *CE = dyn_cast<CXXConstructExpr>(CallSite)) {
263 Args = CE->getArgs();
264 NumArgs = CE->getNumArgs();
265 } else
266 assert(false && "Can't get arguments from that expression type");
267
268 assert(NumArgs >= Func->getNumWrittenParams());
269 NumVarArgs = NumArgs - (Func->getNumWrittenParams() +
270 isa<CXXOperatorCallExpr>(CallSite));
271 for (unsigned I = 0; I != NumVarArgs; ++I) {
272 const Expr *A = Args[NumArgs - 1 - I];
273 popArg(S, A);
274 }
275 }
276
277 // And in any case, remove the fixed parameters (the non-variadic ones)
278 // at the end.
279 for (PrimType Ty : Func->args_reverse())
280 TYPE_SWITCH(Ty, S.Stk.discard<T>());
281}
282
283bool CheckExtern(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
284 if (!Ptr.isExtern())
285 return true;
286
287 if (Ptr.isInitialized() ||
288 (Ptr.getDeclDesc()->asVarDecl() == S.EvaluatingDecl))
289 return true;
290
291 if (!S.checkingPotentialConstantExpression() && S.getLangOpts().CPlusPlus) {
292 const auto *VD = Ptr.getDeclDesc()->asValueDecl();
293 diagnoseNonConstVariable(S, OpPC, VD);
294 }
295 return false;
296}
297
298bool CheckArray(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
299 if (!Ptr.isUnknownSizeArray())
300 return true;
301 const SourceInfo &E = S.Current->getSource(OpPC);
302 S.FFDiag(E, diag::note_constexpr_unsized_array_indexed);
303 return false;
304}
305
306bool CheckLive(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
307 AccessKinds AK) {
308 if (Ptr.isZero()) {
309 const auto &Src = S.Current->getSource(OpPC);
310
311 if (Ptr.isField())
312 S.FFDiag(Src, diag::note_constexpr_null_subobject) << CSK_Field;
313 else
314 S.FFDiag(Src, diag::note_constexpr_access_null) << AK;
315
316 return false;
317 }
318
319 if (!Ptr.isLive()) {
320 const auto &Src = S.Current->getSource(OpPC);
321
322 if (Ptr.isDynamic()) {
323 S.FFDiag(Src, diag::note_constexpr_access_deleted_object) << AK;
324 } else {
325 bool IsTemp = Ptr.isTemporary();
326 S.FFDiag(Src, diag::note_constexpr_lifetime_ended, 1) << AK << !IsTemp;
327
328 if (IsTemp)
329 S.Note(Ptr.getDeclLoc(), diag::note_constexpr_temporary_here);
330 else
331 S.Note(Ptr.getDeclLoc(), diag::note_declared_at);
332 }
333
334 return false;
335 }
336
337 return true;
338}
339
340bool CheckConstant(InterpState &S, CodePtr OpPC, const Descriptor *Desc) {
341 assert(Desc);
342
343 const auto *D = Desc->asVarDecl();
344 if (!D || !D->hasGlobalStorage())
345 return true;
346
347 if (D == S.EvaluatingDecl)
348 return true;
349
350 if (D->isConstexpr())
351 return true;
352
353 // If we're evaluating the initializer for a constexpr variable in C23, we may
354 // only read other contexpr variables. Abort here since this one isn't
355 // constexpr.
356 if (const auto *VD = dyn_cast_if_present<VarDecl>(S.EvaluatingDecl);
357 VD && VD->isConstexpr() && S.getLangOpts().C23)
358 return Invalid(S, OpPC);
359
360 QualType T = D->getType();
361 bool IsConstant = T.isConstant(S.getASTContext());
363 if (!IsConstant) {
364 diagnoseNonConstVariable(S, OpPC, D);
365 return false;
366 }
367 return true;
368 }
369
370 if (IsConstant) {
371 if (S.getLangOpts().CPlusPlus) {
372 S.CCEDiag(S.Current->getLocation(OpPC),
373 S.getLangOpts().CPlusPlus11
374 ? diag::note_constexpr_ltor_non_constexpr
375 : diag::note_constexpr_ltor_non_integral,
376 1)
377 << D << T;
378 S.Note(D->getLocation(), diag::note_declared_at);
379 } else {
380 S.CCEDiag(S.Current->getLocation(OpPC));
381 }
382 return true;
383 }
384
387 !S.getLangOpts().CPlusPlus11) {
388 diagnoseNonConstVariable(S, OpPC, D);
389 return false;
390 }
391 return true;
392 }
393
394 diagnoseNonConstVariable(S, OpPC, D);
395 return false;
396}
397
398static bool CheckConstant(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
399 if (!Ptr.isStatic() || !Ptr.isBlockPointer())
400 return true;
401 return CheckConstant(S, OpPC, Ptr.getDeclDesc());
402}
403
404bool CheckNull(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
405 CheckSubobjectKind CSK) {
406 if (!Ptr.isZero())
407 return true;
408 const SourceInfo &Loc = S.Current->getSource(OpPC);
409 S.FFDiag(Loc, diag::note_constexpr_null_subobject)
410 << CSK << S.Current->getRange(OpPC);
411
412 return false;
413}
414
415bool CheckRange(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
416 AccessKinds AK) {
417 if (!Ptr.isOnePastEnd())
418 return true;
419 const SourceInfo &Loc = S.Current->getSource(OpPC);
420 S.FFDiag(Loc, diag::note_constexpr_access_past_end)
421 << AK << S.Current->getRange(OpPC);
422 return false;
423}
424
425bool CheckRange(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
426 CheckSubobjectKind CSK) {
427 if (!Ptr.isElementPastEnd())
428 return true;
429 const SourceInfo &Loc = S.Current->getSource(OpPC);
430 S.FFDiag(Loc, diag::note_constexpr_past_end_subobject)
431 << CSK << S.Current->getRange(OpPC);
432 return false;
433}
434
435bool CheckSubobject(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
436 CheckSubobjectKind CSK) {
437 if (!Ptr.isOnePastEnd())
438 return true;
439
440 const SourceInfo &Loc = S.Current->getSource(OpPC);
441 S.FFDiag(Loc, diag::note_constexpr_past_end_subobject)
442 << CSK << S.Current->getRange(OpPC);
443 return false;
444}
445
446bool CheckDowncast(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
447 uint32_t Offset) {
448 uint32_t MinOffset = Ptr.getDeclDesc()->getMetadataSize();
449 uint32_t PtrOffset = Ptr.getByteOffset();
450
451 // We subtract Offset from PtrOffset. The result must be at least
452 // MinOffset.
453 if (Offset < PtrOffset && (PtrOffset - Offset) >= MinOffset)
454 return true;
455
456 const auto *E = cast<CastExpr>(S.Current->getExpr(OpPC));
457 QualType TargetQT = E->getType()->getPointeeType();
458 QualType MostDerivedQT = Ptr.getDeclPtr().getType();
459
460 S.CCEDiag(E, diag::note_constexpr_invalid_downcast)
461 << MostDerivedQT << TargetQT;
462
463 return false;
464}
465
466bool CheckConst(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
467 assert(Ptr.isLive() && "Pointer is not live");
468 if (!Ptr.isConst() || Ptr.isMutable())
469 return true;
470
471 // The This pointer is writable in constructors and destructors,
472 // even if isConst() returns true.
473 // TODO(perf): We could be hitting this code path quite a lot in complex
474 // constructors. Is there a better way to do this?
475 if (S.Current->getFunction()) {
476 for (const InterpFrame *Frame = S.Current; Frame; Frame = Frame->Caller) {
477 if (const Function *Func = Frame->getFunction();
478 Func && (Func->isConstructor() || Func->isDestructor()) &&
479 Ptr.block() == Frame->getThis().block()) {
480 return true;
481 }
482 }
483 }
484
485 if (!Ptr.isBlockPointer())
486 return false;
487
488 const QualType Ty = Ptr.getType();
489 const SourceInfo &Loc = S.Current->getSource(OpPC);
490 S.FFDiag(Loc, diag::note_constexpr_modify_const_type) << Ty;
491 return false;
492}
493
494bool CheckMutable(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
495 assert(Ptr.isLive() && "Pointer is not live");
496 if (!Ptr.isMutable())
497 return true;
498
499 // In C++14 onwards, it is permitted to read a mutable member whose
500 // lifetime began within the evaluation.
501 if (S.getLangOpts().CPlusPlus14 &&
502 Ptr.block()->getEvalID() == S.Ctx.getEvalID())
503 return true;
504
505 const SourceInfo &Loc = S.Current->getSource(OpPC);
506 const FieldDecl *Field = Ptr.getField();
507 S.FFDiag(Loc, diag::note_constexpr_access_mutable, 1) << AK_Read << Field;
508 S.Note(Field->getLocation(), diag::note_declared_at);
509 return false;
510}
511
512static bool CheckVolatile(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
513 AccessKinds AK) {
514 assert(Ptr.isLive());
515
516 // FIXME: This check here might be kinda expensive. Maybe it would be better
517 // to have another field in InlineDescriptor for this?
518 if (!Ptr.isBlockPointer())
519 return true;
520
521 QualType PtrType = Ptr.getType();
522 if (!PtrType.isVolatileQualified())
523 return true;
524
525 const SourceInfo &Loc = S.Current->getSource(OpPC);
526 if (S.getLangOpts().CPlusPlus)
527 S.FFDiag(Loc, diag::note_constexpr_access_volatile_type) << AK << PtrType;
528 else
529 S.FFDiag(Loc);
530 return false;
531}
532
533bool CheckInitialized(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
534 AccessKinds AK) {
535 assert(Ptr.isLive());
536
537 if (Ptr.isInitialized())
538 return true;
539
540 if (const auto *VD = Ptr.getDeclDesc()->asVarDecl();
541 VD && VD->hasGlobalStorage()) {
542 const SourceInfo &Loc = S.Current->getSource(OpPC);
543 if (VD->getAnyInitializer()) {
544 S.FFDiag(Loc, diag::note_constexpr_var_init_non_constant, 1) << VD;
545 S.Note(VD->getLocation(), diag::note_declared_at);
546 } else {
547 diagnoseMissingInitializer(S, OpPC, VD);
548 }
549 return false;
550 }
551
552 if (!S.checkingPotentialConstantExpression()) {
553 S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_access_uninit)
554 << AK << /*uninitialized=*/true << S.Current->getRange(OpPC);
555 }
556 return false;
557}
558
560 if (Ptr.isInitialized())
561 return true;
562
563 assert(S.getLangOpts().CPlusPlus);
564 const auto *VD = cast<VarDecl>(Ptr.getDeclDesc()->asValueDecl());
565 if ((!VD->hasConstantInitialization() &&
566 VD->mightBeUsableInConstantExpressions(S.getASTContext())) ||
567 (S.getLangOpts().OpenCL && !S.getLangOpts().CPlusPlus11 &&
568 !VD->hasICEInitializer(S.getASTContext()))) {
569 const SourceInfo &Loc = S.Current->getSource(OpPC);
570 S.FFDiag(Loc, diag::note_constexpr_var_init_non_constant, 1) << VD;
571 S.Note(VD->getLocation(), diag::note_declared_at);
572 }
573 return false;
574}
575
576static bool CheckWeak(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
577 if (!Ptr.isWeak())
578 return true;
579
580 const auto *VD = Ptr.getDeclDesc()->asVarDecl();
581 assert(VD);
582 S.FFDiag(S.Current->getLocation(OpPC), diag::note_constexpr_var_init_weak)
583 << VD;
584 S.Note(VD->getLocation(), diag::note_declared_at);
585
586 return false;
587}
588
589bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
590 AccessKinds AK) {
591 if (!CheckLive(S, OpPC, Ptr, AK))
592 return false;
593 if (!CheckConstant(S, OpPC, Ptr))
594 return false;
595 if (!CheckDummy(S, OpPC, Ptr, AK))
596 return false;
597 if (!CheckExtern(S, OpPC, Ptr))
598 return false;
599 if (!CheckRange(S, OpPC, Ptr, AK))
600 return false;
601 if (!CheckActive(S, OpPC, Ptr, AK))
602 return false;
603 if (!CheckInitialized(S, OpPC, Ptr, AK))
604 return false;
605 if (!CheckTemporary(S, OpPC, Ptr, AK))
606 return false;
607 if (!CheckWeak(S, OpPC, Ptr))
608 return false;
609 if (!CheckMutable(S, OpPC, Ptr))
610 return false;
611 if (!CheckVolatile(S, OpPC, Ptr, AK))
612 return false;
613 return true;
614}
615
616/// This is not used by any of the opcodes directly. It's used by
617/// EvalEmitter to do the final lvalue-to-rvalue conversion.
618bool CheckFinalLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
619 if (!CheckLive(S, OpPC, Ptr, AK_Read))
620 return false;
621 if (!CheckConstant(S, OpPC, Ptr))
622 return false;
623
624 if (!CheckDummy(S, OpPC, Ptr, AK_Read))
625 return false;
626 if (!CheckExtern(S, OpPC, Ptr))
627 return false;
628 if (!CheckRange(S, OpPC, Ptr, AK_Read))
629 return false;
630 if (!CheckActive(S, OpPC, Ptr, AK_Read))
631 return false;
632 if (!CheckInitialized(S, OpPC, Ptr, AK_Read))
633 return false;
634 if (!CheckTemporary(S, OpPC, Ptr, AK_Read))
635 return false;
636 if (!CheckWeak(S, OpPC, Ptr))
637 return false;
638 if (!CheckMutable(S, OpPC, Ptr))
639 return false;
640 return true;
641}
642
643bool CheckStore(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
644 if (!CheckLive(S, OpPC, Ptr, AK_Assign))
645 return false;
646 if (!CheckDummy(S, OpPC, Ptr, AK_Assign))
647 return false;
648 if (!CheckExtern(S, OpPC, Ptr))
649 return false;
650 if (!CheckRange(S, OpPC, Ptr, AK_Assign))
651 return false;
652 if (!CheckGlobal(S, OpPC, Ptr))
653 return false;
654 if (!CheckConst(S, OpPC, Ptr))
655 return false;
656 return true;
657}
658
659bool CheckInvoke(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
660 if (!CheckLive(S, OpPC, Ptr, AK_MemberCall))
661 return false;
662 if (!Ptr.isDummy()) {
663 if (!CheckExtern(S, OpPC, Ptr))
664 return false;
665 if (!CheckRange(S, OpPC, Ptr, AK_MemberCall))
666 return false;
667 }
668 return true;
669}
670
671bool CheckInit(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
672 if (!CheckLive(S, OpPC, Ptr, AK_Assign))
673 return false;
674 if (!CheckRange(S, OpPC, Ptr, AK_Assign))
675 return false;
676 return true;
677}
678
679bool CheckCallable(InterpState &S, CodePtr OpPC, const Function *F) {
680
681 if (F->isVirtual() && !S.getLangOpts().CPlusPlus20) {
682 const SourceLocation &Loc = S.Current->getLocation(OpPC);
683 S.CCEDiag(Loc, diag::note_constexpr_virtual_call);
684 return false;
685 }
686
687 if (F->isConstexpr() && F->hasBody() &&
688 (F->getDecl()->isConstexpr() || F->getDecl()->hasAttr<MSConstexprAttr>()))
689 return true;
690
691 // Implicitly constexpr.
692 if (F->isLambdaStaticInvoker())
693 return true;
694
695 const SourceLocation &Loc = S.Current->getLocation(OpPC);
696 if (S.getLangOpts().CPlusPlus11) {
697 const FunctionDecl *DiagDecl = F->getDecl();
698
699 // Invalid decls have been diagnosed before.
700 if (DiagDecl->isInvalidDecl())
701 return false;
702
703 // If this function is not constexpr because it is an inherited
704 // non-constexpr constructor, diagnose that directly.
705 const auto *CD = dyn_cast<CXXConstructorDecl>(DiagDecl);
706 if (CD && CD->isInheritingConstructor()) {
707 const auto *Inherited = CD->getInheritedConstructor().getConstructor();
708 if (!Inherited->isConstexpr())
709 DiagDecl = CD = Inherited;
710 }
711
712 // FIXME: If DiagDecl is an implicitly-declared special member function
713 // or an inheriting constructor, we should be much more explicit about why
714 // it's not constexpr.
715 if (CD && CD->isInheritingConstructor()) {
716 S.FFDiag(Loc, diag::note_constexpr_invalid_inhctor, 1)
717 << CD->getInheritedConstructor().getConstructor()->getParent();
718 S.Note(DiagDecl->getLocation(), diag::note_declared_at);
719 } else {
720 // Don't emit anything if the function isn't defined and we're checking
721 // for a constant expression. It might be defined at the point we're
722 // actually calling it.
723 bool IsExtern = DiagDecl->getStorageClass() == SC_Extern;
724 if (!DiagDecl->isDefined() && !IsExtern && DiagDecl->isConstexpr() &&
725 S.checkingPotentialConstantExpression())
726 return false;
727
728 // If the declaration is defined, declared 'constexpr' _and_ has a body,
729 // the below diagnostic doesn't add anything useful.
730 if (DiagDecl->isDefined() && DiagDecl->isConstexpr() &&
731 DiagDecl->hasBody())
732 return false;
733
734 S.FFDiag(Loc, diag::note_constexpr_invalid_function, 1)
735 << DiagDecl->isConstexpr() << (bool)CD << DiagDecl;
736
737 if (DiagDecl->getDefinition())
738 S.Note(DiagDecl->getDefinition()->getLocation(),
739 diag::note_declared_at);
740 else
741 S.Note(DiagDecl->getLocation(), diag::note_declared_at);
742 }
743 } else {
744 S.FFDiag(Loc, diag::note_invalid_subexpr_in_const_expr);
745 }
746
747 return false;
748}
749
751 if ((S.Current->getDepth() + 1) > S.getLangOpts().ConstexprCallDepth) {
752 S.FFDiag(S.Current->getSource(OpPC),
753 diag::note_constexpr_depth_limit_exceeded)
754 << S.getLangOpts().ConstexprCallDepth;
755 return false;
756 }
757
758 return true;
759}
760
761bool CheckThis(InterpState &S, CodePtr OpPC, const Pointer &This) {
762 if (!This.isZero())
763 return true;
764
765 const SourceInfo &Loc = S.Current->getSource(OpPC);
766
767 bool IsImplicit = false;
768 if (const auto *E = dyn_cast_if_present<CXXThisExpr>(Loc.asExpr()))
769 IsImplicit = E->isImplicit();
770
771 if (S.getLangOpts().CPlusPlus11)
772 S.FFDiag(Loc, diag::note_constexpr_this) << IsImplicit;
773 else
774 S.FFDiag(Loc);
775
776 return false;
777}
778
779bool CheckPure(InterpState &S, CodePtr OpPC, const CXXMethodDecl *MD) {
780 if (!MD->isPureVirtual())
781 return true;
782 const SourceInfo &E = S.Current->getSource(OpPC);
783 S.FFDiag(E, diag::note_constexpr_pure_virtual_call, 1) << MD;
784 S.Note(MD->getLocation(), diag::note_declared_at);
785 return false;
786}
787
789 APFloat::opStatus Status, FPOptions FPO) {
790 // [expr.pre]p4:
791 // If during the evaluation of an expression, the result is not
792 // mathematically defined [...], the behavior is undefined.
793 // FIXME: C++ rules require us to not conform to IEEE 754 here.
794 if (Result.isNan()) {
795 const SourceInfo &E = S.Current->getSource(OpPC);
796 S.CCEDiag(E, diag::note_constexpr_float_arithmetic)
797 << /*NaN=*/true << S.Current->getRange(OpPC);
798 return S.noteUndefinedBehavior();
799 }
800
801 // In a constant context, assume that any dynamic rounding mode or FP
802 // exception state matches the default floating-point environment.
803 if (S.inConstantContext())
804 return true;
805
806 if ((Status & APFloat::opInexact) &&
807 FPO.getRoundingMode() == llvm::RoundingMode::Dynamic) {
808 // Inexact result means that it depends on rounding mode. If the requested
809 // mode is dynamic, the evaluation cannot be made in compile time.
810 const SourceInfo &E = S.Current->getSource(OpPC);
811 S.FFDiag(E, diag::note_constexpr_dynamic_rounding);
812 return false;
813 }
814
815 if ((Status != APFloat::opOK) &&
816 (FPO.getRoundingMode() == llvm::RoundingMode::Dynamic ||
818 FPO.getAllowFEnvAccess())) {
819 const SourceInfo &E = S.Current->getSource(OpPC);
820 S.FFDiag(E, diag::note_constexpr_float_arithmetic_strict);
821 return false;
822 }
823
824 if ((Status & APFloat::opStatus::opInvalidOp) &&
826 const SourceInfo &E = S.Current->getSource(OpPC);
827 // There is no usefully definable result.
828 S.FFDiag(E);
829 return false;
830 }
831
832 return true;
833}
834
836 if (S.getLangOpts().CPlusPlus20)
837 return true;
838
839 const SourceInfo &E = S.Current->getSource(OpPC);
840 S.CCEDiag(E, diag::note_constexpr_new);
841 return true;
842}
843
845 DynamicAllocator::Form AllocForm,
846 DynamicAllocator::Form DeleteForm, const Descriptor *D,
847 const Expr *NewExpr) {
848 if (AllocForm == DeleteForm)
849 return true;
850
851 QualType TypeToDiagnose;
852 // We need to shuffle things around a bit here to get a better diagnostic,
853 // because the expression we allocated the block for was of type int*,
854 // but we want to get the array size right.
855 if (D->isArray()) {
856 QualType ElemQT = D->getType()->getPointeeType();
857 TypeToDiagnose = S.getASTContext().getConstantArrayType(
858 ElemQT, APInt(64, static_cast<uint64_t>(D->getNumElems()), false),
859 nullptr, ArraySizeModifier::Normal, 0);
860 } else
861 TypeToDiagnose = D->getType()->getPointeeType();
862
863 const SourceInfo &E = S.Current->getSource(OpPC);
864 S.FFDiag(E, diag::note_constexpr_new_delete_mismatch)
865 << static_cast<int>(DeleteForm) << static_cast<int>(AllocForm)
866 << TypeToDiagnose;
867 S.Note(NewExpr->getExprLoc(), diag::note_constexpr_dynamic_alloc_here)
868 << NewExpr->getSourceRange();
869 return false;
870}
871
872bool CheckDeleteSource(InterpState &S, CodePtr OpPC, const Expr *Source,
873 const Pointer &Ptr) {
874 // The two sources we currently allow are new expressions and
875 // __builtin_operator_new calls.
876 if (isa_and_nonnull<CXXNewExpr>(Source))
877 return true;
878 if (const CallExpr *CE = dyn_cast_if_present<CallExpr>(Source);
879 CE && CE->getBuiltinCallee() == Builtin::BI__builtin_operator_new)
880 return true;
881
882 // Whatever this is, we didn't heap allocate it.
883 const SourceInfo &Loc = S.Current->getSource(OpPC);
884 S.FFDiag(Loc, diag::note_constexpr_delete_not_heap_alloc)
886
887 if (Ptr.isTemporary())
888 S.Note(Ptr.getDeclLoc(), diag::note_constexpr_temporary_here);
889 else
890 S.Note(Ptr.getDeclLoc(), diag::note_declared_at);
891 return false;
892}
893
894/// We aleady know the given DeclRefExpr is invalid for some reason,
895/// now figure out why and print appropriate diagnostics.
896bool CheckDeclRef(InterpState &S, CodePtr OpPC, const DeclRefExpr *DR) {
897 const ValueDecl *D = DR->getDecl();
898 return diagnoseUnknownDecl(S, OpPC, D);
899}
900
901bool CheckDummy(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
902 AccessKinds AK) {
903 if (!Ptr.isDummy())
904 return true;
905
906 const Descriptor *Desc = Ptr.getDeclDesc();
907 const ValueDecl *D = Desc->asValueDecl();
908 if (!D)
909 return false;
910
911 if (AK == AK_Read || AK == AK_Increment || AK == AK_Decrement)
912 return diagnoseUnknownDecl(S, OpPC, D);
913
914 assert(AK == AK_Assign);
915 if (S.getLangOpts().CPlusPlus14) {
916 const SourceInfo &E = S.Current->getSource(OpPC);
917 S.FFDiag(E, diag::note_constexpr_modify_global);
918 }
919 return false;
920}
921
923 const CallExpr *CE, unsigned ArgSize) {
924 auto Args = llvm::ArrayRef(CE->getArgs(), CE->getNumArgs());
925 auto NonNullArgs = collectNonNullArgs(F->getDecl(), Args);
926 unsigned Offset = 0;
927 unsigned Index = 0;
928 for (const Expr *Arg : Args) {
929 if (NonNullArgs[Index] && Arg->getType()->isPointerType()) {
930 const Pointer &ArgPtr = S.Stk.peek<Pointer>(ArgSize - Offset);
931 if (ArgPtr.isZero()) {
932 const SourceLocation &Loc = S.Current->getLocation(OpPC);
933 S.CCEDiag(Loc, diag::note_non_null_attribute_failed);
934 return false;
935 }
936 }
937
938 Offset += align(primSize(S.Ctx.classify(Arg).value_or(PT_Ptr)));
939 ++Index;
940 }
941 return true;
942}
943
944// FIXME: This is similar to code we already have in Compiler.cpp.
945// I think it makes sense to instead add the field and base destruction stuff
946// to the destructor Function itself. Then destroying a record would really
947// _just_ be calling its destructor. That would also help with the diagnostic
948// difference when the destructor or a field/base fails.
950 const Pointer &BasePtr,
951 const Descriptor *Desc) {
952 assert(Desc->isRecord());
953 const Record *R = Desc->ElemRecord;
954 assert(R);
955
956 if (Pointer::pointToSameBlock(BasePtr, S.Current->getThis())) {
957 const SourceInfo &Loc = S.Current->getSource(OpPC);
958 S.FFDiag(Loc, diag::note_constexpr_double_destroy);
959 return false;
960 }
961
962 // Destructor of this record.
963 if (const CXXDestructorDecl *Dtor = R->getDestructor();
964 Dtor && !Dtor->isTrivial()) {
965 const Function *DtorFunc = S.getContext().getOrCreateFunction(Dtor);
966 if (!DtorFunc)
967 return false;
968
969 S.Stk.push<Pointer>(BasePtr);
970 if (!Call(S, OpPC, DtorFunc, 0))
971 return false;
972 }
973 return true;
974}
975
976static bool RunDestructors(InterpState &S, CodePtr OpPC, const Block *B) {
977 assert(B);
978 const Descriptor *Desc = B->getDescriptor();
979
980 if (Desc->isPrimitive() || Desc->isPrimitiveArray())
981 return true;
982
983 assert(Desc->isRecord() || Desc->isCompositeArray());
984
985 if (Desc->isCompositeArray()) {
986 const Descriptor *ElemDesc = Desc->ElemDesc;
987 assert(ElemDesc->isRecord());
988
989 Pointer RP(const_cast<Block *>(B));
990 for (unsigned I = 0; I != Desc->getNumElems(); ++I) {
991 if (!runRecordDestructor(S, OpPC, RP.atIndex(I).narrow(), ElemDesc))
992 return false;
993 }
994 return true;
995 }
996
997 assert(Desc->isRecord());
998 return runRecordDestructor(S, OpPC, Pointer(const_cast<Block *>(B)), Desc);
999}
1000
1002 if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl())
1003 if (const CXXDestructorDecl *DD = RD->getDestructor())
1004 return DD->isVirtual();
1005 return false;
1006}
1007
1008bool Free(InterpState &S, CodePtr OpPC, bool DeleteIsArrayForm,
1009 bool IsGlobalDelete) {
1010 if (!CheckDynamicMemoryAllocation(S, OpPC))
1011 return false;
1012
1013 const Expr *Source = nullptr;
1014 const Block *BlockToDelete = nullptr;
1015 {
1016 // Extra scope for this so the block doesn't have this pointer
1017 // pointing to it when we destroy it.
1018 Pointer Ptr = S.Stk.pop<Pointer>();
1019
1020 // Deleteing nullptr is always fine.
1021 if (Ptr.isZero())
1022 return true;
1023
1024 // Remove base casts.
1025 QualType InitialType = Ptr.getType();
1026 while (Ptr.isBaseClass())
1027 Ptr = Ptr.getBase();
1028
1029 // For the non-array case, the types must match if the static type
1030 // does not have a virtual destructor.
1031 if (!DeleteIsArrayForm && Ptr.getType() != InitialType &&
1032 !hasVirtualDestructor(InitialType)) {
1033 S.FFDiag(S.Current->getSource(OpPC),
1034 diag::note_constexpr_delete_base_nonvirt_dtor)
1035 << InitialType << Ptr.getType();
1036 return false;
1037 }
1038
1039 if (!Ptr.isRoot() || Ptr.isOnePastEnd() || Ptr.isArrayElement()) {
1040 const SourceInfo &Loc = S.Current->getSource(OpPC);
1041 S.FFDiag(Loc, diag::note_constexpr_delete_subobject)
1042 << Ptr.toDiagnosticString(S.getASTContext()) << Ptr.isOnePastEnd();
1043 return false;
1044 }
1045
1046 Source = Ptr.getDeclDesc()->asExpr();
1047 BlockToDelete = Ptr.block();
1048
1049 if (!CheckDeleteSource(S, OpPC, Source, Ptr))
1050 return false;
1051
1052 // For a class type with a virtual destructor, the selected operator delete
1053 // is the one looked up when building the destructor.
1054 QualType AllocType = Ptr.getType();
1055 if (!DeleteIsArrayForm && !IsGlobalDelete) {
1056 auto getVirtualOperatorDelete = [](QualType T) -> const FunctionDecl * {
1057 if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl())
1058 if (const CXXDestructorDecl *DD = RD->getDestructor())
1059 return DD->isVirtual() ? DD->getOperatorDelete() : nullptr;
1060 return nullptr;
1061 };
1062
1063 if (const FunctionDecl *VirtualDelete =
1064 getVirtualOperatorDelete(AllocType);
1065 VirtualDelete &&
1066 !VirtualDelete->isReplaceableGlobalAllocationFunction()) {
1067 S.FFDiag(S.Current->getSource(OpPC),
1068 diag::note_constexpr_new_non_replaceable)
1069 << isa<CXXMethodDecl>(VirtualDelete) << VirtualDelete;
1070 return false;
1071 }
1072 }
1073 }
1074 assert(Source);
1075 assert(BlockToDelete);
1076
1077 // Invoke destructors before deallocating the memory.
1078 if (!RunDestructors(S, OpPC, BlockToDelete))
1079 return false;
1080
1081 DynamicAllocator &Allocator = S.getAllocator();
1082 const Descriptor *BlockDesc = BlockToDelete->getDescriptor();
1083 std::optional<DynamicAllocator::Form> AllocForm =
1084 Allocator.getAllocationForm(Source);
1085
1086 if (!Allocator.deallocate(Source, BlockToDelete, S)) {
1087 // Nothing has been deallocated, this must be a double-delete.
1088 const SourceInfo &Loc = S.Current->getSource(OpPC);
1089 S.FFDiag(Loc, diag::note_constexpr_double_delete);
1090 return false;
1091 }
1092
1093 assert(AllocForm);
1094 DynamicAllocator::Form DeleteForm = DeleteIsArrayForm
1097 return CheckNewDeleteForms(S, OpPC, *AllocForm, DeleteForm, BlockDesc,
1098 Source);
1099}
1100
1102 const APSInt &Value) {
1103 llvm::APInt Min;
1104 llvm::APInt Max;
1105
1106 if (S.EvaluatingDecl && !S.EvaluatingDecl->isConstexpr())
1107 return;
1108
1109 ED->getValueRange(Max, Min);
1110 --Max;
1111
1112 if (ED->getNumNegativeBits() &&
1113 (Max.slt(Value.getSExtValue()) || Min.sgt(Value.getSExtValue()))) {
1114 const SourceLocation &Loc = S.Current->getLocation(OpPC);
1115 S.CCEDiag(Loc, diag::note_constexpr_unscoped_enum_out_of_range)
1116 << llvm::toString(Value, 10) << Min.getSExtValue() << Max.getSExtValue()
1117 << ED;
1118 } else if (!ED->getNumNegativeBits() && Max.ult(Value.getZExtValue())) {
1119 const SourceLocation &Loc = S.Current->getLocation(OpPC);
1120 S.CCEDiag(Loc, diag::note_constexpr_unscoped_enum_out_of_range)
1121 << llvm::toString(Value, 10) << Min.getZExtValue() << Max.getZExtValue()
1122 << ED;
1123 }
1124}
1125
1127 assert(T);
1128 assert(!S.getLangOpts().CPlusPlus23);
1129
1130 // C++1y: A constant initializer for an object o [...] may also invoke
1131 // constexpr constructors for o and its subobjects even if those objects
1132 // are of non-literal class types.
1133 //
1134 // C++11 missed this detail for aggregates, so classes like this:
1135 // struct foo_t { union { int i; volatile int j; } u; };
1136 // are not (obviously) initializable like so:
1137 // __attribute__((__require_constant_initialization__))
1138 // static const foo_t x = {{0}};
1139 // because "i" is a subobject with non-literal initialization (due to the
1140 // volatile member of the union). See:
1141 // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1677
1142 // Therefore, we use the C++1y behavior.
1143
1144 if (S.Current->getFunction() && S.Current->getFunction()->isConstructor() &&
1145 S.Current->getThis().getDeclDesc()->asDecl() == S.EvaluatingDecl) {
1146 return true;
1147 }
1148
1149 const Expr *E = S.Current->getExpr(OpPC);
1150 if (S.getLangOpts().CPlusPlus11)
1151 S.FFDiag(E, diag::note_constexpr_nonliteral) << E->getType();
1152 else
1153 S.FFDiag(E, diag::note_invalid_subexpr_in_const_expr);
1154 return false;
1155}
1156
1157static bool checkConstructor(InterpState &S, CodePtr OpPC, const Function *Func,
1158 const Pointer &ThisPtr) {
1159 assert(Func->isConstructor());
1160
1161 const Descriptor *D = ThisPtr.getFieldDesc();
1162
1163 // FIXME: I think this case is not 100% correct. E.g. a pointer into a
1164 // subobject of a composite array.
1165 if (!D->ElemRecord)
1166 return true;
1167
1168 if (D->ElemRecord->getNumVirtualBases() == 0)
1169 return true;
1170
1171 S.FFDiag(S.Current->getLocation(OpPC), diag::note_constexpr_virtual_base)
1172 << Func->getParentDecl();
1173 return false;
1174}
1175
1177 uint32_t VarArgSize) {
1178 if (Func->hasThisPointer()) {
1179 size_t ArgSize = Func->getArgSize() + VarArgSize;
1180 size_t ThisOffset = ArgSize - (Func->hasRVO() ? primSize(PT_Ptr) : 0);
1181 const Pointer &ThisPtr = S.Stk.peek<Pointer>(ThisOffset);
1182
1183 // If the current function is a lambda static invoker and
1184 // the function we're about to call is a lambda call operator,
1185 // skip the CheckInvoke, since the ThisPtr is a null pointer
1186 // anyway.
1187 if (!(S.Current->getFunction() &&
1188 S.Current->getFunction()->isLambdaStaticInvoker() &&
1189 Func->isLambdaCallOperator())) {
1190 if (!CheckInvoke(S, OpPC, ThisPtr))
1191 return false;
1192 }
1193
1194 if (S.checkingPotentialConstantExpression())
1195 return false;
1196 }
1197
1198 if (!CheckCallable(S, OpPC, Func))
1199 return false;
1200
1201 if (!CheckCallDepth(S, OpPC))
1202 return false;
1203
1204 auto NewFrame = std::make_unique<InterpFrame>(S, Func, OpPC, VarArgSize);
1205 InterpFrame *FrameBefore = S.Current;
1206 S.Current = NewFrame.get();
1207
1208 // Note that we cannot assert(CallResult.hasValue()) here since
1209 // Ret() above only sets the APValue if the curent frame doesn't
1210 // have a caller set.
1211 if (Interpret(S)) {
1212 NewFrame.release(); // Frame was delete'd already.
1213 assert(S.Current == FrameBefore);
1214 return true;
1215 }
1216
1217 // Interpreting the function failed somehow. Reset to
1218 // previous state.
1219 S.Current = FrameBefore;
1220 return false;
1221}
1222
1223bool Call(InterpState &S, CodePtr OpPC, const Function *Func,
1224 uint32_t VarArgSize) {
1225 assert(Func);
1226 auto cleanup = [&]() -> bool {
1228 return false;
1229 };
1230
1231 if (Func->hasThisPointer()) {
1232 size_t ArgSize = Func->getArgSize() + VarArgSize;
1233 size_t ThisOffset = ArgSize - (Func->hasRVO() ? primSize(PT_Ptr) : 0);
1234
1235 const Pointer &ThisPtr = S.Stk.peek<Pointer>(ThisOffset);
1236
1237 // If the current function is a lambda static invoker and
1238 // the function we're about to call is a lambda call operator,
1239 // skip the CheckInvoke, since the ThisPtr is a null pointer
1240 // anyway.
1241 if (S.Current->getFunction() &&
1242 S.Current->getFunction()->isLambdaStaticInvoker() &&
1243 Func->isLambdaCallOperator()) {
1244 assert(ThisPtr.isZero());
1245 } else {
1246 if (!CheckInvoke(S, OpPC, ThisPtr))
1247 return cleanup();
1248 }
1249
1250 if (Func->isConstructor() && !checkConstructor(S, OpPC, Func, ThisPtr))
1251 return false;
1252 }
1253
1254 if (!CheckCallable(S, OpPC, Func))
1255 return cleanup();
1256
1257 // FIXME: The isConstructor() check here is not always right. The current
1258 // constant evaluator is somewhat inconsistent in when it allows a function
1259 // call when checking for a constant expression.
1260 if (Func->hasThisPointer() && S.checkingPotentialConstantExpression() &&
1261 !Func->isConstructor())
1262 return cleanup();
1263
1264 if (!CheckCallDepth(S, OpPC))
1265 return cleanup();
1266
1267 auto NewFrame = std::make_unique<InterpFrame>(S, Func, OpPC, VarArgSize);
1268 InterpFrame *FrameBefore = S.Current;
1269 S.Current = NewFrame.get();
1270
1271 InterpStateCCOverride CCOverride(S, Func->getDecl()->isImmediateFunction());
1272 // Note that we cannot assert(CallResult.hasValue()) here since
1273 // Ret() above only sets the APValue if the curent frame doesn't
1274 // have a caller set.
1275 if (Interpret(S)) {
1276 NewFrame.release(); // Frame was delete'd already.
1277 assert(S.Current == FrameBefore);
1278 return true;
1279 }
1280
1281 // Interpreting the function failed somehow. Reset to
1282 // previous state.
1283 S.Current = FrameBefore;
1284 return false;
1285}
1286
1288 uint32_t VarArgSize) {
1289 assert(Func->hasThisPointer());
1290 assert(Func->isVirtual());
1291 size_t ArgSize = Func->getArgSize() + VarArgSize;
1292 size_t ThisOffset = ArgSize - (Func->hasRVO() ? primSize(PT_Ptr) : 0);
1293 Pointer &ThisPtr = S.Stk.peek<Pointer>(ThisOffset);
1294
1295 const CXXRecordDecl *DynamicDecl = nullptr;
1296 {
1297 Pointer TypePtr = ThisPtr;
1298 while (TypePtr.isBaseClass())
1299 TypePtr = TypePtr.getBase();
1300
1301 QualType DynamicType = TypePtr.getType();
1302 if (DynamicType->isPointerType() || DynamicType->isReferenceType())
1303 DynamicDecl = DynamicType->getPointeeCXXRecordDecl();
1304 else
1305 DynamicDecl = DynamicType->getAsCXXRecordDecl();
1306 }
1307 assert(DynamicDecl);
1308
1309 const auto *StaticDecl = cast<CXXRecordDecl>(Func->getParentDecl());
1310 const auto *InitialFunction = cast<CXXMethodDecl>(Func->getDecl());
1311 const CXXMethodDecl *Overrider = S.getContext().getOverridingFunction(
1312 DynamicDecl, StaticDecl, InitialFunction);
1313
1314 if (Overrider != InitialFunction) {
1315 // DR1872: An instantiated virtual constexpr function can't be called in a
1316 // constant expression (prior to C++20). We can still constant-fold such a
1317 // call.
1318 if (!S.getLangOpts().CPlusPlus20 && Overrider->isVirtual()) {
1319 const Expr *E = S.Current->getExpr(OpPC);
1320 S.CCEDiag(E, diag::note_constexpr_virtual_call) << E->getSourceRange();
1321 }
1322
1323 Func = S.getContext().getOrCreateFunction(Overrider);
1324
1325 const CXXRecordDecl *ThisFieldDecl =
1326 ThisPtr.getFieldDesc()->getType()->getAsCXXRecordDecl();
1327 if (Func->getParentDecl()->isDerivedFrom(ThisFieldDecl)) {
1328 // If the function we call is further DOWN the hierarchy than the
1329 // FieldDesc of our pointer, just go up the hierarchy of this field
1330 // the furthest we can go.
1331 while (ThisPtr.isBaseClass())
1332 ThisPtr = ThisPtr.getBase();
1333 }
1334 }
1335
1336 if (!Call(S, OpPC, Func, VarArgSize))
1337 return false;
1338
1339 // Covariant return types. The return type of Overrider is a pointer
1340 // or reference to a class type.
1341 if (Overrider != InitialFunction &&
1342 Overrider->getReturnType()->isPointerOrReferenceType() &&
1343 InitialFunction->getReturnType()->isPointerOrReferenceType()) {
1344 QualType OverriderPointeeType =
1345 Overrider->getReturnType()->getPointeeType();
1346 QualType InitialPointeeType =
1347 InitialFunction->getReturnType()->getPointeeType();
1348 // We've called Overrider above, but calling code expects us to return what
1349 // InitialFunction returned. According to the rules for covariant return
1350 // types, what InitialFunction returns needs to be a base class of what
1351 // Overrider returns. So, we need to do an upcast here.
1352 unsigned Offset = S.getContext().collectBaseOffset(
1353 InitialPointeeType->getAsRecordDecl(),
1354 OverriderPointeeType->getAsRecordDecl());
1355 return GetPtrBasePop(S, OpPC, Offset);
1356 }
1357
1358 return true;
1359}
1360
1362 const CallExpr *CE, uint32_t BuiltinID) {
1363 // A little arbitrary, but the current interpreter allows evaluation
1364 // of builtin functions in this mode, with some exceptions.
1365 if (BuiltinID == Builtin::BI__builtin_operator_new &&
1366 S.checkingPotentialConstantExpression())
1367 return false;
1368 auto NewFrame = std::make_unique<InterpFrame>(S, Func, OpPC);
1369
1370 InterpFrame *FrameBefore = S.Current;
1371 S.Current = NewFrame.get();
1372
1373 if (InterpretBuiltin(S, OpPC, Func, CE, BuiltinID)) {
1374 // Release ownership of NewFrame to prevent it from being deleted.
1375 NewFrame.release(); // Frame was deleted already.
1376 // Ensure that S.Current is correctly reset to the previous frame.
1377 assert(S.Current == FrameBefore);
1378 return true;
1379 }
1380
1381 // Interpreting the function failed somehow. Reset to
1382 // previous state.
1383 S.Current = FrameBefore;
1384 return false;
1385}
1386
1387bool CallPtr(InterpState &S, CodePtr OpPC, uint32_t ArgSize,
1388 const CallExpr *CE) {
1389 const FunctionPointer &FuncPtr = S.Stk.pop<FunctionPointer>();
1390
1391 const Function *F = FuncPtr.getFunction();
1392 if (!F) {
1393 const auto *E = cast<CallExpr>(S.Current->getExpr(OpPC));
1394 S.FFDiag(E, diag::note_constexpr_null_callee)
1395 << const_cast<Expr *>(E->getCallee()) << E->getSourceRange();
1396 return false;
1397 }
1398
1399 if (!FuncPtr.isValid() || !F->getDecl())
1400 return Invalid(S, OpPC);
1401
1402 assert(F);
1403
1404 // This happens when the call expression has been cast to
1405 // something else, but we don't support that.
1406 if (S.Ctx.classify(F->getDecl()->getReturnType()) !=
1407 S.Ctx.classify(CE->getType()))
1408 return false;
1409
1410 // Check argument nullability state.
1411 if (F->hasNonNullAttr()) {
1412 if (!CheckNonNullArgs(S, OpPC, F, CE, ArgSize))
1413 return false;
1414 }
1415
1416 assert(ArgSize >= F->getWrittenArgSize());
1417 uint32_t VarArgSize = ArgSize - F->getWrittenArgSize();
1418
1419 // We need to do this explicitly here since we don't have the necessary
1420 // information to do it automatically.
1421 if (F->isThisPointerExplicit())
1422 VarArgSize -= align(primSize(PT_Ptr));
1423
1424 if (F->isVirtual())
1425 return CallVirt(S, OpPC, F, VarArgSize);
1426
1427 return Call(S, OpPC, F, VarArgSize);
1428}
1429
1431 std::optional<uint64_t> ArraySize) {
1432 const Pointer &Ptr = S.Stk.peek<Pointer>();
1433
1434 if (!CheckStore(S, OpPC, Ptr))
1435 return false;
1436
1437 if (!InvalidNewDeleteExpr(S, OpPC, E))
1438 return false;
1439
1440 const auto *NewExpr = cast<CXXNewExpr>(E);
1441 QualType StorageType = Ptr.getType();
1442
1443 if (isa_and_nonnull<CXXNewExpr>(Ptr.getFieldDesc()->asExpr()) &&
1444 StorageType->isPointerType()) {
1445 // FIXME: Are there other cases where this is a problem?
1446 StorageType = StorageType->getPointeeType();
1447 }
1448
1449 const ASTContext &ASTCtx = S.getASTContext();
1450 QualType AllocType;
1451 if (ArraySize) {
1452 AllocType = ASTCtx.getConstantArrayType(
1453 NewExpr->getAllocatedType(),
1454 APInt(64, static_cast<uint64_t>(*ArraySize), false), nullptr,
1456 } else {
1457 AllocType = NewExpr->getAllocatedType();
1458 }
1459
1460 unsigned StorageSize = 1;
1461 unsigned AllocSize = 1;
1462 if (const auto *CAT = dyn_cast<ConstantArrayType>(AllocType))
1463 AllocSize = CAT->getZExtSize();
1464 if (const auto *CAT = dyn_cast<ConstantArrayType>(StorageType))
1465 StorageSize = CAT->getZExtSize();
1466
1467 if (AllocSize > StorageSize ||
1468 !ASTCtx.hasSimilarType(ASTCtx.getBaseElementType(AllocType),
1469 ASTCtx.getBaseElementType(StorageType))) {
1470 S.FFDiag(S.Current->getLocation(OpPC),
1471 diag::note_constexpr_placement_new_wrong_type)
1472 << StorageType << AllocType;
1473 return false;
1474 }
1475
1476 // Can't activate fields in a union, unless the direct base is the union.
1477 if (Ptr.inUnion() && !Ptr.isActive() && !Ptr.getBase().getRecord()->isUnion())
1478 return CheckActive(S, OpPC, Ptr, AK_Construct);
1479
1480 return true;
1481}
1482
1484 assert(E);
1485
1486 if (S.getLangOpts().CPlusPlus26)
1487 return true;
1488
1489 const auto &Loc = S.Current->getSource(OpPC);
1490
1491 if (const auto *NewExpr = dyn_cast<CXXNewExpr>(E)) {
1492 const FunctionDecl *OperatorNew = NewExpr->getOperatorNew();
1493
1494 if (!S.getLangOpts().CPlusPlus26 && NewExpr->getNumPlacementArgs() > 0) {
1495 // This is allowed pre-C++26, but only an std function.
1496 if (S.Current->isStdFunction())
1497 return true;
1498 S.FFDiag(Loc, diag::note_constexpr_new_placement)
1499 << /*C++26 feature*/ 1 << E->getSourceRange();
1500 } else if (NewExpr->getNumPlacementArgs() == 1 &&
1501 !OperatorNew->isReservedGlobalPlacementOperator()) {
1502 S.FFDiag(Loc, diag::note_constexpr_new_placement)
1503 << /*Unsupported*/ 0 << E->getSourceRange();
1504 } else if (!OperatorNew->isReplaceableGlobalAllocationFunction()) {
1505 S.FFDiag(Loc, diag::note_constexpr_new_non_replaceable)
1506 << isa<CXXMethodDecl>(OperatorNew) << OperatorNew;
1507 }
1508 } else {
1509 const auto *DeleteExpr = cast<CXXDeleteExpr>(E);
1510 const FunctionDecl *OperatorDelete = DeleteExpr->getOperatorDelete();
1511 if (!OperatorDelete->isReplaceableGlobalAllocationFunction()) {
1512 S.FFDiag(Loc, diag::note_constexpr_new_non_replaceable)
1513 << isa<CXXMethodDecl>(OperatorDelete) << OperatorDelete;
1514 }
1515 }
1516
1517 return false;
1518}
1519
1521 const FixedPoint &FP) {
1522 const Expr *E = S.Current->getExpr(OpPC);
1523 if (S.checkingForUndefinedBehavior()) {
1525 E->getExprLoc(), diag::warn_fixedpoint_constant_overflow)
1526 << FP.toDiagnosticString(S.getASTContext()) << E->getType();
1527 }
1528 S.CCEDiag(E, diag::note_constexpr_overflow)
1529 << FP.toDiagnosticString(S.getASTContext()) << E->getType();
1530 return S.noteUndefinedBehavior();
1531}
1532
1533bool InvalidShuffleVectorIndex(InterpState &S, CodePtr OpPC, uint32_t Index) {
1534 const SourceInfo &Loc = S.Current->getSource(OpPC);
1535 S.FFDiag(Loc,
1536 diag::err_shufflevector_minus_one_is_undefined_behavior_constexpr)
1537 << Index;
1538 return false;
1539}
1540
1542 const Pointer &Ptr, unsigned BitWidth) {
1543 if (Ptr.isDummy())
1544 return false;
1545
1546 const SourceInfo &E = S.Current->getSource(OpPC);
1547 S.CCEDiag(E, diag::note_constexpr_invalid_cast)
1548 << 2 << S.getLangOpts().CPlusPlus << S.Current->getRange(OpPC);
1549
1550 if (Ptr.isBlockPointer() && !Ptr.isZero()) {
1551 // Only allow based lvalue casts if they are lossless.
1553 BitWidth)
1554 return Invalid(S, OpPC);
1555 }
1556 return true;
1557}
1558
1559bool CastPointerIntegralAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
1560 const Pointer &Ptr = S.Stk.pop<Pointer>();
1561
1562 if (!CheckPointerToIntegralCast(S, OpPC, Ptr, BitWidth))
1563 return false;
1564
1565 S.Stk.push<IntegralAP<false>>(
1567 return true;
1568}
1569
1570bool CastPointerIntegralAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
1571 const Pointer &Ptr = S.Stk.pop<Pointer>();
1572
1573 if (!CheckPointerToIntegralCast(S, OpPC, Ptr, BitWidth))
1574 return false;
1575
1576 S.Stk.push<IntegralAP<true>>(
1578 return true;
1579}
1580
1581bool CheckBitCast(InterpState &S, CodePtr OpPC, bool HasIndeterminateBits,
1582 bool TargetIsUCharOrByte) {
1583 // This is always fine.
1584 if (!HasIndeterminateBits)
1585 return true;
1586
1587 // Indeterminate bits can only be bitcast to unsigned char or std::byte.
1588 if (TargetIsUCharOrByte)
1589 return true;
1590
1591 const Expr *E = S.Current->getExpr(OpPC);
1592 QualType ExprType = E->getType();
1593 S.FFDiag(E, diag::note_constexpr_bit_cast_indet_dest)
1594 << ExprType << S.getLangOpts().CharIsSigned << E->getSourceRange();
1595 return false;
1596}
1597
1598// https://github.com/llvm/llvm-project/issues/102513
1599#if defined(_MSC_VER) && !defined(__clang__) && !defined(NDEBUG)
1600#pragma optimize("", off)
1601#endif
1603 // The current stack frame when we started Interpret().
1604 // This is being used by the ops to determine wheter
1605 // to return from this function and thus terminate
1606 // interpretation.
1607 const InterpFrame *StartFrame = S.Current;
1608 assert(!S.Current->isRoot());
1609 CodePtr PC = S.Current->getPC();
1610
1611 // Empty program.
1612 if (!PC)
1613 return true;
1614
1615 for (;;) {
1616 auto Op = PC.read<Opcode>();
1617 CodePtr OpPC = PC;
1618
1619 switch (Op) {
1620#define GET_INTERP
1621#include "Opcodes.inc"
1622#undef GET_INTERP
1623 }
1624 }
1625}
1626// https://github.com/llvm/llvm-project/issues/102513
1627#if defined(_MSC_VER) && !defined(__clang__) && !defined(NDEBUG)
1628#pragma optimize("", on)
1629#endif
1630
1631} // namespace interp
1632} // namespace clang
Defines the clang::ASTContext interface.
const Decl * D
Expr * E
Defines the clang::Expr interface and subclasses for C++ expressions.
static const FunctionDecl * getVirtualOperatorDelete(QualType T)
static bool Jmp(InterpState &S, CodePtr &PC, int32_t Offset)
Definition: Interp.cpp:38
static bool CheckActive(InterpState &S, CodePtr OpPC, const Pointer &Ptr, AccessKinds AK)
Definition: Interp.cpp:129
static bool CheckGlobal(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Definition: Interp.cpp:209
static bool CheckTemporary(InterpState &S, CodePtr OpPC, const Pointer &Ptr, AccessKinds AK)
Definition: Interp.cpp:184
static bool Jt(InterpState &S, CodePtr &PC, int32_t Offset)
Definition: Interp.cpp:43
static void diagnoseNonConstVariable(InterpState &S, CodePtr OpPC, const ValueDecl *VD)
Definition: Interp.cpp:95
static bool diagnoseUnknownDecl(InterpState &S, CodePtr OpPC, const ValueDecl *D)
Definition: Interp.cpp:66
static bool Jf(InterpState &S, CodePtr &PC, int32_t Offset)
Definition: Interp.cpp:50
static void diagnoseMissingInitializer(InterpState &S, CodePtr OpPC, const ValueDecl *VD)
Definition: Interp.cpp:57
static bool RetValue(InterpState &S, CodePtr &Pt)
Definition: Interp.cpp:30
#define TYPE_SWITCH(Expr, B)
Definition: PrimType.h:153
SourceLocation Loc
Definition: SemaObjC.cpp:759
#define bool
Definition: amdgpuintrin.h:20
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:188
Builtin::Context & BuiltinInfo
Definition: ASTContext.h:682
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:799
bool hasCustomTypechecking(unsigned ID) const
Determines whether this builtin has custom typechecking.
Definition: Builtins.h:194
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2817
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:2078
bool isVirtual() const
Definition: DeclCXX.h:2133
Represents a C++ struct/union/class.
Definition: DeclCXX.h:258
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2874
unsigned getBuiltinCallee() const
getBuiltinCallee - If this is a call to a builtin, return the builtin ID of the callee.
Definition: Expr.cpp:1577
unsigned getNumArgs() const
getNumArgs - Return the number of actual arguments to this call.
Definition: Expr.h:3055
Expr ** getArgs()
Retrieve the call arguments.
Definition: Expr.h:3058
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:1265
ValueDecl * getDecl()
Definition: Expr.h:1333
bool isInvalidDecl() const
Definition: DeclBase.h:591
SourceLocation getLocation() const
Definition: DeclBase.h:442
bool hasAttr() const
Definition: DeclBase.h:580
virtual SourceRange getSourceRange() const LLVM_READONLY
Source range that this declaration covers.
Definition: DeclBase.h:430
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1493
Represents an enum.
Definition: Decl.h:3847
unsigned getNumNegativeBits() const
Returns the width in bits required to store all the negative enumerators of this enum.
Definition: Decl.h:4044
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:4996
This represents one expression.
Definition: Expr.h:110
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition: Expr.cpp:277
QualType getType() const
Definition: Expr.h:142
LangOptions::FPExceptionModeKind getExceptionMode() const
Definition: LangOptions.h:924
RoundingMode getRoundingMode() const
Definition: LangOptions.h:912
Represents a member of a struct/union/class.
Definition: Decl.h:3033
Represents a function declaration or definition.
Definition: Decl.h:1935
QualType getReturnType() const
Definition: Decl.h:2720
bool isTrivial() const
Whether this function is "trivial" in some specialized C++ senses.
Definition: Decl.h:2305
StorageClass getStorageClass() const
Returns the storage class as written in the source.
Definition: Decl.h:2763
bool isConstexpr() const
Whether this is a (C++11) constexpr function or constexpr constructor.
Definition: Decl.h:2398
bool isPureVirtual() const
Whether this virtual function is pure, i.e.
Definition: Decl.h:2288
FunctionDecl * getDefinition()
Get the definition for this declaration.
Definition: Decl.h:2217
bool isReplaceableGlobalAllocationFunction(std::optional< unsigned > *AlignmentParam=nullptr, bool *IsNothrow=nullptr) const
Determines whether this function is one of the replaceable global allocation functions: void *operato...
Definition: Decl.cpp:3372
bool hasBody(const FunctionDecl *&Definition) const
Returns true if the function has a body.
Definition: Decl.cpp:3163
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:3210
@ FPE_Ignore
Assume that floating-point exceptions are masked.
Definition: LangOptions.h:289
A (possibly-)qualified type.
Definition: Type.h:929
bool isVolatileQualified() const
Determine whether this type is volatile-qualified.
Definition: Type.h:8015
bool isConstant(const ASTContext &Ctx) const
Definition: Type.h:1089
ASTContext & getASTContext() const
Definition: Sema.h:531
const LangOptions & getLangOpts() const
Definition: Sema.h:524
Encodes a location in the source.
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition: Stmt.cpp:324
uint64_t getPointerWidth(LangAS AddrSpace) const
Return the width of pointers on this target, for the specified address space.
Definition: TargetInfo.h:471
The base class of the type hierarchy.
Definition: Type.h:1828
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition: Type.cpp:1916
bool isPointerType() const
Definition: Type.h:8186
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition: Type.cpp:738
bool isIntegralOrEnumerationType() const
Determine whether this type is an integral or enumeration type.
Definition: Type.h:8625
bool isPointerOrReferenceType() const
Definition: Type.h:8190
RecordDecl * getAsRecordDecl() const
Retrieves the RecordDecl this type refers to.
Definition: Type.cpp:1920
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition: Decl.h:671
QualType getType() const
Definition: Decl.h:682
bool hasGlobalStorage() const
Returns true for all variables that do not have local storage.
Definition: Decl.h:1177
A memory block, either on the stack or in the heap.
Definition: InterpBlock.h:49
const Descriptor * getDescriptor() const
Returns the block's descriptor.
Definition: InterpBlock.h:68
unsigned getEvalID() const
The Evaluation ID this block was created in.
Definition: InterpBlock.h:87
Pointer into the code segment.
Definition: Source.h:30
std::enable_if_t<!std::is_pointer< T >::value, T > read()
Reads data and advances the pointer.
Definition: Source.h:60
Manages dynamic memory allocations done during bytecode interpretation.
Wrapper around fixed point types.
Definition: FixedPoint.h:23
std::string toDiagnosticString(const ASTContext &Ctx) const
Definition: FixedPoint.h:81
Base class for stack frames, shared between VM and walker.
Definition: Frame.h:25
const Function * getFunction() const
Bytecode function.
Definition: Function.h:81
const FunctionDecl * getDecl() const
Returns the original FunctionDecl.
Definition: Function.h:96
bool hasBody() const
Checks if the function already has a body attached.
Definition: Function.h:189
bool isVirtual() const
Checks if the function is virtual.
Definition: Function.cpp:48
bool isConstexpr() const
Checks if the function is valid to call in constexpr.
Definition: Function.h:141
bool isLambdaStaticInvoker() const
Returns whether this function is a lambda static invoker, which we generate custom byte code for.
Definition: Function.h:167
static IntegralAP from(T Value, unsigned NumBits=0)
Definition: IntegralAP.h:96
Frame storing local variables.
Definition: InterpFrame.h:26
Interpreter context.
Definition: InterpState.h:36
A pointer to a memory block, live or dead.
Definition: Pointer.h:83
Pointer narrow() const
Restricts the scope of an array element pointer.
Definition: Pointer.h:185
bool isInitialized() const
Checks if an object was initialized.
Definition: Pointer.cpp:320
bool isStatic() const
Checks if the storage is static.
Definition: Pointer.h:491
bool isDynamic() const
Checks if the storage has been dynamically allocated.
Definition: Pointer.h:506
bool inUnion() const
Definition: Pointer.h:404
Pointer atIndex(uint64_t Idx) const
Offsets a pointer inside an array.
Definition: Pointer.h:151
bool isDummy() const
Checks if the pointer points to a dummy value.
Definition: Pointer.h:544
bool isExtern() const
Checks if the storage is extern.
Definition: Pointer.h:485
bool isActive() const
Checks if the object is active.
Definition: Pointer.h:533
bool isConst() const
Checks if an object or a subfield is mutable.
Definition: Pointer.h:555
bool isWeak() const
Definition: Pointer.h:523
bool isMutable() const
Checks if the field is mutable.
Definition: Pointer.h:517
bool isUnknownSizeArray() const
Checks if the structure is an array of unknown size.
Definition: Pointer.h:417
QualType getType() const
Returns the type of the innermost field.
Definition: Pointer.h:339
bool isArrayElement() const
Checks if the pointer points to an array.
Definition: Pointer.h:423
bool isLive() const
Checks if the pointer is live.
Definition: Pointer.h:270
bool isStaticTemporary() const
Checks if the storage is a static temporary.
Definition: Pointer.h:514
Pointer getBase() const
Returns a pointer to the object of which this pointer is a field.
Definition: Pointer.h:309
uint64_t getByteOffset() const
Returns the byte offset from the start.
Definition: Pointer.h:571
std::string toDiagnosticString(const ASTContext &Ctx) const
Converts the pointer to a string usable in diagnostics.
Definition: Pointer.cpp:310
bool isZero() const
Checks if the pointer is null.
Definition: Pointer.h:261
bool isRoot() const
Pointer points directly to a block.
Definition: Pointer.h:439
const Descriptor * getDeclDesc() const
Accessor for information about the declaration site.
Definition: Pointer.h:284
static bool pointToSameBlock(const Pointer &A, const Pointer &B)
Checks if both given pointers point to the same block.
Definition: Pointer.cpp:463
bool isOnePastEnd() const
Checks if the index is one past end.
Definition: Pointer.h:607
uint64_t getIntegerRepresentation() const
Definition: Pointer.h:138
const FieldDecl * getField() const
Returns the field information.
Definition: Pointer.h:479
bool isElementPastEnd() const
Checks if the pointer is an out-of-bounds element pointer.
Definition: Pointer.h:630
bool isBlockPointer() const
Definition: Pointer.h:467
bool isTemporary() const
Checks if the storage is temporary.
Definition: Pointer.h:498
SourceLocation getDeclLoc() const
Definition: Pointer.h:294
const Block * block() const
Definition: Pointer.h:586
Pointer getDeclPtr() const
Definition: Pointer.h:353
const Descriptor * getFieldDesc() const
Accessors for information about the innermost field.
Definition: Pointer.h:329
std::optional< unsigned > getDeclID() const
Returns the declaration ID.
Definition: Pointer.h:562
bool isBaseClass() const
Checks if a structure is a base class.
Definition: Pointer.h:539
bool isField() const
Checks if the item is a field in an object.
Definition: Pointer.h:276
const Record * getRecord() const
Returns the record descriptor of a class.
Definition: Pointer.h:472
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
llvm::iterator_range< const_field_iter > fields() const
Definition: Record.h:80
Describes the statement/declaration an opcode was generated from.
Definition: Source.h:77
Defines the clang::TargetInfo interface.
bool GetPtrBasePop(InterpState &S, CodePtr OpPC, uint32_t Off)
Definition: Interp.h:1653
bool CastPointerIntegralAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth)
Definition: Interp.cpp:1570
static bool CheckVolatile(InterpState &S, CodePtr OpPC, const Pointer &Ptr, AccessKinds AK)
Definition: Interp.cpp:512
bool CastPointerIntegralAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth)
Definition: Interp.cpp:1559
bool CheckInit(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Checks if a value can be initialized.
Definition: Interp.cpp:671
llvm::APInt APInt
Definition: FixedPoint.h:19
static bool runRecordDestructor(InterpState &S, CodePtr OpPC, const Pointer &BasePtr, const Descriptor *Desc)
Definition: Interp.cpp:949
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:446
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:844
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:896
bool CheckCallDepth(InterpState &S, CodePtr OpPC)
Checks if calling the currently active function would exceed the allowed call depth.
Definition: Interp.cpp:750
bool CheckThis(InterpState &S, CodePtr OpPC, const Pointer &This)
Checks the 'this' pointer.
Definition: Interp.cpp:761
bool CheckConstant(InterpState &S, CodePtr OpPC, const Descriptor *Desc)
Checks if the Descriptor is of a constexpr or const global variable.
Definition: Interp.cpp:340
bool CheckPointerToIntegralCast(InterpState &S, CodePtr OpPC, const Pointer &Ptr, unsigned BitWidth)
Definition: Interp.cpp:1541
static bool RunDestructors(InterpState &S, CodePtr OpPC, const Block *B)
Definition: Interp.cpp:976
bool CheckMutable(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Checks if a pointer points to a mutable field.
Definition: Interp.cpp:494
bool CheckSubobject(InterpState &S, CodePtr OpPC, const Pointer &Ptr, CheckSubobjectKind CSK)
Checks if Ptr is a one-past-the-end pointer.
Definition: Interp.cpp:435
bool handleFixedPointOverflow(InterpState &S, CodePtr OpPC, const FixedPoint &FP)
Definition: Interp.cpp:1520
static bool hasVirtualDestructor(QualType T)
Definition: Interp.cpp:1001
bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr, AccessKinds AK)
Checks if a value can be loaded from a block.
Definition: Interp.cpp:589
bool CheckInitialized(InterpState &S, CodePtr OpPC, const Pointer &Ptr, AccessKinds AK)
Definition: Interp.cpp:533
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition: PrimType.h:131
bool CheckRange(InterpState &S, CodePtr OpPC, const Pointer &Ptr, AccessKinds AK)
Checks if a pointer is in range.
Definition: Interp.cpp:415
bool CheckPure(InterpState &S, CodePtr OpPC, const CXXMethodDecl *MD)
Checks if a method is pure virtual.
Definition: Interp.cpp:779
bool This(InterpState &S, CodePtr OpPC)
Definition: Interp.h:2445
bool CheckDynamicMemoryAllocation(InterpState &S, CodePtr OpPC)
Checks if dynamic memory allocation is available in the current language mode.
Definition: Interp.cpp:835
bool CheckLive(InterpState &S, CodePtr OpPC, const Pointer &Ptr, AccessKinds AK)
Checks if a pointer is live and accessible.
Definition: Interp.cpp:306
bool CheckFinalLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
This is not used by any of the opcodes directly.
Definition: Interp.cpp:618
bool CheckBitCast(InterpState &S, CodePtr OpPC, bool HasIndeterminateBits, bool TargetIsUCharOrByte)
Definition: Interp.cpp:1581
static void popArg(InterpState &S, const Expr *Arg)
Definition: Interp.cpp:225
static bool checkConstructor(InterpState &S, CodePtr OpPC, const Function *Func, const Pointer &ThisPtr)
Definition: Interp.cpp:1157
void diagnoseEnumValue(InterpState &S, CodePtr OpPC, const EnumDecl *ED, const APSInt &Value)
Definition: Interp.cpp:1101
PrimType
Enumeration of the primitive types of the VM.
Definition: PrimType.h:34
bool CheckStore(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Checks if a value can be stored in a block.
Definition: Interp.cpp:643
bool CheckNull(InterpState &S, CodePtr OpPC, const Pointer &Ptr, CheckSubobjectKind CSK)
Checks if a pointer is null.
Definition: Interp.cpp:404
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:872
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:788
bool CallVar(InterpState &S, CodePtr OpPC, const Function *Func, uint32_t VarArgSize)
Definition: Interp.cpp:1176
bool InvalidShuffleVectorIndex(InterpState &S, CodePtr OpPC, uint32_t Index)
Definition: Interp.cpp:1533
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:1430
bool CheckLiteralType(InterpState &S, CodePtr OpPC, const Type *T)
Definition: Interp.cpp:1126
bool CheckArray(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Checks if the array is offsetable.
Definition: Interp.cpp:298
bool CheckGlobalInitialized(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Check if a global variable is initialized.
Definition: Interp.cpp:559
bool CheckNonNullArgs(InterpState &S, CodePtr OpPC, const Function *F, const CallExpr *CE, unsigned ArgSize)
Checks if all the arguments annotated as 'nonnull' are in fact not null.
Definition: Interp.cpp:922
bool CheckDummy(InterpState &S, CodePtr OpPC, const Pointer &Ptr, AccessKinds AK)
Checks if a pointer is a dummy pointer.
Definition: Interp.cpp:901
static bool CheckWeak(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Definition: Interp.cpp:576
void cleanupAfterFunctionCall(InterpState &S, CodePtr OpPC, const Function *Func)
Definition: Interp.cpp:230
llvm::BitVector collectNonNullArgs(const FunctionDecl *F, const llvm::ArrayRef< const Expr * > &Args)
size_t primSize(PrimType Type)
Returns the size of a primitive type in bytes.
Definition: PrimType.cpp:23
bool CallBI(InterpState &S, CodePtr OpPC, const Function *Func, const CallExpr *CE, uint32_t BuiltinID)
Definition: Interp.cpp:1361
bool Free(InterpState &S, CodePtr OpPC, bool DeleteIsArrayForm, bool IsGlobalDelete)
Definition: Interp.cpp:1008
bool InvalidNewDeleteExpr(InterpState &S, CodePtr OpPC, const Expr *E)
Definition: Interp.cpp:1483
bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F, const CallExpr *Call, uint32_t BuiltinID)
Interpret a builtin function.
llvm::APSInt APSInt
Definition: FixedPoint.h:20
bool CheckExtern(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Checks if the variable has externally defined storage.
Definition: Interp.cpp:283
bool CheckCallable(InterpState &S, CodePtr OpPC, const Function *F)
Checks if a method can be called.
Definition: Interp.cpp:679
bool CallPtr(InterpState &S, CodePtr OpPC, uint32_t ArgSize, const CallExpr *CE)
Definition: Interp.cpp:1387
bool CallVirt(InterpState &S, CodePtr OpPC, const Function *Func, uint32_t VarArgSize)
Definition: Interp.cpp:1287
bool CheckConst(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Checks if a pointer points to const storage.
Definition: Interp.cpp:466
bool Interpret(InterpState &S)
Interpreter entry point.
Definition: Interp.cpp:1602
bool CheckInvoke(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Checks if a method can be invoked on an object.
Definition: Interp.cpp:659
The JSON file list parser is used to communicate input to InstallAPI.
@ SC_Extern
Definition: Specifiers.h:251
CheckSubobjectKind
The order of this enum is important for diagnostics.
Definition: State.h:41
@ CSK_Field
Definition: State.h:44
@ Result
The result type of a method or function.
AccessKinds
Kinds of access we can perform on an object, for diagnostics.
Definition: State.h:26
@ AK_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_Decrement
Definition: State.h:31
const FunctionProtoType * T
Describes a memory block created by an allocation site.
Definition: Descriptor.h:116
unsigned getNumElems() const
Returns the number of elements stored in the block.
Definition: Descriptor.h:243
bool isPrimitive() const
Checks if the descriptor is of a primitive.
Definition: Descriptor.h:257
bool isCompositeArray() const
Checks if the descriptor is of an array of composites.
Definition: Descriptor.h:250
const ValueDecl * asValueDecl() const
Definition: Descriptor.h:208
const Descriptor *const ElemDesc
Descriptor of the array element.
Definition: Descriptor.h:148
unsigned getMetadataSize() const
Returns the size of the metadata.
Definition: Descriptor.h:240
bool isPrimitiveArray() const
Checks if the descriptor is of an array of primitives.
Definition: Descriptor.h:248
const VarDecl * asVarDecl() const
Definition: Descriptor.h:212
bool isRecord() const
Checks if the descriptor is of a record.
Definition: Descriptor.h:262
const Record *const ElemRecord
Pointer to the record, if block contains records.
Definition: Descriptor.h:146
const Expr * asExpr() const
Definition: Descriptor.h:205