ExprConstant.cpp revision 8ae4ec28451a16a57718286da3e476fc2f495c3f
1b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattner//===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===// 2c44eec6dd29ee9415cbd38a35deff4c8b67abb6aAnders Carlsson// 3c44eec6dd29ee9415cbd38a35deff4c8b67abb6aAnders Carlsson// The LLVM Compiler Infrastructure 4c44eec6dd29ee9415cbd38a35deff4c8b67abb6aAnders Carlsson// 5c44eec6dd29ee9415cbd38a35deff4c8b67abb6aAnders Carlsson// This file is distributed under the University of Illinois Open Source 6c44eec6dd29ee9415cbd38a35deff4c8b67abb6aAnders Carlsson// License. See LICENSE.TXT for details. 7c44eec6dd29ee9415cbd38a35deff4c8b67abb6aAnders Carlsson// 8c44eec6dd29ee9415cbd38a35deff4c8b67abb6aAnders Carlsson//===----------------------------------------------------------------------===// 9c44eec6dd29ee9415cbd38a35deff4c8b67abb6aAnders Carlsson// 10c44eec6dd29ee9415cbd38a35deff4c8b67abb6aAnders Carlsson// This file implements the Expr constant evaluator. 11c44eec6dd29ee9415cbd38a35deff4c8b67abb6aAnders Carlsson// 12745f5147e065900267c85a5568785a1991d4838fRichard Smith// Constant expression evaluation produces four main results: 13745f5147e065900267c85a5568785a1991d4838fRichard Smith// 14745f5147e065900267c85a5568785a1991d4838fRichard Smith// * A success/failure flag indicating whether constant folding was successful. 15745f5147e065900267c85a5568785a1991d4838fRichard Smith// This is the 'bool' return value used by most of the code in this file. A 16745f5147e065900267c85a5568785a1991d4838fRichard Smith// 'false' return value indicates that constant folding has failed, and any 17745f5147e065900267c85a5568785a1991d4838fRichard Smith// appropriate diagnostic has already been produced. 18745f5147e065900267c85a5568785a1991d4838fRichard Smith// 19745f5147e065900267c85a5568785a1991d4838fRichard Smith// * An evaluated result, valid only if constant folding has not failed. 20745f5147e065900267c85a5568785a1991d4838fRichard Smith// 21745f5147e065900267c85a5568785a1991d4838fRichard Smith// * A flag indicating if evaluation encountered (unevaluated) side-effects. 22745f5147e065900267c85a5568785a1991d4838fRichard Smith// These arise in cases such as (sideEffect(), 0) and (sideEffect() || 1), 23745f5147e065900267c85a5568785a1991d4838fRichard Smith// where it is possible to determine the evaluated result regardless. 24745f5147e065900267c85a5568785a1991d4838fRichard Smith// 25745f5147e065900267c85a5568785a1991d4838fRichard Smith// * A set of notes indicating why the evaluation was not a constant expression 26745f5147e065900267c85a5568785a1991d4838fRichard Smith// (under the C++11 rules only, at the moment), or, if folding failed too, 27745f5147e065900267c85a5568785a1991d4838fRichard Smith// why the expression could not be folded. 28745f5147e065900267c85a5568785a1991d4838fRichard Smith// 29745f5147e065900267c85a5568785a1991d4838fRichard Smith// If we are checking for a potential constant expression, failure to constant 30745f5147e065900267c85a5568785a1991d4838fRichard Smith// fold a potential constant sub-expression will be indicated by a 'false' 31745f5147e065900267c85a5568785a1991d4838fRichard Smith// return value (the expression could not be folded) and no diagnostic (the 32745f5147e065900267c85a5568785a1991d4838fRichard Smith// expression is not necessarily non-constant). 33745f5147e065900267c85a5568785a1991d4838fRichard Smith// 34c44eec6dd29ee9415cbd38a35deff4c8b67abb6aAnders Carlsson//===----------------------------------------------------------------------===// 35c44eec6dd29ee9415cbd38a35deff4c8b67abb6aAnders Carlsson 36c44eec6dd29ee9415cbd38a35deff4c8b67abb6aAnders Carlsson#include "clang/AST/APValue.h" 37c44eec6dd29ee9415cbd38a35deff4c8b67abb6aAnders Carlsson#include "clang/AST/ASTContext.h" 38199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck#include "clang/AST/CharUnits.h" 3919cc4abea06a9b49e0e16a50d335c064cd723572Anders Carlsson#include "clang/AST/RecordLayout.h" 400fe52e1bcaa69ba127f1bda036f057fec1f478deSeo Sanghyeon#include "clang/AST/StmtVisitor.h" 418ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor#include "clang/AST/TypeLoc.h" 42500d3297d2a21edeac4d46cbcbe21bc2352c2a28Chris Lattner#include "clang/AST/ASTDiagnostic.h" 438ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor#include "clang/AST/Expr.h" 441b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner#include "clang/Basic/Builtins.h" 4506a3675627e3b3c47b49c689c8e404a33144194aAnders Carlsson#include "clang/Basic/TargetInfo.h" 467462b39a9bccaf4392687831036713f09f9c0681Mike Stump#include "llvm/ADT/SmallString.h" 474572baba9d18c275968ac113fd73b0e3c77cccb8Mike Stump#include <cstring> 487b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith#include <functional> 494572baba9d18c275968ac113fd73b0e3c77cccb8Mike Stump 50c44eec6dd29ee9415cbd38a35deff4c8b67abb6aAnders Carlssonusing namespace clang; 51f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattnerusing llvm::APSInt; 52d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmanusing llvm::APFloat; 53c44eec6dd29ee9415cbd38a35deff4c8b67abb6aAnders Carlsson 5483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smithstatic bool IsGlobalLValue(APValue::LValueBase B); 5583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith 56c54061a679d8db4169438b2f97f81e3f443c6a25Benjamin Kramernamespace { 57180f47959a066795cc0f409433023af448bb0328Richard Smith struct LValue; 58d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith struct CallStackFrame; 59bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith struct EvalInfo; 60d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 6183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith static QualType getType(APValue::LValueBase B) { 621bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith if (!B) return QualType(); 631bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) 641bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith return D->getType(); 651bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith return B.get<const Expr*>()->getType(); 661bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith } 671bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith 68180f47959a066795cc0f409433023af448bb0328Richard Smith /// Get an LValue path entry, which is known to not be an array index, as a 69f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith /// field or base class. 7083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith static 71f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith APValue::BaseOrMemberType getAsBaseOrMember(APValue::LValuePathEntry E) { 72180f47959a066795cc0f409433023af448bb0328Richard Smith APValue::BaseOrMemberType Value; 73180f47959a066795cc0f409433023af448bb0328Richard Smith Value.setFromOpaqueValue(E.BaseOrMember); 74f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith return Value; 75f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith } 76f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 77f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith /// Get an LValue path entry, which is known to not be an array index, as a 78f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith /// field declaration. 7983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith static const FieldDecl *getAsField(APValue::LValuePathEntry E) { 80f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith return dyn_cast<FieldDecl>(getAsBaseOrMember(E).getPointer()); 81180f47959a066795cc0f409433023af448bb0328Richard Smith } 82180f47959a066795cc0f409433023af448bb0328Richard Smith /// Get an LValue path entry, which is known to not be an array index, as a 83180f47959a066795cc0f409433023af448bb0328Richard Smith /// base class declaration. 8483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith static const CXXRecordDecl *getAsBaseClass(APValue::LValuePathEntry E) { 85f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith return dyn_cast<CXXRecordDecl>(getAsBaseOrMember(E).getPointer()); 86180f47959a066795cc0f409433023af448bb0328Richard Smith } 87180f47959a066795cc0f409433023af448bb0328Richard Smith /// Determine whether this LValue path entry for a base class names a virtual 88180f47959a066795cc0f409433023af448bb0328Richard Smith /// base class. 8983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith static bool isVirtualBaseClass(APValue::LValuePathEntry E) { 90f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith return getAsBaseOrMember(E).getInt(); 91180f47959a066795cc0f409433023af448bb0328Richard Smith } 92180f47959a066795cc0f409433023af448bb0328Richard Smith 93b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith /// Find the path length and type of the most-derived subobject in the given 94b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith /// path, and find the size of the containing array, if any. 95b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith static 96b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith unsigned findMostDerivedSubobject(ASTContext &Ctx, QualType Base, 97b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith ArrayRef<APValue::LValuePathEntry> Path, 98b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith uint64_t &ArraySize, QualType &Type) { 99b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith unsigned MostDerivedLength = 0; 100b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Type = Base; 1019a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith for (unsigned I = 0, N = Path.size(); I != N; ++I) { 102b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (Type->isArrayType()) { 103b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith const ConstantArrayType *CAT = 104b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith cast<ConstantArrayType>(Ctx.getAsArrayType(Type)); 105b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Type = CAT->getElementType(); 106b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith ArraySize = CAT->getSize().getZExtValue(); 107b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith MostDerivedLength = I + 1; 10886024013d4c3728122c58fa07a2a67e6c15837efRichard Smith } else if (Type->isAnyComplexType()) { 10986024013d4c3728122c58fa07a2a67e6c15837efRichard Smith const ComplexType *CT = Type->castAs<ComplexType>(); 11086024013d4c3728122c58fa07a2a67e6c15837efRichard Smith Type = CT->getElementType(); 11186024013d4c3728122c58fa07a2a67e6c15837efRichard Smith ArraySize = 2; 11286024013d4c3728122c58fa07a2a67e6c15837efRichard Smith MostDerivedLength = I + 1; 113b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } else if (const FieldDecl *FD = getAsField(Path[I])) { 114b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Type = FD->getType(); 115b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith ArraySize = 0; 116b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith MostDerivedLength = I + 1; 117b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } else { 1189a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith // Path[I] describes a base class. 119b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith ArraySize = 0; 120b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } 1219a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith } 122b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return MostDerivedLength; 1239a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith } 1249a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith 125b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // The order of this enum is important for diagnostics. 126b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith enum CheckSubobjectKind { 127b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith CSK_Base, CSK_Derived, CSK_Field, CSK_ArrayToPointer, CSK_ArrayIndex, 12886024013d4c3728122c58fa07a2a67e6c15837efRichard Smith CSK_This, CSK_Real, CSK_Imag 129b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith }; 130b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 1310a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith /// A path from a glvalue to a subobject of that glvalue. 1320a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith struct SubobjectDesignator { 1330a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith /// True if the subobject was named in a manner not supported by C++11. Such 1340a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith /// lvalues can still be folded, but they are not core constant expressions 1350a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith /// and we cannot perform lvalue-to-rvalue conversions on them. 1360a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith bool Invalid : 1; 1370a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith 138b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith /// Is this a pointer one past the end of an object? 139b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith bool IsOnePastTheEnd : 1; 140b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 141b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith /// The length of the path to the most-derived object of which this is a 142b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith /// subobject. 143b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith unsigned MostDerivedPathLength : 30; 144b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 145b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith /// The size of the array of which the most-derived object is an element, or 146b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith /// 0 if the most-derived object is not an array element. 147b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith uint64_t MostDerivedArraySize; 1480a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith 149b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith /// The type of the most derived object referred to by this address. 150b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith QualType MostDerivedType; 1510a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith 1529a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith typedef APValue::LValuePathEntry PathEntry; 1539a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith 1540a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith /// The entries on the path from the glvalue to the designated subobject. 1550a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith SmallVector<PathEntry, 8> Entries; 1560a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith 157b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith SubobjectDesignator() : Invalid(true) {} 1580a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith 159b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith explicit SubobjectDesignator(QualType T) 160b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith : Invalid(false), IsOnePastTheEnd(false), MostDerivedPathLength(0), 161b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith MostDerivedArraySize(0), MostDerivedType(T) {} 162b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 163b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith SubobjectDesignator(ASTContext &Ctx, const APValue &V) 164b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith : Invalid(!V.isLValue() || !V.hasLValuePath()), IsOnePastTheEnd(false), 165b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith MostDerivedPathLength(0), MostDerivedArraySize(0) { 1669a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith if (!Invalid) { 167b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith IsOnePastTheEnd = V.isLValueOnePastTheEnd(); 1689a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith ArrayRef<PathEntry> VEntries = V.getLValuePath(); 1699a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith Entries.insert(Entries.end(), VEntries.begin(), VEntries.end()); 1709a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith if (V.getLValueBase()) 171b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith MostDerivedPathLength = 172b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith findMostDerivedSubobject(Ctx, getType(V.getLValueBase()), 173b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith V.getLValuePath(), MostDerivedArraySize, 174b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith MostDerivedType); 1759a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith } 1769a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith } 1779a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith 1780a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith void setInvalid() { 1790a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith Invalid = true; 1800a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith Entries.clear(); 1810a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith } 182b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 183b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith /// Determine whether this is a one-past-the-end pointer. 184b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith bool isOnePastTheEnd() const { 185b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (IsOnePastTheEnd) 186b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return true; 187b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (MostDerivedArraySize && 188b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Entries[MostDerivedPathLength - 1].ArrayIndex == MostDerivedArraySize) 189b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return true; 190b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return false; 191b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } 192b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 193b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith /// Check that this refers to a valid subobject. 194b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith bool isValidSubobject() const { 195b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (Invalid) 196b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return false; 197b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return !isOnePastTheEnd(); 198b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } 199b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith /// Check that this refers to a valid subobject, and if not, produce a 200b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith /// relevant diagnostic and set the designator as invalid. 201b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK); 202b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 203b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith /// Update this designator to refer to the first element within this array. 204b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith void addArrayUnchecked(const ConstantArrayType *CAT) { 2050a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith PathEntry Entry; 206b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Entry.ArrayIndex = 0; 2070a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith Entries.push_back(Entry); 208b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 209b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // This is a most-derived object. 210b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith MostDerivedType = CAT->getElementType(); 211b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith MostDerivedArraySize = CAT->getSize().getZExtValue(); 212b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith MostDerivedPathLength = Entries.size(); 2130a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith } 2140a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith /// Update this designator to refer to the given base or member of this 2150a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith /// object. 216b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith void addDeclUnchecked(const Decl *D, bool Virtual = false) { 2170a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith PathEntry Entry; 218180f47959a066795cc0f409433023af448bb0328Richard Smith APValue::BaseOrMemberType Value(D, Virtual); 219180f47959a066795cc0f409433023af448bb0328Richard Smith Entry.BaseOrMember = Value.getOpaqueValue(); 2200a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith Entries.push_back(Entry); 221b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 222b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // If this isn't a base class, it's a new most-derived object. 223b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (const FieldDecl *FD = dyn_cast<FieldDecl>(D)) { 224b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith MostDerivedType = FD->getType(); 225b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith MostDerivedArraySize = 0; 226b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith MostDerivedPathLength = Entries.size(); 227b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } 2280a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith } 22986024013d4c3728122c58fa07a2a67e6c15837efRichard Smith /// Update this designator to refer to the given complex component. 23086024013d4c3728122c58fa07a2a67e6c15837efRichard Smith void addComplexUnchecked(QualType EltTy, bool Imag) { 23186024013d4c3728122c58fa07a2a67e6c15837efRichard Smith PathEntry Entry; 23286024013d4c3728122c58fa07a2a67e6c15837efRichard Smith Entry.ArrayIndex = Imag; 23386024013d4c3728122c58fa07a2a67e6c15837efRichard Smith Entries.push_back(Entry); 23486024013d4c3728122c58fa07a2a67e6c15837efRichard Smith 23586024013d4c3728122c58fa07a2a67e6c15837efRichard Smith // This is technically a most-derived object, though in practice this 23686024013d4c3728122c58fa07a2a67e6c15837efRichard Smith // is unlikely to matter. 23786024013d4c3728122c58fa07a2a67e6c15837efRichard Smith MostDerivedType = EltTy; 23886024013d4c3728122c58fa07a2a67e6c15837efRichard Smith MostDerivedArraySize = 2; 23986024013d4c3728122c58fa07a2a67e6c15837efRichard Smith MostDerivedPathLength = Entries.size(); 24086024013d4c3728122c58fa07a2a67e6c15837efRichard Smith } 241b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith void diagnosePointerArithmetic(EvalInfo &Info, const Expr *E, uint64_t N); 2420a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith /// Add N to the address of this subobject. 243b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith void adjustIndex(EvalInfo &Info, const Expr *E, uint64_t N) { 2440a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith if (Invalid) return; 245b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (MostDerivedPathLength == Entries.size() && MostDerivedArraySize) { 2469a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith Entries.back().ArrayIndex += N; 247b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (Entries.back().ArrayIndex > MostDerivedArraySize) { 248b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith diagnosePointerArithmetic(Info, E, Entries.back().ArrayIndex); 249b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith setInvalid(); 250b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } 2510a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith return; 2520a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith } 253b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // [expr.add]p4: For the purposes of these operators, a pointer to a 254b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // nonarray object behaves the same as a pointer to the first element of 255b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // an array of length one with the type of the object as its element type. 256b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (IsOnePastTheEnd && N == (uint64_t)-1) 257b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith IsOnePastTheEnd = false; 258b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith else if (!IsOnePastTheEnd && N == 1) 259b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith IsOnePastTheEnd = true; 260b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith else if (N != 0) { 261b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith diagnosePointerArithmetic(Info, E, uint64_t(IsOnePastTheEnd) + N); 2620a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith setInvalid(); 263b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } 2640a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith } 2650a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith }; 2660a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith 267bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith /// A stack frame in the constexpr call stack. 268bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith struct CallStackFrame { 269bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith EvalInfo &Info; 270bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith 271bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith /// Parent - The caller of this stack frame. 272bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith CallStackFrame *Caller; 273bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith 27408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith /// CallLoc - The location of the call expression for this call. 27508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith SourceLocation CallLoc; 27608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith 27708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith /// Callee - The function which was called. 27808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith const FunctionDecl *Callee; 27908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith 28083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith /// Index - The call index of this call. 28183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith unsigned Index; 28283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith 283180f47959a066795cc0f409433023af448bb0328Richard Smith /// This - The binding for the this pointer in this call, if any. 284180f47959a066795cc0f409433023af448bb0328Richard Smith const LValue *This; 285180f47959a066795cc0f409433023af448bb0328Richard Smith 286bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith /// ParmBindings - Parameter bindings for this function call, indexed by 287bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith /// parameters' function scope indices. 2881aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith const APValue *Arguments; 289bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith 290f6172aee547241427e6dabdd0bd6fcaf1c046689Eli Friedman // Note that we intentionally use std::map here so that references to 291f6172aee547241427e6dabdd0bd6fcaf1c046689Eli Friedman // values are stable. 292f6172aee547241427e6dabdd0bd6fcaf1c046689Eli Friedman typedef std::map<const Expr*, APValue> MapTy; 293bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith typedef MapTy::const_iterator temp_iterator; 294bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith /// Temporaries - Temporary lvalues materialized within this stack frame. 295bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith MapTy Temporaries; 296bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith 29708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith CallStackFrame(EvalInfo &Info, SourceLocation CallLoc, 29808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith const FunctionDecl *Callee, const LValue *This, 2991aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith const APValue *Arguments); 300bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith ~CallStackFrame(); 301bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith }; 302bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith 303dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith /// A partial diagnostic which we might know in advance that we are not going 304dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith /// to emit. 305dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith class OptionalDiagnostic { 306dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith PartialDiagnostic *Diag; 307dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith 308dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith public: 309dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith explicit OptionalDiagnostic(PartialDiagnostic *Diag = 0) : Diag(Diag) {} 310dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith 311dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith template<typename T> 312dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith OptionalDiagnostic &operator<<(const T &v) { 313dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith if (Diag) 314dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith *Diag << v; 315dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith return *this; 316dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith } 317789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith 318789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith OptionalDiagnostic &operator<<(const APSInt &I) { 319789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith if (Diag) { 320789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith llvm::SmallVector<char, 32> Buffer; 321789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith I.toString(Buffer); 322789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith *Diag << StringRef(Buffer.data(), Buffer.size()); 323789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith } 324789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith return *this; 325789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith } 326789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith 327789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith OptionalDiagnostic &operator<<(const APFloat &F) { 328789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith if (Diag) { 329789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith llvm::SmallVector<char, 32> Buffer; 330789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith F.toString(Buffer); 331789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith *Diag << StringRef(Buffer.data(), Buffer.size()); 332789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith } 333789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith return *this; 334789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith } 335dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith }; 336dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith 33783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith /// EvalInfo - This is a private struct used by the evaluator to capture 33883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith /// information about a subexpression as it is folded. It retains information 33983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith /// about the AST context, but also maintains information about the folded 34083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith /// expression. 34183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith /// 34283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith /// If an expression could be evaluated, it is still possible it is not a C 34383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith /// "integer constant expression" or constant expression. If not, this struct 34483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith /// captures information about how and why not. 34583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith /// 34683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith /// One bit of information passed *into* the request for constant folding 34783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith /// indicates whether the subexpression is "evaluated" or not according to C 34883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith /// rules. For example, the RHS of (0 && foo()) is not evaluated. We can 34983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith /// evaluate the expression regardless of what the RHS is, but C only allows 35083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith /// certain things in certain situations. 351c54061a679d8db4169438b2f97f81e3f443c6a25Benjamin Kramer struct EvalInfo { 352dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith ASTContext &Ctx; 353d411a4b23077b29e19c9371bbb19b054a374d922Argyrios Kyrtzidis 3541e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith /// EvalStatus - Contains information about the evaluation. 3551e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith Expr::EvalStatus &EvalStatus; 356f0c1e4b679e15c26bffb5892e35985bf3c52f77aAnders Carlsson 357d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith /// CurrentCall - The top of the constexpr call stack. 358bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith CallStackFrame *CurrentCall; 359d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 360d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith /// CallStackDepth - The number of calls in the call stack right now. 361d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith unsigned CallStackDepth; 362d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 36383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith /// NextCallIndex - The next call index to assign. 36483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith unsigned NextCallIndex; 36583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith 366bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith /// BottomFrame - The frame in which evaluation started. This must be 367745f5147e065900267c85a5568785a1991d4838fRichard Smith /// initialized after CurrentCall and CallStackDepth. 368bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith CallStackFrame BottomFrame; 369bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith 370180f47959a066795cc0f409433023af448bb0328Richard Smith /// EvaluatingDecl - This is the declaration whose initializer is being 371180f47959a066795cc0f409433023af448bb0328Richard Smith /// evaluated, if any. 372180f47959a066795cc0f409433023af448bb0328Richard Smith const VarDecl *EvaluatingDecl; 373180f47959a066795cc0f409433023af448bb0328Richard Smith 374180f47959a066795cc0f409433023af448bb0328Richard Smith /// EvaluatingDeclValue - This is the value being constructed for the 375180f47959a066795cc0f409433023af448bb0328Richard Smith /// declaration whose initializer is being evaluated, if any. 376180f47959a066795cc0f409433023af448bb0328Richard Smith APValue *EvaluatingDeclValue; 377180f47959a066795cc0f409433023af448bb0328Richard Smith 378c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith /// HasActiveDiagnostic - Was the previous diagnostic stored? If so, further 379c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith /// notes attached to it will also be stored, otherwise they will not be. 380c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith bool HasActiveDiagnostic; 381c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 382745f5147e065900267c85a5568785a1991d4838fRichard Smith /// CheckingPotentialConstantExpression - Are we checking whether the 383745f5147e065900267c85a5568785a1991d4838fRichard Smith /// expression is a potential constant expression? If so, some diagnostics 384745f5147e065900267c85a5568785a1991d4838fRichard Smith /// are suppressed. 385745f5147e065900267c85a5568785a1991d4838fRichard Smith bool CheckingPotentialConstantExpression; 386745f5147e065900267c85a5568785a1991d4838fRichard Smith 387bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith EvalInfo(const ASTContext &C, Expr::EvalStatus &S) 388dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith : Ctx(const_cast<ASTContext&>(C)), EvalStatus(S), CurrentCall(0), 38983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith CallStackDepth(0), NextCallIndex(1), 39083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith BottomFrame(*this, SourceLocation(), 0, 0, 0), 391745f5147e065900267c85a5568785a1991d4838fRichard Smith EvaluatingDecl(0), EvaluatingDeclValue(0), HasActiveDiagnostic(false), 392649dfbc389671d0c852ead5953da630d675a5d43Argyrios Kyrtzidis CheckingPotentialConstantExpression(false) {} 393bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith 394180f47959a066795cc0f409433023af448bb0328Richard Smith void setEvaluatingDecl(const VarDecl *VD, APValue &Value) { 395180f47959a066795cc0f409433023af448bb0328Richard Smith EvaluatingDecl = VD; 396180f47959a066795cc0f409433023af448bb0328Richard Smith EvaluatingDeclValue = &Value; 397180f47959a066795cc0f409433023af448bb0328Richard Smith } 398180f47959a066795cc0f409433023af448bb0328Richard Smith 3994e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie const LangOptions &getLangOpts() const { return Ctx.getLangOpts(); } 400c18c42345636e2866fed75c7e434fb659d747672Richard Smith 401c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith bool CheckCallLimit(SourceLocation Loc) { 402745f5147e065900267c85a5568785a1991d4838fRichard Smith // Don't perform any constexpr calls (other than the call we're checking) 403745f5147e065900267c85a5568785a1991d4838fRichard Smith // when checking a potential constant expression. 404745f5147e065900267c85a5568785a1991d4838fRichard Smith if (CheckingPotentialConstantExpression && CallStackDepth > 1) 405745f5147e065900267c85a5568785a1991d4838fRichard Smith return false; 40683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith if (NextCallIndex == 0) { 40783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith // NextCallIndex has wrapped around. 40883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith Diag(Loc, diag::note_constexpr_call_limit_exceeded); 40983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith return false; 41083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith } 411c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (CallStackDepth <= getLangOpts().ConstexprCallDepth) 412c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return true; 413c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Diag(Loc, diag::note_constexpr_depth_limit_exceeded) 414c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith << getLangOpts().ConstexprCallDepth; 415c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return false; 416c18c42345636e2866fed75c7e434fb659d747672Richard Smith } 417f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 41883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith CallStackFrame *getCallFrame(unsigned CallIndex) { 41983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith assert(CallIndex && "no call index in getCallFrame"); 42083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith // We will eventually hit BottomFrame, which has Index 1, so Frame can't 42183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith // be null in this loop. 42283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith CallStackFrame *Frame = CurrentCall; 42383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith while (Frame->Index > CallIndex) 42483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith Frame = Frame->Caller; 42583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith return (Frame->Index == CallIndex) ? Frame : 0; 42683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith } 42783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith 428c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith private: 429c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith /// Add a diagnostic to the diagnostics list. 430c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith PartialDiagnostic &addDiag(SourceLocation Loc, diag::kind DiagId) { 431c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith PartialDiagnostic PD(DiagId, Ctx.getDiagAllocator()); 432c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith EvalStatus.Diag->push_back(std::make_pair(Loc, PD)); 433c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return EvalStatus.Diag->back().second; 434c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } 435c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 43608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith /// Add notes containing a call stack to the current point of evaluation. 43708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith void addCallStack(unsigned Limit); 43808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith 439c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith public: 440f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith /// Diagnose that the evaluation cannot be folded. 4417098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith OptionalDiagnostic Diag(SourceLocation Loc, diag::kind DiagId 4427098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith = diag::note_invalid_subexpr_in_const_expr, 443c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith unsigned ExtraNotes = 0) { 444f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith // If we have a prior diagnostic, it will be noting that the expression 445f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith // isn't a constant expression. This diagnostic is more important. 446f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith // FIXME: We might want to show both diagnostics to the user. 447dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith if (EvalStatus.Diag) { 44808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith unsigned CallStackNotes = CallStackDepth - 1; 44908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith unsigned Limit = Ctx.getDiagnostics().getConstexprBacktraceLimit(); 45008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith if (Limit) 45108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith CallStackNotes = std::min(CallStackNotes, Limit + 1); 452745f5147e065900267c85a5568785a1991d4838fRichard Smith if (CheckingPotentialConstantExpression) 453745f5147e065900267c85a5568785a1991d4838fRichard Smith CallStackNotes = 0; 45408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith 455c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith HasActiveDiagnostic = true; 456dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith EvalStatus.Diag->clear(); 45708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith EvalStatus.Diag->reserve(1 + ExtraNotes + CallStackNotes); 45808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith addDiag(Loc, DiagId); 459745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!CheckingPotentialConstantExpression) 460745f5147e065900267c85a5568785a1991d4838fRichard Smith addCallStack(Limit); 46108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith return OptionalDiagnostic(&(*EvalStatus.Diag)[0].second); 462dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith } 463c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith HasActiveDiagnostic = false; 464dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith return OptionalDiagnostic(); 465dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith } 466dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith 4675cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith OptionalDiagnostic Diag(const Expr *E, diag::kind DiagId 4685cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith = diag::note_invalid_subexpr_in_const_expr, 4695cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith unsigned ExtraNotes = 0) { 4705cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith if (EvalStatus.Diag) 4715cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith return Diag(E->getExprLoc(), DiagId, ExtraNotes); 4725cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith HasActiveDiagnostic = false; 4735cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith return OptionalDiagnostic(); 4745cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith } 4755cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith 476dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith /// Diagnose that the evaluation does not produce a C++11 core constant 477dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith /// expression. 4785cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith template<typename LocArg> 4795cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith OptionalDiagnostic CCEDiag(LocArg Loc, diag::kind DiagId 4807098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith = diag::note_invalid_subexpr_in_const_expr, 481c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith unsigned ExtraNotes = 0) { 482dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith // Don't override a previous diagnostic. 48351e47df5a57430f1b691b04258e663cce68aef9dEli Friedman if (!EvalStatus.Diag || !EvalStatus.Diag->empty()) { 48451e47df5a57430f1b691b04258e663cce68aef9dEli Friedman HasActiveDiagnostic = false; 485dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith return OptionalDiagnostic(); 48651e47df5a57430f1b691b04258e663cce68aef9dEli Friedman } 487c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return Diag(Loc, DiagId, ExtraNotes); 488c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } 489c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 490c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith /// Add a note to a prior diagnostic. 491c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith OptionalDiagnostic Note(SourceLocation Loc, diag::kind DiagId) { 492c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (!HasActiveDiagnostic) 493c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return OptionalDiagnostic(); 494c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return OptionalDiagnostic(&addDiag(Loc, DiagId)); 495f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 496099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith 497099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith /// Add a stack of notes to a prior diagnostic. 498099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith void addNotes(ArrayRef<PartialDiagnosticAt> Diags) { 499099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith if (HasActiveDiagnostic) { 500099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith EvalStatus.Diag->insert(EvalStatus.Diag->end(), 501099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Diags.begin(), Diags.end()); 502099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith } 503099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith } 504745f5147e065900267c85a5568785a1991d4838fRichard Smith 505745f5147e065900267c85a5568785a1991d4838fRichard Smith /// Should we continue evaluation as much as possible after encountering a 506745f5147e065900267c85a5568785a1991d4838fRichard Smith /// construct which can't be folded? 507745f5147e065900267c85a5568785a1991d4838fRichard Smith bool keepEvaluatingAfterFailure() { 50874e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith return CheckingPotentialConstantExpression && 50974e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith EvalStatus.Diag && EvalStatus.Diag->empty(); 510745f5147e065900267c85a5568785a1991d4838fRichard Smith } 511c54061a679d8db4169438b2f97f81e3f443c6a25Benjamin Kramer }; 512f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 513f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith /// Object used to treat all foldable expressions as constant expressions. 514f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith struct FoldConstant { 515f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith bool Enabled; 516f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 517f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith explicit FoldConstant(EvalInfo &Info) 518f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith : Enabled(Info.EvalStatus.Diag && Info.EvalStatus.Diag->empty() && 519f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith !Info.EvalStatus.HasSideEffects) { 520f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith } 521f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // Treat the value we've computed since this object was created as constant. 522f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith void Fold(EvalInfo &Info) { 523f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (Enabled && !Info.EvalStatus.Diag->empty() && 524f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith !Info.EvalStatus.HasSideEffects) 525f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith Info.EvalStatus.Diag->clear(); 526f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith } 527f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith }; 52874e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith 52974e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith /// RAII object used to suppress diagnostics and side-effects from a 53074e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith /// speculative evaluation. 53174e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith class SpeculativeEvaluationRAII { 53274e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith EvalInfo &Info; 53374e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith Expr::EvalStatus Old; 53474e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith 53574e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith public: 53674e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith SpeculativeEvaluationRAII(EvalInfo &Info, 53774e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith llvm::SmallVectorImpl<PartialDiagnosticAt> 53874e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith *NewDiag = 0) 53974e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith : Info(Info), Old(Info.EvalStatus) { 54074e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith Info.EvalStatus.Diag = NewDiag; 54174e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith } 54274e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith ~SpeculativeEvaluationRAII() { 54374e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith Info.EvalStatus = Old; 54474e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith } 54574e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith }; 54608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith} 54708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith 548b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithbool SubobjectDesignator::checkSubobject(EvalInfo &Info, const Expr *E, 549b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith CheckSubobjectKind CSK) { 550b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (Invalid) 551b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return false; 552b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (isOnePastTheEnd()) { 5535cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.CCEDiag(E, diag::note_constexpr_past_end_subobject) 554b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith << CSK; 555b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith setInvalid(); 556b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return false; 557b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } 558b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return true; 559b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith} 560b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 561b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithvoid SubobjectDesignator::diagnosePointerArithmetic(EvalInfo &Info, 562b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith const Expr *E, uint64_t N) { 563b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (MostDerivedPathLength == Entries.size() && MostDerivedArraySize) 5645cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.CCEDiag(E, diag::note_constexpr_array_index) 565b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith << static_cast<int>(N) << /*array*/ 0 566b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith << static_cast<unsigned>(MostDerivedArraySize); 567b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith else 5685cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.CCEDiag(E, diag::note_constexpr_array_index) 569b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith << static_cast<int>(N) << /*non-array*/ 1; 570b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith setInvalid(); 571b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith} 572b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 57308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard SmithCallStackFrame::CallStackFrame(EvalInfo &Info, SourceLocation CallLoc, 57408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith const FunctionDecl *Callee, const LValue *This, 5751aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith const APValue *Arguments) 57608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith : Info(Info), Caller(Info.CurrentCall), CallLoc(CallLoc), Callee(Callee), 57783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith Index(Info.NextCallIndex++), This(This), Arguments(Arguments) { 57808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith Info.CurrentCall = this; 57908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith ++Info.CallStackDepth; 58008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith} 58108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith 58208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard SmithCallStackFrame::~CallStackFrame() { 58308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith assert(Info.CurrentCall == this && "calls retired out of order"); 58408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith --Info.CallStackDepth; 58508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith Info.CurrentCall = Caller; 58608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith} 58787eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner 58808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith/// Produce a string describing the given constexpr call. 58908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smithstatic void describeCall(CallStackFrame *Frame, llvm::raw_ostream &Out) { 59008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith unsigned ArgIndex = 0; 59108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith bool IsMemberCall = isa<CXXMethodDecl>(Frame->Callee) && 5925ba73e1af8ef519161bd40063dc325457e21676aRichard Smith !isa<CXXConstructorDecl>(Frame->Callee) && 5935ba73e1af8ef519161bd40063dc325457e21676aRichard Smith cast<CXXMethodDecl>(Frame->Callee)->isInstance(); 59408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith 59508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith if (!IsMemberCall) 59608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith Out << *Frame->Callee << '('; 59708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith 59808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith for (FunctionDecl::param_const_iterator I = Frame->Callee->param_begin(), 59908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith E = Frame->Callee->param_end(); I != E; ++I, ++ArgIndex) { 6005fe31228c883040cf016cfc71ad4bfeba462602eNAKAMURA Takumi if (ArgIndex > (unsigned)IsMemberCall) 60108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith Out << ", "; 60208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith 60308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith const ParmVarDecl *Param = *I; 6041aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith const APValue &Arg = Frame->Arguments[ArgIndex]; 6051aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith Arg.printPretty(Out, Frame->Info.Ctx, Param->getType()); 60608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith 60708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith if (ArgIndex == 0 && IsMemberCall) 60808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith Out << "->" << *Frame->Callee << '('; 609bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith } 610d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 61108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith Out << ')'; 61208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith} 61308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith 61408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smithvoid EvalInfo::addCallStack(unsigned Limit) { 61508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith // Determine which calls to skip, if any. 61608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith unsigned ActiveCalls = CallStackDepth - 1; 61708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith unsigned SkipStart = ActiveCalls, SkipEnd = SkipStart; 61808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith if (Limit && Limit < ActiveCalls) { 61908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith SkipStart = Limit / 2 + Limit % 2; 62008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith SkipEnd = ActiveCalls - Limit / 2; 62108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith } 62208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith 62308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith // Walk the call stack and add the diagnostics. 62408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith unsigned CallIdx = 0; 62508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith for (CallStackFrame *Frame = CurrentCall; Frame != &BottomFrame; 62608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith Frame = Frame->Caller, ++CallIdx) { 62708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith // Skip this call? 62808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith if (CallIdx >= SkipStart && CallIdx < SkipEnd) { 62908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith if (CallIdx == SkipStart) { 63008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith // Note that we're skipping calls. 63108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith addDiag(Frame->CallLoc, diag::note_constexpr_calls_suppressed) 63208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith << unsigned(ActiveCalls - Limit); 63308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith } 63408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith continue; 63508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith } 63608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith 63708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith llvm::SmallVector<char, 128> Buffer; 63808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith llvm::raw_svector_ostream Out(Buffer); 63908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith describeCall(Frame, Out); 64008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith addDiag(Frame->CallLoc, diag::note_constexpr_call_here) << Out.str(); 641bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith } 64208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith} 643d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 64408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smithnamespace { 645f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall struct ComplexValue { 646f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall private: 647f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall bool IsInt; 648f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall 649f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall public: 650f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall APSInt IntReal, IntImag; 651f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall APFloat FloatReal, FloatImag; 652f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall 653f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall ComplexValue() : FloatReal(APFloat::Bogus), FloatImag(APFloat::Bogus) {} 654f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall 655f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall void makeComplexFloat() { IsInt = false; } 656f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall bool isComplexFloat() const { return !IsInt; } 657f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall APFloat &getComplexFloatReal() { return FloatReal; } 658f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall APFloat &getComplexFloatImag() { return FloatImag; } 659f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall 660f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall void makeComplexInt() { IsInt = true; } 661f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall bool isComplexInt() const { return IsInt; } 662f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall APSInt &getComplexIntReal() { return IntReal; } 663f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall APSInt &getComplexIntImag() { return IntImag; } 664f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall 6651aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith void moveInto(APValue &v) const { 666f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall if (isComplexFloat()) 6671aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith v = APValue(FloatReal, FloatImag); 668f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall else 6691aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith v = APValue(IntReal, IntImag); 670f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall } 6711aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith void setFrom(const APValue &v) { 67256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall assert(v.isComplexFloat() || v.isComplexInt()); 67356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall if (v.isComplexFloat()) { 67456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall makeComplexFloat(); 67556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall FloatReal = v.getComplexFloatReal(); 67656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall FloatImag = v.getComplexFloatImag(); 67756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall } else { 67856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall makeComplexInt(); 67956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall IntReal = v.getComplexIntReal(); 68056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall IntImag = v.getComplexIntImag(); 68156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall } 68256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall } 683f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall }; 684efdb83e26f9a1fd2566afe54461216cd84814d42John McCall 685efdb83e26f9a1fd2566afe54461216cd84814d42John McCall struct LValue { 6861bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith APValue::LValueBase Base; 687efdb83e26f9a1fd2566afe54461216cd84814d42John McCall CharUnits Offset; 68883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith unsigned CallIndex; 6890a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith SubobjectDesignator Designator; 690efdb83e26f9a1fd2566afe54461216cd84814d42John McCall 6911bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith const APValue::LValueBase getLValueBase() const { return Base; } 69247a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith CharUnits &getLValueOffset() { return Offset; } 693625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith const CharUnits &getLValueOffset() const { return Offset; } 69483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith unsigned getLValueCallIndex() const { return CallIndex; } 6950a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith SubobjectDesignator &getLValueDesignator() { return Designator; } 6960a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith const SubobjectDesignator &getLValueDesignator() const { return Designator;} 697efdb83e26f9a1fd2566afe54461216cd84814d42John McCall 6981aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith void moveInto(APValue &V) const { 6991aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith if (Designator.Invalid) 7001aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith V = APValue(Base, Offset, APValue::NoLValuePath(), CallIndex); 7011aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith else 7021aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith V = APValue(Base, Offset, Designator.Entries, 7031aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith Designator.IsOnePastTheEnd, CallIndex); 704efdb83e26f9a1fd2566afe54461216cd84814d42John McCall } 7051aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith void setFrom(ASTContext &Ctx, const APValue &V) { 70647a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith assert(V.isLValue()); 70747a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith Base = V.getLValueBase(); 70847a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith Offset = V.getLValueOffset(); 70983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith CallIndex = V.getLValueCallIndex(); 7101aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith Designator = SubobjectDesignator(Ctx, V); 7110a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith } 7120a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith 71383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith void set(APValue::LValueBase B, unsigned I = 0) { 7141bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith Base = B; 7150a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith Offset = CharUnits::Zero(); 71683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith CallIndex = I; 717b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Designator = SubobjectDesignator(getType(B)); 718b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } 719b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 720b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // Check that this LValue is not based on a null pointer. If it is, produce 721b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // a diagnostic and mark the designator as invalid. 722b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith bool checkNullPointer(EvalInfo &Info, const Expr *E, 723b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith CheckSubobjectKind CSK) { 724b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (Designator.Invalid) 725b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return false; 726b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (!Base) { 7275cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.CCEDiag(E, diag::note_constexpr_null_subobject) 728b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith << CSK; 729b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Designator.setInvalid(); 730b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return false; 731b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } 732b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return true; 733b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } 734b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 735b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // Check this LValue refers to an object. If not, set the designator to be 736b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // invalid and emit a diagnostic. 737b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK) { 7385cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith // Outside C++11, do not build a designator referring to a subobject of 7395cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith // any object: we won't use such a designator for anything. 7405cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith if (!Info.getLangOpts().CPlusPlus0x) 7415cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Designator.setInvalid(); 742b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return checkNullPointer(Info, E, CSK) && 743b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Designator.checkSubobject(Info, E, CSK); 744b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } 745b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 746b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith void addDecl(EvalInfo &Info, const Expr *E, 747b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith const Decl *D, bool Virtual = false) { 7485cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith if (checkSubobject(Info, E, isa<FieldDecl>(D) ? CSK_Field : CSK_Base)) 7495cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Designator.addDeclUnchecked(D, Virtual); 750b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } 751b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith void addArray(EvalInfo &Info, const Expr *E, const ConstantArrayType *CAT) { 7525cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith if (checkSubobject(Info, E, CSK_ArrayToPointer)) 7535cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Designator.addArrayUnchecked(CAT); 754b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } 75586024013d4c3728122c58fa07a2a67e6c15837efRichard Smith void addComplex(EvalInfo &Info, const Expr *E, QualType EltTy, bool Imag) { 7565cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith if (checkSubobject(Info, E, Imag ? CSK_Imag : CSK_Real)) 7575cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Designator.addComplexUnchecked(EltTy, Imag); 75886024013d4c3728122c58fa07a2a67e6c15837efRichard Smith } 759b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith void adjustIndex(EvalInfo &Info, const Expr *E, uint64_t N) { 7605cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith if (checkNullPointer(Info, E, CSK_ArrayIndex)) 7615cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Designator.adjustIndex(Info, E, N); 76256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall } 763efdb83e26f9a1fd2566afe54461216cd84814d42John McCall }; 764e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 765e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith struct MemberPtr { 766e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith MemberPtr() {} 767e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith explicit MemberPtr(const ValueDecl *Decl) : 768e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith DeclAndIsDerivedMember(Decl, false), Path() {} 769e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 770e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith /// The member or (direct or indirect) field referred to by this member 771e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith /// pointer, or 0 if this is a null member pointer. 772e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const ValueDecl *getDecl() const { 773e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return DeclAndIsDerivedMember.getPointer(); 774e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 775e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith /// Is this actually a member of some type derived from the relevant class? 776e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool isDerivedMember() const { 777e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return DeclAndIsDerivedMember.getInt(); 778e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 779e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith /// Get the class which the declaration actually lives in. 780e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const CXXRecordDecl *getContainingRecord() const { 781e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return cast<CXXRecordDecl>( 782e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith DeclAndIsDerivedMember.getPointer()->getDeclContext()); 783e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 784e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 7851aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith void moveInto(APValue &V) const { 7861aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith V = APValue(getDecl(), isDerivedMember(), Path); 787e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 7881aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith void setFrom(const APValue &V) { 789e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith assert(V.isMemberPointer()); 790e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith DeclAndIsDerivedMember.setPointer(V.getMemberPointerDecl()); 791e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith DeclAndIsDerivedMember.setInt(V.isMemberPointerToDerivedMember()); 792e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Path.clear(); 793e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith ArrayRef<const CXXRecordDecl*> P = V.getMemberPointerPath(); 794e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Path.insert(Path.end(), P.begin(), P.end()); 795e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 796e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 797e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith /// DeclAndIsDerivedMember - The member declaration, and a flag indicating 798e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith /// whether the member is a member of some class derived from the class type 799e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith /// of the member pointer. 800e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith llvm::PointerIntPair<const ValueDecl*, 1, bool> DeclAndIsDerivedMember; 801e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith /// Path - The path of base/derived classes from the member declaration's 802e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith /// class (exclusive) to the class type of the member pointer (inclusive). 803e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith SmallVector<const CXXRecordDecl*, 4> Path; 804e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 805e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith /// Perform a cast towards the class of the Decl (either up or down the 806e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith /// hierarchy). 807e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool castBack(const CXXRecordDecl *Class) { 808e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith assert(!Path.empty()); 809e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const CXXRecordDecl *Expected; 810e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (Path.size() >= 2) 811e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Expected = Path[Path.size() - 2]; 812e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith else 813e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Expected = getContainingRecord(); 814e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (Expected->getCanonicalDecl() != Class->getCanonicalDecl()) { 815e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // C++11 [expr.static.cast]p12: In a conversion from (D::*) to (B::*), 816e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // if B does not contain the original member and is not a base or 817e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // derived class of the class containing the original member, the result 818e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // of the cast is undefined. 819e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // C++11 [conv.mem]p2 does not cover this case for a cast from (B::*) to 820e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // (D::*). We consider that to be a language defect. 821e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 822e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 823e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Path.pop_back(); 824e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 825e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 826e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith /// Perform a base-to-derived member pointer cast. 827e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool castToDerived(const CXXRecordDecl *Derived) { 828e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!getDecl()) 829e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 830e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!isDerivedMember()) { 831e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Path.push_back(Derived); 832e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 833e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 834e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!castBack(Derived)) 835e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 836e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (Path.empty()) 837e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith DeclAndIsDerivedMember.setInt(false); 838e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 839e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 840e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith /// Perform a derived-to-base member pointer cast. 841e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool castToBase(const CXXRecordDecl *Base) { 842e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!getDecl()) 843e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 844e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (Path.empty()) 845e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith DeclAndIsDerivedMember.setInt(true); 846e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (isDerivedMember()) { 847e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Path.push_back(Base); 848e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 849e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 850e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return castBack(Base); 851e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 852e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith }; 853c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 854b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith /// Compare two member pointers, which are assumed to be of the same type. 855b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith static bool operator==(const MemberPtr &LHS, const MemberPtr &RHS) { 856b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith if (!LHS.getDecl() || !RHS.getDecl()) 857b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith return !LHS.getDecl() && !RHS.getDecl(); 858b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith if (LHS.getDecl()->getCanonicalDecl() != RHS.getDecl()->getCanonicalDecl()) 859b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith return false; 860b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith return LHS.Path == RHS.Path; 861b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith } 862b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith 863c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith /// Kinds of constant expression checking, for diagnostics. 864c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith enum CheckConstantExpressionKind { 865c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith CCEK_Constant, ///< A normal constant. 866c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith CCEK_ReturnValue, ///< A constexpr function return value. 867c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith CCEK_MemberInit ///< A constexpr constructor mem-initializer. 868c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 869f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall} 87087eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner 8711aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smithstatic bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E); 87283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smithstatic bool EvaluateInPlace(APValue &Result, EvalInfo &Info, 87383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith const LValue &This, const Expr *E, 87483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith CheckConstantExpressionKind CCEK = CCEK_Constant, 87583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith bool AllowNonLiteralTypes = false); 876efdb83e26f9a1fd2566afe54461216cd84814d42John McCallstatic bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info); 877efdb83e26f9a1fd2566afe54461216cd84814d42John McCallstatic bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info); 878e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, 879e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith EvalInfo &Info); 880e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info); 88187eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattnerstatic bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info); 8821aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smithstatic bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, 883d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner EvalInfo &Info); 884d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmanstatic bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info); 885f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCallstatic bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info); 886f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner 887f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===// 8884efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman// Misc utilities 8894efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman//===----------------------------------------------------------------------===// 8904efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 891180f47959a066795cc0f409433023af448bb0328Richard Smith/// Should this call expression be treated as a string literal? 892180f47959a066795cc0f409433023af448bb0328Richard Smithstatic bool IsStringLiteralCall(const CallExpr *E) { 893180f47959a066795cc0f409433023af448bb0328Richard Smith unsigned Builtin = E->isBuiltinCall(); 894180f47959a066795cc0f409433023af448bb0328Richard Smith return (Builtin == Builtin::BI__builtin___CFStringMakeConstantString || 895180f47959a066795cc0f409433023af448bb0328Richard Smith Builtin == Builtin::BI__builtin___NSStringMakeConstantString); 896180f47959a066795cc0f409433023af448bb0328Richard Smith} 897180f47959a066795cc0f409433023af448bb0328Richard Smith 8981bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smithstatic bool IsGlobalLValue(APValue::LValueBase B) { 899180f47959a066795cc0f409433023af448bb0328Richard Smith // C++11 [expr.const]p3 An address constant expression is a prvalue core 900180f47959a066795cc0f409433023af448bb0328Richard Smith // constant expression of pointer type that evaluates to... 901180f47959a066795cc0f409433023af448bb0328Richard Smith 902180f47959a066795cc0f409433023af448bb0328Richard Smith // ... a null pointer value, or a prvalue core constant expression of type 903180f47959a066795cc0f409433023af448bb0328Richard Smith // std::nullptr_t. 9041bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith if (!B) return true; 90542c8f87eb60958170c46767273bf93e6c96125bfJohn McCall 9061bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { 907180f47959a066795cc0f409433023af448bb0328Richard Smith // ... the address of an object with static storage duration, 9081bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith if (const VarDecl *VD = dyn_cast<VarDecl>(D)) 90942c8f87eb60958170c46767273bf93e6c96125bfJohn McCall return VD->hasGlobalStorage(); 9101bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith // ... the address of a function, 9111bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith return isa<FunctionDecl>(D); 91242c8f87eb60958170c46767273bf93e6c96125bfJohn McCall } 9131bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith 9141bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith const Expr *E = B.get<const Expr*>(); 9151bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith switch (E->getStmtClass()) { 9161bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith default: 9171bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith return false; 918b78ae9716576399145786b93f687943f8b197170Richard Smith case Expr::CompoundLiteralExprClass: { 919b78ae9716576399145786b93f687943f8b197170Richard Smith const CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E); 920b78ae9716576399145786b93f687943f8b197170Richard Smith return CLE->isFileScope() && CLE->isLValue(); 921b78ae9716576399145786b93f687943f8b197170Richard Smith } 922180f47959a066795cc0f409433023af448bb0328Richard Smith // A string literal has static storage duration. 923180f47959a066795cc0f409433023af448bb0328Richard Smith case Expr::StringLiteralClass: 924180f47959a066795cc0f409433023af448bb0328Richard Smith case Expr::PredefinedExprClass: 925180f47959a066795cc0f409433023af448bb0328Richard Smith case Expr::ObjCStringLiteralClass: 926180f47959a066795cc0f409433023af448bb0328Richard Smith case Expr::ObjCEncodeExprClass: 92747d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith case Expr::CXXTypeidExprClass: 928e275a1845b9e32bd3034f2593dee1780855c8fd6Francois Pichet case Expr::CXXUuidofExprClass: 929180f47959a066795cc0f409433023af448bb0328Richard Smith return true; 930180f47959a066795cc0f409433023af448bb0328Richard Smith case Expr::CallExprClass: 931180f47959a066795cc0f409433023af448bb0328Richard Smith return IsStringLiteralCall(cast<CallExpr>(E)); 932180f47959a066795cc0f409433023af448bb0328Richard Smith // For GCC compatibility, &&label has static storage duration. 933180f47959a066795cc0f409433023af448bb0328Richard Smith case Expr::AddrLabelExprClass: 934180f47959a066795cc0f409433023af448bb0328Richard Smith return true; 935180f47959a066795cc0f409433023af448bb0328Richard Smith // A Block literal expression may be used as the initialization value for 936180f47959a066795cc0f409433023af448bb0328Richard Smith // Block variables at global or local static scope. 937180f47959a066795cc0f409433023af448bb0328Richard Smith case Expr::BlockExprClass: 938180f47959a066795cc0f409433023af448bb0328Richard Smith return !cast<BlockExpr>(E)->getBlockDecl()->hasCaptures(); 939745f5147e065900267c85a5568785a1991d4838fRichard Smith case Expr::ImplicitValueInitExprClass: 940745f5147e065900267c85a5568785a1991d4838fRichard Smith // FIXME: 941745f5147e065900267c85a5568785a1991d4838fRichard Smith // We can never form an lvalue with an implicit value initialization as its 942745f5147e065900267c85a5568785a1991d4838fRichard Smith // base through expression evaluation, so these only appear in one case: the 943745f5147e065900267c85a5568785a1991d4838fRichard Smith // implicit variable declaration we invent when checking whether a constexpr 944745f5147e065900267c85a5568785a1991d4838fRichard Smith // constructor can produce a constant expression. We must assume that such 945745f5147e065900267c85a5568785a1991d4838fRichard Smith // an expression might be a global lvalue. 946745f5147e065900267c85a5568785a1991d4838fRichard Smith return true; 947180f47959a066795cc0f409433023af448bb0328Richard Smith } 94842c8f87eb60958170c46767273bf93e6c96125bfJohn McCall} 94942c8f87eb60958170c46767273bf93e6c96125bfJohn McCall 95083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smithstatic void NoteLValueLocation(EvalInfo &Info, APValue::LValueBase Base) { 95183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith assert(Base && "no location for a null lvalue"); 95283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); 95383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith if (VD) 95483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith Info.Note(VD->getLocation(), diag::note_declared_at); 95583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith else 95683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith Info.Note(Base.dyn_cast<const Expr*>()->getExprLoc(), 95783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith diag::note_constexpr_temporary_here); 95883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith} 95983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith 9609a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith/// Check that this reference or pointer core constant expression is a valid 9611aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith/// value for an address or reference constant expression. Return true if we 9621aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith/// can fold this expression, whether or not it's a constant expression. 96383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smithstatic bool CheckLValueConstantExpression(EvalInfo &Info, SourceLocation Loc, 96483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith QualType Type, const LValue &LVal) { 96583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith bool IsReferenceType = Type->isReferenceType(); 96683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith 967c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith APValue::LValueBase Base = LVal.getLValueBase(); 968c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const SubobjectDesignator &Designator = LVal.getLValueDesignator(); 969c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 970b78ae9716576399145786b93f687943f8b197170Richard Smith // Check that the object is a global. Note that the fake 'this' object we 971b78ae9716576399145786b93f687943f8b197170Richard Smith // manufacture when checking potential constant expressions is conservatively 972b78ae9716576399145786b93f687943f8b197170Richard Smith // assumed to be global here. 973c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (!IsGlobalLValue(Base)) { 974c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (Info.getLangOpts().CPlusPlus0x) { 975c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); 97683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith Info.Diag(Loc, diag::note_constexpr_non_global, 1) 97783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith << IsReferenceType << !Designator.Entries.empty() 97883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith << !!VD << VD; 97983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith NoteLValueLocation(Info, Base); 980c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } else { 98183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith Info.Diag(Loc); 982c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } 98361e616206413d1779c7545c7a8ad1ce1129ad9c1Richard Smith // Don't allow references to temporaries to escape. 98469c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith return false; 985f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 98683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith assert((Info.CheckingPotentialConstantExpression || 98783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith LVal.getLValueCallIndex() == 0) && 98883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith "have call index for global lvalue"); 989b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 990b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // Allow address constant expressions to be past-the-end pointers. This is 991b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // an extension: the standard requires them to point to an object. 992b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (!IsReferenceType) 993b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return true; 994b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 995b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // A reference constant expression must refer to an object. 996b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (!Base) { 997b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // FIXME: diagnostic 99883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith Info.CCEDiag(Loc); 99961e616206413d1779c7545c7a8ad1ce1129ad9c1Richard Smith return true; 1000b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } 1001b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 1002c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith // Does this refer one past the end of some object? 1003b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (Designator.isOnePastTheEnd()) { 1004c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); 100583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith Info.Diag(Loc, diag::note_constexpr_past_end, 1) 1006c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith << !Designator.Entries.empty() << !!VD << VD; 100783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith NoteLValueLocation(Info, Base); 1008c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } 1009c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 101069c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith return true; 101147a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith} 101247a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith 101351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// Check that this core constant expression is of literal type, and if not, 101451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// produce an appropriate diagnostic. 101551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smithstatic bool CheckLiteralType(EvalInfo &Info, const Expr *E) { 101651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (!E->isRValue() || E->getType()->isLiteralType()) 101751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return true; 101851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 101951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith // Prvalue constant expressions must be of literal types. 102051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (Info.getLangOpts().CPlusPlus0x) 10215cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.Diag(E, diag::note_constexpr_nonliteral) 102251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith << E->getType(); 102351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith else 10245cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); 102551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return false; 102651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith} 102751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 10289a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith/// Check that this core constant expression value is a valid value for a 102983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith/// constant expression. If not, report an appropriate diagnostic. Does not 103083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith/// check that the expression is of literal type. 103183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smithstatic bool CheckConstantExpression(EvalInfo &Info, SourceLocation DiagLoc, 103283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith QualType Type, const APValue &Value) { 103383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith // Core issue 1454: For a literal constant expression of array or class type, 103483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith // each subobject of its value shall have been initialized by a constant 103583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith // expression. 103683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith if (Value.isArray()) { 103783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith QualType EltTy = Type->castAsArrayTypeUnsafe()->getElementType(); 103883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith for (unsigned I = 0, N = Value.getArrayInitializedElts(); I != N; ++I) { 103983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith if (!CheckConstantExpression(Info, DiagLoc, EltTy, 104083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith Value.getArrayInitializedElt(I))) 104183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith return false; 104283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith } 104383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith if (!Value.hasArrayFiller()) 104483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith return true; 104583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith return CheckConstantExpression(Info, DiagLoc, EltTy, 104683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith Value.getArrayFiller()); 104783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith } 104883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith if (Value.isUnion() && Value.getUnionField()) { 104983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith return CheckConstantExpression(Info, DiagLoc, 105083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith Value.getUnionField()->getType(), 105183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith Value.getUnionValue()); 105283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith } 105383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith if (Value.isStruct()) { 105483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith RecordDecl *RD = Type->castAs<RecordType>()->getDecl(); 105583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD)) { 105683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith unsigned BaseIndex = 0; 105783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(), 105883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith End = CD->bases_end(); I != End; ++I, ++BaseIndex) { 105983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith if (!CheckConstantExpression(Info, DiagLoc, I->getType(), 106083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith Value.getStructBase(BaseIndex))) 106183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith return false; 106283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith } 106383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith } 106483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); 106583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith I != E; ++I) { 1066262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie if (!CheckConstantExpression(Info, DiagLoc, I->getType(), 1067262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie Value.getStructField(I->getFieldIndex()))) 106883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith return false; 106983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith } 107083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith } 107183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith 107283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith if (Value.isLValue()) { 107383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith LValue LVal; 10741aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith LVal.setFrom(Info.Ctx, Value); 107583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith return CheckLValueConstantExpression(Info, DiagLoc, Type, LVal); 10769a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith } 107783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith 107883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith // Everything else is fine. 107983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith return true; 10809a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith} 10819a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith 10829e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smithconst ValueDecl *GetLValueBaseDecl(const LValue &LVal) { 10831bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith return LVal.Base.dyn_cast<const ValueDecl*>(); 10849e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith} 10859e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith 10869e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smithstatic bool IsLiteralLValue(const LValue &Value) { 108783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith return Value.Base.dyn_cast<const Expr*>() && !Value.CallIndex; 10889e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith} 10899e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith 109065ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smithstatic bool IsWeakLValue(const LValue &Value) { 109165ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith const ValueDecl *Decl = GetLValueBaseDecl(Value); 10920dd7a25e8d679de1dc0ce788222d6dee0e879885Lang Hames return Decl && Decl->isWeak(); 109365ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith} 109465ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith 10951aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smithstatic bool EvalPointerValueAsBool(const APValue &Value, bool &Result) { 10963554283157190e67918fad4221a5e6faf9317362John McCall // A null base expression indicates a null pointer. These are always 10973554283157190e67918fad4221a5e6faf9317362John McCall // evaluatable, and they are false unless the offset is zero. 1098e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!Value.getLValueBase()) { 1099e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Result = !Value.getLValueOffset().isZero(); 11003554283157190e67918fad4221a5e6faf9317362John McCall return true; 11013554283157190e67918fad4221a5e6faf9317362John McCall } 11023554283157190e67918fad4221a5e6faf9317362John McCall 1103e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // We have a non-null base. These are generally known to be true, but if it's 1104e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // a weak declaration it can be null at runtime. 11053554283157190e67918fad4221a5e6faf9317362John McCall Result = true; 1106e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const ValueDecl *Decl = Value.getLValueBase().dyn_cast<const ValueDecl*>(); 11070dd7a25e8d679de1dc0ce788222d6dee0e879885Lang Hames return !Decl || !Decl->isWeak(); 11085bc86103767c2abcbfdd6518e0ccbbbb6aa59e0fEli Friedman} 11095bc86103767c2abcbfdd6518e0ccbbbb6aa59e0fEli Friedman 11101aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smithstatic bool HandleConversionToBool(const APValue &Val, bool &Result) { 1111c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith switch (Val.getKind()) { 1112c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case APValue::Uninitialized: 1113c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return false; 1114c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case APValue::Int: 1115c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith Result = Val.getInt().getBoolValue(); 111641bf4f38348561a0f12c10d34f1673cd19a6eb04Richard Smith return true; 1117c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case APValue::Float: 1118c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith Result = !Val.getFloat().isZero(); 1119a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman return true; 1120c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case APValue::ComplexInt: 1121c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith Result = Val.getComplexIntReal().getBoolValue() || 1122c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith Val.getComplexIntImag().getBoolValue(); 1123c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return true; 1124c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case APValue::ComplexFloat: 1125c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith Result = !Val.getComplexFloatReal().isZero() || 1126c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith !Val.getComplexFloatImag().isZero(); 1127436c8898cd1c93c5bacd3fcc4ac586bc5cd77062Richard Smith return true; 1128e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case APValue::LValue: 1129e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return EvalPointerValueAsBool(Val, Result); 1130e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case APValue::MemberPointer: 1131e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Result = Val.getMemberPointerDecl(); 1132e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 1133c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case APValue::Vector: 1134cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith case APValue::Array: 1135180f47959a066795cc0f409433023af448bb0328Richard Smith case APValue::Struct: 1136180f47959a066795cc0f409433023af448bb0328Richard Smith case APValue::Union: 113765639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman case APValue::AddrLabelDiff: 1138c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return false; 11394efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman } 11404efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 1141c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith llvm_unreachable("unknown APValue kind"); 1142c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith} 1143c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 1144c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smithstatic bool EvaluateAsBooleanCondition(const Expr *E, bool &Result, 1145c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith EvalInfo &Info) { 1146c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith assert(E->isRValue() && "missing lvalue-to-rvalue conv in bool condition"); 11471aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith APValue Val; 1148d411a4b23077b29e19c9371bbb19b054a374d922Argyrios Kyrtzidis if (!Evaluate(Val, Info, E)) 1149c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return false; 1150d411a4b23077b29e19c9371bbb19b054a374d922Argyrios Kyrtzidis return HandleConversionToBool(Val, Result); 11514efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman} 11524efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 1153c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithtemplate<typename T> 115426dc97cbeba8ced19972a259720a71aefa01ef43Eli Friedmanstatic void HandleOverflow(EvalInfo &Info, const Expr *E, 1155c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const T &SrcValue, QualType DestType) { 115626dc97cbeba8ced19972a259720a71aefa01ef43Eli Friedman Info.CCEDiag(E, diag::note_constexpr_overflow) 1157789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith << SrcValue << DestType; 1158c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith} 1159c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 1160c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstatic bool HandleFloatToIntCast(EvalInfo &Info, const Expr *E, 1161c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith QualType SrcType, const APFloat &Value, 1162c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith QualType DestType, APSInt &Result) { 1163c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith unsigned DestWidth = Info.Ctx.getIntWidth(DestType); 1164a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar // Determine whether we are converting to unsigned or signed. 1165575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor bool DestSigned = DestType->isSignedIntegerOrEnumerationType(); 11661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1167c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Result = APSInt(DestWidth, !DestSigned); 1168a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar bool ignored; 1169c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (Value.convertToInteger(Result, llvm::APFloat::rmTowardZero, &ignored) 1170c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith & APFloat::opInvalidOp) 117126dc97cbeba8ced19972a259720a71aefa01ef43Eli Friedman HandleOverflow(Info, E, Value, DestType); 1172c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return true; 1173a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar} 1174a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar 1175c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstatic bool HandleFloatToFloatCast(EvalInfo &Info, const Expr *E, 1176c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith QualType SrcType, QualType DestType, 1177c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith APFloat &Result) { 1178c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith APFloat Value = Result; 1179a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar bool ignored; 1180c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (Result.convert(Info.Ctx.getFloatTypeSemantics(DestType), 1181c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith APFloat::rmNearestTiesToEven, &ignored) 1182c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith & APFloat::opOverflow) 118326dc97cbeba8ced19972a259720a71aefa01ef43Eli Friedman HandleOverflow(Info, E, Value, DestType); 1184c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return true; 1185a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar} 1186a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar 1187f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smithstatic APSInt HandleIntToIntCast(EvalInfo &Info, const Expr *E, 1188f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith QualType DestType, QualType SrcType, 1189f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith APSInt &Value) { 1190f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith unsigned DestWidth = Info.Ctx.getIntWidth(DestType); 1191a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar APSInt Result = Value; 1192a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar // Figure out if this is a truncate, extend or noop cast. 1193a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar // If the input is signed, do a sign extend, noop, or truncate. 11949f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad Result = Result.extOrTrunc(DestWidth); 1195575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType()); 1196a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar return Result; 1197a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar} 1198a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar 1199c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstatic bool HandleIntToFloatCast(EvalInfo &Info, const Expr *E, 1200c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith QualType SrcType, const APSInt &Value, 1201c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith QualType DestType, APFloat &Result) { 1202c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Result = APFloat(Info.Ctx.getFloatTypeSemantics(DestType), 1); 1203c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (Result.convertFromAPInt(Value, Value.isSigned(), 1204c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith APFloat::rmNearestTiesToEven) 1205c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith & APFloat::opOverflow) 120626dc97cbeba8ced19972a259720a71aefa01ef43Eli Friedman HandleOverflow(Info, E, Value, DestType); 1207c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return true; 1208a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar} 1209a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar 1210e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedmanstatic bool EvalAndBitcastToAPInt(EvalInfo &Info, const Expr *E, 1211e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman llvm::APInt &Res) { 12121aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith APValue SVal; 1213e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman if (!Evaluate(SVal, Info, E)) 1214e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman return false; 1215e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman if (SVal.isInt()) { 1216e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman Res = SVal.getInt(); 1217e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman return true; 1218e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman } 1219e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman if (SVal.isFloat()) { 1220e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman Res = SVal.getFloat().bitcastToAPInt(); 1221e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman return true; 1222e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman } 1223e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman if (SVal.isVector()) { 1224e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman QualType VecTy = E->getType(); 1225e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman unsigned VecSize = Info.Ctx.getTypeSize(VecTy); 1226e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman QualType EltTy = VecTy->castAs<VectorType>()->getElementType(); 1227e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman unsigned EltSize = Info.Ctx.getTypeSize(EltTy); 1228e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); 1229e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman Res = llvm::APInt::getNullValue(VecSize); 1230e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman for (unsigned i = 0; i < SVal.getVectorLength(); i++) { 1231e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman APValue &Elt = SVal.getVectorElt(i); 1232e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman llvm::APInt EltAsInt; 1233e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman if (Elt.isInt()) { 1234e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman EltAsInt = Elt.getInt(); 1235e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman } else if (Elt.isFloat()) { 1236e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman EltAsInt = Elt.getFloat().bitcastToAPInt(); 1237e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman } else { 1238e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman // Don't try to handle vectors of anything other than int or float 1239e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman // (not sure if it's possible to hit this case). 12405cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); 1241e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman return false; 1242e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman } 1243e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman unsigned BaseEltSize = EltAsInt.getBitWidth(); 1244e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman if (BigEndian) 1245e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman Res |= EltAsInt.zextOrTrunc(VecSize).rotr(i*EltSize+BaseEltSize); 1246e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman else 1247e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman Res |= EltAsInt.zextOrTrunc(VecSize).rotl(i*EltSize); 1248e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman } 1249e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman return true; 1250e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman } 1251e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman // Give up if the input isn't an int, float, or vector. For example, we 1252e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman // reject "(v4i16)(intptr_t)&a". 12535cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); 1254e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman return false; 1255e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman} 1256e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman 1257b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith/// Cast an lvalue referring to a base subobject to a derived class, by 1258b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith/// truncating the lvalue's path to the given length. 1259b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithstatic bool CastToDerivedClass(EvalInfo &Info, const Expr *E, LValue &Result, 1260b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith const RecordDecl *TruncatedType, 1261b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith unsigned TruncatedElements) { 1262b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith SubobjectDesignator &D = Result.Designator; 1263180f47959a066795cc0f409433023af448bb0328Richard Smith 1264b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // Check we actually point to a derived class object. 1265b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (TruncatedElements == D.Entries.size()) 1266b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return true; 1267b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith assert(TruncatedElements >= D.MostDerivedPathLength && 1268b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith "not casting to a derived class"); 1269b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (!Result.checkSubobject(Info, E, CSK_Derived)) 1270180f47959a066795cc0f409433023af448bb0328Richard Smith return false; 1271180f47959a066795cc0f409433023af448bb0328Richard Smith 1272b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // Truncate the path to the subobject, and remove any derived-to-base offsets. 1273e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const RecordDecl *RD = TruncatedType; 1274e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith for (unsigned I = TruncatedElements, N = D.Entries.size(); I != N; ++I) { 12758d59deec807ed53efcd07855199cdc9c979f447fJohn McCall if (RD->isInvalidDecl()) return false; 1276180f47959a066795cc0f409433023af448bb0328Richard Smith const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); 1277180f47959a066795cc0f409433023af448bb0328Richard Smith const CXXRecordDecl *Base = getAsBaseClass(D.Entries[I]); 1278e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (isVirtualBaseClass(D.Entries[I])) 1279180f47959a066795cc0f409433023af448bb0328Richard Smith Result.Offset -= Layout.getVBaseClassOffset(Base); 1280e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith else 1281180f47959a066795cc0f409433023af448bb0328Richard Smith Result.Offset -= Layout.getBaseClassOffset(Base); 1282180f47959a066795cc0f409433023af448bb0328Richard Smith RD = Base; 1283180f47959a066795cc0f409433023af448bb0328Richard Smith } 1284e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith D.Entries.resize(TruncatedElements); 1285180f47959a066795cc0f409433023af448bb0328Richard Smith return true; 1286180f47959a066795cc0f409433023af448bb0328Richard Smith} 1287180f47959a066795cc0f409433023af448bb0328Richard Smith 12888d59deec807ed53efcd07855199cdc9c979f447fJohn McCallstatic bool HandleLValueDirectBase(EvalInfo &Info, const Expr *E, LValue &Obj, 1289180f47959a066795cc0f409433023af448bb0328Richard Smith const CXXRecordDecl *Derived, 1290180f47959a066795cc0f409433023af448bb0328Richard Smith const CXXRecordDecl *Base, 1291180f47959a066795cc0f409433023af448bb0328Richard Smith const ASTRecordLayout *RL = 0) { 12928d59deec807ed53efcd07855199cdc9c979f447fJohn McCall if (!RL) { 12938d59deec807ed53efcd07855199cdc9c979f447fJohn McCall if (Derived->isInvalidDecl()) return false; 12948d59deec807ed53efcd07855199cdc9c979f447fJohn McCall RL = &Info.Ctx.getASTRecordLayout(Derived); 12958d59deec807ed53efcd07855199cdc9c979f447fJohn McCall } 12968d59deec807ed53efcd07855199cdc9c979f447fJohn McCall 1297180f47959a066795cc0f409433023af448bb0328Richard Smith Obj.getLValueOffset() += RL->getBaseClassOffset(Base); 1298b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Obj.addDecl(Info, E, Base, /*Virtual*/ false); 12998d59deec807ed53efcd07855199cdc9c979f447fJohn McCall return true; 1300180f47959a066795cc0f409433023af448bb0328Richard Smith} 1301180f47959a066795cc0f409433023af448bb0328Richard Smith 1302b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithstatic bool HandleLValueBase(EvalInfo &Info, const Expr *E, LValue &Obj, 1303180f47959a066795cc0f409433023af448bb0328Richard Smith const CXXRecordDecl *DerivedDecl, 1304180f47959a066795cc0f409433023af448bb0328Richard Smith const CXXBaseSpecifier *Base) { 1305180f47959a066795cc0f409433023af448bb0328Richard Smith const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl(); 1306180f47959a066795cc0f409433023af448bb0328Richard Smith 13078d59deec807ed53efcd07855199cdc9c979f447fJohn McCall if (!Base->isVirtual()) 13088d59deec807ed53efcd07855199cdc9c979f447fJohn McCall return HandleLValueDirectBase(Info, E, Obj, DerivedDecl, BaseDecl); 1309180f47959a066795cc0f409433023af448bb0328Richard Smith 1310b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith SubobjectDesignator &D = Obj.Designator; 1311b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (D.Invalid) 1312b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return false; 1313b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 1314180f47959a066795cc0f409433023af448bb0328Richard Smith // Extract most-derived object and corresponding type. 1315b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith DerivedDecl = D.MostDerivedType->getAsCXXRecordDecl(); 1316b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (!CastToDerivedClass(Info, E, Obj, DerivedDecl, D.MostDerivedPathLength)) 1317180f47959a066795cc0f409433023af448bb0328Richard Smith return false; 1318180f47959a066795cc0f409433023af448bb0328Richard Smith 1319b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // Find the virtual base class. 13208d59deec807ed53efcd07855199cdc9c979f447fJohn McCall if (DerivedDecl->isInvalidDecl()) return false; 1321180f47959a066795cc0f409433023af448bb0328Richard Smith const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(DerivedDecl); 1322180f47959a066795cc0f409433023af448bb0328Richard Smith Obj.getLValueOffset() += Layout.getVBaseClassOffset(BaseDecl); 1323b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Obj.addDecl(Info, E, BaseDecl, /*Virtual*/ true); 1324180f47959a066795cc0f409433023af448bb0328Richard Smith return true; 1325180f47959a066795cc0f409433023af448bb0328Richard Smith} 1326180f47959a066795cc0f409433023af448bb0328Richard Smith 1327180f47959a066795cc0f409433023af448bb0328Richard Smith/// Update LVal to refer to the given field, which must be a member of the type 1328180f47959a066795cc0f409433023af448bb0328Richard Smith/// currently described by LVal. 13298d59deec807ed53efcd07855199cdc9c979f447fJohn McCallstatic bool HandleLValueMember(EvalInfo &Info, const Expr *E, LValue &LVal, 1330180f47959a066795cc0f409433023af448bb0328Richard Smith const FieldDecl *FD, 1331180f47959a066795cc0f409433023af448bb0328Richard Smith const ASTRecordLayout *RL = 0) { 13328d59deec807ed53efcd07855199cdc9c979f447fJohn McCall if (!RL) { 13338d59deec807ed53efcd07855199cdc9c979f447fJohn McCall if (FD->getParent()->isInvalidDecl()) return false; 1334180f47959a066795cc0f409433023af448bb0328Richard Smith RL = &Info.Ctx.getASTRecordLayout(FD->getParent()); 13358d59deec807ed53efcd07855199cdc9c979f447fJohn McCall } 1336180f47959a066795cc0f409433023af448bb0328Richard Smith 1337180f47959a066795cc0f409433023af448bb0328Richard Smith unsigned I = FD->getFieldIndex(); 1338180f47959a066795cc0f409433023af448bb0328Richard Smith LVal.Offset += Info.Ctx.toCharUnitsFromBits(RL->getFieldOffset(I)); 1339b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith LVal.addDecl(Info, E, FD); 13408d59deec807ed53efcd07855199cdc9c979f447fJohn McCall return true; 1341180f47959a066795cc0f409433023af448bb0328Richard Smith} 1342180f47959a066795cc0f409433023af448bb0328Richard Smith 1343d9b02e726262e4009dda830998bb934172ac0020Richard Smith/// Update LVal to refer to the given indirect field. 13448d59deec807ed53efcd07855199cdc9c979f447fJohn McCallstatic bool HandleLValueIndirectMember(EvalInfo &Info, const Expr *E, 1345d9b02e726262e4009dda830998bb934172ac0020Richard Smith LValue &LVal, 1346d9b02e726262e4009dda830998bb934172ac0020Richard Smith const IndirectFieldDecl *IFD) { 1347d9b02e726262e4009dda830998bb934172ac0020Richard Smith for (IndirectFieldDecl::chain_iterator C = IFD->chain_begin(), 1348d9b02e726262e4009dda830998bb934172ac0020Richard Smith CE = IFD->chain_end(); C != CE; ++C) 13498d59deec807ed53efcd07855199cdc9c979f447fJohn McCall if (!HandleLValueMember(Info, E, LVal, cast<FieldDecl>(*C))) 13508d59deec807ed53efcd07855199cdc9c979f447fJohn McCall return false; 13518d59deec807ed53efcd07855199cdc9c979f447fJohn McCall return true; 1352d9b02e726262e4009dda830998bb934172ac0020Richard Smith} 1353d9b02e726262e4009dda830998bb934172ac0020Richard Smith 1354180f47959a066795cc0f409433023af448bb0328Richard Smith/// Get the size of the given type in char units. 135574e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smithstatic bool HandleSizeof(EvalInfo &Info, SourceLocation Loc, 135674e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith QualType Type, CharUnits &Size) { 1357180f47959a066795cc0f409433023af448bb0328Richard Smith // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc 1358180f47959a066795cc0f409433023af448bb0328Richard Smith // extension. 1359180f47959a066795cc0f409433023af448bb0328Richard Smith if (Type->isVoidType() || Type->isFunctionType()) { 1360180f47959a066795cc0f409433023af448bb0328Richard Smith Size = CharUnits::One(); 1361180f47959a066795cc0f409433023af448bb0328Richard Smith return true; 1362180f47959a066795cc0f409433023af448bb0328Richard Smith } 1363180f47959a066795cc0f409433023af448bb0328Richard Smith 1364180f47959a066795cc0f409433023af448bb0328Richard Smith if (!Type->isConstantSizeType()) { 1365180f47959a066795cc0f409433023af448bb0328Richard Smith // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. 136674e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith // FIXME: Better diagnostic. 136774e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith Info.Diag(Loc); 1368180f47959a066795cc0f409433023af448bb0328Richard Smith return false; 1369180f47959a066795cc0f409433023af448bb0328Richard Smith } 1370180f47959a066795cc0f409433023af448bb0328Richard Smith 1371180f47959a066795cc0f409433023af448bb0328Richard Smith Size = Info.Ctx.getTypeSizeInChars(Type); 1372180f47959a066795cc0f409433023af448bb0328Richard Smith return true; 1373180f47959a066795cc0f409433023af448bb0328Richard Smith} 1374180f47959a066795cc0f409433023af448bb0328Richard Smith 1375180f47959a066795cc0f409433023af448bb0328Richard Smith/// Update a pointer value to model pointer arithmetic. 1376180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param Info - Information about the ongoing evaluation. 1377b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith/// \param E - The expression being evaluated, for diagnostic purposes. 1378180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param LVal - The pointer value to be updated. 1379180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param EltTy - The pointee type represented by LVal. 1380180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param Adjustment - The adjustment, in objects of type EltTy, to add. 1381b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithstatic bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E, 1382b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith LValue &LVal, QualType EltTy, 1383b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith int64_t Adjustment) { 1384180f47959a066795cc0f409433023af448bb0328Richard Smith CharUnits SizeOfPointee; 138574e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfPointee)) 1386180f47959a066795cc0f409433023af448bb0328Richard Smith return false; 1387180f47959a066795cc0f409433023af448bb0328Richard Smith 1388180f47959a066795cc0f409433023af448bb0328Richard Smith // Compute the new offset in the appropriate width. 1389180f47959a066795cc0f409433023af448bb0328Richard Smith LVal.Offset += Adjustment * SizeOfPointee; 1390b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith LVal.adjustIndex(Info, E, Adjustment); 1391180f47959a066795cc0f409433023af448bb0328Richard Smith return true; 1392180f47959a066795cc0f409433023af448bb0328Richard Smith} 1393180f47959a066795cc0f409433023af448bb0328Richard Smith 139486024013d4c3728122c58fa07a2a67e6c15837efRichard Smith/// Update an lvalue to refer to a component of a complex number. 139586024013d4c3728122c58fa07a2a67e6c15837efRichard Smith/// \param Info - Information about the ongoing evaluation. 139686024013d4c3728122c58fa07a2a67e6c15837efRichard Smith/// \param LVal - The lvalue to be updated. 139786024013d4c3728122c58fa07a2a67e6c15837efRichard Smith/// \param EltTy - The complex number's component type. 139886024013d4c3728122c58fa07a2a67e6c15837efRichard Smith/// \param Imag - False for the real component, true for the imaginary. 139986024013d4c3728122c58fa07a2a67e6c15837efRichard Smithstatic bool HandleLValueComplexElement(EvalInfo &Info, const Expr *E, 140086024013d4c3728122c58fa07a2a67e6c15837efRichard Smith LValue &LVal, QualType EltTy, 140186024013d4c3728122c58fa07a2a67e6c15837efRichard Smith bool Imag) { 140286024013d4c3728122c58fa07a2a67e6c15837efRichard Smith if (Imag) { 140386024013d4c3728122c58fa07a2a67e6c15837efRichard Smith CharUnits SizeOfComponent; 140486024013d4c3728122c58fa07a2a67e6c15837efRichard Smith if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfComponent)) 140586024013d4c3728122c58fa07a2a67e6c15837efRichard Smith return false; 140686024013d4c3728122c58fa07a2a67e6c15837efRichard Smith LVal.Offset += SizeOfComponent; 140786024013d4c3728122c58fa07a2a67e6c15837efRichard Smith } 140886024013d4c3728122c58fa07a2a67e6c15837efRichard Smith LVal.addComplex(Info, E, EltTy, Imag); 140986024013d4c3728122c58fa07a2a67e6c15837efRichard Smith return true; 141086024013d4c3728122c58fa07a2a67e6c15837efRichard Smith} 141186024013d4c3728122c58fa07a2a67e6c15837efRichard Smith 141203f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith/// Try to evaluate the initializer for a variable declaration. 1413f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool EvaluateVarDeclInit(EvalInfo &Info, const Expr *E, 1414f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith const VarDecl *VD, 14151aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith CallStackFrame *Frame, APValue &Result) { 1416d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith // If this is a parameter to an active constexpr function call, perform 1417d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith // argument substitution. 1418d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(VD)) { 1419745f5147e065900267c85a5568785a1991d4838fRichard Smith // Assume arguments of a potential constant expression are unknown 1420745f5147e065900267c85a5568785a1991d4838fRichard Smith // constant expressions. 1421745f5147e065900267c85a5568785a1991d4838fRichard Smith if (Info.CheckingPotentialConstantExpression) 1422745f5147e065900267c85a5568785a1991d4838fRichard Smith return false; 1423f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!Frame || !Frame->Arguments) { 14245cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); 1425177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith return false; 1426f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 1427177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith Result = Frame->Arguments[PVD->getFunctionScopeIndex()]; 1428177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith return true; 1429d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith } 143003f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith 1431099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith // Dig out the initializer, and use the declaration which it's attached to. 1432099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith const Expr *Init = VD->getAnyInitializer(VD); 1433099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith if (!Init || Init->isValueDependent()) { 1434745f5147e065900267c85a5568785a1991d4838fRichard Smith // If we're checking a potential constant expression, the variable could be 1435745f5147e065900267c85a5568785a1991d4838fRichard Smith // initialized later. 1436745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!Info.CheckingPotentialConstantExpression) 14375cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); 1438099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith return false; 1439099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith } 1440099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith 1441180f47959a066795cc0f409433023af448bb0328Richard Smith // If we're currently evaluating the initializer of this declaration, use that 1442180f47959a066795cc0f409433023af448bb0328Richard Smith // in-flight value. 1443180f47959a066795cc0f409433023af448bb0328Richard Smith if (Info.EvaluatingDecl == VD) { 14441aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith Result = *Info.EvaluatingDeclValue; 1445180f47959a066795cc0f409433023af448bb0328Richard Smith return !Result.isUninit(); 1446180f47959a066795cc0f409433023af448bb0328Richard Smith } 1447180f47959a066795cc0f409433023af448bb0328Richard Smith 144865ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith // Never evaluate the initializer of a weak variable. We can't be sure that 144965ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith // this is the definition which will be used. 1450f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (VD->isWeak()) { 14515cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); 145265ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith return false; 1453f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 145465ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith 1455099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith // Check that we can fold the initializer. In C++, we will have already done 1456099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith // this in the cases where it matters for conformance. 1457099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith llvm::SmallVector<PartialDiagnosticAt, 8> Notes; 1458099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith if (!VD->evaluateValue(Notes)) { 14595cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.Diag(E, diag::note_constexpr_var_init_non_constant, 1460099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Notes.size() + 1) << VD; 1461099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Info.Note(VD->getLocation(), diag::note_declared_at); 1462099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Info.addNotes(Notes); 146347a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith return false; 1464099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith } else if (!VD->checkInitIsICE()) { 14655cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.CCEDiag(E, diag::note_constexpr_var_init_non_constant, 1466099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Notes.size() + 1) << VD; 1467099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Info.Note(VD->getLocation(), diag::note_declared_at); 1468099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Info.addNotes(Notes); 1469f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 147003f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith 14711aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith Result = *VD->getEvaluatedValue(); 147247a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith return true; 147303f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith} 147403f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith 1475c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smithstatic bool IsConstNonVolatile(QualType T) { 147603f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith Qualifiers Quals = T.getQualifiers(); 147703f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith return Quals.hasConst() && !Quals.hasVolatile(); 147803f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith} 147903f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith 148059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith/// Get the base index of the given base class within an APValue representing 148159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith/// the given derived class. 148259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithstatic unsigned getBaseIndex(const CXXRecordDecl *Derived, 148359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith const CXXRecordDecl *Base) { 148459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith Base = Base->getCanonicalDecl(); 148559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith unsigned Index = 0; 148659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith for (CXXRecordDecl::base_class_const_iterator I = Derived->bases_begin(), 148759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith E = Derived->bases_end(); I != E; ++I, ++Index) { 148859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith if (I->getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base) 148959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith return Index; 149059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith } 149159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith 149259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith llvm_unreachable("base class missing from derived class's bases list"); 149359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith} 149459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith 1495fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith/// Extract the value of a character from a string literal. CharType is used to 1496fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith/// determine the expected signedness of the result -- a string literal used to 1497fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith/// initialize an array of 'signed char' or 'unsigned char' might contain chars 1498fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith/// of the wrong signedness. 1499f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smithstatic APSInt ExtractStringLiteralCharacter(EvalInfo &Info, const Expr *Lit, 1500fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith uint64_t Index, QualType CharType) { 1501f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith // FIXME: Support PredefinedExpr, ObjCEncodeExpr, MakeStringConstant 1502f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith const StringLiteral *S = dyn_cast<StringLiteral>(Lit); 1503f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith assert(S && "unexpected string literal expression kind"); 1504fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith assert(CharType->isIntegerType() && "unexpected character type"); 1505f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith 1506f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(), 1507fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith CharType->isUnsignedIntegerType()); 1508f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith if (Index < S->getLength()) 1509f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith Value = S->getCodeUnit(Index); 1510f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith return Value; 1511f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith} 1512f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith 1513cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith/// Extract the designated sub-object of an rvalue. 1514f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool ExtractSubobject(EvalInfo &Info, const Expr *E, 15151aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith APValue &Obj, QualType ObjType, 1516cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith const SubobjectDesignator &Sub, QualType SubType) { 1517b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (Sub.Invalid) 1518b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // A diagnostic will have already been produced. 1519cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith return false; 1520b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (Sub.isOnePastTheEnd()) { 15215cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.Diag(E, Info.getLangOpts().CPlusPlus0x ? 1522aa5d533427d803e52ee42b250ffd6645ef5ccb0fMatt Beaumont-Gay (unsigned)diag::note_constexpr_read_past_end : 1523aa5d533427d803e52ee42b250ffd6645ef5ccb0fMatt Beaumont-Gay (unsigned)diag::note_invalid_subexpr_in_const_expr); 15247098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith return false; 15257098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } 1526f64699e8db3946e21b5f4a0421cbc58a3e439599Richard Smith if (Sub.Entries.empty()) 1527cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith return true; 1528745f5147e065900267c85a5568785a1991d4838fRichard Smith if (Info.CheckingPotentialConstantExpression && Obj.isUninit()) 1529745f5147e065900267c85a5568785a1991d4838fRichard Smith // This object might be initialized later. 1530745f5147e065900267c85a5568785a1991d4838fRichard Smith return false; 1531cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith 15320069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith APValue *O = &Obj; 1533180f47959a066795cc0f409433023af448bb0328Richard Smith // Walk the designator's path to find the subobject. 1534cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith for (unsigned I = 0, N = Sub.Entries.size(); I != N; ++I) { 1535cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith if (ObjType->isArrayType()) { 1536180f47959a066795cc0f409433023af448bb0328Richard Smith // Next subobject is an array element. 1537cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(ObjType); 1538f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith assert(CAT && "vla in literal type?"); 1539cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith uint64_t Index = Sub.Entries[I].ArrayIndex; 1540f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (CAT->getSize().ule(Index)) { 15417098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // Note, it should not be possible to form a pointer with a valid 15427098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // designator which points more than one past the end of the array. 15435cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.Diag(E, Info.getLangOpts().CPlusPlus0x ? 1544aa5d533427d803e52ee42b250ffd6645ef5ccb0fMatt Beaumont-Gay (unsigned)diag::note_constexpr_read_past_end : 1545aa5d533427d803e52ee42b250ffd6645ef5ccb0fMatt Beaumont-Gay (unsigned)diag::note_invalid_subexpr_in_const_expr); 1546cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith return false; 1547f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 1548f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith // An array object is represented as either an Array APValue or as an 1549f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith // LValue which refers to a string literal. 1550f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith if (O->isLValue()) { 1551f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith assert(I == N - 1 && "extracting subobject of character?"); 1552f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith assert(!O->hasLValuePath() || O->getLValuePath().empty()); 15531aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith Obj = APValue(ExtractStringLiteralCharacter( 1554fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith Info, O->getLValueBase().get<const Expr*>(), Index, SubType)); 1555f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith return true; 1556f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith } else if (O->getArrayInitializedElts() > Index) 1557cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith O = &O->getArrayInitializedElt(Index); 1558cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith else 1559cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith O = &O->getArrayFiller(); 1560cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith ObjType = CAT->getElementType(); 156186024013d4c3728122c58fa07a2a67e6c15837efRichard Smith } else if (ObjType->isAnyComplexType()) { 156286024013d4c3728122c58fa07a2a67e6c15837efRichard Smith // Next subobject is a complex number. 156386024013d4c3728122c58fa07a2a67e6c15837efRichard Smith uint64_t Index = Sub.Entries[I].ArrayIndex; 156486024013d4c3728122c58fa07a2a67e6c15837efRichard Smith if (Index > 1) { 15655cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.Diag(E, Info.getLangOpts().CPlusPlus0x ? 156686024013d4c3728122c58fa07a2a67e6c15837efRichard Smith (unsigned)diag::note_constexpr_read_past_end : 156786024013d4c3728122c58fa07a2a67e6c15837efRichard Smith (unsigned)diag::note_invalid_subexpr_in_const_expr); 156886024013d4c3728122c58fa07a2a67e6c15837efRichard Smith return false; 156986024013d4c3728122c58fa07a2a67e6c15837efRichard Smith } 157086024013d4c3728122c58fa07a2a67e6c15837efRichard Smith assert(I == N - 1 && "extracting subobject of scalar?"); 157186024013d4c3728122c58fa07a2a67e6c15837efRichard Smith if (O->isComplexInt()) { 15721aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith Obj = APValue(Index ? O->getComplexIntImag() 157386024013d4c3728122c58fa07a2a67e6c15837efRichard Smith : O->getComplexIntReal()); 157486024013d4c3728122c58fa07a2a67e6c15837efRichard Smith } else { 157586024013d4c3728122c58fa07a2a67e6c15837efRichard Smith assert(O->isComplexFloat()); 15761aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith Obj = APValue(Index ? O->getComplexFloatImag() 157786024013d4c3728122c58fa07a2a67e6c15837efRichard Smith : O->getComplexFloatReal()); 157886024013d4c3728122c58fa07a2a67e6c15837efRichard Smith } 157986024013d4c3728122c58fa07a2a67e6c15837efRichard Smith return true; 1580180f47959a066795cc0f409433023af448bb0328Richard Smith } else if (const FieldDecl *Field = getAsField(Sub.Entries[I])) { 1581b4e5e286a5cd156247720b1eb204abaa8e09568dRichard Smith if (Field->isMutable()) { 15825cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.Diag(E, diag::note_constexpr_ltor_mutable, 1) 1583b4e5e286a5cd156247720b1eb204abaa8e09568dRichard Smith << Field; 1584b4e5e286a5cd156247720b1eb204abaa8e09568dRichard Smith Info.Note(Field->getLocation(), diag::note_declared_at); 1585b4e5e286a5cd156247720b1eb204abaa8e09568dRichard Smith return false; 1586b4e5e286a5cd156247720b1eb204abaa8e09568dRichard Smith } 1587b4e5e286a5cd156247720b1eb204abaa8e09568dRichard Smith 1588180f47959a066795cc0f409433023af448bb0328Richard Smith // Next subobject is a class, struct or union field. 1589180f47959a066795cc0f409433023af448bb0328Richard Smith RecordDecl *RD = ObjType->castAs<RecordType>()->getDecl(); 1590180f47959a066795cc0f409433023af448bb0328Richard Smith if (RD->isUnion()) { 1591180f47959a066795cc0f409433023af448bb0328Richard Smith const FieldDecl *UnionField = O->getUnionField(); 1592180f47959a066795cc0f409433023af448bb0328Richard Smith if (!UnionField || 1593f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith UnionField->getCanonicalDecl() != Field->getCanonicalDecl()) { 15945cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.Diag(E, diag::note_constexpr_read_inactive_union_member) 15957098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith << Field << !UnionField << UnionField; 1596180f47959a066795cc0f409433023af448bb0328Richard Smith return false; 1597f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 1598180f47959a066795cc0f409433023af448bb0328Richard Smith O = &O->getUnionValue(); 1599180f47959a066795cc0f409433023af448bb0328Richard Smith } else 1600180f47959a066795cc0f409433023af448bb0328Richard Smith O = &O->getStructField(Field->getFieldIndex()); 1601180f47959a066795cc0f409433023af448bb0328Richard Smith ObjType = Field->getType(); 16027098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith 16037098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (ObjType.isVolatileQualified()) { 16047098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (Info.getLangOpts().CPlusPlus) { 16057098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // FIXME: Include a description of the path to the volatile subobject. 16065cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.Diag(E, diag::note_constexpr_ltor_volatile_obj, 1) 16077098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith << 2 << Field; 16087098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Note(Field->getLocation(), diag::note_declared_at); 16097098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } else { 16105cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); 16117098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } 16127098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith return false; 16137098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } 1614cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith } else { 1615180f47959a066795cc0f409433023af448bb0328Richard Smith // Next subobject is a base class. 161659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith const CXXRecordDecl *Derived = ObjType->getAsCXXRecordDecl(); 161759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith const CXXRecordDecl *Base = getAsBaseClass(Sub.Entries[I]); 161859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith O = &O->getStructBase(getBaseIndex(Derived, Base)); 161959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith ObjType = Info.Ctx.getRecordType(Base); 1620cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith } 1621180f47959a066795cc0f409433023af448bb0328Richard Smith 1622f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (O->isUninit()) { 1623745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!Info.CheckingPotentialConstantExpression) 16245cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.Diag(E, diag::note_constexpr_read_uninit); 1625180f47959a066795cc0f409433023af448bb0328Richard Smith return false; 1626f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 1627cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith } 1628cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith 16290069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith // This may look super-stupid, but it serves an important purpose: if we just 16300069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith // swapped Obj and *O, we'd create an object which had itself as a subobject. 16310069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith // To avoid the leak, we ensure that Tmp ends up owning the original complete 16320069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith // object, which is destroyed by Tmp's destructor. 16330069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith APValue Tmp; 16340069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith O->swap(Tmp); 16350069b84c2aa7cc39263e85997b7cb1ed0b132ccdRichard Smith Obj.swap(Tmp); 1636cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith return true; 1637cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith} 1638cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith 1639f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith/// Find the position where two subobject designators diverge, or equivalently 1640f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith/// the length of the common initial subsequence. 1641f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smithstatic unsigned FindDesignatorMismatch(QualType ObjType, 1642f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith const SubobjectDesignator &A, 1643f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith const SubobjectDesignator &B, 1644f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith bool &WasArrayIndex) { 1645f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith unsigned I = 0, N = std::min(A.Entries.size(), B.Entries.size()); 1646f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith for (/**/; I != N; ++I) { 164786024013d4c3728122c58fa07a2a67e6c15837efRichard Smith if (!ObjType.isNull() && 164886024013d4c3728122c58fa07a2a67e6c15837efRichard Smith (ObjType->isArrayType() || ObjType->isAnyComplexType())) { 1649f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // Next subobject is an array element. 1650f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (A.Entries[I].ArrayIndex != B.Entries[I].ArrayIndex) { 1651f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith WasArrayIndex = true; 1652f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith return I; 1653f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith } 165486024013d4c3728122c58fa07a2a67e6c15837efRichard Smith if (ObjType->isAnyComplexType()) 165586024013d4c3728122c58fa07a2a67e6c15837efRichard Smith ObjType = ObjType->castAs<ComplexType>()->getElementType(); 165686024013d4c3728122c58fa07a2a67e6c15837efRichard Smith else 165786024013d4c3728122c58fa07a2a67e6c15837efRichard Smith ObjType = ObjType->castAsArrayTypeUnsafe()->getElementType(); 1658f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith } else { 1659f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (A.Entries[I].BaseOrMember != B.Entries[I].BaseOrMember) { 1660f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith WasArrayIndex = false; 1661f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith return I; 1662f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith } 1663f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (const FieldDecl *FD = getAsField(A.Entries[I])) 1664f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // Next subobject is a field. 1665f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith ObjType = FD->getType(); 1666f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith else 1667f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // Next subobject is a base class. 1668f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith ObjType = QualType(); 1669f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith } 1670f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith } 1671f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith WasArrayIndex = false; 1672f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith return I; 1673f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith} 1674f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 1675f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith/// Determine whether the given subobject designators refer to elements of the 1676f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith/// same array object. 1677f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smithstatic bool AreElementsOfSameArray(QualType ObjType, 1678f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith const SubobjectDesignator &A, 1679f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith const SubobjectDesignator &B) { 1680f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (A.Entries.size() != B.Entries.size()) 1681f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith return false; 1682f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 1683f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith bool IsArray = A.MostDerivedArraySize != 0; 1684f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (IsArray && A.MostDerivedPathLength != A.Entries.size()) 1685f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // A is a subobject of the array element. 1686f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith return false; 1687f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 1688f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // If A (and B) designates an array element, the last entry will be the array 1689f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // index. That doesn't have to match. Otherwise, we're in the 'implicit array 1690f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // of length 1' case, and the entire path must match. 1691f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith bool WasArrayIndex; 1692f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith unsigned CommonLength = FindDesignatorMismatch(ObjType, A, B, WasArrayIndex); 1693f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith return CommonLength >= A.Entries.size() - IsArray; 1694f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith} 1695f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 1696180f47959a066795cc0f409433023af448bb0328Richard Smith/// HandleLValueToRValueConversion - Perform an lvalue-to-rvalue conversion on 1697180f47959a066795cc0f409433023af448bb0328Richard Smith/// the given lvalue. This can also be used for 'lvalue-to-lvalue' conversions 1698180f47959a066795cc0f409433023af448bb0328Richard Smith/// for looking up the glvalue referred to by an entity of reference type. 1699180f47959a066795cc0f409433023af448bb0328Richard Smith/// 1700180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param Info - Information about the ongoing evaluation. 1701f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// \param Conv - The expression for which we are performing the conversion. 1702f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// Used for diagnostics. 17039ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith/// \param Type - The type we expect this conversion to produce, before 17049ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith/// stripping cv-qualifiers in the case of a non-clas type. 1705180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param LVal - The glvalue on which we are attempting to perform this action. 1706180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param RVal - The produced value will be placed here. 1707f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool HandleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv, 1708f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith QualType Type, 17091aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith const LValue &LVal, APValue &RVal) { 1710b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (LVal.Designator.Invalid) 1711b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // A diagnostic will have already been produced. 1712b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return false; 1713b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 17141bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); 1715c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 1716f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!LVal.Base) { 1717f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith // FIXME: Indirection through a null pointer deserves a specific diagnostic. 17185cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.Diag(Conv, diag::note_invalid_subexpr_in_const_expr); 17197098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith return false; 17207098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } 17217098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith 172283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith CallStackFrame *Frame = 0; 172383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith if (LVal.CallIndex) { 172483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith Frame = Info.getCallFrame(LVal.CallIndex); 172583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith if (!Frame) { 17265cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.Diag(Conv, diag::note_constexpr_lifetime_ended, 1) << !Base; 172783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith NoteLValueLocation(Info, LVal.Base); 172883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith return false; 172983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith } 173083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith } 173183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith 17327098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // C++11 DR1311: An lvalue-to-rvalue conversion on a volatile-qualified type 17337098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // is not a constant expression (even if the object is non-volatile). We also 17347098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // apply this rule to C++98, in order to conform to the expected 'volatile' 17357098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // semantics. 17367098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (Type.isVolatileQualified()) { 17377098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (Info.getLangOpts().CPlusPlus) 17385cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.Diag(Conv, diag::note_constexpr_ltor_volatile_type) << Type; 17397098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith else 17405cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.Diag(Conv); 1741c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return false; 1742f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 1743c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 17441bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith if (const ValueDecl *D = LVal.Base.dyn_cast<const ValueDecl*>()) { 1745c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // In C++98, const, non-volatile integers initialized with ICEs are ICEs. 1746c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // In C++11, constexpr, non-volatile variables initialized with constant 1747d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith // expressions are constant expressions too. Inside constexpr functions, 1748d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith // parameters are constant expressions even if they're non-const. 1749c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // In C, such things can also be folded, although they are not ICEs. 1750c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith const VarDecl *VD = dyn_cast<VarDecl>(D); 1751d2008e2c80d6c9282044ec873a937a17a0f33579Douglas Gregor if (VD) { 1752d2008e2c80d6c9282044ec873a937a17a0f33579Douglas Gregor if (const VarDecl *VDef = VD->getDefinition(Info.Ctx)) 1753d2008e2c80d6c9282044ec873a937a17a0f33579Douglas Gregor VD = VDef; 1754d2008e2c80d6c9282044ec873a937a17a0f33579Douglas Gregor } 1755f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!VD || VD->isInvalidDecl()) { 17565cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.Diag(Conv); 17570a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith return false; 1758f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 1759f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 17607098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // DR1313: If the object is volatile-qualified but the glvalue was not, 17617098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // behavior is undefined so the result is not a constant expression. 17621bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith QualType VT = VD->getType(); 17637098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (VT.isVolatileQualified()) { 17647098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (Info.getLangOpts().CPlusPlus) { 17655cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.Diag(Conv, diag::note_constexpr_ltor_volatile_obj, 1) << 1 << VD; 17667098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Note(VD->getLocation(), diag::note_declared_at); 17677098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } else { 17685cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.Diag(Conv); 1769f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 17707098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith return false; 17717098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } 17727098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith 17737098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (!isa<ParmVarDecl>(VD)) { 17747098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (VD->isConstexpr()) { 17757098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // OK, we can read this variable. 17767098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } else if (VT->isIntegralOrEnumerationType()) { 17777098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (!VT.isConstQualified()) { 17787098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (Info.getLangOpts().CPlusPlus) { 17795cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.Diag(Conv, diag::note_constexpr_ltor_non_const_int, 1) << VD; 17807098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Note(VD->getLocation(), diag::note_declared_at); 17817098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } else { 17825cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.Diag(Conv); 17837098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } 17847098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith return false; 17857098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } 17867098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } else if (VT->isFloatingType() && VT.isConstQualified()) { 17877098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // We support folding of const floating-point types, in order to make 17887098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // static const data members of such types (supported as an extension) 17897098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // more useful. 17907098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (Info.getLangOpts().CPlusPlus0x) { 17915cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.CCEDiag(Conv, diag::note_constexpr_ltor_non_constexpr, 1) << VD; 17927098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Note(VD->getLocation(), diag::note_declared_at); 17937098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } else { 17945cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.CCEDiag(Conv); 17957098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } 17967098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } else { 17977098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // FIXME: Allow folding of values of any literal type in all languages. 17987098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (Info.getLangOpts().CPlusPlus0x) { 17995cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.Diag(Conv, diag::note_constexpr_ltor_non_constexpr, 1) << VD; 18007098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Note(VD->getLocation(), diag::note_declared_at); 18017098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } else { 18025cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.Diag(Conv); 18037098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } 18040a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith return false; 1805f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 18060a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith } 18077098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith 1808f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!EvaluateVarDeclInit(Info, Conv, VD, Frame, RVal)) 1809c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return false; 1810c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 181147a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith if (isa<ParmVarDecl>(VD) || !VD->getAnyInitializer()->isLValue()) 1812f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return ExtractSubobject(Info, Conv, RVal, VT, LVal.Designator, Type); 1813c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 1814c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // The declaration was initialized by an lvalue, with no lvalue-to-rvalue 1815c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // conversion. This happens when the declaration and the lvalue should be 1816c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // considered synonymous, for instance when initializing an array of char 1817c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // from a string literal. Continue as if the initializer lvalue was the 1818c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // value we were originally given. 18190a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith assert(RVal.getLValueOffset().isZero() && 18200a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith "offset for lvalue init of non-reference"); 18211bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith Base = RVal.getLValueBase().get<const Expr*>(); 182283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith 182383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith if (unsigned CallIndex = RVal.getLValueCallIndex()) { 182483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith Frame = Info.getCallFrame(CallIndex); 182583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith if (!Frame) { 18265cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.Diag(Conv, diag::note_constexpr_lifetime_ended, 1) << !Base; 182783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith NoteLValueLocation(Info, RVal.getLValueBase()); 182883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith return false; 182983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith } 183083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith } else { 183183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith Frame = 0; 183283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith } 1833c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith } 1834c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 18357098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // Volatile temporary objects cannot be read in constant expressions. 18367098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (Base->getType().isVolatileQualified()) { 18377098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (Info.getLangOpts().CPlusPlus) { 18385cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.Diag(Conv, diag::note_constexpr_ltor_volatile_obj, 1) << 0; 18397098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Note(Base->getExprLoc(), diag::note_constexpr_temporary_here); 18407098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } else { 18415cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.Diag(Conv); 18427098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } 18437098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith return false; 18447098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } 18457098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith 1846177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith if (Frame) { 1847cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith // If this is a temporary expression with a nontrivial initializer, grab the 1848cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith // value from the relevant stack frame. 1849177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith RVal = Frame->Temporaries[Base]; 1850cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith } else if (const CompoundLiteralExpr *CLE 1851cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith = dyn_cast<CompoundLiteralExpr>(Base)) { 1852cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith // In C99, a CompoundLiteralExpr is an lvalue, and we defer evaluating the 1853cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith // initializer until now for such expressions. Such an expression can't be 1854cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith // an ICE in C, so this only matters for fold. 1855c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?"); 1856cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith if (!Evaluate(RVal, Info, CLE->getInitializer())) 1857cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith return false; 1858f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith } else if (isa<StringLiteral>(Base)) { 1859f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith // We represent a string literal array as an lvalue pointing at the 1860f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith // corresponding expression, rather than building an array of chars. 1861f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith // FIXME: Support PredefinedExpr, ObjCEncodeExpr, MakeStringConstant 18621aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith RVal = APValue(Base, CharUnits::Zero(), APValue::NoLValuePath(), 0); 1863f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } else { 18645cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.Diag(Conv, diag::note_invalid_subexpr_in_const_expr); 1865cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith return false; 1866f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 1867c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 1868f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return ExtractSubobject(Info, Conv, RVal, Base->getType(), LVal.Designator, 1869f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith Type); 1870c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith} 1871c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 187259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith/// Build an lvalue for the object argument of a member function call. 187359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithstatic bool EvaluateObjectArgument(EvalInfo &Info, const Expr *Object, 187459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith LValue &This) { 187559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith if (Object->getType()->isPointerType()) 187659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith return EvaluatePointer(Object, This, Info); 187759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith 187859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith if (Object->isGLValue()) 187959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith return EvaluateLValue(Object, This, Info); 188059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith 1881e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (Object->getType()->isLiteralType()) 1882e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return EvaluateTemporary(Object, This, Info); 1883e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 1884e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 1885e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith} 1886e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 1887e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// HandleMemberPointerAccess - Evaluate a member access operation and build an 1888e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// lvalue referring to the result. 1889e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// 1890e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// \param Info - Information about the ongoing evaluation. 1891e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// \param BO - The member pointer access operation. 1892e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// \param LV - Filled in with a reference to the resulting object. 1893e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// \param IncludeMember - Specifies whether the member itself is included in 1894e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// the resulting LValue subobject designator. This is not possible when 1895e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// creating a bound member function. 1896e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// \return The field or method declaration to which the member pointer refers, 1897e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// or 0 if evaluation fails. 1898e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info, 1899e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const BinaryOperator *BO, 1900e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LValue &LV, 1901e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool IncludeMember = true) { 1902e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith assert(BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI); 1903e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 1904745f5147e065900267c85a5568785a1991d4838fRichard Smith bool EvalObjOK = EvaluateObjectArgument(Info, BO->getLHS(), LV); 1905745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!EvalObjOK && !Info.keepEvaluatingAfterFailure()) 1906e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return 0; 1907e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 1908e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith MemberPtr MemPtr; 1909e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!EvaluateMemberPointer(BO->getRHS(), MemPtr, Info)) 1910e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return 0; 1911e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 1912e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // C++11 [expr.mptr.oper]p6: If the second operand is the null pointer to 1913e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // member value, the behavior is undefined. 1914e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!MemPtr.getDecl()) 1915e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return 0; 1916e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 1917745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!EvalObjOK) 1918745f5147e065900267c85a5568785a1991d4838fRichard Smith return 0; 1919745f5147e065900267c85a5568785a1991d4838fRichard Smith 1920e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (MemPtr.isDerivedMember()) { 1921e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // This is a member of some derived class. Truncate LV appropriately. 1922e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // The end of the derived-to-base path for the base object must match the 1923e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // derived-to-base path for the member pointer. 1924b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (LV.Designator.MostDerivedPathLength + MemPtr.Path.size() > 1925e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LV.Designator.Entries.size()) 1926e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return 0; 1927e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith unsigned PathLengthToMember = 1928e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LV.Designator.Entries.size() - MemPtr.Path.size(); 1929e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith for (unsigned I = 0, N = MemPtr.Path.size(); I != N; ++I) { 1930e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const CXXRecordDecl *LVDecl = getAsBaseClass( 1931e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LV.Designator.Entries[PathLengthToMember + I]); 1932e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const CXXRecordDecl *MPDecl = MemPtr.Path[I]; 1933e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (LVDecl->getCanonicalDecl() != MPDecl->getCanonicalDecl()) 1934e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return 0; 1935e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 1936e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 1937e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // Truncate the lvalue to the appropriate derived class. 1938b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (!CastToDerivedClass(Info, BO, LV, MemPtr.getContainingRecord(), 1939b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith PathLengthToMember)) 1940b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return 0; 1941e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } else if (!MemPtr.Path.empty()) { 1942e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // Extend the LValue path with the member pointer's path. 1943e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LV.Designator.Entries.reserve(LV.Designator.Entries.size() + 1944e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith MemPtr.Path.size() + IncludeMember); 1945e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 1946e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // Walk down to the appropriate base class. 1947e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith QualType LVType = BO->getLHS()->getType(); 1948e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (const PointerType *PT = LVType->getAs<PointerType>()) 1949e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LVType = PT->getPointeeType(); 1950e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const CXXRecordDecl *RD = LVType->getAsCXXRecordDecl(); 1951e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith assert(RD && "member pointer access on non-class-type expression"); 1952e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // The first class in the path is that of the lvalue. 1953e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith for (unsigned I = 1, N = MemPtr.Path.size(); I != N; ++I) { 1954e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const CXXRecordDecl *Base = MemPtr.Path[N - I - 1]; 19558d59deec807ed53efcd07855199cdc9c979f447fJohn McCall if (!HandleLValueDirectBase(Info, BO, LV, RD, Base)) 19568d59deec807ed53efcd07855199cdc9c979f447fJohn McCall return 0; 1957e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith RD = Base; 1958e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 1959e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // Finally cast to the class containing the member. 19608d59deec807ed53efcd07855199cdc9c979f447fJohn McCall if (!HandleLValueDirectBase(Info, BO, LV, RD, MemPtr.getContainingRecord())) 19618d59deec807ed53efcd07855199cdc9c979f447fJohn McCall return 0; 1962e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 1963e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 1964e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // Add the member. Note that we cannot build bound member functions here. 1965e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (IncludeMember) { 19668d59deec807ed53efcd07855199cdc9c979f447fJohn McCall if (const FieldDecl *FD = dyn_cast<FieldDecl>(MemPtr.getDecl())) { 19678d59deec807ed53efcd07855199cdc9c979f447fJohn McCall if (!HandleLValueMember(Info, BO, LV, FD)) 19688d59deec807ed53efcd07855199cdc9c979f447fJohn McCall return 0; 19698d59deec807ed53efcd07855199cdc9c979f447fJohn McCall } else if (const IndirectFieldDecl *IFD = 19708d59deec807ed53efcd07855199cdc9c979f447fJohn McCall dyn_cast<IndirectFieldDecl>(MemPtr.getDecl())) { 19718d59deec807ed53efcd07855199cdc9c979f447fJohn McCall if (!HandleLValueIndirectMember(Info, BO, LV, IFD)) 19728d59deec807ed53efcd07855199cdc9c979f447fJohn McCall return 0; 19738d59deec807ed53efcd07855199cdc9c979f447fJohn McCall } else { 1974d9b02e726262e4009dda830998bb934172ac0020Richard Smith llvm_unreachable("can't construct reference to bound member function"); 19758d59deec807ed53efcd07855199cdc9c979f447fJohn McCall } 1976e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 1977e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 1978e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return MemPtr.getDecl(); 1979e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith} 1980e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 1981e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// HandleBaseToDerivedCast - Apply the given base-to-derived cast operation on 1982e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// the provided lvalue, which currently refers to the base object. 1983e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic bool HandleBaseToDerivedCast(EvalInfo &Info, const CastExpr *E, 1984e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LValue &Result) { 1985e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith SubobjectDesignator &D = Result.Designator; 1986b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (D.Invalid || !Result.checkNullPointer(Info, E, CSK_Derived)) 1987e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 1988e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 1989e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith QualType TargetQT = E->getType(); 1990e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (const PointerType *PT = TargetQT->getAs<PointerType>()) 1991e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith TargetQT = PT->getPointeeType(); 1992b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 1993b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // Check this cast lands within the final derived-to-base subobject path. 1994b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (D.MostDerivedPathLength + E->path_size() > D.Entries.size()) { 19955cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.CCEDiag(E, diag::note_constexpr_invalid_downcast) 1996b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith << D.MostDerivedType << TargetQT; 1997b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return false; 1998b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } 1999b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 2000b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // Check the type of the final cast. We don't need to check the path, 2001b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // since a cast can only be formed if the path is unique. 2002b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith unsigned NewEntriesSize = D.Entries.size() - E->path_size(); 2003e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const CXXRecordDecl *TargetType = TargetQT->getAsCXXRecordDecl(); 2004e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const CXXRecordDecl *FinalType; 2005b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (NewEntriesSize == D.MostDerivedPathLength) 2006b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith FinalType = D.MostDerivedType->getAsCXXRecordDecl(); 2007b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith else 2008e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith FinalType = getAsBaseClass(D.Entries[NewEntriesSize - 1]); 2009b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (FinalType->getCanonicalDecl() != TargetType->getCanonicalDecl()) { 20105cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.CCEDiag(E, diag::note_constexpr_invalid_downcast) 2011b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith << D.MostDerivedType << TargetQT; 2012e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 2013b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } 2014e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2015e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // Truncate the lvalue to the appropriate derived class. 2016b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return CastToDerivedClass(Info, E, Result, TargetType, NewEntriesSize); 201759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith} 201859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith 2019c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stumpnamespace { 2020d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smithenum EvalStmtResult { 2021d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith /// Evaluation failed. 2022d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith ESR_Failed, 2023d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith /// Hit a 'return' statement. 2024d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith ESR_Returned, 2025d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith /// Evaluation succeeded. 2026d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith ESR_Succeeded 2027d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith}; 2028d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith} 2029d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 2030d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith// Evaluate a statement. 20311aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smithstatic EvalStmtResult EvaluateStmt(APValue &Result, EvalInfo &Info, 2032d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith const Stmt *S) { 2033d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith switch (S->getStmtClass()) { 2034d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith default: 2035d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith return ESR_Failed; 2036d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 2037d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith case Stmt::NullStmtClass: 2038d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith case Stmt::DeclStmtClass: 2039d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith return ESR_Succeeded; 2040d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 2041c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith case Stmt::ReturnStmtClass: { 2042c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const Expr *RetExpr = cast<ReturnStmt>(S)->getRetValue(); 204383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith if (!Evaluate(Result, Info, RetExpr)) 2044c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return ESR_Failed; 2045c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return ESR_Returned; 2046c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } 2047d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 2048d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith case Stmt::CompoundStmtClass: { 2049d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith const CompoundStmt *CS = cast<CompoundStmt>(S); 2050d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith for (CompoundStmt::const_body_iterator BI = CS->body_begin(), 2051d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith BE = CS->body_end(); BI != BE; ++BI) { 2052d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith EvalStmtResult ESR = EvaluateStmt(Result, Info, *BI); 2053d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith if (ESR != ESR_Succeeded) 2054d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith return ESR; 2055d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith } 2056d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith return ESR_Succeeded; 2057d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith } 2058d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith } 2059d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith} 2060d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 20616180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith/// CheckTrivialDefaultConstructor - Check whether a constructor is a trivial 20626180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith/// default constructor. If so, we'll fold it whether or not it's marked as 20636180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith/// constexpr. If it is marked as constexpr, we will never implicitly define it, 20646180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith/// so we need special handling. 20656180245e9f63d2927b185ec251fb75aba30f1cacRichard Smithstatic bool CheckTrivialDefaultConstructor(EvalInfo &Info, SourceLocation Loc, 206651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith const CXXConstructorDecl *CD, 206751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith bool IsValueInitialization) { 20686180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith if (!CD->isTrivial() || !CD->isDefaultConstructor()) 20696180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith return false; 20706180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith 20714c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith // Value-initialization does not call a trivial default constructor, so such a 20724c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith // call is a core constant expression whether or not the constructor is 20734c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith // constexpr. 20744c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith if (!CD->isConstexpr() && !IsValueInitialization) { 20756180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith if (Info.getLangOpts().CPlusPlus0x) { 20764c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith // FIXME: If DiagDecl is an implicitly-declared special member function, 20774c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith // we should be much more explicit about why it's not constexpr. 20784c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith Info.CCEDiag(Loc, diag::note_constexpr_invalid_function, 1) 20794c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith << /*IsConstexpr*/0 << /*IsConstructor*/1 << CD; 20804c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith Info.Note(CD->getLocation(), diag::note_declared_at); 20816180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith } else { 20826180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith Info.CCEDiag(Loc, diag::note_invalid_subexpr_in_const_expr); 20836180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith } 20846180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith } 20856180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith return true; 20866180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith} 20876180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith 2088c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith/// CheckConstexprFunction - Check that a function can be called in a constant 2089c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith/// expression. 2090c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstatic bool CheckConstexprFunction(EvalInfo &Info, SourceLocation CallLoc, 2091c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const FunctionDecl *Declaration, 2092c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const FunctionDecl *Definition) { 2093745f5147e065900267c85a5568785a1991d4838fRichard Smith // Potential constant expressions can contain calls to declared, but not yet 2094745f5147e065900267c85a5568785a1991d4838fRichard Smith // defined, constexpr functions. 2095745f5147e065900267c85a5568785a1991d4838fRichard Smith if (Info.CheckingPotentialConstantExpression && !Definition && 2096745f5147e065900267c85a5568785a1991d4838fRichard Smith Declaration->isConstexpr()) 2097745f5147e065900267c85a5568785a1991d4838fRichard Smith return false; 2098745f5147e065900267c85a5568785a1991d4838fRichard Smith 2099c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith // Can we evaluate this function call? 2100c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (Definition && Definition->isConstexpr() && !Definition->isInvalidDecl()) 2101c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return true; 2102c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 2103c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (Info.getLangOpts().CPlusPlus0x) { 2104c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const FunctionDecl *DiagDecl = Definition ? Definition : Declaration; 2105099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith // FIXME: If DiagDecl is an implicitly-declared special member function, we 2106099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith // should be much more explicit about why it's not constexpr. 2107c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Info.Diag(CallLoc, diag::note_constexpr_invalid_function, 1) 2108c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith << DiagDecl->isConstexpr() << isa<CXXConstructorDecl>(DiagDecl) 2109c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith << DiagDecl; 2110c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Info.Note(DiagDecl->getLocation(), diag::note_declared_at); 2111c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } else { 2112c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Info.Diag(CallLoc, diag::note_invalid_subexpr_in_const_expr); 2113c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } 2114c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return false; 2115c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith} 2116c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 2117180f47959a066795cc0f409433023af448bb0328Richard Smithnamespace { 21181aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smithtypedef SmallVector<APValue, 8> ArgVector; 2119180f47959a066795cc0f409433023af448bb0328Richard Smith} 2120180f47959a066795cc0f409433023af448bb0328Richard Smith 2121180f47959a066795cc0f409433023af448bb0328Richard Smith/// EvaluateArgs - Evaluate the arguments to a function call. 2122180f47959a066795cc0f409433023af448bb0328Richard Smithstatic bool EvaluateArgs(ArrayRef<const Expr*> Args, ArgVector &ArgValues, 2123180f47959a066795cc0f409433023af448bb0328Richard Smith EvalInfo &Info) { 2124745f5147e065900267c85a5568785a1991d4838fRichard Smith bool Success = true; 2125180f47959a066795cc0f409433023af448bb0328Richard Smith for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end(); 2126745f5147e065900267c85a5568785a1991d4838fRichard Smith I != E; ++I) { 2127745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!Evaluate(ArgValues[I - Args.begin()], Info, *I)) { 2128745f5147e065900267c85a5568785a1991d4838fRichard Smith // If we're checking for a potential constant expression, evaluate all 2129745f5147e065900267c85a5568785a1991d4838fRichard Smith // initializers even if some of them fail. 2130745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!Info.keepEvaluatingAfterFailure()) 2131745f5147e065900267c85a5568785a1991d4838fRichard Smith return false; 2132745f5147e065900267c85a5568785a1991d4838fRichard Smith Success = false; 2133745f5147e065900267c85a5568785a1991d4838fRichard Smith } 2134745f5147e065900267c85a5568785a1991d4838fRichard Smith } 2135745f5147e065900267c85a5568785a1991d4838fRichard Smith return Success; 2136180f47959a066795cc0f409433023af448bb0328Richard Smith} 2137180f47959a066795cc0f409433023af448bb0328Richard Smith 2138d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith/// Evaluate a function call. 2139745f5147e065900267c85a5568785a1991d4838fRichard Smithstatic bool HandleFunctionCall(SourceLocation CallLoc, 2140745f5147e065900267c85a5568785a1991d4838fRichard Smith const FunctionDecl *Callee, const LValue *This, 2141f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith ArrayRef<const Expr*> Args, const Stmt *Body, 21421aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith EvalInfo &Info, APValue &Result) { 2143180f47959a066795cc0f409433023af448bb0328Richard Smith ArgVector ArgValues(Args.size()); 2144180f47959a066795cc0f409433023af448bb0328Richard Smith if (!EvaluateArgs(Args, ArgValues, Info)) 2145180f47959a066795cc0f409433023af448bb0328Richard Smith return false; 2146d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 2147745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!Info.CheckCallLimit(CallLoc)) 2148745f5147e065900267c85a5568785a1991d4838fRichard Smith return false; 2149745f5147e065900267c85a5568785a1991d4838fRichard Smith 2150745f5147e065900267c85a5568785a1991d4838fRichard Smith CallStackFrame Frame(Info, CallLoc, Callee, This, ArgValues.data()); 2151d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith return EvaluateStmt(Result, Info, Body) == ESR_Returned; 2152d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith} 2153d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 2154180f47959a066795cc0f409433023af448bb0328Richard Smith/// Evaluate a constructor call. 2155745f5147e065900267c85a5568785a1991d4838fRichard Smithstatic bool HandleConstructorCall(SourceLocation CallLoc, const LValue &This, 215659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith ArrayRef<const Expr*> Args, 2157180f47959a066795cc0f409433023af448bb0328Richard Smith const CXXConstructorDecl *Definition, 215851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith EvalInfo &Info, APValue &Result) { 2159180f47959a066795cc0f409433023af448bb0328Richard Smith ArgVector ArgValues(Args.size()); 2160180f47959a066795cc0f409433023af448bb0328Richard Smith if (!EvaluateArgs(Args, ArgValues, Info)) 2161180f47959a066795cc0f409433023af448bb0328Richard Smith return false; 2162180f47959a066795cc0f409433023af448bb0328Richard Smith 2163745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!Info.CheckCallLimit(CallLoc)) 2164745f5147e065900267c85a5568785a1991d4838fRichard Smith return false; 2165745f5147e065900267c85a5568785a1991d4838fRichard Smith 216686c3ae46250cdcc57778c27826060779a92f3815Richard Smith const CXXRecordDecl *RD = Definition->getParent(); 216786c3ae46250cdcc57778c27826060779a92f3815Richard Smith if (RD->getNumVBases()) { 216886c3ae46250cdcc57778c27826060779a92f3815Richard Smith Info.Diag(CallLoc, diag::note_constexpr_virtual_base) << RD; 216986c3ae46250cdcc57778c27826060779a92f3815Richard Smith return false; 217086c3ae46250cdcc57778c27826060779a92f3815Richard Smith } 217186c3ae46250cdcc57778c27826060779a92f3815Richard Smith 2172745f5147e065900267c85a5568785a1991d4838fRichard Smith CallStackFrame Frame(Info, CallLoc, Definition, &This, ArgValues.data()); 2173180f47959a066795cc0f409433023af448bb0328Richard Smith 2174180f47959a066795cc0f409433023af448bb0328Richard Smith // If it's a delegating constructor, just delegate. 2175180f47959a066795cc0f409433023af448bb0328Richard Smith if (Definition->isDelegatingConstructor()) { 2176180f47959a066795cc0f409433023af448bb0328Richard Smith CXXConstructorDecl::init_const_iterator I = Definition->init_begin(); 217783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith return EvaluateInPlace(Result, Info, This, (*I)->getInit()); 2178180f47959a066795cc0f409433023af448bb0328Richard Smith } 2179180f47959a066795cc0f409433023af448bb0328Richard Smith 2180610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith // For a trivial copy or move constructor, perform an APValue copy. This is 2181610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith // essential for unions, where the operations performed by the constructor 2182610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith // cannot be represented by ctor-initializers. 2183610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith if (Definition->isDefaulted() && 2184f6cfe8ba2b4d98c20181568e449edf0b60904b03Douglas Gregor ((Definition->isCopyConstructor() && Definition->isTrivial()) || 2185f6cfe8ba2b4d98c20181568e449edf0b60904b03Douglas Gregor (Definition->isMoveConstructor() && Definition->isTrivial()))) { 2186610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith LValue RHS; 21871aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith RHS.setFrom(Info.Ctx, ArgValues[0]); 21881aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith return HandleLValueToRValueConversion(Info, Args[0], Args[0]->getType(), 21891aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith RHS, Result); 2190610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith } 2191610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith 2192610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith // Reserve space for the struct members. 219351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (!RD->isUnion() && Result.isUninit()) 2194180f47959a066795cc0f409433023af448bb0328Richard Smith Result = APValue(APValue::UninitStruct(), RD->getNumBases(), 2195180f47959a066795cc0f409433023af448bb0328Richard Smith std::distance(RD->field_begin(), RD->field_end())); 2196180f47959a066795cc0f409433023af448bb0328Richard Smith 21978d59deec807ed53efcd07855199cdc9c979f447fJohn McCall if (RD->isInvalidDecl()) return false; 2198180f47959a066795cc0f409433023af448bb0328Richard Smith const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); 2199180f47959a066795cc0f409433023af448bb0328Richard Smith 2200745f5147e065900267c85a5568785a1991d4838fRichard Smith bool Success = true; 2201180f47959a066795cc0f409433023af448bb0328Richard Smith unsigned BasesSeen = 0; 2202180f47959a066795cc0f409433023af448bb0328Richard Smith#ifndef NDEBUG 2203180f47959a066795cc0f409433023af448bb0328Richard Smith CXXRecordDecl::base_class_const_iterator BaseIt = RD->bases_begin(); 2204180f47959a066795cc0f409433023af448bb0328Richard Smith#endif 2205180f47959a066795cc0f409433023af448bb0328Richard Smith for (CXXConstructorDecl::init_const_iterator I = Definition->init_begin(), 2206180f47959a066795cc0f409433023af448bb0328Richard Smith E = Definition->init_end(); I != E; ++I) { 2207745f5147e065900267c85a5568785a1991d4838fRichard Smith LValue Subobject = This; 2208745f5147e065900267c85a5568785a1991d4838fRichard Smith APValue *Value = &Result; 2209745f5147e065900267c85a5568785a1991d4838fRichard Smith 2210745f5147e065900267c85a5568785a1991d4838fRichard Smith // Determine the subobject to initialize. 2211180f47959a066795cc0f409433023af448bb0328Richard Smith if ((*I)->isBaseInitializer()) { 2212180f47959a066795cc0f409433023af448bb0328Richard Smith QualType BaseType((*I)->getBaseClass(), 0); 2213180f47959a066795cc0f409433023af448bb0328Richard Smith#ifndef NDEBUG 2214180f47959a066795cc0f409433023af448bb0328Richard Smith // Non-virtual base classes are initialized in the order in the class 221586c3ae46250cdcc57778c27826060779a92f3815Richard Smith // definition. We have already checked for virtual base classes. 2216180f47959a066795cc0f409433023af448bb0328Richard Smith assert(!BaseIt->isVirtual() && "virtual base for literal type"); 2217180f47959a066795cc0f409433023af448bb0328Richard Smith assert(Info.Ctx.hasSameType(BaseIt->getType(), BaseType) && 2218180f47959a066795cc0f409433023af448bb0328Richard Smith "base class initializers not in expected order"); 2219180f47959a066795cc0f409433023af448bb0328Richard Smith ++BaseIt; 2220180f47959a066795cc0f409433023af448bb0328Richard Smith#endif 22218d59deec807ed53efcd07855199cdc9c979f447fJohn McCall if (!HandleLValueDirectBase(Info, (*I)->getInit(), Subobject, RD, 22228d59deec807ed53efcd07855199cdc9c979f447fJohn McCall BaseType->getAsCXXRecordDecl(), &Layout)) 22238d59deec807ed53efcd07855199cdc9c979f447fJohn McCall return false; 2224745f5147e065900267c85a5568785a1991d4838fRichard Smith Value = &Result.getStructBase(BasesSeen++); 2225180f47959a066795cc0f409433023af448bb0328Richard Smith } else if (FieldDecl *FD = (*I)->getMember()) { 22268d59deec807ed53efcd07855199cdc9c979f447fJohn McCall if (!HandleLValueMember(Info, (*I)->getInit(), Subobject, FD, &Layout)) 22278d59deec807ed53efcd07855199cdc9c979f447fJohn McCall return false; 2228180f47959a066795cc0f409433023af448bb0328Richard Smith if (RD->isUnion()) { 2229180f47959a066795cc0f409433023af448bb0328Richard Smith Result = APValue(FD); 2230745f5147e065900267c85a5568785a1991d4838fRichard Smith Value = &Result.getUnionValue(); 2231745f5147e065900267c85a5568785a1991d4838fRichard Smith } else { 2232745f5147e065900267c85a5568785a1991d4838fRichard Smith Value = &Result.getStructField(FD->getFieldIndex()); 2233745f5147e065900267c85a5568785a1991d4838fRichard Smith } 2234d9b02e726262e4009dda830998bb934172ac0020Richard Smith } else if (IndirectFieldDecl *IFD = (*I)->getIndirectMember()) { 2235d9b02e726262e4009dda830998bb934172ac0020Richard Smith // Walk the indirect field decl's chain to find the object to initialize, 2236d9b02e726262e4009dda830998bb934172ac0020Richard Smith // and make sure we've initialized every step along it. 2237d9b02e726262e4009dda830998bb934172ac0020Richard Smith for (IndirectFieldDecl::chain_iterator C = IFD->chain_begin(), 2238d9b02e726262e4009dda830998bb934172ac0020Richard Smith CE = IFD->chain_end(); 2239d9b02e726262e4009dda830998bb934172ac0020Richard Smith C != CE; ++C) { 2240d9b02e726262e4009dda830998bb934172ac0020Richard Smith FieldDecl *FD = cast<FieldDecl>(*C); 2241d9b02e726262e4009dda830998bb934172ac0020Richard Smith CXXRecordDecl *CD = cast<CXXRecordDecl>(FD->getParent()); 2242d9b02e726262e4009dda830998bb934172ac0020Richard Smith // Switch the union field if it differs. This happens if we had 2243d9b02e726262e4009dda830998bb934172ac0020Richard Smith // preceding zero-initialization, and we're now initializing a union 2244d9b02e726262e4009dda830998bb934172ac0020Richard Smith // subobject other than the first. 2245d9b02e726262e4009dda830998bb934172ac0020Richard Smith // FIXME: In this case, the values of the other subobjects are 2246d9b02e726262e4009dda830998bb934172ac0020Richard Smith // specified, since zero-initialization sets all padding bits to zero. 2247d9b02e726262e4009dda830998bb934172ac0020Richard Smith if (Value->isUninit() || 2248d9b02e726262e4009dda830998bb934172ac0020Richard Smith (Value->isUnion() && Value->getUnionField() != FD)) { 2249d9b02e726262e4009dda830998bb934172ac0020Richard Smith if (CD->isUnion()) 2250d9b02e726262e4009dda830998bb934172ac0020Richard Smith *Value = APValue(FD); 2251d9b02e726262e4009dda830998bb934172ac0020Richard Smith else 2252d9b02e726262e4009dda830998bb934172ac0020Richard Smith *Value = APValue(APValue::UninitStruct(), CD->getNumBases(), 2253d9b02e726262e4009dda830998bb934172ac0020Richard Smith std::distance(CD->field_begin(), CD->field_end())); 2254d9b02e726262e4009dda830998bb934172ac0020Richard Smith } 22558d59deec807ed53efcd07855199cdc9c979f447fJohn McCall if (!HandleLValueMember(Info, (*I)->getInit(), Subobject, FD)) 22568d59deec807ed53efcd07855199cdc9c979f447fJohn McCall return false; 2257d9b02e726262e4009dda830998bb934172ac0020Richard Smith if (CD->isUnion()) 2258d9b02e726262e4009dda830998bb934172ac0020Richard Smith Value = &Value->getUnionValue(); 2259d9b02e726262e4009dda830998bb934172ac0020Richard Smith else 2260d9b02e726262e4009dda830998bb934172ac0020Richard Smith Value = &Value->getStructField(FD->getFieldIndex()); 2261d9b02e726262e4009dda830998bb934172ac0020Richard Smith } 2262180f47959a066795cc0f409433023af448bb0328Richard Smith } else { 2263d9b02e726262e4009dda830998bb934172ac0020Richard Smith llvm_unreachable("unknown base initializer kind"); 2264180f47959a066795cc0f409433023af448bb0328Richard Smith } 2265745f5147e065900267c85a5568785a1991d4838fRichard Smith 226683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith if (!EvaluateInPlace(*Value, Info, Subobject, (*I)->getInit(), 226783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith (*I)->isBaseInitializer() 2268745f5147e065900267c85a5568785a1991d4838fRichard Smith ? CCEK_Constant : CCEK_MemberInit)) { 2269745f5147e065900267c85a5568785a1991d4838fRichard Smith // If we're checking for a potential constant expression, evaluate all 2270745f5147e065900267c85a5568785a1991d4838fRichard Smith // initializers even if some of them fail. 2271745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!Info.keepEvaluatingAfterFailure()) 2272745f5147e065900267c85a5568785a1991d4838fRichard Smith return false; 2273745f5147e065900267c85a5568785a1991d4838fRichard Smith Success = false; 2274745f5147e065900267c85a5568785a1991d4838fRichard Smith } 2275180f47959a066795cc0f409433023af448bb0328Richard Smith } 2276180f47959a066795cc0f409433023af448bb0328Richard Smith 2277745f5147e065900267c85a5568785a1991d4838fRichard Smith return Success; 2278180f47959a066795cc0f409433023af448bb0328Richard Smith} 2279180f47959a066795cc0f409433023af448bb0328Richard Smith 22804efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman//===----------------------------------------------------------------------===// 22818cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne// Generic Evaluation 22828cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne//===----------------------------------------------------------------------===// 22838cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournenamespace { 22848cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 2285f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith// FIXME: RetTy is always bool. Remove it. 2286f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithtemplate <class Derived, typename RetTy=bool> 22878cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourneclass ExprEvaluatorBase 22888cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne : public ConstStmtVisitor<Derived, RetTy> { 22898cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourneprivate: 22901aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith RetTy DerivedSuccess(const APValue &V, const Expr *E) { 22918cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return static_cast<Derived*>(this)->Success(V, E); 22928cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne } 229351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith RetTy DerivedZeroInitialization(const Expr *E) { 229451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return static_cast<Derived*>(this)->ZeroInitialization(E); 2295f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith } 22968cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 229774e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith // Check whether a conditional operator with a non-constant condition is a 229874e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith // potential constant expression. If neither arm is a potential constant 229974e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith // expression, then the conditional operator is not either. 230074e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith template<typename ConditionalOperator> 230174e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith void CheckPotentialConstantConditional(const ConditionalOperator *E) { 230274e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith assert(Info.CheckingPotentialConstantExpression); 230374e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith 230474e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith // Speculatively evaluate both arms. 230574e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith { 230674e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith llvm::SmallVector<PartialDiagnosticAt, 8> Diag; 230774e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith SpeculativeEvaluationRAII Speculate(Info, &Diag); 230874e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith 230974e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith StmtVisitorTy::Visit(E->getFalseExpr()); 231074e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith if (Diag.empty()) 231174e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith return; 231274e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith 231374e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith Diag.clear(); 231474e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith StmtVisitorTy::Visit(E->getTrueExpr()); 231574e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith if (Diag.empty()) 231674e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith return; 231774e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith } 231874e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith 231974e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith Error(E, diag::note_constexpr_conditional_never_const); 232074e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith } 232174e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith 232274e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith 232374e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith template<typename ConditionalOperator> 232474e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith bool HandleConditionalOperator(const ConditionalOperator *E) { 232574e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith bool BoolResult; 232674e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info)) { 232774e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith if (Info.CheckingPotentialConstantExpression) 232874e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith CheckPotentialConstantConditional(E); 232974e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith return false; 233074e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith } 233174e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith 233274e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith Expr *EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); 233374e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith return StmtVisitorTy::Visit(EvalExpr); 233474e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith } 233574e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith 23368cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourneprotected: 23378cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne EvalInfo &Info; 23388cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne typedef ConstStmtVisitor<Derived, RetTy> StmtVisitorTy; 23398cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne typedef ExprEvaluatorBase ExprEvaluatorBaseTy; 23408cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 2341dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { 23425cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith return Info.CCEDiag(E, D); 2343f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 2344f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 2345cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis RetTy ZeroInitialization(const Expr *E) { return Error(E); } 2346cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 2347cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidispublic: 2348cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {} 2349cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 2350cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis EvalInfo &getEvalInfo() { return Info; } 2351cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 2352f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith /// Report an evaluation error. This should only be called when an error is 2353f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith /// first discovered. When propagating an error, just return false. 2354f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith bool Error(const Expr *E, diag::kind D) { 23555cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.Diag(E, D); 2356f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 2357f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 2358f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith bool Error(const Expr *E) { 2359f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E, diag::note_invalid_subexpr_in_const_expr); 2360f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 2361f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 23628cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne RetTy VisitStmt(const Stmt *) { 2363b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie llvm_unreachable("Expression evaluator should not be called on stmts"); 23648cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne } 23658cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne RetTy VisitExpr(const Expr *E) { 2366f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 23678cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne } 23688cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 23698cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne RetTy VisitParenExpr(const ParenExpr *E) 23708cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne { return StmtVisitorTy::Visit(E->getSubExpr()); } 23718cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne RetTy VisitUnaryExtension(const UnaryOperator *E) 23728cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne { return StmtVisitorTy::Visit(E->getSubExpr()); } 23738cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne RetTy VisitUnaryPlus(const UnaryOperator *E) 23748cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne { return StmtVisitorTy::Visit(E->getSubExpr()); } 23758cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne RetTy VisitChooseExpr(const ChooseExpr *E) 23768cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne { return StmtVisitorTy::Visit(E->getChosenSubExpr(Info.Ctx)); } 23778cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne RetTy VisitGenericSelectionExpr(const GenericSelectionExpr *E) 23788cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne { return StmtVisitorTy::Visit(E->getResultExpr()); } 237991a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall RetTy VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E) 238091a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall { return StmtVisitorTy::Visit(E->getReplacement()); } 23813d75ca836205856077c18e30e9447accbd85f751Richard Smith RetTy VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) 23823d75ca836205856077c18e30e9447accbd85f751Richard Smith { return StmtVisitorTy::Visit(E->getExpr()); } 2383bc6abe93a5d6b1305411f8b6f54c2caa686ddc69Richard Smith // We cannot create any objects for which cleanups are required, so there is 2384bc6abe93a5d6b1305411f8b6f54c2caa686ddc69Richard Smith // nothing to do here; all cleanups must come from unevaluated subexpressions. 2385bc6abe93a5d6b1305411f8b6f54c2caa686ddc69Richard Smith RetTy VisitExprWithCleanups(const ExprWithCleanups *E) 2386bc6abe93a5d6b1305411f8b6f54c2caa686ddc69Richard Smith { return StmtVisitorTy::Visit(E->getSubExpr()); } 23878cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 2388c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith RetTy VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E) { 2389c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith CCEDiag(E, diag::note_constexpr_invalid_cast) << 0; 2390c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith return static_cast<Derived*>(this)->VisitCastExpr(E); 2391c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith } 2392c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith RetTy VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) { 2393c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith CCEDiag(E, diag::note_constexpr_invalid_cast) << 1; 2394c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith return static_cast<Derived*>(this)->VisitCastExpr(E); 2395c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith } 2396c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith 2397e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith RetTy VisitBinaryOperator(const BinaryOperator *E) { 2398e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith switch (E->getOpcode()) { 2399e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith default: 2400f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 2401e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2402e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case BO_Comma: 2403e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith VisitIgnoredValue(E->getLHS()); 2404e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return StmtVisitorTy::Visit(E->getRHS()); 2405e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2406e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case BO_PtrMemD: 2407e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case BO_PtrMemI: { 2408e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LValue Obj; 2409e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!HandleMemberPointerAccess(Info, E, Obj)) 2410e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 24111aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith APValue Result; 2412f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!HandleLValueToRValueConversion(Info, E, E->getType(), Obj, Result)) 2413e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 2414e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return DerivedSuccess(Result, E); 2415e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2416e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2417e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2418e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 24198cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne RetTy VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) { 2420e92b1f4917bfb669a09d220dc979fc3676df4da8Richard Smith // Evaluate and cache the common expression. We treat it as a temporary, 2421e92b1f4917bfb669a09d220dc979fc3676df4da8Richard Smith // even though it's not quite the same thing. 2422e92b1f4917bfb669a09d220dc979fc3676df4da8Richard Smith if (!Evaluate(Info.CurrentCall->Temporaries[E->getOpaqueValue()], 2423e92b1f4917bfb669a09d220dc979fc3676df4da8Richard Smith Info, E->getCommon())) 2424f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 24258cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 242674e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith return HandleConditionalOperator(E); 24278cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne } 24288cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 24298cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne RetTy VisitConditionalOperator(const ConditionalOperator *E) { 2430f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith bool IsBcpCall = false; 2431f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // If the condition (ignoring parens) is a __builtin_constant_p call, 2432f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // the result is a constant expression if it can be folded without 2433f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // side-effects. This is an important GNU extension. See GCC PR38377 2434f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // for discussion. 2435f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (const CallExpr *CallCE = 2436f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith dyn_cast<CallExpr>(E->getCond()->IgnoreParenCasts())) 2437f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p) 2438f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith IsBcpCall = true; 2439f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 2440f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // Always assume __builtin_constant_p(...) ? ... : ... is a potential 2441f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // constant expression; we can't check whether it's potentially foldable. 2442f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (Info.CheckingPotentialConstantExpression && IsBcpCall) 2443f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith return false; 2444f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 2445f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith FoldConstant Fold(Info); 2446f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 244774e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith if (!HandleConditionalOperator(E)) 2448f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith return false; 2449f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 2450f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (IsBcpCall) 2451f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith Fold.Fold(Info); 2452f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 2453f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith return true; 24548cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne } 24558cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 24568cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne RetTy VisitOpaqueValueExpr(const OpaqueValueExpr *E) { 2457e92b1f4917bfb669a09d220dc979fc3676df4da8Richard Smith APValue &Value = Info.CurrentCall->Temporaries[E]; 2458e92b1f4917bfb669a09d220dc979fc3676df4da8Richard Smith if (Value.isUninit()) { 245942786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis const Expr *Source = E->getSourceExpr(); 246042786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis if (!Source) 2461f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 246242786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis if (Source == E) { // sanity checking. 246342786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis assert(0 && "OpaqueValueExpr recursively refers to itself"); 2464f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 246542786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis } 246642786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis return StmtVisitorTy::Visit(Source); 246742786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis } 2468e92b1f4917bfb669a09d220dc979fc3676df4da8Richard Smith return DerivedSuccess(Value, E); 24698cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne } 2470f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith 2471d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith RetTy VisitCallExpr(const CallExpr *E) { 2472e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const Expr *Callee = E->getCallee()->IgnoreParens(); 2473d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith QualType CalleeType = Callee->getType(); 2474d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 24756142ca7790aa09a6e13592b70f142cc4bbcadcaeDevang Patel const FunctionDecl *FD = 0; 247659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith LValue *This = 0, ThisVal; 247759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs()); 247886c3ae46250cdcc57778c27826060779a92f3815Richard Smith bool HasQualifier = false; 247959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith 248059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith // Extract function decl and 'this' pointer from the callee. 248159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith if (CalleeType->isSpecificBuiltinType(BuiltinType::BoundMember)) { 2482f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith const ValueDecl *Member = 0; 2483e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (const MemberExpr *ME = dyn_cast<MemberExpr>(Callee)) { 2484e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // Explicit bound member calls, such as x.f() or p->g(); 2485e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!EvaluateObjectArgument(Info, ME->getBase(), ThisVal)) 2486f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 2487f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith Member = ME->getMemberDecl(); 2488e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith This = &ThisVal; 248986c3ae46250cdcc57778c27826060779a92f3815Richard Smith HasQualifier = ME->hasQualifier(); 2490e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(Callee)) { 2491e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // Indirect bound member calls ('.*' or '->*'). 2492f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith Member = HandleMemberPointerAccess(Info, BE, ThisVal, false); 2493f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!Member) return false; 2494e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith This = &ThisVal; 2495e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } else 2496f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(Callee); 2497f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 2498f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith FD = dyn_cast<FunctionDecl>(Member); 2499f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!FD) 2500f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(Callee); 250159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith } else if (CalleeType->isFunctionPointerType()) { 2502b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith LValue Call; 2503b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (!EvaluatePointer(Callee, Call, Info)) 2504f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 250559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith 2506b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (!Call.getLValueOffset().isZero()) 2507f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(Callee); 25081bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith FD = dyn_cast_or_null<FunctionDecl>( 25091bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith Call.getLValueBase().dyn_cast<const ValueDecl*>()); 251059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith if (!FD) 2511f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(Callee); 251259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith 251359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith // Overloaded operator calls to member functions are represented as normal 251459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith // calls with '*this' as the first argument. 251559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); 251659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith if (MD && !MD->isStatic()) { 2517f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith // FIXME: When selecting an implicit conversion for an overloaded 2518f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith // operator delete, we sometimes try to evaluate calls to conversion 2519f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith // operators without a 'this' parameter! 2520f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (Args.empty()) 2521f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 2522f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 252359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith if (!EvaluateObjectArgument(Info, Args[0], ThisVal)) 252459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith return false; 252559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith This = &ThisVal; 252659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith Args = Args.slice(1); 252759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith } 2528d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 252959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith // Don't call function pointers which have been cast to some other type. 253059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith if (!Info.Ctx.hasSameType(CalleeType->getPointeeType(), FD->getType())) 2531f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 253259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith } else 2533f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 2534d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 2535b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith if (This && !This->checkSubobject(Info, E, CSK_This)) 2536b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith return false; 2537b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith 253886c3ae46250cdcc57778c27826060779a92f3815Richard Smith // DR1358 allows virtual constexpr functions in some cases. Don't allow 253986c3ae46250cdcc57778c27826060779a92f3815Richard Smith // calls to such functions in constant expressions. 254086c3ae46250cdcc57778c27826060779a92f3815Richard Smith if (This && !HasQualifier && 254186c3ae46250cdcc57778c27826060779a92f3815Richard Smith isa<CXXMethodDecl>(FD) && cast<CXXMethodDecl>(FD)->isVirtual()) 254286c3ae46250cdcc57778c27826060779a92f3815Richard Smith return Error(E, diag::note_constexpr_virtual_call); 254386c3ae46250cdcc57778c27826060779a92f3815Richard Smith 2544c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const FunctionDecl *Definition = 0; 2545d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith Stmt *Body = FD->getBody(Definition); 25461aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith APValue Result; 2547d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 2548c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition) || 2549745f5147e065900267c85a5568785a1991d4838fRichard Smith !HandleFunctionCall(E->getExprLoc(), Definition, This, Args, Body, 2550745f5147e065900267c85a5568785a1991d4838fRichard Smith Info, Result)) 2551f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 2552d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 255383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith return DerivedSuccess(Result, E); 2554d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith } 2555d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 2556c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith RetTy VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { 2557c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return StmtVisitorTy::Visit(E->getInitializer()); 2558c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith } 2559f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith RetTy VisitInitListExpr(const InitListExpr *E) { 256071523d6c41e1599fc42f420d02dd2895fd8f65d4Eli Friedman if (E->getNumInits() == 0) 256171523d6c41e1599fc42f420d02dd2895fd8f65d4Eli Friedman return DerivedZeroInitialization(E); 256271523d6c41e1599fc42f420d02dd2895fd8f65d4Eli Friedman if (E->getNumInits() == 1) 256371523d6c41e1599fc42f420d02dd2895fd8f65d4Eli Friedman return StmtVisitorTy::Visit(E->getInit(0)); 2564f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 2565f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith } 2566f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith RetTy VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { 256751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return DerivedZeroInitialization(E); 2568f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith } 2569f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith RetTy VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) { 257051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return DerivedZeroInitialization(E); 2571f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith } 2572e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith RetTy VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) { 257351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return DerivedZeroInitialization(E); 2574e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2575f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith 2576180f47959a066795cc0f409433023af448bb0328Richard Smith /// A member expression where the object is a prvalue is itself a prvalue. 2577180f47959a066795cc0f409433023af448bb0328Richard Smith RetTy VisitMemberExpr(const MemberExpr *E) { 2578180f47959a066795cc0f409433023af448bb0328Richard Smith assert(!E->isArrow() && "missing call to bound member function?"); 2579180f47959a066795cc0f409433023af448bb0328Richard Smith 25801aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith APValue Val; 2581180f47959a066795cc0f409433023af448bb0328Richard Smith if (!Evaluate(Val, Info, E->getBase())) 2582180f47959a066795cc0f409433023af448bb0328Richard Smith return false; 2583180f47959a066795cc0f409433023af448bb0328Richard Smith 2584180f47959a066795cc0f409433023af448bb0328Richard Smith QualType BaseTy = E->getBase()->getType(); 2585180f47959a066795cc0f409433023af448bb0328Richard Smith 2586180f47959a066795cc0f409433023af448bb0328Richard Smith const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); 2587f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!FD) return Error(E); 2588180f47959a066795cc0f409433023af448bb0328Richard Smith assert(!FD->getType()->isReferenceType() && "prvalue reference?"); 2589180f47959a066795cc0f409433023af448bb0328Richard Smith assert(BaseTy->getAs<RecordType>()->getDecl()->getCanonicalDecl() == 2590180f47959a066795cc0f409433023af448bb0328Richard Smith FD->getParent()->getCanonicalDecl() && "record / field mismatch"); 2591180f47959a066795cc0f409433023af448bb0328Richard Smith 2592b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith SubobjectDesignator Designator(BaseTy); 2593b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Designator.addDeclUnchecked(FD); 2594180f47959a066795cc0f409433023af448bb0328Richard Smith 2595f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return ExtractSubobject(Info, E, Val, BaseTy, Designator, E->getType()) && 2596180f47959a066795cc0f409433023af448bb0328Richard Smith DerivedSuccess(Val, E); 2597180f47959a066795cc0f409433023af448bb0328Richard Smith } 2598180f47959a066795cc0f409433023af448bb0328Richard Smith 2599c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith RetTy VisitCastExpr(const CastExpr *E) { 2600c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith switch (E->getCastKind()) { 2601c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith default: 2602c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith break; 2603c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 26047a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall case CK_AtomicToNonAtomic: 26057a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall case CK_NonAtomicToAtomic: 2606c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case CK_NoOp: 26077d580a4e9e47dffc3c17aa2b957ac57ca3c4e451Richard Smith case CK_UserDefinedConversion: 2608c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return StmtVisitorTy::Visit(E->getSubExpr()); 2609c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 2610c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case CK_LValueToRValue: { 2611c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith LValue LVal; 2612f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!EvaluateLValue(E->getSubExpr(), LVal, Info)) 2613f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 26141aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith APValue RVal; 26159ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith // Note, we use the subexpression's type in order to retain cv-qualifiers. 26169ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith if (!HandleLValueToRValueConversion(Info, E, E->getSubExpr()->getType(), 26179ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith LVal, RVal)) 2618f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 2619f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return DerivedSuccess(RVal, E); 2620c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith } 2621c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith } 2622c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 2623f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 2624c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith } 2625c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 26268327fad71da34492d82c532f42a58cb4baff81a3Richard Smith /// Visit a value which is evaluated, but whose value is ignored. 26278327fad71da34492d82c532f42a58cb4baff81a3Richard Smith void VisitIgnoredValue(const Expr *E) { 26281aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith APValue Scratch; 26298327fad71da34492d82c532f42a58cb4baff81a3Richard Smith if (!Evaluate(Scratch, Info, E)) 26308327fad71da34492d82c532f42a58cb4baff81a3Richard Smith Info.EvalStatus.HasSideEffects = true; 26318327fad71da34492d82c532f42a58cb4baff81a3Richard Smith } 26328cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne}; 26338cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 26348cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne} 26358cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 26368cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne//===----------------------------------------------------------------------===// 2637e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// Common base class for lvalue and temporary evaluation. 2638e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===// 2639e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithnamespace { 2640e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithtemplate<class Derived> 2641e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithclass LValueExprEvaluatorBase 2642e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith : public ExprEvaluatorBase<Derived, bool> { 2643e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithprotected: 2644e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LValue &Result; 2645e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith typedef LValueExprEvaluatorBase LValueExprEvaluatorBaseTy; 2646e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith typedef ExprEvaluatorBase<Derived, bool> ExprEvaluatorBaseTy; 2647e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2648e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool Success(APValue::LValueBase B) { 2649e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Result.set(B); 2650e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 2651e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2652e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2653e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithpublic: 2654e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LValueExprEvaluatorBase(EvalInfo &Info, LValue &Result) : 2655e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith ExprEvaluatorBaseTy(Info), Result(Result) {} 2656e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 26571aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith bool Success(const APValue &V, const Expr *E) { 26581aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith Result.setFrom(this->Info.Ctx, V); 2659e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 2660e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2661e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2662e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool VisitMemberExpr(const MemberExpr *E) { 2663e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // Handle non-static data members. 2664e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith QualType BaseTy; 2665e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (E->isArrow()) { 2666e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!EvaluatePointer(E->getBase(), Result, this->Info)) 2667e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 2668e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith BaseTy = E->getBase()->getType()->getAs<PointerType>()->getPointeeType(); 2669c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } else if (E->getBase()->isRValue()) { 2670af2c7a194592401394233b7cbcdd3cfd0a7a38ddRichard Smith assert(E->getBase()->getType()->isRecordType()); 2671c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (!EvaluateTemporary(E->getBase(), Result, this->Info)) 2672c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return false; 2673c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith BaseTy = E->getBase()->getType(); 2674e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } else { 2675e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!this->Visit(E->getBase())) 2676e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 2677e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith BaseTy = E->getBase()->getType(); 2678e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2679e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2680d9b02e726262e4009dda830998bb934172ac0020Richard Smith const ValueDecl *MD = E->getMemberDecl(); 2681d9b02e726262e4009dda830998bb934172ac0020Richard Smith if (const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) { 2682d9b02e726262e4009dda830998bb934172ac0020Richard Smith assert(BaseTy->getAs<RecordType>()->getDecl()->getCanonicalDecl() == 2683d9b02e726262e4009dda830998bb934172ac0020Richard Smith FD->getParent()->getCanonicalDecl() && "record / field mismatch"); 2684d9b02e726262e4009dda830998bb934172ac0020Richard Smith (void)BaseTy; 26858d59deec807ed53efcd07855199cdc9c979f447fJohn McCall if (!HandleLValueMember(this->Info, E, Result, FD)) 26868d59deec807ed53efcd07855199cdc9c979f447fJohn McCall return false; 2687d9b02e726262e4009dda830998bb934172ac0020Richard Smith } else if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(MD)) { 26888d59deec807ed53efcd07855199cdc9c979f447fJohn McCall if (!HandleLValueIndirectMember(this->Info, E, Result, IFD)) 26898d59deec807ed53efcd07855199cdc9c979f447fJohn McCall return false; 2690d9b02e726262e4009dda830998bb934172ac0020Richard Smith } else 2691d9b02e726262e4009dda830998bb934172ac0020Richard Smith return this->Error(E); 2692e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2693d9b02e726262e4009dda830998bb934172ac0020Richard Smith if (MD->getType()->isReferenceType()) { 26941aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith APValue RefValue; 2695d9b02e726262e4009dda830998bb934172ac0020Richard Smith if (!HandleLValueToRValueConversion(this->Info, E, MD->getType(), Result, 2696e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith RefValue)) 2697e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 2698e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return Success(RefValue, E); 2699e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2700e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 2701e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2702e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2703e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool VisitBinaryOperator(const BinaryOperator *E) { 2704e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith switch (E->getOpcode()) { 2705e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith default: 2706e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return ExprEvaluatorBaseTy::VisitBinaryOperator(E); 2707e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2708e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case BO_PtrMemD: 2709e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case BO_PtrMemI: 2710e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return HandleMemberPointerAccess(this->Info, E, Result); 2711e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2712e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2713e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2714e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool VisitCastExpr(const CastExpr *E) { 2715e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith switch (E->getCastKind()) { 2716e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith default: 2717e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return ExprEvaluatorBaseTy::VisitCastExpr(E); 2718e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2719e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case CK_DerivedToBase: 2720e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case CK_UncheckedDerivedToBase: { 2721e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!this->Visit(E->getSubExpr())) 2722e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 2723e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2724e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // Now figure out the necessary offset to add to the base LV to get from 2725e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // the derived class to the base class. 2726e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith QualType Type = E->getSubExpr()->getType(); 2727e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2728e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith for (CastExpr::path_const_iterator PathI = E->path_begin(), 2729e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith PathE = E->path_end(); PathI != PathE; ++PathI) { 2730b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (!HandleLValueBase(this->Info, E, Result, Type->getAsCXXRecordDecl(), 2731e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith *PathI)) 2732e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 2733e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Type = (*PathI)->getType(); 2734e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2735e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2736e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 2737e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2738e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2739e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2740e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}; 2741e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith} 2742e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2743e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===// 27444efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman// LValue Evaluation 2745c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// 2746c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// This is used for evaluating lvalues (in C and C++), xvalues (in C++11), 2747c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// function designators (in C), decl references to void objects (in C), and 2748c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// temporaries (if building with -Wno-address-of-temporary). 2749c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// 2750c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// LValue evaluation produces values comprising a base expression of one of the 2751c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// following types: 27521bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith// - Declarations 27531bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith// * VarDecl 27541bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith// * FunctionDecl 27551bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith// - Literals 2756c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// * CompoundLiteralExpr in C 2757c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// * StringLiteral 275847d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith// * CXXTypeidExpr 2759c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// * PredefinedExpr 2760180f47959a066795cc0f409433023af448bb0328Richard Smith// * ObjCStringLiteralExpr 2761c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// * ObjCEncodeExpr 2762c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// * AddrLabelExpr 2763c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// * BlockExpr 2764c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// * CallExpr for a MakeStringConstant builtin 27651bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith// - Locals and temporaries 276683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith// * Any Expr, with a CallIndex indicating the function in which the temporary 276783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith// was evaluated. 27681bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith// plus an offset in bytes. 27694efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman//===----------------------------------------------------------------------===// 27704efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedmannamespace { 2771770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass LValueExprEvaluator 2772e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith : public LValueExprEvaluatorBase<LValueExprEvaluator> { 27734efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedmanpublic: 2774e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LValueExprEvaluator(EvalInfo &Info, LValue &Result) : 2775e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LValueExprEvaluatorBaseTy(Info, Result) {} 27761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2777c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith bool VisitVarDecl(const Expr *E, const VarDecl *VD); 2778c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 27798cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitDeclRefExpr(const DeclRefExpr *E); 27808cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(E); } 2781bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E); 27828cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); 27838cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitMemberExpr(const MemberExpr *E); 27848cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitStringLiteral(const StringLiteral *E) { return Success(E); } 27858cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(E); } 278647d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith bool VisitCXXTypeidExpr(const CXXTypeidExpr *E); 2787e275a1845b9e32bd3034f2593dee1780855c8fd6Francois Pichet bool VisitCXXUuidofExpr(const CXXUuidofExpr *E); 27888cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E); 27898cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitUnaryDeref(const UnaryOperator *E); 279086024013d4c3728122c58fa07a2a67e6c15837efRichard Smith bool VisitUnaryReal(const UnaryOperator *E); 279186024013d4c3728122c58fa07a2a67e6c15837efRichard Smith bool VisitUnaryImag(const UnaryOperator *E); 27928cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 27938cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitCastExpr(const CastExpr *E) { 279426bc220377705292a0519a71d3ea3aef68fcfec6Anders Carlsson switch (E->getCastKind()) { 279526bc220377705292a0519a71d3ea3aef68fcfec6Anders Carlsson default: 2796e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return LValueExprEvaluatorBaseTy::VisitCastExpr(E); 279726bc220377705292a0519a71d3ea3aef68fcfec6Anders Carlsson 2798db924224b51b153f24fbe492102d4edebcbbb7f4Eli Friedman case CK_LValueBitCast: 2799c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith this->CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; 28000a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith if (!Visit(E->getSubExpr())) 28010a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith return false; 28020a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith Result.Designator.setInvalid(); 28030a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith return true; 2804db924224b51b153f24fbe492102d4edebcbbb7f4Eli Friedman 2805e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case CK_BaseToDerived: 2806180f47959a066795cc0f409433023af448bb0328Richard Smith if (!Visit(E->getSubExpr())) 2807180f47959a066795cc0f409433023af448bb0328Richard Smith return false; 2808e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return HandleBaseToDerivedCast(Info, E, Result); 280926bc220377705292a0519a71d3ea3aef68fcfec6Anders Carlsson } 281026bc220377705292a0519a71d3ea3aef68fcfec6Anders Carlsson } 28114efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman}; 28124efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman} // end anonymous namespace 28134efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 2814c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// Evaluate an expression as an lvalue. This can be legitimately called on 2815c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// expressions which are not glvalues, in a few cases: 2816c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// * function designators in C, 2817c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// * "extern void" objects, 2818c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// * temporaries, if building with -Wno-address-of-temporary. 2819efdb83e26f9a1fd2566afe54461216cd84814d42John McCallstatic bool EvaluateLValue(const Expr* E, LValue& Result, EvalInfo &Info) { 2820c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith assert((E->isGLValue() || E->getType()->isFunctionType() || 2821c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith E->getType()->isVoidType() || isa<CXXTemporaryObjectExpr>(E)) && 2822c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith "can't evaluate expression as an lvalue"); 28238cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return LValueExprEvaluator(Info, Result).Visit(E); 28244efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman} 28254efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 28268cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) { 28271bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl())) 28281bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith return Success(FD); 28291bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith if (const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) 2830c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return VisitVarDecl(E, VD); 2831c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return Error(E); 2832c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith} 2833436c8898cd1c93c5bacd3fcc4ac586bc5cd77062Richard Smith 2834c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smithbool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) { 2835177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith if (!VD->getType()->isReferenceType()) { 2836177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith if (isa<ParmVarDecl>(VD)) { 283783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith Result.set(VD, Info.CurrentCall->Index); 2838177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith return true; 2839177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith } 28401bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith return Success(VD); 2841177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith } 284250c39ea4858265f3f5f42a0c624557ce2281936bEli Friedman 28431aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith APValue V; 2844f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!EvaluateVarDeclInit(Info, E, VD, Info.CurrentCall, V)) 2845f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 2846f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Success(V, E); 284735873c49adad211ff466e34342a52665742794f5Anders Carlsson} 284835873c49adad211ff466e34342a52665742794f5Anders Carlsson 2849bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smithbool LValueExprEvaluator::VisitMaterializeTemporaryExpr( 2850bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith const MaterializeTemporaryExpr *E) { 2851e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (E->GetTemporaryExpr()->isRValue()) { 2852af2c7a194592401394233b7cbcdd3cfd0a7a38ddRichard Smith if (E->getType()->isRecordType()) 2853e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return EvaluateTemporary(E->GetTemporaryExpr(), Result, Info); 2854e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 285583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith Result.set(E, Info.CurrentCall->Index); 285683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith return EvaluateInPlace(Info.CurrentCall->Temporaries[E], Info, 285783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith Result, E->GetTemporaryExpr()); 2858e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2859e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2860e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // Materialization of an lvalue temporary occurs when we need to force a copy 2861e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // (for instance, if it's a bitfield). 2862e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // FIXME: The AST should contain an lvalue-to-rvalue node for such cases. 2863e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!Visit(E->GetTemporaryExpr())) 2864e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 2865f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!HandleLValueToRValueConversion(Info, E, E->getType(), Result, 2866e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Info.CurrentCall->Temporaries[E])) 2867e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 286883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith Result.set(E, Info.CurrentCall->Index); 2869e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 2870bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith} 2871bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith 28728cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool 28738cad3046be06ea73ff8892d947697a21d7a440d3Peter CollingbourneLValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { 2874c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?"); 2875c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // Defer visiting the literal until the lvalue-to-rvalue conversion. We can 2876c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // only see this when folding in C, so there's no standard to follow here. 2877efdb83e26f9a1fd2566afe54461216cd84814d42John McCall return Success(E); 28784efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman} 28794efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 288047d2145675099893d702be4bc06bd9f26d8ddd13Richard Smithbool LValueExprEvaluator::VisitCXXTypeidExpr(const CXXTypeidExpr *E) { 288147d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith if (E->isTypeOperand()) 288247d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith return Success(E); 288347d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith CXXRecordDecl *RD = E->getExprOperand()->getType()->getAsCXXRecordDecl(); 288447d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith if (RD && RD->isPolymorphic()) { 28855cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.Diag(E, diag::note_constexpr_typeid_polymorphic) 288647d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith << E->getExprOperand()->getType() 288747d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith << E->getExprOperand()->getSourceRange(); 288847d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith return false; 288947d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith } 289047d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith return Success(E); 289147d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith} 289247d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith 2893e275a1845b9e32bd3034f2593dee1780855c8fd6Francois Pichetbool LValueExprEvaluator::VisitCXXUuidofExpr(const CXXUuidofExpr *E) { 2894e275a1845b9e32bd3034f2593dee1780855c8fd6Francois Pichet return Success(E); 2895e275a1845b9e32bd3034f2593dee1780855c8fd6Francois Pichet} 2896e275a1845b9e32bd3034f2593dee1780855c8fd6Francois Pichet 28978cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) { 2898c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // Handle static data members. 2899c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith if (const VarDecl *VD = dyn_cast<VarDecl>(E->getMemberDecl())) { 2900c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith VisitIgnoredValue(E->getBase()); 2901c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return VisitVarDecl(E, VD); 2902c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith } 2903c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 2904d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith // Handle static member functions. 2905d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl())) { 2906d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith if (MD->isStatic()) { 2907d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith VisitIgnoredValue(E->getBase()); 29081bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith return Success(MD); 2909d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith } 2910d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith } 2911d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 2912180f47959a066795cc0f409433023af448bb0328Richard Smith // Handle non-static data members. 2913e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return LValueExprEvaluatorBaseTy::VisitMemberExpr(E); 29144efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman} 29154efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 29168cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) { 2917c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // FIXME: Deal with vectors as array subscript bases. 2918c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith if (E->getBase()->getType()->isVectorType()) 2919f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 2920c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 29213068d117951a8df54bae9db039b56201ab10962bAnders Carlsson if (!EvaluatePointer(E->getBase(), Result, Info)) 2922efdb83e26f9a1fd2566afe54461216cd84814d42John McCall return false; 29231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 29243068d117951a8df54bae9db039b56201ab10962bAnders Carlsson APSInt Index; 29253068d117951a8df54bae9db039b56201ab10962bAnders Carlsson if (!EvaluateInteger(E->getIdx(), Index, Info)) 2926efdb83e26f9a1fd2566afe54461216cd84814d42John McCall return false; 2927180f47959a066795cc0f409433023af448bb0328Richard Smith int64_t IndexValue 2928180f47959a066795cc0f409433023af448bb0328Richard Smith = Index.isSigned() ? Index.getSExtValue() 2929180f47959a066795cc0f409433023af448bb0328Richard Smith : static_cast<int64_t>(Index.getZExtValue()); 29303068d117951a8df54bae9db039b56201ab10962bAnders Carlsson 2931b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return HandleLValueArrayAdjustment(Info, E, Result, E->getType(), IndexValue); 29323068d117951a8df54bae9db039b56201ab10962bAnders Carlsson} 29334efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 29348cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) { 2935efdb83e26f9a1fd2566afe54461216cd84814d42John McCall return EvaluatePointer(E->getSubExpr(), Result, Info); 2936e8761c8fe2ee6b628104a0885f49fd3c21c08a4fEli Friedman} 2937e8761c8fe2ee6b628104a0885f49fd3c21c08a4fEli Friedman 293886024013d4c3728122c58fa07a2a67e6c15837efRichard Smithbool LValueExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { 293986024013d4c3728122c58fa07a2a67e6c15837efRichard Smith if (!Visit(E->getSubExpr())) 294086024013d4c3728122c58fa07a2a67e6c15837efRichard Smith return false; 294186024013d4c3728122c58fa07a2a67e6c15837efRichard Smith // __real is a no-op on scalar lvalues. 294286024013d4c3728122c58fa07a2a67e6c15837efRichard Smith if (E->getSubExpr()->getType()->isAnyComplexType()) 294386024013d4c3728122c58fa07a2a67e6c15837efRichard Smith HandleLValueComplexElement(Info, E, Result, E->getType(), false); 294486024013d4c3728122c58fa07a2a67e6c15837efRichard Smith return true; 294586024013d4c3728122c58fa07a2a67e6c15837efRichard Smith} 294686024013d4c3728122c58fa07a2a67e6c15837efRichard Smith 294786024013d4c3728122c58fa07a2a67e6c15837efRichard Smithbool LValueExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { 294886024013d4c3728122c58fa07a2a67e6c15837efRichard Smith assert(E->getSubExpr()->getType()->isAnyComplexType() && 294986024013d4c3728122c58fa07a2a67e6c15837efRichard Smith "lvalue __imag__ on scalar?"); 295086024013d4c3728122c58fa07a2a67e6c15837efRichard Smith if (!Visit(E->getSubExpr())) 295186024013d4c3728122c58fa07a2a67e6c15837efRichard Smith return false; 295286024013d4c3728122c58fa07a2a67e6c15837efRichard Smith HandleLValueComplexElement(Info, E, Result, E->getType(), true); 295386024013d4c3728122c58fa07a2a67e6c15837efRichard Smith return true; 295486024013d4c3728122c58fa07a2a67e6c15837efRichard Smith} 295586024013d4c3728122c58fa07a2a67e6c15837efRichard Smith 29564efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman//===----------------------------------------------------------------------===// 2957f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner// Pointer Evaluation 2958f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===// 2959f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner 2960c754aa62643e66ab967ca32ae8b0b3fc419bba25Anders Carlssonnamespace { 2961770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass PointerExprEvaluator 29628cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne : public ExprEvaluatorBase<PointerExprEvaluator, bool> { 2963efdb83e26f9a1fd2566afe54461216cd84814d42John McCall LValue &Result; 2964efdb83e26f9a1fd2566afe54461216cd84814d42John McCall 29658cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool Success(const Expr *E) { 29661bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith Result.set(E); 2967efdb83e26f9a1fd2566afe54461216cd84814d42John McCall return true; 2968efdb83e26f9a1fd2566afe54461216cd84814d42John McCall } 29692bad1687fe6f00e10767a691a33b070b151902b6Anders Carlssonpublic: 29701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2971efdb83e26f9a1fd2566afe54461216cd84814d42John McCall PointerExprEvaluator(EvalInfo &info, LValue &Result) 29728cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne : ExprEvaluatorBaseTy(info), Result(Result) {} 2973f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner 29741aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith bool Success(const APValue &V, const Expr *E) { 29751aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith Result.setFrom(Info.Ctx, V); 29768cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return true; 29772bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson } 297851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith bool ZeroInitialization(const Expr *E) { 2979f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith return Success((Expr*)0); 2980f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith } 29812bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson 2982efdb83e26f9a1fd2566afe54461216cd84814d42John McCall bool VisitBinaryOperator(const BinaryOperator *E); 29838cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitCastExpr(const CastExpr* E); 2984efdb83e26f9a1fd2566afe54461216cd84814d42John McCall bool VisitUnaryAddrOf(const UnaryOperator *E); 29858cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitObjCStringLiteral(const ObjCStringLiteral *E) 2986efdb83e26f9a1fd2566afe54461216cd84814d42John McCall { return Success(E); } 2987eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard bool VisitObjCBoxedExpr(const ObjCBoxedExpr *E) 2988ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek { return Success(E); } 29898cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitAddrLabelExpr(const AddrLabelExpr *E) 2990efdb83e26f9a1fd2566afe54461216cd84814d42John McCall { return Success(E); } 29918cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitCallExpr(const CallExpr *E); 29928cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitBlockExpr(const BlockExpr *E) { 2993469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall if (!E->getBlockDecl()->hasCaptures()) 2994efdb83e26f9a1fd2566afe54461216cd84814d42John McCall return Success(E); 2995f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 2996b83d287bc7f47d36fb0751a481e2ef9308b37252Mike Stump } 2997180f47959a066795cc0f409433023af448bb0328Richard Smith bool VisitCXXThisExpr(const CXXThisExpr *E) { 2998180f47959a066795cc0f409433023af448bb0328Richard Smith if (!Info.CurrentCall->This) 2999f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 3000180f47959a066795cc0f409433023af448bb0328Richard Smith Result = *Info.CurrentCall->This; 3001180f47959a066795cc0f409433023af448bb0328Richard Smith return true; 3002180f47959a066795cc0f409433023af448bb0328Richard Smith } 300356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall 3004ba98d6bb414861965a1f22628494ea046785ecd4Eli Friedman // FIXME: Missing: @protocol, @selector 30052bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson}; 3006f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner} // end anonymous namespace 30072bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson 3008efdb83e26f9a1fd2566afe54461216cd84814d42John McCallstatic bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info) { 3009c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith assert(E->isRValue() && E->getType()->hasPointerRepresentation()); 30108cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return PointerExprEvaluator(Info, Result).Visit(E); 3011f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner} 3012650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson 3013efdb83e26f9a1fd2566afe54461216cd84814d42John McCallbool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { 30142de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (E->getOpcode() != BO_Add && 30152de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall E->getOpcode() != BO_Sub) 3016e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return ExprEvaluatorBaseTy::VisitBinaryOperator(E); 30171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3018650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson const Expr *PExp = E->getLHS(); 3019650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson const Expr *IExp = E->getRHS(); 3020650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson if (IExp->getType()->isPointerType()) 3021f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner std::swap(PExp, IExp); 30221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3023745f5147e065900267c85a5568785a1991d4838fRichard Smith bool EvalPtrOK = EvaluatePointer(PExp, Result, Info); 3024745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!EvalPtrOK && !Info.keepEvaluatingAfterFailure()) 3025efdb83e26f9a1fd2566afe54461216cd84814d42John McCall return false; 30261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3027efdb83e26f9a1fd2566afe54461216cd84814d42John McCall llvm::APSInt Offset; 3028745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!EvaluateInteger(IExp, Offset, Info) || !EvalPtrOK) 3029efdb83e26f9a1fd2566afe54461216cd84814d42John McCall return false; 3030efdb83e26f9a1fd2566afe54461216cd84814d42John McCall int64_t AdditionalOffset 3031efdb83e26f9a1fd2566afe54461216cd84814d42John McCall = Offset.isSigned() ? Offset.getSExtValue() 3032efdb83e26f9a1fd2566afe54461216cd84814d42John McCall : static_cast<int64_t>(Offset.getZExtValue()); 30330a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith if (E->getOpcode() == BO_Sub) 30340a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith AdditionalOffset = -AdditionalOffset; 3035650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson 3036180f47959a066795cc0f409433023af448bb0328Richard Smith QualType Pointee = PExp->getType()->getAs<PointerType>()->getPointeeType(); 3037b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return HandleLValueArrayAdjustment(Info, E, Result, Pointee, 3038b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith AdditionalOffset); 3039650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson} 30404efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 3041efdb83e26f9a1fd2566afe54461216cd84814d42John McCallbool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { 3042efdb83e26f9a1fd2566afe54461216cd84814d42John McCall return EvaluateLValue(E->getSubExpr(), Result, Info); 30434efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman} 30441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 30458cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool PointerExprEvaluator::VisitCastExpr(const CastExpr* E) { 30468cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne const Expr* SubExpr = E->getSubExpr(); 3047650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson 304809a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman switch (E->getCastKind()) { 304909a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman default: 305009a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman break; 305109a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman 30522de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case CK_BitCast: 30531d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall case CK_CPointerToObjCPointerCast: 30541d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall case CK_BlockPointerToObjCPointerCast: 30552de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case CK_AnyPointerToBlockPointerCast: 305628c1ce789322ab99f9b5887015d63ec5f088957aRichard Smith if (!Visit(SubExpr)) 305728c1ce789322ab99f9b5887015d63ec5f088957aRichard Smith return false; 3058c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith // Bitcasts to cv void* are static_casts, not reinterpret_casts, so are 3059c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith // permitted in constant expressions in C++11. Bitcasts from cv void* are 3060c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith // also static_casts, but we disallow them as a resolution to DR1312. 30614cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith if (!E->getType()->isVoidPointerType()) { 306228c1ce789322ab99f9b5887015d63ec5f088957aRichard Smith Result.Designator.setInvalid(); 30634cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith if (SubExpr->getType()->isVoidPointerType()) 30644cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith CCEDiag(E, diag::note_constexpr_invalid_cast) 30654cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith << 3 << SubExpr->getType(); 30664cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith else 30674cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; 30684cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith } 30690a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith return true; 307009a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman 30715c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson case CK_DerivedToBase: 30725c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson case CK_UncheckedDerivedToBase: { 307347a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith if (!EvaluatePointer(E->getSubExpr(), Result, Info)) 30745c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson return false; 3075e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!Result.Base && Result.Offset.isZero()) 3076e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 30775c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson 3078180f47959a066795cc0f409433023af448bb0328Richard Smith // Now figure out the necessary offset to add to the base LV to get from 30795c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson // the derived class to the base class. 3080180f47959a066795cc0f409433023af448bb0328Richard Smith QualType Type = 3081180f47959a066795cc0f409433023af448bb0328Richard Smith E->getSubExpr()->getType()->castAs<PointerType>()->getPointeeType(); 30825c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson 3083180f47959a066795cc0f409433023af448bb0328Richard Smith for (CastExpr::path_const_iterator PathI = E->path_begin(), 30845c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson PathE = E->path_end(); PathI != PathE; ++PathI) { 3085b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (!HandleLValueBase(Info, E, Result, Type->getAsCXXRecordDecl(), 3086b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith *PathI)) 30875c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson return false; 3088180f47959a066795cc0f409433023af448bb0328Richard Smith Type = (*PathI)->getType(); 30895c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson } 30905c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson 30915c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson return true; 30925c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson } 30935c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson 3094e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case CK_BaseToDerived: 3095e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!Visit(E->getSubExpr())) 3096e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 3097e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!Result.Base && Result.Offset.isZero()) 3098e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 3099e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return HandleBaseToDerivedCast(Info, E, Result); 3100e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 310147a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith case CK_NullToPointer: 310249149fe0d2be06ce1ceed1e9d2548a0b75a59c47Richard Smith VisitIgnoredValue(E->getSubExpr()); 310351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return ZeroInitialization(E); 3104404cd1669c3ba138a9ae0a619bd689cce5aae271John McCall 31052de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case CK_IntegralToPointer: { 3106c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; 3107c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith 31081aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith APValue Value; 3109efdb83e26f9a1fd2566afe54461216cd84814d42John McCall if (!EvaluateIntegerOrLValue(SubExpr, Value, Info)) 311009a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman break; 311169ab26a8623141f35e86817cfc6e0fbe7639a40fDaniel Dunbar 3112efdb83e26f9a1fd2566afe54461216cd84814d42John McCall if (Value.isInt()) { 311347a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith unsigned Size = Info.Ctx.getTypeSize(E->getType()); 311447a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith uint64_t N = Value.getInt().extOrTrunc(Size).getZExtValue(); 31151bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith Result.Base = (Expr*)0; 311647a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith Result.Offset = CharUnits::fromQuantity(N); 311783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith Result.CallIndex = 0; 31180a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith Result.Designator.setInvalid(); 3119efdb83e26f9a1fd2566afe54461216cd84814d42John McCall return true; 3120efdb83e26f9a1fd2566afe54461216cd84814d42John McCall } else { 3121efdb83e26f9a1fd2566afe54461216cd84814d42John McCall // Cast is of an lvalue, no need to change value. 31221aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith Result.setFrom(Info.Ctx, Value); 3123efdb83e26f9a1fd2566afe54461216cd84814d42John McCall return true; 3124650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson } 3125650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson } 31262de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case CK_ArrayToPointerDecay: 3127e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (SubExpr->isGLValue()) { 3128e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!EvaluateLValue(SubExpr, Result, Info)) 3129e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 3130e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } else { 313183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith Result.set(SubExpr, Info.CurrentCall->Index); 313283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith if (!EvaluateInPlace(Info.CurrentCall->Temporaries[SubExpr], 313383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith Info, Result, SubExpr)) 3134e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 3135e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 31360a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith // The result is a pointer to the first element of the array. 3137b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (const ConstantArrayType *CAT 3138b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith = Info.Ctx.getAsConstantArrayType(SubExpr->getType())) 3139b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Result.addArray(Info, E, CAT); 3140b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith else 3141b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Result.Designator.setInvalid(); 31420a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith return true; 31436a7c94af983717e2c2d6aebe42cb4737c1c7b9e6Richard Smith 31442de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case CK_FunctionToPointerDecay: 31456a7c94af983717e2c2d6aebe42cb4737c1c7b9e6Richard Smith return EvaluateLValue(SubExpr, Result, Info); 31464efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman } 31474efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 3148c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return ExprEvaluatorBaseTy::VisitCastExpr(E); 31491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump} 3150650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson 31518cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) { 3152180f47959a066795cc0f409433023af448bb0328Richard Smith if (IsStringLiteralCall(E)) 3153efdb83e26f9a1fd2566afe54461216cd84814d42John McCall return Success(E); 315456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall 31558cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return ExprEvaluatorBaseTy::VisitCallExpr(E); 31564efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman} 3157f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner 3158f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===// 3159e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// Member Pointer Evaluation 3160e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===// 3161e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3162e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithnamespace { 3163e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithclass MemberPointerExprEvaluator 3164e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith : public ExprEvaluatorBase<MemberPointerExprEvaluator, bool> { 3165e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith MemberPtr &Result; 3166e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3167e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool Success(const ValueDecl *D) { 3168e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Result = MemberPtr(D); 3169e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 3170e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 3171e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithpublic: 3172e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3173e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith MemberPointerExprEvaluator(EvalInfo &Info, MemberPtr &Result) 3174e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith : ExprEvaluatorBaseTy(Info), Result(Result) {} 3175e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 31761aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith bool Success(const APValue &V, const Expr *E) { 3177e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Result.setFrom(V); 3178e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 3179e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 318051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith bool ZeroInitialization(const Expr *E) { 3181e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return Success((const ValueDecl*)0); 3182e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 3183e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3184e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool VisitCastExpr(const CastExpr *E); 3185e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool VisitUnaryAddrOf(const UnaryOperator *E); 3186e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}; 3187e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith} // end anonymous namespace 3188e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3189e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, 3190e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith EvalInfo &Info) { 3191e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith assert(E->isRValue() && E->getType()->isMemberPointerType()); 3192e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return MemberPointerExprEvaluator(Info, Result).Visit(E); 3193e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith} 3194e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3195e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithbool MemberPointerExprEvaluator::VisitCastExpr(const CastExpr *E) { 3196e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith switch (E->getCastKind()) { 3197e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith default: 3198e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return ExprEvaluatorBaseTy::VisitCastExpr(E); 3199e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3200e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case CK_NullToMemberPointer: 320149149fe0d2be06ce1ceed1e9d2548a0b75a59c47Richard Smith VisitIgnoredValue(E->getSubExpr()); 320251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return ZeroInitialization(E); 3203e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3204e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case CK_BaseToDerivedMemberPointer: { 3205e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!Visit(E->getSubExpr())) 3206e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 3207e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (E->path_empty()) 3208e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 3209e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // Base-to-derived member pointer casts store the path in derived-to-base 3210e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // order, so iterate backwards. The CXXBaseSpecifier also provides us with 3211e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // the wrong end of the derived->base arc, so stagger the path by one class. 3212e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter; 3213e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin()); 3214e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith PathI != PathE; ++PathI) { 3215e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); 3216e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const CXXRecordDecl *Derived = (*PathI)->getType()->getAsCXXRecordDecl(); 3217e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!Result.castToDerived(Derived)) 3218f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 3219e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 3220e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const Type *FinalTy = E->getType()->castAs<MemberPointerType>()->getClass(); 3221e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!Result.castToDerived(FinalTy->getAsCXXRecordDecl())) 3222f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 3223e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 3224e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 3225e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3226e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case CK_DerivedToBaseMemberPointer: 3227e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!Visit(E->getSubExpr())) 3228e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 3229e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith for (CastExpr::path_const_iterator PathI = E->path_begin(), 3230e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith PathE = E->path_end(); PathI != PathE; ++PathI) { 3231e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); 3232e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); 3233e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!Result.castToBase(Base)) 3234f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 3235e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 3236e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 3237e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 3238e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith} 3239e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3240e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithbool MemberPointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { 3241e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // C++11 [expr.unary.op]p3 has very strict rules on how the address of a 3242e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // member can be formed. 3243e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return Success(cast<DeclRefExpr>(E->getSubExpr())->getDecl()); 3244e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith} 3245e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3246e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===// 3247180f47959a066795cc0f409433023af448bb0328Richard Smith// Record Evaluation 3248180f47959a066795cc0f409433023af448bb0328Richard Smith//===----------------------------------------------------------------------===// 3249180f47959a066795cc0f409433023af448bb0328Richard Smith 3250180f47959a066795cc0f409433023af448bb0328Richard Smithnamespace { 3251180f47959a066795cc0f409433023af448bb0328Richard Smith class RecordExprEvaluator 3252180f47959a066795cc0f409433023af448bb0328Richard Smith : public ExprEvaluatorBase<RecordExprEvaluator, bool> { 3253180f47959a066795cc0f409433023af448bb0328Richard Smith const LValue &This; 3254180f47959a066795cc0f409433023af448bb0328Richard Smith APValue &Result; 3255180f47959a066795cc0f409433023af448bb0328Richard Smith public: 3256180f47959a066795cc0f409433023af448bb0328Richard Smith 3257180f47959a066795cc0f409433023af448bb0328Richard Smith RecordExprEvaluator(EvalInfo &info, const LValue &This, APValue &Result) 3258180f47959a066795cc0f409433023af448bb0328Richard Smith : ExprEvaluatorBaseTy(info), This(This), Result(Result) {} 3259180f47959a066795cc0f409433023af448bb0328Richard Smith 32601aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith bool Success(const APValue &V, const Expr *E) { 326183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith Result = V; 326283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith return true; 3263180f47959a066795cc0f409433023af448bb0328Richard Smith } 326451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith bool ZeroInitialization(const Expr *E); 3265180f47959a066795cc0f409433023af448bb0328Richard Smith 326659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith bool VisitCastExpr(const CastExpr *E); 3267180f47959a066795cc0f409433023af448bb0328Richard Smith bool VisitInitListExpr(const InitListExpr *E); 3268180f47959a066795cc0f409433023af448bb0328Richard Smith bool VisitCXXConstructExpr(const CXXConstructExpr *E); 3269180f47959a066795cc0f409433023af448bb0328Richard Smith }; 3270180f47959a066795cc0f409433023af448bb0328Richard Smith} 3271180f47959a066795cc0f409433023af448bb0328Richard Smith 327251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// Perform zero-initialization on an object of non-union class type. 327351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// C++11 [dcl.init]p5: 327451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// To zero-initialize an object or reference of type T means: 327551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// [...] 327651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// -- if T is a (possibly cv-qualified) non-union class type, 327751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// each non-static data member and each base-class subobject is 327851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// zero-initialized 3279b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithstatic bool HandleClassZeroInitialization(EvalInfo &Info, const Expr *E, 3280b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith const RecordDecl *RD, 328151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith const LValue &This, APValue &Result) { 328251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith assert(!RD->isUnion() && "Expected non-union class type"); 328351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD); 328451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith Result = APValue(APValue::UninitStruct(), CD ? CD->getNumBases() : 0, 328551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith std::distance(RD->field_begin(), RD->field_end())); 328651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 32878d59deec807ed53efcd07855199cdc9c979f447fJohn McCall if (RD->isInvalidDecl()) return false; 328851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); 328951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 329051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (CD) { 329151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith unsigned Index = 0; 329251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(), 3293b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith End = CD->bases_end(); I != End; ++I, ++Index) { 329451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith const CXXRecordDecl *Base = I->getType()->getAsCXXRecordDecl(); 329551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith LValue Subobject = This; 32968d59deec807ed53efcd07855199cdc9c979f447fJohn McCall if (!HandleLValueDirectBase(Info, E, Subobject, CD, Base, &Layout)) 32978d59deec807ed53efcd07855199cdc9c979f447fJohn McCall return false; 3298b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (!HandleClassZeroInitialization(Info, E, Base, Subobject, 329951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith Result.getStructBase(Index))) 330051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return false; 330151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith } 330251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith } 330351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 3304b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith for (RecordDecl::field_iterator I = RD->field_begin(), End = RD->field_end(); 3305b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith I != End; ++I) { 330651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith // -- if T is a reference type, no initialization is performed. 3307262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie if (I->getType()->isReferenceType()) 330851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith continue; 330951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 331051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith LValue Subobject = This; 3311581deb3da481053c4993c7600f97acf7768caac5David Blaikie if (!HandleLValueMember(Info, E, Subobject, *I, &Layout)) 33128d59deec807ed53efcd07855199cdc9c979f447fJohn McCall return false; 331351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 3314262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie ImplicitValueInitExpr VIE(I->getType()); 331583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith if (!EvaluateInPlace( 3316262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie Result.getStructField(I->getFieldIndex()), Info, Subobject, &VIE)) 331751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return false; 331851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith } 331951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 332051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return true; 332151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith} 332251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 332351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smithbool RecordExprEvaluator::ZeroInitialization(const Expr *E) { 332451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl(); 33251de9d7de172379d6af75fd11dda2a713e4f36f62John McCall if (RD->isInvalidDecl()) return false; 332651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (RD->isUnion()) { 332751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the 332851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith // object's first non-static named data member is zero-initialized 332951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith RecordDecl::field_iterator I = RD->field_begin(); 333051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (I == RD->field_end()) { 333151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith Result = APValue((const FieldDecl*)0); 333251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return true; 333351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith } 333451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 333551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith LValue Subobject = This; 3336581deb3da481053c4993c7600f97acf7768caac5David Blaikie if (!HandleLValueMember(Info, E, Subobject, *I)) 33378d59deec807ed53efcd07855199cdc9c979f447fJohn McCall return false; 3338581deb3da481053c4993c7600f97acf7768caac5David Blaikie Result = APValue(*I); 3339262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie ImplicitValueInitExpr VIE(I->getType()); 334083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, &VIE); 334151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith } 334251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 3343ce582fe2a7aad8b14b3636ad9cac0a3b8bbb219bRichard Smith if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->getNumVBases()) { 33445cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.Diag(E, diag::note_constexpr_virtual_base) << RD; 3345ce582fe2a7aad8b14b3636ad9cac0a3b8bbb219bRichard Smith return false; 3346ce582fe2a7aad8b14b3636ad9cac0a3b8bbb219bRichard Smith } 3347ce582fe2a7aad8b14b3636ad9cac0a3b8bbb219bRichard Smith 3348b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return HandleClassZeroInitialization(Info, E, RD, This, Result); 334951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith} 335051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 335159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithbool RecordExprEvaluator::VisitCastExpr(const CastExpr *E) { 335259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith switch (E->getCastKind()) { 335359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith default: 335459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith return ExprEvaluatorBaseTy::VisitCastExpr(E); 335559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith 335659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith case CK_ConstructorConversion: 335759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith return Visit(E->getSubExpr()); 335859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith 335959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith case CK_DerivedToBase: 336059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith case CK_UncheckedDerivedToBase: { 33611aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith APValue DerivedObject; 3362f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!Evaluate(DerivedObject, Info, E->getSubExpr())) 336359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith return false; 3364f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!DerivedObject.isStruct()) 3365f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E->getSubExpr()); 336659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith 336759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith // Derived-to-base rvalue conversion: just slice off the derived part. 336859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith APValue *Value = &DerivedObject; 336959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith const CXXRecordDecl *RD = E->getSubExpr()->getType()->getAsCXXRecordDecl(); 337059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith for (CastExpr::path_const_iterator PathI = E->path_begin(), 337159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith PathE = E->path_end(); PathI != PathE; ++PathI) { 337259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith assert(!(*PathI)->isVirtual() && "record rvalue with virtual base"); 337359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); 337459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith Value = &Value->getStructBase(getBaseIndex(RD, Base)); 337559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith RD = Base; 337659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith } 337759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith Result = *Value; 337859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith return true; 337959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith } 338059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith } 338159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith} 338259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith 3383180f47959a066795cc0f409433023af448bb0328Richard Smithbool RecordExprEvaluator::VisitInitListExpr(const InitListExpr *E) { 338424fe798fffc1748d8bce1321af42981c3719cb85Sebastian Redl // Cannot constant-evaluate std::initializer_list inits. 338524fe798fffc1748d8bce1321af42981c3719cb85Sebastian Redl if (E->initializesStdInitializerList()) 338624fe798fffc1748d8bce1321af42981c3719cb85Sebastian Redl return false; 338724fe798fffc1748d8bce1321af42981c3719cb85Sebastian Redl 3388180f47959a066795cc0f409433023af448bb0328Richard Smith const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl(); 33891de9d7de172379d6af75fd11dda2a713e4f36f62John McCall if (RD->isInvalidDecl()) return false; 3390180f47959a066795cc0f409433023af448bb0328Richard Smith const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); 3391180f47959a066795cc0f409433023af448bb0328Richard Smith 3392180f47959a066795cc0f409433023af448bb0328Richard Smith if (RD->isUnion()) { 3393ec789163a42a7be654ac34aadb750b508954d53cRichard Smith const FieldDecl *Field = E->getInitializedFieldInUnion(); 3394ec789163a42a7be654ac34aadb750b508954d53cRichard Smith Result = APValue(Field); 3395ec789163a42a7be654ac34aadb750b508954d53cRichard Smith if (!Field) 3396180f47959a066795cc0f409433023af448bb0328Richard Smith return true; 3397ec789163a42a7be654ac34aadb750b508954d53cRichard Smith 3398ec789163a42a7be654ac34aadb750b508954d53cRichard Smith // If the initializer list for a union does not contain any elements, the 3399ec789163a42a7be654ac34aadb750b508954d53cRichard Smith // first element of the union is value-initialized. 3400ec789163a42a7be654ac34aadb750b508954d53cRichard Smith ImplicitValueInitExpr VIE(Field->getType()); 3401ec789163a42a7be654ac34aadb750b508954d53cRichard Smith const Expr *InitExpr = E->getNumInits() ? E->getInit(0) : &VIE; 3402ec789163a42a7be654ac34aadb750b508954d53cRichard Smith 3403180f47959a066795cc0f409433023af448bb0328Richard Smith LValue Subobject = This; 34048d59deec807ed53efcd07855199cdc9c979f447fJohn McCall if (!HandleLValueMember(Info, InitExpr, Subobject, Field, &Layout)) 34058d59deec807ed53efcd07855199cdc9c979f447fJohn McCall return false; 340683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, InitExpr); 3407180f47959a066795cc0f409433023af448bb0328Richard Smith } 3408180f47959a066795cc0f409433023af448bb0328Richard Smith 3409180f47959a066795cc0f409433023af448bb0328Richard Smith assert((!isa<CXXRecordDecl>(RD) || !cast<CXXRecordDecl>(RD)->getNumBases()) && 3410180f47959a066795cc0f409433023af448bb0328Richard Smith "initializer list for class with base classes"); 3411180f47959a066795cc0f409433023af448bb0328Richard Smith Result = APValue(APValue::UninitStruct(), 0, 3412180f47959a066795cc0f409433023af448bb0328Richard Smith std::distance(RD->field_begin(), RD->field_end())); 3413180f47959a066795cc0f409433023af448bb0328Richard Smith unsigned ElementNo = 0; 3414745f5147e065900267c85a5568785a1991d4838fRichard Smith bool Success = true; 3415180f47959a066795cc0f409433023af448bb0328Richard Smith for (RecordDecl::field_iterator Field = RD->field_begin(), 3416180f47959a066795cc0f409433023af448bb0328Richard Smith FieldEnd = RD->field_end(); Field != FieldEnd; ++Field) { 3417180f47959a066795cc0f409433023af448bb0328Richard Smith // Anonymous bit-fields are not considered members of the class for 3418180f47959a066795cc0f409433023af448bb0328Richard Smith // purposes of aggregate initialization. 3419180f47959a066795cc0f409433023af448bb0328Richard Smith if (Field->isUnnamedBitfield()) 3420180f47959a066795cc0f409433023af448bb0328Richard Smith continue; 3421180f47959a066795cc0f409433023af448bb0328Richard Smith 3422180f47959a066795cc0f409433023af448bb0328Richard Smith LValue Subobject = This; 3423180f47959a066795cc0f409433023af448bb0328Richard Smith 3424745f5147e065900267c85a5568785a1991d4838fRichard Smith bool HaveInit = ElementNo < E->getNumInits(); 3425745f5147e065900267c85a5568785a1991d4838fRichard Smith 3426745f5147e065900267c85a5568785a1991d4838fRichard Smith // FIXME: Diagnostics here should point to the end of the initializer 3427745f5147e065900267c85a5568785a1991d4838fRichard Smith // list, not the start. 34288d59deec807ed53efcd07855199cdc9c979f447fJohn McCall if (!HandleLValueMember(Info, HaveInit ? E->getInit(ElementNo) : E, 3429581deb3da481053c4993c7600f97acf7768caac5David Blaikie Subobject, *Field, &Layout)) 34308d59deec807ed53efcd07855199cdc9c979f447fJohn McCall return false; 3431745f5147e065900267c85a5568785a1991d4838fRichard Smith 3432745f5147e065900267c85a5568785a1991d4838fRichard Smith // Perform an implicit value-initialization for members beyond the end of 3433745f5147e065900267c85a5568785a1991d4838fRichard Smith // the initializer list. 3434745f5147e065900267c85a5568785a1991d4838fRichard Smith ImplicitValueInitExpr VIE(HaveInit ? Info.Ctx.IntTy : Field->getType()); 3435745f5147e065900267c85a5568785a1991d4838fRichard Smith 343683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith if (!EvaluateInPlace( 3437262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie Result.getStructField(Field->getFieldIndex()), 3438745f5147e065900267c85a5568785a1991d4838fRichard Smith Info, Subobject, HaveInit ? E->getInit(ElementNo++) : &VIE)) { 3439745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!Info.keepEvaluatingAfterFailure()) 3440180f47959a066795cc0f409433023af448bb0328Richard Smith return false; 3441745f5147e065900267c85a5568785a1991d4838fRichard Smith Success = false; 3442180f47959a066795cc0f409433023af448bb0328Richard Smith } 3443180f47959a066795cc0f409433023af448bb0328Richard Smith } 3444180f47959a066795cc0f409433023af448bb0328Richard Smith 3445745f5147e065900267c85a5568785a1991d4838fRichard Smith return Success; 3446180f47959a066795cc0f409433023af448bb0328Richard Smith} 3447180f47959a066795cc0f409433023af448bb0328Richard Smith 3448180f47959a066795cc0f409433023af448bb0328Richard Smithbool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { 3449180f47959a066795cc0f409433023af448bb0328Richard Smith const CXXConstructorDecl *FD = E->getConstructor(); 34501de9d7de172379d6af75fd11dda2a713e4f36f62John McCall if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl()) return false; 34511de9d7de172379d6af75fd11dda2a713e4f36f62John McCall 345251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith bool ZeroInit = E->requiresZeroInitialization(); 345351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) { 3454ec789163a42a7be654ac34aadb750b508954d53cRichard Smith // If we've already performed zero-initialization, we're already done. 3455ec789163a42a7be654ac34aadb750b508954d53cRichard Smith if (!Result.isUninit()) 3456ec789163a42a7be654ac34aadb750b508954d53cRichard Smith return true; 3457ec789163a42a7be654ac34aadb750b508954d53cRichard Smith 345851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (ZeroInit) 345951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return ZeroInitialization(E); 346051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 34616180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith const CXXRecordDecl *RD = FD->getParent(); 34626180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith if (RD->isUnion()) 34636180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith Result = APValue((FieldDecl*)0); 34646180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith else 34656180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith Result = APValue(APValue::UninitStruct(), RD->getNumBases(), 34666180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith std::distance(RD->field_begin(), RD->field_end())); 34676180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith return true; 34686180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith } 34696180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith 3470180f47959a066795cc0f409433023af448bb0328Richard Smith const FunctionDecl *Definition = 0; 3471180f47959a066795cc0f409433023af448bb0328Richard Smith FD->getBody(Definition); 3472180f47959a066795cc0f409433023af448bb0328Richard Smith 3473c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition)) 3474c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return false; 3475180f47959a066795cc0f409433023af448bb0328Richard Smith 3476610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith // Avoid materializing a temporary for an elidable copy/move constructor. 347751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (E->isElidable() && !ZeroInit) 3478180f47959a066795cc0f409433023af448bb0328Richard Smith if (const MaterializeTemporaryExpr *ME 3479180f47959a066795cc0f409433023af448bb0328Richard Smith = dyn_cast<MaterializeTemporaryExpr>(E->getArg(0))) 3480180f47959a066795cc0f409433023af448bb0328Richard Smith return Visit(ME->GetTemporaryExpr()); 3481180f47959a066795cc0f409433023af448bb0328Richard Smith 348251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (ZeroInit && !ZeroInitialization(E)) 348351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return false; 348451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 3485180f47959a066795cc0f409433023af448bb0328Richard Smith llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs()); 3486745f5147e065900267c85a5568785a1991d4838fRichard Smith return HandleConstructorCall(E->getExprLoc(), This, Args, 3487f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith cast<CXXConstructorDecl>(Definition), Info, 3488f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith Result); 3489180f47959a066795cc0f409433023af448bb0328Richard Smith} 3490180f47959a066795cc0f409433023af448bb0328Richard Smith 3491180f47959a066795cc0f409433023af448bb0328Richard Smithstatic bool EvaluateRecord(const Expr *E, const LValue &This, 3492180f47959a066795cc0f409433023af448bb0328Richard Smith APValue &Result, EvalInfo &Info) { 3493180f47959a066795cc0f409433023af448bb0328Richard Smith assert(E->isRValue() && E->getType()->isRecordType() && 3494180f47959a066795cc0f409433023af448bb0328Richard Smith "can't evaluate expression as a record rvalue"); 3495180f47959a066795cc0f409433023af448bb0328Richard Smith return RecordExprEvaluator(Info, This, Result).Visit(E); 3496180f47959a066795cc0f409433023af448bb0328Richard Smith} 3497180f47959a066795cc0f409433023af448bb0328Richard Smith 3498180f47959a066795cc0f409433023af448bb0328Richard Smith//===----------------------------------------------------------------------===// 3499e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// Temporary Evaluation 3500e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// 3501e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// Temporaries are represented in the AST as rvalues, but generally behave like 3502e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// lvalues. The full-object of which the temporary is a subobject is implicitly 3503e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// materialized so that a reference can bind to it. 3504e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===// 3505e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithnamespace { 3506e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithclass TemporaryExprEvaluator 3507e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith : public LValueExprEvaluatorBase<TemporaryExprEvaluator> { 3508e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithpublic: 3509e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith TemporaryExprEvaluator(EvalInfo &Info, LValue &Result) : 3510e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LValueExprEvaluatorBaseTy(Info, Result) {} 3511e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3512e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith /// Visit an expression which constructs the value of this temporary. 3513e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool VisitConstructExpr(const Expr *E) { 351483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith Result.set(E, Info.CurrentCall->Index); 351583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith return EvaluateInPlace(Info.CurrentCall->Temporaries[E], Info, Result, E); 3516e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 3517e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3518e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool VisitCastExpr(const CastExpr *E) { 3519e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith switch (E->getCastKind()) { 3520e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith default: 3521e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return LValueExprEvaluatorBaseTy::VisitCastExpr(E); 3522e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3523e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case CK_ConstructorConversion: 3524e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return VisitConstructExpr(E->getSubExpr()); 3525e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 3526e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 3527e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool VisitInitListExpr(const InitListExpr *E) { 3528e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return VisitConstructExpr(E); 3529e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 3530e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool VisitCXXConstructExpr(const CXXConstructExpr *E) { 3531e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return VisitConstructExpr(E); 3532e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 3533e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool VisitCallExpr(const CallExpr *E) { 3534e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return VisitConstructExpr(E); 3535e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 3536e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}; 3537e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith} // end anonymous namespace 3538e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3539e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// Evaluate an expression of record type as a temporary. 3540e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info) { 3541af2c7a194592401394233b7cbcdd3cfd0a7a38ddRichard Smith assert(E->isRValue() && E->getType()->isRecordType()); 3542e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return TemporaryExprEvaluator(Info, Result).Visit(E); 3543e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith} 3544e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3545e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===// 354659b5da6d853b4368b984700315adf7b37de05764Nate Begeman// Vector Evaluation 354759b5da6d853b4368b984700315adf7b37de05764Nate Begeman//===----------------------------------------------------------------------===// 354859b5da6d853b4368b984700315adf7b37de05764Nate Begeman 354959b5da6d853b4368b984700315adf7b37de05764Nate Begemannamespace { 3550770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramer class VectorExprEvaluator 355107fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith : public ExprEvaluatorBase<VectorExprEvaluator, bool> { 355207fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith APValue &Result; 355359b5da6d853b4368b984700315adf7b37de05764Nate Begeman public: 35541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 355507fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith VectorExprEvaluator(EvalInfo &info, APValue &Result) 355607fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith : ExprEvaluatorBaseTy(info), Result(Result) {} 35571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 355807fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith bool Success(const ArrayRef<APValue> &V, const Expr *E) { 355907fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith assert(V.size() == E->getType()->castAs<VectorType>()->getNumElements()); 356007fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith // FIXME: remove this APValue copy. 356107fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith Result = APValue(V.data(), V.size()); 356207fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith return true; 356307fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith } 35641aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith bool Success(const APValue &V, const Expr *E) { 356569c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith assert(V.isVector()); 356607fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith Result = V; 356707fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith return true; 356807fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith } 356951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith bool ZeroInitialization(const Expr *E); 35701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 357107fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith bool VisitUnaryReal(const UnaryOperator *E) 357291110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman { return Visit(E->getSubExpr()); } 357307fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith bool VisitCastExpr(const CastExpr* E); 357407fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith bool VisitInitListExpr(const InitListExpr *E); 357507fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith bool VisitUnaryImag(const UnaryOperator *E); 357691110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div, 35772217c87bdc5ab357046a5453bdb06f469c41024eEli Friedman // binary comparisons, binary and/or/xor, 357891110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman // shufflevector, ExtVectorElementExpr 357959b5da6d853b4368b984700315adf7b37de05764Nate Begeman }; 358059b5da6d853b4368b984700315adf7b37de05764Nate Begeman} // end anonymous namespace 358159b5da6d853b4368b984700315adf7b37de05764Nate Begeman 358259b5da6d853b4368b984700315adf7b37de05764Nate Begemanstatic bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) { 3583c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith assert(E->isRValue() && E->getType()->isVectorType() &&"not a vector rvalue"); 358407fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith return VectorExprEvaluator(Info, Result).Visit(E); 358559b5da6d853b4368b984700315adf7b37de05764Nate Begeman} 358659b5da6d853b4368b984700315adf7b37de05764Nate Begeman 358707fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smithbool VectorExprEvaluator::VisitCastExpr(const CastExpr* E) { 358807fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith const VectorType *VTy = E->getType()->castAs<VectorType>(); 3589c0b8b19bd8056d6b5d831623a0825cce150f4507Nate Begeman unsigned NElts = VTy->getNumElements(); 35901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3591d62ca370b03b8c6ad58002d3399383baf744e32bRichard Smith const Expr *SE = E->getSubExpr(); 3592e8c9e9218f215ec6089f12b076c7b9d310fd5194Nate Begeman QualType SETy = SE->getType(); 359359b5da6d853b4368b984700315adf7b37de05764Nate Begeman 359446a523285928aa07bf14803178dc04616ac85994Eli Friedman switch (E->getCastKind()) { 359546a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_VectorSplat: { 359607fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith APValue Val = APValue(); 359746a523285928aa07bf14803178dc04616ac85994Eli Friedman if (SETy->isIntegerType()) { 359846a523285928aa07bf14803178dc04616ac85994Eli Friedman APSInt IntResult; 359946a523285928aa07bf14803178dc04616ac85994Eli Friedman if (!EvaluateInteger(SE, IntResult, Info)) 3600f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 360107fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith Val = APValue(IntResult); 360246a523285928aa07bf14803178dc04616ac85994Eli Friedman } else if (SETy->isRealFloatingType()) { 360346a523285928aa07bf14803178dc04616ac85994Eli Friedman APFloat F(0.0); 360446a523285928aa07bf14803178dc04616ac85994Eli Friedman if (!EvaluateFloat(SE, F, Info)) 3605f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 360607fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith Val = APValue(F); 360746a523285928aa07bf14803178dc04616ac85994Eli Friedman } else { 360807fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith return Error(E); 360946a523285928aa07bf14803178dc04616ac85994Eli Friedman } 3610c0b8b19bd8056d6b5d831623a0825cce150f4507Nate Begeman 3611c0b8b19bd8056d6b5d831623a0825cce150f4507Nate Begeman // Splat and create vector APValue. 361207fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith SmallVector<APValue, 4> Elts(NElts, Val); 361307fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith return Success(Elts, E); 3614e8c9e9218f215ec6089f12b076c7b9d310fd5194Nate Begeman } 3615e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman case CK_BitCast: { 3616e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman // Evaluate the operand into an APInt we can extract from. 3617e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman llvm::APInt SValInt; 3618e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman if (!EvalAndBitcastToAPInt(Info, SE, SValInt)) 3619e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman return false; 3620e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman // Extract the elements 3621e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman QualType EltTy = VTy->getElementType(); 3622e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman unsigned EltSize = Info.Ctx.getTypeSize(EltTy); 3623e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); 3624e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman SmallVector<APValue, 4> Elts; 3625e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman if (EltTy->isRealFloatingType()) { 3626e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(EltTy); 3627e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman bool isIEESem = &Sem != &APFloat::PPCDoubleDouble; 3628e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman unsigned FloatEltSize = EltSize; 3629e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman if (&Sem == &APFloat::x87DoubleExtended) 3630e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman FloatEltSize = 80; 3631e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman for (unsigned i = 0; i < NElts; i++) { 3632e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman llvm::APInt Elt; 3633e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman if (BigEndian) 3634e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman Elt = SValInt.rotl(i*EltSize+FloatEltSize).trunc(FloatEltSize); 3635e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman else 3636e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman Elt = SValInt.rotr(i*EltSize).trunc(FloatEltSize); 3637e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman Elts.push_back(APValue(APFloat(Elt, isIEESem))); 3638e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman } 3639e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman } else if (EltTy->isIntegerType()) { 3640e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman for (unsigned i = 0; i < NElts; i++) { 3641e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman llvm::APInt Elt; 3642e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman if (BigEndian) 3643e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman Elt = SValInt.rotl(i*EltSize+EltSize).zextOrTrunc(EltSize); 3644e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman else 3645e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman Elt = SValInt.rotr(i*EltSize).zextOrTrunc(EltSize); 3646e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman Elts.push_back(APValue(APSInt(Elt, EltTy->isSignedIntegerType()))); 3647e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman } 3648e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman } else { 3649e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman return Error(E); 3650e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman } 3651e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman return Success(Elts, E); 3652e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman } 365346a523285928aa07bf14803178dc04616ac85994Eli Friedman default: 3654c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return ExprEvaluatorBaseTy::VisitCastExpr(E); 3655c0b8b19bd8056d6b5d831623a0825cce150f4507Nate Begeman } 365659b5da6d853b4368b984700315adf7b37de05764Nate Begeman} 365759b5da6d853b4368b984700315adf7b37de05764Nate Begeman 365807fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smithbool 365959b5da6d853b4368b984700315adf7b37de05764Nate BegemanVectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) { 366007fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith const VectorType *VT = E->getType()->castAs<VectorType>(); 366159b5da6d853b4368b984700315adf7b37de05764Nate Begeman unsigned NumInits = E->getNumInits(); 366291110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman unsigned NumElements = VT->getNumElements(); 36631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 366459b5da6d853b4368b984700315adf7b37de05764Nate Begeman QualType EltTy = VT->getElementType(); 36655f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<APValue, 4> Elements; 366659b5da6d853b4368b984700315adf7b37de05764Nate Begeman 36673edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman // The number of initializers can be less than the number of 36683edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman // vector elements. For OpenCL, this can be due to nested vector 36693edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman // initialization. For GCC compatibility, missing trailing elements 36703edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman // should be initialized with zeroes. 36713edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman unsigned CountInits = 0, CountElts = 0; 36723edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman while (CountElts < NumElements) { 36733edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman // Handle nested vector initialization. 36743edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman if (CountInits < NumInits 36753edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman && E->getInit(CountInits)->getType()->isExtVectorType()) { 36763edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman APValue v; 36773edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman if (!EvaluateVector(E->getInit(CountInits), v, Info)) 36783edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman return Error(E); 36793edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman unsigned vlen = v.getVectorLength(); 36803edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman for (unsigned j = 0; j < vlen; j++) 36813edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman Elements.push_back(v.getVectorElt(j)); 36823edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman CountElts += vlen; 36833edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman } else if (EltTy->isIntegerType()) { 368459b5da6d853b4368b984700315adf7b37de05764Nate Begeman llvm::APSInt sInt(32); 36853edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman if (CountInits < NumInits) { 36863edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman if (!EvaluateInteger(E->getInit(CountInits), sInt, Info)) 36874b1f684416980ef6f1a7cb9e6af9c4fa4a164617Richard Smith return false; 36883edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman } else // trailing integer zero. 36893edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman sInt = Info.Ctx.MakeIntValue(0, EltTy); 36903edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman Elements.push_back(APValue(sInt)); 36913edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman CountElts++; 369259b5da6d853b4368b984700315adf7b37de05764Nate Begeman } else { 369359b5da6d853b4368b984700315adf7b37de05764Nate Begeman llvm::APFloat f(0.0); 36943edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman if (CountInits < NumInits) { 36953edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman if (!EvaluateFloat(E->getInit(CountInits), f, Info)) 36964b1f684416980ef6f1a7cb9e6af9c4fa4a164617Richard Smith return false; 36973edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman } else // trailing float zero. 36983edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)); 36993edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman Elements.push_back(APValue(f)); 37003edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman CountElts++; 370159b5da6d853b4368b984700315adf7b37de05764Nate Begeman } 37023edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman CountInits++; 370359b5da6d853b4368b984700315adf7b37de05764Nate Begeman } 370407fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith return Success(Elements, E); 370559b5da6d853b4368b984700315adf7b37de05764Nate Begeman} 370659b5da6d853b4368b984700315adf7b37de05764Nate Begeman 370707fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smithbool 370851201882382fb40c9456a06c7f93d6ddd4a57712Richard SmithVectorExprEvaluator::ZeroInitialization(const Expr *E) { 370907fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith const VectorType *VT = E->getType()->getAs<VectorType>(); 371091110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman QualType EltTy = VT->getElementType(); 371191110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman APValue ZeroElement; 371291110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman if (EltTy->isIntegerType()) 371391110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy)); 371491110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman else 371591110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman ZeroElement = 371691110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy))); 371791110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman 37185f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement); 371907fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith return Success(Elements, E); 372091110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman} 372191110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman 372207fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smithbool VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { 37238327fad71da34492d82c532f42a58cb4baff81a3Richard Smith VisitIgnoredValue(E->getSubExpr()); 372451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return ZeroInitialization(E); 372591110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman} 372691110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman 372759b5da6d853b4368b984700315adf7b37de05764Nate Begeman//===----------------------------------------------------------------------===// 3728cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith// Array Evaluation 3729cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith//===----------------------------------------------------------------------===// 3730cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith 3731cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smithnamespace { 3732cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith class ArrayExprEvaluator 3733cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith : public ExprEvaluatorBase<ArrayExprEvaluator, bool> { 3734180f47959a066795cc0f409433023af448bb0328Richard Smith const LValue &This; 3735cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith APValue &Result; 3736cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith public: 3737cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith 3738180f47959a066795cc0f409433023af448bb0328Richard Smith ArrayExprEvaluator(EvalInfo &Info, const LValue &This, APValue &Result) 3739180f47959a066795cc0f409433023af448bb0328Richard Smith : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {} 3740cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith 3741cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith bool Success(const APValue &V, const Expr *E) { 3742f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith assert((V.isArray() || V.isLValue()) && 3743f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith "expected array or string literal"); 3744cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith Result = V; 3745cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith return true; 3746cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith } 3747cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith 374851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith bool ZeroInitialization(const Expr *E) { 3749180f47959a066795cc0f409433023af448bb0328Richard Smith const ConstantArrayType *CAT = 3750180f47959a066795cc0f409433023af448bb0328Richard Smith Info.Ctx.getAsConstantArrayType(E->getType()); 3751180f47959a066795cc0f409433023af448bb0328Richard Smith if (!CAT) 3752f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 3753180f47959a066795cc0f409433023af448bb0328Richard Smith 3754180f47959a066795cc0f409433023af448bb0328Richard Smith Result = APValue(APValue::UninitArray(), 0, 3755180f47959a066795cc0f409433023af448bb0328Richard Smith CAT->getSize().getZExtValue()); 3756180f47959a066795cc0f409433023af448bb0328Richard Smith if (!Result.hasArrayFiller()) return true; 3757180f47959a066795cc0f409433023af448bb0328Richard Smith 375851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith // Zero-initialize all elements. 3759180f47959a066795cc0f409433023af448bb0328Richard Smith LValue Subobject = This; 3760b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Subobject.addArray(Info, E, CAT); 3761180f47959a066795cc0f409433023af448bb0328Richard Smith ImplicitValueInitExpr VIE(CAT->getElementType()); 376283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, &VIE); 3763180f47959a066795cc0f409433023af448bb0328Richard Smith } 3764180f47959a066795cc0f409433023af448bb0328Richard Smith 3765cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith bool VisitInitListExpr(const InitListExpr *E); 3766e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool VisitCXXConstructExpr(const CXXConstructExpr *E); 3767cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith }; 3768cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith} // end anonymous namespace 3769cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith 3770180f47959a066795cc0f409433023af448bb0328Richard Smithstatic bool EvaluateArray(const Expr *E, const LValue &This, 3771180f47959a066795cc0f409433023af448bb0328Richard Smith APValue &Result, EvalInfo &Info) { 377251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith assert(E->isRValue() && E->getType()->isArrayType() && "not an array rvalue"); 3773180f47959a066795cc0f409433023af448bb0328Richard Smith return ArrayExprEvaluator(Info, This, Result).Visit(E); 3774cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith} 3775cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith 3776cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smithbool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E) { 3777cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(E->getType()); 3778cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith if (!CAT) 3779f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 3780cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith 3781974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith // C++11 [dcl.init.string]p1: A char array [...] can be initialized by [...] 3782974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith // an appropriately-typed string literal enclosed in braces. 3783fe587201feaebc69e6d18858bea85c77926b6ecfRichard Smith if (E->isStringLiteralInit()) { 3784974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith LValue LV; 3785974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith if (!EvaluateLValue(E->getInit(0), LV, Info)) 3786974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith return false; 37871aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith APValue Val; 3788f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith LV.moveInto(Val); 3789f3908f2ae111b1b12ade2524dda71c669ed6f121Richard Smith return Success(Val, E); 3790974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith } 3791974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith 3792745f5147e065900267c85a5568785a1991d4838fRichard Smith bool Success = true; 3793745f5147e065900267c85a5568785a1991d4838fRichard Smith 3794de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith assert((!Result.isArray() || Result.getArrayInitializedElts() == 0) && 3795de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith "zero-initialized array shouldn't have any initialized elts"); 3796de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith APValue Filler; 3797de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith if (Result.isArray() && Result.hasArrayFiller()) 3798de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith Filler = Result.getArrayFiller(); 3799de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith 3800cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith Result = APValue(APValue::UninitArray(), E->getNumInits(), 3801cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith CAT->getSize().getZExtValue()); 3802de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith 3803de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith // If the array was previously zero-initialized, preserve the 3804de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith // zero-initialized values. 3805de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith if (!Filler.isUninit()) { 3806de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith for (unsigned I = 0, E = Result.getArrayInitializedElts(); I != E; ++I) 3807de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith Result.getArrayInitializedElt(I) = Filler; 3808de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith if (Result.hasArrayFiller()) 3809de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith Result.getArrayFiller() = Filler; 3810de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith } 3811de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith 3812180f47959a066795cc0f409433023af448bb0328Richard Smith LValue Subobject = This; 3813b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Subobject.addArray(Info, E, CAT); 3814180f47959a066795cc0f409433023af448bb0328Richard Smith unsigned Index = 0; 3815cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith for (InitListExpr::const_iterator I = E->begin(), End = E->end(); 3816180f47959a066795cc0f409433023af448bb0328Richard Smith I != End; ++I, ++Index) { 381783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith if (!EvaluateInPlace(Result.getArrayInitializedElt(Index), 381883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith Info, Subobject, cast<Expr>(*I)) || 3819745f5147e065900267c85a5568785a1991d4838fRichard Smith !HandleLValueArrayAdjustment(Info, cast<Expr>(*I), Subobject, 3820745f5147e065900267c85a5568785a1991d4838fRichard Smith CAT->getElementType(), 1)) { 3821745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!Info.keepEvaluatingAfterFailure()) 3822745f5147e065900267c85a5568785a1991d4838fRichard Smith return false; 3823745f5147e065900267c85a5568785a1991d4838fRichard Smith Success = false; 3824745f5147e065900267c85a5568785a1991d4838fRichard Smith } 3825180f47959a066795cc0f409433023af448bb0328Richard Smith } 3826cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith 3827745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!Result.hasArrayFiller()) return Success; 3828cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith assert(E->hasArrayFiller() && "no array filler for incomplete init list"); 3829180f47959a066795cc0f409433023af448bb0328Richard Smith // FIXME: The Subobject here isn't necessarily right. This rarely matters, 3830180f47959a066795cc0f409433023af448bb0328Richard Smith // but sometimes does: 3831180f47959a066795cc0f409433023af448bb0328Richard Smith // struct S { constexpr S() : p(&p) {} void *p; }; 3832180f47959a066795cc0f409433023af448bb0328Richard Smith // S s[10] = {}; 383383587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith return EvaluateInPlace(Result.getArrayFiller(), Info, 383483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith Subobject, E->getArrayFiller()) && Success; 3835cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith} 3836cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith 3837e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithbool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { 3838de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith // FIXME: The Subobject here isn't necessarily right. This rarely matters, 3839de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith // but sometimes does: 3840de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith // struct S { constexpr S() : p(&p) {} void *p; }; 3841de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith // S s[10]; 3842de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith LValue Subobject = This; 3843de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith 3844de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith APValue *Value = &Result; 3845de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith bool HadZeroInit = true; 3846a4334dffde250c22c339a974a7131914fe723180Richard Smith QualType ElemTy = E->getType(); 3847a4334dffde250c22c339a974a7131914fe723180Richard Smith while (const ConstantArrayType *CAT = 3848a4334dffde250c22c339a974a7131914fe723180Richard Smith Info.Ctx.getAsConstantArrayType(ElemTy)) { 3849de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith Subobject.addArray(Info, E, CAT); 3850de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith HadZeroInit &= !Value->isUninit(); 3851de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith if (!HadZeroInit) 3852de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith *Value = APValue(APValue::UninitArray(), 0, CAT->getSize().getZExtValue()); 3853de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith if (!Value->hasArrayFiller()) 3854de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith return true; 3855de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith Value = &Value->getArrayFiller(); 3856a4334dffde250c22c339a974a7131914fe723180Richard Smith ElemTy = CAT->getElementType(); 3857de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith } 3858e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3859a4334dffde250c22c339a974a7131914fe723180Richard Smith if (!ElemTy->isRecordType()) 3860a4334dffde250c22c339a974a7131914fe723180Richard Smith return Error(E); 3861a4334dffde250c22c339a974a7131914fe723180Richard Smith 3862e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const CXXConstructorDecl *FD = E->getConstructor(); 38636180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith 386451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith bool ZeroInit = E->requiresZeroInitialization(); 386551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) { 3866ec789163a42a7be654ac34aadb750b508954d53cRichard Smith if (HadZeroInit) 3867ec789163a42a7be654ac34aadb750b508954d53cRichard Smith return true; 3868ec789163a42a7be654ac34aadb750b508954d53cRichard Smith 386951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (ZeroInit) { 3870a4334dffde250c22c339a974a7131914fe723180Richard Smith ImplicitValueInitExpr VIE(ElemTy); 3871de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith return EvaluateInPlace(*Value, Info, Subobject, &VIE); 387251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith } 387351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 38746180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith const CXXRecordDecl *RD = FD->getParent(); 38756180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith if (RD->isUnion()) 3876de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith *Value = APValue((FieldDecl*)0); 38776180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith else 3878de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith *Value = 38796180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith APValue(APValue::UninitStruct(), RD->getNumBases(), 38806180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith std::distance(RD->field_begin(), RD->field_end())); 38816180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith return true; 38826180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith } 38836180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith 3884e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const FunctionDecl *Definition = 0; 3885e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith FD->getBody(Definition); 3886e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3887c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition)) 3888c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return false; 3889e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3890ec789163a42a7be654ac34aadb750b508954d53cRichard Smith if (ZeroInit && !HadZeroInit) { 3891a4334dffde250c22c339a974a7131914fe723180Richard Smith ImplicitValueInitExpr VIE(ElemTy); 3892de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith if (!EvaluateInPlace(*Value, Info, Subobject, &VIE)) 389351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return false; 389451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith } 389551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 3896e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs()); 3897745f5147e065900267c85a5568785a1991d4838fRichard Smith return HandleConstructorCall(E->getExprLoc(), Subobject, Args, 3898e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith cast<CXXConstructorDecl>(Definition), 3899de31aa7f0ef71f5c162372e319cbc03c0924f074Richard Smith Info, *Value); 3900e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith} 3901e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3902cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith//===----------------------------------------------------------------------===// 3903f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner// Integer Evaluation 3904c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// 3905c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// As a GNU extension, we support casting pointers to sufficiently-wide integer 3906c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// types and back in constant folding. Integer values are thus represented 3907c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// either as an integer-valued APValue, or as an lvalue-valued APValue. 3908f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===// 3909f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner 3910f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattnernamespace { 3911770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass IntExprEvaluator 39128cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne : public ExprEvaluatorBase<IntExprEvaluator, bool> { 39131aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith APValue &Result; 3914f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattnerpublic: 39151aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith IntExprEvaluator(EvalInfo &info, APValue &result) 39168cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne : ExprEvaluatorBaseTy(info), Result(result) {} 3917f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner 3918cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis bool Success(const llvm::APSInt &SI, const Expr *E, APValue &Result) { 3919973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara assert(E->getType()->isIntegralOrEnumerationType() && 39202ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor "Invalid evaluation result."); 3921973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() && 39223f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar "Invalid evaluation result."); 3923973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && 39243f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar "Invalid evaluation result."); 39251aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith Result = APValue(SI); 39263f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar return true; 39273f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar } 3928cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis bool Success(const llvm::APSInt &SI, const Expr *E) { 3929cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis return Success(SI, E, Result); 3930cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis } 39313f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar 3932cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis bool Success(const llvm::APInt &I, const Expr *E, APValue &Result) { 39332ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor assert(E->getType()->isIntegralOrEnumerationType() && 39342ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor "Invalid evaluation result."); 393530c37f4d2ee5811e85f692c22fb67d74ddc88079Daniel Dunbar assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && 39363f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar "Invalid evaluation result."); 39371aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith Result = APValue(APSInt(I)); 3938575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor Result.getInt().setIsUnsigned( 3939575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor E->getType()->isUnsignedIntegerOrEnumerationType()); 3940131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return true; 3941131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar } 3942cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis bool Success(const llvm::APInt &I, const Expr *E) { 3943cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis return Success(I, E, Result); 3944cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis } 3945131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar 3946cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis bool Success(uint64_t Value, const Expr *E, APValue &Result) { 39472ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor assert(E->getType()->isIntegralOrEnumerationType() && 39482ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor "Invalid evaluation result."); 39491aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType())); 3950131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return true; 3951131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar } 3952cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis bool Success(uint64_t Value, const Expr *E) { 3953cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis return Success(Value, E, Result); 3954cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis } 3955131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar 39564f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck bool Success(CharUnits Size, const Expr *E) { 39574f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck return Success(Size.getQuantity(), E); 39584f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck } 39594f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck 39601aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith bool Success(const APValue &V, const Expr *E) { 39615930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman if (V.isLValue() || V.isAddrLabelDiff()) { 3962342f1f8b0a402c5a7f8c5055db7f60a7808f1687Richard Smith Result = V; 3963342f1f8b0a402c5a7f8c5055db7f60a7808f1687Richard Smith return true; 3964342f1f8b0a402c5a7f8c5055db7f60a7808f1687Richard Smith } 39658cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return Success(V.getInt(), E); 396632fea9d18cc3658a1b01df5ca6f2ac302625c61dChris Lattner } 39671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 396851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith bool ZeroInitialization(const Expr *E) { return Success(0, E); } 3969f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith 39708cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne //===--------------------------------------------------------------------===// 39718cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne // Visitor Methods 39728cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne //===--------------------------------------------------------------------===// 3973f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner 39744c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner bool VisitIntegerLiteral(const IntegerLiteral *E) { 3975131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success(E->getValue(), E); 39764c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner } 39774c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner bool VisitCharacterLiteral(const CharacterLiteral *E) { 3978131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success(E->getValue(), E); 39794c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner } 3980043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman 3981043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman bool CheckReferencedDecl(const Expr *E, const Decl *D); 3982043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman bool VisitDeclRefExpr(const DeclRefExpr *E) { 39838cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne if (CheckReferencedDecl(E, E->getDecl())) 39848cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return true; 39858cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 39868cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return ExprEvaluatorBaseTy::VisitDeclRefExpr(E); 3987043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman } 3988043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman bool VisitMemberExpr(const MemberExpr *E) { 3989043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman if (CheckReferencedDecl(E, E->getMemberDecl())) { 3990c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith VisitIgnoredValue(E->getBase()); 3991043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman return true; 3992043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman } 39938cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 39948cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return ExprEvaluatorBaseTy::VisitMemberExpr(E); 3995043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman } 3996043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman 39978cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitCallExpr(const CallExpr *E); 3998b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattner bool VisitBinaryOperator(const BinaryOperator *E); 39998ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor bool VisitOffsetOfExpr(const OffsetOfExpr *E); 4000b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattner bool VisitUnaryOperator(const UnaryOperator *E); 4001f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner 40028cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitCastExpr(const CastExpr* E); 4003f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E); 40040518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl 40053068d117951a8df54bae9db039b56201ab10962bAnders Carlsson bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { 4006131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success(E->getValue(), E); 40073068d117951a8df54bae9db039b56201ab10962bAnders Carlsson } 40081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4009ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek bool VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) { 4010ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek return Success(E->getValue(), E); 4011ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } 4012ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 4013f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith // Note, GNU defines __null as an integer, not a pointer. 40143f70456b8adb0405ef2a47d51f9fc2d5937ae8aeAnders Carlsson bool VisitGNUNullExpr(const GNUNullExpr *E) { 401551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return ZeroInitialization(E); 4016664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman } 4017664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman 401864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) { 40190dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl return Success(E->getValue(), E); 402064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl } 402164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl 40226ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet bool VisitBinaryTypeTraitExpr(const BinaryTypeTraitExpr *E) { 40236ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet return Success(E->getValue(), E); 40246ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet } 40256ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet 40264ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor bool VisitTypeTraitExpr(const TypeTraitExpr *E) { 40274ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor return Success(E->getValue(), E); 40284ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor } 40294ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor 403021ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) { 403121ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley return Success(E->getValue(), E); 403221ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley } 403321ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley 4034552622067dc45013d240f73952fece703f5e63bdJohn Wiegley bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) { 4035552622067dc45013d240f73952fece703f5e63bdJohn Wiegley return Success(E->getValue(), E); 4036552622067dc45013d240f73952fece703f5e63bdJohn Wiegley } 4037552622067dc45013d240f73952fece703f5e63bdJohn Wiegley 4038722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman bool VisitUnaryReal(const UnaryOperator *E); 4039664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman bool VisitUnaryImag(const UnaryOperator *E); 4040664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman 4041295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E); 4042ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor bool VisitSizeOfPackExpr(const SizeOfPackExpr *E); 4043cea8d966f826554f0679595e9371e314e8dbc1cfSebastian Redl 4044fcee0019b76f9f368f2b3d6d4048a98232593f29Chris Lattnerprivate: 40458b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck CharUnits GetAlignOfExpr(const Expr *E); 40468b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck CharUnits GetAlignOfType(QualType T); 40471bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith static QualType GetObjectType(APValue::LValueBase B); 40488cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool TryEvaluateBuiltinObjectSize(const CallExpr *E); 4049664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman // FIXME: Missing: array subscript of vector, member of vector 4050f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner}; 4051f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner} // end anonymous namespace 4052f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner 4053c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// EvaluateIntegerOrLValue - Evaluate an rvalue integral-typed expression, and 4054c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// produce either the integer value or a pointer. 4055c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// 4056c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// GCC has a heinous extension which folds casts between pointer types and 4057c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// pointer-sized integral types. We support this by allowing the evaluation of 4058c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// an integer rvalue to produce a pointer (represented as an lvalue) instead. 4059c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// Some simple arithmetic on such values is supported (they are treated much 4060c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// like char*). 40611aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smithstatic bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, 406247a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith EvalInfo &Info) { 4063c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith assert(E->isRValue() && E->getType()->isIntegralOrEnumerationType()); 40648cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return IntExprEvaluator(Info, Result).Visit(E); 406569ab26a8623141f35e86817cfc6e0fbe7639a40fDaniel Dunbar} 406669ab26a8623141f35e86817cfc6e0fbe7639a40fDaniel Dunbar 4067f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info) { 40681aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith APValue Val; 4069f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!EvaluateIntegerOrLValue(E, Val, Info)) 4070f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 4071f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!Val.isInt()) { 4072f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith // FIXME: It would be better to produce the diagnostic for casting 4073f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith // a pointer to an integer. 40745cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); 407530c37f4d2ee5811e85f692c22fb67d74ddc88079Daniel Dunbar return false; 4076f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 407730c37f4d2ee5811e85f692c22fb67d74ddc88079Daniel Dunbar Result = Val.getInt(); 407830c37f4d2ee5811e85f692c22fb67d74ddc88079Daniel Dunbar return true; 4079f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner} 4080f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner 4081f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// Check whether the given declaration can be directly converted to an integral 4082f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// rvalue. If not, no diagnostic is produced; there are other things we can 4083f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// try. 4084043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedmanbool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) { 40854c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner // Enums are integer constant exprs. 4086bfbdcd861a4364bfc21a9e5047bdbd56812d6693Abramo Bagnara if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) { 4087973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara // Check for signedness/width mismatches between E type and ECD value. 4088973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara bool SameSign = (ECD->getInitVal().isSigned() 4089973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara == E->getType()->isSignedIntegerOrEnumerationType()); 4090973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara bool SameWidth = (ECD->getInitVal().getBitWidth() 4091973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara == Info.Ctx.getIntWidth(E->getType())); 4092973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara if (SameSign && SameWidth) 4093973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara return Success(ECD->getInitVal(), E); 4094973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara else { 4095973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara // Get rid of mismatch (otherwise Success assertions will fail) 4096973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara // by computing a new value matching the type of E. 4097973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara llvm::APSInt Val = ECD->getInitVal(); 4098973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara if (!SameSign) 4099973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara Val.setIsSigned(!ECD->getInitVal().isSigned()); 4100973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara if (!SameWidth) 4101973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara Val = Val.extOrTrunc(Info.Ctx.getIntWidth(E->getType())); 4102973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara return Success(Val, E); 4103973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara } 4104bfbdcd861a4364bfc21a9e5047bdbd56812d6693Abramo Bagnara } 41058cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return false; 41064c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner} 41074c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner 4108a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way 4109a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner/// as GCC. 4110a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattnerstatic int EvaluateBuiltinClassifyType(const CallExpr *E) { 4111a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner // The following enum mimics the values returned by GCC. 41127c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl // FIXME: Does GCC differ between lvalue and rvalue references here? 4113a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner enum gcc_type_class { 4114a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner no_type_class = -1, 4115a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner void_type_class, integer_type_class, char_type_class, 4116a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner enumeral_type_class, boolean_type_class, 4117a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner pointer_type_class, reference_type_class, offset_type_class, 4118a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner real_type_class, complex_type_class, 4119a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner function_type_class, method_type_class, 4120a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner record_type_class, union_type_class, 4121a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner array_type_class, string_type_class, 4122a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner lang_type_class 4123a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner }; 41241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 41251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // If no argument was supplied, default to "no_type_class". This isn't 4126a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner // ideal, however it is what gcc does. 4127a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner if (E->getNumArgs() == 0) 4128a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return no_type_class; 41291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4130a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner QualType ArgTy = E->getArg(0)->getType(); 4131a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner if (ArgTy->isVoidType()) 4132a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return void_type_class; 4133a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner else if (ArgTy->isEnumeralType()) 4134a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return enumeral_type_class; 4135a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner else if (ArgTy->isBooleanType()) 4136a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return boolean_type_class; 4137a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner else if (ArgTy->isCharType()) 4138a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return string_type_class; // gcc doesn't appear to use char_type_class 4139a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner else if (ArgTy->isIntegerType()) 4140a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return integer_type_class; 4141a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner else if (ArgTy->isPointerType()) 4142a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return pointer_type_class; 4143a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner else if (ArgTy->isReferenceType()) 4144a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return reference_type_class; 4145a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner else if (ArgTy->isRealType()) 4146a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return real_type_class; 4147a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner else if (ArgTy->isComplexType()) 4148a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return complex_type_class; 4149a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner else if (ArgTy->isFunctionType()) 4150a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return function_type_class; 4151fb87b89fc9eb103e19fb8e4b925c23f0bd091b99Douglas Gregor else if (ArgTy->isStructureOrClassType()) 4152a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return record_type_class; 4153a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner else if (ArgTy->isUnionType()) 4154a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return union_type_class; 4155a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner else if (ArgTy->isArrayType()) 4156a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return array_type_class; 4157a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner else if (ArgTy->isUnionType()) 4158a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return union_type_class; 4159a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner else // FIXME: offset_type_class, method_type_class, & lang_type_class? 4160b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie llvm_unreachable("CallExpr::isBuiltinClassifyType(): unimplemented type"); 4161a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner} 4162a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner 416380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// EvaluateBuiltinConstantPForLValue - Determine the result of 416480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// __builtin_constant_p when applied to the given lvalue. 416580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// 416680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// An lvalue is only "constant" if it is a pointer or reference to the first 416780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// character of a string literal. 416880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smithtemplate<typename LValue> 416980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smithstatic bool EvaluateBuiltinConstantPForLValue(const LValue &LV) { 41708e55ed1ad3acf9c7f8424aa7d326b3b2c18e943bDouglas Gregor const Expr *E = LV.getLValueBase().template dyn_cast<const Expr*>(); 417180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith return E && isa<StringLiteral>(E) && LV.getLValueOffset().isZero(); 417280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith} 417380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith 417480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// EvaluateBuiltinConstantP - Evaluate __builtin_constant_p as similarly to 417580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// GCC as we can manage. 417680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smithstatic bool EvaluateBuiltinConstantP(ASTContext &Ctx, const Expr *Arg) { 417780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith QualType ArgType = Arg->getType(); 417880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith 417980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // __builtin_constant_p always has one operand. The rules which gcc follows 418080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // are not precisely documented, but are as follows: 418180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // 418280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // - If the operand is of integral, floating, complex or enumeration type, 418380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // and can be folded to a known value of that type, it returns 1. 418480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // - If the operand and can be folded to a pointer to the first character 418580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // of a string literal (or such a pointer cast to an integral type), it 418680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // returns 1. 418780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // 418880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // Otherwise, it returns 0. 418980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // 419080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // FIXME: GCC also intends to return 1 for literals of aggregate types, but 419180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // its support for this does not currently work. 419280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith if (ArgType->isIntegralOrEnumerationType()) { 419380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith Expr::EvalResult Result; 419480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith if (!Arg->EvaluateAsRValue(Result, Ctx) || Result.HasSideEffects) 419580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith return false; 419680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith 419780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith APValue &V = Result.Val; 419880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith if (V.getKind() == APValue::Int) 419980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith return true; 420080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith 420180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith return EvaluateBuiltinConstantPForLValue(V); 420280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith } else if (ArgType->isFloatingType() || ArgType->isAnyComplexType()) { 420380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith return Arg->isEvaluatable(Ctx); 420480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith } else if (ArgType->isPointerType() || Arg->isGLValue()) { 420580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith LValue LV; 420680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith Expr::EvalStatus Status; 420780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith EvalInfo Info(Ctx, Status); 420880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith if ((Arg->isGLValue() ? EvaluateLValue(Arg, LV, Info) 420980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith : EvaluatePointer(Arg, LV, Info)) && 421080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith !Status.HasSideEffects) 421180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith return EvaluateBuiltinConstantPForLValue(LV); 421280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith } 421380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith 421480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // Anything else isn't considered to be sufficiently constant. 421580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith return false; 421680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith} 421780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith 421842c8f87eb60958170c46767273bf93e6c96125bfJohn McCall/// Retrieves the "underlying object type" of the given expression, 421942c8f87eb60958170c46767273bf93e6c96125bfJohn McCall/// as used by __builtin_object_size. 42201bf9a9e6a5bdc0de7939908855dcddf46b661800Richard SmithQualType IntExprEvaluator::GetObjectType(APValue::LValueBase B) { 42211bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { 42221bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith if (const VarDecl *VD = dyn_cast<VarDecl>(D)) 422342c8f87eb60958170c46767273bf93e6c96125bfJohn McCall return VD->getType(); 42241bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith } else if (const Expr *E = B.get<const Expr*>()) { 42251bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith if (isa<CompoundLiteralExpr>(E)) 42261bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith return E->getType(); 422742c8f87eb60958170c46767273bf93e6c96125bfJohn McCall } 422842c8f87eb60958170c46767273bf93e6c96125bfJohn McCall 422942c8f87eb60958170c46767273bf93e6c96125bfJohn McCall return QualType(); 423042c8f87eb60958170c46767273bf93e6c96125bfJohn McCall} 423142c8f87eb60958170c46767273bf93e6c96125bfJohn McCall 42328cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool IntExprEvaluator::TryEvaluateBuiltinObjectSize(const CallExpr *E) { 423342c8f87eb60958170c46767273bf93e6c96125bfJohn McCall LValue Base; 4234c6794850a570a91c5f224b6f0293db9f560f4213Richard Smith 4235c6794850a570a91c5f224b6f0293db9f560f4213Richard Smith { 4236c6794850a570a91c5f224b6f0293db9f560f4213Richard Smith // The operand of __builtin_object_size is never evaluated for side-effects. 4237c6794850a570a91c5f224b6f0293db9f560f4213Richard Smith // If there are any, but we can determine the pointed-to object anyway, then 4238c6794850a570a91c5f224b6f0293db9f560f4213Richard Smith // ignore the side-effects. 4239c6794850a570a91c5f224b6f0293db9f560f4213Richard Smith SpeculativeEvaluationRAII SpeculativeEval(Info); 4240c6794850a570a91c5f224b6f0293db9f560f4213Richard Smith if (!EvaluatePointer(E->getArg(0), Base, Info)) 4241c6794850a570a91c5f224b6f0293db9f560f4213Richard Smith return false; 4242c6794850a570a91c5f224b6f0293db9f560f4213Richard Smith } 424342c8f87eb60958170c46767273bf93e6c96125bfJohn McCall 424442c8f87eb60958170c46767273bf93e6c96125bfJohn McCall // If we can prove the base is null, lower to zero now. 42451bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith if (!Base.getLValueBase()) return Success(0, E); 424642c8f87eb60958170c46767273bf93e6c96125bfJohn McCall 42471bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith QualType T = GetObjectType(Base.getLValueBase()); 424842c8f87eb60958170c46767273bf93e6c96125bfJohn McCall if (T.isNull() || 424942c8f87eb60958170c46767273bf93e6c96125bfJohn McCall T->isIncompleteType() || 42501357869bc5983cdfbc986db1f3d18265bb34cb0eEli Friedman T->isFunctionType() || 425142c8f87eb60958170c46767273bf93e6c96125bfJohn McCall T->isVariablyModifiedType() || 425242c8f87eb60958170c46767273bf93e6c96125bfJohn McCall T->isDependentType()) 4253f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 425442c8f87eb60958170c46767273bf93e6c96125bfJohn McCall 425542c8f87eb60958170c46767273bf93e6c96125bfJohn McCall CharUnits Size = Info.Ctx.getTypeSizeInChars(T); 425642c8f87eb60958170c46767273bf93e6c96125bfJohn McCall CharUnits Offset = Base.getLValueOffset(); 425742c8f87eb60958170c46767273bf93e6c96125bfJohn McCall 425842c8f87eb60958170c46767273bf93e6c96125bfJohn McCall if (!Offset.isNegative() && Offset <= Size) 425942c8f87eb60958170c46767273bf93e6c96125bfJohn McCall Size -= Offset; 426042c8f87eb60958170c46767273bf93e6c96125bfJohn McCall else 426142c8f87eb60958170c46767273bf93e6c96125bfJohn McCall Size = CharUnits::Zero(); 42624f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck return Success(Size, E); 426342c8f87eb60958170c46767273bf93e6c96125bfJohn McCall} 426442c8f87eb60958170c46767273bf93e6c96125bfJohn McCall 42658cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { 42662c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith switch (unsigned BuiltinOp = E->isBuiltinCall()) { 4267019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner default: 42688cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return ExprEvaluatorBaseTy::VisitCallExpr(E); 426964eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump 427064eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump case Builtin::BI__builtin_object_size: { 427142c8f87eb60958170c46767273bf93e6c96125bfJohn McCall if (TryEvaluateBuiltinObjectSize(E)) 427242c8f87eb60958170c46767273bf93e6c96125bfJohn McCall return true; 427364eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump 42748ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith // If evaluating the argument has side-effects, we can't determine the size 42758ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith // of the object, and so we lower it to unknown now. CodeGen relies on us to 42768ae4ec28451a16a57718286da3e476fc2f495c3fRichard Smith // handle all cases where the expression has side-effects. 4277393c247fe025ccb5f914e37e948192ea86faef8cFariborz Jahanian if (E->getArg(0)->HasSideEffects(Info.Ctx)) { 4278a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith if (E->getArg(1)->EvaluateKnownConstInt(Info.Ctx).getZExtValue() <= 1) 4279cf184655319cf7a5b811067cff9d26a5741fd161Chris Lattner return Success(-1ULL, E); 428064eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump return Success(0, E); 428164eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump } 4282c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump 4283c6794850a570a91c5f224b6f0293db9f560f4213Richard Smith // Expression had no side effects, but we couldn't statically determine the 4284c6794850a570a91c5f224b6f0293db9f560f4213Richard Smith // size of the referenced object. 4285f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 428664eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump } 428764eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump 4288019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner case Builtin::BI__builtin_classify_type: 4289131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success(EvaluateBuiltinClassifyType(E), E); 42901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 429180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith case Builtin::BI__builtin_constant_p: 429280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith return Success(EvaluateBuiltinConstantP(Info.Ctx, E->getArg(0)), E); 4293e052d46f4db91f9ba572859ffc984e85cbf5d5ffRichard Smith 429421fb98ee003e992b0c4e204d98a19e0ef544cae3Chris Lattner case Builtin::BI__builtin_eh_return_data_regno: { 4295a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith int Operand = E->getArg(0)->EvaluateKnownConstInt(Info.Ctx).getZExtValue(); 4296bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor Operand = Info.Ctx.getTargetInfo().getEHDataRegisterNumber(Operand); 429721fb98ee003e992b0c4e204d98a19e0ef544cae3Chris Lattner return Success(Operand, E); 429821fb98ee003e992b0c4e204d98a19e0ef544cae3Chris Lattner } 4299c4a2638b5ef3e2d35d872614ceb655a7a22c58beEli Friedman 4300c4a2638b5ef3e2d35d872614ceb655a7a22c58beEli Friedman case Builtin::BI__builtin_expect: 4301c4a2638b5ef3e2d35d872614ceb655a7a22c58beEli Friedman return Visit(E->getArg(0)); 430240b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith 43035726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor case Builtin::BIstrlen: 430440b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith // A call to strlen is not a constant expression. 430540b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith if (Info.getLangOpts().CPlusPlus0x) 43065cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.CCEDiag(E, diag::note_constexpr_invalid_function) 430740b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith << /*isConstexpr*/0 << /*isConstructor*/0 << "'strlen'"; 430840b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith else 43095cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); 431040b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith // Fall through. 43115726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor case Builtin::BI__builtin_strlen: 43125726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor // As an extension, we support strlen() and __builtin_strlen() as constant 43135726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor // expressions when the argument is a string literal. 43148cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne if (const StringLiteral *S 43155726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor = dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenImpCasts())) { 43165726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor // The string literal may have embedded null characters. Find the first 43175726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor // one and truncate there. 43185f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef Str = S->getString(); 43195f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef::size_type Pos = Str.find(0); 43205f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner if (Pos != StringRef::npos) 43215726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor Str = Str.substr(0, Pos); 43225726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor 43235726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor return Success(Str.size(), E); 43245726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor } 43255726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor 4326f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 4327454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman 43282c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith case Builtin::BI__atomic_always_lock_free: 4329fafbf06732746f3ceca21d452d77b144ba8652aeRichard Smith case Builtin::BI__atomic_is_lock_free: 4330fafbf06732746f3ceca21d452d77b144ba8652aeRichard Smith case Builtin::BI__c11_atomic_is_lock_free: { 4331454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman APSInt SizeVal; 4332454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman if (!EvaluateInteger(E->getArg(0), SizeVal, Info)) 4333454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman return false; 4334454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman 4335454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman // For __atomic_is_lock_free(sizeof(_Atomic(T))), if the size is a power 4336454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman // of two less than the maximum inline atomic width, we know it is 4337454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman // lock-free. If the size isn't a power of two, or greater than the 4338454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman // maximum alignment where we promote atomics, we know it is not lock-free 4339454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman // (at least not in the sense of atomic_is_lock_free). Otherwise, 4340454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman // the answer can only be determined at runtime; for example, 16-byte 4341454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman // atomics have lock-free implementations on some, but not all, 4342454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman // x86-64 processors. 4343454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman 4344454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman // Check power-of-two. 4345454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman CharUnits Size = CharUnits::fromQuantity(SizeVal.getZExtValue()); 43462c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith if (Size.isPowerOfTwo()) { 43472c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith // Check against inlining width. 43482c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith unsigned InlineWidthBits = 43492c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth(); 43502c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith if (Size <= Info.Ctx.toCharUnitsFromBits(InlineWidthBits)) { 43512c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith if (BuiltinOp == Builtin::BI__c11_atomic_is_lock_free || 43522c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith Size == CharUnits::One() || 43532c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith E->getArg(1)->isNullPointerConstant(Info.Ctx, 43542c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith Expr::NPC_NeverValueDependent)) 43552c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith // OK, we will inline appropriately-aligned operations of this size, 43562c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith // and _Atomic(T) is appropriately-aligned. 43572c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith return Success(1, E); 43582c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith 43592c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith QualType PointeeType = E->getArg(1)->IgnoreImpCasts()->getType()-> 43602c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith castAs<PointerType>()->getPointeeType(); 43612c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith if (!PointeeType->isIncompleteType() && 43622c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith Info.Ctx.getTypeAlignInChars(PointeeType) >= Size) { 43632c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith // OK, we will inline operations on this object. 43642c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith return Success(1, E); 43652c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith } 43662c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith } 43672c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith } 4368454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman 43692c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith return BuiltinOp == Builtin::BI__atomic_always_lock_free ? 43702c39d71bb7cefdfe6116fa52454f3b3dc5abd517Richard Smith Success(0, E) : Error(E); 4371454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman } 4372019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner } 43734c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner} 4374f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner 4375625b80755b603d28f36fb4212c81484d87ad08d3Richard Smithstatic bool HasSameBase(const LValue &A, const LValue &B) { 4376625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith if (!A.getLValueBase()) 4377625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith return !B.getLValueBase(); 4378625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith if (!B.getLValueBase()) 4379625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith return false; 4380625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith 43811bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith if (A.getLValueBase().getOpaqueValue() != 43821bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith B.getLValueBase().getOpaqueValue()) { 4383625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith const Decl *ADecl = GetLValueBaseDecl(A); 4384625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith if (!ADecl) 4385625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith return false; 4386625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith const Decl *BDecl = GetLValueBaseDecl(B); 43879a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith if (!BDecl || ADecl->getCanonicalDecl() != BDecl->getCanonicalDecl()) 4388625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith return false; 4389625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith } 4390625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith 4391625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith return IsGlobalLValue(A.getLValueBase()) || 439283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith A.getLValueCallIndex() == B.getLValueCallIndex(); 4393625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith} 4394625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith 43957b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith/// Perform the given integer operation, which is known to need at most BitWidth 43967b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith/// bits, and check for overflow in the original type (if that type was not an 43977b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith/// unsigned type). 43987b48a2986345480241f3b8209f71bb21b0530b4fRichard Smithtemplate<typename Operation> 43997b48a2986345480241f3b8209f71bb21b0530b4fRichard Smithstatic APSInt CheckedIntArithmetic(EvalInfo &Info, const Expr *E, 44007b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith const APSInt &LHS, const APSInt &RHS, 44017b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith unsigned BitWidth, Operation Op) { 44027b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith if (LHS.isUnsigned()) 44037b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith return Op(LHS, RHS); 44047b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith 44057b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith APSInt Value(Op(LHS.extend(BitWidth), RHS.extend(BitWidth)), false); 44067b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith APSInt Result = Value.trunc(LHS.getBitWidth()); 44077b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith if (Result.extend(BitWidth) != Value) 44087b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith HandleOverflow(Info, E, Value, E->getType()); 44097b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith return Result; 44107b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith} 44117b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith 4412cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidisnamespace { 4413c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 4414cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis/// \brief Data recursive integer evaluator of certain binary operators. 4415cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis/// 4416cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis/// We use a data recursive algorithm for binary operators so that we are able 4417cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis/// to handle extreme cases of chained binary operators without causing stack 4418cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis/// overflow. 4419cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidisclass DataRecursiveIntBinOpEvaluator { 4420cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis struct EvalResult { 4421cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis APValue Val; 4422cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis bool Failed; 4423cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4424cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis EvalResult() : Failed(false) { } 4425cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4426cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis void swap(EvalResult &RHS) { 4427cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis Val.swap(RHS.Val); 4428cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis Failed = RHS.Failed; 4429cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis RHS.Failed = false; 4430cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis } 4431cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis }; 4432cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4433cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis struct Job { 4434cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis const Expr *E; 4435cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis EvalResult LHSResult; // meaningful only for binary operator expression. 4436cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis enum { AnyExprKind, BinOpKind, BinOpVisitedLHSKind } Kind; 4437cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4438cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis Job() : StoredInfo(0) { } 4439cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis void startSpeculativeEval(EvalInfo &Info) { 4440cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis OldEvalStatus = Info.EvalStatus; 4441cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis Info.EvalStatus.Diag = 0; 4442cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis StoredInfo = &Info; 4443cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis } 4444cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis ~Job() { 4445cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis if (StoredInfo) { 4446cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis StoredInfo->EvalStatus = OldEvalStatus; 4447cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis } 4448cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis } 4449cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis private: 4450cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis EvalInfo *StoredInfo; // non-null if status changed. 4451cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis Expr::EvalStatus OldEvalStatus; 4452cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis }; 4453cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4454cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis SmallVector<Job, 16> Queue; 4455cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4456cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis IntExprEvaluator &IntEval; 4457cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis EvalInfo &Info; 4458cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis APValue &FinalResult; 4459cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4460cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidispublic: 4461cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis DataRecursiveIntBinOpEvaluator(IntExprEvaluator &IntEval, APValue &Result) 4462cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis : IntEval(IntEval), Info(IntEval.getEvalInfo()), FinalResult(Result) { } 4463cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4464cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis /// \brief True if \param E is a binary operator that we are going to handle 4465cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis /// data recursively. 4466cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis /// We handle binary operators that are comma, logical, or that have operands 4467cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis /// with integral or enumeration type. 4468cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis static bool shouldEnqueue(const BinaryOperator *E) { 4469cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis return E->getOpcode() == BO_Comma || 4470cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis E->isLogicalOp() || 4471cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis (E->getLHS()->getType()->isIntegralOrEnumerationType() && 4472cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis E->getRHS()->getType()->isIntegralOrEnumerationType()); 4473cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis } 4474cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4475cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis bool Traverse(const BinaryOperator *E) { 4476cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis enqueue(E); 4477cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis EvalResult PrevResult; 4478b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu while (!Queue.empty()) 4479b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu process(PrevResult); 4480b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu 4481b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu if (PrevResult.Failed) return false; 4482cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4483cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis FinalResult.swap(PrevResult.Val); 4484cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis return true; 4485a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman } 4486a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman 4487cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidisprivate: 4488cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis bool Success(uint64_t Value, const Expr *E, APValue &Result) { 4489cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis return IntEval.Success(Value, E, Result); 4490cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis } 4491cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis bool Success(const APSInt &Value, const Expr *E, APValue &Result) { 4492cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis return IntEval.Success(Value, E, Result); 4493cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis } 4494cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis bool Error(const Expr *E) { 4495cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis return IntEval.Error(E); 4496cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis } 4497cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis bool Error(const Expr *E, diag::kind D) { 4498cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis return IntEval.Error(E, D); 4499cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis } 4500cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4501cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { 4502cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis return Info.CCEDiag(E, D); 4503cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis } 4504cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 45059293fff58aa0198fa7a6bb504169bcac01dbbff7Argyrios Kyrtzidis // \brief Returns true if visiting the RHS is necessary, false otherwise. 45069293fff58aa0198fa7a6bb504169bcac01dbbff7Argyrios Kyrtzidis bool VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E, 4507cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis bool &SuppressRHSDiags); 45082fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis 4509cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis bool VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult, 4510cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis const BinaryOperator *E, APValue &Result); 4511cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4512cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis void EvaluateExpr(const Expr *E, EvalResult &Result) { 4513cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis Result.Failed = !Evaluate(Result.Val, Info, E); 4514cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis if (Result.Failed) 4515cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis Result.Val = APValue(); 4516cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis } 4517cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4518b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu void process(EvalResult &Result); 4519cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4520cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis void enqueue(const Expr *E) { 4521cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis E = E->IgnoreParens(); 4522cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis Queue.resize(Queue.size()+1); 4523cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis Queue.back().E = E; 4524cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis Queue.back().Kind = Job::AnyExprKind; 4525cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis } 4526cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis}; 4527cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4528cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis} 4529cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4530cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidisbool DataRecursiveIntBinOpEvaluator:: 45319293fff58aa0198fa7a6bb504169bcac01dbbff7Argyrios Kyrtzidis VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E, 4532cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis bool &SuppressRHSDiags) { 4533cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis if (E->getOpcode() == BO_Comma) { 4534cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis // Ignore LHS but note if we could not evaluate it. 4535cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis if (LHSResult.Failed) 4536cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis Info.EvalStatus.HasSideEffects = true; 4537cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis return true; 4538cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis } 4539cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4540cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis if (E->isLogicalOp()) { 4541cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis bool lhsResult; 4542cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis if (HandleConversionToBool(LHSResult.Val, lhsResult)) { 45432fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis // We were able to evaluate the LHS, see if we can get away with not 45442fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis // evaluating the RHS: 0 && X -> 0, 1 || X -> 1 4545cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis if (lhsResult == (E->getOpcode() == BO_LOr)) { 45469293fff58aa0198fa7a6bb504169bcac01dbbff7Argyrios Kyrtzidis Success(lhsResult, E, LHSResult.Val); 45479293fff58aa0198fa7a6bb504169bcac01dbbff7Argyrios Kyrtzidis return false; // Ignore RHS 45482fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis } 45492fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis } else { 45502fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis // Since we weren't able to evaluate the left hand side, it 45512fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis // must have had side effects. 45522fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis Info.EvalStatus.HasSideEffects = true; 4553cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4554cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis // We can't evaluate the LHS; however, sometimes the result 4555cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. 4556cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis // Don't ignore RHS and suppress diagnostics from this arm. 4557cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis SuppressRHSDiags = true; 4558cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis } 4559cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4560cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis return true; 4561cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis } 4562cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4563cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis assert(E->getLHS()->getType()->isIntegralOrEnumerationType() && 4564cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis E->getRHS()->getType()->isIntegralOrEnumerationType()); 4565cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4566cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis if (LHSResult.Failed && !Info.keepEvaluatingAfterFailure()) 45679293fff58aa0198fa7a6bb504169bcac01dbbff7Argyrios Kyrtzidis return false; // Ignore RHS; 45689293fff58aa0198fa7a6bb504169bcac01dbbff7Argyrios Kyrtzidis 4569cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis return true; 4570cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis} 45712fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis 4572cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidisbool DataRecursiveIntBinOpEvaluator:: 4573cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult, 4574cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis const BinaryOperator *E, APValue &Result) { 4575cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis if (E->getOpcode() == BO_Comma) { 4576cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis if (RHSResult.Failed) 4577cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis return false; 4578cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis Result = RHSResult.Val; 4579cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis return true; 4580cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis } 4581cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4582cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis if (E->isLogicalOp()) { 4583cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis bool lhsResult, rhsResult; 4584cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis bool LHSIsOK = HandleConversionToBool(LHSResult.Val, lhsResult); 4585cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis bool RHSIsOK = HandleConversionToBool(RHSResult.Val, rhsResult); 4586cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4587cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis if (LHSIsOK) { 4588cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis if (RHSIsOK) { 4589cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis if (E->getOpcode() == BO_LOr) 4590cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis return Success(lhsResult || rhsResult, E, Result); 4591cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis else 4592cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis return Success(lhsResult && rhsResult, E, Result); 4593cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis } 4594cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis } else { 4595cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis if (RHSIsOK) { 45962fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis // We can't evaluate the LHS; however, sometimes the result 45972fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. 45982fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis if (rhsResult == (E->getOpcode() == BO_LOr)) 4599cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis return Success(rhsResult, E, Result); 46002fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis } 46012fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis } 4602cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 46032fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis return false; 46042fa975c94027c6565cb112ffcf93c05b22922c0eArgyrios Kyrtzidis } 4605cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4606cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis assert(E->getLHS()->getType()->isIntegralOrEnumerationType() && 4607cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis E->getRHS()->getType()->isIntegralOrEnumerationType()); 4608cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4609cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis if (LHSResult.Failed || RHSResult.Failed) 4610cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis return false; 4611cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4612cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis const APValue &LHSVal = LHSResult.Val; 4613cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis const APValue &RHSVal = RHSResult.Val; 4614cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4615cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis // Handle cases like (unsigned long)&a + 4. 4616cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis if (E->isAdditiveOp() && LHSVal.isLValue() && RHSVal.isInt()) { 4617cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis Result = LHSVal; 4618cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis CharUnits AdditionalOffset = CharUnits::fromQuantity( 4619cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis RHSVal.getInt().getZExtValue()); 4620cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis if (E->getOpcode() == BO_Add) 4621cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis Result.getLValueOffset() += AdditionalOffset; 4622cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis else 4623cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis Result.getLValueOffset() -= AdditionalOffset; 4624cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis return true; 4625cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis } 4626cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4627cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis // Handle cases like 4 + (unsigned long)&a 4628cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis if (E->getOpcode() == BO_Add && 4629cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis RHSVal.isLValue() && LHSVal.isInt()) { 4630cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis Result = RHSVal; 4631cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis Result.getLValueOffset() += CharUnits::fromQuantity( 4632cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis LHSVal.getInt().getZExtValue()); 4633cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis return true; 4634cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis } 4635cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4636cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis if (E->getOpcode() == BO_Sub && LHSVal.isLValue() && RHSVal.isLValue()) { 4637cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis // Handle (intptr_t)&&A - (intptr_t)&&B. 4638cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis if (!LHSVal.getLValueOffset().isZero() || 4639cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis !RHSVal.getLValueOffset().isZero()) 4640cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis return false; 4641cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis const Expr *LHSExpr = LHSVal.getLValueBase().dyn_cast<const Expr*>(); 4642cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis const Expr *RHSExpr = RHSVal.getLValueBase().dyn_cast<const Expr*>(); 4643cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis if (!LHSExpr || !RHSExpr) 4644cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis return false; 4645cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); 4646cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); 4647cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis if (!LHSAddrExpr || !RHSAddrExpr) 4648cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis return false; 4649cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis // Make sure both labels come from the same function. 4650cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis if (LHSAddrExpr->getLabel()->getDeclContext() != 4651cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis RHSAddrExpr->getLabel()->getDeclContext()) 4652cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis return false; 4653cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis Result = APValue(LHSAddrExpr, RHSAddrExpr); 4654cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis return true; 4655cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis } 4656cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4657cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis // All the following cases expect both operands to be an integer 4658cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis if (!LHSVal.isInt() || !RHSVal.isInt()) 4659cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis return Error(E); 4660cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4661cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis const APSInt &LHS = LHSVal.getInt(); 4662cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis APSInt RHS = RHSVal.getInt(); 4663cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4664cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis switch (E->getOpcode()) { 4665cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis default: 4666cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis return Error(E); 4667cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis case BO_Mul: 4668cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis return Success(CheckedIntArithmetic(Info, E, LHS, RHS, 4669cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis LHS.getBitWidth() * 2, 4670cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis std::multiplies<APSInt>()), E, 4671cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis Result); 4672cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis case BO_Add: 4673cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis return Success(CheckedIntArithmetic(Info, E, LHS, RHS, 4674cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis LHS.getBitWidth() + 1, 4675cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis std::plus<APSInt>()), E, Result); 4676cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis case BO_Sub: 4677cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis return Success(CheckedIntArithmetic(Info, E, LHS, RHS, 4678cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis LHS.getBitWidth() + 1, 4679cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis std::minus<APSInt>()), E, Result); 4680cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis case BO_And: return Success(LHS & RHS, E, Result); 4681cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis case BO_Xor: return Success(LHS ^ RHS, E, Result); 4682cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis case BO_Or: return Success(LHS | RHS, E, Result); 4683cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis case BO_Div: 4684cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis case BO_Rem: 4685cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis if (RHS == 0) 4686cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis return Error(E, diag::note_expr_divide_by_zero); 4687cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis // Check for overflow case: INT_MIN / -1 or INT_MIN % -1. The latter is 4688cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis // not actually undefined behavior in C++11 due to a language defect. 4689cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis if (RHS.isNegative() && RHS.isAllOnesValue() && 4690cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis LHS.isSigned() && LHS.isMinSignedValue()) 4691cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis HandleOverflow(Info, E, -LHS.extend(LHS.getBitWidth() + 1), E->getType()); 4692cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis return Success(E->getOpcode() == BO_Rem ? LHS % RHS : LHS / RHS, E, 4693cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis Result); 4694cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis case BO_Shl: { 4695cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis // During constant-folding, a negative shift is an opposite shift. Such 4696cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis // a shift is not a constant expression. 4697cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis if (RHS.isSigned() && RHS.isNegative()) { 4698cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; 4699cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis RHS = -RHS; 4700cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis goto shift_right; 4701cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis } 4702cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4703cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis shift_left: 4704cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis // C++11 [expr.shift]p1: Shift width must be less than the bit width of 4705cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis // the shifted type. 4706cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); 4707cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis if (SA != RHS) { 4708cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis CCEDiag(E, diag::note_constexpr_large_shift) 4709cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis << RHS << E->getType() << LHS.getBitWidth(); 4710cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis } else if (LHS.isSigned()) { 4711cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis // C++11 [expr.shift]p2: A signed left shift must have a non-negative 4712cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis // operand, and must not overflow the corresponding unsigned type. 4713cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis if (LHS.isNegative()) 4714cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis CCEDiag(E, diag::note_constexpr_lshift_of_negative) << LHS; 4715cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis else if (LHS.countLeadingZeros() < SA) 4716cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis CCEDiag(E, diag::note_constexpr_lshift_discards); 4717cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis } 4718cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4719cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis return Success(LHS << SA, E, Result); 4720cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis } 4721cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis case BO_Shr: { 4722cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis // During constant-folding, a negative shift is an opposite shift. Such a 4723cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis // shift is not a constant expression. 4724cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis if (RHS.isSigned() && RHS.isNegative()) { 4725cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; 4726cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis RHS = -RHS; 4727cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis goto shift_left; 4728cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis } 4729cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4730cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis shift_right: 4731cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis // C++11 [expr.shift]p1: Shift width must be less than the bit width of the 4732cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis // shifted type. 4733cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); 4734cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis if (SA != RHS) 4735cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis CCEDiag(E, diag::note_constexpr_large_shift) 4736cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis << RHS << E->getType() << LHS.getBitWidth(); 4737cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4738cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis return Success(LHS >> SA, E, Result); 4739cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis } 4740cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4741cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis case BO_LT: return Success(LHS < RHS, E, Result); 4742cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis case BO_GT: return Success(LHS > RHS, E, Result); 4743cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis case BO_LE: return Success(LHS <= RHS, E, Result); 4744cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis case BO_GE: return Success(LHS >= RHS, E, Result); 4745cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis case BO_EQ: return Success(LHS == RHS, E, Result); 4746cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis case BO_NE: return Success(LHS != RHS, E, Result); 4747cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis } 4748cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis} 4749cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4750b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieuvoid DataRecursiveIntBinOpEvaluator::process(EvalResult &Result) { 4751cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis Job &job = Queue.back(); 4752cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4753cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis switch (job.Kind) { 4754cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis case Job::AnyExprKind: { 4755cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(job.E)) { 4756cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis if (shouldEnqueue(Bop)) { 4757cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis job.Kind = Job::BinOpKind; 4758cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis enqueue(Bop->getLHS()); 4759b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu return; 4760cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis } 4761cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis } 4762cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4763cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis EvaluateExpr(job.E, Result); 4764cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis Queue.pop_back(); 4765b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu return; 4766cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis } 4767cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4768cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis case Job::BinOpKind: { 4769cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis const BinaryOperator *Bop = cast<BinaryOperator>(job.E); 4770cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis bool SuppressRHSDiags = false; 47719293fff58aa0198fa7a6bb504169bcac01dbbff7Argyrios Kyrtzidis if (!VisitBinOpLHSOnly(Result, Bop, SuppressRHSDiags)) { 4772cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis Queue.pop_back(); 4773b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu return; 4774cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis } 4775cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis if (SuppressRHSDiags) 4776cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis job.startSpeculativeEval(Info); 47779293fff58aa0198fa7a6bb504169bcac01dbbff7Argyrios Kyrtzidis job.LHSResult.swap(Result); 4778cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis job.Kind = Job::BinOpVisitedLHSKind; 4779cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis enqueue(Bop->getRHS()); 4780b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu return; 4781cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis } 4782cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4783cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis case Job::BinOpVisitedLHSKind: { 4784cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis const BinaryOperator *Bop = cast<BinaryOperator>(job.E); 4785cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis EvalResult RHS; 4786cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis RHS.swap(Result); 4787b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu Result.Failed = !VisitBinOp(job.LHSResult, RHS, Bop, Result.Val); 4788cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis Queue.pop_back(); 4789b778305e95f9977e6710f2b04830ecc36398ab5eRichard Trieu return; 4790cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis } 4791cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis } 4792cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4793cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis llvm_unreachable("Invalid Job::Kind!"); 4794cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis} 4795cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4796cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidisbool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { 4797cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis if (E->isAssignmentOp()) 4798cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis return Error(E); 4799cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis 4800cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis if (DataRecursiveIntBinOpEvaluator::shouldEnqueue(E)) 4801cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis return DataRecursiveIntBinOpEvaluator(*this, Result).Traverse(E); 480254176fdb044312b4b77c3da6682d3575b3728d30Chris Lattner 4803286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson QualType LHSTy = E->getLHS()->getType(); 4804286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson QualType RHSTy = E->getRHS()->getType(); 48054087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar 48064087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar if (LHSTy->isAnyComplexType()) { 48074087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar assert(RHSTy->isAnyComplexType() && "Invalid comparison"); 4808f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall ComplexValue LHS, RHS; 48094087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar 4810745f5147e065900267c85a5568785a1991d4838fRichard Smith bool LHSOK = EvaluateComplex(E->getLHS(), LHS, Info); 4811745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!LHSOK && !Info.keepEvaluatingAfterFailure()) 48124087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar return false; 48134087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar 4814745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK) 48154087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar return false; 48164087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar 48174087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar if (LHS.isComplexFloat()) { 48181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump APFloat::cmpResult CR_r = 48194087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal()); 48201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump APFloat::cmpResult CR_i = 48214087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag()); 48224087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar 48232de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (E->getOpcode() == BO_EQ) 4824131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success((CR_r == APFloat::cmpEqual && 4825131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar CR_i == APFloat::cmpEqual), E); 4826131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar else { 48272de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall assert(E->getOpcode() == BO_NE && 4828131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar "Invalid complex comparison."); 48291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return Success(((CR_r == APFloat::cmpGreaterThan || 4830fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang CR_r == APFloat::cmpLessThan || 4831fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang CR_r == APFloat::cmpUnordered) || 48321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump (CR_i == APFloat::cmpGreaterThan || 4833fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang CR_i == APFloat::cmpLessThan || 4834fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang CR_i == APFloat::cmpUnordered)), E); 4835131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar } 48364087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar } else { 48372de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (E->getOpcode() == BO_EQ) 4838131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() && 4839131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar LHS.getComplexIntImag() == RHS.getComplexIntImag()), E); 4840131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar else { 48412de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall assert(E->getOpcode() == BO_NE && 4842131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar "Invalid compex comparison."); 4843131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() || 4844131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar LHS.getComplexIntImag() != RHS.getComplexIntImag()), E); 4845131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar } 48464087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar } 48474087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar } 48481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4849286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson if (LHSTy->isRealFloatingType() && 4850286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson RHSTy->isRealFloatingType()) { 4851286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson APFloat RHS(0.0), LHS(0.0); 48521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4853745f5147e065900267c85a5568785a1991d4838fRichard Smith bool LHSOK = EvaluateFloat(E->getRHS(), RHS, Info); 4854745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!LHSOK && !Info.keepEvaluatingAfterFailure()) 4855286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson return false; 48561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4857745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!EvaluateFloat(E->getLHS(), LHS, Info) || !LHSOK) 4858286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson return false; 48591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4860286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson APFloat::cmpResult CR = LHS.compare(RHS); 4861529569e68d10b0fd3750fd2124faf742249b846bAnders Carlsson 4862286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson switch (E->getOpcode()) { 4863286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson default: 4864b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie llvm_unreachable("Invalid binary operator!"); 48652de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_LT: 4866131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success(CR == APFloat::cmpLessThan, E); 48672de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_GT: 4868131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success(CR == APFloat::cmpGreaterThan, E); 48692de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_LE: 4870131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E); 48712de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_GE: 48721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual, 4873131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar E); 48742de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_EQ: 4875131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success(CR == APFloat::cmpEqual, E); 48762de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_NE: 48771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return Success(CR == APFloat::cmpGreaterThan 4878fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang || CR == APFloat::cmpLessThan 4879fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang || CR == APFloat::cmpUnordered, E); 4880286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson } 4881286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson } 48821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4883ad02d7debd03ff275ac8ea27891a4ecccdb78068Eli Friedman if (LHSTy->isPointerType() && RHSTy->isPointerType()) { 4884625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith if (E->getOpcode() == BO_Sub || E->isComparisonOp()) { 4885745f5147e065900267c85a5568785a1991d4838fRichard Smith LValue LHSValue, RHSValue; 4886745f5147e065900267c85a5568785a1991d4838fRichard Smith 4887745f5147e065900267c85a5568785a1991d4838fRichard Smith bool LHSOK = EvaluatePointer(E->getLHS(), LHSValue, Info); 4888745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!LHSOK && Info.keepEvaluatingAfterFailure()) 48893068d117951a8df54bae9db039b56201ab10962bAnders Carlsson return false; 4890a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman 4891745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!EvaluatePointer(E->getRHS(), RHSValue, Info) || !LHSOK) 48923068d117951a8df54bae9db039b56201ab10962bAnders Carlsson return false; 4893a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman 4894625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith // Reject differing bases from the normal codepath; we special-case 4895625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith // comparisons to null. 4896625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith if (!HasSameBase(LHSValue, RHSValue)) { 489765639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman if (E->getOpcode() == BO_Sub) { 489865639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman // Handle &&A - &&B. 489965639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman if (!LHSValue.Offset.isZero() || !RHSValue.Offset.isZero()) 490065639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman return false; 490165639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman const Expr *LHSExpr = LHSValue.Base.dyn_cast<const Expr*>(); 490265639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman const Expr *RHSExpr = LHSValue.Base.dyn_cast<const Expr*>(); 490365639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman if (!LHSExpr || !RHSExpr) 490465639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman return false; 490565639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); 490665639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); 490765639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman if (!LHSAddrExpr || !RHSAddrExpr) 490865639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman return false; 49095930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman // Make sure both labels come from the same function. 49105930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman if (LHSAddrExpr->getLabel()->getDeclContext() != 49115930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman RHSAddrExpr->getLabel()->getDeclContext()) 49125930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman return false; 49131aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith Result = APValue(LHSAddrExpr, RHSAddrExpr); 491465639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman return true; 491565639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman } 49169e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith // Inequalities and subtractions between unrelated pointers have 49179e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith // unspecified or undefined behavior. 49185bc86103767c2abcbfdd6518e0ccbbbb6aa59e0fEli Friedman if (!E->isEqualityOp()) 4919f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 4920ffbda40a1fb7169591dc01771f3511178a2f727cEli Friedman // A constant address may compare equal to the address of a symbol. 4921ffbda40a1fb7169591dc01771f3511178a2f727cEli Friedman // The one exception is that address of an object cannot compare equal 4922c45061bd0c0fdad4df8eea7e9e5af186d11427e5Eli Friedman // to a null pointer constant. 4923ffbda40a1fb7169591dc01771f3511178a2f727cEli Friedman if ((!LHSValue.Base && !LHSValue.Offset.isZero()) || 4924ffbda40a1fb7169591dc01771f3511178a2f727cEli Friedman (!RHSValue.Base && !RHSValue.Offset.isZero())) 4925f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 49269e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith // It's implementation-defined whether distinct literals will have 4927b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith // distinct addresses. In clang, the result of such a comparison is 4928b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith // unspecified, so it is not a constant expression. However, we do know 4929b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith // that the address of a literal will be non-null. 493074f4634781cee06e28eb741bda5d0f936fdd1948Richard Smith if ((IsLiteralLValue(LHSValue) || IsLiteralLValue(RHSValue)) && 493174f4634781cee06e28eb741bda5d0f936fdd1948Richard Smith LHSValue.Base && RHSValue.Base) 4932f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 49339e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith // We can't tell whether weak symbols will end up pointing to the same 49349e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith // object. 49359e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith if (IsWeakLValue(LHSValue) || IsWeakLValue(RHSValue)) 4936f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 49379e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith // Pointers with different bases cannot represent the same object. 4938c45061bd0c0fdad4df8eea7e9e5af186d11427e5Eli Friedman // (Note that clang defaults to -fmerge-all-constants, which can 4939c45061bd0c0fdad4df8eea7e9e5af186d11427e5Eli Friedman // lead to inconsistent results for comparisons involving the address 4940c45061bd0c0fdad4df8eea7e9e5af186d11427e5Eli Friedman // of a constant; this generally doesn't matter in practice.) 49419e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith return Success(E->getOpcode() == BO_NE, E); 49425bc86103767c2abcbfdd6518e0ccbbbb6aa59e0fEli Friedman } 4943a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman 494415efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith const CharUnits &LHSOffset = LHSValue.getLValueOffset(); 494515efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith const CharUnits &RHSOffset = RHSValue.getLValueOffset(); 494615efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith 4947f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator(); 4948f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator(); 4949f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 49502de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (E->getOpcode() == BO_Sub) { 4951f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // C++11 [expr.add]p6: 4952f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // Unless both pointers point to elements of the same array object, or 4953f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // one past the last element of the array object, the behavior is 4954f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // undefined. 4955f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && 4956f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith !AreElementsOfSameArray(getType(LHSValue.Base), 4957f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith LHSDesignator, RHSDesignator)) 4958f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith CCEDiag(E, diag::note_constexpr_pointer_subtraction_not_same_array); 4959f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 49604992bdde387c5f033bb450a716eaabc0fda52688Chris Lattner QualType Type = E->getLHS()->getType(); 49614992bdde387c5f033bb450a716eaabc0fda52688Chris Lattner QualType ElementType = Type->getAs<PointerType>()->getPointeeType(); 49623068d117951a8df54bae9db039b56201ab10962bAnders Carlsson 4963180f47959a066795cc0f409433023af448bb0328Richard Smith CharUnits ElementSize; 496474e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith if (!HandleSizeof(Info, E->getExprLoc(), ElementType, ElementSize)) 4965180f47959a066795cc0f409433023af448bb0328Richard Smith return false; 4966a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman 496715efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith // FIXME: LLVM and GCC both compute LHSOffset - RHSOffset at runtime, 496815efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith // and produce incorrect results when it overflows. Such behavior 496915efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith // appears to be non-conforming, but is common, so perhaps we should 497015efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith // assume the standard intended for such cases to be undefined behavior 497115efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith // and check for them. 497215efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith 497315efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith // Compute (LHSOffset - RHSOffset) / Size carefully, checking for 497415efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith // overflow in the final conversion to ptrdiff_t. 497515efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith APSInt LHS( 497615efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith llvm::APInt(65, (int64_t)LHSOffset.getQuantity(), true), false); 497715efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith APSInt RHS( 497815efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith llvm::APInt(65, (int64_t)RHSOffset.getQuantity(), true), false); 497915efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith APSInt ElemSize( 498015efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith llvm::APInt(65, (int64_t)ElementSize.getQuantity(), true), false); 498115efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith APSInt TrueResult = (LHS - RHS) / ElemSize; 498215efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith APSInt Result = TrueResult.trunc(Info.Ctx.getIntWidth(E->getType())); 498315efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith 498415efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith if (Result.extend(65) != TrueResult) 498515efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith HandleOverflow(Info, E, TrueResult, E->getType()); 498615efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith return Success(Result, E); 4987ad02d7debd03ff275ac8ea27891a4ecccdb78068Eli Friedman } 4988625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith 498982f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith // C++11 [expr.rel]p3: 499082f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith // Pointers to void (after pointer conversions) can be compared, with a 499182f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith // result defined as follows: If both pointers represent the same 499282f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith // address or are both the null pointer value, the result is true if the 499382f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith // operator is <= or >= and false otherwise; otherwise the result is 499482f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith // unspecified. 499582f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith // We interpret this as applying to pointers to *cv* void. 499682f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith if (LHSTy->isVoidPointerType() && LHSOffset != RHSOffset && 4997f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith E->isRelationalOp()) 499882f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith CCEDiag(E, diag::note_constexpr_void_comparison); 499982f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith 5000f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // C++11 [expr.rel]p2: 5001f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // - If two pointers point to non-static data members of the same object, 5002f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // or to subobjects or array elements fo such members, recursively, the 5003f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // pointer to the later declared member compares greater provided the 5004f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // two members have the same access control and provided their class is 5005f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // not a union. 5006f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // [...] 5007f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // - Otherwise pointer comparisons are unspecified. 5008f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && 5009f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith E->isRelationalOp()) { 5010f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith bool WasArrayIndex; 5011f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith unsigned Mismatch = 5012f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith FindDesignatorMismatch(getType(LHSValue.Base), LHSDesignator, 5013f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith RHSDesignator, WasArrayIndex); 5014f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // At the point where the designators diverge, the comparison has a 5015f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // specified value if: 5016f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // - we are comparing array indices 5017f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // - we are comparing fields of a union, or fields with the same access 5018f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // Otherwise, the result is unspecified and thus the comparison is not a 5019f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // constant expression. 5020f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (!WasArrayIndex && Mismatch < LHSDesignator.Entries.size() && 5021f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith Mismatch < RHSDesignator.Entries.size()) { 5022f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith const FieldDecl *LF = getAsField(LHSDesignator.Entries[Mismatch]); 5023f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith const FieldDecl *RF = getAsField(RHSDesignator.Entries[Mismatch]); 5024f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (!LF && !RF) 5025f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith CCEDiag(E, diag::note_constexpr_pointer_comparison_base_classes); 5026f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith else if (!LF) 5027f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) 5028f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith << getAsBaseClass(LHSDesignator.Entries[Mismatch]) 5029f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith << RF->getParent() << RF; 5030f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith else if (!RF) 5031f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) 5032f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith << getAsBaseClass(RHSDesignator.Entries[Mismatch]) 5033f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith << LF->getParent() << LF; 5034f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith else if (!LF->getParent()->isUnion() && 5035f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith LF->getAccess() != RF->getAccess()) 5036f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith CCEDiag(E, diag::note_constexpr_pointer_comparison_differing_access) 5037f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith << LF << LF->getAccess() << RF << RF->getAccess() 5038f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith << LF->getParent(); 5039f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith } 5040f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith } 5041f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 5042a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman // The comparison here must be unsigned, and performed with the same 5043a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman // width as the pointer. 5044a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman unsigned PtrSize = Info.Ctx.getTypeSize(LHSTy); 5045a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman uint64_t CompareLHS = LHSOffset.getQuantity(); 5046a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman uint64_t CompareRHS = RHSOffset.getQuantity(); 5047a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman assert(PtrSize <= 64 && "Unexpected pointer width"); 5048a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman uint64_t Mask = ~0ULL >> (64 - PtrSize); 5049a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman CompareLHS &= Mask; 5050a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman CompareRHS &= Mask; 5051a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman 50522850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman // If there is a base and this is a relational operator, we can only 50532850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman // compare pointers within the object in question; otherwise, the result 50542850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman // depends on where the object is located in memory. 50552850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman if (!LHSValue.Base.isNull() && E->isRelationalOp()) { 50562850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman QualType BaseTy = getType(LHSValue.Base); 50572850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman if (BaseTy->isIncompleteType()) 50582850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman return Error(E); 50592850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman CharUnits Size = Info.Ctx.getTypeSizeInChars(BaseTy); 50602850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman uint64_t OffsetLimit = Size.getQuantity(); 50612850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman if (CompareLHS > OffsetLimit || CompareRHS > OffsetLimit) 50622850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman return Error(E); 50632850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman } 50642850376184b7e7aa81b5034ba44b001f8c55e07aEli Friedman 5065625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith switch (E->getOpcode()) { 5066625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith default: llvm_unreachable("missing comparison operator"); 5067a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman case BO_LT: return Success(CompareLHS < CompareRHS, E); 5068a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman case BO_GT: return Success(CompareLHS > CompareRHS, E); 5069a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman case BO_LE: return Success(CompareLHS <= CompareRHS, E); 5070a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman case BO_GE: return Success(CompareLHS >= CompareRHS, E); 5071a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman case BO_EQ: return Success(CompareLHS == CompareRHS, E); 5072a31698842e9893559d58a7bf1a987f2ae90bea0bEli Friedman case BO_NE: return Success(CompareLHS != CompareRHS, E); 5073ad02d7debd03ff275ac8ea27891a4ecccdb78068Eli Friedman } 50743068d117951a8df54bae9db039b56201ab10962bAnders Carlsson } 50753068d117951a8df54bae9db039b56201ab10962bAnders Carlsson } 5076b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith 5077b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith if (LHSTy->isMemberPointerType()) { 5078b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith assert(E->isEqualityOp() && "unexpected member pointer operation"); 5079b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith assert(RHSTy->isMemberPointerType() && "invalid comparison"); 5080b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith 5081b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith MemberPtr LHSValue, RHSValue; 5082b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith 5083b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith bool LHSOK = EvaluateMemberPointer(E->getLHS(), LHSValue, Info); 5084b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith if (!LHSOK && Info.keepEvaluatingAfterFailure()) 5085b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith return false; 5086b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith 5087b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith if (!EvaluateMemberPointer(E->getRHS(), RHSValue, Info) || !LHSOK) 5088b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith return false; 5089b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith 5090b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith // C++11 [expr.eq]p2: 5091b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith // If both operands are null, they compare equal. Otherwise if only one is 5092b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith // null, they compare unequal. 5093b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith if (!LHSValue.getDecl() || !RHSValue.getDecl()) { 5094b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith bool Equal = !LHSValue.getDecl() && !RHSValue.getDecl(); 5095b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith return Success(E->getOpcode() == BO_EQ ? Equal : !Equal, E); 5096b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith } 5097b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith 5098b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith // Otherwise if either is a pointer to a virtual member function, the 5099b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith // result is unspecified. 5100b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(LHSValue.getDecl())) 5101b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith if (MD->isVirtual()) 5102b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; 5103b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(RHSValue.getDecl())) 5104b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith if (MD->isVirtual()) 5105b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; 5106b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith 5107b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith // Otherwise they compare equal if and only if they would refer to the 5108b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith // same member of the same most derived object or the same subobject if 5109b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith // they were dereferenced with a hypothetical object of the associated 5110b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith // class type. 5111b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith bool Equal = LHSValue == RHSValue; 5112b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith return Success(E->getOpcode() == BO_EQ ? Equal : !Equal, E); 5113b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith } 5114b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith 511526f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith if (LHSTy->isNullPtrType()) { 511626f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith assert(E->isComparisonOp() && "unexpected nullptr operation"); 511726f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith assert(RHSTy->isNullPtrType() && "missing pointer conversion"); 511826f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith // C++11 [expr.rel]p4, [expr.eq]p3: If two operands of type std::nullptr_t 511926f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith // are compared, the result is true of the operator is <=, >= or ==, and 512026f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith // false otherwise. 512126f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith BinaryOperator::Opcode Opcode = E->getOpcode(); 512226f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith return Success(Opcode == BO_EQ || Opcode == BO_LE || Opcode == BO_GE, E); 512326f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith } 512426f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith 5125cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis assert((!LHSTy->isIntegralOrEnumerationType() || 5126cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis !RHSTy->isIntegralOrEnumerationType()) && 5127cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis "DataRecursiveIntBinOpEvaluator should have handled integral types"); 5128cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis // We can't continue from here for non-integral types. 5129cc2f77a7dbc5fb58fe188d55fbfb074e80fe5663Argyrios Kyrtzidis return ExprEvaluatorBaseTy::VisitBinaryOperator(E); 5130a25ae3d68d84d2b89907f998df6a396549589da5Anders Carlsson} 5131a25ae3d68d84d2b89907f998df6a396549589da5Anders Carlsson 51328b752f10c394b140f9ef89e049cbad1a7676fc25Ken DyckCharUnits IntExprEvaluator::GetAlignOfType(QualType T) { 51335d484e8cf710207010720589d89602233de61d01Sebastian Redl // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the 51345d484e8cf710207010720589d89602233de61d01Sebastian Redl // result shall be the alignment of the referenced type." 51355d484e8cf710207010720589d89602233de61d01Sebastian Redl if (const ReferenceType *Ref = T->getAs<ReferenceType>()) 51365d484e8cf710207010720589d89602233de61d01Sebastian Redl T = Ref->getPointeeType(); 51379f1210c3280104417a4ad30f0a00825ac8fa718aChad Rosier 51389f1210c3280104417a4ad30f0a00825ac8fa718aChad Rosier // __alignof is defined to return the preferred alignment. 51399f1210c3280104417a4ad30f0a00825ac8fa718aChad Rosier return Info.Ctx.toCharUnitsFromBits( 51409f1210c3280104417a4ad30f0a00825ac8fa718aChad Rosier Info.Ctx.getPreferredTypeAlign(T.getTypePtr())); 5141e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner} 5142e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner 51438b752f10c394b140f9ef89e049cbad1a7676fc25Ken DyckCharUnits IntExprEvaluator::GetAlignOfExpr(const Expr *E) { 5144af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner E = E->IgnoreParens(); 5145af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner 5146af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner // alignof decl is always accepted, even if it doesn't make sense: we default 51471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // to 1 in those cases. 5148af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) 51498b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck return Info.Ctx.getDeclAlign(DRE->getDecl(), 51508b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck /*RefAsPointee*/true); 5151a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman 5152af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) 51538b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck return Info.Ctx.getDeclAlign(ME->getMemberDecl(), 51548b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck /*RefAsPointee*/true); 5155af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner 5156e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner return GetAlignOfType(E->getType()); 5157e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner} 5158e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner 5159e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner 5160f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne/// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with 5161f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne/// a result as the expression's type. 5162f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbournebool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr( 5163f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne const UnaryExprOrTypeTraitExpr *E) { 5164f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne switch(E->getKind()) { 5165f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne case UETT_AlignOf: { 5166e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner if (E->isArgumentType()) 51674f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck return Success(GetAlignOfType(E->getArgumentType()), E); 5168e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner else 51694f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck return Success(GetAlignOfExpr(E->getArgumentExpr()), E); 5170e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner } 5171a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman 5172f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne case UETT_VecStep: { 5173f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne QualType Ty = E->getTypeOfArgument(); 51740518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl 5175f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne if (Ty->isVectorType()) { 5176f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne unsigned n = Ty->getAs<VectorType>()->getNumElements(); 5177a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman 5178f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne // The vec_step built-in functions that take a 3-component 5179f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne // vector return 4. (OpenCL 1.1 spec 6.11.12) 5180f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne if (n == 3) 5181f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne n = 4; 5182f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne 5183f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne return Success(n, E); 5184f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne } else 5185f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne return Success(1, E); 5186f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne } 5187f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne 5188f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne case UETT_SizeOf: { 5189f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne QualType SrcTy = E->getTypeOfArgument(); 5190f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, 5191f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne // the result is the size of the referenced type." 5192f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>()) 5193f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne SrcTy = Ref->getPointeeType(); 5194f2da9dfef96dc11b7b5effb1d02cb427b2d71599Eli Friedman 5195180f47959a066795cc0f409433023af448bb0328Richard Smith CharUnits Sizeof; 519674e1ad93fa8d6347549bcb10279fdf1fbc775321Richard Smith if (!HandleSizeof(Info, E->getExprLoc(), SrcTy, Sizeof)) 5197f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne return false; 5198180f47959a066795cc0f409433023af448bb0328Richard Smith return Success(Sizeof, E); 5199f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne } 5200f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne } 5201f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne 5202f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne llvm_unreachable("unknown expr/type trait"); 5203fcee0019b76f9f368f2b3d6d4048a98232593f29Chris Lattner} 5204fcee0019b76f9f368f2b3d6d4048a98232593f29Chris Lattner 52058cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) { 52068ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor CharUnits Result; 52078cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne unsigned n = OOE->getNumComponents(); 52088ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor if (n == 0) 5209f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(OOE); 52108cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne QualType CurrentType = OOE->getTypeSourceInfo()->getType(); 52118ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor for (unsigned i = 0; i != n; ++i) { 52128ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor OffsetOfExpr::OffsetOfNode ON = OOE->getComponent(i); 52138ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor switch (ON.getKind()) { 52148ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor case OffsetOfExpr::OffsetOfNode::Array: { 52158cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne const Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex()); 52168ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor APSInt IdxResult; 52178ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor if (!EvaluateInteger(Idx, IdxResult, Info)) 52188ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor return false; 52198ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType); 52208ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor if (!AT) 5221f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(OOE); 52228ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor CurrentType = AT->getElementType(); 52238ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType); 52248ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor Result += IdxResult.getSExtValue() * ElementSize; 52258ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor break; 52268ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor } 5227f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 52288ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor case OffsetOfExpr::OffsetOfNode::Field: { 52298ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor FieldDecl *MemberDecl = ON.getField(); 52308ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor const RecordType *RT = CurrentType->getAs<RecordType>(); 5231f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!RT) 5232f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(OOE); 52338ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor RecordDecl *RD = RT->getDecl(); 52348d59deec807ed53efcd07855199cdc9c979f447fJohn McCall if (RD->isInvalidDecl()) return false; 52358ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); 5236ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall unsigned i = MemberDecl->getFieldIndex(); 5237cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor assert(i < RL.getFieldCount() && "offsetof field in wrong type"); 5238fb1e3bc29b667f4275e1d5a43d64ec173f4f9a7dKen Dyck Result += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i)); 52398ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor CurrentType = MemberDecl->getType().getNonReferenceType(); 52408ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor break; 52418ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor } 5242f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 52438ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor case OffsetOfExpr::OffsetOfNode::Identifier: 52448ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor llvm_unreachable("dependent __builtin_offsetof"); 5245f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 5246cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor case OffsetOfExpr::OffsetOfNode::Base: { 5247cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor CXXBaseSpecifier *BaseSpec = ON.getBase(); 5248cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor if (BaseSpec->isVirtual()) 5249f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(OOE); 5250cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor 5251cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor // Find the layout of the class whose base we are looking into. 5252cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor const RecordType *RT = CurrentType->getAs<RecordType>(); 5253f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!RT) 5254f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(OOE); 5255cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor RecordDecl *RD = RT->getDecl(); 52568d59deec807ed53efcd07855199cdc9c979f447fJohn McCall if (RD->isInvalidDecl()) return false; 5257cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); 5258cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor 5259cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor // Find the base class itself. 5260cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor CurrentType = BaseSpec->getType(); 5261cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor const RecordType *BaseRT = CurrentType->getAs<RecordType>(); 5262cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor if (!BaseRT) 5263f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(OOE); 5264cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor 5265cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor // Add the offset to the base. 52667c7f820d70c925b29290a8563b59615816a827fcKen Dyck Result += RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl())); 5267cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor break; 5268cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor } 52698ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor } 52708ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor } 52718cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return Success(Result, OOE); 52728ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor} 52738ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor 5274b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattnerbool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { 527575a4881047deeb3a300ff9293dc6ba8570048bb5Chris Lattner switch (E->getOpcode()) { 52764c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner default: 527775a4881047deeb3a300ff9293dc6ba8570048bb5Chris Lattner // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. 527875a4881047deeb3a300ff9293dc6ba8570048bb5Chris Lattner // See C99 6.6p3. 5279f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 52802de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_Extension: 52814c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner // FIXME: Should extension allow i-c-e extension expressions in its scope? 52824c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner // If so, we could clear the diagnostic ID. 5283f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Visit(E->getSubExpr()); 52842de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_Plus: 5285c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // The result is just the value. 5286f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Visit(E->getSubExpr()); 5287f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith case UO_Minus: { 5288f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!Visit(E->getSubExpr())) 5289f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 5290f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!Result.isInt()) return Error(E); 5291789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith const APSInt &Value = Result.getInt(); 5292789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith if (Value.isSigned() && Value.isMinSignedValue()) 5293789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith HandleOverflow(Info, E, -Value.extend(Value.getBitWidth() + 1), 5294789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith E->getType()); 5295789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith return Success(-Value, E); 5296f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 5297f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith case UO_Not: { 5298f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!Visit(E->getSubExpr())) 5299f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 5300f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!Result.isInt()) return Error(E); 5301f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Success(~Result.getInt(), E); 5302f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 5303f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith case UO_LNot: { 5304f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith bool bres; 5305f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!EvaluateAsBooleanCondition(E->getSubExpr(), bres, Info)) 5306f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 5307f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Success(!bres, E); 5308f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 530906a3675627e3b3c47b49c689c8e404a33144194aAnders Carlsson } 5310a25ae3d68d84d2b89907f998df6a396549589da5Anders Carlsson} 53111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5312732b2236ae4a4f11e7642677cebbd169c07ea877Chris Lattner/// HandleCast - This is used to evaluate implicit or explicit casts where the 5313732b2236ae4a4f11e7642677cebbd169c07ea877Chris Lattner/// result type is integer. 53148cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool IntExprEvaluator::VisitCastExpr(const CastExpr *E) { 53158cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne const Expr *SubExpr = E->getSubExpr(); 531682206e267ce6cc709797127616f64672d255b310Anders Carlsson QualType DestType = E->getType(); 5317b92dac8bc2f6f73919825f9af693a8a7e89ae1d4Daniel Dunbar QualType SrcType = SubExpr->getType(); 531882206e267ce6cc709797127616f64672d255b310Anders Carlsson 531946a523285928aa07bf14803178dc04616ac85994Eli Friedman switch (E->getCastKind()) { 532046a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_BaseToDerived: 532146a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_DerivedToBase: 532246a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_UncheckedDerivedToBase: 532346a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_Dynamic: 532446a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_ToUnion: 532546a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_ArrayToPointerDecay: 532646a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_FunctionToPointerDecay: 532746a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_NullToPointer: 532846a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_NullToMemberPointer: 532946a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_BaseToDerivedMemberPointer: 533046a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_DerivedToBaseMemberPointer: 53314d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall case CK_ReinterpretMemberPointer: 533246a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_ConstructorConversion: 533346a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_IntegralToPointer: 533446a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_ToVoid: 533546a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_VectorSplat: 533646a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_IntegralToFloating: 533746a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_FloatingCast: 53381d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall case CK_CPointerToObjCPointerCast: 53391d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall case CK_BlockPointerToObjCPointerCast: 534046a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_AnyPointerToBlockPointerCast: 534146a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_ObjCObjectLValueCast: 534246a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_FloatingRealToComplex: 534346a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_FloatingComplexToReal: 534446a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_FloatingComplexCast: 534546a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_FloatingComplexToIntegralComplex: 534646a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_IntegralRealToComplex: 534746a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_IntegralComplexCast: 534846a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_IntegralComplexToFloatingComplex: 534946a523285928aa07bf14803178dc04616ac85994Eli Friedman llvm_unreachable("invalid cast kind for integral value"); 535046a523285928aa07bf14803178dc04616ac85994Eli Friedman 5351e50c297f92914ca996deb8b597624193273b62e4Eli Friedman case CK_BitCast: 535246a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_Dependent: 535346a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_LValueBitCast: 535433e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCProduceObject: 535533e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCConsumeObject: 535633e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCReclaimReturnedObject: 535733e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCExtendBlockObject: 5358ac1303eca6cbe3e623fb5ec6fe7ec184ef4b0dfaDouglas Gregor case CK_CopyAndAutoreleaseBlockObject: 5359f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 536046a523285928aa07bf14803178dc04616ac85994Eli Friedman 53617d580a4e9e47dffc3c17aa2b957ac57ca3c4e451Richard Smith case CK_UserDefinedConversion: 536246a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_LValueToRValue: 53637a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall case CK_AtomicToNonAtomic: 53647a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall case CK_NonAtomicToAtomic: 536546a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_NoOp: 5366c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return ExprEvaluatorBaseTy::VisitCastExpr(E); 536746a523285928aa07bf14803178dc04616ac85994Eli Friedman 536846a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_MemberPointerToBoolean: 536946a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_PointerToBoolean: 537046a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_IntegralToBoolean: 537146a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_FloatingToBoolean: 537246a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_FloatingComplexToBoolean: 537346a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_IntegralComplexToBoolean: { 53744efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman bool BoolResult; 5375c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith if (!EvaluateAsBooleanCondition(SubExpr, BoolResult, Info)) 53764efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman return false; 5377131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success(BoolResult, E); 53784efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman } 53794efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 538046a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_IntegralCast: { 5381732b2236ae4a4f11e7642677cebbd169c07ea877Chris Lattner if (!Visit(SubExpr)) 5382b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattner return false; 5383a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar 5384be26570e3faa009bdcefedfaf04473e518940520Eli Friedman if (!Result.isInt()) { 538565639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman // Allow casts of address-of-label differences if they are no-ops 538665639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman // or narrowing. (The narrowing case isn't actually guaranteed to 538765639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman // be constant-evaluatable except in some narrow cases which are hard 538865639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman // to detect here. We let it through on the assumption the user knows 538965639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman // what they are doing.) 539065639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman if (Result.isAddrLabelDiff()) 539165639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman return Info.Ctx.getTypeSize(DestType) <= Info.Ctx.getTypeSize(SrcType); 5392be26570e3faa009bdcefedfaf04473e518940520Eli Friedman // Only allow casts of lvalues if they are lossless. 5393be26570e3faa009bdcefedfaf04473e518940520Eli Friedman return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType); 5394be26570e3faa009bdcefedfaf04473e518940520Eli Friedman } 539530c37f4d2ee5811e85f692c22fb67d74ddc88079Daniel Dunbar 5396f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith return Success(HandleIntToIntCast(Info, E, DestType, SrcType, 5397f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith Result.getInt()), E); 5398732b2236ae4a4f11e7642677cebbd169c07ea877Chris Lattner } 53991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 540046a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_PointerToIntegral: { 5401c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; 5402c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith 5403efdb83e26f9a1fd2566afe54461216cd84814d42John McCall LValue LV; 540487eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner if (!EvaluatePointer(SubExpr, LV, Info)) 5405b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattner return false; 54064efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 5407dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar if (LV.getLValueBase()) { 5408dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar // Only allow based lvalue casts if they are lossless. 5409f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith // FIXME: Allow a larger integer size than the pointer size, and allow 5410f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith // narrowing back down to pointer width in subsequent integral casts. 5411f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith // FIXME: Check integer type's active bits, not its type size. 5412dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType)) 5413f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 5414dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar 5415b755a9da095d2f2f04444797f1e1a9511693815bRichard Smith LV.Designator.setInvalid(); 5416efdb83e26f9a1fd2566afe54461216cd84814d42John McCall LV.moveInto(Result); 5417dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar return true; 5418dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar } 54194efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 5420a73058324197b7bdfd19307965954f626e26199dKen Dyck APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(), 5421a73058324197b7bdfd19307965954f626e26199dKen Dyck SrcType); 5422f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith return Success(HandleIntToIntCast(Info, E, DestType, SrcType, AsInt), E); 54232bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson } 54244efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 542546a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_IntegralComplexToReal: { 5426f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall ComplexValue C; 54271725f683432715e5afe34d476024bd6f16eac3fcEli Friedman if (!EvaluateComplex(SubExpr, C, Info)) 54281725f683432715e5afe34d476024bd6f16eac3fcEli Friedman return false; 542946a523285928aa07bf14803178dc04616ac85994Eli Friedman return Success(C.getComplexIntReal(), E); 54301725f683432715e5afe34d476024bd6f16eac3fcEli Friedman } 54312217c87bdc5ab357046a5453bdb06f469c41024eEli Friedman 543246a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_FloatingToIntegral: { 543346a523285928aa07bf14803178dc04616ac85994Eli Friedman APFloat F(0.0); 543446a523285928aa07bf14803178dc04616ac85994Eli Friedman if (!EvaluateFloat(SubExpr, F, Info)) 543546a523285928aa07bf14803178dc04616ac85994Eli Friedman return false; 5436732b2236ae4a4f11e7642677cebbd169c07ea877Chris Lattner 5437c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith APSInt Value; 5438c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (!HandleFloatToIntCast(Info, E, SrcType, F, DestType, Value)) 5439c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return false; 5440c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return Success(Value, E); 544146a523285928aa07bf14803178dc04616ac85994Eli Friedman } 544246a523285928aa07bf14803178dc04616ac85994Eli Friedman } 54431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 544446a523285928aa07bf14803178dc04616ac85994Eli Friedman llvm_unreachable("unknown cast resulting in integral value"); 5445a25ae3d68d84d2b89907f998df6a396549589da5Anders Carlsson} 54462bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson 5447722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedmanbool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { 5448722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman if (E->getSubExpr()->getType()->isAnyComplexType()) { 5449f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall ComplexValue LV; 5450f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!EvaluateComplex(E->getSubExpr(), LV, Info)) 5451f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 5452f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!LV.isComplexInt()) 5453f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 5454722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman return Success(LV.getComplexIntReal(), E); 5455722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman } 5456722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman 5457722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman return Visit(E->getSubExpr()); 5458722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman} 5459722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman 5460664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedmanbool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { 5461722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman if (E->getSubExpr()->getType()->isComplexIntegerType()) { 5462f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall ComplexValue LV; 5463f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!EvaluateComplex(E->getSubExpr(), LV, Info)) 5464f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 5465f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!LV.isComplexInt()) 5466f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 5467722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman return Success(LV.getComplexIntImag(), E); 5468722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman } 5469722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman 54708327fad71da34492d82c532f42a58cb4baff81a3Richard Smith VisitIgnoredValue(E->getSubExpr()); 5471664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman return Success(0, E); 5472664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman} 5473664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman 5474ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregorbool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) { 5475ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor return Success(E->getPackLength(), E); 5476ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor} 5477ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor 5478295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redlbool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) { 5479295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl return Success(E->getValue(), E); 5480295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl} 5481295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl 5482f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===// 5483d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman// Float Evaluation 5484d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman//===----------------------------------------------------------------------===// 5485d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman 5486d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmannamespace { 5487770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass FloatExprEvaluator 54888cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne : public ExprEvaluatorBase<FloatExprEvaluator, bool> { 5489d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman APFloat &Result; 5490d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmanpublic: 5491d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman FloatExprEvaluator(EvalInfo &info, APFloat &result) 54928cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne : ExprEvaluatorBaseTy(info), Result(result) {} 5493d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman 54941aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith bool Success(const APValue &V, const Expr *e) { 54958cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne Result = V.getFloat(); 54968cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return true; 54978cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne } 5498d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman 549951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith bool ZeroInitialization(const Expr *E) { 5500f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType())); 5501f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith return true; 5502f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith } 5503f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith 5504019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner bool VisitCallExpr(const CallExpr *E); 5505d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman 55065db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar bool VisitUnaryOperator(const UnaryOperator *E); 5507d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman bool VisitBinaryOperator(const BinaryOperator *E); 5508d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman bool VisitFloatingLiteral(const FloatingLiteral *E); 55098cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitCastExpr(const CastExpr *E); 55102217c87bdc5ab357046a5453bdb06f469c41024eEli Friedman 5511abd3a857ace59100305790545d1baae5877b8945John McCall bool VisitUnaryReal(const UnaryOperator *E); 5512abd3a857ace59100305790545d1baae5877b8945John McCall bool VisitUnaryImag(const UnaryOperator *E); 5513ba98d6bb414861965a1f22628494ea046785ecd4Eli Friedman 551451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith // FIXME: Missing: array subscript of vector, member of vector 5515d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman}; 5516d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman} // end anonymous namespace 5517d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman 5518d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmanstatic bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { 5519c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith assert(E->isRValue() && E->getType()->isRealFloatingType()); 55208cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return FloatExprEvaluator(Info, Result).Visit(E); 5521d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman} 5522d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman 55234ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadstatic bool TryEvaluateBuiltinNaN(const ASTContext &Context, 5524db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall QualType ResultTy, 5525db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall const Expr *Arg, 5526db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall bool SNaN, 5527db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall llvm::APFloat &Result) { 5528db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts()); 5529db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall if (!S) return false; 5530db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall 5531db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy); 5532db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall 5533db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall llvm::APInt fill; 5534db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall 5535db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall // Treat empty strings as if they were zero. 5536db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall if (S->getString().empty()) 5537db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall fill = llvm::APInt(32, 0); 5538db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall else if (S->getString().getAsInteger(0, fill)) 5539db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall return false; 5540db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall 5541db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall if (SNaN) 5542db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall Result = llvm::APFloat::getSNaN(Sem, false, &fill); 5543db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall else 5544db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall Result = llvm::APFloat::getQNaN(Sem, false, &fill); 5545db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall return true; 5546db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall} 5547db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall 5548019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattnerbool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { 5549180f47959a066795cc0f409433023af448bb0328Richard Smith switch (E->isBuiltinCall()) { 55508cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne default: 55518cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return ExprEvaluatorBaseTy::VisitCallExpr(E); 55528cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 5553019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner case Builtin::BI__builtin_huge_val: 5554019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner case Builtin::BI__builtin_huge_valf: 5555019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner case Builtin::BI__builtin_huge_vall: 5556019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner case Builtin::BI__builtin_inf: 5557019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner case Builtin::BI__builtin_inff: 55587cbed03c00e246682e5292785d01e1c120ce54bdDaniel Dunbar case Builtin::BI__builtin_infl: { 55597cbed03c00e246682e5292785d01e1c120ce54bdDaniel Dunbar const llvm::fltSemantics &Sem = 55607cbed03c00e246682e5292785d01e1c120ce54bdDaniel Dunbar Info.Ctx.getFloatTypeSemantics(E->getType()); 556134a74ab81600a40c6324fd76adb724b803dfaf91Chris Lattner Result = llvm::APFloat::getInf(Sem); 556234a74ab81600a40c6324fd76adb724b803dfaf91Chris Lattner return true; 55637cbed03c00e246682e5292785d01e1c120ce54bdDaniel Dunbar } 55641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5565db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall case Builtin::BI__builtin_nans: 5566db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall case Builtin::BI__builtin_nansf: 5567db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall case Builtin::BI__builtin_nansl: 5568f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), 5569f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith true, Result)) 5570f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 5571f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return true; 5572db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall 55739e62171a25e3a08fb5c49fb370f83faf5ae786f5Chris Lattner case Builtin::BI__builtin_nan: 55749e62171a25e3a08fb5c49fb370f83faf5ae786f5Chris Lattner case Builtin::BI__builtin_nanf: 55759e62171a25e3a08fb5c49fb370f83faf5ae786f5Chris Lattner case Builtin::BI__builtin_nanl: 55764572baba9d18c275968ac113fd73b0e3c77cccb8Mike Stump // If this is __builtin_nan() turn this into a nan, otherwise we 55779e62171a25e3a08fb5c49fb370f83faf5ae786f5Chris Lattner // can't constant fold it. 5578f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), 5579f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith false, Result)) 5580f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 5581f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return true; 55825db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar 55835db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar case Builtin::BI__builtin_fabs: 55845db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar case Builtin::BI__builtin_fabsf: 55855db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar case Builtin::BI__builtin_fabsl: 55865db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar if (!EvaluateFloat(E->getArg(0), Result, Info)) 55875db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar return false; 55881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 55895db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar if (Result.isNegative()) 55905db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar Result.changeSign(); 55915db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar return true; 55925db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar 55931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump case Builtin::BI__builtin_copysign: 55941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump case Builtin::BI__builtin_copysignf: 55955db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar case Builtin::BI__builtin_copysignl: { 55965db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar APFloat RHS(0.); 55975db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar if (!EvaluateFloat(E->getArg(0), Result, Info) || 55985db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar !EvaluateFloat(E->getArg(1), RHS, Info)) 55995db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar return false; 56005db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar Result.copySign(RHS); 56015db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar return true; 56025db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar } 5603019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner } 5604019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner} 5605019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner 5606abd3a857ace59100305790545d1baae5877b8945John McCallbool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { 560743efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman if (E->getSubExpr()->getType()->isAnyComplexType()) { 560843efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman ComplexValue CV; 560943efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman if (!EvaluateComplex(E->getSubExpr(), CV, Info)) 561043efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman return false; 561143efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman Result = CV.FloatReal; 561243efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman return true; 561343efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman } 561443efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman 561543efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman return Visit(E->getSubExpr()); 5616abd3a857ace59100305790545d1baae5877b8945John McCall} 5617abd3a857ace59100305790545d1baae5877b8945John McCall 5618abd3a857ace59100305790545d1baae5877b8945John McCallbool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { 561943efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman if (E->getSubExpr()->getType()->isAnyComplexType()) { 562043efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman ComplexValue CV; 562143efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman if (!EvaluateComplex(E->getSubExpr(), CV, Info)) 562243efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman return false; 562343efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman Result = CV.FloatImag; 562443efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman return true; 562543efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman } 562643efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman 56278327fad71da34492d82c532f42a58cb4baff81a3Richard Smith VisitIgnoredValue(E->getSubExpr()); 562843efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(E->getType()); 562943efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman Result = llvm::APFloat::getZero(Sem); 5630abd3a857ace59100305790545d1baae5877b8945John McCall return true; 5631abd3a857ace59100305790545d1baae5877b8945John McCall} 5632abd3a857ace59100305790545d1baae5877b8945John McCall 56335db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbarbool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { 56345db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar switch (E->getOpcode()) { 5635f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith default: return Error(E); 56362de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_Plus: 56377993e8a2a26bf408c2a6d45f24fffa12db336b2bRichard Smith return EvaluateFloat(E->getSubExpr(), Result, Info); 56382de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_Minus: 56397993e8a2a26bf408c2a6d45f24fffa12db336b2bRichard Smith if (!EvaluateFloat(E->getSubExpr(), Result, Info)) 56407993e8a2a26bf408c2a6d45f24fffa12db336b2bRichard Smith return false; 56415db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar Result.changeSign(); 56425db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar return true; 56435db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar } 56445db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar} 5645019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner 5646d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmanbool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { 5647e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) 5648e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return ExprEvaluatorBaseTy::VisitBinaryOperator(E); 564996e93660124c8028a4c3bcc038ab0cdd18cd7ab2Anders Carlsson 56505db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar APFloat RHS(0.0); 5651745f5147e065900267c85a5568785a1991d4838fRichard Smith bool LHSOK = EvaluateFloat(E->getLHS(), Result, Info); 5652745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!LHSOK && !Info.keepEvaluatingAfterFailure()) 5653d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman return false; 5654745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!EvaluateFloat(E->getRHS(), RHS, Info) || !LHSOK) 5655d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman return false; 5656d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman 5657d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman switch (E->getOpcode()) { 5658f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith default: return Error(E); 56592de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Mul: 5660d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman Result.multiply(RHS, APFloat::rmNearestTiesToEven); 56617b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith break; 56622de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Add: 5663d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman Result.add(RHS, APFloat::rmNearestTiesToEven); 56647b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith break; 56652de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Sub: 5666d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman Result.subtract(RHS, APFloat::rmNearestTiesToEven); 56677b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith break; 56682de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Div: 5669d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman Result.divide(RHS, APFloat::rmNearestTiesToEven); 56707b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith break; 5671d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman } 56727b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith 56737b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith if (Result.isInfinity() || Result.isNaN()) 56747b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith CCEDiag(E, diag::note_constexpr_float_arithmetic) << Result.isNaN(); 56757b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith return true; 5676d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman} 5677d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman 5678d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmanbool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { 5679d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman Result = E->getValue(); 5680d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman return true; 5681d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman} 5682d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman 56838cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) { 56848cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne const Expr* SubExpr = E->getSubExpr(); 56851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 56862a523eec6a31955be876625819b89e8dc5def707Eli Friedman switch (E->getCastKind()) { 56872a523eec6a31955be876625819b89e8dc5def707Eli Friedman default: 5688c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return ExprEvaluatorBaseTy::VisitCastExpr(E); 56892a523eec6a31955be876625819b89e8dc5def707Eli Friedman 56902a523eec6a31955be876625819b89e8dc5def707Eli Friedman case CK_IntegralToFloating: { 56914efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman APSInt IntResult; 5692c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return EvaluateInteger(SubExpr, IntResult, Info) && 5693c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith HandleIntToFloatCast(Info, E, SubExpr->getType(), IntResult, 5694c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith E->getType(), Result); 56954efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman } 56962a523eec6a31955be876625819b89e8dc5def707Eli Friedman 56972a523eec6a31955be876625819b89e8dc5def707Eli Friedman case CK_FloatingCast: { 56984efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman if (!Visit(SubExpr)) 56994efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman return false; 5700c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return HandleFloatToFloatCast(Info, E, SubExpr->getType(), E->getType(), 5701c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Result); 57024efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman } 5703f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall 57042a523eec6a31955be876625819b89e8dc5def707Eli Friedman case CK_FloatingComplexToReal: { 5705f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall ComplexValue V; 5706f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall if (!EvaluateComplex(SubExpr, V, Info)) 5707f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall return false; 5708f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall Result = V.getComplexFloatReal(); 5709f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall return true; 5710f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall } 57112a523eec6a31955be876625819b89e8dc5def707Eli Friedman } 57124efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman} 57134efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 5714d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman//===----------------------------------------------------------------------===// 5715a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar// Complex Evaluation (for float and integer) 57169ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson//===----------------------------------------------------------------------===// 57179ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson 57189ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlssonnamespace { 5719770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass ComplexExprEvaluator 57208cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne : public ExprEvaluatorBase<ComplexExprEvaluator, bool> { 5721f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall ComplexValue &Result; 57221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 57239ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlssonpublic: 5724f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result) 57258cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne : ExprEvaluatorBaseTy(info), Result(Result) {} 57261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 57271aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith bool Success(const APValue &V, const Expr *e) { 57288cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne Result.setFrom(V); 57298cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return true; 57308cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne } 57311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 57327ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman bool ZeroInitialization(const Expr *E); 57337ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman 57348cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne //===--------------------------------------------------------------------===// 57358cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne // Visitor Methods 57368cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne //===--------------------------------------------------------------------===// 57379ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson 57388cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitImaginaryLiteral(const ImaginaryLiteral *E); 57398cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitCastExpr(const CastExpr *E); 5740b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman bool VisitBinaryOperator(const BinaryOperator *E); 574196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara bool VisitUnaryOperator(const UnaryOperator *E); 57427ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman bool VisitInitListExpr(const InitListExpr *E); 5743b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman}; 5744b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman} // end anonymous namespace 57451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5746b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedmanstatic bool EvaluateComplex(const Expr *E, ComplexValue &Result, 5747b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman EvalInfo &Info) { 5748c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith assert(E->isRValue() && E->getType()->isAnyComplexType()); 57498cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return ComplexExprEvaluator(Info, Result).Visit(E); 5750b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman} 5751b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman 57527ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedmanbool ComplexExprEvaluator::ZeroInitialization(const Expr *E) { 5753f6c17a439f3320ac620639a3ee66dbdabb93810cEli Friedman QualType ElemTy = E->getType()->getAs<ComplexType>()->getElementType(); 57547ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman if (ElemTy->isRealFloatingType()) { 57557ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman Result.makeComplexFloat(); 57567ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman APFloat Zero = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(ElemTy)); 57577ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman Result.FloatReal = Zero; 57587ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman Result.FloatImag = Zero; 57597ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman } else { 57607ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman Result.makeComplexInt(); 57617ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman APSInt Zero = Info.Ctx.MakeIntValue(0, ElemTy); 57627ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman Result.IntReal = Zero; 57637ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman Result.IntImag = Zero; 57647ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman } 57657ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman return true; 57667ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman} 57677ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman 57688cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) { 57698cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne const Expr* SubExpr = E->getSubExpr(); 5770b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman 5771b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman if (SubExpr->getType()->isRealFloatingType()) { 5772b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman Result.makeComplexFloat(); 5773b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman APFloat &Imag = Result.FloatImag; 5774b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman if (!EvaluateFloat(SubExpr, Imag, Info)) 5775b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman return false; 5776b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman 5777b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman Result.FloatReal = APFloat(Imag.getSemantics()); 5778b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman return true; 5779b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman } else { 5780b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman assert(SubExpr->getType()->isIntegerType() && 5781b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman "Unexpected imaginary literal."); 5782b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman 5783b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman Result.makeComplexInt(); 5784b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman APSInt &Imag = Result.IntImag; 5785b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman if (!EvaluateInteger(SubExpr, Imag, Info)) 5786b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman return false; 5787b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman 5788b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned()); 5789b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman return true; 5790b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman } 5791b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman} 5792b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman 57938cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) { 5794b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman 57958786da77984e81d48e0e1b2bd339809b1efc19f3John McCall switch (E->getCastKind()) { 57968786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_BitCast: 57978786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_BaseToDerived: 57988786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_DerivedToBase: 57998786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_UncheckedDerivedToBase: 58008786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_Dynamic: 58018786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_ToUnion: 58028786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_ArrayToPointerDecay: 58038786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_FunctionToPointerDecay: 58048786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_NullToPointer: 58058786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_NullToMemberPointer: 58068786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_BaseToDerivedMemberPointer: 58078786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_DerivedToBaseMemberPointer: 58088786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_MemberPointerToBoolean: 58094d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall case CK_ReinterpretMemberPointer: 58108786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_ConstructorConversion: 58118786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_IntegralToPointer: 58128786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_PointerToIntegral: 58138786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_PointerToBoolean: 58148786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_ToVoid: 58158786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_VectorSplat: 58168786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_IntegralCast: 58178786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_IntegralToBoolean: 58188786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_IntegralToFloating: 58198786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_FloatingToIntegral: 58208786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_FloatingToBoolean: 58218786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_FloatingCast: 58221d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall case CK_CPointerToObjCPointerCast: 58231d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall case CK_BlockPointerToObjCPointerCast: 58248786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_AnyPointerToBlockPointerCast: 58258786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_ObjCObjectLValueCast: 58268786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_FloatingComplexToReal: 58278786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_FloatingComplexToBoolean: 58288786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_IntegralComplexToReal: 58298786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_IntegralComplexToBoolean: 583033e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCProduceObject: 583133e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCConsumeObject: 583233e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCReclaimReturnedObject: 583333e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCExtendBlockObject: 5834ac1303eca6cbe3e623fb5ec6fe7ec184ef4b0dfaDouglas Gregor case CK_CopyAndAutoreleaseBlockObject: 58358786da77984e81d48e0e1b2bd339809b1efc19f3John McCall llvm_unreachable("invalid cast kind for complex value"); 58368786da77984e81d48e0e1b2bd339809b1efc19f3John McCall 58378786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_LValueToRValue: 58387a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall case CK_AtomicToNonAtomic: 58397a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall case CK_NonAtomicToAtomic: 58408786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_NoOp: 5841c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return ExprEvaluatorBaseTy::VisitCastExpr(E); 58422bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall 58438786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_Dependent: 584446a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_LValueBitCast: 58458786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_UserDefinedConversion: 5846f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 58478786da77984e81d48e0e1b2bd339809b1efc19f3John McCall 58488786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_FloatingRealToComplex: { 5849b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman APFloat &Real = Result.FloatReal; 58508786da77984e81d48e0e1b2bd339809b1efc19f3John McCall if (!EvaluateFloat(E->getSubExpr(), Real, Info)) 5851b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman return false; 5852b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman 58538786da77984e81d48e0e1b2bd339809b1efc19f3John McCall Result.makeComplexFloat(); 58548786da77984e81d48e0e1b2bd339809b1efc19f3John McCall Result.FloatImag = APFloat(Real.getSemantics()); 58558786da77984e81d48e0e1b2bd339809b1efc19f3John McCall return true; 58568786da77984e81d48e0e1b2bd339809b1efc19f3John McCall } 58578786da77984e81d48e0e1b2bd339809b1efc19f3John McCall 58588786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_FloatingComplexCast: { 58598786da77984e81d48e0e1b2bd339809b1efc19f3John McCall if (!Visit(E->getSubExpr())) 58608786da77984e81d48e0e1b2bd339809b1efc19f3John McCall return false; 58618786da77984e81d48e0e1b2bd339809b1efc19f3John McCall 58628786da77984e81d48e0e1b2bd339809b1efc19f3John McCall QualType To = E->getType()->getAs<ComplexType>()->getElementType(); 58638786da77984e81d48e0e1b2bd339809b1efc19f3John McCall QualType From 58648786da77984e81d48e0e1b2bd339809b1efc19f3John McCall = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); 58658786da77984e81d48e0e1b2bd339809b1efc19f3John McCall 5866c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return HandleFloatToFloatCast(Info, E, From, To, Result.FloatReal) && 5867c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith HandleFloatToFloatCast(Info, E, From, To, Result.FloatImag); 58688786da77984e81d48e0e1b2bd339809b1efc19f3John McCall } 58698786da77984e81d48e0e1b2bd339809b1efc19f3John McCall 58708786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_FloatingComplexToIntegralComplex: { 58718786da77984e81d48e0e1b2bd339809b1efc19f3John McCall if (!Visit(E->getSubExpr())) 58728786da77984e81d48e0e1b2bd339809b1efc19f3John McCall return false; 58738786da77984e81d48e0e1b2bd339809b1efc19f3John McCall 58748786da77984e81d48e0e1b2bd339809b1efc19f3John McCall QualType To = E->getType()->getAs<ComplexType>()->getElementType(); 58758786da77984e81d48e0e1b2bd339809b1efc19f3John McCall QualType From 58768786da77984e81d48e0e1b2bd339809b1efc19f3John McCall = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); 58778786da77984e81d48e0e1b2bd339809b1efc19f3John McCall Result.makeComplexInt(); 5878c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return HandleFloatToIntCast(Info, E, From, Result.FloatReal, 5879c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith To, Result.IntReal) && 5880c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith HandleFloatToIntCast(Info, E, From, Result.FloatImag, 5881c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith To, Result.IntImag); 58828786da77984e81d48e0e1b2bd339809b1efc19f3John McCall } 58838786da77984e81d48e0e1b2bd339809b1efc19f3John McCall 58848786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_IntegralRealToComplex: { 5885b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman APSInt &Real = Result.IntReal; 58868786da77984e81d48e0e1b2bd339809b1efc19f3John McCall if (!EvaluateInteger(E->getSubExpr(), Real, Info)) 5887b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman return false; 58889ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson 58898786da77984e81d48e0e1b2bd339809b1efc19f3John McCall Result.makeComplexInt(); 58908786da77984e81d48e0e1b2bd339809b1efc19f3John McCall Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned()); 58918786da77984e81d48e0e1b2bd339809b1efc19f3John McCall return true; 58928786da77984e81d48e0e1b2bd339809b1efc19f3John McCall } 58938786da77984e81d48e0e1b2bd339809b1efc19f3John McCall 58948786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_IntegralComplexCast: { 58958786da77984e81d48e0e1b2bd339809b1efc19f3John McCall if (!Visit(E->getSubExpr())) 5896b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman return false; 5897ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson 58988786da77984e81d48e0e1b2bd339809b1efc19f3John McCall QualType To = E->getType()->getAs<ComplexType>()->getElementType(); 58998786da77984e81d48e0e1b2bd339809b1efc19f3John McCall QualType From 59008786da77984e81d48e0e1b2bd339809b1efc19f3John McCall = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); 59011725f683432715e5afe34d476024bd6f16eac3fcEli Friedman 5902f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith Result.IntReal = HandleIntToIntCast(Info, E, To, From, Result.IntReal); 5903f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith Result.IntImag = HandleIntToIntCast(Info, E, To, From, Result.IntImag); 59048786da77984e81d48e0e1b2bd339809b1efc19f3John McCall return true; 59058786da77984e81d48e0e1b2bd339809b1efc19f3John McCall } 59068786da77984e81d48e0e1b2bd339809b1efc19f3John McCall 59078786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_IntegralComplexToFloatingComplex: { 59088786da77984e81d48e0e1b2bd339809b1efc19f3John McCall if (!Visit(E->getSubExpr())) 59098786da77984e81d48e0e1b2bd339809b1efc19f3John McCall return false; 59108786da77984e81d48e0e1b2bd339809b1efc19f3John McCall 59118786da77984e81d48e0e1b2bd339809b1efc19f3John McCall QualType To = E->getType()->getAs<ComplexType>()->getElementType(); 59128786da77984e81d48e0e1b2bd339809b1efc19f3John McCall QualType From 59138786da77984e81d48e0e1b2bd339809b1efc19f3John McCall = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); 59148786da77984e81d48e0e1b2bd339809b1efc19f3John McCall Result.makeComplexFloat(); 5915c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return HandleIntToFloatCast(Info, E, From, Result.IntReal, 5916c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith To, Result.FloatReal) && 5917c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith HandleIntToFloatCast(Info, E, From, Result.IntImag, 5918c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith To, Result.FloatImag); 59198786da77984e81d48e0e1b2bd339809b1efc19f3John McCall } 5920ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson } 59211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 59228786da77984e81d48e0e1b2bd339809b1efc19f3John McCall llvm_unreachable("unknown cast resulting in complex value"); 59239ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson} 59249ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson 5925f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCallbool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { 5926e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) 59272ad226bdc847df6b6b6e4f832856478ab63bb3dcRichard Smith return ExprEvaluatorBaseTy::VisitBinaryOperator(E); 59282ad226bdc847df6b6b6e4f832856478ab63bb3dcRichard Smith 5929745f5147e065900267c85a5568785a1991d4838fRichard Smith bool LHSOK = Visit(E->getLHS()); 5930745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!LHSOK && !Info.keepEvaluatingAfterFailure()) 5931f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall return false; 59321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5933f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall ComplexValue RHS; 5934745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK) 5935f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall return false; 5936a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar 59373f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar assert(Result.isComplexFloat() == RHS.isComplexFloat() && 59383f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar "Invalid operands to binary operator."); 5939ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson switch (E->getOpcode()) { 5940f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith default: return Error(E); 59412de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Add: 5942a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar if (Result.isComplexFloat()) { 5943a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar Result.getComplexFloatReal().add(RHS.getComplexFloatReal(), 5944a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar APFloat::rmNearestTiesToEven); 5945a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar Result.getComplexFloatImag().add(RHS.getComplexFloatImag(), 5946a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar APFloat::rmNearestTiesToEven); 5947a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar } else { 5948a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar Result.getComplexIntReal() += RHS.getComplexIntReal(); 5949a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar Result.getComplexIntImag() += RHS.getComplexIntImag(); 5950a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar } 59513f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar break; 59522de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Sub: 5953a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar if (Result.isComplexFloat()) { 5954a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(), 5955a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar APFloat::rmNearestTiesToEven); 5956a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(), 5957a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar APFloat::rmNearestTiesToEven); 5958a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar } else { 5959a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar Result.getComplexIntReal() -= RHS.getComplexIntReal(); 5960a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar Result.getComplexIntImag() -= RHS.getComplexIntImag(); 5961a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar } 59623f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar break; 59632de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Mul: 59643f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar if (Result.isComplexFloat()) { 5965f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall ComplexValue LHS = Result; 59663f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar APFloat &LHS_r = LHS.getComplexFloatReal(); 59673f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar APFloat &LHS_i = LHS.getComplexFloatImag(); 59683f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar APFloat &RHS_r = RHS.getComplexFloatReal(); 59693f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar APFloat &RHS_i = RHS.getComplexFloatImag(); 59701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 59713f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar APFloat Tmp = LHS_r; 59723f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); 59733f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar Result.getComplexFloatReal() = Tmp; 59743f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar Tmp = LHS_i; 59753f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); 59763f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven); 59773f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar 59783f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar Tmp = LHS_r; 59793f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); 59803f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar Result.getComplexFloatImag() = Tmp; 59813f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar Tmp = LHS_i; 59823f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); 59833f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven); 59843f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar } else { 5985f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall ComplexValue LHS = Result; 59861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Result.getComplexIntReal() = 59873f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar (LHS.getComplexIntReal() * RHS.getComplexIntReal() - 59883f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar LHS.getComplexIntImag() * RHS.getComplexIntImag()); 59891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Result.getComplexIntImag() = 59903f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar (LHS.getComplexIntReal() * RHS.getComplexIntImag() + 59913f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar LHS.getComplexIntImag() * RHS.getComplexIntReal()); 59923f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar } 59933f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar break; 599496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara case BO_Div: 599596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara if (Result.isComplexFloat()) { 599696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara ComplexValue LHS = Result; 599796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara APFloat &LHS_r = LHS.getComplexFloatReal(); 599896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara APFloat &LHS_i = LHS.getComplexFloatImag(); 599996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara APFloat &RHS_r = RHS.getComplexFloatReal(); 600096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara APFloat &RHS_i = RHS.getComplexFloatImag(); 600196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara APFloat &Res_r = Result.getComplexFloatReal(); 600296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara APFloat &Res_i = Result.getComplexFloatImag(); 600396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara 600496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara APFloat Den = RHS_r; 600596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Den.multiply(RHS_r, APFloat::rmNearestTiesToEven); 600696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara APFloat Tmp = RHS_i; 600796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); 600896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Den.add(Tmp, APFloat::rmNearestTiesToEven); 600996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara 601096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Res_r = LHS_r; 601196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Res_r.multiply(RHS_r, APFloat::rmNearestTiesToEven); 601296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Tmp = LHS_i; 601396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); 601496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Res_r.add(Tmp, APFloat::rmNearestTiesToEven); 601596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Res_r.divide(Den, APFloat::rmNearestTiesToEven); 601696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara 601796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Res_i = LHS_i; 601896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Res_i.multiply(RHS_r, APFloat::rmNearestTiesToEven); 601996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Tmp = LHS_r; 602096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); 602196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Res_i.subtract(Tmp, APFloat::rmNearestTiesToEven); 602296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Res_i.divide(Den, APFloat::rmNearestTiesToEven); 602396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara } else { 6024f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0) 6025f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E, diag::note_expr_divide_by_zero); 6026f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 602796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara ComplexValue LHS = Result; 602896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() + 602996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara RHS.getComplexIntImag() * RHS.getComplexIntImag(); 603096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Result.getComplexIntReal() = 603196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara (LHS.getComplexIntReal() * RHS.getComplexIntReal() + 603296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den; 603396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Result.getComplexIntImag() = 603496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara (LHS.getComplexIntImag() * RHS.getComplexIntReal() - 603596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den; 603696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara } 603796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara break; 6038ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson } 6039ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson 6040f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall return true; 6041ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson} 6042ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson 604396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnarabool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { 604496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara // Get the operand value into 'Result'. 604596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara if (!Visit(E->getSubExpr())) 604696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara return false; 604796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara 604896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara switch (E->getOpcode()) { 604996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara default: 6050f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 605196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara case UO_Extension: 605296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara return true; 605396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara case UO_Plus: 605496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara // The result is always just the subexpr. 605596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara return true; 605696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara case UO_Minus: 605796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara if (Result.isComplexFloat()) { 605896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Result.getComplexFloatReal().changeSign(); 605996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Result.getComplexFloatImag().changeSign(); 606096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara } 606196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara else { 606296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Result.getComplexIntReal() = -Result.getComplexIntReal(); 606396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Result.getComplexIntImag() = -Result.getComplexIntImag(); 606496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara } 606596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara return true; 606696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara case UO_Not: 606796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara if (Result.isComplexFloat()) 606896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Result.getComplexFloatImag().changeSign(); 606996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara else 607096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Result.getComplexIntImag() = -Result.getComplexIntImag(); 607196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara return true; 607296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara } 607396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara} 607496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara 60757ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedmanbool ComplexExprEvaluator::VisitInitListExpr(const InitListExpr *E) { 60767ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman if (E->getNumInits() == 2) { 60777ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman if (E->getType()->isComplexType()) { 60787ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman Result.makeComplexFloat(); 60797ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman if (!EvaluateFloat(E->getInit(0), Result.FloatReal, Info)) 60807ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman return false; 60817ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman if (!EvaluateFloat(E->getInit(1), Result.FloatImag, Info)) 60827ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman return false; 60837ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman } else { 60847ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman Result.makeComplexInt(); 60857ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman if (!EvaluateInteger(E->getInit(0), Result.IntReal, Info)) 60867ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman return false; 60877ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman if (!EvaluateInteger(E->getInit(1), Result.IntImag, Info)) 60887ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman return false; 60897ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman } 60907ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman return true; 60917ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman } 60927ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman return ExprEvaluatorBaseTy::VisitInitListExpr(E); 60937ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman} 60947ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman 60959ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson//===----------------------------------------------------------------------===// 6096aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith// Void expression evaluation, primarily for a cast to void on the LHS of a 6097aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith// comma operator 6098aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith//===----------------------------------------------------------------------===// 6099aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith 6100aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smithnamespace { 6101aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smithclass VoidExprEvaluator 6102aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith : public ExprEvaluatorBase<VoidExprEvaluator, bool> { 6103aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smithpublic: 6104aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith VoidExprEvaluator(EvalInfo &Info) : ExprEvaluatorBaseTy(Info) {} 6105aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith 61061aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith bool Success(const APValue &V, const Expr *e) { return true; } 6107aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith 6108aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith bool VisitCastExpr(const CastExpr *E) { 6109aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith switch (E->getCastKind()) { 6110aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith default: 6111aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith return ExprEvaluatorBaseTy::VisitCastExpr(E); 6112aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith case CK_ToVoid: 6113aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith VisitIgnoredValue(E->getSubExpr()); 6114aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith return true; 6115aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith } 6116aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith } 6117aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith}; 6118aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith} // end anonymous namespace 6119aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith 6120aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smithstatic bool EvaluateVoid(const Expr *E, EvalInfo &Info) { 6121aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith assert(E->isRValue() && E->getType()->isVoidType()); 6122aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith return VoidExprEvaluator(Info).Visit(E); 6123aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith} 6124aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith 6125aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith//===----------------------------------------------------------------------===// 612651f4708c00110940ca3f337961915f2ca1668375Richard Smith// Top level Expr::EvaluateAsRValue method. 6127f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===// 6128f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner 61291aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smithstatic bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E) { 6130c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // In C, function designators are not lvalues, but we evaluate them as if they 6131c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // are. 6132c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith if (E->isGLValue() || E->getType()->isFunctionType()) { 6133c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith LValue LV; 6134c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith if (!EvaluateLValue(E, LV, Info)) 6135c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return false; 6136c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith LV.moveInto(Result); 6137c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith } else if (E->getType()->isVectorType()) { 61381e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith if (!EvaluateVector(E, Result, Info)) 613959b5da6d853b4368b984700315adf7b37de05764Nate Begeman return false; 6140575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor } else if (E->getType()->isIntegralOrEnumerationType()) { 61411e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith if (!IntExprEvaluator(Info, Result).Visit(E)) 61426dde0d5dc09f45f4d9508c964703e36fef1a0198Anders Carlsson return false; 6143efdb83e26f9a1fd2566afe54461216cd84814d42John McCall } else if (E->getType()->hasPointerRepresentation()) { 6144efdb83e26f9a1fd2566afe54461216cd84814d42John McCall LValue LV; 6145efdb83e26f9a1fd2566afe54461216cd84814d42John McCall if (!EvaluatePointer(E, LV, Info)) 61466dde0d5dc09f45f4d9508c964703e36fef1a0198Anders Carlsson return false; 61471e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith LV.moveInto(Result); 6148efdb83e26f9a1fd2566afe54461216cd84814d42John McCall } else if (E->getType()->isRealFloatingType()) { 6149efdb83e26f9a1fd2566afe54461216cd84814d42John McCall llvm::APFloat F(0.0); 6150efdb83e26f9a1fd2566afe54461216cd84814d42John McCall if (!EvaluateFloat(E, F, Info)) 61516dde0d5dc09f45f4d9508c964703e36fef1a0198Anders Carlsson return false; 61521aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith Result = APValue(F); 6153efdb83e26f9a1fd2566afe54461216cd84814d42John McCall } else if (E->getType()->isAnyComplexType()) { 6154efdb83e26f9a1fd2566afe54461216cd84814d42John McCall ComplexValue C; 6155efdb83e26f9a1fd2566afe54461216cd84814d42John McCall if (!EvaluateComplex(E, C, Info)) 6156660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump return false; 61571e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith C.moveInto(Result); 615869c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith } else if (E->getType()->isMemberPointerType()) { 6159e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith MemberPtr P; 6160e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!EvaluateMemberPointer(E, P, Info)) 6161e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 6162e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith P.moveInto(Result); 6163e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 616451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith } else if (E->getType()->isArrayType()) { 6165180f47959a066795cc0f409433023af448bb0328Richard Smith LValue LV; 616683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith LV.set(E, Info.CurrentCall->Index); 6167180f47959a066795cc0f409433023af448bb0328Richard Smith if (!EvaluateArray(E, LV, Info.CurrentCall->Temporaries[E], Info)) 6168cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith return false; 6169180f47959a066795cc0f409433023af448bb0328Richard Smith Result = Info.CurrentCall->Temporaries[E]; 617051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith } else if (E->getType()->isRecordType()) { 6171180f47959a066795cc0f409433023af448bb0328Richard Smith LValue LV; 617283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith LV.set(E, Info.CurrentCall->Index); 6173180f47959a066795cc0f409433023af448bb0328Richard Smith if (!EvaluateRecord(E, LV, Info.CurrentCall->Temporaries[E], Info)) 6174180f47959a066795cc0f409433023af448bb0328Richard Smith return false; 6175180f47959a066795cc0f409433023af448bb0328Richard Smith Result = Info.CurrentCall->Temporaries[E]; 6176aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith } else if (E->getType()->isVoidType()) { 6177c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (Info.getLangOpts().CPlusPlus0x) 61785cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.CCEDiag(E, diag::note_constexpr_nonliteral) 6179c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith << E->getType(); 6180c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith else 61815cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); 6182aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith if (!EvaluateVoid(E, Info)) 6183aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith return false; 6184c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } else if (Info.getLangOpts().CPlusPlus0x) { 61855cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.Diag(E, diag::note_constexpr_nonliteral) << E->getType(); 6186c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return false; 6187f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } else { 61885cfc7d85fe13f144c9a8b264d6de9d38dfebc383Richard Smith Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); 6189660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump return false; 6190f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 6191660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump 6192660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump return true; 6193660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump} 6194660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump 619583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith/// EvaluateInPlace - Evaluate an expression in-place in an APValue. In some 619683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith/// cases, the in-place evaluation is essential, since later initializers for 619783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith/// an object can indirectly refer to subobjects which were initialized earlier. 619883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smithstatic bool EvaluateInPlace(APValue &Result, EvalInfo &Info, const LValue &This, 619983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith const Expr *E, CheckConstantExpressionKind CCEK, 620083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith bool AllowNonLiteralTypes) { 62017ca4850a3e3530fa6c93b64b740446e32c97f992Richard Smith if (!AllowNonLiteralTypes && !CheckLiteralType(Info, E)) 620251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return false; 620351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 620451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (E->isRValue()) { 620569c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith // Evaluate arrays and record types in-place, so that later initializers can 620669c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith // refer to earlier-initialized members of the object. 6207180f47959a066795cc0f409433023af448bb0328Richard Smith if (E->getType()->isArrayType()) 6208180f47959a066795cc0f409433023af448bb0328Richard Smith return EvaluateArray(E, This, Result, Info); 6209180f47959a066795cc0f409433023af448bb0328Richard Smith else if (E->getType()->isRecordType()) 6210180f47959a066795cc0f409433023af448bb0328Richard Smith return EvaluateRecord(E, This, Result, Info); 621169c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith } 621269c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith 621369c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith // For any other type, in-place evaluation is unimportant. 62141aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith return Evaluate(Result, Info, E); 621569c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith} 621669c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith 6217f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// EvaluateAsRValue - Try to evaluate this expression, performing an implicit 6218f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// lvalue-to-rvalue cast if it is an lvalue. 6219f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result) { 622051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (!CheckLiteralType(Info, E)) 622151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return false; 622251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 62231aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith if (!::Evaluate(Result, Info, E)) 6224f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 6225f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 6226f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (E->isGLValue()) { 6227f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith LValue LV; 62281aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith LV.setFrom(Info.Ctx, Result); 62291aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith if (!HandleLValueToRValueConversion(Info, E, E->getType(), LV, Result)) 6230f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 6231f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 6232f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 62331aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith // Check this core constant expression is a constant expression. 623483587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith return CheckConstantExpression(Info, E->getExprLoc(), E->getType(), Result); 6235f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith} 6236c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 623751f4708c00110940ca3f337961915f2ca1668375Richard Smith/// EvaluateAsRValue - Return true if this is a constant which we can fold using 623856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall/// any crazy technique (that has nothing to do with language standards) that 623956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall/// we want to. If this function returns true, it returns the folded constant 6240c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// in Result. If this expression is a glvalue, an lvalue-to-rvalue conversion 6241c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// will be applied to the result. 624251f4708c00110940ca3f337961915f2ca1668375Richard Smithbool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx) const { 6243ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith // Fast-path evaluations of integer literals, since we sometimes see files 6244ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith // containing vast quantities of these. 6245ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith if (const IntegerLiteral *L = dyn_cast<IntegerLiteral>(this)) { 6246ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith Result.Val = APValue(APSInt(L->getValue(), 6247ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith L->getType()->isUnsignedIntegerType())); 6248ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith return true; 6249ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith } 6250ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith 62512d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith // FIXME: Evaluating values of large array and record types can cause 62522d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith // performance problems. Only do so in C++11 for now. 6253e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (isRValue() && (getType()->isArrayType() || getType()->isRecordType()) && 62544e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie !Ctx.getLangOpts().CPlusPlus0x) 62551445bbacf4c8de5f208ff4ccb302424a4d9e233eRichard Smith return false; 62561445bbacf4c8de5f208ff4ccb302424a4d9e233eRichard Smith 6257f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith EvalInfo Info(Ctx, Result); 6258f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return ::EvaluateAsRValue(Info, this, Result.Val); 625956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall} 626056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall 62614ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool Expr::EvaluateAsBooleanCondition(bool &Result, 62624ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad const ASTContext &Ctx) const { 6263c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith EvalResult Scratch; 626451f4708c00110940ca3f337961915f2ca1668375Richard Smith return EvaluateAsRValue(Scratch, Ctx) && 62651aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith HandleConversionToBool(Scratch.Val, Result); 6266cd7a445c6b46c5585580dfb652300c8483c0cb6bJohn McCall} 6267cd7a445c6b46c5585580dfb652300c8483c0cb6bJohn McCall 626880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smithbool Expr::EvaluateAsInt(APSInt &Result, const ASTContext &Ctx, 626980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith SideEffectsKind AllowSideEffects) const { 627080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith if (!getType()->isIntegralOrEnumerationType()) 627180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith return false; 627280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith 6273c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith EvalResult ExprResult; 627480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith if (!EvaluateAsRValue(ExprResult, Ctx) || !ExprResult.Val.isInt() || 627580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith (!AllowSideEffects && ExprResult.HasSideEffects)) 6276c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return false; 6277f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 6278c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith Result = ExprResult.Val.getInt(); 6279c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return true; 6280a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith} 6281a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith 62824ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx) const { 62831b78276a75a5a0f496a82429c1ff9604d622a76dAnders Carlsson EvalInfo Info(Ctx, Result); 62841b78276a75a5a0f496a82429c1ff9604d622a76dAnders Carlsson 6285efdb83e26f9a1fd2566afe54461216cd84814d42John McCall LValue LV; 628683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith if (!EvaluateLValue(this, LV, Info) || Result.HasSideEffects || 628783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith !CheckLValueConstantExpression(Info, getExprLoc(), 628883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith Ctx.getLValueReferenceType(getType()), LV)) 628983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith return false; 629083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith 62911aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith LV.moveInto(Result.Val); 629283587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith return true; 6293b2f295c8050fb8c141bf2cf38eed0a56e99d0092Eli Friedman} 6294b2f295c8050fb8c141bf2cf38eed0a56e99d0092Eli Friedman 6295099e7f647ccda915513f2b2ec53352dc756082d3Richard Smithbool Expr::EvaluateAsInitializer(APValue &Value, const ASTContext &Ctx, 6296099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith const VarDecl *VD, 6297099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const { 62982d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith // FIXME: Evaluating initializers for large array and record types can cause 62992d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith // performance problems. Only do so in C++11 for now. 63002d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith if (isRValue() && (getType()->isArrayType() || getType()->isRecordType()) && 63014e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie !Ctx.getLangOpts().CPlusPlus0x) 63022d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith return false; 63032d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith 6304099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Expr::EvalStatus EStatus; 6305099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith EStatus.Diag = &Notes; 6306099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith 6307099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith EvalInfo InitInfo(Ctx, EStatus); 6308099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith InitInfo.setEvaluatingDecl(VD, Value); 6309099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith 6310099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith LValue LVal; 6311099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith LVal.set(VD); 6312099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith 631351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith // C++11 [basic.start.init]p2: 631451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith // Variables with static storage duration or thread storage duration shall be 631551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith // zero-initialized before any other initialization takes place. 631651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith // This behavior is not present in C. 63174e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (Ctx.getLangOpts().CPlusPlus && !VD->hasLocalStorage() && 631851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith !VD->getType()->isReferenceType()) { 631951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith ImplicitValueInitExpr VIE(VD->getType()); 632083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith if (!EvaluateInPlace(Value, InitInfo, LVal, &VIE, CCEK_Constant, 632183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith /*AllowNonLiteralTypes=*/true)) 632251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return false; 632351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith } 632451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 632583587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith if (!EvaluateInPlace(Value, InitInfo, LVal, this, CCEK_Constant, 632683587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith /*AllowNonLiteralTypes=*/true) || 632783587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith EStatus.HasSideEffects) 632883587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith return false; 632983587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith 633083587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith return CheckConstantExpression(InitInfo, VD->getLocation(), VD->getType(), 633183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith Value); 6332099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith} 6333099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith 633451f4708c00110940ca3f337961915f2ca1668375Richard Smith/// isEvaluatable - Call EvaluateAsRValue to see if this expression can be 633551f4708c00110940ca3f337961915f2ca1668375Richard Smith/// constant folded, but discard the result. 63364ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool Expr::isEvaluatable(const ASTContext &Ctx) const { 63374fdfb0965b396f2778091f7e6c051d17ff9791baAnders Carlsson EvalResult Result; 633851f4708c00110940ca3f337961915f2ca1668375Richard Smith return EvaluateAsRValue(Result, Ctx) && !Result.HasSideEffects; 633945b6b9d080ac56917337d73d8f1cd6374b27b05dChris Lattner} 634051fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson 6341a6b8b2c09610b8bc4330e948ece8b940c2386406Richard SmithAPSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx) const { 63421c0cfd4599e816cfd7a8f348286bf0ad79652ffcAnders Carlsson EvalResult EvalResult; 634351f4708c00110940ca3f337961915f2ca1668375Richard Smith bool Result = EvaluateAsRValue(EvalResult, Ctx); 6344c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin (void)Result; 634551fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson assert(Result && "Could not evaluate expression"); 63461c0cfd4599e816cfd7a8f348286bf0ad79652ffcAnders Carlsson assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer"); 634751fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson 63481c0cfd4599e816cfd7a8f348286bf0ad79652ffcAnders Carlsson return EvalResult.Val.getInt(); 634951fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson} 6350d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 6351e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara bool Expr::EvalResult::isGlobalLValue() const { 6352e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara assert(Val.isLValue()); 6353e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara return IsGlobalLValue(Val.getLValueBase()); 6354e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara } 6355e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara 6356e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara 6357d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// isIntegerConstantExpr - this recursive routine will test if an expression is 6358d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// an integer constant expression. 6359d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 6360d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, 6361d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// comma, etc 6362d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// 6363d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof 6364d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer 6365d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// cast+dereference. 6366d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 6367d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// CheckICE - This function does the fundamental ICE checking: the returned 6368d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation. 6369d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// Note that to reduce code duplication, this helper does no evaluation 6370d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// itself; the caller checks whether the expression is evaluatable, and 6371d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// in the rare cases where CheckICE actually cares about the evaluated 6372d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// value, it calls into Evalute. 6373d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// 6374d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// Meanings of Val: 637551f4708c00110940ca3f337961915f2ca1668375Richard Smith// 0: This expression is an ICE. 6376d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// 1: This expression is not an ICE, but if it isn't evaluated, it's 6377d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// a legal subexpression for an ICE. This return value is used to handle 6378d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// the comma operator in C99 mode. 6379d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// 2: This expression is not an ICE, and is not a legal subexpression for one. 6380d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 63813c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohmannamespace { 63823c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman 6383d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCallstruct ICEDiag { 6384d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall unsigned Val; 6385d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall SourceLocation Loc; 6386d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 6387d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall public: 6388d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {} 6389d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall ICEDiag() : Val(0) {} 6390d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall}; 6391d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 63923c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman} 63933c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman 63943c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohmanstatic ICEDiag NoDiag() { return ICEDiag(); } 6395d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 6396d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCallstatic ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) { 6397d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall Expr::EvalResult EVResult; 639851f4708c00110940ca3f337961915f2ca1668375Richard Smith if (!E->EvaluateAsRValue(EVResult, Ctx) || EVResult.HasSideEffects || 6399d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall !EVResult.Val.isInt()) { 6400d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return ICEDiag(2, E->getLocStart()); 6401d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6402d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return NoDiag(); 6403d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall} 6404d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 6405d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCallstatic ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) { 6406d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall assert(!E->isValueDependent() && "Should not see value dependent exprs!"); 64072ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor if (!E->getType()->isIntegralOrEnumerationType()) { 6408d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return ICEDiag(2, E->getLocStart()); 6409d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6410d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 6411d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall switch (E->getStmtClass()) { 641263c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall#define ABSTRACT_STMT(Node) 6413d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall#define STMT(Node, Base) case Expr::Node##Class: 6414d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall#define EXPR(Node, Base) 6415d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall#include "clang/AST/StmtNodes.inc" 6416d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::PredefinedExprClass: 6417d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::FloatingLiteralClass: 6418d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ImaginaryLiteralClass: 6419d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::StringLiteralClass: 6420d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ArraySubscriptExprClass: 6421d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::MemberExprClass: 6422d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CompoundAssignOperatorClass: 6423d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CompoundLiteralExprClass: 6424d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ExtVectorElementExprClass: 6425d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::DesignatedInitExprClass: 6426d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ImplicitValueInitExprClass: 6427d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ParenListExprClass: 6428d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::VAArgExprClass: 6429d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::AddrLabelExprClass: 6430d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::StmtExprClass: 6431d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXMemberCallExprClass: 6432e08ce650a2b02410eddd1f60a4aa6b3d4be71e73Peter Collingbourne case Expr::CUDAKernelCallExprClass: 6433d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXDynamicCastExprClass: 6434d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXTypeidExprClass: 64359be88403e965cc49af76c9d33d818781d44b333eFrancois Pichet case Expr::CXXUuidofExprClass: 6436d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXNullPtrLiteralExprClass: 64379fcce65e7e1307b5b8da9be13e4092d6bb94dc1dRichard Smith case Expr::UserDefinedLiteralClass: 6438d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXThisExprClass: 6439d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXThrowExprClass: 6440d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXNewExprClass: 6441d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXDeleteExprClass: 6442d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXPseudoDestructorExprClass: 6443d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::UnresolvedLookupExprClass: 6444d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::DependentScopeDeclRefExprClass: 6445d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXConstructExprClass: 6446d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXBindTemporaryExprClass: 64474765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall case Expr::ExprWithCleanupsClass: 6448d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXTemporaryObjectExprClass: 6449d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXUnresolvedConstructExprClass: 6450d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXDependentScopeMemberExprClass: 6451d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::UnresolvedMemberExprClass: 6452d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ObjCStringLiteralClass: 6453eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard case Expr::ObjCBoxedExprClass: 6454ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek case Expr::ObjCArrayLiteralClass: 6455ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek case Expr::ObjCDictionaryLiteralClass: 6456d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ObjCEncodeExprClass: 6457d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ObjCMessageExprClass: 6458d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ObjCSelectorExprClass: 6459d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ObjCProtocolExprClass: 6460d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ObjCIvarRefExprClass: 6461d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ObjCPropertyRefExprClass: 6462ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek case Expr::ObjCSubscriptRefExprClass: 6463d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ObjCIsaExprClass: 6464d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ShuffleVectorExprClass: 6465d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::BlockExprClass: 6466d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::NoStmtClass: 64677cd7d1ad33fdf49eef83942e8855fe20d95aa1b9John McCall case Expr::OpaqueValueExprClass: 6468be230c36e32142cbdcdbe9c97511d097beeecbabDouglas Gregor case Expr::PackExpansionExprClass: 6469c7793c73ba8a343de3f2552d984851985a46f159Douglas Gregor case Expr::SubstNonTypeTemplateParmPackExprClass: 647061eee0ca33b29e102f11bab77c8b74cc00e2392bTanya Lattner case Expr::AsTypeExprClass: 6471f85e193739c953358c865005855253af4f68a497John McCall case Expr::ObjCIndirectCopyRestoreExprClass: 647203e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor case Expr::MaterializeTemporaryExprClass: 64734b9c2d235fb9449e249d74f48ecfec601650de93John McCall case Expr::PseudoObjectExprClass: 6474276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman case Expr::AtomicExprClass: 6475cea8d966f826554f0679595e9371e314e8dbc1cfSebastian Redl case Expr::InitListExprClass: 647601d08018b7cf5ce1601707cfd7a84d22015fc04eDouglas Gregor case Expr::LambdaExprClass: 6477cea8d966f826554f0679595e9371e314e8dbc1cfSebastian Redl return ICEDiag(2, E->getLocStart()); 6478cea8d966f826554f0679595e9371e314e8dbc1cfSebastian Redl 6479ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor case Expr::SizeOfPackExprClass: 6480d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::GNUNullExprClass: 6481d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // GCC considers the GNU __null value to be an integral constant expression. 6482d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return NoDiag(); 6483d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 648491a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall case Expr::SubstNonTypeTemplateParmExprClass: 648591a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall return 648691a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall CheckICE(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(), Ctx); 648791a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall 6488d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ParenExprClass: 6489d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx); 6490f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne case Expr::GenericSelectionExprClass: 6491f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne return CheckICE(cast<GenericSelectionExpr>(E)->getResultExpr(), Ctx); 6492d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::IntegerLiteralClass: 6493d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CharacterLiteralClass: 6494ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek case Expr::ObjCBoolLiteralExprClass: 6495d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXBoolLiteralExprClass: 6496ed8abf18329df67b0abcbb3a10458bd8c1d2a595Douglas Gregor case Expr::CXXScalarValueInitExprClass: 6497d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::UnaryTypeTraitExprClass: 64986ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet case Expr::BinaryTypeTraitExprClass: 64994ca8ac2e61c37ddadf37024af86f3e1019af8532Douglas Gregor case Expr::TypeTraitExprClass: 650021ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley case Expr::ArrayTypeTraitExprClass: 6501552622067dc45013d240f73952fece703f5e63bdJohn Wiegley case Expr::ExpressionTraitExprClass: 65022e156225a29407a50dd19041aa5750171ad44ea3Sebastian Redl case Expr::CXXNoexceptExprClass: 6503d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return NoDiag(); 6504d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CallExprClass: 65056cf750298d3621d8a10a6dd07fcee8e274b9d94dSean Hunt case Expr::CXXOperatorCallExprClass: { 650605830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith // C99 6.6/3 allows function calls within unevaluated subexpressions of 650705830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith // constant expressions, but they can never be ICEs because an ICE cannot 650805830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith // contain an operand of (pointer to) function type. 6509d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall const CallExpr *CE = cast<CallExpr>(E); 6510180f47959a066795cc0f409433023af448bb0328Richard Smith if (CE->isBuiltinCall()) 6511d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return CheckEvalInICE(E, Ctx); 6512d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return ICEDiag(2, E->getLocStart()); 6513d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6514359c89df5479810c9d4784fc0b6ab592eb136777Richard Smith case Expr::DeclRefExprClass: { 6515d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl())) 6516d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return NoDiag(); 6517359c89df5479810c9d4784fc0b6ab592eb136777Richard Smith const ValueDecl *D = dyn_cast<ValueDecl>(cast<DeclRefExpr>(E)->getDecl()); 65184e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (Ctx.getLangOpts().CPlusPlus && 6519359c89df5479810c9d4784fc0b6ab592eb136777Richard Smith D && IsConstNonVolatile(D->getType())) { 6520d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // Parameter variables are never constants. Without this check, 6521d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // getAnyInitializer() can find a default argument, which leads 6522d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // to chaos. 6523d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (isa<ParmVarDecl>(D)) 6524d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); 6525d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 6526d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // C++ 7.1.5.1p2 6527d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // A variable of non-volatile const-qualified integral or enumeration 6528d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // type initialized by an ICE can be used in ICEs. 6529d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) { 6530db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith if (!Dcl->getType()->isIntegralOrEnumerationType()) 6531db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); 6532db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith 6533099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith const VarDecl *VD; 6534099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith // Look for a declaration of this variable that has an initializer, and 6535099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith // check whether it is an ICE. 6536099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith if (Dcl->getAnyInitializer(VD) && VD->checkInitIsICE()) 6537099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith return NoDiag(); 6538099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith else 6539099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); 6540d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6541d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6542d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return ICEDiag(2, E->getLocStart()); 6543359c89df5479810c9d4784fc0b6ab592eb136777Richard Smith } 6544d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::UnaryOperatorClass: { 6545d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall const UnaryOperator *Exp = cast<UnaryOperator>(E); 6546d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall switch (Exp->getOpcode()) { 65472de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_PostInc: 65482de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_PostDec: 65492de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_PreInc: 65502de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_PreDec: 65512de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_AddrOf: 65522de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_Deref: 655305830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith // C99 6.6/3 allows increment and decrement within unevaluated 655405830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith // subexpressions of constant expressions, but they can never be ICEs 655505830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith // because an ICE cannot contain an lvalue operand. 6556d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return ICEDiag(2, E->getLocStart()); 65572de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_Extension: 65582de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_LNot: 65592de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_Plus: 65602de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_Minus: 65612de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_Not: 65622de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_Real: 65632de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_Imag: 6564d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return CheckICE(Exp->getSubExpr(), Ctx); 6565d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6566d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 6567d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // OffsetOf falls through here. 6568d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6569d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::OffsetOfExprClass: { 6570d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // Note that per C99, offsetof must be an ICE. And AFAIK, using 657151f4708c00110940ca3f337961915f2ca1668375Richard Smith // EvaluateAsRValue matches the proposed gcc behavior for cases like 657205830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith // "offsetof(struct s{int x[4];}, x[1.0])". This doesn't affect 6573d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // compliance: we should warn earlier for offsetof expressions with 6574d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // array subscripts that aren't ICEs, and if the array subscripts 6575d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // are ICEs, the value of the offsetof must be an integer constant. 6576d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return CheckEvalInICE(E, Ctx); 6577d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6578f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne case Expr::UnaryExprOrTypeTraitExprClass: { 6579f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(E); 6580f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne if ((Exp->getKind() == UETT_SizeOf) && 6581f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne Exp->getTypeOfArgument()->isVariableArrayType()) 6582d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return ICEDiag(2, E->getLocStart()); 6583d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return NoDiag(); 6584d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6585d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::BinaryOperatorClass: { 6586d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall const BinaryOperator *Exp = cast<BinaryOperator>(E); 6587d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall switch (Exp->getOpcode()) { 65882de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_PtrMemD: 65892de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_PtrMemI: 65902de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Assign: 65912de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_MulAssign: 65922de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_DivAssign: 65932de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_RemAssign: 65942de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_AddAssign: 65952de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_SubAssign: 65962de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_ShlAssign: 65972de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_ShrAssign: 65982de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_AndAssign: 65992de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_XorAssign: 66002de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_OrAssign: 660105830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith // C99 6.6/3 allows assignments within unevaluated subexpressions of 660205830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith // constant expressions, but they can never be ICEs because an ICE cannot 660305830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith // contain an lvalue operand. 6604d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return ICEDiag(2, E->getLocStart()); 6605d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 66062de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Mul: 66072de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Div: 66082de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Rem: 66092de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Add: 66102de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Sub: 66112de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Shl: 66122de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Shr: 66132de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_LT: 66142de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_GT: 66152de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_LE: 66162de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_GE: 66172de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_EQ: 66182de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_NE: 66192de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_And: 66202de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Xor: 66212de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Or: 66222de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Comma: { 6623d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); 6624d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); 66252de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (Exp->getOpcode() == BO_Div || 66262de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall Exp->getOpcode() == BO_Rem) { 662751f4708c00110940ca3f337961915f2ca1668375Richard Smith // EvaluateAsRValue gives an error for undefined Div/Rem, so make sure 6628d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // we don't evaluate one. 66293b332ab132fa85c83833d74d400f6e126f52fbd2John McCall if (LHSResult.Val == 0 && RHSResult.Val == 0) { 6630a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith llvm::APSInt REval = Exp->getRHS()->EvaluateKnownConstInt(Ctx); 6631d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (REval == 0) 6632d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return ICEDiag(1, E->getLocStart()); 6633d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (REval.isSigned() && REval.isAllOnesValue()) { 6634a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith llvm::APSInt LEval = Exp->getLHS()->EvaluateKnownConstInt(Ctx); 6635d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (LEval.isMinSignedValue()) 6636d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return ICEDiag(1, E->getLocStart()); 6637d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6638d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6639d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 66402de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (Exp->getOpcode() == BO_Comma) { 66414e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (Ctx.getLangOpts().C99) { 6642d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // C99 6.6p3 introduces a strange edge case: comma can be in an ICE 6643d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // if it isn't evaluated. 6644d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (LHSResult.Val == 0 && RHSResult.Val == 0) 6645d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return ICEDiag(1, E->getLocStart()); 6646d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } else { 6647d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // In both C89 and C++, commas in ICEs are illegal. 6648d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return ICEDiag(2, E->getLocStart()); 6649d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6650d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6651d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (LHSResult.Val >= RHSResult.Val) 6652d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return LHSResult; 6653d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return RHSResult; 6654d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 66552de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_LAnd: 66562de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_LOr: { 6657d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); 6658d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); 6659d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (LHSResult.Val == 0 && RHSResult.Val == 1) { 6660d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // Rare case where the RHS has a comma "side-effect"; we need 6661d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // to actually check the condition to see whether the side 6662d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // with the comma is evaluated. 66632de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if ((Exp->getOpcode() == BO_LAnd) != 6664a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith (Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0)) 6665d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return RHSResult; 6666d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return NoDiag(); 6667d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6668d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 6669d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (LHSResult.Val >= RHSResult.Val) 6670d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return LHSResult; 6671d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return RHSResult; 6672d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6673d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6674d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6675d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ImplicitCastExprClass: 6676d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CStyleCastExprClass: 6677d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXFunctionalCastExprClass: 6678d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXStaticCastExprClass: 6679d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXReinterpretCastExprClass: 668032cb47174304bc7ec11478b9497c4e10f48273d9Richard Smith case Expr::CXXConstCastExprClass: 6681f85e193739c953358c865005855253af4f68a497John McCall case Expr::ObjCBridgedCastExprClass: { 6682d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr(); 66832116b144cf07f2574d20517187eb8863645376ebRichard Smith if (isa<ExplicitCastExpr>(E)) { 66842116b144cf07f2574d20517187eb8863645376ebRichard Smith if (const FloatingLiteral *FL 66852116b144cf07f2574d20517187eb8863645376ebRichard Smith = dyn_cast<FloatingLiteral>(SubExpr->IgnoreParenImpCasts())) { 66862116b144cf07f2574d20517187eb8863645376ebRichard Smith unsigned DestWidth = Ctx.getIntWidth(E->getType()); 66872116b144cf07f2574d20517187eb8863645376ebRichard Smith bool DestSigned = E->getType()->isSignedIntegerOrEnumerationType(); 66882116b144cf07f2574d20517187eb8863645376ebRichard Smith APSInt IgnoredVal(DestWidth, !DestSigned); 66892116b144cf07f2574d20517187eb8863645376ebRichard Smith bool Ignored; 66902116b144cf07f2574d20517187eb8863645376ebRichard Smith // If the value does not fit in the destination type, the behavior is 66912116b144cf07f2574d20517187eb8863645376ebRichard Smith // undefined, so we are not required to treat it as a constant 66922116b144cf07f2574d20517187eb8863645376ebRichard Smith // expression. 66932116b144cf07f2574d20517187eb8863645376ebRichard Smith if (FL->getValue().convertToInteger(IgnoredVal, 66942116b144cf07f2574d20517187eb8863645376ebRichard Smith llvm::APFloat::rmTowardZero, 66952116b144cf07f2574d20517187eb8863645376ebRichard Smith &Ignored) & APFloat::opInvalidOp) 66962116b144cf07f2574d20517187eb8863645376ebRichard Smith return ICEDiag(2, E->getLocStart()); 66972116b144cf07f2574d20517187eb8863645376ebRichard Smith return NoDiag(); 66982116b144cf07f2574d20517187eb8863645376ebRichard Smith } 66992116b144cf07f2574d20517187eb8863645376ebRichard Smith } 6700eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman switch (cast<CastExpr>(E)->getCastKind()) { 6701eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman case CK_LValueToRValue: 67027a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall case CK_AtomicToNonAtomic: 67037a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall case CK_NonAtomicToAtomic: 6704eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman case CK_NoOp: 6705eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman case CK_IntegralToBoolean: 6706eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman case CK_IntegralCast: 6707d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return CheckICE(SubExpr, Ctx); 6708eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman default: 6709eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman return ICEDiag(2, E->getLocStart()); 6710eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman } 6711d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 671256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall case Expr::BinaryConditionalOperatorClass: { 671356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(E); 671456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall ICEDiag CommonResult = CheckICE(Exp->getCommon(), Ctx); 671556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall if (CommonResult.Val == 2) return CommonResult; 671656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); 671756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall if (FalseResult.Val == 2) return FalseResult; 671856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall if (CommonResult.Val == 1) return CommonResult; 671956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall if (FalseResult.Val == 1 && 6720a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith Exp->getCommon()->EvaluateKnownConstInt(Ctx) == 0) return NoDiag(); 672156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall return FalseResult; 672256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall } 6723d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ConditionalOperatorClass: { 6724d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall const ConditionalOperator *Exp = cast<ConditionalOperator>(E); 6725d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // If the condition (ignoring parens) is a __builtin_constant_p call, 6726d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // then only the true side is actually considered in an integer constant 6727d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // expression, and it is fully evaluated. This is an important GNU 6728d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // extension. See GCC PR38377 for discussion. 6729d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (const CallExpr *CallCE 6730d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts())) 673180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p) 673280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith return CheckEvalInICE(E, Ctx); 6733d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx); 6734d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (CondResult.Val == 2) 6735d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return CondResult; 673663fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor 6737f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx); 6738f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); 673963fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor 6740d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (TrueResult.Val == 2) 6741d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return TrueResult; 6742d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (FalseResult.Val == 2) 6743d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return FalseResult; 6744d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (CondResult.Val == 1) 6745d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return CondResult; 6746d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (TrueResult.Val == 0 && FalseResult.Val == 0) 6747d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return NoDiag(); 6748d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // Rare case where the diagnostics depend on which side is evaluated 6749d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // Note that if we get here, CondResult is 0, and at least one of 6750d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // TrueResult and FalseResult is non-zero. 6751a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith if (Exp->getCond()->EvaluateKnownConstInt(Ctx) == 0) { 6752d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return FalseResult; 6753d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6754d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return TrueResult; 6755d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6756d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXDefaultArgExprClass: 6757d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx); 6758d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ChooseExprClass: { 6759d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx); 6760d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6761d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6762d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 67633026348bd4c13a0f83b59839f64065e0fcbea253David Blaikie llvm_unreachable("Invalid StmtClass!"); 6764d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall} 6765d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 6766f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// Evaluate an expression as a C++11 integral constant expression. 6767f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool EvaluateCPlusPlus11IntegralConstantExpr(ASTContext &Ctx, 6768f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith const Expr *E, 6769f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith llvm::APSInt *Value, 6770f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith SourceLocation *Loc) { 6771f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!E->getType()->isIntegralOrEnumerationType()) { 6772f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (Loc) *Loc = E->getExprLoc(); 6773f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 6774f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 6775f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 67764c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith APValue Result; 67774c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith if (!E->isCXX11ConstantExpr(Ctx, &Result, Loc)) 6778dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith return false; 6779dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith 67804c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith assert(Result.isInt() && "pointer cast to int is not an ICE"); 67814c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith if (Value) *Value = Result.getInt(); 6782dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith return true; 6783f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith} 6784f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 6785dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smithbool Expr::isIntegerConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const { 67864e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (Ctx.getLangOpts().CPlusPlus0x) 6787f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, 0, Loc); 6788f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 6789d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall ICEDiag d = CheckICE(this, Ctx); 6790d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (d.Val != 0) { 6791d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (Loc) *Loc = d.Loc; 6792d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return false; 6793d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6794f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return true; 6795f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith} 6796f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 6797f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithbool Expr::isIntegerConstantExpr(llvm::APSInt &Value, ASTContext &Ctx, 6798f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith SourceLocation *Loc, bool isEvaluated) const { 67994e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (Ctx.getLangOpts().CPlusPlus0x) 6800f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, &Value, Loc); 6801f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 6802f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!isIntegerConstantExpr(Ctx, Loc)) 6803f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 6804f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!EvaluateAsInt(Value, Ctx)) 6805d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall llvm_unreachable("ICE cannot be evaluated!"); 6806d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return true; 6807d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall} 68084c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith 680970488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smithbool Expr::isCXX98IntegralConstantExpr(ASTContext &Ctx) const { 681070488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smith return CheckICE(this, Ctx).Val == 0; 681170488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smith} 681270488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smith 68134c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smithbool Expr::isCXX11ConstantExpr(ASTContext &Ctx, APValue *Result, 68144c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith SourceLocation *Loc) const { 68154c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith // We support this checking in C++98 mode in order to diagnose compatibility 68164c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith // issues. 68174e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie assert(Ctx.getLangOpts().CPlusPlus); 68184c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith 681970488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smith // Build evaluation settings. 68204c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith Expr::EvalStatus Status; 68214c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith llvm::SmallVector<PartialDiagnosticAt, 8> Diags; 68224c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith Status.Diag = &Diags; 68234c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith EvalInfo Info(Ctx, Status); 68244c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith 68254c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith APValue Scratch; 68264c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith bool IsConstExpr = ::EvaluateAsRValue(Info, this, Result ? *Result : Scratch); 68274c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith 68284c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith if (!Diags.empty()) { 68294c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith IsConstExpr = false; 68304c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith if (Loc) *Loc = Diags[0].first; 68314c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith } else if (!IsConstExpr) { 68324c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith // FIXME: This shouldn't happen. 68334c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith if (Loc) *Loc = getExprLoc(); 68344c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith } 68354c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith 68364c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith return IsConstExpr; 68374c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith} 6838745f5147e065900267c85a5568785a1991d4838fRichard Smith 6839745f5147e065900267c85a5568785a1991d4838fRichard Smithbool Expr::isPotentialConstantExpr(const FunctionDecl *FD, 6840745f5147e065900267c85a5568785a1991d4838fRichard Smith llvm::SmallVectorImpl< 6841745f5147e065900267c85a5568785a1991d4838fRichard Smith PartialDiagnosticAt> &Diags) { 6842745f5147e065900267c85a5568785a1991d4838fRichard Smith // FIXME: It would be useful to check constexpr function templates, but at the 6843745f5147e065900267c85a5568785a1991d4838fRichard Smith // moment the constant expression evaluator cannot cope with the non-rigorous 6844745f5147e065900267c85a5568785a1991d4838fRichard Smith // ASTs which we build for dependent expressions. 6845745f5147e065900267c85a5568785a1991d4838fRichard Smith if (FD->isDependentContext()) 6846745f5147e065900267c85a5568785a1991d4838fRichard Smith return true; 6847745f5147e065900267c85a5568785a1991d4838fRichard Smith 6848745f5147e065900267c85a5568785a1991d4838fRichard Smith Expr::EvalStatus Status; 6849745f5147e065900267c85a5568785a1991d4838fRichard Smith Status.Diag = &Diags; 6850745f5147e065900267c85a5568785a1991d4838fRichard Smith 6851745f5147e065900267c85a5568785a1991d4838fRichard Smith EvalInfo Info(FD->getASTContext(), Status); 6852745f5147e065900267c85a5568785a1991d4838fRichard Smith Info.CheckingPotentialConstantExpression = true; 6853745f5147e065900267c85a5568785a1991d4838fRichard Smith 6854745f5147e065900267c85a5568785a1991d4838fRichard Smith const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); 6855745f5147e065900267c85a5568785a1991d4838fRichard Smith const CXXRecordDecl *RD = MD ? MD->getParent()->getCanonicalDecl() : 0; 6856745f5147e065900267c85a5568785a1991d4838fRichard Smith 6857745f5147e065900267c85a5568785a1991d4838fRichard Smith // FIXME: Fabricate an arbitrary expression on the stack and pretend that it 6858745f5147e065900267c85a5568785a1991d4838fRichard Smith // is a temporary being used as the 'this' pointer. 6859745f5147e065900267c85a5568785a1991d4838fRichard Smith LValue This; 6860745f5147e065900267c85a5568785a1991d4838fRichard Smith ImplicitValueInitExpr VIE(RD ? Info.Ctx.getRecordType(RD) : Info.Ctx.IntTy); 686183587db1bda97f45d2b5a4189e584e2a18be511aRichard Smith This.set(&VIE, Info.CurrentCall->Index); 6862745f5147e065900267c85a5568785a1991d4838fRichard Smith 6863745f5147e065900267c85a5568785a1991d4838fRichard Smith ArrayRef<const Expr*> Args; 6864745f5147e065900267c85a5568785a1991d4838fRichard Smith 6865745f5147e065900267c85a5568785a1991d4838fRichard Smith SourceLocation Loc = FD->getLocation(); 6866745f5147e065900267c85a5568785a1991d4838fRichard Smith 68671aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith APValue Scratch; 68681aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) 6869745f5147e065900267c85a5568785a1991d4838fRichard Smith HandleConstructorCall(Loc, This, Args, CD, Info, Scratch); 68701aa0be86358002fe876e5a4a00c3038c96be28eeRichard Smith else 6871745f5147e065900267c85a5568785a1991d4838fRichard Smith HandleFunctionCall(Loc, FD, (MD && MD->isInstance()) ? &This : 0, 6872745f5147e065900267c85a5568785a1991d4838fRichard Smith Args, FD->getBody(), Info, Scratch); 6873745f5147e065900267c85a5568785a1991d4838fRichard Smith 6874745f5147e065900267c85a5568785a1991d4838fRichard Smith return Diags.empty(); 6875745f5147e065900267c85a5568785a1991d4838fRichard Smith} 6876