xref: /llvm-project/clang/lib/AST/ByteCode/Interp.cpp (revision 83fea8b809b284594e6dd133150bb6d365775e5b)
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, bool NewWasArray,
816                          bool DeleteIsArray, const Descriptor *D,
817                          const Expr *NewExpr) {
818   if (NewWasArray == DeleteIsArray)
819     return true;
820 
821   QualType TypeToDiagnose;
822   // We need to shuffle things around a bit here to get a better diagnostic,
823   // because the expression we allocated the block for was of type int*,
824   // but we want to get the array size right.
825   if (D->isArray()) {
826     QualType ElemQT = D->getType()->getPointeeType();
827     TypeToDiagnose = S.getASTContext().getConstantArrayType(
828         ElemQT, APInt(64, static_cast<uint64_t>(D->getNumElems()), false),
829         nullptr, ArraySizeModifier::Normal, 0);
830   } else
831     TypeToDiagnose = D->getType()->getPointeeType();
832 
833   const SourceInfo &E = S.Current->getSource(OpPC);
834   S.FFDiag(E, diag::note_constexpr_new_delete_mismatch)
835       << DeleteIsArray << 0 << TypeToDiagnose;
836   S.Note(NewExpr->getExprLoc(), diag::note_constexpr_dynamic_alloc_here)
837       << NewExpr->getSourceRange();
838   return false;
839 }
840 
841 bool CheckDeleteSource(InterpState &S, CodePtr OpPC, const Expr *Source,
842                        const Pointer &Ptr) {
843   if (Source && isa<CXXNewExpr>(Source))
844     return true;
845 
846   // Whatever this is, we didn't heap allocate it.
847   const SourceInfo &Loc = S.Current->getSource(OpPC);
848   S.FFDiag(Loc, diag::note_constexpr_delete_not_heap_alloc)
849       << Ptr.toDiagnosticString(S.getASTContext());
850 
851   if (Ptr.isTemporary())
852     S.Note(Ptr.getDeclLoc(), diag::note_constexpr_temporary_here);
853   else
854     S.Note(Ptr.getDeclLoc(), diag::note_declared_at);
855   return false;
856 }
857 
858 /// We aleady know the given DeclRefExpr is invalid for some reason,
859 /// now figure out why and print appropriate diagnostics.
860 bool CheckDeclRef(InterpState &S, CodePtr OpPC, const DeclRefExpr *DR) {
861   const ValueDecl *D = DR->getDecl();
862   return diagnoseUnknownDecl(S, OpPC, D);
863 }
864 
865 bool CheckDummy(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
866                 AccessKinds AK) {
867   if (!Ptr.isDummy())
868     return true;
869 
870   const Descriptor *Desc = Ptr.getDeclDesc();
871   const ValueDecl *D = Desc->asValueDecl();
872   if (!D)
873     return false;
874 
875   if (AK == AK_Read || AK == AK_Increment || AK == AK_Decrement)
876     return diagnoseUnknownDecl(S, OpPC, D);
877 
878   assert(AK == AK_Assign);
879   if (S.getLangOpts().CPlusPlus11) {
880     const SourceInfo &E = S.Current->getSource(OpPC);
881     S.FFDiag(E, diag::note_constexpr_modify_global);
882   }
883   return false;
884 }
885 
886 bool CheckNonNullArgs(InterpState &S, CodePtr OpPC, const Function *F,
887                       const CallExpr *CE, unsigned ArgSize) {
888   auto Args = llvm::ArrayRef(CE->getArgs(), CE->getNumArgs());
889   auto NonNullArgs = collectNonNullArgs(F->getDecl(), Args);
890   unsigned Offset = 0;
891   unsigned Index = 0;
892   for (const Expr *Arg : Args) {
893     if (NonNullArgs[Index] && Arg->getType()->isPointerType()) {
894       const Pointer &ArgPtr = S.Stk.peek<Pointer>(ArgSize - Offset);
895       if (ArgPtr.isZero()) {
896         const SourceLocation &Loc = S.Current->getLocation(OpPC);
897         S.CCEDiag(Loc, diag::note_non_null_attribute_failed);
898         return false;
899       }
900     }
901 
902     Offset += align(primSize(S.Ctx.classify(Arg).value_or(PT_Ptr)));
903     ++Index;
904   }
905   return true;
906 }
907 
908 // FIXME: This is similar to code we already have in Compiler.cpp.
909 // I think it makes sense to instead add the field and base destruction stuff
910 // to the destructor Function itself. Then destroying a record would really
911 // _just_ be calling its destructor. That would also help with the diagnostic
912 // difference when the destructor or a field/base fails.
913 static bool runRecordDestructor(InterpState &S, CodePtr OpPC,
914                                 const Pointer &BasePtr,
915                                 const Descriptor *Desc) {
916   assert(Desc->isRecord());
917   const Record *R = Desc->ElemRecord;
918   assert(R);
919 
920   if (Pointer::pointToSameBlock(BasePtr, S.Current->getThis())) {
921     const SourceInfo &Loc = S.Current->getSource(OpPC);
922     S.FFDiag(Loc, diag::note_constexpr_double_destroy);
923     return false;
924   }
925 
926   // Destructor of this record.
927   if (const CXXDestructorDecl *Dtor = R->getDestructor();
928       Dtor && !Dtor->isTrivial()) {
929     const Function *DtorFunc = S.getContext().getOrCreateFunction(Dtor);
930     if (!DtorFunc)
931       return false;
932 
933     S.Stk.push<Pointer>(BasePtr);
934     if (!Call(S, OpPC, DtorFunc, 0))
935       return false;
936   }
937   return true;
938 }
939 
940 bool RunDestructors(InterpState &S, CodePtr OpPC, const Block *B) {
941   assert(B);
942   const Descriptor *Desc = B->getDescriptor();
943 
944   if (Desc->isPrimitive() || Desc->isPrimitiveArray())
945     return true;
946 
947   assert(Desc->isRecord() || Desc->isCompositeArray());
948 
949   if (Desc->isCompositeArray()) {
950     const Descriptor *ElemDesc = Desc->ElemDesc;
951     assert(ElemDesc->isRecord());
952 
953     Pointer RP(const_cast<Block *>(B));
954     for (unsigned I = 0; I != Desc->getNumElems(); ++I) {
955       if (!runRecordDestructor(S, OpPC, RP.atIndex(I).narrow(), ElemDesc))
956         return false;
957     }
958     return true;
959   }
960 
961   assert(Desc->isRecord());
962   return runRecordDestructor(S, OpPC, Pointer(const_cast<Block *>(B)), Desc);
963 }
964 
965 void diagnoseEnumValue(InterpState &S, CodePtr OpPC, const EnumDecl *ED,
966                        const APSInt &Value) {
967   llvm::APInt Min;
968   llvm::APInt Max;
969 
970   if (S.EvaluatingDecl && !S.EvaluatingDecl->isConstexpr())
971     return;
972 
973   ED->getValueRange(Max, Min);
974   --Max;
975 
976   if (ED->getNumNegativeBits() &&
977       (Max.slt(Value.getSExtValue()) || Min.sgt(Value.getSExtValue()))) {
978     const SourceLocation &Loc = S.Current->getLocation(OpPC);
979     S.CCEDiag(Loc, diag::note_constexpr_unscoped_enum_out_of_range)
980         << llvm::toString(Value, 10) << Min.getSExtValue() << Max.getSExtValue()
981         << ED;
982   } else if (!ED->getNumNegativeBits() && Max.ult(Value.getZExtValue())) {
983     const SourceLocation &Loc = S.Current->getLocation(OpPC);
984     S.CCEDiag(Loc, diag::note_constexpr_unscoped_enum_out_of_range)
985         << llvm::toString(Value, 10) << Min.getZExtValue() << Max.getZExtValue()
986         << ED;
987   }
988 }
989 
990 bool CallVar(InterpState &S, CodePtr OpPC, const Function *Func,
991              uint32_t VarArgSize) {
992   if (Func->hasThisPointer()) {
993     size_t ArgSize = Func->getArgSize() + VarArgSize;
994     size_t ThisOffset = ArgSize - (Func->hasRVO() ? primSize(PT_Ptr) : 0);
995     const Pointer &ThisPtr = S.Stk.peek<Pointer>(ThisOffset);
996 
997     // If the current function is a lambda static invoker and
998     // the function we're about to call is a lambda call operator,
999     // skip the CheckInvoke, since the ThisPtr is a null pointer
1000     // anyway.
1001     if (!(S.Current->getFunction() &&
1002           S.Current->getFunction()->isLambdaStaticInvoker() &&
1003           Func->isLambdaCallOperator())) {
1004       if (!CheckInvoke(S, OpPC, ThisPtr))
1005         return false;
1006     }
1007 
1008     if (S.checkingPotentialConstantExpression())
1009       return false;
1010   }
1011 
1012   if (!CheckCallable(S, OpPC, Func))
1013     return false;
1014 
1015   if (!CheckCallDepth(S, OpPC))
1016     return false;
1017 
1018   auto NewFrame = std::make_unique<InterpFrame>(S, Func, OpPC, VarArgSize);
1019   InterpFrame *FrameBefore = S.Current;
1020   S.Current = NewFrame.get();
1021 
1022   APValue CallResult;
1023   // Note that we cannot assert(CallResult.hasValue()) here since
1024   // Ret() above only sets the APValue if the curent frame doesn't
1025   // have a caller set.
1026   if (Interpret(S, CallResult)) {
1027     NewFrame.release(); // Frame was delete'd already.
1028     assert(S.Current == FrameBefore);
1029     return true;
1030   }
1031 
1032   // Interpreting the function failed somehow. Reset to
1033   // previous state.
1034   S.Current = FrameBefore;
1035   return false;
1036 }
1037 
1038 bool Call(InterpState &S, CodePtr OpPC, const Function *Func,
1039           uint32_t VarArgSize) {
1040   assert(Func);
1041   auto cleanup = [&]() -> bool {
1042     cleanupAfterFunctionCall(S, OpPC, Func);
1043     return false;
1044   };
1045 
1046   if (Func->hasThisPointer()) {
1047     size_t ArgSize = Func->getArgSize() + VarArgSize;
1048     size_t ThisOffset = ArgSize - (Func->hasRVO() ? primSize(PT_Ptr) : 0);
1049 
1050     const Pointer &ThisPtr = S.Stk.peek<Pointer>(ThisOffset);
1051 
1052     // If the current function is a lambda static invoker and
1053     // the function we're about to call is a lambda call operator,
1054     // skip the CheckInvoke, since the ThisPtr is a null pointer
1055     // anyway.
1056     if (S.Current->getFunction() &&
1057         S.Current->getFunction()->isLambdaStaticInvoker() &&
1058         Func->isLambdaCallOperator()) {
1059       assert(ThisPtr.isZero());
1060     } else {
1061       if (!CheckInvoke(S, OpPC, ThisPtr))
1062         return cleanup();
1063     }
1064   }
1065 
1066   if (!CheckCallable(S, OpPC, Func))
1067     return cleanup();
1068 
1069   // FIXME: The isConstructor() check here is not always right. The current
1070   // constant evaluator is somewhat inconsistent in when it allows a function
1071   // call when checking for a constant expression.
1072   if (Func->hasThisPointer() && S.checkingPotentialConstantExpression() &&
1073       !Func->isConstructor())
1074     return cleanup();
1075 
1076   if (!CheckCallDepth(S, OpPC))
1077     return cleanup();
1078 
1079   auto NewFrame = std::make_unique<InterpFrame>(S, Func, OpPC, VarArgSize);
1080   InterpFrame *FrameBefore = S.Current;
1081   S.Current = NewFrame.get();
1082 
1083   APValue CallResult;
1084   // Note that we cannot assert(CallResult.hasValue()) here since
1085   // Ret() above only sets the APValue if the curent frame doesn't
1086   // have a caller set.
1087   if (Interpret(S, CallResult)) {
1088     NewFrame.release(); // Frame was delete'd already.
1089     assert(S.Current == FrameBefore);
1090     return true;
1091   }
1092 
1093   // Interpreting the function failed somehow. Reset to
1094   // previous state.
1095   S.Current = FrameBefore;
1096   return false;
1097 }
1098 
1099 bool CallVirt(InterpState &S, CodePtr OpPC, const Function *Func,
1100               uint32_t VarArgSize) {
1101   assert(Func->hasThisPointer());
1102   assert(Func->isVirtual());
1103   size_t ArgSize = Func->getArgSize() + VarArgSize;
1104   size_t ThisOffset = ArgSize - (Func->hasRVO() ? primSize(PT_Ptr) : 0);
1105   Pointer &ThisPtr = S.Stk.peek<Pointer>(ThisOffset);
1106 
1107   const CXXRecordDecl *DynamicDecl = nullptr;
1108   {
1109     Pointer TypePtr = ThisPtr;
1110     while (TypePtr.isBaseClass())
1111       TypePtr = TypePtr.getBase();
1112 
1113     QualType DynamicType = TypePtr.getType();
1114     if (DynamicType->isPointerType() || DynamicType->isReferenceType())
1115       DynamicDecl = DynamicType->getPointeeCXXRecordDecl();
1116     else
1117       DynamicDecl = DynamicType->getAsCXXRecordDecl();
1118   }
1119   assert(DynamicDecl);
1120 
1121   const auto *StaticDecl = cast<CXXRecordDecl>(Func->getParentDecl());
1122   const auto *InitialFunction = cast<CXXMethodDecl>(Func->getDecl());
1123   const CXXMethodDecl *Overrider = S.getContext().getOverridingFunction(
1124       DynamicDecl, StaticDecl, InitialFunction);
1125 
1126   if (Overrider != InitialFunction) {
1127     // DR1872: An instantiated virtual constexpr function can't be called in a
1128     // constant expression (prior to C++20). We can still constant-fold such a
1129     // call.
1130     if (!S.getLangOpts().CPlusPlus20 && Overrider->isVirtual()) {
1131       const Expr *E = S.Current->getExpr(OpPC);
1132       S.CCEDiag(E, diag::note_constexpr_virtual_call) << E->getSourceRange();
1133     }
1134 
1135     Func = S.getContext().getOrCreateFunction(Overrider);
1136 
1137     const CXXRecordDecl *ThisFieldDecl =
1138         ThisPtr.getFieldDesc()->getType()->getAsCXXRecordDecl();
1139     if (Func->getParentDecl()->isDerivedFrom(ThisFieldDecl)) {
1140       // If the function we call is further DOWN the hierarchy than the
1141       // FieldDesc of our pointer, just go up the hierarchy of this field
1142       // the furthest we can go.
1143       while (ThisPtr.isBaseClass())
1144         ThisPtr = ThisPtr.getBase();
1145     }
1146   }
1147 
1148   if (!Call(S, OpPC, Func, VarArgSize))
1149     return false;
1150 
1151   // Covariant return types. The return type of Overrider is a pointer
1152   // or reference to a class type.
1153   if (Overrider != InitialFunction &&
1154       Overrider->getReturnType()->isPointerOrReferenceType() &&
1155       InitialFunction->getReturnType()->isPointerOrReferenceType()) {
1156     QualType OverriderPointeeType =
1157         Overrider->getReturnType()->getPointeeType();
1158     QualType InitialPointeeType =
1159         InitialFunction->getReturnType()->getPointeeType();
1160     // We've called Overrider above, but calling code expects us to return what
1161     // InitialFunction returned. According to the rules for covariant return
1162     // types, what InitialFunction returns needs to be a base class of what
1163     // Overrider returns. So, we need to do an upcast here.
1164     unsigned Offset = S.getContext().collectBaseOffset(
1165         InitialPointeeType->getAsRecordDecl(),
1166         OverriderPointeeType->getAsRecordDecl());
1167     return GetPtrBasePop(S, OpPC, Offset);
1168   }
1169 
1170   return true;
1171 }
1172 
1173 bool CallBI(InterpState &S, CodePtr &PC, const Function *Func,
1174             const CallExpr *CE) {
1175   auto NewFrame = std::make_unique<InterpFrame>(S, Func, PC);
1176 
1177   InterpFrame *FrameBefore = S.Current;
1178   S.Current = NewFrame.get();
1179 
1180   if (InterpretBuiltin(S, PC, Func, CE)) {
1181     NewFrame.release();
1182     return true;
1183   }
1184   S.Current = FrameBefore;
1185   return false;
1186 }
1187 
1188 bool CallPtr(InterpState &S, CodePtr OpPC, uint32_t ArgSize,
1189              const CallExpr *CE) {
1190   const FunctionPointer &FuncPtr = S.Stk.pop<FunctionPointer>();
1191 
1192   const Function *F = FuncPtr.getFunction();
1193   if (!F) {
1194     const auto *E = cast<CallExpr>(S.Current->getExpr(OpPC));
1195     S.FFDiag(E, diag::note_constexpr_null_callee)
1196         << const_cast<Expr *>(E->getCallee()) << E->getSourceRange();
1197     return false;
1198   }
1199 
1200   if (!FuncPtr.isValid() || !F->getDecl())
1201     return Invalid(S, OpPC);
1202 
1203   assert(F);
1204 
1205   // This happens when the call expression has been cast to
1206   // something else, but we don't support that.
1207   if (S.Ctx.classify(F->getDecl()->getReturnType()) !=
1208       S.Ctx.classify(CE->getType()))
1209     return false;
1210 
1211   // Check argument nullability state.
1212   if (F->hasNonNullAttr()) {
1213     if (!CheckNonNullArgs(S, OpPC, F, CE, ArgSize))
1214       return false;
1215   }
1216 
1217   assert(ArgSize >= F->getWrittenArgSize());
1218   uint32_t VarArgSize = ArgSize - F->getWrittenArgSize();
1219 
1220   // We need to do this explicitly here since we don't have the necessary
1221   // information to do it automatically.
1222   if (F->isThisPointerExplicit())
1223     VarArgSize -= align(primSize(PT_Ptr));
1224 
1225   if (F->isVirtual())
1226     return CallVirt(S, OpPC, F, VarArgSize);
1227 
1228   return Call(S, OpPC, F, VarArgSize);
1229 }
1230 
1231 bool Interpret(InterpState &S, APValue &Result) {
1232   // The current stack frame when we started Interpret().
1233   // This is being used by the ops to determine wheter
1234   // to return from this function and thus terminate
1235   // interpretation.
1236   const InterpFrame *StartFrame = S.Current;
1237   assert(!S.Current->isRoot());
1238   CodePtr PC = S.Current->getPC();
1239 
1240   // Empty program.
1241   if (!PC)
1242     return true;
1243 
1244   for (;;) {
1245     auto Op = PC.read<Opcode>();
1246     CodePtr OpPC = PC;
1247 
1248     switch (Op) {
1249 #define GET_INTERP
1250 #include "Opcodes.inc"
1251 #undef GET_INTERP
1252     }
1253   }
1254 }
1255 
1256 } // namespace interp
1257 } // namespace clang
1258