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 static bool CheckWeak(InterpState &S, CodePtr OpPC, const Pointer &Ptr) { 564 if (!Ptr.isWeak()) 565 return true; 566 567 const auto *VD = Ptr.getDeclDesc()->asVarDecl(); 568 assert(VD); 569 S.FFDiag(S.Current->getLocation(OpPC), diag::note_constexpr_var_init_weak) 570 << VD; 571 S.Note(VD->getLocation(), diag::note_declared_at); 572 573 return false; 574 } 575 576 bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr, 577 AccessKinds AK) { 578 if (!CheckLive(S, OpPC, Ptr, AK)) 579 return false; 580 if (!CheckConstant(S, OpPC, Ptr)) 581 return false; 582 if (!CheckDummy(S, OpPC, Ptr, AK)) 583 return false; 584 if (!CheckExtern(S, OpPC, Ptr)) 585 return false; 586 if (!CheckRange(S, OpPC, Ptr, AK)) 587 return false; 588 if (!CheckActive(S, OpPC, Ptr, AK)) 589 return false; 590 if (!CheckInitialized(S, OpPC, Ptr, AK)) 591 return false; 592 if (!CheckTemporary(S, OpPC, Ptr, AK)) 593 return false; 594 if (!CheckWeak(S, OpPC, Ptr)) 595 return false; 596 if (!CheckMutable(S, OpPC, Ptr)) 597 return false; 598 if (!CheckVolatile(S, OpPC, Ptr, AK)) 599 return false; 600 return true; 601 } 602 603 /// This is not used by any of the opcodes directly. It's used by 604 /// EvalEmitter to do the final lvalue-to-rvalue conversion. 605 bool CheckFinalLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr) { 606 if (!CheckLive(S, OpPC, Ptr, AK_Read)) 607 return false; 608 if (!CheckConstant(S, OpPC, Ptr)) 609 return false; 610 611 if (!CheckDummy(S, OpPC, Ptr, AK_Read)) 612 return false; 613 if (!CheckExtern(S, OpPC, Ptr)) 614 return false; 615 if (!CheckRange(S, OpPC, Ptr, AK_Read)) 616 return false; 617 if (!CheckActive(S, OpPC, Ptr, AK_Read)) 618 return false; 619 if (!CheckInitialized(S, OpPC, Ptr, AK_Read)) 620 return false; 621 if (!CheckTemporary(S, OpPC, Ptr, AK_Read)) 622 return false; 623 if (!CheckMutable(S, OpPC, Ptr)) 624 return false; 625 return true; 626 } 627 628 bool CheckStore(InterpState &S, CodePtr OpPC, const Pointer &Ptr) { 629 if (!CheckLive(S, OpPC, Ptr, AK_Assign)) 630 return false; 631 if (!CheckDummy(S, OpPC, Ptr, AK_Assign)) 632 return false; 633 if (!CheckExtern(S, OpPC, Ptr)) 634 return false; 635 if (!CheckRange(S, OpPC, Ptr, AK_Assign)) 636 return false; 637 if (!CheckGlobal(S, OpPC, Ptr)) 638 return false; 639 if (!CheckConst(S, OpPC, Ptr)) 640 return false; 641 return true; 642 } 643 644 bool CheckInvoke(InterpState &S, CodePtr OpPC, const Pointer &Ptr) { 645 if (!CheckLive(S, OpPC, Ptr, AK_MemberCall)) 646 return false; 647 if (!Ptr.isDummy()) { 648 if (!CheckExtern(S, OpPC, Ptr)) 649 return false; 650 if (!CheckRange(S, OpPC, Ptr, AK_MemberCall)) 651 return false; 652 } 653 return true; 654 } 655 656 bool CheckInit(InterpState &S, CodePtr OpPC, const Pointer &Ptr) { 657 if (!CheckLive(S, OpPC, Ptr, AK_Assign)) 658 return false; 659 if (!CheckRange(S, OpPC, Ptr, AK_Assign)) 660 return false; 661 return true; 662 } 663 664 bool CheckCallable(InterpState &S, CodePtr OpPC, const Function *F) { 665 666 if (F->isVirtual() && !S.getLangOpts().CPlusPlus20) { 667 const SourceLocation &Loc = S.Current->getLocation(OpPC); 668 S.CCEDiag(Loc, diag::note_constexpr_virtual_call); 669 return false; 670 } 671 672 if (F->isConstexpr() && F->hasBody() && 673 (F->getDecl()->isConstexpr() || F->getDecl()->hasAttr<MSConstexprAttr>())) 674 return true; 675 676 // Implicitly constexpr. 677 if (F->isLambdaStaticInvoker()) 678 return true; 679 680 const SourceLocation &Loc = S.Current->getLocation(OpPC); 681 if (S.getLangOpts().CPlusPlus11) { 682 const FunctionDecl *DiagDecl = F->getDecl(); 683 684 // Invalid decls have been diagnosed before. 685 if (DiagDecl->isInvalidDecl()) 686 return false; 687 688 // If this function is not constexpr because it is an inherited 689 // non-constexpr constructor, diagnose that directly. 690 const auto *CD = dyn_cast<CXXConstructorDecl>(DiagDecl); 691 if (CD && CD->isInheritingConstructor()) { 692 const auto *Inherited = CD->getInheritedConstructor().getConstructor(); 693 if (!Inherited->isConstexpr()) 694 DiagDecl = CD = Inherited; 695 } 696 697 // FIXME: If DiagDecl is an implicitly-declared special member function 698 // or an inheriting constructor, we should be much more explicit about why 699 // it's not constexpr. 700 if (CD && CD->isInheritingConstructor()) { 701 S.FFDiag(Loc, diag::note_constexpr_invalid_inhctor, 1) 702 << CD->getInheritedConstructor().getConstructor()->getParent(); 703 S.Note(DiagDecl->getLocation(), diag::note_declared_at); 704 } else { 705 // Don't emit anything if the function isn't defined and we're checking 706 // for a constant expression. It might be defined at the point we're 707 // actually calling it. 708 bool IsExtern = DiagDecl->getStorageClass() == SC_Extern; 709 if (!DiagDecl->isDefined() && !IsExtern && DiagDecl->isConstexpr() && 710 S.checkingPotentialConstantExpression()) 711 return false; 712 713 // If the declaration is defined, declared 'constexpr' _and_ has a body, 714 // the below diagnostic doesn't add anything useful. 715 if (DiagDecl->isDefined() && DiagDecl->isConstexpr() && 716 DiagDecl->hasBody()) 717 return false; 718 719 S.FFDiag(Loc, diag::note_constexpr_invalid_function, 1) 720 << DiagDecl->isConstexpr() << (bool)CD << DiagDecl; 721 722 if (DiagDecl->getDefinition()) 723 S.Note(DiagDecl->getDefinition()->getLocation(), 724 diag::note_declared_at); 725 else 726 S.Note(DiagDecl->getLocation(), diag::note_declared_at); 727 } 728 } else { 729 S.FFDiag(Loc, diag::note_invalid_subexpr_in_const_expr); 730 } 731 732 return false; 733 } 734 735 bool CheckCallDepth(InterpState &S, CodePtr OpPC) { 736 if ((S.Current->getDepth() + 1) > S.getLangOpts().ConstexprCallDepth) { 737 S.FFDiag(S.Current->getSource(OpPC), 738 diag::note_constexpr_depth_limit_exceeded) 739 << S.getLangOpts().ConstexprCallDepth; 740 return false; 741 } 742 743 return true; 744 } 745 746 bool CheckThis(InterpState &S, CodePtr OpPC, const Pointer &This) { 747 if (!This.isZero()) 748 return true; 749 750 const SourceInfo &Loc = S.Current->getSource(OpPC); 751 752 bool IsImplicit = false; 753 if (const auto *E = dyn_cast_if_present<CXXThisExpr>(Loc.asExpr())) 754 IsImplicit = E->isImplicit(); 755 756 if (S.getLangOpts().CPlusPlus11) 757 S.FFDiag(Loc, diag::note_constexpr_this) << IsImplicit; 758 else 759 S.FFDiag(Loc); 760 761 return false; 762 } 763 764 bool CheckPure(InterpState &S, CodePtr OpPC, const CXXMethodDecl *MD) { 765 if (!MD->isPureVirtual()) 766 return true; 767 const SourceInfo &E = S.Current->getSource(OpPC); 768 S.FFDiag(E, diag::note_constexpr_pure_virtual_call, 1) << MD; 769 S.Note(MD->getLocation(), diag::note_declared_at); 770 return false; 771 } 772 773 bool CheckFloatResult(InterpState &S, CodePtr OpPC, const Floating &Result, 774 APFloat::opStatus Status, FPOptions FPO) { 775 // [expr.pre]p4: 776 // If during the evaluation of an expression, the result is not 777 // mathematically defined [...], the behavior is undefined. 778 // FIXME: C++ rules require us to not conform to IEEE 754 here. 779 if (Result.isNan()) { 780 const SourceInfo &E = S.Current->getSource(OpPC); 781 S.CCEDiag(E, diag::note_constexpr_float_arithmetic) 782 << /*NaN=*/true << S.Current->getRange(OpPC); 783 return S.noteUndefinedBehavior(); 784 } 785 786 // In a constant context, assume that any dynamic rounding mode or FP 787 // exception state matches the default floating-point environment. 788 if (S.inConstantContext()) 789 return true; 790 791 if ((Status & APFloat::opInexact) && 792 FPO.getRoundingMode() == llvm::RoundingMode::Dynamic) { 793 // Inexact result means that it depends on rounding mode. If the requested 794 // mode is dynamic, the evaluation cannot be made in compile time. 795 const SourceInfo &E = S.Current->getSource(OpPC); 796 S.FFDiag(E, diag::note_constexpr_dynamic_rounding); 797 return false; 798 } 799 800 if ((Status != APFloat::opOK) && 801 (FPO.getRoundingMode() == llvm::RoundingMode::Dynamic || 802 FPO.getExceptionMode() != LangOptions::FPE_Ignore || 803 FPO.getAllowFEnvAccess())) { 804 const SourceInfo &E = S.Current->getSource(OpPC); 805 S.FFDiag(E, diag::note_constexpr_float_arithmetic_strict); 806 return false; 807 } 808 809 if ((Status & APFloat::opStatus::opInvalidOp) && 810 FPO.getExceptionMode() != LangOptions::FPE_Ignore) { 811 const SourceInfo &E = S.Current->getSource(OpPC); 812 // There is no usefully definable result. 813 S.FFDiag(E); 814 return false; 815 } 816 817 return true; 818 } 819 820 bool CheckDynamicMemoryAllocation(InterpState &S, CodePtr OpPC) { 821 if (S.getLangOpts().CPlusPlus20) 822 return true; 823 824 const SourceInfo &E = S.Current->getSource(OpPC); 825 S.CCEDiag(E, diag::note_constexpr_new); 826 return true; 827 } 828 829 bool CheckNewDeleteForms(InterpState &S, CodePtr OpPC, 830 DynamicAllocator::Form AllocForm, 831 DynamicAllocator::Form DeleteForm, const Descriptor *D, 832 const Expr *NewExpr) { 833 if (AllocForm == DeleteForm) 834 return true; 835 836 QualType TypeToDiagnose; 837 // We need to shuffle things around a bit here to get a better diagnostic, 838 // because the expression we allocated the block for was of type int*, 839 // but we want to get the array size right. 840 if (D->isArray()) { 841 QualType ElemQT = D->getType()->getPointeeType(); 842 TypeToDiagnose = S.getASTContext().getConstantArrayType( 843 ElemQT, APInt(64, static_cast<uint64_t>(D->getNumElems()), false), 844 nullptr, ArraySizeModifier::Normal, 0); 845 } else 846 TypeToDiagnose = D->getType()->getPointeeType(); 847 848 const SourceInfo &E = S.Current->getSource(OpPC); 849 S.FFDiag(E, diag::note_constexpr_new_delete_mismatch) 850 << static_cast<int>(DeleteForm) << static_cast<int>(AllocForm) 851 << TypeToDiagnose; 852 S.Note(NewExpr->getExprLoc(), diag::note_constexpr_dynamic_alloc_here) 853 << NewExpr->getSourceRange(); 854 return false; 855 } 856 857 bool CheckDeleteSource(InterpState &S, CodePtr OpPC, const Expr *Source, 858 const Pointer &Ptr) { 859 // The two sources we currently allow are new expressions and 860 // __builtin_operator_new calls. 861 if (isa_and_nonnull<CXXNewExpr>(Source)) 862 return true; 863 if (const CallExpr *CE = dyn_cast_if_present<CallExpr>(Source); 864 CE && CE->getBuiltinCallee() == Builtin::BI__builtin_operator_new) 865 return true; 866 867 // Whatever this is, we didn't heap allocate it. 868 const SourceInfo &Loc = S.Current->getSource(OpPC); 869 S.FFDiag(Loc, diag::note_constexpr_delete_not_heap_alloc) 870 << Ptr.toDiagnosticString(S.getASTContext()); 871 872 if (Ptr.isTemporary()) 873 S.Note(Ptr.getDeclLoc(), diag::note_constexpr_temporary_here); 874 else 875 S.Note(Ptr.getDeclLoc(), diag::note_declared_at); 876 return false; 877 } 878 879 /// We aleady know the given DeclRefExpr is invalid for some reason, 880 /// now figure out why and print appropriate diagnostics. 881 bool CheckDeclRef(InterpState &S, CodePtr OpPC, const DeclRefExpr *DR) { 882 const ValueDecl *D = DR->getDecl(); 883 return diagnoseUnknownDecl(S, OpPC, D); 884 } 885 886 bool CheckDummy(InterpState &S, CodePtr OpPC, const Pointer &Ptr, 887 AccessKinds AK) { 888 if (!Ptr.isDummy()) 889 return true; 890 891 const Descriptor *Desc = Ptr.getDeclDesc(); 892 const ValueDecl *D = Desc->asValueDecl(); 893 if (!D) 894 return false; 895 896 if (AK == AK_Read || AK == AK_Increment || AK == AK_Decrement) 897 return diagnoseUnknownDecl(S, OpPC, D); 898 899 assert(AK == AK_Assign); 900 if (S.getLangOpts().CPlusPlus14) { 901 const SourceInfo &E = S.Current->getSource(OpPC); 902 S.FFDiag(E, diag::note_constexpr_modify_global); 903 } 904 return false; 905 } 906 907 bool CheckNonNullArgs(InterpState &S, CodePtr OpPC, const Function *F, 908 const CallExpr *CE, unsigned ArgSize) { 909 auto Args = llvm::ArrayRef(CE->getArgs(), CE->getNumArgs()); 910 auto NonNullArgs = collectNonNullArgs(F->getDecl(), Args); 911 unsigned Offset = 0; 912 unsigned Index = 0; 913 for (const Expr *Arg : Args) { 914 if (NonNullArgs[Index] && Arg->getType()->isPointerType()) { 915 const Pointer &ArgPtr = S.Stk.peek<Pointer>(ArgSize - Offset); 916 if (ArgPtr.isZero()) { 917 const SourceLocation &Loc = S.Current->getLocation(OpPC); 918 S.CCEDiag(Loc, diag::note_non_null_attribute_failed); 919 return false; 920 } 921 } 922 923 Offset += align(primSize(S.Ctx.classify(Arg).value_or(PT_Ptr))); 924 ++Index; 925 } 926 return true; 927 } 928 929 // FIXME: This is similar to code we already have in Compiler.cpp. 930 // I think it makes sense to instead add the field and base destruction stuff 931 // to the destructor Function itself. Then destroying a record would really 932 // _just_ be calling its destructor. That would also help with the diagnostic 933 // difference when the destructor or a field/base fails. 934 static bool runRecordDestructor(InterpState &S, CodePtr OpPC, 935 const Pointer &BasePtr, 936 const Descriptor *Desc) { 937 assert(Desc->isRecord()); 938 const Record *R = Desc->ElemRecord; 939 assert(R); 940 941 if (Pointer::pointToSameBlock(BasePtr, S.Current->getThis())) { 942 const SourceInfo &Loc = S.Current->getSource(OpPC); 943 S.FFDiag(Loc, diag::note_constexpr_double_destroy); 944 return false; 945 } 946 947 // Destructor of this record. 948 if (const CXXDestructorDecl *Dtor = R->getDestructor(); 949 Dtor && !Dtor->isTrivial()) { 950 const Function *DtorFunc = S.getContext().getOrCreateFunction(Dtor); 951 if (!DtorFunc) 952 return false; 953 954 S.Stk.push<Pointer>(BasePtr); 955 if (!Call(S, OpPC, DtorFunc, 0)) 956 return false; 957 } 958 return true; 959 } 960 961 bool RunDestructors(InterpState &S, CodePtr OpPC, const Block *B) { 962 assert(B); 963 const Descriptor *Desc = B->getDescriptor(); 964 965 if (Desc->isPrimitive() || Desc->isPrimitiveArray()) 966 return true; 967 968 assert(Desc->isRecord() || Desc->isCompositeArray()); 969 970 if (Desc->isCompositeArray()) { 971 const Descriptor *ElemDesc = Desc->ElemDesc; 972 assert(ElemDesc->isRecord()); 973 974 Pointer RP(const_cast<Block *>(B)); 975 for (unsigned I = 0; I != Desc->getNumElems(); ++I) { 976 if (!runRecordDestructor(S, OpPC, RP.atIndex(I).narrow(), ElemDesc)) 977 return false; 978 } 979 return true; 980 } 981 982 assert(Desc->isRecord()); 983 return runRecordDestructor(S, OpPC, Pointer(const_cast<Block *>(B)), Desc); 984 } 985 986 void diagnoseEnumValue(InterpState &S, CodePtr OpPC, const EnumDecl *ED, 987 const APSInt &Value) { 988 llvm::APInt Min; 989 llvm::APInt Max; 990 991 if (S.EvaluatingDecl && !S.EvaluatingDecl->isConstexpr()) 992 return; 993 994 ED->getValueRange(Max, Min); 995 --Max; 996 997 if (ED->getNumNegativeBits() && 998 (Max.slt(Value.getSExtValue()) || Min.sgt(Value.getSExtValue()))) { 999 const SourceLocation &Loc = S.Current->getLocation(OpPC); 1000 S.CCEDiag(Loc, diag::note_constexpr_unscoped_enum_out_of_range) 1001 << llvm::toString(Value, 10) << Min.getSExtValue() << Max.getSExtValue() 1002 << ED; 1003 } else if (!ED->getNumNegativeBits() && Max.ult(Value.getZExtValue())) { 1004 const SourceLocation &Loc = S.Current->getLocation(OpPC); 1005 S.CCEDiag(Loc, diag::note_constexpr_unscoped_enum_out_of_range) 1006 << llvm::toString(Value, 10) << Min.getZExtValue() << Max.getZExtValue() 1007 << ED; 1008 } 1009 } 1010 1011 bool CallVar(InterpState &S, CodePtr OpPC, const Function *Func, 1012 uint32_t VarArgSize) { 1013 if (Func->hasThisPointer()) { 1014 size_t ArgSize = Func->getArgSize() + VarArgSize; 1015 size_t ThisOffset = ArgSize - (Func->hasRVO() ? primSize(PT_Ptr) : 0); 1016 const Pointer &ThisPtr = S.Stk.peek<Pointer>(ThisOffset); 1017 1018 // If the current function is a lambda static invoker and 1019 // the function we're about to call is a lambda call operator, 1020 // skip the CheckInvoke, since the ThisPtr is a null pointer 1021 // anyway. 1022 if (!(S.Current->getFunction() && 1023 S.Current->getFunction()->isLambdaStaticInvoker() && 1024 Func->isLambdaCallOperator())) { 1025 if (!CheckInvoke(S, OpPC, ThisPtr)) 1026 return false; 1027 } 1028 1029 if (S.checkingPotentialConstantExpression()) 1030 return false; 1031 } 1032 1033 if (!CheckCallable(S, OpPC, Func)) 1034 return false; 1035 1036 if (!CheckCallDepth(S, OpPC)) 1037 return false; 1038 1039 auto NewFrame = std::make_unique<InterpFrame>(S, Func, OpPC, VarArgSize); 1040 InterpFrame *FrameBefore = S.Current; 1041 S.Current = NewFrame.get(); 1042 1043 APValue CallResult; 1044 // Note that we cannot assert(CallResult.hasValue()) here since 1045 // Ret() above only sets the APValue if the curent frame doesn't 1046 // have a caller set. 1047 if (Interpret(S, CallResult)) { 1048 NewFrame.release(); // Frame was delete'd already. 1049 assert(S.Current == FrameBefore); 1050 return true; 1051 } 1052 1053 // Interpreting the function failed somehow. Reset to 1054 // previous state. 1055 S.Current = FrameBefore; 1056 return false; 1057 } 1058 1059 bool Call(InterpState &S, CodePtr OpPC, const Function *Func, 1060 uint32_t VarArgSize) { 1061 assert(Func); 1062 auto cleanup = [&]() -> bool { 1063 cleanupAfterFunctionCall(S, OpPC, Func); 1064 return false; 1065 }; 1066 1067 if (Func->hasThisPointer()) { 1068 size_t ArgSize = Func->getArgSize() + VarArgSize; 1069 size_t ThisOffset = ArgSize - (Func->hasRVO() ? primSize(PT_Ptr) : 0); 1070 1071 const Pointer &ThisPtr = S.Stk.peek<Pointer>(ThisOffset); 1072 1073 // If the current function is a lambda static invoker and 1074 // the function we're about to call is a lambda call operator, 1075 // skip the CheckInvoke, since the ThisPtr is a null pointer 1076 // anyway. 1077 if (S.Current->getFunction() && 1078 S.Current->getFunction()->isLambdaStaticInvoker() && 1079 Func->isLambdaCallOperator()) { 1080 assert(ThisPtr.isZero()); 1081 } else { 1082 if (!CheckInvoke(S, OpPC, ThisPtr)) 1083 return cleanup(); 1084 } 1085 } 1086 1087 if (!CheckCallable(S, OpPC, Func)) 1088 return cleanup(); 1089 1090 // FIXME: The isConstructor() check here is not always right. The current 1091 // constant evaluator is somewhat inconsistent in when it allows a function 1092 // call when checking for a constant expression. 1093 if (Func->hasThisPointer() && S.checkingPotentialConstantExpression() && 1094 !Func->isConstructor()) 1095 return cleanup(); 1096 1097 if (!CheckCallDepth(S, OpPC)) 1098 return cleanup(); 1099 1100 auto NewFrame = std::make_unique<InterpFrame>(S, Func, OpPC, VarArgSize); 1101 InterpFrame *FrameBefore = S.Current; 1102 S.Current = NewFrame.get(); 1103 1104 APValue CallResult; 1105 // Note that we cannot assert(CallResult.hasValue()) here since 1106 // Ret() above only sets the APValue if the curent frame doesn't 1107 // have a caller set. 1108 if (Interpret(S, CallResult)) { 1109 NewFrame.release(); // Frame was delete'd already. 1110 assert(S.Current == FrameBefore); 1111 return true; 1112 } 1113 1114 // Interpreting the function failed somehow. Reset to 1115 // previous state. 1116 S.Current = FrameBefore; 1117 return false; 1118 } 1119 1120 bool CallVirt(InterpState &S, CodePtr OpPC, const Function *Func, 1121 uint32_t VarArgSize) { 1122 assert(Func->hasThisPointer()); 1123 assert(Func->isVirtual()); 1124 size_t ArgSize = Func->getArgSize() + VarArgSize; 1125 size_t ThisOffset = ArgSize - (Func->hasRVO() ? primSize(PT_Ptr) : 0); 1126 Pointer &ThisPtr = S.Stk.peek<Pointer>(ThisOffset); 1127 1128 const CXXRecordDecl *DynamicDecl = nullptr; 1129 { 1130 Pointer TypePtr = ThisPtr; 1131 while (TypePtr.isBaseClass()) 1132 TypePtr = TypePtr.getBase(); 1133 1134 QualType DynamicType = TypePtr.getType(); 1135 if (DynamicType->isPointerType() || DynamicType->isReferenceType()) 1136 DynamicDecl = DynamicType->getPointeeCXXRecordDecl(); 1137 else 1138 DynamicDecl = DynamicType->getAsCXXRecordDecl(); 1139 } 1140 assert(DynamicDecl); 1141 1142 const auto *StaticDecl = cast<CXXRecordDecl>(Func->getParentDecl()); 1143 const auto *InitialFunction = cast<CXXMethodDecl>(Func->getDecl()); 1144 const CXXMethodDecl *Overrider = S.getContext().getOverridingFunction( 1145 DynamicDecl, StaticDecl, InitialFunction); 1146 1147 if (Overrider != InitialFunction) { 1148 // DR1872: An instantiated virtual constexpr function can't be called in a 1149 // constant expression (prior to C++20). We can still constant-fold such a 1150 // call. 1151 if (!S.getLangOpts().CPlusPlus20 && Overrider->isVirtual()) { 1152 const Expr *E = S.Current->getExpr(OpPC); 1153 S.CCEDiag(E, diag::note_constexpr_virtual_call) << E->getSourceRange(); 1154 } 1155 1156 Func = S.getContext().getOrCreateFunction(Overrider); 1157 1158 const CXXRecordDecl *ThisFieldDecl = 1159 ThisPtr.getFieldDesc()->getType()->getAsCXXRecordDecl(); 1160 if (Func->getParentDecl()->isDerivedFrom(ThisFieldDecl)) { 1161 // If the function we call is further DOWN the hierarchy than the 1162 // FieldDesc of our pointer, just go up the hierarchy of this field 1163 // the furthest we can go. 1164 while (ThisPtr.isBaseClass()) 1165 ThisPtr = ThisPtr.getBase(); 1166 } 1167 } 1168 1169 if (!Call(S, OpPC, Func, VarArgSize)) 1170 return false; 1171 1172 // Covariant return types. The return type of Overrider is a pointer 1173 // or reference to a class type. 1174 if (Overrider != InitialFunction && 1175 Overrider->getReturnType()->isPointerOrReferenceType() && 1176 InitialFunction->getReturnType()->isPointerOrReferenceType()) { 1177 QualType OverriderPointeeType = 1178 Overrider->getReturnType()->getPointeeType(); 1179 QualType InitialPointeeType = 1180 InitialFunction->getReturnType()->getPointeeType(); 1181 // We've called Overrider above, but calling code expects us to return what 1182 // InitialFunction returned. According to the rules for covariant return 1183 // types, what InitialFunction returns needs to be a base class of what 1184 // Overrider returns. So, we need to do an upcast here. 1185 unsigned Offset = S.getContext().collectBaseOffset( 1186 InitialPointeeType->getAsRecordDecl(), 1187 OverriderPointeeType->getAsRecordDecl()); 1188 return GetPtrBasePop(S, OpPC, Offset); 1189 } 1190 1191 return true; 1192 } 1193 1194 bool CallBI(InterpState &S, CodePtr OpPC, const Function *Func, 1195 const CallExpr *CE, uint32_t BuiltinID) { 1196 if (S.checkingPotentialConstantExpression()) 1197 return false; 1198 auto NewFrame = std::make_unique<InterpFrame>(S, Func, OpPC); 1199 1200 InterpFrame *FrameBefore = S.Current; 1201 S.Current = NewFrame.get(); 1202 1203 if (InterpretBuiltin(S, OpPC, Func, CE, BuiltinID)) { 1204 NewFrame.release(); 1205 return true; 1206 } 1207 S.Current = FrameBefore; 1208 return false; 1209 } 1210 1211 bool CallPtr(InterpState &S, CodePtr OpPC, uint32_t ArgSize, 1212 const CallExpr *CE) { 1213 const FunctionPointer &FuncPtr = S.Stk.pop<FunctionPointer>(); 1214 1215 const Function *F = FuncPtr.getFunction(); 1216 if (!F) { 1217 const auto *E = cast<CallExpr>(S.Current->getExpr(OpPC)); 1218 S.FFDiag(E, diag::note_constexpr_null_callee) 1219 << const_cast<Expr *>(E->getCallee()) << E->getSourceRange(); 1220 return false; 1221 } 1222 1223 if (!FuncPtr.isValid() || !F->getDecl()) 1224 return Invalid(S, OpPC); 1225 1226 assert(F); 1227 1228 // This happens when the call expression has been cast to 1229 // something else, but we don't support that. 1230 if (S.Ctx.classify(F->getDecl()->getReturnType()) != 1231 S.Ctx.classify(CE->getType())) 1232 return false; 1233 1234 // Check argument nullability state. 1235 if (F->hasNonNullAttr()) { 1236 if (!CheckNonNullArgs(S, OpPC, F, CE, ArgSize)) 1237 return false; 1238 } 1239 1240 assert(ArgSize >= F->getWrittenArgSize()); 1241 uint32_t VarArgSize = ArgSize - F->getWrittenArgSize(); 1242 1243 // We need to do this explicitly here since we don't have the necessary 1244 // information to do it automatically. 1245 if (F->isThisPointerExplicit()) 1246 VarArgSize -= align(primSize(PT_Ptr)); 1247 1248 if (F->isVirtual()) 1249 return CallVirt(S, OpPC, F, VarArgSize); 1250 1251 return Call(S, OpPC, F, VarArgSize); 1252 } 1253 1254 bool Interpret(InterpState &S, APValue &Result) { 1255 // The current stack frame when we started Interpret(). 1256 // This is being used by the ops to determine wheter 1257 // to return from this function and thus terminate 1258 // interpretation. 1259 const InterpFrame *StartFrame = S.Current; 1260 assert(!S.Current->isRoot()); 1261 CodePtr PC = S.Current->getPC(); 1262 1263 // Empty program. 1264 if (!PC) 1265 return true; 1266 1267 for (;;) { 1268 auto Op = PC.read<Opcode>(); 1269 CodePtr OpPC = PC; 1270 1271 switch (Op) { 1272 #define GET_INTERP 1273 #include "Opcodes.inc" 1274 #undef GET_INTERP 1275 } 1276 } 1277 } 1278 1279 } // namespace interp 1280 } // namespace clang 1281