xref: /llvm-project/clang/lib/AST/ByteCode/Interp.cpp (revision 78cf9b830c78daa5aed51b5ca77bb7db9974d1ec)
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"
18 #include "clang/AST/ASTContext.h"
19 #include "clang/AST/ASTDiagnostic.h"
20 #include "clang/AST/CXXInheritance.h"
21 #include "clang/AST/DeclObjC.h"
22 #include "clang/AST/Expr.h"
23 #include "clang/AST/ExprCXX.h"
24 #include "llvm/ADT/APSInt.h"
25 #include "llvm/ADT/StringExtras.h"
26 #include <limits>
27 #include <vector>
28 
29 using namespace clang;
30 using namespace clang::interp;
31 
32 static bool RetValue(InterpState &S, CodePtr &Pt, APValue &Result) {
33   llvm::report_fatal_error("Interpreter cannot return values");
34 }
35 
36 //===----------------------------------------------------------------------===//
37 // Jmp, Jt, Jf
38 //===----------------------------------------------------------------------===//
39 
40 static bool Jmp(InterpState &S, CodePtr &PC, int32_t Offset) {
41   PC += Offset;
42   return true;
43 }
44 
45 static bool Jt(InterpState &S, CodePtr &PC, int32_t Offset) {
46   if (S.Stk.pop<bool>()) {
47     PC += Offset;
48   }
49   return true;
50 }
51 
52 static bool Jf(InterpState &S, CodePtr &PC, int32_t Offset) {
53   if (!S.Stk.pop<bool>()) {
54     PC += Offset;
55   }
56   return true;
57 }
58 
59 static void diagnoseMissingInitializer(InterpState &S, CodePtr OpPC,
60                                        const ValueDecl *VD) {
61   const SourceInfo &E = S.Current->getSource(OpPC);
62   S.FFDiag(E, diag::note_constexpr_var_init_unknown, 1) << VD;
63   S.Note(VD->getLocation(), diag::note_declared_at) << VD->getSourceRange();
64 }
65 
66 static void diagnoseNonConstVariable(InterpState &S, CodePtr OpPC,
67                                      const ValueDecl *VD);
68 static bool diagnoseUnknownDecl(InterpState &S, CodePtr OpPC,
69                                 const ValueDecl *D) {
70   const SourceInfo &E = S.Current->getSource(OpPC);
71 
72   if (isa<ParmVarDecl>(D)) {
73     if (S.getLangOpts().CPlusPlus11) {
74       S.FFDiag(E, diag::note_constexpr_function_param_value_unknown) << D;
75       S.Note(D->getLocation(), diag::note_declared_at) << D->getSourceRange();
76     } else {
77       S.FFDiag(E);
78     }
79     return false;
80   }
81 
82   if (!D->getType().isConstQualified())
83     diagnoseNonConstVariable(S, OpPC, D);
84   else if (const auto *VD = dyn_cast<VarDecl>(D);
85            VD && !VD->getAnyInitializer())
86     diagnoseMissingInitializer(S, OpPC, VD);
87 
88   return false;
89 }
90 
91 static void diagnoseNonConstVariable(InterpState &S, CodePtr OpPC,
92                                      const ValueDecl *VD) {
93   if (!S.getLangOpts().CPlusPlus)
94     return;
95 
96   const SourceInfo &Loc = S.Current->getSource(OpPC);
97   if (const auto *VarD = dyn_cast<VarDecl>(VD);
98       VarD && VarD->getType().isConstQualified() &&
99       !VarD->getAnyInitializer()) {
100     diagnoseMissingInitializer(S, OpPC, VD);
101     return;
102   }
103 
104   // Rather random, but this is to match the diagnostic output of the current
105   // interpreter.
106   if (isa<ObjCIvarDecl>(VD))
107     return;
108 
109   if (VD->getType()->isIntegralOrEnumerationType()) {
110     S.FFDiag(Loc, diag::note_constexpr_ltor_non_const_int, 1) << VD;
111     S.Note(VD->getLocation(), diag::note_declared_at);
112     return;
113   }
114 
115   S.FFDiag(Loc,
116            S.getLangOpts().CPlusPlus11 ? diag::note_constexpr_ltor_non_constexpr
117                                        : diag::note_constexpr_ltor_non_integral,
118            1)
119       << VD << VD->getType();
120   S.Note(VD->getLocation(), diag::note_declared_at);
121 }
122 
123 static bool CheckActive(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
124                         AccessKinds AK) {
125   if (Ptr.isActive())
126     return true;
127 
128   assert(Ptr.inUnion());
129   assert(Ptr.isField() && Ptr.getField());
130 
131   Pointer U = Ptr.getBase();
132   Pointer C = Ptr;
133   while (!U.isRoot() && U.inUnion() && !U.isActive()) {
134     if (U.getField())
135       C = U;
136     U = U.getBase();
137   }
138   assert(C.isField());
139 
140   // Get the inactive field descriptor.
141   const FieldDecl *InactiveField = C.getField();
142   assert(InactiveField);
143 
144   // Consider:
145   // union U {
146   //   struct {
147   //     int x;
148   //     int y;
149   //   } a;
150   // }
151   //
152   // When activating x, we will also activate a. If we now try to read
153   // from y, we will get to CheckActive, because y is not active. In that
154   // case, our U will be a (not a union). We return here and let later code
155   // handle this.
156   if (!U.getFieldDesc()->isUnion())
157     return true;
158 
159   // Find the active field of the union.
160   const Record *R = U.getRecord();
161   assert(R && R->isUnion() && "Not a union");
162 
163   const FieldDecl *ActiveField = nullptr;
164   for (const Record::Field &F : R->fields()) {
165     const Pointer &Field = U.atField(F.Offset);
166     if (Field.isActive()) {
167       ActiveField = Field.getField();
168       break;
169     }
170   }
171 
172   const SourceInfo &Loc = S.Current->getSource(OpPC);
173   S.FFDiag(Loc, diag::note_constexpr_access_inactive_union_member)
174       << AK << InactiveField << !ActiveField << ActiveField;
175   return false;
176 }
177 
178 static bool CheckTemporary(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
179                            AccessKinds AK) {
180   if (auto ID = Ptr.getDeclID()) {
181     if (!Ptr.isStaticTemporary())
182       return true;
183 
184     const auto *MTE = dyn_cast_if_present<MaterializeTemporaryExpr>(
185         Ptr.getDeclDesc()->asExpr());
186     if (!MTE)
187       return true;
188 
189     // FIXME(perf): Since we do this check on every Load from a static
190     // temporary, it might make sense to cache the value of the
191     // isUsableInConstantExpressions call.
192     if (!MTE->isUsableInConstantExpressions(S.getASTContext()) &&
193         Ptr.block()->getEvalID() != S.Ctx.getEvalID()) {
194       const SourceInfo &E = S.Current->getSource(OpPC);
195       S.FFDiag(E, diag::note_constexpr_access_static_temporary, 1) << AK;
196       S.Note(Ptr.getDeclLoc(), diag::note_constexpr_temporary_here);
197       return false;
198     }
199   }
200   return true;
201 }
202 
203 static bool CheckGlobal(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
204   if (auto ID = Ptr.getDeclID()) {
205     if (!Ptr.isStatic())
206       return true;
207 
208     if (S.P.getCurrentDecl() == ID)
209       return true;
210 
211     S.FFDiag(S.Current->getLocation(OpPC), diag::note_constexpr_modify_global);
212     return false;
213   }
214   return true;
215 }
216 
217 namespace clang {
218 namespace interp {
219 static void popArg(InterpState &S, const Expr *Arg) {
220   PrimType Ty = S.getContext().classify(Arg).value_or(PT_Ptr);
221   TYPE_SWITCH(Ty, S.Stk.discard<T>());
222 }
223 
224 void cleanupAfterFunctionCall(InterpState &S, CodePtr OpPC,
225                               const Function *Func) {
226   assert(S.Current);
227   assert(Func);
228 
229   if (Func->isUnevaluatedBuiltin())
230     return;
231 
232   // Some builtin functions require us to only look at the call site, since
233   // the classified parameter types do not match.
234   if (unsigned BID = Func->getBuiltinID();
235       BID && S.getASTContext().BuiltinInfo.hasCustomTypechecking(BID)) {
236     const auto *CE =
237         cast<CallExpr>(S.Current->Caller->getExpr(S.Current->getRetPC()));
238     for (int32_t I = CE->getNumArgs() - 1; I >= 0; --I) {
239       const Expr *A = CE->getArg(I);
240       popArg(S, A);
241     }
242     return;
243   }
244 
245   if (S.Current->Caller && Func->isVariadic()) {
246     // CallExpr we're look for is at the return PC of the current function, i.e.
247     // in the caller.
248     // This code path should be executed very rarely.
249     unsigned NumVarArgs;
250     const Expr *const *Args = nullptr;
251     unsigned NumArgs = 0;
252     const Expr *CallSite = S.Current->Caller->getExpr(S.Current->getRetPC());
253     if (const auto *CE = dyn_cast<CallExpr>(CallSite)) {
254       Args = CE->getArgs();
255       NumArgs = CE->getNumArgs();
256     } else if (const auto *CE = dyn_cast<CXXConstructExpr>(CallSite)) {
257       Args = CE->getArgs();
258       NumArgs = CE->getNumArgs();
259     } else
260       assert(false && "Can't get arguments from that expression type");
261 
262     assert(NumArgs >= Func->getNumWrittenParams());
263     NumVarArgs = NumArgs - (Func->getNumWrittenParams() +
264                             isa<CXXOperatorCallExpr>(CallSite));
265     for (unsigned I = 0; I != NumVarArgs; ++I) {
266       const Expr *A = Args[NumArgs - 1 - I];
267       popArg(S, A);
268     }
269   }
270 
271   // And in any case, remove the fixed parameters (the non-variadic ones)
272   // at the end.
273   for (PrimType Ty : Func->args_reverse())
274     TYPE_SWITCH(Ty, S.Stk.discard<T>());
275 }
276 
277 bool CheckExtern(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
278   if (!Ptr.isExtern())
279     return true;
280 
281   if (Ptr.isInitialized() ||
282       (Ptr.getDeclDesc()->asVarDecl() == S.EvaluatingDecl))
283     return true;
284 
285   if (!S.checkingPotentialConstantExpression() && S.getLangOpts().CPlusPlus) {
286     const auto *VD = Ptr.getDeclDesc()->asValueDecl();
287     diagnoseNonConstVariable(S, OpPC, VD);
288   }
289   return false;
290 }
291 
292 bool CheckArray(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
293   if (!Ptr.isUnknownSizeArray())
294     return true;
295   const SourceInfo &E = S.Current->getSource(OpPC);
296   S.FFDiag(E, diag::note_constexpr_unsized_array_indexed);
297   return false;
298 }
299 
300 bool CheckLive(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
301                AccessKinds AK) {
302   if (Ptr.isZero()) {
303     const auto &Src = S.Current->getSource(OpPC);
304 
305     if (Ptr.isField())
306       S.FFDiag(Src, diag::note_constexpr_null_subobject) << CSK_Field;
307     else
308       S.FFDiag(Src, diag::note_constexpr_access_null) << AK;
309 
310     return false;
311   }
312 
313   if (!Ptr.isLive()) {
314     const auto &Src = S.Current->getSource(OpPC);
315 
316     if (Ptr.isDynamic()) {
317       S.FFDiag(Src, diag::note_constexpr_access_deleted_object) << AK;
318     } else {
319       bool IsTemp = Ptr.isTemporary();
320       S.FFDiag(Src, diag::note_constexpr_lifetime_ended, 1) << AK << !IsTemp;
321 
322       if (IsTemp)
323         S.Note(Ptr.getDeclLoc(), diag::note_constexpr_temporary_here);
324       else
325         S.Note(Ptr.getDeclLoc(), diag::note_declared_at);
326     }
327 
328     return false;
329   }
330 
331   return true;
332 }
333 
334 bool CheckConstant(InterpState &S, CodePtr OpPC, const Descriptor *Desc) {
335   assert(Desc);
336 
337   const auto *D = Desc->asVarDecl();
338   if (!D || !D->hasGlobalStorage())
339     return true;
340 
341   if (D == S.EvaluatingDecl)
342     return true;
343 
344   if (D->isConstexpr())
345     return true;
346 
347   QualType T = D->getType();
348   bool IsConstant = T.isConstant(S.getASTContext());
349   if (T->isIntegralOrEnumerationType()) {
350     if (!IsConstant) {
351       diagnoseNonConstVariable(S, OpPC, D);
352       return false;
353     }
354     return true;
355   }
356 
357   if (IsConstant) {
358     if (S.getLangOpts().CPlusPlus) {
359       S.CCEDiag(S.Current->getLocation(OpPC),
360                 S.getLangOpts().CPlusPlus11
361                     ? diag::note_constexpr_ltor_non_constexpr
362                     : diag::note_constexpr_ltor_non_integral,
363                 1)
364           << D << T;
365       S.Note(D->getLocation(), diag::note_declared_at);
366     } else {
367       S.CCEDiag(S.Current->getLocation(OpPC));
368     }
369     return true;
370   }
371 
372   if (T->isPointerOrReferenceType()) {
373     if (!T->getPointeeType().isConstant(S.getASTContext()) ||
374         !S.getLangOpts().CPlusPlus11) {
375       diagnoseNonConstVariable(S, OpPC, D);
376       return false;
377     }
378     return true;
379   }
380 
381   diagnoseNonConstVariable(S, OpPC, D);
382   return false;
383 }
384 
385 static bool CheckConstant(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
386   if (!Ptr.isBlockPointer())
387     return true;
388   return CheckConstant(S, OpPC, Ptr.getDeclDesc());
389 }
390 
391 bool CheckNull(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
392                CheckSubobjectKind CSK) {
393   if (!Ptr.isZero())
394     return true;
395   const SourceInfo &Loc = S.Current->getSource(OpPC);
396   S.FFDiag(Loc, diag::note_constexpr_null_subobject)
397       << CSK << S.Current->getRange(OpPC);
398 
399   return false;
400 }
401 
402 bool CheckRange(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
403                 AccessKinds AK) {
404   if (!Ptr.isOnePastEnd())
405     return true;
406   const SourceInfo &Loc = S.Current->getSource(OpPC);
407   S.FFDiag(Loc, diag::note_constexpr_access_past_end)
408       << AK << S.Current->getRange(OpPC);
409   return false;
410 }
411 
412 bool CheckRange(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
413                 CheckSubobjectKind CSK) {
414   if (!Ptr.isElementPastEnd())
415     return true;
416   const SourceInfo &Loc = S.Current->getSource(OpPC);
417   S.FFDiag(Loc, diag::note_constexpr_past_end_subobject)
418       << CSK << S.Current->getRange(OpPC);
419   return false;
420 }
421 
422 bool CheckSubobject(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
423                     CheckSubobjectKind CSK) {
424   if (!Ptr.isOnePastEnd())
425     return true;
426 
427   const SourceInfo &Loc = S.Current->getSource(OpPC);
428   S.FFDiag(Loc, diag::note_constexpr_past_end_subobject)
429       << CSK << S.Current->getRange(OpPC);
430   return false;
431 }
432 
433 bool CheckDowncast(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
434                    uint32_t Offset) {
435   uint32_t MinOffset = Ptr.getDeclDesc()->getMetadataSize();
436   uint32_t PtrOffset = Ptr.getByteOffset();
437 
438   // We subtract Offset from PtrOffset. The result must be at least
439   // MinOffset.
440   if (Offset < PtrOffset && (PtrOffset - Offset) >= MinOffset)
441     return true;
442 
443   const auto *E = cast<CastExpr>(S.Current->getExpr(OpPC));
444   QualType TargetQT = E->getType()->getPointeeType();
445   QualType MostDerivedQT = Ptr.getDeclPtr().getType();
446 
447   S.CCEDiag(E, diag::note_constexpr_invalid_downcast)
448       << MostDerivedQT << TargetQT;
449 
450   return false;
451 }
452 
453 bool CheckConst(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
454   assert(Ptr.isLive() && "Pointer is not live");
455   if (!Ptr.isConst() || Ptr.isMutable())
456     return true;
457 
458   // The This pointer is writable in constructors and destructors,
459   // even if isConst() returns true.
460   // TODO(perf): We could be hitting this code path quite a lot in complex
461   // constructors. Is there a better way to do this?
462   if (S.Current->getFunction()) {
463     for (const InterpFrame *Frame = S.Current; Frame; Frame = Frame->Caller) {
464       if (const Function *Func = Frame->getFunction();
465           Func && (Func->isConstructor() || Func->isDestructor()) &&
466           Ptr.block() == Frame->getThis().block()) {
467         return true;
468       }
469     }
470   }
471 
472   if (!Ptr.isBlockPointer())
473     return false;
474 
475   const QualType Ty = Ptr.getType();
476   const SourceInfo &Loc = S.Current->getSource(OpPC);
477   S.FFDiag(Loc, diag::note_constexpr_modify_const_type) << Ty;
478   return false;
479 }
480 
481 bool CheckMutable(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
482   assert(Ptr.isLive() && "Pointer is not live");
483   if (!Ptr.isMutable())
484     return true;
485 
486   // In C++14 onwards, it is permitted to read a mutable member whose
487   // lifetime began within the evaluation.
488   if (S.getLangOpts().CPlusPlus14 &&
489       Ptr.block()->getEvalID() == S.Ctx.getEvalID())
490     return true;
491 
492   const SourceInfo &Loc = S.Current->getSource(OpPC);
493   const FieldDecl *Field = Ptr.getField();
494   S.FFDiag(Loc, diag::note_constexpr_access_mutable, 1) << AK_Read << Field;
495   S.Note(Field->getLocation(), diag::note_declared_at);
496   return false;
497 }
498 
499 bool CheckVolatile(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
500                    AccessKinds AK) {
501   assert(Ptr.isLive());
502 
503   // FIXME: This check here might be kinda expensive. Maybe it would be better
504   // to have another field in InlineDescriptor for this?
505   if (!Ptr.isBlockPointer())
506     return true;
507 
508   QualType PtrType = Ptr.getType();
509   if (!PtrType.isVolatileQualified())
510     return true;
511 
512   const SourceInfo &Loc = S.Current->getSource(OpPC);
513   if (S.getLangOpts().CPlusPlus)
514     S.FFDiag(Loc, diag::note_constexpr_access_volatile_type) << AK << PtrType;
515   else
516     S.FFDiag(Loc);
517   return false;
518 }
519 
520 bool CheckInitialized(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
521                       AccessKinds AK) {
522   assert(Ptr.isLive());
523 
524   if (Ptr.isInitialized())
525     return true;
526 
527   if (const auto *VD = Ptr.getDeclDesc()->asVarDecl();
528       VD && VD->hasGlobalStorage()) {
529     const SourceInfo &Loc = S.Current->getSource(OpPC);
530     if (VD->getAnyInitializer()) {
531       S.FFDiag(Loc, diag::note_constexpr_var_init_non_constant, 1) << VD;
532       S.Note(VD->getLocation(), diag::note_declared_at);
533     } else {
534       diagnoseMissingInitializer(S, OpPC, VD);
535     }
536     return false;
537   }
538 
539   if (!S.checkingPotentialConstantExpression()) {
540     S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_access_uninit)
541         << AK << /*uninitialized=*/true << S.Current->getRange(OpPC);
542   }
543   return false;
544 }
545 
546 bool CheckGlobalInitialized(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
547   if (Ptr.isInitialized())
548     return true;
549 
550   assert(S.getLangOpts().CPlusPlus);
551   const auto *VD = cast<VarDecl>(Ptr.getDeclDesc()->asValueDecl());
552   if ((!VD->hasConstantInitialization() &&
553        VD->mightBeUsableInConstantExpressions(S.getASTContext())) ||
554       (S.getLangOpts().OpenCL && !S.getLangOpts().CPlusPlus11 &&
555        !VD->hasICEInitializer(S.getASTContext()))) {
556     const SourceInfo &Loc = S.Current->getSource(OpPC);
557     S.FFDiag(Loc, diag::note_constexpr_var_init_non_constant, 1) << VD;
558     S.Note(VD->getLocation(), diag::note_declared_at);
559   }
560   return false;
561 }
562 
563 bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
564                AccessKinds AK) {
565   if (!CheckLive(S, OpPC, Ptr, AK))
566     return false;
567   if (!CheckConstant(S, OpPC, Ptr))
568     return false;
569 
570   if (!CheckDummy(S, OpPC, Ptr, AK))
571     return false;
572   if (!CheckExtern(S, OpPC, Ptr))
573     return false;
574   if (!CheckRange(S, OpPC, Ptr, AK))
575     return false;
576   if (!CheckActive(S, OpPC, Ptr, AK))
577     return false;
578   if (!CheckInitialized(S, OpPC, Ptr, AK))
579     return false;
580   if (!CheckTemporary(S, OpPC, Ptr, AK))
581     return false;
582   if (!CheckMutable(S, OpPC, Ptr))
583     return false;
584   if (!CheckVolatile(S, OpPC, Ptr, AK))
585     return false;
586   return true;
587 }
588 
589 /// This is not used by any of the opcodes directly. It's used by
590 /// EvalEmitter to do the final lvalue-to-rvalue conversion.
591 bool CheckFinalLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
592   if (!CheckLive(S, OpPC, Ptr, AK_Read))
593     return false;
594   if (!CheckConstant(S, OpPC, Ptr))
595     return false;
596 
597   if (!CheckDummy(S, OpPC, Ptr, AK_Read))
598     return false;
599   if (!CheckExtern(S, OpPC, Ptr))
600     return false;
601   if (!CheckRange(S, OpPC, Ptr, AK_Read))
602     return false;
603   if (!CheckActive(S, OpPC, Ptr, AK_Read))
604     return false;
605   if (!CheckInitialized(S, OpPC, Ptr, AK_Read))
606     return false;
607   if (!CheckTemporary(S, OpPC, Ptr, AK_Read))
608     return false;
609   if (!CheckMutable(S, OpPC, Ptr))
610     return false;
611   return true;
612 }
613 
614 bool CheckStore(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
615   if (!CheckLive(S, OpPC, Ptr, AK_Assign))
616     return false;
617   if (!CheckDummy(S, OpPC, Ptr, AK_Assign))
618     return false;
619   if (!CheckExtern(S, OpPC, Ptr))
620     return false;
621   if (!CheckRange(S, OpPC, Ptr, AK_Assign))
622     return false;
623   if (!CheckGlobal(S, OpPC, Ptr))
624     return false;
625   if (!CheckConst(S, OpPC, Ptr))
626     return false;
627   return true;
628 }
629 
630 bool CheckInvoke(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
631   if (!CheckLive(S, OpPC, Ptr, AK_MemberCall))
632     return false;
633   if (!Ptr.isDummy()) {
634     if (!CheckExtern(S, OpPC, Ptr))
635       return false;
636     if (!CheckRange(S, OpPC, Ptr, AK_MemberCall))
637       return false;
638   }
639   return true;
640 }
641 
642 bool CheckInit(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
643   if (!CheckLive(S, OpPC, Ptr, AK_Assign))
644     return false;
645   if (!CheckRange(S, OpPC, Ptr, AK_Assign))
646     return false;
647   return true;
648 }
649 
650 bool CheckCallable(InterpState &S, CodePtr OpPC, const Function *F) {
651 
652   if (F->isVirtual() && !S.getLangOpts().CPlusPlus20) {
653     const SourceLocation &Loc = S.Current->getLocation(OpPC);
654     S.CCEDiag(Loc, diag::note_constexpr_virtual_call);
655     return false;
656   }
657 
658   if (F->isConstexpr() && F->hasBody() &&
659       (F->getDecl()->isConstexpr() || F->getDecl()->hasAttr<MSConstexprAttr>()))
660     return true;
661 
662   // Implicitly constexpr.
663   if (F->isLambdaStaticInvoker())
664     return true;
665 
666   const SourceLocation &Loc = S.Current->getLocation(OpPC);
667   if (S.getLangOpts().CPlusPlus11) {
668     const FunctionDecl *DiagDecl = F->getDecl();
669 
670     // Invalid decls have been diagnosed before.
671     if (DiagDecl->isInvalidDecl())
672       return false;
673 
674     // If this function is not constexpr because it is an inherited
675     // non-constexpr constructor, diagnose that directly.
676     const auto *CD = dyn_cast<CXXConstructorDecl>(DiagDecl);
677     if (CD && CD->isInheritingConstructor()) {
678       const auto *Inherited = CD->getInheritedConstructor().getConstructor();
679       if (!Inherited->isConstexpr())
680         DiagDecl = CD = Inherited;
681     }
682 
683     // FIXME: If DiagDecl is an implicitly-declared special member function
684     // or an inheriting constructor, we should be much more explicit about why
685     // it's not constexpr.
686     if (CD && CD->isInheritingConstructor()) {
687       S.FFDiag(Loc, diag::note_constexpr_invalid_inhctor, 1)
688           << CD->getInheritedConstructor().getConstructor()->getParent();
689       S.Note(DiagDecl->getLocation(), diag::note_declared_at);
690     } else {
691       // Don't emit anything if the function isn't defined and we're checking
692       // for a constant expression. It might be defined at the point we're
693       // actually calling it.
694       bool IsExtern = DiagDecl->getStorageClass() == SC_Extern;
695       if (!DiagDecl->isDefined() && !IsExtern && DiagDecl->isConstexpr() &&
696           S.checkingPotentialConstantExpression())
697         return false;
698 
699       // If the declaration is defined, declared 'constexpr' _and_ has a body,
700       // the below diagnostic doesn't add anything useful.
701       if (DiagDecl->isDefined() && DiagDecl->isConstexpr() &&
702           DiagDecl->hasBody())
703         return false;
704 
705       S.FFDiag(Loc, diag::note_constexpr_invalid_function, 1)
706           << DiagDecl->isConstexpr() << (bool)CD << DiagDecl;
707 
708       if (DiagDecl->getDefinition())
709         S.Note(DiagDecl->getDefinition()->getLocation(),
710                diag::note_declared_at);
711       else
712         S.Note(DiagDecl->getLocation(), diag::note_declared_at);
713     }
714   } else {
715     S.FFDiag(Loc, diag::note_invalid_subexpr_in_const_expr);
716   }
717 
718   return false;
719 }
720 
721 bool CheckCallDepth(InterpState &S, CodePtr OpPC) {
722   if ((S.Current->getDepth() + 1) > S.getLangOpts().ConstexprCallDepth) {
723     S.FFDiag(S.Current->getSource(OpPC),
724              diag::note_constexpr_depth_limit_exceeded)
725         << S.getLangOpts().ConstexprCallDepth;
726     return false;
727   }
728 
729   return true;
730 }
731 
732 bool CheckThis(InterpState &S, CodePtr OpPC, const Pointer &This) {
733   if (!This.isZero())
734     return true;
735 
736   const SourceInfo &Loc = S.Current->getSource(OpPC);
737 
738   bool IsImplicit = false;
739   if (const auto *E = dyn_cast_if_present<CXXThisExpr>(Loc.asExpr()))
740     IsImplicit = E->isImplicit();
741 
742   if (S.getLangOpts().CPlusPlus11)
743     S.FFDiag(Loc, diag::note_constexpr_this) << IsImplicit;
744   else
745     S.FFDiag(Loc);
746 
747   return false;
748 }
749 
750 bool CheckPure(InterpState &S, CodePtr OpPC, const CXXMethodDecl *MD) {
751   if (!MD->isPureVirtual())
752     return true;
753   const SourceInfo &E = S.Current->getSource(OpPC);
754   S.FFDiag(E, diag::note_constexpr_pure_virtual_call, 1) << MD;
755   S.Note(MD->getLocation(), diag::note_declared_at);
756   return false;
757 }
758 
759 bool CheckFloatResult(InterpState &S, CodePtr OpPC, const Floating &Result,
760                       APFloat::opStatus Status, FPOptions FPO) {
761   // [expr.pre]p4:
762   //   If during the evaluation of an expression, the result is not
763   //   mathematically defined [...], the behavior is undefined.
764   // FIXME: C++ rules require us to not conform to IEEE 754 here.
765   if (Result.isNan()) {
766     const SourceInfo &E = S.Current->getSource(OpPC);
767     S.CCEDiag(E, diag::note_constexpr_float_arithmetic)
768         << /*NaN=*/true << S.Current->getRange(OpPC);
769     return S.noteUndefinedBehavior();
770   }
771 
772   // In a constant context, assume that any dynamic rounding mode or FP
773   // exception state matches the default floating-point environment.
774   if (S.inConstantContext())
775     return true;
776 
777   if ((Status & APFloat::opInexact) &&
778       FPO.getRoundingMode() == llvm::RoundingMode::Dynamic) {
779     // Inexact result means that it depends on rounding mode. If the requested
780     // mode is dynamic, the evaluation cannot be made in compile time.
781     const SourceInfo &E = S.Current->getSource(OpPC);
782     S.FFDiag(E, diag::note_constexpr_dynamic_rounding);
783     return false;
784   }
785 
786   if ((Status != APFloat::opOK) &&
787       (FPO.getRoundingMode() == llvm::RoundingMode::Dynamic ||
788        FPO.getExceptionMode() != LangOptions::FPE_Ignore ||
789        FPO.getAllowFEnvAccess())) {
790     const SourceInfo &E = S.Current->getSource(OpPC);
791     S.FFDiag(E, diag::note_constexpr_float_arithmetic_strict);
792     return false;
793   }
794 
795   if ((Status & APFloat::opStatus::opInvalidOp) &&
796       FPO.getExceptionMode() != LangOptions::FPE_Ignore) {
797     const SourceInfo &E = S.Current->getSource(OpPC);
798     // There is no usefully definable result.
799     S.FFDiag(E);
800     return false;
801   }
802 
803   return true;
804 }
805 
806 bool CheckDynamicMemoryAllocation(InterpState &S, CodePtr OpPC) {
807   if (S.getLangOpts().CPlusPlus20)
808     return true;
809 
810   const SourceInfo &E = S.Current->getSource(OpPC);
811   S.CCEDiag(E, diag::note_constexpr_new);
812   return true;
813 }
814 
815 bool CheckNewDeleteForms(InterpState &S, CodePtr OpPC,
816                          DynamicAllocator::Form AllocForm,
817                          DynamicAllocator::Form DeleteForm, const Descriptor *D,
818                          const Expr *NewExpr) {
819   if (AllocForm == DeleteForm)
820     return true;
821 
822   QualType TypeToDiagnose;
823   // We need to shuffle things around a bit here to get a better diagnostic,
824   // because the expression we allocated the block for was of type int*,
825   // but we want to get the array size right.
826   if (D->isArray()) {
827     QualType ElemQT = D->getType()->getPointeeType();
828     TypeToDiagnose = S.getASTContext().getConstantArrayType(
829         ElemQT, APInt(64, static_cast<uint64_t>(D->getNumElems()), false),
830         nullptr, ArraySizeModifier::Normal, 0);
831   } else
832     TypeToDiagnose = D->getType()->getPointeeType();
833 
834   const SourceInfo &E = S.Current->getSource(OpPC);
835   S.FFDiag(E, diag::note_constexpr_new_delete_mismatch)
836       << static_cast<int>(DeleteForm) << static_cast<int>(AllocForm)
837       << TypeToDiagnose;
838   S.Note(NewExpr->getExprLoc(), diag::note_constexpr_dynamic_alloc_here)
839       << NewExpr->getSourceRange();
840   return false;
841 }
842 
843 bool CheckDeleteSource(InterpState &S, CodePtr OpPC, const Expr *Source,
844                        const Pointer &Ptr) {
845   // The two sources we currently allow are new expressions and
846   // __builtin_operator_new calls.
847   if (isa_and_nonnull<CXXNewExpr>(Source))
848     return true;
849   if (const CallExpr *CE = dyn_cast_if_present<CallExpr>(Source);
850       CE && CE->getBuiltinCallee() == Builtin::BI__builtin_operator_new)
851     return true;
852 
853   // Whatever this is, we didn't heap allocate it.
854   const SourceInfo &Loc = S.Current->getSource(OpPC);
855   S.FFDiag(Loc, diag::note_constexpr_delete_not_heap_alloc)
856       << Ptr.toDiagnosticString(S.getASTContext());
857 
858   if (Ptr.isTemporary())
859     S.Note(Ptr.getDeclLoc(), diag::note_constexpr_temporary_here);
860   else
861     S.Note(Ptr.getDeclLoc(), diag::note_declared_at);
862   return false;
863 }
864 
865 /// We aleady know the given DeclRefExpr is invalid for some reason,
866 /// now figure out why and print appropriate diagnostics.
867 bool CheckDeclRef(InterpState &S, CodePtr OpPC, const DeclRefExpr *DR) {
868   const ValueDecl *D = DR->getDecl();
869   return diagnoseUnknownDecl(S, OpPC, D);
870 }
871 
872 bool CheckDummy(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
873                 AccessKinds AK) {
874   if (!Ptr.isDummy())
875     return true;
876 
877   const Descriptor *Desc = Ptr.getDeclDesc();
878   const ValueDecl *D = Desc->asValueDecl();
879   if (!D)
880     return false;
881 
882   if (AK == AK_Read || AK == AK_Increment || AK == AK_Decrement)
883     return diagnoseUnknownDecl(S, OpPC, D);
884 
885   assert(AK == AK_Assign);
886   if (S.getLangOpts().CPlusPlus11) {
887     const SourceInfo &E = S.Current->getSource(OpPC);
888     S.FFDiag(E, diag::note_constexpr_modify_global);
889   }
890   return false;
891 }
892 
893 bool CheckNonNullArgs(InterpState &S, CodePtr OpPC, const Function *F,
894                       const CallExpr *CE, unsigned ArgSize) {
895   auto Args = llvm::ArrayRef(CE->getArgs(), CE->getNumArgs());
896   auto NonNullArgs = collectNonNullArgs(F->getDecl(), Args);
897   unsigned Offset = 0;
898   unsigned Index = 0;
899   for (const Expr *Arg : Args) {
900     if (NonNullArgs[Index] && Arg->getType()->isPointerType()) {
901       const Pointer &ArgPtr = S.Stk.peek<Pointer>(ArgSize - Offset);
902       if (ArgPtr.isZero()) {
903         const SourceLocation &Loc = S.Current->getLocation(OpPC);
904         S.CCEDiag(Loc, diag::note_non_null_attribute_failed);
905         return false;
906       }
907     }
908 
909     Offset += align(primSize(S.Ctx.classify(Arg).value_or(PT_Ptr)));
910     ++Index;
911   }
912   return true;
913 }
914 
915 // FIXME: This is similar to code we already have in Compiler.cpp.
916 // I think it makes sense to instead add the field and base destruction stuff
917 // to the destructor Function itself. Then destroying a record would really
918 // _just_ be calling its destructor. That would also help with the diagnostic
919 // difference when the destructor or a field/base fails.
920 static bool runRecordDestructor(InterpState &S, CodePtr OpPC,
921                                 const Pointer &BasePtr,
922                                 const Descriptor *Desc) {
923   assert(Desc->isRecord());
924   const Record *R = Desc->ElemRecord;
925   assert(R);
926 
927   if (Pointer::pointToSameBlock(BasePtr, S.Current->getThis())) {
928     const SourceInfo &Loc = S.Current->getSource(OpPC);
929     S.FFDiag(Loc, diag::note_constexpr_double_destroy);
930     return false;
931   }
932 
933   // Destructor of this record.
934   if (const CXXDestructorDecl *Dtor = R->getDestructor();
935       Dtor && !Dtor->isTrivial()) {
936     const Function *DtorFunc = S.getContext().getOrCreateFunction(Dtor);
937     if (!DtorFunc)
938       return false;
939 
940     S.Stk.push<Pointer>(BasePtr);
941     if (!Call(S, OpPC, DtorFunc, 0))
942       return false;
943   }
944   return true;
945 }
946 
947 bool RunDestructors(InterpState &S, CodePtr OpPC, const Block *B) {
948   assert(B);
949   const Descriptor *Desc = B->getDescriptor();
950 
951   if (Desc->isPrimitive() || Desc->isPrimitiveArray())
952     return true;
953 
954   assert(Desc->isRecord() || Desc->isCompositeArray());
955 
956   if (Desc->isCompositeArray()) {
957     const Descriptor *ElemDesc = Desc->ElemDesc;
958     assert(ElemDesc->isRecord());
959 
960     Pointer RP(const_cast<Block *>(B));
961     for (unsigned I = 0; I != Desc->getNumElems(); ++I) {
962       if (!runRecordDestructor(S, OpPC, RP.atIndex(I).narrow(), ElemDesc))
963         return false;
964     }
965     return true;
966   }
967 
968   assert(Desc->isRecord());
969   return runRecordDestructor(S, OpPC, Pointer(const_cast<Block *>(B)), Desc);
970 }
971 
972 void diagnoseEnumValue(InterpState &S, CodePtr OpPC, const EnumDecl *ED,
973                        const APSInt &Value) {
974   llvm::APInt Min;
975   llvm::APInt Max;
976 
977   if (S.EvaluatingDecl && !S.EvaluatingDecl->isConstexpr())
978     return;
979 
980   ED->getValueRange(Max, Min);
981   --Max;
982 
983   if (ED->getNumNegativeBits() &&
984       (Max.slt(Value.getSExtValue()) || Min.sgt(Value.getSExtValue()))) {
985     const SourceLocation &Loc = S.Current->getLocation(OpPC);
986     S.CCEDiag(Loc, diag::note_constexpr_unscoped_enum_out_of_range)
987         << llvm::toString(Value, 10) << Min.getSExtValue() << Max.getSExtValue()
988         << ED;
989   } else if (!ED->getNumNegativeBits() && Max.ult(Value.getZExtValue())) {
990     const SourceLocation &Loc = S.Current->getLocation(OpPC);
991     S.CCEDiag(Loc, diag::note_constexpr_unscoped_enum_out_of_range)
992         << llvm::toString(Value, 10) << Min.getZExtValue() << Max.getZExtValue()
993         << ED;
994   }
995 }
996 
997 bool CallVar(InterpState &S, CodePtr OpPC, const Function *Func,
998              uint32_t VarArgSize) {
999   if (Func->hasThisPointer()) {
1000     size_t ArgSize = Func->getArgSize() + VarArgSize;
1001     size_t ThisOffset = ArgSize - (Func->hasRVO() ? primSize(PT_Ptr) : 0);
1002     const Pointer &ThisPtr = S.Stk.peek<Pointer>(ThisOffset);
1003 
1004     // If the current function is a lambda static invoker and
1005     // the function we're about to call is a lambda call operator,
1006     // skip the CheckInvoke, since the ThisPtr is a null pointer
1007     // anyway.
1008     if (!(S.Current->getFunction() &&
1009           S.Current->getFunction()->isLambdaStaticInvoker() &&
1010           Func->isLambdaCallOperator())) {
1011       if (!CheckInvoke(S, OpPC, ThisPtr))
1012         return false;
1013     }
1014 
1015     if (S.checkingPotentialConstantExpression())
1016       return false;
1017   }
1018 
1019   if (!CheckCallable(S, OpPC, Func))
1020     return false;
1021 
1022   if (!CheckCallDepth(S, OpPC))
1023     return false;
1024 
1025   auto NewFrame = std::make_unique<InterpFrame>(S, Func, OpPC, VarArgSize);
1026   InterpFrame *FrameBefore = S.Current;
1027   S.Current = NewFrame.get();
1028 
1029   APValue CallResult;
1030   // Note that we cannot assert(CallResult.hasValue()) here since
1031   // Ret() above only sets the APValue if the curent frame doesn't
1032   // have a caller set.
1033   if (Interpret(S, CallResult)) {
1034     NewFrame.release(); // Frame was delete'd already.
1035     assert(S.Current == FrameBefore);
1036     return true;
1037   }
1038 
1039   // Interpreting the function failed somehow. Reset to
1040   // previous state.
1041   S.Current = FrameBefore;
1042   return false;
1043 }
1044 
1045 bool Call(InterpState &S, CodePtr OpPC, const Function *Func,
1046           uint32_t VarArgSize) {
1047   assert(Func);
1048   auto cleanup = [&]() -> bool {
1049     cleanupAfterFunctionCall(S, OpPC, Func);
1050     return false;
1051   };
1052 
1053   if (Func->hasThisPointer()) {
1054     size_t ArgSize = Func->getArgSize() + VarArgSize;
1055     size_t ThisOffset = ArgSize - (Func->hasRVO() ? primSize(PT_Ptr) : 0);
1056 
1057     const Pointer &ThisPtr = S.Stk.peek<Pointer>(ThisOffset);
1058 
1059     // If the current function is a lambda static invoker and
1060     // the function we're about to call is a lambda call operator,
1061     // skip the CheckInvoke, since the ThisPtr is a null pointer
1062     // anyway.
1063     if (S.Current->getFunction() &&
1064         S.Current->getFunction()->isLambdaStaticInvoker() &&
1065         Func->isLambdaCallOperator()) {
1066       assert(ThisPtr.isZero());
1067     } else {
1068       if (!CheckInvoke(S, OpPC, ThisPtr))
1069         return cleanup();
1070     }
1071   }
1072 
1073   if (!CheckCallable(S, OpPC, Func))
1074     return cleanup();
1075 
1076   // FIXME: The isConstructor() check here is not always right. The current
1077   // constant evaluator is somewhat inconsistent in when it allows a function
1078   // call when checking for a constant expression.
1079   if (Func->hasThisPointer() && S.checkingPotentialConstantExpression() &&
1080       !Func->isConstructor())
1081     return cleanup();
1082 
1083   if (!CheckCallDepth(S, OpPC))
1084     return cleanup();
1085 
1086   auto NewFrame = std::make_unique<InterpFrame>(S, Func, OpPC, VarArgSize);
1087   InterpFrame *FrameBefore = S.Current;
1088   S.Current = NewFrame.get();
1089 
1090   APValue CallResult;
1091   // Note that we cannot assert(CallResult.hasValue()) here since
1092   // Ret() above only sets the APValue if the curent frame doesn't
1093   // have a caller set.
1094   if (Interpret(S, CallResult)) {
1095     NewFrame.release(); // Frame was delete'd already.
1096     assert(S.Current == FrameBefore);
1097     return true;
1098   }
1099 
1100   // Interpreting the function failed somehow. Reset to
1101   // previous state.
1102   S.Current = FrameBefore;
1103   return false;
1104 }
1105 
1106 bool CallVirt(InterpState &S, CodePtr OpPC, const Function *Func,
1107               uint32_t VarArgSize) {
1108   assert(Func->hasThisPointer());
1109   assert(Func->isVirtual());
1110   size_t ArgSize = Func->getArgSize() + VarArgSize;
1111   size_t ThisOffset = ArgSize - (Func->hasRVO() ? primSize(PT_Ptr) : 0);
1112   Pointer &ThisPtr = S.Stk.peek<Pointer>(ThisOffset);
1113 
1114   const CXXRecordDecl *DynamicDecl = nullptr;
1115   {
1116     Pointer TypePtr = ThisPtr;
1117     while (TypePtr.isBaseClass())
1118       TypePtr = TypePtr.getBase();
1119 
1120     QualType DynamicType = TypePtr.getType();
1121     if (DynamicType->isPointerType() || DynamicType->isReferenceType())
1122       DynamicDecl = DynamicType->getPointeeCXXRecordDecl();
1123     else
1124       DynamicDecl = DynamicType->getAsCXXRecordDecl();
1125   }
1126   assert(DynamicDecl);
1127 
1128   const auto *StaticDecl = cast<CXXRecordDecl>(Func->getParentDecl());
1129   const auto *InitialFunction = cast<CXXMethodDecl>(Func->getDecl());
1130   const CXXMethodDecl *Overrider = S.getContext().getOverridingFunction(
1131       DynamicDecl, StaticDecl, InitialFunction);
1132 
1133   if (Overrider != InitialFunction) {
1134     // DR1872: An instantiated virtual constexpr function can't be called in a
1135     // constant expression (prior to C++20). We can still constant-fold such a
1136     // call.
1137     if (!S.getLangOpts().CPlusPlus20 && Overrider->isVirtual()) {
1138       const Expr *E = S.Current->getExpr(OpPC);
1139       S.CCEDiag(E, diag::note_constexpr_virtual_call) << E->getSourceRange();
1140     }
1141 
1142     Func = S.getContext().getOrCreateFunction(Overrider);
1143 
1144     const CXXRecordDecl *ThisFieldDecl =
1145         ThisPtr.getFieldDesc()->getType()->getAsCXXRecordDecl();
1146     if (Func->getParentDecl()->isDerivedFrom(ThisFieldDecl)) {
1147       // If the function we call is further DOWN the hierarchy than the
1148       // FieldDesc of our pointer, just go up the hierarchy of this field
1149       // the furthest we can go.
1150       while (ThisPtr.isBaseClass())
1151         ThisPtr = ThisPtr.getBase();
1152     }
1153   }
1154 
1155   if (!Call(S, OpPC, Func, VarArgSize))
1156     return false;
1157 
1158   // Covariant return types. The return type of Overrider is a pointer
1159   // or reference to a class type.
1160   if (Overrider != InitialFunction &&
1161       Overrider->getReturnType()->isPointerOrReferenceType() &&
1162       InitialFunction->getReturnType()->isPointerOrReferenceType()) {
1163     QualType OverriderPointeeType =
1164         Overrider->getReturnType()->getPointeeType();
1165     QualType InitialPointeeType =
1166         InitialFunction->getReturnType()->getPointeeType();
1167     // We've called Overrider above, but calling code expects us to return what
1168     // InitialFunction returned. According to the rules for covariant return
1169     // types, what InitialFunction returns needs to be a base class of what
1170     // Overrider returns. So, we need to do an upcast here.
1171     unsigned Offset = S.getContext().collectBaseOffset(
1172         InitialPointeeType->getAsRecordDecl(),
1173         OverriderPointeeType->getAsRecordDecl());
1174     return GetPtrBasePop(S, OpPC, Offset);
1175   }
1176 
1177   return true;
1178 }
1179 
1180 bool CallBI(InterpState &S, CodePtr &PC, const Function *Func,
1181             const CallExpr *CE, uint32_t BuiltinID) {
1182   if (S.checkingPotentialConstantExpression())
1183     return false;
1184   auto NewFrame = std::make_unique<InterpFrame>(S, Func, PC);
1185 
1186   InterpFrame *FrameBefore = S.Current;
1187   S.Current = NewFrame.get();
1188 
1189   if (InterpretBuiltin(S, PC, Func, CE, BuiltinID)) {
1190     NewFrame.release();
1191     return true;
1192   }
1193   S.Current = FrameBefore;
1194   return false;
1195 }
1196 
1197 bool CallPtr(InterpState &S, CodePtr OpPC, uint32_t ArgSize,
1198              const CallExpr *CE) {
1199   const FunctionPointer &FuncPtr = S.Stk.pop<FunctionPointer>();
1200 
1201   const Function *F = FuncPtr.getFunction();
1202   if (!F) {
1203     const auto *E = cast<CallExpr>(S.Current->getExpr(OpPC));
1204     S.FFDiag(E, diag::note_constexpr_null_callee)
1205         << const_cast<Expr *>(E->getCallee()) << E->getSourceRange();
1206     return false;
1207   }
1208 
1209   if (!FuncPtr.isValid() || !F->getDecl())
1210     return Invalid(S, OpPC);
1211 
1212   assert(F);
1213 
1214   // This happens when the call expression has been cast to
1215   // something else, but we don't support that.
1216   if (S.Ctx.classify(F->getDecl()->getReturnType()) !=
1217       S.Ctx.classify(CE->getType()))
1218     return false;
1219 
1220   // Check argument nullability state.
1221   if (F->hasNonNullAttr()) {
1222     if (!CheckNonNullArgs(S, OpPC, F, CE, ArgSize))
1223       return false;
1224   }
1225 
1226   assert(ArgSize >= F->getWrittenArgSize());
1227   uint32_t VarArgSize = ArgSize - F->getWrittenArgSize();
1228 
1229   // We need to do this explicitly here since we don't have the necessary
1230   // information to do it automatically.
1231   if (F->isThisPointerExplicit())
1232     VarArgSize -= align(primSize(PT_Ptr));
1233 
1234   if (F->isVirtual())
1235     return CallVirt(S, OpPC, F, VarArgSize);
1236 
1237   return Call(S, OpPC, F, VarArgSize);
1238 }
1239 
1240 bool Interpret(InterpState &S, APValue &Result) {
1241   // The current stack frame when we started Interpret().
1242   // This is being used by the ops to determine wheter
1243   // to return from this function and thus terminate
1244   // interpretation.
1245   const InterpFrame *StartFrame = S.Current;
1246   assert(!S.Current->isRoot());
1247   CodePtr PC = S.Current->getPC();
1248 
1249   // Empty program.
1250   if (!PC)
1251     return true;
1252 
1253   for (;;) {
1254     auto Op = PC.read<Opcode>();
1255     CodePtr OpPC = PC;
1256 
1257     switch (Op) {
1258 #define GET_INTERP
1259 #include "Opcodes.inc"
1260 #undef GET_INTERP
1261     }
1262   }
1263 }
1264 
1265 } // namespace interp
1266 } // namespace clang
1267