21388 lines · cpp
1//===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===//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-exception6//7//===----------------------------------------------------------------------===//8//9// This file implements the Expr constant evaluator.10//11// Constant expression evaluation produces four main results:12//13// * A success/failure flag indicating whether constant folding was successful.14// This is the 'bool' return value used by most of the code in this file. A15// 'false' return value indicates that constant folding has failed, and any16// appropriate diagnostic has already been produced.17//18// * An evaluated result, valid only if constant folding has not failed.19//20// * A flag indicating if evaluation encountered (unevaluated) side-effects.21// These arise in cases such as (sideEffect(), 0) and (sideEffect() || 1),22// where it is possible to determine the evaluated result regardless.23//24// * A set of notes indicating why the evaluation was not a constant expression25// (under the C++11 / C++1y rules only, at the moment), or, if folding failed26// too, why the expression could not be folded.27//28// If we are checking for a potential constant expression, failure to constant29// fold a potential constant sub-expression will be indicated by a 'false'30// return value (the expression could not be folded) and no diagnostic (the31// expression is not necessarily non-constant).32//33//===----------------------------------------------------------------------===//34 35#include "ByteCode/Context.h"36#include "ByteCode/Frame.h"37#include "ByteCode/State.h"38#include "ExprConstShared.h"39#include "clang/AST/APValue.h"40#include "clang/AST/ASTContext.h"41#include "clang/AST/ASTLambda.h"42#include "clang/AST/Attr.h"43#include "clang/AST/CXXInheritance.h"44#include "clang/AST/CharUnits.h"45#include "clang/AST/CurrentSourceLocExprScope.h"46#include "clang/AST/Expr.h"47#include "clang/AST/InferAlloc.h"48#include "clang/AST/OSLog.h"49#include "clang/AST/OptionalDiagnostic.h"50#include "clang/AST/RecordLayout.h"51#include "clang/AST/StmtVisitor.h"52#include "clang/AST/Type.h"53#include "clang/AST/TypeLoc.h"54#include "clang/Basic/Builtins.h"55#include "clang/Basic/DiagnosticSema.h"56#include "clang/Basic/TargetBuiltins.h"57#include "clang/Basic/TargetInfo.h"58#include "llvm/ADT/APFixedPoint.h"59#include "llvm/ADT/Sequence.h"60#include "llvm/ADT/SmallBitVector.h"61#include "llvm/ADT/StringExtras.h"62#include "llvm/Support/Casting.h"63#include "llvm/Support/Debug.h"64#include "llvm/Support/SaveAndRestore.h"65#include "llvm/Support/SipHash.h"66#include "llvm/Support/TimeProfiler.h"67#include "llvm/Support/raw_ostream.h"68#include <cstring>69#include <functional>70#include <limits>71#include <optional>72 73#define DEBUG_TYPE "exprconstant"74 75using namespace clang;76using llvm::APFixedPoint;77using llvm::APInt;78using llvm::APSInt;79using llvm::APFloat;80using llvm::FixedPointSemantics;81 82namespace {83 struct LValue;84 class CallStackFrame;85 class EvalInfo;86 87 using SourceLocExprScopeGuard =88 CurrentSourceLocExprScope::SourceLocExprScopeGuard;89 90 static QualType getType(APValue::LValueBase B) {91 return B.getType();92 }93 94 /// Get an LValue path entry, which is known to not be an array index, as a95 /// field declaration.96 static const FieldDecl *getAsField(APValue::LValuePathEntry E) {97 return dyn_cast_or_null<FieldDecl>(E.getAsBaseOrMember().getPointer());98 }99 /// Get an LValue path entry, which is known to not be an array index, as a100 /// base class declaration.101 static const CXXRecordDecl *getAsBaseClass(APValue::LValuePathEntry E) {102 return dyn_cast_or_null<CXXRecordDecl>(E.getAsBaseOrMember().getPointer());103 }104 /// Determine whether this LValue path entry for a base class names a virtual105 /// base class.106 static bool isVirtualBaseClass(APValue::LValuePathEntry E) {107 return E.getAsBaseOrMember().getInt();108 }109 110 /// Given an expression, determine the type used to store the result of111 /// evaluating that expression.112 static QualType getStorageType(const ASTContext &Ctx, const Expr *E) {113 if (E->isPRValue())114 return E->getType();115 return Ctx.getLValueReferenceType(E->getType());116 }117 118 /// Attempts to unwrap a CallExpr (with an alloc_size attribute) from an Expr.119 /// This will look through a single cast.120 ///121 /// Returns null if we couldn't unwrap a function with alloc_size.122 static const CallExpr *tryUnwrapAllocSizeCall(const Expr *E) {123 if (!E->getType()->isPointerType())124 return nullptr;125 126 E = E->IgnoreParens();127 // If we're doing a variable assignment from e.g. malloc(N), there will128 // probably be a cast of some kind. In exotic cases, we might also see a129 // top-level ExprWithCleanups. Ignore them either way.130 if (const auto *FE = dyn_cast<FullExpr>(E))131 E = FE->getSubExpr()->IgnoreParens();132 133 if (const auto *Cast = dyn_cast<CastExpr>(E))134 E = Cast->getSubExpr()->IgnoreParens();135 136 if (const auto *CE = dyn_cast<CallExpr>(E))137 return CE->getCalleeAllocSizeAttr() ? CE : nullptr;138 return nullptr;139 }140 141 /// Determines whether or not the given Base contains a call to a function142 /// with the alloc_size attribute.143 static bool isBaseAnAllocSizeCall(APValue::LValueBase Base) {144 const auto *E = Base.dyn_cast<const Expr *>();145 return E && E->getType()->isPointerType() && tryUnwrapAllocSizeCall(E);146 }147 148 /// Determines whether the given kind of constant expression is only ever149 /// used for name mangling. If so, it's permitted to reference things that we150 /// can't generate code for (in particular, dllimported functions).151 static bool isForManglingOnly(ConstantExprKind Kind) {152 switch (Kind) {153 case ConstantExprKind::Normal:154 case ConstantExprKind::ClassTemplateArgument:155 case ConstantExprKind::ImmediateInvocation:156 // Note that non-type template arguments of class type are emitted as157 // template parameter objects.158 return false;159 160 case ConstantExprKind::NonClassTemplateArgument:161 return true;162 }163 llvm_unreachable("unknown ConstantExprKind");164 }165 166 static bool isTemplateArgument(ConstantExprKind Kind) {167 switch (Kind) {168 case ConstantExprKind::Normal:169 case ConstantExprKind::ImmediateInvocation:170 return false;171 172 case ConstantExprKind::ClassTemplateArgument:173 case ConstantExprKind::NonClassTemplateArgument:174 return true;175 }176 llvm_unreachable("unknown ConstantExprKind");177 }178 179 /// The bound to claim that an array of unknown bound has.180 /// The value in MostDerivedArraySize is undefined in this case. So, set it181 /// to an arbitrary value that's likely to loudly break things if it's used.182 static const uint64_t AssumedSizeForUnsizedArray =183 std::numeric_limits<uint64_t>::max() / 2;184 185 /// Determines if an LValue with the given LValueBase will have an unsized186 /// array in its designator.187 /// Find the path length and type of the most-derived subobject in the given188 /// path, and find the size of the containing array, if any.189 static unsigned190 findMostDerivedSubobject(const ASTContext &Ctx, APValue::LValueBase Base,191 ArrayRef<APValue::LValuePathEntry> Path,192 uint64_t &ArraySize, QualType &Type, bool &IsArray,193 bool &FirstEntryIsUnsizedArray) {194 // This only accepts LValueBases from APValues, and APValues don't support195 // arrays that lack size info.196 assert(!isBaseAnAllocSizeCall(Base) &&197 "Unsized arrays shouldn't appear here");198 unsigned MostDerivedLength = 0;199 // The type of Base is a reference type if the base is a constexpr-unknown200 // variable. In that case, look through the reference type.201 Type = getType(Base).getNonReferenceType();202 203 for (unsigned I = 0, N = Path.size(); I != N; ++I) {204 if (Type->isArrayType()) {205 const ArrayType *AT = Ctx.getAsArrayType(Type);206 Type = AT->getElementType();207 MostDerivedLength = I + 1;208 IsArray = true;209 210 if (auto *CAT = dyn_cast<ConstantArrayType>(AT)) {211 ArraySize = CAT->getZExtSize();212 } else {213 assert(I == 0 && "unexpected unsized array designator");214 FirstEntryIsUnsizedArray = true;215 ArraySize = AssumedSizeForUnsizedArray;216 }217 } else if (Type->isAnyComplexType()) {218 const ComplexType *CT = Type->castAs<ComplexType>();219 Type = CT->getElementType();220 ArraySize = 2;221 MostDerivedLength = I + 1;222 IsArray = true;223 } else if (const auto *VT = Type->getAs<VectorType>()) {224 Type = VT->getElementType();225 ArraySize = VT->getNumElements();226 MostDerivedLength = I + 1;227 IsArray = true;228 } else if (const FieldDecl *FD = getAsField(Path[I])) {229 Type = FD->getType();230 ArraySize = 0;231 MostDerivedLength = I + 1;232 IsArray = false;233 } else {234 // Path[I] describes a base class.235 ArraySize = 0;236 IsArray = false;237 }238 }239 return MostDerivedLength;240 }241 242 /// A path from a glvalue to a subobject of that glvalue.243 struct SubobjectDesignator {244 /// True if the subobject was named in a manner not supported by C++11. Such245 /// lvalues can still be folded, but they are not core constant expressions246 /// and we cannot perform lvalue-to-rvalue conversions on them.247 LLVM_PREFERRED_TYPE(bool)248 unsigned Invalid : 1;249 250 /// Is this a pointer one past the end of an object?251 LLVM_PREFERRED_TYPE(bool)252 unsigned IsOnePastTheEnd : 1;253 254 /// Indicator of whether the first entry is an unsized array.255 LLVM_PREFERRED_TYPE(bool)256 unsigned FirstEntryIsAnUnsizedArray : 1;257 258 /// Indicator of whether the most-derived object is an array element.259 LLVM_PREFERRED_TYPE(bool)260 unsigned MostDerivedIsArrayElement : 1;261 262 /// The length of the path to the most-derived object of which this is a263 /// subobject.264 unsigned MostDerivedPathLength : 28;265 266 /// The size of the array of which the most-derived object is an element.267 /// This will always be 0 if the most-derived object is not an array268 /// element. 0 is not an indicator of whether or not the most-derived object269 /// is an array, however, because 0-length arrays are allowed.270 ///271 /// If the current array is an unsized array, the value of this is272 /// undefined.273 uint64_t MostDerivedArraySize;274 /// The type of the most derived object referred to by this address.275 QualType MostDerivedType;276 277 typedef APValue::LValuePathEntry PathEntry;278 279 /// The entries on the path from the glvalue to the designated subobject.280 SmallVector<PathEntry, 8> Entries;281 282 SubobjectDesignator() : Invalid(true) {}283 284 explicit SubobjectDesignator(QualType T)285 : Invalid(false), IsOnePastTheEnd(false),286 FirstEntryIsAnUnsizedArray(false), MostDerivedIsArrayElement(false),287 MostDerivedPathLength(0), MostDerivedArraySize(0),288 MostDerivedType(T.isNull() ? QualType() : T.getNonReferenceType()) {}289 290 SubobjectDesignator(const ASTContext &Ctx, const APValue &V)291 : Invalid(!V.isLValue() || !V.hasLValuePath()), IsOnePastTheEnd(false),292 FirstEntryIsAnUnsizedArray(false), MostDerivedIsArrayElement(false),293 MostDerivedPathLength(0), MostDerivedArraySize(0) {294 assert(V.isLValue() && "Non-LValue used to make an LValue designator?");295 if (!Invalid) {296 IsOnePastTheEnd = V.isLValueOnePastTheEnd();297 llvm::append_range(Entries, V.getLValuePath());298 if (V.getLValueBase()) {299 bool IsArray = false;300 bool FirstIsUnsizedArray = false;301 MostDerivedPathLength = findMostDerivedSubobject(302 Ctx, V.getLValueBase(), V.getLValuePath(), MostDerivedArraySize,303 MostDerivedType, IsArray, FirstIsUnsizedArray);304 MostDerivedIsArrayElement = IsArray;305 FirstEntryIsAnUnsizedArray = FirstIsUnsizedArray;306 }307 }308 }309 310 void truncate(ASTContext &Ctx, APValue::LValueBase Base,311 unsigned NewLength) {312 if (Invalid)313 return;314 315 assert(Base && "cannot truncate path for null pointer");316 assert(NewLength <= Entries.size() && "not a truncation");317 318 if (NewLength == Entries.size())319 return;320 Entries.resize(NewLength);321 322 bool IsArray = false;323 bool FirstIsUnsizedArray = false;324 MostDerivedPathLength = findMostDerivedSubobject(325 Ctx, Base, Entries, MostDerivedArraySize, MostDerivedType, IsArray,326 FirstIsUnsizedArray);327 MostDerivedIsArrayElement = IsArray;328 FirstEntryIsAnUnsizedArray = FirstIsUnsizedArray;329 }330 331 void setInvalid() {332 Invalid = true;333 Entries.clear();334 }335 336 /// Determine whether the most derived subobject is an array without a337 /// known bound.338 bool isMostDerivedAnUnsizedArray() const {339 assert(!Invalid && "Calling this makes no sense on invalid designators");340 return Entries.size() == 1 && FirstEntryIsAnUnsizedArray;341 }342 343 /// Determine what the most derived array's size is. Results in an assertion344 /// failure if the most derived array lacks a size.345 uint64_t getMostDerivedArraySize() const {346 assert(!isMostDerivedAnUnsizedArray() && "Unsized array has no size");347 return MostDerivedArraySize;348 }349 350 /// Determine whether this is a one-past-the-end pointer.351 bool isOnePastTheEnd() const {352 assert(!Invalid);353 if (IsOnePastTheEnd)354 return true;355 if (!isMostDerivedAnUnsizedArray() && MostDerivedIsArrayElement &&356 Entries[MostDerivedPathLength - 1].getAsArrayIndex() ==357 MostDerivedArraySize)358 return true;359 return false;360 }361 362 /// Get the range of valid index adjustments in the form363 /// {maximum value that can be subtracted from this pointer,364 /// maximum value that can be added to this pointer}365 std::pair<uint64_t, uint64_t> validIndexAdjustments() {366 if (Invalid || isMostDerivedAnUnsizedArray())367 return {0, 0};368 369 // [expr.add]p4: For the purposes of these operators, a pointer to a370 // nonarray object behaves the same as a pointer to the first element of371 // an array of length one with the type of the object as its element type.372 bool IsArray = MostDerivedPathLength == Entries.size() &&373 MostDerivedIsArrayElement;374 uint64_t ArrayIndex = IsArray ? Entries.back().getAsArrayIndex()375 : (uint64_t)IsOnePastTheEnd;376 uint64_t ArraySize =377 IsArray ? getMostDerivedArraySize() : (uint64_t)1;378 return {ArrayIndex, ArraySize - ArrayIndex};379 }380 381 /// Check that this refers to a valid subobject.382 bool isValidSubobject() const {383 if (Invalid)384 return false;385 return !isOnePastTheEnd();386 }387 /// Check that this refers to a valid subobject, and if not, produce a388 /// relevant diagnostic and set the designator as invalid.389 bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK);390 391 /// Get the type of the designated object.392 QualType getType(ASTContext &Ctx) const {393 assert(!Invalid && "invalid designator has no subobject type");394 return MostDerivedPathLength == Entries.size()395 ? MostDerivedType396 : Ctx.getCanonicalTagType(getAsBaseClass(Entries.back()));397 }398 399 /// Update this designator to refer to the first element within this array.400 void addArrayUnchecked(const ConstantArrayType *CAT) {401 Entries.push_back(PathEntry::ArrayIndex(0));402 403 // This is a most-derived object.404 MostDerivedType = CAT->getElementType();405 MostDerivedIsArrayElement = true;406 MostDerivedArraySize = CAT->getZExtSize();407 MostDerivedPathLength = Entries.size();408 }409 /// Update this designator to refer to the first element within the array of410 /// elements of type T. This is an array of unknown size.411 void addUnsizedArrayUnchecked(QualType ElemTy) {412 Entries.push_back(PathEntry::ArrayIndex(0));413 414 MostDerivedType = ElemTy;415 MostDerivedIsArrayElement = true;416 // The value in MostDerivedArraySize is undefined in this case. So, set it417 // to an arbitrary value that's likely to loudly break things if it's418 // used.419 MostDerivedArraySize = AssumedSizeForUnsizedArray;420 MostDerivedPathLength = Entries.size();421 }422 /// Update this designator to refer to the given base or member of this423 /// object.424 void addDeclUnchecked(const Decl *D, bool Virtual = false) {425 Entries.push_back(APValue::BaseOrMemberType(D, Virtual));426 427 // If this isn't a base class, it's a new most-derived object.428 if (const FieldDecl *FD = dyn_cast<FieldDecl>(D)) {429 MostDerivedType = FD->getType();430 MostDerivedIsArrayElement = false;431 MostDerivedArraySize = 0;432 MostDerivedPathLength = Entries.size();433 }434 }435 /// Update this designator to refer to the given complex component.436 void addComplexUnchecked(QualType EltTy, bool Imag) {437 Entries.push_back(PathEntry::ArrayIndex(Imag));438 439 // This is technically a most-derived object, though in practice this440 // is unlikely to matter.441 MostDerivedType = EltTy;442 MostDerivedIsArrayElement = true;443 MostDerivedArraySize = 2;444 MostDerivedPathLength = Entries.size();445 }446 447 void addVectorElementUnchecked(QualType EltTy, uint64_t Size,448 uint64_t Idx) {449 Entries.push_back(PathEntry::ArrayIndex(Idx));450 MostDerivedType = EltTy;451 MostDerivedPathLength = Entries.size();452 MostDerivedArraySize = 0;453 MostDerivedIsArrayElement = false;454 }455 456 void diagnoseUnsizedArrayPointerArithmetic(EvalInfo &Info, const Expr *E);457 void diagnosePointerArithmetic(EvalInfo &Info, const Expr *E,458 const APSInt &N);459 /// Add N to the address of this subobject.460 void adjustIndex(EvalInfo &Info, const Expr *E, APSInt N, const LValue &LV);461 };462 463 /// A scope at the end of which an object can need to be destroyed.464 enum class ScopeKind {465 Block,466 FullExpression,467 Call468 };469 470 /// A reference to a particular call and its arguments.471 struct CallRef {472 CallRef() : OrigCallee(), CallIndex(0), Version() {}473 CallRef(const FunctionDecl *Callee, unsigned CallIndex, unsigned Version)474 : OrigCallee(Callee), CallIndex(CallIndex), Version(Version) {}475 476 explicit operator bool() const { return OrigCallee; }477 478 /// Get the parameter that the caller initialized, corresponding to the479 /// given parameter in the callee.480 const ParmVarDecl *getOrigParam(const ParmVarDecl *PVD) const {481 return OrigCallee ? OrigCallee->getParamDecl(PVD->getFunctionScopeIndex())482 : PVD;483 }484 485 /// The callee at the point where the arguments were evaluated. This might486 /// be different from the actual callee (a different redeclaration, or a487 /// virtual override), but this function's parameters are the ones that488 /// appear in the parameter map.489 const FunctionDecl *OrigCallee;490 /// The call index of the frame that holds the argument values.491 unsigned CallIndex;492 /// The version of the parameters corresponding to this call.493 unsigned Version;494 };495 496 /// A stack frame in the constexpr call stack.497 class CallStackFrame : public interp::Frame {498 public:499 EvalInfo &Info;500 501 /// Parent - The caller of this stack frame.502 CallStackFrame *Caller;503 504 /// Callee - The function which was called.505 const FunctionDecl *Callee;506 507 /// This - The binding for the this pointer in this call, if any.508 const LValue *This;509 510 /// CallExpr - The syntactical structure of member function calls511 const Expr *CallExpr;512 513 /// Information on how to find the arguments to this call. Our arguments514 /// are stored in our parent's CallStackFrame, using the ParmVarDecl* as a515 /// key and this value as the version.516 CallRef Arguments;517 518 /// Source location information about the default argument or default519 /// initializer expression we're evaluating, if any.520 CurrentSourceLocExprScope CurSourceLocExprScope;521 522 // Note that we intentionally use std::map here so that references to523 // values are stable.524 typedef std::pair<const void *, unsigned> MapKeyTy;525 typedef std::map<MapKeyTy, APValue> MapTy;526 /// Temporaries - Temporary lvalues materialized within this stack frame.527 MapTy Temporaries;528 529 /// CallRange - The source range of the call expression for this call.530 SourceRange CallRange;531 532 /// Index - The call index of this call.533 unsigned Index;534 535 /// The stack of integers for tracking version numbers for temporaries.536 SmallVector<unsigned, 2> TempVersionStack = {1};537 unsigned CurTempVersion = TempVersionStack.back();538 539 unsigned getTempVersion() const { return TempVersionStack.back(); }540 541 void pushTempVersion() {542 TempVersionStack.push_back(++CurTempVersion);543 }544 545 void popTempVersion() {546 TempVersionStack.pop_back();547 }548 549 CallRef createCall(const FunctionDecl *Callee) {550 return {Callee, Index, ++CurTempVersion};551 }552 553 // FIXME: Adding this to every 'CallStackFrame' may have a nontrivial impact554 // on the overall stack usage of deeply-recursing constexpr evaluations.555 // (We should cache this map rather than recomputing it repeatedly.)556 // But let's try this and see how it goes; we can look into caching the map557 // as a later change.558 559 /// LambdaCaptureFields - Mapping from captured variables/this to560 /// corresponding data members in the closure class.561 llvm::DenseMap<const ValueDecl *, FieldDecl *> LambdaCaptureFields;562 FieldDecl *LambdaThisCaptureField = nullptr;563 564 CallStackFrame(EvalInfo &Info, SourceRange CallRange,565 const FunctionDecl *Callee, const LValue *This,566 const Expr *CallExpr, CallRef Arguments);567 ~CallStackFrame();568 569 // Return the temporary for Key whose version number is Version.570 APValue *getTemporary(const void *Key, unsigned Version) {571 MapKeyTy KV(Key, Version);572 auto LB = Temporaries.lower_bound(KV);573 if (LB != Temporaries.end() && LB->first == KV)574 return &LB->second;575 return nullptr;576 }577 578 // Return the current temporary for Key in the map.579 APValue *getCurrentTemporary(const void *Key) {580 auto UB = Temporaries.upper_bound(MapKeyTy(Key, UINT_MAX));581 if (UB != Temporaries.begin() && std::prev(UB)->first.first == Key)582 return &std::prev(UB)->second;583 return nullptr;584 }585 586 // Return the version number of the current temporary for Key.587 unsigned getCurrentTemporaryVersion(const void *Key) const {588 auto UB = Temporaries.upper_bound(MapKeyTy(Key, UINT_MAX));589 if (UB != Temporaries.begin() && std::prev(UB)->first.first == Key)590 return std::prev(UB)->first.second;591 return 0;592 }593 594 /// Allocate storage for an object of type T in this stack frame.595 /// Populates LV with a handle to the created object. Key identifies596 /// the temporary within the stack frame, and must not be reused without597 /// bumping the temporary version number.598 template<typename KeyT>599 APValue &createTemporary(const KeyT *Key, QualType T,600 ScopeKind Scope, LValue &LV);601 602 /// Allocate storage for a parameter of a function call made in this frame.603 APValue &createParam(CallRef Args, const ParmVarDecl *PVD, LValue &LV);604 605 void describe(llvm::raw_ostream &OS) const override;606 607 Frame *getCaller() const override { return Caller; }608 SourceRange getCallRange() const override { return CallRange; }609 const FunctionDecl *getCallee() const override { return Callee; }610 611 bool isStdFunction() const {612 for (const DeclContext *DC = Callee; DC; DC = DC->getParent())613 if (DC->isStdNamespace())614 return true;615 return false;616 }617 618 /// Whether we're in a context where [[msvc::constexpr]] evaluation is619 /// permitted. See MSConstexprDocs for description of permitted contexts.620 bool CanEvalMSConstexpr = false;621 622 private:623 APValue &createLocal(APValue::LValueBase Base, const void *Key, QualType T,624 ScopeKind Scope);625 };626 627 /// Temporarily override 'this'.628 class ThisOverrideRAII {629 public:630 ThisOverrideRAII(CallStackFrame &Frame, const LValue *NewThis, bool Enable)631 : Frame(Frame), OldThis(Frame.This) {632 if (Enable)633 Frame.This = NewThis;634 }635 ~ThisOverrideRAII() {636 Frame.This = OldThis;637 }638 private:639 CallStackFrame &Frame;640 const LValue *OldThis;641 };642 643 // A shorthand time trace scope struct, prints source range, for example644 // {"name":"EvaluateAsRValue","args":{"detail":"<test.cc:8:21, col:25>"}}}645 class ExprTimeTraceScope {646 public:647 ExprTimeTraceScope(const Expr *E, const ASTContext &Ctx, StringRef Name)648 : TimeScope(Name, [E, &Ctx] {649 return E->getSourceRange().printToString(Ctx.getSourceManager());650 }) {}651 652 private:653 llvm::TimeTraceScope TimeScope;654 };655 656 /// RAII object used to change the current ability of657 /// [[msvc::constexpr]] evaulation.658 struct MSConstexprContextRAII {659 CallStackFrame &Frame;660 bool OldValue;661 explicit MSConstexprContextRAII(CallStackFrame &Frame, bool Value)662 : Frame(Frame), OldValue(Frame.CanEvalMSConstexpr) {663 Frame.CanEvalMSConstexpr = Value;664 }665 666 ~MSConstexprContextRAII() { Frame.CanEvalMSConstexpr = OldValue; }667 };668}669 670static bool HandleDestruction(EvalInfo &Info, const Expr *E,671 const LValue &This, QualType ThisType);672static bool HandleDestruction(EvalInfo &Info, SourceLocation Loc,673 APValue::LValueBase LVBase, APValue &Value,674 QualType T);675 676namespace {677 /// A cleanup, and a flag indicating whether it is lifetime-extended.678 class Cleanup {679 llvm::PointerIntPair<APValue*, 2, ScopeKind> Value;680 APValue::LValueBase Base;681 QualType T;682 683 public:684 Cleanup(APValue *Val, APValue::LValueBase Base, QualType T,685 ScopeKind Scope)686 : Value(Val, Scope), Base(Base), T(T) {}687 688 /// Determine whether this cleanup should be performed at the end of the689 /// given kind of scope.690 bool isDestroyedAtEndOf(ScopeKind K) const {691 return (int)Value.getInt() >= (int)K;692 }693 bool endLifetime(EvalInfo &Info, bool RunDestructors) {694 if (RunDestructors) {695 SourceLocation Loc;696 if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>())697 Loc = VD->getLocation();698 else if (const Expr *E = Base.dyn_cast<const Expr*>())699 Loc = E->getExprLoc();700 return HandleDestruction(Info, Loc, Base, *Value.getPointer(), T);701 }702 *Value.getPointer() = APValue();703 return true;704 }705 706 bool hasSideEffect() {707 return T.isDestructedType();708 }709 };710 711 /// A reference to an object whose construction we are currently evaluating.712 struct ObjectUnderConstruction {713 APValue::LValueBase Base;714 ArrayRef<APValue::LValuePathEntry> Path;715 friend bool operator==(const ObjectUnderConstruction &LHS,716 const ObjectUnderConstruction &RHS) {717 return LHS.Base == RHS.Base && LHS.Path == RHS.Path;718 }719 friend llvm::hash_code hash_value(const ObjectUnderConstruction &Obj) {720 return llvm::hash_combine(Obj.Base, Obj.Path);721 }722 };723 enum class ConstructionPhase {724 None,725 Bases,726 AfterBases,727 AfterFields,728 Destroying,729 DestroyingBases730 };731}732 733namespace llvm {734template<> struct DenseMapInfo<ObjectUnderConstruction> {735 using Base = DenseMapInfo<APValue::LValueBase>;736 static ObjectUnderConstruction getEmptyKey() {737 return {Base::getEmptyKey(), {}}; }738 static ObjectUnderConstruction getTombstoneKey() {739 return {Base::getTombstoneKey(), {}};740 }741 static unsigned getHashValue(const ObjectUnderConstruction &Object) {742 return hash_value(Object);743 }744 static bool isEqual(const ObjectUnderConstruction &LHS,745 const ObjectUnderConstruction &RHS) {746 return LHS == RHS;747 }748};749}750 751namespace {752 /// A dynamically-allocated heap object.753 struct DynAlloc {754 /// The value of this heap-allocated object.755 APValue Value;756 /// The allocating expression; used for diagnostics. Either a CXXNewExpr757 /// or a CallExpr (the latter is for direct calls to operator new inside758 /// std::allocator<T>::allocate).759 const Expr *AllocExpr = nullptr;760 761 enum Kind {762 New,763 ArrayNew,764 StdAllocator765 };766 767 /// Get the kind of the allocation. This must match between allocation768 /// and deallocation.769 Kind getKind() const {770 if (auto *NE = dyn_cast<CXXNewExpr>(AllocExpr))771 return NE->isArray() ? ArrayNew : New;772 assert(isa<CallExpr>(AllocExpr));773 return StdAllocator;774 }775 };776 777 struct DynAllocOrder {778 bool operator()(DynamicAllocLValue L, DynamicAllocLValue R) const {779 return L.getIndex() < R.getIndex();780 }781 };782 783 /// EvalInfo - This is a private struct used by the evaluator to capture784 /// information about a subexpression as it is folded. It retains information785 /// about the AST context, but also maintains information about the folded786 /// expression.787 ///788 /// If an expression could be evaluated, it is still possible it is not a C789 /// "integer constant expression" or constant expression. If not, this struct790 /// captures information about how and why not.791 ///792 /// One bit of information passed *into* the request for constant folding793 /// indicates whether the subexpression is "evaluated" or not according to C794 /// rules. For example, the RHS of (0 && foo()) is not evaluated. We can795 /// evaluate the expression regardless of what the RHS is, but C only allows796 /// certain things in certain situations.797 class EvalInfo : public interp::State {798 public:799 ASTContext &Ctx;800 801 /// EvalStatus - Contains information about the evaluation.802 Expr::EvalStatus &EvalStatus;803 804 /// CurrentCall - The top of the constexpr call stack.805 CallStackFrame *CurrentCall;806 807 /// CallStackDepth - The number of calls in the call stack right now.808 unsigned CallStackDepth;809 810 /// NextCallIndex - The next call index to assign.811 unsigned NextCallIndex;812 813 /// StepsLeft - The remaining number of evaluation steps we're permitted814 /// to perform. This is essentially a limit for the number of statements815 /// we will evaluate.816 unsigned StepsLeft;817 818 /// Enable the experimental new constant interpreter. If an expression is819 /// not supported by the interpreter, an error is triggered.820 bool EnableNewConstInterp;821 822 /// BottomFrame - The frame in which evaluation started. This must be823 /// initialized after CurrentCall and CallStackDepth.824 CallStackFrame BottomFrame;825 826 /// A stack of values whose lifetimes end at the end of some surrounding827 /// evaluation frame.828 llvm::SmallVector<Cleanup, 16> CleanupStack;829 830 /// EvaluatingDecl - This is the declaration whose initializer is being831 /// evaluated, if any.832 APValue::LValueBase EvaluatingDecl;833 834 enum class EvaluatingDeclKind {835 None,836 /// We're evaluating the construction of EvaluatingDecl.837 Ctor,838 /// We're evaluating the destruction of EvaluatingDecl.839 Dtor,840 };841 EvaluatingDeclKind IsEvaluatingDecl = EvaluatingDeclKind::None;842 843 /// EvaluatingDeclValue - This is the value being constructed for the844 /// declaration whose initializer is being evaluated, if any.845 APValue *EvaluatingDeclValue;846 847 /// Stack of loops and 'switch' statements which we're currently848 /// breaking/continuing; null entries are used to mark unlabeled849 /// break/continue.850 SmallVector<const Stmt *> BreakContinueStack;851 852 /// Set of objects that are currently being constructed.853 llvm::DenseMap<ObjectUnderConstruction, ConstructionPhase>854 ObjectsUnderConstruction;855 856 /// Current heap allocations, along with the location where each was857 /// allocated. We use std::map here because we need stable addresses858 /// for the stored APValues.859 std::map<DynamicAllocLValue, DynAlloc, DynAllocOrder> HeapAllocs;860 861 /// The number of heap allocations performed so far in this evaluation.862 unsigned NumHeapAllocs = 0;863 864 struct EvaluatingConstructorRAII {865 EvalInfo &EI;866 ObjectUnderConstruction Object;867 bool DidInsert;868 EvaluatingConstructorRAII(EvalInfo &EI, ObjectUnderConstruction Object,869 bool HasBases)870 : EI(EI), Object(Object) {871 DidInsert =872 EI.ObjectsUnderConstruction873 .insert({Object, HasBases ? ConstructionPhase::Bases874 : ConstructionPhase::AfterBases})875 .second;876 }877 void finishedConstructingBases() {878 EI.ObjectsUnderConstruction[Object] = ConstructionPhase::AfterBases;879 }880 void finishedConstructingFields() {881 EI.ObjectsUnderConstruction[Object] = ConstructionPhase::AfterFields;882 }883 ~EvaluatingConstructorRAII() {884 if (DidInsert) EI.ObjectsUnderConstruction.erase(Object);885 }886 };887 888 struct EvaluatingDestructorRAII {889 EvalInfo &EI;890 ObjectUnderConstruction Object;891 bool DidInsert;892 EvaluatingDestructorRAII(EvalInfo &EI, ObjectUnderConstruction Object)893 : EI(EI), Object(Object) {894 DidInsert = EI.ObjectsUnderConstruction895 .insert({Object, ConstructionPhase::Destroying})896 .second;897 }898 void startedDestroyingBases() {899 EI.ObjectsUnderConstruction[Object] =900 ConstructionPhase::DestroyingBases;901 }902 ~EvaluatingDestructorRAII() {903 if (DidInsert)904 EI.ObjectsUnderConstruction.erase(Object);905 }906 };907 908 ConstructionPhase909 isEvaluatingCtorDtor(APValue::LValueBase Base,910 ArrayRef<APValue::LValuePathEntry> Path) {911 return ObjectsUnderConstruction.lookup({Base, Path});912 }913 914 /// If we're currently speculatively evaluating, the outermost call stack915 /// depth at which we can mutate state, otherwise 0.916 unsigned SpeculativeEvaluationDepth = 0;917 918 /// The current array initialization index, if we're performing array919 /// initialization.920 uint64_t ArrayInitIndex = -1;921 922 /// HasActiveDiagnostic - Was the previous diagnostic stored? If so, further923 /// notes attached to it will also be stored, otherwise they will not be.924 bool HasActiveDiagnostic;925 926 /// Have we emitted a diagnostic explaining why we couldn't constant927 /// fold (not just why it's not strictly a constant expression)?928 bool HasFoldFailureDiagnostic;929 930 EvalInfo(const ASTContext &C, Expr::EvalStatus &S, EvaluationMode Mode)931 : Ctx(const_cast<ASTContext &>(C)), EvalStatus(S), CurrentCall(nullptr),932 CallStackDepth(0), NextCallIndex(1),933 StepsLeft(C.getLangOpts().ConstexprStepLimit),934 EnableNewConstInterp(C.getLangOpts().EnableNewConstInterp),935 BottomFrame(*this, SourceLocation(), /*Callee=*/nullptr,936 /*This=*/nullptr,937 /*CallExpr=*/nullptr, CallRef()),938 EvaluatingDecl((const ValueDecl *)nullptr),939 EvaluatingDeclValue(nullptr), HasActiveDiagnostic(false),940 HasFoldFailureDiagnostic(false) {941 EvalMode = Mode;942 }943 944 ~EvalInfo() {945 discardCleanups();946 }947 948 ASTContext &getASTContext() const override { return Ctx; }949 const LangOptions &getLangOpts() const { return Ctx.getLangOpts(); }950 951 void setEvaluatingDecl(APValue::LValueBase Base, APValue &Value,952 EvaluatingDeclKind EDK = EvaluatingDeclKind::Ctor) {953 EvaluatingDecl = Base;954 IsEvaluatingDecl = EDK;955 EvaluatingDeclValue = &Value;956 }957 958 bool CheckCallLimit(SourceLocation Loc) {959 // Don't perform any constexpr calls (other than the call we're checking)960 // when checking a potential constant expression.961 if (checkingPotentialConstantExpression() && CallStackDepth > 1)962 return false;963 if (NextCallIndex == 0) {964 // NextCallIndex has wrapped around.965 FFDiag(Loc, diag::note_constexpr_call_limit_exceeded);966 return false;967 }968 if (CallStackDepth <= getLangOpts().ConstexprCallDepth)969 return true;970 FFDiag(Loc, diag::note_constexpr_depth_limit_exceeded)971 << getLangOpts().ConstexprCallDepth;972 return false;973 }974 975 bool CheckArraySize(SourceLocation Loc, unsigned BitWidth,976 uint64_t ElemCount, bool Diag) {977 // FIXME: GH63562978 // APValue stores array extents as unsigned,979 // so anything that is greater that unsigned would overflow when980 // constructing the array, we catch this here.981 if (BitWidth > ConstantArrayType::getMaxSizeBits(Ctx) ||982 ElemCount > uint64_t(std::numeric_limits<unsigned>::max())) {983 if (Diag)984 FFDiag(Loc, diag::note_constexpr_new_too_large) << ElemCount;985 return false;986 }987 988 // FIXME: GH63562989 // Arrays allocate an APValue per element.990 // We use the number of constexpr steps as a proxy for the maximum size991 // of arrays to avoid exhausting the system resources, as initialization992 // of each element is likely to take some number of steps anyway.993 uint64_t Limit = Ctx.getLangOpts().ConstexprStepLimit;994 if (Limit != 0 && ElemCount > Limit) {995 if (Diag)996 FFDiag(Loc, diag::note_constexpr_new_exceeds_limits)997 << ElemCount << Limit;998 return false;999 }1000 return true;1001 }1002 1003 std::pair<CallStackFrame *, unsigned>1004 getCallFrameAndDepth(unsigned CallIndex) {1005 assert(CallIndex && "no call index in getCallFrameAndDepth");1006 // We will eventually hit BottomFrame, which has Index 1, so Frame can't1007 // be null in this loop.1008 unsigned Depth = CallStackDepth;1009 CallStackFrame *Frame = CurrentCall;1010 while (Frame->Index > CallIndex) {1011 Frame = Frame->Caller;1012 --Depth;1013 }1014 if (Frame->Index == CallIndex)1015 return {Frame, Depth};1016 return {nullptr, 0};1017 }1018 1019 bool nextStep(const Stmt *S) {1020 if (Ctx.getLangOpts().ConstexprStepLimit == 0)1021 return true;1022 1023 if (!StepsLeft) {1024 FFDiag(S->getBeginLoc(), diag::note_constexpr_step_limit_exceeded);1025 return false;1026 }1027 --StepsLeft;1028 return true;1029 }1030 1031 APValue *createHeapAlloc(const Expr *E, QualType T, LValue &LV);1032 1033 std::optional<DynAlloc *> lookupDynamicAlloc(DynamicAllocLValue DA) {1034 std::optional<DynAlloc *> Result;1035 auto It = HeapAllocs.find(DA);1036 if (It != HeapAllocs.end())1037 Result = &It->second;1038 return Result;1039 }1040 1041 /// Get the allocated storage for the given parameter of the given call.1042 APValue *getParamSlot(CallRef Call, const ParmVarDecl *PVD) {1043 CallStackFrame *Frame = getCallFrameAndDepth(Call.CallIndex).first;1044 return Frame ? Frame->getTemporary(Call.getOrigParam(PVD), Call.Version)1045 : nullptr;1046 }1047 1048 /// Information about a stack frame for std::allocator<T>::[de]allocate.1049 struct StdAllocatorCaller {1050 unsigned FrameIndex;1051 QualType ElemType;1052 const Expr *Call;1053 explicit operator bool() const { return FrameIndex != 0; };1054 };1055 1056 StdAllocatorCaller getStdAllocatorCaller(StringRef FnName) const {1057 for (const CallStackFrame *Call = CurrentCall; Call != &BottomFrame;1058 Call = Call->Caller) {1059 const auto *MD = dyn_cast_or_null<CXXMethodDecl>(Call->Callee);1060 if (!MD)1061 continue;1062 const IdentifierInfo *FnII = MD->getIdentifier();1063 if (!FnII || !FnII->isStr(FnName))1064 continue;1065 1066 const auto *CTSD =1067 dyn_cast<ClassTemplateSpecializationDecl>(MD->getParent());1068 if (!CTSD)1069 continue;1070 1071 const IdentifierInfo *ClassII = CTSD->getIdentifier();1072 const TemplateArgumentList &TAL = CTSD->getTemplateArgs();1073 if (CTSD->isInStdNamespace() && ClassII &&1074 ClassII->isStr("allocator") && TAL.size() >= 1 &&1075 TAL[0].getKind() == TemplateArgument::Type)1076 return {Call->Index, TAL[0].getAsType(), Call->CallExpr};1077 }1078 1079 return {};1080 }1081 1082 void performLifetimeExtension() {1083 // Disable the cleanups for lifetime-extended temporaries.1084 llvm::erase_if(CleanupStack, [](Cleanup &C) {1085 return !C.isDestroyedAtEndOf(ScopeKind::FullExpression);1086 });1087 }1088 1089 /// Throw away any remaining cleanups at the end of evaluation. If any1090 /// cleanups would have had a side-effect, note that as an unmodeled1091 /// side-effect and return false. Otherwise, return true.1092 bool discardCleanups() {1093 for (Cleanup &C : CleanupStack) {1094 if (C.hasSideEffect() && !noteSideEffect()) {1095 CleanupStack.clear();1096 return false;1097 }1098 }1099 CleanupStack.clear();1100 return true;1101 }1102 1103 private:1104 interp::Frame *getCurrentFrame() override { return CurrentCall; }1105 const interp::Frame *getBottomFrame() const override { return &BottomFrame; }1106 1107 bool hasActiveDiagnostic() override { return HasActiveDiagnostic; }1108 void setActiveDiagnostic(bool Flag) override { HasActiveDiagnostic = Flag; }1109 1110 void setFoldFailureDiagnostic(bool Flag) override {1111 HasFoldFailureDiagnostic = Flag;1112 }1113 1114 Expr::EvalStatus &getEvalStatus() const override { return EvalStatus; }1115 1116 // If we have a prior diagnostic, it will be noting that the expression1117 // isn't a constant expression. This diagnostic is more important,1118 // unless we require this evaluation to produce a constant expression.1119 //1120 // FIXME: We might want to show both diagnostics to the user in1121 // EvaluationMode::ConstantFold mode.1122 bool hasPriorDiagnostic() override {1123 if (!EvalStatus.Diag->empty()) {1124 switch (EvalMode) {1125 case EvaluationMode::ConstantFold:1126 case EvaluationMode::IgnoreSideEffects:1127 if (!HasFoldFailureDiagnostic)1128 break;1129 // We've already failed to fold something. Keep that diagnostic.1130 [[fallthrough]];1131 case EvaluationMode::ConstantExpression:1132 case EvaluationMode::ConstantExpressionUnevaluated:1133 setActiveDiagnostic(false);1134 return true;1135 }1136 }1137 return false;1138 }1139 1140 unsigned getCallStackDepth() override { return CallStackDepth; }1141 1142 public:1143 /// Should we continue evaluation after encountering a side-effect that we1144 /// couldn't model?1145 bool keepEvaluatingAfterSideEffect() const override {1146 switch (EvalMode) {1147 case EvaluationMode::IgnoreSideEffects:1148 return true;1149 1150 case EvaluationMode::ConstantExpression:1151 case EvaluationMode::ConstantExpressionUnevaluated:1152 case EvaluationMode::ConstantFold:1153 // By default, assume any side effect might be valid in some other1154 // evaluation of this expression from a different context.1155 return checkingPotentialConstantExpression() ||1156 checkingForUndefinedBehavior();1157 }1158 llvm_unreachable("Missed EvalMode case");1159 }1160 1161 /// Note that we have had a side-effect, and determine whether we should1162 /// keep evaluating.1163 bool noteSideEffect() override {1164 EvalStatus.HasSideEffects = true;1165 return keepEvaluatingAfterSideEffect();1166 }1167 1168 /// Should we continue evaluation after encountering undefined behavior?1169 bool keepEvaluatingAfterUndefinedBehavior() {1170 switch (EvalMode) {1171 case EvaluationMode::IgnoreSideEffects:1172 case EvaluationMode::ConstantFold:1173 return true;1174 1175 case EvaluationMode::ConstantExpression:1176 case EvaluationMode::ConstantExpressionUnevaluated:1177 return checkingForUndefinedBehavior();1178 }1179 llvm_unreachable("Missed EvalMode case");1180 }1181 1182 /// Note that we hit something that was technically undefined behavior, but1183 /// that we can evaluate past it (such as signed overflow or floating-point1184 /// division by zero.)1185 bool noteUndefinedBehavior() override {1186 EvalStatus.HasUndefinedBehavior = true;1187 return keepEvaluatingAfterUndefinedBehavior();1188 }1189 1190 /// Should we continue evaluation as much as possible after encountering a1191 /// construct which can't be reduced to a value?1192 bool keepEvaluatingAfterFailure() const override {1193 uint64_t Limit = Ctx.getLangOpts().ConstexprStepLimit;1194 if (Limit != 0 && !StepsLeft)1195 return false;1196 1197 switch (EvalMode) {1198 case EvaluationMode::ConstantExpression:1199 case EvaluationMode::ConstantExpressionUnevaluated:1200 case EvaluationMode::ConstantFold:1201 case EvaluationMode::IgnoreSideEffects:1202 return checkingPotentialConstantExpression() ||1203 checkingForUndefinedBehavior();1204 }1205 llvm_unreachable("Missed EvalMode case");1206 }1207 1208 /// Notes that we failed to evaluate an expression that other expressions1209 /// directly depend on, and determine if we should keep evaluating. This1210 /// should only be called if we actually intend to keep evaluating.1211 ///1212 /// Call noteSideEffect() instead if we may be able to ignore the value that1213 /// we failed to evaluate, e.g. if we failed to evaluate Foo() in:1214 ///1215 /// (Foo(), 1) // use noteSideEffect1216 /// (Foo() || true) // use noteSideEffect1217 /// Foo() + 1 // use noteFailure1218 [[nodiscard]] bool noteFailure() {1219 // Failure when evaluating some expression often means there is some1220 // subexpression whose evaluation was skipped. Therefore, (because we1221 // don't track whether we skipped an expression when unwinding after an1222 // evaluation failure) every evaluation failure that bubbles up from a1223 // subexpression implies that a side-effect has potentially happened. We1224 // skip setting the HasSideEffects flag to true until we decide to1225 // continue evaluating after that point, which happens here.1226 bool KeepGoing = keepEvaluatingAfterFailure();1227 EvalStatus.HasSideEffects |= KeepGoing;1228 return KeepGoing;1229 }1230 1231 class ArrayInitLoopIndex {1232 EvalInfo &Info;1233 uint64_t OuterIndex;1234 1235 public:1236 ArrayInitLoopIndex(EvalInfo &Info)1237 : Info(Info), OuterIndex(Info.ArrayInitIndex) {1238 Info.ArrayInitIndex = 0;1239 }1240 ~ArrayInitLoopIndex() { Info.ArrayInitIndex = OuterIndex; }1241 1242 operator uint64_t&() { return Info.ArrayInitIndex; }1243 };1244 };1245 1246 /// Object used to treat all foldable expressions as constant expressions.1247 struct FoldConstant {1248 EvalInfo &Info;1249 bool Enabled;1250 bool HadNoPriorDiags;1251 EvaluationMode OldMode;1252 1253 explicit FoldConstant(EvalInfo &Info, bool Enabled)1254 : Info(Info),1255 Enabled(Enabled),1256 HadNoPriorDiags(Info.EvalStatus.Diag &&1257 Info.EvalStatus.Diag->empty() &&1258 !Info.EvalStatus.HasSideEffects),1259 OldMode(Info.EvalMode) {1260 if (Enabled)1261 Info.EvalMode = EvaluationMode::ConstantFold;1262 }1263 void keepDiagnostics() { Enabled = false; }1264 ~FoldConstant() {1265 if (Enabled && HadNoPriorDiags && !Info.EvalStatus.Diag->empty() &&1266 !Info.EvalStatus.HasSideEffects)1267 Info.EvalStatus.Diag->clear();1268 Info.EvalMode = OldMode;1269 }1270 };1271 1272 /// RAII object used to set the current evaluation mode to ignore1273 /// side-effects.1274 struct IgnoreSideEffectsRAII {1275 EvalInfo &Info;1276 EvaluationMode OldMode;1277 explicit IgnoreSideEffectsRAII(EvalInfo &Info)1278 : Info(Info), OldMode(Info.EvalMode) {1279 Info.EvalMode = EvaluationMode::IgnoreSideEffects;1280 }1281 1282 ~IgnoreSideEffectsRAII() { Info.EvalMode = OldMode; }1283 };1284 1285 /// RAII object used to optionally suppress diagnostics and side-effects from1286 /// a speculative evaluation.1287 class SpeculativeEvaluationRAII {1288 EvalInfo *Info = nullptr;1289 Expr::EvalStatus OldStatus;1290 unsigned OldSpeculativeEvaluationDepth = 0;1291 1292 void moveFromAndCancel(SpeculativeEvaluationRAII &&Other) {1293 Info = Other.Info;1294 OldStatus = Other.OldStatus;1295 OldSpeculativeEvaluationDepth = Other.OldSpeculativeEvaluationDepth;1296 Other.Info = nullptr;1297 }1298 1299 void maybeRestoreState() {1300 if (!Info)1301 return;1302 1303 Info->EvalStatus = OldStatus;1304 Info->SpeculativeEvaluationDepth = OldSpeculativeEvaluationDepth;1305 }1306 1307 public:1308 SpeculativeEvaluationRAII() = default;1309 1310 SpeculativeEvaluationRAII(1311 EvalInfo &Info, SmallVectorImpl<PartialDiagnosticAt> *NewDiag = nullptr)1312 : Info(&Info), OldStatus(Info.EvalStatus),1313 OldSpeculativeEvaluationDepth(Info.SpeculativeEvaluationDepth) {1314 Info.EvalStatus.Diag = NewDiag;1315 Info.SpeculativeEvaluationDepth = Info.CallStackDepth + 1;1316 }1317 1318 SpeculativeEvaluationRAII(const SpeculativeEvaluationRAII &Other) = delete;1319 SpeculativeEvaluationRAII(SpeculativeEvaluationRAII &&Other) {1320 moveFromAndCancel(std::move(Other));1321 }1322 1323 SpeculativeEvaluationRAII &operator=(SpeculativeEvaluationRAII &&Other) {1324 maybeRestoreState();1325 moveFromAndCancel(std::move(Other));1326 return *this;1327 }1328 1329 ~SpeculativeEvaluationRAII() { maybeRestoreState(); }1330 };1331 1332 /// RAII object wrapping a full-expression or block scope, and handling1333 /// the ending of the lifetime of temporaries created within it.1334 template<ScopeKind Kind>1335 class ScopeRAII {1336 EvalInfo &Info;1337 unsigned OldStackSize;1338 public:1339 ScopeRAII(EvalInfo &Info)1340 : Info(Info), OldStackSize(Info.CleanupStack.size()) {1341 // Push a new temporary version. This is needed to distinguish between1342 // temporaries created in different iterations of a loop.1343 Info.CurrentCall->pushTempVersion();1344 }1345 bool destroy(bool RunDestructors = true) {1346 bool OK = cleanup(Info, RunDestructors, OldStackSize);1347 OldStackSize = std::numeric_limits<unsigned>::max();1348 return OK;1349 }1350 ~ScopeRAII() {1351 if (OldStackSize != std::numeric_limits<unsigned>::max())1352 destroy(false);1353 // Body moved to a static method to encourage the compiler to inline away1354 // instances of this class.1355 Info.CurrentCall->popTempVersion();1356 }1357 private:1358 static bool cleanup(EvalInfo &Info, bool RunDestructors,1359 unsigned OldStackSize) {1360 assert(OldStackSize <= Info.CleanupStack.size() &&1361 "running cleanups out of order?");1362 1363 // Run all cleanups for a block scope, and non-lifetime-extended cleanups1364 // for a full-expression scope.1365 bool Success = true;1366 for (unsigned I = Info.CleanupStack.size(); I > OldStackSize; --I) {1367 if (Info.CleanupStack[I - 1].isDestroyedAtEndOf(Kind)) {1368 if (!Info.CleanupStack[I - 1].endLifetime(Info, RunDestructors)) {1369 Success = false;1370 break;1371 }1372 }1373 }1374 1375 // Compact any retained cleanups.1376 auto NewEnd = Info.CleanupStack.begin() + OldStackSize;1377 if (Kind != ScopeKind::Block)1378 NewEnd =1379 std::remove_if(NewEnd, Info.CleanupStack.end(), [](Cleanup &C) {1380 return C.isDestroyedAtEndOf(Kind);1381 });1382 Info.CleanupStack.erase(NewEnd, Info.CleanupStack.end());1383 return Success;1384 }1385 };1386 typedef ScopeRAII<ScopeKind::Block> BlockScopeRAII;1387 typedef ScopeRAII<ScopeKind::FullExpression> FullExpressionRAII;1388 typedef ScopeRAII<ScopeKind::Call> CallScopeRAII;1389}1390 1391bool SubobjectDesignator::checkSubobject(EvalInfo &Info, const Expr *E,1392 CheckSubobjectKind CSK) {1393 if (Invalid)1394 return false;1395 if (isOnePastTheEnd()) {1396 Info.CCEDiag(E, diag::note_constexpr_past_end_subobject)1397 << CSK;1398 setInvalid();1399 return false;1400 }1401 // Note, we do not diagnose if isMostDerivedAnUnsizedArray(), because there1402 // must actually be at least one array element; even a VLA cannot have a1403 // bound of zero. And if our index is nonzero, we already had a CCEDiag.1404 return true;1405}1406 1407void SubobjectDesignator::diagnoseUnsizedArrayPointerArithmetic(EvalInfo &Info,1408 const Expr *E) {1409 Info.CCEDiag(E, diag::note_constexpr_unsized_array_indexed);1410 // Do not set the designator as invalid: we can represent this situation,1411 // and correct handling of __builtin_object_size requires us to do so.1412}1413 1414void SubobjectDesignator::diagnosePointerArithmetic(EvalInfo &Info,1415 const Expr *E,1416 const APSInt &N) {1417 // If we're complaining, we must be able to statically determine the size of1418 // the most derived array.1419 if (MostDerivedPathLength == Entries.size() && MostDerivedIsArrayElement)1420 Info.CCEDiag(E, diag::note_constexpr_array_index)1421 << N << /*array*/ 01422 << static_cast<unsigned>(getMostDerivedArraySize());1423 else1424 Info.CCEDiag(E, diag::note_constexpr_array_index)1425 << N << /*non-array*/ 1;1426 setInvalid();1427}1428 1429CallStackFrame::CallStackFrame(EvalInfo &Info, SourceRange CallRange,1430 const FunctionDecl *Callee, const LValue *This,1431 const Expr *CallExpr, CallRef Call)1432 : Info(Info), Caller(Info.CurrentCall), Callee(Callee), This(This),1433 CallExpr(CallExpr), Arguments(Call), CallRange(CallRange),1434 Index(Info.NextCallIndex++) {1435 Info.CurrentCall = this;1436 ++Info.CallStackDepth;1437}1438 1439CallStackFrame::~CallStackFrame() {1440 assert(Info.CurrentCall == this && "calls retired out of order");1441 --Info.CallStackDepth;1442 Info.CurrentCall = Caller;1443}1444 1445static bool isRead(AccessKinds AK) {1446 return AK == AK_Read || AK == AK_ReadObjectRepresentation ||1447 AK == AK_IsWithinLifetime || AK == AK_Dereference;1448}1449 1450static bool isModification(AccessKinds AK) {1451 switch (AK) {1452 case AK_Read:1453 case AK_ReadObjectRepresentation:1454 case AK_MemberCall:1455 case AK_DynamicCast:1456 case AK_TypeId:1457 case AK_IsWithinLifetime:1458 case AK_Dereference:1459 return false;1460 case AK_Assign:1461 case AK_Increment:1462 case AK_Decrement:1463 case AK_Construct:1464 case AK_Destroy:1465 return true;1466 }1467 llvm_unreachable("unknown access kind");1468}1469 1470static bool isAnyAccess(AccessKinds AK) {1471 return isRead(AK) || isModification(AK);1472}1473 1474/// Is this an access per the C++ definition?1475static bool isFormalAccess(AccessKinds AK) {1476 return isAnyAccess(AK) && AK != AK_Construct && AK != AK_Destroy &&1477 AK != AK_IsWithinLifetime && AK != AK_Dereference;1478}1479 1480/// Is this kind of access valid on an indeterminate object value?1481static bool isValidIndeterminateAccess(AccessKinds AK) {1482 switch (AK) {1483 case AK_Read:1484 case AK_Increment:1485 case AK_Decrement:1486 case AK_Dereference:1487 // These need the object's value.1488 return false;1489 1490 case AK_IsWithinLifetime:1491 case AK_ReadObjectRepresentation:1492 case AK_Assign:1493 case AK_Construct:1494 case AK_Destroy:1495 // Construction and destruction don't need the value.1496 return true;1497 1498 case AK_MemberCall:1499 case AK_DynamicCast:1500 case AK_TypeId:1501 // These aren't really meaningful on scalars.1502 return true;1503 }1504 llvm_unreachable("unknown access kind");1505}1506 1507namespace {1508 struct ComplexValue {1509 private:1510 bool IsInt;1511 1512 public:1513 APSInt IntReal, IntImag;1514 APFloat FloatReal, FloatImag;1515 1516 ComplexValue() : FloatReal(APFloat::Bogus()), FloatImag(APFloat::Bogus()) {}1517 1518 void makeComplexFloat() { IsInt = false; }1519 bool isComplexFloat() const { return !IsInt; }1520 APFloat &getComplexFloatReal() { return FloatReal; }1521 APFloat &getComplexFloatImag() { return FloatImag; }1522 1523 void makeComplexInt() { IsInt = true; }1524 bool isComplexInt() const { return IsInt; }1525 APSInt &getComplexIntReal() { return IntReal; }1526 APSInt &getComplexIntImag() { return IntImag; }1527 1528 void moveInto(APValue &v) const {1529 if (isComplexFloat())1530 v = APValue(FloatReal, FloatImag);1531 else1532 v = APValue(IntReal, IntImag);1533 }1534 void setFrom(const APValue &v) {1535 assert(v.isComplexFloat() || v.isComplexInt());1536 if (v.isComplexFloat()) {1537 makeComplexFloat();1538 FloatReal = v.getComplexFloatReal();1539 FloatImag = v.getComplexFloatImag();1540 } else {1541 makeComplexInt();1542 IntReal = v.getComplexIntReal();1543 IntImag = v.getComplexIntImag();1544 }1545 }1546 };1547 1548 struct LValue {1549 APValue::LValueBase Base;1550 CharUnits Offset;1551 SubobjectDesignator Designator;1552 bool IsNullPtr : 1;1553 bool InvalidBase : 1;1554 // P2280R4 track if we have an unknown reference or pointer.1555 bool AllowConstexprUnknown = false;1556 1557 const APValue::LValueBase getLValueBase() const { return Base; }1558 bool allowConstexprUnknown() const { return AllowConstexprUnknown; }1559 CharUnits &getLValueOffset() { return Offset; }1560 const CharUnits &getLValueOffset() const { return Offset; }1561 SubobjectDesignator &getLValueDesignator() { return Designator; }1562 const SubobjectDesignator &getLValueDesignator() const { return Designator;}1563 bool isNullPointer() const { return IsNullPtr;}1564 1565 unsigned getLValueCallIndex() const { return Base.getCallIndex(); }1566 unsigned getLValueVersion() const { return Base.getVersion(); }1567 1568 void moveInto(APValue &V) const {1569 if (Designator.Invalid)1570 V = APValue(Base, Offset, APValue::NoLValuePath(), IsNullPtr);1571 else {1572 assert(!InvalidBase && "APValues can't handle invalid LValue bases");1573 V = APValue(Base, Offset, Designator.Entries,1574 Designator.IsOnePastTheEnd, IsNullPtr);1575 }1576 if (AllowConstexprUnknown)1577 V.setConstexprUnknown();1578 }1579 void setFrom(const ASTContext &Ctx, const APValue &V) {1580 assert(V.isLValue() && "Setting LValue from a non-LValue?");1581 Base = V.getLValueBase();1582 Offset = V.getLValueOffset();1583 InvalidBase = false;1584 Designator = SubobjectDesignator(Ctx, V);1585 IsNullPtr = V.isNullPointer();1586 AllowConstexprUnknown = V.allowConstexprUnknown();1587 }1588 1589 void set(APValue::LValueBase B, bool BInvalid = false) {1590#ifndef NDEBUG1591 // We only allow a few types of invalid bases. Enforce that here.1592 if (BInvalid) {1593 const auto *E = B.get<const Expr *>();1594 assert((isa<MemberExpr>(E) || tryUnwrapAllocSizeCall(E)) &&1595 "Unexpected type of invalid base");1596 }1597#endif1598 1599 Base = B;1600 Offset = CharUnits::fromQuantity(0);1601 InvalidBase = BInvalid;1602 Designator = SubobjectDesignator(getType(B));1603 IsNullPtr = false;1604 AllowConstexprUnknown = false;1605 }1606 1607 void setNull(ASTContext &Ctx, QualType PointerTy) {1608 Base = (const ValueDecl *)nullptr;1609 Offset =1610 CharUnits::fromQuantity(Ctx.getTargetNullPointerValue(PointerTy));1611 InvalidBase = false;1612 Designator = SubobjectDesignator(PointerTy->getPointeeType());1613 IsNullPtr = true;1614 AllowConstexprUnknown = false;1615 }1616 1617 void setInvalid(APValue::LValueBase B, unsigned I = 0) {1618 set(B, true);1619 }1620 1621 std::string toString(ASTContext &Ctx, QualType T) const {1622 APValue Printable;1623 moveInto(Printable);1624 return Printable.getAsString(Ctx, T);1625 }1626 1627 private:1628 // Check that this LValue is not based on a null pointer. If it is, produce1629 // a diagnostic and mark the designator as invalid.1630 template <typename GenDiagType>1631 bool checkNullPointerDiagnosingWith(const GenDiagType &GenDiag) {1632 if (Designator.Invalid)1633 return false;1634 if (IsNullPtr) {1635 GenDiag();1636 Designator.setInvalid();1637 return false;1638 }1639 return true;1640 }1641 1642 public:1643 bool checkNullPointer(EvalInfo &Info, const Expr *E,1644 CheckSubobjectKind CSK) {1645 return checkNullPointerDiagnosingWith([&Info, E, CSK] {1646 Info.CCEDiag(E, diag::note_constexpr_null_subobject) << CSK;1647 });1648 }1649 1650 bool checkNullPointerForFoldAccess(EvalInfo &Info, const Expr *E,1651 AccessKinds AK) {1652 return checkNullPointerDiagnosingWith([&Info, E, AK] {1653 if (AK == AccessKinds::AK_Dereference)1654 Info.FFDiag(E, diag::note_constexpr_dereferencing_null);1655 else1656 Info.FFDiag(E, diag::note_constexpr_access_null) << AK;1657 });1658 }1659 1660 // Check this LValue refers to an object. If not, set the designator to be1661 // invalid and emit a diagnostic.1662 bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK) {1663 return (CSK == CSK_ArrayToPointer || checkNullPointer(Info, E, CSK)) &&1664 Designator.checkSubobject(Info, E, CSK);1665 }1666 1667 void addDecl(EvalInfo &Info, const Expr *E,1668 const Decl *D, bool Virtual = false) {1669 if (checkSubobject(Info, E, isa<FieldDecl>(D) ? CSK_Field : CSK_Base))1670 Designator.addDeclUnchecked(D, Virtual);1671 }1672 void addUnsizedArray(EvalInfo &Info, const Expr *E, QualType ElemTy) {1673 if (!Designator.Entries.empty()) {1674 Info.CCEDiag(E, diag::note_constexpr_unsupported_unsized_array);1675 Designator.setInvalid();1676 return;1677 }1678 if (checkSubobject(Info, E, CSK_ArrayToPointer)) {1679 assert(getType(Base).getNonReferenceType()->isPointerType() ||1680 getType(Base).getNonReferenceType()->isArrayType());1681 Designator.FirstEntryIsAnUnsizedArray = true;1682 Designator.addUnsizedArrayUnchecked(ElemTy);1683 }1684 }1685 void addArray(EvalInfo &Info, const Expr *E, const ConstantArrayType *CAT) {1686 if (checkSubobject(Info, E, CSK_ArrayToPointer))1687 Designator.addArrayUnchecked(CAT);1688 }1689 void addComplex(EvalInfo &Info, const Expr *E, QualType EltTy, bool Imag) {1690 if (checkSubobject(Info, E, Imag ? CSK_Imag : CSK_Real))1691 Designator.addComplexUnchecked(EltTy, Imag);1692 }1693 void addVectorElement(EvalInfo &Info, const Expr *E, QualType EltTy,1694 uint64_t Size, uint64_t Idx) {1695 if (checkSubobject(Info, E, CSK_VectorElement))1696 Designator.addVectorElementUnchecked(EltTy, Size, Idx);1697 }1698 void clearIsNullPointer() {1699 IsNullPtr = false;1700 }1701 void adjustOffsetAndIndex(EvalInfo &Info, const Expr *E,1702 const APSInt &Index, CharUnits ElementSize) {1703 // An index of 0 has no effect. (In C, adding 0 to a null pointer is UB,1704 // but we're not required to diagnose it and it's valid in C++.)1705 if (!Index)1706 return;1707 1708 // Compute the new offset in the appropriate width, wrapping at 64 bits.1709 // FIXME: When compiling for a 32-bit target, we should use 32-bit1710 // offsets.1711 uint64_t Offset64 = Offset.getQuantity();1712 uint64_t ElemSize64 = ElementSize.getQuantity();1713 uint64_t Index64 = Index.extOrTrunc(64).getZExtValue();1714 Offset = CharUnits::fromQuantity(Offset64 + ElemSize64 * Index64);1715 1716 if (checkNullPointer(Info, E, CSK_ArrayIndex))1717 Designator.adjustIndex(Info, E, Index, *this);1718 clearIsNullPointer();1719 }1720 void adjustOffset(CharUnits N) {1721 Offset += N;1722 if (N.getQuantity())1723 clearIsNullPointer();1724 }1725 };1726 1727 struct MemberPtr {1728 MemberPtr() {}1729 explicit MemberPtr(const ValueDecl *Decl)1730 : DeclAndIsDerivedMember(Decl, false) {}1731 1732 /// The member or (direct or indirect) field referred to by this member1733 /// pointer, or 0 if this is a null member pointer.1734 const ValueDecl *getDecl() const {1735 return DeclAndIsDerivedMember.getPointer();1736 }1737 /// Is this actually a member of some type derived from the relevant class?1738 bool isDerivedMember() const {1739 return DeclAndIsDerivedMember.getInt();1740 }1741 /// Get the class which the declaration actually lives in.1742 const CXXRecordDecl *getContainingRecord() const {1743 return cast<CXXRecordDecl>(1744 DeclAndIsDerivedMember.getPointer()->getDeclContext());1745 }1746 1747 void moveInto(APValue &V) const {1748 V = APValue(getDecl(), isDerivedMember(), Path);1749 }1750 void setFrom(const APValue &V) {1751 assert(V.isMemberPointer());1752 DeclAndIsDerivedMember.setPointer(V.getMemberPointerDecl());1753 DeclAndIsDerivedMember.setInt(V.isMemberPointerToDerivedMember());1754 Path.clear();1755 llvm::append_range(Path, V.getMemberPointerPath());1756 }1757 1758 /// DeclAndIsDerivedMember - The member declaration, and a flag indicating1759 /// whether the member is a member of some class derived from the class type1760 /// of the member pointer.1761 llvm::PointerIntPair<const ValueDecl*, 1, bool> DeclAndIsDerivedMember;1762 /// Path - The path of base/derived classes from the member declaration's1763 /// class (exclusive) to the class type of the member pointer (inclusive).1764 SmallVector<const CXXRecordDecl*, 4> Path;1765 1766 /// Perform a cast towards the class of the Decl (either up or down the1767 /// hierarchy).1768 bool castBack(const CXXRecordDecl *Class) {1769 assert(!Path.empty());1770 const CXXRecordDecl *Expected;1771 if (Path.size() >= 2)1772 Expected = Path[Path.size() - 2];1773 else1774 Expected = getContainingRecord();1775 if (Expected->getCanonicalDecl() != Class->getCanonicalDecl()) {1776 // C++11 [expr.static.cast]p12: In a conversion from (D::*) to (B::*),1777 // if B does not contain the original member and is not a base or1778 // derived class of the class containing the original member, the result1779 // of the cast is undefined.1780 // C++11 [conv.mem]p2 does not cover this case for a cast from (B::*) to1781 // (D::*). We consider that to be a language defect.1782 return false;1783 }1784 Path.pop_back();1785 return true;1786 }1787 /// Perform a base-to-derived member pointer cast.1788 bool castToDerived(const CXXRecordDecl *Derived) {1789 if (!getDecl())1790 return true;1791 if (!isDerivedMember()) {1792 Path.push_back(Derived);1793 return true;1794 }1795 if (!castBack(Derived))1796 return false;1797 if (Path.empty())1798 DeclAndIsDerivedMember.setInt(false);1799 return true;1800 }1801 /// Perform a derived-to-base member pointer cast.1802 bool castToBase(const CXXRecordDecl *Base) {1803 if (!getDecl())1804 return true;1805 if (Path.empty())1806 DeclAndIsDerivedMember.setInt(true);1807 if (isDerivedMember()) {1808 Path.push_back(Base);1809 return true;1810 }1811 return castBack(Base);1812 }1813 };1814 1815 /// Compare two member pointers, which are assumed to be of the same type.1816 static bool operator==(const MemberPtr &LHS, const MemberPtr &RHS) {1817 if (!LHS.getDecl() || !RHS.getDecl())1818 return !LHS.getDecl() && !RHS.getDecl();1819 if (LHS.getDecl()->getCanonicalDecl() != RHS.getDecl()->getCanonicalDecl())1820 return false;1821 return LHS.Path == RHS.Path;1822 }1823}1824 1825void SubobjectDesignator::adjustIndex(EvalInfo &Info, const Expr *E, APSInt N,1826 const LValue &LV) {1827 if (Invalid || !N)1828 return;1829 uint64_t TruncatedN = N.extOrTrunc(64).getZExtValue();1830 if (isMostDerivedAnUnsizedArray()) {1831 diagnoseUnsizedArrayPointerArithmetic(Info, E);1832 // Can't verify -- trust that the user is doing the right thing (or if1833 // not, trust that the caller will catch the bad behavior).1834 // FIXME: Should we reject if this overflows, at least?1835 Entries.back() =1836 PathEntry::ArrayIndex(Entries.back().getAsArrayIndex() + TruncatedN);1837 return;1838 }1839 1840 // [expr.add]p4: For the purposes of these operators, a pointer to a1841 // nonarray object behaves the same as a pointer to the first element of1842 // an array of length one with the type of the object as its element type.1843 bool IsArray =1844 MostDerivedPathLength == Entries.size() && MostDerivedIsArrayElement;1845 uint64_t ArrayIndex =1846 IsArray ? Entries.back().getAsArrayIndex() : (uint64_t)IsOnePastTheEnd;1847 uint64_t ArraySize = IsArray ? getMostDerivedArraySize() : (uint64_t)1;1848 1849 if (N < -(int64_t)ArrayIndex || N > ArraySize - ArrayIndex) {1850 if (!Info.checkingPotentialConstantExpression() ||1851 !LV.AllowConstexprUnknown) {1852 // Calculate the actual index in a wide enough type, so we can include1853 // it in the note.1854 N = N.extend(std::max<unsigned>(N.getBitWidth() + 1, 65));1855 (llvm::APInt &)N += ArrayIndex;1856 assert(N.ugt(ArraySize) && "bounds check failed for in-bounds index");1857 diagnosePointerArithmetic(Info, E, N);1858 }1859 setInvalid();1860 return;1861 }1862 1863 ArrayIndex += TruncatedN;1864 assert(ArrayIndex <= ArraySize &&1865 "bounds check succeeded for out-of-bounds index");1866 1867 if (IsArray)1868 Entries.back() = PathEntry::ArrayIndex(ArrayIndex);1869 else1870 IsOnePastTheEnd = (ArrayIndex != 0);1871}1872 1873static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E);1874static bool EvaluateInPlace(APValue &Result, EvalInfo &Info,1875 const LValue &This, const Expr *E,1876 bool AllowNonLiteralTypes = false);1877static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info,1878 bool InvalidBaseOK = false);1879static bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info,1880 bool InvalidBaseOK = false);1881static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result,1882 EvalInfo &Info);1883static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info);1884static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info);1885static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result,1886 EvalInfo &Info);1887static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);1888static bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info);1889static bool EvaluateAtomic(const Expr *E, const LValue *This, APValue &Result,1890 EvalInfo &Info);1891static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result);1892static bool EvaluateBuiltinStrLen(const Expr *E, uint64_t &Result,1893 EvalInfo &Info,1894 std::string *StringResult = nullptr);1895 1896/// Evaluate an integer or fixed point expression into an APResult.1897static bool EvaluateFixedPointOrInteger(const Expr *E, APFixedPoint &Result,1898 EvalInfo &Info);1899 1900/// Evaluate only a fixed point expression into an APResult.1901static bool EvaluateFixedPoint(const Expr *E, APFixedPoint &Result,1902 EvalInfo &Info);1903 1904//===----------------------------------------------------------------------===//1905// Misc utilities1906//===----------------------------------------------------------------------===//1907 1908/// Negate an APSInt in place, converting it to a signed form if necessary, and1909/// preserving its value (by extending by up to one bit as needed).1910static void negateAsSigned(APSInt &Int) {1911 if (Int.isUnsigned() || Int.isMinSignedValue()) {1912 Int = Int.extend(Int.getBitWidth() + 1);1913 Int.setIsSigned(true);1914 }1915 Int = -Int;1916}1917 1918template<typename KeyT>1919APValue &CallStackFrame::createTemporary(const KeyT *Key, QualType T,1920 ScopeKind Scope, LValue &LV) {1921 unsigned Version = getTempVersion();1922 APValue::LValueBase Base(Key, Index, Version);1923 LV.set(Base);1924 return createLocal(Base, Key, T, Scope);1925}1926 1927/// Allocate storage for a parameter of a function call made in this frame.1928APValue &CallStackFrame::createParam(CallRef Args, const ParmVarDecl *PVD,1929 LValue &LV) {1930 assert(Args.CallIndex == Index && "creating parameter in wrong frame");1931 APValue::LValueBase Base(PVD, Index, Args.Version);1932 LV.set(Base);1933 // We always destroy parameters at the end of the call, even if we'd allow1934 // them to live to the end of the full-expression at runtime, in order to1935 // give portable results and match other compilers.1936 return createLocal(Base, PVD, PVD->getType(), ScopeKind::Call);1937}1938 1939APValue &CallStackFrame::createLocal(APValue::LValueBase Base, const void *Key,1940 QualType T, ScopeKind Scope) {1941 assert(Base.getCallIndex() == Index && "lvalue for wrong frame");1942 unsigned Version = Base.getVersion();1943 APValue &Result = Temporaries[MapKeyTy(Key, Version)];1944 assert(Result.isAbsent() && "local created multiple times");1945 1946 // If we're creating a local immediately in the operand of a speculative1947 // evaluation, don't register a cleanup to be run outside the speculative1948 // evaluation context, since we won't actually be able to initialize this1949 // object.1950 if (Index <= Info.SpeculativeEvaluationDepth) {1951 if (T.isDestructedType())1952 Info.noteSideEffect();1953 } else {1954 Info.CleanupStack.push_back(Cleanup(&Result, Base, T, Scope));1955 }1956 return Result;1957}1958 1959APValue *EvalInfo::createHeapAlloc(const Expr *E, QualType T, LValue &LV) {1960 if (NumHeapAllocs > DynamicAllocLValue::getMaxIndex()) {1961 FFDiag(E, diag::note_constexpr_heap_alloc_limit_exceeded);1962 return nullptr;1963 }1964 1965 DynamicAllocLValue DA(NumHeapAllocs++);1966 LV.set(APValue::LValueBase::getDynamicAlloc(DA, T));1967 auto Result = HeapAllocs.emplace(std::piecewise_construct,1968 std::forward_as_tuple(DA), std::tuple<>());1969 assert(Result.second && "reused a heap alloc index?");1970 Result.first->second.AllocExpr = E;1971 return &Result.first->second.Value;1972}1973 1974/// Produce a string describing the given constexpr call.1975void CallStackFrame::describe(raw_ostream &Out) const {1976 unsigned ArgIndex = 0;1977 bool IsMemberCall =1978 isa<CXXMethodDecl>(Callee) && !isa<CXXConstructorDecl>(Callee) &&1979 cast<CXXMethodDecl>(Callee)->isImplicitObjectMemberFunction();1980 1981 if (!IsMemberCall)1982 Callee->getNameForDiagnostic(Out, Info.Ctx.getPrintingPolicy(),1983 /*Qualified=*/false);1984 1985 if (This && IsMemberCall) {1986 if (const auto *MCE = dyn_cast_if_present<CXXMemberCallExpr>(CallExpr)) {1987 const Expr *Object = MCE->getImplicitObjectArgument();1988 Object->printPretty(Out, /*Helper=*/nullptr, Info.Ctx.getPrintingPolicy(),1989 /*Indentation=*/0);1990 if (Object->getType()->isPointerType())1991 Out << "->";1992 else1993 Out << ".";1994 } else if (const auto *OCE =1995 dyn_cast_if_present<CXXOperatorCallExpr>(CallExpr)) {1996 OCE->getArg(0)->printPretty(Out, /*Helper=*/nullptr,1997 Info.Ctx.getPrintingPolicy(),1998 /*Indentation=*/0);1999 Out << ".";2000 } else {2001 APValue Val;2002 This->moveInto(Val);2003 Val.printPretty(2004 Out, Info.Ctx,2005 Info.Ctx.getLValueReferenceType(This->Designator.MostDerivedType));2006 Out << ".";2007 }2008 Callee->getNameForDiagnostic(Out, Info.Ctx.getPrintingPolicy(),2009 /*Qualified=*/false);2010 IsMemberCall = false;2011 }2012 2013 Out << '(';2014 2015 for (FunctionDecl::param_const_iterator I = Callee->param_begin(),2016 E = Callee->param_end(); I != E; ++I, ++ArgIndex) {2017 if (ArgIndex > (unsigned)IsMemberCall)2018 Out << ", ";2019 2020 const ParmVarDecl *Param = *I;2021 APValue *V = Info.getParamSlot(Arguments, Param);2022 if (V)2023 V->printPretty(Out, Info.Ctx, Param->getType());2024 else2025 Out << "<...>";2026 2027 if (ArgIndex == 0 && IsMemberCall)2028 Out << "->" << *Callee << '(';2029 }2030 2031 Out << ')';2032}2033 2034/// Evaluate an expression to see if it had side-effects, and discard its2035/// result.2036/// \return \c true if the caller should keep evaluating.2037static bool EvaluateIgnoredValue(EvalInfo &Info, const Expr *E) {2038 assert(!E->isValueDependent());2039 APValue Scratch;2040 if (!Evaluate(Scratch, Info, E))2041 // We don't need the value, but we might have skipped a side effect here.2042 return Info.noteSideEffect();2043 return true;2044}2045 2046/// Should this call expression be treated as forming an opaque constant?2047static bool IsOpaqueConstantCall(const CallExpr *E) {2048 unsigned Builtin = E->getBuiltinCallee();2049 return (Builtin == Builtin::BI__builtin___CFStringMakeConstantString ||2050 Builtin == Builtin::BI__builtin___NSStringMakeConstantString ||2051 Builtin == Builtin::BI__builtin_ptrauth_sign_constant ||2052 Builtin == Builtin::BI__builtin_function_start);2053}2054 2055static bool IsOpaqueConstantCall(const LValue &LVal) {2056 const auto *BaseExpr =2057 llvm::dyn_cast_if_present<CallExpr>(LVal.Base.dyn_cast<const Expr *>());2058 return BaseExpr && IsOpaqueConstantCall(BaseExpr);2059}2060 2061static bool IsGlobalLValue(APValue::LValueBase B) {2062 // C++11 [expr.const]p3 An address constant expression is a prvalue core2063 // constant expression of pointer type that evaluates to...2064 2065 // ... a null pointer value, or a prvalue core constant expression of type2066 // std::nullptr_t.2067 if (!B)2068 return true;2069 2070 if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) {2071 // ... the address of an object with static storage duration,2072 if (const VarDecl *VD = dyn_cast<VarDecl>(D))2073 return VD->hasGlobalStorage();2074 if (isa<TemplateParamObjectDecl>(D))2075 return true;2076 // ... the address of a function,2077 // ... the address of a GUID [MS extension],2078 // ... the address of an unnamed global constant2079 return isa<FunctionDecl, MSGuidDecl, UnnamedGlobalConstantDecl>(D);2080 }2081 2082 if (B.is<TypeInfoLValue>() || B.is<DynamicAllocLValue>())2083 return true;2084 2085 const Expr *E = B.get<const Expr*>();2086 switch (E->getStmtClass()) {2087 default:2088 return false;2089 case Expr::CompoundLiteralExprClass: {2090 const CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E);2091 return CLE->isFileScope() && CLE->isLValue();2092 }2093 case Expr::MaterializeTemporaryExprClass:2094 // A materialized temporary might have been lifetime-extended to static2095 // storage duration.2096 return cast<MaterializeTemporaryExpr>(E)->getStorageDuration() == SD_Static;2097 // A string literal has static storage duration.2098 case Expr::StringLiteralClass:2099 case Expr::PredefinedExprClass:2100 case Expr::ObjCStringLiteralClass:2101 case Expr::ObjCEncodeExprClass:2102 return true;2103 case Expr::ObjCBoxedExprClass:2104 return cast<ObjCBoxedExpr>(E)->isExpressibleAsConstantInitializer();2105 case Expr::CallExprClass:2106 return IsOpaqueConstantCall(cast<CallExpr>(E));2107 // For GCC compatibility, &&label has static storage duration.2108 case Expr::AddrLabelExprClass:2109 return true;2110 // A Block literal expression may be used as the initialization value for2111 // Block variables at global or local static scope.2112 case Expr::BlockExprClass:2113 return !cast<BlockExpr>(E)->getBlockDecl()->hasCaptures();2114 // The APValue generated from a __builtin_source_location will be emitted as a2115 // literal.2116 case Expr::SourceLocExprClass:2117 return true;2118 case Expr::ImplicitValueInitExprClass:2119 // FIXME:2120 // We can never form an lvalue with an implicit value initialization as its2121 // base through expression evaluation, so these only appear in one case: the2122 // implicit variable declaration we invent when checking whether a constexpr2123 // constructor can produce a constant expression. We must assume that such2124 // an expression might be a global lvalue.2125 return true;2126 }2127}2128 2129static const ValueDecl *GetLValueBaseDecl(const LValue &LVal) {2130 return LVal.Base.dyn_cast<const ValueDecl*>();2131}2132 2133// Information about an LValueBase that is some kind of string.2134struct LValueBaseString {2135 std::string ObjCEncodeStorage;2136 StringRef Bytes;2137 int CharWidth;2138};2139 2140// Gets the lvalue base of LVal as a string.2141static bool GetLValueBaseAsString(const EvalInfo &Info, const LValue &LVal,2142 LValueBaseString &AsString) {2143 const auto *BaseExpr = LVal.Base.dyn_cast<const Expr *>();2144 if (!BaseExpr)2145 return false;2146 2147 // For ObjCEncodeExpr, we need to compute and store the string.2148 if (const auto *EE = dyn_cast<ObjCEncodeExpr>(BaseExpr)) {2149 Info.Ctx.getObjCEncodingForType(EE->getEncodedType(),2150 AsString.ObjCEncodeStorage);2151 AsString.Bytes = AsString.ObjCEncodeStorage;2152 AsString.CharWidth = 1;2153 return true;2154 }2155 2156 // Otherwise, we have a StringLiteral.2157 const auto *Lit = dyn_cast<StringLiteral>(BaseExpr);2158 if (const auto *PE = dyn_cast<PredefinedExpr>(BaseExpr))2159 Lit = PE->getFunctionName();2160 2161 if (!Lit)2162 return false;2163 2164 AsString.Bytes = Lit->getBytes();2165 AsString.CharWidth = Lit->getCharByteWidth();2166 return true;2167}2168 2169// Determine whether two string literals potentially overlap. This will be the2170// case if they agree on the values of all the bytes on the overlapping region2171// between them.2172//2173// The overlapping region is the portion of the two string literals that must2174// overlap in memory if the pointers actually point to the same address at2175// runtime. For example, if LHS is "abcdef" + 3 and RHS is "cdef\0gh" + 1 then2176// the overlapping region is "cdef\0", which in this case does agree, so the2177// strings are potentially overlapping. Conversely, for "foobar" + 3 versus2178// "bazbar" + 3, the overlapping region contains all of both strings, so they2179// are not potentially overlapping, even though they agree from the given2180// addresses onwards.2181//2182// See open core issue CWG2765 which is discussing the desired rule here.2183static bool ArePotentiallyOverlappingStringLiterals(const EvalInfo &Info,2184 const LValue &LHS,2185 const LValue &RHS) {2186 LValueBaseString LHSString, RHSString;2187 if (!GetLValueBaseAsString(Info, LHS, LHSString) ||2188 !GetLValueBaseAsString(Info, RHS, RHSString))2189 return false;2190 2191 // This is the byte offset to the location of the first character of LHS2192 // within RHS. We don't need to look at the characters of one string that2193 // would appear before the start of the other string if they were merged.2194 CharUnits Offset = RHS.Offset - LHS.Offset;2195 if (Offset.isNegative()) {2196 if (LHSString.Bytes.size() < (size_t)-Offset.getQuantity())2197 return false;2198 LHSString.Bytes = LHSString.Bytes.drop_front(-Offset.getQuantity());2199 } else {2200 if (RHSString.Bytes.size() < (size_t)Offset.getQuantity())2201 return false;2202 RHSString.Bytes = RHSString.Bytes.drop_front(Offset.getQuantity());2203 }2204 2205 bool LHSIsLonger = LHSString.Bytes.size() > RHSString.Bytes.size();2206 StringRef Longer = LHSIsLonger ? LHSString.Bytes : RHSString.Bytes;2207 StringRef Shorter = LHSIsLonger ? RHSString.Bytes : LHSString.Bytes;2208 int ShorterCharWidth = (LHSIsLonger ? RHSString : LHSString).CharWidth;2209 2210 // The null terminator isn't included in the string data, so check for it2211 // manually. If the longer string doesn't have a null terminator where the2212 // shorter string ends, they aren't potentially overlapping.2213 for (int NullByte : llvm::seq(ShorterCharWidth)) {2214 if (Shorter.size() + NullByte >= Longer.size())2215 break;2216 if (Longer[Shorter.size() + NullByte])2217 return false;2218 }2219 2220 // Otherwise, they're potentially overlapping if and only if the overlapping2221 // region is the same.2222 return Shorter == Longer.take_front(Shorter.size());2223}2224 2225static bool IsWeakLValue(const LValue &Value) {2226 const ValueDecl *Decl = GetLValueBaseDecl(Value);2227 return Decl && Decl->isWeak();2228}2229 2230static bool isZeroSized(const LValue &Value) {2231 const ValueDecl *Decl = GetLValueBaseDecl(Value);2232 if (isa_and_nonnull<VarDecl>(Decl)) {2233 QualType Ty = Decl->getType();2234 if (Ty->isArrayType())2235 return Ty->isIncompleteType() ||2236 Decl->getASTContext().getTypeSize(Ty) == 0;2237 }2238 return false;2239}2240 2241static bool HasSameBase(const LValue &A, const LValue &B) {2242 if (!A.getLValueBase())2243 return !B.getLValueBase();2244 if (!B.getLValueBase())2245 return false;2246 2247 if (A.getLValueBase().getOpaqueValue() !=2248 B.getLValueBase().getOpaqueValue())2249 return false;2250 2251 return A.getLValueCallIndex() == B.getLValueCallIndex() &&2252 A.getLValueVersion() == B.getLValueVersion();2253}2254 2255static void NoteLValueLocation(EvalInfo &Info, APValue::LValueBase Base) {2256 assert(Base && "no location for a null lvalue");2257 const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>();2258 2259 // For a parameter, find the corresponding call stack frame (if it still2260 // exists), and point at the parameter of the function definition we actually2261 // invoked.2262 if (auto *PVD = dyn_cast_or_null<ParmVarDecl>(VD)) {2263 unsigned Idx = PVD->getFunctionScopeIndex();2264 for (CallStackFrame *F = Info.CurrentCall; F; F = F->Caller) {2265 if (F->Arguments.CallIndex == Base.getCallIndex() &&2266 F->Arguments.Version == Base.getVersion() && F->Callee &&2267 Idx < F->Callee->getNumParams()) {2268 VD = F->Callee->getParamDecl(Idx);2269 break;2270 }2271 }2272 }2273 2274 if (VD)2275 Info.Note(VD->getLocation(), diag::note_declared_at);2276 else if (const Expr *E = Base.dyn_cast<const Expr*>())2277 Info.Note(E->getExprLoc(), diag::note_constexpr_temporary_here);2278 else if (DynamicAllocLValue DA = Base.dyn_cast<DynamicAllocLValue>()) {2279 // FIXME: Produce a note for dangling pointers too.2280 if (std::optional<DynAlloc *> Alloc = Info.lookupDynamicAlloc(DA))2281 Info.Note((*Alloc)->AllocExpr->getExprLoc(),2282 diag::note_constexpr_dynamic_alloc_here);2283 }2284 2285 // We have no information to show for a typeid(T) object.2286}2287 2288enum class CheckEvaluationResultKind {2289 ConstantExpression,2290 FullyInitialized,2291};2292 2293/// Materialized temporaries that we've already checked to determine if they're2294/// initializsed by a constant expression.2295using CheckedTemporaries =2296 llvm::SmallPtrSet<const MaterializeTemporaryExpr *, 8>;2297 2298static bool CheckEvaluationResult(CheckEvaluationResultKind CERK,2299 EvalInfo &Info, SourceLocation DiagLoc,2300 QualType Type, const APValue &Value,2301 ConstantExprKind Kind,2302 const FieldDecl *SubobjectDecl,2303 CheckedTemporaries &CheckedTemps);2304 2305/// Check that this reference or pointer core constant expression is a valid2306/// value for an address or reference constant expression. Return true if we2307/// can fold this expression, whether or not it's a constant expression.2308static bool CheckLValueConstantExpression(EvalInfo &Info, SourceLocation Loc,2309 QualType Type, const LValue &LVal,2310 ConstantExprKind Kind,2311 CheckedTemporaries &CheckedTemps) {2312 bool IsReferenceType = Type->isReferenceType();2313 2314 APValue::LValueBase Base = LVal.getLValueBase();2315 const SubobjectDesignator &Designator = LVal.getLValueDesignator();2316 2317 const Expr *BaseE = Base.dyn_cast<const Expr *>();2318 const ValueDecl *BaseVD = Base.dyn_cast<const ValueDecl*>();2319 2320 // Additional restrictions apply in a template argument. We only enforce the2321 // C++20 restrictions here; additional syntactic and semantic restrictions2322 // are applied elsewhere.2323 if (isTemplateArgument(Kind)) {2324 int InvalidBaseKind = -1;2325 StringRef Ident;2326 if (Base.is<TypeInfoLValue>())2327 InvalidBaseKind = 0;2328 else if (isa_and_nonnull<StringLiteral>(BaseE))2329 InvalidBaseKind = 1;2330 else if (isa_and_nonnull<MaterializeTemporaryExpr>(BaseE) ||2331 isa_and_nonnull<LifetimeExtendedTemporaryDecl>(BaseVD))2332 InvalidBaseKind = 2;2333 else if (auto *PE = dyn_cast_or_null<PredefinedExpr>(BaseE)) {2334 InvalidBaseKind = 3;2335 Ident = PE->getIdentKindName();2336 }2337 2338 if (InvalidBaseKind != -1) {2339 Info.FFDiag(Loc, diag::note_constexpr_invalid_template_arg)2340 << IsReferenceType << !Designator.Entries.empty() << InvalidBaseKind2341 << Ident;2342 return false;2343 }2344 }2345 2346 if (auto *FD = dyn_cast_or_null<FunctionDecl>(BaseVD);2347 FD && FD->isImmediateFunction()) {2348 Info.FFDiag(Loc, diag::note_consteval_address_accessible)2349 << !Type->isAnyPointerType();2350 Info.Note(FD->getLocation(), diag::note_declared_at);2351 return false;2352 }2353 2354 // Check that the object is a global. Note that the fake 'this' object we2355 // manufacture when checking potential constant expressions is conservatively2356 // assumed to be global here.2357 if (!IsGlobalLValue(Base)) {2358 if (Info.getLangOpts().CPlusPlus11) {2359 Info.FFDiag(Loc, diag::note_constexpr_non_global, 1)2360 << IsReferenceType << !Designator.Entries.empty() << !!BaseVD2361 << BaseVD;2362 auto *VarD = dyn_cast_or_null<VarDecl>(BaseVD);2363 if (VarD && VarD->isConstexpr()) {2364 // Non-static local constexpr variables have unintuitive semantics:2365 // constexpr int a = 1;2366 // constexpr const int *p = &a;2367 // ... is invalid because the address of 'a' is not constant. Suggest2368 // adding a 'static' in this case.2369 Info.Note(VarD->getLocation(), diag::note_constexpr_not_static)2370 << VarD2371 << FixItHint::CreateInsertion(VarD->getBeginLoc(), "static ");2372 } else {2373 NoteLValueLocation(Info, Base);2374 }2375 } else {2376 Info.FFDiag(Loc);2377 }2378 // Don't allow references to temporaries to escape.2379 return false;2380 }2381 assert((Info.checkingPotentialConstantExpression() ||2382 LVal.getLValueCallIndex() == 0) &&2383 "have call index for global lvalue");2384 2385 if (LVal.allowConstexprUnknown()) {2386 if (BaseVD) {2387 Info.FFDiag(Loc, diag::note_constexpr_var_init_non_constant, 1) << BaseVD;2388 NoteLValueLocation(Info, Base);2389 } else {2390 Info.FFDiag(Loc);2391 }2392 return false;2393 }2394 2395 if (Base.is<DynamicAllocLValue>()) {2396 Info.FFDiag(Loc, diag::note_constexpr_dynamic_alloc)2397 << IsReferenceType << !Designator.Entries.empty();2398 NoteLValueLocation(Info, Base);2399 return false;2400 }2401 2402 if (BaseVD) {2403 if (const VarDecl *Var = dyn_cast<const VarDecl>(BaseVD)) {2404 // Check if this is a thread-local variable.2405 if (Var->getTLSKind())2406 // FIXME: Diagnostic!2407 return false;2408 2409 // A dllimport variable never acts like a constant, unless we're2410 // evaluating a value for use only in name mangling.2411 if (!isForManglingOnly(Kind) && Var->hasAttr<DLLImportAttr>())2412 // FIXME: Diagnostic!2413 return false;2414 2415 // In CUDA/HIP device compilation, only device side variables have2416 // constant addresses.2417 if (Info.getASTContext().getLangOpts().CUDA &&2418 Info.getASTContext().getLangOpts().CUDAIsDevice &&2419 Info.getASTContext().CUDAConstantEvalCtx.NoWrongSidedVars) {2420 if ((!Var->hasAttr<CUDADeviceAttr>() &&2421 !Var->hasAttr<CUDAConstantAttr>() &&2422 !Var->getType()->isCUDADeviceBuiltinSurfaceType() &&2423 !Var->getType()->isCUDADeviceBuiltinTextureType()) ||2424 Var->hasAttr<HIPManagedAttr>())2425 return false;2426 }2427 }2428 if (const auto *FD = dyn_cast<const FunctionDecl>(BaseVD)) {2429 // __declspec(dllimport) must be handled very carefully:2430 // We must never initialize an expression with the thunk in C++.2431 // Doing otherwise would allow the same id-expression to yield2432 // different addresses for the same function in different translation2433 // units. However, this means that we must dynamically initialize the2434 // expression with the contents of the import address table at runtime.2435 //2436 // The C language has no notion of ODR; furthermore, it has no notion of2437 // dynamic initialization. This means that we are permitted to2438 // perform initialization with the address of the thunk.2439 if (Info.getLangOpts().CPlusPlus && !isForManglingOnly(Kind) &&2440 FD->hasAttr<DLLImportAttr>())2441 // FIXME: Diagnostic!2442 return false;2443 }2444 } else if (const auto *MTE =2445 dyn_cast_or_null<MaterializeTemporaryExpr>(BaseE)) {2446 if (CheckedTemps.insert(MTE).second) {2447 QualType TempType = getType(Base);2448 if (TempType.isDestructedType()) {2449 Info.FFDiag(MTE->getExprLoc(),2450 diag::note_constexpr_unsupported_temporary_nontrivial_dtor)2451 << TempType;2452 return false;2453 }2454 2455 APValue *V = MTE->getOrCreateValue(false);2456 assert(V && "evasluation result refers to uninitialised temporary");2457 if (!CheckEvaluationResult(CheckEvaluationResultKind::ConstantExpression,2458 Info, MTE->getExprLoc(), TempType, *V, Kind,2459 /*SubobjectDecl=*/nullptr, CheckedTemps))2460 return false;2461 }2462 }2463 2464 // Allow address constant expressions to be past-the-end pointers. This is2465 // an extension: the standard requires them to point to an object.2466 if (!IsReferenceType)2467 return true;2468 2469 // A reference constant expression must refer to an object.2470 if (!Base) {2471 // FIXME: diagnostic2472 Info.CCEDiag(Loc);2473 return true;2474 }2475 2476 // Does this refer one past the end of some object?2477 if (!Designator.Invalid && Designator.isOnePastTheEnd()) {2478 Info.FFDiag(Loc, diag::note_constexpr_past_end, 1)2479 << !Designator.Entries.empty() << !!BaseVD << BaseVD;2480 NoteLValueLocation(Info, Base);2481 }2482 2483 return true;2484}2485 2486/// Member pointers are constant expressions unless they point to a2487/// non-virtual dllimport member function.2488static bool CheckMemberPointerConstantExpression(EvalInfo &Info,2489 SourceLocation Loc,2490 QualType Type,2491 const APValue &Value,2492 ConstantExprKind Kind) {2493 const ValueDecl *Member = Value.getMemberPointerDecl();2494 const auto *FD = dyn_cast_or_null<CXXMethodDecl>(Member);2495 if (!FD)2496 return true;2497 if (FD->isImmediateFunction()) {2498 Info.FFDiag(Loc, diag::note_consteval_address_accessible) << /*pointer*/ 0;2499 Info.Note(FD->getLocation(), diag::note_declared_at);2500 return false;2501 }2502 return isForManglingOnly(Kind) || FD->isVirtual() ||2503 !FD->hasAttr<DLLImportAttr>();2504}2505 2506/// Check that this core constant expression is of literal type, and if not,2507/// produce an appropriate diagnostic.2508static bool CheckLiteralType(EvalInfo &Info, const Expr *E,2509 const LValue *This = nullptr) {2510 // The restriction to literal types does not exist in C++23 anymore.2511 if (Info.getLangOpts().CPlusPlus23)2512 return true;2513 2514 if (!E->isPRValue() || E->getType()->isLiteralType(Info.Ctx))2515 return true;2516 2517 // C++1y: A constant initializer for an object o [...] may also invoke2518 // constexpr constructors for o and its subobjects even if those objects2519 // are of non-literal class types.2520 //2521 // C++11 missed this detail for aggregates, so classes like this:2522 // struct foo_t { union { int i; volatile int j; } u; };2523 // are not (obviously) initializable like so:2524 // __attribute__((__require_constant_initialization__))2525 // static const foo_t x = {{0}};2526 // because "i" is a subobject with non-literal initialization (due to the2527 // volatile member of the union). See:2528 // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#16772529 // Therefore, we use the C++1y behavior.2530 if (This && Info.EvaluatingDecl == This->getLValueBase())2531 return true;2532 2533 // Prvalue constant expressions must be of literal types.2534 if (Info.getLangOpts().CPlusPlus11)2535 Info.FFDiag(E, diag::note_constexpr_nonliteral)2536 << E->getType();2537 else2538 Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr);2539 return false;2540}2541 2542static bool CheckEvaluationResult(CheckEvaluationResultKind CERK,2543 EvalInfo &Info, SourceLocation DiagLoc,2544 QualType Type, const APValue &Value,2545 ConstantExprKind Kind,2546 const FieldDecl *SubobjectDecl,2547 CheckedTemporaries &CheckedTemps) {2548 if (!Value.hasValue()) {2549 if (SubobjectDecl) {2550 Info.FFDiag(DiagLoc, diag::note_constexpr_uninitialized)2551 << /*(name)*/ 1 << SubobjectDecl;2552 Info.Note(SubobjectDecl->getLocation(),2553 diag::note_constexpr_subobject_declared_here);2554 } else {2555 Info.FFDiag(DiagLoc, diag::note_constexpr_uninitialized)2556 << /*of type*/ 0 << Type;2557 }2558 return false;2559 }2560 2561 // We allow _Atomic(T) to be initialized from anything that T can be2562 // initialized from.2563 if (const AtomicType *AT = Type->getAs<AtomicType>())2564 Type = AT->getValueType();2565 2566 // Core issue 1454: For a literal constant expression of array or class type,2567 // each subobject of its value shall have been initialized by a constant2568 // expression.2569 if (Value.isArray()) {2570 QualType EltTy = Type->castAsArrayTypeUnsafe()->getElementType();2571 for (unsigned I = 0, N = Value.getArrayInitializedElts(); I != N; ++I) {2572 if (!CheckEvaluationResult(CERK, Info, DiagLoc, EltTy,2573 Value.getArrayInitializedElt(I), Kind,2574 SubobjectDecl, CheckedTemps))2575 return false;2576 }2577 if (!Value.hasArrayFiller())2578 return true;2579 return CheckEvaluationResult(CERK, Info, DiagLoc, EltTy,2580 Value.getArrayFiller(), Kind, SubobjectDecl,2581 CheckedTemps);2582 }2583 if (Value.isUnion() && Value.getUnionField()) {2584 return CheckEvaluationResult(2585 CERK, Info, DiagLoc, Value.getUnionField()->getType(),2586 Value.getUnionValue(), Kind, Value.getUnionField(), CheckedTemps);2587 }2588 if (Value.isStruct()) {2589 auto *RD = Type->castAsRecordDecl();2590 if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD)) {2591 unsigned BaseIndex = 0;2592 for (const CXXBaseSpecifier &BS : CD->bases()) {2593 const APValue &BaseValue = Value.getStructBase(BaseIndex);2594 if (!BaseValue.hasValue()) {2595 SourceLocation TypeBeginLoc = BS.getBaseTypeLoc();2596 Info.FFDiag(TypeBeginLoc, diag::note_constexpr_uninitialized_base)2597 << BS.getType() << SourceRange(TypeBeginLoc, BS.getEndLoc());2598 return false;2599 }2600 if (!CheckEvaluationResult(CERK, Info, DiagLoc, BS.getType(), BaseValue,2601 Kind, /*SubobjectDecl=*/nullptr,2602 CheckedTemps))2603 return false;2604 ++BaseIndex;2605 }2606 }2607 for (const auto *I : RD->fields()) {2608 if (I->isUnnamedBitField())2609 continue;2610 2611 if (!CheckEvaluationResult(CERK, Info, DiagLoc, I->getType(),2612 Value.getStructField(I->getFieldIndex()), Kind,2613 I, CheckedTemps))2614 return false;2615 }2616 }2617 2618 if (Value.isLValue() &&2619 CERK == CheckEvaluationResultKind::ConstantExpression) {2620 LValue LVal;2621 LVal.setFrom(Info.Ctx, Value);2622 return CheckLValueConstantExpression(Info, DiagLoc, Type, LVal, Kind,2623 CheckedTemps);2624 }2625 2626 if (Value.isMemberPointer() &&2627 CERK == CheckEvaluationResultKind::ConstantExpression)2628 return CheckMemberPointerConstantExpression(Info, DiagLoc, Type, Value, Kind);2629 2630 // Everything else is fine.2631 return true;2632}2633 2634/// Check that this core constant expression value is a valid value for a2635/// constant expression. If not, report an appropriate diagnostic. Does not2636/// check that the expression is of literal type.2637static bool CheckConstantExpression(EvalInfo &Info, SourceLocation DiagLoc,2638 QualType Type, const APValue &Value,2639 ConstantExprKind Kind) {2640 // Nothing to check for a constant expression of type 'cv void'.2641 if (Type->isVoidType())2642 return true;2643 2644 CheckedTemporaries CheckedTemps;2645 return CheckEvaluationResult(CheckEvaluationResultKind::ConstantExpression,2646 Info, DiagLoc, Type, Value, Kind,2647 /*SubobjectDecl=*/nullptr, CheckedTemps);2648}2649 2650/// Check that this evaluated value is fully-initialized and can be loaded by2651/// an lvalue-to-rvalue conversion.2652static bool CheckFullyInitialized(EvalInfo &Info, SourceLocation DiagLoc,2653 QualType Type, const APValue &Value) {2654 CheckedTemporaries CheckedTemps;2655 return CheckEvaluationResult(2656 CheckEvaluationResultKind::FullyInitialized, Info, DiagLoc, Type, Value,2657 ConstantExprKind::Normal, /*SubobjectDecl=*/nullptr, CheckedTemps);2658}2659 2660/// Enforce C++2a [expr.const]/4.17, which disallows new-expressions unless2661/// "the allocated storage is deallocated within the evaluation".2662static bool CheckMemoryLeaks(EvalInfo &Info) {2663 if (!Info.HeapAllocs.empty()) {2664 // We can still fold to a constant despite a compile-time memory leak,2665 // so long as the heap allocation isn't referenced in the result (we check2666 // that in CheckConstantExpression).2667 Info.CCEDiag(Info.HeapAllocs.begin()->second.AllocExpr,2668 diag::note_constexpr_memory_leak)2669 << unsigned(Info.HeapAllocs.size() - 1);2670 }2671 return true;2672}2673 2674static bool EvalPointerValueAsBool(const APValue &Value, bool &Result) {2675 // A null base expression indicates a null pointer. These are always2676 // evaluatable, and they are false unless the offset is zero.2677 if (!Value.getLValueBase()) {2678 // TODO: Should a non-null pointer with an offset of zero evaluate to true?2679 Result = !Value.getLValueOffset().isZero();2680 return true;2681 }2682 2683 // We have a non-null base. These are generally known to be true, but if it's2684 // a weak declaration it can be null at runtime.2685 Result = true;2686 const ValueDecl *Decl = Value.getLValueBase().dyn_cast<const ValueDecl*>();2687 return !Decl || !Decl->isWeak();2688}2689 2690static bool HandleConversionToBool(const APValue &Val, bool &Result) {2691 // TODO: This function should produce notes if it fails.2692 switch (Val.getKind()) {2693 case APValue::None:2694 case APValue::Indeterminate:2695 return false;2696 case APValue::Int:2697 Result = Val.getInt().getBoolValue();2698 return true;2699 case APValue::FixedPoint:2700 Result = Val.getFixedPoint().getBoolValue();2701 return true;2702 case APValue::Float:2703 Result = !Val.getFloat().isZero();2704 return true;2705 case APValue::ComplexInt:2706 Result = Val.getComplexIntReal().getBoolValue() ||2707 Val.getComplexIntImag().getBoolValue();2708 return true;2709 case APValue::ComplexFloat:2710 Result = !Val.getComplexFloatReal().isZero() ||2711 !Val.getComplexFloatImag().isZero();2712 return true;2713 case APValue::LValue:2714 return EvalPointerValueAsBool(Val, Result);2715 case APValue::MemberPointer:2716 if (Val.getMemberPointerDecl() && Val.getMemberPointerDecl()->isWeak()) {2717 return false;2718 }2719 Result = Val.getMemberPointerDecl();2720 return true;2721 case APValue::Vector:2722 case APValue::Array:2723 case APValue::Struct:2724 case APValue::Union:2725 case APValue::AddrLabelDiff:2726 return false;2727 }2728 2729 llvm_unreachable("unknown APValue kind");2730}2731 2732static bool EvaluateAsBooleanCondition(const Expr *E, bool &Result,2733 EvalInfo &Info) {2734 assert(!E->isValueDependent());2735 assert(E->isPRValue() && "missing lvalue-to-rvalue conv in bool condition");2736 APValue Val;2737 if (!Evaluate(Val, Info, E))2738 return false;2739 return HandleConversionToBool(Val, Result);2740}2741 2742template<typename T>2743static bool HandleOverflow(EvalInfo &Info, const Expr *E,2744 const T &SrcValue, QualType DestType) {2745 Info.CCEDiag(E, diag::note_constexpr_overflow)2746 << SrcValue << DestType;2747 return Info.noteUndefinedBehavior();2748}2749 2750static bool HandleFloatToIntCast(EvalInfo &Info, const Expr *E,2751 QualType SrcType, const APFloat &Value,2752 QualType DestType, APSInt &Result) {2753 unsigned DestWidth = Info.Ctx.getIntWidth(DestType);2754 // Determine whether we are converting to unsigned or signed.2755 bool DestSigned = DestType->isSignedIntegerOrEnumerationType();2756 2757 Result = APSInt(DestWidth, !DestSigned);2758 bool ignored;2759 if (Value.convertToInteger(Result, llvm::APFloat::rmTowardZero, &ignored)2760 & APFloat::opInvalidOp)2761 return HandleOverflow(Info, E, Value, DestType);2762 return true;2763}2764 2765/// Get rounding mode to use in evaluation of the specified expression.2766///2767/// If rounding mode is unknown at compile time, still try to evaluate the2768/// expression. If the result is exact, it does not depend on rounding mode.2769/// So return "tonearest" mode instead of "dynamic".2770static llvm::RoundingMode getActiveRoundingMode(EvalInfo &Info, const Expr *E) {2771 llvm::RoundingMode RM =2772 E->getFPFeaturesInEffect(Info.Ctx.getLangOpts()).getRoundingMode();2773 if (RM == llvm::RoundingMode::Dynamic)2774 RM = llvm::RoundingMode::NearestTiesToEven;2775 return RM;2776}2777 2778/// Check if the given evaluation result is allowed for constant evaluation.2779static bool checkFloatingPointResult(EvalInfo &Info, const Expr *E,2780 APFloat::opStatus St) {2781 // In a constant context, assume that any dynamic rounding mode or FP2782 // exception state matches the default floating-point environment.2783 if (Info.InConstantContext)2784 return true;2785 2786 FPOptions FPO = E->getFPFeaturesInEffect(Info.Ctx.getLangOpts());2787 if ((St & APFloat::opInexact) &&2788 FPO.getRoundingMode() == llvm::RoundingMode::Dynamic) {2789 // Inexact result means that it depends on rounding mode. If the requested2790 // mode is dynamic, the evaluation cannot be made in compile time.2791 Info.FFDiag(E, diag::note_constexpr_dynamic_rounding);2792 return false;2793 }2794 2795 if ((St != APFloat::opOK) &&2796 (FPO.getRoundingMode() == llvm::RoundingMode::Dynamic ||2797 FPO.getExceptionMode() != LangOptions::FPE_Ignore ||2798 FPO.getAllowFEnvAccess())) {2799 Info.FFDiag(E, diag::note_constexpr_float_arithmetic_strict);2800 return false;2801 }2802 2803 if ((St & APFloat::opStatus::opInvalidOp) &&2804 FPO.getExceptionMode() != LangOptions::FPE_Ignore) {2805 // There is no usefully definable result.2806 Info.FFDiag(E);2807 return false;2808 }2809 2810 // FIXME: if:2811 // - evaluation triggered other FP exception, and2812 // - exception mode is not "ignore", and2813 // - the expression being evaluated is not a part of global variable2814 // initializer,2815 // the evaluation probably need to be rejected.2816 return true;2817}2818 2819static bool HandleFloatToFloatCast(EvalInfo &Info, const Expr *E,2820 QualType SrcType, QualType DestType,2821 APFloat &Result) {2822 assert((isa<CastExpr>(E) || isa<CompoundAssignOperator>(E) ||2823 isa<ConvertVectorExpr>(E)) &&2824 "HandleFloatToFloatCast has been checked with only CastExpr, "2825 "CompoundAssignOperator and ConvertVectorExpr. Please either validate "2826 "the new expression or address the root cause of this usage.");2827 llvm::RoundingMode RM = getActiveRoundingMode(Info, E);2828 APFloat::opStatus St;2829 APFloat Value = Result;2830 bool ignored;2831 St = Result.convert(Info.Ctx.getFloatTypeSemantics(DestType), RM, &ignored);2832 return checkFloatingPointResult(Info, E, St);2833}2834 2835static APSInt HandleIntToIntCast(EvalInfo &Info, const Expr *E,2836 QualType DestType, QualType SrcType,2837 const APSInt &Value) {2838 unsigned DestWidth = Info.Ctx.getIntWidth(DestType);2839 // Figure out if this is a truncate, extend or noop cast.2840 // If the input is signed, do a sign extend, noop, or truncate.2841 APSInt Result = Value.extOrTrunc(DestWidth);2842 Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType());2843 if (DestType->isBooleanType())2844 Result = Value.getBoolValue();2845 return Result;2846}2847 2848static bool HandleIntToFloatCast(EvalInfo &Info, const Expr *E,2849 const FPOptions FPO,2850 QualType SrcType, const APSInt &Value,2851 QualType DestType, APFloat &Result) {2852 Result = APFloat(Info.Ctx.getFloatTypeSemantics(DestType), 1);2853 llvm::RoundingMode RM = getActiveRoundingMode(Info, E);2854 APFloat::opStatus St = Result.convertFromAPInt(Value, Value.isSigned(), RM);2855 return checkFloatingPointResult(Info, E, St);2856}2857 2858static bool truncateBitfieldValue(EvalInfo &Info, const Expr *E,2859 APValue &Value, const FieldDecl *FD) {2860 assert(FD->isBitField() && "truncateBitfieldValue on non-bitfield");2861 2862 if (!Value.isInt()) {2863 // Trying to store a pointer-cast-to-integer into a bitfield.2864 // FIXME: In this case, we should provide the diagnostic for casting2865 // a pointer to an integer.2866 assert(Value.isLValue() && "integral value neither int nor lvalue?");2867 Info.FFDiag(E);2868 return false;2869 }2870 2871 APSInt &Int = Value.getInt();2872 unsigned OldBitWidth = Int.getBitWidth();2873 unsigned NewBitWidth = FD->getBitWidthValue();2874 if (NewBitWidth < OldBitWidth)2875 Int = Int.trunc(NewBitWidth).extend(OldBitWidth);2876 return true;2877}2878 2879/// Perform the given integer operation, which is known to need at most BitWidth2880/// bits, and check for overflow in the original type (if that type was not an2881/// unsigned type).2882template<typename Operation>2883static bool CheckedIntArithmetic(EvalInfo &Info, const Expr *E,2884 const APSInt &LHS, const APSInt &RHS,2885 unsigned BitWidth, Operation Op,2886 APSInt &Result) {2887 if (LHS.isUnsigned()) {2888 Result = Op(LHS, RHS);2889 return true;2890 }2891 2892 APSInt Value(Op(LHS.extend(BitWidth), RHS.extend(BitWidth)), false);2893 Result = Value.trunc(LHS.getBitWidth());2894 if (Result.extend(BitWidth) != Value) {2895 if (Info.checkingForUndefinedBehavior())2896 Info.Ctx.getDiagnostics().Report(E->getExprLoc(),2897 diag::warn_integer_constant_overflow)2898 << toString(Result, 10, Result.isSigned(), /*formatAsCLiteral=*/false,2899 /*UpperCase=*/true, /*InsertSeparators=*/true)2900 << E->getType() << E->getSourceRange();2901 return HandleOverflow(Info, E, Value, E->getType());2902 }2903 return true;2904}2905 2906/// Perform the given binary integer operation.2907static bool handleIntIntBinOp(EvalInfo &Info, const BinaryOperator *E,2908 const APSInt &LHS, BinaryOperatorKind Opcode,2909 APSInt RHS, APSInt &Result) {2910 bool HandleOverflowResult = true;2911 switch (Opcode) {2912 default:2913 Info.FFDiag(E);2914 return false;2915 case BO_Mul:2916 return CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() * 2,2917 std::multiplies<APSInt>(), Result);2918 case BO_Add:2919 return CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() + 1,2920 std::plus<APSInt>(), Result);2921 case BO_Sub:2922 return CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() + 1,2923 std::minus<APSInt>(), Result);2924 case BO_And: Result = LHS & RHS; return true;2925 case BO_Xor: Result = LHS ^ RHS; return true;2926 case BO_Or: Result = LHS | RHS; return true;2927 case BO_Div:2928 case BO_Rem:2929 if (RHS == 0) {2930 Info.FFDiag(E, diag::note_expr_divide_by_zero)2931 << E->getRHS()->getSourceRange();2932 return false;2933 }2934 // Check for overflow case: INT_MIN / -1 or INT_MIN % -1. APSInt supports2935 // this operation and gives the two's complement result.2936 if (RHS.isNegative() && RHS.isAllOnes() && LHS.isSigned() &&2937 LHS.isMinSignedValue())2938 HandleOverflowResult = HandleOverflow(2939 Info, E, -LHS.extend(LHS.getBitWidth() + 1), E->getType());2940 Result = (Opcode == BO_Rem ? LHS % RHS : LHS / RHS);2941 return HandleOverflowResult;2942 case BO_Shl: {2943 if (Info.getLangOpts().OpenCL)2944 // OpenCL 6.3j: shift values are effectively % word size of LHS.2945 RHS &= APSInt(llvm::APInt(RHS.getBitWidth(),2946 static_cast<uint64_t>(LHS.getBitWidth() - 1)),2947 RHS.isUnsigned());2948 else if (RHS.isSigned() && RHS.isNegative()) {2949 // During constant-folding, a negative shift is an opposite shift. Such2950 // a shift is not a constant expression.2951 Info.CCEDiag(E, diag::note_constexpr_negative_shift) << RHS;2952 if (!Info.noteUndefinedBehavior())2953 return false;2954 RHS = -RHS;2955 goto shift_right;2956 }2957 shift_left:2958 // C++11 [expr.shift]p1: Shift width must be less than the bit width of2959 // the shifted type.2960 unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1);2961 if (SA != RHS) {2962 Info.CCEDiag(E, diag::note_constexpr_large_shift)2963 << RHS << E->getType() << LHS.getBitWidth();2964 if (!Info.noteUndefinedBehavior())2965 return false;2966 } else if (LHS.isSigned() && !Info.getLangOpts().CPlusPlus20) {2967 // C++11 [expr.shift]p2: A signed left shift must have a non-negative2968 // operand, and must not overflow the corresponding unsigned type.2969 // C++2a [expr.shift]p2: E1 << E2 is the unique value congruent to2970 // E1 x 2^E2 module 2^N.2971 if (LHS.isNegative()) {2972 Info.CCEDiag(E, diag::note_constexpr_lshift_of_negative) << LHS;2973 if (!Info.noteUndefinedBehavior())2974 return false;2975 } else if (LHS.countl_zero() < SA) {2976 Info.CCEDiag(E, diag::note_constexpr_lshift_discards);2977 if (!Info.noteUndefinedBehavior())2978 return false;2979 }2980 }2981 Result = LHS << SA;2982 return true;2983 }2984 case BO_Shr: {2985 if (Info.getLangOpts().OpenCL)2986 // OpenCL 6.3j: shift values are effectively % word size of LHS.2987 RHS &= APSInt(llvm::APInt(RHS.getBitWidth(),2988 static_cast<uint64_t>(LHS.getBitWidth() - 1)),2989 RHS.isUnsigned());2990 else if (RHS.isSigned() && RHS.isNegative()) {2991 // During constant-folding, a negative shift is an opposite shift. Such a2992 // shift is not a constant expression.2993 Info.CCEDiag(E, diag::note_constexpr_negative_shift) << RHS;2994 if (!Info.noteUndefinedBehavior())2995 return false;2996 RHS = -RHS;2997 goto shift_left;2998 }2999 shift_right:3000 // C++11 [expr.shift]p1: Shift width must be less than the bit width of the3001 // shifted type.3002 unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1);3003 if (SA != RHS) {3004 Info.CCEDiag(E, diag::note_constexpr_large_shift)3005 << RHS << E->getType() << LHS.getBitWidth();3006 if (!Info.noteUndefinedBehavior())3007 return false;3008 }3009 3010 Result = LHS >> SA;3011 return true;3012 }3013 3014 case BO_LT: Result = LHS < RHS; return true;3015 case BO_GT: Result = LHS > RHS; return true;3016 case BO_LE: Result = LHS <= RHS; return true;3017 case BO_GE: Result = LHS >= RHS; return true;3018 case BO_EQ: Result = LHS == RHS; return true;3019 case BO_NE: Result = LHS != RHS; return true;3020 case BO_Cmp:3021 llvm_unreachable("BO_Cmp should be handled elsewhere");3022 }3023}3024 3025/// Perform the given binary floating-point operation, in-place, on LHS.3026static bool handleFloatFloatBinOp(EvalInfo &Info, const BinaryOperator *E,3027 APFloat &LHS, BinaryOperatorKind Opcode,3028 const APFloat &RHS) {3029 llvm::RoundingMode RM = getActiveRoundingMode(Info, E);3030 APFloat::opStatus St;3031 switch (Opcode) {3032 default:3033 Info.FFDiag(E);3034 return false;3035 case BO_Mul:3036 St = LHS.multiply(RHS, RM);3037 break;3038 case BO_Add:3039 St = LHS.add(RHS, RM);3040 break;3041 case BO_Sub:3042 St = LHS.subtract(RHS, RM);3043 break;3044 case BO_Div:3045 // [expr.mul]p4:3046 // If the second operand of / or % is zero the behavior is undefined.3047 if (RHS.isZero())3048 Info.CCEDiag(E, diag::note_expr_divide_by_zero);3049 St = LHS.divide(RHS, RM);3050 break;3051 }3052 3053 // [expr.pre]p4:3054 // If during the evaluation of an expression, the result is not3055 // mathematically defined [...], the behavior is undefined.3056 // FIXME: C++ rules require us to not conform to IEEE 754 here.3057 if (LHS.isNaN()) {3058 Info.CCEDiag(E, diag::note_constexpr_float_arithmetic) << LHS.isNaN();3059 return Info.noteUndefinedBehavior();3060 }3061 3062 return checkFloatingPointResult(Info, E, St);3063}3064 3065static bool handleLogicalOpForVector(const APInt &LHSValue,3066 BinaryOperatorKind Opcode,3067 const APInt &RHSValue, APInt &Result) {3068 bool LHS = (LHSValue != 0);3069 bool RHS = (RHSValue != 0);3070 3071 if (Opcode == BO_LAnd)3072 Result = LHS && RHS;3073 else3074 Result = LHS || RHS;3075 return true;3076}3077static bool handleLogicalOpForVector(const APFloat &LHSValue,3078 BinaryOperatorKind Opcode,3079 const APFloat &RHSValue, APInt &Result) {3080 bool LHS = !LHSValue.isZero();3081 bool RHS = !RHSValue.isZero();3082 3083 if (Opcode == BO_LAnd)3084 Result = LHS && RHS;3085 else3086 Result = LHS || RHS;3087 return true;3088}3089 3090static bool handleLogicalOpForVector(const APValue &LHSValue,3091 BinaryOperatorKind Opcode,3092 const APValue &RHSValue, APInt &Result) {3093 // The result is always an int type, however operands match the first.3094 if (LHSValue.getKind() == APValue::Int)3095 return handleLogicalOpForVector(LHSValue.getInt(), Opcode,3096 RHSValue.getInt(), Result);3097 assert(LHSValue.getKind() == APValue::Float && "Should be no other options");3098 return handleLogicalOpForVector(LHSValue.getFloat(), Opcode,3099 RHSValue.getFloat(), Result);3100}3101 3102template <typename APTy>3103static bool3104handleCompareOpForVectorHelper(const APTy &LHSValue, BinaryOperatorKind Opcode,3105 const APTy &RHSValue, APInt &Result) {3106 switch (Opcode) {3107 default:3108 llvm_unreachable("unsupported binary operator");3109 case BO_EQ:3110 Result = (LHSValue == RHSValue);3111 break;3112 case BO_NE:3113 Result = (LHSValue != RHSValue);3114 break;3115 case BO_LT:3116 Result = (LHSValue < RHSValue);3117 break;3118 case BO_GT:3119 Result = (LHSValue > RHSValue);3120 break;3121 case BO_LE:3122 Result = (LHSValue <= RHSValue);3123 break;3124 case BO_GE:3125 Result = (LHSValue >= RHSValue);3126 break;3127 }3128 3129 // The boolean operations on these vector types use an instruction that3130 // results in a mask of '-1' for the 'truth' value. Ensure that we negate 13131 // to -1 to make sure that we produce the correct value.3132 Result.negate();3133 3134 return true;3135}3136 3137static bool handleCompareOpForVector(const APValue &LHSValue,3138 BinaryOperatorKind Opcode,3139 const APValue &RHSValue, APInt &Result) {3140 // The result is always an int type, however operands match the first.3141 if (LHSValue.getKind() == APValue::Int)3142 return handleCompareOpForVectorHelper(LHSValue.getInt(), Opcode,3143 RHSValue.getInt(), Result);3144 assert(LHSValue.getKind() == APValue::Float && "Should be no other options");3145 return handleCompareOpForVectorHelper(LHSValue.getFloat(), Opcode,3146 RHSValue.getFloat(), Result);3147}3148 3149// Perform binary operations for vector types, in place on the LHS.3150static bool handleVectorVectorBinOp(EvalInfo &Info, const BinaryOperator *E,3151 BinaryOperatorKind Opcode,3152 APValue &LHSValue,3153 const APValue &RHSValue) {3154 assert(Opcode != BO_PtrMemD && Opcode != BO_PtrMemI &&3155 "Operation not supported on vector types");3156 3157 const auto *VT = E->getType()->castAs<VectorType>();3158 unsigned NumElements = VT->getNumElements();3159 QualType EltTy = VT->getElementType();3160 3161 // In the cases (typically C as I've observed) where we aren't evaluating3162 // constexpr but are checking for cases where the LHS isn't yet evaluatable,3163 // just give up.3164 if (!LHSValue.isVector()) {3165 assert(LHSValue.isLValue() &&3166 "A vector result that isn't a vector OR uncalculated LValue");3167 Info.FFDiag(E);3168 return false;3169 }3170 3171 assert(LHSValue.getVectorLength() == NumElements &&3172 RHSValue.getVectorLength() == NumElements && "Different vector sizes");3173 3174 SmallVector<APValue, 4> ResultElements;3175 3176 for (unsigned EltNum = 0; EltNum < NumElements; ++EltNum) {3177 APValue LHSElt = LHSValue.getVectorElt(EltNum);3178 APValue RHSElt = RHSValue.getVectorElt(EltNum);3179 3180 if (EltTy->isIntegerType()) {3181 APSInt EltResult{Info.Ctx.getIntWidth(EltTy),3182 EltTy->isUnsignedIntegerType()};3183 bool Success = true;3184 3185 if (BinaryOperator::isLogicalOp(Opcode))3186 Success = handleLogicalOpForVector(LHSElt, Opcode, RHSElt, EltResult);3187 else if (BinaryOperator::isComparisonOp(Opcode))3188 Success = handleCompareOpForVector(LHSElt, Opcode, RHSElt, EltResult);3189 else3190 Success = handleIntIntBinOp(Info, E, LHSElt.getInt(), Opcode,3191 RHSElt.getInt(), EltResult);3192 3193 if (!Success) {3194 Info.FFDiag(E);3195 return false;3196 }3197 ResultElements.emplace_back(EltResult);3198 3199 } else if (EltTy->isFloatingType()) {3200 assert(LHSElt.getKind() == APValue::Float &&3201 RHSElt.getKind() == APValue::Float &&3202 "Mismatched LHS/RHS/Result Type");3203 APFloat LHSFloat = LHSElt.getFloat();3204 3205 if (!handleFloatFloatBinOp(Info, E, LHSFloat, Opcode,3206 RHSElt.getFloat())) {3207 Info.FFDiag(E);3208 return false;3209 }3210 3211 ResultElements.emplace_back(LHSFloat);3212 }3213 }3214 3215 LHSValue = APValue(ResultElements.data(), ResultElements.size());3216 return true;3217}3218 3219/// Cast an lvalue referring to a base subobject to a derived class, by3220/// truncating the lvalue's path to the given length.3221static bool CastToDerivedClass(EvalInfo &Info, const Expr *E, LValue &Result,3222 const RecordDecl *TruncatedType,3223 unsigned TruncatedElements) {3224 SubobjectDesignator &D = Result.Designator;3225 3226 // Check we actually point to a derived class object.3227 if (TruncatedElements == D.Entries.size())3228 return true;3229 assert(TruncatedElements >= D.MostDerivedPathLength &&3230 "not casting to a derived class");3231 if (!Result.checkSubobject(Info, E, CSK_Derived))3232 return false;3233 3234 // Truncate the path to the subobject, and remove any derived-to-base offsets.3235 const RecordDecl *RD = TruncatedType;3236 for (unsigned I = TruncatedElements, N = D.Entries.size(); I != N; ++I) {3237 if (RD->isInvalidDecl()) return false;3238 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD);3239 const CXXRecordDecl *Base = getAsBaseClass(D.Entries[I]);3240 if (isVirtualBaseClass(D.Entries[I]))3241 Result.Offset -= Layout.getVBaseClassOffset(Base);3242 else3243 Result.Offset -= Layout.getBaseClassOffset(Base);3244 RD = Base;3245 }3246 D.Entries.resize(TruncatedElements);3247 return true;3248}3249 3250static bool HandleLValueDirectBase(EvalInfo &Info, const Expr *E, LValue &Obj,3251 const CXXRecordDecl *Derived,3252 const CXXRecordDecl *Base,3253 const ASTRecordLayout *RL = nullptr) {3254 if (!RL) {3255 if (Derived->isInvalidDecl()) return false;3256 RL = &Info.Ctx.getASTRecordLayout(Derived);3257 }3258 3259 Obj.addDecl(Info, E, Base, /*Virtual*/ false);3260 Obj.getLValueOffset() += RL->getBaseClassOffset(Base);3261 return true;3262}3263 3264static bool HandleLValueBase(EvalInfo &Info, const Expr *E, LValue &Obj,3265 const CXXRecordDecl *DerivedDecl,3266 const CXXBaseSpecifier *Base) {3267 const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl();3268 3269 if (!Base->isVirtual())3270 return HandleLValueDirectBase(Info, E, Obj, DerivedDecl, BaseDecl);3271 3272 SubobjectDesignator &D = Obj.Designator;3273 if (D.Invalid)3274 return false;3275 3276 // Extract most-derived object and corresponding type.3277 // FIXME: After implementing P2280R4 it became possible to get references3278 // here. We do MostDerivedType->getAsCXXRecordDecl() in several other3279 // locations and if we see crashes in those locations in the future3280 // it may make more sense to move this fix into Lvalue::set.3281 DerivedDecl = D.MostDerivedType.getNonReferenceType()->getAsCXXRecordDecl();3282 if (!CastToDerivedClass(Info, E, Obj, DerivedDecl, D.MostDerivedPathLength))3283 return false;3284 3285 // Find the virtual base class.3286 if (DerivedDecl->isInvalidDecl()) return false;3287 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(DerivedDecl);3288 Obj.addDecl(Info, E, BaseDecl, /*Virtual*/ true);3289 Obj.getLValueOffset() += Layout.getVBaseClassOffset(BaseDecl);3290 return true;3291}3292 3293static bool HandleLValueBasePath(EvalInfo &Info, const CastExpr *E,3294 QualType Type, LValue &Result) {3295 for (CastExpr::path_const_iterator PathI = E->path_begin(),3296 PathE = E->path_end();3297 PathI != PathE; ++PathI) {3298 if (!HandleLValueBase(Info, E, Result, Type->getAsCXXRecordDecl(),3299 *PathI))3300 return false;3301 Type = (*PathI)->getType();3302 }3303 return true;3304}3305 3306/// Cast an lvalue referring to a derived class to a known base subobject.3307static bool CastToBaseClass(EvalInfo &Info, const Expr *E, LValue &Result,3308 const CXXRecordDecl *DerivedRD,3309 const CXXRecordDecl *BaseRD) {3310 CXXBasePaths Paths(/*FindAmbiguities=*/false,3311 /*RecordPaths=*/true, /*DetectVirtual=*/false);3312 if (!DerivedRD->isDerivedFrom(BaseRD, Paths))3313 llvm_unreachable("Class must be derived from the passed in base class!");3314 3315 for (CXXBasePathElement &Elem : Paths.front())3316 if (!HandleLValueBase(Info, E, Result, Elem.Class, Elem.Base))3317 return false;3318 return true;3319}3320 3321/// Update LVal to refer to the given field, which must be a member of the type3322/// currently described by LVal.3323static bool HandleLValueMember(EvalInfo &Info, const Expr *E, LValue &LVal,3324 const FieldDecl *FD,3325 const ASTRecordLayout *RL = nullptr) {3326 if (!RL) {3327 if (FD->getParent()->isInvalidDecl()) return false;3328 RL = &Info.Ctx.getASTRecordLayout(FD->getParent());3329 }3330 3331 unsigned I = FD->getFieldIndex();3332 LVal.addDecl(Info, E, FD);3333 LVal.adjustOffset(Info.Ctx.toCharUnitsFromBits(RL->getFieldOffset(I)));3334 return true;3335}3336 3337/// Update LVal to refer to the given indirect field.3338static bool HandleLValueIndirectMember(EvalInfo &Info, const Expr *E,3339 LValue &LVal,3340 const IndirectFieldDecl *IFD) {3341 for (const auto *C : IFD->chain())3342 if (!HandleLValueMember(Info, E, LVal, cast<FieldDecl>(C)))3343 return false;3344 return true;3345}3346 3347enum class SizeOfType {3348 SizeOf,3349 DataSizeOf,3350};3351 3352/// Get the size of the given type in char units.3353static bool HandleSizeof(EvalInfo &Info, SourceLocation Loc, QualType Type,3354 CharUnits &Size, SizeOfType SOT = SizeOfType::SizeOf) {3355 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc3356 // extension.3357 if (Type->isVoidType() || Type->isFunctionType()) {3358 Size = CharUnits::One();3359 return true;3360 }3361 3362 if (Type->isDependentType()) {3363 Info.FFDiag(Loc);3364 return false;3365 }3366 3367 if (!Type->isConstantSizeType()) {3368 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.3369 // FIXME: Better diagnostic.3370 Info.FFDiag(Loc);3371 return false;3372 }3373 3374 if (SOT == SizeOfType::SizeOf)3375 Size = Info.Ctx.getTypeSizeInChars(Type);3376 else3377 Size = Info.Ctx.getTypeInfoDataSizeInChars(Type).Width;3378 return true;3379}3380 3381/// Update a pointer value to model pointer arithmetic.3382/// \param Info - Information about the ongoing evaluation.3383/// \param E - The expression being evaluated, for diagnostic purposes.3384/// \param LVal - The pointer value to be updated.3385/// \param EltTy - The pointee type represented by LVal.3386/// \param Adjustment - The adjustment, in objects of type EltTy, to add.3387static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E,3388 LValue &LVal, QualType EltTy,3389 APSInt Adjustment) {3390 CharUnits SizeOfPointee;3391 if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfPointee))3392 return false;3393 3394 LVal.adjustOffsetAndIndex(Info, E, Adjustment, SizeOfPointee);3395 return true;3396}3397 3398static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E,3399 LValue &LVal, QualType EltTy,3400 int64_t Adjustment) {3401 return HandleLValueArrayAdjustment(Info, E, LVal, EltTy,3402 APSInt::get(Adjustment));3403}3404 3405/// Update an lvalue to refer to a component of a complex number.3406/// \param Info - Information about the ongoing evaluation.3407/// \param LVal - The lvalue to be updated.3408/// \param EltTy - The complex number's component type.3409/// \param Imag - False for the real component, true for the imaginary.3410static bool HandleLValueComplexElement(EvalInfo &Info, const Expr *E,3411 LValue &LVal, QualType EltTy,3412 bool Imag) {3413 if (Imag) {3414 CharUnits SizeOfComponent;3415 if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfComponent))3416 return false;3417 LVal.Offset += SizeOfComponent;3418 }3419 LVal.addComplex(Info, E, EltTy, Imag);3420 return true;3421}3422 3423static bool HandleLValueVectorElement(EvalInfo &Info, const Expr *E,3424 LValue &LVal, QualType EltTy,3425 uint64_t Size, uint64_t Idx) {3426 if (Idx) {3427 CharUnits SizeOfElement;3428 if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfElement))3429 return false;3430 LVal.Offset += SizeOfElement * Idx;3431 }3432 LVal.addVectorElement(Info, E, EltTy, Size, Idx);3433 return true;3434}3435 3436/// Try to evaluate the initializer for a variable declaration.3437///3438/// \param Info Information about the ongoing evaluation.3439/// \param E An expression to be used when printing diagnostics.3440/// \param VD The variable whose initializer should be obtained.3441/// \param Version The version of the variable within the frame.3442/// \param Frame The frame in which the variable was created. Must be null3443/// if this variable is not local to the evaluation.3444/// \param Result Filled in with a pointer to the value of the variable.3445static bool evaluateVarDeclInit(EvalInfo &Info, const Expr *E,3446 const VarDecl *VD, CallStackFrame *Frame,3447 unsigned Version, APValue *&Result) {3448 // C++23 [expr.const]p8 If we have a reference type allow unknown references3449 // and pointers.3450 bool AllowConstexprUnknown =3451 Info.getLangOpts().CPlusPlus23 && VD->getType()->isReferenceType();3452 3453 APValue::LValueBase Base(VD, Frame ? Frame->Index : 0, Version);3454 3455 auto CheckUninitReference = [&](bool IsLocalVariable) {3456 if (!Result || (!Result->hasValue() && VD->getType()->isReferenceType())) {3457 // C++23 [expr.const]p83458 // ... For such an object that is not usable in constant expressions, the3459 // dynamic type of the object is constexpr-unknown. For such a reference3460 // that is not usable in constant expressions, the reference is treated3461 // as binding to an unspecified object of the referenced type whose3462 // lifetime and that of all subobjects includes the entire constant3463 // evaluation and whose dynamic type is constexpr-unknown.3464 //3465 // Variables that are part of the current evaluation are not3466 // constexpr-unknown.3467 if (!AllowConstexprUnknown || IsLocalVariable) {3468 if (!Info.checkingPotentialConstantExpression())3469 Info.FFDiag(E, diag::note_constexpr_use_uninit_reference);3470 return false;3471 }3472 Result = nullptr;3473 }3474 return true;3475 };3476 3477 // If this is a local variable, dig out its value.3478 if (Frame) {3479 Result = Frame->getTemporary(VD, Version);3480 if (Result)3481 return CheckUninitReference(/*IsLocalVariable=*/true);3482 3483 if (!isa<ParmVarDecl>(VD)) {3484 // Assume variables referenced within a lambda's call operator that were3485 // not declared within the call operator are captures and during checking3486 // of a potential constant expression, assume they are unknown constant3487 // expressions.3488 assert(isLambdaCallOperator(Frame->Callee) &&3489 (VD->getDeclContext() != Frame->Callee || VD->isInitCapture()) &&3490 "missing value for local variable");3491 if (Info.checkingPotentialConstantExpression())3492 return false;3493 // FIXME: This diagnostic is bogus; we do support captures. Is this code3494 // still reachable at all?3495 Info.FFDiag(E->getBeginLoc(),3496 diag::note_unimplemented_constexpr_lambda_feature_ast)3497 << "captures not currently allowed";3498 return false;3499 }3500 }3501 3502 // If we're currently evaluating the initializer of this declaration, use that3503 // in-flight value.3504 if (Info.EvaluatingDecl == Base) {3505 Result = Info.EvaluatingDeclValue;3506 return CheckUninitReference(/*IsLocalVariable=*/false);3507 }3508 3509 // P2280R4 struck the restriction that variable of reference type lifetime3510 // should begin within the evaluation of E3511 // Used to be C++20 [expr.const]p5.12.2:3512 // ... its lifetime began within the evaluation of E;3513 if (isa<ParmVarDecl>(VD)) {3514 if (AllowConstexprUnknown) {3515 Result = nullptr;3516 return true;3517 }3518 3519 // Assume parameters of a potential constant expression are usable in3520 // constant expressions.3521 if (!Info.checkingPotentialConstantExpression() ||3522 !Info.CurrentCall->Callee ||3523 !Info.CurrentCall->Callee->Equals(VD->getDeclContext())) {3524 if (Info.getLangOpts().CPlusPlus11) {3525 Info.FFDiag(E, diag::note_constexpr_function_param_value_unknown)3526 << VD;3527 NoteLValueLocation(Info, Base);3528 } else {3529 Info.FFDiag(E);3530 }3531 }3532 return false;3533 }3534 3535 if (E->isValueDependent())3536 return false;3537 3538 // Dig out the initializer, and use the declaration which it's attached to.3539 // FIXME: We should eventually check whether the variable has a reachable3540 // initializing declaration.3541 const Expr *Init = VD->getAnyInitializer(VD);3542 // P2280R4 struck the restriction that variable of reference type should have3543 // a preceding initialization.3544 // Used to be C++20 [expr.const]p5.12:3545 // ... reference has a preceding initialization and either ...3546 if (!Init && !AllowConstexprUnknown) {3547 // Don't diagnose during potential constant expression checking; an3548 // initializer might be added later.3549 if (!Info.checkingPotentialConstantExpression()) {3550 Info.FFDiag(E, diag::note_constexpr_var_init_unknown, 1)3551 << VD;3552 NoteLValueLocation(Info, Base);3553 }3554 return false;3555 }3556 3557 // P2280R4 struck the initialization requirement for variables of reference3558 // type so we can no longer assume we have an Init.3559 // Used to be C++20 [expr.const]p5.12:3560 // ... reference has a preceding initialization and either ...3561 if (Init && Init->isValueDependent()) {3562 // The DeclRefExpr is not value-dependent, but the variable it refers to3563 // has a value-dependent initializer. This should only happen in3564 // constant-folding cases, where the variable is not actually of a suitable3565 // type for use in a constant expression (otherwise the DeclRefExpr would3566 // have been value-dependent too), so diagnose that.3567 assert(!VD->mightBeUsableInConstantExpressions(Info.Ctx));3568 if (!Info.checkingPotentialConstantExpression()) {3569 Info.FFDiag(E, Info.getLangOpts().CPlusPlus113570 ? diag::note_constexpr_ltor_non_constexpr3571 : diag::note_constexpr_ltor_non_integral, 1)3572 << VD << VD->getType();3573 NoteLValueLocation(Info, Base);3574 }3575 return false;3576 }3577 3578 // Check that we can fold the initializer. In C++, we will have already done3579 // this in the cases where it matters for conformance.3580 // P2280R4 struck the initialization requirement for variables of reference3581 // type so we can no longer assume we have an Init.3582 // Used to be C++20 [expr.const]p5.12:3583 // ... reference has a preceding initialization and either ...3584 if (Init && !VD->evaluateValue() && !AllowConstexprUnknown) {3585 Info.FFDiag(E, diag::note_constexpr_var_init_non_constant, 1) << VD;3586 NoteLValueLocation(Info, Base);3587 return false;3588 }3589 3590 // Check that the variable is actually usable in constant expressions. For a3591 // const integral variable or a reference, we might have a non-constant3592 // initializer that we can nonetheless evaluate the initializer for. Such3593 // variables are not usable in constant expressions. In C++98, the3594 // initializer also syntactically needs to be an ICE.3595 //3596 // FIXME: We don't diagnose cases that aren't potentially usable in constant3597 // expressions here; doing so would regress diagnostics for things like3598 // reading from a volatile constexpr variable.3599 if ((Info.getLangOpts().CPlusPlus && !VD->hasConstantInitialization() &&3600 VD->mightBeUsableInConstantExpressions(Info.Ctx) &&3601 !AllowConstexprUnknown) ||3602 ((Info.getLangOpts().CPlusPlus || Info.getLangOpts().OpenCL) &&3603 !Info.getLangOpts().CPlusPlus11 && !VD->hasICEInitializer(Info.Ctx))) {3604 if (Init) {3605 Info.CCEDiag(E, diag::note_constexpr_var_init_non_constant, 1) << VD;3606 NoteLValueLocation(Info, Base);3607 } else {3608 Info.CCEDiag(E);3609 }3610 }3611 3612 // Never use the initializer of a weak variable, not even for constant3613 // folding. We can't be sure that this is the definition that will be used.3614 if (VD->isWeak()) {3615 Info.FFDiag(E, diag::note_constexpr_var_init_weak) << VD;3616 NoteLValueLocation(Info, Base);3617 return false;3618 }3619 3620 Result = VD->getEvaluatedValue();3621 3622 if (!Result && !AllowConstexprUnknown)3623 return false;3624 3625 return CheckUninitReference(/*IsLocalVariable=*/false);3626}3627 3628/// Get the base index of the given base class within an APValue representing3629/// the given derived class.3630static unsigned getBaseIndex(const CXXRecordDecl *Derived,3631 const CXXRecordDecl *Base) {3632 Base = Base->getCanonicalDecl();3633 unsigned Index = 0;3634 for (CXXRecordDecl::base_class_const_iterator I = Derived->bases_begin(),3635 E = Derived->bases_end(); I != E; ++I, ++Index) {3636 if (I->getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base)3637 return Index;3638 }3639 3640 llvm_unreachable("base class missing from derived class's bases list");3641}3642 3643/// Extract the value of a character from a string literal.3644static APSInt extractStringLiteralCharacter(EvalInfo &Info, const Expr *Lit,3645 uint64_t Index) {3646 assert(!isa<SourceLocExpr>(Lit) &&3647 "SourceLocExpr should have already been converted to a StringLiteral");3648 3649 // FIXME: Support MakeStringConstant3650 if (const auto *ObjCEnc = dyn_cast<ObjCEncodeExpr>(Lit)) {3651 std::string Str;3652 Info.Ctx.getObjCEncodingForType(ObjCEnc->getEncodedType(), Str);3653 assert(Index <= Str.size() && "Index too large");3654 return APSInt::getUnsigned(Str.c_str()[Index]);3655 }3656 3657 if (auto PE = dyn_cast<PredefinedExpr>(Lit))3658 Lit = PE->getFunctionName();3659 const StringLiteral *S = cast<StringLiteral>(Lit);3660 const ConstantArrayType *CAT =3661 Info.Ctx.getAsConstantArrayType(S->getType());3662 assert(CAT && "string literal isn't an array");3663 QualType CharType = CAT->getElementType();3664 assert(CharType->isIntegerType() && "unexpected character type");3665 APSInt Value(Info.Ctx.getTypeSize(CharType),3666 CharType->isUnsignedIntegerType());3667 if (Index < S->getLength())3668 Value = S->getCodeUnit(Index);3669 return Value;3670}3671 3672// Expand a string literal into an array of characters.3673//3674// FIXME: This is inefficient; we should probably introduce something similar3675// to the LLVM ConstantDataArray to make this cheaper.3676static void expandStringLiteral(EvalInfo &Info, const StringLiteral *S,3677 APValue &Result,3678 QualType AllocType = QualType()) {3679 const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(3680 AllocType.isNull() ? S->getType() : AllocType);3681 assert(CAT && "string literal isn't an array");3682 QualType CharType = CAT->getElementType();3683 assert(CharType->isIntegerType() && "unexpected character type");3684 3685 unsigned Elts = CAT->getZExtSize();3686 Result = APValue(APValue::UninitArray(),3687 std::min(S->getLength(), Elts), Elts);3688 APSInt Value(Info.Ctx.getTypeSize(CharType),3689 CharType->isUnsignedIntegerType());3690 if (Result.hasArrayFiller())3691 Result.getArrayFiller() = APValue(Value);3692 for (unsigned I = 0, N = Result.getArrayInitializedElts(); I != N; ++I) {3693 Value = S->getCodeUnit(I);3694 Result.getArrayInitializedElt(I) = APValue(Value);3695 }3696}3697 3698// Expand an array so that it has more than Index filled elements.3699static void expandArray(APValue &Array, unsigned Index) {3700 unsigned Size = Array.getArraySize();3701 assert(Index < Size);3702 3703 // Always at least double the number of elements for which we store a value.3704 unsigned OldElts = Array.getArrayInitializedElts();3705 unsigned NewElts = std::max(Index+1, OldElts * 2);3706 NewElts = std::min(Size, std::max(NewElts, 8u));3707 3708 // Copy the data across.3709 APValue NewValue(APValue::UninitArray(), NewElts, Size);3710 for (unsigned I = 0; I != OldElts; ++I)3711 NewValue.getArrayInitializedElt(I).swap(Array.getArrayInitializedElt(I));3712 for (unsigned I = OldElts; I != NewElts; ++I)3713 NewValue.getArrayInitializedElt(I) = Array.getArrayFiller();3714 if (NewValue.hasArrayFiller())3715 NewValue.getArrayFiller() = Array.getArrayFiller();3716 Array.swap(NewValue);3717}3718 3719/// Determine whether a type would actually be read by an lvalue-to-rvalue3720/// conversion. If it's of class type, we may assume that the copy operation3721/// is trivial. Note that this is never true for a union type with fields3722/// (because the copy always "reads" the active member) and always true for3723/// a non-class type.3724static bool isReadByLvalueToRvalueConversion(const CXXRecordDecl *RD);3725static bool isReadByLvalueToRvalueConversion(QualType T) {3726 CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();3727 return !RD || isReadByLvalueToRvalueConversion(RD);3728}3729static bool isReadByLvalueToRvalueConversion(const CXXRecordDecl *RD) {3730 // FIXME: A trivial copy of a union copies the object representation, even if3731 // the union is empty.3732 if (RD->isUnion())3733 return !RD->field_empty();3734 if (RD->isEmpty())3735 return false;3736 3737 for (auto *Field : RD->fields())3738 if (!Field->isUnnamedBitField() &&3739 isReadByLvalueToRvalueConversion(Field->getType()))3740 return true;3741 3742 for (auto &BaseSpec : RD->bases())3743 if (isReadByLvalueToRvalueConversion(BaseSpec.getType()))3744 return true;3745 3746 return false;3747}3748 3749/// Diagnose an attempt to read from any unreadable field within the specified3750/// type, which might be a class type.3751static bool diagnoseMutableFields(EvalInfo &Info, const Expr *E, AccessKinds AK,3752 QualType T) {3753 CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();3754 if (!RD)3755 return false;3756 3757 if (!RD->hasMutableFields())3758 return false;3759 3760 for (auto *Field : RD->fields()) {3761 // If we're actually going to read this field in some way, then it can't3762 // be mutable. If we're in a union, then assigning to a mutable field3763 // (even an empty one) can change the active member, so that's not OK.3764 // FIXME: Add core issue number for the union case.3765 if (Field->isMutable() &&3766 (RD->isUnion() || isReadByLvalueToRvalueConversion(Field->getType()))) {3767 Info.FFDiag(E, diag::note_constexpr_access_mutable, 1) << AK << Field;3768 Info.Note(Field->getLocation(), diag::note_declared_at);3769 return true;3770 }3771 3772 if (diagnoseMutableFields(Info, E, AK, Field->getType()))3773 return true;3774 }3775 3776 for (auto &BaseSpec : RD->bases())3777 if (diagnoseMutableFields(Info, E, AK, BaseSpec.getType()))3778 return true;3779 3780 // All mutable fields were empty, and thus not actually read.3781 return false;3782}3783 3784static bool lifetimeStartedInEvaluation(EvalInfo &Info,3785 APValue::LValueBase Base,3786 bool MutableSubobject = false) {3787 // A temporary or transient heap allocation we created.3788 if (Base.getCallIndex() || Base.is<DynamicAllocLValue>())3789 return true;3790 3791 switch (Info.IsEvaluatingDecl) {3792 case EvalInfo::EvaluatingDeclKind::None:3793 return false;3794 3795 case EvalInfo::EvaluatingDeclKind::Ctor:3796 // The variable whose initializer we're evaluating.3797 if (Info.EvaluatingDecl == Base)3798 return true;3799 3800 // A temporary lifetime-extended by the variable whose initializer we're3801 // evaluating.3802 if (auto *BaseE = Base.dyn_cast<const Expr *>())3803 if (auto *BaseMTE = dyn_cast<MaterializeTemporaryExpr>(BaseE))3804 return Info.EvaluatingDecl == BaseMTE->getExtendingDecl();3805 return false;3806 3807 case EvalInfo::EvaluatingDeclKind::Dtor:3808 // C++2a [expr.const]p6:3809 // [during constant destruction] the lifetime of a and its non-mutable3810 // subobjects (but not its mutable subobjects) [are] considered to start3811 // within e.3812 if (MutableSubobject || Base != Info.EvaluatingDecl)3813 return false;3814 // FIXME: We can meaningfully extend this to cover non-const objects, but3815 // we will need special handling: we should be able to access only3816 // subobjects of such objects that are themselves declared const.3817 QualType T = getType(Base);3818 return T.isConstQualified() || T->isReferenceType();3819 }3820 3821 llvm_unreachable("unknown evaluating decl kind");3822}3823 3824static bool CheckArraySize(EvalInfo &Info, const ConstantArrayType *CAT,3825 SourceLocation CallLoc = {}) {3826 return Info.CheckArraySize(3827 CAT->getSizeExpr() ? CAT->getSizeExpr()->getBeginLoc() : CallLoc,3828 CAT->getNumAddressingBits(Info.Ctx), CAT->getZExtSize(),3829 /*Diag=*/true);3830}3831 3832static bool handleScalarCast(EvalInfo &Info, const FPOptions FPO, const Expr *E,3833 QualType SourceTy, QualType DestTy,3834 APValue const &Original, APValue &Result) {3835 // boolean must be checked before integer3836 // since IsIntegerType() is true for bool3837 if (SourceTy->isBooleanType()) {3838 if (DestTy->isBooleanType()) {3839 Result = Original;3840 return true;3841 }3842 if (DestTy->isIntegerType() || DestTy->isRealFloatingType()) {3843 bool BoolResult;3844 if (!HandleConversionToBool(Original, BoolResult))3845 return false;3846 uint64_t IntResult = BoolResult;3847 QualType IntType = DestTy->isIntegerType()3848 ? DestTy3849 : Info.Ctx.getIntTypeForBitwidth(64, false);3850 Result = APValue(Info.Ctx.MakeIntValue(IntResult, IntType));3851 }3852 if (DestTy->isRealFloatingType()) {3853 APValue Result2 = APValue(APFloat(0.0));3854 if (!HandleIntToFloatCast(Info, E, FPO,3855 Info.Ctx.getIntTypeForBitwidth(64, false),3856 Result.getInt(), DestTy, Result2.getFloat()))3857 return false;3858 Result = Result2;3859 }3860 return true;3861 }3862 if (SourceTy->isIntegerType()) {3863 if (DestTy->isRealFloatingType()) {3864 Result = APValue(APFloat(0.0));3865 return HandleIntToFloatCast(Info, E, FPO, SourceTy, Original.getInt(),3866 DestTy, Result.getFloat());3867 }3868 if (DestTy->isBooleanType()) {3869 bool BoolResult;3870 if (!HandleConversionToBool(Original, BoolResult))3871 return false;3872 uint64_t IntResult = BoolResult;3873 Result = APValue(Info.Ctx.MakeIntValue(IntResult, DestTy));3874 return true;3875 }3876 if (DestTy->isIntegerType()) {3877 Result = APValue(3878 HandleIntToIntCast(Info, E, DestTy, SourceTy, Original.getInt()));3879 return true;3880 }3881 } else if (SourceTy->isRealFloatingType()) {3882 if (DestTy->isRealFloatingType()) {3883 Result = Original;3884 return HandleFloatToFloatCast(Info, E, SourceTy, DestTy,3885 Result.getFloat());3886 }3887 if (DestTy->isBooleanType()) {3888 bool BoolResult;3889 if (!HandleConversionToBool(Original, BoolResult))3890 return false;3891 uint64_t IntResult = BoolResult;3892 Result = APValue(Info.Ctx.MakeIntValue(IntResult, DestTy));3893 return true;3894 }3895 if (DestTy->isIntegerType()) {3896 Result = APValue(APSInt());3897 return HandleFloatToIntCast(Info, E, SourceTy, Original.getFloat(),3898 DestTy, Result.getInt());3899 }3900 }3901 3902 Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr);3903 return false;3904}3905 3906// do the heavy lifting for casting to aggregate types3907// because we have to deal with bitfields specially3908static bool constructAggregate(EvalInfo &Info, const FPOptions FPO,3909 const Expr *E, APValue &Result,3910 QualType ResultType,3911 SmallVectorImpl<APValue> &Elements,3912 SmallVectorImpl<QualType> &ElTypes) {3913 3914 SmallVector<std::tuple<APValue *, QualType, unsigned>> WorkList = {3915 {&Result, ResultType, 0}};3916 3917 unsigned ElI = 0;3918 while (!WorkList.empty() && ElI < Elements.size()) {3919 auto [Res, Type, BitWidth] = WorkList.pop_back_val();3920 3921 if (Type->isRealFloatingType()) {3922 if (!handleScalarCast(Info, FPO, E, ElTypes[ElI], Type, Elements[ElI],3923 *Res))3924 return false;3925 ElI++;3926 continue;3927 }3928 if (Type->isIntegerType()) {3929 if (!handleScalarCast(Info, FPO, E, ElTypes[ElI], Type, Elements[ElI],3930 *Res))3931 return false;3932 if (BitWidth > 0) {3933 if (!Res->isInt())3934 return false;3935 APSInt &Int = Res->getInt();3936 unsigned OldBitWidth = Int.getBitWidth();3937 unsigned NewBitWidth = BitWidth;3938 if (NewBitWidth < OldBitWidth)3939 Int = Int.trunc(NewBitWidth).extend(OldBitWidth);3940 }3941 ElI++;3942 continue;3943 }3944 if (Type->isVectorType()) {3945 QualType ElTy = Type->castAs<VectorType>()->getElementType();3946 unsigned NumEl = Type->castAs<VectorType>()->getNumElements();3947 SmallVector<APValue> Vals(NumEl);3948 for (unsigned I = 0; I < NumEl; ++I) {3949 if (!handleScalarCast(Info, FPO, E, ElTypes[ElI], ElTy, Elements[ElI],3950 Vals[I]))3951 return false;3952 ElI++;3953 }3954 *Res = APValue(Vals.data(), NumEl);3955 continue;3956 }3957 if (Type->isConstantArrayType()) {3958 QualType ElTy = cast<ConstantArrayType>(Info.Ctx.getAsArrayType(Type))3959 ->getElementType();3960 uint64_t Size =3961 cast<ConstantArrayType>(Info.Ctx.getAsArrayType(Type))->getZExtSize();3962 *Res = APValue(APValue::UninitArray(), Size, Size);3963 for (int64_t I = Size - 1; I > -1; --I)3964 WorkList.emplace_back(&Res->getArrayInitializedElt(I), ElTy, 0u);3965 continue;3966 }3967 if (Type->isRecordType()) {3968 const RecordDecl *RD = Type->getAsRecordDecl();3969 3970 unsigned NumBases = 0;3971 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD))3972 NumBases = CXXRD->getNumBases();3973 3974 *Res = APValue(APValue::UninitStruct(), NumBases, RD->getNumFields());3975 3976 SmallVector<std::tuple<APValue *, QualType, unsigned>> ReverseList;3977 // we need to traverse backwards3978 // Visit the base classes.3979 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {3980 if (CXXRD->getNumBases() > 0) {3981 assert(CXXRD->getNumBases() == 1);3982 const CXXBaseSpecifier &BS = CXXRD->bases_begin()[0];3983 ReverseList.emplace_back(&Res->getStructBase(0), BS.getType(), 0u);3984 }3985 }3986 3987 // Visit the fields.3988 for (FieldDecl *FD : RD->fields()) {3989 unsigned FDBW = 0;3990 if (FD->isUnnamedBitField())3991 continue;3992 if (FD->isBitField()) {3993 FDBW = FD->getBitWidthValue();3994 }3995 3996 ReverseList.emplace_back(&Res->getStructField(FD->getFieldIndex()),3997 FD->getType(), FDBW);3998 }3999 4000 std::reverse(ReverseList.begin(), ReverseList.end());4001 llvm::append_range(WorkList, ReverseList);4002 continue;4003 }4004 Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr);4005 return false;4006 }4007 return true;4008}4009 4010static bool handleElementwiseCast(EvalInfo &Info, const Expr *E,4011 const FPOptions FPO,4012 SmallVectorImpl<APValue> &Elements,4013 SmallVectorImpl<QualType> &SrcTypes,4014 SmallVectorImpl<QualType> &DestTypes,4015 SmallVectorImpl<APValue> &Results) {4016 4017 assert((Elements.size() == SrcTypes.size()) &&4018 (Elements.size() == DestTypes.size()));4019 4020 for (unsigned I = 0, ESz = Elements.size(); I < ESz; ++I) {4021 APValue Original = Elements[I];4022 QualType SourceTy = SrcTypes[I];4023 QualType DestTy = DestTypes[I];4024 4025 if (!handleScalarCast(Info, FPO, E, SourceTy, DestTy, Original, Results[I]))4026 return false;4027 }4028 return true;4029}4030 4031static unsigned elementwiseSize(EvalInfo &Info, QualType BaseTy) {4032 4033 SmallVector<QualType> WorkList = {BaseTy};4034 4035 unsigned Size = 0;4036 while (!WorkList.empty()) {4037 QualType Type = WorkList.pop_back_val();4038 if (Type->isRealFloatingType() || Type->isIntegerType() ||4039 Type->isBooleanType()) {4040 ++Size;4041 continue;4042 }4043 if (Type->isVectorType()) {4044 unsigned NumEl = Type->castAs<VectorType>()->getNumElements();4045 Size += NumEl;4046 continue;4047 }4048 if (Type->isConstantArrayType()) {4049 QualType ElTy = cast<ConstantArrayType>(Info.Ctx.getAsArrayType(Type))4050 ->getElementType();4051 uint64_t ArrSize =4052 cast<ConstantArrayType>(Info.Ctx.getAsArrayType(Type))->getZExtSize();4053 for (uint64_t I = 0; I < ArrSize; ++I) {4054 WorkList.push_back(ElTy);4055 }4056 continue;4057 }4058 if (Type->isRecordType()) {4059 const RecordDecl *RD = Type->getAsRecordDecl();4060 4061 // Visit the base classes.4062 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {4063 if (CXXRD->getNumBases() > 0) {4064 assert(CXXRD->getNumBases() == 1);4065 const CXXBaseSpecifier &BS = CXXRD->bases_begin()[0];4066 WorkList.push_back(BS.getType());4067 }4068 }4069 4070 // visit the fields.4071 for (FieldDecl *FD : RD->fields()) {4072 if (FD->isUnnamedBitField())4073 continue;4074 WorkList.push_back(FD->getType());4075 }4076 continue;4077 }4078 }4079 return Size;4080}4081 4082static bool hlslAggSplatHelper(EvalInfo &Info, const Expr *E, APValue &SrcVal,4083 QualType &SrcTy) {4084 SrcTy = E->getType();4085 4086 if (!Evaluate(SrcVal, Info, E))4087 return false;4088 4089 assert((SrcVal.isFloat() || SrcVal.isInt() ||4090 (SrcVal.isVector() && SrcVal.getVectorLength() == 1)) &&4091 "Not a valid HLSLAggregateSplatCast.");4092 4093 if (SrcVal.isVector()) {4094 assert(SrcTy->isVectorType() && "Type mismatch.");4095 SrcTy = SrcTy->castAs<VectorType>()->getElementType();4096 SrcVal = SrcVal.getVectorElt(0);4097 }4098 return true;4099}4100 4101static bool flattenAPValue(EvalInfo &Info, const Expr *E, APValue Value,4102 QualType BaseTy, SmallVectorImpl<APValue> &Elements,4103 SmallVectorImpl<QualType> &Types, unsigned Size) {4104 4105 SmallVector<std::pair<APValue, QualType>> WorkList = {{Value, BaseTy}};4106 unsigned Populated = 0;4107 while (!WorkList.empty() && Populated < Size) {4108 auto [Work, Type] = WorkList.pop_back_val();4109 4110 if (Work.isFloat() || Work.isInt()) {4111 Elements.push_back(Work);4112 Types.push_back(Type);4113 Populated++;4114 continue;4115 }4116 if (Work.isVector()) {4117 assert(Type->isVectorType() && "Type mismatch.");4118 QualType ElTy = Type->castAs<VectorType>()->getElementType();4119 for (unsigned I = 0; I < Work.getVectorLength() && Populated < Size;4120 I++) {4121 Elements.push_back(Work.getVectorElt(I));4122 Types.push_back(ElTy);4123 Populated++;4124 }4125 continue;4126 }4127 if (Work.isArray()) {4128 assert(Type->isConstantArrayType() && "Type mismatch.");4129 QualType ElTy = cast<ConstantArrayType>(Info.Ctx.getAsArrayType(Type))4130 ->getElementType();4131 for (int64_t I = Work.getArraySize() - 1; I > -1; --I) {4132 WorkList.emplace_back(Work.getArrayInitializedElt(I), ElTy);4133 }4134 continue;4135 }4136 4137 if (Work.isStruct()) {4138 assert(Type->isRecordType() && "Type mismatch.");4139 4140 const RecordDecl *RD = Type->getAsRecordDecl();4141 4142 SmallVector<std::pair<APValue, QualType>> ReverseList;4143 // Visit the fields.4144 for (FieldDecl *FD : RD->fields()) {4145 if (FD->isUnnamedBitField())4146 continue;4147 ReverseList.emplace_back(Work.getStructField(FD->getFieldIndex()),4148 FD->getType());4149 }4150 4151 std::reverse(ReverseList.begin(), ReverseList.end());4152 llvm::append_range(WorkList, ReverseList);4153 4154 // Visit the base classes.4155 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {4156 if (CXXRD->getNumBases() > 0) {4157 assert(CXXRD->getNumBases() == 1);4158 const CXXBaseSpecifier &BS = CXXRD->bases_begin()[0];4159 const APValue &Base = Work.getStructBase(0);4160 4161 // Can happen in error cases.4162 if (!Base.isStruct())4163 return false;4164 4165 WorkList.emplace_back(Base, BS.getType());4166 }4167 }4168 continue;4169 }4170 Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr);4171 return false;4172 }4173 return true;4174}4175 4176namespace {4177/// A handle to a complete object (an object that is not a subobject of4178/// another object).4179struct CompleteObject {4180 /// The identity of the object.4181 APValue::LValueBase Base;4182 /// The value of the complete object.4183 APValue *Value;4184 /// The type of the complete object.4185 QualType Type;4186 4187 CompleteObject() : Value(nullptr) {}4188 CompleteObject(APValue::LValueBase Base, APValue *Value, QualType Type)4189 : Base(Base), Value(Value), Type(Type) {}4190 4191 bool mayAccessMutableMembers(EvalInfo &Info, AccessKinds AK) const {4192 // If this isn't a "real" access (eg, if it's just accessing the type4193 // info), allow it. We assume the type doesn't change dynamically for4194 // subobjects of constexpr objects (even though we'd hit UB here if it4195 // did). FIXME: Is this right?4196 if (!isAnyAccess(AK))4197 return true;4198 4199 // In C++14 onwards, it is permitted to read a mutable member whose4200 // lifetime began within the evaluation.4201 // FIXME: Should we also allow this in C++11?4202 if (!Info.getLangOpts().CPlusPlus14 &&4203 AK != AccessKinds::AK_IsWithinLifetime)4204 return false;4205 return lifetimeStartedInEvaluation(Info, Base, /*MutableSubobject*/true);4206 }4207 4208 explicit operator bool() const { return !Type.isNull(); }4209};4210} // end anonymous namespace4211 4212static QualType getSubobjectType(QualType ObjType, QualType SubobjType,4213 bool IsMutable = false) {4214 // C++ [basic.type.qualifier]p1:4215 // - A const object is an object of type const T or a non-mutable subobject4216 // of a const object.4217 if (ObjType.isConstQualified() && !IsMutable)4218 SubobjType.addConst();4219 // - A volatile object is an object of type const T or a subobject of a4220 // volatile object.4221 if (ObjType.isVolatileQualified())4222 SubobjType.addVolatile();4223 return SubobjType;4224}4225 4226/// Find the designated sub-object of an rvalue.4227template <typename SubobjectHandler>4228static typename SubobjectHandler::result_type4229findSubobject(EvalInfo &Info, const Expr *E, const CompleteObject &Obj,4230 const SubobjectDesignator &Sub, SubobjectHandler &handler) {4231 if (Sub.Invalid)4232 // A diagnostic will have already been produced.4233 return handler.failed();4234 if (Sub.isOnePastTheEnd() || Sub.isMostDerivedAnUnsizedArray()) {4235 if (Info.getLangOpts().CPlusPlus11)4236 Info.FFDiag(E, Sub.isOnePastTheEnd()4237 ? diag::note_constexpr_access_past_end4238 : diag::note_constexpr_access_unsized_array)4239 << handler.AccessKind;4240 else4241 Info.FFDiag(E);4242 return handler.failed();4243 }4244 4245 APValue *O = Obj.Value;4246 QualType ObjType = Obj.Type;4247 const FieldDecl *LastField = nullptr;4248 const FieldDecl *VolatileField = nullptr;4249 4250 // Walk the designator's path to find the subobject.4251 for (unsigned I = 0, N = Sub.Entries.size(); /**/; ++I) {4252 // Reading an indeterminate value is undefined, but assigning over one is OK.4253 if ((O->isAbsent() && !(handler.AccessKind == AK_Construct && I == N)) ||4254 (O->isIndeterminate() &&4255 !isValidIndeterminateAccess(handler.AccessKind))) {4256 // Object has ended lifetime.4257 // If I is non-zero, some subobject (member or array element) of a4258 // complete object has ended its lifetime, so this is valid for4259 // IsWithinLifetime, resulting in false.4260 if (I != 0 && handler.AccessKind == AK_IsWithinLifetime)4261 return false;4262 if (!Info.checkingPotentialConstantExpression())4263 Info.FFDiag(E, diag::note_constexpr_access_uninit)4264 << handler.AccessKind << O->isIndeterminate()4265 << E->getSourceRange();4266 return handler.failed();4267 }4268 4269 // C++ [class.ctor]p5, C++ [class.dtor]p5:4270 // const and volatile semantics are not applied on an object under4271 // {con,de}struction.4272 if ((ObjType.isConstQualified() || ObjType.isVolatileQualified()) &&4273 ObjType->isRecordType() &&4274 Info.isEvaluatingCtorDtor(4275 Obj.Base, ArrayRef(Sub.Entries.begin(), Sub.Entries.begin() + I)) !=4276 ConstructionPhase::None) {4277 ObjType = Info.Ctx.getCanonicalType(ObjType);4278 ObjType.removeLocalConst();4279 ObjType.removeLocalVolatile();4280 }4281 4282 // If this is our last pass, check that the final object type is OK.4283 if (I == N || (I == N - 1 && ObjType->isAnyComplexType())) {4284 // Accesses to volatile objects are prohibited.4285 if (ObjType.isVolatileQualified() && isFormalAccess(handler.AccessKind)) {4286 if (Info.getLangOpts().CPlusPlus) {4287 int DiagKind;4288 SourceLocation Loc;4289 const NamedDecl *Decl = nullptr;4290 if (VolatileField) {4291 DiagKind = 2;4292 Loc = VolatileField->getLocation();4293 Decl = VolatileField;4294 } else if (auto *VD = Obj.Base.dyn_cast<const ValueDecl*>()) {4295 DiagKind = 1;4296 Loc = VD->getLocation();4297 Decl = VD;4298 } else {4299 DiagKind = 0;4300 if (auto *E = Obj.Base.dyn_cast<const Expr *>())4301 Loc = E->getExprLoc();4302 }4303 Info.FFDiag(E, diag::note_constexpr_access_volatile_obj, 1)4304 << handler.AccessKind << DiagKind << Decl;4305 Info.Note(Loc, diag::note_constexpr_volatile_here) << DiagKind;4306 } else {4307 Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr);4308 }4309 return handler.failed();4310 }4311 4312 // If we are reading an object of class type, there may still be more4313 // things we need to check: if there are any mutable subobjects, we4314 // cannot perform this read. (This only happens when performing a trivial4315 // copy or assignment.)4316 if (ObjType->isRecordType() &&4317 !Obj.mayAccessMutableMembers(Info, handler.AccessKind) &&4318 diagnoseMutableFields(Info, E, handler.AccessKind, ObjType))4319 return handler.failed();4320 }4321 4322 if (I == N) {4323 if (!handler.found(*O, ObjType))4324 return false;4325 4326 // If we modified a bit-field, truncate it to the right width.4327 if (isModification(handler.AccessKind) &&4328 LastField && LastField->isBitField() &&4329 !truncateBitfieldValue(Info, E, *O, LastField))4330 return false;4331 4332 return true;4333 }4334 4335 LastField = nullptr;4336 if (ObjType->isArrayType()) {4337 // Next subobject is an array element.4338 const ArrayType *AT = Info.Ctx.getAsArrayType(ObjType);4339 assert((isa<ConstantArrayType>(AT) || isa<IncompleteArrayType>(AT)) &&4340 "vla in literal type?");4341 uint64_t Index = Sub.Entries[I].getAsArrayIndex();4342 if (const auto *CAT = dyn_cast<ConstantArrayType>(AT);4343 CAT && CAT->getSize().ule(Index)) {4344 // Note, it should not be possible to form a pointer with a valid4345 // designator which points more than one past the end of the array.4346 if (Info.getLangOpts().CPlusPlus11)4347 Info.FFDiag(E, diag::note_constexpr_access_past_end)4348 << handler.AccessKind;4349 else4350 Info.FFDiag(E);4351 return handler.failed();4352 }4353 4354 ObjType = AT->getElementType();4355 4356 if (O->getArrayInitializedElts() > Index)4357 O = &O->getArrayInitializedElt(Index);4358 else if (!isRead(handler.AccessKind)) {4359 if (const auto *CAT = dyn_cast<ConstantArrayType>(AT);4360 CAT && !CheckArraySize(Info, CAT, E->getExprLoc()))4361 return handler.failed();4362 4363 expandArray(*O, Index);4364 O = &O->getArrayInitializedElt(Index);4365 } else4366 O = &O->getArrayFiller();4367 } else if (ObjType->isAnyComplexType()) {4368 // Next subobject is a complex number.4369 uint64_t Index = Sub.Entries[I].getAsArrayIndex();4370 if (Index > 1) {4371 if (Info.getLangOpts().CPlusPlus11)4372 Info.FFDiag(E, diag::note_constexpr_access_past_end)4373 << handler.AccessKind;4374 else4375 Info.FFDiag(E);4376 return handler.failed();4377 }4378 4379 ObjType = getSubobjectType(4380 ObjType, ObjType->castAs<ComplexType>()->getElementType());4381 4382 assert(I == N - 1 && "extracting subobject of scalar?");4383 if (O->isComplexInt()) {4384 return handler.found(Index ? O->getComplexIntImag()4385 : O->getComplexIntReal(), ObjType);4386 } else {4387 assert(O->isComplexFloat());4388 return handler.found(Index ? O->getComplexFloatImag()4389 : O->getComplexFloatReal(), ObjType);4390 }4391 } else if (const auto *VT = ObjType->getAs<VectorType>()) {4392 uint64_t Index = Sub.Entries[I].getAsArrayIndex();4393 unsigned NumElements = VT->getNumElements();4394 if (Index == NumElements) {4395 if (Info.getLangOpts().CPlusPlus11)4396 Info.FFDiag(E, diag::note_constexpr_access_past_end)4397 << handler.AccessKind;4398 else4399 Info.FFDiag(E);4400 return handler.failed();4401 }4402 4403 if (Index > NumElements) {4404 Info.CCEDiag(E, diag::note_constexpr_array_index)4405 << Index << /*array*/ 0 << NumElements;4406 return handler.failed();4407 }4408 4409 ObjType = VT->getElementType();4410 assert(I == N - 1 && "extracting subobject of scalar?");4411 return handler.found(O->getVectorElt(Index), ObjType);4412 } else if (const FieldDecl *Field = getAsField(Sub.Entries[I])) {4413 if (Field->isMutable() &&4414 !Obj.mayAccessMutableMembers(Info, handler.AccessKind)) {4415 Info.FFDiag(E, diag::note_constexpr_access_mutable, 1)4416 << handler.AccessKind << Field;4417 Info.Note(Field->getLocation(), diag::note_declared_at);4418 return handler.failed();4419 }4420 4421 // Next subobject is a class, struct or union field.4422 RecordDecl *RD = ObjType->castAsCanonical<RecordType>()->getDecl();4423 if (RD->isUnion()) {4424 const FieldDecl *UnionField = O->getUnionField();4425 if (!UnionField ||4426 UnionField->getCanonicalDecl() != Field->getCanonicalDecl()) {4427 if (I == N - 1 && handler.AccessKind == AK_Construct) {4428 // Placement new onto an inactive union member makes it active.4429 O->setUnion(Field, APValue());4430 } else {4431 // Pointer to/into inactive union member: Not within lifetime4432 if (handler.AccessKind == AK_IsWithinLifetime)4433 return false;4434 // FIXME: If O->getUnionValue() is absent, report that there's no4435 // active union member rather than reporting the prior active union4436 // member. We'll need to fix nullptr_t to not use APValue() as its4437 // representation first.4438 Info.FFDiag(E, diag::note_constexpr_access_inactive_union_member)4439 << handler.AccessKind << Field << !UnionField << UnionField;4440 return handler.failed();4441 }4442 }4443 O = &O->getUnionValue();4444 } else4445 O = &O->getStructField(Field->getFieldIndex());4446 4447 ObjType = getSubobjectType(ObjType, Field->getType(), Field->isMutable());4448 LastField = Field;4449 if (Field->getType().isVolatileQualified())4450 VolatileField = Field;4451 } else {4452 // Next subobject is a base class.4453 const CXXRecordDecl *Derived = ObjType->getAsCXXRecordDecl();4454 const CXXRecordDecl *Base = getAsBaseClass(Sub.Entries[I]);4455 O = &O->getStructBase(getBaseIndex(Derived, Base));4456 4457 ObjType = getSubobjectType(ObjType, Info.Ctx.getCanonicalTagType(Base));4458 }4459 }4460}4461 4462namespace {4463struct ExtractSubobjectHandler {4464 EvalInfo &Info;4465 const Expr *E;4466 APValue &Result;4467 const AccessKinds AccessKind;4468 4469 typedef bool result_type;4470 bool failed() { return false; }4471 bool found(APValue &Subobj, QualType SubobjType) {4472 Result = Subobj;4473 if (AccessKind == AK_ReadObjectRepresentation)4474 return true;4475 return CheckFullyInitialized(Info, E->getExprLoc(), SubobjType, Result);4476 }4477 bool found(APSInt &Value, QualType SubobjType) {4478 Result = APValue(Value);4479 return true;4480 }4481 bool found(APFloat &Value, QualType SubobjType) {4482 Result = APValue(Value);4483 return true;4484 }4485};4486} // end anonymous namespace4487 4488/// Extract the designated sub-object of an rvalue.4489static bool extractSubobject(EvalInfo &Info, const Expr *E,4490 const CompleteObject &Obj,4491 const SubobjectDesignator &Sub, APValue &Result,4492 AccessKinds AK = AK_Read) {4493 assert(AK == AK_Read || AK == AK_ReadObjectRepresentation);4494 ExtractSubobjectHandler Handler = {Info, E, Result, AK};4495 return findSubobject(Info, E, Obj, Sub, Handler);4496}4497 4498namespace {4499struct ModifySubobjectHandler {4500 EvalInfo &Info;4501 APValue &NewVal;4502 const Expr *E;4503 4504 typedef bool result_type;4505 static const AccessKinds AccessKind = AK_Assign;4506 4507 bool checkConst(QualType QT) {4508 // Assigning to a const object has undefined behavior.4509 if (QT.isConstQualified()) {4510 Info.FFDiag(E, diag::note_constexpr_modify_const_type) << QT;4511 return false;4512 }4513 return true;4514 }4515 4516 bool failed() { return false; }4517 bool found(APValue &Subobj, QualType SubobjType) {4518 if (!checkConst(SubobjType))4519 return false;4520 // We've been given ownership of NewVal, so just swap it in.4521 Subobj.swap(NewVal);4522 return true;4523 }4524 bool found(APSInt &Value, QualType SubobjType) {4525 if (!checkConst(SubobjType))4526 return false;4527 if (!NewVal.isInt()) {4528 // Maybe trying to write a cast pointer value into a complex?4529 Info.FFDiag(E);4530 return false;4531 }4532 Value = NewVal.getInt();4533 return true;4534 }4535 bool found(APFloat &Value, QualType SubobjType) {4536 if (!checkConst(SubobjType))4537 return false;4538 Value = NewVal.getFloat();4539 return true;4540 }4541};4542} // end anonymous namespace4543 4544const AccessKinds ModifySubobjectHandler::AccessKind;4545 4546/// Update the designated sub-object of an rvalue to the given value.4547static bool modifySubobject(EvalInfo &Info, const Expr *E,4548 const CompleteObject &Obj,4549 const SubobjectDesignator &Sub,4550 APValue &NewVal) {4551 ModifySubobjectHandler Handler = { Info, NewVal, E };4552 return findSubobject(Info, E, Obj, Sub, Handler);4553}4554 4555/// Find the position where two subobject designators diverge, or equivalently4556/// the length of the common initial subsequence.4557static unsigned FindDesignatorMismatch(QualType ObjType,4558 const SubobjectDesignator &A,4559 const SubobjectDesignator &B,4560 bool &WasArrayIndex) {4561 unsigned I = 0, N = std::min(A.Entries.size(), B.Entries.size());4562 for (/**/; I != N; ++I) {4563 if (!ObjType.isNull() &&4564 (ObjType->isArrayType() || ObjType->isAnyComplexType())) {4565 // Next subobject is an array element.4566 if (A.Entries[I].getAsArrayIndex() != B.Entries[I].getAsArrayIndex()) {4567 WasArrayIndex = true;4568 return I;4569 }4570 if (ObjType->isAnyComplexType())4571 ObjType = ObjType->castAs<ComplexType>()->getElementType();4572 else4573 ObjType = ObjType->castAsArrayTypeUnsafe()->getElementType();4574 } else {4575 if (A.Entries[I].getAsBaseOrMember() !=4576 B.Entries[I].getAsBaseOrMember()) {4577 WasArrayIndex = false;4578 return I;4579 }4580 if (const FieldDecl *FD = getAsField(A.Entries[I]))4581 // Next subobject is a field.4582 ObjType = FD->getType();4583 else4584 // Next subobject is a base class.4585 ObjType = QualType();4586 }4587 }4588 WasArrayIndex = false;4589 return I;4590}4591 4592/// Determine whether the given subobject designators refer to elements of the4593/// same array object.4594static bool AreElementsOfSameArray(QualType ObjType,4595 const SubobjectDesignator &A,4596 const SubobjectDesignator &B) {4597 if (A.Entries.size() != B.Entries.size())4598 return false;4599 4600 bool IsArray = A.MostDerivedIsArrayElement;4601 if (IsArray && A.MostDerivedPathLength != A.Entries.size())4602 // A is a subobject of the array element.4603 return false;4604 4605 // If A (and B) designates an array element, the last entry will be the array4606 // index. That doesn't have to match. Otherwise, we're in the 'implicit array4607 // of length 1' case, and the entire path must match.4608 bool WasArrayIndex;4609 unsigned CommonLength = FindDesignatorMismatch(ObjType, A, B, WasArrayIndex);4610 return CommonLength >= A.Entries.size() - IsArray;4611}4612 4613/// Find the complete object to which an LValue refers.4614static CompleteObject findCompleteObject(EvalInfo &Info, const Expr *E,4615 AccessKinds AK, const LValue &LVal,4616 QualType LValType) {4617 if (LVal.InvalidBase) {4618 Info.FFDiag(E);4619 return CompleteObject();4620 }4621 4622 if (!LVal.Base) {4623 if (AK == AccessKinds::AK_Dereference)4624 Info.FFDiag(E, diag::note_constexpr_dereferencing_null);4625 else4626 Info.FFDiag(E, diag::note_constexpr_access_null) << AK;4627 return CompleteObject();4628 }4629 4630 CallStackFrame *Frame = nullptr;4631 unsigned Depth = 0;4632 if (LVal.getLValueCallIndex()) {4633 std::tie(Frame, Depth) =4634 Info.getCallFrameAndDepth(LVal.getLValueCallIndex());4635 if (!Frame) {4636 Info.FFDiag(E, diag::note_constexpr_lifetime_ended, 1)4637 << AK << LVal.Base.is<const ValueDecl*>();4638 NoteLValueLocation(Info, LVal.Base);4639 return CompleteObject();4640 }4641 }4642 4643 bool IsAccess = isAnyAccess(AK);4644 4645 // C++11 DR1311: An lvalue-to-rvalue conversion on a volatile-qualified type4646 // is not a constant expression (even if the object is non-volatile). We also4647 // apply this rule to C++98, in order to conform to the expected 'volatile'4648 // semantics.4649 if (isFormalAccess(AK) && LValType.isVolatileQualified()) {4650 if (Info.getLangOpts().CPlusPlus)4651 Info.FFDiag(E, diag::note_constexpr_access_volatile_type)4652 << AK << LValType;4653 else4654 Info.FFDiag(E);4655 return CompleteObject();4656 }4657 4658 // Compute value storage location and type of base object.4659 APValue *BaseVal = nullptr;4660 QualType BaseType = getType(LVal.Base);4661 4662 if (Info.getLangOpts().CPlusPlus14 && LVal.Base == Info.EvaluatingDecl &&4663 lifetimeStartedInEvaluation(Info, LVal.Base)) {4664 // This is the object whose initializer we're evaluating, so its lifetime4665 // started in the current evaluation.4666 BaseVal = Info.EvaluatingDeclValue;4667 } else if (const ValueDecl *D = LVal.Base.dyn_cast<const ValueDecl *>()) {4668 // Allow reading from a GUID declaration.4669 if (auto *GD = dyn_cast<MSGuidDecl>(D)) {4670 if (isModification(AK)) {4671 // All the remaining cases do not permit modification of the object.4672 Info.FFDiag(E, diag::note_constexpr_modify_global);4673 return CompleteObject();4674 }4675 APValue &V = GD->getAsAPValue();4676 if (V.isAbsent()) {4677 Info.FFDiag(E, diag::note_constexpr_unsupported_layout)4678 << GD->getType();4679 return CompleteObject();4680 }4681 return CompleteObject(LVal.Base, &V, GD->getType());4682 }4683 4684 // Allow reading the APValue from an UnnamedGlobalConstantDecl.4685 if (auto *GCD = dyn_cast<UnnamedGlobalConstantDecl>(D)) {4686 if (isModification(AK)) {4687 Info.FFDiag(E, diag::note_constexpr_modify_global);4688 return CompleteObject();4689 }4690 return CompleteObject(LVal.Base, const_cast<APValue *>(&GCD->getValue()),4691 GCD->getType());4692 }4693 4694 // Allow reading from template parameter objects.4695 if (auto *TPO = dyn_cast<TemplateParamObjectDecl>(D)) {4696 if (isModification(AK)) {4697 Info.FFDiag(E, diag::note_constexpr_modify_global);4698 return CompleteObject();4699 }4700 return CompleteObject(LVal.Base, const_cast<APValue *>(&TPO->getValue()),4701 TPO->getType());4702 }4703 4704 // In C++98, const, non-volatile integers initialized with ICEs are ICEs.4705 // In C++11, constexpr, non-volatile variables initialized with constant4706 // expressions are constant expressions too. Inside constexpr functions,4707 // parameters are constant expressions even if they're non-const.4708 // In C++1y, objects local to a constant expression (those with a Frame) are4709 // both readable and writable inside constant expressions.4710 // In C, such things can also be folded, although they are not ICEs.4711 const VarDecl *VD = dyn_cast<VarDecl>(D);4712 if (VD) {4713 if (const VarDecl *VDef = VD->getDefinition(Info.Ctx))4714 VD = VDef;4715 }4716 if (!VD || VD->isInvalidDecl()) {4717 Info.FFDiag(E);4718 return CompleteObject();4719 }4720 4721 bool IsConstant = BaseType.isConstant(Info.Ctx);4722 bool ConstexprVar = false;4723 if (const auto *VD = dyn_cast_if_present<VarDecl>(4724 Info.EvaluatingDecl.dyn_cast<const ValueDecl *>()))4725 ConstexprVar = VD->isConstexpr();4726 4727 // Unless we're looking at a local variable or argument in a constexpr call,4728 // the variable we're reading must be const (unless we are binding to a4729 // reference).4730 if (AK != clang::AK_Dereference && !Frame) {4731 if (IsAccess && isa<ParmVarDecl>(VD)) {4732 // Access of a parameter that's not associated with a frame isn't going4733 // to work out, but we can leave it to evaluateVarDeclInit to provide a4734 // suitable diagnostic.4735 } else if (Info.getLangOpts().CPlusPlus14 &&4736 lifetimeStartedInEvaluation(Info, LVal.Base)) {4737 // OK, we can read and modify an object if we're in the process of4738 // evaluating its initializer, because its lifetime began in this4739 // evaluation.4740 } else if (isModification(AK)) {4741 // All the remaining cases do not permit modification of the object.4742 Info.FFDiag(E, diag::note_constexpr_modify_global);4743 return CompleteObject();4744 } else if (VD->isConstexpr()) {4745 // OK, we can read this variable.4746 } else if (Info.getLangOpts().C23 && ConstexprVar) {4747 Info.FFDiag(E);4748 return CompleteObject();4749 } else if (BaseType->isIntegralOrEnumerationType()) {4750 if (!IsConstant) {4751 if (!IsAccess)4752 return CompleteObject(LVal.getLValueBase(), nullptr, BaseType);4753 if (Info.getLangOpts().CPlusPlus) {4754 Info.FFDiag(E, diag::note_constexpr_ltor_non_const_int, 1) << VD;4755 Info.Note(VD->getLocation(), diag::note_declared_at);4756 } else {4757 Info.FFDiag(E);4758 }4759 return CompleteObject();4760 }4761 } else if (!IsAccess) {4762 return CompleteObject(LVal.getLValueBase(), nullptr, BaseType);4763 } else if ((IsConstant || BaseType->isReferenceType()) &&4764 Info.checkingPotentialConstantExpression() &&4765 BaseType->isLiteralType(Info.Ctx) && !VD->hasDefinition()) {4766 // This variable might end up being constexpr. Don't diagnose it yet.4767 } else if (IsConstant) {4768 // Keep evaluating to see what we can do. In particular, we support4769 // folding of const floating-point types, in order to make static const4770 // data members of such types (supported as an extension) more useful.4771 if (Info.getLangOpts().CPlusPlus) {4772 Info.CCEDiag(E, Info.getLangOpts().CPlusPlus114773 ? diag::note_constexpr_ltor_non_constexpr4774 : diag::note_constexpr_ltor_non_integral, 1)4775 << VD << BaseType;4776 Info.Note(VD->getLocation(), diag::note_declared_at);4777 } else {4778 Info.CCEDiag(E);4779 }4780 } else {4781 // Never allow reading a non-const value.4782 if (Info.getLangOpts().CPlusPlus) {4783 Info.FFDiag(E, Info.getLangOpts().CPlusPlus114784 ? diag::note_constexpr_ltor_non_constexpr4785 : diag::note_constexpr_ltor_non_integral, 1)4786 << VD << BaseType;4787 Info.Note(VD->getLocation(), diag::note_declared_at);4788 } else {4789 Info.FFDiag(E);4790 }4791 return CompleteObject();4792 }4793 }4794 4795 // When binding to a reference, the variable does not need to be constexpr4796 // or have constant initalization.4797 if (AK != clang::AK_Dereference &&4798 !evaluateVarDeclInit(Info, E, VD, Frame, LVal.getLValueVersion(),4799 BaseVal))4800 return CompleteObject();4801 // If evaluateVarDeclInit sees a constexpr-unknown variable, it returns4802 // a null BaseVal. Any constexpr-unknown variable seen here is an error:4803 // we can't access a constexpr-unknown object.4804 if (AK != clang::AK_Dereference && !BaseVal) {4805 if (!Info.checkingPotentialConstantExpression()) {4806 Info.FFDiag(E, diag::note_constexpr_access_unknown_variable, 1)4807 << AK << VD;4808 Info.Note(VD->getLocation(), diag::note_declared_at);4809 }4810 return CompleteObject();4811 }4812 } else if (DynamicAllocLValue DA = LVal.Base.dyn_cast<DynamicAllocLValue>()) {4813 std::optional<DynAlloc *> Alloc = Info.lookupDynamicAlloc(DA);4814 if (!Alloc) {4815 Info.FFDiag(E, diag::note_constexpr_access_deleted_object) << AK;4816 return CompleteObject();4817 }4818 return CompleteObject(LVal.Base, &(*Alloc)->Value,4819 LVal.Base.getDynamicAllocType());4820 }4821 // When binding to a reference, the variable does not need to be4822 // within its lifetime.4823 else if (AK != clang::AK_Dereference) {4824 const Expr *Base = LVal.Base.dyn_cast<const Expr*>();4825 4826 if (!Frame) {4827 if (const MaterializeTemporaryExpr *MTE =4828 dyn_cast_or_null<MaterializeTemporaryExpr>(Base)) {4829 assert(MTE->getStorageDuration() == SD_Static &&4830 "should have a frame for a non-global materialized temporary");4831 4832 // C++20 [expr.const]p4: [DR2126]4833 // An object or reference is usable in constant expressions if it is4834 // - a temporary object of non-volatile const-qualified literal type4835 // whose lifetime is extended to that of a variable that is usable4836 // in constant expressions4837 //4838 // C++20 [expr.const]p5:4839 // an lvalue-to-rvalue conversion [is not allowed unless it applies to]4840 // - a non-volatile glvalue that refers to an object that is usable4841 // in constant expressions, or4842 // - a non-volatile glvalue of literal type that refers to a4843 // non-volatile object whose lifetime began within the evaluation4844 // of E;4845 //4846 // C++11 misses the 'began within the evaluation of e' check and4847 // instead allows all temporaries, including things like:4848 // int &&r = 1;4849 // int x = ++r;4850 // constexpr int k = r;4851 // Therefore we use the C++14-onwards rules in C++11 too.4852 //4853 // Note that temporaries whose lifetimes began while evaluating a4854 // variable's constructor are not usable while evaluating the4855 // corresponding destructor, not even if they're of const-qualified4856 // types.4857 if (!MTE->isUsableInConstantExpressions(Info.Ctx) &&4858 !lifetimeStartedInEvaluation(Info, LVal.Base)) {4859 if (!IsAccess)4860 return CompleteObject(LVal.getLValueBase(), nullptr, BaseType);4861 Info.FFDiag(E, diag::note_constexpr_access_static_temporary, 1) << AK;4862 Info.Note(MTE->getExprLoc(), diag::note_constexpr_temporary_here);4863 return CompleteObject();4864 }4865 4866 BaseVal = MTE->getOrCreateValue(false);4867 assert(BaseVal && "got reference to unevaluated temporary");4868 } else if (const CompoundLiteralExpr *CLE =4869 dyn_cast_or_null<CompoundLiteralExpr>(Base)) {4870 // According to GCC info page:4871 //4872 // 6.28 Compound Literals4873 //4874 // As an optimization, G++ sometimes gives array compound literals4875 // longer lifetimes: when the array either appears outside a function or4876 // has a const-qualified type. If foo and its initializer had elements4877 // of type char *const rather than char *, or if foo were a global4878 // variable, the array would have static storage duration. But it is4879 // probably safest just to avoid the use of array compound literals in4880 // C++ code.4881 //4882 // Obey that rule by checking constness for converted array types.4883 if (QualType CLETy = CLE->getType(); CLETy->isArrayType() &&4884 !LValType->isArrayType() &&4885 !CLETy.isConstant(Info.Ctx)) {4886 Info.FFDiag(E);4887 Info.Note(CLE->getExprLoc(), diag::note_declared_at);4888 return CompleteObject();4889 }4890 4891 BaseVal = &CLE->getStaticValue();4892 } else {4893 if (!IsAccess)4894 return CompleteObject(LVal.getLValueBase(), nullptr, BaseType);4895 APValue Val;4896 LVal.moveInto(Val);4897 Info.FFDiag(E, diag::note_constexpr_access_unreadable_object)4898 << AK4899 << Val.getAsString(Info.Ctx,4900 Info.Ctx.getLValueReferenceType(LValType));4901 NoteLValueLocation(Info, LVal.Base);4902 return CompleteObject();4903 }4904 } else if (AK != clang::AK_Dereference) {4905 BaseVal = Frame->getTemporary(Base, LVal.Base.getVersion());4906 assert(BaseVal && "missing value for temporary");4907 }4908 }4909 4910 // In C++14, we can't safely access any mutable state when we might be4911 // evaluating after an unmodeled side effect. Parameters are modeled as state4912 // in the caller, but aren't visible once the call returns, so they can be4913 // modified in a speculatively-evaluated call.4914 //4915 // FIXME: Not all local state is mutable. Allow local constant subobjects4916 // to be read here (but take care with 'mutable' fields).4917 unsigned VisibleDepth = Depth;4918 if (llvm::isa_and_nonnull<ParmVarDecl>(4919 LVal.Base.dyn_cast<const ValueDecl *>()))4920 ++VisibleDepth;4921 if ((Frame && Info.getLangOpts().CPlusPlus14 &&4922 Info.EvalStatus.HasSideEffects) ||4923 (isModification(AK) && VisibleDepth < Info.SpeculativeEvaluationDepth))4924 return CompleteObject();4925 4926 return CompleteObject(LVal.getLValueBase(), BaseVal, BaseType);4927}4928 4929/// Perform an lvalue-to-rvalue conversion on the given glvalue. This4930/// can also be used for 'lvalue-to-lvalue' conversions for looking up the4931/// glvalue referred to by an entity of reference type.4932///4933/// \param Info - Information about the ongoing evaluation.4934/// \param Conv - The expression for which we are performing the conversion.4935/// Used for diagnostics.4936/// \param Type - The type of the glvalue (before stripping cv-qualifiers in the4937/// case of a non-class type).4938/// \param LVal - The glvalue on which we are attempting to perform this action.4939/// \param RVal - The produced value will be placed here.4940/// \param WantObjectRepresentation - If true, we're looking for the object4941/// representation rather than the value, and in particular,4942/// there is no requirement that the result be fully initialized.4943static bool4944handleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv, QualType Type,4945 const LValue &LVal, APValue &RVal,4946 bool WantObjectRepresentation = false) {4947 if (LVal.Designator.Invalid)4948 return false;4949 4950 // Check for special cases where there is no existing APValue to look at.4951 const Expr *Base = LVal.Base.dyn_cast<const Expr*>();4952 4953 AccessKinds AK =4954 WantObjectRepresentation ? AK_ReadObjectRepresentation : AK_Read;4955 4956 if (Base && !LVal.getLValueCallIndex() && !Type.isVolatileQualified()) {4957 if (isa<StringLiteral>(Base) || isa<PredefinedExpr>(Base)) {4958 // Special-case character extraction so we don't have to construct an4959 // APValue for the whole string.4960 assert(LVal.Designator.Entries.size() <= 1 &&4961 "Can only read characters from string literals");4962 if (LVal.Designator.Entries.empty()) {4963 // Fail for now for LValue to RValue conversion of an array.4964 // (This shouldn't show up in C/C++, but it could be triggered by a4965 // weird EvaluateAsRValue call from a tool.)4966 Info.FFDiag(Conv);4967 return false;4968 }4969 if (LVal.Designator.isOnePastTheEnd()) {4970 if (Info.getLangOpts().CPlusPlus11)4971 Info.FFDiag(Conv, diag::note_constexpr_access_past_end) << AK;4972 else4973 Info.FFDiag(Conv);4974 return false;4975 }4976 uint64_t CharIndex = LVal.Designator.Entries[0].getAsArrayIndex();4977 RVal = APValue(extractStringLiteralCharacter(Info, Base, CharIndex));4978 return true;4979 }4980 }4981 4982 CompleteObject Obj = findCompleteObject(Info, Conv, AK, LVal, Type);4983 return Obj && extractSubobject(Info, Conv, Obj, LVal.Designator, RVal, AK);4984}4985 4986static bool hlslElementwiseCastHelper(EvalInfo &Info, const Expr *E,4987 QualType DestTy,4988 SmallVectorImpl<APValue> &SrcVals,4989 SmallVectorImpl<QualType> &SrcTypes) {4990 APValue Val;4991 if (!Evaluate(Val, Info, E))4992 return false;4993 4994 // must be dealing with a record4995 if (Val.isLValue()) {4996 LValue LVal;4997 LVal.setFrom(Info.Ctx, Val);4998 if (!handleLValueToRValueConversion(Info, E, E->getType(), LVal, Val))4999 return false;5000 }5001 5002 unsigned NEls = elementwiseSize(Info, DestTy);5003 // flatten the source5004 if (!flattenAPValue(Info, E, Val, E->getType(), SrcVals, SrcTypes, NEls))5005 return false;5006 5007 return true;5008}5009 5010/// Perform an assignment of Val to LVal. Takes ownership of Val.5011static bool handleAssignment(EvalInfo &Info, const Expr *E, const LValue &LVal,5012 QualType LValType, APValue &Val) {5013 if (LVal.Designator.Invalid)5014 return false;5015 5016 if (!Info.getLangOpts().CPlusPlus14) {5017 Info.FFDiag(E);5018 return false;5019 }5020 5021 CompleteObject Obj = findCompleteObject(Info, E, AK_Assign, LVal, LValType);5022 return Obj && modifySubobject(Info, E, Obj, LVal.Designator, Val);5023}5024 5025namespace {5026struct CompoundAssignSubobjectHandler {5027 EvalInfo &Info;5028 const CompoundAssignOperator *E;5029 QualType PromotedLHSType;5030 BinaryOperatorKind Opcode;5031 const APValue &RHS;5032 5033 static const AccessKinds AccessKind = AK_Assign;5034 5035 typedef bool result_type;5036 5037 bool checkConst(QualType QT) {5038 // Assigning to a const object has undefined behavior.5039 if (QT.isConstQualified()) {5040 Info.FFDiag(E, diag::note_constexpr_modify_const_type) << QT;5041 return false;5042 }5043 return true;5044 }5045 5046 bool failed() { return false; }5047 bool found(APValue &Subobj, QualType SubobjType) {5048 switch (Subobj.getKind()) {5049 case APValue::Int:5050 return found(Subobj.getInt(), SubobjType);5051 case APValue::Float:5052 return found(Subobj.getFloat(), SubobjType);5053 case APValue::ComplexInt:5054 case APValue::ComplexFloat:5055 // FIXME: Implement complex compound assignment.5056 Info.FFDiag(E);5057 return false;5058 case APValue::LValue:5059 return foundPointer(Subobj, SubobjType);5060 case APValue::Vector:5061 return foundVector(Subobj, SubobjType);5062 case APValue::Indeterminate:5063 Info.FFDiag(E, diag::note_constexpr_access_uninit)5064 << /*read of=*/0 << /*uninitialized object=*/15065 << E->getLHS()->getSourceRange();5066 return false;5067 default:5068 // FIXME: can this happen?5069 Info.FFDiag(E);5070 return false;5071 }5072 }5073 5074 bool foundVector(APValue &Value, QualType SubobjType) {5075 if (!checkConst(SubobjType))5076 return false;5077 5078 if (!SubobjType->isVectorType()) {5079 Info.FFDiag(E);5080 return false;5081 }5082 return handleVectorVectorBinOp(Info, E, Opcode, Value, RHS);5083 }5084 5085 bool found(APSInt &Value, QualType SubobjType) {5086 if (!checkConst(SubobjType))5087 return false;5088 5089 if (!SubobjType->isIntegerType()) {5090 // We don't support compound assignment on integer-cast-to-pointer5091 // values.5092 Info.FFDiag(E);5093 return false;5094 }5095 5096 if (RHS.isInt()) {5097 APSInt LHS =5098 HandleIntToIntCast(Info, E, PromotedLHSType, SubobjType, Value);5099 if (!handleIntIntBinOp(Info, E, LHS, Opcode, RHS.getInt(), LHS))5100 return false;5101 Value = HandleIntToIntCast(Info, E, SubobjType, PromotedLHSType, LHS);5102 return true;5103 } else if (RHS.isFloat()) {5104 const FPOptions FPO = E->getFPFeaturesInEffect(5105 Info.Ctx.getLangOpts());5106 APFloat FValue(0.0);5107 return HandleIntToFloatCast(Info, E, FPO, SubobjType, Value,5108 PromotedLHSType, FValue) &&5109 handleFloatFloatBinOp(Info, E, FValue, Opcode, RHS.getFloat()) &&5110 HandleFloatToIntCast(Info, E, PromotedLHSType, FValue, SubobjType,5111 Value);5112 }5113 5114 Info.FFDiag(E);5115 return false;5116 }5117 bool found(APFloat &Value, QualType SubobjType) {5118 return checkConst(SubobjType) &&5119 HandleFloatToFloatCast(Info, E, SubobjType, PromotedLHSType,5120 Value) &&5121 handleFloatFloatBinOp(Info, E, Value, Opcode, RHS.getFloat()) &&5122 HandleFloatToFloatCast(Info, E, PromotedLHSType, SubobjType, Value);5123 }5124 bool foundPointer(APValue &Subobj, QualType SubobjType) {5125 if (!checkConst(SubobjType))5126 return false;5127 5128 QualType PointeeType;5129 if (const PointerType *PT = SubobjType->getAs<PointerType>())5130 PointeeType = PT->getPointeeType();5131 5132 if (PointeeType.isNull() || !RHS.isInt() ||5133 (Opcode != BO_Add && Opcode != BO_Sub)) {5134 Info.FFDiag(E);5135 return false;5136 }5137 5138 APSInt Offset = RHS.getInt();5139 if (Opcode == BO_Sub)5140 negateAsSigned(Offset);5141 5142 LValue LVal;5143 LVal.setFrom(Info.Ctx, Subobj);5144 if (!HandleLValueArrayAdjustment(Info, E, LVal, PointeeType, Offset))5145 return false;5146 LVal.moveInto(Subobj);5147 return true;5148 }5149};5150} // end anonymous namespace5151 5152const AccessKinds CompoundAssignSubobjectHandler::AccessKind;5153 5154/// Perform a compound assignment of LVal <op>= RVal.5155static bool handleCompoundAssignment(EvalInfo &Info,5156 const CompoundAssignOperator *E,5157 const LValue &LVal, QualType LValType,5158 QualType PromotedLValType,5159 BinaryOperatorKind Opcode,5160 const APValue &RVal) {5161 if (LVal.Designator.Invalid)5162 return false;5163 5164 if (!Info.getLangOpts().CPlusPlus14) {5165 Info.FFDiag(E);5166 return false;5167 }5168 5169 CompleteObject Obj = findCompleteObject(Info, E, AK_Assign, LVal, LValType);5170 CompoundAssignSubobjectHandler Handler = { Info, E, PromotedLValType, Opcode,5171 RVal };5172 return Obj && findSubobject(Info, E, Obj, LVal.Designator, Handler);5173}5174 5175namespace {5176struct IncDecSubobjectHandler {5177 EvalInfo &Info;5178 const UnaryOperator *E;5179 AccessKinds AccessKind;5180 APValue *Old;5181 5182 typedef bool result_type;5183 5184 bool checkConst(QualType QT) {5185 // Assigning to a const object has undefined behavior.5186 if (QT.isConstQualified()) {5187 Info.FFDiag(E, diag::note_constexpr_modify_const_type) << QT;5188 return false;5189 }5190 return true;5191 }5192 5193 bool failed() { return false; }5194 bool found(APValue &Subobj, QualType SubobjType) {5195 // Stash the old value. Also clear Old, so we don't clobber it later5196 // if we're post-incrementing a complex.5197 if (Old) {5198 *Old = Subobj;5199 Old = nullptr;5200 }5201 5202 switch (Subobj.getKind()) {5203 case APValue::Int:5204 return found(Subobj.getInt(), SubobjType);5205 case APValue::Float:5206 return found(Subobj.getFloat(), SubobjType);5207 case APValue::ComplexInt:5208 return found(Subobj.getComplexIntReal(),5209 SubobjType->castAs<ComplexType>()->getElementType()5210 .withCVRQualifiers(SubobjType.getCVRQualifiers()));5211 case APValue::ComplexFloat:5212 return found(Subobj.getComplexFloatReal(),5213 SubobjType->castAs<ComplexType>()->getElementType()5214 .withCVRQualifiers(SubobjType.getCVRQualifiers()));5215 case APValue::LValue:5216 return foundPointer(Subobj, SubobjType);5217 default:5218 // FIXME: can this happen?5219 Info.FFDiag(E);5220 return false;5221 }5222 }5223 bool found(APSInt &Value, QualType SubobjType) {5224 if (!checkConst(SubobjType))5225 return false;5226 5227 if (!SubobjType->isIntegerType()) {5228 // We don't support increment / decrement on integer-cast-to-pointer5229 // values.5230 Info.FFDiag(E);5231 return false;5232 }5233 5234 if (Old) *Old = APValue(Value);5235 5236 // bool arithmetic promotes to int, and the conversion back to bool5237 // doesn't reduce mod 2^n, so special-case it.5238 if (SubobjType->isBooleanType()) {5239 if (AccessKind == AK_Increment)5240 Value = 1;5241 else5242 Value = !Value;5243 return true;5244 }5245 5246 bool WasNegative = Value.isNegative();5247 if (AccessKind == AK_Increment) {5248 ++Value;5249 5250 if (!WasNegative && Value.isNegative() && E->canOverflow()) {5251 APSInt ActualValue(Value, /*IsUnsigned*/true);5252 return HandleOverflow(Info, E, ActualValue, SubobjType);5253 }5254 } else {5255 --Value;5256 5257 if (WasNegative && !Value.isNegative() && E->canOverflow()) {5258 unsigned BitWidth = Value.getBitWidth();5259 APSInt ActualValue(Value.sext(BitWidth + 1), /*IsUnsigned*/false);5260 ActualValue.setBit(BitWidth);5261 return HandleOverflow(Info, E, ActualValue, SubobjType);5262 }5263 }5264 return true;5265 }5266 bool found(APFloat &Value, QualType SubobjType) {5267 if (!checkConst(SubobjType))5268 return false;5269 5270 if (Old) *Old = APValue(Value);5271 5272 APFloat One(Value.getSemantics(), 1);5273 llvm::RoundingMode RM = getActiveRoundingMode(Info, E);5274 APFloat::opStatus St;5275 if (AccessKind == AK_Increment)5276 St = Value.add(One, RM);5277 else5278 St = Value.subtract(One, RM);5279 return checkFloatingPointResult(Info, E, St);5280 }5281 bool foundPointer(APValue &Subobj, QualType SubobjType) {5282 if (!checkConst(SubobjType))5283 return false;5284 5285 QualType PointeeType;5286 if (const PointerType *PT = SubobjType->getAs<PointerType>())5287 PointeeType = PT->getPointeeType();5288 else {5289 Info.FFDiag(E);5290 return false;5291 }5292 5293 LValue LVal;5294 LVal.setFrom(Info.Ctx, Subobj);5295 if (!HandleLValueArrayAdjustment(Info, E, LVal, PointeeType,5296 AccessKind == AK_Increment ? 1 : -1))5297 return false;5298 LVal.moveInto(Subobj);5299 return true;5300 }5301};5302} // end anonymous namespace5303 5304/// Perform an increment or decrement on LVal.5305static bool handleIncDec(EvalInfo &Info, const Expr *E, const LValue &LVal,5306 QualType LValType, bool IsIncrement, APValue *Old) {5307 if (LVal.Designator.Invalid)5308 return false;5309 5310 if (!Info.getLangOpts().CPlusPlus14) {5311 Info.FFDiag(E);5312 return false;5313 }5314 5315 AccessKinds AK = IsIncrement ? AK_Increment : AK_Decrement;5316 CompleteObject Obj = findCompleteObject(Info, E, AK, LVal, LValType);5317 IncDecSubobjectHandler Handler = {Info, cast<UnaryOperator>(E), AK, Old};5318 return Obj && findSubobject(Info, E, Obj, LVal.Designator, Handler);5319}5320 5321/// Build an lvalue for the object argument of a member function call.5322static bool EvaluateObjectArgument(EvalInfo &Info, const Expr *Object,5323 LValue &This) {5324 if (Object->getType()->isPointerType() && Object->isPRValue())5325 return EvaluatePointer(Object, This, Info);5326 5327 if (Object->isGLValue())5328 return EvaluateLValue(Object, This, Info);5329 5330 if (Object->getType()->isLiteralType(Info.Ctx))5331 return EvaluateTemporary(Object, This, Info);5332 5333 if (Object->getType()->isRecordType() && Object->isPRValue())5334 return EvaluateTemporary(Object, This, Info);5335 5336 Info.FFDiag(Object, diag::note_constexpr_nonliteral) << Object->getType();5337 return false;5338}5339 5340/// HandleMemberPointerAccess - Evaluate a member access operation and build an5341/// lvalue referring to the result.5342///5343/// \param Info - Information about the ongoing evaluation.5344/// \param LV - An lvalue referring to the base of the member pointer.5345/// \param RHS - The member pointer expression.5346/// \param IncludeMember - Specifies whether the member itself is included in5347/// the resulting LValue subobject designator. This is not possible when5348/// creating a bound member function.5349/// \return The field or method declaration to which the member pointer refers,5350/// or 0 if evaluation fails.5351static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info,5352 QualType LVType,5353 LValue &LV,5354 const Expr *RHS,5355 bool IncludeMember = true) {5356 MemberPtr MemPtr;5357 if (!EvaluateMemberPointer(RHS, MemPtr, Info))5358 return nullptr;5359 5360 // C++11 [expr.mptr.oper]p6: If the second operand is the null pointer to5361 // member value, the behavior is undefined.5362 if (!MemPtr.getDecl()) {5363 // FIXME: Specific diagnostic.5364 Info.FFDiag(RHS);5365 return nullptr;5366 }5367 5368 if (MemPtr.isDerivedMember()) {5369 // This is a member of some derived class. Truncate LV appropriately.5370 // The end of the derived-to-base path for the base object must match the5371 // derived-to-base path for the member pointer.5372 // C++23 [expr.mptr.oper]p4:5373 // If the result of E1 is an object [...] whose most derived object does5374 // not contain the member to which E2 refers, the behavior is undefined.5375 if (LV.Designator.MostDerivedPathLength + MemPtr.Path.size() >5376 LV.Designator.Entries.size()) {5377 Info.FFDiag(RHS);5378 return nullptr;5379 }5380 unsigned PathLengthToMember =5381 LV.Designator.Entries.size() - MemPtr.Path.size();5382 for (unsigned I = 0, N = MemPtr.Path.size(); I != N; ++I) {5383 const CXXRecordDecl *LVDecl = getAsBaseClass(5384 LV.Designator.Entries[PathLengthToMember + I]);5385 const CXXRecordDecl *MPDecl = MemPtr.Path[I];5386 if (LVDecl->getCanonicalDecl() != MPDecl->getCanonicalDecl()) {5387 Info.FFDiag(RHS);5388 return nullptr;5389 }5390 }5391 // MemPtr.Path only contains the base classes of the class directly5392 // containing the member E2. It is still necessary to check that the class5393 // directly containing the member E2 lies on the derived-to-base path of E15394 // to avoid incorrectly permitting member pointer access into a sibling5395 // class of the class containing the member E2. If this class would5396 // correspond to the most-derived class of E1, it either isn't contained in5397 // LV.Designator.Entries or the corresponding entry refers to an array5398 // element instead. Therefore get the most derived class directly in this5399 // case. Otherwise the previous entry should correpond to this class.5400 const CXXRecordDecl *LastLVDecl =5401 (PathLengthToMember > LV.Designator.MostDerivedPathLength)5402 ? getAsBaseClass(LV.Designator.Entries[PathLengthToMember - 1])5403 : LV.Designator.MostDerivedType->getAsCXXRecordDecl();5404 const CXXRecordDecl *LastMPDecl = MemPtr.getContainingRecord();5405 if (LastLVDecl->getCanonicalDecl() != LastMPDecl->getCanonicalDecl()) {5406 Info.FFDiag(RHS);5407 return nullptr;5408 }5409 5410 // Truncate the lvalue to the appropriate derived class.5411 if (!CastToDerivedClass(Info, RHS, LV, MemPtr.getContainingRecord(),5412 PathLengthToMember))5413 return nullptr;5414 } else if (!MemPtr.Path.empty()) {5415 // Extend the LValue path with the member pointer's path.5416 LV.Designator.Entries.reserve(LV.Designator.Entries.size() +5417 MemPtr.Path.size() + IncludeMember);5418 5419 // Walk down to the appropriate base class.5420 if (const PointerType *PT = LVType->getAs<PointerType>())5421 LVType = PT->getPointeeType();5422 const CXXRecordDecl *RD = LVType->getAsCXXRecordDecl();5423 assert(RD && "member pointer access on non-class-type expression");5424 // The first class in the path is that of the lvalue.5425 for (unsigned I = 1, N = MemPtr.Path.size(); I != N; ++I) {5426 const CXXRecordDecl *Base = MemPtr.Path[N - I - 1];5427 if (!HandleLValueDirectBase(Info, RHS, LV, RD, Base))5428 return nullptr;5429 RD = Base;5430 }5431 // Finally cast to the class containing the member.5432 if (!HandleLValueDirectBase(Info, RHS, LV, RD,5433 MemPtr.getContainingRecord()))5434 return nullptr;5435 }5436 5437 // Add the member. Note that we cannot build bound member functions here.5438 if (IncludeMember) {5439 if (const FieldDecl *FD = dyn_cast<FieldDecl>(MemPtr.getDecl())) {5440 if (!HandleLValueMember(Info, RHS, LV, FD))5441 return nullptr;5442 } else if (const IndirectFieldDecl *IFD =5443 dyn_cast<IndirectFieldDecl>(MemPtr.getDecl())) {5444 if (!HandleLValueIndirectMember(Info, RHS, LV, IFD))5445 return nullptr;5446 } else {5447 llvm_unreachable("can't construct reference to bound member function");5448 }5449 }5450 5451 return MemPtr.getDecl();5452}5453 5454static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info,5455 const BinaryOperator *BO,5456 LValue &LV,5457 bool IncludeMember = true) {5458 assert(BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI);5459 5460 if (!EvaluateObjectArgument(Info, BO->getLHS(), LV)) {5461 if (Info.noteFailure()) {5462 MemberPtr MemPtr;5463 EvaluateMemberPointer(BO->getRHS(), MemPtr, Info);5464 }5465 return nullptr;5466 }5467 5468 return HandleMemberPointerAccess(Info, BO->getLHS()->getType(), LV,5469 BO->getRHS(), IncludeMember);5470}5471 5472/// HandleBaseToDerivedCast - Apply the given base-to-derived cast operation on5473/// the provided lvalue, which currently refers to the base object.5474static bool HandleBaseToDerivedCast(EvalInfo &Info, const CastExpr *E,5475 LValue &Result) {5476 SubobjectDesignator &D = Result.Designator;5477 if (D.Invalid || !Result.checkNullPointer(Info, E, CSK_Derived))5478 return false;5479 5480 QualType TargetQT = E->getType();5481 if (const PointerType *PT = TargetQT->getAs<PointerType>())5482 TargetQT = PT->getPointeeType();5483 5484 auto InvalidCast = [&]() {5485 if (!Info.checkingPotentialConstantExpression() ||5486 !Result.AllowConstexprUnknown) {5487 Info.CCEDiag(E, diag::note_constexpr_invalid_downcast)5488 << D.MostDerivedType << TargetQT;5489 }5490 return false;5491 };5492 5493 // Check this cast lands within the final derived-to-base subobject path.5494 if (D.MostDerivedPathLength + E->path_size() > D.Entries.size())5495 return InvalidCast();5496 5497 // Check the type of the final cast. We don't need to check the path,5498 // since a cast can only be formed if the path is unique.5499 unsigned NewEntriesSize = D.Entries.size() - E->path_size();5500 const CXXRecordDecl *TargetType = TargetQT->getAsCXXRecordDecl();5501 const CXXRecordDecl *FinalType;5502 if (NewEntriesSize == D.MostDerivedPathLength)5503 FinalType = D.MostDerivedType->getAsCXXRecordDecl();5504 else5505 FinalType = getAsBaseClass(D.Entries[NewEntriesSize - 1]);5506 if (FinalType->getCanonicalDecl() != TargetType->getCanonicalDecl())5507 return InvalidCast();5508 5509 // Truncate the lvalue to the appropriate derived class.5510 return CastToDerivedClass(Info, E, Result, TargetType, NewEntriesSize);5511}5512 5513/// Get the value to use for a default-initialized object of type T.5514/// Return false if it encounters something invalid.5515static bool handleDefaultInitValue(QualType T, APValue &Result) {5516 bool Success = true;5517 5518 // If there is already a value present don't overwrite it.5519 if (!Result.isAbsent())5520 return true;5521 5522 if (auto *RD = T->getAsCXXRecordDecl()) {5523 if (RD->isInvalidDecl()) {5524 Result = APValue();5525 return false;5526 }5527 if (RD->isUnion()) {5528 Result = APValue((const FieldDecl *)nullptr);5529 return true;5530 }5531 Result =5532 APValue(APValue::UninitStruct(), RD->getNumBases(), RD->getNumFields());5533 5534 unsigned Index = 0;5535 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),5536 End = RD->bases_end();5537 I != End; ++I, ++Index)5538 Success &=5539 handleDefaultInitValue(I->getType(), Result.getStructBase(Index));5540 5541 for (const auto *I : RD->fields()) {5542 if (I->isUnnamedBitField())5543 continue;5544 Success &= handleDefaultInitValue(5545 I->getType(), Result.getStructField(I->getFieldIndex()));5546 }5547 return Success;5548 }5549 5550 if (auto *AT =5551 dyn_cast_or_null<ConstantArrayType>(T->getAsArrayTypeUnsafe())) {5552 Result = APValue(APValue::UninitArray(), 0, AT->getZExtSize());5553 if (Result.hasArrayFiller())5554 Success &=5555 handleDefaultInitValue(AT->getElementType(), Result.getArrayFiller());5556 5557 return Success;5558 }5559 5560 Result = APValue::IndeterminateValue();5561 return true;5562}5563 5564namespace {5565enum EvalStmtResult {5566 /// Evaluation failed.5567 ESR_Failed,5568 /// Hit a 'return' statement.5569 ESR_Returned,5570 /// Evaluation succeeded.5571 ESR_Succeeded,5572 /// Hit a 'continue' statement.5573 ESR_Continue,5574 /// Hit a 'break' statement.5575 ESR_Break,5576 /// Still scanning for 'case' or 'default' statement.5577 ESR_CaseNotFound5578};5579}5580/// Evaluates the initializer of a reference.5581static bool EvaluateInitForDeclOfReferenceType(EvalInfo &Info,5582 const ValueDecl *D,5583 const Expr *Init, LValue &Result,5584 APValue &Val) {5585 assert(Init->isGLValue() && D->getType()->isReferenceType());5586 // A reference is an lvalue.5587 if (!EvaluateLValue(Init, Result, Info))5588 return false;5589 // [C++26][decl.ref]5590 // The object designated by such a glvalue can be outside its lifetime5591 // Because a null pointer value or a pointer past the end of an object5592 // does not point to an object, a reference in a well-defined program cannot5593 // refer to such things;5594 if (!Result.Designator.Invalid && Result.Designator.isOnePastTheEnd()) {5595 Info.FFDiag(Init, diag::note_constexpr_access_past_end) << AK_Dereference;5596 return false;5597 }5598 5599 // Save the result.5600 Result.moveInto(Val);5601 return true;5602}5603 5604static bool EvaluateVarDecl(EvalInfo &Info, const VarDecl *VD) {5605 if (VD->isInvalidDecl())5606 return false;5607 // We don't need to evaluate the initializer for a static local.5608 if (!VD->hasLocalStorage())5609 return true;5610 5611 LValue Result;5612 APValue &Val = Info.CurrentCall->createTemporary(VD, VD->getType(),5613 ScopeKind::Block, Result);5614 5615 const Expr *InitE = VD->getInit();5616 if (!InitE) {5617 if (VD->getType()->isDependentType())5618 return Info.noteSideEffect();5619 return handleDefaultInitValue(VD->getType(), Val);5620 }5621 if (InitE->isValueDependent())5622 return false;5623 5624 // For references to objects, check they do not designate a one-past-the-end5625 // object.5626 if (VD->getType()->isReferenceType()) {5627 return EvaluateInitForDeclOfReferenceType(Info, VD, InitE, Result, Val);5628 } else if (!EvaluateInPlace(Val, Info, Result, InitE)) {5629 // Wipe out any partially-computed value, to allow tracking that this5630 // evaluation failed.5631 Val = APValue();5632 return false;5633 }5634 5635 return true;5636}5637 5638static bool EvaluateDecompositionDeclInit(EvalInfo &Info,5639 const DecompositionDecl *DD);5640 5641static bool EvaluateDecl(EvalInfo &Info, const Decl *D,5642 bool EvaluateConditionDecl = false) {5643 bool OK = true;5644 if (const VarDecl *VD = dyn_cast<VarDecl>(D))5645 OK &= EvaluateVarDecl(Info, VD);5646 5647 if (const DecompositionDecl *DD = dyn_cast<DecompositionDecl>(D);5648 EvaluateConditionDecl && DD)5649 OK &= EvaluateDecompositionDeclInit(Info, DD);5650 5651 return OK;5652}5653 5654static bool EvaluateDecompositionDeclInit(EvalInfo &Info,5655 const DecompositionDecl *DD) {5656 bool OK = true;5657 for (auto *BD : DD->flat_bindings())5658 if (auto *VD = BD->getHoldingVar())5659 OK &= EvaluateDecl(Info, VD, /*EvaluateConditionDecl=*/true);5660 5661 return OK;5662}5663 5664static bool MaybeEvaluateDeferredVarDeclInit(EvalInfo &Info,5665 const VarDecl *VD) {5666 if (auto *DD = dyn_cast_if_present<DecompositionDecl>(VD)) {5667 if (!EvaluateDecompositionDeclInit(Info, DD))5668 return false;5669 }5670 return true;5671}5672 5673static bool EvaluateDependentExpr(const Expr *E, EvalInfo &Info) {5674 assert(E->isValueDependent());5675 if (Info.noteSideEffect())5676 return true;5677 assert(E->containsErrors() && "valid value-dependent expression should never "5678 "reach invalid code path.");5679 return false;5680}5681 5682/// Evaluate a condition (either a variable declaration or an expression).5683static bool EvaluateCond(EvalInfo &Info, const VarDecl *CondDecl,5684 const Expr *Cond, bool &Result) {5685 if (Cond->isValueDependent())5686 return false;5687 FullExpressionRAII Scope(Info);5688 if (CondDecl && !EvaluateDecl(Info, CondDecl))5689 return false;5690 if (!EvaluateAsBooleanCondition(Cond, Result, Info))5691 return false;5692 if (!MaybeEvaluateDeferredVarDeclInit(Info, CondDecl))5693 return false;5694 return Scope.destroy();5695}5696 5697namespace {5698/// A location where the result (returned value) of evaluating a5699/// statement should be stored.5700struct StmtResult {5701 /// The APValue that should be filled in with the returned value.5702 APValue &Value;5703 /// The location containing the result, if any (used to support RVO).5704 const LValue *Slot;5705};5706 5707struct TempVersionRAII {5708 CallStackFrame &Frame;5709 5710 TempVersionRAII(CallStackFrame &Frame) : Frame(Frame) {5711 Frame.pushTempVersion();5712 }5713 5714 ~TempVersionRAII() {5715 Frame.popTempVersion();5716 }5717};5718 5719}5720 5721static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info,5722 const Stmt *S,5723 const SwitchCase *SC = nullptr);5724 5725/// Helper to implement named break/continue. Returns 'true' if the evaluation5726/// result should be propagated up. Otherwise, it sets the evaluation result5727/// to either Continue to continue the current loop, or Succeeded to break it.5728static bool ShouldPropagateBreakContinue(EvalInfo &Info,5729 const Stmt *LoopOrSwitch,5730 ArrayRef<BlockScopeRAII *> Scopes,5731 EvalStmtResult &ESR) {5732 bool IsSwitch = isa<SwitchStmt>(LoopOrSwitch);5733 5734 // For loops, map Succeeded to Continue so we don't have to check for both.5735 if (!IsSwitch && ESR == ESR_Succeeded) {5736 ESR = ESR_Continue;5737 return false;5738 }5739 5740 if (ESR != ESR_Break && ESR != ESR_Continue)5741 return false;5742 5743 // Are we breaking out of or continuing this statement?5744 bool CanBreakOrContinue = !IsSwitch || ESR == ESR_Break;5745 const Stmt *StackTop = Info.BreakContinueStack.back();5746 if (CanBreakOrContinue && (StackTop == nullptr || StackTop == LoopOrSwitch)) {5747 Info.BreakContinueStack.pop_back();5748 if (ESR == ESR_Break)5749 ESR = ESR_Succeeded;5750 return false;5751 }5752 5753 // We're not. Propagate the result up.5754 for (BlockScopeRAII *S : Scopes) {5755 if (!S->destroy()) {5756 ESR = ESR_Failed;5757 break;5758 }5759 }5760 return true;5761}5762 5763/// Evaluate the body of a loop, and translate the result as appropriate.5764static EvalStmtResult EvaluateLoopBody(StmtResult &Result, EvalInfo &Info,5765 const Stmt *Body,5766 const SwitchCase *Case = nullptr) {5767 BlockScopeRAII Scope(Info);5768 5769 EvalStmtResult ESR = EvaluateStmt(Result, Info, Body, Case);5770 if (ESR != ESR_Failed && ESR != ESR_CaseNotFound && !Scope.destroy())5771 ESR = ESR_Failed;5772 5773 return ESR;5774}5775 5776/// Evaluate a switch statement.5777static EvalStmtResult EvaluateSwitch(StmtResult &Result, EvalInfo &Info,5778 const SwitchStmt *SS) {5779 BlockScopeRAII Scope(Info);5780 5781 // Evaluate the switch condition.5782 APSInt Value;5783 {5784 if (const Stmt *Init = SS->getInit()) {5785 EvalStmtResult ESR = EvaluateStmt(Result, Info, Init);5786 if (ESR != ESR_Succeeded) {5787 if (ESR != ESR_Failed && !Scope.destroy())5788 ESR = ESR_Failed;5789 return ESR;5790 }5791 }5792 5793 FullExpressionRAII CondScope(Info);5794 if (SS->getConditionVariable() &&5795 !EvaluateDecl(Info, SS->getConditionVariable()))5796 return ESR_Failed;5797 if (SS->getCond()->isValueDependent()) {5798 // We don't know what the value is, and which branch should jump to.5799 EvaluateDependentExpr(SS->getCond(), Info);5800 return ESR_Failed;5801 }5802 if (!EvaluateInteger(SS->getCond(), Value, Info))5803 return ESR_Failed;5804 5805 if (!MaybeEvaluateDeferredVarDeclInit(Info, SS->getConditionVariable()))5806 return ESR_Failed;5807 5808 if (!CondScope.destroy())5809 return ESR_Failed;5810 }5811 5812 // Find the switch case corresponding to the value of the condition.5813 // FIXME: Cache this lookup.5814 const SwitchCase *Found = nullptr;5815 for (const SwitchCase *SC = SS->getSwitchCaseList(); SC;5816 SC = SC->getNextSwitchCase()) {5817 if (isa<DefaultStmt>(SC)) {5818 Found = SC;5819 continue;5820 }5821 5822 const CaseStmt *CS = cast<CaseStmt>(SC);5823 const Expr *LHS = CS->getLHS();5824 const Expr *RHS = CS->getRHS();5825 if (LHS->isValueDependent() || (RHS && RHS->isValueDependent()))5826 return ESR_Failed;5827 APSInt LHSValue = LHS->EvaluateKnownConstInt(Info.Ctx);5828 APSInt RHSValue = RHS ? RHS->EvaluateKnownConstInt(Info.Ctx) : LHSValue;5829 if (LHSValue <= Value && Value <= RHSValue) {5830 Found = SC;5831 break;5832 }5833 }5834 5835 if (!Found)5836 return Scope.destroy() ? ESR_Succeeded : ESR_Failed;5837 5838 // Search the switch body for the switch case and evaluate it from there.5839 EvalStmtResult ESR = EvaluateStmt(Result, Info, SS->getBody(), Found);5840 if (ESR != ESR_Failed && ESR != ESR_CaseNotFound && !Scope.destroy())5841 return ESR_Failed;5842 if (ShouldPropagateBreakContinue(Info, SS, /*Scopes=*/{}, ESR))5843 return ESR;5844 5845 switch (ESR) {5846 case ESR_Break:5847 llvm_unreachable("Should have been converted to Succeeded");5848 case ESR_Succeeded:5849 case ESR_Continue:5850 case ESR_Failed:5851 case ESR_Returned:5852 return ESR;5853 case ESR_CaseNotFound:5854 // This can only happen if the switch case is nested within a statement5855 // expression. We have no intention of supporting that.5856 Info.FFDiag(Found->getBeginLoc(),5857 diag::note_constexpr_stmt_expr_unsupported);5858 return ESR_Failed;5859 }5860 llvm_unreachable("Invalid EvalStmtResult!");5861}5862 5863static bool CheckLocalVariableDeclaration(EvalInfo &Info, const VarDecl *VD) {5864 // An expression E is a core constant expression unless the evaluation of E5865 // would evaluate one of the following: [C++23] - a control flow that passes5866 // through a declaration of a variable with static or thread storage duration5867 // unless that variable is usable in constant expressions.5868 if (VD->isLocalVarDecl() && VD->isStaticLocal() &&5869 !VD->isUsableInConstantExpressions(Info.Ctx)) {5870 Info.CCEDiag(VD->getLocation(), diag::note_constexpr_static_local)5871 << (VD->getTSCSpec() == TSCS_unspecified ? 0 : 1) << VD;5872 return false;5873 }5874 return true;5875}5876 5877// Evaluate a statement.5878static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info,5879 const Stmt *S, const SwitchCase *Case) {5880 if (!Info.nextStep(S))5881 return ESR_Failed;5882 5883 // If we're hunting down a 'case' or 'default' label, recurse through5884 // substatements until we hit the label.5885 if (Case) {5886 switch (S->getStmtClass()) {5887 case Stmt::CompoundStmtClass:5888 // FIXME: Precompute which substatement of a compound statement we5889 // would jump to, and go straight there rather than performing a5890 // linear scan each time.5891 case Stmt::LabelStmtClass:5892 case Stmt::AttributedStmtClass:5893 case Stmt::DoStmtClass:5894 break;5895 5896 case Stmt::CaseStmtClass:5897 case Stmt::DefaultStmtClass:5898 if (Case == S)5899 Case = nullptr;5900 break;5901 5902 case Stmt::IfStmtClass: {5903 // FIXME: Precompute which side of an 'if' we would jump to, and go5904 // straight there rather than scanning both sides.5905 const IfStmt *IS = cast<IfStmt>(S);5906 5907 // Wrap the evaluation in a block scope, in case it's a DeclStmt5908 // preceded by our switch label.5909 BlockScopeRAII Scope(Info);5910 5911 // Step into the init statement in case it brings an (uninitialized)5912 // variable into scope.5913 if (const Stmt *Init = IS->getInit()) {5914 EvalStmtResult ESR = EvaluateStmt(Result, Info, Init, Case);5915 if (ESR != ESR_CaseNotFound) {5916 assert(ESR != ESR_Succeeded);5917 return ESR;5918 }5919 }5920 5921 // Condition variable must be initialized if it exists.5922 // FIXME: We can skip evaluating the body if there's a condition5923 // variable, as there can't be any case labels within it.5924 // (The same is true for 'for' statements.)5925 5926 EvalStmtResult ESR = EvaluateStmt(Result, Info, IS->getThen(), Case);5927 if (ESR == ESR_Failed)5928 return ESR;5929 if (ESR != ESR_CaseNotFound)5930 return Scope.destroy() ? ESR : ESR_Failed;5931 if (!IS->getElse())5932 return ESR_CaseNotFound;5933 5934 ESR = EvaluateStmt(Result, Info, IS->getElse(), Case);5935 if (ESR == ESR_Failed)5936 return ESR;5937 if (ESR != ESR_CaseNotFound)5938 return Scope.destroy() ? ESR : ESR_Failed;5939 return ESR_CaseNotFound;5940 }5941 5942 case Stmt::WhileStmtClass: {5943 EvalStmtResult ESR =5944 EvaluateLoopBody(Result, Info, cast<WhileStmt>(S)->getBody(), Case);5945 if (ShouldPropagateBreakContinue(Info, S, /*Scopes=*/{}, ESR))5946 return ESR;5947 if (ESR != ESR_Continue)5948 return ESR;5949 break;5950 }5951 5952 case Stmt::ForStmtClass: {5953 const ForStmt *FS = cast<ForStmt>(S);5954 BlockScopeRAII Scope(Info);5955 5956 // Step into the init statement in case it brings an (uninitialized)5957 // variable into scope.5958 if (const Stmt *Init = FS->getInit()) {5959 EvalStmtResult ESR = EvaluateStmt(Result, Info, Init, Case);5960 if (ESR != ESR_CaseNotFound) {5961 assert(ESR != ESR_Succeeded);5962 return ESR;5963 }5964 }5965 5966 EvalStmtResult ESR =5967 EvaluateLoopBody(Result, Info, FS->getBody(), Case);5968 if (ShouldPropagateBreakContinue(Info, FS, /*Scopes=*/{}, ESR))5969 return ESR;5970 if (ESR != ESR_Continue)5971 return ESR;5972 if (const auto *Inc = FS->getInc()) {5973 if (Inc->isValueDependent()) {5974 if (!EvaluateDependentExpr(Inc, Info))5975 return ESR_Failed;5976 } else {5977 FullExpressionRAII IncScope(Info);5978 if (!EvaluateIgnoredValue(Info, Inc) || !IncScope.destroy())5979 return ESR_Failed;5980 }5981 }5982 break;5983 }5984 5985 case Stmt::DeclStmtClass: {5986 // Start the lifetime of any uninitialized variables we encounter. They5987 // might be used by the selected branch of the switch.5988 const DeclStmt *DS = cast<DeclStmt>(S);5989 for (const auto *D : DS->decls()) {5990 if (const auto *VD = dyn_cast<VarDecl>(D)) {5991 if (!CheckLocalVariableDeclaration(Info, VD))5992 return ESR_Failed;5993 if (VD->hasLocalStorage() && !VD->getInit())5994 if (!EvaluateVarDecl(Info, VD))5995 return ESR_Failed;5996 // FIXME: If the variable has initialization that can't be jumped5997 // over, bail out of any immediately-surrounding compound-statement5998 // too. There can't be any case labels here.5999 }6000 }6001 return ESR_CaseNotFound;6002 }6003 6004 default:6005 return ESR_CaseNotFound;6006 }6007 }6008 6009 switch (S->getStmtClass()) {6010 default:6011 if (const Expr *E = dyn_cast<Expr>(S)) {6012 if (E->isValueDependent()) {6013 if (!EvaluateDependentExpr(E, Info))6014 return ESR_Failed;6015 } else {6016 // Don't bother evaluating beyond an expression-statement which couldn't6017 // be evaluated.6018 // FIXME: Do we need the FullExpressionRAII object here?6019 // VisitExprWithCleanups should create one when necessary.6020 FullExpressionRAII Scope(Info);6021 if (!EvaluateIgnoredValue(Info, E) || !Scope.destroy())6022 return ESR_Failed;6023 }6024 return ESR_Succeeded;6025 }6026 6027 Info.FFDiag(S->getBeginLoc()) << S->getSourceRange();6028 return ESR_Failed;6029 6030 case Stmt::NullStmtClass:6031 return ESR_Succeeded;6032 6033 case Stmt::DeclStmtClass: {6034 const DeclStmt *DS = cast<DeclStmt>(S);6035 for (const auto *D : DS->decls()) {6036 const VarDecl *VD = dyn_cast_or_null<VarDecl>(D);6037 if (VD && !CheckLocalVariableDeclaration(Info, VD))6038 return ESR_Failed;6039 // Each declaration initialization is its own full-expression.6040 FullExpressionRAII Scope(Info);6041 if (!EvaluateDecl(Info, D, /*EvaluateConditionDecl=*/true) &&6042 !Info.noteFailure())6043 return ESR_Failed;6044 if (!Scope.destroy())6045 return ESR_Failed;6046 }6047 return ESR_Succeeded;6048 }6049 6050 case Stmt::ReturnStmtClass: {6051 const Expr *RetExpr = cast<ReturnStmt>(S)->getRetValue();6052 FullExpressionRAII Scope(Info);6053 if (RetExpr && RetExpr->isValueDependent()) {6054 EvaluateDependentExpr(RetExpr, Info);6055 // We know we returned, but we don't know what the value is.6056 return ESR_Failed;6057 }6058 if (RetExpr &&6059 !(Result.Slot6060 ? EvaluateInPlace(Result.Value, Info, *Result.Slot, RetExpr)6061 : Evaluate(Result.Value, Info, RetExpr)))6062 return ESR_Failed;6063 return Scope.destroy() ? ESR_Returned : ESR_Failed;6064 }6065 6066 case Stmt::CompoundStmtClass: {6067 BlockScopeRAII Scope(Info);6068 6069 const CompoundStmt *CS = cast<CompoundStmt>(S);6070 for (const auto *BI : CS->body()) {6071 EvalStmtResult ESR = EvaluateStmt(Result, Info, BI, Case);6072 if (ESR == ESR_Succeeded)6073 Case = nullptr;6074 else if (ESR != ESR_CaseNotFound) {6075 if (ESR != ESR_Failed && !Scope.destroy())6076 return ESR_Failed;6077 return ESR;6078 }6079 }6080 if (Case)6081 return ESR_CaseNotFound;6082 return Scope.destroy() ? ESR_Succeeded : ESR_Failed;6083 }6084 6085 case Stmt::IfStmtClass: {6086 const IfStmt *IS = cast<IfStmt>(S);6087 6088 // Evaluate the condition, as either a var decl or as an expression.6089 BlockScopeRAII Scope(Info);6090 if (const Stmt *Init = IS->getInit()) {6091 EvalStmtResult ESR = EvaluateStmt(Result, Info, Init);6092 if (ESR != ESR_Succeeded) {6093 if (ESR != ESR_Failed && !Scope.destroy())6094 return ESR_Failed;6095 return ESR;6096 }6097 }6098 bool Cond;6099 if (IS->isConsteval()) {6100 Cond = IS->isNonNegatedConsteval();6101 // If we are not in a constant context, if consteval should not evaluate6102 // to true.6103 if (!Info.InConstantContext)6104 Cond = !Cond;6105 } else if (!EvaluateCond(Info, IS->getConditionVariable(), IS->getCond(),6106 Cond))6107 return ESR_Failed;6108 6109 if (const Stmt *SubStmt = Cond ? IS->getThen() : IS->getElse()) {6110 EvalStmtResult ESR = EvaluateStmt(Result, Info, SubStmt);6111 if (ESR != ESR_Succeeded) {6112 if (ESR != ESR_Failed && !Scope.destroy())6113 return ESR_Failed;6114 return ESR;6115 }6116 }6117 return Scope.destroy() ? ESR_Succeeded : ESR_Failed;6118 }6119 6120 case Stmt::WhileStmtClass: {6121 const WhileStmt *WS = cast<WhileStmt>(S);6122 while (true) {6123 BlockScopeRAII Scope(Info);6124 bool Continue;6125 if (!EvaluateCond(Info, WS->getConditionVariable(), WS->getCond(),6126 Continue))6127 return ESR_Failed;6128 if (!Continue)6129 break;6130 6131 EvalStmtResult ESR = EvaluateLoopBody(Result, Info, WS->getBody());6132 if (ShouldPropagateBreakContinue(Info, WS, &Scope, ESR))6133 return ESR;6134 6135 if (ESR != ESR_Continue) {6136 if (ESR != ESR_Failed && !Scope.destroy())6137 return ESR_Failed;6138 return ESR;6139 }6140 if (!Scope.destroy())6141 return ESR_Failed;6142 }6143 return ESR_Succeeded;6144 }6145 6146 case Stmt::DoStmtClass: {6147 const DoStmt *DS = cast<DoStmt>(S);6148 bool Continue;6149 do {6150 EvalStmtResult ESR = EvaluateLoopBody(Result, Info, DS->getBody(), Case);6151 if (ShouldPropagateBreakContinue(Info, DS, /*Scopes=*/{}, ESR))6152 return ESR;6153 if (ESR != ESR_Continue)6154 return ESR;6155 Case = nullptr;6156 6157 if (DS->getCond()->isValueDependent()) {6158 EvaluateDependentExpr(DS->getCond(), Info);6159 // Bailout as we don't know whether to keep going or terminate the loop.6160 return ESR_Failed;6161 }6162 FullExpressionRAII CondScope(Info);6163 if (!EvaluateAsBooleanCondition(DS->getCond(), Continue, Info) ||6164 !CondScope.destroy())6165 return ESR_Failed;6166 } while (Continue);6167 return ESR_Succeeded;6168 }6169 6170 case Stmt::ForStmtClass: {6171 const ForStmt *FS = cast<ForStmt>(S);6172 BlockScopeRAII ForScope(Info);6173 if (FS->getInit()) {6174 EvalStmtResult ESR = EvaluateStmt(Result, Info, FS->getInit());6175 if (ESR != ESR_Succeeded) {6176 if (ESR != ESR_Failed && !ForScope.destroy())6177 return ESR_Failed;6178 return ESR;6179 }6180 }6181 while (true) {6182 BlockScopeRAII IterScope(Info);6183 bool Continue = true;6184 if (FS->getCond() && !EvaluateCond(Info, FS->getConditionVariable(),6185 FS->getCond(), Continue))6186 return ESR_Failed;6187 6188 if (!Continue) {6189 if (!IterScope.destroy())6190 return ESR_Failed;6191 break;6192 }6193 6194 EvalStmtResult ESR = EvaluateLoopBody(Result, Info, FS->getBody());6195 if (ShouldPropagateBreakContinue(Info, FS, {&IterScope, &ForScope}, ESR))6196 return ESR;6197 if (ESR != ESR_Continue) {6198 if (ESR != ESR_Failed && (!IterScope.destroy() || !ForScope.destroy()))6199 return ESR_Failed;6200 return ESR;6201 }6202 6203 if (const auto *Inc = FS->getInc()) {6204 if (Inc->isValueDependent()) {6205 if (!EvaluateDependentExpr(Inc, Info))6206 return ESR_Failed;6207 } else {6208 FullExpressionRAII IncScope(Info);6209 if (!EvaluateIgnoredValue(Info, Inc) || !IncScope.destroy())6210 return ESR_Failed;6211 }6212 }6213 6214 if (!IterScope.destroy())6215 return ESR_Failed;6216 }6217 return ForScope.destroy() ? ESR_Succeeded : ESR_Failed;6218 }6219 6220 case Stmt::CXXForRangeStmtClass: {6221 const CXXForRangeStmt *FS = cast<CXXForRangeStmt>(S);6222 BlockScopeRAII Scope(Info);6223 6224 // Evaluate the init-statement if present.6225 if (FS->getInit()) {6226 EvalStmtResult ESR = EvaluateStmt(Result, Info, FS->getInit());6227 if (ESR != ESR_Succeeded) {6228 if (ESR != ESR_Failed && !Scope.destroy())6229 return ESR_Failed;6230 return ESR;6231 }6232 }6233 6234 // Initialize the __range variable.6235 EvalStmtResult ESR = EvaluateStmt(Result, Info, FS->getRangeStmt());6236 if (ESR != ESR_Succeeded) {6237 if (ESR != ESR_Failed && !Scope.destroy())6238 return ESR_Failed;6239 return ESR;6240 }6241 6242 // In error-recovery cases it's possible to get here even if we failed to6243 // synthesize the __begin and __end variables.6244 if (!FS->getBeginStmt() || !FS->getEndStmt() || !FS->getCond())6245 return ESR_Failed;6246 6247 // Create the __begin and __end iterators.6248 ESR = EvaluateStmt(Result, Info, FS->getBeginStmt());6249 if (ESR != ESR_Succeeded) {6250 if (ESR != ESR_Failed && !Scope.destroy())6251 return ESR_Failed;6252 return ESR;6253 }6254 ESR = EvaluateStmt(Result, Info, FS->getEndStmt());6255 if (ESR != ESR_Succeeded) {6256 if (ESR != ESR_Failed && !Scope.destroy())6257 return ESR_Failed;6258 return ESR;6259 }6260 6261 while (true) {6262 // Condition: __begin != __end.6263 {6264 if (FS->getCond()->isValueDependent()) {6265 EvaluateDependentExpr(FS->getCond(), Info);6266 // We don't know whether to keep going or terminate the loop.6267 return ESR_Failed;6268 }6269 bool Continue = true;6270 FullExpressionRAII CondExpr(Info);6271 if (!EvaluateAsBooleanCondition(FS->getCond(), Continue, Info))6272 return ESR_Failed;6273 if (!Continue)6274 break;6275 }6276 6277 // User's variable declaration, initialized by *__begin.6278 BlockScopeRAII InnerScope(Info);6279 ESR = EvaluateStmt(Result, Info, FS->getLoopVarStmt());6280 if (ESR != ESR_Succeeded) {6281 if (ESR != ESR_Failed && (!InnerScope.destroy() || !Scope.destroy()))6282 return ESR_Failed;6283 return ESR;6284 }6285 6286 // Loop body.6287 ESR = EvaluateLoopBody(Result, Info, FS->getBody());6288 if (ShouldPropagateBreakContinue(Info, FS, {&InnerScope, &Scope}, ESR))6289 return ESR;6290 if (ESR != ESR_Continue) {6291 if (ESR != ESR_Failed && (!InnerScope.destroy() || !Scope.destroy()))6292 return ESR_Failed;6293 return ESR;6294 }6295 if (FS->getInc()->isValueDependent()) {6296 if (!EvaluateDependentExpr(FS->getInc(), Info))6297 return ESR_Failed;6298 } else {6299 // Increment: ++__begin6300 if (!EvaluateIgnoredValue(Info, FS->getInc()))6301 return ESR_Failed;6302 }6303 6304 if (!InnerScope.destroy())6305 return ESR_Failed;6306 }6307 6308 return Scope.destroy() ? ESR_Succeeded : ESR_Failed;6309 }6310 6311 case Stmt::SwitchStmtClass:6312 return EvaluateSwitch(Result, Info, cast<SwitchStmt>(S));6313 6314 case Stmt::ContinueStmtClass:6315 case Stmt::BreakStmtClass: {6316 auto *B = cast<LoopControlStmt>(S);6317 Info.BreakContinueStack.push_back(B->getNamedLoopOrSwitch());6318 return isa<ContinueStmt>(S) ? ESR_Continue : ESR_Break;6319 }6320 6321 case Stmt::LabelStmtClass:6322 return EvaluateStmt(Result, Info, cast<LabelStmt>(S)->getSubStmt(), Case);6323 6324 case Stmt::AttributedStmtClass: {6325 const auto *AS = cast<AttributedStmt>(S);6326 const auto *SS = AS->getSubStmt();6327 MSConstexprContextRAII ConstexprContext(6328 *Info.CurrentCall, hasSpecificAttr<MSConstexprAttr>(AS->getAttrs()) &&6329 isa<ReturnStmt>(SS));6330 6331 auto LO = Info.getASTContext().getLangOpts();6332 if (LO.CXXAssumptions && !LO.MSVCCompat) {6333 for (auto *Attr : AS->getAttrs()) {6334 auto *AA = dyn_cast<CXXAssumeAttr>(Attr);6335 if (!AA)6336 continue;6337 6338 auto *Assumption = AA->getAssumption();6339 if (Assumption->isValueDependent())6340 return ESR_Failed;6341 6342 if (Assumption->HasSideEffects(Info.getASTContext()))6343 continue;6344 6345 bool Value;6346 if (!EvaluateAsBooleanCondition(Assumption, Value, Info))6347 return ESR_Failed;6348 if (!Value) {6349 Info.CCEDiag(Assumption->getExprLoc(),6350 diag::note_constexpr_assumption_failed);6351 return ESR_Failed;6352 }6353 }6354 }6355 6356 return EvaluateStmt(Result, Info, SS, Case);6357 }6358 6359 case Stmt::CaseStmtClass:6360 case Stmt::DefaultStmtClass:6361 return EvaluateStmt(Result, Info, cast<SwitchCase>(S)->getSubStmt(), Case);6362 case Stmt::CXXTryStmtClass:6363 // Evaluate try blocks by evaluating all sub statements.6364 return EvaluateStmt(Result, Info, cast<CXXTryStmt>(S)->getTryBlock(), Case);6365 }6366}6367 6368/// CheckTrivialDefaultConstructor - Check whether a constructor is a trivial6369/// default constructor. If so, we'll fold it whether or not it's marked as6370/// constexpr. If it is marked as constexpr, we will never implicitly define it,6371/// so we need special handling.6372static bool CheckTrivialDefaultConstructor(EvalInfo &Info, SourceLocation Loc,6373 const CXXConstructorDecl *CD,6374 bool IsValueInitialization) {6375 if (!CD->isTrivial() || !CD->isDefaultConstructor())6376 return false;6377 6378 // Value-initialization does not call a trivial default constructor, so such a6379 // call is a core constant expression whether or not the constructor is6380 // constexpr.6381 if (!CD->isConstexpr() && !IsValueInitialization) {6382 if (Info.getLangOpts().CPlusPlus11) {6383 // FIXME: If DiagDecl is an implicitly-declared special member function,6384 // we should be much more explicit about why it's not constexpr.6385 Info.CCEDiag(Loc, diag::note_constexpr_invalid_function, 1)6386 << /*IsConstexpr*/0 << /*IsConstructor*/1 << CD;6387 Info.Note(CD->getLocation(), diag::note_declared_at);6388 } else {6389 Info.CCEDiag(Loc, diag::note_invalid_subexpr_in_const_expr);6390 }6391 }6392 return true;6393}6394 6395/// CheckConstexprFunction - Check that a function can be called in a constant6396/// expression.6397static bool CheckConstexprFunction(EvalInfo &Info, SourceLocation CallLoc,6398 const FunctionDecl *Declaration,6399 const FunctionDecl *Definition,6400 const Stmt *Body) {6401 // Potential constant expressions can contain calls to declared, but not yet6402 // defined, constexpr functions.6403 if (Info.checkingPotentialConstantExpression() && !Definition &&6404 Declaration->isConstexpr())6405 return false;6406 6407 // Bail out if the function declaration itself is invalid. We will6408 // have produced a relevant diagnostic while parsing it, so just6409 // note the problematic sub-expression.6410 if (Declaration->isInvalidDecl()) {6411 Info.FFDiag(CallLoc, diag::note_invalid_subexpr_in_const_expr);6412 return false;6413 }6414 6415 // DR1872: An instantiated virtual constexpr function can't be called in a6416 // constant expression (prior to C++20). We can still constant-fold such a6417 // call.6418 if (!Info.Ctx.getLangOpts().CPlusPlus20 && isa<CXXMethodDecl>(Declaration) &&6419 cast<CXXMethodDecl>(Declaration)->isVirtual())6420 Info.CCEDiag(CallLoc, diag::note_constexpr_virtual_call);6421 6422 if (Definition && Definition->isInvalidDecl()) {6423 Info.FFDiag(CallLoc, diag::note_invalid_subexpr_in_const_expr);6424 return false;6425 }6426 6427 // Can we evaluate this function call?6428 if (Definition && Body &&6429 (Definition->isConstexpr() || (Info.CurrentCall->CanEvalMSConstexpr &&6430 Definition->hasAttr<MSConstexprAttr>())))6431 return true;6432 6433 const FunctionDecl *DiagDecl = Definition ? Definition : Declaration;6434 // Special note for the assert() macro, as the normal error message falsely6435 // implies we cannot use an assertion during constant evaluation.6436 if (CallLoc.isMacroID() && DiagDecl->getIdentifier()) {6437 // FIXME: Instead of checking for an implementation-defined function,6438 // check and evaluate the assert() macro.6439 StringRef Name = DiagDecl->getName();6440 bool AssertFailed =6441 Name == "__assert_rtn" || Name == "__assert_fail" || Name == "_wassert";6442 if (AssertFailed) {6443 Info.FFDiag(CallLoc, diag::note_constexpr_assert_failed);6444 return false;6445 }6446 }6447 6448 if (Info.getLangOpts().CPlusPlus11) {6449 // If this function is not constexpr because it is an inherited6450 // non-constexpr constructor, diagnose that directly.6451 auto *CD = dyn_cast<CXXConstructorDecl>(DiagDecl);6452 if (CD && CD->isInheritingConstructor()) {6453 auto *Inherited = CD->getInheritedConstructor().getConstructor();6454 if (!Inherited->isConstexpr())6455 DiagDecl = CD = Inherited;6456 }6457 6458 // FIXME: If DiagDecl is an implicitly-declared special member function6459 // or an inheriting constructor, we should be much more explicit about why6460 // it's not constexpr.6461 if (CD && CD->isInheritingConstructor())6462 Info.FFDiag(CallLoc, diag::note_constexpr_invalid_inhctor, 1)6463 << CD->getInheritedConstructor().getConstructor()->getParent();6464 else6465 Info.FFDiag(CallLoc, diag::note_constexpr_invalid_function, 1)6466 << DiagDecl->isConstexpr() << (bool)CD << DiagDecl;6467 Info.Note(DiagDecl->getLocation(), diag::note_declared_at);6468 } else {6469 Info.FFDiag(CallLoc, diag::note_invalid_subexpr_in_const_expr);6470 }6471 return false;6472}6473 6474namespace {6475struct CheckDynamicTypeHandler {6476 AccessKinds AccessKind;6477 typedef bool result_type;6478 bool failed() { return false; }6479 bool found(APValue &Subobj, QualType SubobjType) { return true; }6480 bool found(APSInt &Value, QualType SubobjType) { return true; }6481 bool found(APFloat &Value, QualType SubobjType) { return true; }6482};6483} // end anonymous namespace6484 6485/// Check that we can access the notional vptr of an object / determine its6486/// dynamic type.6487static bool checkDynamicType(EvalInfo &Info, const Expr *E, const LValue &This,6488 AccessKinds AK, bool Polymorphic) {6489 if (This.Designator.Invalid)6490 return false;6491 6492 CompleteObject Obj = findCompleteObject(Info, E, AK, This, QualType());6493 6494 if (!Obj)6495 return false;6496 6497 if (!Obj.Value) {6498 // The object is not usable in constant expressions, so we can't inspect6499 // its value to see if it's in-lifetime or what the active union members6500 // are. We can still check for a one-past-the-end lvalue.6501 if (This.Designator.isOnePastTheEnd() ||6502 This.Designator.isMostDerivedAnUnsizedArray()) {6503 Info.FFDiag(E, This.Designator.isOnePastTheEnd()6504 ? diag::note_constexpr_access_past_end6505 : diag::note_constexpr_access_unsized_array)6506 << AK;6507 return false;6508 } else if (Polymorphic) {6509 // Conservatively refuse to perform a polymorphic operation if we would6510 // not be able to read a notional 'vptr' value.6511 if (!Info.checkingPotentialConstantExpression() ||6512 !This.AllowConstexprUnknown) {6513 APValue Val;6514 This.moveInto(Val);6515 QualType StarThisType =6516 Info.Ctx.getLValueReferenceType(This.Designator.getType(Info.Ctx));6517 Info.FFDiag(E, diag::note_constexpr_polymorphic_unknown_dynamic_type)6518 << AK << Val.getAsString(Info.Ctx, StarThisType);6519 }6520 return false;6521 }6522 return true;6523 }6524 6525 CheckDynamicTypeHandler Handler{AK};6526 return Obj && findSubobject(Info, E, Obj, This.Designator, Handler);6527}6528 6529/// Check that the pointee of the 'this' pointer in a member function call is6530/// either within its lifetime or in its period of construction or destruction.6531static bool6532checkNonVirtualMemberCallThisPointer(EvalInfo &Info, const Expr *E,6533 const LValue &This,6534 const CXXMethodDecl *NamedMember) {6535 return checkDynamicType(6536 Info, E, This,6537 isa<CXXDestructorDecl>(NamedMember) ? AK_Destroy : AK_MemberCall, false);6538}6539 6540struct DynamicType {6541 /// The dynamic class type of the object.6542 const CXXRecordDecl *Type;6543 /// The corresponding path length in the lvalue.6544 unsigned PathLength;6545};6546 6547static const CXXRecordDecl *getBaseClassType(SubobjectDesignator &Designator,6548 unsigned PathLength) {6549 assert(PathLength >= Designator.MostDerivedPathLength && PathLength <=6550 Designator.Entries.size() && "invalid path length");6551 return (PathLength == Designator.MostDerivedPathLength)6552 ? Designator.MostDerivedType->getAsCXXRecordDecl()6553 : getAsBaseClass(Designator.Entries[PathLength - 1]);6554}6555 6556/// Determine the dynamic type of an object.6557static std::optional<DynamicType> ComputeDynamicType(EvalInfo &Info,6558 const Expr *E,6559 LValue &This,6560 AccessKinds AK) {6561 // If we don't have an lvalue denoting an object of class type, there is no6562 // meaningful dynamic type. (We consider objects of non-class type to have no6563 // dynamic type.)6564 if (!checkDynamicType(Info, E, This, AK,6565 AK != AK_TypeId || This.AllowConstexprUnknown))6566 return std::nullopt;6567 6568 if (This.Designator.Invalid)6569 return std::nullopt;6570 6571 // Refuse to compute a dynamic type in the presence of virtual bases. This6572 // shouldn't happen other than in constant-folding situations, since literal6573 // types can't have virtual bases.6574 //6575 // Note that consumers of DynamicType assume that the type has no virtual6576 // bases, and will need modifications if this restriction is relaxed.6577 const CXXRecordDecl *Class =6578 This.Designator.MostDerivedType->getAsCXXRecordDecl();6579 if (!Class || Class->getNumVBases()) {6580 Info.FFDiag(E);6581 return std::nullopt;6582 }6583 6584 // FIXME: For very deep class hierarchies, it might be beneficial to use a6585 // binary search here instead. But the overwhelmingly common case is that6586 // we're not in the middle of a constructor, so it probably doesn't matter6587 // in practice.6588 ArrayRef<APValue::LValuePathEntry> Path = This.Designator.Entries;6589 for (unsigned PathLength = This.Designator.MostDerivedPathLength;6590 PathLength <= Path.size(); ++PathLength) {6591 switch (Info.isEvaluatingCtorDtor(This.getLValueBase(),6592 Path.slice(0, PathLength))) {6593 case ConstructionPhase::Bases:6594 case ConstructionPhase::DestroyingBases:6595 // We're constructing or destroying a base class. This is not the dynamic6596 // type.6597 break;6598 6599 case ConstructionPhase::None:6600 case ConstructionPhase::AfterBases:6601 case ConstructionPhase::AfterFields:6602 case ConstructionPhase::Destroying:6603 // We've finished constructing the base classes and not yet started6604 // destroying them again, so this is the dynamic type.6605 return DynamicType{getBaseClassType(This.Designator, PathLength),6606 PathLength};6607 }6608 }6609 6610 // CWG issue 1517: we're constructing a base class of the object described by6611 // 'This', so that object has not yet begun its period of construction and6612 // any polymorphic operation on it results in undefined behavior.6613 Info.FFDiag(E);6614 return std::nullopt;6615}6616 6617/// Perform virtual dispatch.6618static const CXXMethodDecl *HandleVirtualDispatch(6619 EvalInfo &Info, const Expr *E, LValue &This, const CXXMethodDecl *Found,6620 llvm::SmallVectorImpl<QualType> &CovariantAdjustmentPath) {6621 std::optional<DynamicType> DynType = ComputeDynamicType(6622 Info, E, This,6623 isa<CXXDestructorDecl>(Found) ? AK_Destroy : AK_MemberCall);6624 if (!DynType)6625 return nullptr;6626 6627 // Find the final overrider. It must be declared in one of the classes on the6628 // path from the dynamic type to the static type.6629 // FIXME: If we ever allow literal types to have virtual base classes, that6630 // won't be true.6631 const CXXMethodDecl *Callee = Found;6632 unsigned PathLength = DynType->PathLength;6633 for (/**/; PathLength <= This.Designator.Entries.size(); ++PathLength) {6634 const CXXRecordDecl *Class = getBaseClassType(This.Designator, PathLength);6635 const CXXMethodDecl *Overrider =6636 Found->getCorrespondingMethodDeclaredInClass(Class, false);6637 if (Overrider) {6638 Callee = Overrider;6639 break;6640 }6641 }6642 6643 // C++2a [class.abstract]p6:6644 // the effect of making a virtual call to a pure virtual function [...] is6645 // undefined6646 if (Callee->isPureVirtual()) {6647 Info.FFDiag(E, diag::note_constexpr_pure_virtual_call, 1) << Callee;6648 Info.Note(Callee->getLocation(), diag::note_declared_at);6649 return nullptr;6650 }6651 6652 // If necessary, walk the rest of the path to determine the sequence of6653 // covariant adjustment steps to apply.6654 if (!Info.Ctx.hasSameUnqualifiedType(Callee->getReturnType(),6655 Found->getReturnType())) {6656 CovariantAdjustmentPath.push_back(Callee->getReturnType());6657 for (unsigned CovariantPathLength = PathLength + 1;6658 CovariantPathLength != This.Designator.Entries.size();6659 ++CovariantPathLength) {6660 const CXXRecordDecl *NextClass =6661 getBaseClassType(This.Designator, CovariantPathLength);6662 const CXXMethodDecl *Next =6663 Found->getCorrespondingMethodDeclaredInClass(NextClass, false);6664 if (Next && !Info.Ctx.hasSameUnqualifiedType(6665 Next->getReturnType(), CovariantAdjustmentPath.back()))6666 CovariantAdjustmentPath.push_back(Next->getReturnType());6667 }6668 if (!Info.Ctx.hasSameUnqualifiedType(Found->getReturnType(),6669 CovariantAdjustmentPath.back()))6670 CovariantAdjustmentPath.push_back(Found->getReturnType());6671 }6672 6673 // Perform 'this' adjustment.6674 if (!CastToDerivedClass(Info, E, This, Callee->getParent(), PathLength))6675 return nullptr;6676 6677 return Callee;6678}6679 6680/// Perform the adjustment from a value returned by a virtual function to6681/// a value of the statically expected type, which may be a pointer or6682/// reference to a base class of the returned type.6683static bool HandleCovariantReturnAdjustment(EvalInfo &Info, const Expr *E,6684 APValue &Result,6685 ArrayRef<QualType> Path) {6686 assert(Result.isLValue() &&6687 "unexpected kind of APValue for covariant return");6688 if (Result.isNullPointer())6689 return true;6690 6691 LValue LVal;6692 LVal.setFrom(Info.Ctx, Result);6693 6694 const CXXRecordDecl *OldClass = Path[0]->getPointeeCXXRecordDecl();6695 for (unsigned I = 1; I != Path.size(); ++I) {6696 const CXXRecordDecl *NewClass = Path[I]->getPointeeCXXRecordDecl();6697 assert(OldClass && NewClass && "unexpected kind of covariant return");6698 if (OldClass != NewClass &&6699 !CastToBaseClass(Info, E, LVal, OldClass, NewClass))6700 return false;6701 OldClass = NewClass;6702 }6703 6704 LVal.moveInto(Result);6705 return true;6706}6707 6708/// Determine whether \p Base, which is known to be a direct base class of6709/// \p Derived, is a public base class.6710static bool isBaseClassPublic(const CXXRecordDecl *Derived,6711 const CXXRecordDecl *Base) {6712 for (const CXXBaseSpecifier &BaseSpec : Derived->bases()) {6713 auto *BaseClass = BaseSpec.getType()->getAsCXXRecordDecl();6714 if (BaseClass && declaresSameEntity(BaseClass, Base))6715 return BaseSpec.getAccessSpecifier() == AS_public;6716 }6717 llvm_unreachable("Base is not a direct base of Derived");6718}6719 6720/// Apply the given dynamic cast operation on the provided lvalue.6721///6722/// This implements the hard case of dynamic_cast, requiring a "runtime check"6723/// to find a suitable target subobject.6724static bool HandleDynamicCast(EvalInfo &Info, const ExplicitCastExpr *E,6725 LValue &Ptr) {6726 // We can't do anything with a non-symbolic pointer value.6727 SubobjectDesignator &D = Ptr.Designator;6728 if (D.Invalid)6729 return false;6730 6731 // C++ [expr.dynamic.cast]p6:6732 // If v is a null pointer value, the result is a null pointer value.6733 if (Ptr.isNullPointer() && !E->isGLValue())6734 return true;6735 6736 // For all the other cases, we need the pointer to point to an object within6737 // its lifetime / period of construction / destruction, and we need to know6738 // its dynamic type.6739 std::optional<DynamicType> DynType =6740 ComputeDynamicType(Info, E, Ptr, AK_DynamicCast);6741 if (!DynType)6742 return false;6743 6744 // C++ [expr.dynamic.cast]p7:6745 // If T is "pointer to cv void", then the result is a pointer to the most6746 // derived object6747 if (E->getType()->isVoidPointerType())6748 return CastToDerivedClass(Info, E, Ptr, DynType->Type, DynType->PathLength);6749 6750 const CXXRecordDecl *C = E->getTypeAsWritten()->getPointeeCXXRecordDecl();6751 assert(C && "dynamic_cast target is not void pointer nor class");6752 CanQualType CQT = Info.Ctx.getCanonicalTagType(C);6753 6754 auto RuntimeCheckFailed = [&] (CXXBasePaths *Paths) {6755 // C++ [expr.dynamic.cast]p9:6756 if (!E->isGLValue()) {6757 // The value of a failed cast to pointer type is the null pointer value6758 // of the required result type.6759 Ptr.setNull(Info.Ctx, E->getType());6760 return true;6761 }6762 6763 // A failed cast to reference type throws [...] std::bad_cast.6764 unsigned DiagKind;6765 if (!Paths && (declaresSameEntity(DynType->Type, C) ||6766 DynType->Type->isDerivedFrom(C)))6767 DiagKind = 0;6768 else if (!Paths || Paths->begin() == Paths->end())6769 DiagKind = 1;6770 else if (Paths->isAmbiguous(CQT))6771 DiagKind = 2;6772 else {6773 assert(Paths->front().Access != AS_public && "why did the cast fail?");6774 DiagKind = 3;6775 }6776 Info.FFDiag(E, diag::note_constexpr_dynamic_cast_to_reference_failed)6777 << DiagKind << Ptr.Designator.getType(Info.Ctx)6778 << Info.Ctx.getCanonicalTagType(DynType->Type)6779 << E->getType().getUnqualifiedType();6780 return false;6781 };6782 6783 // Runtime check, phase 1:6784 // Walk from the base subobject towards the derived object looking for the6785 // target type.6786 for (int PathLength = Ptr.Designator.Entries.size();6787 PathLength >= (int)DynType->PathLength; --PathLength) {6788 const CXXRecordDecl *Class = getBaseClassType(Ptr.Designator, PathLength);6789 if (declaresSameEntity(Class, C))6790 return CastToDerivedClass(Info, E, Ptr, Class, PathLength);6791 // We can only walk across public inheritance edges.6792 if (PathLength > (int)DynType->PathLength &&6793 !isBaseClassPublic(getBaseClassType(Ptr.Designator, PathLength - 1),6794 Class))6795 return RuntimeCheckFailed(nullptr);6796 }6797 6798 // Runtime check, phase 2:6799 // Search the dynamic type for an unambiguous public base of type C.6800 CXXBasePaths Paths(/*FindAmbiguities=*/true,6801 /*RecordPaths=*/true, /*DetectVirtual=*/false);6802 if (DynType->Type->isDerivedFrom(C, Paths) && !Paths.isAmbiguous(CQT) &&6803 Paths.front().Access == AS_public) {6804 // Downcast to the dynamic type...6805 if (!CastToDerivedClass(Info, E, Ptr, DynType->Type, DynType->PathLength))6806 return false;6807 // ... then upcast to the chosen base class subobject.6808 for (CXXBasePathElement &Elem : Paths.front())6809 if (!HandleLValueBase(Info, E, Ptr, Elem.Class, Elem.Base))6810 return false;6811 return true;6812 }6813 6814 // Otherwise, the runtime check fails.6815 return RuntimeCheckFailed(&Paths);6816}6817 6818namespace {6819struct StartLifetimeOfUnionMemberHandler {6820 EvalInfo &Info;6821 const Expr *LHSExpr;6822 const FieldDecl *Field;6823 bool DuringInit;6824 bool Failed = false;6825 static const AccessKinds AccessKind = AK_Assign;6826 6827 typedef bool result_type;6828 bool failed() { return Failed; }6829 bool found(APValue &Subobj, QualType SubobjType) {6830 // We are supposed to perform no initialization but begin the lifetime of6831 // the object. We interpret that as meaning to do what default6832 // initialization of the object would do if all constructors involved were6833 // trivial:6834 // * All base, non-variant member, and array element subobjects' lifetimes6835 // begin6836 // * No variant members' lifetimes begin6837 // * All scalar subobjects whose lifetimes begin have indeterminate values6838 assert(SubobjType->isUnionType());6839 if (declaresSameEntity(Subobj.getUnionField(), Field)) {6840 // This union member is already active. If it's also in-lifetime, there's6841 // nothing to do.6842 if (Subobj.getUnionValue().hasValue())6843 return true;6844 } else if (DuringInit) {6845 // We're currently in the process of initializing a different union6846 // member. If we carried on, that initialization would attempt to6847 // store to an inactive union member, resulting in undefined behavior.6848 Info.FFDiag(LHSExpr,6849 diag::note_constexpr_union_member_change_during_init);6850 return false;6851 }6852 APValue Result;6853 Failed = !handleDefaultInitValue(Field->getType(), Result);6854 Subobj.setUnion(Field, Result);6855 return true;6856 }6857 bool found(APSInt &Value, QualType SubobjType) {6858 llvm_unreachable("wrong value kind for union object");6859 }6860 bool found(APFloat &Value, QualType SubobjType) {6861 llvm_unreachable("wrong value kind for union object");6862 }6863};6864} // end anonymous namespace6865 6866const AccessKinds StartLifetimeOfUnionMemberHandler::AccessKind;6867 6868/// Handle a builtin simple-assignment or a call to a trivial assignment6869/// operator whose left-hand side might involve a union member access. If it6870/// does, implicitly start the lifetime of any accessed union elements per6871/// C++20 [class.union]5.6872static bool MaybeHandleUnionActiveMemberChange(EvalInfo &Info,6873 const Expr *LHSExpr,6874 const LValue &LHS) {6875 if (LHS.InvalidBase || LHS.Designator.Invalid)6876 return false;6877 6878 llvm::SmallVector<std::pair<unsigned, const FieldDecl*>, 4> UnionPathLengths;6879 // C++ [class.union]p5:6880 // define the set S(E) of subexpressions of E as follows:6881 unsigned PathLength = LHS.Designator.Entries.size();6882 for (const Expr *E = LHSExpr; E != nullptr;) {6883 // -- If E is of the form A.B, S(E) contains the elements of S(A)...6884 if (auto *ME = dyn_cast<MemberExpr>(E)) {6885 auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl());6886 // Note that we can't implicitly start the lifetime of a reference,6887 // so we don't need to proceed any further if we reach one.6888 if (!FD || FD->getType()->isReferenceType())6889 break;6890 6891 // ... and also contains A.B if B names a union member ...6892 if (FD->getParent()->isUnion()) {6893 // ... of a non-class, non-array type, or of a class type with a6894 // trivial default constructor that is not deleted, or an array of6895 // such types.6896 auto *RD =6897 FD->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();6898 if (!RD || RD->hasTrivialDefaultConstructor())6899 UnionPathLengths.push_back({PathLength - 1, FD});6900 }6901 6902 E = ME->getBase();6903 --PathLength;6904 assert(declaresSameEntity(FD,6905 LHS.Designator.Entries[PathLength]6906 .getAsBaseOrMember().getPointer()));6907 6908 // -- If E is of the form A[B] and is interpreted as a built-in array6909 // subscripting operator, S(E) is [S(the array operand, if any)].6910 } else if (auto *ASE = dyn_cast<ArraySubscriptExpr>(E)) {6911 // Step over an ArrayToPointerDecay implicit cast.6912 auto *Base = ASE->getBase()->IgnoreImplicit();6913 if (!Base->getType()->isArrayType())6914 break;6915 6916 E = Base;6917 --PathLength;6918 6919 } else if (auto *ICE = dyn_cast<ImplicitCastExpr>(E)) {6920 // Step over a derived-to-base conversion.6921 E = ICE->getSubExpr();6922 if (ICE->getCastKind() == CK_NoOp)6923 continue;6924 if (ICE->getCastKind() != CK_DerivedToBase &&6925 ICE->getCastKind() != CK_UncheckedDerivedToBase)6926 break;6927 // Walk path backwards as we walk up from the base to the derived class.6928 for (const CXXBaseSpecifier *Elt : llvm::reverse(ICE->path())) {6929 if (Elt->isVirtual()) {6930 // A class with virtual base classes never has a trivial default6931 // constructor, so S(E) is empty in this case.6932 E = nullptr;6933 break;6934 }6935 6936 --PathLength;6937 assert(declaresSameEntity(Elt->getType()->getAsCXXRecordDecl(),6938 LHS.Designator.Entries[PathLength]6939 .getAsBaseOrMember().getPointer()));6940 }6941 6942 // -- Otherwise, S(E) is empty.6943 } else {6944 break;6945 }6946 }6947 6948 // Common case: no unions' lifetimes are started.6949 if (UnionPathLengths.empty())6950 return true;6951 6952 // if modification of X [would access an inactive union member], an object6953 // of the type of X is implicitly created6954 CompleteObject Obj =6955 findCompleteObject(Info, LHSExpr, AK_Assign, LHS, LHSExpr->getType());6956 if (!Obj)6957 return false;6958 for (std::pair<unsigned, const FieldDecl *> LengthAndField :6959 llvm::reverse(UnionPathLengths)) {6960 // Form a designator for the union object.6961 SubobjectDesignator D = LHS.Designator;6962 D.truncate(Info.Ctx, LHS.Base, LengthAndField.first);6963 6964 bool DuringInit = Info.isEvaluatingCtorDtor(LHS.Base, D.Entries) ==6965 ConstructionPhase::AfterBases;6966 StartLifetimeOfUnionMemberHandler StartLifetime{6967 Info, LHSExpr, LengthAndField.second, DuringInit};6968 if (!findSubobject(Info, LHSExpr, Obj, D, StartLifetime))6969 return false;6970 }6971 6972 return true;6973}6974 6975static bool EvaluateCallArg(const ParmVarDecl *PVD, const Expr *Arg,6976 CallRef Call, EvalInfo &Info, bool NonNull = false,6977 APValue **EvaluatedArg = nullptr) {6978 LValue LV;6979 // Create the parameter slot and register its destruction. For a vararg6980 // argument, create a temporary.6981 // FIXME: For calling conventions that destroy parameters in the callee,6982 // should we consider performing destruction when the function returns6983 // instead?6984 APValue &V = PVD ? Info.CurrentCall->createParam(Call, PVD, LV)6985 : Info.CurrentCall->createTemporary(Arg, Arg->getType(),6986 ScopeKind::Call, LV);6987 if (!EvaluateInPlace(V, Info, LV, Arg))6988 return false;6989 6990 // Passing a null pointer to an __attribute__((nonnull)) parameter results in6991 // undefined behavior, so is non-constant.6992 if (NonNull && V.isLValue() && V.isNullPointer()) {6993 Info.CCEDiag(Arg, diag::note_non_null_attribute_failed);6994 return false;6995 }6996 6997 if (EvaluatedArg)6998 *EvaluatedArg = &V;6999 7000 return true;7001}7002 7003/// Evaluate the arguments to a function call.7004static bool EvaluateArgs(ArrayRef<const Expr *> Args, CallRef Call,7005 EvalInfo &Info, const FunctionDecl *Callee,7006 bool RightToLeft = false,7007 LValue *ObjectArg = nullptr) {7008 bool Success = true;7009 llvm::SmallBitVector ForbiddenNullArgs;7010 if (Callee->hasAttr<NonNullAttr>()) {7011 ForbiddenNullArgs.resize(Args.size());7012 for (const auto *Attr : Callee->specific_attrs<NonNullAttr>()) {7013 if (!Attr->args_size()) {7014 ForbiddenNullArgs.set();7015 break;7016 } else7017 for (auto Idx : Attr->args()) {7018 unsigned ASTIdx = Idx.getASTIndex();7019 if (ASTIdx >= Args.size())7020 continue;7021 ForbiddenNullArgs[ASTIdx] = true;7022 }7023 }7024 }7025 for (unsigned I = 0; I < Args.size(); I++) {7026 unsigned Idx = RightToLeft ? Args.size() - I - 1 : I;7027 const ParmVarDecl *PVD =7028 Idx < Callee->getNumParams() ? Callee->getParamDecl(Idx) : nullptr;7029 bool NonNull = !ForbiddenNullArgs.empty() && ForbiddenNullArgs[Idx];7030 APValue *That = nullptr;7031 if (!EvaluateCallArg(PVD, Args[Idx], Call, Info, NonNull, &That)) {7032 // If we're checking for a potential constant expression, evaluate all7033 // initializers even if some of them fail.7034 if (!Info.noteFailure())7035 return false;7036 Success = false;7037 }7038 if (PVD && PVD->isExplicitObjectParameter() && That && That->isLValue())7039 ObjectArg->setFrom(Info.Ctx, *That);7040 }7041 return Success;7042}7043 7044/// Perform a trivial copy from Param, which is the parameter of a copy or move7045/// constructor or assignment operator.7046static bool handleTrivialCopy(EvalInfo &Info, const ParmVarDecl *Param,7047 const Expr *E, APValue &Result,7048 bool CopyObjectRepresentation) {7049 // Find the reference argument.7050 CallStackFrame *Frame = Info.CurrentCall;7051 APValue *RefValue = Info.getParamSlot(Frame->Arguments, Param);7052 if (!RefValue) {7053 Info.FFDiag(E);7054 return false;7055 }7056 7057 // Copy out the contents of the RHS object.7058 LValue RefLValue;7059 RefLValue.setFrom(Info.Ctx, *RefValue);7060 return handleLValueToRValueConversion(7061 Info, E, Param->getType().getNonReferenceType(), RefLValue, Result,7062 CopyObjectRepresentation);7063}7064 7065/// Evaluate a function call.7066static bool HandleFunctionCall(SourceLocation CallLoc,7067 const FunctionDecl *Callee,7068 const LValue *ObjectArg, const Expr *E,7069 ArrayRef<const Expr *> Args, CallRef Call,7070 const Stmt *Body, EvalInfo &Info,7071 APValue &Result, const LValue *ResultSlot) {7072 if (!Info.CheckCallLimit(CallLoc))7073 return false;7074 7075 CallStackFrame Frame(Info, E->getSourceRange(), Callee, ObjectArg, E, Call);7076 7077 // For a trivial copy or move assignment, perform an APValue copy. This is7078 // essential for unions, where the operations performed by the assignment7079 // operator cannot be represented as statements.7080 //7081 // Skip this for non-union classes with no fields; in that case, the defaulted7082 // copy/move does not actually read the object.7083 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Callee);7084 if (MD && MD->isDefaulted() &&7085 (MD->getParent()->isUnion() ||7086 (MD->isTrivial() &&7087 isReadByLvalueToRvalueConversion(MD->getParent())))) {7088 unsigned ExplicitOffset = MD->isExplicitObjectMemberFunction() ? 1 : 0;7089 assert(ObjectArg &&7090 (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator()));7091 APValue RHSValue;7092 if (!handleTrivialCopy(Info, MD->getParamDecl(0), Args[0], RHSValue,7093 MD->getParent()->isUnion()))7094 return false;7095 7096 LValue Obj;7097 if (!handleAssignment(Info, Args[ExplicitOffset], *ObjectArg,7098 MD->getFunctionObjectParameterReferenceType(),7099 RHSValue))7100 return false;7101 ObjectArg->moveInto(Result);7102 return true;7103 } else if (MD && isLambdaCallOperator(MD)) {7104 // We're in a lambda; determine the lambda capture field maps unless we're7105 // just constexpr checking a lambda's call operator. constexpr checking is7106 // done before the captures have been added to the closure object (unless7107 // we're inferring constexpr-ness), so we don't have access to them in this7108 // case. But since we don't need the captures to constexpr check, we can7109 // just ignore them.7110 if (!Info.checkingPotentialConstantExpression())7111 MD->getParent()->getCaptureFields(Frame.LambdaCaptureFields,7112 Frame.LambdaThisCaptureField);7113 }7114 7115 StmtResult Ret = {Result, ResultSlot};7116 EvalStmtResult ESR = EvaluateStmt(Ret, Info, Body);7117 if (ESR == ESR_Succeeded) {7118 if (Callee->getReturnType()->isVoidType())7119 return true;7120 Info.FFDiag(Callee->getEndLoc(), diag::note_constexpr_no_return);7121 }7122 return ESR == ESR_Returned;7123}7124 7125/// Evaluate a constructor call.7126static bool HandleConstructorCall(const Expr *E, const LValue &This,7127 CallRef Call,7128 const CXXConstructorDecl *Definition,7129 EvalInfo &Info, APValue &Result) {7130 SourceLocation CallLoc = E->getExprLoc();7131 if (!Info.CheckCallLimit(CallLoc))7132 return false;7133 7134 const CXXRecordDecl *RD = Definition->getParent();7135 if (RD->getNumVBases()) {7136 Info.FFDiag(CallLoc, diag::note_constexpr_virtual_base) << RD;7137 return false;7138 }7139 7140 EvalInfo::EvaluatingConstructorRAII EvalObj(7141 Info,7142 ObjectUnderConstruction{This.getLValueBase(), This.Designator.Entries},7143 RD->getNumBases());7144 CallStackFrame Frame(Info, E->getSourceRange(), Definition, &This, E, Call);7145 7146 // FIXME: Creating an APValue just to hold a nonexistent return value is7147 // wasteful.7148 APValue RetVal;7149 StmtResult Ret = {RetVal, nullptr};7150 7151 // If it's a delegating constructor, delegate.7152 if (Definition->isDelegatingConstructor()) {7153 CXXConstructorDecl::init_const_iterator I = Definition->init_begin();7154 if ((*I)->getInit()->isValueDependent()) {7155 if (!EvaluateDependentExpr((*I)->getInit(), Info))7156 return false;7157 } else {7158 FullExpressionRAII InitScope(Info);7159 if (!EvaluateInPlace(Result, Info, This, (*I)->getInit()) ||7160 !InitScope.destroy())7161 return false;7162 }7163 return EvaluateStmt(Ret, Info, Definition->getBody()) != ESR_Failed;7164 }7165 7166 // For a trivial copy or move constructor, perform an APValue copy. This is7167 // essential for unions (or classes with anonymous union members), where the7168 // operations performed by the constructor cannot be represented by7169 // ctor-initializers.7170 //7171 // Skip this for empty non-union classes; we should not perform an7172 // lvalue-to-rvalue conversion on them because their copy constructor does not7173 // actually read them.7174 if (Definition->isDefaulted() && Definition->isCopyOrMoveConstructor() &&7175 (Definition->getParent()->isUnion() ||7176 (Definition->isTrivial() &&7177 isReadByLvalueToRvalueConversion(Definition->getParent())))) {7178 return handleTrivialCopy(Info, Definition->getParamDecl(0), E, Result,7179 Definition->getParent()->isUnion());7180 }7181 7182 // Reserve space for the struct members.7183 if (!Result.hasValue()) {7184 if (!RD->isUnion())7185 Result = APValue(APValue::UninitStruct(), RD->getNumBases(),7186 RD->getNumFields());7187 else7188 // A union starts with no active member.7189 Result = APValue((const FieldDecl*)nullptr);7190 }7191 7192 if (RD->isInvalidDecl()) return false;7193 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD);7194 7195 // A scope for temporaries lifetime-extended by reference members.7196 BlockScopeRAII LifetimeExtendedScope(Info);7197 7198 bool Success = true;7199 unsigned BasesSeen = 0;7200#ifndef NDEBUG7201 CXXRecordDecl::base_class_const_iterator BaseIt = RD->bases_begin();7202#endif7203 CXXRecordDecl::field_iterator FieldIt = RD->field_begin();7204 auto SkipToField = [&](FieldDecl *FD, bool Indirect) {7205 // We might be initializing the same field again if this is an indirect7206 // field initialization.7207 if (FieldIt == RD->field_end() ||7208 FieldIt->getFieldIndex() > FD->getFieldIndex()) {7209 assert(Indirect && "fields out of order?");7210 return;7211 }7212 7213 // Default-initialize any fields with no explicit initializer.7214 for (; !declaresSameEntity(*FieldIt, FD); ++FieldIt) {7215 assert(FieldIt != RD->field_end() && "missing field?");7216 if (!FieldIt->isUnnamedBitField())7217 Success &= handleDefaultInitValue(7218 FieldIt->getType(),7219 Result.getStructField(FieldIt->getFieldIndex()));7220 }7221 ++FieldIt;7222 };7223 for (const auto *I : Definition->inits()) {7224 LValue Subobject = This;7225 LValue SubobjectParent = This;7226 APValue *Value = &Result;7227 7228 // Determine the subobject to initialize.7229 FieldDecl *FD = nullptr;7230 if (I->isBaseInitializer()) {7231 QualType BaseType(I->getBaseClass(), 0);7232#ifndef NDEBUG7233 // Non-virtual base classes are initialized in the order in the class7234 // definition. We have already checked for virtual base classes.7235 assert(!BaseIt->isVirtual() && "virtual base for literal type");7236 assert(Info.Ctx.hasSameUnqualifiedType(BaseIt->getType(), BaseType) &&7237 "base class initializers not in expected order");7238 ++BaseIt;7239#endif7240 if (!HandleLValueDirectBase(Info, I->getInit(), Subobject, RD,7241 BaseType->getAsCXXRecordDecl(), &Layout))7242 return false;7243 Value = &Result.getStructBase(BasesSeen++);7244 } else if ((FD = I->getMember())) {7245 if (!HandleLValueMember(Info, I->getInit(), Subobject, FD, &Layout))7246 return false;7247 if (RD->isUnion()) {7248 Result = APValue(FD);7249 Value = &Result.getUnionValue();7250 } else {7251 SkipToField(FD, false);7252 Value = &Result.getStructField(FD->getFieldIndex());7253 }7254 } else if (IndirectFieldDecl *IFD = I->getIndirectMember()) {7255 // Walk the indirect field decl's chain to find the object to initialize,7256 // and make sure we've initialized every step along it.7257 auto IndirectFieldChain = IFD->chain();7258 for (auto *C : IndirectFieldChain) {7259 FD = cast<FieldDecl>(C);7260 CXXRecordDecl *CD = cast<CXXRecordDecl>(FD->getParent());7261 // Switch the union field if it differs. This happens if we had7262 // preceding zero-initialization, and we're now initializing a union7263 // subobject other than the first.7264 // FIXME: In this case, the values of the other subobjects are7265 // specified, since zero-initialization sets all padding bits to zero.7266 if (!Value->hasValue() ||7267 (Value->isUnion() &&7268 !declaresSameEntity(Value->getUnionField(), FD))) {7269 if (CD->isUnion())7270 *Value = APValue(FD);7271 else7272 // FIXME: This immediately starts the lifetime of all members of7273 // an anonymous struct. It would be preferable to strictly start7274 // member lifetime in initialization order.7275 Success &= handleDefaultInitValue(Info.Ctx.getCanonicalTagType(CD),7276 *Value);7277 }7278 // Store Subobject as its parent before updating it for the last element7279 // in the chain.7280 if (C == IndirectFieldChain.back())7281 SubobjectParent = Subobject;7282 if (!HandleLValueMember(Info, I->getInit(), Subobject, FD))7283 return false;7284 if (CD->isUnion())7285 Value = &Value->getUnionValue();7286 else {7287 if (C == IndirectFieldChain.front() && !RD->isUnion())7288 SkipToField(FD, true);7289 Value = &Value->getStructField(FD->getFieldIndex());7290 }7291 }7292 } else {7293 llvm_unreachable("unknown base initializer kind");7294 }7295 7296 // Need to override This for implicit field initializers as in this case7297 // This refers to innermost anonymous struct/union containing initializer,7298 // not to currently constructed class.7299 const Expr *Init = I->getInit();7300 if (Init->isValueDependent()) {7301 if (!EvaluateDependentExpr(Init, Info))7302 return false;7303 } else {7304 ThisOverrideRAII ThisOverride(*Info.CurrentCall, &SubobjectParent,7305 isa<CXXDefaultInitExpr>(Init));7306 FullExpressionRAII InitScope(Info);7307 if (FD && FD->getType()->isReferenceType() &&7308 !FD->getType()->isFunctionReferenceType()) {7309 LValue Result;7310 if (!EvaluateInitForDeclOfReferenceType(Info, FD, Init, Result,7311 *Value)) {7312 if (!Info.noteFailure())7313 return false;7314 Success = false;7315 }7316 } else if (!EvaluateInPlace(*Value, Info, Subobject, Init) ||7317 (FD && FD->isBitField() &&7318 !truncateBitfieldValue(Info, Init, *Value, FD))) {7319 // If we're checking for a potential constant expression, evaluate all7320 // initializers even if some of them fail.7321 if (!Info.noteFailure())7322 return false;7323 Success = false;7324 }7325 }7326 7327 // This is the point at which the dynamic type of the object becomes this7328 // class type.7329 if (I->isBaseInitializer() && BasesSeen == RD->getNumBases())7330 EvalObj.finishedConstructingBases();7331 }7332 7333 // Default-initialize any remaining fields.7334 if (!RD->isUnion()) {7335 for (; FieldIt != RD->field_end(); ++FieldIt) {7336 if (!FieldIt->isUnnamedBitField())7337 Success &= handleDefaultInitValue(7338 FieldIt->getType(),7339 Result.getStructField(FieldIt->getFieldIndex()));7340 }7341 }7342 7343 EvalObj.finishedConstructingFields();7344 7345 return Success &&7346 EvaluateStmt(Ret, Info, Definition->getBody()) != ESR_Failed &&7347 LifetimeExtendedScope.destroy();7348}7349 7350static bool HandleConstructorCall(const Expr *E, const LValue &This,7351 ArrayRef<const Expr*> Args,7352 const CXXConstructorDecl *Definition,7353 EvalInfo &Info, APValue &Result) {7354 CallScopeRAII CallScope(Info);7355 CallRef Call = Info.CurrentCall->createCall(Definition);7356 if (!EvaluateArgs(Args, Call, Info, Definition))7357 return false;7358 7359 return HandleConstructorCall(E, This, Call, Definition, Info, Result) &&7360 CallScope.destroy();7361}7362 7363static bool HandleDestructionImpl(EvalInfo &Info, SourceRange CallRange,7364 const LValue &This, APValue &Value,7365 QualType T) {7366 // Objects can only be destroyed while they're within their lifetimes.7367 // FIXME: We have no representation for whether an object of type nullptr_t7368 // is in its lifetime; it usually doesn't matter. Perhaps we should model it7369 // as indeterminate instead?7370 if (Value.isAbsent() && !T->isNullPtrType()) {7371 APValue Printable;7372 This.moveInto(Printable);7373 Info.FFDiag(CallRange.getBegin(),7374 diag::note_constexpr_destroy_out_of_lifetime)7375 << Printable.getAsString(Info.Ctx, Info.Ctx.getLValueReferenceType(T));7376 return false;7377 }7378 7379 // Invent an expression for location purposes.7380 // FIXME: We shouldn't need to do this.7381 OpaqueValueExpr LocE(CallRange.getBegin(), Info.Ctx.IntTy, VK_PRValue);7382 7383 // For arrays, destroy elements right-to-left.7384 if (const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(T)) {7385 uint64_t Size = CAT->getZExtSize();7386 QualType ElemT = CAT->getElementType();7387 7388 if (!CheckArraySize(Info, CAT, CallRange.getBegin()))7389 return false;7390 7391 LValue ElemLV = This;7392 ElemLV.addArray(Info, &LocE, CAT);7393 if (!HandleLValueArrayAdjustment(Info, &LocE, ElemLV, ElemT, Size))7394 return false;7395 7396 // Ensure that we have actual array elements available to destroy; the7397 // destructors might mutate the value, so we can't run them on the array7398 // filler.7399 if (Size && Size > Value.getArrayInitializedElts())7400 expandArray(Value, Value.getArraySize() - 1);7401 7402 // The size of the array might have been reduced by7403 // a placement new.7404 for (Size = Value.getArraySize(); Size != 0; --Size) {7405 APValue &Elem = Value.getArrayInitializedElt(Size - 1);7406 if (!HandleLValueArrayAdjustment(Info, &LocE, ElemLV, ElemT, -1) ||7407 !HandleDestructionImpl(Info, CallRange, ElemLV, Elem, ElemT))7408 return false;7409 }7410 7411 // End the lifetime of this array now.7412 Value = APValue();7413 return true;7414 }7415 7416 const CXXRecordDecl *RD = T->getAsCXXRecordDecl();7417 if (!RD) {7418 if (T.isDestructedType()) {7419 Info.FFDiag(CallRange.getBegin(),7420 diag::note_constexpr_unsupported_destruction)7421 << T;7422 return false;7423 }7424 7425 Value = APValue();7426 return true;7427 }7428 7429 if (RD->getNumVBases()) {7430 Info.FFDiag(CallRange.getBegin(), diag::note_constexpr_virtual_base) << RD;7431 return false;7432 }7433 7434 const CXXDestructorDecl *DD = RD->getDestructor();7435 if (!DD && !RD->hasTrivialDestructor()) {7436 Info.FFDiag(CallRange.getBegin());7437 return false;7438 }7439 7440 if (!DD || DD->isTrivial() ||7441 (RD->isAnonymousStructOrUnion() && RD->isUnion())) {7442 // A trivial destructor just ends the lifetime of the object. Check for7443 // this case before checking for a body, because we might not bother7444 // building a body for a trivial destructor. Note that it doesn't matter7445 // whether the destructor is constexpr in this case; all trivial7446 // destructors are constexpr.7447 //7448 // If an anonymous union would be destroyed, some enclosing destructor must7449 // have been explicitly defined, and the anonymous union destruction should7450 // have no effect.7451 Value = APValue();7452 return true;7453 }7454 7455 if (!Info.CheckCallLimit(CallRange.getBegin()))7456 return false;7457 7458 const FunctionDecl *Definition = nullptr;7459 const Stmt *Body = DD->getBody(Definition);7460 7461 if (!CheckConstexprFunction(Info, CallRange.getBegin(), DD, Definition, Body))7462 return false;7463 7464 CallStackFrame Frame(Info, CallRange, Definition, &This, /*CallExpr=*/nullptr,7465 CallRef());7466 7467 // We're now in the period of destruction of this object.7468 unsigned BasesLeft = RD->getNumBases();7469 EvalInfo::EvaluatingDestructorRAII EvalObj(7470 Info,7471 ObjectUnderConstruction{This.getLValueBase(), This.Designator.Entries});7472 if (!EvalObj.DidInsert) {7473 // C++2a [class.dtor]p19:7474 // the behavior is undefined if the destructor is invoked for an object7475 // whose lifetime has ended7476 // (Note that formally the lifetime ends when the period of destruction7477 // begins, even though certain uses of the object remain valid until the7478 // period of destruction ends.)7479 Info.FFDiag(CallRange.getBegin(), diag::note_constexpr_double_destroy);7480 return false;7481 }7482 7483 // FIXME: Creating an APValue just to hold a nonexistent return value is7484 // wasteful.7485 APValue RetVal;7486 StmtResult Ret = {RetVal, nullptr};7487 if (EvaluateStmt(Ret, Info, Definition->getBody()) == ESR_Failed)7488 return false;7489 7490 // A union destructor does not implicitly destroy its members.7491 if (RD->isUnion())7492 return true;7493 7494 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD);7495 7496 // We don't have a good way to iterate fields in reverse, so collect all the7497 // fields first and then walk them backwards.7498 SmallVector<FieldDecl*, 16> Fields(RD->fields());7499 for (const FieldDecl *FD : llvm::reverse(Fields)) {7500 if (FD->isUnnamedBitField())7501 continue;7502 7503 LValue Subobject = This;7504 if (!HandleLValueMember(Info, &LocE, Subobject, FD, &Layout))7505 return false;7506 7507 APValue *SubobjectValue = &Value.getStructField(FD->getFieldIndex());7508 if (!HandleDestructionImpl(Info, CallRange, Subobject, *SubobjectValue,7509 FD->getType()))7510 return false;7511 }7512 7513 if (BasesLeft != 0)7514 EvalObj.startedDestroyingBases();7515 7516 // Destroy base classes in reverse order.7517 for (const CXXBaseSpecifier &Base : llvm::reverse(RD->bases())) {7518 --BasesLeft;7519 7520 QualType BaseType = Base.getType();7521 LValue Subobject = This;7522 if (!HandleLValueDirectBase(Info, &LocE, Subobject, RD,7523 BaseType->getAsCXXRecordDecl(), &Layout))7524 return false;7525 7526 APValue *SubobjectValue = &Value.getStructBase(BasesLeft);7527 if (!HandleDestructionImpl(Info, CallRange, Subobject, *SubobjectValue,7528 BaseType))7529 return false;7530 }7531 assert(BasesLeft == 0 && "NumBases was wrong?");7532 7533 // The period of destruction ends now. The object is gone.7534 Value = APValue();7535 return true;7536}7537 7538namespace {7539struct DestroyObjectHandler {7540 EvalInfo &Info;7541 const Expr *E;7542 const LValue &This;7543 const AccessKinds AccessKind;7544 7545 typedef bool result_type;7546 bool failed() { return false; }7547 bool found(APValue &Subobj, QualType SubobjType) {7548 return HandleDestructionImpl(Info, E->getSourceRange(), This, Subobj,7549 SubobjType);7550 }7551 bool found(APSInt &Value, QualType SubobjType) {7552 Info.FFDiag(E, diag::note_constexpr_destroy_complex_elem);7553 return false;7554 }7555 bool found(APFloat &Value, QualType SubobjType) {7556 Info.FFDiag(E, diag::note_constexpr_destroy_complex_elem);7557 return false;7558 }7559};7560}7561 7562/// Perform a destructor or pseudo-destructor call on the given object, which7563/// might in general not be a complete object.7564static bool HandleDestruction(EvalInfo &Info, const Expr *E,7565 const LValue &This, QualType ThisType) {7566 CompleteObject Obj = findCompleteObject(Info, E, AK_Destroy, This, ThisType);7567 DestroyObjectHandler Handler = {Info, E, This, AK_Destroy};7568 return Obj && findSubobject(Info, E, Obj, This.Designator, Handler);7569}7570 7571/// Destroy and end the lifetime of the given complete object.7572static bool HandleDestruction(EvalInfo &Info, SourceLocation Loc,7573 APValue::LValueBase LVBase, APValue &Value,7574 QualType T) {7575 // If we've had an unmodeled side-effect, we can't rely on mutable state7576 // (such as the object we're about to destroy) being correct.7577 if (Info.EvalStatus.HasSideEffects)7578 return false;7579 7580 LValue LV;7581 LV.set({LVBase});7582 return HandleDestructionImpl(Info, Loc, LV, Value, T);7583}7584 7585/// Perform a call to 'operator new' or to `__builtin_operator_new'.7586static bool HandleOperatorNewCall(EvalInfo &Info, const CallExpr *E,7587 LValue &Result) {7588 if (Info.checkingPotentialConstantExpression() ||7589 Info.SpeculativeEvaluationDepth)7590 return false;7591 7592 // This is permitted only within a call to std::allocator<T>::allocate.7593 auto Caller = Info.getStdAllocatorCaller("allocate");7594 if (!Caller) {7595 Info.FFDiag(E->getExprLoc(), Info.getLangOpts().CPlusPlus207596 ? diag::note_constexpr_new_untyped7597 : diag::note_constexpr_new);7598 return false;7599 }7600 7601 QualType ElemType = Caller.ElemType;7602 if (ElemType->isIncompleteType() || ElemType->isFunctionType()) {7603 Info.FFDiag(E->getExprLoc(),7604 diag::note_constexpr_new_not_complete_object_type)7605 << (ElemType->isIncompleteType() ? 0 : 1) << ElemType;7606 return false;7607 }7608 7609 APSInt ByteSize;7610 if (!EvaluateInteger(E->getArg(0), ByteSize, Info))7611 return false;7612 bool IsNothrow = false;7613 for (unsigned I = 1, N = E->getNumArgs(); I != N; ++I) {7614 EvaluateIgnoredValue(Info, E->getArg(I));7615 IsNothrow |= E->getType()->isNothrowT();7616 }7617 7618 CharUnits ElemSize;7619 if (!HandleSizeof(Info, E->getExprLoc(), ElemType, ElemSize))7620 return false;7621 APInt Size, Remainder;7622 APInt ElemSizeAP(ByteSize.getBitWidth(), ElemSize.getQuantity());7623 APInt::udivrem(ByteSize, ElemSizeAP, Size, Remainder);7624 if (Remainder != 0) {7625 // This likely indicates a bug in the implementation of 'std::allocator'.7626 Info.FFDiag(E->getExprLoc(), diag::note_constexpr_operator_new_bad_size)7627 << ByteSize << APSInt(ElemSizeAP, true) << ElemType;7628 return false;7629 }7630 7631 if (!Info.CheckArraySize(E->getBeginLoc(), ByteSize.getActiveBits(),7632 Size.getZExtValue(), /*Diag=*/!IsNothrow)) {7633 if (IsNothrow) {7634 Result.setNull(Info.Ctx, E->getType());7635 return true;7636 }7637 return false;7638 }7639 7640 QualType AllocType = Info.Ctx.getConstantArrayType(7641 ElemType, Size, nullptr, ArraySizeModifier::Normal, 0);7642 APValue *Val = Info.createHeapAlloc(Caller.Call, AllocType, Result);7643 *Val = APValue(APValue::UninitArray(), 0, Size.getZExtValue());7644 Result.addArray(Info, E, cast<ConstantArrayType>(AllocType));7645 return true;7646}7647 7648static bool hasVirtualDestructor(QualType T) {7649 if (CXXRecordDecl *RD = T->getAsCXXRecordDecl())7650 if (CXXDestructorDecl *DD = RD->getDestructor())7651 return DD->isVirtual();7652 return false;7653}7654 7655static const FunctionDecl *getVirtualOperatorDelete(QualType T) {7656 if (CXXRecordDecl *RD = T->getAsCXXRecordDecl())7657 if (CXXDestructorDecl *DD = RD->getDestructor())7658 return DD->isVirtual() ? DD->getOperatorDelete() : nullptr;7659 return nullptr;7660}7661 7662/// Check that the given object is a suitable pointer to a heap allocation that7663/// still exists and is of the right kind for the purpose of a deletion.7664///7665/// On success, returns the heap allocation to deallocate. On failure, produces7666/// a diagnostic and returns std::nullopt.7667static std::optional<DynAlloc *> CheckDeleteKind(EvalInfo &Info, const Expr *E,7668 const LValue &Pointer,7669 DynAlloc::Kind DeallocKind) {7670 auto PointerAsString = [&] {7671 return Pointer.toString(Info.Ctx, Info.Ctx.VoidPtrTy);7672 };7673 7674 DynamicAllocLValue DA = Pointer.Base.dyn_cast<DynamicAllocLValue>();7675 if (!DA) {7676 Info.FFDiag(E, diag::note_constexpr_delete_not_heap_alloc)7677 << PointerAsString();7678 if (Pointer.Base)7679 NoteLValueLocation(Info, Pointer.Base);7680 return std::nullopt;7681 }7682 7683 std::optional<DynAlloc *> Alloc = Info.lookupDynamicAlloc(DA);7684 if (!Alloc) {7685 Info.FFDiag(E, diag::note_constexpr_double_delete);7686 return std::nullopt;7687 }7688 7689 if (DeallocKind != (*Alloc)->getKind()) {7690 QualType AllocType = Pointer.Base.getDynamicAllocType();7691 Info.FFDiag(E, diag::note_constexpr_new_delete_mismatch)7692 << DeallocKind << (*Alloc)->getKind() << AllocType;7693 NoteLValueLocation(Info, Pointer.Base);7694 return std::nullopt;7695 }7696 7697 bool Subobject = false;7698 if (DeallocKind == DynAlloc::New) {7699 Subobject = Pointer.Designator.MostDerivedPathLength != 0 ||7700 Pointer.Designator.isOnePastTheEnd();7701 } else {7702 Subobject = Pointer.Designator.Entries.size() != 1 ||7703 Pointer.Designator.Entries[0].getAsArrayIndex() != 0;7704 }7705 if (Subobject) {7706 Info.FFDiag(E, diag::note_constexpr_delete_subobject)7707 << PointerAsString() << Pointer.Designator.isOnePastTheEnd();7708 return std::nullopt;7709 }7710 7711 return Alloc;7712}7713 7714// Perform a call to 'operator delete' or '__builtin_operator_delete'.7715static bool HandleOperatorDeleteCall(EvalInfo &Info, const CallExpr *E) {7716 if (Info.checkingPotentialConstantExpression() ||7717 Info.SpeculativeEvaluationDepth)7718 return false;7719 7720 // This is permitted only within a call to std::allocator<T>::deallocate.7721 if (!Info.getStdAllocatorCaller("deallocate")) {7722 Info.FFDiag(E->getExprLoc());7723 return true;7724 }7725 7726 LValue Pointer;7727 if (!EvaluatePointer(E->getArg(0), Pointer, Info))7728 return false;7729 for (unsigned I = 1, N = E->getNumArgs(); I != N; ++I)7730 EvaluateIgnoredValue(Info, E->getArg(I));7731 7732 if (Pointer.Designator.Invalid)7733 return false;7734 7735 // Deleting a null pointer would have no effect, but it's not permitted by7736 // std::allocator<T>::deallocate's contract.7737 if (Pointer.isNullPointer()) {7738 Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_deallocate_null);7739 return true;7740 }7741 7742 if (!CheckDeleteKind(Info, E, Pointer, DynAlloc::StdAllocator))7743 return false;7744 7745 Info.HeapAllocs.erase(Pointer.Base.get<DynamicAllocLValue>());7746 return true;7747}7748 7749//===----------------------------------------------------------------------===//7750// Generic Evaluation7751//===----------------------------------------------------------------------===//7752namespace {7753 7754class BitCastBuffer {7755 // FIXME: We're going to need bit-level granularity when we support7756 // bit-fields.7757 // FIXME: Its possible under the C++ standard for 'char' to not be 8 bits, but7758 // we don't support a host or target where that is the case. Still, we should7759 // use a more generic type in case we ever do.7760 SmallVector<std::optional<unsigned char>, 32> Bytes;7761 7762 static_assert(std::numeric_limits<unsigned char>::digits >= 8,7763 "Need at least 8 bit unsigned char");7764 7765 bool TargetIsLittleEndian;7766 7767public:7768 BitCastBuffer(CharUnits Width, bool TargetIsLittleEndian)7769 : Bytes(Width.getQuantity()),7770 TargetIsLittleEndian(TargetIsLittleEndian) {}7771 7772 [[nodiscard]] bool readObject(CharUnits Offset, CharUnits Width,7773 SmallVectorImpl<unsigned char> &Output) const {7774 for (CharUnits I = Offset, E = Offset + Width; I != E; ++I) {7775 // If a byte of an integer is uninitialized, then the whole integer is7776 // uninitialized.7777 if (!Bytes[I.getQuantity()])7778 return false;7779 Output.push_back(*Bytes[I.getQuantity()]);7780 }7781 if (llvm::sys::IsLittleEndianHost != TargetIsLittleEndian)7782 std::reverse(Output.begin(), Output.end());7783 return true;7784 }7785 7786 void writeObject(CharUnits Offset, SmallVectorImpl<unsigned char> &Input) {7787 if (llvm::sys::IsLittleEndianHost != TargetIsLittleEndian)7788 std::reverse(Input.begin(), Input.end());7789 7790 size_t Index = 0;7791 for (unsigned char Byte : Input) {7792 assert(!Bytes[Offset.getQuantity() + Index] && "overwriting a byte?");7793 Bytes[Offset.getQuantity() + Index] = Byte;7794 ++Index;7795 }7796 }7797 7798 size_t size() { return Bytes.size(); }7799};7800 7801/// Traverse an APValue to produce an BitCastBuffer, emulating how the current7802/// target would represent the value at runtime.7803class APValueToBufferConverter {7804 EvalInfo &Info;7805 BitCastBuffer Buffer;7806 const CastExpr *BCE;7807 7808 APValueToBufferConverter(EvalInfo &Info, CharUnits ObjectWidth,7809 const CastExpr *BCE)7810 : Info(Info),7811 Buffer(ObjectWidth, Info.Ctx.getTargetInfo().isLittleEndian()),7812 BCE(BCE) {}7813 7814 bool visit(const APValue &Val, QualType Ty) {7815 return visit(Val, Ty, CharUnits::fromQuantity(0));7816 }7817 7818 // Write out Val with type Ty into Buffer starting at Offset.7819 bool visit(const APValue &Val, QualType Ty, CharUnits Offset) {7820 assert((size_t)Offset.getQuantity() <= Buffer.size());7821 7822 // As a special case, nullptr_t has an indeterminate value.7823 if (Ty->isNullPtrType())7824 return true;7825 7826 // Dig through Src to find the byte at SrcOffset.7827 switch (Val.getKind()) {7828 case APValue::Indeterminate:7829 case APValue::None:7830 return true;7831 7832 case APValue::Int:7833 return visitInt(Val.getInt(), Ty, Offset);7834 case APValue::Float:7835 return visitFloat(Val.getFloat(), Ty, Offset);7836 case APValue::Array:7837 return visitArray(Val, Ty, Offset);7838 case APValue::Struct:7839 return visitRecord(Val, Ty, Offset);7840 case APValue::Vector:7841 return visitVector(Val, Ty, Offset);7842 7843 case APValue::ComplexInt:7844 case APValue::ComplexFloat:7845 return visitComplex(Val, Ty, Offset);7846 case APValue::FixedPoint:7847 // FIXME: We should support these.7848 7849 case APValue::Union:7850 case APValue::MemberPointer:7851 case APValue::AddrLabelDiff: {7852 Info.FFDiag(BCE->getBeginLoc(),7853 diag::note_constexpr_bit_cast_unsupported_type)7854 << Ty;7855 return false;7856 }7857 7858 case APValue::LValue:7859 llvm_unreachable("LValue subobject in bit_cast?");7860 }7861 llvm_unreachable("Unhandled APValue::ValueKind");7862 }7863 7864 bool visitRecord(const APValue &Val, QualType Ty, CharUnits Offset) {7865 const RecordDecl *RD = Ty->getAsRecordDecl();7866 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD);7867 7868 // Visit the base classes.7869 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {7870 for (size_t I = 0, E = CXXRD->getNumBases(); I != E; ++I) {7871 const CXXBaseSpecifier &BS = CXXRD->bases_begin()[I];7872 CXXRecordDecl *BaseDecl = BS.getType()->getAsCXXRecordDecl();7873 const APValue &Base = Val.getStructBase(I);7874 7875 // Can happen in error cases.7876 if (!Base.isStruct())7877 return false;7878 7879 if (!visitRecord(Base, BS.getType(),7880 Layout.getBaseClassOffset(BaseDecl) + Offset))7881 return false;7882 }7883 }7884 7885 // Visit the fields.7886 unsigned FieldIdx = 0;7887 for (FieldDecl *FD : RD->fields()) {7888 if (FD->isBitField()) {7889 Info.FFDiag(BCE->getBeginLoc(),7890 diag::note_constexpr_bit_cast_unsupported_bitfield);7891 return false;7892 }7893 7894 uint64_t FieldOffsetBits = Layout.getFieldOffset(FieldIdx);7895 7896 assert(FieldOffsetBits % Info.Ctx.getCharWidth() == 0 &&7897 "only bit-fields can have sub-char alignment");7898 CharUnits FieldOffset =7899 Info.Ctx.toCharUnitsFromBits(FieldOffsetBits) + Offset;7900 QualType FieldTy = FD->getType();7901 if (!visit(Val.getStructField(FieldIdx), FieldTy, FieldOffset))7902 return false;7903 ++FieldIdx;7904 }7905 7906 return true;7907 }7908 7909 bool visitArray(const APValue &Val, QualType Ty, CharUnits Offset) {7910 const auto *CAT =7911 dyn_cast_or_null<ConstantArrayType>(Ty->getAsArrayTypeUnsafe());7912 if (!CAT)7913 return false;7914 7915 CharUnits ElemWidth = Info.Ctx.getTypeSizeInChars(CAT->getElementType());7916 unsigned NumInitializedElts = Val.getArrayInitializedElts();7917 unsigned ArraySize = Val.getArraySize();7918 // First, initialize the initialized elements.7919 for (unsigned I = 0; I != NumInitializedElts; ++I) {7920 const APValue &SubObj = Val.getArrayInitializedElt(I);7921 if (!visit(SubObj, CAT->getElementType(), Offset + I * ElemWidth))7922 return false;7923 }7924 7925 // Next, initialize the rest of the array using the filler.7926 if (Val.hasArrayFiller()) {7927 const APValue &Filler = Val.getArrayFiller();7928 for (unsigned I = NumInitializedElts; I != ArraySize; ++I) {7929 if (!visit(Filler, CAT->getElementType(), Offset + I * ElemWidth))7930 return false;7931 }7932 }7933 7934 return true;7935 }7936 7937 bool visitComplex(const APValue &Val, QualType Ty, CharUnits Offset) {7938 const ComplexType *ComplexTy = Ty->castAs<ComplexType>();7939 QualType EltTy = ComplexTy->getElementType();7940 CharUnits EltSizeChars = Info.Ctx.getTypeSizeInChars(EltTy);7941 bool IsInt = Val.isComplexInt();7942 7943 if (IsInt) {7944 if (!visitInt(Val.getComplexIntReal(), EltTy,7945 Offset + (0 * EltSizeChars)))7946 return false;7947 if (!visitInt(Val.getComplexIntImag(), EltTy,7948 Offset + (1 * EltSizeChars)))7949 return false;7950 } else {7951 if (!visitFloat(Val.getComplexFloatReal(), EltTy,7952 Offset + (0 * EltSizeChars)))7953 return false;7954 if (!visitFloat(Val.getComplexFloatImag(), EltTy,7955 Offset + (1 * EltSizeChars)))7956 return false;7957 }7958 7959 return true;7960 }7961 7962 bool visitVector(const APValue &Val, QualType Ty, CharUnits Offset) {7963 const VectorType *VTy = Ty->castAs<VectorType>();7964 QualType EltTy = VTy->getElementType();7965 unsigned NElts = VTy->getNumElements();7966 7967 if (VTy->isPackedVectorBoolType(Info.Ctx)) {7968 // Special handling for OpenCL bool vectors:7969 // Since these vectors are stored as packed bits, but we can't write7970 // individual bits to the BitCastBuffer, we'll buffer all of the elements7971 // together into an appropriately sized APInt and write them all out at7972 // once. Because we don't accept vectors where NElts * EltSize isn't a7973 // multiple of the char size, there will be no padding space, so we don't7974 // have to worry about writing data which should have been left7975 // uninitialized.7976 bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian();7977 7978 llvm::APInt Res = llvm::APInt::getZero(NElts);7979 for (unsigned I = 0; I < NElts; ++I) {7980 const llvm::APSInt &EltAsInt = Val.getVectorElt(I).getInt();7981 assert(EltAsInt.isUnsigned() && EltAsInt.getBitWidth() == 1 &&7982 "bool vector element must be 1-bit unsigned integer!");7983 7984 Res.insertBits(EltAsInt, BigEndian ? (NElts - I - 1) : I);7985 }7986 7987 SmallVector<uint8_t, 8> Bytes(NElts / 8);7988 llvm::StoreIntToMemory(Res, &*Bytes.begin(), NElts / 8);7989 Buffer.writeObject(Offset, Bytes);7990 } else {7991 // Iterate over each of the elements and write them out to the buffer at7992 // the appropriate offset.7993 CharUnits EltSizeChars = Info.Ctx.getTypeSizeInChars(EltTy);7994 for (unsigned I = 0; I < NElts; ++I) {7995 if (!visit(Val.getVectorElt(I), EltTy, Offset + I * EltSizeChars))7996 return false;7997 }7998 }7999 8000 return true;8001 }8002 8003 bool visitInt(const APSInt &Val, QualType Ty, CharUnits Offset) {8004 APSInt AdjustedVal = Val;8005 unsigned Width = AdjustedVal.getBitWidth();8006 if (Ty->isBooleanType()) {8007 Width = Info.Ctx.getTypeSize(Ty);8008 AdjustedVal = AdjustedVal.extend(Width);8009 }8010 8011 SmallVector<uint8_t, 8> Bytes(Width / 8);8012 llvm::StoreIntToMemory(AdjustedVal, &*Bytes.begin(), Width / 8);8013 Buffer.writeObject(Offset, Bytes);8014 return true;8015 }8016 8017 bool visitFloat(const APFloat &Val, QualType Ty, CharUnits Offset) {8018 APSInt AsInt(Val.bitcastToAPInt());8019 return visitInt(AsInt, Ty, Offset);8020 }8021 8022public:8023 static std::optional<BitCastBuffer>8024 convert(EvalInfo &Info, const APValue &Src, const CastExpr *BCE) {8025 CharUnits DstSize = Info.Ctx.getTypeSizeInChars(BCE->getType());8026 APValueToBufferConverter Converter(Info, DstSize, BCE);8027 if (!Converter.visit(Src, BCE->getSubExpr()->getType()))8028 return std::nullopt;8029 return Converter.Buffer;8030 }8031};8032 8033/// Write an BitCastBuffer into an APValue.8034class BufferToAPValueConverter {8035 EvalInfo &Info;8036 const BitCastBuffer &Buffer;8037 const CastExpr *BCE;8038 8039 BufferToAPValueConverter(EvalInfo &Info, const BitCastBuffer &Buffer,8040 const CastExpr *BCE)8041 : Info(Info), Buffer(Buffer), BCE(BCE) {}8042 8043 // Emit an unsupported bit_cast type error. Sema refuses to build a bit_cast8044 // with an invalid type, so anything left is a deficiency on our part (FIXME).8045 // Ideally this will be unreachable.8046 std::nullopt_t unsupportedType(QualType Ty) {8047 Info.FFDiag(BCE->getBeginLoc(),8048 diag::note_constexpr_bit_cast_unsupported_type)8049 << Ty;8050 return std::nullopt;8051 }8052 8053 std::nullopt_t unrepresentableValue(QualType Ty, const APSInt &Val) {8054 Info.FFDiag(BCE->getBeginLoc(),8055 diag::note_constexpr_bit_cast_unrepresentable_value)8056 << Ty << toString(Val, /*Radix=*/10);8057 return std::nullopt;8058 }8059 8060 std::optional<APValue> visit(const BuiltinType *T, CharUnits Offset,8061 const EnumType *EnumSugar = nullptr) {8062 if (T->isNullPtrType()) {8063 uint64_t NullValue = Info.Ctx.getTargetNullPointerValue(QualType(T, 0));8064 return APValue((Expr *)nullptr,8065 /*Offset=*/CharUnits::fromQuantity(NullValue),8066 APValue::NoLValuePath{}, /*IsNullPtr=*/true);8067 }8068 8069 CharUnits SizeOf = Info.Ctx.getTypeSizeInChars(T);8070 8071 // Work around floating point types that contain unused padding bytes. This8072 // is really just `long double` on x86, which is the only fundamental type8073 // with padding bytes.8074 if (T->isRealFloatingType()) {8075 const llvm::fltSemantics &Semantics =8076 Info.Ctx.getFloatTypeSemantics(QualType(T, 0));8077 unsigned NumBits = llvm::APFloatBase::getSizeInBits(Semantics);8078 assert(NumBits % 8 == 0);8079 CharUnits NumBytes = CharUnits::fromQuantity(NumBits / 8);8080 if (NumBytes != SizeOf)8081 SizeOf = NumBytes;8082 }8083 8084 SmallVector<uint8_t, 8> Bytes;8085 if (!Buffer.readObject(Offset, SizeOf, Bytes)) {8086 // If this is std::byte or unsigned char, then its okay to store an8087 // indeterminate value.8088 bool IsStdByte = EnumSugar && EnumSugar->isStdByteType();8089 bool IsUChar =8090 !EnumSugar && (T->isSpecificBuiltinType(BuiltinType::UChar) ||8091 T->isSpecificBuiltinType(BuiltinType::Char_U));8092 if (!IsStdByte && !IsUChar) {8093 QualType DisplayType(EnumSugar ? (const Type *)EnumSugar : T, 0);8094 Info.FFDiag(BCE->getExprLoc(),8095 diag::note_constexpr_bit_cast_indet_dest)8096 << DisplayType << Info.Ctx.getLangOpts().CharIsSigned;8097 return std::nullopt;8098 }8099 8100 return APValue::IndeterminateValue();8101 }8102 8103 APSInt Val(SizeOf.getQuantity() * Info.Ctx.getCharWidth(), true);8104 llvm::LoadIntFromMemory(Val, &*Bytes.begin(), Bytes.size());8105 8106 if (T->isIntegralOrEnumerationType()) {8107 Val.setIsSigned(T->isSignedIntegerOrEnumerationType());8108 8109 unsigned IntWidth = Info.Ctx.getIntWidth(QualType(T, 0));8110 if (IntWidth != Val.getBitWidth()) {8111 APSInt Truncated = Val.trunc(IntWidth);8112 if (Truncated.extend(Val.getBitWidth()) != Val)8113 return unrepresentableValue(QualType(T, 0), Val);8114 Val = Truncated;8115 }8116 8117 return APValue(Val);8118 }8119 8120 if (T->isRealFloatingType()) {8121 const llvm::fltSemantics &Semantics =8122 Info.Ctx.getFloatTypeSemantics(QualType(T, 0));8123 return APValue(APFloat(Semantics, Val));8124 }8125 8126 return unsupportedType(QualType(T, 0));8127 }8128 8129 std::optional<APValue> visit(const RecordType *RTy, CharUnits Offset) {8130 const RecordDecl *RD = RTy->getAsRecordDecl();8131 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD);8132 8133 unsigned NumBases = 0;8134 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD))8135 NumBases = CXXRD->getNumBases();8136 8137 APValue ResultVal(APValue::UninitStruct(), NumBases, RD->getNumFields());8138 8139 // Visit the base classes.8140 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {8141 for (size_t I = 0, E = CXXRD->getNumBases(); I != E; ++I) {8142 const CXXBaseSpecifier &BS = CXXRD->bases_begin()[I];8143 CXXRecordDecl *BaseDecl = BS.getType()->getAsCXXRecordDecl();8144 8145 std::optional<APValue> SubObj = visitType(8146 BS.getType(), Layout.getBaseClassOffset(BaseDecl) + Offset);8147 if (!SubObj)8148 return std::nullopt;8149 ResultVal.getStructBase(I) = *SubObj;8150 }8151 }8152 8153 // Visit the fields.8154 unsigned FieldIdx = 0;8155 for (FieldDecl *FD : RD->fields()) {8156 // FIXME: We don't currently support bit-fields. A lot of the logic for8157 // this is in CodeGen, so we need to factor it around.8158 if (FD->isBitField()) {8159 Info.FFDiag(BCE->getBeginLoc(),8160 diag::note_constexpr_bit_cast_unsupported_bitfield);8161 return std::nullopt;8162 }8163 8164 uint64_t FieldOffsetBits = Layout.getFieldOffset(FieldIdx);8165 assert(FieldOffsetBits % Info.Ctx.getCharWidth() == 0);8166 8167 CharUnits FieldOffset =8168 CharUnits::fromQuantity(FieldOffsetBits / Info.Ctx.getCharWidth()) +8169 Offset;8170 QualType FieldTy = FD->getType();8171 std::optional<APValue> SubObj = visitType(FieldTy, FieldOffset);8172 if (!SubObj)8173 return std::nullopt;8174 ResultVal.getStructField(FieldIdx) = *SubObj;8175 ++FieldIdx;8176 }8177 8178 return ResultVal;8179 }8180 8181 std::optional<APValue> visit(const EnumType *Ty, CharUnits Offset) {8182 QualType RepresentationType =8183 Ty->getDecl()->getDefinitionOrSelf()->getIntegerType();8184 assert(!RepresentationType.isNull() &&8185 "enum forward decl should be caught by Sema");8186 const auto *AsBuiltin =8187 RepresentationType.getCanonicalType()->castAs<BuiltinType>();8188 // Recurse into the underlying type. Treat std::byte transparently as8189 // unsigned char.8190 return visit(AsBuiltin, Offset, /*EnumTy=*/Ty);8191 }8192 8193 std::optional<APValue> visit(const ConstantArrayType *Ty, CharUnits Offset) {8194 size_t Size = Ty->getLimitedSize();8195 CharUnits ElementWidth = Info.Ctx.getTypeSizeInChars(Ty->getElementType());8196 8197 APValue ArrayValue(APValue::UninitArray(), Size, Size);8198 for (size_t I = 0; I != Size; ++I) {8199 std::optional<APValue> ElementValue =8200 visitType(Ty->getElementType(), Offset + I * ElementWidth);8201 if (!ElementValue)8202 return std::nullopt;8203 ArrayValue.getArrayInitializedElt(I) = std::move(*ElementValue);8204 }8205 8206 return ArrayValue;8207 }8208 8209 std::optional<APValue> visit(const ComplexType *Ty, CharUnits Offset) {8210 QualType ElementType = Ty->getElementType();8211 CharUnits ElementWidth = Info.Ctx.getTypeSizeInChars(ElementType);8212 bool IsInt = ElementType->isIntegerType();8213 8214 std::optional<APValue> Values[2];8215 for (unsigned I = 0; I != 2; ++I) {8216 Values[I] = visitType(Ty->getElementType(), Offset + I * ElementWidth);8217 if (!Values[I])8218 return std::nullopt;8219 }8220 8221 if (IsInt)8222 return APValue(Values[0]->getInt(), Values[1]->getInt());8223 return APValue(Values[0]->getFloat(), Values[1]->getFloat());8224 }8225 8226 std::optional<APValue> visit(const VectorType *VTy, CharUnits Offset) {8227 QualType EltTy = VTy->getElementType();8228 unsigned NElts = VTy->getNumElements();8229 unsigned EltSize =8230 VTy->isPackedVectorBoolType(Info.Ctx) ? 1 : Info.Ctx.getTypeSize(EltTy);8231 8232 SmallVector<APValue, 4> Elts;8233 Elts.reserve(NElts);8234 if (VTy->isPackedVectorBoolType(Info.Ctx)) {8235 // Special handling for OpenCL bool vectors:8236 // Since these vectors are stored as packed bits, but we can't read8237 // individual bits from the BitCastBuffer, we'll buffer all of the8238 // elements together into an appropriately sized APInt and write them all8239 // out at once. Because we don't accept vectors where NElts * EltSize8240 // isn't a multiple of the char size, there will be no padding space, so8241 // we don't have to worry about reading any padding data which didn't8242 // actually need to be accessed.8243 bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian();8244 8245 SmallVector<uint8_t, 8> Bytes;8246 Bytes.reserve(NElts / 8);8247 if (!Buffer.readObject(Offset, CharUnits::fromQuantity(NElts / 8), Bytes))8248 return std::nullopt;8249 8250 APSInt SValInt(NElts, true);8251 llvm::LoadIntFromMemory(SValInt, &*Bytes.begin(), Bytes.size());8252 8253 for (unsigned I = 0; I < NElts; ++I) {8254 llvm::APInt Elt =8255 SValInt.extractBits(1, (BigEndian ? NElts - I - 1 : I) * EltSize);8256 Elts.emplace_back(8257 APSInt(std::move(Elt), !EltTy->isSignedIntegerType()));8258 }8259 } else {8260 // Iterate over each of the elements and read them from the buffer at8261 // the appropriate offset.8262 CharUnits EltSizeChars = Info.Ctx.getTypeSizeInChars(EltTy);8263 for (unsigned I = 0; I < NElts; ++I) {8264 std::optional<APValue> EltValue =8265 visitType(EltTy, Offset + I * EltSizeChars);8266 if (!EltValue)8267 return std::nullopt;8268 Elts.push_back(std::move(*EltValue));8269 }8270 }8271 8272 return APValue(Elts.data(), Elts.size());8273 }8274 8275 std::optional<APValue> visit(const Type *Ty, CharUnits Offset) {8276 return unsupportedType(QualType(Ty, 0));8277 }8278 8279 std::optional<APValue> visitType(QualType Ty, CharUnits Offset) {8280 QualType Can = Ty.getCanonicalType();8281 8282 switch (Can->getTypeClass()) {8283#define TYPE(Class, Base) \8284 case Type::Class: \8285 return visit(cast<Class##Type>(Can.getTypePtr()), Offset);8286#define ABSTRACT_TYPE(Class, Base)8287#define NON_CANONICAL_TYPE(Class, Base) \8288 case Type::Class: \8289 llvm_unreachable("non-canonical type should be impossible!");8290#define DEPENDENT_TYPE(Class, Base) \8291 case Type::Class: \8292 llvm_unreachable( \8293 "dependent types aren't supported in the constant evaluator!");8294#define NON_CANONICAL_UNLESS_DEPENDENT(Class, Base) \8295 case Type::Class: \8296 llvm_unreachable("either dependent or not canonical!");8297#include "clang/AST/TypeNodes.inc"8298 }8299 llvm_unreachable("Unhandled Type::TypeClass");8300 }8301 8302public:8303 // Pull out a full value of type DstType.8304 static std::optional<APValue> convert(EvalInfo &Info, BitCastBuffer &Buffer,8305 const CastExpr *BCE) {8306 BufferToAPValueConverter Converter(Info, Buffer, BCE);8307 return Converter.visitType(BCE->getType(), CharUnits::fromQuantity(0));8308 }8309};8310 8311static bool checkBitCastConstexprEligibilityType(SourceLocation Loc,8312 QualType Ty, EvalInfo *Info,8313 const ASTContext &Ctx,8314 bool CheckingDest) {8315 Ty = Ty.getCanonicalType();8316 8317 auto diag = [&](int Reason) {8318 if (Info)8319 Info->FFDiag(Loc, diag::note_constexpr_bit_cast_invalid_type)8320 << CheckingDest << (Reason == 4) << Reason;8321 return false;8322 };8323 auto note = [&](int Construct, QualType NoteTy, SourceLocation NoteLoc) {8324 if (Info)8325 Info->Note(NoteLoc, diag::note_constexpr_bit_cast_invalid_subtype)8326 << NoteTy << Construct << Ty;8327 return false;8328 };8329 8330 if (Ty->isUnionType())8331 return diag(0);8332 if (Ty->isPointerType())8333 return diag(1);8334 if (Ty->isMemberPointerType())8335 return diag(2);8336 if (Ty.isVolatileQualified())8337 return diag(3);8338 8339 if (RecordDecl *Record = Ty->getAsRecordDecl()) {8340 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Record)) {8341 for (CXXBaseSpecifier &BS : CXXRD->bases())8342 if (!checkBitCastConstexprEligibilityType(Loc, BS.getType(), Info, Ctx,8343 CheckingDest))8344 return note(1, BS.getType(), BS.getBeginLoc());8345 }8346 for (FieldDecl *FD : Record->fields()) {8347 if (FD->getType()->isReferenceType())8348 return diag(4);8349 if (!checkBitCastConstexprEligibilityType(Loc, FD->getType(), Info, Ctx,8350 CheckingDest))8351 return note(0, FD->getType(), FD->getBeginLoc());8352 }8353 }8354 8355 if (Ty->isArrayType() &&8356 !checkBitCastConstexprEligibilityType(Loc, Ctx.getBaseElementType(Ty),8357 Info, Ctx, CheckingDest))8358 return false;8359 8360 if (const auto *VTy = Ty->getAs<VectorType>()) {8361 QualType EltTy = VTy->getElementType();8362 unsigned NElts = VTy->getNumElements();8363 unsigned EltSize =8364 VTy->isPackedVectorBoolType(Ctx) ? 1 : Ctx.getTypeSize(EltTy);8365 8366 if ((NElts * EltSize) % Ctx.getCharWidth() != 0) {8367 // The vector's size in bits is not a multiple of the target's byte size,8368 // so its layout is unspecified. For now, we'll simply treat these cases8369 // as unsupported (this should only be possible with OpenCL bool vectors8370 // whose element count isn't a multiple of the byte size).8371 if (Info)8372 Info->FFDiag(Loc, diag::note_constexpr_bit_cast_invalid_vector)8373 << QualType(VTy, 0) << EltSize << NElts << Ctx.getCharWidth();8374 return false;8375 }8376 8377 if (EltTy->isRealFloatingType() &&8378 &Ctx.getFloatTypeSemantics(EltTy) == &APFloat::x87DoubleExtended()) {8379 // The layout for x86_fp80 vectors seems to be handled very inconsistently8380 // by both clang and LLVM, so for now we won't allow bit_casts involving8381 // it in a constexpr context.8382 if (Info)8383 Info->FFDiag(Loc, diag::note_constexpr_bit_cast_unsupported_type)8384 << EltTy;8385 return false;8386 }8387 }8388 8389 return true;8390}8391 8392static bool checkBitCastConstexprEligibility(EvalInfo *Info,8393 const ASTContext &Ctx,8394 const CastExpr *BCE) {8395 bool DestOK = checkBitCastConstexprEligibilityType(8396 BCE->getBeginLoc(), BCE->getType(), Info, Ctx, true);8397 bool SourceOK = DestOK && checkBitCastConstexprEligibilityType(8398 BCE->getBeginLoc(),8399 BCE->getSubExpr()->getType(), Info, Ctx, false);8400 return SourceOK;8401}8402 8403static bool handleRValueToRValueBitCast(EvalInfo &Info, APValue &DestValue,8404 const APValue &SourceRValue,8405 const CastExpr *BCE) {8406 assert(CHAR_BIT == 8 && Info.Ctx.getTargetInfo().getCharWidth() == 8 &&8407 "no host or target supports non 8-bit chars");8408 8409 if (!checkBitCastConstexprEligibility(&Info, Info.Ctx, BCE))8410 return false;8411 8412 // Read out SourceValue into a char buffer.8413 std::optional<BitCastBuffer> Buffer =8414 APValueToBufferConverter::convert(Info, SourceRValue, BCE);8415 if (!Buffer)8416 return false;8417 8418 // Write out the buffer into a new APValue.8419 std::optional<APValue> MaybeDestValue =8420 BufferToAPValueConverter::convert(Info, *Buffer, BCE);8421 if (!MaybeDestValue)8422 return false;8423 8424 DestValue = std::move(*MaybeDestValue);8425 return true;8426}8427 8428static bool handleLValueToRValueBitCast(EvalInfo &Info, APValue &DestValue,8429 APValue &SourceValue,8430 const CastExpr *BCE) {8431 assert(CHAR_BIT == 8 && Info.Ctx.getTargetInfo().getCharWidth() == 8 &&8432 "no host or target supports non 8-bit chars");8433 assert(SourceValue.isLValue() &&8434 "LValueToRValueBitcast requires an lvalue operand!");8435 8436 LValue SourceLValue;8437 APValue SourceRValue;8438 SourceLValue.setFrom(Info.Ctx, SourceValue);8439 if (!handleLValueToRValueConversion(8440 Info, BCE, BCE->getSubExpr()->getType().withConst(), SourceLValue,8441 SourceRValue, /*WantObjectRepresentation=*/true))8442 return false;8443 8444 return handleRValueToRValueBitCast(Info, DestValue, SourceRValue, BCE);8445}8446 8447template <class Derived>8448class ExprEvaluatorBase8449 : public ConstStmtVisitor<Derived, bool> {8450private:8451 Derived &getDerived() { return static_cast<Derived&>(*this); }8452 bool DerivedSuccess(const APValue &V, const Expr *E) {8453 return getDerived().Success(V, E);8454 }8455 bool DerivedZeroInitialization(const Expr *E) {8456 return getDerived().ZeroInitialization(E);8457 }8458 8459 // Check whether a conditional operator with a non-constant condition is a8460 // potential constant expression. If neither arm is a potential constant8461 // expression, then the conditional operator is not either.8462 template<typename ConditionalOperator>8463 void CheckPotentialConstantConditional(const ConditionalOperator *E) {8464 assert(Info.checkingPotentialConstantExpression());8465 8466 // Speculatively evaluate both arms.8467 SmallVector<PartialDiagnosticAt, 8> Diag;8468 {8469 SpeculativeEvaluationRAII Speculate(Info, &Diag);8470 StmtVisitorTy::Visit(E->getFalseExpr());8471 if (Diag.empty())8472 return;8473 }8474 8475 {8476 SpeculativeEvaluationRAII Speculate(Info, &Diag);8477 Diag.clear();8478 StmtVisitorTy::Visit(E->getTrueExpr());8479 if (Diag.empty())8480 return;8481 }8482 8483 Error(E, diag::note_constexpr_conditional_never_const);8484 }8485 8486 8487 template<typename ConditionalOperator>8488 bool HandleConditionalOperator(const ConditionalOperator *E) {8489 bool BoolResult;8490 if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info)) {8491 if (Info.checkingPotentialConstantExpression() && Info.noteFailure()) {8492 CheckPotentialConstantConditional(E);8493 return false;8494 }8495 if (Info.noteFailure()) {8496 StmtVisitorTy::Visit(E->getTrueExpr());8497 StmtVisitorTy::Visit(E->getFalseExpr());8498 }8499 return false;8500 }8501 8502 Expr *EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();8503 return StmtVisitorTy::Visit(EvalExpr);8504 }8505 8506protected:8507 EvalInfo &Info;8508 typedef ConstStmtVisitor<Derived, bool> StmtVisitorTy;8509 typedef ExprEvaluatorBase ExprEvaluatorBaseTy;8510 8511 OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) {8512 return Info.CCEDiag(E, D);8513 }8514 8515 bool ZeroInitialization(const Expr *E) { return Error(E); }8516 8517 bool IsConstantEvaluatedBuiltinCall(const CallExpr *E) {8518 unsigned BuiltinOp = E->getBuiltinCallee();8519 return BuiltinOp != 0 &&8520 Info.Ctx.BuiltinInfo.isConstantEvaluated(BuiltinOp);8521 }8522 8523public:8524 ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {}8525 8526 EvalInfo &getEvalInfo() { return Info; }8527 8528 /// Report an evaluation error. This should only be called when an error is8529 /// first discovered. When propagating an error, just return false.8530 bool Error(const Expr *E, diag::kind D) {8531 Info.FFDiag(E, D) << E->getSourceRange();8532 return false;8533 }8534 bool Error(const Expr *E) {8535 return Error(E, diag::note_invalid_subexpr_in_const_expr);8536 }8537 8538 bool VisitStmt(const Stmt *) {8539 llvm_unreachable("Expression evaluator should not be called on stmts");8540 }8541 bool VisitExpr(const Expr *E) {8542 return Error(E);8543 }8544 8545 bool VisitEmbedExpr(const EmbedExpr *E) {8546 const auto It = E->begin();8547 return StmtVisitorTy::Visit(*It);8548 }8549 8550 bool VisitPredefinedExpr(const PredefinedExpr *E) {8551 return StmtVisitorTy::Visit(E->getFunctionName());8552 }8553 bool VisitConstantExpr(const ConstantExpr *E) {8554 if (E->hasAPValueResult())8555 return DerivedSuccess(E->getAPValueResult(), E);8556 8557 return StmtVisitorTy::Visit(E->getSubExpr());8558 }8559 8560 bool VisitParenExpr(const ParenExpr *E)8561 { return StmtVisitorTy::Visit(E->getSubExpr()); }8562 bool VisitUnaryExtension(const UnaryOperator *E)8563 { return StmtVisitorTy::Visit(E->getSubExpr()); }8564 bool VisitUnaryPlus(const UnaryOperator *E)8565 { return StmtVisitorTy::Visit(E->getSubExpr()); }8566 bool VisitChooseExpr(const ChooseExpr *E)8567 { return StmtVisitorTy::Visit(E->getChosenSubExpr()); }8568 bool VisitGenericSelectionExpr(const GenericSelectionExpr *E)8569 { return StmtVisitorTy::Visit(E->getResultExpr()); }8570 bool VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E)8571 { return StmtVisitorTy::Visit(E->getReplacement()); }8572 bool VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) {8573 TempVersionRAII RAII(*Info.CurrentCall);8574 SourceLocExprScopeGuard Guard(E, Info.CurrentCall->CurSourceLocExprScope);8575 return StmtVisitorTy::Visit(E->getExpr());8576 }8577 bool VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *E) {8578 TempVersionRAII RAII(*Info.CurrentCall);8579 // The initializer may not have been parsed yet, or might be erroneous.8580 if (!E->getExpr())8581 return Error(E);8582 SourceLocExprScopeGuard Guard(E, Info.CurrentCall->CurSourceLocExprScope);8583 return StmtVisitorTy::Visit(E->getExpr());8584 }8585 8586 bool VisitExprWithCleanups(const ExprWithCleanups *E) {8587 FullExpressionRAII Scope(Info);8588 return StmtVisitorTy::Visit(E->getSubExpr()) && Scope.destroy();8589 }8590 8591 // Temporaries are registered when created, so we don't care about8592 // CXXBindTemporaryExpr.8593 bool VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *E) {8594 return StmtVisitorTy::Visit(E->getSubExpr());8595 }8596 8597 bool VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E) {8598 CCEDiag(E, diag::note_constexpr_invalid_cast)8599 << diag::ConstexprInvalidCastKind::Reinterpret;8600 return static_cast<Derived*>(this)->VisitCastExpr(E);8601 }8602 bool VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) {8603 if (!Info.Ctx.getLangOpts().CPlusPlus20)8604 CCEDiag(E, diag::note_constexpr_invalid_cast)8605 << diag::ConstexprInvalidCastKind::Dynamic;8606 return static_cast<Derived*>(this)->VisitCastExpr(E);8607 }8608 bool VisitBuiltinBitCastExpr(const BuiltinBitCastExpr *E) {8609 return static_cast<Derived*>(this)->VisitCastExpr(E);8610 }8611 8612 bool VisitBinaryOperator(const BinaryOperator *E) {8613 switch (E->getOpcode()) {8614 default:8615 return Error(E);8616 8617 case BO_Comma:8618 VisitIgnoredValue(E->getLHS());8619 return StmtVisitorTy::Visit(E->getRHS());8620 8621 case BO_PtrMemD:8622 case BO_PtrMemI: {8623 LValue Obj;8624 if (!HandleMemberPointerAccess(Info, E, Obj))8625 return false;8626 APValue Result;8627 if (!handleLValueToRValueConversion(Info, E, E->getType(), Obj, Result))8628 return false;8629 return DerivedSuccess(Result, E);8630 }8631 }8632 }8633 8634 bool VisitCXXRewrittenBinaryOperator(const CXXRewrittenBinaryOperator *E) {8635 return StmtVisitorTy::Visit(E->getSemanticForm());8636 }8637 8638 bool VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) {8639 // Evaluate and cache the common expression. We treat it as a temporary,8640 // even though it's not quite the same thing.8641 LValue CommonLV;8642 if (!Evaluate(Info.CurrentCall->createTemporary(8643 E->getOpaqueValue(),8644 getStorageType(Info.Ctx, E->getOpaqueValue()),8645 ScopeKind::FullExpression, CommonLV),8646 Info, E->getCommon()))8647 return false;8648 8649 return HandleConditionalOperator(E);8650 }8651 8652 bool VisitConditionalOperator(const ConditionalOperator *E) {8653 bool IsBcpCall = false;8654 // If the condition (ignoring parens) is a __builtin_constant_p call,8655 // the result is a constant expression if it can be folded without8656 // side-effects. This is an important GNU extension. See GCC PR383778657 // for discussion.8658 if (const CallExpr *CallCE =8659 dyn_cast<CallExpr>(E->getCond()->IgnoreParenCasts()))8660 if (CallCE->getBuiltinCallee() == Builtin::BI__builtin_constant_p)8661 IsBcpCall = true;8662 8663 // Always assume __builtin_constant_p(...) ? ... : ... is a potential8664 // constant expression; we can't check whether it's potentially foldable.8665 // FIXME: We should instead treat __builtin_constant_p as non-constant if8666 // it would return 'false' in this mode.8667 if (Info.checkingPotentialConstantExpression() && IsBcpCall)8668 return false;8669 8670 FoldConstant Fold(Info, IsBcpCall);8671 if (!HandleConditionalOperator(E)) {8672 Fold.keepDiagnostics();8673 return false;8674 }8675 8676 return true;8677 }8678 8679 bool VisitOpaqueValueExpr(const OpaqueValueExpr *E) {8680 if (APValue *Value = Info.CurrentCall->getCurrentTemporary(E);8681 Value && !Value->isAbsent())8682 return DerivedSuccess(*Value, E);8683 8684 const Expr *Source = E->getSourceExpr();8685 if (!Source)8686 return Error(E);8687 if (Source == E) {8688 assert(0 && "OpaqueValueExpr recursively refers to itself");8689 return Error(E);8690 }8691 return StmtVisitorTy::Visit(Source);8692 }8693 8694 bool VisitPseudoObjectExpr(const PseudoObjectExpr *E) {8695 for (const Expr *SemE : E->semantics()) {8696 if (auto *OVE = dyn_cast<OpaqueValueExpr>(SemE)) {8697 // FIXME: We can't handle the case where an OpaqueValueExpr is also the8698 // result expression: there could be two different LValues that would8699 // refer to the same object in that case, and we can't model that.8700 if (SemE == E->getResultExpr())8701 return Error(E);8702 8703 // Unique OVEs get evaluated if and when we encounter them when8704 // emitting the rest of the semantic form, rather than eagerly.8705 if (OVE->isUnique())8706 continue;8707 8708 LValue LV;8709 if (!Evaluate(Info.CurrentCall->createTemporary(8710 OVE, getStorageType(Info.Ctx, OVE),8711 ScopeKind::FullExpression, LV),8712 Info, OVE->getSourceExpr()))8713 return false;8714 } else if (SemE == E->getResultExpr()) {8715 if (!StmtVisitorTy::Visit(SemE))8716 return false;8717 } else {8718 if (!EvaluateIgnoredValue(Info, SemE))8719 return false;8720 }8721 }8722 return true;8723 }8724 8725 bool VisitCallExpr(const CallExpr *E) {8726 APValue Result;8727 if (!handleCallExpr(E, Result, nullptr))8728 return false;8729 return DerivedSuccess(Result, E);8730 }8731 8732 bool handleCallExpr(const CallExpr *E, APValue &Result,8733 const LValue *ResultSlot) {8734 CallScopeRAII CallScope(Info);8735 8736 const Expr *Callee = E->getCallee()->IgnoreParens();8737 QualType CalleeType = Callee->getType();8738 8739 const FunctionDecl *FD = nullptr;8740 LValue *This = nullptr, ObjectArg;8741 auto Args = ArrayRef(E->getArgs(), E->getNumArgs());8742 bool HasQualifier = false;8743 8744 CallRef Call;8745 8746 // Extract function decl and 'this' pointer from the callee.8747 if (CalleeType->isSpecificBuiltinType(BuiltinType::BoundMember)) {8748 const CXXMethodDecl *Member = nullptr;8749 if (const MemberExpr *ME = dyn_cast<MemberExpr>(Callee)) {8750 // Explicit bound member calls, such as x.f() or p->g();8751 if (!EvaluateObjectArgument(Info, ME->getBase(), ObjectArg))8752 return false;8753 Member = dyn_cast<CXXMethodDecl>(ME->getMemberDecl());8754 if (!Member)8755 return Error(Callee);8756 This = &ObjectArg;8757 HasQualifier = ME->hasQualifier();8758 } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(Callee)) {8759 // Indirect bound member calls ('.*' or '->*').8760 const ValueDecl *D =8761 HandleMemberPointerAccess(Info, BE, ObjectArg, false);8762 if (!D)8763 return false;8764 Member = dyn_cast<CXXMethodDecl>(D);8765 if (!Member)8766 return Error(Callee);8767 This = &ObjectArg;8768 } else if (const auto *PDE = dyn_cast<CXXPseudoDestructorExpr>(Callee)) {8769 if (!Info.getLangOpts().CPlusPlus20)8770 Info.CCEDiag(PDE, diag::note_constexpr_pseudo_destructor);8771 return EvaluateObjectArgument(Info, PDE->getBase(), ObjectArg) &&8772 HandleDestruction(Info, PDE, ObjectArg, PDE->getDestroyedType());8773 } else8774 return Error(Callee);8775 FD = Member;8776 } else if (CalleeType->isFunctionPointerType()) {8777 LValue CalleeLV;8778 if (!EvaluatePointer(Callee, CalleeLV, Info))8779 return false;8780 8781 if (!CalleeLV.getLValueOffset().isZero())8782 return Error(Callee);8783 if (CalleeLV.isNullPointer()) {8784 Info.FFDiag(Callee, diag::note_constexpr_null_callee)8785 << const_cast<Expr *>(Callee);8786 return false;8787 }8788 FD = dyn_cast_or_null<FunctionDecl>(8789 CalleeLV.getLValueBase().dyn_cast<const ValueDecl *>());8790 if (!FD)8791 return Error(Callee);8792 // Don't call function pointers which have been cast to some other type.8793 // Per DR (no number yet), the caller and callee can differ in noexcept.8794 if (!Info.Ctx.hasSameFunctionTypeIgnoringExceptionSpec(8795 CalleeType->getPointeeType(), FD->getType())) {8796 return Error(E);8797 }8798 8799 // For an (overloaded) assignment expression, evaluate the RHS before the8800 // LHS.8801 auto *OCE = dyn_cast<CXXOperatorCallExpr>(E);8802 if (OCE && OCE->isAssignmentOp()) {8803 assert(Args.size() == 2 && "wrong number of arguments in assignment");8804 Call = Info.CurrentCall->createCall(FD);8805 bool HasThis = false;8806 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD))8807 HasThis = MD->isImplicitObjectMemberFunction();8808 if (!EvaluateArgs(HasThis ? Args.slice(1) : Args, Call, Info, FD,8809 /*RightToLeft=*/true, &ObjectArg))8810 return false;8811 }8812 8813 // Overloaded operator calls to member functions are represented as normal8814 // calls with '*this' as the first argument.8815 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);8816 if (MD &&8817 (MD->isImplicitObjectMemberFunction() || (OCE && MD->isStatic()))) {8818 // FIXME: When selecting an implicit conversion for an overloaded8819 // operator delete, we sometimes try to evaluate calls to conversion8820 // operators without a 'this' parameter!8821 if (Args.empty())8822 return Error(E);8823 8824 if (!EvaluateObjectArgument(Info, Args[0], ObjectArg))8825 return false;8826 8827 // If we are calling a static operator, the 'this' argument needs to be8828 // ignored after being evaluated.8829 if (MD->isInstance())8830 This = &ObjectArg;8831 8832 // If this is syntactically a simple assignment using a trivial8833 // assignment operator, start the lifetimes of union members as needed,8834 // per C++20 [class.union]5.8835 if (Info.getLangOpts().CPlusPlus20 && OCE &&8836 OCE->getOperator() == OO_Equal && MD->isTrivial() &&8837 !MaybeHandleUnionActiveMemberChange(Info, Args[0], ObjectArg))8838 return false;8839 8840 Args = Args.slice(1);8841 } else if (MD && MD->isLambdaStaticInvoker()) {8842 // Map the static invoker for the lambda back to the call operator.8843 // Conveniently, we don't have to slice out the 'this' argument (as is8844 // being done for the non-static case), since a static member function8845 // doesn't have an implicit argument passed in.8846 const CXXRecordDecl *ClosureClass = MD->getParent();8847 assert(8848 ClosureClass->captures().empty() &&8849 "Number of captures must be zero for conversion to function-ptr");8850 8851 const CXXMethodDecl *LambdaCallOp =8852 ClosureClass->getLambdaCallOperator();8853 8854 // Set 'FD', the function that will be called below, to the call8855 // operator. If the closure object represents a generic lambda, find8856 // the corresponding specialization of the call operator.8857 8858 if (ClosureClass->isGenericLambda()) {8859 assert(MD->isFunctionTemplateSpecialization() &&8860 "A generic lambda's static-invoker function must be a "8861 "template specialization");8862 const TemplateArgumentList *TAL = MD->getTemplateSpecializationArgs();8863 FunctionTemplateDecl *CallOpTemplate =8864 LambdaCallOp->getDescribedFunctionTemplate();8865 void *InsertPos = nullptr;8866 FunctionDecl *CorrespondingCallOpSpecialization =8867 CallOpTemplate->findSpecialization(TAL->asArray(), InsertPos);8868 assert(CorrespondingCallOpSpecialization &&8869 "We must always have a function call operator specialization "8870 "that corresponds to our static invoker specialization");8871 assert(isa<CXXMethodDecl>(CorrespondingCallOpSpecialization));8872 FD = CorrespondingCallOpSpecialization;8873 } else8874 FD = LambdaCallOp;8875 } else if (FD->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) {8876 if (FD->getDeclName().isAnyOperatorNew()) {8877 LValue Ptr;8878 if (!HandleOperatorNewCall(Info, E, Ptr))8879 return false;8880 Ptr.moveInto(Result);8881 return CallScope.destroy();8882 } else {8883 return HandleOperatorDeleteCall(Info, E) && CallScope.destroy();8884 }8885 }8886 } else8887 return Error(E);8888 8889 // Evaluate the arguments now if we've not already done so.8890 if (!Call) {8891 Call = Info.CurrentCall->createCall(FD);8892 if (!EvaluateArgs(Args, Call, Info, FD, /*RightToLeft*/ false,8893 &ObjectArg))8894 return false;8895 }8896 8897 SmallVector<QualType, 4> CovariantAdjustmentPath;8898 if (This) {8899 auto *NamedMember = dyn_cast<CXXMethodDecl>(FD);8900 if (NamedMember && NamedMember->isVirtual() && !HasQualifier) {8901 // Perform virtual dispatch, if necessary.8902 FD = HandleVirtualDispatch(Info, E, *This, NamedMember,8903 CovariantAdjustmentPath);8904 if (!FD)8905 return false;8906 } else if (NamedMember && NamedMember->isImplicitObjectMemberFunction()) {8907 // Check that the 'this' pointer points to an object of the right type.8908 // FIXME: If this is an assignment operator call, we may need to change8909 // the active union member before we check this.8910 if (!checkNonVirtualMemberCallThisPointer(Info, E, *This, NamedMember))8911 return false;8912 }8913 }8914 8915 // Destructor calls are different enough that they have their own codepath.8916 if (auto *DD = dyn_cast<CXXDestructorDecl>(FD)) {8917 assert(This && "no 'this' pointer for destructor call");8918 return HandleDestruction(Info, E, *This,8919 Info.Ctx.getCanonicalTagType(DD->getParent())) &&8920 CallScope.destroy();8921 }8922 8923 const FunctionDecl *Definition = nullptr;8924 Stmt *Body = FD->getBody(Definition);8925 SourceLocation Loc = E->getExprLoc();8926 8927 // Treat the object argument as `this` when evaluating defaulted8928 // special menmber functions8929 if (FD->hasCXXExplicitFunctionObjectParameter())8930 This = &ObjectArg;8931 8932 if (!CheckConstexprFunction(Info, Loc, FD, Definition, Body) ||8933 !HandleFunctionCall(Loc, Definition, This, E, Args, Call, Body, Info,8934 Result, ResultSlot))8935 return false;8936 8937 if (!CovariantAdjustmentPath.empty() &&8938 !HandleCovariantReturnAdjustment(Info, E, Result,8939 CovariantAdjustmentPath))8940 return false;8941 8942 return CallScope.destroy();8943 }8944 8945 bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {8946 return StmtVisitorTy::Visit(E->getInitializer());8947 }8948 bool VisitInitListExpr(const InitListExpr *E) {8949 if (E->getNumInits() == 0)8950 return DerivedZeroInitialization(E);8951 if (E->getNumInits() == 1)8952 return StmtVisitorTy::Visit(E->getInit(0));8953 return Error(E);8954 }8955 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {8956 return DerivedZeroInitialization(E);8957 }8958 bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) {8959 return DerivedZeroInitialization(E);8960 }8961 bool VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) {8962 return DerivedZeroInitialization(E);8963 }8964 8965 /// A member expression where the object is a prvalue is itself a prvalue.8966 bool VisitMemberExpr(const MemberExpr *E) {8967 assert(!Info.Ctx.getLangOpts().CPlusPlus11 &&8968 "missing temporary materialization conversion");8969 assert(!E->isArrow() && "missing call to bound member function?");8970 8971 APValue Val;8972 if (!Evaluate(Val, Info, E->getBase()))8973 return false;8974 8975 QualType BaseTy = E->getBase()->getType();8976 8977 const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());8978 if (!FD) return Error(E);8979 assert(!FD->getType()->isReferenceType() && "prvalue reference?");8980 assert(BaseTy->castAsCanonical<RecordType>()->getDecl() ==8981 FD->getParent()->getCanonicalDecl() &&8982 "record / field mismatch");8983 8984 // Note: there is no lvalue base here. But this case should only ever8985 // happen in C or in C++98, where we cannot be evaluating a constexpr8986 // constructor, which is the only case the base matters.8987 CompleteObject Obj(APValue::LValueBase(), &Val, BaseTy);8988 SubobjectDesignator Designator(BaseTy);8989 Designator.addDeclUnchecked(FD);8990 8991 APValue Result;8992 return extractSubobject(Info, E, Obj, Designator, Result) &&8993 DerivedSuccess(Result, E);8994 }8995 8996 bool VisitExtVectorElementExpr(const ExtVectorElementExpr *E) {8997 APValue Val;8998 if (!Evaluate(Val, Info, E->getBase()))8999 return false;9000 9001 if (Val.isVector()) {9002 SmallVector<uint32_t, 4> Indices;9003 E->getEncodedElementAccess(Indices);9004 if (Indices.size() == 1) {9005 // Return scalar.9006 return DerivedSuccess(Val.getVectorElt(Indices[0]), E);9007 } else {9008 // Construct new APValue vector.9009 SmallVector<APValue, 4> Elts;9010 for (unsigned I = 0; I < Indices.size(); ++I) {9011 Elts.push_back(Val.getVectorElt(Indices[I]));9012 }9013 APValue VecResult(Elts.data(), Indices.size());9014 return DerivedSuccess(VecResult, E);9015 }9016 }9017 9018 return false;9019 }9020 9021 bool VisitCastExpr(const CastExpr *E) {9022 switch (E->getCastKind()) {9023 default:9024 break;9025 9026 case CK_AtomicToNonAtomic: {9027 APValue AtomicVal;9028 // This does not need to be done in place even for class/array types:9029 // atomic-to-non-atomic conversion implies copying the object9030 // representation.9031 if (!Evaluate(AtomicVal, Info, E->getSubExpr()))9032 return false;9033 return DerivedSuccess(AtomicVal, E);9034 }9035 9036 case CK_NoOp:9037 case CK_UserDefinedConversion:9038 return StmtVisitorTy::Visit(E->getSubExpr());9039 9040 case CK_HLSLArrayRValue: {9041 const Expr *SubExpr = E->getSubExpr();9042 if (!SubExpr->isGLValue()) {9043 APValue Val;9044 if (!Evaluate(Val, Info, SubExpr))9045 return false;9046 return DerivedSuccess(Val, E);9047 }9048 9049 LValue LVal;9050 if (!EvaluateLValue(SubExpr, LVal, Info))9051 return false;9052 APValue RVal;9053 // Note, we use the subexpression's type in order to retain cv-qualifiers.9054 if (!handleLValueToRValueConversion(Info, E, SubExpr->getType(), LVal,9055 RVal))9056 return false;9057 return DerivedSuccess(RVal, E);9058 }9059 case CK_LValueToRValue: {9060 LValue LVal;9061 if (!EvaluateLValue(E->getSubExpr(), LVal, Info))9062 return false;9063 APValue RVal;9064 // Note, we use the subexpression's type in order to retain cv-qualifiers.9065 if (!handleLValueToRValueConversion(Info, E, E->getSubExpr()->getType(),9066 LVal, RVal))9067 return false;9068 return DerivedSuccess(RVal, E);9069 }9070 case CK_LValueToRValueBitCast: {9071 APValue DestValue, SourceValue;9072 if (!Evaluate(SourceValue, Info, E->getSubExpr()))9073 return false;9074 if (!handleLValueToRValueBitCast(Info, DestValue, SourceValue, E))9075 return false;9076 return DerivedSuccess(DestValue, E);9077 }9078 9079 case CK_AddressSpaceConversion: {9080 APValue Value;9081 if (!Evaluate(Value, Info, E->getSubExpr()))9082 return false;9083 return DerivedSuccess(Value, E);9084 }9085 }9086 9087 return Error(E);9088 }9089 9090 bool VisitUnaryPostInc(const UnaryOperator *UO) {9091 return VisitUnaryPostIncDec(UO);9092 }9093 bool VisitUnaryPostDec(const UnaryOperator *UO) {9094 return VisitUnaryPostIncDec(UO);9095 }9096 bool VisitUnaryPostIncDec(const UnaryOperator *UO) {9097 if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure())9098 return Error(UO);9099 9100 LValue LVal;9101 if (!EvaluateLValue(UO->getSubExpr(), LVal, Info))9102 return false;9103 APValue RVal;9104 if (!handleIncDec(this->Info, UO, LVal, UO->getSubExpr()->getType(),9105 UO->isIncrementOp(), &RVal))9106 return false;9107 return DerivedSuccess(RVal, UO);9108 }9109 9110 bool VisitStmtExpr(const StmtExpr *E) {9111 // We will have checked the full-expressions inside the statement expression9112 // when they were completed, and don't need to check them again now.9113 llvm::SaveAndRestore NotCheckingForUB(Info.CheckingForUndefinedBehavior,9114 false);9115 9116 const CompoundStmt *CS = E->getSubStmt();9117 if (CS->body_empty())9118 return true;9119 9120 BlockScopeRAII Scope(Info);9121 for (CompoundStmt::const_body_iterator BI = CS->body_begin(),9122 BE = CS->body_end();9123 /**/; ++BI) {9124 if (BI + 1 == BE) {9125 const Expr *FinalExpr = dyn_cast<Expr>(*BI);9126 if (!FinalExpr) {9127 Info.FFDiag((*BI)->getBeginLoc(),9128 diag::note_constexpr_stmt_expr_unsupported);9129 return false;9130 }9131 return this->Visit(FinalExpr) && Scope.destroy();9132 }9133 9134 APValue ReturnValue;9135 StmtResult Result = { ReturnValue, nullptr };9136 EvalStmtResult ESR = EvaluateStmt(Result, Info, *BI);9137 if (ESR != ESR_Succeeded) {9138 // FIXME: If the statement-expression terminated due to 'return',9139 // 'break', or 'continue', it would be nice to propagate that to9140 // the outer statement evaluation rather than bailing out.9141 if (ESR != ESR_Failed)9142 Info.FFDiag((*BI)->getBeginLoc(),9143 diag::note_constexpr_stmt_expr_unsupported);9144 return false;9145 }9146 }9147 9148 llvm_unreachable("Return from function from the loop above.");9149 }9150 9151 bool VisitPackIndexingExpr(const PackIndexingExpr *E) {9152 return StmtVisitorTy::Visit(E->getSelectedExpr());9153 }9154 9155 /// Visit a value which is evaluated, but whose value is ignored.9156 void VisitIgnoredValue(const Expr *E) {9157 EvaluateIgnoredValue(Info, E);9158 }9159 9160 /// Potentially visit a MemberExpr's base expression.9161 void VisitIgnoredBaseExpression(const Expr *E) {9162 // While MSVC doesn't evaluate the base expression, it does diagnose the9163 // presence of side-effecting behavior.9164 if (Info.getLangOpts().MSVCCompat && !E->HasSideEffects(Info.Ctx))9165 return;9166 VisitIgnoredValue(E);9167 }9168};9169 9170} // namespace9171 9172//===----------------------------------------------------------------------===//9173// Common base class for lvalue and temporary evaluation.9174//===----------------------------------------------------------------------===//9175namespace {9176template<class Derived>9177class LValueExprEvaluatorBase9178 : public ExprEvaluatorBase<Derived> {9179protected:9180 LValue &Result;9181 bool InvalidBaseOK;9182 typedef LValueExprEvaluatorBase LValueExprEvaluatorBaseTy;9183 typedef ExprEvaluatorBase<Derived> ExprEvaluatorBaseTy;9184 9185 bool Success(APValue::LValueBase B) {9186 Result.set(B);9187 return true;9188 }9189 9190 bool evaluatePointer(const Expr *E, LValue &Result) {9191 return EvaluatePointer(E, Result, this->Info, InvalidBaseOK);9192 }9193 9194public:9195 LValueExprEvaluatorBase(EvalInfo &Info, LValue &Result, bool InvalidBaseOK)9196 : ExprEvaluatorBaseTy(Info), Result(Result),9197 InvalidBaseOK(InvalidBaseOK) {}9198 9199 bool Success(const APValue &V, const Expr *E) {9200 Result.setFrom(this->Info.Ctx, V);9201 return true;9202 }9203 9204 bool VisitMemberExpr(const MemberExpr *E) {9205 // Handle non-static data members.9206 QualType BaseTy;9207 bool EvalOK;9208 if (E->isArrow()) {9209 EvalOK = evaluatePointer(E->getBase(), Result);9210 BaseTy = E->getBase()->getType()->castAs<PointerType>()->getPointeeType();9211 } else if (E->getBase()->isPRValue()) {9212 assert(E->getBase()->getType()->isRecordType());9213 EvalOK = EvaluateTemporary(E->getBase(), Result, this->Info);9214 BaseTy = E->getBase()->getType();9215 } else {9216 EvalOK = this->Visit(E->getBase());9217 BaseTy = E->getBase()->getType();9218 }9219 if (!EvalOK) {9220 if (!InvalidBaseOK)9221 return false;9222 Result.setInvalid(E);9223 return true;9224 }9225 9226 const ValueDecl *MD = E->getMemberDecl();9227 if (const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {9228 assert(BaseTy->castAsCanonical<RecordType>()->getDecl() ==9229 FD->getParent()->getCanonicalDecl() &&9230 "record / field mismatch");9231 (void)BaseTy;9232 if (!HandleLValueMember(this->Info, E, Result, FD))9233 return false;9234 } else if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(MD)) {9235 if (!HandleLValueIndirectMember(this->Info, E, Result, IFD))9236 return false;9237 } else9238 return this->Error(E);9239 9240 if (MD->getType()->isReferenceType()) {9241 APValue RefValue;9242 if (!handleLValueToRValueConversion(this->Info, E, MD->getType(), Result,9243 RefValue))9244 return false;9245 return Success(RefValue, E);9246 }9247 return true;9248 }9249 9250 bool VisitBinaryOperator(const BinaryOperator *E) {9251 switch (E->getOpcode()) {9252 default:9253 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);9254 9255 case BO_PtrMemD:9256 case BO_PtrMemI:9257 return HandleMemberPointerAccess(this->Info, E, Result);9258 }9259 }9260 9261 bool VisitCastExpr(const CastExpr *E) {9262 switch (E->getCastKind()) {9263 default:9264 return ExprEvaluatorBaseTy::VisitCastExpr(E);9265 9266 case CK_DerivedToBase:9267 case CK_UncheckedDerivedToBase:9268 if (!this->Visit(E->getSubExpr()))9269 return false;9270 9271 // Now figure out the necessary offset to add to the base LV to get from9272 // the derived class to the base class.9273 return HandleLValueBasePath(this->Info, E, E->getSubExpr()->getType(),9274 Result);9275 }9276 }9277};9278}9279 9280//===----------------------------------------------------------------------===//9281// LValue Evaluation9282//9283// This is used for evaluating lvalues (in C and C++), xvalues (in C++11),9284// function designators (in C), decl references to void objects (in C), and9285// temporaries (if building with -Wno-address-of-temporary).9286//9287// LValue evaluation produces values comprising a base expression of one of the9288// following types:9289// - Declarations9290// * VarDecl9291// * FunctionDecl9292// - Literals9293// * CompoundLiteralExpr in C (and in global scope in C++)9294// * StringLiteral9295// * PredefinedExpr9296// * ObjCStringLiteralExpr9297// * ObjCEncodeExpr9298// * AddrLabelExpr9299// * BlockExpr9300// * CallExpr for a MakeStringConstant builtin9301// - typeid(T) expressions, as TypeInfoLValues9302// - Locals and temporaries9303// * MaterializeTemporaryExpr9304// * Any Expr, with a CallIndex indicating the function in which the temporary9305// was evaluated, for cases where the MaterializeTemporaryExpr is missing9306// from the AST (FIXME).9307// * A MaterializeTemporaryExpr that has static storage duration, with no9308// CallIndex, for a lifetime-extended temporary.9309// * The ConstantExpr that is currently being evaluated during evaluation of an9310// immediate invocation.9311// plus an offset in bytes.9312//===----------------------------------------------------------------------===//9313namespace {9314class LValueExprEvaluator9315 : public LValueExprEvaluatorBase<LValueExprEvaluator> {9316public:9317 LValueExprEvaluator(EvalInfo &Info, LValue &Result, bool InvalidBaseOK) :9318 LValueExprEvaluatorBaseTy(Info, Result, InvalidBaseOK) {}9319 9320 bool VisitVarDecl(const Expr *E, const VarDecl *VD);9321 bool VisitUnaryPreIncDec(const UnaryOperator *UO);9322 9323 bool VisitCallExpr(const CallExpr *E);9324 bool VisitDeclRefExpr(const DeclRefExpr *E);9325 bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(E); }9326 bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E);9327 bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);9328 bool VisitMemberExpr(const MemberExpr *E);9329 bool VisitStringLiteral(const StringLiteral *E) {9330 return Success(APValue::LValueBase(9331 E, 0, Info.getASTContext().getNextStringLiteralVersion()));9332 }9333 bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(E); }9334 bool VisitCXXTypeidExpr(const CXXTypeidExpr *E);9335 bool VisitCXXUuidofExpr(const CXXUuidofExpr *E);9336 bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E);9337 bool VisitExtVectorElementExpr(const ExtVectorElementExpr *E);9338 bool VisitUnaryDeref(const UnaryOperator *E);9339 bool VisitUnaryReal(const UnaryOperator *E);9340 bool VisitUnaryImag(const UnaryOperator *E);9341 bool VisitUnaryPreInc(const UnaryOperator *UO) {9342 return VisitUnaryPreIncDec(UO);9343 }9344 bool VisitUnaryPreDec(const UnaryOperator *UO) {9345 return VisitUnaryPreIncDec(UO);9346 }9347 bool VisitBinAssign(const BinaryOperator *BO);9348 bool VisitCompoundAssignOperator(const CompoundAssignOperator *CAO);9349 9350 bool VisitCastExpr(const CastExpr *E) {9351 switch (E->getCastKind()) {9352 default:9353 return LValueExprEvaluatorBaseTy::VisitCastExpr(E);9354 9355 case CK_LValueBitCast:9356 this->CCEDiag(E, diag::note_constexpr_invalid_cast)9357 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret9358 << Info.Ctx.getLangOpts().CPlusPlus;9359 if (!Visit(E->getSubExpr()))9360 return false;9361 Result.Designator.setInvalid();9362 return true;9363 9364 case CK_BaseToDerived:9365 if (!Visit(E->getSubExpr()))9366 return false;9367 return HandleBaseToDerivedCast(Info, E, Result);9368 9369 case CK_Dynamic:9370 if (!Visit(E->getSubExpr()))9371 return false;9372 return HandleDynamicCast(Info, cast<ExplicitCastExpr>(E), Result);9373 }9374 }9375};9376} // end anonymous namespace9377 9378/// Get an lvalue to a field of a lambda's closure type.9379static bool HandleLambdaCapture(EvalInfo &Info, const Expr *E, LValue &Result,9380 const CXXMethodDecl *MD, const FieldDecl *FD,9381 bool LValueToRValueConversion) {9382 // Static lambda function call operators can't have captures. We already9383 // diagnosed this, so bail out here.9384 if (MD->isStatic()) {9385 assert(Info.CurrentCall->This == nullptr &&9386 "This should not be set for a static call operator");9387 return false;9388 }9389 9390 // Start with 'Result' referring to the complete closure object...9391 if (MD->isExplicitObjectMemberFunction()) {9392 // Self may be passed by reference or by value.9393 const ParmVarDecl *Self = MD->getParamDecl(0);9394 if (Self->getType()->isReferenceType()) {9395 APValue *RefValue = Info.getParamSlot(Info.CurrentCall->Arguments, Self);9396 if (!RefValue->allowConstexprUnknown() || RefValue->hasValue())9397 Result.setFrom(Info.Ctx, *RefValue);9398 } else {9399 const ParmVarDecl *VD = Info.CurrentCall->Arguments.getOrigParam(Self);9400 CallStackFrame *Frame =9401 Info.getCallFrameAndDepth(Info.CurrentCall->Arguments.CallIndex)9402 .first;9403 unsigned Version = Info.CurrentCall->Arguments.Version;9404 Result.set({VD, Frame->Index, Version});9405 }9406 } else9407 Result = *Info.CurrentCall->This;9408 9409 // ... then update it to refer to the field of the closure object9410 // that represents the capture.9411 if (!HandleLValueMember(Info, E, Result, FD))9412 return false;9413 9414 // And if the field is of reference type (or if we captured '*this' by9415 // reference), update 'Result' to refer to what9416 // the field refers to.9417 if (LValueToRValueConversion) {9418 APValue RVal;9419 if (!handleLValueToRValueConversion(Info, E, FD->getType(), Result, RVal))9420 return false;9421 Result.setFrom(Info.Ctx, RVal);9422 }9423 return true;9424}9425 9426/// Evaluate an expression as an lvalue. This can be legitimately called on9427/// expressions which are not glvalues, in three cases:9428/// * function designators in C, and9429/// * "extern void" objects9430/// * @selector() expressions in Objective-C9431static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info,9432 bool InvalidBaseOK) {9433 assert(!E->isValueDependent());9434 assert(E->isGLValue() || E->getType()->isFunctionType() ||9435 E->getType()->isVoidType() || isa<ObjCSelectorExpr>(E->IgnoreParens()));9436 return LValueExprEvaluator(Info, Result, InvalidBaseOK).Visit(E);9437}9438 9439bool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {9440 const ValueDecl *D = E->getDecl();9441 9442 // If we are within a lambda's call operator, check whether the 'VD' referred9443 // to within 'E' actually represents a lambda-capture that maps to a9444 // data-member/field within the closure object, and if so, evaluate to the9445 // field or what the field refers to.9446 if (Info.CurrentCall && isLambdaCallOperator(Info.CurrentCall->Callee) &&9447 E->refersToEnclosingVariableOrCapture()) {9448 // We don't always have a complete capture-map when checking or inferring if9449 // the function call operator meets the requirements of a constexpr function9450 // - but we don't need to evaluate the captures to determine constexprness9451 // (dcl.constexpr C++17).9452 if (Info.checkingPotentialConstantExpression())9453 return false;9454 9455 if (auto *FD = Info.CurrentCall->LambdaCaptureFields.lookup(D)) {9456 const auto *MD = cast<CXXMethodDecl>(Info.CurrentCall->Callee);9457 return HandleLambdaCapture(Info, E, Result, MD, FD,9458 FD->getType()->isReferenceType());9459 }9460 }9461 9462 if (isa<FunctionDecl, MSGuidDecl, TemplateParamObjectDecl,9463 UnnamedGlobalConstantDecl>(D))9464 return Success(cast<ValueDecl>(D));9465 if (const VarDecl *VD = dyn_cast<VarDecl>(D))9466 return VisitVarDecl(E, VD);9467 if (const BindingDecl *BD = dyn_cast<BindingDecl>(D))9468 return Visit(BD->getBinding());9469 return Error(E);9470}9471 9472bool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) {9473 CallStackFrame *Frame = nullptr;9474 unsigned Version = 0;9475 if (VD->hasLocalStorage()) {9476 // Only if a local variable was declared in the function currently being9477 // evaluated, do we expect to be able to find its value in the current9478 // frame. (Otherwise it was likely declared in an enclosing context and9479 // could either have a valid evaluatable value (for e.g. a constexpr9480 // variable) or be ill-formed (and trigger an appropriate evaluation9481 // diagnostic)).9482 CallStackFrame *CurrFrame = Info.CurrentCall;9483 if (CurrFrame->Callee && CurrFrame->Callee->Equals(VD->getDeclContext())) {9484 // Function parameters are stored in some caller's frame. (Usually the9485 // immediate caller, but for an inherited constructor they may be more9486 // distant.)9487 if (auto *PVD = dyn_cast<ParmVarDecl>(VD)) {9488 if (CurrFrame->Arguments) {9489 VD = CurrFrame->Arguments.getOrigParam(PVD);9490 Frame =9491 Info.getCallFrameAndDepth(CurrFrame->Arguments.CallIndex).first;9492 Version = CurrFrame->Arguments.Version;9493 }9494 } else {9495 Frame = CurrFrame;9496 Version = CurrFrame->getCurrentTemporaryVersion(VD);9497 }9498 }9499 }9500 9501 if (!VD->getType()->isReferenceType()) {9502 if (Frame) {9503 Result.set({VD, Frame->Index, Version});9504 return true;9505 }9506 return Success(VD);9507 }9508 9509 if (!Info.getLangOpts().CPlusPlus11) {9510 Info.CCEDiag(E, diag::note_constexpr_ltor_non_integral, 1)9511 << VD << VD->getType();9512 Info.Note(VD->getLocation(), diag::note_declared_at);9513 }9514 9515 APValue *V;9516 if (!evaluateVarDeclInit(Info, E, VD, Frame, Version, V))9517 return false;9518 9519 if (!V) {9520 Result.set(VD);9521 Result.AllowConstexprUnknown = true;9522 return true;9523 }9524 9525 return Success(*V, E);9526}9527 9528bool LValueExprEvaluator::VisitCallExpr(const CallExpr *E) {9529 if (!IsConstantEvaluatedBuiltinCall(E))9530 return ExprEvaluatorBaseTy::VisitCallExpr(E);9531 9532 switch (E->getBuiltinCallee()) {9533 default:9534 return false;9535 case Builtin::BIas_const:9536 case Builtin::BIforward:9537 case Builtin::BIforward_like:9538 case Builtin::BImove:9539 case Builtin::BImove_if_noexcept:9540 if (cast<FunctionDecl>(E->getCalleeDecl())->isConstexpr())9541 return Visit(E->getArg(0));9542 break;9543 }9544 9545 return ExprEvaluatorBaseTy::VisitCallExpr(E);9546}9547 9548bool LValueExprEvaluator::VisitMaterializeTemporaryExpr(9549 const MaterializeTemporaryExpr *E) {9550 // Walk through the expression to find the materialized temporary itself.9551 SmallVector<const Expr *, 2> CommaLHSs;9552 SmallVector<SubobjectAdjustment, 2> Adjustments;9553 const Expr *Inner =9554 E->getSubExpr()->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments);9555 9556 // If we passed any comma operators, evaluate their LHSs.9557 for (const Expr *E : CommaLHSs)9558 if (!EvaluateIgnoredValue(Info, E))9559 return false;9560 9561 // A materialized temporary with static storage duration can appear within the9562 // result of a constant expression evaluation, so we need to preserve its9563 // value for use outside this evaluation.9564 APValue *Value;9565 if (E->getStorageDuration() == SD_Static) {9566 if (Info.EvalMode == EvaluationMode::ConstantFold)9567 return false;9568 // FIXME: What about SD_Thread?9569 Value = E->getOrCreateValue(true);9570 *Value = APValue();9571 Result.set(E);9572 } else {9573 Value = &Info.CurrentCall->createTemporary(9574 E, Inner->getType(),9575 E->getStorageDuration() == SD_FullExpression ? ScopeKind::FullExpression9576 : ScopeKind::Block,9577 Result);9578 }9579 9580 QualType Type = Inner->getType();9581 9582 // Materialize the temporary itself.9583 if (!EvaluateInPlace(*Value, Info, Result, Inner)) {9584 *Value = APValue();9585 return false;9586 }9587 9588 // Adjust our lvalue to refer to the desired subobject.9589 for (unsigned I = Adjustments.size(); I != 0; /**/) {9590 --I;9591 switch (Adjustments[I].Kind) {9592 case SubobjectAdjustment::DerivedToBaseAdjustment:9593 if (!HandleLValueBasePath(Info, Adjustments[I].DerivedToBase.BasePath,9594 Type, Result))9595 return false;9596 Type = Adjustments[I].DerivedToBase.BasePath->getType();9597 break;9598 9599 case SubobjectAdjustment::FieldAdjustment:9600 if (!HandleLValueMember(Info, E, Result, Adjustments[I].Field))9601 return false;9602 Type = Adjustments[I].Field->getType();9603 break;9604 9605 case SubobjectAdjustment::MemberPointerAdjustment:9606 if (!HandleMemberPointerAccess(this->Info, Type, Result,9607 Adjustments[I].Ptr.RHS))9608 return false;9609 Type = Adjustments[I].Ptr.MPT->getPointeeType();9610 break;9611 }9612 }9613 9614 return true;9615}9616 9617bool9618LValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {9619 assert((!Info.getLangOpts().CPlusPlus || E->isFileScope()) &&9620 "lvalue compound literal in c++?");9621 APValue *Lit;9622 // If CompountLiteral has static storage, its value can be used outside9623 // this expression. So evaluate it once and store it in ASTContext.9624 if (E->hasStaticStorage()) {9625 Lit = &E->getOrCreateStaticValue(Info.Ctx);9626 Result.set(E);9627 // Reset any previously evaluated state, otherwise evaluation below might9628 // fail.9629 // FIXME: Should we just re-use the previously evaluated value instead?9630 *Lit = APValue();9631 } else {9632 assert(!Info.getLangOpts().CPlusPlus);9633 Lit = &Info.CurrentCall->createTemporary(E, E->getInitializer()->getType(),9634 ScopeKind::Block, Result);9635 }9636 // FIXME: Evaluating in place isn't always right. We should figure out how to9637 // use appropriate evaluation context here, see9638 // clang/test/AST/static-compound-literals-reeval.cpp for a failure.9639 if (!EvaluateInPlace(*Lit, Info, Result, E->getInitializer())) {9640 *Lit = APValue();9641 return false;9642 }9643 return true;9644}9645 9646bool LValueExprEvaluator::VisitCXXTypeidExpr(const CXXTypeidExpr *E) {9647 TypeInfoLValue TypeInfo;9648 9649 if (!E->isPotentiallyEvaluated()) {9650 if (E->isTypeOperand())9651 TypeInfo = TypeInfoLValue(E->getTypeOperand(Info.Ctx).getTypePtr());9652 else9653 TypeInfo = TypeInfoLValue(E->getExprOperand()->getType().getTypePtr());9654 } else {9655 if (!Info.Ctx.getLangOpts().CPlusPlus20) {9656 Info.CCEDiag(E, diag::note_constexpr_typeid_polymorphic)9657 << E->getExprOperand()->getType()9658 << E->getExprOperand()->getSourceRange();9659 }9660 9661 if (!Visit(E->getExprOperand()))9662 return false;9663 9664 std::optional<DynamicType> DynType =9665 ComputeDynamicType(Info, E, Result, AK_TypeId);9666 if (!DynType)9667 return false;9668 9669 TypeInfo = TypeInfoLValue(9670 Info.Ctx.getCanonicalTagType(DynType->Type).getTypePtr());9671 }9672 9673 return Success(APValue::LValueBase::getTypeInfo(TypeInfo, E->getType()));9674}9675 9676bool LValueExprEvaluator::VisitCXXUuidofExpr(const CXXUuidofExpr *E) {9677 return Success(E->getGuidDecl());9678}9679 9680bool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) {9681 // Handle static data members.9682 if (const VarDecl *VD = dyn_cast<VarDecl>(E->getMemberDecl())) {9683 VisitIgnoredBaseExpression(E->getBase());9684 return VisitVarDecl(E, VD);9685 }9686 9687 // Handle static member functions.9688 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl())) {9689 if (MD->isStatic()) {9690 VisitIgnoredBaseExpression(E->getBase());9691 return Success(MD);9692 }9693 }9694 9695 // Handle non-static data members.9696 return LValueExprEvaluatorBaseTy::VisitMemberExpr(E);9697}9698 9699bool LValueExprEvaluator::VisitExtVectorElementExpr(9700 const ExtVectorElementExpr *E) {9701 bool Success = true;9702 9703 APValue Val;9704 if (!Evaluate(Val, Info, E->getBase())) {9705 if (!Info.noteFailure())9706 return false;9707 Success = false;9708 }9709 9710 SmallVector<uint32_t, 4> Indices;9711 E->getEncodedElementAccess(Indices);9712 // FIXME: support accessing more than one element9713 if (Indices.size() > 1)9714 return false;9715 9716 if (Success) {9717 Result.setFrom(Info.Ctx, Val);9718 QualType BaseType = E->getBase()->getType();9719 if (E->isArrow())9720 BaseType = BaseType->getPointeeType();9721 const auto *VT = BaseType->castAs<VectorType>();9722 HandleLValueVectorElement(Info, E, Result, VT->getElementType(),9723 VT->getNumElements(), Indices[0]);9724 }9725 9726 return Success;9727}9728 9729bool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) {9730 if (E->getBase()->getType()->isSveVLSBuiltinType())9731 return Error(E);9732 9733 APSInt Index;9734 bool Success = true;9735 9736 if (const auto *VT = E->getBase()->getType()->getAs<VectorType>()) {9737 APValue Val;9738 if (!Evaluate(Val, Info, E->getBase())) {9739 if (!Info.noteFailure())9740 return false;9741 Success = false;9742 }9743 9744 if (!EvaluateInteger(E->getIdx(), Index, Info)) {9745 if (!Info.noteFailure())9746 return false;9747 Success = false;9748 }9749 9750 if (Success) {9751 Result.setFrom(Info.Ctx, Val);9752 HandleLValueVectorElement(Info, E, Result, VT->getElementType(),9753 VT->getNumElements(), Index.getExtValue());9754 }9755 9756 return Success;9757 }9758 9759 // C++17's rules require us to evaluate the LHS first, regardless of which9760 // side is the base.9761 for (const Expr *SubExpr : {E->getLHS(), E->getRHS()}) {9762 if (SubExpr == E->getBase() ? !evaluatePointer(SubExpr, Result)9763 : !EvaluateInteger(SubExpr, Index, Info)) {9764 if (!Info.noteFailure())9765 return false;9766 Success = false;9767 }9768 }9769 9770 return Success &&9771 HandleLValueArrayAdjustment(Info, E, Result, E->getType(), Index);9772}9773 9774bool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) {9775 bool Success = evaluatePointer(E->getSubExpr(), Result);9776 // [C++26][expr.unary.op]9777 // If the operand points to an object or function, the result9778 // denotes that object or function; otherwise, the behavior is undefined.9779 // Because &(*(type*)0) is a common pattern, we do not fail the evaluation9780 // immediately.9781 if (!Success || !E->getType().getNonReferenceType()->isObjectType())9782 return Success;9783 return bool(findCompleteObject(Info, E, AK_Dereference, Result,9784 E->getType())) ||9785 Info.noteUndefinedBehavior();9786}9787 9788bool LValueExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {9789 if (!Visit(E->getSubExpr()))9790 return false;9791 // __real is a no-op on scalar lvalues.9792 if (E->getSubExpr()->getType()->isAnyComplexType())9793 HandleLValueComplexElement(Info, E, Result, E->getType(), false);9794 return true;9795}9796 9797bool LValueExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {9798 assert(E->getSubExpr()->getType()->isAnyComplexType() &&9799 "lvalue __imag__ on scalar?");9800 if (!Visit(E->getSubExpr()))9801 return false;9802 HandleLValueComplexElement(Info, E, Result, E->getType(), true);9803 return true;9804}9805 9806bool LValueExprEvaluator::VisitUnaryPreIncDec(const UnaryOperator *UO) {9807 if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure())9808 return Error(UO);9809 9810 if (!this->Visit(UO->getSubExpr()))9811 return false;9812 9813 return handleIncDec(9814 this->Info, UO, Result, UO->getSubExpr()->getType(),9815 UO->isIncrementOp(), nullptr);9816}9817 9818bool LValueExprEvaluator::VisitCompoundAssignOperator(9819 const CompoundAssignOperator *CAO) {9820 if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure())9821 return Error(CAO);9822 9823 bool Success = true;9824 9825 // C++17 onwards require that we evaluate the RHS first.9826 APValue RHS;9827 if (!Evaluate(RHS, this->Info, CAO->getRHS())) {9828 if (!Info.noteFailure())9829 return false;9830 Success = false;9831 }9832 9833 // The overall lvalue result is the result of evaluating the LHS.9834 if (!this->Visit(CAO->getLHS()) || !Success)9835 return false;9836 9837 return handleCompoundAssignment(9838 this->Info, CAO,9839 Result, CAO->getLHS()->getType(), CAO->getComputationLHSType(),9840 CAO->getOpForCompoundAssignment(CAO->getOpcode()), RHS);9841}9842 9843bool LValueExprEvaluator::VisitBinAssign(const BinaryOperator *E) {9844 if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure())9845 return Error(E);9846 9847 bool Success = true;9848 9849 // C++17 onwards require that we evaluate the RHS first.9850 APValue NewVal;9851 if (!Evaluate(NewVal, this->Info, E->getRHS())) {9852 if (!Info.noteFailure())9853 return false;9854 Success = false;9855 }9856 9857 if (!this->Visit(E->getLHS()) || !Success)9858 return false;9859 9860 if (Info.getLangOpts().CPlusPlus20 &&9861 !MaybeHandleUnionActiveMemberChange(Info, E->getLHS(), Result))9862 return false;9863 9864 return handleAssignment(this->Info, E, Result, E->getLHS()->getType(),9865 NewVal);9866}9867 9868//===----------------------------------------------------------------------===//9869// Pointer Evaluation9870//===----------------------------------------------------------------------===//9871 9872/// Convenience function. LVal's base must be a call to an alloc_size9873/// function.9874static bool getBytesReturnedByAllocSizeCall(const ASTContext &Ctx,9875 const LValue &LVal,9876 llvm::APInt &Result) {9877 assert(isBaseAnAllocSizeCall(LVal.getLValueBase()) &&9878 "Can't get the size of a non alloc_size function");9879 const auto *Base = LVal.getLValueBase().get<const Expr *>();9880 const CallExpr *CE = tryUnwrapAllocSizeCall(Base);9881 std::optional<llvm::APInt> Size =9882 CE->evaluateBytesReturnedByAllocSizeCall(Ctx);9883 if (!Size)9884 return false;9885 9886 Result = std::move(*Size);9887 return true;9888}9889 9890/// Attempts to evaluate the given LValueBase as the result of a call to9891/// a function with the alloc_size attribute. If it was possible to do so, this9892/// function will return true, make Result's Base point to said function call,9893/// and mark Result's Base as invalid.9894static bool evaluateLValueAsAllocSize(EvalInfo &Info, APValue::LValueBase Base,9895 LValue &Result) {9896 if (Base.isNull())9897 return false;9898 9899 // Because we do no form of static analysis, we only support const variables.9900 //9901 // Additionally, we can't support parameters, nor can we support static9902 // variables (in the latter case, use-before-assign isn't UB; in the former,9903 // we have no clue what they'll be assigned to).9904 const auto *VD =9905 dyn_cast_or_null<VarDecl>(Base.dyn_cast<const ValueDecl *>());9906 if (!VD || !VD->isLocalVarDecl() || !VD->getType().isConstQualified())9907 return false;9908 9909 const Expr *Init = VD->getAnyInitializer();9910 if (!Init || Init->getType().isNull())9911 return false;9912 9913 const Expr *E = Init->IgnoreParens();9914 if (!tryUnwrapAllocSizeCall(E))9915 return false;9916 9917 // Store E instead of E unwrapped so that the type of the LValue's base is9918 // what the user wanted.9919 Result.setInvalid(E);9920 9921 QualType Pointee = E->getType()->castAs<PointerType>()->getPointeeType();9922 Result.addUnsizedArray(Info, E, Pointee);9923 return true;9924}9925 9926namespace {9927class PointerExprEvaluator9928 : public ExprEvaluatorBase<PointerExprEvaluator> {9929 LValue &Result;9930 bool InvalidBaseOK;9931 9932 bool Success(const Expr *E) {9933 Result.set(E);9934 return true;9935 }9936 9937 bool evaluateLValue(const Expr *E, LValue &Result) {9938 return EvaluateLValue(E, Result, Info, InvalidBaseOK);9939 }9940 9941 bool evaluatePointer(const Expr *E, LValue &Result) {9942 return EvaluatePointer(E, Result, Info, InvalidBaseOK);9943 }9944 9945 bool visitNonBuiltinCallExpr(const CallExpr *E);9946public:9947 9948 PointerExprEvaluator(EvalInfo &info, LValue &Result, bool InvalidBaseOK)9949 : ExprEvaluatorBaseTy(info), Result(Result),9950 InvalidBaseOK(InvalidBaseOK) {}9951 9952 bool Success(const APValue &V, const Expr *E) {9953 Result.setFrom(Info.Ctx, V);9954 return true;9955 }9956 bool ZeroInitialization(const Expr *E) {9957 Result.setNull(Info.Ctx, E->getType());9958 return true;9959 }9960 9961 bool VisitBinaryOperator(const BinaryOperator *E);9962 bool VisitCastExpr(const CastExpr* E);9963 bool VisitUnaryAddrOf(const UnaryOperator *E);9964 bool VisitObjCStringLiteral(const ObjCStringLiteral *E)9965 { return Success(E); }9966 bool VisitObjCBoxedExpr(const ObjCBoxedExpr *E) {9967 if (E->isExpressibleAsConstantInitializer())9968 return Success(E);9969 if (Info.noteFailure())9970 EvaluateIgnoredValue(Info, E->getSubExpr());9971 return Error(E);9972 }9973 bool VisitAddrLabelExpr(const AddrLabelExpr *E)9974 { return Success(E); }9975 bool VisitCallExpr(const CallExpr *E);9976 bool VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinOp);9977 bool VisitBlockExpr(const BlockExpr *E) {9978 if (!E->getBlockDecl()->hasCaptures())9979 return Success(E);9980 return Error(E);9981 }9982 bool VisitCXXThisExpr(const CXXThisExpr *E) {9983 auto DiagnoseInvalidUseOfThis = [&] {9984 if (Info.getLangOpts().CPlusPlus11)9985 Info.FFDiag(E, diag::note_constexpr_this) << E->isImplicit();9986 else9987 Info.FFDiag(E);9988 };9989 9990 // Can't look at 'this' when checking a potential constant expression.9991 if (Info.checkingPotentialConstantExpression())9992 return false;9993 9994 bool IsExplicitLambda =9995 isLambdaCallWithExplicitObjectParameter(Info.CurrentCall->Callee);9996 if (!IsExplicitLambda) {9997 if (!Info.CurrentCall->This) {9998 DiagnoseInvalidUseOfThis();9999 return false;10000 }10001 10002 Result = *Info.CurrentCall->This;10003 }10004 10005 if (isLambdaCallOperator(Info.CurrentCall->Callee)) {10006 // Ensure we actually have captured 'this'. If something was wrong with10007 // 'this' capture, the error would have been previously reported.10008 // Otherwise we can be inside of a default initialization of an object10009 // declared by lambda's body, so no need to return false.10010 if (!Info.CurrentCall->LambdaThisCaptureField) {10011 if (IsExplicitLambda && !Info.CurrentCall->This) {10012 DiagnoseInvalidUseOfThis();10013 return false;10014 }10015 10016 return true;10017 }10018 10019 const auto *MD = cast<CXXMethodDecl>(Info.CurrentCall->Callee);10020 return HandleLambdaCapture(10021 Info, E, Result, MD, Info.CurrentCall->LambdaThisCaptureField,10022 Info.CurrentCall->LambdaThisCaptureField->getType()->isPointerType());10023 }10024 return true;10025 }10026 10027 bool VisitCXXNewExpr(const CXXNewExpr *E);10028 10029 bool VisitSourceLocExpr(const SourceLocExpr *E) {10030 assert(!E->isIntType() && "SourceLocExpr isn't a pointer type?");10031 APValue LValResult = E->EvaluateInContext(10032 Info.Ctx, Info.CurrentCall->CurSourceLocExprScope.getDefaultExpr());10033 Result.setFrom(Info.Ctx, LValResult);10034 return true;10035 }10036 10037 bool VisitEmbedExpr(const EmbedExpr *E) {10038 llvm::report_fatal_error("Not yet implemented for ExprConstant.cpp");10039 return true;10040 }10041 10042 bool VisitSYCLUniqueStableNameExpr(const SYCLUniqueStableNameExpr *E) {10043 std::string ResultStr = E->ComputeName(Info.Ctx);10044 10045 QualType CharTy = Info.Ctx.CharTy.withConst();10046 APInt Size(Info.Ctx.getTypeSize(Info.Ctx.getSizeType()),10047 ResultStr.size() + 1);10048 QualType ArrayTy = Info.Ctx.getConstantArrayType(10049 CharTy, Size, nullptr, ArraySizeModifier::Normal, 0);10050 10051 StringLiteral *SL =10052 StringLiteral::Create(Info.Ctx, ResultStr, StringLiteralKind::Ordinary,10053 /*Pascal*/ false, ArrayTy, E->getLocation());10054 10055 evaluateLValue(SL, Result);10056 Result.addArray(Info, E, cast<ConstantArrayType>(ArrayTy));10057 return true;10058 }10059 10060 // FIXME: Missing: @protocol, @selector10061};10062} // end anonymous namespace10063 10064static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info,10065 bool InvalidBaseOK) {10066 assert(!E->isValueDependent());10067 assert(E->isPRValue() && E->getType()->hasPointerRepresentation());10068 return PointerExprEvaluator(Info, Result, InvalidBaseOK).Visit(E);10069}10070 10071bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {10072 if (E->getOpcode() != BO_Add &&10073 E->getOpcode() != BO_Sub)10074 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);10075 10076 const Expr *PExp = E->getLHS();10077 const Expr *IExp = E->getRHS();10078 if (IExp->getType()->isPointerType())10079 std::swap(PExp, IExp);10080 10081 bool EvalPtrOK = evaluatePointer(PExp, Result);10082 if (!EvalPtrOK && !Info.noteFailure())10083 return false;10084 10085 llvm::APSInt Offset;10086 if (!EvaluateInteger(IExp, Offset, Info) || !EvalPtrOK)10087 return false;10088 10089 if (E->getOpcode() == BO_Sub)10090 negateAsSigned(Offset);10091 10092 QualType Pointee = PExp->getType()->castAs<PointerType>()->getPointeeType();10093 return HandleLValueArrayAdjustment(Info, E, Result, Pointee, Offset);10094}10095 10096bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {10097 return evaluateLValue(E->getSubExpr(), Result);10098}10099 10100// Is the provided decl 'std::source_location::current'?10101static bool IsDeclSourceLocationCurrent(const FunctionDecl *FD) {10102 if (!FD)10103 return false;10104 const IdentifierInfo *FnII = FD->getIdentifier();10105 if (!FnII || !FnII->isStr("current"))10106 return false;10107 10108 const auto *RD = dyn_cast<RecordDecl>(FD->getParent());10109 if (!RD)10110 return false;10111 10112 const IdentifierInfo *ClassII = RD->getIdentifier();10113 return RD->isInStdNamespace() && ClassII && ClassII->isStr("source_location");10114}10115 10116bool PointerExprEvaluator::VisitCastExpr(const CastExpr *E) {10117 const Expr *SubExpr = E->getSubExpr();10118 10119 switch (E->getCastKind()) {10120 default:10121 break;10122 case CK_BitCast:10123 case CK_CPointerToObjCPointerCast:10124 case CK_BlockPointerToObjCPointerCast:10125 case CK_AnyPointerToBlockPointerCast:10126 case CK_AddressSpaceConversion:10127 if (!Visit(SubExpr))10128 return false;10129 if (E->getType()->isFunctionPointerType() ||10130 SubExpr->getType()->isFunctionPointerType()) {10131 // Casting between two function pointer types, or between a function10132 // pointer and an object pointer, is always a reinterpret_cast.10133 CCEDiag(E, diag::note_constexpr_invalid_cast)10134 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret10135 << Info.Ctx.getLangOpts().CPlusPlus;10136 Result.Designator.setInvalid();10137 } else if (!E->getType()->isVoidPointerType()) {10138 // Bitcasts to cv void* are static_casts, not reinterpret_casts, so are10139 // permitted in constant expressions in C++11. Bitcasts from cv void* are10140 // also static_casts, but we disallow them as a resolution to DR1312.10141 //10142 // In some circumstances, we permit casting from void* to cv1 T*, when the10143 // actual pointee object is actually a cv2 T.10144 bool HasValidResult = !Result.InvalidBase && !Result.Designator.Invalid &&10145 !Result.IsNullPtr;10146 bool VoidPtrCastMaybeOK =10147 Result.IsNullPtr ||10148 (HasValidResult &&10149 Info.Ctx.hasSimilarType(Result.Designator.getType(Info.Ctx),10150 E->getType()->getPointeeType()));10151 // 1. We'll allow it in std::allocator::allocate, and anything which that10152 // calls.10153 // 2. HACK 2022-03-28: Work around an issue with libstdc++'s10154 // <source_location> header. Fixed in GCC 12 and later (2022-04-??).10155 // We'll allow it in the body of std::source_location::current. GCC's10156 // implementation had a parameter of type `void*`, and casts from10157 // that back to `const __impl*` in its body.10158 if (VoidPtrCastMaybeOK &&10159 (Info.getStdAllocatorCaller("allocate") ||10160 IsDeclSourceLocationCurrent(Info.CurrentCall->Callee) ||10161 Info.getLangOpts().CPlusPlus26)) {10162 // Permitted.10163 } else {10164 if (SubExpr->getType()->isVoidPointerType() &&10165 Info.getLangOpts().CPlusPlus) {10166 if (HasValidResult)10167 CCEDiag(E, diag::note_constexpr_invalid_void_star_cast)10168 << SubExpr->getType() << Info.getLangOpts().CPlusPlus2610169 << Result.Designator.getType(Info.Ctx).getCanonicalType()10170 << E->getType()->getPointeeType();10171 else10172 CCEDiag(E, diag::note_constexpr_invalid_cast)10173 << diag::ConstexprInvalidCastKind::CastFrom10174 << SubExpr->getType();10175 } else10176 CCEDiag(E, diag::note_constexpr_invalid_cast)10177 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret10178 << Info.Ctx.getLangOpts().CPlusPlus;10179 Result.Designator.setInvalid();10180 }10181 }10182 if (E->getCastKind() == CK_AddressSpaceConversion && Result.IsNullPtr)10183 ZeroInitialization(E);10184 return true;10185 10186 case CK_DerivedToBase:10187 case CK_UncheckedDerivedToBase:10188 if (!evaluatePointer(E->getSubExpr(), Result))10189 return false;10190 if (!Result.Base && Result.Offset.isZero())10191 return true;10192 10193 // Now figure out the necessary offset to add to the base LV to get from10194 // the derived class to the base class.10195 return HandleLValueBasePath(Info, E, E->getSubExpr()->getType()->10196 castAs<PointerType>()->getPointeeType(),10197 Result);10198 10199 case CK_BaseToDerived:10200 if (!Visit(E->getSubExpr()))10201 return false;10202 if (!Result.Base && Result.Offset.isZero())10203 return true;10204 return HandleBaseToDerivedCast(Info, E, Result);10205 10206 case CK_Dynamic:10207 if (!Visit(E->getSubExpr()))10208 return false;10209 return HandleDynamicCast(Info, cast<ExplicitCastExpr>(E), Result);10210 10211 case CK_NullToPointer:10212 VisitIgnoredValue(E->getSubExpr());10213 return ZeroInitialization(E);10214 10215 case CK_IntegralToPointer: {10216 CCEDiag(E, diag::note_constexpr_invalid_cast)10217 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret10218 << Info.Ctx.getLangOpts().CPlusPlus;10219 10220 APValue Value;10221 if (!EvaluateIntegerOrLValue(SubExpr, Value, Info))10222 break;10223 10224 if (Value.isInt()) {10225 unsigned Size = Info.Ctx.getTypeSize(E->getType());10226 uint64_t N = Value.getInt().extOrTrunc(Size).getZExtValue();10227 if (N == Info.Ctx.getTargetNullPointerValue(E->getType())) {10228 Result.setNull(Info.Ctx, E->getType());10229 } else {10230 Result.Base = (Expr *)nullptr;10231 Result.InvalidBase = false;10232 Result.Offset = CharUnits::fromQuantity(N);10233 Result.Designator.setInvalid();10234 Result.IsNullPtr = false;10235 }10236 return true;10237 } else {10238 // In rare instances, the value isn't an lvalue.10239 // For example, when the value is the difference between the addresses of10240 // two labels. We reject that as a constant expression because we can't10241 // compute a valid offset to convert into a pointer.10242 if (!Value.isLValue())10243 return false;10244 10245 // Cast is of an lvalue, no need to change value.10246 Result.setFrom(Info.Ctx, Value);10247 return true;10248 }10249 }10250 10251 case CK_ArrayToPointerDecay: {10252 if (SubExpr->isGLValue()) {10253 if (!evaluateLValue(SubExpr, Result))10254 return false;10255 } else {10256 APValue &Value = Info.CurrentCall->createTemporary(10257 SubExpr, SubExpr->getType(), ScopeKind::FullExpression, Result);10258 if (!EvaluateInPlace(Value, Info, Result, SubExpr))10259 return false;10260 }10261 // The result is a pointer to the first element of the array.10262 auto *AT = Info.Ctx.getAsArrayType(SubExpr->getType());10263 if (auto *CAT = dyn_cast<ConstantArrayType>(AT))10264 Result.addArray(Info, E, CAT);10265 else10266 Result.addUnsizedArray(Info, E, AT->getElementType());10267 return true;10268 }10269 10270 case CK_FunctionToPointerDecay:10271 return evaluateLValue(SubExpr, Result);10272 10273 case CK_LValueToRValue: {10274 LValue LVal;10275 if (!evaluateLValue(E->getSubExpr(), LVal))10276 return false;10277 10278 APValue RVal;10279 // Note, we use the subexpression's type in order to retain cv-qualifiers.10280 if (!handleLValueToRValueConversion(Info, E, E->getSubExpr()->getType(),10281 LVal, RVal))10282 return InvalidBaseOK &&10283 evaluateLValueAsAllocSize(Info, LVal.Base, Result);10284 return Success(RVal, E);10285 }10286 }10287 10288 return ExprEvaluatorBaseTy::VisitCastExpr(E);10289}10290 10291static CharUnits GetAlignOfType(const ASTContext &Ctx, QualType T,10292 UnaryExprOrTypeTrait ExprKind) {10293 // C++ [expr.alignof]p3:10294 // When alignof is applied to a reference type, the result is the10295 // alignment of the referenced type.10296 T = T.getNonReferenceType();10297 10298 if (T.getQualifiers().hasUnaligned())10299 return CharUnits::One();10300 10301 const bool AlignOfReturnsPreferred =10302 Ctx.getLangOpts().getClangABICompat() <= LangOptions::ClangABI::Ver7;10303 10304 // __alignof is defined to return the preferred alignment.10305 // Before 8, clang returned the preferred alignment for alignof and _Alignof10306 // as well.10307 if (ExprKind == UETT_PreferredAlignOf || AlignOfReturnsPreferred)10308 return Ctx.toCharUnitsFromBits(Ctx.getPreferredTypeAlign(T.getTypePtr()));10309 // alignof and _Alignof are defined to return the ABI alignment.10310 else if (ExprKind == UETT_AlignOf)10311 return Ctx.getTypeAlignInChars(T.getTypePtr());10312 else10313 llvm_unreachable("GetAlignOfType on a non-alignment ExprKind");10314}10315 10316CharUnits GetAlignOfExpr(const ASTContext &Ctx, const Expr *E,10317 UnaryExprOrTypeTrait ExprKind) {10318 E = E->IgnoreParens();10319 10320 // The kinds of expressions that we have special-case logic here for10321 // should be kept up to date with the special checks for those10322 // expressions in Sema.10323 10324 // alignof decl is always accepted, even if it doesn't make sense: we default10325 // to 1 in those cases.10326 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))10327 return Ctx.getDeclAlign(DRE->getDecl(),10328 /*RefAsPointee*/ true);10329 10330 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))10331 return Ctx.getDeclAlign(ME->getMemberDecl(),10332 /*RefAsPointee*/ true);10333 10334 return GetAlignOfType(Ctx, E->getType(), ExprKind);10335}10336 10337static CharUnits getBaseAlignment(EvalInfo &Info, const LValue &Value) {10338 if (const auto *VD = Value.Base.dyn_cast<const ValueDecl *>())10339 return Info.Ctx.getDeclAlign(VD);10340 if (const auto *E = Value.Base.dyn_cast<const Expr *>())10341 return GetAlignOfExpr(Info.Ctx, E, UETT_AlignOf);10342 return GetAlignOfType(Info.Ctx, Value.Base.getTypeInfoType(), UETT_AlignOf);10343}10344 10345/// Evaluate the value of the alignment argument to __builtin_align_{up,down},10346/// __builtin_is_aligned and __builtin_assume_aligned.10347static bool getAlignmentArgument(const Expr *E, QualType ForType,10348 EvalInfo &Info, APSInt &Alignment) {10349 if (!EvaluateInteger(E, Alignment, Info))10350 return false;10351 if (Alignment < 0 || !Alignment.isPowerOf2()) {10352 Info.FFDiag(E, diag::note_constexpr_invalid_alignment) << Alignment;10353 return false;10354 }10355 unsigned SrcWidth = Info.Ctx.getIntWidth(ForType);10356 APSInt MaxValue(APInt::getOneBitSet(SrcWidth, SrcWidth - 1));10357 if (APSInt::compareValues(Alignment, MaxValue) > 0) {10358 Info.FFDiag(E, diag::note_constexpr_alignment_too_big)10359 << MaxValue << ForType << Alignment;10360 return false;10361 }10362 // Ensure both alignment and source value have the same bit width so that we10363 // don't assert when computing the resulting value.10364 APSInt ExtAlignment =10365 APSInt(Alignment.zextOrTrunc(SrcWidth), /*isUnsigned=*/true);10366 assert(APSInt::compareValues(Alignment, ExtAlignment) == 0 &&10367 "Alignment should not be changed by ext/trunc");10368 Alignment = ExtAlignment;10369 assert(Alignment.getBitWidth() == SrcWidth);10370 return true;10371}10372 10373// To be clear: this happily visits unsupported builtins. Better name welcomed.10374bool PointerExprEvaluator::visitNonBuiltinCallExpr(const CallExpr *E) {10375 if (ExprEvaluatorBaseTy::VisitCallExpr(E))10376 return true;10377 10378 if (!(InvalidBaseOK && E->getCalleeAllocSizeAttr()))10379 return false;10380 10381 Result.setInvalid(E);10382 QualType PointeeTy = E->getType()->castAs<PointerType>()->getPointeeType();10383 Result.addUnsizedArray(Info, E, PointeeTy);10384 return true;10385}10386 10387bool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) {10388 if (!IsConstantEvaluatedBuiltinCall(E))10389 return visitNonBuiltinCallExpr(E);10390 return VisitBuiltinCallExpr(E, E->getBuiltinCallee());10391}10392 10393// Determine if T is a character type for which we guarantee that10394// sizeof(T) == 1.10395static bool isOneByteCharacterType(QualType T) {10396 return T->isCharType() || T->isChar8Type();10397}10398 10399bool PointerExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,10400 unsigned BuiltinOp) {10401 if (IsOpaqueConstantCall(E))10402 return Success(E);10403 10404 switch (BuiltinOp) {10405 case Builtin::BIaddressof:10406 case Builtin::BI__addressof:10407 case Builtin::BI__builtin_addressof:10408 return evaluateLValue(E->getArg(0), Result);10409 case Builtin::BI__builtin_assume_aligned: {10410 // We need to be very careful here because: if the pointer does not have the10411 // asserted alignment, then the behavior is undefined, and undefined10412 // behavior is non-constant.10413 if (!evaluatePointer(E->getArg(0), Result))10414 return false;10415 10416 LValue OffsetResult(Result);10417 APSInt Alignment;10418 if (!getAlignmentArgument(E->getArg(1), E->getArg(0)->getType(), Info,10419 Alignment))10420 return false;10421 CharUnits Align = CharUnits::fromQuantity(Alignment.getZExtValue());10422 10423 if (E->getNumArgs() > 2) {10424 APSInt Offset;10425 if (!EvaluateInteger(E->getArg(2), Offset, Info))10426 return false;10427 10428 int64_t AdditionalOffset = -Offset.getZExtValue();10429 OffsetResult.Offset += CharUnits::fromQuantity(AdditionalOffset);10430 }10431 10432 // If there is a base object, then it must have the correct alignment.10433 if (OffsetResult.Base) {10434 CharUnits BaseAlignment = getBaseAlignment(Info, OffsetResult);10435 10436 if (BaseAlignment < Align) {10437 Result.Designator.setInvalid();10438 CCEDiag(E->getArg(0), diag::note_constexpr_baa_insufficient_alignment)10439 << 0 << BaseAlignment.getQuantity() << Align.getQuantity();10440 return false;10441 }10442 }10443 10444 // The offset must also have the correct alignment.10445 if (OffsetResult.Offset.alignTo(Align) != OffsetResult.Offset) {10446 Result.Designator.setInvalid();10447 10448 (OffsetResult.Base10449 ? CCEDiag(E->getArg(0),10450 diag::note_constexpr_baa_insufficient_alignment)10451 << 110452 : CCEDiag(E->getArg(0),10453 diag::note_constexpr_baa_value_insufficient_alignment))10454 << OffsetResult.Offset.getQuantity() << Align.getQuantity();10455 return false;10456 }10457 10458 return true;10459 }10460 case Builtin::BI__builtin_align_up:10461 case Builtin::BI__builtin_align_down: {10462 if (!evaluatePointer(E->getArg(0), Result))10463 return false;10464 APSInt Alignment;10465 if (!getAlignmentArgument(E->getArg(1), E->getArg(0)->getType(), Info,10466 Alignment))10467 return false;10468 CharUnits BaseAlignment = getBaseAlignment(Info, Result);10469 CharUnits PtrAlign = BaseAlignment.alignmentAtOffset(Result.Offset);10470 // For align_up/align_down, we can return the same value if the alignment10471 // is known to be greater or equal to the requested value.10472 if (PtrAlign.getQuantity() >= Alignment)10473 return true;10474 10475 // The alignment could be greater than the minimum at run-time, so we cannot10476 // infer much about the resulting pointer value. One case is possible:10477 // For `_Alignas(32) char buf[N]; __builtin_align_down(&buf[idx], 32)` we10478 // can infer the correct index if the requested alignment is smaller than10479 // the base alignment so we can perform the computation on the offset.10480 if (BaseAlignment.getQuantity() >= Alignment) {10481 assert(Alignment.getBitWidth() <= 64 &&10482 "Cannot handle > 64-bit address-space");10483 uint64_t Alignment64 = Alignment.getZExtValue();10484 CharUnits NewOffset = CharUnits::fromQuantity(10485 BuiltinOp == Builtin::BI__builtin_align_down10486 ? llvm::alignDown(Result.Offset.getQuantity(), Alignment64)10487 : llvm::alignTo(Result.Offset.getQuantity(), Alignment64));10488 Result.adjustOffset(NewOffset - Result.Offset);10489 // TODO: diagnose out-of-bounds values/only allow for arrays?10490 return true;10491 }10492 // Otherwise, we cannot constant-evaluate the result.10493 Info.FFDiag(E->getArg(0), diag::note_constexpr_alignment_adjust)10494 << Alignment;10495 return false;10496 }10497 case Builtin::BI__builtin_operator_new:10498 return HandleOperatorNewCall(Info, E, Result);10499 case Builtin::BI__builtin_launder:10500 return evaluatePointer(E->getArg(0), Result);10501 case Builtin::BIstrchr:10502 case Builtin::BIwcschr:10503 case Builtin::BImemchr:10504 case Builtin::BIwmemchr:10505 if (Info.getLangOpts().CPlusPlus11)10506 Info.CCEDiag(E, diag::note_constexpr_invalid_function)10507 << /*isConstexpr*/ 0 << /*isConstructor*/ 010508 << Info.Ctx.BuiltinInfo.getQuotedName(BuiltinOp);10509 else10510 Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr);10511 [[fallthrough]];10512 case Builtin::BI__builtin_strchr:10513 case Builtin::BI__builtin_wcschr:10514 case Builtin::BI__builtin_memchr:10515 case Builtin::BI__builtin_char_memchr:10516 case Builtin::BI__builtin_wmemchr: {10517 if (!Visit(E->getArg(0)))10518 return false;10519 APSInt Desired;10520 if (!EvaluateInteger(E->getArg(1), Desired, Info))10521 return false;10522 uint64_t MaxLength = uint64_t(-1);10523 if (BuiltinOp != Builtin::BIstrchr &&10524 BuiltinOp != Builtin::BIwcschr &&10525 BuiltinOp != Builtin::BI__builtin_strchr &&10526 BuiltinOp != Builtin::BI__builtin_wcschr) {10527 APSInt N;10528 if (!EvaluateInteger(E->getArg(2), N, Info))10529 return false;10530 MaxLength = N.getZExtValue();10531 }10532 // We cannot find the value if there are no candidates to match against.10533 if (MaxLength == 0u)10534 return ZeroInitialization(E);10535 if (!Result.checkNullPointerForFoldAccess(Info, E, AK_Read) ||10536 Result.Designator.Invalid)10537 return false;10538 QualType CharTy = Result.Designator.getType(Info.Ctx);10539 bool IsRawByte = BuiltinOp == Builtin::BImemchr ||10540 BuiltinOp == Builtin::BI__builtin_memchr;10541 assert(IsRawByte ||10542 Info.Ctx.hasSameUnqualifiedType(10543 CharTy, E->getArg(0)->getType()->getPointeeType()));10544 // Pointers to const void may point to objects of incomplete type.10545 if (IsRawByte && CharTy->isIncompleteType()) {10546 Info.FFDiag(E, diag::note_constexpr_ltor_incomplete_type) << CharTy;10547 return false;10548 }10549 // Give up on byte-oriented matching against multibyte elements.10550 // FIXME: We can compare the bytes in the correct order.10551 if (IsRawByte && !isOneByteCharacterType(CharTy)) {10552 Info.FFDiag(E, diag::note_constexpr_memchr_unsupported)10553 << Info.Ctx.BuiltinInfo.getQuotedName(BuiltinOp) << CharTy;10554 return false;10555 }10556 // Figure out what value we're actually looking for (after converting to10557 // the corresponding unsigned type if necessary).10558 uint64_t DesiredVal;10559 bool StopAtNull = false;10560 switch (BuiltinOp) {10561 case Builtin::BIstrchr:10562 case Builtin::BI__builtin_strchr:10563 // strchr compares directly to the passed integer, and therefore10564 // always fails if given an int that is not a char.10565 if (!APSInt::isSameValue(HandleIntToIntCast(Info, E, CharTy,10566 E->getArg(1)->getType(),10567 Desired),10568 Desired))10569 return ZeroInitialization(E);10570 StopAtNull = true;10571 [[fallthrough]];10572 case Builtin::BImemchr:10573 case Builtin::BI__builtin_memchr:10574 case Builtin::BI__builtin_char_memchr:10575 // memchr compares by converting both sides to unsigned char. That's also10576 // correct for strchr if we get this far (to cope with plain char being10577 // unsigned in the strchr case).10578 DesiredVal = Desired.trunc(Info.Ctx.getCharWidth()).getZExtValue();10579 break;10580 10581 case Builtin::BIwcschr:10582 case Builtin::BI__builtin_wcschr:10583 StopAtNull = true;10584 [[fallthrough]];10585 case Builtin::BIwmemchr:10586 case Builtin::BI__builtin_wmemchr:10587 // wcschr and wmemchr are given a wchar_t to look for. Just use it.10588 DesiredVal = Desired.getZExtValue();10589 break;10590 }10591 10592 for (; MaxLength; --MaxLength) {10593 APValue Char;10594 if (!handleLValueToRValueConversion(Info, E, CharTy, Result, Char) ||10595 !Char.isInt())10596 return false;10597 if (Char.getInt().getZExtValue() == DesiredVal)10598 return true;10599 if (StopAtNull && !Char.getInt())10600 break;10601 if (!HandleLValueArrayAdjustment(Info, E, Result, CharTy, 1))10602 return false;10603 }10604 // Not found: return nullptr.10605 return ZeroInitialization(E);10606 }10607 10608 case Builtin::BImemcpy:10609 case Builtin::BImemmove:10610 case Builtin::BIwmemcpy:10611 case Builtin::BIwmemmove:10612 if (Info.getLangOpts().CPlusPlus11)10613 Info.CCEDiag(E, diag::note_constexpr_invalid_function)10614 << /*isConstexpr*/ 0 << /*isConstructor*/ 010615 << Info.Ctx.BuiltinInfo.getQuotedName(BuiltinOp);10616 else10617 Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr);10618 [[fallthrough]];10619 case Builtin::BI__builtin_memcpy:10620 case Builtin::BI__builtin_memmove:10621 case Builtin::BI__builtin_wmemcpy:10622 case Builtin::BI__builtin_wmemmove: {10623 bool WChar = BuiltinOp == Builtin::BIwmemcpy ||10624 BuiltinOp == Builtin::BIwmemmove ||10625 BuiltinOp == Builtin::BI__builtin_wmemcpy ||10626 BuiltinOp == Builtin::BI__builtin_wmemmove;10627 bool Move = BuiltinOp == Builtin::BImemmove ||10628 BuiltinOp == Builtin::BIwmemmove ||10629 BuiltinOp == Builtin::BI__builtin_memmove ||10630 BuiltinOp == Builtin::BI__builtin_wmemmove;10631 10632 // The result of mem* is the first argument.10633 if (!Visit(E->getArg(0)))10634 return false;10635 LValue Dest = Result;10636 10637 LValue Src;10638 if (!EvaluatePointer(E->getArg(1), Src, Info))10639 return false;10640 10641 APSInt N;10642 if (!EvaluateInteger(E->getArg(2), N, Info))10643 return false;10644 assert(!N.isSigned() && "memcpy and friends take an unsigned size");10645 10646 // If the size is zero, we treat this as always being a valid no-op.10647 // (Even if one of the src and dest pointers is null.)10648 if (!N)10649 return true;10650 10651 // Otherwise, if either of the operands is null, we can't proceed. Don't10652 // try to determine the type of the copied objects, because there aren't10653 // any.10654 if (!Src.Base || !Dest.Base) {10655 APValue Val;10656 (!Src.Base ? Src : Dest).moveInto(Val);10657 Info.FFDiag(E, diag::note_constexpr_memcpy_null)10658 << Move << WChar << !!Src.Base10659 << Val.getAsString(Info.Ctx, E->getArg(0)->getType());10660 return false;10661 }10662 if (Src.Designator.Invalid || Dest.Designator.Invalid)10663 return false;10664 10665 // We require that Src and Dest are both pointers to arrays of10666 // trivially-copyable type. (For the wide version, the designator will be10667 // invalid if the designated object is not a wchar_t.)10668 QualType T = Dest.Designator.getType(Info.Ctx);10669 QualType SrcT = Src.Designator.getType(Info.Ctx);10670 if (!Info.Ctx.hasSameUnqualifiedType(T, SrcT)) {10671 // FIXME: Consider using our bit_cast implementation to support this.10672 Info.FFDiag(E, diag::note_constexpr_memcpy_type_pun) << Move << SrcT << T;10673 return false;10674 }10675 if (T->isIncompleteType()) {10676 Info.FFDiag(E, diag::note_constexpr_memcpy_incomplete_type) << Move << T;10677 return false;10678 }10679 if (!T.isTriviallyCopyableType(Info.Ctx)) {10680 Info.FFDiag(E, diag::note_constexpr_memcpy_nontrivial) << Move << T;10681 return false;10682 }10683 10684 // Figure out how many T's we're copying.10685 uint64_t TSize = Info.Ctx.getTypeSizeInChars(T).getQuantity();10686 if (TSize == 0)10687 return false;10688 if (!WChar) {10689 uint64_t Remainder;10690 llvm::APInt OrigN = N;10691 llvm::APInt::udivrem(OrigN, TSize, N, Remainder);10692 if (Remainder) {10693 Info.FFDiag(E, diag::note_constexpr_memcpy_unsupported)10694 << Move << WChar << 0 << T << toString(OrigN, 10, /*Signed*/false)10695 << (unsigned)TSize;10696 return false;10697 }10698 }10699 10700 // Check that the copying will remain within the arrays, just so that we10701 // can give a more meaningful diagnostic. This implicitly also checks that10702 // N fits into 64 bits.10703 uint64_t RemainingSrcSize = Src.Designator.validIndexAdjustments().second;10704 uint64_t RemainingDestSize = Dest.Designator.validIndexAdjustments().second;10705 if (N.ugt(RemainingSrcSize) || N.ugt(RemainingDestSize)) {10706 Info.FFDiag(E, diag::note_constexpr_memcpy_unsupported)10707 << Move << WChar << (N.ugt(RemainingSrcSize) ? 1 : 2) << T10708 << toString(N, 10, /*Signed*/false);10709 return false;10710 }10711 uint64_t NElems = N.getZExtValue();10712 uint64_t NBytes = NElems * TSize;10713 10714 // Check for overlap.10715 int Direction = 1;10716 if (HasSameBase(Src, Dest)) {10717 uint64_t SrcOffset = Src.getLValueOffset().getQuantity();10718 uint64_t DestOffset = Dest.getLValueOffset().getQuantity();10719 if (DestOffset >= SrcOffset && DestOffset - SrcOffset < NBytes) {10720 // Dest is inside the source region.10721 if (!Move) {10722 Info.FFDiag(E, diag::note_constexpr_memcpy_overlap) << WChar;10723 return false;10724 }10725 // For memmove and friends, copy backwards.10726 if (!HandleLValueArrayAdjustment(Info, E, Src, T, NElems - 1) ||10727 !HandleLValueArrayAdjustment(Info, E, Dest, T, NElems - 1))10728 return false;10729 Direction = -1;10730 } else if (!Move && SrcOffset >= DestOffset &&10731 SrcOffset - DestOffset < NBytes) {10732 // Src is inside the destination region for memcpy: invalid.10733 Info.FFDiag(E, diag::note_constexpr_memcpy_overlap) << WChar;10734 return false;10735 }10736 }10737 10738 while (true) {10739 APValue Val;10740 // FIXME: Set WantObjectRepresentation to true if we're copying a10741 // char-like type?10742 if (!handleLValueToRValueConversion(Info, E, T, Src, Val) ||10743 !handleAssignment(Info, E, Dest, T, Val))10744 return false;10745 // Do not iterate past the last element; if we're copying backwards, that10746 // might take us off the start of the array.10747 if (--NElems == 0)10748 return true;10749 if (!HandleLValueArrayAdjustment(Info, E, Src, T, Direction) ||10750 !HandleLValueArrayAdjustment(Info, E, Dest, T, Direction))10751 return false;10752 }10753 }10754 10755 default:10756 return false;10757 }10758}10759 10760static bool EvaluateArrayNewInitList(EvalInfo &Info, LValue &This,10761 APValue &Result, const InitListExpr *ILE,10762 QualType AllocType);10763static bool EvaluateArrayNewConstructExpr(EvalInfo &Info, LValue &This,10764 APValue &Result,10765 const CXXConstructExpr *CCE,10766 QualType AllocType);10767 10768bool PointerExprEvaluator::VisitCXXNewExpr(const CXXNewExpr *E) {10769 if (!Info.getLangOpts().CPlusPlus20)10770 Info.CCEDiag(E, diag::note_constexpr_new);10771 10772 // We cannot speculatively evaluate a delete expression.10773 if (Info.SpeculativeEvaluationDepth)10774 return false;10775 10776 FunctionDecl *OperatorNew = E->getOperatorNew();10777 QualType AllocType = E->getAllocatedType();10778 QualType TargetType = AllocType;10779 10780 bool IsNothrow = false;10781 bool IsPlacement = false;10782 10783 if (E->getNumPlacementArgs() == 1 &&10784 E->getPlacementArg(0)->getType()->isNothrowT()) {10785 // The only new-placement list we support is of the form (std::nothrow).10786 //10787 // FIXME: There is no restriction on this, but it's not clear that any10788 // other form makes any sense. We get here for cases such as:10789 //10790 // new (std::align_val_t{N}) X(int)10791 //10792 // (which should presumably be valid only if N is a multiple of10793 // alignof(int), and in any case can't be deallocated unless N is10794 // alignof(X) and X has new-extended alignment).10795 LValue Nothrow;10796 if (!EvaluateLValue(E->getPlacementArg(0), Nothrow, Info))10797 return false;10798 IsNothrow = true;10799 } else if (OperatorNew->isReservedGlobalPlacementOperator()) {10800 if (Info.CurrentCall->isStdFunction() || Info.getLangOpts().CPlusPlus26 ||10801 (Info.CurrentCall->CanEvalMSConstexpr &&10802 OperatorNew->hasAttr<MSConstexprAttr>())) {10803 if (!EvaluatePointer(E->getPlacementArg(0), Result, Info))10804 return false;10805 if (Result.Designator.Invalid)10806 return false;10807 TargetType = E->getPlacementArg(0)->getType();10808 IsPlacement = true;10809 } else {10810 Info.FFDiag(E, diag::note_constexpr_new_placement)10811 << /*C++26 feature*/ 1 << E->getSourceRange();10812 return false;10813 }10814 } else if (E->getNumPlacementArgs()) {10815 Info.FFDiag(E, diag::note_constexpr_new_placement)10816 << /*Unsupported*/ 0 << E->getSourceRange();10817 return false;10818 } else if (!OperatorNew10819 ->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) {10820 Info.FFDiag(E, diag::note_constexpr_new_non_replaceable)10821 << isa<CXXMethodDecl>(OperatorNew) << OperatorNew;10822 return false;10823 }10824 10825 const Expr *Init = E->getInitializer();10826 const InitListExpr *ResizedArrayILE = nullptr;10827 const CXXConstructExpr *ResizedArrayCCE = nullptr;10828 bool ValueInit = false;10829 10830 if (std::optional<const Expr *> ArraySize = E->getArraySize()) {10831 const Expr *Stripped = *ArraySize;10832 for (; auto *ICE = dyn_cast<ImplicitCastExpr>(Stripped);10833 Stripped = ICE->getSubExpr())10834 if (ICE->getCastKind() != CK_NoOp &&10835 ICE->getCastKind() != CK_IntegralCast)10836 break;10837 10838 llvm::APSInt ArrayBound;10839 if (!EvaluateInteger(Stripped, ArrayBound, Info))10840 return false;10841 10842 // C++ [expr.new]p9:10843 // The expression is erroneous if:10844 // -- [...] its value before converting to size_t [or] applying the10845 // second standard conversion sequence is less than zero10846 if (ArrayBound.isSigned() && ArrayBound.isNegative()) {10847 if (IsNothrow)10848 return ZeroInitialization(E);10849 10850 Info.FFDiag(*ArraySize, diag::note_constexpr_new_negative)10851 << ArrayBound << (*ArraySize)->getSourceRange();10852 return false;10853 }10854 10855 // -- its value is such that the size of the allocated object would10856 // exceed the implementation-defined limit10857 if (!Info.CheckArraySize(ArraySize.value()->getExprLoc(),10858 ConstantArrayType::getNumAddressingBits(10859 Info.Ctx, AllocType, ArrayBound),10860 ArrayBound.getZExtValue(), /*Diag=*/!IsNothrow)) {10861 if (IsNothrow)10862 return ZeroInitialization(E);10863 return false;10864 }10865 10866 // -- the new-initializer is a braced-init-list and the number of10867 // array elements for which initializers are provided [...]10868 // exceeds the number of elements to initialize10869 if (!Init) {10870 // No initialization is performed.10871 } else if (isa<CXXScalarValueInitExpr>(Init) ||10872 isa<ImplicitValueInitExpr>(Init)) {10873 ValueInit = true;10874 } else if (auto *CCE = dyn_cast<CXXConstructExpr>(Init)) {10875 ResizedArrayCCE = CCE;10876 } else {10877 auto *CAT = Info.Ctx.getAsConstantArrayType(Init->getType());10878 assert(CAT && "unexpected type for array initializer");10879 10880 unsigned Bits =10881 std::max(CAT->getSizeBitWidth(), ArrayBound.getBitWidth());10882 llvm::APInt InitBound = CAT->getSize().zext(Bits);10883 llvm::APInt AllocBound = ArrayBound.zext(Bits);10884 if (InitBound.ugt(AllocBound)) {10885 if (IsNothrow)10886 return ZeroInitialization(E);10887 10888 Info.FFDiag(*ArraySize, diag::note_constexpr_new_too_small)10889 << toString(AllocBound, 10, /*Signed=*/false)10890 << toString(InitBound, 10, /*Signed=*/false)10891 << (*ArraySize)->getSourceRange();10892 return false;10893 }10894 10895 // If the sizes differ, we must have an initializer list, and we need10896 // special handling for this case when we initialize.10897 if (InitBound != AllocBound)10898 ResizedArrayILE = cast<InitListExpr>(Init);10899 }10900 10901 AllocType = Info.Ctx.getConstantArrayType(AllocType, ArrayBound, nullptr,10902 ArraySizeModifier::Normal, 0);10903 } else {10904 assert(!AllocType->isArrayType() &&10905 "array allocation with non-array new");10906 }10907 10908 APValue *Val;10909 if (IsPlacement) {10910 AccessKinds AK = AK_Construct;10911 struct FindObjectHandler {10912 EvalInfo &Info;10913 const Expr *E;10914 QualType AllocType;10915 const AccessKinds AccessKind;10916 APValue *Value;10917 10918 typedef bool result_type;10919 bool failed() { return false; }10920 bool checkConst(QualType QT) {10921 if (QT.isConstQualified()) {10922 Info.FFDiag(E, diag::note_constexpr_modify_const_type) << QT;10923 return false;10924 }10925 return true;10926 }10927 bool found(APValue &Subobj, QualType SubobjType) {10928 if (!checkConst(SubobjType))10929 return false;10930 // FIXME: Reject the cases where [basic.life]p8 would not permit the10931 // old name of the object to be used to name the new object.10932 unsigned SubobjectSize = 1;10933 unsigned AllocSize = 1;10934 if (auto *CAT = dyn_cast<ConstantArrayType>(AllocType))10935 AllocSize = CAT->getZExtSize();10936 if (auto *CAT = dyn_cast<ConstantArrayType>(SubobjType))10937 SubobjectSize = CAT->getZExtSize();10938 if (SubobjectSize < AllocSize ||10939 !Info.Ctx.hasSimilarType(Info.Ctx.getBaseElementType(SubobjType),10940 Info.Ctx.getBaseElementType(AllocType))) {10941 Info.FFDiag(E, diag::note_constexpr_placement_new_wrong_type)10942 << SubobjType << AllocType;10943 return false;10944 }10945 Value = &Subobj;10946 return true;10947 }10948 bool found(APSInt &Value, QualType SubobjType) {10949 Info.FFDiag(E, diag::note_constexpr_construct_complex_elem);10950 return false;10951 }10952 bool found(APFloat &Value, QualType SubobjType) {10953 Info.FFDiag(E, diag::note_constexpr_construct_complex_elem);10954 return false;10955 }10956 } Handler = {Info, E, AllocType, AK, nullptr};10957 10958 CompleteObject Obj = findCompleteObject(Info, E, AK, Result, AllocType);10959 if (!Obj || !findSubobject(Info, E, Obj, Result.Designator, Handler))10960 return false;10961 10962 Val = Handler.Value;10963 10964 // [basic.life]p1:10965 // The lifetime of an object o of type T ends when [...] the storage10966 // which the object occupies is [...] reused by an object that is not10967 // nested within o (6.6.2).10968 *Val = APValue();10969 } else {10970 // Perform the allocation and obtain a pointer to the resulting object.10971 Val = Info.createHeapAlloc(E, AllocType, Result);10972 if (!Val)10973 return false;10974 }10975 10976 if (ValueInit) {10977 ImplicitValueInitExpr VIE(AllocType);10978 if (!EvaluateInPlace(*Val, Info, Result, &VIE))10979 return false;10980 } else if (ResizedArrayILE) {10981 if (!EvaluateArrayNewInitList(Info, Result, *Val, ResizedArrayILE,10982 AllocType))10983 return false;10984 } else if (ResizedArrayCCE) {10985 if (!EvaluateArrayNewConstructExpr(Info, Result, *Val, ResizedArrayCCE,10986 AllocType))10987 return false;10988 } else if (Init) {10989 if (!EvaluateInPlace(*Val, Info, Result, Init))10990 return false;10991 } else if (!handleDefaultInitValue(AllocType, *Val)) {10992 return false;10993 }10994 10995 // Array new returns a pointer to the first element, not a pointer to the10996 // array.10997 if (auto *AT = AllocType->getAsArrayTypeUnsafe())10998 Result.addArray(Info, E, cast<ConstantArrayType>(AT));10999 11000 return true;11001}11002//===----------------------------------------------------------------------===//11003// Member Pointer Evaluation11004//===----------------------------------------------------------------------===//11005 11006namespace {11007class MemberPointerExprEvaluator11008 : public ExprEvaluatorBase<MemberPointerExprEvaluator> {11009 MemberPtr &Result;11010 11011 bool Success(const ValueDecl *D) {11012 Result = MemberPtr(D);11013 return true;11014 }11015public:11016 11017 MemberPointerExprEvaluator(EvalInfo &Info, MemberPtr &Result)11018 : ExprEvaluatorBaseTy(Info), Result(Result) {}11019 11020 bool Success(const APValue &V, const Expr *E) {11021 Result.setFrom(V);11022 return true;11023 }11024 bool ZeroInitialization(const Expr *E) {11025 return Success((const ValueDecl*)nullptr);11026 }11027 11028 bool VisitCastExpr(const CastExpr *E);11029 bool VisitUnaryAddrOf(const UnaryOperator *E);11030};11031} // end anonymous namespace11032 11033static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result,11034 EvalInfo &Info) {11035 assert(!E->isValueDependent());11036 assert(E->isPRValue() && E->getType()->isMemberPointerType());11037 return MemberPointerExprEvaluator(Info, Result).Visit(E);11038}11039 11040bool MemberPointerExprEvaluator::VisitCastExpr(const CastExpr *E) {11041 switch (E->getCastKind()) {11042 default:11043 return ExprEvaluatorBaseTy::VisitCastExpr(E);11044 11045 case CK_NullToMemberPointer:11046 VisitIgnoredValue(E->getSubExpr());11047 return ZeroInitialization(E);11048 11049 case CK_BaseToDerivedMemberPointer: {11050 if (!Visit(E->getSubExpr()))11051 return false;11052 if (E->path_empty())11053 return true;11054 // Base-to-derived member pointer casts store the path in derived-to-base11055 // order, so iterate backwards. The CXXBaseSpecifier also provides us with11056 // the wrong end of the derived->base arc, so stagger the path by one class.11057 typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter;11058 for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin());11059 PathI != PathE; ++PathI) {11060 assert(!(*PathI)->isVirtual() && "memptr cast through vbase");11061 const CXXRecordDecl *Derived = (*PathI)->getType()->getAsCXXRecordDecl();11062 if (!Result.castToDerived(Derived))11063 return Error(E);11064 }11065 if (!Result.castToDerived(E->getType()11066 ->castAs<MemberPointerType>()11067 ->getMostRecentCXXRecordDecl()))11068 return Error(E);11069 return true;11070 }11071 11072 case CK_DerivedToBaseMemberPointer:11073 if (!Visit(E->getSubExpr()))11074 return false;11075 for (CastExpr::path_const_iterator PathI = E->path_begin(),11076 PathE = E->path_end(); PathI != PathE; ++PathI) {11077 assert(!(*PathI)->isVirtual() && "memptr cast through vbase");11078 const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl();11079 if (!Result.castToBase(Base))11080 return Error(E);11081 }11082 return true;11083 }11084}11085 11086bool MemberPointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {11087 // C++11 [expr.unary.op]p3 has very strict rules on how the address of a11088 // member can be formed.11089 return Success(cast<DeclRefExpr>(E->getSubExpr())->getDecl());11090}11091 11092//===----------------------------------------------------------------------===//11093// Record Evaluation11094//===----------------------------------------------------------------------===//11095 11096namespace {11097 class RecordExprEvaluator11098 : public ExprEvaluatorBase<RecordExprEvaluator> {11099 const LValue &This;11100 APValue &Result;11101 public:11102 11103 RecordExprEvaluator(EvalInfo &info, const LValue &This, APValue &Result)11104 : ExprEvaluatorBaseTy(info), This(This), Result(Result) {}11105 11106 bool Success(const APValue &V, const Expr *E) {11107 Result = V;11108 return true;11109 }11110 bool ZeroInitialization(const Expr *E) {11111 return ZeroInitialization(E, E->getType());11112 }11113 bool ZeroInitialization(const Expr *E, QualType T);11114 11115 bool VisitCallExpr(const CallExpr *E) {11116 return handleCallExpr(E, Result, &This);11117 }11118 bool VisitCastExpr(const CastExpr *E);11119 bool VisitInitListExpr(const InitListExpr *E);11120 bool VisitCXXConstructExpr(const CXXConstructExpr *E) {11121 return VisitCXXConstructExpr(E, E->getType());11122 }11123 bool VisitLambdaExpr(const LambdaExpr *E);11124 bool VisitCXXInheritedCtorInitExpr(const CXXInheritedCtorInitExpr *E);11125 bool VisitCXXConstructExpr(const CXXConstructExpr *E, QualType T);11126 bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E);11127 bool VisitBinCmp(const BinaryOperator *E);11128 bool VisitCXXParenListInitExpr(const CXXParenListInitExpr *E);11129 bool VisitCXXParenListOrInitListExpr(const Expr *ExprToVisit,11130 ArrayRef<Expr *> Args);11131 };11132}11133 11134/// Perform zero-initialization on an object of non-union class type.11135/// C++11 [dcl.init]p5:11136/// To zero-initialize an object or reference of type T means:11137/// [...]11138/// -- if T is a (possibly cv-qualified) non-union class type,11139/// each non-static data member and each base-class subobject is11140/// zero-initialized11141static bool HandleClassZeroInitialization(EvalInfo &Info, const Expr *E,11142 const RecordDecl *RD,11143 const LValue &This, APValue &Result) {11144 assert(!RD->isUnion() && "Expected non-union class type");11145 const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD);11146 Result = APValue(APValue::UninitStruct(), CD ? CD->getNumBases() : 0,11147 RD->getNumFields());11148 11149 if (RD->isInvalidDecl()) return false;11150 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD);11151 11152 if (CD) {11153 unsigned Index = 0;11154 for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(),11155 End = CD->bases_end(); I != End; ++I, ++Index) {11156 const CXXRecordDecl *Base = I->getType()->getAsCXXRecordDecl();11157 LValue Subobject = This;11158 if (!HandleLValueDirectBase(Info, E, Subobject, CD, Base, &Layout))11159 return false;11160 if (!HandleClassZeroInitialization(Info, E, Base, Subobject,11161 Result.getStructBase(Index)))11162 return false;11163 }11164 }11165 11166 for (const auto *I : RD->fields()) {11167 // -- if T is a reference type, no initialization is performed.11168 if (I->isUnnamedBitField() || I->getType()->isReferenceType())11169 continue;11170 11171 LValue Subobject = This;11172 if (!HandleLValueMember(Info, E, Subobject, I, &Layout))11173 return false;11174 11175 ImplicitValueInitExpr VIE(I->getType());11176 if (!EvaluateInPlace(11177 Result.getStructField(I->getFieldIndex()), Info, Subobject, &VIE))11178 return false;11179 }11180 11181 return true;11182}11183 11184bool RecordExprEvaluator::ZeroInitialization(const Expr *E, QualType T) {11185 const auto *RD = T->castAsRecordDecl();11186 if (RD->isInvalidDecl()) return false;11187 if (RD->isUnion()) {11188 // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the11189 // object's first non-static named data member is zero-initialized11190 RecordDecl::field_iterator I = RD->field_begin();11191 while (I != RD->field_end() && (*I)->isUnnamedBitField())11192 ++I;11193 if (I == RD->field_end()) {11194 Result = APValue((const FieldDecl*)nullptr);11195 return true;11196 }11197 11198 LValue Subobject = This;11199 if (!HandleLValueMember(Info, E, Subobject, *I))11200 return false;11201 Result = APValue(*I);11202 ImplicitValueInitExpr VIE(I->getType());11203 return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, &VIE);11204 }11205 11206 if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->getNumVBases()) {11207 Info.FFDiag(E, diag::note_constexpr_virtual_base) << RD;11208 return false;11209 }11210 11211 return HandleClassZeroInitialization(Info, E, RD, This, Result);11212}11213 11214bool RecordExprEvaluator::VisitCastExpr(const CastExpr *E) {11215 switch (E->getCastKind()) {11216 default:11217 return ExprEvaluatorBaseTy::VisitCastExpr(E);11218 11219 case CK_ConstructorConversion:11220 return Visit(E->getSubExpr());11221 11222 case CK_DerivedToBase:11223 case CK_UncheckedDerivedToBase: {11224 APValue DerivedObject;11225 if (!Evaluate(DerivedObject, Info, E->getSubExpr()))11226 return false;11227 if (!DerivedObject.isStruct())11228 return Error(E->getSubExpr());11229 11230 // Derived-to-base rvalue conversion: just slice off the derived part.11231 APValue *Value = &DerivedObject;11232 const CXXRecordDecl *RD = E->getSubExpr()->getType()->getAsCXXRecordDecl();11233 for (CastExpr::path_const_iterator PathI = E->path_begin(),11234 PathE = E->path_end(); PathI != PathE; ++PathI) {11235 assert(!(*PathI)->isVirtual() && "record rvalue with virtual base");11236 const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl();11237 Value = &Value->getStructBase(getBaseIndex(RD, Base));11238 RD = Base;11239 }11240 Result = *Value;11241 return true;11242 }11243 case CK_HLSLAggregateSplatCast: {11244 APValue Val;11245 QualType ValTy;11246 11247 if (!hlslAggSplatHelper(Info, E->getSubExpr(), Val, ValTy))11248 return false;11249 11250 unsigned NEls = elementwiseSize(Info, E->getType());11251 // splat our Val11252 SmallVector<APValue> SplatEls(NEls, Val);11253 SmallVector<QualType> SplatType(NEls, ValTy);11254 11255 // cast the elements and construct our struct result11256 const FPOptions FPO = E->getFPFeaturesInEffect(Info.Ctx.getLangOpts());11257 if (!constructAggregate(Info, FPO, E, Result, E->getType(), SplatEls,11258 SplatType))11259 return false;11260 11261 return true;11262 }11263 case CK_HLSLElementwiseCast: {11264 SmallVector<APValue> SrcEls;11265 SmallVector<QualType> SrcTypes;11266 11267 if (!hlslElementwiseCastHelper(Info, E->getSubExpr(), E->getType(), SrcEls,11268 SrcTypes))11269 return false;11270 11271 // cast the elements and construct our struct result11272 const FPOptions FPO = E->getFPFeaturesInEffect(Info.Ctx.getLangOpts());11273 if (!constructAggregate(Info, FPO, E, Result, E->getType(), SrcEls,11274 SrcTypes))11275 return false;11276 11277 return true;11278 }11279 }11280}11281 11282bool RecordExprEvaluator::VisitInitListExpr(const InitListExpr *E) {11283 if (E->isTransparent())11284 return Visit(E->getInit(0));11285 return VisitCXXParenListOrInitListExpr(E, E->inits());11286}11287 11288bool RecordExprEvaluator::VisitCXXParenListOrInitListExpr(11289 const Expr *ExprToVisit, ArrayRef<Expr *> Args) {11290 const auto *RD = ExprToVisit->getType()->castAsRecordDecl();11291 if (RD->isInvalidDecl()) return false;11292 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD);11293 auto *CXXRD = dyn_cast<CXXRecordDecl>(RD);11294 11295 EvalInfo::EvaluatingConstructorRAII EvalObj(11296 Info,11297 ObjectUnderConstruction{This.getLValueBase(), This.Designator.Entries},11298 CXXRD && CXXRD->getNumBases());11299 11300 if (RD->isUnion()) {11301 const FieldDecl *Field;11302 if (auto *ILE = dyn_cast<InitListExpr>(ExprToVisit)) {11303 Field = ILE->getInitializedFieldInUnion();11304 } else if (auto *PLIE = dyn_cast<CXXParenListInitExpr>(ExprToVisit)) {11305 Field = PLIE->getInitializedFieldInUnion();11306 } else {11307 llvm_unreachable(11308 "Expression is neither an init list nor a C++ paren list");11309 }11310 11311 Result = APValue(Field);11312 if (!Field)11313 return true;11314 11315 // If the initializer list for a union does not contain any elements, the11316 // first element of the union is value-initialized.11317 // FIXME: The element should be initialized from an initializer list.11318 // Is this difference ever observable for initializer lists which11319 // we don't build?11320 ImplicitValueInitExpr VIE(Field->getType());11321 const Expr *InitExpr = Args.empty() ? &VIE : Args[0];11322 11323 LValue Subobject = This;11324 if (!HandleLValueMember(Info, InitExpr, Subobject, Field, &Layout))11325 return false;11326 11327 // Temporarily override This, in case there's a CXXDefaultInitExpr in here.11328 ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This,11329 isa<CXXDefaultInitExpr>(InitExpr));11330 11331 if (EvaluateInPlace(Result.getUnionValue(), Info, Subobject, InitExpr)) {11332 if (Field->isBitField())11333 return truncateBitfieldValue(Info, InitExpr, Result.getUnionValue(),11334 Field);11335 return true;11336 }11337 11338 return false;11339 }11340 11341 if (!Result.hasValue())11342 Result = APValue(APValue::UninitStruct(), CXXRD ? CXXRD->getNumBases() : 0,11343 RD->getNumFields());11344 unsigned ElementNo = 0;11345 bool Success = true;11346 11347 // Initialize base classes.11348 if (CXXRD && CXXRD->getNumBases()) {11349 for (const auto &Base : CXXRD->bases()) {11350 assert(ElementNo < Args.size() && "missing init for base class");11351 const Expr *Init = Args[ElementNo];11352 11353 LValue Subobject = This;11354 if (!HandleLValueBase(Info, Init, Subobject, CXXRD, &Base))11355 return false;11356 11357 APValue &FieldVal = Result.getStructBase(ElementNo);11358 if (!EvaluateInPlace(FieldVal, Info, Subobject, Init)) {11359 if (!Info.noteFailure())11360 return false;11361 Success = false;11362 }11363 ++ElementNo;11364 }11365 11366 EvalObj.finishedConstructingBases();11367 }11368 11369 // Initialize members.11370 for (const auto *Field : RD->fields()) {11371 // Anonymous bit-fields are not considered members of the class for11372 // purposes of aggregate initialization.11373 if (Field->isUnnamedBitField())11374 continue;11375 11376 LValue Subobject = This;11377 11378 bool HaveInit = ElementNo < Args.size();11379 11380 // FIXME: Diagnostics here should point to the end of the initializer11381 // list, not the start.11382 if (!HandleLValueMember(Info, HaveInit ? Args[ElementNo] : ExprToVisit,11383 Subobject, Field, &Layout))11384 return false;11385 11386 // Perform an implicit value-initialization for members beyond the end of11387 // the initializer list.11388 ImplicitValueInitExpr VIE(HaveInit ? Info.Ctx.IntTy : Field->getType());11389 const Expr *Init = HaveInit ? Args[ElementNo++] : &VIE;11390 11391 if (Field->getType()->isIncompleteArrayType()) {11392 if (auto *CAT = Info.Ctx.getAsConstantArrayType(Init->getType())) {11393 if (!CAT->isZeroSize()) {11394 // Bail out for now. This might sort of "work", but the rest of the11395 // code isn't really prepared to handle it.11396 Info.FFDiag(Init, diag::note_constexpr_unsupported_flexible_array);11397 return false;11398 }11399 }11400 }11401 11402 // Temporarily override This, in case there's a CXXDefaultInitExpr in here.11403 ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This,11404 isa<CXXDefaultInitExpr>(Init));11405 11406 APValue &FieldVal = Result.getStructField(Field->getFieldIndex());11407 if (Field->getType()->isReferenceType()) {11408 LValue Result;11409 if (!EvaluateInitForDeclOfReferenceType(Info, Field, Init, Result,11410 FieldVal)) {11411 if (!Info.noteFailure())11412 return false;11413 Success = false;11414 }11415 } else if (!EvaluateInPlace(FieldVal, Info, Subobject, Init) ||11416 (Field->isBitField() &&11417 !truncateBitfieldValue(Info, Init, FieldVal, Field))) {11418 if (!Info.noteFailure())11419 return false;11420 Success = false;11421 }11422 }11423 11424 EvalObj.finishedConstructingFields();11425 11426 return Success;11427}11428 11429bool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E,11430 QualType T) {11431 // Note that E's type is not necessarily the type of our class here; we might11432 // be initializing an array element instead.11433 const CXXConstructorDecl *FD = E->getConstructor();11434 if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl()) return false;11435 11436 bool ZeroInit = E->requiresZeroInitialization();11437 if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) {11438 if (ZeroInit)11439 return ZeroInitialization(E, T);11440 11441 return handleDefaultInitValue(T, Result);11442 }11443 11444 const FunctionDecl *Definition = nullptr;11445 auto Body = FD->getBody(Definition);11446 11447 if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition, Body))11448 return false;11449 11450 // Avoid materializing a temporary for an elidable copy/move constructor.11451 if (E->isElidable() && !ZeroInit) {11452 // FIXME: This only handles the simplest case, where the source object11453 // is passed directly as the first argument to the constructor.11454 // This should also handle stepping though implicit casts and11455 // and conversion sequences which involve two steps, with a11456 // conversion operator followed by a converting constructor.11457 const Expr *SrcObj = E->getArg(0);11458 assert(SrcObj->isTemporaryObject(Info.Ctx, FD->getParent()));11459 assert(Info.Ctx.hasSameUnqualifiedType(E->getType(), SrcObj->getType()));11460 if (const MaterializeTemporaryExpr *ME =11461 dyn_cast<MaterializeTemporaryExpr>(SrcObj))11462 return Visit(ME->getSubExpr());11463 }11464 11465 if (ZeroInit && !ZeroInitialization(E, T))11466 return false;11467 11468 auto Args = ArrayRef(E->getArgs(), E->getNumArgs());11469 return HandleConstructorCall(E, This, Args,11470 cast<CXXConstructorDecl>(Definition), Info,11471 Result);11472}11473 11474bool RecordExprEvaluator::VisitCXXInheritedCtorInitExpr(11475 const CXXInheritedCtorInitExpr *E) {11476 if (!Info.CurrentCall) {11477 assert(Info.checkingPotentialConstantExpression());11478 return false;11479 }11480 11481 const CXXConstructorDecl *FD = E->getConstructor();11482 if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl())11483 return false;11484 11485 const FunctionDecl *Definition = nullptr;11486 auto Body = FD->getBody(Definition);11487 11488 if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition, Body))11489 return false;11490 11491 return HandleConstructorCall(E, This, Info.CurrentCall->Arguments,11492 cast<CXXConstructorDecl>(Definition), Info,11493 Result);11494}11495 11496bool RecordExprEvaluator::VisitCXXStdInitializerListExpr(11497 const CXXStdInitializerListExpr *E) {11498 const ConstantArrayType *ArrayType =11499 Info.Ctx.getAsConstantArrayType(E->getSubExpr()->getType());11500 11501 LValue Array;11502 if (!EvaluateLValue(E->getSubExpr(), Array, Info))11503 return false;11504 11505 assert(ArrayType && "unexpected type for array initializer");11506 11507 // Get a pointer to the first element of the array.11508 Array.addArray(Info, E, ArrayType);11509 11510 // FIXME: What if the initializer_list type has base classes, etc?11511 Result = APValue(APValue::UninitStruct(), 0, 2);11512 Array.moveInto(Result.getStructField(0));11513 11514 auto *Record = E->getType()->castAsRecordDecl();11515 RecordDecl::field_iterator Field = Record->field_begin();11516 assert(Field != Record->field_end() &&11517 Info.Ctx.hasSameType(Field->getType()->getPointeeType(),11518 ArrayType->getElementType()) &&11519 "Expected std::initializer_list first field to be const E *");11520 ++Field;11521 assert(Field != Record->field_end() &&11522 "Expected std::initializer_list to have two fields");11523 11524 if (Info.Ctx.hasSameType(Field->getType(), Info.Ctx.getSizeType())) {11525 // Length.11526 Result.getStructField(1) = APValue(APSInt(ArrayType->getSize()));11527 } else {11528 // End pointer.11529 assert(Info.Ctx.hasSameType(Field->getType()->getPointeeType(),11530 ArrayType->getElementType()) &&11531 "Expected std::initializer_list second field to be const E *");11532 if (!HandleLValueArrayAdjustment(Info, E, Array,11533 ArrayType->getElementType(),11534 ArrayType->getZExtSize()))11535 return false;11536 Array.moveInto(Result.getStructField(1));11537 }11538 11539 assert(++Field == Record->field_end() &&11540 "Expected std::initializer_list to only have two fields");11541 11542 return true;11543}11544 11545bool RecordExprEvaluator::VisitLambdaExpr(const LambdaExpr *E) {11546 const CXXRecordDecl *ClosureClass = E->getLambdaClass();11547 if (ClosureClass->isInvalidDecl())11548 return false;11549 11550 const size_t NumFields = ClosureClass->getNumFields();11551 11552 assert(NumFields == (size_t)std::distance(E->capture_init_begin(),11553 E->capture_init_end()) &&11554 "The number of lambda capture initializers should equal the number of "11555 "fields within the closure type");11556 11557 Result = APValue(APValue::UninitStruct(), /*NumBases*/0, NumFields);11558 // Iterate through all the lambda's closure object's fields and initialize11559 // them.11560 auto *CaptureInitIt = E->capture_init_begin();11561 bool Success = true;11562 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(ClosureClass);11563 for (const auto *Field : ClosureClass->fields()) {11564 assert(CaptureInitIt != E->capture_init_end());11565 // Get the initializer for this field11566 Expr *const CurFieldInit = *CaptureInitIt++;11567 11568 // If there is no initializer, either this is a VLA or an error has11569 // occurred.11570 if (!CurFieldInit || CurFieldInit->containsErrors())11571 return Error(E);11572 11573 LValue Subobject = This;11574 11575 if (!HandleLValueMember(Info, E, Subobject, Field, &Layout))11576 return false;11577 11578 APValue &FieldVal = Result.getStructField(Field->getFieldIndex());11579 if (!EvaluateInPlace(FieldVal, Info, Subobject, CurFieldInit)) {11580 if (!Info.keepEvaluatingAfterFailure())11581 return false;11582 Success = false;11583 }11584 }11585 return Success;11586}11587 11588static bool EvaluateRecord(const Expr *E, const LValue &This,11589 APValue &Result, EvalInfo &Info) {11590 assert(!E->isValueDependent());11591 assert(E->isPRValue() && E->getType()->isRecordType() &&11592 "can't evaluate expression as a record rvalue");11593 return RecordExprEvaluator(Info, This, Result).Visit(E);11594}11595 11596//===----------------------------------------------------------------------===//11597// Temporary Evaluation11598//11599// Temporaries are represented in the AST as rvalues, but generally behave like11600// lvalues. The full-object of which the temporary is a subobject is implicitly11601// materialized so that a reference can bind to it.11602//===----------------------------------------------------------------------===//11603namespace {11604class TemporaryExprEvaluator11605 : public LValueExprEvaluatorBase<TemporaryExprEvaluator> {11606public:11607 TemporaryExprEvaluator(EvalInfo &Info, LValue &Result) :11608 LValueExprEvaluatorBaseTy(Info, Result, false) {}11609 11610 /// Visit an expression which constructs the value of this temporary.11611 bool VisitConstructExpr(const Expr *E) {11612 APValue &Value = Info.CurrentCall->createTemporary(11613 E, E->getType(), ScopeKind::FullExpression, Result);11614 return EvaluateInPlace(Value, Info, Result, E);11615 }11616 11617 bool VisitCastExpr(const CastExpr *E) {11618 switch (E->getCastKind()) {11619 default:11620 return LValueExprEvaluatorBaseTy::VisitCastExpr(E);11621 11622 case CK_ConstructorConversion:11623 return VisitConstructExpr(E->getSubExpr());11624 }11625 }11626 bool VisitInitListExpr(const InitListExpr *E) {11627 return VisitConstructExpr(E);11628 }11629 bool VisitCXXConstructExpr(const CXXConstructExpr *E) {11630 return VisitConstructExpr(E);11631 }11632 bool VisitCallExpr(const CallExpr *E) {11633 return VisitConstructExpr(E);11634 }11635 bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E) {11636 return VisitConstructExpr(E);11637 }11638 bool VisitLambdaExpr(const LambdaExpr *E) {11639 return VisitConstructExpr(E);11640 }11641};11642} // end anonymous namespace11643 11644/// Evaluate an expression of record type as a temporary.11645static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info) {11646 assert(!E->isValueDependent());11647 assert(E->isPRValue() && E->getType()->isRecordType());11648 return TemporaryExprEvaluator(Info, Result).Visit(E);11649}11650 11651//===----------------------------------------------------------------------===//11652// Vector Evaluation11653//===----------------------------------------------------------------------===//11654 11655namespace {11656 class VectorExprEvaluator11657 : public ExprEvaluatorBase<VectorExprEvaluator> {11658 APValue &Result;11659 public:11660 11661 VectorExprEvaluator(EvalInfo &info, APValue &Result)11662 : ExprEvaluatorBaseTy(info), Result(Result) {}11663 11664 bool Success(ArrayRef<APValue> V, const Expr *E) {11665 assert(V.size() == E->getType()->castAs<VectorType>()->getNumElements());11666 // FIXME: remove this APValue copy.11667 Result = APValue(V.data(), V.size());11668 return true;11669 }11670 bool Success(const APValue &V, const Expr *E) {11671 assert(V.isVector());11672 Result = V;11673 return true;11674 }11675 bool ZeroInitialization(const Expr *E);11676 11677 bool VisitUnaryReal(const UnaryOperator *E)11678 { return Visit(E->getSubExpr()); }11679 bool VisitCastExpr(const CastExpr* E);11680 bool VisitInitListExpr(const InitListExpr *E);11681 bool VisitUnaryImag(const UnaryOperator *E);11682 bool VisitBinaryOperator(const BinaryOperator *E);11683 bool VisitUnaryOperator(const UnaryOperator *E);11684 bool VisitCallExpr(const CallExpr *E);11685 bool VisitConvertVectorExpr(const ConvertVectorExpr *E);11686 bool VisitShuffleVectorExpr(const ShuffleVectorExpr *E);11687 11688 // FIXME: Missing: conditional operator (for GNU11689 // conditional select), ExtVectorElementExpr11690 };11691} // end anonymous namespace11692 11693static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {11694 assert(E->isPRValue() && E->getType()->isVectorType() &&11695 "not a vector prvalue");11696 return VectorExprEvaluator(Info, Result).Visit(E);11697}11698 11699static llvm::APInt ConvertBoolVectorToInt(const APValue &Val) {11700 assert(Val.isVector() && "expected vector APValue");11701 unsigned NumElts = Val.getVectorLength();11702 11703 // Each element is one bit, so create an integer with NumElts bits.11704 llvm::APInt Result(NumElts, 0);11705 11706 for (unsigned I = 0; I < NumElts; ++I) {11707 const APValue &Elt = Val.getVectorElt(I);11708 assert(Elt.isInt() && "expected integer element in bool vector");11709 11710 if (Elt.getInt().getBoolValue())11711 Result.setBit(I);11712 }11713 11714 return Result;11715}11716 11717bool VectorExprEvaluator::VisitCastExpr(const CastExpr *E) {11718 const VectorType *VTy = E->getType()->castAs<VectorType>();11719 unsigned NElts = VTy->getNumElements();11720 11721 const Expr *SE = E->getSubExpr();11722 QualType SETy = SE->getType();11723 11724 switch (E->getCastKind()) {11725 case CK_VectorSplat: {11726 APValue Val = APValue();11727 if (SETy->isIntegerType()) {11728 APSInt IntResult;11729 if (!EvaluateInteger(SE, IntResult, Info))11730 return false;11731 Val = APValue(std::move(IntResult));11732 } else if (SETy->isRealFloatingType()) {11733 APFloat FloatResult(0.0);11734 if (!EvaluateFloat(SE, FloatResult, Info))11735 return false;11736 Val = APValue(std::move(FloatResult));11737 } else {11738 return Error(E);11739 }11740 11741 // Splat and create vector APValue.11742 SmallVector<APValue, 4> Elts(NElts, Val);11743 return Success(Elts, E);11744 }11745 case CK_BitCast: {11746 APValue SVal;11747 if (!Evaluate(SVal, Info, SE))11748 return false;11749 11750 if (!SVal.isInt() && !SVal.isFloat() && !SVal.isVector()) {11751 // Give up if the input isn't an int, float, or vector. For example, we11752 // reject "(v4i16)(intptr_t)&a".11753 Info.FFDiag(E, diag::note_constexpr_invalid_cast)11754 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret11755 << Info.Ctx.getLangOpts().CPlusPlus;11756 return false;11757 }11758 11759 if (!handleRValueToRValueBitCast(Info, Result, SVal, E))11760 return false;11761 11762 return true;11763 }11764 case CK_HLSLVectorTruncation: {11765 APValue Val;11766 SmallVector<APValue, 4> Elements;11767 if (!EvaluateVector(SE, Val, Info))11768 return Error(E);11769 for (unsigned I = 0; I < NElts; I++)11770 Elements.push_back(Val.getVectorElt(I));11771 return Success(Elements, E);11772 }11773 case CK_HLSLAggregateSplatCast: {11774 APValue Val;11775 QualType ValTy;11776 11777 if (!hlslAggSplatHelper(Info, SE, Val, ValTy))11778 return false;11779 11780 // cast our Val once.11781 APValue Result;11782 const FPOptions FPO = E->getFPFeaturesInEffect(Info.Ctx.getLangOpts());11783 if (!handleScalarCast(Info, FPO, E, ValTy, VTy->getElementType(), Val,11784 Result))11785 return false;11786 11787 SmallVector<APValue, 4> SplatEls(NElts, Result);11788 return Success(SplatEls, E);11789 }11790 case CK_HLSLElementwiseCast: {11791 SmallVector<APValue> SrcVals;11792 SmallVector<QualType> SrcTypes;11793 11794 if (!hlslElementwiseCastHelper(Info, SE, E->getType(), SrcVals, SrcTypes))11795 return false;11796 11797 const FPOptions FPO = E->getFPFeaturesInEffect(Info.Ctx.getLangOpts());11798 SmallVector<QualType, 4> DestTypes(NElts, VTy->getElementType());11799 SmallVector<APValue, 4> ResultEls(NElts);11800 if (!handleElementwiseCast(Info, E, FPO, SrcVals, SrcTypes, DestTypes,11801 ResultEls))11802 return false;11803 return Success(ResultEls, E);11804 }11805 default:11806 return ExprEvaluatorBaseTy::VisitCastExpr(E);11807 }11808}11809 11810bool11811VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {11812 const VectorType *VT = E->getType()->castAs<VectorType>();11813 unsigned NumInits = E->getNumInits();11814 unsigned NumElements = VT->getNumElements();11815 11816 QualType EltTy = VT->getElementType();11817 SmallVector<APValue, 4> Elements;11818 11819 // MFloat8 type doesn't have constants and thus constant folding11820 // is impossible.11821 if (EltTy->isMFloat8Type())11822 return false;11823 11824 // The number of initializers can be less than the number of11825 // vector elements. For OpenCL, this can be due to nested vector11826 // initialization. For GCC compatibility, missing trailing elements11827 // should be initialized with zeroes.11828 unsigned CountInits = 0, CountElts = 0;11829 while (CountElts < NumElements) {11830 // Handle nested vector initialization.11831 if (CountInits < NumInits11832 && E->getInit(CountInits)->getType()->isVectorType()) {11833 APValue v;11834 if (!EvaluateVector(E->getInit(CountInits), v, Info))11835 return Error(E);11836 unsigned vlen = v.getVectorLength();11837 for (unsigned j = 0; j < vlen; j++)11838 Elements.push_back(v.getVectorElt(j));11839 CountElts += vlen;11840 } else if (EltTy->isIntegerType()) {11841 llvm::APSInt sInt(32);11842 if (CountInits < NumInits) {11843 if (!EvaluateInteger(E->getInit(CountInits), sInt, Info))11844 return false;11845 } else // trailing integer zero.11846 sInt = Info.Ctx.MakeIntValue(0, EltTy);11847 Elements.push_back(APValue(sInt));11848 CountElts++;11849 } else {11850 llvm::APFloat f(0.0);11851 if (CountInits < NumInits) {11852 if (!EvaluateFloat(E->getInit(CountInits), f, Info))11853 return false;11854 } else // trailing float zero.11855 f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));11856 Elements.push_back(APValue(f));11857 CountElts++;11858 }11859 CountInits++;11860 }11861 return Success(Elements, E);11862}11863 11864bool11865VectorExprEvaluator::ZeroInitialization(const Expr *E) {11866 const auto *VT = E->getType()->castAs<VectorType>();11867 QualType EltTy = VT->getElementType();11868 APValue ZeroElement;11869 if (EltTy->isIntegerType())11870 ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));11871 else11872 ZeroElement =11873 APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));11874 11875 SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);11876 return Success(Elements, E);11877}11878 11879bool VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {11880 VisitIgnoredValue(E->getSubExpr());11881 return ZeroInitialization(E);11882}11883 11884bool VectorExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {11885 BinaryOperatorKind Op = E->getOpcode();11886 assert(Op != BO_PtrMemD && Op != BO_PtrMemI && Op != BO_Cmp &&11887 "Operation not supported on vector types");11888 11889 if (Op == BO_Comma)11890 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);11891 11892 Expr *LHS = E->getLHS();11893 Expr *RHS = E->getRHS();11894 11895 assert(LHS->getType()->isVectorType() && RHS->getType()->isVectorType() &&11896 "Must both be vector types");11897 // Checking JUST the types are the same would be fine, except shifts don't11898 // need to have their types be the same (since you always shift by an int).11899 assert(LHS->getType()->castAs<VectorType>()->getNumElements() ==11900 E->getType()->castAs<VectorType>()->getNumElements() &&11901 RHS->getType()->castAs<VectorType>()->getNumElements() ==11902 E->getType()->castAs<VectorType>()->getNumElements() &&11903 "All operands must be the same size.");11904 11905 APValue LHSValue;11906 APValue RHSValue;11907 bool LHSOK = Evaluate(LHSValue, Info, LHS);11908 if (!LHSOK && !Info.noteFailure())11909 return false;11910 if (!Evaluate(RHSValue, Info, RHS) || !LHSOK)11911 return false;11912 11913 if (!handleVectorVectorBinOp(Info, E, Op, LHSValue, RHSValue))11914 return false;11915 11916 return Success(LHSValue, E);11917}11918 11919static std::optional<APValue> handleVectorUnaryOperator(ASTContext &Ctx,11920 QualType ResultTy,11921 UnaryOperatorKind Op,11922 APValue Elt) {11923 switch (Op) {11924 case UO_Plus:11925 // Nothing to do here.11926 return Elt;11927 case UO_Minus:11928 if (Elt.getKind() == APValue::Int) {11929 Elt.getInt().negate();11930 } else {11931 assert(Elt.getKind() == APValue::Float &&11932 "Vector can only be int or float type");11933 Elt.getFloat().changeSign();11934 }11935 return Elt;11936 case UO_Not:11937 // This is only valid for integral types anyway, so we don't have to handle11938 // float here.11939 assert(Elt.getKind() == APValue::Int &&11940 "Vector operator ~ can only be int");11941 Elt.getInt().flipAllBits();11942 return Elt;11943 case UO_LNot: {11944 if (Elt.getKind() == APValue::Int) {11945 Elt.getInt() = !Elt.getInt();11946 // operator ! on vectors returns -1 for 'truth', so negate it.11947 Elt.getInt().negate();11948 return Elt;11949 }11950 assert(Elt.getKind() == APValue::Float &&11951 "Vector can only be int or float type");11952 // Float types result in an int of the same size, but -1 for true, or 0 for11953 // false.11954 APSInt EltResult{Ctx.getIntWidth(ResultTy),11955 ResultTy->isUnsignedIntegerType()};11956 if (Elt.getFloat().isZero())11957 EltResult.setAllBits();11958 else11959 EltResult.clearAllBits();11960 11961 return APValue{EltResult};11962 }11963 default:11964 // FIXME: Implement the rest of the unary operators.11965 return std::nullopt;11966 }11967}11968 11969bool VectorExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {11970 Expr *SubExpr = E->getSubExpr();11971 const auto *VD = SubExpr->getType()->castAs<VectorType>();11972 // This result element type differs in the case of negating a floating point11973 // vector, since the result type is the a vector of the equivilant sized11974 // integer.11975 const QualType ResultEltTy = VD->getElementType();11976 UnaryOperatorKind Op = E->getOpcode();11977 11978 APValue SubExprValue;11979 if (!Evaluate(SubExprValue, Info, SubExpr))11980 return false;11981 11982 // FIXME: This vector evaluator someday needs to be changed to be LValue11983 // aware/keep LValue information around, rather than dealing with just vector11984 // types directly. Until then, we cannot handle cases where the operand to11985 // these unary operators is an LValue. The only case I've been able to see11986 // cause this is operator++ assigning to a member expression (only valid in11987 // altivec compilations) in C mode, so this shouldn't limit us too much.11988 if (SubExprValue.isLValue())11989 return false;11990 11991 assert(SubExprValue.getVectorLength() == VD->getNumElements() &&11992 "Vector length doesn't match type?");11993 11994 SmallVector<APValue, 4> ResultElements;11995 for (unsigned EltNum = 0; EltNum < VD->getNumElements(); ++EltNum) {11996 std::optional<APValue> Elt = handleVectorUnaryOperator(11997 Info.Ctx, ResultEltTy, Op, SubExprValue.getVectorElt(EltNum));11998 if (!Elt)11999 return false;12000 ResultElements.push_back(*Elt);12001 }12002 return Success(APValue(ResultElements.data(), ResultElements.size()), E);12003}12004 12005static bool handleVectorElementCast(EvalInfo &Info, const FPOptions FPO,12006 const Expr *E, QualType SourceTy,12007 QualType DestTy, APValue const &Original,12008 APValue &Result) {12009 if (SourceTy->isIntegerType()) {12010 if (DestTy->isRealFloatingType()) {12011 Result = APValue(APFloat(0.0));12012 return HandleIntToFloatCast(Info, E, FPO, SourceTy, Original.getInt(),12013 DestTy, Result.getFloat());12014 }12015 if (DestTy->isIntegerType()) {12016 Result = APValue(12017 HandleIntToIntCast(Info, E, DestTy, SourceTy, Original.getInt()));12018 return true;12019 }12020 } else if (SourceTy->isRealFloatingType()) {12021 if (DestTy->isRealFloatingType()) {12022 Result = Original;12023 return HandleFloatToFloatCast(Info, E, SourceTy, DestTy,12024 Result.getFloat());12025 }12026 if (DestTy->isIntegerType()) {12027 Result = APValue(APSInt());12028 return HandleFloatToIntCast(Info, E, SourceTy, Original.getFloat(),12029 DestTy, Result.getInt());12030 }12031 }12032 12033 Info.FFDiag(E, diag::err_convertvector_constexpr_unsupported_vector_cast)12034 << SourceTy << DestTy;12035 return false;12036}12037 12038static bool evalPackBuiltin(const CallExpr *E, EvalInfo &Info, APValue &Result,12039 llvm::function_ref<APInt(const APSInt &)> PackFn) {12040 APValue LHS, RHS;12041 if (!EvaluateAsRValue(Info, E->getArg(0), LHS) ||12042 !EvaluateAsRValue(Info, E->getArg(1), RHS))12043 return false;12044 12045 unsigned LHSVecLen = LHS.getVectorLength();12046 unsigned RHSVecLen = RHS.getVectorLength();12047 12048 assert(LHSVecLen != 0 && LHSVecLen == RHSVecLen &&12049 "pack builtin LHSVecLen must equal to RHSVecLen");12050 12051 const VectorType *VT0 = E->getArg(0)->getType()->castAs<VectorType>();12052 const unsigned SrcBits = Info.Ctx.getIntWidth(VT0->getElementType());12053 12054 const VectorType *DstVT = E->getType()->castAs<VectorType>();12055 QualType DstElemTy = DstVT->getElementType();12056 const bool DstIsUnsigned = DstElemTy->isUnsignedIntegerType();12057 12058 const unsigned SrcPerLane = 128 / SrcBits;12059 const unsigned Lanes = LHSVecLen * SrcBits / 128;12060 12061 SmallVector<APValue, 64> Out;12062 Out.reserve(LHSVecLen + RHSVecLen);12063 12064 for (unsigned Lane = 0; Lane != Lanes; ++Lane) {12065 unsigned base = Lane * SrcPerLane;12066 for (unsigned I = 0; I != SrcPerLane; ++I)12067 Out.emplace_back(APValue(12068 APSInt(PackFn(LHS.getVectorElt(base + I).getInt()), DstIsUnsigned)));12069 for (unsigned I = 0; I != SrcPerLane; ++I)12070 Out.emplace_back(APValue(12071 APSInt(PackFn(RHS.getVectorElt(base + I).getInt()), DstIsUnsigned)));12072 }12073 12074 Result = APValue(Out.data(), Out.size());12075 return true;12076}12077 12078static bool evalShuffleGeneric(12079 EvalInfo &Info, const CallExpr *Call, APValue &Out,12080 llvm::function_ref<std::pair<unsigned, int>(unsigned, unsigned)>12081 GetSourceIndex) {12082 12083 const auto *VT = Call->getType()->getAs<VectorType>();12084 if (!VT)12085 return false;12086 12087 unsigned ShuffleMask = 0;12088 APValue A, MaskVector, B;12089 bool IsVectorMask = false;12090 bool IsSingleOperand = (Call->getNumArgs() == 2);12091 12092 if (IsSingleOperand) {12093 QualType MaskType = Call->getArg(1)->getType();12094 if (MaskType->isVectorType()) {12095 IsVectorMask = true;12096 if (!EvaluateAsRValue(Info, Call->getArg(0), A) ||12097 !EvaluateAsRValue(Info, Call->getArg(1), MaskVector))12098 return false;12099 B = A;12100 } else if (MaskType->isIntegerType()) {12101 APSInt MaskImm;12102 if (!EvaluateInteger(Call->getArg(1), MaskImm, Info))12103 return false;12104 ShuffleMask = static_cast<unsigned>(MaskImm.getZExtValue());12105 if (!EvaluateAsRValue(Info, Call->getArg(0), A))12106 return false;12107 B = A;12108 } else {12109 return false;12110 }12111 } else {12112 QualType Arg2Type = Call->getArg(2)->getType();12113 if (Arg2Type->isVectorType()) {12114 IsVectorMask = true;12115 if (!EvaluateAsRValue(Info, Call->getArg(0), A) ||12116 !EvaluateAsRValue(Info, Call->getArg(1), MaskVector) ||12117 !EvaluateAsRValue(Info, Call->getArg(2), B))12118 return false;12119 } else if (Arg2Type->isIntegerType()) {12120 APSInt MaskImm;12121 if (!EvaluateInteger(Call->getArg(2), MaskImm, Info))12122 return false;12123 ShuffleMask = static_cast<unsigned>(MaskImm.getZExtValue());12124 if (!EvaluateAsRValue(Info, Call->getArg(0), A) ||12125 !EvaluateAsRValue(Info, Call->getArg(1), B))12126 return false;12127 } else {12128 return false;12129 }12130 }12131 12132 unsigned NumElts = VT->getNumElements();12133 SmallVector<APValue, 64> ResultElements;12134 ResultElements.reserve(NumElts);12135 12136 for (unsigned DstIdx = 0; DstIdx != NumElts; ++DstIdx) {12137 if (IsVectorMask) {12138 ShuffleMask = static_cast<unsigned>(12139 MaskVector.getVectorElt(DstIdx).getInt().getZExtValue());12140 }12141 auto [SrcVecIdx, SrcIdx] = GetSourceIndex(DstIdx, ShuffleMask);12142 12143 if (SrcIdx < 0) {12144 // Zero out this element12145 QualType ElemTy = VT->getElementType();12146 if (ElemTy->isRealFloatingType()) {12147 ResultElements.push_back(12148 APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(ElemTy))));12149 } else if (ElemTy->isIntegerType()) {12150 APValue Zero(Info.Ctx.MakeIntValue(0, ElemTy));12151 ResultElements.push_back(APValue(Zero));12152 } else {12153 // Other types of fallback logic12154 ResultElements.push_back(APValue());12155 }12156 } else {12157 const APValue &Src = (SrcVecIdx == 0) ? A : B;12158 ResultElements.push_back(Src.getVectorElt(SrcIdx));12159 }12160 }12161 12162 Out = APValue(ResultElements.data(), ResultElements.size());12163 return true;12164}12165 12166static bool evalShiftWithCount(12167 EvalInfo &Info, const CallExpr *Call, APValue &Out,12168 llvm::function_ref<APInt(const APInt &, uint64_t)> ShiftOp,12169 llvm::function_ref<APInt(const APInt &, unsigned)> OverflowOp) {12170 12171 APValue Source, Count;12172 if (!EvaluateAsRValue(Info, Call->getArg(0), Source) ||12173 !EvaluateAsRValue(Info, Call->getArg(1), Count))12174 return false;12175 12176 assert(Call->getNumArgs() == 2);12177 12178 QualType SourceTy = Call->getArg(0)->getType();12179 assert(SourceTy->isVectorType() &&12180 Call->getArg(1)->getType()->isVectorType());12181 12182 QualType DestEltTy = SourceTy->castAs<VectorType>()->getElementType();12183 unsigned DestEltWidth = Source.getVectorElt(0).getInt().getBitWidth();12184 unsigned DestLen = Source.getVectorLength();12185 bool IsDestUnsigned = DestEltTy->isUnsignedIntegerType();12186 unsigned CountEltWidth = Count.getVectorElt(0).getInt().getBitWidth();12187 unsigned NumBitsInQWord = 64;12188 unsigned NumCountElts = NumBitsInQWord / CountEltWidth;12189 SmallVector<APValue, 64> Result;12190 Result.reserve(DestLen);12191 12192 uint64_t CountLQWord = 0;12193 for (unsigned EltIdx = 0; EltIdx != NumCountElts; ++EltIdx) {12194 uint64_t Elt = Count.getVectorElt(EltIdx).getInt().getZExtValue();12195 CountLQWord |= (Elt << (EltIdx * CountEltWidth));12196 }12197 12198 for (unsigned EltIdx = 0; EltIdx != DestLen; ++EltIdx) {12199 APInt Elt = Source.getVectorElt(EltIdx).getInt();12200 if (CountLQWord < DestEltWidth) {12201 Result.push_back(12202 APValue(APSInt(ShiftOp(Elt, CountLQWord), IsDestUnsigned)));12203 } else {12204 Result.push_back(12205 APValue(APSInt(OverflowOp(Elt, DestEltWidth), IsDestUnsigned)));12206 }12207 }12208 Out = APValue(Result.data(), Result.size());12209 return true;12210}12211 12212bool VectorExprEvaluator::VisitCallExpr(const CallExpr *E) {12213 if (!IsConstantEvaluatedBuiltinCall(E))12214 return ExprEvaluatorBaseTy::VisitCallExpr(E);12215 12216 auto EvaluateBinOpExpr =12217 [&](llvm::function_ref<APInt(const APSInt &, const APSInt &)> Fn) {12218 APValue SourceLHS, SourceRHS;12219 if (!EvaluateAsRValue(Info, E->getArg(0), SourceLHS) ||12220 !EvaluateAsRValue(Info, E->getArg(1), SourceRHS))12221 return false;12222 12223 auto *DestTy = E->getType()->castAs<VectorType>();12224 QualType DestEltTy = DestTy->getElementType();12225 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();12226 unsigned SourceLen = SourceLHS.getVectorLength();12227 SmallVector<APValue, 4> ResultElements;12228 ResultElements.reserve(SourceLen);12229 12230 if (SourceRHS.isInt()) {12231 const APSInt &RHS = SourceRHS.getInt();12232 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {12233 const APSInt &LHS = SourceLHS.getVectorElt(EltNum).getInt();12234 ResultElements.push_back(12235 APValue(APSInt(Fn(LHS, RHS), DestUnsigned)));12236 }12237 } else {12238 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {12239 const APSInt &LHS = SourceLHS.getVectorElt(EltNum).getInt();12240 const APSInt &RHS = SourceRHS.getVectorElt(EltNum).getInt();12241 ResultElements.push_back(12242 APValue(APSInt(Fn(LHS, RHS), DestUnsigned)));12243 }12244 }12245 return Success(APValue(ResultElements.data(), SourceLen), E);12246 };12247 12248 auto EvalSelectScalar = [&](unsigned Len) -> bool {12249 APSInt Mask;12250 APValue AVal, WVal;12251 if (!EvaluateInteger(E->getArg(0), Mask, Info) ||12252 !EvaluateAsRValue(Info, E->getArg(1), AVal) ||12253 !EvaluateAsRValue(Info, E->getArg(2), WVal))12254 return false;12255 12256 bool TakeA0 = (Mask.getZExtValue() & 1u) != 0;12257 SmallVector<APValue, 4> Res;12258 Res.reserve(Len);12259 Res.push_back(TakeA0 ? AVal.getVectorElt(0) : WVal.getVectorElt(0));12260 for (unsigned I = 1; I < Len; ++I)12261 Res.push_back(WVal.getVectorElt(I));12262 APValue V(Res.data(), Res.size());12263 return Success(V, E);12264 };12265 12266 switch (E->getBuiltinCallee()) {12267 default:12268 return false;12269 case Builtin::BI__builtin_elementwise_popcount:12270 case Builtin::BI__builtin_elementwise_bitreverse: {12271 APValue Source;12272 if (!EvaluateAsRValue(Info, E->getArg(0), Source))12273 return false;12274 12275 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();12276 unsigned SourceLen = Source.getVectorLength();12277 SmallVector<APValue, 4> ResultElements;12278 ResultElements.reserve(SourceLen);12279 12280 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {12281 APSInt Elt = Source.getVectorElt(EltNum).getInt();12282 switch (E->getBuiltinCallee()) {12283 case Builtin::BI__builtin_elementwise_popcount:12284 ResultElements.push_back(APValue(12285 APSInt(APInt(Info.Ctx.getIntWidth(DestEltTy), Elt.popcount()),12286 DestEltTy->isUnsignedIntegerOrEnumerationType())));12287 break;12288 case Builtin::BI__builtin_elementwise_bitreverse:12289 ResultElements.push_back(12290 APValue(APSInt(Elt.reverseBits(),12291 DestEltTy->isUnsignedIntegerOrEnumerationType())));12292 break;12293 }12294 }12295 12296 return Success(APValue(ResultElements.data(), ResultElements.size()), E);12297 }12298 case Builtin::BI__builtin_elementwise_abs: {12299 APValue Source;12300 if (!EvaluateAsRValue(Info, E->getArg(0), Source))12301 return false;12302 12303 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();12304 unsigned SourceLen = Source.getVectorLength();12305 SmallVector<APValue, 4> ResultElements;12306 ResultElements.reserve(SourceLen);12307 12308 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {12309 APValue CurrentEle = Source.getVectorElt(EltNum);12310 APValue Val = DestEltTy->isFloatingType()12311 ? APValue(llvm::abs(CurrentEle.getFloat()))12312 : APValue(APSInt(12313 CurrentEle.getInt().abs(),12314 DestEltTy->isUnsignedIntegerOrEnumerationType()));12315 ResultElements.push_back(Val);12316 }12317 12318 return Success(APValue(ResultElements.data(), ResultElements.size()), E);12319 }12320 12321 case Builtin::BI__builtin_elementwise_add_sat:12322 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {12323 return LHS.isSigned() ? LHS.sadd_sat(RHS) : LHS.uadd_sat(RHS);12324 });12325 12326 case Builtin::BI__builtin_elementwise_sub_sat:12327 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {12328 return LHS.isSigned() ? LHS.ssub_sat(RHS) : LHS.usub_sat(RHS);12329 });12330 12331 case X86::BI__builtin_ia32_extract128i256:12332 case X86::BI__builtin_ia32_vextractf128_pd256:12333 case X86::BI__builtin_ia32_vextractf128_ps256:12334 case X86::BI__builtin_ia32_vextractf128_si256: {12335 APValue SourceVec, SourceImm;12336 if (!EvaluateAsRValue(Info, E->getArg(0), SourceVec) ||12337 !EvaluateAsRValue(Info, E->getArg(1), SourceImm))12338 return false;12339 12340 if (!SourceVec.isVector())12341 return false;12342 12343 const auto *RetVT = E->getType()->castAs<VectorType>();12344 unsigned RetLen = RetVT->getNumElements();12345 unsigned Idx = SourceImm.getInt().getZExtValue() & 1;12346 12347 SmallVector<APValue, 32> ResultElements;12348 ResultElements.reserve(RetLen);12349 12350 for (unsigned I = 0; I < RetLen; I++)12351 ResultElements.push_back(SourceVec.getVectorElt(Idx * RetLen + I));12352 12353 return Success(APValue(ResultElements.data(), RetLen), E);12354 }12355 12356 case X86::BI__builtin_ia32_extracti32x4_256_mask:12357 case X86::BI__builtin_ia32_extractf32x4_256_mask:12358 case X86::BI__builtin_ia32_extracti32x4_mask:12359 case X86::BI__builtin_ia32_extractf32x4_mask:12360 case X86::BI__builtin_ia32_extracti32x8_mask:12361 case X86::BI__builtin_ia32_extractf32x8_mask:12362 case X86::BI__builtin_ia32_extracti64x2_256_mask:12363 case X86::BI__builtin_ia32_extractf64x2_256_mask:12364 case X86::BI__builtin_ia32_extracti64x2_512_mask:12365 case X86::BI__builtin_ia32_extractf64x2_512_mask:12366 case X86::BI__builtin_ia32_extracti64x4_mask:12367 case X86::BI__builtin_ia32_extractf64x4_mask: {12368 APValue SourceVec, MergeVec;12369 APSInt Imm, MaskImm;12370 12371 if (!EvaluateAsRValue(Info, E->getArg(0), SourceVec) ||12372 !EvaluateInteger(E->getArg(1), Imm, Info) ||12373 !EvaluateAsRValue(Info, E->getArg(2), MergeVec) ||12374 !EvaluateInteger(E->getArg(3), MaskImm, Info))12375 return false;12376 12377 const auto *RetVT = E->getType()->castAs<VectorType>();12378 unsigned RetLen = RetVT->getNumElements();12379 12380 if (!SourceVec.isVector() || !MergeVec.isVector())12381 return false;12382 unsigned SrcLen = SourceVec.getVectorLength();12383 unsigned Lanes = SrcLen / RetLen;12384 unsigned Lane = static_cast<unsigned>(Imm.getZExtValue() % Lanes);12385 unsigned Base = Lane * RetLen;12386 12387 SmallVector<APValue, 32> ResultElements;12388 ResultElements.reserve(RetLen);12389 for (unsigned I = 0; I < RetLen; ++I) {12390 if (MaskImm[I])12391 ResultElements.push_back(SourceVec.getVectorElt(Base + I));12392 else12393 ResultElements.push_back(MergeVec.getVectorElt(I));12394 }12395 return Success(APValue(ResultElements.data(), ResultElements.size()), E);12396 }12397 12398 case clang::X86::BI__builtin_ia32_pavgb128:12399 case clang::X86::BI__builtin_ia32_pavgw128:12400 case clang::X86::BI__builtin_ia32_pavgb256:12401 case clang::X86::BI__builtin_ia32_pavgw256:12402 case clang::X86::BI__builtin_ia32_pavgb512:12403 case clang::X86::BI__builtin_ia32_pavgw512:12404 return EvaluateBinOpExpr(llvm::APIntOps::avgCeilU);12405 12406 case clang::X86::BI__builtin_ia32_pmulhrsw128:12407 case clang::X86::BI__builtin_ia32_pmulhrsw256:12408 case clang::X86::BI__builtin_ia32_pmulhrsw512:12409 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {12410 return (llvm::APIntOps::mulsExtended(LHS, RHS).ashr(14) + 1)12411 .extractBits(16, 1);12412 });12413 12414 case clang::X86::BI__builtin_ia32_pmaddubsw128:12415 case clang::X86::BI__builtin_ia32_pmaddubsw256:12416 case clang::X86::BI__builtin_ia32_pmaddubsw512:12417 case clang::X86::BI__builtin_ia32_pmaddwd128:12418 case clang::X86::BI__builtin_ia32_pmaddwd256:12419 case clang::X86::BI__builtin_ia32_pmaddwd512: {12420 APValue SourceLHS, SourceRHS;12421 if (!EvaluateAsRValue(Info, E->getArg(0), SourceLHS) ||12422 !EvaluateAsRValue(Info, E->getArg(1), SourceRHS))12423 return false;12424 12425 auto *DestTy = E->getType()->castAs<VectorType>();12426 QualType DestEltTy = DestTy->getElementType();12427 unsigned SourceLen = SourceLHS.getVectorLength();12428 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();12429 SmallVector<APValue, 4> ResultElements;12430 ResultElements.reserve(SourceLen / 2);12431 12432 for (unsigned EltNum = 0; EltNum < SourceLen; EltNum += 2) {12433 const APSInt &LoLHS = SourceLHS.getVectorElt(EltNum).getInt();12434 const APSInt &HiLHS = SourceLHS.getVectorElt(EltNum + 1).getInt();12435 const APSInt &LoRHS = SourceRHS.getVectorElt(EltNum).getInt();12436 const APSInt &HiRHS = SourceRHS.getVectorElt(EltNum + 1).getInt();12437 unsigned BitWidth = 2 * LoLHS.getBitWidth();12438 12439 switch (E->getBuiltinCallee()) {12440 case clang::X86::BI__builtin_ia32_pmaddubsw128:12441 case clang::X86::BI__builtin_ia32_pmaddubsw256:12442 case clang::X86::BI__builtin_ia32_pmaddubsw512:12443 ResultElements.push_back(APValue(12444 APSInt((LoLHS.zext(BitWidth) * LoRHS.sext(BitWidth))12445 .sadd_sat((HiLHS.zext(BitWidth) * HiRHS.sext(BitWidth))),12446 DestUnsigned)));12447 break;12448 case clang::X86::BI__builtin_ia32_pmaddwd128:12449 case clang::X86::BI__builtin_ia32_pmaddwd256:12450 case clang::X86::BI__builtin_ia32_pmaddwd512:12451 ResultElements.push_back(12452 APValue(APSInt((LoLHS.sext(BitWidth) * LoRHS.sext(BitWidth)) +12453 (HiLHS.sext(BitWidth) * HiRHS.sext(BitWidth)),12454 DestUnsigned)));12455 break;12456 }12457 }12458 12459 return Success(APValue(ResultElements.data(), ResultElements.size()), E);12460 }12461 12462 case clang::X86::BI__builtin_ia32_pmulhuw128:12463 case clang::X86::BI__builtin_ia32_pmulhuw256:12464 case clang::X86::BI__builtin_ia32_pmulhuw512:12465 return EvaluateBinOpExpr(llvm::APIntOps::mulhu);12466 12467 case clang::X86::BI__builtin_ia32_pmulhw128:12468 case clang::X86::BI__builtin_ia32_pmulhw256:12469 case clang::X86::BI__builtin_ia32_pmulhw512:12470 return EvaluateBinOpExpr(llvm::APIntOps::mulhs);12471 12472 case clang::X86::BI__builtin_ia32_psllv2di:12473 case clang::X86::BI__builtin_ia32_psllv4di:12474 case clang::X86::BI__builtin_ia32_psllv4si:12475 case clang::X86::BI__builtin_ia32_psllv8di:12476 case clang::X86::BI__builtin_ia32_psllv8hi:12477 case clang::X86::BI__builtin_ia32_psllv8si:12478 case clang::X86::BI__builtin_ia32_psllv16hi:12479 case clang::X86::BI__builtin_ia32_psllv16si:12480 case clang::X86::BI__builtin_ia32_psllv32hi:12481 case clang::X86::BI__builtin_ia32_psllwi128:12482 case clang::X86::BI__builtin_ia32_pslldi128:12483 case clang::X86::BI__builtin_ia32_psllqi128:12484 case clang::X86::BI__builtin_ia32_psllwi256:12485 case clang::X86::BI__builtin_ia32_pslldi256:12486 case clang::X86::BI__builtin_ia32_psllqi256:12487 case clang::X86::BI__builtin_ia32_psllwi512:12488 case clang::X86::BI__builtin_ia32_pslldi512:12489 case clang::X86::BI__builtin_ia32_psllqi512:12490 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {12491 if (RHS.uge(LHS.getBitWidth())) {12492 return APInt::getZero(LHS.getBitWidth());12493 }12494 return LHS.shl(RHS.getZExtValue());12495 });12496 12497 case clang::X86::BI__builtin_ia32_psrav4si:12498 case clang::X86::BI__builtin_ia32_psrav8di:12499 case clang::X86::BI__builtin_ia32_psrav8hi:12500 case clang::X86::BI__builtin_ia32_psrav8si:12501 case clang::X86::BI__builtin_ia32_psrav16hi:12502 case clang::X86::BI__builtin_ia32_psrav16si:12503 case clang::X86::BI__builtin_ia32_psrav32hi:12504 case clang::X86::BI__builtin_ia32_psravq128:12505 case clang::X86::BI__builtin_ia32_psravq256:12506 case clang::X86::BI__builtin_ia32_psrawi128:12507 case clang::X86::BI__builtin_ia32_psradi128:12508 case clang::X86::BI__builtin_ia32_psraqi128:12509 case clang::X86::BI__builtin_ia32_psrawi256:12510 case clang::X86::BI__builtin_ia32_psradi256:12511 case clang::X86::BI__builtin_ia32_psraqi256:12512 case clang::X86::BI__builtin_ia32_psrawi512:12513 case clang::X86::BI__builtin_ia32_psradi512:12514 case clang::X86::BI__builtin_ia32_psraqi512:12515 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {12516 if (RHS.uge(LHS.getBitWidth())) {12517 return LHS.ashr(LHS.getBitWidth() - 1);12518 }12519 return LHS.ashr(RHS.getZExtValue());12520 });12521 12522 case clang::X86::BI__builtin_ia32_psrlv2di:12523 case clang::X86::BI__builtin_ia32_psrlv4di:12524 case clang::X86::BI__builtin_ia32_psrlv4si:12525 case clang::X86::BI__builtin_ia32_psrlv8di:12526 case clang::X86::BI__builtin_ia32_psrlv8hi:12527 case clang::X86::BI__builtin_ia32_psrlv8si:12528 case clang::X86::BI__builtin_ia32_psrlv16hi:12529 case clang::X86::BI__builtin_ia32_psrlv16si:12530 case clang::X86::BI__builtin_ia32_psrlv32hi:12531 case clang::X86::BI__builtin_ia32_psrlwi128:12532 case clang::X86::BI__builtin_ia32_psrldi128:12533 case clang::X86::BI__builtin_ia32_psrlqi128:12534 case clang::X86::BI__builtin_ia32_psrlwi256:12535 case clang::X86::BI__builtin_ia32_psrldi256:12536 case clang::X86::BI__builtin_ia32_psrlqi256:12537 case clang::X86::BI__builtin_ia32_psrlwi512:12538 case clang::X86::BI__builtin_ia32_psrldi512:12539 case clang::X86::BI__builtin_ia32_psrlqi512:12540 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {12541 if (RHS.uge(LHS.getBitWidth())) {12542 return APInt::getZero(LHS.getBitWidth());12543 }12544 return LHS.lshr(RHS.getZExtValue());12545 });12546 case X86::BI__builtin_ia32_packsswb128:12547 case X86::BI__builtin_ia32_packsswb256:12548 case X86::BI__builtin_ia32_packsswb512:12549 case X86::BI__builtin_ia32_packssdw128:12550 case X86::BI__builtin_ia32_packssdw256:12551 case X86::BI__builtin_ia32_packssdw512:12552 return evalPackBuiltin(E, Info, Result, [](const APSInt &Src) {12553 return APSInt(Src).truncSSat(Src.getBitWidth() / 2);12554 });12555 case X86::BI__builtin_ia32_packusdw128:12556 case X86::BI__builtin_ia32_packusdw256:12557 case X86::BI__builtin_ia32_packusdw512:12558 case X86::BI__builtin_ia32_packuswb128:12559 case X86::BI__builtin_ia32_packuswb256:12560 case X86::BI__builtin_ia32_packuswb512:12561 return evalPackBuiltin(E, Info, Result, [](const APSInt &Src) {12562 unsigned DstBits = Src.getBitWidth() / 2;12563 if (Src.isNegative())12564 return APInt::getZero(DstBits);12565 if (Src.isIntN(DstBits))12566 return APInt((Src).trunc(DstBits));12567 return APInt::getAllOnes(DstBits);12568 });12569 case clang::X86::BI__builtin_ia32_selectss_128:12570 return EvalSelectScalar(4);12571 case clang::X86::BI__builtin_ia32_selectsd_128:12572 return EvalSelectScalar(2);12573 case clang::X86::BI__builtin_ia32_selectsh_128:12574 case clang::X86::BI__builtin_ia32_selectsbf_128:12575 return EvalSelectScalar(8);12576 case clang::X86::BI__builtin_ia32_pmuldq128:12577 case clang::X86::BI__builtin_ia32_pmuldq256:12578 case clang::X86::BI__builtin_ia32_pmuldq512:12579 case clang::X86::BI__builtin_ia32_pmuludq128:12580 case clang::X86::BI__builtin_ia32_pmuludq256:12581 case clang::X86::BI__builtin_ia32_pmuludq512: {12582 APValue SourceLHS, SourceRHS;12583 if (!EvaluateAsRValue(Info, E->getArg(0), SourceLHS) ||12584 !EvaluateAsRValue(Info, E->getArg(1), SourceRHS))12585 return false;12586 12587 unsigned SourceLen = SourceLHS.getVectorLength();12588 SmallVector<APValue, 4> ResultElements;12589 ResultElements.reserve(SourceLen / 2);12590 12591 for (unsigned EltNum = 0; EltNum < SourceLen; EltNum += 2) {12592 APSInt LHS = SourceLHS.getVectorElt(EltNum).getInt();12593 APSInt RHS = SourceRHS.getVectorElt(EltNum).getInt();12594 12595 switch (E->getBuiltinCallee()) {12596 case clang::X86::BI__builtin_ia32_pmuludq128:12597 case clang::X86::BI__builtin_ia32_pmuludq256:12598 case clang::X86::BI__builtin_ia32_pmuludq512:12599 ResultElements.push_back(12600 APValue(APSInt(llvm::APIntOps::muluExtended(LHS, RHS), true)));12601 break;12602 case clang::X86::BI__builtin_ia32_pmuldq128:12603 case clang::X86::BI__builtin_ia32_pmuldq256:12604 case clang::X86::BI__builtin_ia32_pmuldq512:12605 ResultElements.push_back(12606 APValue(APSInt(llvm::APIntOps::mulsExtended(LHS, RHS), false)));12607 break;12608 }12609 }12610 12611 return Success(APValue(ResultElements.data(), ResultElements.size()), E);12612 }12613 12614 case X86::BI__builtin_ia32_vpmadd52luq128:12615 case X86::BI__builtin_ia32_vpmadd52luq256:12616 case X86::BI__builtin_ia32_vpmadd52luq512: {12617 APValue A, B, C;12618 if (!EvaluateAsRValue(Info, E->getArg(0), A) ||12619 !EvaluateAsRValue(Info, E->getArg(1), B) ||12620 !EvaluateAsRValue(Info, E->getArg(2), C))12621 return false;12622 12623 unsigned ALen = A.getVectorLength();12624 SmallVector<APValue, 4> ResultElements;12625 ResultElements.reserve(ALen);12626 12627 for (unsigned EltNum = 0; EltNum < ALen; EltNum += 1) {12628 APInt AElt = A.getVectorElt(EltNum).getInt();12629 APInt BElt = B.getVectorElt(EltNum).getInt().trunc(52);12630 APInt CElt = C.getVectorElt(EltNum).getInt().trunc(52);12631 APSInt ResElt(AElt + (BElt * CElt).zext(64), false);12632 ResultElements.push_back(APValue(ResElt));12633 }12634 12635 return Success(APValue(ResultElements.data(), ResultElements.size()), E);12636 }12637 case X86::BI__builtin_ia32_vpmadd52huq128:12638 case X86::BI__builtin_ia32_vpmadd52huq256:12639 case X86::BI__builtin_ia32_vpmadd52huq512: {12640 APValue A, B, C;12641 if (!EvaluateAsRValue(Info, E->getArg(0), A) ||12642 !EvaluateAsRValue(Info, E->getArg(1), B) ||12643 !EvaluateAsRValue(Info, E->getArg(2), C))12644 return false;12645 12646 unsigned ALen = A.getVectorLength();12647 SmallVector<APValue, 4> ResultElements;12648 ResultElements.reserve(ALen);12649 12650 for (unsigned EltNum = 0; EltNum < ALen; EltNum += 1) {12651 APInt AElt = A.getVectorElt(EltNum).getInt();12652 APInt BElt = B.getVectorElt(EltNum).getInt().trunc(52);12653 APInt CElt = C.getVectorElt(EltNum).getInt().trunc(52);12654 APSInt ResElt(AElt + llvm::APIntOps::mulhu(BElt, CElt).zext(64), false);12655 ResultElements.push_back(APValue(ResElt));12656 }12657 12658 return Success(APValue(ResultElements.data(), ResultElements.size()), E);12659 }12660 12661 case clang::X86::BI__builtin_ia32_vprotbi:12662 case clang::X86::BI__builtin_ia32_vprotdi:12663 case clang::X86::BI__builtin_ia32_vprotqi:12664 case clang::X86::BI__builtin_ia32_vprotwi:12665 case clang::X86::BI__builtin_ia32_prold128:12666 case clang::X86::BI__builtin_ia32_prold256:12667 case clang::X86::BI__builtin_ia32_prold512:12668 case clang::X86::BI__builtin_ia32_prolq128:12669 case clang::X86::BI__builtin_ia32_prolq256:12670 case clang::X86::BI__builtin_ia32_prolq512:12671 return EvaluateBinOpExpr(12672 [](const APSInt &LHS, const APSInt &RHS) { return LHS.rotl(RHS); });12673 12674 case clang::X86::BI__builtin_ia32_prord128:12675 case clang::X86::BI__builtin_ia32_prord256:12676 case clang::X86::BI__builtin_ia32_prord512:12677 case clang::X86::BI__builtin_ia32_prorq128:12678 case clang::X86::BI__builtin_ia32_prorq256:12679 case clang::X86::BI__builtin_ia32_prorq512:12680 return EvaluateBinOpExpr(12681 [](const APSInt &LHS, const APSInt &RHS) { return LHS.rotr(RHS); });12682 12683 case Builtin::BI__builtin_elementwise_max:12684 case Builtin::BI__builtin_elementwise_min: {12685 APValue SourceLHS, SourceRHS;12686 if (!EvaluateAsRValue(Info, E->getArg(0), SourceLHS) ||12687 !EvaluateAsRValue(Info, E->getArg(1), SourceRHS))12688 return false;12689 12690 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();12691 12692 if (!DestEltTy->isIntegerType())12693 return false;12694 12695 unsigned SourceLen = SourceLHS.getVectorLength();12696 SmallVector<APValue, 4> ResultElements;12697 ResultElements.reserve(SourceLen);12698 12699 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {12700 APSInt LHS = SourceLHS.getVectorElt(EltNum).getInt();12701 APSInt RHS = SourceRHS.getVectorElt(EltNum).getInt();12702 switch (E->getBuiltinCallee()) {12703 case Builtin::BI__builtin_elementwise_max:12704 ResultElements.push_back(12705 APValue(APSInt(std::max(LHS, RHS),12706 DestEltTy->isUnsignedIntegerOrEnumerationType())));12707 break;12708 case Builtin::BI__builtin_elementwise_min:12709 ResultElements.push_back(12710 APValue(APSInt(std::min(LHS, RHS),12711 DestEltTy->isUnsignedIntegerOrEnumerationType())));12712 break;12713 }12714 }12715 12716 return Success(APValue(ResultElements.data(), ResultElements.size()), E);12717 }12718 case X86::BI__builtin_ia32_vpshldd128:12719 case X86::BI__builtin_ia32_vpshldd256:12720 case X86::BI__builtin_ia32_vpshldd512:12721 case X86::BI__builtin_ia32_vpshldq128:12722 case X86::BI__builtin_ia32_vpshldq256:12723 case X86::BI__builtin_ia32_vpshldq512:12724 case X86::BI__builtin_ia32_vpshldw128:12725 case X86::BI__builtin_ia32_vpshldw256:12726 case X86::BI__builtin_ia32_vpshldw512: {12727 APValue SourceHi, SourceLo, SourceAmt;12728 if (!EvaluateAsRValue(Info, E->getArg(0), SourceHi) ||12729 !EvaluateAsRValue(Info, E->getArg(1), SourceLo) ||12730 !EvaluateAsRValue(Info, E->getArg(2), SourceAmt))12731 return false;12732 12733 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();12734 unsigned SourceLen = SourceHi.getVectorLength();12735 SmallVector<APValue, 32> ResultElements;12736 ResultElements.reserve(SourceLen);12737 12738 APInt Amt = SourceAmt.getInt();12739 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {12740 APInt Hi = SourceHi.getVectorElt(EltNum).getInt();12741 APInt Lo = SourceLo.getVectorElt(EltNum).getInt();12742 APInt R = llvm::APIntOps::fshl(Hi, Lo, Amt);12743 ResultElements.push_back(12744 APValue(APSInt(R, DestEltTy->isUnsignedIntegerOrEnumerationType())));12745 }12746 12747 return Success(APValue(ResultElements.data(), ResultElements.size()), E);12748 }12749 case X86::BI__builtin_ia32_vpshrdd128:12750 case X86::BI__builtin_ia32_vpshrdd256:12751 case X86::BI__builtin_ia32_vpshrdd512:12752 case X86::BI__builtin_ia32_vpshrdq128:12753 case X86::BI__builtin_ia32_vpshrdq256:12754 case X86::BI__builtin_ia32_vpshrdq512:12755 case X86::BI__builtin_ia32_vpshrdw128:12756 case X86::BI__builtin_ia32_vpshrdw256:12757 case X86::BI__builtin_ia32_vpshrdw512: {12758 // NOTE: Reversed Hi/Lo operands.12759 APValue SourceHi, SourceLo, SourceAmt;12760 if (!EvaluateAsRValue(Info, E->getArg(0), SourceLo) ||12761 !EvaluateAsRValue(Info, E->getArg(1), SourceHi) ||12762 !EvaluateAsRValue(Info, E->getArg(2), SourceAmt))12763 return false;12764 12765 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();12766 unsigned SourceLen = SourceHi.getVectorLength();12767 SmallVector<APValue, 32> ResultElements;12768 ResultElements.reserve(SourceLen);12769 12770 APInt Amt = SourceAmt.getInt();12771 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {12772 APInt Hi = SourceHi.getVectorElt(EltNum).getInt();12773 APInt Lo = SourceLo.getVectorElt(EltNum).getInt();12774 APInt R = llvm::APIntOps::fshr(Hi, Lo, Amt);12775 ResultElements.push_back(12776 APValue(APSInt(R, DestEltTy->isUnsignedIntegerOrEnumerationType())));12777 }12778 12779 return Success(APValue(ResultElements.data(), ResultElements.size()), E);12780 }12781 case X86::BI__builtin_ia32_vpconflictsi_128:12782 case X86::BI__builtin_ia32_vpconflictsi_256:12783 case X86::BI__builtin_ia32_vpconflictsi_512:12784 case X86::BI__builtin_ia32_vpconflictdi_128:12785 case X86::BI__builtin_ia32_vpconflictdi_256:12786 case X86::BI__builtin_ia32_vpconflictdi_512: {12787 APValue Source;12788 12789 if (!EvaluateAsRValue(Info, E->getArg(0), Source))12790 return false;12791 12792 unsigned SourceLen = Source.getVectorLength();12793 SmallVector<APValue, 32> ResultElements;12794 ResultElements.reserve(SourceLen);12795 12796 const auto *VecT = E->getType()->castAs<VectorType>();12797 bool DestUnsigned =12798 VecT->getElementType()->isUnsignedIntegerOrEnumerationType();12799 12800 for (unsigned I = 0; I != SourceLen; ++I) {12801 const APValue &EltI = Source.getVectorElt(I);12802 12803 APInt ConflictMask(EltI.getInt().getBitWidth(), 0);12804 for (unsigned J = 0; J != I; ++J) {12805 const APValue &EltJ = Source.getVectorElt(J);12806 ConflictMask.setBitVal(J, EltI.getInt() == EltJ.getInt());12807 }12808 ResultElements.push_back(APValue(APSInt(ConflictMask, DestUnsigned)));12809 }12810 return Success(APValue(ResultElements.data(), ResultElements.size()), E);12811 }12812 case X86::BI__builtin_ia32_blendpd:12813 case X86::BI__builtin_ia32_blendpd256:12814 case X86::BI__builtin_ia32_blendps:12815 case X86::BI__builtin_ia32_blendps256:12816 case X86::BI__builtin_ia32_pblendw128:12817 case X86::BI__builtin_ia32_pblendw256:12818 case X86::BI__builtin_ia32_pblendd128:12819 case X86::BI__builtin_ia32_pblendd256: {12820 APValue SourceF, SourceT, SourceC;12821 if (!EvaluateAsRValue(Info, E->getArg(0), SourceF) ||12822 !EvaluateAsRValue(Info, E->getArg(1), SourceT) ||12823 !EvaluateAsRValue(Info, E->getArg(2), SourceC))12824 return false;12825 12826 const APInt &C = SourceC.getInt();12827 unsigned SourceLen = SourceF.getVectorLength();12828 SmallVector<APValue, 32> ResultElements;12829 ResultElements.reserve(SourceLen);12830 for (unsigned EltNum = 0; EltNum != SourceLen; ++EltNum) {12831 const APValue &F = SourceF.getVectorElt(EltNum);12832 const APValue &T = SourceT.getVectorElt(EltNum);12833 ResultElements.push_back(C[EltNum % 8] ? T : F);12834 }12835 12836 return Success(APValue(ResultElements.data(), ResultElements.size()), E);12837 }12838 12839 case X86::BI__builtin_ia32_psignb128:12840 case X86::BI__builtin_ia32_psignb256:12841 case X86::BI__builtin_ia32_psignw128:12842 case X86::BI__builtin_ia32_psignw256:12843 case X86::BI__builtin_ia32_psignd128:12844 case X86::BI__builtin_ia32_psignd256:12845 return EvaluateBinOpExpr([](const APInt &AElem, const APInt &BElem) {12846 if (BElem.isZero())12847 return APInt::getZero(AElem.getBitWidth());12848 if (BElem.isNegative())12849 return -AElem;12850 return AElem;12851 });12852 12853 case X86::BI__builtin_ia32_blendvpd:12854 case X86::BI__builtin_ia32_blendvpd256:12855 case X86::BI__builtin_ia32_blendvps:12856 case X86::BI__builtin_ia32_blendvps256:12857 case X86::BI__builtin_ia32_pblendvb128:12858 case X86::BI__builtin_ia32_pblendvb256: {12859 // SSE blendv by mask signbit: "Result = C[] < 0 ? T[] : F[]".12860 APValue SourceF, SourceT, SourceC;12861 if (!EvaluateAsRValue(Info, E->getArg(0), SourceF) ||12862 !EvaluateAsRValue(Info, E->getArg(1), SourceT) ||12863 !EvaluateAsRValue(Info, E->getArg(2), SourceC))12864 return false;12865 12866 unsigned SourceLen = SourceF.getVectorLength();12867 SmallVector<APValue, 32> ResultElements;12868 ResultElements.reserve(SourceLen);12869 12870 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {12871 const APValue &F = SourceF.getVectorElt(EltNum);12872 const APValue &T = SourceT.getVectorElt(EltNum);12873 const APValue &C = SourceC.getVectorElt(EltNum);12874 APInt M = C.isInt() ? (APInt)C.getInt() : C.getFloat().bitcastToAPInt();12875 ResultElements.push_back(M.isNegative() ? T : F);12876 }12877 12878 return Success(APValue(ResultElements.data(), ResultElements.size()), E);12879 }12880 case X86::BI__builtin_ia32_selectb_128:12881 case X86::BI__builtin_ia32_selectb_256:12882 case X86::BI__builtin_ia32_selectb_512:12883 case X86::BI__builtin_ia32_selectw_128:12884 case X86::BI__builtin_ia32_selectw_256:12885 case X86::BI__builtin_ia32_selectw_512:12886 case X86::BI__builtin_ia32_selectd_128:12887 case X86::BI__builtin_ia32_selectd_256:12888 case X86::BI__builtin_ia32_selectd_512:12889 case X86::BI__builtin_ia32_selectq_128:12890 case X86::BI__builtin_ia32_selectq_256:12891 case X86::BI__builtin_ia32_selectq_512:12892 case X86::BI__builtin_ia32_selectph_128:12893 case X86::BI__builtin_ia32_selectph_256:12894 case X86::BI__builtin_ia32_selectph_512:12895 case X86::BI__builtin_ia32_selectpbf_128:12896 case X86::BI__builtin_ia32_selectpbf_256:12897 case X86::BI__builtin_ia32_selectpbf_512:12898 case X86::BI__builtin_ia32_selectps_128:12899 case X86::BI__builtin_ia32_selectps_256:12900 case X86::BI__builtin_ia32_selectps_512:12901 case X86::BI__builtin_ia32_selectpd_128:12902 case X86::BI__builtin_ia32_selectpd_256:12903 case X86::BI__builtin_ia32_selectpd_512: {12904 // AVX512 predicated move: "Result = Mask[] ? LHS[] : RHS[]".12905 APValue SourceMask, SourceLHS, SourceRHS;12906 if (!EvaluateAsRValue(Info, E->getArg(0), SourceMask) ||12907 !EvaluateAsRValue(Info, E->getArg(1), SourceLHS) ||12908 !EvaluateAsRValue(Info, E->getArg(2), SourceRHS))12909 return false;12910 12911 APSInt Mask = SourceMask.getInt();12912 unsigned SourceLen = SourceLHS.getVectorLength();12913 SmallVector<APValue, 4> ResultElements;12914 ResultElements.reserve(SourceLen);12915 12916 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {12917 const APValue &LHS = SourceLHS.getVectorElt(EltNum);12918 const APValue &RHS = SourceRHS.getVectorElt(EltNum);12919 ResultElements.push_back(Mask[EltNum] ? LHS : RHS);12920 }12921 12922 return Success(APValue(ResultElements.data(), ResultElements.size()), E);12923 }12924 case X86::BI__builtin_ia32_shufps:12925 case X86::BI__builtin_ia32_shufps256:12926 case X86::BI__builtin_ia32_shufps512: {12927 APValue R;12928 if (!evalShuffleGeneric(12929 Info, E, R,12930 [](unsigned DstIdx,12931 unsigned ShuffleMask) -> std::pair<unsigned, int> {12932 constexpr unsigned LaneBits = 128u;12933 unsigned NumElemPerLane = LaneBits / 32;12934 unsigned NumSelectableElems = NumElemPerLane / 2;12935 unsigned BitsPerElem = 2;12936 unsigned IndexMask = (1u << BitsPerElem) - 1;12937 unsigned MaskBits = 8;12938 unsigned Lane = DstIdx / NumElemPerLane;12939 unsigned ElemInLane = DstIdx % NumElemPerLane;12940 unsigned LaneOffset = Lane * NumElemPerLane;12941 unsigned BitIndex = (DstIdx * BitsPerElem) % MaskBits;12942 unsigned SrcIdx = (ElemInLane < NumSelectableElems) ? 0 : 1;12943 unsigned Index = (ShuffleMask >> BitIndex) & IndexMask;12944 return {SrcIdx, static_cast<int>(LaneOffset + Index)};12945 }))12946 return false;12947 return Success(R, E);12948 }12949 case X86::BI__builtin_ia32_shufpd:12950 case X86::BI__builtin_ia32_shufpd256:12951 case X86::BI__builtin_ia32_shufpd512: {12952 APValue R;12953 if (!evalShuffleGeneric(12954 Info, E, R,12955 [](unsigned DstIdx,12956 unsigned ShuffleMask) -> std::pair<unsigned, int> {12957 constexpr unsigned LaneBits = 128u;12958 unsigned NumElemPerLane = LaneBits / 64;12959 unsigned NumSelectableElems = NumElemPerLane / 2;12960 unsigned BitsPerElem = 1;12961 unsigned IndexMask = (1u << BitsPerElem) - 1;12962 unsigned MaskBits = 8;12963 unsigned Lane = DstIdx / NumElemPerLane;12964 unsigned ElemInLane = DstIdx % NumElemPerLane;12965 unsigned LaneOffset = Lane * NumElemPerLane;12966 unsigned BitIndex = (DstIdx * BitsPerElem) % MaskBits;12967 unsigned SrcIdx = (ElemInLane < NumSelectableElems) ? 0 : 1;12968 unsigned Index = (ShuffleMask >> BitIndex) & IndexMask;12969 return {SrcIdx, static_cast<int>(LaneOffset + Index)};12970 }))12971 return false;12972 return Success(R, E);12973 }12974 case X86::BI__builtin_ia32_insertps128: {12975 APValue R;12976 if (!evalShuffleGeneric(12977 Info, E, R,12978 [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {12979 // Bits [3:0]: zero mask - if bit is set, zero this element12980 if ((Mask & (1 << DstIdx)) != 0) {12981 return {0, -1};12982 }12983 // Bits [7:6]: select element from source vector Y (0-3)12984 // Bits [5:4]: select destination position (0-3)12985 unsigned SrcElem = (Mask >> 6) & 0x3;12986 unsigned DstElem = (Mask >> 4) & 0x3;12987 if (DstIdx == DstElem) {12988 // Insert element from source vector (B) at this position12989 return {1, static_cast<int>(SrcElem)};12990 } else {12991 // Copy from destination vector (A)12992 return {0, static_cast<int>(DstIdx)};12993 }12994 }))12995 return false;12996 return Success(R, E);12997 }12998 case X86::BI__builtin_ia32_pshufb128:12999 case X86::BI__builtin_ia32_pshufb256:13000 case X86::BI__builtin_ia32_pshufb512: {13001 APValue R;13002 if (!evalShuffleGeneric(13003 Info, E, R,13004 [](unsigned DstIdx,13005 unsigned ShuffleMask) -> std::pair<unsigned, int> {13006 uint8_t Ctlb = static_cast<uint8_t>(ShuffleMask);13007 if (Ctlb & 0x80)13008 return std::make_pair(0, -1);13009 13010 unsigned LaneBase = (DstIdx / 16) * 16;13011 unsigned SrcOffset = Ctlb & 0x0F;13012 unsigned SrcIdx = LaneBase + SrcOffset;13013 return std::make_pair(0, static_cast<int>(SrcIdx));13014 }))13015 return false;13016 return Success(R, E);13017 }13018 13019 case X86::BI__builtin_ia32_pshuflw:13020 case X86::BI__builtin_ia32_pshuflw256:13021 case X86::BI__builtin_ia32_pshuflw512: {13022 APValue R;13023 if (!evalShuffleGeneric(13024 Info, E, R,13025 [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {13026 constexpr unsigned LaneBits = 128u;13027 constexpr unsigned ElemBits = 16u;13028 constexpr unsigned LaneElts = LaneBits / ElemBits;13029 constexpr unsigned HalfSize = 4;13030 unsigned LaneBase = (DstIdx / LaneElts) * LaneElts;13031 unsigned LaneIdx = DstIdx % LaneElts;13032 if (LaneIdx < HalfSize) {13033 unsigned Sel = (Mask >> (2 * LaneIdx)) & 0x3;13034 return std::make_pair(0, static_cast<int>(LaneBase + Sel));13035 }13036 return std::make_pair(0, static_cast<int>(DstIdx));13037 }))13038 return false;13039 return Success(R, E);13040 }13041 13042 case X86::BI__builtin_ia32_pshufhw:13043 case X86::BI__builtin_ia32_pshufhw256:13044 case X86::BI__builtin_ia32_pshufhw512: {13045 APValue R;13046 if (!evalShuffleGeneric(13047 Info, E, R,13048 [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {13049 constexpr unsigned LaneBits = 128u;13050 constexpr unsigned ElemBits = 16u;13051 constexpr unsigned LaneElts = LaneBits / ElemBits;13052 constexpr unsigned HalfSize = 4;13053 unsigned LaneBase = (DstIdx / LaneElts) * LaneElts;13054 unsigned LaneIdx = DstIdx % LaneElts;13055 if (LaneIdx >= HalfSize) {13056 unsigned Rel = LaneIdx - HalfSize;13057 unsigned Sel = (Mask >> (2 * Rel)) & 0x3;13058 return std::make_pair(13059 0, static_cast<int>(LaneBase + HalfSize + Sel));13060 }13061 return std::make_pair(0, static_cast<int>(DstIdx));13062 }))13063 return false;13064 return Success(R, E);13065 }13066 13067 case X86::BI__builtin_ia32_pshufd:13068 case X86::BI__builtin_ia32_pshufd256:13069 case X86::BI__builtin_ia32_pshufd512:13070 case X86::BI__builtin_ia32_vpermilps:13071 case X86::BI__builtin_ia32_vpermilps256:13072 case X86::BI__builtin_ia32_vpermilps512: {13073 APValue R;13074 if (!evalShuffleGeneric(13075 Info, E, R,13076 [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {13077 constexpr unsigned LaneBits = 128u;13078 constexpr unsigned ElemBits = 32u;13079 constexpr unsigned LaneElts = LaneBits / ElemBits;13080 unsigned LaneBase = (DstIdx / LaneElts) * LaneElts;13081 unsigned LaneIdx = DstIdx % LaneElts;13082 unsigned Sel = (Mask >> (2 * LaneIdx)) & 0x3;13083 return std::make_pair(0, static_cast<int>(LaneBase + Sel));13084 }))13085 return false;13086 return Success(R, E);13087 }13088 13089 case X86::BI__builtin_ia32_vpermilvarpd:13090 case X86::BI__builtin_ia32_vpermilvarpd256:13091 case X86::BI__builtin_ia32_vpermilvarpd512: {13092 APValue R;13093 if (!evalShuffleGeneric(13094 Info, E, R,13095 [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {13096 unsigned NumElemPerLane = 2;13097 unsigned Lane = DstIdx / NumElemPerLane;13098 unsigned Offset = Mask & 0b10 ? 1 : 0;13099 return std::make_pair(13100 0, static_cast<int>(Lane * NumElemPerLane + Offset));13101 }))13102 return false;13103 return Success(R, E);13104 }13105 13106 case X86::BI__builtin_ia32_vpermilpd:13107 case X86::BI__builtin_ia32_vpermilpd256:13108 case X86::BI__builtin_ia32_vpermilpd512: {13109 APValue R;13110 if (!evalShuffleGeneric(Info, E, R, [](unsigned DstIdx, unsigned Control) {13111 unsigned NumElemPerLane = 2;13112 unsigned BitsPerElem = 1;13113 unsigned MaskBits = 8;13114 unsigned IndexMask = 0x1;13115 unsigned Lane = DstIdx / NumElemPerLane;13116 unsigned LaneOffset = Lane * NumElemPerLane;13117 unsigned BitIndex = (DstIdx * BitsPerElem) % MaskBits;13118 unsigned Index = (Control >> BitIndex) & IndexMask;13119 return std::make_pair(0, static_cast<int>(LaneOffset + Index));13120 }))13121 return false;13122 return Success(R, E);13123 }13124 13125 case X86::BI__builtin_ia32_vpermilvarps:13126 case X86::BI__builtin_ia32_vpermilvarps256:13127 case X86::BI__builtin_ia32_vpermilvarps512: {13128 APValue R;13129 if (!evalShuffleGeneric(13130 Info, E, R,13131 [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {13132 unsigned NumElemPerLane = 4;13133 unsigned Lane = DstIdx / NumElemPerLane;13134 unsigned Offset = Mask & 0b11;13135 return std::make_pair(13136 0, static_cast<int>(Lane * NumElemPerLane + Offset));13137 }))13138 return false;13139 return Success(R, E);13140 }13141 13142 case X86::BI__builtin_ia32_vpmultishiftqb128:13143 case X86::BI__builtin_ia32_vpmultishiftqb256:13144 case X86::BI__builtin_ia32_vpmultishiftqb512: {13145 assert(E->getNumArgs() == 2);13146 13147 APValue A, B;13148 if (!Evaluate(A, Info, E->getArg(0)) || !Evaluate(B, Info, E->getArg(1)))13149 return false;13150 13151 assert(A.getVectorLength() == B.getVectorLength());13152 unsigned NumBytesInQWord = 8;13153 unsigned NumBitsInByte = 8;13154 unsigned NumBytes = A.getVectorLength();13155 unsigned NumQWords = NumBytes / NumBytesInQWord;13156 SmallVector<APValue, 64> Result;13157 Result.reserve(NumBytes);13158 13159 for (unsigned QWordId = 0; QWordId != NumQWords; ++QWordId) {13160 APInt BQWord(64, 0);13161 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {13162 unsigned Idx = QWordId * NumBytesInQWord + ByteIdx;13163 uint64_t Byte = B.getVectorElt(Idx).getInt().getZExtValue();13164 BQWord.insertBits(APInt(8, Byte & 0xFF), ByteIdx * NumBitsInByte);13165 }13166 13167 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {13168 unsigned Idx = QWordId * NumBytesInQWord + ByteIdx;13169 uint64_t Ctrl = A.getVectorElt(Idx).getInt().getZExtValue() & 0x3F;13170 13171 APInt Byte(8, 0);13172 for (unsigned BitIdx = 0; BitIdx != NumBitsInByte; ++BitIdx) {13173 Byte.setBitVal(BitIdx, BQWord[(Ctrl + BitIdx) & 0x3F]);13174 }13175 Result.push_back(APValue(APSInt(Byte, /*isUnsigned*/ true)));13176 }13177 }13178 return Success(APValue(Result.data(), Result.size()), E);13179 }13180 13181 case X86::BI__builtin_ia32_phminposuw128: {13182 APValue Source;13183 if (!Evaluate(Source, Info, E->getArg(0)))13184 return false;13185 unsigned SourceLen = Source.getVectorLength();13186 const VectorType *VT = E->getArg(0)->getType()->castAs<VectorType>();13187 QualType ElemQT = VT->getElementType();13188 unsigned ElemBitWidth = Info.Ctx.getTypeSize(ElemQT);13189 13190 APInt MinIndex(ElemBitWidth, 0);13191 APInt MinVal = Source.getVectorElt(0).getInt();13192 for (unsigned I = 1; I != SourceLen; ++I) {13193 APInt Val = Source.getVectorElt(I).getInt();13194 if (MinVal.ugt(Val)) {13195 MinVal = Val;13196 MinIndex = I;13197 }13198 }13199 13200 bool ResultUnsigned = E->getCallReturnType(Info.Ctx)13201 ->castAs<VectorType>()13202 ->getElementType()13203 ->isUnsignedIntegerOrEnumerationType();13204 13205 SmallVector<APValue, 8> Result;13206 Result.reserve(SourceLen);13207 Result.emplace_back(APSInt(MinVal, ResultUnsigned));13208 Result.emplace_back(APSInt(MinIndex, ResultUnsigned));13209 for (unsigned I = 0; I != SourceLen - 2; ++I) {13210 Result.emplace_back(APSInt(APInt(ElemBitWidth, 0), ResultUnsigned));13211 }13212 return Success(APValue(Result.data(), Result.size()), E);13213 }13214 13215 case X86::BI__builtin_ia32_psraq128:13216 case X86::BI__builtin_ia32_psraq256:13217 case X86::BI__builtin_ia32_psraq512:13218 case X86::BI__builtin_ia32_psrad128:13219 case X86::BI__builtin_ia32_psrad256:13220 case X86::BI__builtin_ia32_psrad512:13221 case X86::BI__builtin_ia32_psraw128:13222 case X86::BI__builtin_ia32_psraw256:13223 case X86::BI__builtin_ia32_psraw512: {13224 APValue R;13225 if (!evalShiftWithCount(13226 Info, E, R,13227 [](const APInt &Elt, uint64_t Count) { return Elt.ashr(Count); },13228 [](const APInt &Elt, unsigned Width) {13229 return Elt.ashr(Width - 1);13230 }))13231 return false;13232 return Success(R, E);13233 }13234 13235 case X86::BI__builtin_ia32_psllq128:13236 case X86::BI__builtin_ia32_psllq256:13237 case X86::BI__builtin_ia32_psllq512:13238 case X86::BI__builtin_ia32_pslld128:13239 case X86::BI__builtin_ia32_pslld256:13240 case X86::BI__builtin_ia32_pslld512:13241 case X86::BI__builtin_ia32_psllw128:13242 case X86::BI__builtin_ia32_psllw256:13243 case X86::BI__builtin_ia32_psllw512: {13244 APValue R;13245 if (!evalShiftWithCount(13246 Info, E, R,13247 [](const APInt &Elt, uint64_t Count) { return Elt.shl(Count); },13248 [](const APInt &Elt, unsigned Width) {13249 return APInt::getZero(Width);13250 }))13251 return false;13252 return Success(R, E);13253 }13254 13255 case X86::BI__builtin_ia32_psrlq128:13256 case X86::BI__builtin_ia32_psrlq256:13257 case X86::BI__builtin_ia32_psrlq512:13258 case X86::BI__builtin_ia32_psrld128:13259 case X86::BI__builtin_ia32_psrld256:13260 case X86::BI__builtin_ia32_psrld512:13261 case X86::BI__builtin_ia32_psrlw128:13262 case X86::BI__builtin_ia32_psrlw256:13263 case X86::BI__builtin_ia32_psrlw512: {13264 APValue R;13265 if (!evalShiftWithCount(13266 Info, E, R,13267 [](const APInt &Elt, uint64_t Count) { return Elt.lshr(Count); },13268 [](const APInt &Elt, unsigned Width) {13269 return APInt::getZero(Width);13270 }))13271 return false;13272 return Success(R, E);13273 }13274 13275 case X86::BI__builtin_ia32_pternlogd128_mask:13276 case X86::BI__builtin_ia32_pternlogd256_mask:13277 case X86::BI__builtin_ia32_pternlogd512_mask:13278 case X86::BI__builtin_ia32_pternlogq128_mask:13279 case X86::BI__builtin_ia32_pternlogq256_mask:13280 case X86::BI__builtin_ia32_pternlogq512_mask: {13281 APValue AValue, BValue, CValue, ImmValue, UValue;13282 if (!EvaluateAsRValue(Info, E->getArg(0), AValue) ||13283 !EvaluateAsRValue(Info, E->getArg(1), BValue) ||13284 !EvaluateAsRValue(Info, E->getArg(2), CValue) ||13285 !EvaluateAsRValue(Info, E->getArg(3), ImmValue) ||13286 !EvaluateAsRValue(Info, E->getArg(4), UValue))13287 return false;13288 13289 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();13290 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();13291 APInt Imm = ImmValue.getInt();13292 APInt U = UValue.getInt();13293 unsigned ResultLen = AValue.getVectorLength();13294 SmallVector<APValue, 16> ResultElements;13295 ResultElements.reserve(ResultLen);13296 13297 for (unsigned EltNum = 0; EltNum < ResultLen; ++EltNum) {13298 APInt ALane = AValue.getVectorElt(EltNum).getInt();13299 APInt BLane = BValue.getVectorElt(EltNum).getInt();13300 APInt CLane = CValue.getVectorElt(EltNum).getInt();13301 13302 if (U[EltNum]) {13303 unsigned BitWidth = ALane.getBitWidth();13304 APInt ResLane(BitWidth, 0);13305 13306 for (unsigned Bit = 0; Bit < BitWidth; ++Bit) {13307 unsigned ABit = ALane[Bit];13308 unsigned BBit = BLane[Bit];13309 unsigned CBit = CLane[Bit];13310 13311 unsigned Idx = (ABit << 2) | (BBit << 1) | CBit;13312 ResLane.setBitVal(Bit, Imm[Idx]);13313 }13314 ResultElements.push_back(APValue(APSInt(ResLane, DestUnsigned)));13315 } else {13316 ResultElements.push_back(APValue(APSInt(ALane, DestUnsigned)));13317 }13318 }13319 return Success(APValue(ResultElements.data(), ResultElements.size()), E);13320 }13321 case X86::BI__builtin_ia32_pternlogd128_maskz:13322 case X86::BI__builtin_ia32_pternlogd256_maskz:13323 case X86::BI__builtin_ia32_pternlogd512_maskz:13324 case X86::BI__builtin_ia32_pternlogq128_maskz:13325 case X86::BI__builtin_ia32_pternlogq256_maskz:13326 case X86::BI__builtin_ia32_pternlogq512_maskz: {13327 APValue AValue, BValue, CValue, ImmValue, UValue;13328 if (!EvaluateAsRValue(Info, E->getArg(0), AValue) ||13329 !EvaluateAsRValue(Info, E->getArg(1), BValue) ||13330 !EvaluateAsRValue(Info, E->getArg(2), CValue) ||13331 !EvaluateAsRValue(Info, E->getArg(3), ImmValue) ||13332 !EvaluateAsRValue(Info, E->getArg(4), UValue))13333 return false;13334 13335 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();13336 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();13337 APInt Imm = ImmValue.getInt();13338 APInt U = UValue.getInt();13339 unsigned ResultLen = AValue.getVectorLength();13340 SmallVector<APValue, 16> ResultElements;13341 ResultElements.reserve(ResultLen);13342 13343 for (unsigned EltNum = 0; EltNum < ResultLen; ++EltNum) {13344 APInt ALane = AValue.getVectorElt(EltNum).getInt();13345 APInt BLane = BValue.getVectorElt(EltNum).getInt();13346 APInt CLane = CValue.getVectorElt(EltNum).getInt();13347 13348 unsigned BitWidth = ALane.getBitWidth();13349 APInt ResLane(BitWidth, 0);13350 13351 if (U[EltNum]) {13352 for (unsigned Bit = 0; Bit < BitWidth; ++Bit) {13353 unsigned ABit = ALane[Bit];13354 unsigned BBit = BLane[Bit];13355 unsigned CBit = CLane[Bit];13356 13357 unsigned Idx = (ABit << 2) | (BBit << 1) | CBit;13358 ResLane.setBitVal(Bit, Imm[Idx]);13359 }13360 }13361 ResultElements.push_back(APValue(APSInt(ResLane, DestUnsigned)));13362 }13363 return Success(APValue(ResultElements.data(), ResultElements.size()), E);13364 }13365 13366 case Builtin::BI__builtin_elementwise_clzg:13367 case Builtin::BI__builtin_elementwise_ctzg: {13368 APValue SourceLHS;13369 std::optional<APValue> Fallback;13370 if (!EvaluateAsRValue(Info, E->getArg(0), SourceLHS))13371 return false;13372 if (E->getNumArgs() > 1) {13373 APValue FallbackTmp;13374 if (!EvaluateAsRValue(Info, E->getArg(1), FallbackTmp))13375 return false;13376 Fallback = FallbackTmp;13377 }13378 13379 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();13380 unsigned SourceLen = SourceLHS.getVectorLength();13381 SmallVector<APValue, 4> ResultElements;13382 ResultElements.reserve(SourceLen);13383 13384 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {13385 APSInt LHS = SourceLHS.getVectorElt(EltNum).getInt();13386 if (!LHS) {13387 // Without a fallback, a zero element is undefined13388 if (!Fallback) {13389 Info.FFDiag(E, diag::note_constexpr_countzeroes_zero)13390 << /*IsTrailing=*/(E->getBuiltinCallee() ==13391 Builtin::BI__builtin_elementwise_ctzg);13392 return false;13393 }13394 ResultElements.push_back(Fallback->getVectorElt(EltNum));13395 continue;13396 }13397 switch (E->getBuiltinCallee()) {13398 case Builtin::BI__builtin_elementwise_clzg:13399 ResultElements.push_back(APValue(13400 APSInt(APInt(Info.Ctx.getIntWidth(DestEltTy), LHS.countl_zero()),13401 DestEltTy->isUnsignedIntegerOrEnumerationType())));13402 break;13403 case Builtin::BI__builtin_elementwise_ctzg:13404 ResultElements.push_back(APValue(13405 APSInt(APInt(Info.Ctx.getIntWidth(DestEltTy), LHS.countr_zero()),13406 DestEltTy->isUnsignedIntegerOrEnumerationType())));13407 break;13408 }13409 }13410 13411 return Success(APValue(ResultElements.data(), ResultElements.size()), E);13412 }13413 13414 case Builtin::BI__builtin_elementwise_fma: {13415 APValue SourceX, SourceY, SourceZ;13416 if (!EvaluateAsRValue(Info, E->getArg(0), SourceX) ||13417 !EvaluateAsRValue(Info, E->getArg(1), SourceY) ||13418 !EvaluateAsRValue(Info, E->getArg(2), SourceZ))13419 return false;13420 13421 unsigned SourceLen = SourceX.getVectorLength();13422 SmallVector<APValue> ResultElements;13423 ResultElements.reserve(SourceLen);13424 llvm::RoundingMode RM = getActiveRoundingMode(getEvalInfo(), E);13425 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {13426 const APFloat &X = SourceX.getVectorElt(EltNum).getFloat();13427 const APFloat &Y = SourceY.getVectorElt(EltNum).getFloat();13428 const APFloat &Z = SourceZ.getVectorElt(EltNum).getFloat();13429 APFloat Result(X);13430 (void)Result.fusedMultiplyAdd(Y, Z, RM);13431 ResultElements.push_back(APValue(Result));13432 }13433 return Success(APValue(ResultElements.data(), ResultElements.size()), E);13434 }13435 13436 case clang::X86::BI__builtin_ia32_phaddw128:13437 case clang::X86::BI__builtin_ia32_phaddw256:13438 case clang::X86::BI__builtin_ia32_phaddd128:13439 case clang::X86::BI__builtin_ia32_phaddd256:13440 case clang::X86::BI__builtin_ia32_phaddsw128:13441 case clang::X86::BI__builtin_ia32_phaddsw256:13442 13443 case clang::X86::BI__builtin_ia32_phsubw128:13444 case clang::X86::BI__builtin_ia32_phsubw256:13445 case clang::X86::BI__builtin_ia32_phsubd128:13446 case clang::X86::BI__builtin_ia32_phsubd256:13447 case clang::X86::BI__builtin_ia32_phsubsw128:13448 case clang::X86::BI__builtin_ia32_phsubsw256: {13449 APValue SourceLHS, SourceRHS;13450 if (!EvaluateAsRValue(Info, E->getArg(0), SourceLHS) ||13451 !EvaluateAsRValue(Info, E->getArg(1), SourceRHS))13452 return false;13453 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();13454 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();13455 13456 unsigned NumElts = SourceLHS.getVectorLength();13457 unsigned EltBits = Info.Ctx.getIntWidth(DestEltTy);13458 unsigned EltsPerLane = 128 / EltBits;13459 SmallVector<APValue, 4> ResultElements;13460 ResultElements.reserve(NumElts);13461 13462 for (unsigned LaneStart = 0; LaneStart != NumElts;13463 LaneStart += EltsPerLane) {13464 for (unsigned I = 0; I != EltsPerLane; I += 2) {13465 APSInt LHSA = SourceLHS.getVectorElt(LaneStart + I).getInt();13466 APSInt LHSB = SourceLHS.getVectorElt(LaneStart + I + 1).getInt();13467 switch (E->getBuiltinCallee()) {13468 case clang::X86::BI__builtin_ia32_phaddw128:13469 case clang::X86::BI__builtin_ia32_phaddw256:13470 case clang::X86::BI__builtin_ia32_phaddd128:13471 case clang::X86::BI__builtin_ia32_phaddd256: {13472 APSInt Res(LHSA + LHSB, DestUnsigned);13473 ResultElements.push_back(APValue(Res));13474 break;13475 }13476 case clang::X86::BI__builtin_ia32_phaddsw128:13477 case clang::X86::BI__builtin_ia32_phaddsw256: {13478 APSInt Res(LHSA.sadd_sat(LHSB));13479 ResultElements.push_back(APValue(Res));13480 break;13481 }13482 case clang::X86::BI__builtin_ia32_phsubw128:13483 case clang::X86::BI__builtin_ia32_phsubw256:13484 case clang::X86::BI__builtin_ia32_phsubd128:13485 case clang::X86::BI__builtin_ia32_phsubd256: {13486 APSInt Res(LHSA - LHSB, DestUnsigned);13487 ResultElements.push_back(APValue(Res));13488 break;13489 }13490 case clang::X86::BI__builtin_ia32_phsubsw128:13491 case clang::X86::BI__builtin_ia32_phsubsw256: {13492 APSInt Res(LHSA.ssub_sat(LHSB));13493 ResultElements.push_back(APValue(Res));13494 break;13495 }13496 }13497 }13498 for (unsigned I = 0; I != EltsPerLane; I += 2) {13499 APSInt RHSA = SourceRHS.getVectorElt(LaneStart + I).getInt();13500 APSInt RHSB = SourceRHS.getVectorElt(LaneStart + I + 1).getInt();13501 switch (E->getBuiltinCallee()) {13502 case clang::X86::BI__builtin_ia32_phaddw128:13503 case clang::X86::BI__builtin_ia32_phaddw256:13504 case clang::X86::BI__builtin_ia32_phaddd128:13505 case clang::X86::BI__builtin_ia32_phaddd256: {13506 APSInt Res(RHSA + RHSB, DestUnsigned);13507 ResultElements.push_back(APValue(Res));13508 break;13509 }13510 case clang::X86::BI__builtin_ia32_phaddsw128:13511 case clang::X86::BI__builtin_ia32_phaddsw256: {13512 APSInt Res(RHSA.sadd_sat(RHSB));13513 ResultElements.push_back(APValue(Res));13514 break;13515 }13516 case clang::X86::BI__builtin_ia32_phsubw128:13517 case clang::X86::BI__builtin_ia32_phsubw256:13518 case clang::X86::BI__builtin_ia32_phsubd128:13519 case clang::X86::BI__builtin_ia32_phsubd256: {13520 APSInt Res(RHSA - RHSB, DestUnsigned);13521 ResultElements.push_back(APValue(Res));13522 break;13523 }13524 case clang::X86::BI__builtin_ia32_phsubsw128:13525 case clang::X86::BI__builtin_ia32_phsubsw256: {13526 APSInt Res(RHSA.ssub_sat(RHSB));13527 ResultElements.push_back(APValue(Res));13528 break;13529 }13530 }13531 }13532 }13533 return Success(APValue(ResultElements.data(), ResultElements.size()), E);13534 }13535 case clang::X86::BI__builtin_ia32_haddpd:13536 case clang::X86::BI__builtin_ia32_haddps:13537 case clang::X86::BI__builtin_ia32_haddps256:13538 case clang::X86::BI__builtin_ia32_haddpd256:13539 case clang::X86::BI__builtin_ia32_hsubpd:13540 case clang::X86::BI__builtin_ia32_hsubps:13541 case clang::X86::BI__builtin_ia32_hsubps256:13542 case clang::X86::BI__builtin_ia32_hsubpd256: {13543 APValue SourceLHS, SourceRHS;13544 if (!EvaluateAsRValue(Info, E->getArg(0), SourceLHS) ||13545 !EvaluateAsRValue(Info, E->getArg(1), SourceRHS))13546 return false;13547 unsigned NumElts = SourceLHS.getVectorLength();13548 SmallVector<APValue, 4> ResultElements;13549 ResultElements.reserve(NumElts);13550 llvm::RoundingMode RM = getActiveRoundingMode(getEvalInfo(), E);13551 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();13552 unsigned EltBits = Info.Ctx.getTypeSize(DestEltTy);13553 unsigned NumLanes = NumElts * EltBits / 128;13554 unsigned NumElemsPerLane = NumElts / NumLanes;13555 unsigned HalfElemsPerLane = NumElemsPerLane / 2;13556 13557 for (unsigned L = 0; L != NumElts; L += NumElemsPerLane) {13558 for (unsigned I = 0; I != HalfElemsPerLane; ++I) {13559 APFloat LHSA = SourceLHS.getVectorElt(L + (2 * I) + 0).getFloat();13560 APFloat LHSB = SourceLHS.getVectorElt(L + (2 * I) + 1).getFloat();13561 switch (E->getBuiltinCallee()) {13562 case clang::X86::BI__builtin_ia32_haddpd:13563 case clang::X86::BI__builtin_ia32_haddps:13564 case clang::X86::BI__builtin_ia32_haddps256:13565 case clang::X86::BI__builtin_ia32_haddpd256:13566 LHSA.add(LHSB, RM);13567 break;13568 case clang::X86::BI__builtin_ia32_hsubpd:13569 case clang::X86::BI__builtin_ia32_hsubps:13570 case clang::X86::BI__builtin_ia32_hsubps256:13571 case clang::X86::BI__builtin_ia32_hsubpd256:13572 LHSA.subtract(LHSB, RM);13573 break;13574 }13575 ResultElements.push_back(APValue(LHSA));13576 }13577 for (unsigned I = 0; I != HalfElemsPerLane; ++I) {13578 APFloat RHSA = SourceRHS.getVectorElt(L + (2 * I) + 0).getFloat();13579 APFloat RHSB = SourceRHS.getVectorElt(L + (2 * I) + 1).getFloat();13580 switch (E->getBuiltinCallee()) {13581 case clang::X86::BI__builtin_ia32_haddpd:13582 case clang::X86::BI__builtin_ia32_haddps:13583 case clang::X86::BI__builtin_ia32_haddps256:13584 case clang::X86::BI__builtin_ia32_haddpd256:13585 RHSA.add(RHSB, RM);13586 break;13587 case clang::X86::BI__builtin_ia32_hsubpd:13588 case clang::X86::BI__builtin_ia32_hsubps:13589 case clang::X86::BI__builtin_ia32_hsubps256:13590 case clang::X86::BI__builtin_ia32_hsubpd256:13591 RHSA.subtract(RHSB, RM);13592 break;13593 }13594 ResultElements.push_back(APValue(RHSA));13595 }13596 }13597 return Success(APValue(ResultElements.data(), ResultElements.size()), E);13598 }13599 case clang::X86::BI__builtin_ia32_addsubpd:13600 case clang::X86::BI__builtin_ia32_addsubps:13601 case clang::X86::BI__builtin_ia32_addsubpd256:13602 case clang::X86::BI__builtin_ia32_addsubps256: {13603 // Addsub: alternates between subtraction and addition13604 // Result[i] = (i % 2 == 0) ? (a[i] - b[i]) : (a[i] + b[i])13605 APValue SourceLHS, SourceRHS;13606 if (!EvaluateAsRValue(Info, E->getArg(0), SourceLHS) ||13607 !EvaluateAsRValue(Info, E->getArg(1), SourceRHS))13608 return false;13609 unsigned NumElems = SourceLHS.getVectorLength();13610 SmallVector<APValue, 8> ResultElements;13611 ResultElements.reserve(NumElems);13612 llvm::RoundingMode RM = getActiveRoundingMode(getEvalInfo(), E);13613 13614 for (unsigned I = 0; I != NumElems; ++I) {13615 APFloat LHS = SourceLHS.getVectorElt(I).getFloat();13616 APFloat RHS = SourceRHS.getVectorElt(I).getFloat();13617 if (I % 2 == 0) {13618 // Even indices: subtract13619 LHS.subtract(RHS, RM);13620 } else {13621 // Odd indices: add13622 LHS.add(RHS, RM);13623 }13624 ResultElements.push_back(APValue(LHS));13625 }13626 return Success(APValue(ResultElements.data(), ResultElements.size()), E);13627 }13628 case Builtin::BI__builtin_elementwise_fshl:13629 case Builtin::BI__builtin_elementwise_fshr: {13630 APValue SourceHi, SourceLo, SourceShift;13631 if (!EvaluateAsRValue(Info, E->getArg(0), SourceHi) ||13632 !EvaluateAsRValue(Info, E->getArg(1), SourceLo) ||13633 !EvaluateAsRValue(Info, E->getArg(2), SourceShift))13634 return false;13635 13636 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();13637 if (!DestEltTy->isIntegerType())13638 return false;13639 13640 unsigned SourceLen = SourceHi.getVectorLength();13641 SmallVector<APValue> ResultElements;13642 ResultElements.reserve(SourceLen);13643 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {13644 const APSInt &Hi = SourceHi.getVectorElt(EltNum).getInt();13645 const APSInt &Lo = SourceLo.getVectorElt(EltNum).getInt();13646 const APSInt &Shift = SourceShift.getVectorElt(EltNum).getInt();13647 switch (E->getBuiltinCallee()) {13648 case Builtin::BI__builtin_elementwise_fshl:13649 ResultElements.push_back(APValue(13650 APSInt(llvm::APIntOps::fshl(Hi, Lo, Shift), Hi.isUnsigned())));13651 break;13652 case Builtin::BI__builtin_elementwise_fshr:13653 ResultElements.push_back(APValue(13654 APSInt(llvm::APIntOps::fshr(Hi, Lo, Shift), Hi.isUnsigned())));13655 break;13656 }13657 }13658 13659 return Success(APValue(ResultElements.data(), ResultElements.size()), E);13660 }13661 13662 case X86::BI__builtin_ia32_shuf_f32x4_256:13663 case X86::BI__builtin_ia32_shuf_i32x4_256:13664 case X86::BI__builtin_ia32_shuf_f64x2_256:13665 case X86::BI__builtin_ia32_shuf_i64x2_256:13666 case X86::BI__builtin_ia32_shuf_f32x4:13667 case X86::BI__builtin_ia32_shuf_i32x4:13668 case X86::BI__builtin_ia32_shuf_f64x2:13669 case X86::BI__builtin_ia32_shuf_i64x2: {13670 APValue SourceA, SourceB;13671 if (!EvaluateAsRValue(Info, E->getArg(0), SourceA) ||13672 !EvaluateAsRValue(Info, E->getArg(1), SourceB))13673 return false;13674 13675 APSInt Imm;13676 if (!EvaluateInteger(E->getArg(2), Imm, Info))13677 return false;13678 13679 // Destination and sources A, B all have the same type.13680 unsigned NumElems = SourceA.getVectorLength();13681 const VectorType *VT = E->getArg(0)->getType()->castAs<VectorType>();13682 QualType ElemQT = VT->getElementType();13683 unsigned ElemBits = Info.Ctx.getTypeSize(ElemQT);13684 unsigned LaneBits = 128u;13685 unsigned NumLanes = (NumElems * ElemBits) / LaneBits;13686 unsigned NumElemsPerLane = LaneBits / ElemBits;13687 13688 unsigned DstLen = SourceA.getVectorLength();13689 SmallVector<APValue, 16> ResultElements;13690 ResultElements.reserve(DstLen);13691 13692 APValue R;13693 if (!evalShuffleGeneric(13694 Info, E, R,13695 [NumLanes, NumElemsPerLane](unsigned DstIdx, unsigned ShuffleMask)13696 -> std::pair<unsigned, int> {13697 // DstIdx determines source. ShuffleMask selects lane in source.13698 unsigned BitsPerElem = NumLanes / 2;13699 unsigned IndexMask = (1u << BitsPerElem) - 1;13700 unsigned Lane = DstIdx / NumElemsPerLane;13701 unsigned SrcIdx = (Lane < NumLanes / 2) ? 0 : 1;13702 unsigned BitIdx = BitsPerElem * Lane;13703 unsigned SrcLaneIdx = (ShuffleMask >> BitIdx) & IndexMask;13704 unsigned ElemInLane = DstIdx % NumElemsPerLane;13705 unsigned IdxToPick = SrcLaneIdx * NumElemsPerLane + ElemInLane;13706 return {SrcIdx, IdxToPick};13707 }))13708 return false;13709 return Success(R, E);13710 }13711 13712 case X86::BI__builtin_ia32_insertf32x4_256:13713 case X86::BI__builtin_ia32_inserti32x4_256:13714 case X86::BI__builtin_ia32_insertf64x2_256:13715 case X86::BI__builtin_ia32_inserti64x2_256:13716 case X86::BI__builtin_ia32_insertf32x4:13717 case X86::BI__builtin_ia32_inserti32x4:13718 case X86::BI__builtin_ia32_insertf64x2_512:13719 case X86::BI__builtin_ia32_inserti64x2_512:13720 case X86::BI__builtin_ia32_insertf32x8:13721 case X86::BI__builtin_ia32_inserti32x8:13722 case X86::BI__builtin_ia32_insertf64x4:13723 case X86::BI__builtin_ia32_inserti64x4:13724 case X86::BI__builtin_ia32_vinsertf128_ps256:13725 case X86::BI__builtin_ia32_vinsertf128_pd256:13726 case X86::BI__builtin_ia32_vinsertf128_si256:13727 case X86::BI__builtin_ia32_insert128i256: {13728 APValue SourceDst, SourceSub;13729 if (!EvaluateAsRValue(Info, E->getArg(0), SourceDst) ||13730 !EvaluateAsRValue(Info, E->getArg(1), SourceSub))13731 return false;13732 13733 APSInt Imm;13734 if (!EvaluateInteger(E->getArg(2), Imm, Info))13735 return false;13736 13737 assert(SourceDst.isVector() && SourceSub.isVector());13738 unsigned DstLen = SourceDst.getVectorLength();13739 unsigned SubLen = SourceSub.getVectorLength();13740 assert(SubLen != 0 && DstLen != 0 && (DstLen % SubLen) == 0);13741 unsigned NumLanes = DstLen / SubLen;13742 unsigned LaneIdx = (Imm.getZExtValue() % NumLanes) * SubLen;13743 13744 SmallVector<APValue, 16> ResultElements;13745 ResultElements.reserve(DstLen);13746 13747 for (unsigned EltNum = 0; EltNum < DstLen; ++EltNum) {13748 if (EltNum >= LaneIdx && EltNum < LaneIdx + SubLen)13749 ResultElements.push_back(SourceSub.getVectorElt(EltNum - LaneIdx));13750 else13751 ResultElements.push_back(SourceDst.getVectorElt(EltNum));13752 }13753 13754 return Success(APValue(ResultElements.data(), ResultElements.size()), E);13755 }13756 13757 case clang::X86::BI__builtin_ia32_vec_set_v4hi:13758 case clang::X86::BI__builtin_ia32_vec_set_v16qi:13759 case clang::X86::BI__builtin_ia32_vec_set_v8hi:13760 case clang::X86::BI__builtin_ia32_vec_set_v4si:13761 case clang::X86::BI__builtin_ia32_vec_set_v2di:13762 case clang::X86::BI__builtin_ia32_vec_set_v32qi:13763 case clang::X86::BI__builtin_ia32_vec_set_v16hi:13764 case clang::X86::BI__builtin_ia32_vec_set_v8si:13765 case clang::X86::BI__builtin_ia32_vec_set_v4di: {13766 APValue VecVal;13767 APSInt Scalar, IndexAPS;13768 if (!EvaluateVector(E->getArg(0), VecVal, Info) ||13769 !EvaluateInteger(E->getArg(1), Scalar, Info) ||13770 !EvaluateInteger(E->getArg(2), IndexAPS, Info))13771 return false;13772 13773 QualType ElemTy = E->getType()->castAs<VectorType>()->getElementType();13774 unsigned ElemWidth = Info.Ctx.getIntWidth(ElemTy);13775 bool ElemUnsigned = ElemTy->isUnsignedIntegerOrEnumerationType();13776 Scalar.setIsUnsigned(ElemUnsigned);13777 APSInt ElemAPS = Scalar.extOrTrunc(ElemWidth);13778 APValue ElemAV(ElemAPS);13779 13780 unsigned NumElems = VecVal.getVectorLength();13781 unsigned Index =13782 static_cast<unsigned>(IndexAPS.getZExtValue() & (NumElems - 1));13783 13784 SmallVector<APValue, 4> Elems;13785 Elems.reserve(NumElems);13786 for (unsigned ElemNum = 0; ElemNum != NumElems; ++ElemNum)13787 Elems.push_back(ElemNum == Index ? ElemAV : VecVal.getVectorElt(ElemNum));13788 13789 return Success(APValue(Elems.data(), NumElems), E);13790 }13791 13792 case X86::BI__builtin_ia32_pslldqi128_byteshift:13793 case X86::BI__builtin_ia32_pslldqi256_byteshift:13794 case X86::BI__builtin_ia32_pslldqi512_byteshift: {13795 APValue R;13796 if (!evalShuffleGeneric(13797 Info, E, R,13798 [](unsigned DstIdx, unsigned Shift) -> std::pair<unsigned, int> {13799 unsigned LaneBase = (DstIdx / 16) * 16;13800 unsigned LaneIdx = DstIdx % 16;13801 if (LaneIdx < Shift)13802 return std::make_pair(0, -1);13803 13804 return std::make_pair(13805 0, static_cast<int>(LaneBase + LaneIdx - Shift));13806 }))13807 return false;13808 return Success(R, E);13809 }13810 13811 case X86::BI__builtin_ia32_psrldqi128_byteshift:13812 case X86::BI__builtin_ia32_psrldqi256_byteshift:13813 case X86::BI__builtin_ia32_psrldqi512_byteshift: {13814 APValue R;13815 if (!evalShuffleGeneric(13816 Info, E, R,13817 [](unsigned DstIdx, unsigned Shift) -> std::pair<unsigned, int> {13818 unsigned LaneBase = (DstIdx / 16) * 16;13819 unsigned LaneIdx = DstIdx % 16;13820 if (LaneIdx + Shift < 16)13821 return std::make_pair(13822 0, static_cast<int>(LaneBase + LaneIdx + Shift));13823 13824 return std::make_pair(0, -1);13825 }))13826 return false;13827 return Success(R, E);13828 }13829 13830 case X86::BI__builtin_ia32_palignr128:13831 case X86::BI__builtin_ia32_palignr256:13832 case X86::BI__builtin_ia32_palignr512: {13833 APValue R;13834 if (!evalShuffleGeneric(Info, E, R, [](unsigned DstIdx, unsigned Shift) {13835 // Default to -1 → zero-fill this destination element13836 unsigned VecIdx = 1;13837 int ElemIdx = -1;13838 13839 int Lane = DstIdx / 16;13840 int Offset = DstIdx % 16;13841 13842 // Elements come from VecB first, then VecA after the shift boundary13843 unsigned ShiftedIdx = Offset + (Shift & 0xFF);13844 if (ShiftedIdx < 16) { // from VecB13845 ElemIdx = ShiftedIdx + (Lane * 16);13846 } else if (ShiftedIdx < 32) { // from VecA13847 VecIdx = 0;13848 ElemIdx = (ShiftedIdx - 16) + (Lane * 16);13849 }13850 13851 return std::pair<unsigned, int>{VecIdx, ElemIdx};13852 }))13853 return false;13854 return Success(R, E);13855 }13856 case X86::BI__builtin_ia32_alignd128:13857 case X86::BI__builtin_ia32_alignd256:13858 case X86::BI__builtin_ia32_alignd512:13859 case X86::BI__builtin_ia32_alignq128:13860 case X86::BI__builtin_ia32_alignq256:13861 case X86::BI__builtin_ia32_alignq512: {13862 APValue R;13863 unsigned NumElems = E->getType()->castAs<VectorType>()->getNumElements();13864 if (!evalShuffleGeneric(Info, E, R,13865 [NumElems](unsigned DstIdx, unsigned Shift) {13866 unsigned Imm = Shift & 0xFF;13867 unsigned EffectiveShift = Imm & (NumElems - 1);13868 unsigned SourcePos = DstIdx + EffectiveShift;13869 unsigned VecIdx = SourcePos < NumElems ? 1 : 0;13870 unsigned ElemIdx = SourcePos & (NumElems - 1);13871 13872 return std::pair<unsigned, int>{13873 VecIdx, static_cast<int>(ElemIdx)};13874 }))13875 return false;13876 return Success(R, E);13877 }13878 case X86::BI__builtin_ia32_permvarsi256:13879 case X86::BI__builtin_ia32_permvarsf256:13880 case X86::BI__builtin_ia32_permvardf512:13881 case X86::BI__builtin_ia32_permvardi512:13882 case X86::BI__builtin_ia32_permvarhi128: {13883 APValue R;13884 if (!evalShuffleGeneric(Info, E, R,13885 [](unsigned DstIdx, unsigned ShuffleMask) {13886 int Offset = ShuffleMask & 0x7;13887 return std::pair<unsigned, int>{0, Offset};13888 }))13889 return false;13890 return Success(R, E);13891 }13892 case X86::BI__builtin_ia32_permvarqi128:13893 case X86::BI__builtin_ia32_permvarhi256:13894 case X86::BI__builtin_ia32_permvarsi512:13895 case X86::BI__builtin_ia32_permvarsf512: {13896 APValue R;13897 if (!evalShuffleGeneric(Info, E, R,13898 [](unsigned DstIdx, unsigned ShuffleMask) {13899 int Offset = ShuffleMask & 0xF;13900 return std::pair<unsigned, int>{0, Offset};13901 }))13902 return false;13903 return Success(R, E);13904 }13905 case X86::BI__builtin_ia32_permvardi256:13906 case X86::BI__builtin_ia32_permvardf256: {13907 APValue R;13908 if (!evalShuffleGeneric(Info, E, R,13909 [](unsigned DstIdx, unsigned ShuffleMask) {13910 int Offset = ShuffleMask & 0x3;13911 return std::pair<unsigned, int>{0, Offset};13912 }))13913 return false;13914 return Success(R, E);13915 }13916 case X86::BI__builtin_ia32_permvarqi256:13917 case X86::BI__builtin_ia32_permvarhi512: {13918 APValue R;13919 if (!evalShuffleGeneric(Info, E, R,13920 [](unsigned DstIdx, unsigned ShuffleMask) {13921 int Offset = ShuffleMask & 0x1F;13922 return std::pair<unsigned, int>{0, Offset};13923 }))13924 return false;13925 return Success(R, E);13926 }13927 case X86::BI__builtin_ia32_permvarqi512: {13928 APValue R;13929 if (!evalShuffleGeneric(Info, E, R,13930 [](unsigned DstIdx, unsigned ShuffleMask) {13931 int Offset = ShuffleMask & 0x3F;13932 return std::pair<unsigned, int>{0, Offset};13933 }))13934 return false;13935 return Success(R, E);13936 }13937 case X86::BI__builtin_ia32_vpermi2varq128:13938 case X86::BI__builtin_ia32_vpermi2varpd128: {13939 APValue R;13940 if (!evalShuffleGeneric(Info, E, R,13941 [](unsigned DstIdx, unsigned ShuffleMask) {13942 int Offset = ShuffleMask & 0x1;13943 unsigned SrcIdx = (ShuffleMask >> 1) & 0x1;13944 return std::pair<unsigned, int>{SrcIdx, Offset};13945 }))13946 return false;13947 return Success(R, E);13948 }13949 case X86::BI__builtin_ia32_vpermi2vard128:13950 case X86::BI__builtin_ia32_vpermi2varps128:13951 case X86::BI__builtin_ia32_vpermi2varq256:13952 case X86::BI__builtin_ia32_vpermi2varpd256: {13953 APValue R;13954 if (!evalShuffleGeneric(Info, E, R,13955 [](unsigned DstIdx, unsigned ShuffleMask) {13956 int Offset = ShuffleMask & 0x3;13957 unsigned SrcIdx = (ShuffleMask >> 2) & 0x1;13958 return std::pair<unsigned, int>{SrcIdx, Offset};13959 }))13960 return false;13961 return Success(R, E);13962 }13963 case X86::BI__builtin_ia32_vpermi2varhi128:13964 case X86::BI__builtin_ia32_vpermi2vard256:13965 case X86::BI__builtin_ia32_vpermi2varps256:13966 case X86::BI__builtin_ia32_vpermi2varq512:13967 case X86::BI__builtin_ia32_vpermi2varpd512: {13968 APValue R;13969 if (!evalShuffleGeneric(Info, E, R,13970 [](unsigned DstIdx, unsigned ShuffleMask) {13971 int Offset = ShuffleMask & 0x7;13972 unsigned SrcIdx = (ShuffleMask >> 3) & 0x1;13973 return std::pair<unsigned, int>{SrcIdx, Offset};13974 }))13975 return false;13976 return Success(R, E);13977 }13978 case X86::BI__builtin_ia32_vpermi2varqi128:13979 case X86::BI__builtin_ia32_vpermi2varhi256:13980 case X86::BI__builtin_ia32_vpermi2vard512:13981 case X86::BI__builtin_ia32_vpermi2varps512: {13982 APValue R;13983 if (!evalShuffleGeneric(Info, E, R,13984 [](unsigned DstIdx, unsigned ShuffleMask) {13985 int Offset = ShuffleMask & 0xF;13986 unsigned SrcIdx = (ShuffleMask >> 4) & 0x1;13987 return std::pair<unsigned, int>{SrcIdx, Offset};13988 }))13989 return false;13990 return Success(R, E);13991 }13992 case X86::BI__builtin_ia32_vpermi2varqi256:13993 case X86::BI__builtin_ia32_vpermi2varhi512: {13994 APValue R;13995 if (!evalShuffleGeneric(Info, E, R,13996 [](unsigned DstIdx, unsigned ShuffleMask) {13997 int Offset = ShuffleMask & 0x1F;13998 unsigned SrcIdx = (ShuffleMask >> 5) & 0x1;13999 return std::pair<unsigned, int>{SrcIdx, Offset};14000 }))14001 return false;14002 return Success(R, E);14003 }14004 case X86::BI__builtin_ia32_vpermi2varqi512: {14005 APValue R;14006 if (!evalShuffleGeneric(Info, E, R,14007 [](unsigned DstIdx, unsigned ShuffleMask) {14008 int Offset = ShuffleMask & 0x3F;14009 unsigned SrcIdx = (ShuffleMask >> 6) & 0x1;14010 return std::pair<unsigned, int>{SrcIdx, Offset};14011 }))14012 return false;14013 return Success(R, E);14014 }14015 14016 case clang::X86::BI__builtin_ia32_vcvtps2ph:14017 case clang::X86::BI__builtin_ia32_vcvtps2ph256: {14018 APValue SrcVec;14019 if (!EvaluateAsRValue(Info, E->getArg(0), SrcVec))14020 return false;14021 14022 APSInt Imm;14023 if (!EvaluateInteger(E->getArg(1), Imm, Info))14024 return false;14025 14026 const auto *SrcVTy = E->getArg(0)->getType()->castAs<VectorType>();14027 unsigned SrcNumElems = SrcVTy->getNumElements();14028 const auto *DstVTy = E->getType()->castAs<VectorType>();14029 unsigned DstNumElems = DstVTy->getNumElements();14030 QualType DstElemTy = DstVTy->getElementType();14031 14032 const llvm::fltSemantics &HalfSem =14033 Info.Ctx.getFloatTypeSemantics(Info.Ctx.HalfTy);14034 14035 int ImmVal = Imm.getZExtValue();14036 bool UseMXCSR = (ImmVal & 4) != 0;14037 bool IsFPConstrained =14038 E->getFPFeaturesInEffect(Info.Ctx.getLangOpts()).isFPConstrained();14039 14040 llvm::RoundingMode RM;14041 if (!UseMXCSR) {14042 switch (ImmVal & 3) {14043 case 0:14044 RM = llvm::RoundingMode::NearestTiesToEven;14045 break;14046 case 1:14047 RM = llvm::RoundingMode::TowardNegative;14048 break;14049 case 2:14050 RM = llvm::RoundingMode::TowardPositive;14051 break;14052 case 3:14053 RM = llvm::RoundingMode::TowardZero;14054 break;14055 default:14056 llvm_unreachable("Invalid immediate rounding mode");14057 }14058 } else {14059 RM = llvm::RoundingMode::NearestTiesToEven;14060 }14061 14062 SmallVector<APValue, 8> ResultElements;14063 ResultElements.reserve(DstNumElems);14064 14065 for (unsigned I = 0; I < SrcNumElems; ++I) {14066 APFloat SrcVal = SrcVec.getVectorElt(I).getFloat();14067 14068 bool LostInfo;14069 APFloat::opStatus St = SrcVal.convert(HalfSem, RM, &LostInfo);14070 14071 if (UseMXCSR && IsFPConstrained && St != APFloat::opOK) {14072 Info.FFDiag(E, diag::note_constexpr_dynamic_rounding);14073 return false;14074 }14075 14076 APSInt DstInt(SrcVal.bitcastToAPInt(),14077 DstElemTy->isUnsignedIntegerOrEnumerationType());14078 ResultElements.push_back(APValue(DstInt));14079 }14080 14081 if (DstNumElems > SrcNumElems) {14082 APSInt Zero = Info.Ctx.MakeIntValue(0, DstElemTy);14083 for (unsigned I = SrcNumElems; I < DstNumElems; ++I) {14084 ResultElements.push_back(APValue(Zero));14085 }14086 }14087 14088 return Success(ResultElements, E);14089 }14090 }14091}14092 14093bool VectorExprEvaluator::VisitConvertVectorExpr(const ConvertVectorExpr *E) {14094 APValue Source;14095 QualType SourceVecType = E->getSrcExpr()->getType();14096 if (!EvaluateAsRValue(Info, E->getSrcExpr(), Source))14097 return false;14098 14099 QualType DestTy = E->getType()->castAs<VectorType>()->getElementType();14100 QualType SourceTy = SourceVecType->castAs<VectorType>()->getElementType();14101 14102 const FPOptions FPO = E->getFPFeaturesInEffect(Info.Ctx.getLangOpts());14103 14104 auto SourceLen = Source.getVectorLength();14105 SmallVector<APValue, 4> ResultElements;14106 ResultElements.reserve(SourceLen);14107 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {14108 APValue Elt;14109 if (!handleVectorElementCast(Info, FPO, E, SourceTy, DestTy,14110 Source.getVectorElt(EltNum), Elt))14111 return false;14112 ResultElements.push_back(std::move(Elt));14113 }14114 14115 return Success(APValue(ResultElements.data(), ResultElements.size()), E);14116}14117 14118static bool handleVectorShuffle(EvalInfo &Info, const ShuffleVectorExpr *E,14119 QualType ElemType, APValue const &VecVal1,14120 APValue const &VecVal2, unsigned EltNum,14121 APValue &Result) {14122 unsigned const TotalElementsInInputVector1 = VecVal1.getVectorLength();14123 unsigned const TotalElementsInInputVector2 = VecVal2.getVectorLength();14124 14125 APSInt IndexVal = E->getShuffleMaskIdx(EltNum);14126 int64_t index = IndexVal.getExtValue();14127 // The spec says that -1 should be treated as undef for optimizations,14128 // but in constexpr we'd have to produce an APValue::Indeterminate,14129 // which is prohibited from being a top-level constant value. Emit a14130 // diagnostic instead.14131 if (index == -1) {14132 Info.FFDiag(14133 E, diag::err_shufflevector_minus_one_is_undefined_behavior_constexpr)14134 << EltNum;14135 return false;14136 }14137 14138 if (index < 0 ||14139 index >= TotalElementsInInputVector1 + TotalElementsInInputVector2)14140 llvm_unreachable("Out of bounds shuffle index");14141 14142 if (index >= TotalElementsInInputVector1)14143 Result = VecVal2.getVectorElt(index - TotalElementsInInputVector1);14144 else14145 Result = VecVal1.getVectorElt(index);14146 return true;14147}14148 14149bool VectorExprEvaluator::VisitShuffleVectorExpr(const ShuffleVectorExpr *E) {14150 // FIXME: Unary shuffle with mask not currently supported.14151 if (E->getNumSubExprs() == 2)14152 return Error(E);14153 APValue VecVal1;14154 const Expr *Vec1 = E->getExpr(0);14155 if (!EvaluateAsRValue(Info, Vec1, VecVal1))14156 return false;14157 APValue VecVal2;14158 const Expr *Vec2 = E->getExpr(1);14159 if (!EvaluateAsRValue(Info, Vec2, VecVal2))14160 return false;14161 14162 VectorType const *DestVecTy = E->getType()->castAs<VectorType>();14163 QualType DestElTy = DestVecTy->getElementType();14164 14165 auto TotalElementsInOutputVector = DestVecTy->getNumElements();14166 14167 SmallVector<APValue, 4> ResultElements;14168 ResultElements.reserve(TotalElementsInOutputVector);14169 for (unsigned EltNum = 0; EltNum < TotalElementsInOutputVector; ++EltNum) {14170 APValue Elt;14171 if (!handleVectorShuffle(Info, E, DestElTy, VecVal1, VecVal2, EltNum, Elt))14172 return false;14173 ResultElements.push_back(std::move(Elt));14174 }14175 14176 return Success(APValue(ResultElements.data(), ResultElements.size()), E);14177}14178 14179//===----------------------------------------------------------------------===//14180// Array Evaluation14181//===----------------------------------------------------------------------===//14182 14183namespace {14184 class ArrayExprEvaluator14185 : public ExprEvaluatorBase<ArrayExprEvaluator> {14186 const LValue &This;14187 APValue &Result;14188 public:14189 14190 ArrayExprEvaluator(EvalInfo &Info, const LValue &This, APValue &Result)14191 : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {}14192 14193 bool Success(const APValue &V, const Expr *E) {14194 assert(V.isArray() && "expected array");14195 Result = V;14196 return true;14197 }14198 14199 bool ZeroInitialization(const Expr *E) {14200 const ConstantArrayType *CAT =14201 Info.Ctx.getAsConstantArrayType(E->getType());14202 if (!CAT) {14203 if (E->getType()->isIncompleteArrayType()) {14204 // We can be asked to zero-initialize a flexible array member; this14205 // is represented as an ImplicitValueInitExpr of incomplete array14206 // type. In this case, the array has zero elements.14207 Result = APValue(APValue::UninitArray(), 0, 0);14208 return true;14209 }14210 // FIXME: We could handle VLAs here.14211 return Error(E);14212 }14213 14214 Result = APValue(APValue::UninitArray(), 0, CAT->getZExtSize());14215 if (!Result.hasArrayFiller())14216 return true;14217 14218 // Zero-initialize all elements.14219 LValue Subobject = This;14220 Subobject.addArray(Info, E, CAT);14221 ImplicitValueInitExpr VIE(CAT->getElementType());14222 return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, &VIE);14223 }14224 14225 bool VisitCallExpr(const CallExpr *E) {14226 return handleCallExpr(E, Result, &This);14227 }14228 bool VisitCastExpr(const CastExpr *E);14229 bool VisitInitListExpr(const InitListExpr *E,14230 QualType AllocType = QualType());14231 bool VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E);14232 bool VisitCXXConstructExpr(const CXXConstructExpr *E);14233 bool VisitCXXConstructExpr(const CXXConstructExpr *E,14234 const LValue &Subobject,14235 APValue *Value, QualType Type);14236 bool VisitStringLiteral(const StringLiteral *E,14237 QualType AllocType = QualType()) {14238 expandStringLiteral(Info, E, Result, AllocType);14239 return true;14240 }14241 bool VisitCXXParenListInitExpr(const CXXParenListInitExpr *E);14242 bool VisitCXXParenListOrInitListExpr(const Expr *ExprToVisit,14243 ArrayRef<Expr *> Args,14244 const Expr *ArrayFiller,14245 QualType AllocType = QualType());14246 };14247} // end anonymous namespace14248 14249static bool EvaluateArray(const Expr *E, const LValue &This,14250 APValue &Result, EvalInfo &Info) {14251 assert(!E->isValueDependent());14252 assert(E->isPRValue() && E->getType()->isArrayType() &&14253 "not an array prvalue");14254 return ArrayExprEvaluator(Info, This, Result).Visit(E);14255}14256 14257static bool EvaluateArrayNewInitList(EvalInfo &Info, LValue &This,14258 APValue &Result, const InitListExpr *ILE,14259 QualType AllocType) {14260 assert(!ILE->isValueDependent());14261 assert(ILE->isPRValue() && ILE->getType()->isArrayType() &&14262 "not an array prvalue");14263 return ArrayExprEvaluator(Info, This, Result)14264 .VisitInitListExpr(ILE, AllocType);14265}14266 14267static bool EvaluateArrayNewConstructExpr(EvalInfo &Info, LValue &This,14268 APValue &Result,14269 const CXXConstructExpr *CCE,14270 QualType AllocType) {14271 assert(!CCE->isValueDependent());14272 assert(CCE->isPRValue() && CCE->getType()->isArrayType() &&14273 "not an array prvalue");14274 return ArrayExprEvaluator(Info, This, Result)14275 .VisitCXXConstructExpr(CCE, This, &Result, AllocType);14276}14277 14278// Return true iff the given array filler may depend on the element index.14279static bool MaybeElementDependentArrayFiller(const Expr *FillerExpr) {14280 // For now, just allow non-class value-initialization and initialization14281 // lists comprised of them.14282 if (isa<ImplicitValueInitExpr>(FillerExpr))14283 return false;14284 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(FillerExpr)) {14285 for (unsigned I = 0, E = ILE->getNumInits(); I != E; ++I) {14286 if (MaybeElementDependentArrayFiller(ILE->getInit(I)))14287 return true;14288 }14289 14290 if (ILE->hasArrayFiller() &&14291 MaybeElementDependentArrayFiller(ILE->getArrayFiller()))14292 return true;14293 14294 return false;14295 }14296 return true;14297}14298 14299bool ArrayExprEvaluator::VisitCastExpr(const CastExpr *E) {14300 const Expr *SE = E->getSubExpr();14301 14302 switch (E->getCastKind()) {14303 default:14304 return ExprEvaluatorBaseTy::VisitCastExpr(E);14305 case CK_HLSLAggregateSplatCast: {14306 APValue Val;14307 QualType ValTy;14308 14309 if (!hlslAggSplatHelper(Info, SE, Val, ValTy))14310 return false;14311 14312 unsigned NEls = elementwiseSize(Info, E->getType());14313 14314 SmallVector<APValue> SplatEls(NEls, Val);14315 SmallVector<QualType> SplatType(NEls, ValTy);14316 14317 // cast the elements14318 const FPOptions FPO = E->getFPFeaturesInEffect(Info.Ctx.getLangOpts());14319 if (!constructAggregate(Info, FPO, E, Result, E->getType(), SplatEls,14320 SplatType))14321 return false;14322 14323 return true;14324 }14325 case CK_HLSLElementwiseCast: {14326 SmallVector<APValue> SrcEls;14327 SmallVector<QualType> SrcTypes;14328 14329 if (!hlslElementwiseCastHelper(Info, SE, E->getType(), SrcEls, SrcTypes))14330 return false;14331 14332 // cast the elements14333 const FPOptions FPO = E->getFPFeaturesInEffect(Info.Ctx.getLangOpts());14334 if (!constructAggregate(Info, FPO, E, Result, E->getType(), SrcEls,14335 SrcTypes))14336 return false;14337 return true;14338 }14339 }14340}14341 14342bool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E,14343 QualType AllocType) {14344 const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(14345 AllocType.isNull() ? E->getType() : AllocType);14346 if (!CAT)14347 return Error(E);14348 14349 // C++11 [dcl.init.string]p1: A char array [...] can be initialized by [...]14350 // an appropriately-typed string literal enclosed in braces.14351 if (E->isStringLiteralInit()) {14352 auto *SL = dyn_cast<StringLiteral>(E->getInit(0)->IgnoreParenImpCasts());14353 // FIXME: Support ObjCEncodeExpr here once we support it in14354 // ArrayExprEvaluator generally.14355 if (!SL)14356 return Error(E);14357 return VisitStringLiteral(SL, AllocType);14358 }14359 // Any other transparent list init will need proper handling of the14360 // AllocType; we can't just recurse to the inner initializer.14361 assert(!E->isTransparent() &&14362 "transparent array list initialization is not string literal init?");14363 14364 return VisitCXXParenListOrInitListExpr(E, E->inits(), E->getArrayFiller(),14365 AllocType);14366}14367 14368bool ArrayExprEvaluator::VisitCXXParenListOrInitListExpr(14369 const Expr *ExprToVisit, ArrayRef<Expr *> Args, const Expr *ArrayFiller,14370 QualType AllocType) {14371 const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(14372 AllocType.isNull() ? ExprToVisit->getType() : AllocType);14373 14374 bool Success = true;14375 14376 assert((!Result.isArray() || Result.getArrayInitializedElts() == 0) &&14377 "zero-initialized array shouldn't have any initialized elts");14378 APValue Filler;14379 if (Result.isArray() && Result.hasArrayFiller())14380 Filler = Result.getArrayFiller();14381 14382 unsigned NumEltsToInit = Args.size();14383 unsigned NumElts = CAT->getZExtSize();14384 14385 // If the initializer might depend on the array index, run it for each14386 // array element.14387 if (NumEltsToInit != NumElts &&14388 MaybeElementDependentArrayFiller(ArrayFiller)) {14389 NumEltsToInit = NumElts;14390 } else {14391 for (auto *Init : Args) {14392 if (auto *EmbedS = dyn_cast<EmbedExpr>(Init->IgnoreParenImpCasts()))14393 NumEltsToInit += EmbedS->getDataElementCount() - 1;14394 }14395 if (NumEltsToInit > NumElts)14396 NumEltsToInit = NumElts;14397 }14398 14399 LLVM_DEBUG(llvm::dbgs() << "The number of elements to initialize: "14400 << NumEltsToInit << ".\n");14401 14402 Result = APValue(APValue::UninitArray(), NumEltsToInit, NumElts);14403 14404 // If the array was previously zero-initialized, preserve the14405 // zero-initialized values.14406 if (Filler.hasValue()) {14407 for (unsigned I = 0, E = Result.getArrayInitializedElts(); I != E; ++I)14408 Result.getArrayInitializedElt(I) = Filler;14409 if (Result.hasArrayFiller())14410 Result.getArrayFiller() = Filler;14411 }14412 14413 LValue Subobject = This;14414 Subobject.addArray(Info, ExprToVisit, CAT);14415 auto Eval = [&](const Expr *Init, unsigned ArrayIndex) {14416 if (Init->isValueDependent())14417 return EvaluateDependentExpr(Init, Info);14418 14419 if (!EvaluateInPlace(Result.getArrayInitializedElt(ArrayIndex), Info,14420 Subobject, Init) ||14421 !HandleLValueArrayAdjustment(Info, Init, Subobject,14422 CAT->getElementType(), 1)) {14423 if (!Info.noteFailure())14424 return false;14425 Success = false;14426 }14427 return true;14428 };14429 unsigned ArrayIndex = 0;14430 QualType DestTy = CAT->getElementType();14431 APSInt Value(Info.Ctx.getTypeSize(DestTy), DestTy->isUnsignedIntegerType());14432 for (unsigned Index = 0; Index != NumEltsToInit; ++Index) {14433 const Expr *Init = Index < Args.size() ? Args[Index] : ArrayFiller;14434 if (ArrayIndex >= NumEltsToInit)14435 break;14436 if (auto *EmbedS = dyn_cast<EmbedExpr>(Init->IgnoreParenImpCasts())) {14437 StringLiteral *SL = EmbedS->getDataStringLiteral();14438 for (unsigned I = EmbedS->getStartingElementPos(),14439 N = EmbedS->getDataElementCount();14440 I != EmbedS->getStartingElementPos() + N; ++I) {14441 Value = SL->getCodeUnit(I);14442 if (DestTy->isIntegerType()) {14443 Result.getArrayInitializedElt(ArrayIndex) = APValue(Value);14444 } else {14445 assert(DestTy->isFloatingType() && "unexpected type");14446 const FPOptions FPO =14447 Init->getFPFeaturesInEffect(Info.Ctx.getLangOpts());14448 APFloat FValue(0.0);14449 if (!HandleIntToFloatCast(Info, Init, FPO, EmbedS->getType(), Value,14450 DestTy, FValue))14451 return false;14452 Result.getArrayInitializedElt(ArrayIndex) = APValue(FValue);14453 }14454 ArrayIndex++;14455 }14456 } else {14457 if (!Eval(Init, ArrayIndex))14458 return false;14459 ++ArrayIndex;14460 }14461 }14462 14463 if (!Result.hasArrayFiller())14464 return Success;14465 14466 // If we get here, we have a trivial filler, which we can just evaluate14467 // once and splat over the rest of the array elements.14468 assert(ArrayFiller && "no array filler for incomplete init list");14469 return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject,14470 ArrayFiller) &&14471 Success;14472}14473 14474bool ArrayExprEvaluator::VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E) {14475 LValue CommonLV;14476 if (E->getCommonExpr() &&14477 !Evaluate(Info.CurrentCall->createTemporary(14478 E->getCommonExpr(),14479 getStorageType(Info.Ctx, E->getCommonExpr()),14480 ScopeKind::FullExpression, CommonLV),14481 Info, E->getCommonExpr()->getSourceExpr()))14482 return false;14483 14484 auto *CAT = cast<ConstantArrayType>(E->getType()->castAsArrayTypeUnsafe());14485 14486 uint64_t Elements = CAT->getZExtSize();14487 Result = APValue(APValue::UninitArray(), Elements, Elements);14488 14489 LValue Subobject = This;14490 Subobject.addArray(Info, E, CAT);14491 14492 bool Success = true;14493 for (EvalInfo::ArrayInitLoopIndex Index(Info); Index != Elements; ++Index) {14494 // C++ [class.temporary]/514495 // There are four contexts in which temporaries are destroyed at a different14496 // point than the end of the full-expression. [...] The second context is14497 // when a copy constructor is called to copy an element of an array while14498 // the entire array is copied [...]. In either case, if the constructor has14499 // one or more default arguments, the destruction of every temporary created14500 // in a default argument is sequenced before the construction of the next14501 // array element, if any.14502 FullExpressionRAII Scope(Info);14503 14504 if (!EvaluateInPlace(Result.getArrayInitializedElt(Index),14505 Info, Subobject, E->getSubExpr()) ||14506 !HandleLValueArrayAdjustment(Info, E, Subobject,14507 CAT->getElementType(), 1)) {14508 if (!Info.noteFailure())14509 return false;14510 Success = false;14511 }14512 14513 // Make sure we run the destructors too.14514 Scope.destroy();14515 }14516 14517 return Success;14518}14519 14520bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) {14521 return VisitCXXConstructExpr(E, This, &Result, E->getType());14522}14523 14524bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E,14525 const LValue &Subobject,14526 APValue *Value,14527 QualType Type) {14528 bool HadZeroInit = Value->hasValue();14529 14530 if (const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(Type)) {14531 unsigned FinalSize = CAT->getZExtSize();14532 14533 // Preserve the array filler if we had prior zero-initialization.14534 APValue Filler =14535 HadZeroInit && Value->hasArrayFiller() ? Value->getArrayFiller()14536 : APValue();14537 14538 *Value = APValue(APValue::UninitArray(), 0, FinalSize);14539 if (FinalSize == 0)14540 return true;14541 14542 bool HasTrivialConstructor = CheckTrivialDefaultConstructor(14543 Info, E->getExprLoc(), E->getConstructor(),14544 E->requiresZeroInitialization());14545 LValue ArrayElt = Subobject;14546 ArrayElt.addArray(Info, E, CAT);14547 // We do the whole initialization in two passes, first for just one element,14548 // then for the whole array. It's possible we may find out we can't do const14549 // init in the first pass, in which case we avoid allocating a potentially14550 // large array. We don't do more passes because expanding array requires14551 // copying the data, which is wasteful.14552 for (const unsigned N : {1u, FinalSize}) {14553 unsigned OldElts = Value->getArrayInitializedElts();14554 if (OldElts == N)14555 break;14556 14557 // Expand the array to appropriate size.14558 APValue NewValue(APValue::UninitArray(), N, FinalSize);14559 for (unsigned I = 0; I < OldElts; ++I)14560 NewValue.getArrayInitializedElt(I).swap(14561 Value->getArrayInitializedElt(I));14562 Value->swap(NewValue);14563 14564 if (HadZeroInit)14565 for (unsigned I = OldElts; I < N; ++I)14566 Value->getArrayInitializedElt(I) = Filler;14567 14568 if (HasTrivialConstructor && N == FinalSize && FinalSize != 1) {14569 // If we have a trivial constructor, only evaluate it once and copy14570 // the result into all the array elements.14571 APValue &FirstResult = Value->getArrayInitializedElt(0);14572 for (unsigned I = OldElts; I < FinalSize; ++I)14573 Value->getArrayInitializedElt(I) = FirstResult;14574 } else {14575 for (unsigned I = OldElts; I < N; ++I) {14576 if (!VisitCXXConstructExpr(E, ArrayElt,14577 &Value->getArrayInitializedElt(I),14578 CAT->getElementType()) ||14579 !HandleLValueArrayAdjustment(Info, E, ArrayElt,14580 CAT->getElementType(), 1))14581 return false;14582 // When checking for const initilization any diagnostic is considered14583 // an error.14584 if (Info.EvalStatus.Diag && !Info.EvalStatus.Diag->empty() &&14585 !Info.keepEvaluatingAfterFailure())14586 return false;14587 }14588 }14589 }14590 14591 return true;14592 }14593 14594 if (!Type->isRecordType())14595 return Error(E);14596 14597 return RecordExprEvaluator(Info, Subobject, *Value)14598 .VisitCXXConstructExpr(E, Type);14599}14600 14601bool ArrayExprEvaluator::VisitCXXParenListInitExpr(14602 const CXXParenListInitExpr *E) {14603 assert(E->getType()->isConstantArrayType() &&14604 "Expression result is not a constant array type");14605 14606 return VisitCXXParenListOrInitListExpr(E, E->getInitExprs(),14607 E->getArrayFiller());14608}14609 14610//===----------------------------------------------------------------------===//14611// Integer Evaluation14612//14613// As a GNU extension, we support casting pointers to sufficiently-wide integer14614// types and back in constant folding. Integer values are thus represented14615// either as an integer-valued APValue, or as an lvalue-valued APValue.14616//===----------------------------------------------------------------------===//14617 14618namespace {14619class IntExprEvaluator14620 : public ExprEvaluatorBase<IntExprEvaluator> {14621 APValue &Result;14622public:14623 IntExprEvaluator(EvalInfo &info, APValue &result)14624 : ExprEvaluatorBaseTy(info), Result(result) {}14625 14626 bool Success(const llvm::APSInt &SI, const Expr *E, APValue &Result) {14627 assert(E->getType()->isIntegralOrEnumerationType() &&14628 "Invalid evaluation result.");14629 assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() &&14630 "Invalid evaluation result.");14631 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&14632 "Invalid evaluation result.");14633 Result = APValue(SI);14634 return true;14635 }14636 bool Success(const llvm::APSInt &SI, const Expr *E) {14637 return Success(SI, E, Result);14638 }14639 14640 bool Success(const llvm::APInt &I, const Expr *E, APValue &Result) {14641 assert(E->getType()->isIntegralOrEnumerationType() &&14642 "Invalid evaluation result.");14643 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&14644 "Invalid evaluation result.");14645 Result = APValue(APSInt(I));14646 Result.getInt().setIsUnsigned(14647 E->getType()->isUnsignedIntegerOrEnumerationType());14648 return true;14649 }14650 bool Success(const llvm::APInt &I, const Expr *E) {14651 return Success(I, E, Result);14652 }14653 14654 bool Success(uint64_t Value, const Expr *E, APValue &Result) {14655 assert(E->getType()->isIntegralOrEnumerationType() &&14656 "Invalid evaluation result.");14657 Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));14658 return true;14659 }14660 bool Success(uint64_t Value, const Expr *E) {14661 return Success(Value, E, Result);14662 }14663 14664 bool Success(CharUnits Size, const Expr *E) {14665 return Success(Size.getQuantity(), E);14666 }14667 14668 bool Success(const APValue &V, const Expr *E) {14669 // C++23 [expr.const]p8 If we have a variable that is unknown reference or14670 // pointer allow further evaluation of the value.14671 if (V.isLValue() || V.isAddrLabelDiff() || V.isIndeterminate() ||14672 V.allowConstexprUnknown()) {14673 Result = V;14674 return true;14675 }14676 return Success(V.getInt(), E);14677 }14678 14679 bool ZeroInitialization(const Expr *E) { return Success(0, E); }14680 14681 friend std::optional<bool> EvaluateBuiltinIsWithinLifetime(IntExprEvaluator &,14682 const CallExpr *);14683 14684 //===--------------------------------------------------------------------===//14685 // Visitor Methods14686 //===--------------------------------------------------------------------===//14687 14688 bool VisitIntegerLiteral(const IntegerLiteral *E) {14689 return Success(E->getValue(), E);14690 }14691 bool VisitCharacterLiteral(const CharacterLiteral *E) {14692 return Success(E->getValue(), E);14693 }14694 14695 bool CheckReferencedDecl(const Expr *E, const Decl *D);14696 bool VisitDeclRefExpr(const DeclRefExpr *E) {14697 if (CheckReferencedDecl(E, E->getDecl()))14698 return true;14699 14700 return ExprEvaluatorBaseTy::VisitDeclRefExpr(E);14701 }14702 bool VisitMemberExpr(const MemberExpr *E) {14703 if (CheckReferencedDecl(E, E->getMemberDecl())) {14704 VisitIgnoredBaseExpression(E->getBase());14705 return true;14706 }14707 14708 return ExprEvaluatorBaseTy::VisitMemberExpr(E);14709 }14710 14711 bool VisitCallExpr(const CallExpr *E);14712 bool VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinOp);14713 bool VisitBinaryOperator(const BinaryOperator *E);14714 bool VisitOffsetOfExpr(const OffsetOfExpr *E);14715 bool VisitUnaryOperator(const UnaryOperator *E);14716 14717 bool VisitCastExpr(const CastExpr* E);14718 bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E);14719 14720 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {14721 return Success(E->getValue(), E);14722 }14723 14724 bool VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) {14725 return Success(E->getValue(), E);14726 }14727 14728 bool VisitArrayInitIndexExpr(const ArrayInitIndexExpr *E) {14729 if (Info.ArrayInitIndex == uint64_t(-1)) {14730 // We were asked to evaluate this subexpression independent of the14731 // enclosing ArrayInitLoopExpr. We can't do that.14732 Info.FFDiag(E);14733 return false;14734 }14735 return Success(Info.ArrayInitIndex, E);14736 }14737 14738 // Note, GNU defines __null as an integer, not a pointer.14739 bool VisitGNUNullExpr(const GNUNullExpr *E) {14740 return ZeroInitialization(E);14741 }14742 14743 bool VisitTypeTraitExpr(const TypeTraitExpr *E) {14744 if (E->isStoredAsBoolean())14745 return Success(E->getBoolValue(), E);14746 if (E->getAPValue().isAbsent())14747 return false;14748 assert(E->getAPValue().isInt() && "APValue type not supported");14749 return Success(E->getAPValue().getInt(), E);14750 }14751 14752 bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) {14753 return Success(E->getValue(), E);14754 }14755 14756 bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) {14757 return Success(E->getValue(), E);14758 }14759 14760 bool VisitOpenACCAsteriskSizeExpr(const OpenACCAsteriskSizeExpr *E) {14761 // This should not be evaluated during constant expr evaluation, as it14762 // should always be in an unevaluated context (the args list of a 'gang' or14763 // 'tile' clause).14764 return Error(E);14765 }14766 14767 bool VisitUnaryReal(const UnaryOperator *E);14768 bool VisitUnaryImag(const UnaryOperator *E);14769 14770 bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E);14771 bool VisitSizeOfPackExpr(const SizeOfPackExpr *E);14772 bool VisitSourceLocExpr(const SourceLocExpr *E);14773 bool VisitConceptSpecializationExpr(const ConceptSpecializationExpr *E);14774 bool VisitRequiresExpr(const RequiresExpr *E);14775 // FIXME: Missing: array subscript of vector, member of vector14776};14777 14778class FixedPointExprEvaluator14779 : public ExprEvaluatorBase<FixedPointExprEvaluator> {14780 APValue &Result;14781 14782 public:14783 FixedPointExprEvaluator(EvalInfo &info, APValue &result)14784 : ExprEvaluatorBaseTy(info), Result(result) {}14785 14786 bool Success(const llvm::APInt &I, const Expr *E) {14787 return Success(14788 APFixedPoint(I, Info.Ctx.getFixedPointSemantics(E->getType())), E);14789 }14790 14791 bool Success(uint64_t Value, const Expr *E) {14792 return Success(14793 APFixedPoint(Value, Info.Ctx.getFixedPointSemantics(E->getType())), E);14794 }14795 14796 bool Success(const APValue &V, const Expr *E) {14797 return Success(V.getFixedPoint(), E);14798 }14799 14800 bool Success(const APFixedPoint &V, const Expr *E) {14801 assert(E->getType()->isFixedPointType() && "Invalid evaluation result.");14802 assert(V.getWidth() == Info.Ctx.getIntWidth(E->getType()) &&14803 "Invalid evaluation result.");14804 Result = APValue(V);14805 return true;14806 }14807 14808 bool ZeroInitialization(const Expr *E) {14809 return Success(0, E);14810 }14811 14812 //===--------------------------------------------------------------------===//14813 // Visitor Methods14814 //===--------------------------------------------------------------------===//14815 14816 bool VisitFixedPointLiteral(const FixedPointLiteral *E) {14817 return Success(E->getValue(), E);14818 }14819 14820 bool VisitCastExpr(const CastExpr *E);14821 bool VisitUnaryOperator(const UnaryOperator *E);14822 bool VisitBinaryOperator(const BinaryOperator *E);14823};14824} // end anonymous namespace14825 14826/// EvaluateIntegerOrLValue - Evaluate an rvalue integral-typed expression, and14827/// produce either the integer value or a pointer.14828///14829/// GCC has a heinous extension which folds casts between pointer types and14830/// pointer-sized integral types. We support this by allowing the evaluation of14831/// an integer rvalue to produce a pointer (represented as an lvalue) instead.14832/// Some simple arithmetic on such values is supported (they are treated much14833/// like char*).14834static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result,14835 EvalInfo &Info) {14836 assert(!E->isValueDependent());14837 assert(E->isPRValue() && E->getType()->isIntegralOrEnumerationType());14838 return IntExprEvaluator(Info, Result).Visit(E);14839}14840 14841static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info) {14842 assert(!E->isValueDependent());14843 APValue Val;14844 if (!EvaluateIntegerOrLValue(E, Val, Info))14845 return false;14846 if (!Val.isInt()) {14847 // FIXME: It would be better to produce the diagnostic for casting14848 // a pointer to an integer.14849 Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr);14850 return false;14851 }14852 Result = Val.getInt();14853 return true;14854}14855 14856bool IntExprEvaluator::VisitSourceLocExpr(const SourceLocExpr *E) {14857 APValue Evaluated = E->EvaluateInContext(14858 Info.Ctx, Info.CurrentCall->CurSourceLocExprScope.getDefaultExpr());14859 return Success(Evaluated, E);14860}14861 14862static bool EvaluateFixedPoint(const Expr *E, APFixedPoint &Result,14863 EvalInfo &Info) {14864 assert(!E->isValueDependent());14865 if (E->getType()->isFixedPointType()) {14866 APValue Val;14867 if (!FixedPointExprEvaluator(Info, Val).Visit(E))14868 return false;14869 if (!Val.isFixedPoint())14870 return false;14871 14872 Result = Val.getFixedPoint();14873 return true;14874 }14875 return false;14876}14877 14878static bool EvaluateFixedPointOrInteger(const Expr *E, APFixedPoint &Result,14879 EvalInfo &Info) {14880 assert(!E->isValueDependent());14881 if (E->getType()->isIntegerType()) {14882 auto FXSema = Info.Ctx.getFixedPointSemantics(E->getType());14883 APSInt Val;14884 if (!EvaluateInteger(E, Val, Info))14885 return false;14886 Result = APFixedPoint(Val, FXSema);14887 return true;14888 } else if (E->getType()->isFixedPointType()) {14889 return EvaluateFixedPoint(E, Result, Info);14890 }14891 return false;14892}14893 14894/// Check whether the given declaration can be directly converted to an integral14895/// rvalue. If not, no diagnostic is produced; there are other things we can14896/// try.14897bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) {14898 // Enums are integer constant exprs.14899 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) {14900 // Check for signedness/width mismatches between E type and ECD value.14901 bool SameSign = (ECD->getInitVal().isSigned()14902 == E->getType()->isSignedIntegerOrEnumerationType());14903 bool SameWidth = (ECD->getInitVal().getBitWidth()14904 == Info.Ctx.getIntWidth(E->getType()));14905 if (SameSign && SameWidth)14906 return Success(ECD->getInitVal(), E);14907 else {14908 // Get rid of mismatch (otherwise Success assertions will fail)14909 // by computing a new value matching the type of E.14910 llvm::APSInt Val = ECD->getInitVal();14911 if (!SameSign)14912 Val.setIsSigned(!ECD->getInitVal().isSigned());14913 if (!SameWidth)14914 Val = Val.extOrTrunc(Info.Ctx.getIntWidth(E->getType()));14915 return Success(Val, E);14916 }14917 }14918 return false;14919}14920 14921/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way14922/// as GCC.14923GCCTypeClass EvaluateBuiltinClassifyType(QualType T,14924 const LangOptions &LangOpts) {14925 assert(!T->isDependentType() && "unexpected dependent type");14926 14927 QualType CanTy = T.getCanonicalType();14928 14929 switch (CanTy->getTypeClass()) {14930#define TYPE(ID, BASE)14931#define DEPENDENT_TYPE(ID, BASE) case Type::ID:14932#define NON_CANONICAL_TYPE(ID, BASE) case Type::ID:14933#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(ID, BASE) case Type::ID:14934#include "clang/AST/TypeNodes.inc"14935 case Type::Auto:14936 case Type::DeducedTemplateSpecialization:14937 llvm_unreachable("unexpected non-canonical or dependent type");14938 14939 case Type::Builtin:14940 switch (cast<BuiltinType>(CanTy)->getKind()) {14941#define BUILTIN_TYPE(ID, SINGLETON_ID)14942#define SIGNED_TYPE(ID, SINGLETON_ID) \14943 case BuiltinType::ID: return GCCTypeClass::Integer;14944#define FLOATING_TYPE(ID, SINGLETON_ID) \14945 case BuiltinType::ID: return GCCTypeClass::RealFloat;14946#define PLACEHOLDER_TYPE(ID, SINGLETON_ID) \14947 case BuiltinType::ID: break;14948#include "clang/AST/BuiltinTypes.def"14949 case BuiltinType::Void:14950 return GCCTypeClass::Void;14951 14952 case BuiltinType::Bool:14953 return GCCTypeClass::Bool;14954 14955 case BuiltinType::Char_U:14956 case BuiltinType::UChar:14957 case BuiltinType::WChar_U:14958 case BuiltinType::Char8:14959 case BuiltinType::Char16:14960 case BuiltinType::Char32:14961 case BuiltinType::UShort:14962 case BuiltinType::UInt:14963 case BuiltinType::ULong:14964 case BuiltinType::ULongLong:14965 case BuiltinType::UInt128:14966 return GCCTypeClass::Integer;14967 14968 case BuiltinType::UShortAccum:14969 case BuiltinType::UAccum:14970 case BuiltinType::ULongAccum:14971 case BuiltinType::UShortFract:14972 case BuiltinType::UFract:14973 case BuiltinType::ULongFract:14974 case BuiltinType::SatUShortAccum:14975 case BuiltinType::SatUAccum:14976 case BuiltinType::SatULongAccum:14977 case BuiltinType::SatUShortFract:14978 case BuiltinType::SatUFract:14979 case BuiltinType::SatULongFract:14980 return GCCTypeClass::None;14981 14982 case BuiltinType::NullPtr:14983 14984 case BuiltinType::ObjCId:14985 case BuiltinType::ObjCClass:14986 case BuiltinType::ObjCSel:14987#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \14988 case BuiltinType::Id:14989#include "clang/Basic/OpenCLImageTypes.def"14990#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \14991 case BuiltinType::Id:14992#include "clang/Basic/OpenCLExtensionTypes.def"14993 case BuiltinType::OCLSampler:14994 case BuiltinType::OCLEvent:14995 case BuiltinType::OCLClkEvent:14996 case BuiltinType::OCLQueue:14997 case BuiltinType::OCLReserveID:14998#define SVE_TYPE(Name, Id, SingletonId) \14999 case BuiltinType::Id:15000#include "clang/Basic/AArch64ACLETypes.def"15001#define PPC_VECTOR_TYPE(Name, Id, Size) \15002 case BuiltinType::Id:15003#include "clang/Basic/PPCTypes.def"15004#define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id:15005#include "clang/Basic/RISCVVTypes.def"15006#define WASM_TYPE(Name, Id, SingletonId) case BuiltinType::Id:15007#include "clang/Basic/WebAssemblyReferenceTypes.def"15008#define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) case BuiltinType::Id:15009#include "clang/Basic/AMDGPUTypes.def"15010#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) case BuiltinType::Id:15011#include "clang/Basic/HLSLIntangibleTypes.def"15012 return GCCTypeClass::None;15013 15014 case BuiltinType::Dependent:15015 llvm_unreachable("unexpected dependent type");15016 };15017 llvm_unreachable("unexpected placeholder type");15018 15019 case Type::Enum:15020 return LangOpts.CPlusPlus ? GCCTypeClass::Enum : GCCTypeClass::Integer;15021 15022 case Type::Pointer:15023 case Type::ConstantArray:15024 case Type::VariableArray:15025 case Type::IncompleteArray:15026 case Type::FunctionNoProto:15027 case Type::FunctionProto:15028 case Type::ArrayParameter:15029 return GCCTypeClass::Pointer;15030 15031 case Type::MemberPointer:15032 return CanTy->isMemberDataPointerType()15033 ? GCCTypeClass::PointerToDataMember15034 : GCCTypeClass::PointerToMemberFunction;15035 15036 case Type::Complex:15037 return GCCTypeClass::Complex;15038 15039 case Type::Record:15040 return CanTy->isUnionType() ? GCCTypeClass::Union15041 : GCCTypeClass::ClassOrStruct;15042 15043 case Type::Atomic:15044 // GCC classifies _Atomic T the same as T.15045 return EvaluateBuiltinClassifyType(15046 CanTy->castAs<AtomicType>()->getValueType(), LangOpts);15047 15048 case Type::Vector:15049 case Type::ExtVector:15050 return GCCTypeClass::Vector;15051 15052 case Type::BlockPointer:15053 case Type::ConstantMatrix:15054 case Type::ObjCObject:15055 case Type::ObjCInterface:15056 case Type::ObjCObjectPointer:15057 case Type::Pipe:15058 case Type::HLSLAttributedResource:15059 case Type::HLSLInlineSpirv:15060 // Classify all other types that don't fit into the regular15061 // classification the same way.15062 return GCCTypeClass::None;15063 15064 case Type::BitInt:15065 return GCCTypeClass::BitInt;15066 15067 case Type::LValueReference:15068 case Type::RValueReference:15069 llvm_unreachable("invalid type for expression");15070 }15071 15072 llvm_unreachable("unexpected type class");15073}15074 15075/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way15076/// as GCC.15077static GCCTypeClass15078EvaluateBuiltinClassifyType(const CallExpr *E, const LangOptions &LangOpts) {15079 // If no argument was supplied, default to None. This isn't15080 // ideal, however it is what gcc does.15081 if (E->getNumArgs() == 0)15082 return GCCTypeClass::None;15083 15084 // FIXME: Bizarrely, GCC treats a call with more than one argument as not15085 // being an ICE, but still folds it to a constant using the type of the first15086 // argument.15087 return EvaluateBuiltinClassifyType(E->getArg(0)->getType(), LangOpts);15088}15089 15090/// EvaluateBuiltinConstantPForLValue - Determine the result of15091/// __builtin_constant_p when applied to the given pointer.15092///15093/// A pointer is only "constant" if it is null (or a pointer cast to integer)15094/// or it points to the first character of a string literal.15095static bool EvaluateBuiltinConstantPForLValue(const APValue &LV) {15096 APValue::LValueBase Base = LV.getLValueBase();15097 if (Base.isNull()) {15098 // A null base is acceptable.15099 return true;15100 } else if (const Expr *E = Base.dyn_cast<const Expr *>()) {15101 if (!isa<StringLiteral>(E))15102 return false;15103 return LV.getLValueOffset().isZero();15104 } else if (Base.is<TypeInfoLValue>()) {15105 // Surprisingly, GCC considers __builtin_constant_p(&typeid(int)) to15106 // evaluate to true.15107 return true;15108 } else {15109 // Any other base is not constant enough for GCC.15110 return false;15111 }15112}15113 15114/// EvaluateBuiltinConstantP - Evaluate __builtin_constant_p as similarly to15115/// GCC as we can manage.15116static bool EvaluateBuiltinConstantP(EvalInfo &Info, const Expr *Arg) {15117 // This evaluation is not permitted to have side-effects, so evaluate it in15118 // a speculative evaluation context.15119 SpeculativeEvaluationRAII SpeculativeEval(Info);15120 15121 // Constant-folding is always enabled for the operand of __builtin_constant_p15122 // (even when the enclosing evaluation context otherwise requires a strict15123 // language-specific constant expression).15124 FoldConstant Fold(Info, true);15125 15126 QualType ArgType = Arg->getType();15127 15128 // __builtin_constant_p always has one operand. The rules which gcc follows15129 // are not precisely documented, but are as follows:15130 //15131 // - If the operand is of integral, floating, complex or enumeration type,15132 // and can be folded to a known value of that type, it returns 1.15133 // - If the operand can be folded to a pointer to the first character15134 // of a string literal (or such a pointer cast to an integral type)15135 // or to a null pointer or an integer cast to a pointer, it returns 1.15136 //15137 // Otherwise, it returns 0.15138 //15139 // FIXME: GCC also intends to return 1 for literals of aggregate types, but15140 // its support for this did not work prior to GCC 9 and is not yet well15141 // understood.15142 if (ArgType->isIntegralOrEnumerationType() || ArgType->isFloatingType() ||15143 ArgType->isAnyComplexType() || ArgType->isPointerType() ||15144 ArgType->isNullPtrType()) {15145 APValue V;15146 if (!::EvaluateAsRValue(Info, Arg, V) || Info.EvalStatus.HasSideEffects) {15147 Fold.keepDiagnostics();15148 return false;15149 }15150 15151 // For a pointer (possibly cast to integer), there are special rules.15152 if (V.getKind() == APValue::LValue)15153 return EvaluateBuiltinConstantPForLValue(V);15154 15155 // Otherwise, any constant value is good enough.15156 return V.hasValue();15157 }15158 15159 // Anything else isn't considered to be sufficiently constant.15160 return false;15161}15162 15163/// Retrieves the "underlying object type" of the given expression,15164/// as used by __builtin_object_size.15165static QualType getObjectType(APValue::LValueBase B) {15166 if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) {15167 if (const VarDecl *VD = dyn_cast<VarDecl>(D))15168 return VD->getType();15169 } else if (const Expr *E = B.dyn_cast<const Expr*>()) {15170 if (isa<CompoundLiteralExpr>(E))15171 return E->getType();15172 } else if (B.is<TypeInfoLValue>()) {15173 return B.getTypeInfoType();15174 } else if (B.is<DynamicAllocLValue>()) {15175 return B.getDynamicAllocType();15176 }15177 15178 return QualType();15179}15180 15181/// A more selective version of E->IgnoreParenCasts for15182/// tryEvaluateBuiltinObjectSize. This ignores some casts/parens that serve only15183/// to change the type of E.15184/// Ex. For E = `(short*)((char*)(&foo))`, returns `&foo`15185///15186/// Always returns an RValue with a pointer representation.15187static const Expr *ignorePointerCastsAndParens(const Expr *E) {15188 assert(E->isPRValue() && E->getType()->hasPointerRepresentation());15189 15190 const Expr *NoParens = E->IgnoreParens();15191 const auto *Cast = dyn_cast<CastExpr>(NoParens);15192 if (Cast == nullptr)15193 return NoParens;15194 15195 // We only conservatively allow a few kinds of casts, because this code is15196 // inherently a simple solution that seeks to support the common case.15197 auto CastKind = Cast->getCastKind();15198 if (CastKind != CK_NoOp && CastKind != CK_BitCast &&15199 CastKind != CK_AddressSpaceConversion)15200 return NoParens;15201 15202 const auto *SubExpr = Cast->getSubExpr();15203 if (!SubExpr->getType()->hasPointerRepresentation() || !SubExpr->isPRValue())15204 return NoParens;15205 return ignorePointerCastsAndParens(SubExpr);15206}15207 15208/// Checks to see if the given LValue's Designator is at the end of the LValue's15209/// record layout. e.g.15210/// struct { struct { int a, b; } fst, snd; } obj;15211/// obj.fst // no15212/// obj.snd // yes15213/// obj.fst.a // no15214/// obj.fst.b // no15215/// obj.snd.a // no15216/// obj.snd.b // yes15217///15218/// Please note: this function is specialized for how __builtin_object_size15219/// views "objects".15220///15221/// If this encounters an invalid RecordDecl or otherwise cannot determine the15222/// correct result, it will always return true.15223static bool isDesignatorAtObjectEnd(const ASTContext &Ctx, const LValue &LVal) {15224 assert(!LVal.Designator.Invalid);15225 15226 auto IsLastOrInvalidFieldDecl = [&Ctx](const FieldDecl *FD) {15227 const RecordDecl *Parent = FD->getParent();15228 if (Parent->isInvalidDecl() || Parent->isUnion())15229 return true;15230 const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(Parent);15231 return FD->getFieldIndex() + 1 == Layout.getFieldCount();15232 };15233 15234 auto &Base = LVal.getLValueBase();15235 if (auto *ME = dyn_cast_or_null<MemberExpr>(Base.dyn_cast<const Expr *>())) {15236 if (auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) {15237 if (!IsLastOrInvalidFieldDecl(FD))15238 return false;15239 } else if (auto *IFD = dyn_cast<IndirectFieldDecl>(ME->getMemberDecl())) {15240 for (auto *FD : IFD->chain()) {15241 if (!IsLastOrInvalidFieldDecl(cast<FieldDecl>(FD)))15242 return false;15243 }15244 }15245 }15246 15247 unsigned I = 0;15248 QualType BaseType = getType(Base);15249 if (LVal.Designator.FirstEntryIsAnUnsizedArray) {15250 // If we don't know the array bound, conservatively assume we're looking at15251 // the final array element.15252 ++I;15253 if (BaseType->isIncompleteArrayType())15254 BaseType = Ctx.getAsArrayType(BaseType)->getElementType();15255 else15256 BaseType = BaseType->castAs<PointerType>()->getPointeeType();15257 }15258 15259 for (unsigned E = LVal.Designator.Entries.size(); I != E; ++I) {15260 const auto &Entry = LVal.Designator.Entries[I];15261 if (BaseType->isArrayType()) {15262 // Because __builtin_object_size treats arrays as objects, we can ignore15263 // the index iff this is the last array in the Designator.15264 if (I + 1 == E)15265 return true;15266 const auto *CAT = cast<ConstantArrayType>(Ctx.getAsArrayType(BaseType));15267 uint64_t Index = Entry.getAsArrayIndex();15268 if (Index + 1 != CAT->getZExtSize())15269 return false;15270 BaseType = CAT->getElementType();15271 } else if (BaseType->isAnyComplexType()) {15272 const auto *CT = BaseType->castAs<ComplexType>();15273 uint64_t Index = Entry.getAsArrayIndex();15274 if (Index != 1)15275 return false;15276 BaseType = CT->getElementType();15277 } else if (auto *FD = getAsField(Entry)) {15278 if (!IsLastOrInvalidFieldDecl(FD))15279 return false;15280 BaseType = FD->getType();15281 } else {15282 assert(getAsBaseClass(Entry) && "Expecting cast to a base class");15283 return false;15284 }15285 }15286 return true;15287}15288 15289/// Tests to see if the LValue has a user-specified designator (that isn't15290/// necessarily valid). Note that this always returns 'true' if the LValue has15291/// an unsized array as its first designator entry, because there's currently no15292/// way to tell if the user typed *foo or foo[0].15293static bool refersToCompleteObject(const LValue &LVal) {15294 if (LVal.Designator.Invalid)15295 return false;15296 15297 if (!LVal.Designator.Entries.empty())15298 return LVal.Designator.isMostDerivedAnUnsizedArray();15299 15300 if (!LVal.InvalidBase)15301 return true;15302 15303 // If `E` is a MemberExpr, then the first part of the designator is hiding in15304 // the LValueBase.15305 const auto *E = LVal.Base.dyn_cast<const Expr *>();15306 return !E || !isa<MemberExpr>(E);15307}15308 15309/// Attempts to detect a user writing into a piece of memory that's impossible15310/// to figure out the size of by just using types.15311static bool isUserWritingOffTheEnd(const ASTContext &Ctx, const LValue &LVal) {15312 const SubobjectDesignator &Designator = LVal.Designator;15313 // Notes:15314 // - Users can only write off of the end when we have an invalid base. Invalid15315 // bases imply we don't know where the memory came from.15316 // - We used to be a bit more aggressive here; we'd only be conservative if15317 // the array at the end was flexible, or if it had 0 or 1 elements. This15318 // broke some common standard library extensions (PR30346), but was15319 // otherwise seemingly fine. It may be useful to reintroduce this behavior15320 // with some sort of list. OTOH, it seems that GCC is always15321 // conservative with the last element in structs (if it's an array), so our15322 // current behavior is more compatible than an explicit list approach would15323 // be.15324 auto isFlexibleArrayMember = [&] {15325 using FAMKind = LangOptions::StrictFlexArraysLevelKind;15326 FAMKind StrictFlexArraysLevel =15327 Ctx.getLangOpts().getStrictFlexArraysLevel();15328 15329 if (Designator.isMostDerivedAnUnsizedArray())15330 return true;15331 15332 if (StrictFlexArraysLevel == FAMKind::Default)15333 return true;15334 15335 if (Designator.getMostDerivedArraySize() == 0 &&15336 StrictFlexArraysLevel != FAMKind::IncompleteOnly)15337 return true;15338 15339 if (Designator.getMostDerivedArraySize() == 1 &&15340 StrictFlexArraysLevel == FAMKind::OneZeroOrIncomplete)15341 return true;15342 15343 return false;15344 };15345 15346 return LVal.InvalidBase &&15347 Designator.Entries.size() == Designator.MostDerivedPathLength &&15348 Designator.MostDerivedIsArrayElement && isFlexibleArrayMember() &&15349 isDesignatorAtObjectEnd(Ctx, LVal);15350}15351 15352/// Converts the given APInt to CharUnits, assuming the APInt is unsigned.15353/// Fails if the conversion would cause loss of precision.15354static bool convertUnsignedAPIntToCharUnits(const llvm::APInt &Int,15355 CharUnits &Result) {15356 auto CharUnitsMax = std::numeric_limits<CharUnits::QuantityType>::max();15357 if (Int.ugt(CharUnitsMax))15358 return false;15359 Result = CharUnits::fromQuantity(Int.getZExtValue());15360 return true;15361}15362 15363/// If we're evaluating the object size of an instance of a struct that15364/// contains a flexible array member, add the size of the initializer.15365static void addFlexibleArrayMemberInitSize(EvalInfo &Info, const QualType &T,15366 const LValue &LV, CharUnits &Size) {15367 if (!T.isNull() && T->isStructureType() &&15368 T->castAsRecordDecl()->hasFlexibleArrayMember())15369 if (const auto *V = LV.getLValueBase().dyn_cast<const ValueDecl *>())15370 if (const auto *VD = dyn_cast<VarDecl>(V))15371 if (VD->hasInit())15372 Size += VD->getFlexibleArrayInitChars(Info.Ctx);15373}15374 15375/// Helper for tryEvaluateBuiltinObjectSize -- Given an LValue, this will15376/// determine how many bytes exist from the beginning of the object to either15377/// the end of the current subobject, or the end of the object itself, depending15378/// on what the LValue looks like + the value of Type.15379///15380/// If this returns false, the value of Result is undefined.15381static bool determineEndOffset(EvalInfo &Info, SourceLocation ExprLoc,15382 unsigned Type, const LValue &LVal,15383 CharUnits &EndOffset) {15384 bool DetermineForCompleteObject = refersToCompleteObject(LVal);15385 15386 auto CheckedHandleSizeof = [&](QualType Ty, CharUnits &Result) {15387 if (Ty.isNull())15388 return false;15389 15390 Ty = Ty.getNonReferenceType();15391 15392 if (Ty->isIncompleteType() || Ty->isFunctionType())15393 return false;15394 15395 return HandleSizeof(Info, ExprLoc, Ty, Result);15396 };15397 15398 // We want to evaluate the size of the entire object. This is a valid fallback15399 // for when Type=1 and the designator is invalid, because we're asked for an15400 // upper-bound.15401 if (!(Type & 1) || LVal.Designator.Invalid || DetermineForCompleteObject) {15402 // Type=3 wants a lower bound, so we can't fall back to this.15403 if (Type == 3 && !DetermineForCompleteObject)15404 return false;15405 15406 llvm::APInt APEndOffset;15407 if (isBaseAnAllocSizeCall(LVal.getLValueBase()) &&15408 getBytesReturnedByAllocSizeCall(Info.Ctx, LVal, APEndOffset))15409 return convertUnsignedAPIntToCharUnits(APEndOffset, EndOffset);15410 15411 if (LVal.InvalidBase)15412 return false;15413 15414 QualType BaseTy = getObjectType(LVal.getLValueBase());15415 const bool Ret = CheckedHandleSizeof(BaseTy, EndOffset);15416 addFlexibleArrayMemberInitSize(Info, BaseTy, LVal, EndOffset);15417 return Ret;15418 }15419 15420 // We want to evaluate the size of a subobject.15421 const SubobjectDesignator &Designator = LVal.Designator;15422 15423 // The following is a moderately common idiom in C:15424 //15425 // struct Foo { int a; char c[1]; };15426 // struct Foo *F = (struct Foo *)malloc(sizeof(struct Foo) + strlen(Bar));15427 // strcpy(&F->c[0], Bar);15428 //15429 // In order to not break too much legacy code, we need to support it.15430 if (isUserWritingOffTheEnd(Info.Ctx, LVal)) {15431 // If we can resolve this to an alloc_size call, we can hand that back,15432 // because we know for certain how many bytes there are to write to.15433 llvm::APInt APEndOffset;15434 if (isBaseAnAllocSizeCall(LVal.getLValueBase()) &&15435 getBytesReturnedByAllocSizeCall(Info.Ctx, LVal, APEndOffset))15436 return convertUnsignedAPIntToCharUnits(APEndOffset, EndOffset);15437 15438 // If we cannot determine the size of the initial allocation, then we can't15439 // given an accurate upper-bound. However, we are still able to give15440 // conservative lower-bounds for Type=3.15441 if (Type == 1)15442 return false;15443 }15444 15445 CharUnits BytesPerElem;15446 if (!CheckedHandleSizeof(Designator.MostDerivedType, BytesPerElem))15447 return false;15448 15449 // According to the GCC documentation, we want the size of the subobject15450 // denoted by the pointer. But that's not quite right -- what we actually15451 // want is the size of the immediately-enclosing array, if there is one.15452 int64_t ElemsRemaining;15453 if (Designator.MostDerivedIsArrayElement &&15454 Designator.Entries.size() == Designator.MostDerivedPathLength) {15455 uint64_t ArraySize = Designator.getMostDerivedArraySize();15456 uint64_t ArrayIndex = Designator.Entries.back().getAsArrayIndex();15457 ElemsRemaining = ArraySize <= ArrayIndex ? 0 : ArraySize - ArrayIndex;15458 } else {15459 ElemsRemaining = Designator.isOnePastTheEnd() ? 0 : 1;15460 }15461 15462 EndOffset = LVal.getLValueOffset() + BytesPerElem * ElemsRemaining;15463 return true;15464}15465 15466/// Tries to evaluate the __builtin_object_size for @p E. If successful,15467/// returns true and stores the result in @p Size.15468///15469/// If @p WasError is non-null, this will report whether the failure to evaluate15470/// is to be treated as an Error in IntExprEvaluator.15471static bool tryEvaluateBuiltinObjectSize(const Expr *E, unsigned Type,15472 EvalInfo &Info, uint64_t &Size) {15473 // Determine the denoted object.15474 LValue LVal;15475 {15476 // The operand of __builtin_object_size is never evaluated for side-effects.15477 // If there are any, but we can determine the pointed-to object anyway, then15478 // ignore the side-effects.15479 SpeculativeEvaluationRAII SpeculativeEval(Info);15480 IgnoreSideEffectsRAII Fold(Info);15481 15482 if (E->isGLValue()) {15483 // It's possible for us to be given GLValues if we're called via15484 // Expr::tryEvaluateObjectSize.15485 APValue RVal;15486 if (!EvaluateAsRValue(Info, E, RVal))15487 return false;15488 LVal.setFrom(Info.Ctx, RVal);15489 } else if (!EvaluatePointer(ignorePointerCastsAndParens(E), LVal, Info,15490 /*InvalidBaseOK=*/true))15491 return false;15492 }15493 15494 // If we point to before the start of the object, there are no accessible15495 // bytes.15496 if (LVal.getLValueOffset().isNegative()) {15497 Size = 0;15498 return true;15499 }15500 15501 CharUnits EndOffset;15502 if (!determineEndOffset(Info, E->getExprLoc(), Type, LVal, EndOffset))15503 return false;15504 15505 // If we've fallen outside of the end offset, just pretend there's nothing to15506 // write to/read from.15507 if (EndOffset <= LVal.getLValueOffset())15508 Size = 0;15509 else15510 Size = (EndOffset - LVal.getLValueOffset()).getQuantity();15511 return true;15512}15513 15514bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {15515 if (!IsConstantEvaluatedBuiltinCall(E))15516 return ExprEvaluatorBaseTy::VisitCallExpr(E);15517 return VisitBuiltinCallExpr(E, E->getBuiltinCallee());15518}15519 15520static bool getBuiltinAlignArguments(const CallExpr *E, EvalInfo &Info,15521 APValue &Val, APSInt &Alignment) {15522 QualType SrcTy = E->getArg(0)->getType();15523 if (!getAlignmentArgument(E->getArg(1), SrcTy, Info, Alignment))15524 return false;15525 // Even though we are evaluating integer expressions we could get a pointer15526 // argument for the __builtin_is_aligned() case.15527 if (SrcTy->isPointerType()) {15528 LValue Ptr;15529 if (!EvaluatePointer(E->getArg(0), Ptr, Info))15530 return false;15531 Ptr.moveInto(Val);15532 } else if (!SrcTy->isIntegralOrEnumerationType()) {15533 Info.FFDiag(E->getArg(0));15534 return false;15535 } else {15536 APSInt SrcInt;15537 if (!EvaluateInteger(E->getArg(0), SrcInt, Info))15538 return false;15539 assert(SrcInt.getBitWidth() >= Alignment.getBitWidth() &&15540 "Bit widths must be the same");15541 Val = APValue(SrcInt);15542 }15543 assert(Val.hasValue());15544 return true;15545}15546 15547bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,15548 unsigned BuiltinOp) {15549 auto EvalTestOp = [&](llvm::function_ref<bool(const APInt &, const APInt &)>15550 Fn) {15551 APValue SourceLHS, SourceRHS;15552 if (!EvaluateAsRValue(Info, E->getArg(0), SourceLHS) ||15553 !EvaluateAsRValue(Info, E->getArg(1), SourceRHS))15554 return false;15555 15556 unsigned SourceLen = SourceLHS.getVectorLength();15557 const VectorType *VT = E->getArg(0)->getType()->castAs<VectorType>();15558 QualType ElemQT = VT->getElementType();15559 unsigned LaneWidth = Info.Ctx.getTypeSize(ElemQT);15560 15561 APInt AWide(LaneWidth * SourceLen, 0);15562 APInt BWide(LaneWidth * SourceLen, 0);15563 15564 for (unsigned I = 0; I != SourceLen; ++I) {15565 APInt ALane;15566 APInt BLane;15567 if (ElemQT->isIntegerType()) { // Get value.15568 ALane = SourceLHS.getVectorElt(I).getInt();15569 BLane = SourceRHS.getVectorElt(I).getInt();15570 } else if (ElemQT->isFloatingType()) { // Get only sign bit.15571 ALane =15572 SourceLHS.getVectorElt(I).getFloat().bitcastToAPInt().isNegative();15573 BLane =15574 SourceRHS.getVectorElt(I).getFloat().bitcastToAPInt().isNegative();15575 } else { // Must be integer or floating type.15576 return false;15577 }15578 AWide.insertBits(ALane, I * LaneWidth);15579 BWide.insertBits(BLane, I * LaneWidth);15580 }15581 return Success(Fn(AWide, BWide), E);15582 };15583 15584 auto HandleMaskBinOp =15585 [&](llvm::function_ref<APSInt(const APSInt &, const APSInt &)> Fn)15586 -> bool {15587 APValue LHS, RHS;15588 if (!Evaluate(LHS, Info, E->getArg(0)) ||15589 !Evaluate(RHS, Info, E->getArg(1)))15590 return false;15591 15592 APSInt ResultInt = Fn(LHS.getInt(), RHS.getInt());15593 15594 return Success(APValue(ResultInt), E);15595 };15596 15597 switch (BuiltinOp) {15598 default:15599 return false;15600 15601 case Builtin::BI__builtin_dynamic_object_size:15602 case Builtin::BI__builtin_object_size: {15603 // The type was checked when we built the expression.15604 unsigned Type =15605 E->getArg(1)->EvaluateKnownConstInt(Info.Ctx).getZExtValue();15606 assert(Type <= 3 && "unexpected type");15607 15608 uint64_t Size;15609 if (tryEvaluateBuiltinObjectSize(E->getArg(0), Type, Info, Size))15610 return Success(Size, E);15611 15612 if (E->getArg(0)->HasSideEffects(Info.Ctx))15613 return Success((Type & 2) ? 0 : -1, E);15614 15615 // Expression had no side effects, but we couldn't statically determine the15616 // size of the referenced object.15617 switch (Info.EvalMode) {15618 case EvaluationMode::ConstantExpression:15619 case EvaluationMode::ConstantFold:15620 case EvaluationMode::IgnoreSideEffects:15621 // Leave it to IR generation.15622 return Error(E);15623 case EvaluationMode::ConstantExpressionUnevaluated:15624 // Reduce it to a constant now.15625 return Success((Type & 2) ? 0 : -1, E);15626 }15627 15628 llvm_unreachable("unexpected EvalMode");15629 }15630 15631 case Builtin::BI__builtin_os_log_format_buffer_size: {15632 analyze_os_log::OSLogBufferLayout Layout;15633 analyze_os_log::computeOSLogBufferLayout(Info.Ctx, E, Layout);15634 return Success(Layout.size().getQuantity(), E);15635 }15636 15637 case Builtin::BI__builtin_is_aligned: {15638 APValue Src;15639 APSInt Alignment;15640 if (!getBuiltinAlignArguments(E, Info, Src, Alignment))15641 return false;15642 if (Src.isLValue()) {15643 // If we evaluated a pointer, check the minimum known alignment.15644 LValue Ptr;15645 Ptr.setFrom(Info.Ctx, Src);15646 CharUnits BaseAlignment = getBaseAlignment(Info, Ptr);15647 CharUnits PtrAlign = BaseAlignment.alignmentAtOffset(Ptr.Offset);15648 // We can return true if the known alignment at the computed offset is15649 // greater than the requested alignment.15650 assert(PtrAlign.isPowerOfTwo());15651 assert(Alignment.isPowerOf2());15652 if (PtrAlign.getQuantity() >= Alignment)15653 return Success(1, E);15654 // If the alignment is not known to be sufficient, some cases could still15655 // be aligned at run time. However, if the requested alignment is less or15656 // equal to the base alignment and the offset is not aligned, we know that15657 // the run-time value can never be aligned.15658 if (BaseAlignment.getQuantity() >= Alignment &&15659 PtrAlign.getQuantity() < Alignment)15660 return Success(0, E);15661 // Otherwise we can't infer whether the value is sufficiently aligned.15662 // TODO: __builtin_is_aligned(__builtin_align_{down,up{(expr, N), N)15663 // in cases where we can't fully evaluate the pointer.15664 Info.FFDiag(E->getArg(0), diag::note_constexpr_alignment_compute)15665 << Alignment;15666 return false;15667 }15668 assert(Src.isInt());15669 return Success((Src.getInt() & (Alignment - 1)) == 0 ? 1 : 0, E);15670 }15671 case Builtin::BI__builtin_align_up: {15672 APValue Src;15673 APSInt Alignment;15674 if (!getBuiltinAlignArguments(E, Info, Src, Alignment))15675 return false;15676 if (!Src.isInt())15677 return Error(E);15678 APSInt AlignedVal =15679 APSInt((Src.getInt() + (Alignment - 1)) & ~(Alignment - 1),15680 Src.getInt().isUnsigned());15681 assert(AlignedVal.getBitWidth() == Src.getInt().getBitWidth());15682 return Success(AlignedVal, E);15683 }15684 case Builtin::BI__builtin_align_down: {15685 APValue Src;15686 APSInt Alignment;15687 if (!getBuiltinAlignArguments(E, Info, Src, Alignment))15688 return false;15689 if (!Src.isInt())15690 return Error(E);15691 APSInt AlignedVal =15692 APSInt(Src.getInt() & ~(Alignment - 1), Src.getInt().isUnsigned());15693 assert(AlignedVal.getBitWidth() == Src.getInt().getBitWidth());15694 return Success(AlignedVal, E);15695 }15696 15697 case Builtin::BI__builtin_bitreverse8:15698 case Builtin::BI__builtin_bitreverse16:15699 case Builtin::BI__builtin_bitreverse32:15700 case Builtin::BI__builtin_bitreverse64:15701 case Builtin::BI__builtin_elementwise_bitreverse: {15702 APSInt Val;15703 if (!EvaluateInteger(E->getArg(0), Val, Info))15704 return false;15705 15706 return Success(Val.reverseBits(), E);15707 }15708 case Builtin::BI__builtin_bswapg:15709 case Builtin::BI__builtin_bswap16:15710 case Builtin::BI__builtin_bswap32:15711 case Builtin::BI__builtin_bswap64: {15712 APSInt Val;15713 if (!EvaluateInteger(E->getArg(0), Val, Info))15714 return false;15715 if (Val.getBitWidth() == 8)15716 return Success(Val, E);15717 15718 return Success(Val.byteSwap(), E);15719 }15720 15721 case Builtin::BI__builtin_classify_type:15722 return Success((int)EvaluateBuiltinClassifyType(E, Info.getLangOpts()), E);15723 15724 case Builtin::BI__builtin_clrsb:15725 case Builtin::BI__builtin_clrsbl:15726 case Builtin::BI__builtin_clrsbll: {15727 APSInt Val;15728 if (!EvaluateInteger(E->getArg(0), Val, Info))15729 return false;15730 15731 return Success(Val.getBitWidth() - Val.getSignificantBits(), E);15732 }15733 15734 case Builtin::BI__builtin_clz:15735 case Builtin::BI__builtin_clzl:15736 case Builtin::BI__builtin_clzll:15737 case Builtin::BI__builtin_clzs:15738 case Builtin::BI__builtin_clzg:15739 case Builtin::BI__builtin_elementwise_clzg:15740 case Builtin::BI__lzcnt16: // Microsoft variants of count leading-zeroes15741 case Builtin::BI__lzcnt:15742 case Builtin::BI__lzcnt64: {15743 APSInt Val;15744 if (E->getArg(0)->getType()->isExtVectorBoolType()) {15745 APValue Vec;15746 if (!EvaluateVector(E->getArg(0), Vec, Info))15747 return false;15748 Val = ConvertBoolVectorToInt(Vec);15749 } else if (!EvaluateInteger(E->getArg(0), Val, Info)) {15750 return false;15751 }15752 15753 std::optional<APSInt> Fallback;15754 if ((BuiltinOp == Builtin::BI__builtin_clzg ||15755 BuiltinOp == Builtin::BI__builtin_elementwise_clzg) &&15756 E->getNumArgs() > 1) {15757 APSInt FallbackTemp;15758 if (!EvaluateInteger(E->getArg(1), FallbackTemp, Info))15759 return false;15760 Fallback = FallbackTemp;15761 }15762 15763 if (!Val) {15764 if (Fallback)15765 return Success(*Fallback, E);15766 15767 // When the argument is 0, the result of GCC builtins is undefined,15768 // whereas for Microsoft intrinsics, the result is the bit-width of the15769 // argument.15770 bool ZeroIsUndefined = BuiltinOp != Builtin::BI__lzcnt16 &&15771 BuiltinOp != Builtin::BI__lzcnt &&15772 BuiltinOp != Builtin::BI__lzcnt64;15773 15774 if (BuiltinOp == Builtin::BI__builtin_elementwise_clzg) {15775 Info.FFDiag(E, diag::note_constexpr_countzeroes_zero)15776 << /*IsTrailing=*/false;15777 }15778 15779 if (ZeroIsUndefined)15780 return Error(E);15781 }15782 15783 return Success(Val.countl_zero(), E);15784 }15785 15786 case Builtin::BI__builtin_constant_p: {15787 const Expr *Arg = E->getArg(0);15788 if (EvaluateBuiltinConstantP(Info, Arg))15789 return Success(true, E);15790 if (Info.InConstantContext || Arg->HasSideEffects(Info.Ctx)) {15791 // Outside a constant context, eagerly evaluate to false in the presence15792 // of side-effects in order to avoid -Wunsequenced false-positives in15793 // a branch on __builtin_constant_p(expr).15794 return Success(false, E);15795 }15796 Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr);15797 return false;15798 }15799 15800 case Builtin::BI__noop:15801 // __noop always evaluates successfully and returns 0.15802 return Success(0, E);15803 15804 case Builtin::BI__builtin_is_constant_evaluated: {15805 const auto *Callee = Info.CurrentCall->getCallee();15806 if (Info.InConstantContext && !Info.CheckingPotentialConstantExpression &&15807 (Info.CallStackDepth == 1 ||15808 (Info.CallStackDepth == 2 && Callee->isInStdNamespace() &&15809 Callee->getIdentifier() &&15810 Callee->getIdentifier()->isStr("is_constant_evaluated")))) {15811 // FIXME: Find a better way to avoid duplicated diagnostics.15812 if (Info.EvalStatus.Diag)15813 Info.report((Info.CallStackDepth == 1)15814 ? E->getExprLoc()15815 : Info.CurrentCall->getCallRange().getBegin(),15816 diag::warn_is_constant_evaluated_always_true_constexpr)15817 << (Info.CallStackDepth == 1 ? "__builtin_is_constant_evaluated"15818 : "std::is_constant_evaluated");15819 }15820 15821 return Success(Info.InConstantContext, E);15822 }15823 15824 case Builtin::BI__builtin_is_within_lifetime:15825 if (auto result = EvaluateBuiltinIsWithinLifetime(*this, E))15826 return Success(*result, E);15827 return false;15828 15829 case Builtin::BI__builtin_ctz:15830 case Builtin::BI__builtin_ctzl:15831 case Builtin::BI__builtin_ctzll:15832 case Builtin::BI__builtin_ctzs:15833 case Builtin::BI__builtin_ctzg:15834 case Builtin::BI__builtin_elementwise_ctzg: {15835 APSInt Val;15836 if (E->getArg(0)->getType()->isExtVectorBoolType()) {15837 APValue Vec;15838 if (!EvaluateVector(E->getArg(0), Vec, Info))15839 return false;15840 Val = ConvertBoolVectorToInt(Vec);15841 } else if (!EvaluateInteger(E->getArg(0), Val, Info)) {15842 return false;15843 }15844 15845 std::optional<APSInt> Fallback;15846 if ((BuiltinOp == Builtin::BI__builtin_ctzg ||15847 BuiltinOp == Builtin::BI__builtin_elementwise_ctzg) &&15848 E->getNumArgs() > 1) {15849 APSInt FallbackTemp;15850 if (!EvaluateInteger(E->getArg(1), FallbackTemp, Info))15851 return false;15852 Fallback = FallbackTemp;15853 }15854 15855 if (!Val) {15856 if (Fallback)15857 return Success(*Fallback, E);15858 15859 if (BuiltinOp == Builtin::BI__builtin_elementwise_ctzg) {15860 Info.FFDiag(E, diag::note_constexpr_countzeroes_zero)15861 << /*IsTrailing=*/true;15862 }15863 return Error(E);15864 }15865 15866 return Success(Val.countr_zero(), E);15867 }15868 15869 case Builtin::BI__builtin_eh_return_data_regno: {15870 int Operand = E->getArg(0)->EvaluateKnownConstInt(Info.Ctx).getZExtValue();15871 Operand = Info.Ctx.getTargetInfo().getEHDataRegisterNumber(Operand);15872 return Success(Operand, E);15873 }15874 15875 case Builtin::BI__builtin_elementwise_abs: {15876 APSInt Val;15877 if (!EvaluateInteger(E->getArg(0), Val, Info))15878 return false;15879 15880 return Success(Val.abs(), E);15881 }15882 15883 case Builtin::BI__builtin_expect:15884 case Builtin::BI__builtin_expect_with_probability:15885 return Visit(E->getArg(0));15886 15887 case Builtin::BI__builtin_ptrauth_string_discriminator: {15888 const auto *Literal =15889 cast<StringLiteral>(E->getArg(0)->IgnoreParenImpCasts());15890 uint64_t Result = getPointerAuthStableSipHash(Literal->getString());15891 return Success(Result, E);15892 }15893 15894 case Builtin::BI__builtin_infer_alloc_token: {15895 // If we fail to infer a type, this fails to be a constant expression; this15896 // can be checked with __builtin_constant_p(...).15897 QualType AllocType = infer_alloc::inferPossibleType(E, Info.Ctx, nullptr);15898 if (AllocType.isNull())15899 return Error(15900 E, diag::note_constexpr_infer_alloc_token_type_inference_failed);15901 auto ATMD = infer_alloc::getAllocTokenMetadata(AllocType, Info.Ctx);15902 if (!ATMD)15903 return Error(E, diag::note_constexpr_infer_alloc_token_no_metadata);15904 auto Mode =15905 Info.getLangOpts().AllocTokenMode.value_or(llvm::DefaultAllocTokenMode);15906 uint64_t BitWidth = Info.Ctx.getTypeSize(Info.Ctx.getSizeType());15907 auto MaxTokensOpt = Info.getLangOpts().AllocTokenMax;15908 uint64_t MaxTokens =15909 MaxTokensOpt.value_or(0) ? *MaxTokensOpt : (~0ULL >> (64 - BitWidth));15910 auto MaybeToken = llvm::getAllocToken(Mode, *ATMD, MaxTokens);15911 if (!MaybeToken)15912 return Error(E, diag::note_constexpr_infer_alloc_token_stateful_mode);15913 return Success(llvm::APInt(BitWidth, *MaybeToken), E);15914 }15915 15916 case Builtin::BI__builtin_ffs:15917 case Builtin::BI__builtin_ffsl:15918 case Builtin::BI__builtin_ffsll: {15919 APSInt Val;15920 if (!EvaluateInteger(E->getArg(0), Val, Info))15921 return false;15922 15923 unsigned N = Val.countr_zero();15924 return Success(N == Val.getBitWidth() ? 0 : N + 1, E);15925 }15926 15927 case Builtin::BI__builtin_fpclassify: {15928 APFloat Val(0.0);15929 if (!EvaluateFloat(E->getArg(5), Val, Info))15930 return false;15931 unsigned Arg;15932 switch (Val.getCategory()) {15933 case APFloat::fcNaN: Arg = 0; break;15934 case APFloat::fcInfinity: Arg = 1; break;15935 case APFloat::fcNormal: Arg = Val.isDenormal() ? 3 : 2; break;15936 case APFloat::fcZero: Arg = 4; break;15937 }15938 return Visit(E->getArg(Arg));15939 }15940 15941 case Builtin::BI__builtin_isinf_sign: {15942 APFloat Val(0.0);15943 return EvaluateFloat(E->getArg(0), Val, Info) &&15944 Success(Val.isInfinity() ? (Val.isNegative() ? -1 : 1) : 0, E);15945 }15946 15947 case Builtin::BI__builtin_isinf: {15948 APFloat Val(0.0);15949 return EvaluateFloat(E->getArg(0), Val, Info) &&15950 Success(Val.isInfinity() ? 1 : 0, E);15951 }15952 15953 case Builtin::BI__builtin_isfinite: {15954 APFloat Val(0.0);15955 return EvaluateFloat(E->getArg(0), Val, Info) &&15956 Success(Val.isFinite() ? 1 : 0, E);15957 }15958 15959 case Builtin::BI__builtin_isnan: {15960 APFloat Val(0.0);15961 return EvaluateFloat(E->getArg(0), Val, Info) &&15962 Success(Val.isNaN() ? 1 : 0, E);15963 }15964 15965 case Builtin::BI__builtin_isnormal: {15966 APFloat Val(0.0);15967 return EvaluateFloat(E->getArg(0), Val, Info) &&15968 Success(Val.isNormal() ? 1 : 0, E);15969 }15970 15971 case Builtin::BI__builtin_issubnormal: {15972 APFloat Val(0.0);15973 return EvaluateFloat(E->getArg(0), Val, Info) &&15974 Success(Val.isDenormal() ? 1 : 0, E);15975 }15976 15977 case Builtin::BI__builtin_iszero: {15978 APFloat Val(0.0);15979 return EvaluateFloat(E->getArg(0), Val, Info) &&15980 Success(Val.isZero() ? 1 : 0, E);15981 }15982 15983 case Builtin::BI__builtin_signbit:15984 case Builtin::BI__builtin_signbitf:15985 case Builtin::BI__builtin_signbitl: {15986 APFloat Val(0.0);15987 return EvaluateFloat(E->getArg(0), Val, Info) &&15988 Success(Val.isNegative() ? 1 : 0, E);15989 }15990 15991 case Builtin::BI__builtin_isgreater:15992 case Builtin::BI__builtin_isgreaterequal:15993 case Builtin::BI__builtin_isless:15994 case Builtin::BI__builtin_islessequal:15995 case Builtin::BI__builtin_islessgreater:15996 case Builtin::BI__builtin_isunordered: {15997 APFloat LHS(0.0);15998 APFloat RHS(0.0);15999 if (!EvaluateFloat(E->getArg(0), LHS, Info) ||16000 !EvaluateFloat(E->getArg(1), RHS, Info))16001 return false;16002 16003 return Success(16004 [&] {16005 switch (BuiltinOp) {16006 case Builtin::BI__builtin_isgreater:16007 return LHS > RHS;16008 case Builtin::BI__builtin_isgreaterequal:16009 return LHS >= RHS;16010 case Builtin::BI__builtin_isless:16011 return LHS < RHS;16012 case Builtin::BI__builtin_islessequal:16013 return LHS <= RHS;16014 case Builtin::BI__builtin_islessgreater: {16015 APFloat::cmpResult cmp = LHS.compare(RHS);16016 return cmp == APFloat::cmpResult::cmpLessThan ||16017 cmp == APFloat::cmpResult::cmpGreaterThan;16018 }16019 case Builtin::BI__builtin_isunordered:16020 return LHS.compare(RHS) == APFloat::cmpResult::cmpUnordered;16021 default:16022 llvm_unreachable("Unexpected builtin ID: Should be a floating "16023 "point comparison function");16024 }16025 }()16026 ? 116027 : 0,16028 E);16029 }16030 16031 case Builtin::BI__builtin_issignaling: {16032 APFloat Val(0.0);16033 return EvaluateFloat(E->getArg(0), Val, Info) &&16034 Success(Val.isSignaling() ? 1 : 0, E);16035 }16036 16037 case Builtin::BI__builtin_isfpclass: {16038 APSInt MaskVal;16039 if (!EvaluateInteger(E->getArg(1), MaskVal, Info))16040 return false;16041 unsigned Test = static_cast<llvm::FPClassTest>(MaskVal.getZExtValue());16042 APFloat Val(0.0);16043 return EvaluateFloat(E->getArg(0), Val, Info) &&16044 Success((Val.classify() & Test) ? 1 : 0, E);16045 }16046 16047 case Builtin::BI__builtin_parity:16048 case Builtin::BI__builtin_parityl:16049 case Builtin::BI__builtin_parityll: {16050 APSInt Val;16051 if (!EvaluateInteger(E->getArg(0), Val, Info))16052 return false;16053 16054 return Success(Val.popcount() % 2, E);16055 }16056 16057 case Builtin::BI__builtin_abs:16058 case Builtin::BI__builtin_labs:16059 case Builtin::BI__builtin_llabs: {16060 APSInt Val;16061 if (!EvaluateInteger(E->getArg(0), Val, Info))16062 return false;16063 if (Val == APSInt(APInt::getSignedMinValue(Val.getBitWidth()),16064 /*IsUnsigned=*/false))16065 return false;16066 if (Val.isNegative())16067 Val.negate();16068 return Success(Val, E);16069 }16070 16071 case Builtin::BI__builtin_popcount:16072 case Builtin::BI__builtin_popcountl:16073 case Builtin::BI__builtin_popcountll:16074 case Builtin::BI__builtin_popcountg:16075 case Builtin::BI__builtin_elementwise_popcount:16076 case Builtin::BI__popcnt16: // Microsoft variants of popcount16077 case Builtin::BI__popcnt:16078 case Builtin::BI__popcnt64: {16079 APSInt Val;16080 if (E->getArg(0)->getType()->isExtVectorBoolType()) {16081 APValue Vec;16082 if (!EvaluateVector(E->getArg(0), Vec, Info))16083 return false;16084 Val = ConvertBoolVectorToInt(Vec);16085 } else if (!EvaluateInteger(E->getArg(0), Val, Info)) {16086 return false;16087 }16088 16089 return Success(Val.popcount(), E);16090 }16091 16092 case Builtin::BI__builtin_rotateleft8:16093 case Builtin::BI__builtin_rotateleft16:16094 case Builtin::BI__builtin_rotateleft32:16095 case Builtin::BI__builtin_rotateleft64:16096 case Builtin::BI_rotl8: // Microsoft variants of rotate right16097 case Builtin::BI_rotl16:16098 case Builtin::BI_rotl:16099 case Builtin::BI_lrotl:16100 case Builtin::BI_rotl64: {16101 APSInt Val, Amt;16102 if (!EvaluateInteger(E->getArg(0), Val, Info) ||16103 !EvaluateInteger(E->getArg(1), Amt, Info))16104 return false;16105 16106 return Success(Val.rotl(Amt), E);16107 }16108 16109 case Builtin::BI__builtin_rotateright8:16110 case Builtin::BI__builtin_rotateright16:16111 case Builtin::BI__builtin_rotateright32:16112 case Builtin::BI__builtin_rotateright64:16113 case Builtin::BI_rotr8: // Microsoft variants of rotate right16114 case Builtin::BI_rotr16:16115 case Builtin::BI_rotr:16116 case Builtin::BI_lrotr:16117 case Builtin::BI_rotr64: {16118 APSInt Val, Amt;16119 if (!EvaluateInteger(E->getArg(0), Val, Info) ||16120 !EvaluateInteger(E->getArg(1), Amt, Info))16121 return false;16122 16123 return Success(Val.rotr(Amt), E);16124 }16125 16126 case Builtin::BI__builtin_elementwise_add_sat: {16127 APSInt LHS, RHS;16128 if (!EvaluateInteger(E->getArg(0), LHS, Info) ||16129 !EvaluateInteger(E->getArg(1), RHS, Info))16130 return false;16131 16132 APInt Result = LHS.isSigned() ? LHS.sadd_sat(RHS) : LHS.uadd_sat(RHS);16133 return Success(APSInt(Result, !LHS.isSigned()), E);16134 }16135 case Builtin::BI__builtin_elementwise_sub_sat: {16136 APSInt LHS, RHS;16137 if (!EvaluateInteger(E->getArg(0), LHS, Info) ||16138 !EvaluateInteger(E->getArg(1), RHS, Info))16139 return false;16140 16141 APInt Result = LHS.isSigned() ? LHS.ssub_sat(RHS) : LHS.usub_sat(RHS);16142 return Success(APSInt(Result, !LHS.isSigned()), E);16143 }16144 case Builtin::BI__builtin_elementwise_max: {16145 APSInt LHS, RHS;16146 if (!EvaluateInteger(E->getArg(0), LHS, Info) ||16147 !EvaluateInteger(E->getArg(1), RHS, Info))16148 return false;16149 16150 APInt Result = std::max(LHS, RHS);16151 return Success(APSInt(Result, !LHS.isSigned()), E);16152 }16153 case Builtin::BI__builtin_elementwise_min: {16154 APSInt LHS, RHS;16155 if (!EvaluateInteger(E->getArg(0), LHS, Info) ||16156 !EvaluateInteger(E->getArg(1), RHS, Info))16157 return false;16158 16159 APInt Result = std::min(LHS, RHS);16160 return Success(APSInt(Result, !LHS.isSigned()), E);16161 }16162 case Builtin::BI__builtin_elementwise_fshl:16163 case Builtin::BI__builtin_elementwise_fshr: {16164 APSInt Hi, Lo, Shift;16165 if (!EvaluateInteger(E->getArg(0), Hi, Info) ||16166 !EvaluateInteger(E->getArg(1), Lo, Info) ||16167 !EvaluateInteger(E->getArg(2), Shift, Info))16168 return false;16169 16170 switch (BuiltinOp) {16171 case Builtin::BI__builtin_elementwise_fshl: {16172 APSInt Result(llvm::APIntOps::fshl(Hi, Lo, Shift), Hi.isUnsigned());16173 return Success(Result, E);16174 }16175 case Builtin::BI__builtin_elementwise_fshr: {16176 APSInt Result(llvm::APIntOps::fshr(Hi, Lo, Shift), Hi.isUnsigned());16177 return Success(Result, E);16178 }16179 }16180 llvm_unreachable("Fully covered switch above");16181 }16182 case Builtin::BIstrlen:16183 case Builtin::BIwcslen:16184 // A call to strlen is not a constant expression.16185 if (Info.getLangOpts().CPlusPlus11)16186 Info.CCEDiag(E, diag::note_constexpr_invalid_function)16187 << /*isConstexpr*/ 0 << /*isConstructor*/ 016188 << Info.Ctx.BuiltinInfo.getQuotedName(BuiltinOp);16189 else16190 Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr);16191 [[fallthrough]];16192 case Builtin::BI__builtin_strlen:16193 case Builtin::BI__builtin_wcslen: {16194 // As an extension, we support __builtin_strlen() as a constant expression,16195 // and support folding strlen() to a constant.16196 uint64_t StrLen;16197 if (EvaluateBuiltinStrLen(E->getArg(0), StrLen, Info))16198 return Success(StrLen, E);16199 return false;16200 }16201 16202 case Builtin::BIstrcmp:16203 case Builtin::BIwcscmp:16204 case Builtin::BIstrncmp:16205 case Builtin::BIwcsncmp:16206 case Builtin::BImemcmp:16207 case Builtin::BIbcmp:16208 case Builtin::BIwmemcmp:16209 // A call to strlen is not a constant expression.16210 if (Info.getLangOpts().CPlusPlus11)16211 Info.CCEDiag(E, diag::note_constexpr_invalid_function)16212 << /*isConstexpr*/ 0 << /*isConstructor*/ 016213 << Info.Ctx.BuiltinInfo.getQuotedName(BuiltinOp);16214 else16215 Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr);16216 [[fallthrough]];16217 case Builtin::BI__builtin_strcmp:16218 case Builtin::BI__builtin_wcscmp:16219 case Builtin::BI__builtin_strncmp:16220 case Builtin::BI__builtin_wcsncmp:16221 case Builtin::BI__builtin_memcmp:16222 case Builtin::BI__builtin_bcmp:16223 case Builtin::BI__builtin_wmemcmp: {16224 LValue String1, String2;16225 if (!EvaluatePointer(E->getArg(0), String1, Info) ||16226 !EvaluatePointer(E->getArg(1), String2, Info))16227 return false;16228 16229 uint64_t MaxLength = uint64_t(-1);16230 if (BuiltinOp != Builtin::BIstrcmp &&16231 BuiltinOp != Builtin::BIwcscmp &&16232 BuiltinOp != Builtin::BI__builtin_strcmp &&16233 BuiltinOp != Builtin::BI__builtin_wcscmp) {16234 APSInt N;16235 if (!EvaluateInteger(E->getArg(2), N, Info))16236 return false;16237 MaxLength = N.getZExtValue();16238 }16239 16240 // Empty substrings compare equal by definition.16241 if (MaxLength == 0u)16242 return Success(0, E);16243 16244 if (!String1.checkNullPointerForFoldAccess(Info, E, AK_Read) ||16245 !String2.checkNullPointerForFoldAccess(Info, E, AK_Read) ||16246 String1.Designator.Invalid || String2.Designator.Invalid)16247 return false;16248 16249 QualType CharTy1 = String1.Designator.getType(Info.Ctx);16250 QualType CharTy2 = String2.Designator.getType(Info.Ctx);16251 16252 bool IsRawByte = BuiltinOp == Builtin::BImemcmp ||16253 BuiltinOp == Builtin::BIbcmp ||16254 BuiltinOp == Builtin::BI__builtin_memcmp ||16255 BuiltinOp == Builtin::BI__builtin_bcmp;16256 16257 assert(IsRawByte ||16258 (Info.Ctx.hasSameUnqualifiedType(16259 CharTy1, E->getArg(0)->getType()->getPointeeType()) &&16260 Info.Ctx.hasSameUnqualifiedType(CharTy1, CharTy2)));16261 16262 // For memcmp, allow comparing any arrays of '[[un]signed] char' or16263 // 'char8_t', but no other types.16264 if (IsRawByte &&16265 !(isOneByteCharacterType(CharTy1) && isOneByteCharacterType(CharTy2))) {16266 // FIXME: Consider using our bit_cast implementation to support this.16267 Info.FFDiag(E, diag::note_constexpr_memcmp_unsupported)16268 << Info.Ctx.BuiltinInfo.getQuotedName(BuiltinOp) << CharTy116269 << CharTy2;16270 return false;16271 }16272 16273 const auto &ReadCurElems = [&](APValue &Char1, APValue &Char2) {16274 return handleLValueToRValueConversion(Info, E, CharTy1, String1, Char1) &&16275 handleLValueToRValueConversion(Info, E, CharTy2, String2, Char2) &&16276 Char1.isInt() && Char2.isInt();16277 };16278 const auto &AdvanceElems = [&] {16279 return HandleLValueArrayAdjustment(Info, E, String1, CharTy1, 1) &&16280 HandleLValueArrayAdjustment(Info, E, String2, CharTy2, 1);16281 };16282 16283 bool StopAtNull =16284 (BuiltinOp != Builtin::BImemcmp && BuiltinOp != Builtin::BIbcmp &&16285 BuiltinOp != Builtin::BIwmemcmp &&16286 BuiltinOp != Builtin::BI__builtin_memcmp &&16287 BuiltinOp != Builtin::BI__builtin_bcmp &&16288 BuiltinOp != Builtin::BI__builtin_wmemcmp);16289 bool IsWide = BuiltinOp == Builtin::BIwcscmp ||16290 BuiltinOp == Builtin::BIwcsncmp ||16291 BuiltinOp == Builtin::BIwmemcmp ||16292 BuiltinOp == Builtin::BI__builtin_wcscmp ||16293 BuiltinOp == Builtin::BI__builtin_wcsncmp ||16294 BuiltinOp == Builtin::BI__builtin_wmemcmp;16295 16296 for (; MaxLength; --MaxLength) {16297 APValue Char1, Char2;16298 if (!ReadCurElems(Char1, Char2))16299 return false;16300 if (Char1.getInt().ne(Char2.getInt())) {16301 if (IsWide) // wmemcmp compares with wchar_t signedness.16302 return Success(Char1.getInt() < Char2.getInt() ? -1 : 1, E);16303 // memcmp always compares unsigned chars.16304 return Success(Char1.getInt().ult(Char2.getInt()) ? -1 : 1, E);16305 }16306 if (StopAtNull && !Char1.getInt())16307 return Success(0, E);16308 assert(!(StopAtNull && !Char2.getInt()));16309 if (!AdvanceElems())16310 return false;16311 }16312 // We hit the strncmp / memcmp limit.16313 return Success(0, E);16314 }16315 16316 case Builtin::BI__atomic_always_lock_free:16317 case Builtin::BI__atomic_is_lock_free:16318 case Builtin::BI__c11_atomic_is_lock_free: {16319 APSInt SizeVal;16320 if (!EvaluateInteger(E->getArg(0), SizeVal, Info))16321 return false;16322 16323 // For __atomic_is_lock_free(sizeof(_Atomic(T))), if the size is a power16324 // of two less than or equal to the maximum inline atomic width, we know it16325 // is lock-free. If the size isn't a power of two, or greater than the16326 // maximum alignment where we promote atomics, we know it is not lock-free16327 // (at least not in the sense of atomic_is_lock_free). Otherwise,16328 // the answer can only be determined at runtime; for example, 16-byte16329 // atomics have lock-free implementations on some, but not all,16330 // x86-64 processors.16331 16332 // Check power-of-two.16333 CharUnits Size = CharUnits::fromQuantity(SizeVal.getZExtValue());16334 if (Size.isPowerOfTwo()) {16335 // Check against inlining width.16336 unsigned InlineWidthBits =16337 Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth();16338 if (Size <= Info.Ctx.toCharUnitsFromBits(InlineWidthBits)) {16339 if (BuiltinOp == Builtin::BI__c11_atomic_is_lock_free ||16340 Size == CharUnits::One())16341 return Success(1, E);16342 16343 // If the pointer argument can be evaluated to a compile-time constant16344 // integer (or nullptr), check if that value is appropriately aligned.16345 const Expr *PtrArg = E->getArg(1);16346 Expr::EvalResult ExprResult;16347 APSInt IntResult;16348 if (PtrArg->EvaluateAsRValue(ExprResult, Info.Ctx) &&16349 ExprResult.Val.toIntegralConstant(IntResult, PtrArg->getType(),16350 Info.Ctx) &&16351 IntResult.isAligned(Size.getAsAlign()))16352 return Success(1, E);16353 16354 // Otherwise, check if the type's alignment against Size.16355 if (auto *ICE = dyn_cast<ImplicitCastExpr>(PtrArg)) {16356 // Drop the potential implicit-cast to 'const volatile void*', getting16357 // the underlying type.16358 if (ICE->getCastKind() == CK_BitCast)16359 PtrArg = ICE->getSubExpr();16360 }16361 16362 if (auto PtrTy = PtrArg->getType()->getAs<PointerType>()) {16363 QualType PointeeType = PtrTy->getPointeeType();16364 if (!PointeeType->isIncompleteType() &&16365 Info.Ctx.getTypeAlignInChars(PointeeType) >= Size) {16366 // OK, we will inline operations on this object.16367 return Success(1, E);16368 }16369 }16370 }16371 }16372 16373 return BuiltinOp == Builtin::BI__atomic_always_lock_free ?16374 Success(0, E) : Error(E);16375 }16376 case Builtin::BI__builtin_addcb:16377 case Builtin::BI__builtin_addcs:16378 case Builtin::BI__builtin_addc:16379 case Builtin::BI__builtin_addcl:16380 case Builtin::BI__builtin_addcll:16381 case Builtin::BI__builtin_subcb:16382 case Builtin::BI__builtin_subcs:16383 case Builtin::BI__builtin_subc:16384 case Builtin::BI__builtin_subcl:16385 case Builtin::BI__builtin_subcll: {16386 LValue CarryOutLValue;16387 APSInt LHS, RHS, CarryIn, CarryOut, Result;16388 QualType ResultType = E->getArg(0)->getType();16389 if (!EvaluateInteger(E->getArg(0), LHS, Info) ||16390 !EvaluateInteger(E->getArg(1), RHS, Info) ||16391 !EvaluateInteger(E->getArg(2), CarryIn, Info) ||16392 !EvaluatePointer(E->getArg(3), CarryOutLValue, Info))16393 return false;16394 // Copy the number of bits and sign.16395 Result = LHS;16396 CarryOut = LHS;16397 16398 bool FirstOverflowed = false;16399 bool SecondOverflowed = false;16400 switch (BuiltinOp) {16401 default:16402 llvm_unreachable("Invalid value for BuiltinOp");16403 case Builtin::BI__builtin_addcb:16404 case Builtin::BI__builtin_addcs:16405 case Builtin::BI__builtin_addc:16406 case Builtin::BI__builtin_addcl:16407 case Builtin::BI__builtin_addcll:16408 Result =16409 LHS.uadd_ov(RHS, FirstOverflowed).uadd_ov(CarryIn, SecondOverflowed);16410 break;16411 case Builtin::BI__builtin_subcb:16412 case Builtin::BI__builtin_subcs:16413 case Builtin::BI__builtin_subc:16414 case Builtin::BI__builtin_subcl:16415 case Builtin::BI__builtin_subcll:16416 Result =16417 LHS.usub_ov(RHS, FirstOverflowed).usub_ov(CarryIn, SecondOverflowed);16418 break;16419 }16420 16421 // It is possible for both overflows to happen but CGBuiltin uses an OR so16422 // this is consistent.16423 CarryOut = (uint64_t)(FirstOverflowed | SecondOverflowed);16424 APValue APV{CarryOut};16425 if (!handleAssignment(Info, E, CarryOutLValue, ResultType, APV))16426 return false;16427 return Success(Result, E);16428 }16429 case Builtin::BI__builtin_add_overflow:16430 case Builtin::BI__builtin_sub_overflow:16431 case Builtin::BI__builtin_mul_overflow:16432 case Builtin::BI__builtin_sadd_overflow:16433 case Builtin::BI__builtin_uadd_overflow:16434 case Builtin::BI__builtin_uaddl_overflow:16435 case Builtin::BI__builtin_uaddll_overflow:16436 case Builtin::BI__builtin_usub_overflow:16437 case Builtin::BI__builtin_usubl_overflow:16438 case Builtin::BI__builtin_usubll_overflow:16439 case Builtin::BI__builtin_umul_overflow:16440 case Builtin::BI__builtin_umull_overflow:16441 case Builtin::BI__builtin_umulll_overflow:16442 case Builtin::BI__builtin_saddl_overflow:16443 case Builtin::BI__builtin_saddll_overflow:16444 case Builtin::BI__builtin_ssub_overflow:16445 case Builtin::BI__builtin_ssubl_overflow:16446 case Builtin::BI__builtin_ssubll_overflow:16447 case Builtin::BI__builtin_smul_overflow:16448 case Builtin::BI__builtin_smull_overflow:16449 case Builtin::BI__builtin_smulll_overflow: {16450 LValue ResultLValue;16451 APSInt LHS, RHS;16452 16453 QualType ResultType = E->getArg(2)->getType()->getPointeeType();16454 if (!EvaluateInteger(E->getArg(0), LHS, Info) ||16455 !EvaluateInteger(E->getArg(1), RHS, Info) ||16456 !EvaluatePointer(E->getArg(2), ResultLValue, Info))16457 return false;16458 16459 APSInt Result;16460 bool DidOverflow = false;16461 16462 // If the types don't have to match, enlarge all 3 to the largest of them.16463 if (BuiltinOp == Builtin::BI__builtin_add_overflow ||16464 BuiltinOp == Builtin::BI__builtin_sub_overflow ||16465 BuiltinOp == Builtin::BI__builtin_mul_overflow) {16466 bool IsSigned = LHS.isSigned() || RHS.isSigned() ||16467 ResultType->isSignedIntegerOrEnumerationType();16468 bool AllSigned = LHS.isSigned() && RHS.isSigned() &&16469 ResultType->isSignedIntegerOrEnumerationType();16470 uint64_t LHSSize = LHS.getBitWidth();16471 uint64_t RHSSize = RHS.getBitWidth();16472 uint64_t ResultSize = Info.Ctx.getTypeSize(ResultType);16473 uint64_t MaxBits = std::max(std::max(LHSSize, RHSSize), ResultSize);16474 16475 // Add an additional bit if the signedness isn't uniformly agreed to. We16476 // could do this ONLY if there is a signed and an unsigned that both have16477 // MaxBits, but the code to check that is pretty nasty. The issue will be16478 // caught in the shrink-to-result later anyway.16479 if (IsSigned && !AllSigned)16480 ++MaxBits;16481 16482 LHS = APSInt(LHS.extOrTrunc(MaxBits), !IsSigned);16483 RHS = APSInt(RHS.extOrTrunc(MaxBits), !IsSigned);16484 Result = APSInt(MaxBits, !IsSigned);16485 }16486 16487 // Find largest int.16488 switch (BuiltinOp) {16489 default:16490 llvm_unreachable("Invalid value for BuiltinOp");16491 case Builtin::BI__builtin_add_overflow:16492 case Builtin::BI__builtin_sadd_overflow:16493 case Builtin::BI__builtin_saddl_overflow:16494 case Builtin::BI__builtin_saddll_overflow:16495 case Builtin::BI__builtin_uadd_overflow:16496 case Builtin::BI__builtin_uaddl_overflow:16497 case Builtin::BI__builtin_uaddll_overflow:16498 Result = LHS.isSigned() ? LHS.sadd_ov(RHS, DidOverflow)16499 : LHS.uadd_ov(RHS, DidOverflow);16500 break;16501 case Builtin::BI__builtin_sub_overflow:16502 case Builtin::BI__builtin_ssub_overflow:16503 case Builtin::BI__builtin_ssubl_overflow:16504 case Builtin::BI__builtin_ssubll_overflow:16505 case Builtin::BI__builtin_usub_overflow:16506 case Builtin::BI__builtin_usubl_overflow:16507 case Builtin::BI__builtin_usubll_overflow:16508 Result = LHS.isSigned() ? LHS.ssub_ov(RHS, DidOverflow)16509 : LHS.usub_ov(RHS, DidOverflow);16510 break;16511 case Builtin::BI__builtin_mul_overflow:16512 case Builtin::BI__builtin_smul_overflow:16513 case Builtin::BI__builtin_smull_overflow:16514 case Builtin::BI__builtin_smulll_overflow:16515 case Builtin::BI__builtin_umul_overflow:16516 case Builtin::BI__builtin_umull_overflow:16517 case Builtin::BI__builtin_umulll_overflow:16518 Result = LHS.isSigned() ? LHS.smul_ov(RHS, DidOverflow)16519 : LHS.umul_ov(RHS, DidOverflow);16520 break;16521 }16522 16523 // In the case where multiple sizes are allowed, truncate and see if16524 // the values are the same.16525 if (BuiltinOp == Builtin::BI__builtin_add_overflow ||16526 BuiltinOp == Builtin::BI__builtin_sub_overflow ||16527 BuiltinOp == Builtin::BI__builtin_mul_overflow) {16528 // APSInt doesn't have a TruncOrSelf, so we use extOrTrunc instead,16529 // since it will give us the behavior of a TruncOrSelf in the case where16530 // its parameter <= its size. We previously set Result to be at least the16531 // type-size of the result, so getTypeSize(ResultType) <= Result.BitWidth16532 // will work exactly like TruncOrSelf.16533 APSInt Temp = Result.extOrTrunc(Info.Ctx.getTypeSize(ResultType));16534 Temp.setIsSigned(ResultType->isSignedIntegerOrEnumerationType());16535 16536 if (!APSInt::isSameValue(Temp, Result))16537 DidOverflow = true;16538 Result = Temp;16539 }16540 16541 APValue APV{Result};16542 if (!handleAssignment(Info, E, ResultLValue, ResultType, APV))16543 return false;16544 return Success(DidOverflow, E);16545 }16546 16547 case Builtin::BI__builtin_reduce_add:16548 case Builtin::BI__builtin_reduce_mul:16549 case Builtin::BI__builtin_reduce_and:16550 case Builtin::BI__builtin_reduce_or:16551 case Builtin::BI__builtin_reduce_xor:16552 case Builtin::BI__builtin_reduce_min:16553 case Builtin::BI__builtin_reduce_max: {16554 APValue Source;16555 if (!EvaluateAsRValue(Info, E->getArg(0), Source))16556 return false;16557 16558 unsigned SourceLen = Source.getVectorLength();16559 APSInt Reduced = Source.getVectorElt(0).getInt();16560 for (unsigned EltNum = 1; EltNum < SourceLen; ++EltNum) {16561 switch (BuiltinOp) {16562 default:16563 return false;16564 case Builtin::BI__builtin_reduce_add: {16565 if (!CheckedIntArithmetic(16566 Info, E, Reduced, Source.getVectorElt(EltNum).getInt(),16567 Reduced.getBitWidth() + 1, std::plus<APSInt>(), Reduced))16568 return false;16569 break;16570 }16571 case Builtin::BI__builtin_reduce_mul: {16572 if (!CheckedIntArithmetic(16573 Info, E, Reduced, Source.getVectorElt(EltNum).getInt(),16574 Reduced.getBitWidth() * 2, std::multiplies<APSInt>(), Reduced))16575 return false;16576 break;16577 }16578 case Builtin::BI__builtin_reduce_and: {16579 Reduced &= Source.getVectorElt(EltNum).getInt();16580 break;16581 }16582 case Builtin::BI__builtin_reduce_or: {16583 Reduced |= Source.getVectorElt(EltNum).getInt();16584 break;16585 }16586 case Builtin::BI__builtin_reduce_xor: {16587 Reduced ^= Source.getVectorElt(EltNum).getInt();16588 break;16589 }16590 case Builtin::BI__builtin_reduce_min: {16591 Reduced = std::min(Reduced, Source.getVectorElt(EltNum).getInt());16592 break;16593 }16594 case Builtin::BI__builtin_reduce_max: {16595 Reduced = std::max(Reduced, Source.getVectorElt(EltNum).getInt());16596 break;16597 }16598 }16599 }16600 16601 return Success(Reduced, E);16602 }16603 16604 case clang::X86::BI__builtin_ia32_addcarryx_u32:16605 case clang::X86::BI__builtin_ia32_addcarryx_u64:16606 case clang::X86::BI__builtin_ia32_subborrow_u32:16607 case clang::X86::BI__builtin_ia32_subborrow_u64: {16608 LValue ResultLValue;16609 APSInt CarryIn, LHS, RHS;16610 QualType ResultType = E->getArg(3)->getType()->getPointeeType();16611 if (!EvaluateInteger(E->getArg(0), CarryIn, Info) ||16612 !EvaluateInteger(E->getArg(1), LHS, Info) ||16613 !EvaluateInteger(E->getArg(2), RHS, Info) ||16614 !EvaluatePointer(E->getArg(3), ResultLValue, Info))16615 return false;16616 16617 bool IsAdd = BuiltinOp == clang::X86::BI__builtin_ia32_addcarryx_u32 ||16618 BuiltinOp == clang::X86::BI__builtin_ia32_addcarryx_u64;16619 16620 unsigned BitWidth = LHS.getBitWidth();16621 unsigned CarryInBit = CarryIn.ugt(0) ? 1 : 0;16622 APInt ExResult =16623 IsAdd16624 ? (LHS.zext(BitWidth + 1) + (RHS.zext(BitWidth + 1) + CarryInBit))16625 : (LHS.zext(BitWidth + 1) - (RHS.zext(BitWidth + 1) + CarryInBit));16626 16627 APInt Result = ExResult.extractBits(BitWidth, 0);16628 uint64_t CarryOut = ExResult.extractBitsAsZExtValue(1, BitWidth);16629 16630 APValue APV{APSInt(Result, /*isUnsigned=*/true)};16631 if (!handleAssignment(Info, E, ResultLValue, ResultType, APV))16632 return false;16633 return Success(CarryOut, E);16634 }16635 16636 case clang::X86::BI__builtin_ia32_movmskps:16637 case clang::X86::BI__builtin_ia32_movmskpd:16638 case clang::X86::BI__builtin_ia32_pmovmskb128:16639 case clang::X86::BI__builtin_ia32_pmovmskb256:16640 case clang::X86::BI__builtin_ia32_movmskps256:16641 case clang::X86::BI__builtin_ia32_movmskpd256: {16642 APValue Source;16643 if (!Evaluate(Source, Info, E->getArg(0)))16644 return false;16645 unsigned SourceLen = Source.getVectorLength();16646 const VectorType *VT = E->getArg(0)->getType()->castAs<VectorType>();16647 QualType ElemQT = VT->getElementType();16648 unsigned ResultLen = Info.Ctx.getTypeSize(16649 E->getCallReturnType(Info.Ctx)); // Always 32-bit integer.16650 APInt Result(ResultLen, 0);16651 16652 for (unsigned I = 0; I != SourceLen; ++I) {16653 APInt Elem;16654 if (ElemQT->isIntegerType()) {16655 Elem = Source.getVectorElt(I).getInt();16656 } else if (ElemQT->isRealFloatingType()) {16657 Elem = Source.getVectorElt(I).getFloat().bitcastToAPInt();16658 } else {16659 return false;16660 }16661 Result.setBitVal(I, Elem.isNegative());16662 }16663 return Success(Result, E);16664 }16665 16666 case clang::X86::BI__builtin_ia32_bextr_u32:16667 case clang::X86::BI__builtin_ia32_bextr_u64:16668 case clang::X86::BI__builtin_ia32_bextri_u32:16669 case clang::X86::BI__builtin_ia32_bextri_u64: {16670 APSInt Val, Idx;16671 if (!EvaluateInteger(E->getArg(0), Val, Info) ||16672 !EvaluateInteger(E->getArg(1), Idx, Info))16673 return false;16674 16675 unsigned BitWidth = Val.getBitWidth();16676 uint64_t Shift = Idx.extractBitsAsZExtValue(8, 0);16677 uint64_t Length = Idx.extractBitsAsZExtValue(8, 8);16678 Length = Length > BitWidth ? BitWidth : Length;16679 16680 // Handle out of bounds cases.16681 if (Length == 0 || Shift >= BitWidth)16682 return Success(0, E);16683 16684 uint64_t Result = Val.getZExtValue() >> Shift;16685 Result &= llvm::maskTrailingOnes<uint64_t>(Length);16686 return Success(Result, E);16687 }16688 16689 case clang::X86::BI__builtin_ia32_bzhi_si:16690 case clang::X86::BI__builtin_ia32_bzhi_di: {16691 APSInt Val, Idx;16692 if (!EvaluateInteger(E->getArg(0), Val, Info) ||16693 !EvaluateInteger(E->getArg(1), Idx, Info))16694 return false;16695 16696 unsigned BitWidth = Val.getBitWidth();16697 unsigned Index = Idx.extractBitsAsZExtValue(8, 0);16698 if (Index < BitWidth)16699 Val.clearHighBits(BitWidth - Index);16700 return Success(Val, E);16701 }16702 16703 case clang::X86::BI__builtin_ia32_ktestcqi:16704 case clang::X86::BI__builtin_ia32_ktestchi:16705 case clang::X86::BI__builtin_ia32_ktestcsi:16706 case clang::X86::BI__builtin_ia32_ktestcdi: {16707 APSInt A, B;16708 if (!EvaluateInteger(E->getArg(0), A, Info) ||16709 !EvaluateInteger(E->getArg(1), B, Info))16710 return false;16711 16712 return Success((~A & B) == 0, E);16713 }16714 16715 case clang::X86::BI__builtin_ia32_ktestzqi:16716 case clang::X86::BI__builtin_ia32_ktestzhi:16717 case clang::X86::BI__builtin_ia32_ktestzsi:16718 case clang::X86::BI__builtin_ia32_ktestzdi: {16719 APSInt A, B;16720 if (!EvaluateInteger(E->getArg(0), A, Info) ||16721 !EvaluateInteger(E->getArg(1), B, Info))16722 return false;16723 16724 return Success((A & B) == 0, E);16725 }16726 16727 case clang::X86::BI__builtin_ia32_kortestcqi:16728 case clang::X86::BI__builtin_ia32_kortestchi:16729 case clang::X86::BI__builtin_ia32_kortestcsi:16730 case clang::X86::BI__builtin_ia32_kortestcdi: {16731 APSInt A, B;16732 if (!EvaluateInteger(E->getArg(0), A, Info) ||16733 !EvaluateInteger(E->getArg(1), B, Info))16734 return false;16735 16736 return Success(~(A | B) == 0, E);16737 }16738 16739 case clang::X86::BI__builtin_ia32_kortestzqi:16740 case clang::X86::BI__builtin_ia32_kortestzhi:16741 case clang::X86::BI__builtin_ia32_kortestzsi:16742 case clang::X86::BI__builtin_ia32_kortestzdi: {16743 APSInt A, B;16744 if (!EvaluateInteger(E->getArg(0), A, Info) ||16745 !EvaluateInteger(E->getArg(1), B, Info))16746 return false;16747 16748 return Success((A | B) == 0, E);16749 }16750 16751 case clang::X86::BI__builtin_ia32_kunpckhi:16752 case clang::X86::BI__builtin_ia32_kunpckdi:16753 case clang::X86::BI__builtin_ia32_kunpcksi: {16754 APSInt A, B;16755 if (!EvaluateInteger(E->getArg(0), A, Info) ||16756 !EvaluateInteger(E->getArg(1), B, Info))16757 return false;16758 16759 // Generic kunpack: extract lower half of each operand and concatenate16760 // Result = A[HalfWidth-1:0] concat B[HalfWidth-1:0]16761 unsigned BW = A.getBitWidth();16762 APSInt Result(A.trunc(BW / 2).concat(B.trunc(BW / 2)), A.isUnsigned());16763 return Success(Result, E);16764 }16765 16766 case clang::X86::BI__builtin_ia32_lzcnt_u16:16767 case clang::X86::BI__builtin_ia32_lzcnt_u32:16768 case clang::X86::BI__builtin_ia32_lzcnt_u64: {16769 APSInt Val;16770 if (!EvaluateInteger(E->getArg(0), Val, Info))16771 return false;16772 return Success(Val.countLeadingZeros(), E);16773 }16774 16775 case clang::X86::BI__builtin_ia32_tzcnt_u16:16776 case clang::X86::BI__builtin_ia32_tzcnt_u32:16777 case clang::X86::BI__builtin_ia32_tzcnt_u64: {16778 APSInt Val;16779 if (!EvaluateInteger(E->getArg(0), Val, Info))16780 return false;16781 return Success(Val.countTrailingZeros(), E);16782 }16783 16784 case clang::X86::BI__builtin_ia32_pdep_si:16785 case clang::X86::BI__builtin_ia32_pdep_di: {16786 APSInt Val, Msk;16787 if (!EvaluateInteger(E->getArg(0), Val, Info) ||16788 !EvaluateInteger(E->getArg(1), Msk, Info))16789 return false;16790 16791 unsigned BitWidth = Val.getBitWidth();16792 APInt Result = APInt::getZero(BitWidth);16793 for (unsigned I = 0, P = 0; I != BitWidth; ++I)16794 if (Msk[I])16795 Result.setBitVal(I, Val[P++]);16796 return Success(Result, E);16797 }16798 16799 case clang::X86::BI__builtin_ia32_pext_si:16800 case clang::X86::BI__builtin_ia32_pext_di: {16801 APSInt Val, Msk;16802 if (!EvaluateInteger(E->getArg(0), Val, Info) ||16803 !EvaluateInteger(E->getArg(1), Msk, Info))16804 return false;16805 16806 unsigned BitWidth = Val.getBitWidth();16807 APInt Result = APInt::getZero(BitWidth);16808 for (unsigned I = 0, P = 0; I != BitWidth; ++I)16809 if (Msk[I])16810 Result.setBitVal(P++, Val[I]);16811 return Success(Result, E);16812 }16813 case X86::BI__builtin_ia32_ptestz128:16814 case X86::BI__builtin_ia32_ptestz256:16815 case X86::BI__builtin_ia32_vtestzps:16816 case X86::BI__builtin_ia32_vtestzps256:16817 case X86::BI__builtin_ia32_vtestzpd:16818 case X86::BI__builtin_ia32_vtestzpd256: {16819 return EvalTestOp(16820 [](const APInt &A, const APInt &B) { return (A & B) == 0; });16821 }16822 case X86::BI__builtin_ia32_ptestc128:16823 case X86::BI__builtin_ia32_ptestc256:16824 case X86::BI__builtin_ia32_vtestcps:16825 case X86::BI__builtin_ia32_vtestcps256:16826 case X86::BI__builtin_ia32_vtestcpd:16827 case X86::BI__builtin_ia32_vtestcpd256: {16828 return EvalTestOp(16829 [](const APInt &A, const APInt &B) { return (~A & B) == 0; });16830 }16831 case X86::BI__builtin_ia32_ptestnzc128:16832 case X86::BI__builtin_ia32_ptestnzc256:16833 case X86::BI__builtin_ia32_vtestnzcps:16834 case X86::BI__builtin_ia32_vtestnzcps256:16835 case X86::BI__builtin_ia32_vtestnzcpd:16836 case X86::BI__builtin_ia32_vtestnzcpd256: {16837 return EvalTestOp([](const APInt &A, const APInt &B) {16838 return ((A & B) != 0) && ((~A & B) != 0);16839 });16840 }16841 case X86::BI__builtin_ia32_kandqi:16842 case X86::BI__builtin_ia32_kandhi:16843 case X86::BI__builtin_ia32_kandsi:16844 case X86::BI__builtin_ia32_kanddi: {16845 return HandleMaskBinOp(16846 [](const APSInt &LHS, const APSInt &RHS) { return LHS & RHS; });16847 }16848 16849 case X86::BI__builtin_ia32_kandnqi:16850 case X86::BI__builtin_ia32_kandnhi:16851 case X86::BI__builtin_ia32_kandnsi:16852 case X86::BI__builtin_ia32_kandndi: {16853 return HandleMaskBinOp(16854 [](const APSInt &LHS, const APSInt &RHS) { return ~LHS & RHS; });16855 }16856 16857 case X86::BI__builtin_ia32_korqi:16858 case X86::BI__builtin_ia32_korhi:16859 case X86::BI__builtin_ia32_korsi:16860 case X86::BI__builtin_ia32_kordi: {16861 return HandleMaskBinOp(16862 [](const APSInt &LHS, const APSInt &RHS) { return LHS | RHS; });16863 }16864 16865 case X86::BI__builtin_ia32_kxnorqi:16866 case X86::BI__builtin_ia32_kxnorhi:16867 case X86::BI__builtin_ia32_kxnorsi:16868 case X86::BI__builtin_ia32_kxnordi: {16869 return HandleMaskBinOp(16870 [](const APSInt &LHS, const APSInt &RHS) { return ~(LHS ^ RHS); });16871 }16872 16873 case X86::BI__builtin_ia32_kxorqi:16874 case X86::BI__builtin_ia32_kxorhi:16875 case X86::BI__builtin_ia32_kxorsi:16876 case X86::BI__builtin_ia32_kxordi: {16877 return HandleMaskBinOp(16878 [](const APSInt &LHS, const APSInt &RHS) { return LHS ^ RHS; });16879 }16880 16881 case X86::BI__builtin_ia32_knotqi:16882 case X86::BI__builtin_ia32_knothi:16883 case X86::BI__builtin_ia32_knotsi:16884 case X86::BI__builtin_ia32_knotdi: {16885 APSInt Val;16886 if (!EvaluateInteger(E->getArg(0), Val, Info))16887 return false;16888 APSInt Result = ~Val;16889 return Success(APValue(Result), E);16890 }16891 16892 case X86::BI__builtin_ia32_kaddqi:16893 case X86::BI__builtin_ia32_kaddhi:16894 case X86::BI__builtin_ia32_kaddsi:16895 case X86::BI__builtin_ia32_kadddi: {16896 return HandleMaskBinOp(16897 [](const APSInt &LHS, const APSInt &RHS) { return LHS + RHS; });16898 }16899 16900 case X86::BI__builtin_ia32_kmovb:16901 case X86::BI__builtin_ia32_kmovw:16902 case X86::BI__builtin_ia32_kmovd:16903 case X86::BI__builtin_ia32_kmovq: {16904 APSInt Val;16905 if (!EvaluateInteger(E->getArg(0), Val, Info))16906 return false;16907 return Success(Val, E);16908 }16909 16910 case clang::X86::BI__builtin_ia32_vec_ext_v4hi:16911 case clang::X86::BI__builtin_ia32_vec_ext_v16qi:16912 case clang::X86::BI__builtin_ia32_vec_ext_v8hi:16913 case clang::X86::BI__builtin_ia32_vec_ext_v4si:16914 case clang::X86::BI__builtin_ia32_vec_ext_v2di:16915 case clang::X86::BI__builtin_ia32_vec_ext_v32qi:16916 case clang::X86::BI__builtin_ia32_vec_ext_v16hi:16917 case clang::X86::BI__builtin_ia32_vec_ext_v8si:16918 case clang::X86::BI__builtin_ia32_vec_ext_v4di: {16919 APValue Vec;16920 APSInt IdxAPS;16921 if (!EvaluateVector(E->getArg(0), Vec, Info) ||16922 !EvaluateInteger(E->getArg(1), IdxAPS, Info))16923 return false;16924 unsigned N = Vec.getVectorLength();16925 unsigned Idx = static_cast<unsigned>(IdxAPS.getZExtValue() & (N - 1));16926 return Success(Vec.getVectorElt(Idx).getInt(), E);16927 }16928 16929 case clang::X86::BI__builtin_ia32_cvtb2mask128:16930 case clang::X86::BI__builtin_ia32_cvtb2mask256:16931 case clang::X86::BI__builtin_ia32_cvtb2mask512:16932 case clang::X86::BI__builtin_ia32_cvtw2mask128:16933 case clang::X86::BI__builtin_ia32_cvtw2mask256:16934 case clang::X86::BI__builtin_ia32_cvtw2mask512:16935 case clang::X86::BI__builtin_ia32_cvtd2mask128:16936 case clang::X86::BI__builtin_ia32_cvtd2mask256:16937 case clang::X86::BI__builtin_ia32_cvtd2mask512:16938 case clang::X86::BI__builtin_ia32_cvtq2mask128:16939 case clang::X86::BI__builtin_ia32_cvtq2mask256:16940 case clang::X86::BI__builtin_ia32_cvtq2mask512: {16941 assert(E->getNumArgs() == 1);16942 APValue Vec;16943 if (!EvaluateVector(E->getArg(0), Vec, Info))16944 return false;16945 16946 unsigned VectorLen = Vec.getVectorLength();16947 unsigned RetWidth = Info.Ctx.getIntWidth(E->getType());16948 llvm::APInt Bits(RetWidth, 0);16949 16950 for (unsigned ElemNum = 0; ElemNum != VectorLen; ++ElemNum) {16951 const APSInt &A = Vec.getVectorElt(ElemNum).getInt();16952 unsigned MSB = A[A.getBitWidth() - 1];16953 Bits.setBitVal(ElemNum, MSB);16954 }16955 16956 APSInt RetMask(Bits, /*isUnsigned=*/true);16957 return Success(APValue(RetMask), E);16958 }16959 16960 case clang::X86::BI__builtin_ia32_cmpb128_mask:16961 case clang::X86::BI__builtin_ia32_cmpw128_mask:16962 case clang::X86::BI__builtin_ia32_cmpd128_mask:16963 case clang::X86::BI__builtin_ia32_cmpq128_mask:16964 case clang::X86::BI__builtin_ia32_cmpb256_mask:16965 case clang::X86::BI__builtin_ia32_cmpw256_mask:16966 case clang::X86::BI__builtin_ia32_cmpd256_mask:16967 case clang::X86::BI__builtin_ia32_cmpq256_mask:16968 case clang::X86::BI__builtin_ia32_cmpb512_mask:16969 case clang::X86::BI__builtin_ia32_cmpw512_mask:16970 case clang::X86::BI__builtin_ia32_cmpd512_mask:16971 case clang::X86::BI__builtin_ia32_cmpq512_mask:16972 case clang::X86::BI__builtin_ia32_ucmpb128_mask:16973 case clang::X86::BI__builtin_ia32_ucmpw128_mask:16974 case clang::X86::BI__builtin_ia32_ucmpd128_mask:16975 case clang::X86::BI__builtin_ia32_ucmpq128_mask:16976 case clang::X86::BI__builtin_ia32_ucmpb256_mask:16977 case clang::X86::BI__builtin_ia32_ucmpw256_mask:16978 case clang::X86::BI__builtin_ia32_ucmpd256_mask:16979 case clang::X86::BI__builtin_ia32_ucmpq256_mask:16980 case clang::X86::BI__builtin_ia32_ucmpb512_mask:16981 case clang::X86::BI__builtin_ia32_ucmpw512_mask:16982 case clang::X86::BI__builtin_ia32_ucmpd512_mask:16983 case clang::X86::BI__builtin_ia32_ucmpq512_mask: {16984 assert(E->getNumArgs() == 4);16985 16986 bool IsUnsigned =16987 (BuiltinOp >= clang::X86::BI__builtin_ia32_ucmpb128_mask &&16988 BuiltinOp <= clang::X86::BI__builtin_ia32_ucmpw512_mask);16989 16990 APValue LHS, RHS;16991 APSInt Mask, Opcode;16992 if (!EvaluateVector(E->getArg(0), LHS, Info) ||16993 !EvaluateVector(E->getArg(1), RHS, Info) ||16994 !EvaluateInteger(E->getArg(2), Opcode, Info) ||16995 !EvaluateInteger(E->getArg(3), Mask, Info))16996 return false;16997 16998 assert(LHS.getVectorLength() == RHS.getVectorLength());16999 17000 unsigned VectorLen = LHS.getVectorLength();17001 unsigned RetWidth = Mask.getBitWidth();17002 17003 APSInt RetMask(llvm::APInt(RetWidth, 0), /*isUnsigned=*/true);17004 17005 for (unsigned ElemNum = 0; ElemNum < VectorLen; ++ElemNum) {17006 const APSInt &A = LHS.getVectorElt(ElemNum).getInt();17007 const APSInt &B = RHS.getVectorElt(ElemNum).getInt();17008 bool Result = false;17009 17010 switch (Opcode.getExtValue() & 0x7) {17011 case 0: // _MM_CMPINT_EQ17012 Result = (A == B);17013 break;17014 case 1: // _MM_CMPINT_LT17015 Result = IsUnsigned ? A.ult(B) : A.slt(B);17016 break;17017 case 2: // _MM_CMPINT_LE17018 Result = IsUnsigned ? A.ule(B) : A.sle(B);17019 break;17020 case 3: // _MM_CMPINT_FALSE17021 Result = false;17022 break;17023 case 4: // _MM_CMPINT_NE17024 Result = (A != B);17025 break;17026 case 5: // _MM_CMPINT_NLT (>=)17027 Result = IsUnsigned ? A.uge(B) : A.sge(B);17028 break;17029 case 6: // _MM_CMPINT_NLE (>)17030 Result = IsUnsigned ? A.ugt(B) : A.sgt(B);17031 break;17032 case 7: // _MM_CMPINT_TRUE17033 Result = true;17034 break;17035 }17036 17037 RetMask.setBitVal(ElemNum, Mask[ElemNum] && Result);17038 }17039 17040 return Success(APValue(RetMask), E);17041 }17042 case X86::BI__builtin_ia32_vpshufbitqmb128_mask:17043 case X86::BI__builtin_ia32_vpshufbitqmb256_mask:17044 case X86::BI__builtin_ia32_vpshufbitqmb512_mask: {17045 assert(E->getNumArgs() == 3);17046 17047 APValue Source, ShuffleMask;17048 APSInt ZeroMask;17049 if (!EvaluateVector(E->getArg(0), Source, Info) ||17050 !EvaluateVector(E->getArg(1), ShuffleMask, Info) ||17051 !EvaluateInteger(E->getArg(2), ZeroMask, Info))17052 return false;17053 17054 assert(Source.getVectorLength() == ShuffleMask.getVectorLength());17055 assert(ZeroMask.getBitWidth() == Source.getVectorLength());17056 17057 unsigned NumBytesInQWord = 8;17058 unsigned NumBitsInByte = 8;17059 unsigned NumBytes = Source.getVectorLength();17060 unsigned NumQWords = NumBytes / NumBytesInQWord;17061 unsigned RetWidth = ZeroMask.getBitWidth();17062 APSInt RetMask(llvm::APInt(RetWidth, 0), /*isUnsigned=*/true);17063 17064 for (unsigned QWordId = 0; QWordId != NumQWords; ++QWordId) {17065 APInt SourceQWord(64, 0);17066 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {17067 uint64_t Byte = Source.getVectorElt(QWordId * NumBytesInQWord + ByteIdx)17068 .getInt()17069 .getZExtValue();17070 SourceQWord.insertBits(APInt(8, Byte & 0xFF), ByteIdx * NumBitsInByte);17071 }17072 17073 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {17074 unsigned SelIdx = QWordId * NumBytesInQWord + ByteIdx;17075 unsigned M =17076 ShuffleMask.getVectorElt(SelIdx).getInt().getZExtValue() & 0x3F;17077 if (ZeroMask[SelIdx]) {17078 RetMask.setBitVal(SelIdx, SourceQWord[M]);17079 }17080 }17081 }17082 return Success(APValue(RetMask), E);17083 }17084 }17085}17086 17087/// Determine whether this is a pointer past the end of the complete17088/// object referred to by the lvalue.17089static bool isOnePastTheEndOfCompleteObject(const ASTContext &Ctx,17090 const LValue &LV) {17091 // A null pointer can be viewed as being "past the end" but we don't17092 // choose to look at it that way here.17093 if (!LV.getLValueBase())17094 return false;17095 17096 // If the designator is valid and refers to a subobject, we're not pointing17097 // past the end.17098 if (!LV.getLValueDesignator().Invalid &&17099 !LV.getLValueDesignator().isOnePastTheEnd())17100 return false;17101 17102 // A pointer to an incomplete type might be past-the-end if the type's size is17103 // zero. We cannot tell because the type is incomplete.17104 QualType Ty = getType(LV.getLValueBase());17105 if (Ty->isIncompleteType())17106 return true;17107 17108 // Can't be past the end of an invalid object.17109 if (LV.getLValueDesignator().Invalid)17110 return false;17111 17112 // We're a past-the-end pointer if we point to the byte after the object,17113 // no matter what our type or path is.17114 auto Size = Ctx.getTypeSizeInChars(Ty);17115 return LV.getLValueOffset() == Size;17116}17117 17118namespace {17119 17120/// Data recursive integer evaluator of certain binary operators.17121///17122/// We use a data recursive algorithm for binary operators so that we are able17123/// to handle extreme cases of chained binary operators without causing stack17124/// overflow.17125class DataRecursiveIntBinOpEvaluator {17126 struct EvalResult {17127 APValue Val;17128 bool Failed = false;17129 17130 EvalResult() = default;17131 17132 void swap(EvalResult &RHS) {17133 Val.swap(RHS.Val);17134 Failed = RHS.Failed;17135 RHS.Failed = false;17136 }17137 };17138 17139 struct Job {17140 const Expr *E;17141 EvalResult LHSResult; // meaningful only for binary operator expression.17142 enum { AnyExprKind, BinOpKind, BinOpVisitedLHSKind } Kind;17143 17144 Job() = default;17145 Job(Job &&) = default;17146 17147 void startSpeculativeEval(EvalInfo &Info) {17148 SpecEvalRAII = SpeculativeEvaluationRAII(Info);17149 }17150 17151 private:17152 SpeculativeEvaluationRAII SpecEvalRAII;17153 };17154 17155 SmallVector<Job, 16> Queue;17156 17157 IntExprEvaluator &IntEval;17158 EvalInfo &Info;17159 APValue &FinalResult;17160 17161public:17162 DataRecursiveIntBinOpEvaluator(IntExprEvaluator &IntEval, APValue &Result)17163 : IntEval(IntEval), Info(IntEval.getEvalInfo()), FinalResult(Result) { }17164 17165 /// True if \param E is a binary operator that we are going to handle17166 /// data recursively.17167 /// We handle binary operators that are comma, logical, or that have operands17168 /// with integral or enumeration type.17169 static bool shouldEnqueue(const BinaryOperator *E) {17170 return E->getOpcode() == BO_Comma || E->isLogicalOp() ||17171 (E->isPRValue() && E->getType()->isIntegralOrEnumerationType() &&17172 E->getLHS()->getType()->isIntegralOrEnumerationType() &&17173 E->getRHS()->getType()->isIntegralOrEnumerationType());17174 }17175 17176 bool Traverse(const BinaryOperator *E) {17177 enqueue(E);17178 EvalResult PrevResult;17179 while (!Queue.empty())17180 process(PrevResult);17181 17182 if (PrevResult.Failed) return false;17183 17184 FinalResult.swap(PrevResult.Val);17185 return true;17186 }17187 17188private:17189 bool Success(uint64_t Value, const Expr *E, APValue &Result) {17190 return IntEval.Success(Value, E, Result);17191 }17192 bool Success(const APSInt &Value, const Expr *E, APValue &Result) {17193 return IntEval.Success(Value, E, Result);17194 }17195 bool Error(const Expr *E) {17196 return IntEval.Error(E);17197 }17198 bool Error(const Expr *E, diag::kind D) {17199 return IntEval.Error(E, D);17200 }17201 17202 OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) {17203 return Info.CCEDiag(E, D);17204 }17205 17206 // Returns true if visiting the RHS is necessary, false otherwise.17207 bool VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E,17208 bool &SuppressRHSDiags);17209 17210 bool VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult,17211 const BinaryOperator *E, APValue &Result);17212 17213 void EvaluateExpr(const Expr *E, EvalResult &Result) {17214 Result.Failed = !Evaluate(Result.Val, Info, E);17215 if (Result.Failed)17216 Result.Val = APValue();17217 }17218 17219 void process(EvalResult &Result);17220 17221 void enqueue(const Expr *E) {17222 E = E->IgnoreParens();17223 Queue.resize(Queue.size()+1);17224 Queue.back().E = E;17225 Queue.back().Kind = Job::AnyExprKind;17226 }17227};17228 17229}17230 17231bool DataRecursiveIntBinOpEvaluator::17232 VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E,17233 bool &SuppressRHSDiags) {17234 if (E->getOpcode() == BO_Comma) {17235 // Ignore LHS but note if we could not evaluate it.17236 if (LHSResult.Failed)17237 return Info.noteSideEffect();17238 return true;17239 }17240 17241 if (E->isLogicalOp()) {17242 bool LHSAsBool;17243 if (!LHSResult.Failed && HandleConversionToBool(LHSResult.Val, LHSAsBool)) {17244 // We were able to evaluate the LHS, see if we can get away with not17245 // evaluating the RHS: 0 && X -> 0, 1 || X -> 117246 if (LHSAsBool == (E->getOpcode() == BO_LOr)) {17247 Success(LHSAsBool, E, LHSResult.Val);17248 return false; // Ignore RHS17249 }17250 } else {17251 LHSResult.Failed = true;17252 17253 // Since we weren't able to evaluate the left hand side, it17254 // might have had side effects.17255 if (!Info.noteSideEffect())17256 return false;17257 17258 // We can't evaluate the LHS; however, sometimes the result17259 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.17260 // Don't ignore RHS and suppress diagnostics from this arm.17261 SuppressRHSDiags = true;17262 }17263 17264 return true;17265 }17266 17267 assert(E->getLHS()->getType()->isIntegralOrEnumerationType() &&17268 E->getRHS()->getType()->isIntegralOrEnumerationType());17269 17270 if (LHSResult.Failed && !Info.noteFailure())17271 return false; // Ignore RHS;17272 17273 return true;17274}17275 17276static void addOrSubLValueAsInteger(APValue &LVal, const APSInt &Index,17277 bool IsSub) {17278 // Compute the new offset in the appropriate width, wrapping at 64 bits.17279 // FIXME: When compiling for a 32-bit target, we should use 32-bit17280 // offsets.17281 assert(!LVal.hasLValuePath() && "have designator for integer lvalue");17282 CharUnits &Offset = LVal.getLValueOffset();17283 uint64_t Offset64 = Offset.getQuantity();17284 uint64_t Index64 = Index.extOrTrunc(64).getZExtValue();17285 Offset = CharUnits::fromQuantity(IsSub ? Offset64 - Index6417286 : Offset64 + Index64);17287}17288 17289bool DataRecursiveIntBinOpEvaluator::17290 VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult,17291 const BinaryOperator *E, APValue &Result) {17292 if (E->getOpcode() == BO_Comma) {17293 if (RHSResult.Failed)17294 return false;17295 Result = RHSResult.Val;17296 return true;17297 }17298 17299 if (E->isLogicalOp()) {17300 bool lhsResult, rhsResult;17301 bool LHSIsOK = HandleConversionToBool(LHSResult.Val, lhsResult);17302 bool RHSIsOK = HandleConversionToBool(RHSResult.Val, rhsResult);17303 17304 if (LHSIsOK) {17305 if (RHSIsOK) {17306 if (E->getOpcode() == BO_LOr)17307 return Success(lhsResult || rhsResult, E, Result);17308 else17309 return Success(lhsResult && rhsResult, E, Result);17310 }17311 } else {17312 if (RHSIsOK) {17313 // We can't evaluate the LHS; however, sometimes the result17314 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.17315 if (rhsResult == (E->getOpcode() == BO_LOr))17316 return Success(rhsResult, E, Result);17317 }17318 }17319 17320 return false;17321 }17322 17323 assert(E->getLHS()->getType()->isIntegralOrEnumerationType() &&17324 E->getRHS()->getType()->isIntegralOrEnumerationType());17325 17326 if (LHSResult.Failed || RHSResult.Failed)17327 return false;17328 17329 const APValue &LHSVal = LHSResult.Val;17330 const APValue &RHSVal = RHSResult.Val;17331 17332 // Handle cases like (unsigned long)&a + 4.17333 if (E->isAdditiveOp() && LHSVal.isLValue() && RHSVal.isInt()) {17334 Result = LHSVal;17335 addOrSubLValueAsInteger(Result, RHSVal.getInt(), E->getOpcode() == BO_Sub);17336 return true;17337 }17338 17339 // Handle cases like 4 + (unsigned long)&a17340 if (E->getOpcode() == BO_Add &&17341 RHSVal.isLValue() && LHSVal.isInt()) {17342 Result = RHSVal;17343 addOrSubLValueAsInteger(Result, LHSVal.getInt(), /*IsSub*/false);17344 return true;17345 }17346 17347 if (E->getOpcode() == BO_Sub && LHSVal.isLValue() && RHSVal.isLValue()) {17348 // Handle (intptr_t)&&A - (intptr_t)&&B.17349 if (!LHSVal.getLValueOffset().isZero() ||17350 !RHSVal.getLValueOffset().isZero())17351 return false;17352 const Expr *LHSExpr = LHSVal.getLValueBase().dyn_cast<const Expr*>();17353 const Expr *RHSExpr = RHSVal.getLValueBase().dyn_cast<const Expr*>();17354 if (!LHSExpr || !RHSExpr)17355 return false;17356 const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr);17357 const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr);17358 if (!LHSAddrExpr || !RHSAddrExpr)17359 return false;17360 // Make sure both labels come from the same function.17361 if (LHSAddrExpr->getLabel()->getDeclContext() !=17362 RHSAddrExpr->getLabel()->getDeclContext())17363 return false;17364 Result = APValue(LHSAddrExpr, RHSAddrExpr);17365 return true;17366 }17367 17368 // All the remaining cases expect both operands to be an integer17369 if (!LHSVal.isInt() || !RHSVal.isInt())17370 return Error(E);17371 17372 // Set up the width and signedness manually, in case it can't be deduced17373 // from the operation we're performing.17374 // FIXME: Don't do this in the cases where we can deduce it.17375 APSInt Value(Info.Ctx.getIntWidth(E->getType()),17376 E->getType()->isUnsignedIntegerOrEnumerationType());17377 if (!handleIntIntBinOp(Info, E, LHSVal.getInt(), E->getOpcode(),17378 RHSVal.getInt(), Value))17379 return false;17380 return Success(Value, E, Result);17381}17382 17383void DataRecursiveIntBinOpEvaluator::process(EvalResult &Result) {17384 Job &job = Queue.back();17385 17386 switch (job.Kind) {17387 case Job::AnyExprKind: {17388 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(job.E)) {17389 if (shouldEnqueue(Bop)) {17390 job.Kind = Job::BinOpKind;17391 enqueue(Bop->getLHS());17392 return;17393 }17394 }17395 17396 EvaluateExpr(job.E, Result);17397 Queue.pop_back();17398 return;17399 }17400 17401 case Job::BinOpKind: {17402 const BinaryOperator *Bop = cast<BinaryOperator>(job.E);17403 bool SuppressRHSDiags = false;17404 if (!VisitBinOpLHSOnly(Result, Bop, SuppressRHSDiags)) {17405 Queue.pop_back();17406 return;17407 }17408 if (SuppressRHSDiags)17409 job.startSpeculativeEval(Info);17410 job.LHSResult.swap(Result);17411 job.Kind = Job::BinOpVisitedLHSKind;17412 enqueue(Bop->getRHS());17413 return;17414 }17415 17416 case Job::BinOpVisitedLHSKind: {17417 const BinaryOperator *Bop = cast<BinaryOperator>(job.E);17418 EvalResult RHS;17419 RHS.swap(Result);17420 Result.Failed = !VisitBinOp(job.LHSResult, RHS, Bop, Result.Val);17421 Queue.pop_back();17422 return;17423 }17424 }17425 17426 llvm_unreachable("Invalid Job::Kind!");17427}17428 17429namespace {17430enum class CmpResult {17431 Unequal,17432 Less,17433 Equal,17434 Greater,17435 Unordered,17436};17437}17438 17439template <class SuccessCB, class AfterCB>17440static bool17441EvaluateComparisonBinaryOperator(EvalInfo &Info, const BinaryOperator *E,17442 SuccessCB &&Success, AfterCB &&DoAfter) {17443 assert(!E->isValueDependent());17444 assert(E->isComparisonOp() && "expected comparison operator");17445 assert((E->getOpcode() == BO_Cmp ||17446 E->getType()->isIntegralOrEnumerationType()) &&17447 "unsupported binary expression evaluation");17448 auto Error = [&](const Expr *E) {17449 Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr);17450 return false;17451 };17452 17453 bool IsRelational = E->isRelationalOp() || E->getOpcode() == BO_Cmp;17454 bool IsEquality = E->isEqualityOp();17455 17456 QualType LHSTy = E->getLHS()->getType();17457 QualType RHSTy = E->getRHS()->getType();17458 17459 if (LHSTy->isIntegralOrEnumerationType() &&17460 RHSTy->isIntegralOrEnumerationType()) {17461 APSInt LHS, RHS;17462 bool LHSOK = EvaluateInteger(E->getLHS(), LHS, Info);17463 if (!LHSOK && !Info.noteFailure())17464 return false;17465 if (!EvaluateInteger(E->getRHS(), RHS, Info) || !LHSOK)17466 return false;17467 if (LHS < RHS)17468 return Success(CmpResult::Less, E);17469 if (LHS > RHS)17470 return Success(CmpResult::Greater, E);17471 return Success(CmpResult::Equal, E);17472 }17473 17474 if (LHSTy->isFixedPointType() || RHSTy->isFixedPointType()) {17475 APFixedPoint LHSFX(Info.Ctx.getFixedPointSemantics(LHSTy));17476 APFixedPoint RHSFX(Info.Ctx.getFixedPointSemantics(RHSTy));17477 17478 bool LHSOK = EvaluateFixedPointOrInteger(E->getLHS(), LHSFX, Info);17479 if (!LHSOK && !Info.noteFailure())17480 return false;17481 if (!EvaluateFixedPointOrInteger(E->getRHS(), RHSFX, Info) || !LHSOK)17482 return false;17483 if (LHSFX < RHSFX)17484 return Success(CmpResult::Less, E);17485 if (LHSFX > RHSFX)17486 return Success(CmpResult::Greater, E);17487 return Success(CmpResult::Equal, E);17488 }17489 17490 if (LHSTy->isAnyComplexType() || RHSTy->isAnyComplexType()) {17491 ComplexValue LHS, RHS;17492 bool LHSOK;17493 if (E->isAssignmentOp()) {17494 LValue LV;17495 EvaluateLValue(E->getLHS(), LV, Info);17496 LHSOK = false;17497 } else if (LHSTy->isRealFloatingType()) {17498 LHSOK = EvaluateFloat(E->getLHS(), LHS.FloatReal, Info);17499 if (LHSOK) {17500 LHS.makeComplexFloat();17501 LHS.FloatImag = APFloat(LHS.FloatReal.getSemantics());17502 }17503 } else {17504 LHSOK = EvaluateComplex(E->getLHS(), LHS, Info);17505 }17506 if (!LHSOK && !Info.noteFailure())17507 return false;17508 17509 if (E->getRHS()->getType()->isRealFloatingType()) {17510 if (!EvaluateFloat(E->getRHS(), RHS.FloatReal, Info) || !LHSOK)17511 return false;17512 RHS.makeComplexFloat();17513 RHS.FloatImag = APFloat(RHS.FloatReal.getSemantics());17514 } else if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK)17515 return false;17516 17517 if (LHS.isComplexFloat()) {17518 APFloat::cmpResult CR_r =17519 LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());17520 APFloat::cmpResult CR_i =17521 LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());17522 bool IsEqual = CR_r == APFloat::cmpEqual && CR_i == APFloat::cmpEqual;17523 return Success(IsEqual ? CmpResult::Equal : CmpResult::Unequal, E);17524 } else {17525 assert(IsEquality && "invalid complex comparison");17526 bool IsEqual = LHS.getComplexIntReal() == RHS.getComplexIntReal() &&17527 LHS.getComplexIntImag() == RHS.getComplexIntImag();17528 return Success(IsEqual ? CmpResult::Equal : CmpResult::Unequal, E);17529 }17530 }17531 17532 if (LHSTy->isRealFloatingType() &&17533 RHSTy->isRealFloatingType()) {17534 APFloat RHS(0.0), LHS(0.0);17535 17536 bool LHSOK = EvaluateFloat(E->getRHS(), RHS, Info);17537 if (!LHSOK && !Info.noteFailure())17538 return false;17539 17540 if (!EvaluateFloat(E->getLHS(), LHS, Info) || !LHSOK)17541 return false;17542 17543 assert(E->isComparisonOp() && "Invalid binary operator!");17544 llvm::APFloatBase::cmpResult APFloatCmpResult = LHS.compare(RHS);17545 if (!Info.InConstantContext &&17546 APFloatCmpResult == APFloat::cmpUnordered &&17547 E->getFPFeaturesInEffect(Info.Ctx.getLangOpts()).isFPConstrained()) {17548 // Note: Compares may raise invalid in some cases involving NaN or sNaN.17549 Info.FFDiag(E, diag::note_constexpr_float_arithmetic_strict);17550 return false;17551 }17552 auto GetCmpRes = [&]() {17553 switch (APFloatCmpResult) {17554 case APFloat::cmpEqual:17555 return CmpResult::Equal;17556 case APFloat::cmpLessThan:17557 return CmpResult::Less;17558 case APFloat::cmpGreaterThan:17559 return CmpResult::Greater;17560 case APFloat::cmpUnordered:17561 return CmpResult::Unordered;17562 }17563 llvm_unreachable("Unrecognised APFloat::cmpResult enum");17564 };17565 return Success(GetCmpRes(), E);17566 }17567 17568 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {17569 LValue LHSValue, RHSValue;17570 17571 bool LHSOK = EvaluatePointer(E->getLHS(), LHSValue, Info);17572 if (!LHSOK && !Info.noteFailure())17573 return false;17574 17575 if (!EvaluatePointer(E->getRHS(), RHSValue, Info) || !LHSOK)17576 return false;17577 17578 // Reject differing bases from the normal codepath; we special-case17579 // comparisons to null.17580 if (!HasSameBase(LHSValue, RHSValue)) {17581 // Bail out early if we're checking potential constant expression.17582 // Otherwise, prefer to diagnose other issues.17583 if (Info.checkingPotentialConstantExpression() &&17584 (LHSValue.AllowConstexprUnknown || RHSValue.AllowConstexprUnknown))17585 return false;17586 auto DiagComparison = [&] (unsigned DiagID, bool Reversed = false) {17587 std::string LHS = LHSValue.toString(Info.Ctx, E->getLHS()->getType());17588 std::string RHS = RHSValue.toString(Info.Ctx, E->getRHS()->getType());17589 Info.FFDiag(E, DiagID)17590 << (Reversed ? RHS : LHS) << (Reversed ? LHS : RHS);17591 return false;17592 };17593 // Inequalities and subtractions between unrelated pointers have17594 // unspecified or undefined behavior.17595 if (!IsEquality)17596 return DiagComparison(17597 diag::note_constexpr_pointer_comparison_unspecified);17598 // A constant address may compare equal to the address of a symbol.17599 // The one exception is that address of an object cannot compare equal17600 // to a null pointer constant.17601 // TODO: Should we restrict this to actual null pointers, and exclude the17602 // case of zero cast to pointer type?17603 if ((!LHSValue.Base && !LHSValue.Offset.isZero()) ||17604 (!RHSValue.Base && !RHSValue.Offset.isZero()))17605 return DiagComparison(diag::note_constexpr_pointer_constant_comparison,17606 !RHSValue.Base);17607 // C++2c [intro.object]/10:17608 // Two objects [...] may have the same address if [...] they are both17609 // potentially non-unique objects.17610 // C++2c [intro.object]/9:17611 // An object is potentially non-unique if it is a string literal object,17612 // the backing array of an initializer list, or a subobject thereof.17613 //17614 // This makes the comparison result unspecified, so it's not a constant17615 // expression.17616 //17617 // TODO: Do we need to handle the initializer list case here?17618 if (ArePotentiallyOverlappingStringLiterals(Info, LHSValue, RHSValue))17619 return DiagComparison(diag::note_constexpr_literal_comparison);17620 if (IsOpaqueConstantCall(LHSValue) || IsOpaqueConstantCall(RHSValue))17621 return DiagComparison(diag::note_constexpr_opaque_call_comparison,17622 !IsOpaqueConstantCall(LHSValue));17623 // We can't tell whether weak symbols will end up pointing to the same17624 // object.17625 if (IsWeakLValue(LHSValue) || IsWeakLValue(RHSValue))17626 return DiagComparison(diag::note_constexpr_pointer_weak_comparison,17627 !IsWeakLValue(LHSValue));17628 // We can't compare the address of the start of one object with the17629 // past-the-end address of another object, per C++ DR1652.17630 if (LHSValue.Base && LHSValue.Offset.isZero() &&17631 isOnePastTheEndOfCompleteObject(Info.Ctx, RHSValue))17632 return DiagComparison(diag::note_constexpr_pointer_comparison_past_end,17633 true);17634 if (RHSValue.Base && RHSValue.Offset.isZero() &&17635 isOnePastTheEndOfCompleteObject(Info.Ctx, LHSValue))17636 return DiagComparison(diag::note_constexpr_pointer_comparison_past_end,17637 false);17638 // We can't tell whether an object is at the same address as another17639 // zero sized object.17640 if ((RHSValue.Base && isZeroSized(LHSValue)) ||17641 (LHSValue.Base && isZeroSized(RHSValue)))17642 return DiagComparison(17643 diag::note_constexpr_pointer_comparison_zero_sized);17644 if (LHSValue.AllowConstexprUnknown || RHSValue.AllowConstexprUnknown)17645 return DiagComparison(17646 diag::note_constexpr_pointer_comparison_unspecified);17647 // FIXME: Verify both variables are live.17648 return Success(CmpResult::Unequal, E);17649 }17650 17651 const CharUnits &LHSOffset = LHSValue.getLValueOffset();17652 const CharUnits &RHSOffset = RHSValue.getLValueOffset();17653 17654 SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator();17655 SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator();17656 17657 // C++11 [expr.rel]p2:17658 // - If two pointers point to non-static data members of the same object,17659 // or to subobjects or array elements fo such members, recursively, the17660 // pointer to the later declared member compares greater provided the17661 // two members have the same access control and provided their class is17662 // not a union.17663 // [...]17664 // - Otherwise pointer comparisons are unspecified.17665 if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && IsRelational) {17666 bool WasArrayIndex;17667 unsigned Mismatch = FindDesignatorMismatch(17668 LHSValue.Base.isNull() ? QualType()17669 : getType(LHSValue.Base).getNonReferenceType(),17670 LHSDesignator, RHSDesignator, WasArrayIndex);17671 // At the point where the designators diverge, the comparison has a17672 // specified value if:17673 // - we are comparing array indices17674 // - we are comparing fields of a union, or fields with the same access17675 // Otherwise, the result is unspecified and thus the comparison is not a17676 // constant expression.17677 if (!WasArrayIndex && Mismatch < LHSDesignator.Entries.size() &&17678 Mismatch < RHSDesignator.Entries.size()) {17679 const FieldDecl *LF = getAsField(LHSDesignator.Entries[Mismatch]);17680 const FieldDecl *RF = getAsField(RHSDesignator.Entries[Mismatch]);17681 if (!LF && !RF)17682 Info.CCEDiag(E, diag::note_constexpr_pointer_comparison_base_classes);17683 else if (!LF)17684 Info.CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field)17685 << getAsBaseClass(LHSDesignator.Entries[Mismatch])17686 << RF->getParent() << RF;17687 else if (!RF)17688 Info.CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field)17689 << getAsBaseClass(RHSDesignator.Entries[Mismatch])17690 << LF->getParent() << LF;17691 else if (!LF->getParent()->isUnion() &&17692 LF->getAccess() != RF->getAccess())17693 Info.CCEDiag(E,17694 diag::note_constexpr_pointer_comparison_differing_access)17695 << LF << LF->getAccess() << RF << RF->getAccess()17696 << LF->getParent();17697 }17698 }17699 17700 // The comparison here must be unsigned, and performed with the same17701 // width as the pointer.17702 unsigned PtrSize = Info.Ctx.getTypeSize(LHSTy);17703 uint64_t CompareLHS = LHSOffset.getQuantity();17704 uint64_t CompareRHS = RHSOffset.getQuantity();17705 assert(PtrSize <= 64 && "Unexpected pointer width");17706 uint64_t Mask = ~0ULL >> (64 - PtrSize);17707 CompareLHS &= Mask;17708 CompareRHS &= Mask;17709 17710 // If there is a base and this is a relational operator, we can only17711 // compare pointers within the object in question; otherwise, the result17712 // depends on where the object is located in memory.17713 if (!LHSValue.Base.isNull() && IsRelational) {17714 QualType BaseTy = getType(LHSValue.Base).getNonReferenceType();17715 if (BaseTy->isIncompleteType())17716 return Error(E);17717 CharUnits Size = Info.Ctx.getTypeSizeInChars(BaseTy);17718 uint64_t OffsetLimit = Size.getQuantity();17719 if (CompareLHS > OffsetLimit || CompareRHS > OffsetLimit)17720 return Error(E);17721 }17722 17723 if (CompareLHS < CompareRHS)17724 return Success(CmpResult::Less, E);17725 if (CompareLHS > CompareRHS)17726 return Success(CmpResult::Greater, E);17727 return Success(CmpResult::Equal, E);17728 }17729 17730 if (LHSTy->isMemberPointerType()) {17731 assert(IsEquality && "unexpected member pointer operation");17732 assert(RHSTy->isMemberPointerType() && "invalid comparison");17733 17734 MemberPtr LHSValue, RHSValue;17735 17736 bool LHSOK = EvaluateMemberPointer(E->getLHS(), LHSValue, Info);17737 if (!LHSOK && !Info.noteFailure())17738 return false;17739 17740 if (!EvaluateMemberPointer(E->getRHS(), RHSValue, Info) || !LHSOK)17741 return false;17742 17743 // If either operand is a pointer to a weak function, the comparison is not17744 // constant.17745 if (LHSValue.getDecl() && LHSValue.getDecl()->isWeak()) {17746 Info.FFDiag(E, diag::note_constexpr_mem_pointer_weak_comparison)17747 << LHSValue.getDecl();17748 return false;17749 }17750 if (RHSValue.getDecl() && RHSValue.getDecl()->isWeak()) {17751 Info.FFDiag(E, diag::note_constexpr_mem_pointer_weak_comparison)17752 << RHSValue.getDecl();17753 return false;17754 }17755 17756 // C++11 [expr.eq]p2:17757 // If both operands are null, they compare equal. Otherwise if only one is17758 // null, they compare unequal.17759 if (!LHSValue.getDecl() || !RHSValue.getDecl()) {17760 bool Equal = !LHSValue.getDecl() && !RHSValue.getDecl();17761 return Success(Equal ? CmpResult::Equal : CmpResult::Unequal, E);17762 }17763 17764 // Otherwise if either is a pointer to a virtual member function, the17765 // result is unspecified.17766 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(LHSValue.getDecl()))17767 if (MD->isVirtual())17768 Info.CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD;17769 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(RHSValue.getDecl()))17770 if (MD->isVirtual())17771 Info.CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD;17772 17773 // Otherwise they compare equal if and only if they would refer to the17774 // same member of the same most derived object or the same subobject if17775 // they were dereferenced with a hypothetical object of the associated17776 // class type.17777 bool Equal = LHSValue == RHSValue;17778 return Success(Equal ? CmpResult::Equal : CmpResult::Unequal, E);17779 }17780 17781 if (LHSTy->isNullPtrType()) {17782 assert(E->isComparisonOp() && "unexpected nullptr operation");17783 assert(RHSTy->isNullPtrType() && "missing pointer conversion");17784 // C++11 [expr.rel]p4, [expr.eq]p3: If two operands of type std::nullptr_t17785 // are compared, the result is true of the operator is <=, >= or ==, and17786 // false otherwise.17787 LValue Res;17788 if (!EvaluatePointer(E->getLHS(), Res, Info) ||17789 !EvaluatePointer(E->getRHS(), Res, Info))17790 return false;17791 return Success(CmpResult::Equal, E);17792 }17793 17794 return DoAfter();17795}17796 17797bool RecordExprEvaluator::VisitBinCmp(const BinaryOperator *E) {17798 if (!CheckLiteralType(Info, E))17799 return false;17800 17801 auto OnSuccess = [&](CmpResult CR, const BinaryOperator *E) {17802 ComparisonCategoryResult CCR;17803 switch (CR) {17804 case CmpResult::Unequal:17805 llvm_unreachable("should never produce Unequal for three-way comparison");17806 case CmpResult::Less:17807 CCR = ComparisonCategoryResult::Less;17808 break;17809 case CmpResult::Equal:17810 CCR = ComparisonCategoryResult::Equal;17811 break;17812 case CmpResult::Greater:17813 CCR = ComparisonCategoryResult::Greater;17814 break;17815 case CmpResult::Unordered:17816 CCR = ComparisonCategoryResult::Unordered;17817 break;17818 }17819 // Evaluation succeeded. Lookup the information for the comparison category17820 // type and fetch the VarDecl for the result.17821 const ComparisonCategoryInfo &CmpInfo =17822 Info.Ctx.CompCategories.getInfoForType(E->getType());17823 const VarDecl *VD = CmpInfo.getValueInfo(CmpInfo.makeWeakResult(CCR))->VD;17824 // Check and evaluate the result as a constant expression.17825 LValue LV;17826 LV.set(VD);17827 if (!handleLValueToRValueConversion(Info, E, E->getType(), LV, Result))17828 return false;17829 return CheckConstantExpression(Info, E->getExprLoc(), E->getType(), Result,17830 ConstantExprKind::Normal);17831 };17832 return EvaluateComparisonBinaryOperator(Info, E, OnSuccess, [&]() {17833 return ExprEvaluatorBaseTy::VisitBinCmp(E);17834 });17835}17836 17837bool RecordExprEvaluator::VisitCXXParenListInitExpr(17838 const CXXParenListInitExpr *E) {17839 return VisitCXXParenListOrInitListExpr(E, E->getInitExprs());17840}17841 17842bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {17843 // We don't support assignment in C. C++ assignments don't get here because17844 // assignment is an lvalue in C++.17845 if (E->isAssignmentOp()) {17846 Error(E);17847 if (!Info.noteFailure())17848 return false;17849 }17850 17851 if (DataRecursiveIntBinOpEvaluator::shouldEnqueue(E))17852 return DataRecursiveIntBinOpEvaluator(*this, Result).Traverse(E);17853 17854 assert((!E->getLHS()->getType()->isIntegralOrEnumerationType() ||17855 !E->getRHS()->getType()->isIntegralOrEnumerationType()) &&17856 "DataRecursiveIntBinOpEvaluator should have handled integral types");17857 17858 if (E->isComparisonOp()) {17859 // Evaluate builtin binary comparisons by evaluating them as three-way17860 // comparisons and then translating the result.17861 auto OnSuccess = [&](CmpResult CR, const BinaryOperator *E) {17862 assert((CR != CmpResult::Unequal || E->isEqualityOp()) &&17863 "should only produce Unequal for equality comparisons");17864 bool IsEqual = CR == CmpResult::Equal,17865 IsLess = CR == CmpResult::Less,17866 IsGreater = CR == CmpResult::Greater;17867 auto Op = E->getOpcode();17868 switch (Op) {17869 default:17870 llvm_unreachable("unsupported binary operator");17871 case BO_EQ:17872 case BO_NE:17873 return Success(IsEqual == (Op == BO_EQ), E);17874 case BO_LT:17875 return Success(IsLess, E);17876 case BO_GT:17877 return Success(IsGreater, E);17878 case BO_LE:17879 return Success(IsEqual || IsLess, E);17880 case BO_GE:17881 return Success(IsEqual || IsGreater, E);17882 }17883 };17884 return EvaluateComparisonBinaryOperator(Info, E, OnSuccess, [&]() {17885 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);17886 });17887 }17888 17889 QualType LHSTy = E->getLHS()->getType();17890 QualType RHSTy = E->getRHS()->getType();17891 17892 if (LHSTy->isPointerType() && RHSTy->isPointerType() &&17893 E->getOpcode() == BO_Sub) {17894 LValue LHSValue, RHSValue;17895 17896 bool LHSOK = EvaluatePointer(E->getLHS(), LHSValue, Info);17897 if (!LHSOK && !Info.noteFailure())17898 return false;17899 17900 if (!EvaluatePointer(E->getRHS(), RHSValue, Info) || !LHSOK)17901 return false;17902 17903 // Reject differing bases from the normal codepath; we special-case17904 // comparisons to null.17905 if (!HasSameBase(LHSValue, RHSValue)) {17906 if (Info.checkingPotentialConstantExpression() &&17907 (LHSValue.AllowConstexprUnknown || RHSValue.AllowConstexprUnknown))17908 return false;17909 17910 const Expr *LHSExpr = LHSValue.Base.dyn_cast<const Expr *>();17911 const Expr *RHSExpr = RHSValue.Base.dyn_cast<const Expr *>();17912 17913 auto DiagArith = [&](unsigned DiagID) {17914 std::string LHS = LHSValue.toString(Info.Ctx, E->getLHS()->getType());17915 std::string RHS = RHSValue.toString(Info.Ctx, E->getRHS()->getType());17916 Info.FFDiag(E, DiagID) << LHS << RHS;17917 if (LHSExpr && LHSExpr == RHSExpr)17918 Info.Note(LHSExpr->getExprLoc(),17919 diag::note_constexpr_repeated_literal_eval)17920 << LHSExpr->getSourceRange();17921 return false;17922 };17923 17924 if (!LHSExpr || !RHSExpr)17925 return DiagArith(diag::note_constexpr_pointer_arith_unspecified);17926 17927 if (ArePotentiallyOverlappingStringLiterals(Info, LHSValue, RHSValue))17928 return DiagArith(diag::note_constexpr_literal_arith);17929 17930 const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr);17931 const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr);17932 if (!LHSAddrExpr || !RHSAddrExpr)17933 return Error(E);17934 // Make sure both labels come from the same function.17935 if (LHSAddrExpr->getLabel()->getDeclContext() !=17936 RHSAddrExpr->getLabel()->getDeclContext())17937 return Error(E);17938 return Success(APValue(LHSAddrExpr, RHSAddrExpr), E);17939 }17940 const CharUnits &LHSOffset = LHSValue.getLValueOffset();17941 const CharUnits &RHSOffset = RHSValue.getLValueOffset();17942 17943 SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator();17944 SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator();17945 17946 // C++11 [expr.add]p6:17947 // Unless both pointers point to elements of the same array object, or17948 // one past the last element of the array object, the behavior is17949 // undefined.17950 if (!LHSDesignator.Invalid && !RHSDesignator.Invalid &&17951 !AreElementsOfSameArray(getType(LHSValue.Base), LHSDesignator,17952 RHSDesignator))17953 Info.CCEDiag(E, diag::note_constexpr_pointer_subtraction_not_same_array);17954 17955 QualType Type = E->getLHS()->getType();17956 QualType ElementType = Type->castAs<PointerType>()->getPointeeType();17957 17958 CharUnits ElementSize;17959 if (!HandleSizeof(Info, E->getExprLoc(), ElementType, ElementSize))17960 return false;17961 17962 // As an extension, a type may have zero size (empty struct or union in17963 // C, array of zero length). Pointer subtraction in such cases has17964 // undefined behavior, so is not constant.17965 if (ElementSize.isZero()) {17966 Info.FFDiag(E, diag::note_constexpr_pointer_subtraction_zero_size)17967 << ElementType;17968 return false;17969 }17970 17971 // FIXME: LLVM and GCC both compute LHSOffset - RHSOffset at runtime,17972 // and produce incorrect results when it overflows. Such behavior17973 // appears to be non-conforming, but is common, so perhaps we should17974 // assume the standard intended for such cases to be undefined behavior17975 // and check for them.17976 17977 // Compute (LHSOffset - RHSOffset) / Size carefully, checking for17978 // overflow in the final conversion to ptrdiff_t.17979 APSInt LHS(llvm::APInt(65, (int64_t)LHSOffset.getQuantity(), true), false);17980 APSInt RHS(llvm::APInt(65, (int64_t)RHSOffset.getQuantity(), true), false);17981 APSInt ElemSize(llvm::APInt(65, (int64_t)ElementSize.getQuantity(), true),17982 false);17983 APSInt TrueResult = (LHS - RHS) / ElemSize;17984 APSInt Result = TrueResult.trunc(Info.Ctx.getIntWidth(E->getType()));17985 17986 if (Result.extend(65) != TrueResult &&17987 !HandleOverflow(Info, E, TrueResult, E->getType()))17988 return false;17989 return Success(Result, E);17990 }17991 17992 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);17993}17994 17995/// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with17996/// a result as the expression's type.17997bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr(17998 const UnaryExprOrTypeTraitExpr *E) {17999 switch(E->getKind()) {18000 case UETT_PreferredAlignOf:18001 case UETT_AlignOf: {18002 if (E->isArgumentType())18003 return Success(18004 GetAlignOfType(Info.Ctx, E->getArgumentType(), E->getKind()), E);18005 else18006 return Success(18007 GetAlignOfExpr(Info.Ctx, E->getArgumentExpr(), E->getKind()), E);18008 }18009 18010 case UETT_PtrAuthTypeDiscriminator: {18011 if (E->getArgumentType()->isDependentType())18012 return false;18013 return Success(18014 Info.Ctx.getPointerAuthTypeDiscriminator(E->getArgumentType()), E);18015 }18016 case UETT_VecStep: {18017 QualType Ty = E->getTypeOfArgument();18018 18019 if (Ty->isVectorType()) {18020 unsigned n = Ty->castAs<VectorType>()->getNumElements();18021 18022 // The vec_step built-in functions that take a 3-component18023 // vector return 4. (OpenCL 1.1 spec 6.11.12)18024 if (n == 3)18025 n = 4;18026 18027 return Success(n, E);18028 } else18029 return Success(1, E);18030 }18031 18032 case UETT_DataSizeOf:18033 case UETT_SizeOf: {18034 QualType SrcTy = E->getTypeOfArgument();18035 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,18036 // the result is the size of the referenced type."18037 if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>())18038 SrcTy = Ref->getPointeeType();18039 18040 CharUnits Sizeof;18041 if (!HandleSizeof(Info, E->getExprLoc(), SrcTy, Sizeof,18042 E->getKind() == UETT_DataSizeOf ? SizeOfType::DataSizeOf18043 : SizeOfType::SizeOf)) {18044 return false;18045 }18046 return Success(Sizeof, E);18047 }18048 case UETT_OpenMPRequiredSimdAlign:18049 assert(E->isArgumentType());18050 return Success(18051 Info.Ctx.toCharUnitsFromBits(18052 Info.Ctx.getOpenMPDefaultSimdAlign(E->getArgumentType()))18053 .getQuantity(),18054 E);18055 case UETT_VectorElements: {18056 QualType Ty = E->getTypeOfArgument();18057 // If the vector has a fixed size, we can determine the number of elements18058 // at compile time.18059 if (const auto *VT = Ty->getAs<VectorType>())18060 return Success(VT->getNumElements(), E);18061 18062 assert(Ty->isSizelessVectorType());18063 if (Info.InConstantContext)18064 Info.CCEDiag(E, diag::note_constexpr_non_const_vectorelements)18065 << E->getSourceRange();18066 18067 return false;18068 }18069 case UETT_CountOf: {18070 QualType Ty = E->getTypeOfArgument();18071 assert(Ty->isArrayType());18072 18073 // We don't need to worry about array element qualifiers, so getting the18074 // unsafe array type is fine.18075 if (const auto *CAT =18076 dyn_cast<ConstantArrayType>(Ty->getAsArrayTypeUnsafe())) {18077 return Success(CAT->getSize(), E);18078 }18079 18080 assert(!Ty->isConstantSizeType());18081 18082 // If it's a variable-length array type, we need to check whether it is a18083 // multidimensional array. If so, we need to check the size expression of18084 // the VLA to see if it's a constant size. If so, we can return that value.18085 const auto *VAT = Info.Ctx.getAsVariableArrayType(Ty);18086 assert(VAT);18087 if (VAT->getElementType()->isArrayType()) {18088 // Variable array size expression could be missing (e.g. int a[*][10]) In18089 // that case, it can't be a constant expression.18090 if (!VAT->getSizeExpr()) {18091 Info.FFDiag(E->getBeginLoc());18092 return false;18093 }18094 18095 std::optional<APSInt> Res =18096 VAT->getSizeExpr()->getIntegerConstantExpr(Info.Ctx);18097 if (Res) {18098 // The resulting value always has type size_t, so we need to make the18099 // returned APInt have the correct sign and bit-width.18100 APInt Val{18101 static_cast<unsigned>(Info.Ctx.getTypeSize(Info.Ctx.getSizeType())),18102 Res->getZExtValue()};18103 return Success(Val, E);18104 }18105 }18106 18107 // Definitely a variable-length type, which is not an ICE.18108 // FIXME: Better diagnostic.18109 Info.FFDiag(E->getBeginLoc());18110 return false;18111 }18112 }18113 18114 llvm_unreachable("unknown expr/type trait");18115}18116 18117bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) {18118 CharUnits Result;18119 unsigned n = OOE->getNumComponents();18120 if (n == 0)18121 return Error(OOE);18122 QualType CurrentType = OOE->getTypeSourceInfo()->getType();18123 for (unsigned i = 0; i != n; ++i) {18124 OffsetOfNode ON = OOE->getComponent(i);18125 switch (ON.getKind()) {18126 case OffsetOfNode::Array: {18127 const Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex());18128 APSInt IdxResult;18129 if (!EvaluateInteger(Idx, IdxResult, Info))18130 return false;18131 const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType);18132 if (!AT)18133 return Error(OOE);18134 CurrentType = AT->getElementType();18135 CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType);18136 Result += IdxResult.getSExtValue() * ElementSize;18137 break;18138 }18139 18140 case OffsetOfNode::Field: {18141 FieldDecl *MemberDecl = ON.getField();18142 const auto *RD = CurrentType->getAsRecordDecl();18143 if (!RD)18144 return Error(OOE);18145 if (RD->isInvalidDecl()) return false;18146 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);18147 unsigned i = MemberDecl->getFieldIndex();18148 assert(i < RL.getFieldCount() && "offsetof field in wrong type");18149 Result += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i));18150 CurrentType = MemberDecl->getType().getNonReferenceType();18151 break;18152 }18153 18154 case OffsetOfNode::Identifier:18155 llvm_unreachable("dependent __builtin_offsetof");18156 18157 case OffsetOfNode::Base: {18158 CXXBaseSpecifier *BaseSpec = ON.getBase();18159 if (BaseSpec->isVirtual())18160 return Error(OOE);18161 18162 // Find the layout of the class whose base we are looking into.18163 const auto *RD = CurrentType->getAsCXXRecordDecl();18164 if (!RD)18165 return Error(OOE);18166 if (RD->isInvalidDecl()) return false;18167 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);18168 18169 // Find the base class itself.18170 CurrentType = BaseSpec->getType();18171 const auto *BaseRD = CurrentType->getAsCXXRecordDecl();18172 if (!BaseRD)18173 return Error(OOE);18174 18175 // Add the offset to the base.18176 Result += RL.getBaseClassOffset(BaseRD);18177 break;18178 }18179 }18180 }18181 return Success(Result, OOE);18182}18183 18184bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {18185 switch (E->getOpcode()) {18186 default:18187 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.18188 // See C99 6.6p3.18189 return Error(E);18190 case UO_Extension:18191 // FIXME: Should extension allow i-c-e extension expressions in its scope?18192 // If so, we could clear the diagnostic ID.18193 return Visit(E->getSubExpr());18194 case UO_Plus:18195 // The result is just the value.18196 return Visit(E->getSubExpr());18197 case UO_Minus: {18198 if (!Visit(E->getSubExpr()))18199 return false;18200 if (!Result.isInt()) return Error(E);18201 const APSInt &Value = Result.getInt();18202 if (Value.isSigned() && Value.isMinSignedValue() && E->canOverflow()) {18203 if (Info.checkingForUndefinedBehavior())18204 Info.Ctx.getDiagnostics().Report(E->getExprLoc(),18205 diag::warn_integer_constant_overflow)18206 << toString(Value, 10, Value.isSigned(), /*formatAsCLiteral=*/false,18207 /*UpperCase=*/true, /*InsertSeparators=*/true)18208 << E->getType() << E->getSourceRange();18209 18210 if (!HandleOverflow(Info, E, -Value.extend(Value.getBitWidth() + 1),18211 E->getType()))18212 return false;18213 }18214 return Success(-Value, E);18215 }18216 case UO_Not: {18217 if (!Visit(E->getSubExpr()))18218 return false;18219 if (!Result.isInt()) return Error(E);18220 return Success(~Result.getInt(), E);18221 }18222 case UO_LNot: {18223 bool bres;18224 if (!EvaluateAsBooleanCondition(E->getSubExpr(), bres, Info))18225 return false;18226 return Success(!bres, E);18227 }18228 }18229}18230 18231/// HandleCast - This is used to evaluate implicit or explicit casts where the18232/// result type is integer.18233bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) {18234 const Expr *SubExpr = E->getSubExpr();18235 QualType DestType = E->getType();18236 QualType SrcType = SubExpr->getType();18237 18238 switch (E->getCastKind()) {18239 case CK_BaseToDerived:18240 case CK_DerivedToBase:18241 case CK_UncheckedDerivedToBase:18242 case CK_Dynamic:18243 case CK_ToUnion:18244 case CK_ArrayToPointerDecay:18245 case CK_FunctionToPointerDecay:18246 case CK_NullToPointer:18247 case CK_NullToMemberPointer:18248 case CK_BaseToDerivedMemberPointer:18249 case CK_DerivedToBaseMemberPointer:18250 case CK_ReinterpretMemberPointer:18251 case CK_ConstructorConversion:18252 case CK_IntegralToPointer:18253 case CK_ToVoid:18254 case CK_VectorSplat:18255 case CK_IntegralToFloating:18256 case CK_FloatingCast:18257 case CK_CPointerToObjCPointerCast:18258 case CK_BlockPointerToObjCPointerCast:18259 case CK_AnyPointerToBlockPointerCast:18260 case CK_ObjCObjectLValueCast:18261 case CK_FloatingRealToComplex:18262 case CK_FloatingComplexToReal:18263 case CK_FloatingComplexCast:18264 case CK_FloatingComplexToIntegralComplex:18265 case CK_IntegralRealToComplex:18266 case CK_IntegralComplexCast:18267 case CK_IntegralComplexToFloatingComplex:18268 case CK_BuiltinFnToFnPtr:18269 case CK_ZeroToOCLOpaqueType:18270 case CK_NonAtomicToAtomic:18271 case CK_AddressSpaceConversion:18272 case CK_IntToOCLSampler:18273 case CK_FloatingToFixedPoint:18274 case CK_FixedPointToFloating:18275 case CK_FixedPointCast:18276 case CK_IntegralToFixedPoint:18277 case CK_MatrixCast:18278 case CK_HLSLAggregateSplatCast:18279 llvm_unreachable("invalid cast kind for integral value");18280 18281 case CK_BitCast:18282 case CK_Dependent:18283 case CK_LValueBitCast:18284 case CK_ARCProduceObject:18285 case CK_ARCConsumeObject:18286 case CK_ARCReclaimReturnedObject:18287 case CK_ARCExtendBlockObject:18288 case CK_CopyAndAutoreleaseBlockObject:18289 return Error(E);18290 18291 case CK_UserDefinedConversion:18292 case CK_LValueToRValue:18293 case CK_AtomicToNonAtomic:18294 case CK_NoOp:18295 case CK_LValueToRValueBitCast:18296 case CK_HLSLArrayRValue:18297 return ExprEvaluatorBaseTy::VisitCastExpr(E);18298 18299 case CK_MemberPointerToBoolean:18300 case CK_PointerToBoolean:18301 case CK_IntegralToBoolean:18302 case CK_FloatingToBoolean:18303 case CK_BooleanToSignedIntegral:18304 case CK_FloatingComplexToBoolean:18305 case CK_IntegralComplexToBoolean: {18306 bool BoolResult;18307 if (!EvaluateAsBooleanCondition(SubExpr, BoolResult, Info))18308 return false;18309 uint64_t IntResult = BoolResult;18310 if (BoolResult && E->getCastKind() == CK_BooleanToSignedIntegral)18311 IntResult = (uint64_t)-1;18312 return Success(IntResult, E);18313 }18314 18315 case CK_FixedPointToIntegral: {18316 APFixedPoint Src(Info.Ctx.getFixedPointSemantics(SrcType));18317 if (!EvaluateFixedPoint(SubExpr, Src, Info))18318 return false;18319 bool Overflowed;18320 llvm::APSInt Result = Src.convertToInt(18321 Info.Ctx.getIntWidth(DestType),18322 DestType->isSignedIntegerOrEnumerationType(), &Overflowed);18323 if (Overflowed && !HandleOverflow(Info, E, Result, DestType))18324 return false;18325 return Success(Result, E);18326 }18327 18328 case CK_FixedPointToBoolean: {18329 // Unsigned padding does not affect this.18330 APValue Val;18331 if (!Evaluate(Val, Info, SubExpr))18332 return false;18333 return Success(Val.getFixedPoint().getBoolValue(), E);18334 }18335 18336 case CK_IntegralCast: {18337 if (!Visit(SubExpr))18338 return false;18339 18340 if (!Result.isInt()) {18341 // Allow casts of address-of-label differences if they are no-ops18342 // or narrowing. (The narrowing case isn't actually guaranteed to18343 // be constant-evaluatable except in some narrow cases which are hard18344 // to detect here. We let it through on the assumption the user knows18345 // what they are doing.)18346 if (Result.isAddrLabelDiff())18347 return Info.Ctx.getTypeSize(DestType) <= Info.Ctx.getTypeSize(SrcType);18348 // Only allow casts of lvalues if they are lossless.18349 return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);18350 }18351 18352 if (Info.Ctx.getLangOpts().CPlusPlus && DestType->isEnumeralType()) {18353 const auto *ED = DestType->getAsEnumDecl();18354 // Check that the value is within the range of the enumeration values.18355 //18356 // This corressponds to [expr.static.cast]p10 which says:18357 // A value of integral or enumeration type can be explicitly converted18358 // to a complete enumeration type ... If the enumeration type does not18359 // have a fixed underlying type, the value is unchanged if the original18360 // value is within the range of the enumeration values ([dcl.enum]), and18361 // otherwise, the behavior is undefined.18362 //18363 // This was resolved as part of DR2338 which has CD5 status.18364 if (!ED->isFixed()) {18365 llvm::APInt Min;18366 llvm::APInt Max;18367 18368 ED->getValueRange(Max, Min);18369 --Max;18370 18371 if (ED->getNumNegativeBits() &&18372 (Max.slt(Result.getInt().getSExtValue()) ||18373 Min.sgt(Result.getInt().getSExtValue())))18374 Info.CCEDiag(E, diag::note_constexpr_unscoped_enum_out_of_range)18375 << llvm::toString(Result.getInt(), 10) << Min.getSExtValue()18376 << Max.getSExtValue() << ED;18377 else if (!ED->getNumNegativeBits() &&18378 Max.ult(Result.getInt().getZExtValue()))18379 Info.CCEDiag(E, diag::note_constexpr_unscoped_enum_out_of_range)18380 << llvm::toString(Result.getInt(), 10) << Min.getZExtValue()18381 << Max.getZExtValue() << ED;18382 }18383 }18384 18385 return Success(HandleIntToIntCast(Info, E, DestType, SrcType,18386 Result.getInt()), E);18387 }18388 18389 case CK_PointerToIntegral: {18390 CCEDiag(E, diag::note_constexpr_invalid_cast)18391 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret18392 << Info.Ctx.getLangOpts().CPlusPlus << E->getSourceRange();18393 18394 LValue LV;18395 if (!EvaluatePointer(SubExpr, LV, Info))18396 return false;18397 18398 if (LV.getLValueBase()) {18399 // Only allow based lvalue casts if they are lossless.18400 // FIXME: Allow a larger integer size than the pointer size, and allow18401 // narrowing back down to pointer width in subsequent integral casts.18402 // FIXME: Check integer type's active bits, not its type size.18403 if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))18404 return Error(E);18405 18406 LV.Designator.setInvalid();18407 LV.moveInto(Result);18408 return true;18409 }18410 18411 APSInt AsInt;18412 APValue V;18413 LV.moveInto(V);18414 if (!V.toIntegralConstant(AsInt, SrcType, Info.Ctx))18415 llvm_unreachable("Can't cast this!");18416 18417 return Success(HandleIntToIntCast(Info, E, DestType, SrcType, AsInt), E);18418 }18419 18420 case CK_IntegralComplexToReal: {18421 ComplexValue C;18422 if (!EvaluateComplex(SubExpr, C, Info))18423 return false;18424 return Success(C.getComplexIntReal(), E);18425 }18426 18427 case CK_FloatingToIntegral: {18428 APFloat F(0.0);18429 if (!EvaluateFloat(SubExpr, F, Info))18430 return false;18431 18432 APSInt Value;18433 if (!HandleFloatToIntCast(Info, E, SrcType, F, DestType, Value))18434 return false;18435 return Success(Value, E);18436 }18437 case CK_HLSLVectorTruncation: {18438 APValue Val;18439 if (!EvaluateVector(SubExpr, Val, Info))18440 return Error(E);18441 return Success(Val.getVectorElt(0), E);18442 }18443 case CK_HLSLElementwiseCast: {18444 SmallVector<APValue> SrcVals;18445 SmallVector<QualType> SrcTypes;18446 18447 if (!hlslElementwiseCastHelper(Info, SubExpr, DestType, SrcVals, SrcTypes))18448 return false;18449 18450 // cast our single element18451 const FPOptions FPO = E->getFPFeaturesInEffect(Info.Ctx.getLangOpts());18452 APValue ResultVal;18453 if (!handleScalarCast(Info, FPO, E, SrcTypes[0], DestType, SrcVals[0],18454 ResultVal))18455 return false;18456 return Success(ResultVal, E);18457 }18458 }18459 18460 llvm_unreachable("unknown cast resulting in integral value");18461}18462 18463bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {18464 if (E->getSubExpr()->getType()->isAnyComplexType()) {18465 ComplexValue LV;18466 if (!EvaluateComplex(E->getSubExpr(), LV, Info))18467 return false;18468 if (!LV.isComplexInt())18469 return Error(E);18470 return Success(LV.getComplexIntReal(), E);18471 }18472 18473 return Visit(E->getSubExpr());18474}18475 18476bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {18477 if (E->getSubExpr()->getType()->isComplexIntegerType()) {18478 ComplexValue LV;18479 if (!EvaluateComplex(E->getSubExpr(), LV, Info))18480 return false;18481 if (!LV.isComplexInt())18482 return Error(E);18483 return Success(LV.getComplexIntImag(), E);18484 }18485 18486 VisitIgnoredValue(E->getSubExpr());18487 return Success(0, E);18488}18489 18490bool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) {18491 return Success(E->getPackLength(), E);18492}18493 18494bool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) {18495 return Success(E->getValue(), E);18496}18497 18498bool IntExprEvaluator::VisitConceptSpecializationExpr(18499 const ConceptSpecializationExpr *E) {18500 return Success(E->isSatisfied(), E);18501}18502 18503bool IntExprEvaluator::VisitRequiresExpr(const RequiresExpr *E) {18504 return Success(E->isSatisfied(), E);18505}18506 18507bool FixedPointExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {18508 switch (E->getOpcode()) {18509 default:18510 // Invalid unary operators18511 return Error(E);18512 case UO_Plus:18513 // The result is just the value.18514 return Visit(E->getSubExpr());18515 case UO_Minus: {18516 if (!Visit(E->getSubExpr())) return false;18517 if (!Result.isFixedPoint())18518 return Error(E);18519 bool Overflowed;18520 APFixedPoint Negated = Result.getFixedPoint().negate(&Overflowed);18521 if (Overflowed && !HandleOverflow(Info, E, Negated, E->getType()))18522 return false;18523 return Success(Negated, E);18524 }18525 case UO_LNot: {18526 bool bres;18527 if (!EvaluateAsBooleanCondition(E->getSubExpr(), bres, Info))18528 return false;18529 return Success(!bres, E);18530 }18531 }18532}18533 18534bool FixedPointExprEvaluator::VisitCastExpr(const CastExpr *E) {18535 const Expr *SubExpr = E->getSubExpr();18536 QualType DestType = E->getType();18537 assert(DestType->isFixedPointType() &&18538 "Expected destination type to be a fixed point type");18539 auto DestFXSema = Info.Ctx.getFixedPointSemantics(DestType);18540 18541 switch (E->getCastKind()) {18542 case CK_FixedPointCast: {18543 APFixedPoint Src(Info.Ctx.getFixedPointSemantics(SubExpr->getType()));18544 if (!EvaluateFixedPoint(SubExpr, Src, Info))18545 return false;18546 bool Overflowed;18547 APFixedPoint Result = Src.convert(DestFXSema, &Overflowed);18548 if (Overflowed) {18549 if (Info.checkingForUndefinedBehavior())18550 Info.Ctx.getDiagnostics().Report(E->getExprLoc(),18551 diag::warn_fixedpoint_constant_overflow)18552 << Result.toString() << E->getType();18553 if (!HandleOverflow(Info, E, Result, E->getType()))18554 return false;18555 }18556 return Success(Result, E);18557 }18558 case CK_IntegralToFixedPoint: {18559 APSInt Src;18560 if (!EvaluateInteger(SubExpr, Src, Info))18561 return false;18562 18563 bool Overflowed;18564 APFixedPoint IntResult = APFixedPoint::getFromIntValue(18565 Src, Info.Ctx.getFixedPointSemantics(DestType), &Overflowed);18566 18567 if (Overflowed) {18568 if (Info.checkingForUndefinedBehavior())18569 Info.Ctx.getDiagnostics().Report(E->getExprLoc(),18570 diag::warn_fixedpoint_constant_overflow)18571 << IntResult.toString() << E->getType();18572 if (!HandleOverflow(Info, E, IntResult, E->getType()))18573 return false;18574 }18575 18576 return Success(IntResult, E);18577 }18578 case CK_FloatingToFixedPoint: {18579 APFloat Src(0.0);18580 if (!EvaluateFloat(SubExpr, Src, Info))18581 return false;18582 18583 bool Overflowed;18584 APFixedPoint Result = APFixedPoint::getFromFloatValue(18585 Src, Info.Ctx.getFixedPointSemantics(DestType), &Overflowed);18586 18587 if (Overflowed) {18588 if (Info.checkingForUndefinedBehavior())18589 Info.Ctx.getDiagnostics().Report(E->getExprLoc(),18590 diag::warn_fixedpoint_constant_overflow)18591 << Result.toString() << E->getType();18592 if (!HandleOverflow(Info, E, Result, E->getType()))18593 return false;18594 }18595 18596 return Success(Result, E);18597 }18598 case CK_NoOp:18599 case CK_LValueToRValue:18600 return ExprEvaluatorBaseTy::VisitCastExpr(E);18601 default:18602 return Error(E);18603 }18604}18605 18606bool FixedPointExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {18607 if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma)18608 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);18609 18610 const Expr *LHS = E->getLHS();18611 const Expr *RHS = E->getRHS();18612 FixedPointSemantics ResultFXSema =18613 Info.Ctx.getFixedPointSemantics(E->getType());18614 18615 APFixedPoint LHSFX(Info.Ctx.getFixedPointSemantics(LHS->getType()));18616 if (!EvaluateFixedPointOrInteger(LHS, LHSFX, Info))18617 return false;18618 APFixedPoint RHSFX(Info.Ctx.getFixedPointSemantics(RHS->getType()));18619 if (!EvaluateFixedPointOrInteger(RHS, RHSFX, Info))18620 return false;18621 18622 bool OpOverflow = false, ConversionOverflow = false;18623 APFixedPoint Result(LHSFX.getSemantics());18624 switch (E->getOpcode()) {18625 case BO_Add: {18626 Result = LHSFX.add(RHSFX, &OpOverflow)18627 .convert(ResultFXSema, &ConversionOverflow);18628 break;18629 }18630 case BO_Sub: {18631 Result = LHSFX.sub(RHSFX, &OpOverflow)18632 .convert(ResultFXSema, &ConversionOverflow);18633 break;18634 }18635 case BO_Mul: {18636 Result = LHSFX.mul(RHSFX, &OpOverflow)18637 .convert(ResultFXSema, &ConversionOverflow);18638 break;18639 }18640 case BO_Div: {18641 if (RHSFX.getValue() == 0) {18642 Info.FFDiag(E, diag::note_expr_divide_by_zero);18643 return false;18644 }18645 Result = LHSFX.div(RHSFX, &OpOverflow)18646 .convert(ResultFXSema, &ConversionOverflow);18647 break;18648 }18649 case BO_Shl:18650 case BO_Shr: {18651 FixedPointSemantics LHSSema = LHSFX.getSemantics();18652 llvm::APSInt RHSVal = RHSFX.getValue();18653 18654 unsigned ShiftBW =18655 LHSSema.getWidth() - (unsigned)LHSSema.hasUnsignedPadding();18656 unsigned Amt = RHSVal.getLimitedValue(ShiftBW - 1);18657 // Embedded-C 4.1.6.2.2:18658 // The right operand must be nonnegative and less than the total number18659 // of (nonpadding) bits of the fixed-point operand ...18660 if (RHSVal.isNegative())18661 Info.CCEDiag(E, diag::note_constexpr_negative_shift) << RHSVal;18662 else if (Amt != RHSVal)18663 Info.CCEDiag(E, diag::note_constexpr_large_shift)18664 << RHSVal << E->getType() << ShiftBW;18665 18666 if (E->getOpcode() == BO_Shl)18667 Result = LHSFX.shl(Amt, &OpOverflow);18668 else18669 Result = LHSFX.shr(Amt, &OpOverflow);18670 break;18671 }18672 default:18673 return false;18674 }18675 if (OpOverflow || ConversionOverflow) {18676 if (Info.checkingForUndefinedBehavior())18677 Info.Ctx.getDiagnostics().Report(E->getExprLoc(),18678 diag::warn_fixedpoint_constant_overflow)18679 << Result.toString() << E->getType();18680 if (!HandleOverflow(Info, E, Result, E->getType()))18681 return false;18682 }18683 return Success(Result, E);18684}18685 18686//===----------------------------------------------------------------------===//18687// Float Evaluation18688//===----------------------------------------------------------------------===//18689 18690namespace {18691class FloatExprEvaluator18692 : public ExprEvaluatorBase<FloatExprEvaluator> {18693 APFloat &Result;18694public:18695 FloatExprEvaluator(EvalInfo &info, APFloat &result)18696 : ExprEvaluatorBaseTy(info), Result(result) {}18697 18698 bool Success(const APValue &V, const Expr *e) {18699 Result = V.getFloat();18700 return true;18701 }18702 18703 bool ZeroInitialization(const Expr *E) {18704 Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));18705 return true;18706 }18707 18708 bool VisitCallExpr(const CallExpr *E);18709 18710 bool VisitUnaryOperator(const UnaryOperator *E);18711 bool VisitBinaryOperator(const BinaryOperator *E);18712 bool VisitFloatingLiteral(const FloatingLiteral *E);18713 bool VisitCastExpr(const CastExpr *E);18714 18715 bool VisitUnaryReal(const UnaryOperator *E);18716 bool VisitUnaryImag(const UnaryOperator *E);18717 18718 // FIXME: Missing: array subscript of vector, member of vector18719};18720} // end anonymous namespace18721 18722static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {18723 assert(!E->isValueDependent());18724 assert(E->isPRValue() && E->getType()->isRealFloatingType());18725 return FloatExprEvaluator(Info, Result).Visit(E);18726}18727 18728static bool TryEvaluateBuiltinNaN(const ASTContext &Context,18729 QualType ResultTy,18730 const Expr *Arg,18731 bool SNaN,18732 llvm::APFloat &Result) {18733 const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts());18734 if (!S) return false;18735 18736 const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy);18737 18738 llvm::APInt fill;18739 18740 // Treat empty strings as if they were zero.18741 if (S->getString().empty())18742 fill = llvm::APInt(32, 0);18743 else if (S->getString().getAsInteger(0, fill))18744 return false;18745 18746 if (Context.getTargetInfo().isNan2008()) {18747 if (SNaN)18748 Result = llvm::APFloat::getSNaN(Sem, false, &fill);18749 else18750 Result = llvm::APFloat::getQNaN(Sem, false, &fill);18751 } else {18752 // Prior to IEEE 754-2008, architectures were allowed to choose whether18753 // the first bit of their significand was set for qNaN or sNaN. MIPS chose18754 // a different encoding to what became a standard in 2008, and for pre-18755 // 2008 revisions, MIPS interpreted sNaN-2008 as qNan and qNaN-2008 as18756 // sNaN. This is now known as "legacy NaN" encoding.18757 if (SNaN)18758 Result = llvm::APFloat::getQNaN(Sem, false, &fill);18759 else18760 Result = llvm::APFloat::getSNaN(Sem, false, &fill);18761 }18762 18763 return true;18764}18765 18766bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {18767 if (!IsConstantEvaluatedBuiltinCall(E))18768 return ExprEvaluatorBaseTy::VisitCallExpr(E);18769 18770 switch (E->getBuiltinCallee()) {18771 default:18772 return false;18773 18774 case Builtin::BI__builtin_huge_val:18775 case Builtin::BI__builtin_huge_valf:18776 case Builtin::BI__builtin_huge_vall:18777 case Builtin::BI__builtin_huge_valf16:18778 case Builtin::BI__builtin_huge_valf128:18779 case Builtin::BI__builtin_inf:18780 case Builtin::BI__builtin_inff:18781 case Builtin::BI__builtin_infl:18782 case Builtin::BI__builtin_inff16:18783 case Builtin::BI__builtin_inff128: {18784 const llvm::fltSemantics &Sem =18785 Info.Ctx.getFloatTypeSemantics(E->getType());18786 Result = llvm::APFloat::getInf(Sem);18787 return true;18788 }18789 18790 case Builtin::BI__builtin_nans:18791 case Builtin::BI__builtin_nansf:18792 case Builtin::BI__builtin_nansl:18793 case Builtin::BI__builtin_nansf16:18794 case Builtin::BI__builtin_nansf128:18795 if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),18796 true, Result))18797 return Error(E);18798 return true;18799 18800 case Builtin::BI__builtin_nan:18801 case Builtin::BI__builtin_nanf:18802 case Builtin::BI__builtin_nanl:18803 case Builtin::BI__builtin_nanf16:18804 case Builtin::BI__builtin_nanf128:18805 // If this is __builtin_nan() turn this into a nan, otherwise we18806 // can't constant fold it.18807 if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),18808 false, Result))18809 return Error(E);18810 return true;18811 18812 case Builtin::BI__builtin_elementwise_abs:18813 case Builtin::BI__builtin_fabs:18814 case Builtin::BI__builtin_fabsf:18815 case Builtin::BI__builtin_fabsl:18816 case Builtin::BI__builtin_fabsf128:18817 // The C standard says "fabs raises no floating-point exceptions,18818 // even if x is a signaling NaN. The returned value is independent of18819 // the current rounding direction mode." Therefore constant folding can18820 // proceed without regard to the floating point settings.18821 // Reference, WG14 N2478 F.10.4.318822 if (!EvaluateFloat(E->getArg(0), Result, Info))18823 return false;18824 18825 if (Result.isNegative())18826 Result.changeSign();18827 return true;18828 18829 case Builtin::BI__arithmetic_fence:18830 return EvaluateFloat(E->getArg(0), Result, Info);18831 18832 // FIXME: Builtin::BI__builtin_powi18833 // FIXME: Builtin::BI__builtin_powif18834 // FIXME: Builtin::BI__builtin_powil18835 18836 case Builtin::BI__builtin_copysign:18837 case Builtin::BI__builtin_copysignf:18838 case Builtin::BI__builtin_copysignl:18839 case Builtin::BI__builtin_copysignf128: {18840 APFloat RHS(0.);18841 if (!EvaluateFloat(E->getArg(0), Result, Info) ||18842 !EvaluateFloat(E->getArg(1), RHS, Info))18843 return false;18844 Result.copySign(RHS);18845 return true;18846 }18847 18848 case Builtin::BI__builtin_fmax:18849 case Builtin::BI__builtin_fmaxf:18850 case Builtin::BI__builtin_fmaxl:18851 case Builtin::BI__builtin_fmaxf16:18852 case Builtin::BI__builtin_fmaxf128: {18853 APFloat RHS(0.);18854 if (!EvaluateFloat(E->getArg(0), Result, Info) ||18855 !EvaluateFloat(E->getArg(1), RHS, Info))18856 return false;18857 Result = maxnum(Result, RHS);18858 return true;18859 }18860 18861 case Builtin::BI__builtin_fmin:18862 case Builtin::BI__builtin_fminf:18863 case Builtin::BI__builtin_fminl:18864 case Builtin::BI__builtin_fminf16:18865 case Builtin::BI__builtin_fminf128: {18866 APFloat RHS(0.);18867 if (!EvaluateFloat(E->getArg(0), Result, Info) ||18868 !EvaluateFloat(E->getArg(1), RHS, Info))18869 return false;18870 Result = minnum(Result, RHS);18871 return true;18872 }18873 18874 case Builtin::BI__builtin_fmaximum_num:18875 case Builtin::BI__builtin_fmaximum_numf:18876 case Builtin::BI__builtin_fmaximum_numl:18877 case Builtin::BI__builtin_fmaximum_numf16:18878 case Builtin::BI__builtin_fmaximum_numf128: {18879 APFloat RHS(0.);18880 if (!EvaluateFloat(E->getArg(0), Result, Info) ||18881 !EvaluateFloat(E->getArg(1), RHS, Info))18882 return false;18883 Result = maximumnum(Result, RHS);18884 return true;18885 }18886 18887 case Builtin::BI__builtin_fminimum_num:18888 case Builtin::BI__builtin_fminimum_numf:18889 case Builtin::BI__builtin_fminimum_numl:18890 case Builtin::BI__builtin_fminimum_numf16:18891 case Builtin::BI__builtin_fminimum_numf128: {18892 APFloat RHS(0.);18893 if (!EvaluateFloat(E->getArg(0), Result, Info) ||18894 !EvaluateFloat(E->getArg(1), RHS, Info))18895 return false;18896 Result = minimumnum(Result, RHS);18897 return true;18898 }18899 18900 case Builtin::BI__builtin_elementwise_fma: {18901 if (!E->getArg(0)->isPRValue() || !E->getArg(1)->isPRValue() ||18902 !E->getArg(2)->isPRValue()) {18903 return false;18904 }18905 APFloat SourceY(0.), SourceZ(0.);18906 if (!EvaluateFloat(E->getArg(0), Result, Info) ||18907 !EvaluateFloat(E->getArg(1), SourceY, Info) ||18908 !EvaluateFloat(E->getArg(2), SourceZ, Info))18909 return false;18910 llvm::RoundingMode RM = getActiveRoundingMode(getEvalInfo(), E);18911 (void)Result.fusedMultiplyAdd(SourceY, SourceZ, RM);18912 return true;18913 }18914 18915 case clang::X86::BI__builtin_ia32_vec_ext_v4sf: {18916 APValue Vec;18917 APSInt IdxAPS;18918 if (!EvaluateVector(E->getArg(0), Vec, Info) ||18919 !EvaluateInteger(E->getArg(1), IdxAPS, Info))18920 return false;18921 unsigned N = Vec.getVectorLength();18922 unsigned Idx = static_cast<unsigned>(IdxAPS.getZExtValue() & (N - 1));18923 return Success(Vec.getVectorElt(Idx), E);18924 }18925 }18926}18927 18928bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {18929 if (E->getSubExpr()->getType()->isAnyComplexType()) {18930 ComplexValue CV;18931 if (!EvaluateComplex(E->getSubExpr(), CV, Info))18932 return false;18933 Result = CV.FloatReal;18934 return true;18935 }18936 18937 return Visit(E->getSubExpr());18938}18939 18940bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {18941 if (E->getSubExpr()->getType()->isAnyComplexType()) {18942 ComplexValue CV;18943 if (!EvaluateComplex(E->getSubExpr(), CV, Info))18944 return false;18945 Result = CV.FloatImag;18946 return true;18947 }18948 18949 VisitIgnoredValue(E->getSubExpr());18950 const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(E->getType());18951 Result = llvm::APFloat::getZero(Sem);18952 return true;18953}18954 18955bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {18956 switch (E->getOpcode()) {18957 default: return Error(E);18958 case UO_Plus:18959 return EvaluateFloat(E->getSubExpr(), Result, Info);18960 case UO_Minus:18961 // In C standard, WG14 N2478 F.3 p418962 // "the unary - raises no floating point exceptions,18963 // even if the operand is signalling."18964 if (!EvaluateFloat(E->getSubExpr(), Result, Info))18965 return false;18966 Result.changeSign();18967 return true;18968 }18969}18970 18971bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {18972 if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma)18973 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);18974 18975 APFloat RHS(0.0);18976 bool LHSOK = EvaluateFloat(E->getLHS(), Result, Info);18977 if (!LHSOK && !Info.noteFailure())18978 return false;18979 return EvaluateFloat(E->getRHS(), RHS, Info) && LHSOK &&18980 handleFloatFloatBinOp(Info, E, Result, E->getOpcode(), RHS);18981}18982 18983bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {18984 Result = E->getValue();18985 return true;18986}18987 18988bool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) {18989 const Expr* SubExpr = E->getSubExpr();18990 18991 switch (E->getCastKind()) {18992 default:18993 return ExprEvaluatorBaseTy::VisitCastExpr(E);18994 18995 case CK_HLSLAggregateSplatCast:18996 llvm_unreachable("invalid cast kind for floating value");18997 18998 case CK_IntegralToFloating: {18999 APSInt IntResult;19000 const FPOptions FPO = E->getFPFeaturesInEffect(19001 Info.Ctx.getLangOpts());19002 return EvaluateInteger(SubExpr, IntResult, Info) &&19003 HandleIntToFloatCast(Info, E, FPO, SubExpr->getType(),19004 IntResult, E->getType(), Result);19005 }19006 19007 case CK_FixedPointToFloating: {19008 APFixedPoint FixResult(Info.Ctx.getFixedPointSemantics(SubExpr->getType()));19009 if (!EvaluateFixedPoint(SubExpr, FixResult, Info))19010 return false;19011 Result =19012 FixResult.convertToFloat(Info.Ctx.getFloatTypeSemantics(E->getType()));19013 return true;19014 }19015 19016 case CK_FloatingCast: {19017 if (!Visit(SubExpr))19018 return false;19019 return HandleFloatToFloatCast(Info, E, SubExpr->getType(), E->getType(),19020 Result);19021 }19022 19023 case CK_FloatingComplexToReal: {19024 ComplexValue V;19025 if (!EvaluateComplex(SubExpr, V, Info))19026 return false;19027 Result = V.getComplexFloatReal();19028 return true;19029 }19030 case CK_HLSLVectorTruncation: {19031 APValue Val;19032 if (!EvaluateVector(SubExpr, Val, Info))19033 return Error(E);19034 return Success(Val.getVectorElt(0), E);19035 }19036 case CK_HLSLElementwiseCast: {19037 SmallVector<APValue> SrcVals;19038 SmallVector<QualType> SrcTypes;19039 19040 if (!hlslElementwiseCastHelper(Info, SubExpr, E->getType(), SrcVals,19041 SrcTypes))19042 return false;19043 APValue Val;19044 19045 // cast our single element19046 const FPOptions FPO = E->getFPFeaturesInEffect(Info.Ctx.getLangOpts());19047 APValue ResultVal;19048 if (!handleScalarCast(Info, FPO, E, SrcTypes[0], E->getType(), SrcVals[0],19049 ResultVal))19050 return false;19051 return Success(ResultVal, E);19052 }19053 }19054}19055 19056//===----------------------------------------------------------------------===//19057// Complex Evaluation (for float and integer)19058//===----------------------------------------------------------------------===//19059 19060namespace {19061class ComplexExprEvaluator19062 : public ExprEvaluatorBase<ComplexExprEvaluator> {19063 ComplexValue &Result;19064 19065public:19066 ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result)19067 : ExprEvaluatorBaseTy(info), Result(Result) {}19068 19069 bool Success(const APValue &V, const Expr *e) {19070 Result.setFrom(V);19071 return true;19072 }19073 19074 bool ZeroInitialization(const Expr *E);19075 19076 //===--------------------------------------------------------------------===//19077 // Visitor Methods19078 //===--------------------------------------------------------------------===//19079 19080 bool VisitImaginaryLiteral(const ImaginaryLiteral *E);19081 bool VisitCastExpr(const CastExpr *E);19082 bool VisitBinaryOperator(const BinaryOperator *E);19083 bool VisitUnaryOperator(const UnaryOperator *E);19084 bool VisitInitListExpr(const InitListExpr *E);19085 bool VisitCallExpr(const CallExpr *E);19086};19087} // end anonymous namespace19088 19089static bool EvaluateComplex(const Expr *E, ComplexValue &Result,19090 EvalInfo &Info) {19091 assert(!E->isValueDependent());19092 assert(E->isPRValue() && E->getType()->isAnyComplexType());19093 return ComplexExprEvaluator(Info, Result).Visit(E);19094}19095 19096bool ComplexExprEvaluator::ZeroInitialization(const Expr *E) {19097 QualType ElemTy = E->getType()->castAs<ComplexType>()->getElementType();19098 if (ElemTy->isRealFloatingType()) {19099 Result.makeComplexFloat();19100 APFloat Zero = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(ElemTy));19101 Result.FloatReal = Zero;19102 Result.FloatImag = Zero;19103 } else {19104 Result.makeComplexInt();19105 APSInt Zero = Info.Ctx.MakeIntValue(0, ElemTy);19106 Result.IntReal = Zero;19107 Result.IntImag = Zero;19108 }19109 return true;19110}19111 19112bool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) {19113 const Expr* SubExpr = E->getSubExpr();19114 19115 if (SubExpr->getType()->isRealFloatingType()) {19116 Result.makeComplexFloat();19117 APFloat &Imag = Result.FloatImag;19118 if (!EvaluateFloat(SubExpr, Imag, Info))19119 return false;19120 19121 Result.FloatReal = APFloat(Imag.getSemantics());19122 return true;19123 } else {19124 assert(SubExpr->getType()->isIntegerType() &&19125 "Unexpected imaginary literal.");19126 19127 Result.makeComplexInt();19128 APSInt &Imag = Result.IntImag;19129 if (!EvaluateInteger(SubExpr, Imag, Info))19130 return false;19131 19132 Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned());19133 return true;19134 }19135}19136 19137bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) {19138 19139 switch (E->getCastKind()) {19140 case CK_BitCast:19141 case CK_BaseToDerived:19142 case CK_DerivedToBase:19143 case CK_UncheckedDerivedToBase:19144 case CK_Dynamic:19145 case CK_ToUnion:19146 case CK_ArrayToPointerDecay:19147 case CK_FunctionToPointerDecay:19148 case CK_NullToPointer:19149 case CK_NullToMemberPointer:19150 case CK_BaseToDerivedMemberPointer:19151 case CK_DerivedToBaseMemberPointer:19152 case CK_MemberPointerToBoolean:19153 case CK_ReinterpretMemberPointer:19154 case CK_ConstructorConversion:19155 case CK_IntegralToPointer:19156 case CK_PointerToIntegral:19157 case CK_PointerToBoolean:19158 case CK_ToVoid:19159 case CK_VectorSplat:19160 case CK_IntegralCast:19161 case CK_BooleanToSignedIntegral:19162 case CK_IntegralToBoolean:19163 case CK_IntegralToFloating:19164 case CK_FloatingToIntegral:19165 case CK_FloatingToBoolean:19166 case CK_FloatingCast:19167 case CK_CPointerToObjCPointerCast:19168 case CK_BlockPointerToObjCPointerCast:19169 case CK_AnyPointerToBlockPointerCast:19170 case CK_ObjCObjectLValueCast:19171 case CK_FloatingComplexToReal:19172 case CK_FloatingComplexToBoolean:19173 case CK_IntegralComplexToReal:19174 case CK_IntegralComplexToBoolean:19175 case CK_ARCProduceObject:19176 case CK_ARCConsumeObject:19177 case CK_ARCReclaimReturnedObject:19178 case CK_ARCExtendBlockObject:19179 case CK_CopyAndAutoreleaseBlockObject:19180 case CK_BuiltinFnToFnPtr:19181 case CK_ZeroToOCLOpaqueType:19182 case CK_NonAtomicToAtomic:19183 case CK_AddressSpaceConversion:19184 case CK_IntToOCLSampler:19185 case CK_FloatingToFixedPoint:19186 case CK_FixedPointToFloating:19187 case CK_FixedPointCast:19188 case CK_FixedPointToBoolean:19189 case CK_FixedPointToIntegral:19190 case CK_IntegralToFixedPoint:19191 case CK_MatrixCast:19192 case CK_HLSLVectorTruncation:19193 case CK_HLSLElementwiseCast:19194 case CK_HLSLAggregateSplatCast:19195 llvm_unreachable("invalid cast kind for complex value");19196 19197 case CK_LValueToRValue:19198 case CK_AtomicToNonAtomic:19199 case CK_NoOp:19200 case CK_LValueToRValueBitCast:19201 case CK_HLSLArrayRValue:19202 return ExprEvaluatorBaseTy::VisitCastExpr(E);19203 19204 case CK_Dependent:19205 case CK_LValueBitCast:19206 case CK_UserDefinedConversion:19207 return Error(E);19208 19209 case CK_FloatingRealToComplex: {19210 APFloat &Real = Result.FloatReal;19211 if (!EvaluateFloat(E->getSubExpr(), Real, Info))19212 return false;19213 19214 Result.makeComplexFloat();19215 Result.FloatImag = APFloat(Real.getSemantics());19216 return true;19217 }19218 19219 case CK_FloatingComplexCast: {19220 if (!Visit(E->getSubExpr()))19221 return false;19222 19223 QualType To = E->getType()->castAs<ComplexType>()->getElementType();19224 QualType From19225 = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType();19226 19227 return HandleFloatToFloatCast(Info, E, From, To, Result.FloatReal) &&19228 HandleFloatToFloatCast(Info, E, From, To, Result.FloatImag);19229 }19230 19231 case CK_FloatingComplexToIntegralComplex: {19232 if (!Visit(E->getSubExpr()))19233 return false;19234 19235 QualType To = E->getType()->castAs<ComplexType>()->getElementType();19236 QualType From19237 = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType();19238 Result.makeComplexInt();19239 return HandleFloatToIntCast(Info, E, From, Result.FloatReal,19240 To, Result.IntReal) &&19241 HandleFloatToIntCast(Info, E, From, Result.FloatImag,19242 To, Result.IntImag);19243 }19244 19245 case CK_IntegralRealToComplex: {19246 APSInt &Real = Result.IntReal;19247 if (!EvaluateInteger(E->getSubExpr(), Real, Info))19248 return false;19249 19250 Result.makeComplexInt();19251 Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned());19252 return true;19253 }19254 19255 case CK_IntegralComplexCast: {19256 if (!Visit(E->getSubExpr()))19257 return false;19258 19259 QualType To = E->getType()->castAs<ComplexType>()->getElementType();19260 QualType From19261 = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType();19262 19263 Result.IntReal = HandleIntToIntCast(Info, E, To, From, Result.IntReal);19264 Result.IntImag = HandleIntToIntCast(Info, E, To, From, Result.IntImag);19265 return true;19266 }19267 19268 case CK_IntegralComplexToFloatingComplex: {19269 if (!Visit(E->getSubExpr()))19270 return false;19271 19272 const FPOptions FPO = E->getFPFeaturesInEffect(19273 Info.Ctx.getLangOpts());19274 QualType To = E->getType()->castAs<ComplexType>()->getElementType();19275 QualType From19276 = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType();19277 Result.makeComplexFloat();19278 return HandleIntToFloatCast(Info, E, FPO, From, Result.IntReal,19279 To, Result.FloatReal) &&19280 HandleIntToFloatCast(Info, E, FPO, From, Result.IntImag,19281 To, Result.FloatImag);19282 }19283 }19284 19285 llvm_unreachable("unknown cast resulting in complex value");19286}19287 19288void HandleComplexComplexMul(APFloat A, APFloat B, APFloat C, APFloat D,19289 APFloat &ResR, APFloat &ResI) {19290 // This is an implementation of complex multiplication according to the19291 // constraints laid out in C11 Annex G. The implementation uses the19292 // following naming scheme:19293 // (a + ib) * (c + id)19294 19295 APFloat AC = A * C;19296 APFloat BD = B * D;19297 APFloat AD = A * D;19298 APFloat BC = B * C;19299 ResR = AC - BD;19300 ResI = AD + BC;19301 if (ResR.isNaN() && ResI.isNaN()) {19302 bool Recalc = false;19303 if (A.isInfinity() || B.isInfinity()) {19304 A = APFloat::copySign(APFloat(A.getSemantics(), A.isInfinity() ? 1 : 0),19305 A);19306 B = APFloat::copySign(APFloat(B.getSemantics(), B.isInfinity() ? 1 : 0),19307 B);19308 if (C.isNaN())19309 C = APFloat::copySign(APFloat(C.getSemantics()), C);19310 if (D.isNaN())19311 D = APFloat::copySign(APFloat(D.getSemantics()), D);19312 Recalc = true;19313 }19314 if (C.isInfinity() || D.isInfinity()) {19315 C = APFloat::copySign(APFloat(C.getSemantics(), C.isInfinity() ? 1 : 0),19316 C);19317 D = APFloat::copySign(APFloat(D.getSemantics(), D.isInfinity() ? 1 : 0),19318 D);19319 if (A.isNaN())19320 A = APFloat::copySign(APFloat(A.getSemantics()), A);19321 if (B.isNaN())19322 B = APFloat::copySign(APFloat(B.getSemantics()), B);19323 Recalc = true;19324 }19325 if (!Recalc && (AC.isInfinity() || BD.isInfinity() || AD.isInfinity() ||19326 BC.isInfinity())) {19327 if (A.isNaN())19328 A = APFloat::copySign(APFloat(A.getSemantics()), A);19329 if (B.isNaN())19330 B = APFloat::copySign(APFloat(B.getSemantics()), B);19331 if (C.isNaN())19332 C = APFloat::copySign(APFloat(C.getSemantics()), C);19333 if (D.isNaN())19334 D = APFloat::copySign(APFloat(D.getSemantics()), D);19335 Recalc = true;19336 }19337 if (Recalc) {19338 ResR = APFloat::getInf(A.getSemantics()) * (A * C - B * D);19339 ResI = APFloat::getInf(A.getSemantics()) * (A * D + B * C);19340 }19341 }19342}19343 19344void HandleComplexComplexDiv(APFloat A, APFloat B, APFloat C, APFloat D,19345 APFloat &ResR, APFloat &ResI) {19346 // This is an implementation of complex division according to the19347 // constraints laid out in C11 Annex G. The implementation uses the19348 // following naming scheme:19349 // (a + ib) / (c + id)19350 19351 int DenomLogB = 0;19352 APFloat MaxCD = maxnum(abs(C), abs(D));19353 if (MaxCD.isFinite()) {19354 DenomLogB = ilogb(MaxCD);19355 C = scalbn(C, -DenomLogB, APFloat::rmNearestTiesToEven);19356 D = scalbn(D, -DenomLogB, APFloat::rmNearestTiesToEven);19357 }19358 APFloat Denom = C * C + D * D;19359 ResR =19360 scalbn((A * C + B * D) / Denom, -DenomLogB, APFloat::rmNearestTiesToEven);19361 ResI =19362 scalbn((B * C - A * D) / Denom, -DenomLogB, APFloat::rmNearestTiesToEven);19363 if (ResR.isNaN() && ResI.isNaN()) {19364 if (Denom.isPosZero() && (!A.isNaN() || !B.isNaN())) {19365 ResR = APFloat::getInf(ResR.getSemantics(), C.isNegative()) * A;19366 ResI = APFloat::getInf(ResR.getSemantics(), C.isNegative()) * B;19367 } else if ((A.isInfinity() || B.isInfinity()) && C.isFinite() &&19368 D.isFinite()) {19369 A = APFloat::copySign(APFloat(A.getSemantics(), A.isInfinity() ? 1 : 0),19370 A);19371 B = APFloat::copySign(APFloat(B.getSemantics(), B.isInfinity() ? 1 : 0),19372 B);19373 ResR = APFloat::getInf(ResR.getSemantics()) * (A * C + B * D);19374 ResI = APFloat::getInf(ResI.getSemantics()) * (B * C - A * D);19375 } else if (MaxCD.isInfinity() && A.isFinite() && B.isFinite()) {19376 C = APFloat::copySign(APFloat(C.getSemantics(), C.isInfinity() ? 1 : 0),19377 C);19378 D = APFloat::copySign(APFloat(D.getSemantics(), D.isInfinity() ? 1 : 0),19379 D);19380 ResR = APFloat::getZero(ResR.getSemantics()) * (A * C + B * D);19381 ResI = APFloat::getZero(ResI.getSemantics()) * (B * C - A * D);19382 }19383 }19384}19385 19386bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {19387 if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma)19388 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);19389 19390 // Track whether the LHS or RHS is real at the type system level. When this is19391 // the case we can simplify our evaluation strategy.19392 bool LHSReal = false, RHSReal = false;19393 19394 bool LHSOK;19395 if (E->getLHS()->getType()->isRealFloatingType()) {19396 LHSReal = true;19397 APFloat &Real = Result.FloatReal;19398 LHSOK = EvaluateFloat(E->getLHS(), Real, Info);19399 if (LHSOK) {19400 Result.makeComplexFloat();19401 Result.FloatImag = APFloat(Real.getSemantics());19402 }19403 } else {19404 LHSOK = Visit(E->getLHS());19405 }19406 if (!LHSOK && !Info.noteFailure())19407 return false;19408 19409 ComplexValue RHS;19410 if (E->getRHS()->getType()->isRealFloatingType()) {19411 RHSReal = true;19412 APFloat &Real = RHS.FloatReal;19413 if (!EvaluateFloat(E->getRHS(), Real, Info) || !LHSOK)19414 return false;19415 RHS.makeComplexFloat();19416 RHS.FloatImag = APFloat(Real.getSemantics());19417 } else if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK)19418 return false;19419 19420 assert(!(LHSReal && RHSReal) &&19421 "Cannot have both operands of a complex operation be real.");19422 switch (E->getOpcode()) {19423 default: return Error(E);19424 case BO_Add:19425 if (Result.isComplexFloat()) {19426 Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),19427 APFloat::rmNearestTiesToEven);19428 if (LHSReal)19429 Result.getComplexFloatImag() = RHS.getComplexFloatImag();19430 else if (!RHSReal)19431 Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),19432 APFloat::rmNearestTiesToEven);19433 } else {19434 Result.getComplexIntReal() += RHS.getComplexIntReal();19435 Result.getComplexIntImag() += RHS.getComplexIntImag();19436 }19437 break;19438 case BO_Sub:19439 if (Result.isComplexFloat()) {19440 Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),19441 APFloat::rmNearestTiesToEven);19442 if (LHSReal) {19443 Result.getComplexFloatImag() = RHS.getComplexFloatImag();19444 Result.getComplexFloatImag().changeSign();19445 } else if (!RHSReal) {19446 Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),19447 APFloat::rmNearestTiesToEven);19448 }19449 } else {19450 Result.getComplexIntReal() -= RHS.getComplexIntReal();19451 Result.getComplexIntImag() -= RHS.getComplexIntImag();19452 }19453 break;19454 case BO_Mul:19455 if (Result.isComplexFloat()) {19456 // This is an implementation of complex multiplication according to the19457 // constraints laid out in C11 Annex G. The implementation uses the19458 // following naming scheme:19459 // (a + ib) * (c + id)19460 ComplexValue LHS = Result;19461 APFloat &A = LHS.getComplexFloatReal();19462 APFloat &B = LHS.getComplexFloatImag();19463 APFloat &C = RHS.getComplexFloatReal();19464 APFloat &D = RHS.getComplexFloatImag();19465 APFloat &ResR = Result.getComplexFloatReal();19466 APFloat &ResI = Result.getComplexFloatImag();19467 if (LHSReal) {19468 assert(!RHSReal && "Cannot have two real operands for a complex op!");19469 ResR = A;19470 ResI = A;19471 // ResR = A * C;19472 // ResI = A * D;19473 if (!handleFloatFloatBinOp(Info, E, ResR, BO_Mul, C) ||19474 !handleFloatFloatBinOp(Info, E, ResI, BO_Mul, D))19475 return false;19476 } else if (RHSReal) {19477 // ResR = C * A;19478 // ResI = C * B;19479 ResR = C;19480 ResI = C;19481 if (!handleFloatFloatBinOp(Info, E, ResR, BO_Mul, A) ||19482 !handleFloatFloatBinOp(Info, E, ResI, BO_Mul, B))19483 return false;19484 } else {19485 HandleComplexComplexMul(A, B, C, D, ResR, ResI);19486 }19487 } else {19488 ComplexValue LHS = Result;19489 Result.getComplexIntReal() =19490 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -19491 LHS.getComplexIntImag() * RHS.getComplexIntImag());19492 Result.getComplexIntImag() =19493 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +19494 LHS.getComplexIntImag() * RHS.getComplexIntReal());19495 }19496 break;19497 case BO_Div:19498 if (Result.isComplexFloat()) {19499 // This is an implementation of complex division according to the19500 // constraints laid out in C11 Annex G. The implementation uses the19501 // following naming scheme:19502 // (a + ib) / (c + id)19503 ComplexValue LHS = Result;19504 APFloat &A = LHS.getComplexFloatReal();19505 APFloat &B = LHS.getComplexFloatImag();19506 APFloat &C = RHS.getComplexFloatReal();19507 APFloat &D = RHS.getComplexFloatImag();19508 APFloat &ResR = Result.getComplexFloatReal();19509 APFloat &ResI = Result.getComplexFloatImag();19510 if (RHSReal) {19511 ResR = A;19512 ResI = B;19513 // ResR = A / C;19514 // ResI = B / C;19515 if (!handleFloatFloatBinOp(Info, E, ResR, BO_Div, C) ||19516 !handleFloatFloatBinOp(Info, E, ResI, BO_Div, C))19517 return false;19518 } else {19519 if (LHSReal) {19520 // No real optimizations we can do here, stub out with zero.19521 B = APFloat::getZero(A.getSemantics());19522 }19523 HandleComplexComplexDiv(A, B, C, D, ResR, ResI);19524 }19525 } else {19526 ComplexValue LHS = Result;19527 APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() +19528 RHS.getComplexIntImag() * RHS.getComplexIntImag();19529 if (Den.isZero())19530 return Error(E, diag::note_expr_divide_by_zero);19531 19532 Result.getComplexIntReal() =19533 (LHS.getComplexIntReal() * RHS.getComplexIntReal() +19534 LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den;19535 Result.getComplexIntImag() =19536 (LHS.getComplexIntImag() * RHS.getComplexIntReal() -19537 LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den;19538 }19539 break;19540 }19541 19542 return true;19543}19544 19545bool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {19546 // Get the operand value into 'Result'.19547 if (!Visit(E->getSubExpr()))19548 return false;19549 19550 switch (E->getOpcode()) {19551 default:19552 return Error(E);19553 case UO_Extension:19554 return true;19555 case UO_Plus:19556 // The result is always just the subexpr.19557 return true;19558 case UO_Minus:19559 if (Result.isComplexFloat()) {19560 Result.getComplexFloatReal().changeSign();19561 Result.getComplexFloatImag().changeSign();19562 }19563 else {19564 Result.getComplexIntReal() = -Result.getComplexIntReal();19565 Result.getComplexIntImag() = -Result.getComplexIntImag();19566 }19567 return true;19568 case UO_Not:19569 if (Result.isComplexFloat())19570 Result.getComplexFloatImag().changeSign();19571 else19572 Result.getComplexIntImag() = -Result.getComplexIntImag();19573 return true;19574 }19575}19576 19577bool ComplexExprEvaluator::VisitInitListExpr(const InitListExpr *E) {19578 if (E->getNumInits() == 2) {19579 if (E->getType()->isComplexType()) {19580 Result.makeComplexFloat();19581 if (!EvaluateFloat(E->getInit(0), Result.FloatReal, Info))19582 return false;19583 if (!EvaluateFloat(E->getInit(1), Result.FloatImag, Info))19584 return false;19585 } else {19586 Result.makeComplexInt();19587 if (!EvaluateInteger(E->getInit(0), Result.IntReal, Info))19588 return false;19589 if (!EvaluateInteger(E->getInit(1), Result.IntImag, Info))19590 return false;19591 }19592 return true;19593 }19594 return ExprEvaluatorBaseTy::VisitInitListExpr(E);19595}19596 19597bool ComplexExprEvaluator::VisitCallExpr(const CallExpr *E) {19598 if (!IsConstantEvaluatedBuiltinCall(E))19599 return ExprEvaluatorBaseTy::VisitCallExpr(E);19600 19601 switch (E->getBuiltinCallee()) {19602 case Builtin::BI__builtin_complex:19603 Result.makeComplexFloat();19604 if (!EvaluateFloat(E->getArg(0), Result.FloatReal, Info))19605 return false;19606 if (!EvaluateFloat(E->getArg(1), Result.FloatImag, Info))19607 return false;19608 return true;19609 19610 default:19611 return false;19612 }19613}19614 19615//===----------------------------------------------------------------------===//19616// Atomic expression evaluation, essentially just handling the NonAtomicToAtomic19617// implicit conversion.19618//===----------------------------------------------------------------------===//19619 19620namespace {19621class AtomicExprEvaluator :19622 public ExprEvaluatorBase<AtomicExprEvaluator> {19623 const LValue *This;19624 APValue &Result;19625public:19626 AtomicExprEvaluator(EvalInfo &Info, const LValue *This, APValue &Result)19627 : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {}19628 19629 bool Success(const APValue &V, const Expr *E) {19630 Result = V;19631 return true;19632 }19633 19634 bool ZeroInitialization(const Expr *E) {19635 ImplicitValueInitExpr VIE(19636 E->getType()->castAs<AtomicType>()->getValueType());19637 // For atomic-qualified class (and array) types in C++, initialize the19638 // _Atomic-wrapped subobject directly, in-place.19639 return This ? EvaluateInPlace(Result, Info, *This, &VIE)19640 : Evaluate(Result, Info, &VIE);19641 }19642 19643 bool VisitCastExpr(const CastExpr *E) {19644 switch (E->getCastKind()) {19645 default:19646 return ExprEvaluatorBaseTy::VisitCastExpr(E);19647 case CK_NullToPointer:19648 VisitIgnoredValue(E->getSubExpr());19649 return ZeroInitialization(E);19650 case CK_NonAtomicToAtomic:19651 return This ? EvaluateInPlace(Result, Info, *This, E->getSubExpr())19652 : Evaluate(Result, Info, E->getSubExpr());19653 }19654 }19655};19656} // end anonymous namespace19657 19658static bool EvaluateAtomic(const Expr *E, const LValue *This, APValue &Result,19659 EvalInfo &Info) {19660 assert(!E->isValueDependent());19661 assert(E->isPRValue() && E->getType()->isAtomicType());19662 return AtomicExprEvaluator(Info, This, Result).Visit(E);19663}19664 19665//===----------------------------------------------------------------------===//19666// Void expression evaluation, primarily for a cast to void on the LHS of a19667// comma operator19668//===----------------------------------------------------------------------===//19669 19670namespace {19671class VoidExprEvaluator19672 : public ExprEvaluatorBase<VoidExprEvaluator> {19673public:19674 VoidExprEvaluator(EvalInfo &Info) : ExprEvaluatorBaseTy(Info) {}19675 19676 bool Success(const APValue &V, const Expr *e) { return true; }19677 19678 bool ZeroInitialization(const Expr *E) { return true; }19679 19680 bool VisitCastExpr(const CastExpr *E) {19681 switch (E->getCastKind()) {19682 default:19683 return ExprEvaluatorBaseTy::VisitCastExpr(E);19684 case CK_ToVoid:19685 VisitIgnoredValue(E->getSubExpr());19686 return true;19687 }19688 }19689 19690 bool VisitCallExpr(const CallExpr *E) {19691 if (!IsConstantEvaluatedBuiltinCall(E))19692 return ExprEvaluatorBaseTy::VisitCallExpr(E);19693 19694 switch (E->getBuiltinCallee()) {19695 case Builtin::BI__assume:19696 case Builtin::BI__builtin_assume:19697 // The argument is not evaluated!19698 return true;19699 19700 case Builtin::BI__builtin_operator_delete:19701 return HandleOperatorDeleteCall(Info, E);19702 19703 default:19704 return false;19705 }19706 }19707 19708 bool VisitCXXDeleteExpr(const CXXDeleteExpr *E);19709};19710} // end anonymous namespace19711 19712bool VoidExprEvaluator::VisitCXXDeleteExpr(const CXXDeleteExpr *E) {19713 // We cannot speculatively evaluate a delete expression.19714 if (Info.SpeculativeEvaluationDepth)19715 return false;19716 19717 FunctionDecl *OperatorDelete = E->getOperatorDelete();19718 if (!OperatorDelete19719 ->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) {19720 Info.FFDiag(E, diag::note_constexpr_new_non_replaceable)19721 << isa<CXXMethodDecl>(OperatorDelete) << OperatorDelete;19722 return false;19723 }19724 19725 const Expr *Arg = E->getArgument();19726 19727 LValue Pointer;19728 if (!EvaluatePointer(Arg, Pointer, Info))19729 return false;19730 if (Pointer.Designator.Invalid)19731 return false;19732 19733 // Deleting a null pointer has no effect.19734 if (Pointer.isNullPointer()) {19735 // This is the only case where we need to produce an extension warning:19736 // the only other way we can succeed is if we find a dynamic allocation,19737 // and we will have warned when we allocated it in that case.19738 if (!Info.getLangOpts().CPlusPlus20)19739 Info.CCEDiag(E, diag::note_constexpr_new);19740 return true;19741 }19742 19743 std::optional<DynAlloc *> Alloc = CheckDeleteKind(19744 Info, E, Pointer, E->isArrayForm() ? DynAlloc::ArrayNew : DynAlloc::New);19745 if (!Alloc)19746 return false;19747 QualType AllocType = Pointer.Base.getDynamicAllocType();19748 19749 // For the non-array case, the designator must be empty if the static type19750 // does not have a virtual destructor.19751 if (!E->isArrayForm() && Pointer.Designator.Entries.size() != 0 &&19752 !hasVirtualDestructor(Arg->getType()->getPointeeType())) {19753 Info.FFDiag(E, diag::note_constexpr_delete_base_nonvirt_dtor)19754 << Arg->getType()->getPointeeType() << AllocType;19755 return false;19756 }19757 19758 // For a class type with a virtual destructor, the selected operator delete19759 // is the one looked up when building the destructor.19760 if (!E->isArrayForm() && !E->isGlobalDelete()) {19761 const FunctionDecl *VirtualDelete = getVirtualOperatorDelete(AllocType);19762 if (VirtualDelete &&19763 !VirtualDelete19764 ->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) {19765 Info.FFDiag(E, diag::note_constexpr_new_non_replaceable)19766 << isa<CXXMethodDecl>(VirtualDelete) << VirtualDelete;19767 return false;19768 }19769 }19770 19771 if (!HandleDestruction(Info, E->getExprLoc(), Pointer.getLValueBase(),19772 (*Alloc)->Value, AllocType))19773 return false;19774 19775 if (!Info.HeapAllocs.erase(Pointer.Base.dyn_cast<DynamicAllocLValue>())) {19776 // The element was already erased. This means the destructor call also19777 // deleted the object.19778 // FIXME: This probably results in undefined behavior before we get this19779 // far, and should be diagnosed elsewhere first.19780 Info.FFDiag(E, diag::note_constexpr_double_delete);19781 return false;19782 }19783 19784 return true;19785}19786 19787static bool EvaluateVoid(const Expr *E, EvalInfo &Info) {19788 assert(!E->isValueDependent());19789 assert(E->isPRValue() && E->getType()->isVoidType());19790 return VoidExprEvaluator(Info).Visit(E);19791}19792 19793//===----------------------------------------------------------------------===//19794// Top level Expr::EvaluateAsRValue method.19795//===----------------------------------------------------------------------===//19796 19797static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E) {19798 assert(!E->isValueDependent());19799 // In C, function designators are not lvalues, but we evaluate them as if they19800 // are.19801 QualType T = E->getType();19802 if (E->isGLValue() || T->isFunctionType()) {19803 LValue LV;19804 if (!EvaluateLValue(E, LV, Info))19805 return false;19806 LV.moveInto(Result);19807 } else if (T->isVectorType()) {19808 if (!EvaluateVector(E, Result, Info))19809 return false;19810 } else if (T->isIntegralOrEnumerationType()) {19811 if (!IntExprEvaluator(Info, Result).Visit(E))19812 return false;19813 } else if (T->hasPointerRepresentation()) {19814 LValue LV;19815 if (!EvaluatePointer(E, LV, Info))19816 return false;19817 LV.moveInto(Result);19818 } else if (T->isRealFloatingType()) {19819 llvm::APFloat F(0.0);19820 if (!EvaluateFloat(E, F, Info))19821 return false;19822 Result = APValue(F);19823 } else if (T->isAnyComplexType()) {19824 ComplexValue C;19825 if (!EvaluateComplex(E, C, Info))19826 return false;19827 C.moveInto(Result);19828 } else if (T->isFixedPointType()) {19829 if (!FixedPointExprEvaluator(Info, Result).Visit(E)) return false;19830 } else if (T->isMemberPointerType()) {19831 MemberPtr P;19832 if (!EvaluateMemberPointer(E, P, Info))19833 return false;19834 P.moveInto(Result);19835 return true;19836 } else if (T->isArrayType()) {19837 LValue LV;19838 APValue &Value =19839 Info.CurrentCall->createTemporary(E, T, ScopeKind::FullExpression, LV);19840 if (!EvaluateArray(E, LV, Value, Info))19841 return false;19842 Result = Value;19843 } else if (T->isRecordType()) {19844 LValue LV;19845 APValue &Value =19846 Info.CurrentCall->createTemporary(E, T, ScopeKind::FullExpression, LV);19847 if (!EvaluateRecord(E, LV, Value, Info))19848 return false;19849 Result = Value;19850 } else if (T->isVoidType()) {19851 if (!Info.getLangOpts().CPlusPlus11)19852 Info.CCEDiag(E, diag::note_constexpr_nonliteral)19853 << E->getType();19854 if (!EvaluateVoid(E, Info))19855 return false;19856 } else if (T->isAtomicType()) {19857 QualType Unqual = T.getAtomicUnqualifiedType();19858 if (Unqual->isArrayType() || Unqual->isRecordType()) {19859 LValue LV;19860 APValue &Value = Info.CurrentCall->createTemporary(19861 E, Unqual, ScopeKind::FullExpression, LV);19862 if (!EvaluateAtomic(E, &LV, Value, Info))19863 return false;19864 Result = Value;19865 } else {19866 if (!EvaluateAtomic(E, nullptr, Result, Info))19867 return false;19868 }19869 } else if (Info.getLangOpts().CPlusPlus11) {19870 Info.FFDiag(E, diag::note_constexpr_nonliteral) << E->getType();19871 return false;19872 } else {19873 Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr);19874 return false;19875 }19876 19877 return true;19878}19879 19880/// EvaluateInPlace - Evaluate an expression in-place in an APValue. In some19881/// cases, the in-place evaluation is essential, since later initializers for19882/// an object can indirectly refer to subobjects which were initialized earlier.19883static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, const LValue &This,19884 const Expr *E, bool AllowNonLiteralTypes) {19885 assert(!E->isValueDependent());19886 19887 // Normally expressions passed to EvaluateInPlace have a type, but not when19888 // a VarDecl initializer is evaluated before the untyped ParenListExpr is19889 // replaced with a CXXConstructExpr. This can happen in LLDB.19890 if (E->getType().isNull())19891 return false;19892 19893 if (!AllowNonLiteralTypes && !CheckLiteralType(Info, E, &This))19894 return false;19895 19896 if (E->isPRValue()) {19897 // Evaluate arrays and record types in-place, so that later initializers can19898 // refer to earlier-initialized members of the object.19899 QualType T = E->getType();19900 if (T->isArrayType())19901 return EvaluateArray(E, This, Result, Info);19902 else if (T->isRecordType())19903 return EvaluateRecord(E, This, Result, Info);19904 else if (T->isAtomicType()) {19905 QualType Unqual = T.getAtomicUnqualifiedType();19906 if (Unqual->isArrayType() || Unqual->isRecordType())19907 return EvaluateAtomic(E, &This, Result, Info);19908 }19909 }19910 19911 // For any other type, in-place evaluation is unimportant.19912 return Evaluate(Result, Info, E);19913}19914 19915/// EvaluateAsRValue - Try to evaluate this expression, performing an implicit19916/// lvalue-to-rvalue cast if it is an lvalue.19917static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result) {19918 assert(!E->isValueDependent());19919 19920 if (E->getType().isNull())19921 return false;19922 19923 if (!CheckLiteralType(Info, E))19924 return false;19925 19926 if (Info.EnableNewConstInterp) {19927 if (!Info.Ctx.getInterpContext().evaluateAsRValue(Info, E, Result))19928 return false;19929 return CheckConstantExpression(Info, E->getExprLoc(), E->getType(), Result,19930 ConstantExprKind::Normal);19931 }19932 19933 if (!::Evaluate(Result, Info, E))19934 return false;19935 19936 // Implicit lvalue-to-rvalue cast.19937 if (E->isGLValue()) {19938 LValue LV;19939 LV.setFrom(Info.Ctx, Result);19940 if (!handleLValueToRValueConversion(Info, E, E->getType(), LV, Result))19941 return false;19942 }19943 19944 // Check this core constant expression is a constant expression.19945 return CheckConstantExpression(Info, E->getExprLoc(), E->getType(), Result,19946 ConstantExprKind::Normal) &&19947 CheckMemoryLeaks(Info);19948}19949 19950static bool FastEvaluateAsRValue(const Expr *Exp, APValue &Result,19951 const ASTContext &Ctx, bool &IsConst) {19952 // Fast-path evaluations of integer literals, since we sometimes see files19953 // containing vast quantities of these.19954 if (const auto *L = dyn_cast<IntegerLiteral>(Exp)) {19955 Result =19956 APValue(APSInt(L->getValue(), L->getType()->isUnsignedIntegerType()));19957 IsConst = true;19958 return true;19959 }19960 19961 if (const auto *L = dyn_cast<CXXBoolLiteralExpr>(Exp)) {19962 Result = APValue(APSInt(APInt(1, L->getValue())));19963 IsConst = true;19964 return true;19965 }19966 19967 if (const auto *FL = dyn_cast<FloatingLiteral>(Exp)) {19968 Result = APValue(FL->getValue());19969 IsConst = true;19970 return true;19971 }19972 19973 if (const auto *L = dyn_cast<CharacterLiteral>(Exp)) {19974 Result = APValue(Ctx.MakeIntValue(L->getValue(), L->getType()));19975 IsConst = true;19976 return true;19977 }19978 19979 if (const auto *CE = dyn_cast<ConstantExpr>(Exp)) {19980 if (CE->hasAPValueResult()) {19981 APValue APV = CE->getAPValueResult();19982 if (!APV.isLValue()) {19983 Result = std::move(APV);19984 IsConst = true;19985 return true;19986 }19987 }19988 19989 // The SubExpr is usually just an IntegerLiteral.19990 return FastEvaluateAsRValue(CE->getSubExpr(), Result, Ctx, IsConst);19991 }19992 19993 // This case should be rare, but we need to check it before we check on19994 // the type below.19995 if (Exp->getType().isNull()) {19996 IsConst = false;19997 return true;19998 }19999 20000 return false;20001}20002 20003static bool hasUnacceptableSideEffect(Expr::EvalStatus &Result,20004 Expr::SideEffectsKind SEK) {20005 return (SEK < Expr::SE_AllowSideEffects && Result.HasSideEffects) ||20006 (SEK < Expr::SE_AllowUndefinedBehavior && Result.HasUndefinedBehavior);20007}20008 20009static bool EvaluateAsRValue(const Expr *E, Expr::EvalResult &Result,20010 const ASTContext &Ctx, EvalInfo &Info) {20011 assert(!E->isValueDependent());20012 bool IsConst;20013 if (FastEvaluateAsRValue(E, Result.Val, Ctx, IsConst))20014 return IsConst;20015 20016 return EvaluateAsRValue(Info, E, Result.Val);20017}20018 20019static bool EvaluateAsInt(const Expr *E, Expr::EvalResult &ExprResult,20020 const ASTContext &Ctx,20021 Expr::SideEffectsKind AllowSideEffects,20022 EvalInfo &Info) {20023 assert(!E->isValueDependent());20024 if (!E->getType()->isIntegralOrEnumerationType())20025 return false;20026 20027 if (!::EvaluateAsRValue(E, ExprResult, Ctx, Info) ||20028 !ExprResult.Val.isInt() ||20029 hasUnacceptableSideEffect(ExprResult, AllowSideEffects))20030 return false;20031 20032 return true;20033}20034 20035static bool EvaluateAsFixedPoint(const Expr *E, Expr::EvalResult &ExprResult,20036 const ASTContext &Ctx,20037 Expr::SideEffectsKind AllowSideEffects,20038 EvalInfo &Info) {20039 assert(!E->isValueDependent());20040 if (!E->getType()->isFixedPointType())20041 return false;20042 20043 if (!::EvaluateAsRValue(E, ExprResult, Ctx, Info))20044 return false;20045 20046 if (!ExprResult.Val.isFixedPoint() ||20047 hasUnacceptableSideEffect(ExprResult, AllowSideEffects))20048 return false;20049 20050 return true;20051}20052 20053/// EvaluateAsRValue - Return true if this is a constant which we can fold using20054/// any crazy technique (that has nothing to do with language standards) that20055/// we want to. If this function returns true, it returns the folded constant20056/// in Result. If this expression is a glvalue, an lvalue-to-rvalue conversion20057/// will be applied to the result.20058bool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx,20059 bool InConstantContext) const {20060 assert(!isValueDependent() &&20061 "Expression evaluator can't be called on a dependent expression.");20062 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsRValue");20063 EvalInfo Info(Ctx, Result, EvaluationMode::IgnoreSideEffects);20064 Info.InConstantContext = InConstantContext;20065 return ::EvaluateAsRValue(this, Result, Ctx, Info);20066}20067 20068bool Expr::EvaluateAsBooleanCondition(bool &Result, const ASTContext &Ctx,20069 bool InConstantContext) const {20070 assert(!isValueDependent() &&20071 "Expression evaluator can't be called on a dependent expression.");20072 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsBooleanCondition");20073 EvalResult Scratch;20074 return EvaluateAsRValue(Scratch, Ctx, InConstantContext) &&20075 HandleConversionToBool(Scratch.Val, Result);20076}20077 20078bool Expr::EvaluateAsInt(EvalResult &Result, const ASTContext &Ctx,20079 SideEffectsKind AllowSideEffects,20080 bool InConstantContext) const {20081 assert(!isValueDependent() &&20082 "Expression evaluator can't be called on a dependent expression.");20083 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsInt");20084 EvalInfo Info(Ctx, Result, EvaluationMode::IgnoreSideEffects);20085 Info.InConstantContext = InConstantContext;20086 return ::EvaluateAsInt(this, Result, Ctx, AllowSideEffects, Info);20087}20088 20089bool Expr::EvaluateAsFixedPoint(EvalResult &Result, const ASTContext &Ctx,20090 SideEffectsKind AllowSideEffects,20091 bool InConstantContext) const {20092 assert(!isValueDependent() &&20093 "Expression evaluator can't be called on a dependent expression.");20094 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsFixedPoint");20095 EvalInfo Info(Ctx, Result, EvaluationMode::IgnoreSideEffects);20096 Info.InConstantContext = InConstantContext;20097 return ::EvaluateAsFixedPoint(this, Result, Ctx, AllowSideEffects, Info);20098}20099 20100bool Expr::EvaluateAsFloat(APFloat &Result, const ASTContext &Ctx,20101 SideEffectsKind AllowSideEffects,20102 bool InConstantContext) const {20103 assert(!isValueDependent() &&20104 "Expression evaluator can't be called on a dependent expression.");20105 20106 if (!getType()->isRealFloatingType())20107 return false;20108 20109 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsFloat");20110 EvalResult ExprResult;20111 if (!EvaluateAsRValue(ExprResult, Ctx, InConstantContext) ||20112 !ExprResult.Val.isFloat() ||20113 hasUnacceptableSideEffect(ExprResult, AllowSideEffects))20114 return false;20115 20116 Result = ExprResult.Val.getFloat();20117 return true;20118}20119 20120bool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx,20121 bool InConstantContext) const {20122 assert(!isValueDependent() &&20123 "Expression evaluator can't be called on a dependent expression.");20124 20125 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsLValue");20126 EvalInfo Info(Ctx, Result, EvaluationMode::ConstantFold);20127 Info.InConstantContext = InConstantContext;20128 LValue LV;20129 CheckedTemporaries CheckedTemps;20130 20131 if (Info.EnableNewConstInterp) {20132 if (!Info.Ctx.getInterpContext().evaluate(Info, this, Result.Val,20133 ConstantExprKind::Normal))20134 return false;20135 20136 LV.setFrom(Ctx, Result.Val);20137 return CheckLValueConstantExpression(20138 Info, getExprLoc(), Ctx.getLValueReferenceType(getType()), LV,20139 ConstantExprKind::Normal, CheckedTemps);20140 }20141 20142 if (!EvaluateLValue(this, LV, Info) || !Info.discardCleanups() ||20143 Result.HasSideEffects ||20144 !CheckLValueConstantExpression(Info, getExprLoc(),20145 Ctx.getLValueReferenceType(getType()), LV,20146 ConstantExprKind::Normal, CheckedTemps))20147 return false;20148 20149 LV.moveInto(Result.Val);20150 return true;20151}20152 20153static bool EvaluateDestruction(const ASTContext &Ctx, APValue::LValueBase Base,20154 APValue DestroyedValue, QualType Type,20155 SourceLocation Loc, Expr::EvalStatus &EStatus,20156 bool IsConstantDestruction) {20157 EvalInfo Info(Ctx, EStatus,20158 IsConstantDestruction ? EvaluationMode::ConstantExpression20159 : EvaluationMode::ConstantFold);20160 Info.setEvaluatingDecl(Base, DestroyedValue,20161 EvalInfo::EvaluatingDeclKind::Dtor);20162 Info.InConstantContext = IsConstantDestruction;20163 20164 LValue LVal;20165 LVal.set(Base);20166 20167 if (!HandleDestruction(Info, Loc, Base, DestroyedValue, Type) ||20168 EStatus.HasSideEffects)20169 return false;20170 20171 if (!Info.discardCleanups())20172 llvm_unreachable("Unhandled cleanup; missing full expression marker?");20173 20174 return true;20175}20176 20177bool Expr::EvaluateAsConstantExpr(EvalResult &Result, const ASTContext &Ctx,20178 ConstantExprKind Kind) const {20179 assert(!isValueDependent() &&20180 "Expression evaluator can't be called on a dependent expression.");20181 bool IsConst;20182 if (FastEvaluateAsRValue(this, Result.Val, Ctx, IsConst) &&20183 Result.Val.hasValue())20184 return true;20185 20186 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsConstantExpr");20187 EvaluationMode EM = EvaluationMode::ConstantExpression;20188 EvalInfo Info(Ctx, Result, EM);20189 Info.InConstantContext = true;20190 20191 if (Info.EnableNewConstInterp) {20192 if (!Info.Ctx.getInterpContext().evaluate(Info, this, Result.Val, Kind))20193 return false;20194 return CheckConstantExpression(Info, getExprLoc(),20195 getStorageType(Ctx, this), Result.Val, Kind);20196 }20197 20198 // The type of the object we're initializing is 'const T' for a class NTTP.20199 QualType T = getType();20200 if (Kind == ConstantExprKind::ClassTemplateArgument)20201 T.addConst();20202 20203 // If we're evaluating a prvalue, fake up a MaterializeTemporaryExpr to20204 // represent the result of the evaluation. CheckConstantExpression ensures20205 // this doesn't escape.20206 MaterializeTemporaryExpr BaseMTE(T, const_cast<Expr*>(this), true);20207 APValue::LValueBase Base(&BaseMTE);20208 Info.setEvaluatingDecl(Base, Result.Val);20209 20210 LValue LVal;20211 LVal.set(Base);20212 // C++23 [intro.execution]/p520213 // A full-expression is [...] a constant-expression20214 // So we need to make sure temporary objects are destroyed after having20215 // evaluating the expression (per C++23 [class.temporary]/p4).20216 FullExpressionRAII Scope(Info);20217 if (!::EvaluateInPlace(Result.Val, Info, LVal, this) ||20218 Result.HasSideEffects || !Scope.destroy())20219 return false;20220 20221 if (!Info.discardCleanups())20222 llvm_unreachable("Unhandled cleanup; missing full expression marker?");20223 20224 if (!CheckConstantExpression(Info, getExprLoc(), getStorageType(Ctx, this),20225 Result.Val, Kind))20226 return false;20227 if (!CheckMemoryLeaks(Info))20228 return false;20229 20230 // If this is a class template argument, it's required to have constant20231 // destruction too.20232 if (Kind == ConstantExprKind::ClassTemplateArgument &&20233 (!EvaluateDestruction(Ctx, Base, Result.Val, T, getBeginLoc(), Result,20234 true) ||20235 Result.HasSideEffects)) {20236 // FIXME: Prefix a note to indicate that the problem is lack of constant20237 // destruction.20238 return false;20239 }20240 20241 return true;20242}20243 20244bool Expr::EvaluateAsInitializer(APValue &Value, const ASTContext &Ctx,20245 const VarDecl *VD,20246 SmallVectorImpl<PartialDiagnosticAt> &Notes,20247 bool IsConstantInitialization) const {20248 assert(!isValueDependent() &&20249 "Expression evaluator can't be called on a dependent expression.");20250 assert(VD && "Need a valid VarDecl");20251 20252 llvm::TimeTraceScope TimeScope("EvaluateAsInitializer", [&] {20253 std::string Name;20254 llvm::raw_string_ostream OS(Name);20255 VD->printQualifiedName(OS);20256 return Name;20257 });20258 20259 Expr::EvalStatus EStatus;20260 EStatus.Diag = &Notes;20261 20262 EvalInfo Info(Ctx, EStatus,20263 (IsConstantInitialization &&20264 (Ctx.getLangOpts().CPlusPlus || Ctx.getLangOpts().C23))20265 ? EvaluationMode::ConstantExpression20266 : EvaluationMode::ConstantFold);20267 Info.setEvaluatingDecl(VD, Value);20268 Info.InConstantContext = IsConstantInitialization;20269 20270 SourceLocation DeclLoc = VD->getLocation();20271 QualType DeclTy = VD->getType();20272 20273 if (Info.EnableNewConstInterp) {20274 auto &InterpCtx = const_cast<ASTContext &>(Ctx).getInterpContext();20275 if (!InterpCtx.evaluateAsInitializer(Info, VD, this, Value))20276 return false;20277 20278 return CheckConstantExpression(Info, DeclLoc, DeclTy, Value,20279 ConstantExprKind::Normal);20280 } else {20281 LValue LVal;20282 LVal.set(VD);20283 20284 {20285 // C++23 [intro.execution]/p520286 // A full-expression is ... an init-declarator ([dcl.decl]) or a20287 // mem-initializer.20288 // So we need to make sure temporary objects are destroyed after having20289 // evaluated the expression (per C++23 [class.temporary]/p4).20290 //20291 // FIXME: Otherwise this may break test/Modules/pr68702.cpp because the20292 // serialization code calls ParmVarDecl::getDefaultArg() which strips the20293 // outermost FullExpr, such as ExprWithCleanups.20294 FullExpressionRAII Scope(Info);20295 if (!EvaluateInPlace(Value, Info, LVal, this,20296 /*AllowNonLiteralTypes=*/true) ||20297 EStatus.HasSideEffects)20298 return false;20299 }20300 20301 // At this point, any lifetime-extended temporaries are completely20302 // initialized.20303 Info.performLifetimeExtension();20304 20305 if (!Info.discardCleanups())20306 llvm_unreachable("Unhandled cleanup; missing full expression marker?");20307 }20308 20309 return CheckConstantExpression(Info, DeclLoc, DeclTy, Value,20310 ConstantExprKind::Normal) &&20311 CheckMemoryLeaks(Info);20312}20313 20314bool VarDecl::evaluateDestruction(20315 SmallVectorImpl<PartialDiagnosticAt> &Notes) const {20316 Expr::EvalStatus EStatus;20317 EStatus.Diag = &Notes;20318 20319 // Only treat the destruction as constant destruction if we formally have20320 // constant initialization (or are usable in a constant expression).20321 bool IsConstantDestruction = hasConstantInitialization();20322 20323 // Make a copy of the value for the destructor to mutate, if we know it.20324 // Otherwise, treat the value as default-initialized; if the destructor works20325 // anyway, then the destruction is constant (and must be essentially empty).20326 APValue DestroyedValue;20327 if (getEvaluatedValue() && !getEvaluatedValue()->isAbsent())20328 DestroyedValue = *getEvaluatedValue();20329 else if (!handleDefaultInitValue(getType(), DestroyedValue))20330 return false;20331 20332 if (!EvaluateDestruction(getASTContext(), this, std::move(DestroyedValue),20333 getType(), getLocation(), EStatus,20334 IsConstantDestruction) ||20335 EStatus.HasSideEffects)20336 return false;20337 20338 ensureEvaluatedStmt()->HasConstantDestruction = true;20339 return true;20340}20341 20342/// isEvaluatable - Call EvaluateAsRValue to see if this expression can be20343/// constant folded, but discard the result.20344bool Expr::isEvaluatable(const ASTContext &Ctx, SideEffectsKind SEK) const {20345 assert(!isValueDependent() &&20346 "Expression evaluator can't be called on a dependent expression.");20347 20348 EvalResult Result;20349 return EvaluateAsRValue(Result, Ctx, /* in constant context */ true) &&20350 !hasUnacceptableSideEffect(Result, SEK);20351}20352 20353APSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx) const {20354 assert(!isValueDependent() &&20355 "Expression evaluator can't be called on a dependent expression.");20356 20357 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateKnownConstInt");20358 EvalResult EVResult;20359 EvalInfo Info(Ctx, EVResult, EvaluationMode::IgnoreSideEffects);20360 Info.InConstantContext = true;20361 20362 bool Result = ::EvaluateAsRValue(this, EVResult, Ctx, Info);20363 (void)Result;20364 assert(Result && "Could not evaluate expression");20365 assert(EVResult.Val.isInt() && "Expression did not evaluate to integer");20366 20367 return EVResult.Val.getInt();20368}20369 20370APSInt Expr::EvaluateKnownConstIntCheckOverflow(20371 const ASTContext &Ctx, SmallVectorImpl<PartialDiagnosticAt> *Diag) const {20372 assert(!isValueDependent() &&20373 "Expression evaluator can't be called on a dependent expression.");20374 20375 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateKnownConstIntCheckOverflow");20376 EvalResult EVResult;20377 EVResult.Diag = Diag;20378 EvalInfo Info(Ctx, EVResult, EvaluationMode::IgnoreSideEffects);20379 Info.InConstantContext = true;20380 Info.CheckingForUndefinedBehavior = true;20381 20382 bool Result = ::EvaluateAsRValue(Info, this, EVResult.Val);20383 (void)Result;20384 assert(Result && "Could not evaluate expression");20385 assert(EVResult.Val.isInt() && "Expression did not evaluate to integer");20386 20387 return EVResult.Val.getInt();20388}20389 20390void Expr::EvaluateForOverflow(const ASTContext &Ctx) const {20391 assert(!isValueDependent() &&20392 "Expression evaluator can't be called on a dependent expression.");20393 20394 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateForOverflow");20395 bool IsConst;20396 EvalResult EVResult;20397 if (!FastEvaluateAsRValue(this, EVResult.Val, Ctx, IsConst)) {20398 EvalInfo Info(Ctx, EVResult, EvaluationMode::IgnoreSideEffects);20399 Info.CheckingForUndefinedBehavior = true;20400 (void)::EvaluateAsRValue(Info, this, EVResult.Val);20401 }20402}20403 20404bool Expr::EvalResult::isGlobalLValue() const {20405 assert(Val.isLValue());20406 return IsGlobalLValue(Val.getLValueBase());20407}20408 20409/// isIntegerConstantExpr - this recursive routine will test if an expression is20410/// an integer constant expression.20411 20412/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,20413/// comma, etc20414 20415// CheckICE - This function does the fundamental ICE checking: the returned20416// ICEDiag contains an ICEKind indicating whether the expression is an ICE.20417//20418// Note that to reduce code duplication, this helper does no evaluation20419// itself; the caller checks whether the expression is evaluatable, and20420// in the rare cases where CheckICE actually cares about the evaluated20421// value, it calls into Evaluate.20422 20423namespace {20424 20425enum ICEKind {20426 /// This expression is an ICE.20427 IK_ICE,20428 /// This expression is not an ICE, but if it isn't evaluated, it's20429 /// a legal subexpression for an ICE. This return value is used to handle20430 /// the comma operator in C99 mode, and non-constant subexpressions.20431 IK_ICEIfUnevaluated,20432 /// This expression is not an ICE, and is not a legal subexpression for one.20433 IK_NotICE20434};20435 20436struct ICEDiag {20437 ICEKind Kind;20438 SourceLocation Loc;20439 20440 ICEDiag(ICEKind IK, SourceLocation l) : Kind(IK), Loc(l) {}20441};20442 20443}20444 20445static ICEDiag NoDiag() { return ICEDiag(IK_ICE, SourceLocation()); }20446 20447static ICEDiag Worst(ICEDiag A, ICEDiag B) { return A.Kind >= B.Kind ? A : B; }20448 20449static ICEDiag CheckEvalInICE(const Expr* E, const ASTContext &Ctx) {20450 Expr::EvalResult EVResult;20451 Expr::EvalStatus Status;20452 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantExpression);20453 20454 Info.InConstantContext = true;20455 if (!::EvaluateAsRValue(E, EVResult, Ctx, Info) || EVResult.HasSideEffects ||20456 !EVResult.Val.isInt())20457 return ICEDiag(IK_NotICE, E->getBeginLoc());20458 20459 return NoDiag();20460}20461 20462static ICEDiag CheckICE(const Expr* E, const ASTContext &Ctx) {20463 assert(!E->isValueDependent() && "Should not see value dependent exprs!");20464 if (!E->getType()->isIntegralOrEnumerationType())20465 return ICEDiag(IK_NotICE, E->getBeginLoc());20466 20467 switch (E->getStmtClass()) {20468#define ABSTRACT_STMT(Node)20469#define STMT(Node, Base) case Expr::Node##Class:20470#define EXPR(Node, Base)20471#include "clang/AST/StmtNodes.inc"20472 case Expr::PredefinedExprClass:20473 case Expr::FloatingLiteralClass:20474 case Expr::ImaginaryLiteralClass:20475 case Expr::StringLiteralClass:20476 case Expr::ArraySubscriptExprClass:20477 case Expr::MatrixSubscriptExprClass:20478 case Expr::ArraySectionExprClass:20479 case Expr::OMPArrayShapingExprClass:20480 case Expr::OMPIteratorExprClass:20481 case Expr::MemberExprClass:20482 case Expr::CompoundAssignOperatorClass:20483 case Expr::CompoundLiteralExprClass:20484 case Expr::ExtVectorElementExprClass:20485 case Expr::DesignatedInitExprClass:20486 case Expr::ArrayInitLoopExprClass:20487 case Expr::ArrayInitIndexExprClass:20488 case Expr::NoInitExprClass:20489 case Expr::DesignatedInitUpdateExprClass:20490 case Expr::ImplicitValueInitExprClass:20491 case Expr::ParenListExprClass:20492 case Expr::VAArgExprClass:20493 case Expr::AddrLabelExprClass:20494 case Expr::StmtExprClass:20495 case Expr::CXXMemberCallExprClass:20496 case Expr::CUDAKernelCallExprClass:20497 case Expr::CXXAddrspaceCastExprClass:20498 case Expr::CXXDynamicCastExprClass:20499 case Expr::CXXTypeidExprClass:20500 case Expr::CXXUuidofExprClass:20501 case Expr::MSPropertyRefExprClass:20502 case Expr::MSPropertySubscriptExprClass:20503 case Expr::CXXNullPtrLiteralExprClass:20504 case Expr::UserDefinedLiteralClass:20505 case Expr::CXXThisExprClass:20506 case Expr::CXXThrowExprClass:20507 case Expr::CXXNewExprClass:20508 case Expr::CXXDeleteExprClass:20509 case Expr::CXXPseudoDestructorExprClass:20510 case Expr::UnresolvedLookupExprClass:20511 case Expr::RecoveryExprClass:20512 case Expr::DependentScopeDeclRefExprClass:20513 case Expr::CXXConstructExprClass:20514 case Expr::CXXInheritedCtorInitExprClass:20515 case Expr::CXXStdInitializerListExprClass:20516 case Expr::CXXBindTemporaryExprClass:20517 case Expr::ExprWithCleanupsClass:20518 case Expr::CXXTemporaryObjectExprClass:20519 case Expr::CXXUnresolvedConstructExprClass:20520 case Expr::CXXDependentScopeMemberExprClass:20521 case Expr::UnresolvedMemberExprClass:20522 case Expr::ObjCStringLiteralClass:20523 case Expr::ObjCBoxedExprClass:20524 case Expr::ObjCArrayLiteralClass:20525 case Expr::ObjCDictionaryLiteralClass:20526 case Expr::ObjCEncodeExprClass:20527 case Expr::ObjCMessageExprClass:20528 case Expr::ObjCSelectorExprClass:20529 case Expr::ObjCProtocolExprClass:20530 case Expr::ObjCIvarRefExprClass:20531 case Expr::ObjCPropertyRefExprClass:20532 case Expr::ObjCSubscriptRefExprClass:20533 case Expr::ObjCIsaExprClass:20534 case Expr::ObjCAvailabilityCheckExprClass:20535 case Expr::ShuffleVectorExprClass:20536 case Expr::ConvertVectorExprClass:20537 case Expr::BlockExprClass:20538 case Expr::NoStmtClass:20539 case Expr::OpaqueValueExprClass:20540 case Expr::PackExpansionExprClass:20541 case Expr::SubstNonTypeTemplateParmPackExprClass:20542 case Expr::FunctionParmPackExprClass:20543 case Expr::AsTypeExprClass:20544 case Expr::ObjCIndirectCopyRestoreExprClass:20545 case Expr::MaterializeTemporaryExprClass:20546 case Expr::PseudoObjectExprClass:20547 case Expr::AtomicExprClass:20548 case Expr::LambdaExprClass:20549 case Expr::CXXFoldExprClass:20550 case Expr::CoawaitExprClass:20551 case Expr::DependentCoawaitExprClass:20552 case Expr::CoyieldExprClass:20553 case Expr::SYCLUniqueStableNameExprClass:20554 case Expr::CXXParenListInitExprClass:20555 case Expr::HLSLOutArgExprClass:20556 return ICEDiag(IK_NotICE, E->getBeginLoc());20557 20558 case Expr::InitListExprClass: {20559 // C++03 [dcl.init]p13: If T is a scalar type, then a declaration of the20560 // form "T x = { a };" is equivalent to "T x = a;".20561 // Unless we're initializing a reference, T is a scalar as it is known to be20562 // of integral or enumeration type.20563 if (E->isPRValue())20564 if (cast<InitListExpr>(E)->getNumInits() == 1)20565 return CheckICE(cast<InitListExpr>(E)->getInit(0), Ctx);20566 return ICEDiag(IK_NotICE, E->getBeginLoc());20567 }20568 20569 case Expr::SizeOfPackExprClass:20570 case Expr::GNUNullExprClass:20571 case Expr::SourceLocExprClass:20572 case Expr::EmbedExprClass:20573 case Expr::OpenACCAsteriskSizeExprClass:20574 return NoDiag();20575 20576 case Expr::PackIndexingExprClass:20577 return CheckICE(cast<PackIndexingExpr>(E)->getSelectedExpr(), Ctx);20578 20579 case Expr::SubstNonTypeTemplateParmExprClass:20580 return20581 CheckICE(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(), Ctx);20582 20583 case Expr::ConstantExprClass:20584 return CheckICE(cast<ConstantExpr>(E)->getSubExpr(), Ctx);20585 20586 case Expr::ParenExprClass:20587 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);20588 case Expr::GenericSelectionExprClass:20589 return CheckICE(cast<GenericSelectionExpr>(E)->getResultExpr(), Ctx);20590 case Expr::IntegerLiteralClass:20591 case Expr::FixedPointLiteralClass:20592 case Expr::CharacterLiteralClass:20593 case Expr::ObjCBoolLiteralExprClass:20594 case Expr::CXXBoolLiteralExprClass:20595 case Expr::CXXScalarValueInitExprClass:20596 case Expr::TypeTraitExprClass:20597 case Expr::ConceptSpecializationExprClass:20598 case Expr::RequiresExprClass:20599 case Expr::ArrayTypeTraitExprClass:20600 case Expr::ExpressionTraitExprClass:20601 case Expr::CXXNoexceptExprClass:20602 return NoDiag();20603 case Expr::CallExprClass:20604 case Expr::CXXOperatorCallExprClass: {20605 // C99 6.6/3 allows function calls within unevaluated subexpressions of20606 // constant expressions, but they can never be ICEs because an ICE cannot20607 // contain an operand of (pointer to) function type.20608 const CallExpr *CE = cast<CallExpr>(E);20609 if (CE->getBuiltinCallee())20610 return CheckEvalInICE(E, Ctx);20611 return ICEDiag(IK_NotICE, E->getBeginLoc());20612 }20613 case Expr::CXXRewrittenBinaryOperatorClass:20614 return CheckICE(cast<CXXRewrittenBinaryOperator>(E)->getSemanticForm(),20615 Ctx);20616 case Expr::DeclRefExprClass: {20617 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();20618 if (isa<EnumConstantDecl>(D))20619 return NoDiag();20620 20621 // C++ and OpenCL (FIXME: spec reference?) allow reading const-qualified20622 // integer variables in constant expressions:20623 //20624 // C++ 7.1.5.1p220625 // A variable of non-volatile const-qualified integral or enumeration20626 // type initialized by an ICE can be used in ICEs.20627 //20628 // We sometimes use CheckICE to check the C++98 rules in C++11 mode. In20629 // that mode, use of reference variables should not be allowed.20630 const VarDecl *VD = dyn_cast<VarDecl>(D);20631 if (VD && VD->isUsableInConstantExpressions(Ctx) &&20632 !VD->getType()->isReferenceType())20633 return NoDiag();20634 20635 return ICEDiag(IK_NotICE, E->getBeginLoc());20636 }20637 case Expr::UnaryOperatorClass: {20638 const UnaryOperator *Exp = cast<UnaryOperator>(E);20639 switch (Exp->getOpcode()) {20640 case UO_PostInc:20641 case UO_PostDec:20642 case UO_PreInc:20643 case UO_PreDec:20644 case UO_AddrOf:20645 case UO_Deref:20646 case UO_Coawait:20647 // C99 6.6/3 allows increment and decrement within unevaluated20648 // subexpressions of constant expressions, but they can never be ICEs20649 // because an ICE cannot contain an lvalue operand.20650 return ICEDiag(IK_NotICE, E->getBeginLoc());20651 case UO_Extension:20652 case UO_LNot:20653 case UO_Plus:20654 case UO_Minus:20655 case UO_Not:20656 case UO_Real:20657 case UO_Imag:20658 return CheckICE(Exp->getSubExpr(), Ctx);20659 }20660 llvm_unreachable("invalid unary operator class");20661 }20662 case Expr::OffsetOfExprClass: {20663 // Note that per C99, offsetof must be an ICE. And AFAIK, using20664 // EvaluateAsRValue matches the proposed gcc behavior for cases like20665 // "offsetof(struct s{int x[4];}, x[1.0])". This doesn't affect20666 // compliance: we should warn earlier for offsetof expressions with20667 // array subscripts that aren't ICEs, and if the array subscripts20668 // are ICEs, the value of the offsetof must be an integer constant.20669 return CheckEvalInICE(E, Ctx);20670 }20671 case Expr::UnaryExprOrTypeTraitExprClass: {20672 const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(E);20673 if ((Exp->getKind() == UETT_SizeOf) &&20674 Exp->getTypeOfArgument()->isVariableArrayType())20675 return ICEDiag(IK_NotICE, E->getBeginLoc());20676 if (Exp->getKind() == UETT_CountOf) {20677 QualType ArgTy = Exp->getTypeOfArgument();20678 if (ArgTy->isVariableArrayType()) {20679 // We need to look whether the array is multidimensional. If it is,20680 // then we want to check the size expression manually to see whether20681 // it is an ICE or not.20682 const auto *VAT = Ctx.getAsVariableArrayType(ArgTy);20683 if (VAT->getElementType()->isArrayType())20684 // Variable array size expression could be missing (e.g. int a[*][10])20685 // In that case, it can't be a constant expression.20686 return VAT->getSizeExpr() ? CheckICE(VAT->getSizeExpr(), Ctx)20687 : ICEDiag(IK_NotICE, E->getBeginLoc());20688 20689 // Otherwise, this is a regular VLA, which is definitely not an ICE.20690 return ICEDiag(IK_NotICE, E->getBeginLoc());20691 }20692 }20693 return NoDiag();20694 }20695 case Expr::BinaryOperatorClass: {20696 const BinaryOperator *Exp = cast<BinaryOperator>(E);20697 switch (Exp->getOpcode()) {20698 case BO_PtrMemD:20699 case BO_PtrMemI:20700 case BO_Assign:20701 case BO_MulAssign:20702 case BO_DivAssign:20703 case BO_RemAssign:20704 case BO_AddAssign:20705 case BO_SubAssign:20706 case BO_ShlAssign:20707 case BO_ShrAssign:20708 case BO_AndAssign:20709 case BO_XorAssign:20710 case BO_OrAssign:20711 // C99 6.6/3 allows assignments within unevaluated subexpressions of20712 // constant expressions, but they can never be ICEs because an ICE cannot20713 // contain an lvalue operand.20714 return ICEDiag(IK_NotICE, E->getBeginLoc());20715 20716 case BO_Mul:20717 case BO_Div:20718 case BO_Rem:20719 case BO_Add:20720 case BO_Sub:20721 case BO_Shl:20722 case BO_Shr:20723 case BO_LT:20724 case BO_GT:20725 case BO_LE:20726 case BO_GE:20727 case BO_EQ:20728 case BO_NE:20729 case BO_And:20730 case BO_Xor:20731 case BO_Or:20732 case BO_Comma:20733 case BO_Cmp: {20734 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);20735 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);20736 if (Exp->getOpcode() == BO_Div ||20737 Exp->getOpcode() == BO_Rem) {20738 // EvaluateAsRValue gives an error for undefined Div/Rem, so make sure20739 // we don't evaluate one.20740 if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE) {20741 llvm::APSInt REval = Exp->getRHS()->EvaluateKnownConstInt(Ctx);20742 if (REval == 0)20743 return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc());20744 if (REval.isSigned() && REval.isAllOnes()) {20745 llvm::APSInt LEval = Exp->getLHS()->EvaluateKnownConstInt(Ctx);20746 if (LEval.isMinSignedValue())20747 return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc());20748 }20749 }20750 }20751 if (Exp->getOpcode() == BO_Comma) {20752 if (Ctx.getLangOpts().C99) {20753 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE20754 // if it isn't evaluated.20755 if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE)20756 return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc());20757 } else {20758 // In both C89 and C++, commas in ICEs are illegal.20759 return ICEDiag(IK_NotICE, E->getBeginLoc());20760 }20761 }20762 return Worst(LHSResult, RHSResult);20763 }20764 case BO_LAnd:20765 case BO_LOr: {20766 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);20767 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);20768 if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICEIfUnevaluated) {20769 // Rare case where the RHS has a comma "side-effect"; we need20770 // to actually check the condition to see whether the side20771 // with the comma is evaluated.20772 if ((Exp->getOpcode() == BO_LAnd) !=20773 (Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0))20774 return RHSResult;20775 return NoDiag();20776 }20777 20778 return Worst(LHSResult, RHSResult);20779 }20780 }20781 llvm_unreachable("invalid binary operator kind");20782 }20783 case Expr::ImplicitCastExprClass:20784 case Expr::CStyleCastExprClass:20785 case Expr::CXXFunctionalCastExprClass:20786 case Expr::CXXStaticCastExprClass:20787 case Expr::CXXReinterpretCastExprClass:20788 case Expr::CXXConstCastExprClass:20789 case Expr::ObjCBridgedCastExprClass: {20790 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();20791 if (isa<ExplicitCastExpr>(E)) {20792 if (const FloatingLiteral *FL20793 = dyn_cast<FloatingLiteral>(SubExpr->IgnoreParenImpCasts())) {20794 unsigned DestWidth = Ctx.getIntWidth(E->getType());20795 bool DestSigned = E->getType()->isSignedIntegerOrEnumerationType();20796 APSInt IgnoredVal(DestWidth, !DestSigned);20797 bool Ignored;20798 // If the value does not fit in the destination type, the behavior is20799 // undefined, so we are not required to treat it as a constant20800 // expression.20801 if (FL->getValue().convertToInteger(IgnoredVal,20802 llvm::APFloat::rmTowardZero,20803 &Ignored) & APFloat::opInvalidOp)20804 return ICEDiag(IK_NotICE, E->getBeginLoc());20805 return NoDiag();20806 }20807 }20808 switch (cast<CastExpr>(E)->getCastKind()) {20809 case CK_LValueToRValue:20810 case CK_AtomicToNonAtomic:20811 case CK_NonAtomicToAtomic:20812 case CK_NoOp:20813 case CK_IntegralToBoolean:20814 case CK_IntegralCast:20815 return CheckICE(SubExpr, Ctx);20816 default:20817 return ICEDiag(IK_NotICE, E->getBeginLoc());20818 }20819 }20820 case Expr::BinaryConditionalOperatorClass: {20821 const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(E);20822 ICEDiag CommonResult = CheckICE(Exp->getCommon(), Ctx);20823 if (CommonResult.Kind == IK_NotICE) return CommonResult;20824 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);20825 if (FalseResult.Kind == IK_NotICE) return FalseResult;20826 if (CommonResult.Kind == IK_ICEIfUnevaluated) return CommonResult;20827 if (FalseResult.Kind == IK_ICEIfUnevaluated &&20828 Exp->getCommon()->EvaluateKnownConstInt(Ctx) != 0) return NoDiag();20829 return FalseResult;20830 }20831 case Expr::ConditionalOperatorClass: {20832 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);20833 // If the condition (ignoring parens) is a __builtin_constant_p call,20834 // then only the true side is actually considered in an integer constant20835 // expression, and it is fully evaluated. This is an important GNU20836 // extension. See GCC PR38377 for discussion.20837 if (const CallExpr *CallCE20838 = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))20839 if (CallCE->getBuiltinCallee() == Builtin::BI__builtin_constant_p)20840 return CheckEvalInICE(E, Ctx);20841 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);20842 if (CondResult.Kind == IK_NotICE)20843 return CondResult;20844 20845 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);20846 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);20847 20848 if (TrueResult.Kind == IK_NotICE)20849 return TrueResult;20850 if (FalseResult.Kind == IK_NotICE)20851 return FalseResult;20852 if (CondResult.Kind == IK_ICEIfUnevaluated)20853 return CondResult;20854 if (TrueResult.Kind == IK_ICE && FalseResult.Kind == IK_ICE)20855 return NoDiag();20856 // Rare case where the diagnostics depend on which side is evaluated20857 // Note that if we get here, CondResult is 0, and at least one of20858 // TrueResult and FalseResult is non-zero.20859 if (Exp->getCond()->EvaluateKnownConstInt(Ctx) == 0)20860 return FalseResult;20861 return TrueResult;20862 }20863 case Expr::CXXDefaultArgExprClass:20864 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);20865 case Expr::CXXDefaultInitExprClass:20866 return CheckICE(cast<CXXDefaultInitExpr>(E)->getExpr(), Ctx);20867 case Expr::ChooseExprClass: {20868 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(), Ctx);20869 }20870 case Expr::BuiltinBitCastExprClass: {20871 if (!checkBitCastConstexprEligibility(nullptr, Ctx, cast<CastExpr>(E)))20872 return ICEDiag(IK_NotICE, E->getBeginLoc());20873 return CheckICE(cast<CastExpr>(E)->getSubExpr(), Ctx);20874 }20875 }20876 20877 llvm_unreachable("Invalid StmtClass!");20878}20879 20880/// Evaluate an expression as a C++11 integral constant expression.20881static bool EvaluateCPlusPlus11IntegralConstantExpr(const ASTContext &Ctx,20882 const Expr *E,20883 llvm::APSInt *Value) {20884 if (!E->getType()->isIntegralOrUnscopedEnumerationType())20885 return false;20886 20887 APValue Result;20888 if (!E->isCXX11ConstantExpr(Ctx, &Result))20889 return false;20890 20891 if (!Result.isInt())20892 return false;20893 20894 if (Value) *Value = Result.getInt();20895 return true;20896}20897 20898bool Expr::isIntegerConstantExpr(const ASTContext &Ctx) const {20899 assert(!isValueDependent() &&20900 "Expression evaluator can't be called on a dependent expression.");20901 20902 ExprTimeTraceScope TimeScope(this, Ctx, "isIntegerConstantExpr");20903 20904 if (Ctx.getLangOpts().CPlusPlus11)20905 return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, nullptr);20906 20907 ICEDiag D = CheckICE(this, Ctx);20908 if (D.Kind != IK_ICE)20909 return false;20910 return true;20911}20912 20913std::optional<llvm::APSInt>20914Expr::getIntegerConstantExpr(const ASTContext &Ctx) const {20915 if (isValueDependent()) {20916 // Expression evaluator can't succeed on a dependent expression.20917 return std::nullopt;20918 }20919 20920 if (Ctx.getLangOpts().CPlusPlus11) {20921 APSInt Value;20922 if (EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, &Value))20923 return Value;20924 return std::nullopt;20925 }20926 20927 if (!isIntegerConstantExpr(Ctx))20928 return std::nullopt;20929 20930 // The only possible side-effects here are due to UB discovered in the20931 // evaluation (for instance, INT_MAX + 1). In such a case, we are still20932 // required to treat the expression as an ICE, so we produce the folded20933 // value.20934 EvalResult ExprResult;20935 Expr::EvalStatus Status;20936 EvalInfo Info(Ctx, Status, EvaluationMode::IgnoreSideEffects);20937 Info.InConstantContext = true;20938 20939 if (!::EvaluateAsInt(this, ExprResult, Ctx, SE_AllowSideEffects, Info))20940 llvm_unreachable("ICE cannot be evaluated!");20941 20942 return ExprResult.Val.getInt();20943}20944 20945bool Expr::isCXX98IntegralConstantExpr(const ASTContext &Ctx) const {20946 assert(!isValueDependent() &&20947 "Expression evaluator can't be called on a dependent expression.");20948 20949 return CheckICE(this, Ctx).Kind == IK_ICE;20950}20951 20952bool Expr::isCXX11ConstantExpr(const ASTContext &Ctx, APValue *Result) const {20953 assert(!isValueDependent() &&20954 "Expression evaluator can't be called on a dependent expression.");20955 20956 // We support this checking in C++98 mode in order to diagnose compatibility20957 // issues.20958 assert(Ctx.getLangOpts().CPlusPlus);20959 20960 bool IsConst;20961 APValue Scratch;20962 if (FastEvaluateAsRValue(this, Scratch, Ctx, IsConst) && Scratch.hasValue()) {20963 if (Result)20964 *Result = Scratch;20965 return true;20966 }20967 20968 // Build evaluation settings.20969 Expr::EvalStatus Status;20970 SmallVector<PartialDiagnosticAt, 8> Diags;20971 Status.Diag = &Diags;20972 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantExpression);20973 20974 bool IsConstExpr =20975 ::EvaluateAsRValue(Info, this, Result ? *Result : Scratch) &&20976 // FIXME: We don't produce a diagnostic for this, but the callers that20977 // call us on arbitrary full-expressions should generally not care.20978 Info.discardCleanups() && !Status.HasSideEffects;20979 20980 return IsConstExpr && Diags.empty();20981}20982 20983bool Expr::EvaluateWithSubstitution(APValue &Value, ASTContext &Ctx,20984 const FunctionDecl *Callee,20985 ArrayRef<const Expr*> Args,20986 const Expr *This) const {20987 assert(!isValueDependent() &&20988 "Expression evaluator can't be called on a dependent expression.");20989 20990 llvm::TimeTraceScope TimeScope("EvaluateWithSubstitution", [&] {20991 std::string Name;20992 llvm::raw_string_ostream OS(Name);20993 Callee->getNameForDiagnostic(OS, Ctx.getPrintingPolicy(),20994 /*Qualified=*/true);20995 return Name;20996 });20997 20998 Expr::EvalStatus Status;20999 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantExpressionUnevaluated);21000 Info.InConstantContext = true;21001 21002 LValue ThisVal;21003 const LValue *ThisPtr = nullptr;21004 if (This) {21005#ifndef NDEBUG21006 auto *MD = dyn_cast<CXXMethodDecl>(Callee);21007 assert(MD && "Don't provide `this` for non-methods.");21008 assert(MD->isImplicitObjectMemberFunction() &&21009 "Don't provide `this` for methods without an implicit object.");21010#endif21011 if (!This->isValueDependent() &&21012 EvaluateObjectArgument(Info, This, ThisVal) &&21013 !Info.EvalStatus.HasSideEffects)21014 ThisPtr = &ThisVal;21015 21016 // Ignore any side-effects from a failed evaluation. This is safe because21017 // they can't interfere with any other argument evaluation.21018 Info.EvalStatus.HasSideEffects = false;21019 }21020 21021 CallRef Call = Info.CurrentCall->createCall(Callee);21022 for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end();21023 I != E; ++I) {21024 unsigned Idx = I - Args.begin();21025 if (Idx >= Callee->getNumParams())21026 break;21027 const ParmVarDecl *PVD = Callee->getParamDecl(Idx);21028 if ((*I)->isValueDependent() ||21029 !EvaluateCallArg(PVD, *I, Call, Info) ||21030 Info.EvalStatus.HasSideEffects) {21031 // If evaluation fails, throw away the argument entirely.21032 if (APValue *Slot = Info.getParamSlot(Call, PVD))21033 *Slot = APValue();21034 }21035 21036 // Ignore any side-effects from a failed evaluation. This is safe because21037 // they can't interfere with any other argument evaluation.21038 Info.EvalStatus.HasSideEffects = false;21039 }21040 21041 // Parameter cleanups happen in the caller and are not part of this21042 // evaluation.21043 Info.discardCleanups();21044 Info.EvalStatus.HasSideEffects = false;21045 21046 // Build fake call to Callee.21047 CallStackFrame Frame(Info, Callee->getLocation(), Callee, ThisPtr, This,21048 Call);21049 // FIXME: Missing ExprWithCleanups in enable_if conditions?21050 FullExpressionRAII Scope(Info);21051 return Evaluate(Value, Info, this) && Scope.destroy() &&21052 !Info.EvalStatus.HasSideEffects;21053}21054 21055bool Expr::isPotentialConstantExpr(const FunctionDecl *FD,21056 SmallVectorImpl<21057 PartialDiagnosticAt> &Diags) {21058 // FIXME: It would be useful to check constexpr function templates, but at the21059 // moment the constant expression evaluator cannot cope with the non-rigorous21060 // ASTs which we build for dependent expressions.21061 if (FD->isDependentContext())21062 return true;21063 21064 llvm::TimeTraceScope TimeScope("isPotentialConstantExpr", [&] {21065 std::string Name;21066 llvm::raw_string_ostream OS(Name);21067 FD->getNameForDiagnostic(OS, FD->getASTContext().getPrintingPolicy(),21068 /*Qualified=*/true);21069 return Name;21070 });21071 21072 Expr::EvalStatus Status;21073 Status.Diag = &Diags;21074 21075 EvalInfo Info(FD->getASTContext(), Status,21076 EvaluationMode::ConstantExpression);21077 Info.InConstantContext = true;21078 Info.CheckingPotentialConstantExpression = true;21079 21080 // The constexpr VM attempts to compile all methods to bytecode here.21081 if (Info.EnableNewConstInterp) {21082 Info.Ctx.getInterpContext().isPotentialConstantExpr(Info, FD);21083 return Diags.empty();21084 }21085 21086 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);21087 const CXXRecordDecl *RD = MD ? MD->getParent()->getCanonicalDecl() : nullptr;21088 21089 // Fabricate an arbitrary expression on the stack and pretend that it21090 // is a temporary being used as the 'this' pointer.21091 LValue This;21092 ImplicitValueInitExpr VIE(RD ? Info.Ctx.getCanonicalTagType(RD)21093 : Info.Ctx.IntTy);21094 This.set({&VIE, Info.CurrentCall->Index});21095 21096 ArrayRef<const Expr*> Args;21097 21098 APValue Scratch;21099 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) {21100 // Evaluate the call as a constant initializer, to allow the construction21101 // of objects of non-literal types.21102 Info.setEvaluatingDecl(This.getLValueBase(), Scratch);21103 HandleConstructorCall(&VIE, This, Args, CD, Info, Scratch);21104 } else {21105 SourceLocation Loc = FD->getLocation();21106 HandleFunctionCall(21107 Loc, FD, (MD && MD->isImplicitObjectMemberFunction()) ? &This : nullptr,21108 &VIE, Args, CallRef(), FD->getBody(), Info, Scratch,21109 /*ResultSlot=*/nullptr);21110 }21111 21112 return Diags.empty();21113}21114 21115bool Expr::isPotentialConstantExprUnevaluated(Expr *E,21116 const FunctionDecl *FD,21117 SmallVectorImpl<21118 PartialDiagnosticAt> &Diags) {21119 assert(!E->isValueDependent() &&21120 "Expression evaluator can't be called on a dependent expression.");21121 21122 Expr::EvalStatus Status;21123 Status.Diag = &Diags;21124 21125 EvalInfo Info(FD->getASTContext(), Status,21126 EvaluationMode::ConstantExpressionUnevaluated);21127 Info.InConstantContext = true;21128 Info.CheckingPotentialConstantExpression = true;21129 21130 if (Info.EnableNewConstInterp) {21131 Info.Ctx.getInterpContext().isPotentialConstantExprUnevaluated(Info, E, FD);21132 return Diags.empty();21133 }21134 21135 // Fabricate a call stack frame to give the arguments a plausible cover story.21136 CallStackFrame Frame(Info, SourceLocation(), FD, /*This=*/nullptr,21137 /*CallExpr=*/nullptr, CallRef());21138 21139 APValue ResultScratch;21140 Evaluate(ResultScratch, Info, E);21141 return Diags.empty();21142}21143 21144bool Expr::tryEvaluateObjectSize(uint64_t &Result, ASTContext &Ctx,21145 unsigned Type) const {21146 if (!getType()->isPointerType())21147 return false;21148 21149 Expr::EvalStatus Status;21150 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantFold);21151 return tryEvaluateBuiltinObjectSize(this, Type, Info, Result);21152}21153 21154static bool EvaluateBuiltinStrLen(const Expr *E, uint64_t &Result,21155 EvalInfo &Info, std::string *StringResult) {21156 if (!E->getType()->hasPointerRepresentation() || !E->isPRValue())21157 return false;21158 21159 LValue String;21160 21161 if (!EvaluatePointer(E, String, Info))21162 return false;21163 21164 QualType CharTy = E->getType()->getPointeeType();21165 21166 // Fast path: if it's a string literal, search the string value.21167 if (const StringLiteral *S = dyn_cast_or_null<StringLiteral>(21168 String.getLValueBase().dyn_cast<const Expr *>())) {21169 StringRef Str = S->getBytes();21170 int64_t Off = String.Offset.getQuantity();21171 if (Off >= 0 && (uint64_t)Off <= (uint64_t)Str.size() &&21172 S->getCharByteWidth() == 1 &&21173 // FIXME: Add fast-path for wchar_t too.21174 Info.Ctx.hasSameUnqualifiedType(CharTy, Info.Ctx.CharTy)) {21175 Str = Str.substr(Off);21176 21177 StringRef::size_type Pos = Str.find(0);21178 if (Pos != StringRef::npos)21179 Str = Str.substr(0, Pos);21180 21181 Result = Str.size();21182 if (StringResult)21183 *StringResult = Str;21184 return true;21185 }21186 21187 // Fall through to slow path.21188 }21189 21190 // Slow path: scan the bytes of the string looking for the terminating 0.21191 for (uint64_t Strlen = 0; /**/; ++Strlen) {21192 APValue Char;21193 if (!handleLValueToRValueConversion(Info, E, CharTy, String, Char) ||21194 !Char.isInt())21195 return false;21196 if (!Char.getInt()) {21197 Result = Strlen;21198 return true;21199 } else if (StringResult)21200 StringResult->push_back(Char.getInt().getExtValue());21201 if (!HandleLValueArrayAdjustment(Info, E, String, CharTy, 1))21202 return false;21203 }21204}21205 21206std::optional<std::string> Expr::tryEvaluateString(ASTContext &Ctx) const {21207 Expr::EvalStatus Status;21208 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantFold);21209 uint64_t Result;21210 std::string StringResult;21211 21212 if (Info.EnableNewConstInterp) {21213 if (!Info.Ctx.getInterpContext().evaluateString(Info, this, StringResult))21214 return std::nullopt;21215 return StringResult;21216 }21217 21218 if (EvaluateBuiltinStrLen(this, Result, Info, &StringResult))21219 return StringResult;21220 return std::nullopt;21221}21222 21223template <typename T>21224static bool EvaluateCharRangeAsStringImpl(const Expr *, T &Result,21225 const Expr *SizeExpression,21226 const Expr *PtrExpression,21227 ASTContext &Ctx,21228 Expr::EvalResult &Status) {21229 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantExpression);21230 Info.InConstantContext = true;21231 21232 if (Info.EnableNewConstInterp)21233 return Info.Ctx.getInterpContext().evaluateCharRange(Info, SizeExpression,21234 PtrExpression, Result);21235 21236 LValue String;21237 FullExpressionRAII Scope(Info);21238 APSInt SizeValue;21239 if (!::EvaluateInteger(SizeExpression, SizeValue, Info))21240 return false;21241 21242 uint64_t Size = SizeValue.getZExtValue();21243 21244 // FIXME: better protect against invalid or excessive sizes21245 if constexpr (std::is_same_v<APValue, T>)21246 Result = APValue(APValue::UninitArray{}, Size, Size);21247 else {21248 if (Size < Result.max_size())21249 Result.reserve(Size);21250 }21251 if (!::EvaluatePointer(PtrExpression, String, Info))21252 return false;21253 21254 QualType CharTy = PtrExpression->getType()->getPointeeType();21255 for (uint64_t I = 0; I < Size; ++I) {21256 APValue Char;21257 if (!handleLValueToRValueConversion(Info, PtrExpression, CharTy, String,21258 Char))21259 return false;21260 21261 if constexpr (std::is_same_v<APValue, T>) {21262 Result.getArrayInitializedElt(I) = std::move(Char);21263 } else {21264 APSInt C = Char.getInt();21265 21266 assert(C.getBitWidth() <= 8 &&21267 "string element not representable in char");21268 21269 Result.push_back(static_cast<char>(C.getExtValue()));21270 }21271 21272 if (!HandleLValueArrayAdjustment(Info, PtrExpression, String, CharTy, 1))21273 return false;21274 }21275 21276 return Scope.destroy() && CheckMemoryLeaks(Info);21277}21278 21279bool Expr::EvaluateCharRangeAsString(std::string &Result,21280 const Expr *SizeExpression,21281 const Expr *PtrExpression, ASTContext &Ctx,21282 EvalResult &Status) const {21283 return EvaluateCharRangeAsStringImpl(this, Result, SizeExpression,21284 PtrExpression, Ctx, Status);21285}21286 21287bool Expr::EvaluateCharRangeAsString(APValue &Result,21288 const Expr *SizeExpression,21289 const Expr *PtrExpression, ASTContext &Ctx,21290 EvalResult &Status) const {21291 return EvaluateCharRangeAsStringImpl(this, Result, SizeExpression,21292 PtrExpression, Ctx, Status);21293}21294 21295bool Expr::tryEvaluateStrLen(uint64_t &Result, ASTContext &Ctx) const {21296 Expr::EvalStatus Status;21297 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantFold);21298 21299 if (Info.EnableNewConstInterp)21300 return Info.Ctx.getInterpContext().evaluateStrlen(Info, this, Result);21301 21302 return EvaluateBuiltinStrLen(this, Result, Info);21303}21304 21305namespace {21306struct IsWithinLifetimeHandler {21307 EvalInfo &Info;21308 static constexpr AccessKinds AccessKind = AccessKinds::AK_IsWithinLifetime;21309 using result_type = std::optional<bool>;21310 std::optional<bool> failed() { return std::nullopt; }21311 template <typename T>21312 std::optional<bool> found(T &Subobj, QualType SubobjType) {21313 return true;21314 }21315};21316 21317std::optional<bool> EvaluateBuiltinIsWithinLifetime(IntExprEvaluator &IEE,21318 const CallExpr *E) {21319 EvalInfo &Info = IEE.Info;21320 // Sometimes this is called during some sorts of constant folding / early21321 // evaluation. These are meant for non-constant expressions and are not21322 // necessary since this consteval builtin will never be evaluated at runtime.21323 // Just fail to evaluate when not in a constant context.21324 if (!Info.InConstantContext)21325 return std::nullopt;21326 assert(E->getBuiltinCallee() == Builtin::BI__builtin_is_within_lifetime);21327 const Expr *Arg = E->getArg(0);21328 if (Arg->isValueDependent())21329 return std::nullopt;21330 LValue Val;21331 if (!EvaluatePointer(Arg, Val, Info))21332 return std::nullopt;21333 21334 if (Val.allowConstexprUnknown())21335 return true;21336 21337 auto Error = [&](int Diag) {21338 bool CalledFromStd = false;21339 const auto *Callee = Info.CurrentCall->getCallee();21340 if (Callee && Callee->isInStdNamespace()) {21341 const IdentifierInfo *Identifier = Callee->getIdentifier();21342 CalledFromStd = Identifier && Identifier->isStr("is_within_lifetime");21343 }21344 Info.CCEDiag(CalledFromStd ? Info.CurrentCall->getCallRange().getBegin()21345 : E->getExprLoc(),21346 diag::err_invalid_is_within_lifetime)21347 << (CalledFromStd ? "std::is_within_lifetime"21348 : "__builtin_is_within_lifetime")21349 << Diag;21350 return std::nullopt;21351 };21352 // C++2c [meta.const.eval]p4:21353 // During the evaluation of an expression E as a core constant expression, a21354 // call to this function is ill-formed unless p points to an object that is21355 // usable in constant expressions or whose complete object's lifetime began21356 // within E.21357 21358 // Make sure it points to an object21359 // nullptr does not point to an object21360 if (Val.isNullPointer() || Val.getLValueBase().isNull())21361 return Error(0);21362 QualType T = Val.getLValueBase().getType();21363 assert(!T->isFunctionType() &&21364 "Pointers to functions should have been typed as function pointers "21365 "which would have been rejected earlier");21366 assert(T->isObjectType());21367 // Hypothetical array element is not an object21368 if (Val.getLValueDesignator().isOnePastTheEnd())21369 return Error(1);21370 assert(Val.getLValueDesignator().isValidSubobject() &&21371 "Unchecked case for valid subobject");21372 // All other ill-formed values should have failed EvaluatePointer, so the21373 // object should be a pointer to an object that is usable in a constant21374 // expression or whose complete lifetime began within the expression21375 CompleteObject CO =21376 findCompleteObject(Info, E, AccessKinds::AK_IsWithinLifetime, Val, T);21377 // The lifetime hasn't begun yet if we are still evaluating the21378 // initializer ([basic.life]p(1.2))21379 if (Info.EvaluatingDeclValue && CO.Value == Info.EvaluatingDeclValue)21380 return Error(2);21381 21382 if (!CO)21383 return false;21384 IsWithinLifetimeHandler handler{Info};21385 return findSubobject(Info, E, CO, Val.getLValueDesignator(), handler);21386}21387} // namespace21388