ExprConstant.cpp revision b4e5e286a5cd156247720b1eb204abaa8e09568d
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 5487eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner/// EvalInfo - This is a private struct used by the evaluator to capture 5587eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner/// information about a subexpression as it is folded. It retains information 5687eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner/// about the AST context, but also maintains information about the folded 5787eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner/// expression. 5887eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner/// 5987eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner/// If an expression could be evaluated, it is still possible it is not a C 6087eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner/// "integer constant expression" or constant expression. If not, this struct 6187eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner/// captures information about how and why not. 6287eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner/// 6387eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner/// One bit of information passed *into* the request for constant folding 6487eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner/// indicates whether the subexpression is "evaluated" or not according to C 6587eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner/// rules. For example, the RHS of (0 && foo()) is not evaluated. We can 6687eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner/// evaluate the expression regardless of what the RHS is, but C only allows 6787eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner/// certain things in certain situations. 68c54061a679d8db4169438b2f97f81e3f443c6a25Benjamin Kramernamespace { 69180f47959a066795cc0f409433023af448bb0328Richard Smith struct LValue; 70d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith struct CallStackFrame; 71bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith struct EvalInfo; 72d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 731bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith QualType getType(APValue::LValueBase B) { 741bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith if (!B) return QualType(); 751bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) 761bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith return D->getType(); 771bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith return B.get<const Expr*>()->getType(); 781bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith } 791bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith 80180f47959a066795cc0f409433023af448bb0328Richard Smith /// Get an LValue path entry, which is known to not be an array index, as a 81f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith /// field or base class. 82f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith APValue::BaseOrMemberType getAsBaseOrMember(APValue::LValuePathEntry E) { 83180f47959a066795cc0f409433023af448bb0328Richard Smith APValue::BaseOrMemberType Value; 84180f47959a066795cc0f409433023af448bb0328Richard Smith Value.setFromOpaqueValue(E.BaseOrMember); 85f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith return Value; 86f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith } 87f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 88f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith /// Get an LValue path entry, which is known to not be an array index, as a 89f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith /// field declaration. 90f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith const FieldDecl *getAsField(APValue::LValuePathEntry E) { 91f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith return dyn_cast<FieldDecl>(getAsBaseOrMember(E).getPointer()); 92180f47959a066795cc0f409433023af448bb0328Richard Smith } 93180f47959a066795cc0f409433023af448bb0328Richard Smith /// Get an LValue path entry, which is known to not be an array index, as a 94180f47959a066795cc0f409433023af448bb0328Richard Smith /// base class declaration. 95180f47959a066795cc0f409433023af448bb0328Richard Smith const CXXRecordDecl *getAsBaseClass(APValue::LValuePathEntry E) { 96f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith return dyn_cast<CXXRecordDecl>(getAsBaseOrMember(E).getPointer()); 97180f47959a066795cc0f409433023af448bb0328Richard Smith } 98180f47959a066795cc0f409433023af448bb0328Richard Smith /// Determine whether this LValue path entry for a base class names a virtual 99180f47959a066795cc0f409433023af448bb0328Richard Smith /// base class. 100180f47959a066795cc0f409433023af448bb0328Richard Smith bool isVirtualBaseClass(APValue::LValuePathEntry E) { 101f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith return getAsBaseOrMember(E).getInt(); 102180f47959a066795cc0f409433023af448bb0328Richard Smith } 103180f47959a066795cc0f409433023af448bb0328Richard Smith 104b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith /// Find the path length and type of the most-derived subobject in the given 105b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith /// path, and find the size of the containing array, if any. 106b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith static 107b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith unsigned findMostDerivedSubobject(ASTContext &Ctx, QualType Base, 108b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith ArrayRef<APValue::LValuePathEntry> Path, 109b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith uint64_t &ArraySize, QualType &Type) { 110b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith unsigned MostDerivedLength = 0; 111b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Type = Base; 1129a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith for (unsigned I = 0, N = Path.size(); I != N; ++I) { 113b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (Type->isArrayType()) { 114b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith const ConstantArrayType *CAT = 115b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith cast<ConstantArrayType>(Ctx.getAsArrayType(Type)); 116b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Type = CAT->getElementType(); 117b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith ArraySize = CAT->getSize().getZExtValue(); 118b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith MostDerivedLength = I + 1; 119b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } else if (const FieldDecl *FD = getAsField(Path[I])) { 120b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Type = FD->getType(); 121b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith ArraySize = 0; 122b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith MostDerivedLength = I + 1; 123b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } else { 1249a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith // Path[I] describes a base class. 125b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith ArraySize = 0; 126b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } 1279a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith } 128b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return MostDerivedLength; 1299a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith } 1309a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith 131b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // The order of this enum is important for diagnostics. 132b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith enum CheckSubobjectKind { 133b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith CSK_Base, CSK_Derived, CSK_Field, CSK_ArrayToPointer, CSK_ArrayIndex, 134b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith CSK_This 135b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith }; 136b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 1370a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith /// A path from a glvalue to a subobject of that glvalue. 1380a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith struct SubobjectDesignator { 1390a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith /// True if the subobject was named in a manner not supported by C++11. Such 1400a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith /// lvalues can still be folded, but they are not core constant expressions 1410a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith /// and we cannot perform lvalue-to-rvalue conversions on them. 1420a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith bool Invalid : 1; 1430a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith 144b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith /// Is this a pointer one past the end of an object? 145b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith bool IsOnePastTheEnd : 1; 146b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 147b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith /// The length of the path to the most-derived object of which this is a 148b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith /// subobject. 149b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith unsigned MostDerivedPathLength : 30; 150b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 151b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith /// The size of the array of which the most-derived object is an element, or 152b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith /// 0 if the most-derived object is not an array element. 153b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith uint64_t MostDerivedArraySize; 1540a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith 155b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith /// The type of the most derived object referred to by this address. 156b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith QualType MostDerivedType; 1570a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith 1589a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith typedef APValue::LValuePathEntry PathEntry; 1599a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith 1600a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith /// The entries on the path from the glvalue to the designated subobject. 1610a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith SmallVector<PathEntry, 8> Entries; 1620a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith 163b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith SubobjectDesignator() : Invalid(true) {} 1640a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith 165b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith explicit SubobjectDesignator(QualType T) 166b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith : Invalid(false), IsOnePastTheEnd(false), MostDerivedPathLength(0), 167b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith MostDerivedArraySize(0), MostDerivedType(T) {} 168b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 169b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith SubobjectDesignator(ASTContext &Ctx, const APValue &V) 170b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith : Invalid(!V.isLValue() || !V.hasLValuePath()), IsOnePastTheEnd(false), 171b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith MostDerivedPathLength(0), MostDerivedArraySize(0) { 1729a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith if (!Invalid) { 173b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith IsOnePastTheEnd = V.isLValueOnePastTheEnd(); 1749a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith ArrayRef<PathEntry> VEntries = V.getLValuePath(); 1759a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith Entries.insert(Entries.end(), VEntries.begin(), VEntries.end()); 1769a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith if (V.getLValueBase()) 177b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith MostDerivedPathLength = 178b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith findMostDerivedSubobject(Ctx, getType(V.getLValueBase()), 179b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith V.getLValuePath(), MostDerivedArraySize, 180b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith MostDerivedType); 1819a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith } 1829a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith } 1839a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith 1840a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith void setInvalid() { 1850a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith Invalid = true; 1860a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith Entries.clear(); 1870a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith } 188b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 189b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith /// Determine whether this is a one-past-the-end pointer. 190b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith bool isOnePastTheEnd() const { 191b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (IsOnePastTheEnd) 192b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return true; 193b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (MostDerivedArraySize && 194b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Entries[MostDerivedPathLength - 1].ArrayIndex == MostDerivedArraySize) 195b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return true; 196b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return false; 197b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } 198b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 199b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith /// Check that this refers to a valid subobject. 200b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith bool isValidSubobject() const { 201b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (Invalid) 202b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return false; 203b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return !isOnePastTheEnd(); 204b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } 205b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith /// Check that this refers to a valid subobject, and if not, produce a 206b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith /// relevant diagnostic and set the designator as invalid. 207b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK); 208b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 209b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith /// Update this designator to refer to the first element within this array. 210b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith void addArrayUnchecked(const ConstantArrayType *CAT) { 2110a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith PathEntry Entry; 212b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Entry.ArrayIndex = 0; 2130a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith Entries.push_back(Entry); 214b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 215b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // This is a most-derived object. 216b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith MostDerivedType = CAT->getElementType(); 217b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith MostDerivedArraySize = CAT->getSize().getZExtValue(); 218b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith MostDerivedPathLength = Entries.size(); 2190a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith } 2200a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith /// Update this designator to refer to the given base or member of this 2210a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith /// object. 222b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith void addDeclUnchecked(const Decl *D, bool Virtual = false) { 2230a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith PathEntry Entry; 224180f47959a066795cc0f409433023af448bb0328Richard Smith APValue::BaseOrMemberType Value(D, Virtual); 225180f47959a066795cc0f409433023af448bb0328Richard Smith Entry.BaseOrMember = Value.getOpaqueValue(); 2260a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith Entries.push_back(Entry); 227b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 228b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // If this isn't a base class, it's a new most-derived object. 229b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (const FieldDecl *FD = dyn_cast<FieldDecl>(D)) { 230b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith MostDerivedType = FD->getType(); 231b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith MostDerivedArraySize = 0; 232b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith MostDerivedPathLength = Entries.size(); 233b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } 2340a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith } 235b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith void diagnosePointerArithmetic(EvalInfo &Info, const Expr *E, uint64_t N); 2360a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith /// Add N to the address of this subobject. 237b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith void adjustIndex(EvalInfo &Info, const Expr *E, uint64_t N) { 2380a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith if (Invalid) return; 239b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (MostDerivedPathLength == Entries.size() && MostDerivedArraySize) { 2409a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith Entries.back().ArrayIndex += N; 241b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (Entries.back().ArrayIndex > MostDerivedArraySize) { 242b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith diagnosePointerArithmetic(Info, E, Entries.back().ArrayIndex); 243b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith setInvalid(); 244b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } 2450a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith return; 2460a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith } 247b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // [expr.add]p4: For the purposes of these operators, a pointer to a 248b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // nonarray object behaves the same as a pointer to the first element of 249b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // an array of length one with the type of the object as its element type. 250b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (IsOnePastTheEnd && N == (uint64_t)-1) 251b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith IsOnePastTheEnd = false; 252b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith else if (!IsOnePastTheEnd && N == 1) 253b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith IsOnePastTheEnd = true; 254b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith else if (N != 0) { 255b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith diagnosePointerArithmetic(Info, E, uint64_t(IsOnePastTheEnd) + N); 2560a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith setInvalid(); 257b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } 2580a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith } 2590a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith }; 2600a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith 26147a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith /// A core constant value. This can be the value of any constant expression, 26247a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith /// or a pointer or reference to a non-static object or function parameter. 263e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith /// 264e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith /// For an LValue, the base and offset are stored in the APValue subobject, 265e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith /// but the other information is stored in the SubobjectDesignator. For all 266e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith /// other value kinds, the value is stored directly in the APValue subobject. 26747a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith class CCValue : public APValue { 26847a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith typedef llvm::APSInt APSInt; 26947a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith typedef llvm::APFloat APFloat; 270177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith /// If the value is a reference or pointer into a parameter or temporary, 271177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith /// this is the corresponding call stack frame. 272177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith CallStackFrame *CallFrame; 2730a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith /// If the value is a reference or pointer, this is a description of how the 2740a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith /// subobject was specified. 2750a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith SubobjectDesignator Designator; 27647a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith public: 277177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith struct GlobalValue {}; 278177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith 27947a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith CCValue() {} 28047a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith explicit CCValue(const APSInt &I) : APValue(I) {} 28147a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith explicit CCValue(const APFloat &F) : APValue(F) {} 28247a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith CCValue(const APValue *E, unsigned N) : APValue(E, N) {} 28347a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith CCValue(const APSInt &R, const APSInt &I) : APValue(R, I) {} 28447a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith CCValue(const APFloat &R, const APFloat &I) : APValue(R, I) {} 285177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith CCValue(const CCValue &V) : APValue(V), CallFrame(V.CallFrame) {} 2861bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith CCValue(LValueBase B, const CharUnits &O, CallStackFrame *F, 2870a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith const SubobjectDesignator &D) : 2889a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith APValue(B, O, APValue::NoLValuePath()), CallFrame(F), Designator(D) {} 289b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith CCValue(ASTContext &Ctx, const APValue &V, GlobalValue) : 290b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith APValue(V), CallFrame(0), Designator(Ctx, V) {} 291e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith CCValue(const ValueDecl *D, bool IsDerivedMember, 292e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith ArrayRef<const CXXRecordDecl*> Path) : 293e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith APValue(D, IsDerivedMember, Path) {} 29465639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman CCValue(const AddrLabelExpr* LHSExpr, const AddrLabelExpr* RHSExpr) : 29565639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman APValue(LHSExpr, RHSExpr) {} 29647a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith 297177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith CallStackFrame *getLValueFrame() const { 29847a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith assert(getKind() == LValue); 299177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith return CallFrame; 30047a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith } 3010a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith SubobjectDesignator &getLValueDesignator() { 3020a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith assert(getKind() == LValue); 3030a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith return Designator; 3040a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith } 3050a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith const SubobjectDesignator &getLValueDesignator() const { 3060a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith return const_cast<CCValue*>(this)->getLValueDesignator(); 3070a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith } 30847a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith }; 30947a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith 310bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith /// A stack frame in the constexpr call stack. 311bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith struct CallStackFrame { 312bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith EvalInfo &Info; 313bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith 314bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith /// Parent - The caller of this stack frame. 315bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith CallStackFrame *Caller; 316bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith 31708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith /// CallLoc - The location of the call expression for this call. 31808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith SourceLocation CallLoc; 31908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith 32008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith /// Callee - The function which was called. 32108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith const FunctionDecl *Callee; 32208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith 323180f47959a066795cc0f409433023af448bb0328Richard Smith /// This - The binding for the this pointer in this call, if any. 324180f47959a066795cc0f409433023af448bb0328Richard Smith const LValue *This; 325180f47959a066795cc0f409433023af448bb0328Richard Smith 326bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith /// ParmBindings - Parameter bindings for this function call, indexed by 327bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith /// parameters' function scope indices. 328bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith const CCValue *Arguments; 329bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith 330bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith typedef llvm::DenseMap<const Expr*, CCValue> MapTy; 331bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith typedef MapTy::const_iterator temp_iterator; 332bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith /// Temporaries - Temporary lvalues materialized within this stack frame. 333bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith MapTy Temporaries; 334bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith 33508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith CallStackFrame(EvalInfo &Info, SourceLocation CallLoc, 33608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith const FunctionDecl *Callee, const LValue *This, 337180f47959a066795cc0f409433023af448bb0328Richard Smith const CCValue *Arguments); 338bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith ~CallStackFrame(); 339bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith }; 340bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith 341dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith /// A partial diagnostic which we might know in advance that we are not going 342dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith /// to emit. 343dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith class OptionalDiagnostic { 344dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith PartialDiagnostic *Diag; 345dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith 346dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith public: 347dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith explicit OptionalDiagnostic(PartialDiagnostic *Diag = 0) : Diag(Diag) {} 348dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith 349dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith template<typename T> 350dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith OptionalDiagnostic &operator<<(const T &v) { 351dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith if (Diag) 352dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith *Diag << v; 353dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith return *this; 354dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith } 355789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith 356789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith OptionalDiagnostic &operator<<(const APSInt &I) { 357789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith if (Diag) { 358789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith llvm::SmallVector<char, 32> Buffer; 359789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith I.toString(Buffer); 360789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith *Diag << StringRef(Buffer.data(), Buffer.size()); 361789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith } 362789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith return *this; 363789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith } 364789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith 365789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith OptionalDiagnostic &operator<<(const APFloat &F) { 366789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith if (Diag) { 367789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith llvm::SmallVector<char, 32> Buffer; 368789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith F.toString(Buffer); 369789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith *Diag << StringRef(Buffer.data(), Buffer.size()); 370789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith } 371789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith return *this; 372789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith } 373dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith }; 374dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith 375c54061a679d8db4169438b2f97f81e3f443c6a25Benjamin Kramer struct EvalInfo { 376dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith ASTContext &Ctx; 3771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3781e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith /// EvalStatus - Contains information about the evaluation. 3791e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith Expr::EvalStatus &EvalStatus; 380f0c1e4b679e15c26bffb5892e35985bf3c52f77aAnders Carlsson 381d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith /// CurrentCall - The top of the constexpr call stack. 382bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith CallStackFrame *CurrentCall; 383d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 384d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith /// CallStackDepth - The number of calls in the call stack right now. 385d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith unsigned CallStackDepth; 386d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 38747a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith typedef llvm::DenseMap<const OpaqueValueExpr*, CCValue> MapTy; 388bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith /// OpaqueValues - Values used as the common expression in a 389bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith /// BinaryConditionalOperator. 390c54061a679d8db4169438b2f97f81e3f443c6a25Benjamin Kramer MapTy OpaqueValues; 391bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith 392bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith /// BottomFrame - The frame in which evaluation started. This must be 393745f5147e065900267c85a5568785a1991d4838fRichard Smith /// initialized after CurrentCall and CallStackDepth. 394bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith CallStackFrame BottomFrame; 395bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith 396180f47959a066795cc0f409433023af448bb0328Richard Smith /// EvaluatingDecl - This is the declaration whose initializer is being 397180f47959a066795cc0f409433023af448bb0328Richard Smith /// evaluated, if any. 398180f47959a066795cc0f409433023af448bb0328Richard Smith const VarDecl *EvaluatingDecl; 399180f47959a066795cc0f409433023af448bb0328Richard Smith 400180f47959a066795cc0f409433023af448bb0328Richard Smith /// EvaluatingDeclValue - This is the value being constructed for the 401180f47959a066795cc0f409433023af448bb0328Richard Smith /// declaration whose initializer is being evaluated, if any. 402180f47959a066795cc0f409433023af448bb0328Richard Smith APValue *EvaluatingDeclValue; 403180f47959a066795cc0f409433023af448bb0328Richard Smith 404c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith /// HasActiveDiagnostic - Was the previous diagnostic stored? If so, further 405c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith /// notes attached to it will also be stored, otherwise they will not be. 406c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith bool HasActiveDiagnostic; 407c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 408745f5147e065900267c85a5568785a1991d4838fRichard Smith /// CheckingPotentialConstantExpression - Are we checking whether the 409745f5147e065900267c85a5568785a1991d4838fRichard Smith /// expression is a potential constant expression? If so, some diagnostics 410745f5147e065900267c85a5568785a1991d4838fRichard Smith /// are suppressed. 411745f5147e065900267c85a5568785a1991d4838fRichard Smith bool CheckingPotentialConstantExpression; 412745f5147e065900267c85a5568785a1991d4838fRichard Smith 413bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith 414bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith EvalInfo(const ASTContext &C, Expr::EvalStatus &S) 415dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith : Ctx(const_cast<ASTContext&>(C)), EvalStatus(S), CurrentCall(0), 41608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith CallStackDepth(0), BottomFrame(*this, SourceLocation(), 0, 0, 0), 417745f5147e065900267c85a5568785a1991d4838fRichard Smith EvaluatingDecl(0), EvaluatingDeclValue(0), HasActiveDiagnostic(false), 418745f5147e065900267c85a5568785a1991d4838fRichard Smith CheckingPotentialConstantExpression(false) {} 419bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith 42047a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith const CCValue *getOpaqueValue(const OpaqueValueExpr *e) const { 421c54061a679d8db4169438b2f97f81e3f443c6a25Benjamin Kramer MapTy::const_iterator i = OpaqueValues.find(e); 422c54061a679d8db4169438b2f97f81e3f443c6a25Benjamin Kramer if (i == OpaqueValues.end()) return 0; 423c54061a679d8db4169438b2f97f81e3f443c6a25Benjamin Kramer return &i->second; 424c54061a679d8db4169438b2f97f81e3f443c6a25Benjamin Kramer } 42556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall 426180f47959a066795cc0f409433023af448bb0328Richard Smith void setEvaluatingDecl(const VarDecl *VD, APValue &Value) { 427180f47959a066795cc0f409433023af448bb0328Richard Smith EvaluatingDecl = VD; 428180f47959a066795cc0f409433023af448bb0328Richard Smith EvaluatingDeclValue = &Value; 429180f47959a066795cc0f409433023af448bb0328Richard Smith } 430180f47959a066795cc0f409433023af448bb0328Richard Smith 431c18c42345636e2866fed75c7e434fb659d747672Richard Smith const LangOptions &getLangOpts() const { return Ctx.getLangOptions(); } 432c18c42345636e2866fed75c7e434fb659d747672Richard Smith 433c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith bool CheckCallLimit(SourceLocation Loc) { 434745f5147e065900267c85a5568785a1991d4838fRichard Smith // Don't perform any constexpr calls (other than the call we're checking) 435745f5147e065900267c85a5568785a1991d4838fRichard Smith // when checking a potential constant expression. 436745f5147e065900267c85a5568785a1991d4838fRichard Smith if (CheckingPotentialConstantExpression && CallStackDepth > 1) 437745f5147e065900267c85a5568785a1991d4838fRichard Smith return false; 438c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (CallStackDepth <= getLangOpts().ConstexprCallDepth) 439c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return true; 440c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Diag(Loc, diag::note_constexpr_depth_limit_exceeded) 441c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith << getLangOpts().ConstexprCallDepth; 442c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return false; 443c18c42345636e2866fed75c7e434fb659d747672Richard Smith } 444f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 445c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith private: 446c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith /// Add a diagnostic to the diagnostics list. 447c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith PartialDiagnostic &addDiag(SourceLocation Loc, diag::kind DiagId) { 448c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith PartialDiagnostic PD(DiagId, Ctx.getDiagAllocator()); 449c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith EvalStatus.Diag->push_back(std::make_pair(Loc, PD)); 450c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return EvalStatus.Diag->back().second; 451c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } 452c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 45308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith /// Add notes containing a call stack to the current point of evaluation. 45408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith void addCallStack(unsigned Limit); 45508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith 456c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith public: 457f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith /// Diagnose that the evaluation cannot be folded. 4587098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith OptionalDiagnostic Diag(SourceLocation Loc, diag::kind DiagId 4597098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith = diag::note_invalid_subexpr_in_const_expr, 460c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith unsigned ExtraNotes = 0) { 461f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith // If we have a prior diagnostic, it will be noting that the expression 462f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith // isn't a constant expression. This diagnostic is more important. 463f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith // FIXME: We might want to show both diagnostics to the user. 464dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith if (EvalStatus.Diag) { 46508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith unsigned CallStackNotes = CallStackDepth - 1; 46608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith unsigned Limit = Ctx.getDiagnostics().getConstexprBacktraceLimit(); 46708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith if (Limit) 46808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith CallStackNotes = std::min(CallStackNotes, Limit + 1); 469745f5147e065900267c85a5568785a1991d4838fRichard Smith if (CheckingPotentialConstantExpression) 470745f5147e065900267c85a5568785a1991d4838fRichard Smith CallStackNotes = 0; 47108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith 472c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith HasActiveDiagnostic = true; 473dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith EvalStatus.Diag->clear(); 47408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith EvalStatus.Diag->reserve(1 + ExtraNotes + CallStackNotes); 47508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith addDiag(Loc, DiagId); 476745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!CheckingPotentialConstantExpression) 477745f5147e065900267c85a5568785a1991d4838fRichard Smith addCallStack(Limit); 47808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith return OptionalDiagnostic(&(*EvalStatus.Diag)[0].second); 479dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith } 480c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith HasActiveDiagnostic = false; 481dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith return OptionalDiagnostic(); 482dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith } 483dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith 484dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith /// Diagnose that the evaluation does not produce a C++11 core constant 485dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith /// expression. 4867098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith OptionalDiagnostic CCEDiag(SourceLocation Loc, diag::kind DiagId 4877098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith = diag::note_invalid_subexpr_in_const_expr, 488c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith unsigned ExtraNotes = 0) { 489dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith // Don't override a previous diagnostic. 490dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith if (!EvalStatus.Diag || !EvalStatus.Diag->empty()) 491dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith return OptionalDiagnostic(); 492c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return Diag(Loc, DiagId, ExtraNotes); 493c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } 494c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 495c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith /// Add a note to a prior diagnostic. 496c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith OptionalDiagnostic Note(SourceLocation Loc, diag::kind DiagId) { 497c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (!HasActiveDiagnostic) 498c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return OptionalDiagnostic(); 499c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return OptionalDiagnostic(&addDiag(Loc, DiagId)); 500f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 501099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith 502099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith /// Add a stack of notes to a prior diagnostic. 503099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith void addNotes(ArrayRef<PartialDiagnosticAt> Diags) { 504099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith if (HasActiveDiagnostic) { 505099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith EvalStatus.Diag->insert(EvalStatus.Diag->end(), 506099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Diags.begin(), Diags.end()); 507099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith } 508099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith } 509745f5147e065900267c85a5568785a1991d4838fRichard Smith 510745f5147e065900267c85a5568785a1991d4838fRichard Smith /// Should we continue evaluation as much as possible after encountering a 511745f5147e065900267c85a5568785a1991d4838fRichard Smith /// construct which can't be folded? 512745f5147e065900267c85a5568785a1991d4838fRichard Smith bool keepEvaluatingAfterFailure() { 513745f5147e065900267c85a5568785a1991d4838fRichard Smith return CheckingPotentialConstantExpression && EvalStatus.Diag->empty(); 514745f5147e065900267c85a5568785a1991d4838fRichard Smith } 515c54061a679d8db4169438b2f97f81e3f443c6a25Benjamin Kramer }; 516f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 517f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith /// Object used to treat all foldable expressions as constant expressions. 518f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith struct FoldConstant { 519f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith bool Enabled; 520f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 521f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith explicit FoldConstant(EvalInfo &Info) 522f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith : Enabled(Info.EvalStatus.Diag && Info.EvalStatus.Diag->empty() && 523f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith !Info.EvalStatus.HasSideEffects) { 524f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith } 525f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // Treat the value we've computed since this object was created as constant. 526f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith void Fold(EvalInfo &Info) { 527f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (Enabled && !Info.EvalStatus.Diag->empty() && 528f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith !Info.EvalStatus.HasSideEffects) 529f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith Info.EvalStatus.Diag->clear(); 530f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith } 531f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith }; 53208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith} 53308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith 534b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithbool SubobjectDesignator::checkSubobject(EvalInfo &Info, const Expr *E, 535b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith CheckSubobjectKind CSK) { 536b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (Invalid) 537b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return false; 538b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (isOnePastTheEnd()) { 539b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_past_end_subobject) 540b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith << CSK; 541b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith setInvalid(); 542b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return false; 543b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } 544b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return true; 545b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith} 546b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 547b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithvoid SubobjectDesignator::diagnosePointerArithmetic(EvalInfo &Info, 548b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith const Expr *E, uint64_t N) { 549b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (MostDerivedPathLength == Entries.size() && MostDerivedArraySize) 550b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_array_index) 551b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith << static_cast<int>(N) << /*array*/ 0 552b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith << static_cast<unsigned>(MostDerivedArraySize); 553b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith else 554b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_array_index) 555b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith << static_cast<int>(N) << /*non-array*/ 1; 556b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith setInvalid(); 557b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith} 558b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 55908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard SmithCallStackFrame::CallStackFrame(EvalInfo &Info, SourceLocation CallLoc, 56008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith const FunctionDecl *Callee, const LValue *This, 56108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith const CCValue *Arguments) 56208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith : Info(Info), Caller(Info.CurrentCall), CallLoc(CallLoc), Callee(Callee), 56308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith This(This), Arguments(Arguments) { 56408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith Info.CurrentCall = this; 56508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith ++Info.CallStackDepth; 56608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith} 56708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith 56808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard SmithCallStackFrame::~CallStackFrame() { 56908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith assert(Info.CurrentCall == this && "calls retired out of order"); 57008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith --Info.CallStackDepth; 57108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith Info.CurrentCall = Caller; 57208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith} 57387eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner 57408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith/// Produce a string describing the given constexpr call. 57508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smithstatic void describeCall(CallStackFrame *Frame, llvm::raw_ostream &Out) { 57608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith unsigned ArgIndex = 0; 57708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith bool IsMemberCall = isa<CXXMethodDecl>(Frame->Callee) && 5785ba73e1af8ef519161bd40063dc325457e21676aRichard Smith !isa<CXXConstructorDecl>(Frame->Callee) && 5795ba73e1af8ef519161bd40063dc325457e21676aRichard Smith cast<CXXMethodDecl>(Frame->Callee)->isInstance(); 58008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith 58108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith if (!IsMemberCall) 58208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith Out << *Frame->Callee << '('; 58308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith 58408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith for (FunctionDecl::param_const_iterator I = Frame->Callee->param_begin(), 58508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith E = Frame->Callee->param_end(); I != E; ++I, ++ArgIndex) { 5865fe31228c883040cf016cfc71ad4bfeba462602eNAKAMURA Takumi if (ArgIndex > (unsigned)IsMemberCall) 58708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith Out << ", "; 58808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith 58908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith const ParmVarDecl *Param = *I; 59008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith const CCValue &Arg = Frame->Arguments[ArgIndex]; 59108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith if (!Arg.isLValue() || Arg.getLValueDesignator().Invalid) 59208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith Arg.printPretty(Out, Frame->Info.Ctx, Param->getType()); 59308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith else { 59408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith // Deliberately slice off the frame to form an APValue we can print. 59508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith APValue Value(Arg.getLValueBase(), Arg.getLValueOffset(), 59608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith Arg.getLValueDesignator().Entries, 597b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Arg.getLValueDesignator().IsOnePastTheEnd); 59808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith Value.printPretty(Out, Frame->Info.Ctx, Param->getType()); 59908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith } 60008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith 60108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith if (ArgIndex == 0 && IsMemberCall) 60208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith Out << "->" << *Frame->Callee << '('; 603bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith } 604d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 60508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith Out << ')'; 60608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith} 60708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith 60808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smithvoid EvalInfo::addCallStack(unsigned Limit) { 60908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith // Determine which calls to skip, if any. 61008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith unsigned ActiveCalls = CallStackDepth - 1; 61108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith unsigned SkipStart = ActiveCalls, SkipEnd = SkipStart; 61208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith if (Limit && Limit < ActiveCalls) { 61308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith SkipStart = Limit / 2 + Limit % 2; 61408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith SkipEnd = ActiveCalls - Limit / 2; 61508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith } 61608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith 61708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith // Walk the call stack and add the diagnostics. 61808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith unsigned CallIdx = 0; 61908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith for (CallStackFrame *Frame = CurrentCall; Frame != &BottomFrame; 62008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith Frame = Frame->Caller, ++CallIdx) { 62108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith // Skip this call? 62208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith if (CallIdx >= SkipStart && CallIdx < SkipEnd) { 62308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith if (CallIdx == SkipStart) { 62408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith // Note that we're skipping calls. 62508d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith addDiag(Frame->CallLoc, diag::note_constexpr_calls_suppressed) 62608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith << unsigned(ActiveCalls - Limit); 62708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith } 62808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith continue; 62908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith } 63008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith 63108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith llvm::SmallVector<char, 128> Buffer; 63208d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith llvm::raw_svector_ostream Out(Buffer); 63308d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith describeCall(Frame, Out); 63408d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith addDiag(Frame->CallLoc, diag::note_constexpr_call_here) << Out.str(); 635bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith } 63608d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith} 637d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 63808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smithnamespace { 639f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall struct ComplexValue { 640f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall private: 641f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall bool IsInt; 642f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall 643f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall public: 644f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall APSInt IntReal, IntImag; 645f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall APFloat FloatReal, FloatImag; 646f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall 647f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall ComplexValue() : FloatReal(APFloat::Bogus), FloatImag(APFloat::Bogus) {} 648f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall 649f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall void makeComplexFloat() { IsInt = false; } 650f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall bool isComplexFloat() const { return !IsInt; } 651f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall APFloat &getComplexFloatReal() { return FloatReal; } 652f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall APFloat &getComplexFloatImag() { return FloatImag; } 653f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall 654f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall void makeComplexInt() { IsInt = true; } 655f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall bool isComplexInt() const { return IsInt; } 656f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall APSInt &getComplexIntReal() { return IntReal; } 657f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall APSInt &getComplexIntImag() { return IntImag; } 658f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall 65947a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith void moveInto(CCValue &v) const { 660f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall if (isComplexFloat()) 66147a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith v = CCValue(FloatReal, FloatImag); 662f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall else 66347a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith v = CCValue(IntReal, IntImag); 664f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall } 66547a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith void setFrom(const CCValue &v) { 66656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall assert(v.isComplexFloat() || v.isComplexInt()); 66756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall if (v.isComplexFloat()) { 66856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall makeComplexFloat(); 66956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall FloatReal = v.getComplexFloatReal(); 67056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall FloatImag = v.getComplexFloatImag(); 67156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall } else { 67256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall makeComplexInt(); 67356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall IntReal = v.getComplexIntReal(); 67456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall IntImag = v.getComplexIntImag(); 67556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall } 67656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall } 677f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall }; 678efdb83e26f9a1fd2566afe54461216cd84814d42John McCall 679efdb83e26f9a1fd2566afe54461216cd84814d42John McCall struct LValue { 6801bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith APValue::LValueBase Base; 681efdb83e26f9a1fd2566afe54461216cd84814d42John McCall CharUnits Offset; 682177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith CallStackFrame *Frame; 6830a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith SubobjectDesignator Designator; 684efdb83e26f9a1fd2566afe54461216cd84814d42John McCall 6851bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith const APValue::LValueBase getLValueBase() const { return Base; } 68647a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith CharUnits &getLValueOffset() { return Offset; } 687625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith const CharUnits &getLValueOffset() const { return Offset; } 688177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith CallStackFrame *getLValueFrame() const { return Frame; } 6890a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith SubobjectDesignator &getLValueDesignator() { return Designator; } 6900a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith const SubobjectDesignator &getLValueDesignator() const { return Designator;} 691efdb83e26f9a1fd2566afe54461216cd84814d42John McCall 69247a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith void moveInto(CCValue &V) const { 6930a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith V = CCValue(Base, Offset, Frame, Designator); 694efdb83e26f9a1fd2566afe54461216cd84814d42John McCall } 69547a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith void setFrom(const CCValue &V) { 69647a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith assert(V.isLValue()); 69747a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith Base = V.getLValueBase(); 69847a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith Offset = V.getLValueOffset(); 699177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith Frame = V.getLValueFrame(); 7000a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith Designator = V.getLValueDesignator(); 7010a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith } 7020a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith 7031bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith void set(APValue::LValueBase B, CallStackFrame *F = 0) { 7041bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith Base = B; 7050a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith Offset = CharUnits::Zero(); 7060a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith Frame = F; 707b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Designator = SubobjectDesignator(getType(B)); 708b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } 709b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 710b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // Check that this LValue is not based on a null pointer. If it is, produce 711b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // a diagnostic and mark the designator as invalid. 712b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith bool checkNullPointer(EvalInfo &Info, const Expr *E, 713b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith CheckSubobjectKind CSK) { 714b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (Designator.Invalid) 715b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return false; 716b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (!Base) { 717b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_null_subobject) 718b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith << CSK; 719b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Designator.setInvalid(); 720b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return false; 721b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } 722b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return true; 723b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } 724b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 725b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // Check this LValue refers to an object. If not, set the designator to be 726b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // invalid and emit a diagnostic. 727b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK) { 728b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return checkNullPointer(Info, E, CSK) && 729b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Designator.checkSubobject(Info, E, CSK); 730b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } 731b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 732b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith void addDecl(EvalInfo &Info, const Expr *E, 733b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith const Decl *D, bool Virtual = false) { 734b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith checkSubobject(Info, E, isa<FieldDecl>(D) ? CSK_Field : CSK_Base); 735b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Designator.addDeclUnchecked(D, Virtual); 736b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } 737b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith void addArray(EvalInfo &Info, const Expr *E, const ConstantArrayType *CAT) { 738b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith checkSubobject(Info, E, CSK_ArrayToPointer); 739b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Designator.addArrayUnchecked(CAT); 740b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } 741b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith void adjustIndex(EvalInfo &Info, const Expr *E, uint64_t N) { 742b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (!checkNullPointer(Info, E, CSK_ArrayIndex)) 743b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return; 744b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Designator.adjustIndex(Info, E, N); 74556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall } 746efdb83e26f9a1fd2566afe54461216cd84814d42John McCall }; 747e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 748e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith struct MemberPtr { 749e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith MemberPtr() {} 750e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith explicit MemberPtr(const ValueDecl *Decl) : 751e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith DeclAndIsDerivedMember(Decl, false), Path() {} 752e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 753e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith /// The member or (direct or indirect) field referred to by this member 754e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith /// pointer, or 0 if this is a null member pointer. 755e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const ValueDecl *getDecl() const { 756e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return DeclAndIsDerivedMember.getPointer(); 757e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 758e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith /// Is this actually a member of some type derived from the relevant class? 759e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool isDerivedMember() const { 760e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return DeclAndIsDerivedMember.getInt(); 761e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 762e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith /// Get the class which the declaration actually lives in. 763e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const CXXRecordDecl *getContainingRecord() const { 764e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return cast<CXXRecordDecl>( 765e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith DeclAndIsDerivedMember.getPointer()->getDeclContext()); 766e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 767e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 768e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith void moveInto(CCValue &V) const { 769e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith V = CCValue(getDecl(), isDerivedMember(), Path); 770e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 771e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith void setFrom(const CCValue &V) { 772e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith assert(V.isMemberPointer()); 773e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith DeclAndIsDerivedMember.setPointer(V.getMemberPointerDecl()); 774e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith DeclAndIsDerivedMember.setInt(V.isMemberPointerToDerivedMember()); 775e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Path.clear(); 776e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith ArrayRef<const CXXRecordDecl*> P = V.getMemberPointerPath(); 777e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Path.insert(Path.end(), P.begin(), P.end()); 778e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 779e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 780e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith /// DeclAndIsDerivedMember - The member declaration, and a flag indicating 781e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith /// whether the member is a member of some class derived from the class type 782e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith /// of the member pointer. 783e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith llvm::PointerIntPair<const ValueDecl*, 1, bool> DeclAndIsDerivedMember; 784e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith /// Path - The path of base/derived classes from the member declaration's 785e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith /// class (exclusive) to the class type of the member pointer (inclusive). 786e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith SmallVector<const CXXRecordDecl*, 4> Path; 787e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 788e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith /// Perform a cast towards the class of the Decl (either up or down the 789e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith /// hierarchy). 790e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool castBack(const CXXRecordDecl *Class) { 791e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith assert(!Path.empty()); 792e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const CXXRecordDecl *Expected; 793e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (Path.size() >= 2) 794e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Expected = Path[Path.size() - 2]; 795e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith else 796e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Expected = getContainingRecord(); 797e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (Expected->getCanonicalDecl() != Class->getCanonicalDecl()) { 798e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // C++11 [expr.static.cast]p12: In a conversion from (D::*) to (B::*), 799e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // if B does not contain the original member and is not a base or 800e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // derived class of the class containing the original member, the result 801e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // of the cast is undefined. 802e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // C++11 [conv.mem]p2 does not cover this case for a cast from (B::*) to 803e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // (D::*). We consider that to be a language defect. 804e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 805e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 806e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Path.pop_back(); 807e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 808e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 809e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith /// Perform a base-to-derived member pointer cast. 810e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool castToDerived(const CXXRecordDecl *Derived) { 811e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!getDecl()) 812e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 813e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!isDerivedMember()) { 814e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Path.push_back(Derived); 815e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 816e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 817e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!castBack(Derived)) 818e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 819e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (Path.empty()) 820e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith DeclAndIsDerivedMember.setInt(false); 821e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 822e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 823e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith /// Perform a derived-to-base member pointer cast. 824e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool castToBase(const CXXRecordDecl *Base) { 825e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!getDecl()) 826e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 827e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (Path.empty()) 828e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith DeclAndIsDerivedMember.setInt(true); 829e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (isDerivedMember()) { 830e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Path.push_back(Base); 831e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 832e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 833e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return castBack(Base); 834e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 835e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith }; 836c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 837b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith /// Compare two member pointers, which are assumed to be of the same type. 838b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith static bool operator==(const MemberPtr &LHS, const MemberPtr &RHS) { 839b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith if (!LHS.getDecl() || !RHS.getDecl()) 840b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith return !LHS.getDecl() && !RHS.getDecl(); 841b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith if (LHS.getDecl()->getCanonicalDecl() != RHS.getDecl()->getCanonicalDecl()) 842b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith return false; 843b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith return LHS.Path == RHS.Path; 844b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith } 845b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith 846c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith /// Kinds of constant expression checking, for diagnostics. 847c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith enum CheckConstantExpressionKind { 848c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith CCEK_Constant, ///< A normal constant. 849c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith CCEK_ReturnValue, ///< A constexpr function return value. 850c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith CCEK_MemberInit ///< A constexpr constructor mem-initializer. 851c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 852f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall} 85387eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner 85447a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smithstatic bool Evaluate(CCValue &Result, EvalInfo &Info, const Expr *E); 85569c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smithstatic bool EvaluateConstantExpression(APValue &Result, EvalInfo &Info, 856c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const LValue &This, const Expr *E, 857c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith CheckConstantExpressionKind CCEK 858c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith = CCEK_Constant); 859efdb83e26f9a1fd2566afe54461216cd84814d42John McCallstatic bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info); 860efdb83e26f9a1fd2566afe54461216cd84814d42John McCallstatic bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info); 861e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, 862e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith EvalInfo &Info); 863e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info); 86487eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattnerstatic bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info); 86547a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smithstatic bool EvaluateIntegerOrLValue(const Expr *E, CCValue &Result, 866d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner EvalInfo &Info); 867d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmanstatic bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info); 868f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCallstatic bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info); 869f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner 870f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===// 8714efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman// Misc utilities 8724efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman//===----------------------------------------------------------------------===// 8734efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 874180f47959a066795cc0f409433023af448bb0328Richard Smith/// Should this call expression be treated as a string literal? 875180f47959a066795cc0f409433023af448bb0328Richard Smithstatic bool IsStringLiteralCall(const CallExpr *E) { 876180f47959a066795cc0f409433023af448bb0328Richard Smith unsigned Builtin = E->isBuiltinCall(); 877180f47959a066795cc0f409433023af448bb0328Richard Smith return (Builtin == Builtin::BI__builtin___CFStringMakeConstantString || 878180f47959a066795cc0f409433023af448bb0328Richard Smith Builtin == Builtin::BI__builtin___NSStringMakeConstantString); 879180f47959a066795cc0f409433023af448bb0328Richard Smith} 880180f47959a066795cc0f409433023af448bb0328Richard Smith 8811bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smithstatic bool IsGlobalLValue(APValue::LValueBase B) { 882180f47959a066795cc0f409433023af448bb0328Richard Smith // C++11 [expr.const]p3 An address constant expression is a prvalue core 883180f47959a066795cc0f409433023af448bb0328Richard Smith // constant expression of pointer type that evaluates to... 884180f47959a066795cc0f409433023af448bb0328Richard Smith 885180f47959a066795cc0f409433023af448bb0328Richard Smith // ... a null pointer value, or a prvalue core constant expression of type 886180f47959a066795cc0f409433023af448bb0328Richard Smith // std::nullptr_t. 8871bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith if (!B) return true; 88842c8f87eb60958170c46767273bf93e6c96125bfJohn McCall 8891bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { 890180f47959a066795cc0f409433023af448bb0328Richard Smith // ... the address of an object with static storage duration, 8911bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith if (const VarDecl *VD = dyn_cast<VarDecl>(D)) 89242c8f87eb60958170c46767273bf93e6c96125bfJohn McCall return VD->hasGlobalStorage(); 8931bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith // ... the address of a function, 8941bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith return isa<FunctionDecl>(D); 89542c8f87eb60958170c46767273bf93e6c96125bfJohn McCall } 8961bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith 8971bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith const Expr *E = B.get<const Expr*>(); 8981bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith switch (E->getStmtClass()) { 8991bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith default: 9001bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith return false; 901180f47959a066795cc0f409433023af448bb0328Richard Smith case Expr::CompoundLiteralExprClass: 902180f47959a066795cc0f409433023af448bb0328Richard Smith return cast<CompoundLiteralExpr>(E)->isFileScope(); 903180f47959a066795cc0f409433023af448bb0328Richard Smith // A string literal has static storage duration. 904180f47959a066795cc0f409433023af448bb0328Richard Smith case Expr::StringLiteralClass: 905180f47959a066795cc0f409433023af448bb0328Richard Smith case Expr::PredefinedExprClass: 906180f47959a066795cc0f409433023af448bb0328Richard Smith case Expr::ObjCStringLiteralClass: 907180f47959a066795cc0f409433023af448bb0328Richard Smith case Expr::ObjCEncodeExprClass: 90847d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith case Expr::CXXTypeidExprClass: 909180f47959a066795cc0f409433023af448bb0328Richard Smith return true; 910180f47959a066795cc0f409433023af448bb0328Richard Smith case Expr::CallExprClass: 911180f47959a066795cc0f409433023af448bb0328Richard Smith return IsStringLiteralCall(cast<CallExpr>(E)); 912180f47959a066795cc0f409433023af448bb0328Richard Smith // For GCC compatibility, &&label has static storage duration. 913180f47959a066795cc0f409433023af448bb0328Richard Smith case Expr::AddrLabelExprClass: 914180f47959a066795cc0f409433023af448bb0328Richard Smith return true; 915180f47959a066795cc0f409433023af448bb0328Richard Smith // A Block literal expression may be used as the initialization value for 916180f47959a066795cc0f409433023af448bb0328Richard Smith // Block variables at global or local static scope. 917180f47959a066795cc0f409433023af448bb0328Richard Smith case Expr::BlockExprClass: 918180f47959a066795cc0f409433023af448bb0328Richard Smith return !cast<BlockExpr>(E)->getBlockDecl()->hasCaptures(); 919745f5147e065900267c85a5568785a1991d4838fRichard Smith case Expr::ImplicitValueInitExprClass: 920745f5147e065900267c85a5568785a1991d4838fRichard Smith // FIXME: 921745f5147e065900267c85a5568785a1991d4838fRichard Smith // We can never form an lvalue with an implicit value initialization as its 922745f5147e065900267c85a5568785a1991d4838fRichard Smith // base through expression evaluation, so these only appear in one case: the 923745f5147e065900267c85a5568785a1991d4838fRichard Smith // implicit variable declaration we invent when checking whether a constexpr 924745f5147e065900267c85a5568785a1991d4838fRichard Smith // constructor can produce a constant expression. We must assume that such 925745f5147e065900267c85a5568785a1991d4838fRichard Smith // an expression might be a global lvalue. 926745f5147e065900267c85a5568785a1991d4838fRichard Smith return true; 927180f47959a066795cc0f409433023af448bb0328Richard Smith } 92842c8f87eb60958170c46767273bf93e6c96125bfJohn McCall} 92942c8f87eb60958170c46767273bf93e6c96125bfJohn McCall 9309a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith/// Check that this reference or pointer core constant expression is a valid 931b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith/// value for an address or reference constant expression. Type T should be 93261e616206413d1779c7545c7a8ad1ce1129ad9c1Richard Smith/// either LValue or CCValue. Return true if we can fold this expression, 93361e616206413d1779c7545c7a8ad1ce1129ad9c1Richard Smith/// whether or not it's a constant expression. 9349a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smithtemplate<typename T> 935f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool CheckLValueConstantExpression(EvalInfo &Info, const Expr *E, 936c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const T &LVal, APValue &Value, 937c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith CheckConstantExpressionKind CCEK) { 938c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith APValue::LValueBase Base = LVal.getLValueBase(); 939c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const SubobjectDesignator &Designator = LVal.getLValueDesignator(); 940c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 941c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (!IsGlobalLValue(Base)) { 942c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (Info.getLangOpts().CPlusPlus0x) { 943c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); 944c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Info.Diag(E->getExprLoc(), diag::note_constexpr_non_global, 1) 945c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith << E->isGLValue() << !Designator.Entries.empty() 946c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith << !!VD << CCEK << VD; 947c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (VD) 948c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Info.Note(VD->getLocation(), diag::note_declared_at); 949c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith else 950c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Info.Note(Base.dyn_cast<const Expr*>()->getExprLoc(), 951c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith diag::note_constexpr_temporary_here); 952c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } else { 9537098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Diag(E->getExprLoc()); 954c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } 95561e616206413d1779c7545c7a8ad1ce1129ad9c1Richard Smith // Don't allow references to temporaries to escape. 95669c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith return false; 957f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 95869c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith 959b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith bool IsReferenceType = E->isGLValue(); 960b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 961b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (Designator.Invalid) { 96261e616206413d1779c7545c7a8ad1ce1129ad9c1Richard Smith // This is not a core constant expression. An appropriate diagnostic will 96361e616206413d1779c7545c7a8ad1ce1129ad9c1Richard Smith // have already been produced. 9649a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith Value = APValue(LVal.getLValueBase(), LVal.getLValueOffset(), 9659a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith APValue::NoLValuePath()); 9669a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith return true; 9679a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith } 9689a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith 969b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Value = APValue(LVal.getLValueBase(), LVal.getLValueOffset(), 970b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Designator.Entries, Designator.IsOnePastTheEnd); 971b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 972b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // Allow address constant expressions to be past-the-end pointers. This is 973b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // an extension: the standard requires them to point to an object. 974b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (!IsReferenceType) 975b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return true; 976b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 977b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // A reference constant expression must refer to an object. 978b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (!Base) { 979b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // FIXME: diagnostic 980b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Info.CCEDiag(E->getExprLoc()); 98161e616206413d1779c7545c7a8ad1ce1129ad9c1Richard Smith return true; 982b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } 983b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 984c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith // Does this refer one past the end of some object? 985b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (Designator.isOnePastTheEnd()) { 986c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); 987c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Info.Diag(E->getExprLoc(), diag::note_constexpr_past_end, 1) 988c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith << !Designator.Entries.empty() << !!VD << VD; 989c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (VD) 990c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Info.Note(VD->getLocation(), diag::note_declared_at); 991c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith else 992c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Info.Note(Base.dyn_cast<const Expr*>()->getExprLoc(), 993c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith diag::note_constexpr_temporary_here); 994c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } 995c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 99669c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith return true; 99747a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith} 99847a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith 99951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// Check that this core constant expression is of literal type, and if not, 100051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// produce an appropriate diagnostic. 100151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smithstatic bool CheckLiteralType(EvalInfo &Info, const Expr *E) { 100251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (!E->isRValue() || E->getType()->isLiteralType()) 100351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return true; 100451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 100551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith // Prvalue constant expressions must be of literal types. 100651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (Info.getLangOpts().CPlusPlus0x) 100751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith Info.Diag(E->getExprLoc(), diag::note_constexpr_nonliteral) 100851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith << E->getType(); 100951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith else 101051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); 101151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return false; 101251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith} 101351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 10149a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith/// Check that this core constant expression value is a valid value for a 10159a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith/// constant expression, and if it is, produce the corresponding constant value. 101651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// If not, report an appropriate diagnostic. Does not check that the expression 101751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// is of literal type. 1018f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool CheckConstantExpression(EvalInfo &Info, const Expr *E, 1019c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const CCValue &CCValue, APValue &Value, 1020c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith CheckConstantExpressionKind CCEK 1021c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith = CCEK_Constant) { 10229a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith if (!CCValue.isLValue()) { 10239a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith Value = CCValue; 10249a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith return true; 10259a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith } 1026c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return CheckLValueConstantExpression(Info, E, CCValue, Value, CCEK); 10279a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith} 10289a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith 10299e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smithconst ValueDecl *GetLValueBaseDecl(const LValue &LVal) { 10301bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith return LVal.Base.dyn_cast<const ValueDecl*>(); 10319e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith} 10329e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith 10339e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smithstatic bool IsLiteralLValue(const LValue &Value) { 10341bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith return Value.Base.dyn_cast<const Expr*>() && !Value.Frame; 10359e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith} 10369e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith 103765ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smithstatic bool IsWeakLValue(const LValue &Value) { 103865ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith const ValueDecl *Decl = GetLValueBaseDecl(Value); 10390dd7a25e8d679de1dc0ce788222d6dee0e879885Lang Hames return Decl && Decl->isWeak(); 104065ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith} 104165ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith 1042e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic bool EvalPointerValueAsBool(const CCValue &Value, bool &Result) { 10433554283157190e67918fad4221a5e6faf9317362John McCall // A null base expression indicates a null pointer. These are always 10443554283157190e67918fad4221a5e6faf9317362John McCall // evaluatable, and they are false unless the offset is zero. 1045e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!Value.getLValueBase()) { 1046e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Result = !Value.getLValueOffset().isZero(); 10473554283157190e67918fad4221a5e6faf9317362John McCall return true; 10483554283157190e67918fad4221a5e6faf9317362John McCall } 10493554283157190e67918fad4221a5e6faf9317362John McCall 1050e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // We have a non-null base. These are generally known to be true, but if it's 1051e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // a weak declaration it can be null at runtime. 10523554283157190e67918fad4221a5e6faf9317362John McCall Result = true; 1053e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const ValueDecl *Decl = Value.getLValueBase().dyn_cast<const ValueDecl*>(); 10540dd7a25e8d679de1dc0ce788222d6dee0e879885Lang Hames return !Decl || !Decl->isWeak(); 10555bc86103767c2abcbfdd6518e0ccbbbb6aa59e0fEli Friedman} 10565bc86103767c2abcbfdd6518e0ccbbbb6aa59e0fEli Friedman 105747a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smithstatic bool HandleConversionToBool(const CCValue &Val, bool &Result) { 1058c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith switch (Val.getKind()) { 1059c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case APValue::Uninitialized: 1060c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return false; 1061c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case APValue::Int: 1062c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith Result = Val.getInt().getBoolValue(); 106341bf4f38348561a0f12c10d34f1673cd19a6eb04Richard Smith return true; 1064c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case APValue::Float: 1065c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith Result = !Val.getFloat().isZero(); 1066a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman return true; 1067c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case APValue::ComplexInt: 1068c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith Result = Val.getComplexIntReal().getBoolValue() || 1069c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith Val.getComplexIntImag().getBoolValue(); 1070c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return true; 1071c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case APValue::ComplexFloat: 1072c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith Result = !Val.getComplexFloatReal().isZero() || 1073c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith !Val.getComplexFloatImag().isZero(); 1074436c8898cd1c93c5bacd3fcc4ac586bc5cd77062Richard Smith return true; 1075e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case APValue::LValue: 1076e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return EvalPointerValueAsBool(Val, Result); 1077e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case APValue::MemberPointer: 1078e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Result = Val.getMemberPointerDecl(); 1079e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 1080c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case APValue::Vector: 1081cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith case APValue::Array: 1082180f47959a066795cc0f409433023af448bb0328Richard Smith case APValue::Struct: 1083180f47959a066795cc0f409433023af448bb0328Richard Smith case APValue::Union: 108465639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman case APValue::AddrLabelDiff: 1085c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return false; 10864efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman } 10874efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 1088c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith llvm_unreachable("unknown APValue kind"); 1089c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith} 1090c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 1091c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smithstatic bool EvaluateAsBooleanCondition(const Expr *E, bool &Result, 1092c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith EvalInfo &Info) { 1093c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith assert(E->isRValue() && "missing lvalue-to-rvalue conv in bool condition"); 109447a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith CCValue Val; 1095c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith if (!Evaluate(Val, Info, E)) 1096c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return false; 1097c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return HandleConversionToBool(Val, Result); 10984efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman} 10994efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 1100c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithtemplate<typename T> 1101c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstatic bool HandleOverflow(EvalInfo &Info, const Expr *E, 1102c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const T &SrcValue, QualType DestType) { 1103c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Info.Diag(E->getExprLoc(), diag::note_constexpr_overflow) 1104789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith << SrcValue << DestType; 1105c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return false; 1106c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith} 1107c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 1108c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstatic bool HandleFloatToIntCast(EvalInfo &Info, const Expr *E, 1109c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith QualType SrcType, const APFloat &Value, 1110c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith QualType DestType, APSInt &Result) { 1111c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith unsigned DestWidth = Info.Ctx.getIntWidth(DestType); 1112a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar // Determine whether we are converting to unsigned or signed. 1113575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor bool DestSigned = DestType->isSignedIntegerOrEnumerationType(); 11141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1115c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Result = APSInt(DestWidth, !DestSigned); 1116a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar bool ignored; 1117c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (Value.convertToInteger(Result, llvm::APFloat::rmTowardZero, &ignored) 1118c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith & APFloat::opInvalidOp) 1119c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return HandleOverflow(Info, E, Value, DestType); 1120c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return true; 1121a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar} 1122a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar 1123c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstatic bool HandleFloatToFloatCast(EvalInfo &Info, const Expr *E, 1124c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith QualType SrcType, QualType DestType, 1125c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith APFloat &Result) { 1126c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith APFloat Value = Result; 1127a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar bool ignored; 1128c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (Result.convert(Info.Ctx.getFloatTypeSemantics(DestType), 1129c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith APFloat::rmNearestTiesToEven, &ignored) 1130c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith & APFloat::opOverflow) 1131c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return HandleOverflow(Info, E, Value, DestType); 1132c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return true; 1133a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar} 1134a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar 1135f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smithstatic APSInt HandleIntToIntCast(EvalInfo &Info, const Expr *E, 1136f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith QualType DestType, QualType SrcType, 1137f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith APSInt &Value) { 1138f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith unsigned DestWidth = Info.Ctx.getIntWidth(DestType); 1139a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar APSInt Result = Value; 1140a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar // Figure out if this is a truncate, extend or noop cast. 1141a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar // If the input is signed, do a sign extend, noop, or truncate. 11429f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad Result = Result.extOrTrunc(DestWidth); 1143575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType()); 1144a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar return Result; 1145a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar} 1146a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar 1147c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstatic bool HandleIntToFloatCast(EvalInfo &Info, const Expr *E, 1148c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith QualType SrcType, const APSInt &Value, 1149c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith QualType DestType, APFloat &Result) { 1150c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Result = APFloat(Info.Ctx.getFloatTypeSemantics(DestType), 1); 1151c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (Result.convertFromAPInt(Value, Value.isSigned(), 1152c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith APFloat::rmNearestTiesToEven) 1153c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith & APFloat::opOverflow) 1154c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return HandleOverflow(Info, E, Value, DestType); 1155c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return true; 1156a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar} 1157a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar 1158e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedmanstatic bool EvalAndBitcastToAPInt(EvalInfo &Info, const Expr *E, 1159e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman llvm::APInt &Res) { 1160e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman CCValue SVal; 1161e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman if (!Evaluate(SVal, Info, E)) 1162e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman return false; 1163e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman if (SVal.isInt()) { 1164e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman Res = SVal.getInt(); 1165e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman return true; 1166e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman } 1167e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman if (SVal.isFloat()) { 1168e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman Res = SVal.getFloat().bitcastToAPInt(); 1169e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman return true; 1170e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman } 1171e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman if (SVal.isVector()) { 1172e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman QualType VecTy = E->getType(); 1173e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman unsigned VecSize = Info.Ctx.getTypeSize(VecTy); 1174e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman QualType EltTy = VecTy->castAs<VectorType>()->getElementType(); 1175e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman unsigned EltSize = Info.Ctx.getTypeSize(EltTy); 1176e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); 1177e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman Res = llvm::APInt::getNullValue(VecSize); 1178e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman for (unsigned i = 0; i < SVal.getVectorLength(); i++) { 1179e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman APValue &Elt = SVal.getVectorElt(i); 1180e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman llvm::APInt EltAsInt; 1181e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman if (Elt.isInt()) { 1182e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman EltAsInt = Elt.getInt(); 1183e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman } else if (Elt.isFloat()) { 1184e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman EltAsInt = Elt.getFloat().bitcastToAPInt(); 1185e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman } else { 1186e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman // Don't try to handle vectors of anything other than int or float 1187e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman // (not sure if it's possible to hit this case). 1188e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); 1189e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman return false; 1190e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman } 1191e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman unsigned BaseEltSize = EltAsInt.getBitWidth(); 1192e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman if (BigEndian) 1193e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman Res |= EltAsInt.zextOrTrunc(VecSize).rotr(i*EltSize+BaseEltSize); 1194e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman else 1195e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman Res |= EltAsInt.zextOrTrunc(VecSize).rotl(i*EltSize); 1196e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman } 1197e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman return true; 1198e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman } 1199e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman // Give up if the input isn't an int, float, or vector. For example, we 1200e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman // reject "(v4i16)(intptr_t)&a". 1201e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); 1202e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman return false; 1203e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman} 1204e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman 1205b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith/// Cast an lvalue referring to a base subobject to a derived class, by 1206b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith/// truncating the lvalue's path to the given length. 1207b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithstatic bool CastToDerivedClass(EvalInfo &Info, const Expr *E, LValue &Result, 1208b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith const RecordDecl *TruncatedType, 1209b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith unsigned TruncatedElements) { 1210b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith SubobjectDesignator &D = Result.Designator; 1211180f47959a066795cc0f409433023af448bb0328Richard Smith 1212b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // Check we actually point to a derived class object. 1213b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (TruncatedElements == D.Entries.size()) 1214b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return true; 1215b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith assert(TruncatedElements >= D.MostDerivedPathLength && 1216b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith "not casting to a derived class"); 1217b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (!Result.checkSubobject(Info, E, CSK_Derived)) 1218180f47959a066795cc0f409433023af448bb0328Richard Smith return false; 1219180f47959a066795cc0f409433023af448bb0328Richard Smith 1220b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // Truncate the path to the subobject, and remove any derived-to-base offsets. 1221e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const RecordDecl *RD = TruncatedType; 1222e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith for (unsigned I = TruncatedElements, N = D.Entries.size(); I != N; ++I) { 1223180f47959a066795cc0f409433023af448bb0328Richard Smith const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); 1224180f47959a066795cc0f409433023af448bb0328Richard Smith const CXXRecordDecl *Base = getAsBaseClass(D.Entries[I]); 1225e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (isVirtualBaseClass(D.Entries[I])) 1226180f47959a066795cc0f409433023af448bb0328Richard Smith Result.Offset -= Layout.getVBaseClassOffset(Base); 1227e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith else 1228180f47959a066795cc0f409433023af448bb0328Richard Smith Result.Offset -= Layout.getBaseClassOffset(Base); 1229180f47959a066795cc0f409433023af448bb0328Richard Smith RD = Base; 1230180f47959a066795cc0f409433023af448bb0328Richard Smith } 1231e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith D.Entries.resize(TruncatedElements); 1232180f47959a066795cc0f409433023af448bb0328Richard Smith return true; 1233180f47959a066795cc0f409433023af448bb0328Richard Smith} 1234180f47959a066795cc0f409433023af448bb0328Richard Smith 1235b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithstatic void HandleLValueDirectBase(EvalInfo &Info, const Expr *E, LValue &Obj, 1236180f47959a066795cc0f409433023af448bb0328Richard Smith const CXXRecordDecl *Derived, 1237180f47959a066795cc0f409433023af448bb0328Richard Smith const CXXRecordDecl *Base, 1238180f47959a066795cc0f409433023af448bb0328Richard Smith const ASTRecordLayout *RL = 0) { 1239180f47959a066795cc0f409433023af448bb0328Richard Smith if (!RL) RL = &Info.Ctx.getASTRecordLayout(Derived); 1240180f47959a066795cc0f409433023af448bb0328Richard Smith Obj.getLValueOffset() += RL->getBaseClassOffset(Base); 1241b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Obj.addDecl(Info, E, Base, /*Virtual*/ false); 1242180f47959a066795cc0f409433023af448bb0328Richard Smith} 1243180f47959a066795cc0f409433023af448bb0328Richard Smith 1244b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithstatic bool HandleLValueBase(EvalInfo &Info, const Expr *E, LValue &Obj, 1245180f47959a066795cc0f409433023af448bb0328Richard Smith const CXXRecordDecl *DerivedDecl, 1246180f47959a066795cc0f409433023af448bb0328Richard Smith const CXXBaseSpecifier *Base) { 1247180f47959a066795cc0f409433023af448bb0328Richard Smith const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl(); 1248180f47959a066795cc0f409433023af448bb0328Richard Smith 1249180f47959a066795cc0f409433023af448bb0328Richard Smith if (!Base->isVirtual()) { 1250b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith HandleLValueDirectBase(Info, E, Obj, DerivedDecl, BaseDecl); 1251180f47959a066795cc0f409433023af448bb0328Richard Smith return true; 1252180f47959a066795cc0f409433023af448bb0328Richard Smith } 1253180f47959a066795cc0f409433023af448bb0328Richard Smith 1254b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith SubobjectDesignator &D = Obj.Designator; 1255b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (D.Invalid) 1256b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return false; 1257b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 1258180f47959a066795cc0f409433023af448bb0328Richard Smith // Extract most-derived object and corresponding type. 1259b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith DerivedDecl = D.MostDerivedType->getAsCXXRecordDecl(); 1260b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (!CastToDerivedClass(Info, E, Obj, DerivedDecl, D.MostDerivedPathLength)) 1261180f47959a066795cc0f409433023af448bb0328Richard Smith return false; 1262180f47959a066795cc0f409433023af448bb0328Richard Smith 1263b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // Find the virtual base class. 1264180f47959a066795cc0f409433023af448bb0328Richard Smith const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(DerivedDecl); 1265180f47959a066795cc0f409433023af448bb0328Richard Smith Obj.getLValueOffset() += Layout.getVBaseClassOffset(BaseDecl); 1266b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Obj.addDecl(Info, E, BaseDecl, /*Virtual*/ true); 1267180f47959a066795cc0f409433023af448bb0328Richard Smith return true; 1268180f47959a066795cc0f409433023af448bb0328Richard Smith} 1269180f47959a066795cc0f409433023af448bb0328Richard Smith 1270180f47959a066795cc0f409433023af448bb0328Richard Smith/// Update LVal to refer to the given field, which must be a member of the type 1271180f47959a066795cc0f409433023af448bb0328Richard Smith/// currently described by LVal. 1272b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithstatic void HandleLValueMember(EvalInfo &Info, const Expr *E, LValue &LVal, 1273180f47959a066795cc0f409433023af448bb0328Richard Smith const FieldDecl *FD, 1274180f47959a066795cc0f409433023af448bb0328Richard Smith const ASTRecordLayout *RL = 0) { 1275180f47959a066795cc0f409433023af448bb0328Richard Smith if (!RL) 1276180f47959a066795cc0f409433023af448bb0328Richard Smith RL = &Info.Ctx.getASTRecordLayout(FD->getParent()); 1277180f47959a066795cc0f409433023af448bb0328Richard Smith 1278180f47959a066795cc0f409433023af448bb0328Richard Smith unsigned I = FD->getFieldIndex(); 1279180f47959a066795cc0f409433023af448bb0328Richard Smith LVal.Offset += Info.Ctx.toCharUnitsFromBits(RL->getFieldOffset(I)); 1280b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith LVal.addDecl(Info, E, FD); 1281180f47959a066795cc0f409433023af448bb0328Richard Smith} 1282180f47959a066795cc0f409433023af448bb0328Richard Smith 1283d9b02e726262e4009dda830998bb934172ac0020Richard Smith/// Update LVal to refer to the given indirect field. 1284d9b02e726262e4009dda830998bb934172ac0020Richard Smithstatic void HandleLValueIndirectMember(EvalInfo &Info, const Expr *E, 1285d9b02e726262e4009dda830998bb934172ac0020Richard Smith LValue &LVal, 1286d9b02e726262e4009dda830998bb934172ac0020Richard Smith const IndirectFieldDecl *IFD) { 1287d9b02e726262e4009dda830998bb934172ac0020Richard Smith for (IndirectFieldDecl::chain_iterator C = IFD->chain_begin(), 1288d9b02e726262e4009dda830998bb934172ac0020Richard Smith CE = IFD->chain_end(); C != CE; ++C) 1289d9b02e726262e4009dda830998bb934172ac0020Richard Smith HandleLValueMember(Info, E, LVal, cast<FieldDecl>(*C)); 1290d9b02e726262e4009dda830998bb934172ac0020Richard Smith} 1291d9b02e726262e4009dda830998bb934172ac0020Richard Smith 1292180f47959a066795cc0f409433023af448bb0328Richard Smith/// Get the size of the given type in char units. 1293180f47959a066795cc0f409433023af448bb0328Richard Smithstatic bool HandleSizeof(EvalInfo &Info, QualType Type, CharUnits &Size) { 1294180f47959a066795cc0f409433023af448bb0328Richard Smith // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc 1295180f47959a066795cc0f409433023af448bb0328Richard Smith // extension. 1296180f47959a066795cc0f409433023af448bb0328Richard Smith if (Type->isVoidType() || Type->isFunctionType()) { 1297180f47959a066795cc0f409433023af448bb0328Richard Smith Size = CharUnits::One(); 1298180f47959a066795cc0f409433023af448bb0328Richard Smith return true; 1299180f47959a066795cc0f409433023af448bb0328Richard Smith } 1300180f47959a066795cc0f409433023af448bb0328Richard Smith 1301180f47959a066795cc0f409433023af448bb0328Richard Smith if (!Type->isConstantSizeType()) { 1302180f47959a066795cc0f409433023af448bb0328Richard Smith // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. 1303b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // FIXME: Diagnostic. 1304180f47959a066795cc0f409433023af448bb0328Richard Smith return false; 1305180f47959a066795cc0f409433023af448bb0328Richard Smith } 1306180f47959a066795cc0f409433023af448bb0328Richard Smith 1307180f47959a066795cc0f409433023af448bb0328Richard Smith Size = Info.Ctx.getTypeSizeInChars(Type); 1308180f47959a066795cc0f409433023af448bb0328Richard Smith return true; 1309180f47959a066795cc0f409433023af448bb0328Richard Smith} 1310180f47959a066795cc0f409433023af448bb0328Richard Smith 1311180f47959a066795cc0f409433023af448bb0328Richard Smith/// Update a pointer value to model pointer arithmetic. 1312180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param Info - Information about the ongoing evaluation. 1313b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith/// \param E - The expression being evaluated, for diagnostic purposes. 1314180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param LVal - The pointer value to be updated. 1315180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param EltTy - The pointee type represented by LVal. 1316180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param Adjustment - The adjustment, in objects of type EltTy, to add. 1317b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithstatic bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E, 1318b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith LValue &LVal, QualType EltTy, 1319b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith int64_t Adjustment) { 1320180f47959a066795cc0f409433023af448bb0328Richard Smith CharUnits SizeOfPointee; 1321180f47959a066795cc0f409433023af448bb0328Richard Smith if (!HandleSizeof(Info, EltTy, SizeOfPointee)) 1322180f47959a066795cc0f409433023af448bb0328Richard Smith return false; 1323180f47959a066795cc0f409433023af448bb0328Richard Smith 1324180f47959a066795cc0f409433023af448bb0328Richard Smith // Compute the new offset in the appropriate width. 1325180f47959a066795cc0f409433023af448bb0328Richard Smith LVal.Offset += Adjustment * SizeOfPointee; 1326b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith LVal.adjustIndex(Info, E, Adjustment); 1327180f47959a066795cc0f409433023af448bb0328Richard Smith return true; 1328180f47959a066795cc0f409433023af448bb0328Richard Smith} 1329180f47959a066795cc0f409433023af448bb0328Richard Smith 133003f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith/// Try to evaluate the initializer for a variable declaration. 1331f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool EvaluateVarDeclInit(EvalInfo &Info, const Expr *E, 1332f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith const VarDecl *VD, 1333177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith CallStackFrame *Frame, CCValue &Result) { 1334d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith // If this is a parameter to an active constexpr function call, perform 1335d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith // argument substitution. 1336d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(VD)) { 1337745f5147e065900267c85a5568785a1991d4838fRichard Smith // Assume arguments of a potential constant expression are unknown 1338745f5147e065900267c85a5568785a1991d4838fRichard Smith // constant expressions. 1339745f5147e065900267c85a5568785a1991d4838fRichard Smith if (Info.CheckingPotentialConstantExpression) 1340745f5147e065900267c85a5568785a1991d4838fRichard Smith return false; 1341f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!Frame || !Frame->Arguments) { 1342dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); 1343177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith return false; 1344f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 1345177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith Result = Frame->Arguments[PVD->getFunctionScopeIndex()]; 1346177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith return true; 1347d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith } 134803f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith 1349099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith // Dig out the initializer, and use the declaration which it's attached to. 1350099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith const Expr *Init = VD->getAnyInitializer(VD); 1351099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith if (!Init || Init->isValueDependent()) { 1352745f5147e065900267c85a5568785a1991d4838fRichard Smith // If we're checking a potential constant expression, the variable could be 1353745f5147e065900267c85a5568785a1991d4838fRichard Smith // initialized later. 1354745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!Info.CheckingPotentialConstantExpression) 1355745f5147e065900267c85a5568785a1991d4838fRichard Smith Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); 1356099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith return false; 1357099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith } 1358099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith 1359180f47959a066795cc0f409433023af448bb0328Richard Smith // If we're currently evaluating the initializer of this declaration, use that 1360180f47959a066795cc0f409433023af448bb0328Richard Smith // in-flight value. 1361180f47959a066795cc0f409433023af448bb0328Richard Smith if (Info.EvaluatingDecl == VD) { 1362b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Result = CCValue(Info.Ctx, *Info.EvaluatingDeclValue, 1363b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith CCValue::GlobalValue()); 1364180f47959a066795cc0f409433023af448bb0328Richard Smith return !Result.isUninit(); 1365180f47959a066795cc0f409433023af448bb0328Richard Smith } 1366180f47959a066795cc0f409433023af448bb0328Richard Smith 136765ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith // Never evaluate the initializer of a weak variable. We can't be sure that 136865ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith // this is the definition which will be used. 1369f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (VD->isWeak()) { 1370dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); 137165ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith return false; 1372f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 137365ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith 1374099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith // Check that we can fold the initializer. In C++, we will have already done 1375099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith // this in the cases where it matters for conformance. 1376099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith llvm::SmallVector<PartialDiagnosticAt, 8> Notes; 1377099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith if (!VD->evaluateValue(Notes)) { 1378099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Info.Diag(E->getExprLoc(), diag::note_constexpr_var_init_non_constant, 1379099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Notes.size() + 1) << VD; 1380099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Info.Note(VD->getLocation(), diag::note_declared_at); 1381099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Info.addNotes(Notes); 138247a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith return false; 1383099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith } else if (!VD->checkInitIsICE()) { 1384099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_var_init_non_constant, 1385099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Notes.size() + 1) << VD; 1386099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Info.Note(VD->getLocation(), diag::note_declared_at); 1387099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Info.addNotes(Notes); 1388f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 138903f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith 1390b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Result = CCValue(Info.Ctx, *VD->getEvaluatedValue(), CCValue::GlobalValue()); 139147a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith return true; 139203f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith} 139303f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith 1394c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smithstatic bool IsConstNonVolatile(QualType T) { 139503f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith Qualifiers Quals = T.getQualifiers(); 139603f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith return Quals.hasConst() && !Quals.hasVolatile(); 139703f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith} 139803f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith 139959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith/// Get the base index of the given base class within an APValue representing 140059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith/// the given derived class. 140159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithstatic unsigned getBaseIndex(const CXXRecordDecl *Derived, 140259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith const CXXRecordDecl *Base) { 140359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith Base = Base->getCanonicalDecl(); 140459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith unsigned Index = 0; 140559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith for (CXXRecordDecl::base_class_const_iterator I = Derived->bases_begin(), 140659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith E = Derived->bases_end(); I != E; ++I, ++Index) { 140759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith if (I->getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base) 140859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith return Index; 140959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith } 141059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith 141159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith llvm_unreachable("base class missing from derived class's bases list"); 141259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith} 141359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith 1414cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith/// Extract the designated sub-object of an rvalue. 1415f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool ExtractSubobject(EvalInfo &Info, const Expr *E, 1416f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith CCValue &Obj, QualType ObjType, 1417cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith const SubobjectDesignator &Sub, QualType SubType) { 1418b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (Sub.Invalid) 1419b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // A diagnostic will have already been produced. 1420cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith return false; 1421b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (Sub.isOnePastTheEnd()) { 14227098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Diag(E->getExprLoc(), Info.getLangOpts().CPlusPlus0x ? 1423aa5d533427d803e52ee42b250ffd6645ef5ccb0fMatt Beaumont-Gay (unsigned)diag::note_constexpr_read_past_end : 1424aa5d533427d803e52ee42b250ffd6645ef5ccb0fMatt Beaumont-Gay (unsigned)diag::note_invalid_subexpr_in_const_expr); 14257098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith return false; 14267098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } 1427f64699e8db3946e21b5f4a0421cbc58a3e439599Richard Smith if (Sub.Entries.empty()) 1428cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith return true; 1429745f5147e065900267c85a5568785a1991d4838fRichard Smith if (Info.CheckingPotentialConstantExpression && Obj.isUninit()) 1430745f5147e065900267c85a5568785a1991d4838fRichard Smith // This object might be initialized later. 1431745f5147e065900267c85a5568785a1991d4838fRichard Smith return false; 1432cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith 1433cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith assert(!Obj.isLValue() && "extracting subobject of lvalue"); 1434cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith const APValue *O = &Obj; 1435180f47959a066795cc0f409433023af448bb0328Richard Smith // Walk the designator's path to find the subobject. 1436cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith for (unsigned I = 0, N = Sub.Entries.size(); I != N; ++I) { 1437cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith if (ObjType->isArrayType()) { 1438180f47959a066795cc0f409433023af448bb0328Richard Smith // Next subobject is an array element. 1439cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(ObjType); 1440f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith assert(CAT && "vla in literal type?"); 1441cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith uint64_t Index = Sub.Entries[I].ArrayIndex; 1442f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (CAT->getSize().ule(Index)) { 14437098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // Note, it should not be possible to form a pointer with a valid 14447098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // designator which points more than one past the end of the array. 14457098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Diag(E->getExprLoc(), Info.getLangOpts().CPlusPlus0x ? 1446aa5d533427d803e52ee42b250ffd6645ef5ccb0fMatt Beaumont-Gay (unsigned)diag::note_constexpr_read_past_end : 1447aa5d533427d803e52ee42b250ffd6645ef5ccb0fMatt Beaumont-Gay (unsigned)diag::note_invalid_subexpr_in_const_expr); 1448cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith return false; 1449f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 1450cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith if (O->getArrayInitializedElts() > Index) 1451cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith O = &O->getArrayInitializedElt(Index); 1452cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith else 1453cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith O = &O->getArrayFiller(); 1454cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith ObjType = CAT->getElementType(); 1455180f47959a066795cc0f409433023af448bb0328Richard Smith } else if (const FieldDecl *Field = getAsField(Sub.Entries[I])) { 1456b4e5e286a5cd156247720b1eb204abaa8e09568dRichard Smith if (Field->isMutable()) { 1457b4e5e286a5cd156247720b1eb204abaa8e09568dRichard Smith Info.Diag(E->getExprLoc(), diag::note_constexpr_ltor_mutable, 1) 1458b4e5e286a5cd156247720b1eb204abaa8e09568dRichard Smith << Field; 1459b4e5e286a5cd156247720b1eb204abaa8e09568dRichard Smith Info.Note(Field->getLocation(), diag::note_declared_at); 1460b4e5e286a5cd156247720b1eb204abaa8e09568dRichard Smith return false; 1461b4e5e286a5cd156247720b1eb204abaa8e09568dRichard Smith } 1462b4e5e286a5cd156247720b1eb204abaa8e09568dRichard Smith 1463180f47959a066795cc0f409433023af448bb0328Richard Smith // Next subobject is a class, struct or union field. 1464180f47959a066795cc0f409433023af448bb0328Richard Smith RecordDecl *RD = ObjType->castAs<RecordType>()->getDecl(); 1465180f47959a066795cc0f409433023af448bb0328Richard Smith if (RD->isUnion()) { 1466180f47959a066795cc0f409433023af448bb0328Richard Smith const FieldDecl *UnionField = O->getUnionField(); 1467180f47959a066795cc0f409433023af448bb0328Richard Smith if (!UnionField || 1468f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith UnionField->getCanonicalDecl() != Field->getCanonicalDecl()) { 14697098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Diag(E->getExprLoc(), 14707098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith diag::note_constexpr_read_inactive_union_member) 14717098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith << Field << !UnionField << UnionField; 1472180f47959a066795cc0f409433023af448bb0328Richard Smith return false; 1473f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 1474180f47959a066795cc0f409433023af448bb0328Richard Smith O = &O->getUnionValue(); 1475180f47959a066795cc0f409433023af448bb0328Richard Smith } else 1476180f47959a066795cc0f409433023af448bb0328Richard Smith O = &O->getStructField(Field->getFieldIndex()); 1477180f47959a066795cc0f409433023af448bb0328Richard Smith ObjType = Field->getType(); 14787098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith 14797098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (ObjType.isVolatileQualified()) { 14807098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (Info.getLangOpts().CPlusPlus) { 14817098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // FIXME: Include a description of the path to the volatile subobject. 14827098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Diag(E->getExprLoc(), diag::note_constexpr_ltor_volatile_obj, 1) 14837098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith << 2 << Field; 14847098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Note(Field->getLocation(), diag::note_declared_at); 14857098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } else { 14867098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); 14877098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } 14887098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith return false; 14897098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } 1490cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith } else { 1491180f47959a066795cc0f409433023af448bb0328Richard Smith // Next subobject is a base class. 149259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith const CXXRecordDecl *Derived = ObjType->getAsCXXRecordDecl(); 149359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith const CXXRecordDecl *Base = getAsBaseClass(Sub.Entries[I]); 149459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith O = &O->getStructBase(getBaseIndex(Derived, Base)); 149559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith ObjType = Info.Ctx.getRecordType(Base); 1496cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith } 1497180f47959a066795cc0f409433023af448bb0328Richard Smith 1498f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (O->isUninit()) { 1499745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!Info.CheckingPotentialConstantExpression) 1500745f5147e065900267c85a5568785a1991d4838fRichard Smith Info.Diag(E->getExprLoc(), diag::note_constexpr_read_uninit); 1501180f47959a066795cc0f409433023af448bb0328Richard Smith return false; 1502f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 1503cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith } 1504cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith 1505b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Obj = CCValue(Info.Ctx, *O, CCValue::GlobalValue()); 1506cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith return true; 1507cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith} 1508cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith 1509f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith/// Find the position where two subobject designators diverge, or equivalently 1510f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith/// the length of the common initial subsequence. 1511f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smithstatic unsigned FindDesignatorMismatch(QualType ObjType, 1512f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith const SubobjectDesignator &A, 1513f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith const SubobjectDesignator &B, 1514f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith bool &WasArrayIndex) { 1515f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith unsigned I = 0, N = std::min(A.Entries.size(), B.Entries.size()); 1516f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith for (/**/; I != N; ++I) { 1517f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (!ObjType.isNull() && ObjType->isArrayType()) { 1518f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // Next subobject is an array element. 1519f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (A.Entries[I].ArrayIndex != B.Entries[I].ArrayIndex) { 1520f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith WasArrayIndex = true; 1521f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith return I; 1522f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith } 1523f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith ObjType = ObjType->castAsArrayTypeUnsafe()->getElementType(); 1524f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith } else { 1525f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (A.Entries[I].BaseOrMember != B.Entries[I].BaseOrMember) { 1526f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith WasArrayIndex = false; 1527f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith return I; 1528f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith } 1529f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (const FieldDecl *FD = getAsField(A.Entries[I])) 1530f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // Next subobject is a field. 1531f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith ObjType = FD->getType(); 1532f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith else 1533f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // Next subobject is a base class. 1534f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith ObjType = QualType(); 1535f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith } 1536f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith } 1537f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith WasArrayIndex = false; 1538f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith return I; 1539f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith} 1540f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 1541f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith/// Determine whether the given subobject designators refer to elements of the 1542f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith/// same array object. 1543f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smithstatic bool AreElementsOfSameArray(QualType ObjType, 1544f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith const SubobjectDesignator &A, 1545f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith const SubobjectDesignator &B) { 1546f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (A.Entries.size() != B.Entries.size()) 1547f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith return false; 1548f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 1549f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith bool IsArray = A.MostDerivedArraySize != 0; 1550f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (IsArray && A.MostDerivedPathLength != A.Entries.size()) 1551f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // A is a subobject of the array element. 1552f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith return false; 1553f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 1554f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // If A (and B) designates an array element, the last entry will be the array 1555f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // index. That doesn't have to match. Otherwise, we're in the 'implicit array 1556f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // of length 1' case, and the entire path must match. 1557f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith bool WasArrayIndex; 1558f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith unsigned CommonLength = FindDesignatorMismatch(ObjType, A, B, WasArrayIndex); 1559f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith return CommonLength >= A.Entries.size() - IsArray; 1560f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith} 1561f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 1562180f47959a066795cc0f409433023af448bb0328Richard Smith/// HandleLValueToRValueConversion - Perform an lvalue-to-rvalue conversion on 1563180f47959a066795cc0f409433023af448bb0328Richard Smith/// the given lvalue. This can also be used for 'lvalue-to-lvalue' conversions 1564180f47959a066795cc0f409433023af448bb0328Richard Smith/// for looking up the glvalue referred to by an entity of reference type. 1565180f47959a066795cc0f409433023af448bb0328Richard Smith/// 1566180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param Info - Information about the ongoing evaluation. 1567f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// \param Conv - The expression for which we are performing the conversion. 1568f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// Used for diagnostics. 15699ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith/// \param Type - The type we expect this conversion to produce, before 15709ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith/// stripping cv-qualifiers in the case of a non-clas type. 1571180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param LVal - The glvalue on which we are attempting to perform this action. 1572180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param RVal - The produced value will be placed here. 1573f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool HandleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv, 1574f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith QualType Type, 1575cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith const LValue &LVal, CCValue &RVal) { 15767098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // In C, an lvalue-to-rvalue conversion is never a constant expression. 15777098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (!Info.getLangOpts().CPlusPlus) 15787098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.CCEDiag(Conv->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); 15797098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith 1580b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (LVal.Designator.Invalid) 1581b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // A diagnostic will have already been produced. 1582b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return false; 1583b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 15841bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); 1585177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith CallStackFrame *Frame = LVal.Frame; 15867098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith SourceLocation Loc = Conv->getExprLoc(); 1587c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 1588f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!LVal.Base) { 1589f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith // FIXME: Indirection through a null pointer deserves a specific diagnostic. 15907098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Diag(Loc, diag::note_invalid_subexpr_in_const_expr); 15917098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith return false; 15927098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } 15937098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith 15947098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // C++11 DR1311: An lvalue-to-rvalue conversion on a volatile-qualified type 15957098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // is not a constant expression (even if the object is non-volatile). We also 15967098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // apply this rule to C++98, in order to conform to the expected 'volatile' 15977098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // semantics. 15987098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (Type.isVolatileQualified()) { 15997098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (Info.getLangOpts().CPlusPlus) 16007098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Diag(Loc, diag::note_constexpr_ltor_volatile_type) << Type; 16017098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith else 16027098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Diag(Loc); 1603c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return false; 1604f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 1605c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 16061bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith if (const ValueDecl *D = LVal.Base.dyn_cast<const ValueDecl*>()) { 1607c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // In C++98, const, non-volatile integers initialized with ICEs are ICEs. 1608c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // In C++11, constexpr, non-volatile variables initialized with constant 1609d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith // expressions are constant expressions too. Inside constexpr functions, 1610d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith // parameters are constant expressions even if they're non-const. 1611c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // In C, such things can also be folded, although they are not ICEs. 1612c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith const VarDecl *VD = dyn_cast<VarDecl>(D); 1613f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (const VarDecl *VDef = VD->getDefinition()) 1614f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith VD = VDef; 1615f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!VD || VD->isInvalidDecl()) { 16167098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Diag(Loc); 16170a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith return false; 1618f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 1619f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 16207098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // DR1313: If the object is volatile-qualified but the glvalue was not, 16217098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // behavior is undefined so the result is not a constant expression. 16221bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith QualType VT = VD->getType(); 16237098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (VT.isVolatileQualified()) { 16247098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (Info.getLangOpts().CPlusPlus) { 16257098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Diag(Loc, diag::note_constexpr_ltor_volatile_obj, 1) << 1 << VD; 16267098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Note(VD->getLocation(), diag::note_declared_at); 16277098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } else { 16287098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Diag(Loc); 1629f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 16307098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith return false; 16317098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } 16327098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith 16337098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (!isa<ParmVarDecl>(VD)) { 16347098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (VD->isConstexpr()) { 16357098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // OK, we can read this variable. 16367098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } else if (VT->isIntegralOrEnumerationType()) { 16377098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (!VT.isConstQualified()) { 16387098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (Info.getLangOpts().CPlusPlus) { 16397098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Diag(Loc, diag::note_constexpr_ltor_non_const_int, 1) << VD; 16407098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Note(VD->getLocation(), diag::note_declared_at); 16417098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } else { 16427098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Diag(Loc); 16437098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } 16447098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith return false; 16457098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } 16467098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } else if (VT->isFloatingType() && VT.isConstQualified()) { 16477098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // We support folding of const floating-point types, in order to make 16487098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // static const data members of such types (supported as an extension) 16497098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // more useful. 16507098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (Info.getLangOpts().CPlusPlus0x) { 16517098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.CCEDiag(Loc, diag::note_constexpr_ltor_non_constexpr, 1) << VD; 16527098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Note(VD->getLocation(), diag::note_declared_at); 16537098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } else { 16547098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.CCEDiag(Loc); 16557098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } 16567098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } else { 16577098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // FIXME: Allow folding of values of any literal type in all languages. 16587098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (Info.getLangOpts().CPlusPlus0x) { 16597098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Diag(Loc, diag::note_constexpr_ltor_non_constexpr, 1) << VD; 16607098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Note(VD->getLocation(), diag::note_declared_at); 16617098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } else { 16627098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Diag(Loc); 16637098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } 16640a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith return false; 1665f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 16660a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith } 16677098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith 1668f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!EvaluateVarDeclInit(Info, Conv, VD, Frame, RVal)) 1669c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return false; 1670c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 167147a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith if (isa<ParmVarDecl>(VD) || !VD->getAnyInitializer()->isLValue()) 1672f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return ExtractSubobject(Info, Conv, RVal, VT, LVal.Designator, Type); 1673c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 1674c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // The declaration was initialized by an lvalue, with no lvalue-to-rvalue 1675c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // conversion. This happens when the declaration and the lvalue should be 1676c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // considered synonymous, for instance when initializing an array of char 1677c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // from a string literal. Continue as if the initializer lvalue was the 1678c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // value we were originally given. 16790a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith assert(RVal.getLValueOffset().isZero() && 16800a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith "offset for lvalue init of non-reference"); 16811bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith Base = RVal.getLValueBase().get<const Expr*>(); 1682177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith Frame = RVal.getLValueFrame(); 1683c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith } 1684c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 16857098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // Volatile temporary objects cannot be read in constant expressions. 16867098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (Base->getType().isVolatileQualified()) { 16877098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (Info.getLangOpts().CPlusPlus) { 16887098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Diag(Loc, diag::note_constexpr_ltor_volatile_obj, 1) << 0; 16897098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Note(Base->getExprLoc(), diag::note_constexpr_temporary_here); 16907098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } else { 16917098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Diag(Loc); 16927098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } 16937098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith return false; 16947098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } 16957098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith 16960a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith // FIXME: Support PredefinedExpr, ObjCEncodeExpr, MakeStringConstant 16970a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith if (const StringLiteral *S = dyn_cast<StringLiteral>(Base)) { 16980a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith const SubobjectDesignator &Designator = LVal.Designator; 1699f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (Designator.Invalid || Designator.Entries.size() != 1) { 1700dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith Info.Diag(Conv->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); 17010a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith return false; 1702f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 17030a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith 17040a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith assert(Type->isIntegerType() && "string element not integer type"); 17059a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith uint64_t Index = Designator.Entries[0].ArrayIndex; 17067098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith const ConstantArrayType *CAT = 17077098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Ctx.getAsConstantArrayType(S->getType()); 17087098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (Index >= CAT->getSize().getZExtValue()) { 17097098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // Note, it should not be possible to form a pointer which points more 17107098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // than one past the end of the array without producing a prior const expr 17117098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // diagnostic. 17127098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Diag(Loc, diag::note_constexpr_read_past_end); 17130a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith return false; 1714f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 17150a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(), 17160a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith Type->isUnsignedIntegerType()); 17170a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith if (Index < S->getLength()) 17180a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith Value = S->getCodeUnit(Index); 17190a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith RVal = CCValue(Value); 17200a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith return true; 17210a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith } 17220a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith 1723177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith if (Frame) { 1724cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith // If this is a temporary expression with a nontrivial initializer, grab the 1725cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith // value from the relevant stack frame. 1726177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith RVal = Frame->Temporaries[Base]; 1727cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith } else if (const CompoundLiteralExpr *CLE 1728cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith = dyn_cast<CompoundLiteralExpr>(Base)) { 1729cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith // In C99, a CompoundLiteralExpr is an lvalue, and we defer evaluating the 1730cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith // initializer until now for such expressions. Such an expression can't be 1731cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith // an ICE in C, so this only matters for fold. 1732c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?"); 1733cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith if (!Evaluate(RVal, Info, CLE->getInitializer())) 1734cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith return false; 1735f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } else { 1736dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith Info.Diag(Conv->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); 1737cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith return false; 1738f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 1739c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 1740f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return ExtractSubobject(Info, Conv, RVal, Base->getType(), LVal.Designator, 1741f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith Type); 1742c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith} 1743c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 174459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith/// Build an lvalue for the object argument of a member function call. 174559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithstatic bool EvaluateObjectArgument(EvalInfo &Info, const Expr *Object, 174659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith LValue &This) { 174759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith if (Object->getType()->isPointerType()) 174859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith return EvaluatePointer(Object, This, Info); 174959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith 175059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith if (Object->isGLValue()) 175159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith return EvaluateLValue(Object, This, Info); 175259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith 1753e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (Object->getType()->isLiteralType()) 1754e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return EvaluateTemporary(Object, This, Info); 1755e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 1756e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 1757e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith} 1758e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 1759e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// HandleMemberPointerAccess - Evaluate a member access operation and build an 1760e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// lvalue referring to the result. 1761e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// 1762e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// \param Info - Information about the ongoing evaluation. 1763e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// \param BO - The member pointer access operation. 1764e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// \param LV - Filled in with a reference to the resulting object. 1765e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// \param IncludeMember - Specifies whether the member itself is included in 1766e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// the resulting LValue subobject designator. This is not possible when 1767e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// creating a bound member function. 1768e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// \return The field or method declaration to which the member pointer refers, 1769e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// or 0 if evaluation fails. 1770e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info, 1771e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const BinaryOperator *BO, 1772e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LValue &LV, 1773e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool IncludeMember = true) { 1774e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith assert(BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI); 1775e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 1776745f5147e065900267c85a5568785a1991d4838fRichard Smith bool EvalObjOK = EvaluateObjectArgument(Info, BO->getLHS(), LV); 1777745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!EvalObjOK && !Info.keepEvaluatingAfterFailure()) 1778e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return 0; 1779e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 1780e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith MemberPtr MemPtr; 1781e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!EvaluateMemberPointer(BO->getRHS(), MemPtr, Info)) 1782e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return 0; 1783e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 1784e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // C++11 [expr.mptr.oper]p6: If the second operand is the null pointer to 1785e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // member value, the behavior is undefined. 1786e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!MemPtr.getDecl()) 1787e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return 0; 1788e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 1789745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!EvalObjOK) 1790745f5147e065900267c85a5568785a1991d4838fRichard Smith return 0; 1791745f5147e065900267c85a5568785a1991d4838fRichard Smith 1792e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (MemPtr.isDerivedMember()) { 1793e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // This is a member of some derived class. Truncate LV appropriately. 1794e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // The end of the derived-to-base path for the base object must match the 1795e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // derived-to-base path for the member pointer. 1796b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (LV.Designator.MostDerivedPathLength + MemPtr.Path.size() > 1797e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LV.Designator.Entries.size()) 1798e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return 0; 1799e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith unsigned PathLengthToMember = 1800e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LV.Designator.Entries.size() - MemPtr.Path.size(); 1801e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith for (unsigned I = 0, N = MemPtr.Path.size(); I != N; ++I) { 1802e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const CXXRecordDecl *LVDecl = getAsBaseClass( 1803e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LV.Designator.Entries[PathLengthToMember + I]); 1804e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const CXXRecordDecl *MPDecl = MemPtr.Path[I]; 1805e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (LVDecl->getCanonicalDecl() != MPDecl->getCanonicalDecl()) 1806e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return 0; 1807e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 1808e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 1809e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // Truncate the lvalue to the appropriate derived class. 1810b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (!CastToDerivedClass(Info, BO, LV, MemPtr.getContainingRecord(), 1811b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith PathLengthToMember)) 1812b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return 0; 1813e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } else if (!MemPtr.Path.empty()) { 1814e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // Extend the LValue path with the member pointer's path. 1815e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LV.Designator.Entries.reserve(LV.Designator.Entries.size() + 1816e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith MemPtr.Path.size() + IncludeMember); 1817e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 1818e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // Walk down to the appropriate base class. 1819e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith QualType LVType = BO->getLHS()->getType(); 1820e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (const PointerType *PT = LVType->getAs<PointerType>()) 1821e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LVType = PT->getPointeeType(); 1822e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const CXXRecordDecl *RD = LVType->getAsCXXRecordDecl(); 1823e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith assert(RD && "member pointer access on non-class-type expression"); 1824e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // The first class in the path is that of the lvalue. 1825e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith for (unsigned I = 1, N = MemPtr.Path.size(); I != N; ++I) { 1826e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const CXXRecordDecl *Base = MemPtr.Path[N - I - 1]; 1827b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith HandleLValueDirectBase(Info, BO, LV, RD, Base); 1828e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith RD = Base; 1829e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 1830e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // Finally cast to the class containing the member. 1831b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith HandleLValueDirectBase(Info, BO, LV, RD, MemPtr.getContainingRecord()); 1832e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 1833e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 1834e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // Add the member. Note that we cannot build bound member functions here. 1835e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (IncludeMember) { 1836d9b02e726262e4009dda830998bb934172ac0020Richard Smith if (const FieldDecl *FD = dyn_cast<FieldDecl>(MemPtr.getDecl())) 1837d9b02e726262e4009dda830998bb934172ac0020Richard Smith HandleLValueMember(Info, BO, LV, FD); 1838d9b02e726262e4009dda830998bb934172ac0020Richard Smith else if (const IndirectFieldDecl *IFD = 1839d9b02e726262e4009dda830998bb934172ac0020Richard Smith dyn_cast<IndirectFieldDecl>(MemPtr.getDecl())) 1840d9b02e726262e4009dda830998bb934172ac0020Richard Smith HandleLValueIndirectMember(Info, BO, LV, IFD); 1841d9b02e726262e4009dda830998bb934172ac0020Richard Smith else 1842d9b02e726262e4009dda830998bb934172ac0020Richard Smith llvm_unreachable("can't construct reference to bound member function"); 1843e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 1844e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 1845e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return MemPtr.getDecl(); 1846e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith} 1847e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 1848e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// HandleBaseToDerivedCast - Apply the given base-to-derived cast operation on 1849e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// the provided lvalue, which currently refers to the base object. 1850e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic bool HandleBaseToDerivedCast(EvalInfo &Info, const CastExpr *E, 1851e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LValue &Result) { 1852e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith SubobjectDesignator &D = Result.Designator; 1853b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (D.Invalid || !Result.checkNullPointer(Info, E, CSK_Derived)) 1854e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 1855e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 1856e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith QualType TargetQT = E->getType(); 1857e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (const PointerType *PT = TargetQT->getAs<PointerType>()) 1858e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith TargetQT = PT->getPointeeType(); 1859b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 1860b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // Check this cast lands within the final derived-to-base subobject path. 1861b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (D.MostDerivedPathLength + E->path_size() > D.Entries.size()) { 1862b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_invalid_downcast) 1863b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith << D.MostDerivedType << TargetQT; 1864b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return false; 1865b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } 1866b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 1867b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // Check the type of the final cast. We don't need to check the path, 1868b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // since a cast can only be formed if the path is unique. 1869b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith unsigned NewEntriesSize = D.Entries.size() - E->path_size(); 1870e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const CXXRecordDecl *TargetType = TargetQT->getAsCXXRecordDecl(); 1871e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const CXXRecordDecl *FinalType; 1872b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (NewEntriesSize == D.MostDerivedPathLength) 1873b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith FinalType = D.MostDerivedType->getAsCXXRecordDecl(); 1874b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith else 1875e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith FinalType = getAsBaseClass(D.Entries[NewEntriesSize - 1]); 1876b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (FinalType->getCanonicalDecl() != TargetType->getCanonicalDecl()) { 1877b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_invalid_downcast) 1878b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith << D.MostDerivedType << TargetQT; 1879e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 1880b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } 1881e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 1882e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // Truncate the lvalue to the appropriate derived class. 1883b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return CastToDerivedClass(Info, E, Result, TargetType, NewEntriesSize); 188459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith} 188559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith 1886c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stumpnamespace { 1887d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smithenum EvalStmtResult { 1888d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith /// Evaluation failed. 1889d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith ESR_Failed, 1890d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith /// Hit a 'return' statement. 1891d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith ESR_Returned, 1892d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith /// Evaluation succeeded. 1893d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith ESR_Succeeded 1894d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith}; 1895d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith} 1896d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 1897d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith// Evaluate a statement. 1898c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstatic EvalStmtResult EvaluateStmt(APValue &Result, EvalInfo &Info, 1899d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith const Stmt *S) { 1900d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith switch (S->getStmtClass()) { 1901d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith default: 1902d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith return ESR_Failed; 1903d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 1904d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith case Stmt::NullStmtClass: 1905d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith case Stmt::DeclStmtClass: 1906d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith return ESR_Succeeded; 1907d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 1908c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith case Stmt::ReturnStmtClass: { 1909c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith CCValue CCResult; 1910c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const Expr *RetExpr = cast<ReturnStmt>(S)->getRetValue(); 1911c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (!Evaluate(CCResult, Info, RetExpr) || 1912c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith !CheckConstantExpression(Info, RetExpr, CCResult, Result, 1913c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith CCEK_ReturnValue)) 1914c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return ESR_Failed; 1915c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return ESR_Returned; 1916c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } 1917d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 1918d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith case Stmt::CompoundStmtClass: { 1919d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith const CompoundStmt *CS = cast<CompoundStmt>(S); 1920d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith for (CompoundStmt::const_body_iterator BI = CS->body_begin(), 1921d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith BE = CS->body_end(); BI != BE; ++BI) { 1922d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith EvalStmtResult ESR = EvaluateStmt(Result, Info, *BI); 1923d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith if (ESR != ESR_Succeeded) 1924d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith return ESR; 1925d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith } 1926d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith return ESR_Succeeded; 1927d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith } 1928d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith } 1929d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith} 1930d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 19316180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith/// CheckTrivialDefaultConstructor - Check whether a constructor is a trivial 19326180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith/// default constructor. If so, we'll fold it whether or not it's marked as 19336180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith/// constexpr. If it is marked as constexpr, we will never implicitly define it, 19346180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith/// so we need special handling. 19356180245e9f63d2927b185ec251fb75aba30f1cacRichard Smithstatic bool CheckTrivialDefaultConstructor(EvalInfo &Info, SourceLocation Loc, 193651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith const CXXConstructorDecl *CD, 193751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith bool IsValueInitialization) { 19386180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith if (!CD->isTrivial() || !CD->isDefaultConstructor()) 19396180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith return false; 19406180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith 19414c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith // Value-initialization does not call a trivial default constructor, so such a 19424c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith // call is a core constant expression whether or not the constructor is 19434c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith // constexpr. 19444c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith if (!CD->isConstexpr() && !IsValueInitialization) { 19456180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith if (Info.getLangOpts().CPlusPlus0x) { 19464c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith // FIXME: If DiagDecl is an implicitly-declared special member function, 19474c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith // we should be much more explicit about why it's not constexpr. 19484c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith Info.CCEDiag(Loc, diag::note_constexpr_invalid_function, 1) 19494c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith << /*IsConstexpr*/0 << /*IsConstructor*/1 << CD; 19504c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith Info.Note(CD->getLocation(), diag::note_declared_at); 19516180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith } else { 19526180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith Info.CCEDiag(Loc, diag::note_invalid_subexpr_in_const_expr); 19536180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith } 19546180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith } 19556180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith return true; 19566180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith} 19576180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith 1958c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith/// CheckConstexprFunction - Check that a function can be called in a constant 1959c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith/// expression. 1960c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstatic bool CheckConstexprFunction(EvalInfo &Info, SourceLocation CallLoc, 1961c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const FunctionDecl *Declaration, 1962c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const FunctionDecl *Definition) { 1963745f5147e065900267c85a5568785a1991d4838fRichard Smith // Potential constant expressions can contain calls to declared, but not yet 1964745f5147e065900267c85a5568785a1991d4838fRichard Smith // defined, constexpr functions. 1965745f5147e065900267c85a5568785a1991d4838fRichard Smith if (Info.CheckingPotentialConstantExpression && !Definition && 1966745f5147e065900267c85a5568785a1991d4838fRichard Smith Declaration->isConstexpr()) 1967745f5147e065900267c85a5568785a1991d4838fRichard Smith return false; 1968745f5147e065900267c85a5568785a1991d4838fRichard Smith 1969c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith // Can we evaluate this function call? 1970c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (Definition && Definition->isConstexpr() && !Definition->isInvalidDecl()) 1971c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return true; 1972c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 1973c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (Info.getLangOpts().CPlusPlus0x) { 1974c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const FunctionDecl *DiagDecl = Definition ? Definition : Declaration; 1975099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith // FIXME: If DiagDecl is an implicitly-declared special member function, we 1976099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith // should be much more explicit about why it's not constexpr. 1977c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Info.Diag(CallLoc, diag::note_constexpr_invalid_function, 1) 1978c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith << DiagDecl->isConstexpr() << isa<CXXConstructorDecl>(DiagDecl) 1979c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith << DiagDecl; 1980c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Info.Note(DiagDecl->getLocation(), diag::note_declared_at); 1981c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } else { 1982c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Info.Diag(CallLoc, diag::note_invalid_subexpr_in_const_expr); 1983c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } 1984c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return false; 1985c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith} 1986c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 1987180f47959a066795cc0f409433023af448bb0328Richard Smithnamespace { 1988cd99b072e8b32075da6233cbf14ea58a5c82de4fRichard Smithtypedef SmallVector<CCValue, 8> ArgVector; 1989180f47959a066795cc0f409433023af448bb0328Richard Smith} 1990180f47959a066795cc0f409433023af448bb0328Richard Smith 1991180f47959a066795cc0f409433023af448bb0328Richard Smith/// EvaluateArgs - Evaluate the arguments to a function call. 1992180f47959a066795cc0f409433023af448bb0328Richard Smithstatic bool EvaluateArgs(ArrayRef<const Expr*> Args, ArgVector &ArgValues, 1993180f47959a066795cc0f409433023af448bb0328Richard Smith EvalInfo &Info) { 1994745f5147e065900267c85a5568785a1991d4838fRichard Smith bool Success = true; 1995180f47959a066795cc0f409433023af448bb0328Richard Smith for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end(); 1996745f5147e065900267c85a5568785a1991d4838fRichard Smith I != E; ++I) { 1997745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!Evaluate(ArgValues[I - Args.begin()], Info, *I)) { 1998745f5147e065900267c85a5568785a1991d4838fRichard Smith // If we're checking for a potential constant expression, evaluate all 1999745f5147e065900267c85a5568785a1991d4838fRichard Smith // initializers even if some of them fail. 2000745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!Info.keepEvaluatingAfterFailure()) 2001745f5147e065900267c85a5568785a1991d4838fRichard Smith return false; 2002745f5147e065900267c85a5568785a1991d4838fRichard Smith Success = false; 2003745f5147e065900267c85a5568785a1991d4838fRichard Smith } 2004745f5147e065900267c85a5568785a1991d4838fRichard Smith } 2005745f5147e065900267c85a5568785a1991d4838fRichard Smith return Success; 2006180f47959a066795cc0f409433023af448bb0328Richard Smith} 2007180f47959a066795cc0f409433023af448bb0328Richard Smith 2008d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith/// Evaluate a function call. 2009745f5147e065900267c85a5568785a1991d4838fRichard Smithstatic bool HandleFunctionCall(SourceLocation CallLoc, 2010745f5147e065900267c85a5568785a1991d4838fRichard Smith const FunctionDecl *Callee, const LValue *This, 2011f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith ArrayRef<const Expr*> Args, const Stmt *Body, 2012c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith EvalInfo &Info, APValue &Result) { 2013180f47959a066795cc0f409433023af448bb0328Richard Smith ArgVector ArgValues(Args.size()); 2014180f47959a066795cc0f409433023af448bb0328Richard Smith if (!EvaluateArgs(Args, ArgValues, Info)) 2015180f47959a066795cc0f409433023af448bb0328Richard Smith return false; 2016d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 2017745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!Info.CheckCallLimit(CallLoc)) 2018745f5147e065900267c85a5568785a1991d4838fRichard Smith return false; 2019745f5147e065900267c85a5568785a1991d4838fRichard Smith 2020745f5147e065900267c85a5568785a1991d4838fRichard Smith CallStackFrame Frame(Info, CallLoc, Callee, This, ArgValues.data()); 2021d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith return EvaluateStmt(Result, Info, Body) == ESR_Returned; 2022d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith} 2023d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 2024180f47959a066795cc0f409433023af448bb0328Richard Smith/// Evaluate a constructor call. 2025745f5147e065900267c85a5568785a1991d4838fRichard Smithstatic bool HandleConstructorCall(SourceLocation CallLoc, const LValue &This, 202659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith ArrayRef<const Expr*> Args, 2027180f47959a066795cc0f409433023af448bb0328Richard Smith const CXXConstructorDecl *Definition, 202851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith EvalInfo &Info, APValue &Result) { 2029180f47959a066795cc0f409433023af448bb0328Richard Smith ArgVector ArgValues(Args.size()); 2030180f47959a066795cc0f409433023af448bb0328Richard Smith if (!EvaluateArgs(Args, ArgValues, Info)) 2031180f47959a066795cc0f409433023af448bb0328Richard Smith return false; 2032180f47959a066795cc0f409433023af448bb0328Richard Smith 2033745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!Info.CheckCallLimit(CallLoc)) 2034745f5147e065900267c85a5568785a1991d4838fRichard Smith return false; 2035745f5147e065900267c85a5568785a1991d4838fRichard Smith 2036745f5147e065900267c85a5568785a1991d4838fRichard Smith CallStackFrame Frame(Info, CallLoc, Definition, &This, ArgValues.data()); 2037180f47959a066795cc0f409433023af448bb0328Richard Smith 2038180f47959a066795cc0f409433023af448bb0328Richard Smith // If it's a delegating constructor, just delegate. 2039180f47959a066795cc0f409433023af448bb0328Richard Smith if (Definition->isDelegatingConstructor()) { 2040180f47959a066795cc0f409433023af448bb0328Richard Smith CXXConstructorDecl::init_const_iterator I = Definition->init_begin(); 2041180f47959a066795cc0f409433023af448bb0328Richard Smith return EvaluateConstantExpression(Result, Info, This, (*I)->getInit()); 2042180f47959a066795cc0f409433023af448bb0328Richard Smith } 2043180f47959a066795cc0f409433023af448bb0328Richard Smith 2044610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith // For a trivial copy or move constructor, perform an APValue copy. This is 2045610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith // essential for unions, where the operations performed by the constructor 2046610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith // cannot be represented by ctor-initializers. 2047180f47959a066795cc0f409433023af448bb0328Richard Smith const CXXRecordDecl *RD = Definition->getParent(); 2048610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith if (Definition->isDefaulted() && 2049610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith ((Definition->isCopyConstructor() && RD->hasTrivialCopyConstructor()) || 2050610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith (Definition->isMoveConstructor() && RD->hasTrivialMoveConstructor()))) { 2051610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith LValue RHS; 2052610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith RHS.setFrom(ArgValues[0]); 2053610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith CCValue Value; 2054745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!HandleLValueToRValueConversion(Info, Args[0], Args[0]->getType(), 2055745f5147e065900267c85a5568785a1991d4838fRichard Smith RHS, Value)) 2056745f5147e065900267c85a5568785a1991d4838fRichard Smith return false; 2057745f5147e065900267c85a5568785a1991d4838fRichard Smith assert((Value.isStruct() || Value.isUnion()) && 2058745f5147e065900267c85a5568785a1991d4838fRichard Smith "trivial copy/move from non-class type?"); 2059745f5147e065900267c85a5568785a1991d4838fRichard Smith // Any CCValue of class type must already be a constant expression. 2060745f5147e065900267c85a5568785a1991d4838fRichard Smith Result = Value; 2061745f5147e065900267c85a5568785a1991d4838fRichard Smith return true; 2062610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith } 2063610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith 2064610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith // Reserve space for the struct members. 206551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (!RD->isUnion() && Result.isUninit()) 2066180f47959a066795cc0f409433023af448bb0328Richard Smith Result = APValue(APValue::UninitStruct(), RD->getNumBases(), 2067180f47959a066795cc0f409433023af448bb0328Richard Smith std::distance(RD->field_begin(), RD->field_end())); 2068180f47959a066795cc0f409433023af448bb0328Richard Smith 2069180f47959a066795cc0f409433023af448bb0328Richard Smith const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); 2070180f47959a066795cc0f409433023af448bb0328Richard Smith 2071745f5147e065900267c85a5568785a1991d4838fRichard Smith bool Success = true; 2072180f47959a066795cc0f409433023af448bb0328Richard Smith unsigned BasesSeen = 0; 2073180f47959a066795cc0f409433023af448bb0328Richard Smith#ifndef NDEBUG 2074180f47959a066795cc0f409433023af448bb0328Richard Smith CXXRecordDecl::base_class_const_iterator BaseIt = RD->bases_begin(); 2075180f47959a066795cc0f409433023af448bb0328Richard Smith#endif 2076180f47959a066795cc0f409433023af448bb0328Richard Smith for (CXXConstructorDecl::init_const_iterator I = Definition->init_begin(), 2077180f47959a066795cc0f409433023af448bb0328Richard Smith E = Definition->init_end(); I != E; ++I) { 2078745f5147e065900267c85a5568785a1991d4838fRichard Smith LValue Subobject = This; 2079745f5147e065900267c85a5568785a1991d4838fRichard Smith APValue *Value = &Result; 2080745f5147e065900267c85a5568785a1991d4838fRichard Smith 2081745f5147e065900267c85a5568785a1991d4838fRichard Smith // Determine the subobject to initialize. 2082180f47959a066795cc0f409433023af448bb0328Richard Smith if ((*I)->isBaseInitializer()) { 2083180f47959a066795cc0f409433023af448bb0328Richard Smith QualType BaseType((*I)->getBaseClass(), 0); 2084180f47959a066795cc0f409433023af448bb0328Richard Smith#ifndef NDEBUG 2085180f47959a066795cc0f409433023af448bb0328Richard Smith // Non-virtual base classes are initialized in the order in the class 2086180f47959a066795cc0f409433023af448bb0328Richard Smith // definition. We cannot have a virtual base class for a literal type. 2087180f47959a066795cc0f409433023af448bb0328Richard Smith assert(!BaseIt->isVirtual() && "virtual base for literal type"); 2088180f47959a066795cc0f409433023af448bb0328Richard Smith assert(Info.Ctx.hasSameType(BaseIt->getType(), BaseType) && 2089180f47959a066795cc0f409433023af448bb0328Richard Smith "base class initializers not in expected order"); 2090180f47959a066795cc0f409433023af448bb0328Richard Smith ++BaseIt; 2091180f47959a066795cc0f409433023af448bb0328Richard Smith#endif 2092b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith HandleLValueDirectBase(Info, (*I)->getInit(), Subobject, RD, 2093180f47959a066795cc0f409433023af448bb0328Richard Smith BaseType->getAsCXXRecordDecl(), &Layout); 2094745f5147e065900267c85a5568785a1991d4838fRichard Smith Value = &Result.getStructBase(BasesSeen++); 2095180f47959a066795cc0f409433023af448bb0328Richard Smith } else if (FieldDecl *FD = (*I)->getMember()) { 2096b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith HandleLValueMember(Info, (*I)->getInit(), Subobject, FD, &Layout); 2097180f47959a066795cc0f409433023af448bb0328Richard Smith if (RD->isUnion()) { 2098180f47959a066795cc0f409433023af448bb0328Richard Smith Result = APValue(FD); 2099745f5147e065900267c85a5568785a1991d4838fRichard Smith Value = &Result.getUnionValue(); 2100745f5147e065900267c85a5568785a1991d4838fRichard Smith } else { 2101745f5147e065900267c85a5568785a1991d4838fRichard Smith Value = &Result.getStructField(FD->getFieldIndex()); 2102745f5147e065900267c85a5568785a1991d4838fRichard Smith } 2103d9b02e726262e4009dda830998bb934172ac0020Richard Smith } else if (IndirectFieldDecl *IFD = (*I)->getIndirectMember()) { 2104d9b02e726262e4009dda830998bb934172ac0020Richard Smith // Walk the indirect field decl's chain to find the object to initialize, 2105d9b02e726262e4009dda830998bb934172ac0020Richard Smith // and make sure we've initialized every step along it. 2106d9b02e726262e4009dda830998bb934172ac0020Richard Smith for (IndirectFieldDecl::chain_iterator C = IFD->chain_begin(), 2107d9b02e726262e4009dda830998bb934172ac0020Richard Smith CE = IFD->chain_end(); 2108d9b02e726262e4009dda830998bb934172ac0020Richard Smith C != CE; ++C) { 2109d9b02e726262e4009dda830998bb934172ac0020Richard Smith FieldDecl *FD = cast<FieldDecl>(*C); 2110d9b02e726262e4009dda830998bb934172ac0020Richard Smith CXXRecordDecl *CD = cast<CXXRecordDecl>(FD->getParent()); 2111d9b02e726262e4009dda830998bb934172ac0020Richard Smith // Switch the union field if it differs. This happens if we had 2112d9b02e726262e4009dda830998bb934172ac0020Richard Smith // preceding zero-initialization, and we're now initializing a union 2113d9b02e726262e4009dda830998bb934172ac0020Richard Smith // subobject other than the first. 2114d9b02e726262e4009dda830998bb934172ac0020Richard Smith // FIXME: In this case, the values of the other subobjects are 2115d9b02e726262e4009dda830998bb934172ac0020Richard Smith // specified, since zero-initialization sets all padding bits to zero. 2116d9b02e726262e4009dda830998bb934172ac0020Richard Smith if (Value->isUninit() || 2117d9b02e726262e4009dda830998bb934172ac0020Richard Smith (Value->isUnion() && Value->getUnionField() != FD)) { 2118d9b02e726262e4009dda830998bb934172ac0020Richard Smith if (CD->isUnion()) 2119d9b02e726262e4009dda830998bb934172ac0020Richard Smith *Value = APValue(FD); 2120d9b02e726262e4009dda830998bb934172ac0020Richard Smith else 2121d9b02e726262e4009dda830998bb934172ac0020Richard Smith *Value = APValue(APValue::UninitStruct(), CD->getNumBases(), 2122d9b02e726262e4009dda830998bb934172ac0020Richard Smith std::distance(CD->field_begin(), CD->field_end())); 2123d9b02e726262e4009dda830998bb934172ac0020Richard Smith } 2124745f5147e065900267c85a5568785a1991d4838fRichard Smith HandleLValueMember(Info, (*I)->getInit(), Subobject, FD); 2125d9b02e726262e4009dda830998bb934172ac0020Richard Smith if (CD->isUnion()) 2126d9b02e726262e4009dda830998bb934172ac0020Richard Smith Value = &Value->getUnionValue(); 2127d9b02e726262e4009dda830998bb934172ac0020Richard Smith else 2128d9b02e726262e4009dda830998bb934172ac0020Richard Smith Value = &Value->getStructField(FD->getFieldIndex()); 2129d9b02e726262e4009dda830998bb934172ac0020Richard Smith } 2130180f47959a066795cc0f409433023af448bb0328Richard Smith } else { 2131d9b02e726262e4009dda830998bb934172ac0020Richard Smith llvm_unreachable("unknown base initializer kind"); 2132180f47959a066795cc0f409433023af448bb0328Richard Smith } 2133745f5147e065900267c85a5568785a1991d4838fRichard Smith 2134745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!EvaluateConstantExpression(*Value, Info, Subobject, (*I)->getInit(), 2135745f5147e065900267c85a5568785a1991d4838fRichard Smith (*I)->isBaseInitializer() 2136745f5147e065900267c85a5568785a1991d4838fRichard Smith ? CCEK_Constant : CCEK_MemberInit)) { 2137745f5147e065900267c85a5568785a1991d4838fRichard Smith // If we're checking for a potential constant expression, evaluate all 2138745f5147e065900267c85a5568785a1991d4838fRichard Smith // initializers even if some of them fail. 2139745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!Info.keepEvaluatingAfterFailure()) 2140745f5147e065900267c85a5568785a1991d4838fRichard Smith return false; 2141745f5147e065900267c85a5568785a1991d4838fRichard Smith Success = false; 2142745f5147e065900267c85a5568785a1991d4838fRichard Smith } 2143180f47959a066795cc0f409433023af448bb0328Richard Smith } 2144180f47959a066795cc0f409433023af448bb0328Richard Smith 2145745f5147e065900267c85a5568785a1991d4838fRichard Smith return Success; 2146180f47959a066795cc0f409433023af448bb0328Richard Smith} 2147180f47959a066795cc0f409433023af448bb0328Richard Smith 2148d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smithnamespace { 2149770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass HasSideEffect 21508cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne : public ConstStmtVisitor<HasSideEffect, bool> { 21511e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith const ASTContext &Ctx; 2152c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stumppublic: 2153c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump 21541e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith HasSideEffect(const ASTContext &C) : Ctx(C) {} 2155c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump 2156c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump // Unhandled nodes conservatively default to having side effects. 21578cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitStmt(const Stmt *S) { 2158c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump return true; 2159c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump } 2160c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump 21618cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitParenExpr(const ParenExpr *E) { return Visit(E->getSubExpr()); } 21628cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitGenericSelectionExpr(const GenericSelectionExpr *E) { 2163f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne return Visit(E->getResultExpr()); 2164f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne } 21658cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitDeclRefExpr(const DeclRefExpr *E) { 21661e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith if (Ctx.getCanonicalType(E->getType()).isVolatileQualified()) 2167c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump return true; 2168c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump return false; 2169c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump } 2170f85e193739c953358c865005855253af4f68a497John McCall bool VisitObjCIvarRefExpr(const ObjCIvarRefExpr *E) { 21711e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith if (Ctx.getCanonicalType(E->getType()).isVolatileQualified()) 2172f85e193739c953358c865005855253af4f68a497John McCall return true; 2173f85e193739c953358c865005855253af4f68a497John McCall return false; 2174f85e193739c953358c865005855253af4f68a497John McCall } 2175f85e193739c953358c865005855253af4f68a497John McCall bool VisitBlockDeclRefExpr (const BlockDeclRefExpr *E) { 21761e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith if (Ctx.getCanonicalType(E->getType()).isVolatileQualified()) 2177f85e193739c953358c865005855253af4f68a497John McCall return true; 2178f85e193739c953358c865005855253af4f68a497John McCall return false; 2179f85e193739c953358c865005855253af4f68a497John McCall } 2180f85e193739c953358c865005855253af4f68a497John McCall 2181c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump // We don't want to evaluate BlockExprs multiple times, as they generate 2182c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump // a ton of code. 21838cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitBlockExpr(const BlockExpr *E) { return true; } 21848cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitPredefinedExpr(const PredefinedExpr *E) { return false; } 21858cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) 2186c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump { return Visit(E->getInitializer()); } 21878cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitMemberExpr(const MemberExpr *E) { return Visit(E->getBase()); } 21888cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitIntegerLiteral(const IntegerLiteral *E) { return false; } 21898cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitFloatingLiteral(const FloatingLiteral *E) { return false; } 21908cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitStringLiteral(const StringLiteral *E) { return false; } 21918cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitCharacterLiteral(const CharacterLiteral *E) { return false; } 21928cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E) 2193f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne { return false; } 21948cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E) 2195980ca220848d27ef55ef4c2d37423461a8ed0da3Mike Stump { return Visit(E->getLHS()) || Visit(E->getRHS()); } 21968cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitChooseExpr(const ChooseExpr *E) 21971e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith { return Visit(E->getChosenSubExpr(Ctx)); } 21988cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitCastExpr(const CastExpr *E) { return Visit(E->getSubExpr()); } 21998cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitBinAssign(const BinaryOperator *E) { return true; } 22008cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitCompoundAssignOperator(const BinaryOperator *E) { return true; } 22018cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitBinaryOperator(const BinaryOperator *E) 2202980ca220848d27ef55ef4c2d37423461a8ed0da3Mike Stump { return Visit(E->getLHS()) || Visit(E->getRHS()); } 22038cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitUnaryPreInc(const UnaryOperator *E) { return true; } 22048cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitUnaryPostInc(const UnaryOperator *E) { return true; } 22058cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitUnaryPreDec(const UnaryOperator *E) { return true; } 22068cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitUnaryPostDec(const UnaryOperator *E) { return true; } 22078cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitUnaryDeref(const UnaryOperator *E) { 22081e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith if (Ctx.getCanonicalType(E->getType()).isVolatileQualified()) 2209c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump return true; 2210980ca220848d27ef55ef4c2d37423461a8ed0da3Mike Stump return Visit(E->getSubExpr()); 2211c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump } 22128cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitUnaryOperator(const UnaryOperator *E) { return Visit(E->getSubExpr()); } 2213363ff23cfddb51abe4ee4212a6dd3c9b534fcc8bChris Lattner 2214363ff23cfddb51abe4ee4212a6dd3c9b534fcc8bChris Lattner // Has side effects if any element does. 22158cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitInitListExpr(const InitListExpr *E) { 2216363ff23cfddb51abe4ee4212a6dd3c9b534fcc8bChris Lattner for (unsigned i = 0, e = E->getNumInits(); i != e; ++i) 2217363ff23cfddb51abe4ee4212a6dd3c9b534fcc8bChris Lattner if (Visit(E->getInit(i))) return true; 22188cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne if (const Expr *filler = E->getArrayFiller()) 22194423ac0282acb8ba801eb05b38712438dc0c1e3eArgyrios Kyrtzidis return Visit(filler); 2220363ff23cfddb51abe4ee4212a6dd3c9b534fcc8bChris Lattner return false; 2221363ff23cfddb51abe4ee4212a6dd3c9b534fcc8bChris Lattner } 2222ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor 22238cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitSizeOfPackExpr(const SizeOfPackExpr *) { return false; } 2224c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump}; 2225c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump 222656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCallclass OpaqueValueEvaluation { 222756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall EvalInfo &info; 222856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall OpaqueValueExpr *opaqueValue; 222956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall 223056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCallpublic: 223156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall OpaqueValueEvaluation(EvalInfo &info, OpaqueValueExpr *opaqueValue, 223256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall Expr *value) 223356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall : info(info), opaqueValue(opaqueValue) { 223456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall 223556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall // If evaluation fails, fail immediately. 22361e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith if (!Evaluate(info.OpaqueValues[opaqueValue], info, value)) { 223756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall this->opaqueValue = 0; 223856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall return; 223956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall } 224056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall } 224156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall 224256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall bool hasError() const { return opaqueValue == 0; } 224356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall 224456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall ~OpaqueValueEvaluation() { 22451e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith // FIXME: This will not work for recursive constexpr functions using opaque 22461e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith // values. Restore the former value. 224756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall if (opaqueValue) info.OpaqueValues.erase(opaqueValue); 224856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall } 224956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall}; 225056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall 2251c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump} // end anonymous namespace 2252c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump 22534efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman//===----------------------------------------------------------------------===// 22548cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne// Generic Evaluation 22558cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne//===----------------------------------------------------------------------===// 22568cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournenamespace { 22578cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 2258f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith// FIXME: RetTy is always bool. Remove it. 2259f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithtemplate <class Derived, typename RetTy=bool> 22608cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourneclass ExprEvaluatorBase 22618cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne : public ConstStmtVisitor<Derived, RetTy> { 22628cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourneprivate: 226347a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith RetTy DerivedSuccess(const CCValue &V, const Expr *E) { 22648cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return static_cast<Derived*>(this)->Success(V, E); 22658cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne } 226651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith RetTy DerivedZeroInitialization(const Expr *E) { 226751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return static_cast<Derived*>(this)->ZeroInitialization(E); 2268f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith } 22698cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 22708cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourneprotected: 22718cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne EvalInfo &Info; 22728cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne typedef ConstStmtVisitor<Derived, RetTy> StmtVisitorTy; 22738cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne typedef ExprEvaluatorBase ExprEvaluatorBaseTy; 22748cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 2275dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { 2276d5093420308784e31071c8cd723a58d74159f393Richard Smith return Info.CCEDiag(E->getExprLoc(), D); 2277f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 2278f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 2279f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith /// Report an evaluation error. This should only be called when an error is 2280f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith /// first discovered. When propagating an error, just return false. 2281f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith bool Error(const Expr *E, diag::kind D) { 2282dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith Info.Diag(E->getExprLoc(), D); 2283f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 2284f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 2285f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith bool Error(const Expr *E) { 2286f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E, diag::note_invalid_subexpr_in_const_expr); 2287f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 2288f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 228951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith RetTy ZeroInitialization(const Expr *E) { return Error(E); } 2290f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith 22918cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournepublic: 22928cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {} 22938cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 22948cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne RetTy VisitStmt(const Stmt *) { 2295b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie llvm_unreachable("Expression evaluator should not be called on stmts"); 22968cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne } 22978cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne RetTy VisitExpr(const Expr *E) { 2298f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 22998cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne } 23008cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 23018cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne RetTy VisitParenExpr(const ParenExpr *E) 23028cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne { return StmtVisitorTy::Visit(E->getSubExpr()); } 23038cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne RetTy VisitUnaryExtension(const UnaryOperator *E) 23048cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne { return StmtVisitorTy::Visit(E->getSubExpr()); } 23058cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne RetTy VisitUnaryPlus(const UnaryOperator *E) 23068cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne { return StmtVisitorTy::Visit(E->getSubExpr()); } 23078cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne RetTy VisitChooseExpr(const ChooseExpr *E) 23088cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne { return StmtVisitorTy::Visit(E->getChosenSubExpr(Info.Ctx)); } 23098cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne RetTy VisitGenericSelectionExpr(const GenericSelectionExpr *E) 23108cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne { return StmtVisitorTy::Visit(E->getResultExpr()); } 231191a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall RetTy VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E) 231291a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall { return StmtVisitorTy::Visit(E->getReplacement()); } 23133d75ca836205856077c18e30e9447accbd85f751Richard Smith RetTy VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) 23143d75ca836205856077c18e30e9447accbd85f751Richard Smith { return StmtVisitorTy::Visit(E->getExpr()); } 2315bc6abe93a5d6b1305411f8b6f54c2caa686ddc69Richard Smith // We cannot create any objects for which cleanups are required, so there is 2316bc6abe93a5d6b1305411f8b6f54c2caa686ddc69Richard Smith // nothing to do here; all cleanups must come from unevaluated subexpressions. 2317bc6abe93a5d6b1305411f8b6f54c2caa686ddc69Richard Smith RetTy VisitExprWithCleanups(const ExprWithCleanups *E) 2318bc6abe93a5d6b1305411f8b6f54c2caa686ddc69Richard Smith { return StmtVisitorTy::Visit(E->getSubExpr()); } 23198cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 2320c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith RetTy VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E) { 2321c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith CCEDiag(E, diag::note_constexpr_invalid_cast) << 0; 2322c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith return static_cast<Derived*>(this)->VisitCastExpr(E); 2323c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith } 2324c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith RetTy VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) { 2325c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith CCEDiag(E, diag::note_constexpr_invalid_cast) << 1; 2326c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith return static_cast<Derived*>(this)->VisitCastExpr(E); 2327c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith } 2328c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith 2329e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith RetTy VisitBinaryOperator(const BinaryOperator *E) { 2330e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith switch (E->getOpcode()) { 2331e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith default: 2332f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 2333e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2334e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case BO_Comma: 2335e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith VisitIgnoredValue(E->getLHS()); 2336e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return StmtVisitorTy::Visit(E->getRHS()); 2337e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2338e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case BO_PtrMemD: 2339e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case BO_PtrMemI: { 2340e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LValue Obj; 2341e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!HandleMemberPointerAccess(Info, E, Obj)) 2342e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 2343e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith CCValue Result; 2344f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!HandleLValueToRValueConversion(Info, E, E->getType(), Obj, Result)) 2345e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 2346e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return DerivedSuccess(Result, E); 2347e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2348e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2349e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2350e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 23518cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne RetTy VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) { 23528cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne OpaqueValueEvaluation opaque(Info, E->getOpaqueValue(), E->getCommon()); 23538cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne if (opaque.hasError()) 2354f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 23558cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 23568cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool cond; 2357c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith if (!EvaluateAsBooleanCondition(E->getCond(), cond, Info)) 2358f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 23598cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 23608cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return StmtVisitorTy::Visit(cond ? E->getTrueExpr() : E->getFalseExpr()); 23618cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne } 23628cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 23638cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne RetTy VisitConditionalOperator(const ConditionalOperator *E) { 2364f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith bool IsBcpCall = false; 2365f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // If the condition (ignoring parens) is a __builtin_constant_p call, 2366f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // the result is a constant expression if it can be folded without 2367f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // side-effects. This is an important GNU extension. See GCC PR38377 2368f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // for discussion. 2369f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (const CallExpr *CallCE = 2370f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith dyn_cast<CallExpr>(E->getCond()->IgnoreParenCasts())) 2371f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p) 2372f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith IsBcpCall = true; 2373f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 2374f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // Always assume __builtin_constant_p(...) ? ... : ... is a potential 2375f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // constant expression; we can't check whether it's potentially foldable. 2376f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (Info.CheckingPotentialConstantExpression && IsBcpCall) 2377f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith return false; 2378f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 2379f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith FoldConstant Fold(Info); 2380f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 23818cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool BoolResult; 2382c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info)) 2383f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 23848cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 2385c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith Expr *EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); 2386f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (!StmtVisitorTy::Visit(EvalExpr)) 2387f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith return false; 2388f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 2389f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (IsBcpCall) 2390f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith Fold.Fold(Info); 2391f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 2392f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith return true; 23938cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne } 23948cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 23958cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne RetTy VisitOpaqueValueExpr(const OpaqueValueExpr *E) { 239647a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith const CCValue *Value = Info.getOpaqueValue(E); 239742786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis if (!Value) { 239842786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis const Expr *Source = E->getSourceExpr(); 239942786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis if (!Source) 2400f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 240142786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis if (Source == E) { // sanity checking. 240242786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis assert(0 && "OpaqueValueExpr recursively refers to itself"); 2403f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 240442786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis } 240542786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis return StmtVisitorTy::Visit(Source); 240642786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis } 240747a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith return DerivedSuccess(*Value, E); 24088cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne } 2409f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith 2410d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith RetTy VisitCallExpr(const CallExpr *E) { 2411e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const Expr *Callee = E->getCallee()->IgnoreParens(); 2412d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith QualType CalleeType = Callee->getType(); 2413d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 24146142ca7790aa09a6e13592b70f142cc4bbcadcaeDevang Patel const FunctionDecl *FD = 0; 241559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith LValue *This = 0, ThisVal; 241659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs()); 241759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith 241859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith // Extract function decl and 'this' pointer from the callee. 241959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith if (CalleeType->isSpecificBuiltinType(BuiltinType::BoundMember)) { 2420f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith const ValueDecl *Member = 0; 2421e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (const MemberExpr *ME = dyn_cast<MemberExpr>(Callee)) { 2422e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // Explicit bound member calls, such as x.f() or p->g(); 2423e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!EvaluateObjectArgument(Info, ME->getBase(), ThisVal)) 2424f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 2425f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith Member = ME->getMemberDecl(); 2426e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith This = &ThisVal; 2427e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(Callee)) { 2428e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // Indirect bound member calls ('.*' or '->*'). 2429f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith Member = HandleMemberPointerAccess(Info, BE, ThisVal, false); 2430f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!Member) return false; 2431e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith This = &ThisVal; 2432e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } else 2433f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(Callee); 2434f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 2435f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith FD = dyn_cast<FunctionDecl>(Member); 2436f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!FD) 2437f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(Callee); 243859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith } else if (CalleeType->isFunctionPointerType()) { 2439b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith LValue Call; 2440b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (!EvaluatePointer(Callee, Call, Info)) 2441f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 244259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith 2443b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (!Call.getLValueOffset().isZero()) 2444f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(Callee); 24451bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith FD = dyn_cast_or_null<FunctionDecl>( 24461bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith Call.getLValueBase().dyn_cast<const ValueDecl*>()); 244759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith if (!FD) 2448f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(Callee); 244959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith 245059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith // Overloaded operator calls to member functions are represented as normal 245159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith // calls with '*this' as the first argument. 245259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); 245359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith if (MD && !MD->isStatic()) { 2454f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith // FIXME: When selecting an implicit conversion for an overloaded 2455f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith // operator delete, we sometimes try to evaluate calls to conversion 2456f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith // operators without a 'this' parameter! 2457f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (Args.empty()) 2458f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 2459f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 246059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith if (!EvaluateObjectArgument(Info, Args[0], ThisVal)) 246159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith return false; 246259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith This = &ThisVal; 246359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith Args = Args.slice(1); 246459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith } 2465d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 246659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith // Don't call function pointers which have been cast to some other type. 246759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith if (!Info.Ctx.hasSameType(CalleeType->getPointeeType(), FD->getType())) 2468f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 246959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith } else 2470f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 2471d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 2472b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith if (This && !This->checkSubobject(Info, E, CSK_This)) 2473b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith return false; 2474b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith 2475c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const FunctionDecl *Definition = 0; 2476d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith Stmt *Body = FD->getBody(Definition); 247769c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith APValue Result; 2478d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 2479c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition) || 2480745f5147e065900267c85a5568785a1991d4838fRichard Smith !HandleFunctionCall(E->getExprLoc(), Definition, This, Args, Body, 2481745f5147e065900267c85a5568785a1991d4838fRichard Smith Info, Result)) 2482f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 2483d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 2484b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return DerivedSuccess(CCValue(Info.Ctx, Result, CCValue::GlobalValue()), E); 2485d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith } 2486d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 2487c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith RetTy VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { 2488c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return StmtVisitorTy::Visit(E->getInitializer()); 2489c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith } 2490f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith RetTy VisitInitListExpr(const InitListExpr *E) { 249171523d6c41e1599fc42f420d02dd2895fd8f65d4Eli Friedman if (E->getNumInits() == 0) 249271523d6c41e1599fc42f420d02dd2895fd8f65d4Eli Friedman return DerivedZeroInitialization(E); 249371523d6c41e1599fc42f420d02dd2895fd8f65d4Eli Friedman if (E->getNumInits() == 1) 249471523d6c41e1599fc42f420d02dd2895fd8f65d4Eli Friedman return StmtVisitorTy::Visit(E->getInit(0)); 2495f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 2496f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith } 2497f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith RetTy VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { 249851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return DerivedZeroInitialization(E); 2499f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith } 2500f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith RetTy VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) { 250151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return DerivedZeroInitialization(E); 2502f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith } 2503e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith RetTy VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) { 250451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return DerivedZeroInitialization(E); 2505e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2506f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith 2507180f47959a066795cc0f409433023af448bb0328Richard Smith /// A member expression where the object is a prvalue is itself a prvalue. 2508180f47959a066795cc0f409433023af448bb0328Richard Smith RetTy VisitMemberExpr(const MemberExpr *E) { 2509180f47959a066795cc0f409433023af448bb0328Richard Smith assert(!E->isArrow() && "missing call to bound member function?"); 2510180f47959a066795cc0f409433023af448bb0328Richard Smith 2511180f47959a066795cc0f409433023af448bb0328Richard Smith CCValue Val; 2512180f47959a066795cc0f409433023af448bb0328Richard Smith if (!Evaluate(Val, Info, E->getBase())) 2513180f47959a066795cc0f409433023af448bb0328Richard Smith return false; 2514180f47959a066795cc0f409433023af448bb0328Richard Smith 2515180f47959a066795cc0f409433023af448bb0328Richard Smith QualType BaseTy = E->getBase()->getType(); 2516180f47959a066795cc0f409433023af448bb0328Richard Smith 2517180f47959a066795cc0f409433023af448bb0328Richard Smith const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); 2518f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!FD) return Error(E); 2519180f47959a066795cc0f409433023af448bb0328Richard Smith assert(!FD->getType()->isReferenceType() && "prvalue reference?"); 2520180f47959a066795cc0f409433023af448bb0328Richard Smith assert(BaseTy->getAs<RecordType>()->getDecl()->getCanonicalDecl() == 2521180f47959a066795cc0f409433023af448bb0328Richard Smith FD->getParent()->getCanonicalDecl() && "record / field mismatch"); 2522180f47959a066795cc0f409433023af448bb0328Richard Smith 2523b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith SubobjectDesignator Designator(BaseTy); 2524b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Designator.addDeclUnchecked(FD); 2525180f47959a066795cc0f409433023af448bb0328Richard Smith 2526f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return ExtractSubobject(Info, E, Val, BaseTy, Designator, E->getType()) && 2527180f47959a066795cc0f409433023af448bb0328Richard Smith DerivedSuccess(Val, E); 2528180f47959a066795cc0f409433023af448bb0328Richard Smith } 2529180f47959a066795cc0f409433023af448bb0328Richard Smith 2530c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith RetTy VisitCastExpr(const CastExpr *E) { 2531c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith switch (E->getCastKind()) { 2532c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith default: 2533c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith break; 2534c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 25357a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall case CK_AtomicToNonAtomic: 25367a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall case CK_NonAtomicToAtomic: 2537c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case CK_NoOp: 25387d580a4e9e47dffc3c17aa2b957ac57ca3c4e451Richard Smith case CK_UserDefinedConversion: 2539c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return StmtVisitorTy::Visit(E->getSubExpr()); 2540c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 2541c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case CK_LValueToRValue: { 2542c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith LValue LVal; 2543f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!EvaluateLValue(E->getSubExpr(), LVal, Info)) 2544f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 2545f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith CCValue RVal; 25469ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith // Note, we use the subexpression's type in order to retain cv-qualifiers. 25479ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith if (!HandleLValueToRValueConversion(Info, E, E->getSubExpr()->getType(), 25489ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith LVal, RVal)) 2549f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 2550f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return DerivedSuccess(RVal, E); 2551c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith } 2552c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith } 2553c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 2554f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 2555c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith } 2556c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 25578327fad71da34492d82c532f42a58cb4baff81a3Richard Smith /// Visit a value which is evaluated, but whose value is ignored. 25588327fad71da34492d82c532f42a58cb4baff81a3Richard Smith void VisitIgnoredValue(const Expr *E) { 255947a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith CCValue Scratch; 25608327fad71da34492d82c532f42a58cb4baff81a3Richard Smith if (!Evaluate(Scratch, Info, E)) 25618327fad71da34492d82c532f42a58cb4baff81a3Richard Smith Info.EvalStatus.HasSideEffects = true; 25628327fad71da34492d82c532f42a58cb4baff81a3Richard Smith } 25638cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne}; 25648cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 25658cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne} 25668cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 25678cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne//===----------------------------------------------------------------------===// 2568e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// Common base class for lvalue and temporary evaluation. 2569e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===// 2570e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithnamespace { 2571e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithtemplate<class Derived> 2572e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithclass LValueExprEvaluatorBase 2573e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith : public ExprEvaluatorBase<Derived, bool> { 2574e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithprotected: 2575e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LValue &Result; 2576e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith typedef LValueExprEvaluatorBase LValueExprEvaluatorBaseTy; 2577e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith typedef ExprEvaluatorBase<Derived, bool> ExprEvaluatorBaseTy; 2578e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2579e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool Success(APValue::LValueBase B) { 2580e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Result.set(B); 2581e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 2582e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2583e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2584e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithpublic: 2585e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LValueExprEvaluatorBase(EvalInfo &Info, LValue &Result) : 2586e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith ExprEvaluatorBaseTy(Info), Result(Result) {} 2587e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2588e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool Success(const CCValue &V, const Expr *E) { 2589e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Result.setFrom(V); 2590e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 2591e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2592e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2593e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool VisitMemberExpr(const MemberExpr *E) { 2594e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // Handle non-static data members. 2595e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith QualType BaseTy; 2596e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (E->isArrow()) { 2597e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!EvaluatePointer(E->getBase(), Result, this->Info)) 2598e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 2599e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith BaseTy = E->getBase()->getType()->getAs<PointerType>()->getPointeeType(); 2600c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } else if (E->getBase()->isRValue()) { 2601af2c7a194592401394233b7cbcdd3cfd0a7a38ddRichard Smith assert(E->getBase()->getType()->isRecordType()); 2602c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (!EvaluateTemporary(E->getBase(), Result, this->Info)) 2603c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return false; 2604c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith BaseTy = E->getBase()->getType(); 2605e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } else { 2606e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!this->Visit(E->getBase())) 2607e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 2608e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith BaseTy = E->getBase()->getType(); 2609e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2610e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2611d9b02e726262e4009dda830998bb934172ac0020Richard Smith const ValueDecl *MD = E->getMemberDecl(); 2612d9b02e726262e4009dda830998bb934172ac0020Richard Smith if (const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) { 2613d9b02e726262e4009dda830998bb934172ac0020Richard Smith assert(BaseTy->getAs<RecordType>()->getDecl()->getCanonicalDecl() == 2614d9b02e726262e4009dda830998bb934172ac0020Richard Smith FD->getParent()->getCanonicalDecl() && "record / field mismatch"); 2615d9b02e726262e4009dda830998bb934172ac0020Richard Smith (void)BaseTy; 2616d9b02e726262e4009dda830998bb934172ac0020Richard Smith HandleLValueMember(this->Info, E, Result, FD); 2617d9b02e726262e4009dda830998bb934172ac0020Richard Smith } else if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(MD)) { 2618d9b02e726262e4009dda830998bb934172ac0020Richard Smith HandleLValueIndirectMember(this->Info, E, Result, IFD); 2619d9b02e726262e4009dda830998bb934172ac0020Richard Smith } else 2620d9b02e726262e4009dda830998bb934172ac0020Richard Smith return this->Error(E); 2621e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2622d9b02e726262e4009dda830998bb934172ac0020Richard Smith if (MD->getType()->isReferenceType()) { 2623e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith CCValue RefValue; 2624d9b02e726262e4009dda830998bb934172ac0020Richard Smith if (!HandleLValueToRValueConversion(this->Info, E, MD->getType(), Result, 2625e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith RefValue)) 2626e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 2627e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return Success(RefValue, E); 2628e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2629e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 2630e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2631e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2632e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool VisitBinaryOperator(const BinaryOperator *E) { 2633e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith switch (E->getOpcode()) { 2634e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith default: 2635e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return ExprEvaluatorBaseTy::VisitBinaryOperator(E); 2636e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2637e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case BO_PtrMemD: 2638e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case BO_PtrMemI: 2639e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return HandleMemberPointerAccess(this->Info, E, Result); 2640e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2641e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2642e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2643e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool VisitCastExpr(const CastExpr *E) { 2644e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith switch (E->getCastKind()) { 2645e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith default: 2646e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return ExprEvaluatorBaseTy::VisitCastExpr(E); 2647e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2648e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case CK_DerivedToBase: 2649e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case CK_UncheckedDerivedToBase: { 2650e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!this->Visit(E->getSubExpr())) 2651e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 2652e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2653e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // Now figure out the necessary offset to add to the base LV to get from 2654e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // the derived class to the base class. 2655e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith QualType Type = E->getSubExpr()->getType(); 2656e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2657e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith for (CastExpr::path_const_iterator PathI = E->path_begin(), 2658e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith PathE = E->path_end(); PathI != PathE; ++PathI) { 2659b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (!HandleLValueBase(this->Info, E, Result, Type->getAsCXXRecordDecl(), 2660e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith *PathI)) 2661e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 2662e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Type = (*PathI)->getType(); 2663e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2664e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2665e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 2666e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2667e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2668e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2669e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}; 2670e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith} 2671e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2672e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===// 26734efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman// LValue Evaluation 2674c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// 2675c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// This is used for evaluating lvalues (in C and C++), xvalues (in C++11), 2676c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// function designators (in C), decl references to void objects (in C), and 2677c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// temporaries (if building with -Wno-address-of-temporary). 2678c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// 2679c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// LValue evaluation produces values comprising a base expression of one of the 2680c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// following types: 26811bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith// - Declarations 26821bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith// * VarDecl 26831bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith// * FunctionDecl 26841bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith// - Literals 2685c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// * CompoundLiteralExpr in C 2686c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// * StringLiteral 268747d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith// * CXXTypeidExpr 2688c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// * PredefinedExpr 2689180f47959a066795cc0f409433023af448bb0328Richard Smith// * ObjCStringLiteralExpr 2690c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// * ObjCEncodeExpr 2691c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// * AddrLabelExpr 2692c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// * BlockExpr 2693c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// * CallExpr for a MakeStringConstant builtin 26941bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith// - Locals and temporaries 26951bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith// * Any Expr, with a Frame indicating the function in which the temporary was 26961bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith// evaluated. 26971bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith// plus an offset in bytes. 26984efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman//===----------------------------------------------------------------------===// 26994efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedmannamespace { 2700770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass LValueExprEvaluator 2701e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith : public LValueExprEvaluatorBase<LValueExprEvaluator> { 27024efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedmanpublic: 2703e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LValueExprEvaluator(EvalInfo &Info, LValue &Result) : 2704e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LValueExprEvaluatorBaseTy(Info, Result) {} 27051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2706c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith bool VisitVarDecl(const Expr *E, const VarDecl *VD); 2707c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 27088cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitDeclRefExpr(const DeclRefExpr *E); 27098cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(E); } 2710bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E); 27118cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); 27128cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitMemberExpr(const MemberExpr *E); 27138cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitStringLiteral(const StringLiteral *E) { return Success(E); } 27148cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(E); } 271547d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith bool VisitCXXTypeidExpr(const CXXTypeidExpr *E); 27168cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E); 27178cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitUnaryDeref(const UnaryOperator *E); 27188cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 27198cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitCastExpr(const CastExpr *E) { 272026bc220377705292a0519a71d3ea3aef68fcfec6Anders Carlsson switch (E->getCastKind()) { 272126bc220377705292a0519a71d3ea3aef68fcfec6Anders Carlsson default: 2722e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return LValueExprEvaluatorBaseTy::VisitCastExpr(E); 272326bc220377705292a0519a71d3ea3aef68fcfec6Anders Carlsson 2724db924224b51b153f24fbe492102d4edebcbbb7f4Eli Friedman case CK_LValueBitCast: 2725c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith this->CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; 27260a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith if (!Visit(E->getSubExpr())) 27270a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith return false; 27280a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith Result.Designator.setInvalid(); 27290a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith return true; 2730db924224b51b153f24fbe492102d4edebcbbb7f4Eli Friedman 2731e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case CK_BaseToDerived: 2732180f47959a066795cc0f409433023af448bb0328Richard Smith if (!Visit(E->getSubExpr())) 2733180f47959a066795cc0f409433023af448bb0328Richard Smith return false; 2734e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return HandleBaseToDerivedCast(Info, E, Result); 273526bc220377705292a0519a71d3ea3aef68fcfec6Anders Carlsson } 273626bc220377705292a0519a71d3ea3aef68fcfec6Anders Carlsson } 2737cea8d966f826554f0679595e9371e314e8dbc1cfSebastian Redl 2738ba98d6bb414861965a1f22628494ea046785ecd4Eli Friedman // FIXME: Missing: __real__, __imag__ 27398cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 27404efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman}; 27414efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman} // end anonymous namespace 27424efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 2743c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// Evaluate an expression as an lvalue. This can be legitimately called on 2744c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// expressions which are not glvalues, in a few cases: 2745c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// * function designators in C, 2746c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// * "extern void" objects, 2747c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// * temporaries, if building with -Wno-address-of-temporary. 2748efdb83e26f9a1fd2566afe54461216cd84814d42John McCallstatic bool EvaluateLValue(const Expr* E, LValue& Result, EvalInfo &Info) { 2749c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith assert((E->isGLValue() || E->getType()->isFunctionType() || 2750c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith E->getType()->isVoidType() || isa<CXXTemporaryObjectExpr>(E)) && 2751c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith "can't evaluate expression as an lvalue"); 27528cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return LValueExprEvaluator(Info, Result).Visit(E); 27534efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman} 27544efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 27558cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) { 27561bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl())) 27571bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith return Success(FD); 27581bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith if (const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) 2759c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return VisitVarDecl(E, VD); 2760c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return Error(E); 2761c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith} 2762436c8898cd1c93c5bacd3fcc4ac586bc5cd77062Richard Smith 2763c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smithbool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) { 2764177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith if (!VD->getType()->isReferenceType()) { 2765177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith if (isa<ParmVarDecl>(VD)) { 27661bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith Result.set(VD, Info.CurrentCall); 2767177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith return true; 2768177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith } 27691bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith return Success(VD); 2770177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith } 277150c39ea4858265f3f5f42a0c624557ce2281936bEli Friedman 277247a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith CCValue V; 2773f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!EvaluateVarDeclInit(Info, E, VD, Info.CurrentCall, V)) 2774f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 2775f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Success(V, E); 277635873c49adad211ff466e34342a52665742794f5Anders Carlsson} 277735873c49adad211ff466e34342a52665742794f5Anders Carlsson 2778bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smithbool LValueExprEvaluator::VisitMaterializeTemporaryExpr( 2779bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith const MaterializeTemporaryExpr *E) { 2780e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (E->GetTemporaryExpr()->isRValue()) { 2781af2c7a194592401394233b7cbcdd3cfd0a7a38ddRichard Smith if (E->getType()->isRecordType()) 2782e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return EvaluateTemporary(E->GetTemporaryExpr(), Result, Info); 2783e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2784e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Result.set(E, Info.CurrentCall); 2785e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return EvaluateConstantExpression(Info.CurrentCall->Temporaries[E], Info, 2786e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Result, E->GetTemporaryExpr()); 2787e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2788e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2789e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // Materialization of an lvalue temporary occurs when we need to force a copy 2790e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // (for instance, if it's a bitfield). 2791e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // FIXME: The AST should contain an lvalue-to-rvalue node for such cases. 2792e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!Visit(E->GetTemporaryExpr())) 2793e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 2794f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!HandleLValueToRValueConversion(Info, E, E->getType(), Result, 2795e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Info.CurrentCall->Temporaries[E])) 2796e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 27971bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith Result.set(E, Info.CurrentCall); 2798e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 2799bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith} 2800bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith 28018cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool 28028cad3046be06ea73ff8892d947697a21d7a440d3Peter CollingbourneLValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { 2803c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?"); 2804c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // Defer visiting the literal until the lvalue-to-rvalue conversion. We can 2805c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // only see this when folding in C, so there's no standard to follow here. 2806efdb83e26f9a1fd2566afe54461216cd84814d42John McCall return Success(E); 28074efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman} 28084efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 280947d2145675099893d702be4bc06bd9f26d8ddd13Richard Smithbool LValueExprEvaluator::VisitCXXTypeidExpr(const CXXTypeidExpr *E) { 281047d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith if (E->isTypeOperand()) 281147d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith return Success(E); 281247d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith CXXRecordDecl *RD = E->getExprOperand()->getType()->getAsCXXRecordDecl(); 281347d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith if (RD && RD->isPolymorphic()) { 281447d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith Info.Diag(E->getExprLoc(), diag::note_constexpr_typeid_polymorphic) 281547d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith << E->getExprOperand()->getType() 281647d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith << E->getExprOperand()->getSourceRange(); 281747d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith return false; 281847d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith } 281947d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith return Success(E); 282047d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith} 282147d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith 28228cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) { 2823c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // Handle static data members. 2824c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith if (const VarDecl *VD = dyn_cast<VarDecl>(E->getMemberDecl())) { 2825c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith VisitIgnoredValue(E->getBase()); 2826c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return VisitVarDecl(E, VD); 2827c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith } 2828c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 2829d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith // Handle static member functions. 2830d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl())) { 2831d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith if (MD->isStatic()) { 2832d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith VisitIgnoredValue(E->getBase()); 28331bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith return Success(MD); 2834d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith } 2835d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith } 2836d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 2837180f47959a066795cc0f409433023af448bb0328Richard Smith // Handle non-static data members. 2838e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return LValueExprEvaluatorBaseTy::VisitMemberExpr(E); 28394efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman} 28404efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 28418cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) { 2842c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // FIXME: Deal with vectors as array subscript bases. 2843c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith if (E->getBase()->getType()->isVectorType()) 2844f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 2845c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 28463068d117951a8df54bae9db039b56201ab10962bAnders Carlsson if (!EvaluatePointer(E->getBase(), Result, Info)) 2847efdb83e26f9a1fd2566afe54461216cd84814d42John McCall return false; 28481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 28493068d117951a8df54bae9db039b56201ab10962bAnders Carlsson APSInt Index; 28503068d117951a8df54bae9db039b56201ab10962bAnders Carlsson if (!EvaluateInteger(E->getIdx(), Index, Info)) 2851efdb83e26f9a1fd2566afe54461216cd84814d42John McCall return false; 2852180f47959a066795cc0f409433023af448bb0328Richard Smith int64_t IndexValue 2853180f47959a066795cc0f409433023af448bb0328Richard Smith = Index.isSigned() ? Index.getSExtValue() 2854180f47959a066795cc0f409433023af448bb0328Richard Smith : static_cast<int64_t>(Index.getZExtValue()); 28553068d117951a8df54bae9db039b56201ab10962bAnders Carlsson 2856b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return HandleLValueArrayAdjustment(Info, E, Result, E->getType(), IndexValue); 28573068d117951a8df54bae9db039b56201ab10962bAnders Carlsson} 28584efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 28598cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) { 2860efdb83e26f9a1fd2566afe54461216cd84814d42John McCall return EvaluatePointer(E->getSubExpr(), Result, Info); 2861e8761c8fe2ee6b628104a0885f49fd3c21c08a4fEli Friedman} 2862e8761c8fe2ee6b628104a0885f49fd3c21c08a4fEli Friedman 28634efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman//===----------------------------------------------------------------------===// 2864f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner// Pointer Evaluation 2865f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===// 2866f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner 2867c754aa62643e66ab967ca32ae8b0b3fc419bba25Anders Carlssonnamespace { 2868770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass PointerExprEvaluator 28698cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne : public ExprEvaluatorBase<PointerExprEvaluator, bool> { 2870efdb83e26f9a1fd2566afe54461216cd84814d42John McCall LValue &Result; 2871efdb83e26f9a1fd2566afe54461216cd84814d42John McCall 28728cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool Success(const Expr *E) { 28731bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith Result.set(E); 2874efdb83e26f9a1fd2566afe54461216cd84814d42John McCall return true; 2875efdb83e26f9a1fd2566afe54461216cd84814d42John McCall } 28762bad1687fe6f00e10767a691a33b070b151902b6Anders Carlssonpublic: 28771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2878efdb83e26f9a1fd2566afe54461216cd84814d42John McCall PointerExprEvaluator(EvalInfo &info, LValue &Result) 28798cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne : ExprEvaluatorBaseTy(info), Result(Result) {} 2880f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner 288147a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith bool Success(const CCValue &V, const Expr *E) { 28828cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne Result.setFrom(V); 28838cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return true; 28842bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson } 288551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith bool ZeroInitialization(const Expr *E) { 2886f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith return Success((Expr*)0); 2887f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith } 28882bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson 2889efdb83e26f9a1fd2566afe54461216cd84814d42John McCall bool VisitBinaryOperator(const BinaryOperator *E); 28908cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitCastExpr(const CastExpr* E); 2891efdb83e26f9a1fd2566afe54461216cd84814d42John McCall bool VisitUnaryAddrOf(const UnaryOperator *E); 28928cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitObjCStringLiteral(const ObjCStringLiteral *E) 2893efdb83e26f9a1fd2566afe54461216cd84814d42John McCall { return Success(E); } 28948cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitAddrLabelExpr(const AddrLabelExpr *E) 2895efdb83e26f9a1fd2566afe54461216cd84814d42John McCall { return Success(E); } 28968cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitCallExpr(const CallExpr *E); 28978cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitBlockExpr(const BlockExpr *E) { 2898469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall if (!E->getBlockDecl()->hasCaptures()) 2899efdb83e26f9a1fd2566afe54461216cd84814d42John McCall return Success(E); 2900f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 2901b83d287bc7f47d36fb0751a481e2ef9308b37252Mike Stump } 2902180f47959a066795cc0f409433023af448bb0328Richard Smith bool VisitCXXThisExpr(const CXXThisExpr *E) { 2903180f47959a066795cc0f409433023af448bb0328Richard Smith if (!Info.CurrentCall->This) 2904f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 2905180f47959a066795cc0f409433023af448bb0328Richard Smith Result = *Info.CurrentCall->This; 2906180f47959a066795cc0f409433023af448bb0328Richard Smith return true; 2907180f47959a066795cc0f409433023af448bb0328Richard Smith } 290856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall 2909ba98d6bb414861965a1f22628494ea046785ecd4Eli Friedman // FIXME: Missing: @protocol, @selector 29102bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson}; 2911f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner} // end anonymous namespace 29122bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson 2913efdb83e26f9a1fd2566afe54461216cd84814d42John McCallstatic bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info) { 2914c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith assert(E->isRValue() && E->getType()->hasPointerRepresentation()); 29158cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return PointerExprEvaluator(Info, Result).Visit(E); 2916f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner} 2917650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson 2918efdb83e26f9a1fd2566afe54461216cd84814d42John McCallbool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { 29192de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (E->getOpcode() != BO_Add && 29202de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall E->getOpcode() != BO_Sub) 2921e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return ExprEvaluatorBaseTy::VisitBinaryOperator(E); 29221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2923650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson const Expr *PExp = E->getLHS(); 2924650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson const Expr *IExp = E->getRHS(); 2925650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson if (IExp->getType()->isPointerType()) 2926f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner std::swap(PExp, IExp); 29271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2928745f5147e065900267c85a5568785a1991d4838fRichard Smith bool EvalPtrOK = EvaluatePointer(PExp, Result, Info); 2929745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!EvalPtrOK && !Info.keepEvaluatingAfterFailure()) 2930efdb83e26f9a1fd2566afe54461216cd84814d42John McCall return false; 29311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2932efdb83e26f9a1fd2566afe54461216cd84814d42John McCall llvm::APSInt Offset; 2933745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!EvaluateInteger(IExp, Offset, Info) || !EvalPtrOK) 2934efdb83e26f9a1fd2566afe54461216cd84814d42John McCall return false; 2935efdb83e26f9a1fd2566afe54461216cd84814d42John McCall int64_t AdditionalOffset 2936efdb83e26f9a1fd2566afe54461216cd84814d42John McCall = Offset.isSigned() ? Offset.getSExtValue() 2937efdb83e26f9a1fd2566afe54461216cd84814d42John McCall : static_cast<int64_t>(Offset.getZExtValue()); 29380a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith if (E->getOpcode() == BO_Sub) 29390a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith AdditionalOffset = -AdditionalOffset; 2940650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson 2941180f47959a066795cc0f409433023af448bb0328Richard Smith QualType Pointee = PExp->getType()->getAs<PointerType>()->getPointeeType(); 2942b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return HandleLValueArrayAdjustment(Info, E, Result, Pointee, 2943b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith AdditionalOffset); 2944650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson} 29454efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 2946efdb83e26f9a1fd2566afe54461216cd84814d42John McCallbool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { 29472fd5983e0da447291a651a347c206aee37a1de5fRichard Smith QualType SrcTy = E->getSubExpr()->getType(); 29482fd5983e0da447291a651a347c206aee37a1de5fRichard Smith // In C++, taking the address of an object of incomplete class type has 29492fd5983e0da447291a651a347c206aee37a1de5fRichard Smith // undefined behavior if the complete class type has an overloaded operator&. 29502fd5983e0da447291a651a347c206aee37a1de5fRichard Smith // DR1458 makes such expressions non-constant. 29512fd5983e0da447291a651a347c206aee37a1de5fRichard Smith if (Info.getLangOpts().CPlusPlus && 29522fd5983e0da447291a651a347c206aee37a1de5fRichard Smith SrcTy->isRecordType() && SrcTy->isIncompleteType()) { 29532fd5983e0da447291a651a347c206aee37a1de5fRichard Smith const RecordType *RT = SrcTy->getAs<RecordType>(); 29542fd5983e0da447291a651a347c206aee37a1de5fRichard Smith Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_addr_of_incomplete, 1) 29552fd5983e0da447291a651a347c206aee37a1de5fRichard Smith << SrcTy; 29562fd5983e0da447291a651a347c206aee37a1de5fRichard Smith Info.Note(RT->getDecl()->getLocation(), diag::note_forward_declaration) 29572fd5983e0da447291a651a347c206aee37a1de5fRichard Smith << RT->getDecl(); 29582fd5983e0da447291a651a347c206aee37a1de5fRichard Smith } 2959efdb83e26f9a1fd2566afe54461216cd84814d42John McCall return EvaluateLValue(E->getSubExpr(), Result, Info); 29604efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman} 29611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 29628cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool PointerExprEvaluator::VisitCastExpr(const CastExpr* E) { 29638cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne const Expr* SubExpr = E->getSubExpr(); 2964650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson 296509a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman switch (E->getCastKind()) { 296609a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman default: 296709a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman break; 296809a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman 29692de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case CK_BitCast: 29701d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall case CK_CPointerToObjCPointerCast: 29711d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall case CK_BlockPointerToObjCPointerCast: 29722de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case CK_AnyPointerToBlockPointerCast: 297328c1ce789322ab99f9b5887015d63ec5f088957aRichard Smith if (!Visit(SubExpr)) 297428c1ce789322ab99f9b5887015d63ec5f088957aRichard Smith return false; 2975c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith // Bitcasts to cv void* are static_casts, not reinterpret_casts, so are 2976c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith // permitted in constant expressions in C++11. Bitcasts from cv void* are 2977c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith // also static_casts, but we disallow them as a resolution to DR1312. 29784cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith if (!E->getType()->isVoidPointerType()) { 297928c1ce789322ab99f9b5887015d63ec5f088957aRichard Smith Result.Designator.setInvalid(); 29804cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith if (SubExpr->getType()->isVoidPointerType()) 29814cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith CCEDiag(E, diag::note_constexpr_invalid_cast) 29824cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith << 3 << SubExpr->getType(); 29834cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith else 29844cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; 29854cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith } 29860a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith return true; 298709a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman 29885c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson case CK_DerivedToBase: 29895c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson case CK_UncheckedDerivedToBase: { 299047a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith if (!EvaluatePointer(E->getSubExpr(), Result, Info)) 29915c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson return false; 2992e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!Result.Base && Result.Offset.isZero()) 2993e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 29945c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson 2995180f47959a066795cc0f409433023af448bb0328Richard Smith // Now figure out the necessary offset to add to the base LV to get from 29965c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson // the derived class to the base class. 2997180f47959a066795cc0f409433023af448bb0328Richard Smith QualType Type = 2998180f47959a066795cc0f409433023af448bb0328Richard Smith E->getSubExpr()->getType()->castAs<PointerType>()->getPointeeType(); 29995c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson 3000180f47959a066795cc0f409433023af448bb0328Richard Smith for (CastExpr::path_const_iterator PathI = E->path_begin(), 30015c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson PathE = E->path_end(); PathI != PathE; ++PathI) { 3002b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (!HandleLValueBase(Info, E, Result, Type->getAsCXXRecordDecl(), 3003b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith *PathI)) 30045c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson return false; 3005180f47959a066795cc0f409433023af448bb0328Richard Smith Type = (*PathI)->getType(); 30065c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson } 30075c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson 30085c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson return true; 30095c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson } 30105c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson 3011e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case CK_BaseToDerived: 3012e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!Visit(E->getSubExpr())) 3013e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 3014e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!Result.Base && Result.Offset.isZero()) 3015e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 3016e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return HandleBaseToDerivedCast(Info, E, Result); 3017e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 301847a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith case CK_NullToPointer: 301951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return ZeroInitialization(E); 3020404cd1669c3ba138a9ae0a619bd689cce5aae271John McCall 30212de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case CK_IntegralToPointer: { 3022c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; 3023c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith 302447a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith CCValue Value; 3025efdb83e26f9a1fd2566afe54461216cd84814d42John McCall if (!EvaluateIntegerOrLValue(SubExpr, Value, Info)) 302609a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman break; 302769ab26a8623141f35e86817cfc6e0fbe7639a40fDaniel Dunbar 3028efdb83e26f9a1fd2566afe54461216cd84814d42John McCall if (Value.isInt()) { 302947a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith unsigned Size = Info.Ctx.getTypeSize(E->getType()); 303047a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith uint64_t N = Value.getInt().extOrTrunc(Size).getZExtValue(); 30311bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith Result.Base = (Expr*)0; 303247a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith Result.Offset = CharUnits::fromQuantity(N); 3033177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith Result.Frame = 0; 30340a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith Result.Designator.setInvalid(); 3035efdb83e26f9a1fd2566afe54461216cd84814d42John McCall return true; 3036efdb83e26f9a1fd2566afe54461216cd84814d42John McCall } else { 3037efdb83e26f9a1fd2566afe54461216cd84814d42John McCall // Cast is of an lvalue, no need to change value. 303847a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith Result.setFrom(Value); 3039efdb83e26f9a1fd2566afe54461216cd84814d42John McCall return true; 3040650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson } 3041650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson } 30422de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case CK_ArrayToPointerDecay: 3043e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (SubExpr->isGLValue()) { 3044e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!EvaluateLValue(SubExpr, Result, Info)) 3045e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 3046e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } else { 3047e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Result.set(SubExpr, Info.CurrentCall); 3048e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!EvaluateConstantExpression(Info.CurrentCall->Temporaries[SubExpr], 3049e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Info, Result, SubExpr)) 3050e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 3051e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 30520a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith // The result is a pointer to the first element of the array. 3053b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (const ConstantArrayType *CAT 3054b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith = Info.Ctx.getAsConstantArrayType(SubExpr->getType())) 3055b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Result.addArray(Info, E, CAT); 3056b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith else 3057b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Result.Designator.setInvalid(); 30580a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith return true; 30596a7c94af983717e2c2d6aebe42cb4737c1c7b9e6Richard Smith 30602de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case CK_FunctionToPointerDecay: 30616a7c94af983717e2c2d6aebe42cb4737c1c7b9e6Richard Smith return EvaluateLValue(SubExpr, Result, Info); 30624efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman } 30634efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 3064c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return ExprEvaluatorBaseTy::VisitCastExpr(E); 30651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump} 3066650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson 30678cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) { 3068180f47959a066795cc0f409433023af448bb0328Richard Smith if (IsStringLiteralCall(E)) 3069efdb83e26f9a1fd2566afe54461216cd84814d42John McCall return Success(E); 307056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall 30718cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return ExprEvaluatorBaseTy::VisitCallExpr(E); 30724efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman} 3073f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner 3074f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===// 3075e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// Member Pointer Evaluation 3076e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===// 3077e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3078e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithnamespace { 3079e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithclass MemberPointerExprEvaluator 3080e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith : public ExprEvaluatorBase<MemberPointerExprEvaluator, bool> { 3081e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith MemberPtr &Result; 3082e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3083e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool Success(const ValueDecl *D) { 3084e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Result = MemberPtr(D); 3085e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 3086e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 3087e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithpublic: 3088e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3089e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith MemberPointerExprEvaluator(EvalInfo &Info, MemberPtr &Result) 3090e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith : ExprEvaluatorBaseTy(Info), Result(Result) {} 3091e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3092e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool Success(const CCValue &V, const Expr *E) { 3093e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Result.setFrom(V); 3094e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 3095e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 309651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith bool ZeroInitialization(const Expr *E) { 3097e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return Success((const ValueDecl*)0); 3098e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 3099e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3100e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool VisitCastExpr(const CastExpr *E); 3101e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool VisitUnaryAddrOf(const UnaryOperator *E); 3102e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}; 3103e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith} // end anonymous namespace 3104e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3105e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, 3106e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith EvalInfo &Info) { 3107e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith assert(E->isRValue() && E->getType()->isMemberPointerType()); 3108e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return MemberPointerExprEvaluator(Info, Result).Visit(E); 3109e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith} 3110e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3111e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithbool MemberPointerExprEvaluator::VisitCastExpr(const CastExpr *E) { 3112e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith switch (E->getCastKind()) { 3113e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith default: 3114e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return ExprEvaluatorBaseTy::VisitCastExpr(E); 3115e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3116e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case CK_NullToMemberPointer: 311751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return ZeroInitialization(E); 3118e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3119e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case CK_BaseToDerivedMemberPointer: { 3120e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!Visit(E->getSubExpr())) 3121e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 3122e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (E->path_empty()) 3123e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 3124e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // Base-to-derived member pointer casts store the path in derived-to-base 3125e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // order, so iterate backwards. The CXXBaseSpecifier also provides us with 3126e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // the wrong end of the derived->base arc, so stagger the path by one class. 3127e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter; 3128e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin()); 3129e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith PathI != PathE; ++PathI) { 3130e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); 3131e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const CXXRecordDecl *Derived = (*PathI)->getType()->getAsCXXRecordDecl(); 3132e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!Result.castToDerived(Derived)) 3133f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 3134e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 3135e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const Type *FinalTy = E->getType()->castAs<MemberPointerType>()->getClass(); 3136e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!Result.castToDerived(FinalTy->getAsCXXRecordDecl())) 3137f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 3138e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 3139e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 3140e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3141e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case CK_DerivedToBaseMemberPointer: 3142e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!Visit(E->getSubExpr())) 3143e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 3144e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith for (CastExpr::path_const_iterator PathI = E->path_begin(), 3145e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith PathE = E->path_end(); PathI != PathE; ++PathI) { 3146e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); 3147e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); 3148e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!Result.castToBase(Base)) 3149f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 3150e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 3151e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 3152e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 3153e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith} 3154e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3155e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithbool MemberPointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { 3156e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // C++11 [expr.unary.op]p3 has very strict rules on how the address of a 3157e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // member can be formed. 3158e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return Success(cast<DeclRefExpr>(E->getSubExpr())->getDecl()); 3159e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith} 3160e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3161e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===// 3162180f47959a066795cc0f409433023af448bb0328Richard Smith// Record Evaluation 3163180f47959a066795cc0f409433023af448bb0328Richard Smith//===----------------------------------------------------------------------===// 3164180f47959a066795cc0f409433023af448bb0328Richard Smith 3165180f47959a066795cc0f409433023af448bb0328Richard Smithnamespace { 3166180f47959a066795cc0f409433023af448bb0328Richard Smith class RecordExprEvaluator 3167180f47959a066795cc0f409433023af448bb0328Richard Smith : public ExprEvaluatorBase<RecordExprEvaluator, bool> { 3168180f47959a066795cc0f409433023af448bb0328Richard Smith const LValue &This; 3169180f47959a066795cc0f409433023af448bb0328Richard Smith APValue &Result; 3170180f47959a066795cc0f409433023af448bb0328Richard Smith public: 3171180f47959a066795cc0f409433023af448bb0328Richard Smith 3172180f47959a066795cc0f409433023af448bb0328Richard Smith RecordExprEvaluator(EvalInfo &info, const LValue &This, APValue &Result) 3173180f47959a066795cc0f409433023af448bb0328Richard Smith : ExprEvaluatorBaseTy(info), This(This), Result(Result) {} 3174180f47959a066795cc0f409433023af448bb0328Richard Smith 3175180f47959a066795cc0f409433023af448bb0328Richard Smith bool Success(const CCValue &V, const Expr *E) { 3176f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return CheckConstantExpression(Info, E, V, Result); 3177180f47959a066795cc0f409433023af448bb0328Richard Smith } 317851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith bool ZeroInitialization(const Expr *E); 3179180f47959a066795cc0f409433023af448bb0328Richard Smith 318059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith bool VisitCastExpr(const CastExpr *E); 3181180f47959a066795cc0f409433023af448bb0328Richard Smith bool VisitInitListExpr(const InitListExpr *E); 3182180f47959a066795cc0f409433023af448bb0328Richard Smith bool VisitCXXConstructExpr(const CXXConstructExpr *E); 3183180f47959a066795cc0f409433023af448bb0328Richard Smith }; 3184180f47959a066795cc0f409433023af448bb0328Richard Smith} 3185180f47959a066795cc0f409433023af448bb0328Richard Smith 318651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// Perform zero-initialization on an object of non-union class type. 318751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// C++11 [dcl.init]p5: 318851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// To zero-initialize an object or reference of type T means: 318951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// [...] 319051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// -- if T is a (possibly cv-qualified) non-union class type, 319151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// each non-static data member and each base-class subobject is 319251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// zero-initialized 3193b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithstatic bool HandleClassZeroInitialization(EvalInfo &Info, const Expr *E, 3194b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith const RecordDecl *RD, 319551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith const LValue &This, APValue &Result) { 319651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith assert(!RD->isUnion() && "Expected non-union class type"); 319751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD); 319851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith Result = APValue(APValue::UninitStruct(), CD ? CD->getNumBases() : 0, 319951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith std::distance(RD->field_begin(), RD->field_end())); 320051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 320151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); 320251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 320351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (CD) { 320451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith unsigned Index = 0; 320551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(), 3206b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith End = CD->bases_end(); I != End; ++I, ++Index) { 320751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith const CXXRecordDecl *Base = I->getType()->getAsCXXRecordDecl(); 320851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith LValue Subobject = This; 3209b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith HandleLValueDirectBase(Info, E, Subobject, CD, Base, &Layout); 3210b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (!HandleClassZeroInitialization(Info, E, Base, Subobject, 321151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith Result.getStructBase(Index))) 321251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return false; 321351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith } 321451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith } 321551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 3216b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith for (RecordDecl::field_iterator I = RD->field_begin(), End = RD->field_end(); 3217b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith I != End; ++I) { 321851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith // -- if T is a reference type, no initialization is performed. 321951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if ((*I)->getType()->isReferenceType()) 322051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith continue; 322151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 322251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith LValue Subobject = This; 3223b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith HandleLValueMember(Info, E, Subobject, *I, &Layout); 322451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 322551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith ImplicitValueInitExpr VIE((*I)->getType()); 322651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (!EvaluateConstantExpression( 322751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith Result.getStructField((*I)->getFieldIndex()), Info, Subobject, &VIE)) 322851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return false; 322951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith } 323051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 323151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return true; 323251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith} 323351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 323451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smithbool RecordExprEvaluator::ZeroInitialization(const Expr *E) { 323551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl(); 323651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (RD->isUnion()) { 323751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the 323851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith // object's first non-static named data member is zero-initialized 323951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith RecordDecl::field_iterator I = RD->field_begin(); 324051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (I == RD->field_end()) { 324151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith Result = APValue((const FieldDecl*)0); 324251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return true; 324351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith } 324451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 324551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith LValue Subobject = This; 3246b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith HandleLValueMember(Info, E, Subobject, *I); 324751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith Result = APValue(*I); 324851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith ImplicitValueInitExpr VIE((*I)->getType()); 324951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return EvaluateConstantExpression(Result.getUnionValue(), Info, 325051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith Subobject, &VIE); 325151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith } 325251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 3253b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return HandleClassZeroInitialization(Info, E, RD, This, Result); 325451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith} 325551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 325659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithbool RecordExprEvaluator::VisitCastExpr(const CastExpr *E) { 325759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith switch (E->getCastKind()) { 325859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith default: 325959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith return ExprEvaluatorBaseTy::VisitCastExpr(E); 326059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith 326159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith case CK_ConstructorConversion: 326259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith return Visit(E->getSubExpr()); 326359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith 326459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith case CK_DerivedToBase: 326559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith case CK_UncheckedDerivedToBase: { 326659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith CCValue DerivedObject; 3267f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!Evaluate(DerivedObject, Info, E->getSubExpr())) 326859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith return false; 3269f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!DerivedObject.isStruct()) 3270f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E->getSubExpr()); 327159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith 327259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith // Derived-to-base rvalue conversion: just slice off the derived part. 327359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith APValue *Value = &DerivedObject; 327459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith const CXXRecordDecl *RD = E->getSubExpr()->getType()->getAsCXXRecordDecl(); 327559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith for (CastExpr::path_const_iterator PathI = E->path_begin(), 327659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith PathE = E->path_end(); PathI != PathE; ++PathI) { 327759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith assert(!(*PathI)->isVirtual() && "record rvalue with virtual base"); 327859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); 327959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith Value = &Value->getStructBase(getBaseIndex(RD, Base)); 328059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith RD = Base; 328159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith } 328259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith Result = *Value; 328359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith return true; 328459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith } 328559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith } 328659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith} 328759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith 3288180f47959a066795cc0f409433023af448bb0328Richard Smithbool RecordExprEvaluator::VisitInitListExpr(const InitListExpr *E) { 3289180f47959a066795cc0f409433023af448bb0328Richard Smith const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl(); 3290180f47959a066795cc0f409433023af448bb0328Richard Smith const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); 3291180f47959a066795cc0f409433023af448bb0328Richard Smith 3292180f47959a066795cc0f409433023af448bb0328Richard Smith if (RD->isUnion()) { 3293ec789163a42a7be654ac34aadb750b508954d53cRichard Smith const FieldDecl *Field = E->getInitializedFieldInUnion(); 3294ec789163a42a7be654ac34aadb750b508954d53cRichard Smith Result = APValue(Field); 3295ec789163a42a7be654ac34aadb750b508954d53cRichard Smith if (!Field) 3296180f47959a066795cc0f409433023af448bb0328Richard Smith return true; 3297ec789163a42a7be654ac34aadb750b508954d53cRichard Smith 3298ec789163a42a7be654ac34aadb750b508954d53cRichard Smith // If the initializer list for a union does not contain any elements, the 3299ec789163a42a7be654ac34aadb750b508954d53cRichard Smith // first element of the union is value-initialized. 3300ec789163a42a7be654ac34aadb750b508954d53cRichard Smith ImplicitValueInitExpr VIE(Field->getType()); 3301ec789163a42a7be654ac34aadb750b508954d53cRichard Smith const Expr *InitExpr = E->getNumInits() ? E->getInit(0) : &VIE; 3302ec789163a42a7be654ac34aadb750b508954d53cRichard Smith 3303180f47959a066795cc0f409433023af448bb0328Richard Smith LValue Subobject = This; 3304ec789163a42a7be654ac34aadb750b508954d53cRichard Smith HandleLValueMember(Info, InitExpr, Subobject, Field, &Layout); 3305180f47959a066795cc0f409433023af448bb0328Richard Smith return EvaluateConstantExpression(Result.getUnionValue(), Info, 3306ec789163a42a7be654ac34aadb750b508954d53cRichard Smith Subobject, InitExpr); 3307180f47959a066795cc0f409433023af448bb0328Richard Smith } 3308180f47959a066795cc0f409433023af448bb0328Richard Smith 3309180f47959a066795cc0f409433023af448bb0328Richard Smith assert((!isa<CXXRecordDecl>(RD) || !cast<CXXRecordDecl>(RD)->getNumBases()) && 3310180f47959a066795cc0f409433023af448bb0328Richard Smith "initializer list for class with base classes"); 3311180f47959a066795cc0f409433023af448bb0328Richard Smith Result = APValue(APValue::UninitStruct(), 0, 3312180f47959a066795cc0f409433023af448bb0328Richard Smith std::distance(RD->field_begin(), RD->field_end())); 3313180f47959a066795cc0f409433023af448bb0328Richard Smith unsigned ElementNo = 0; 3314745f5147e065900267c85a5568785a1991d4838fRichard Smith bool Success = true; 3315180f47959a066795cc0f409433023af448bb0328Richard Smith for (RecordDecl::field_iterator Field = RD->field_begin(), 3316180f47959a066795cc0f409433023af448bb0328Richard Smith FieldEnd = RD->field_end(); Field != FieldEnd; ++Field) { 3317180f47959a066795cc0f409433023af448bb0328Richard Smith // Anonymous bit-fields are not considered members of the class for 3318180f47959a066795cc0f409433023af448bb0328Richard Smith // purposes of aggregate initialization. 3319180f47959a066795cc0f409433023af448bb0328Richard Smith if (Field->isUnnamedBitfield()) 3320180f47959a066795cc0f409433023af448bb0328Richard Smith continue; 3321180f47959a066795cc0f409433023af448bb0328Richard Smith 3322180f47959a066795cc0f409433023af448bb0328Richard Smith LValue Subobject = This; 3323180f47959a066795cc0f409433023af448bb0328Richard Smith 3324745f5147e065900267c85a5568785a1991d4838fRichard Smith bool HaveInit = ElementNo < E->getNumInits(); 3325745f5147e065900267c85a5568785a1991d4838fRichard Smith 3326745f5147e065900267c85a5568785a1991d4838fRichard Smith // FIXME: Diagnostics here should point to the end of the initializer 3327745f5147e065900267c85a5568785a1991d4838fRichard Smith // list, not the start. 3328745f5147e065900267c85a5568785a1991d4838fRichard Smith HandleLValueMember(Info, HaveInit ? E->getInit(ElementNo) : E, Subobject, 3329745f5147e065900267c85a5568785a1991d4838fRichard Smith *Field, &Layout); 3330745f5147e065900267c85a5568785a1991d4838fRichard Smith 3331745f5147e065900267c85a5568785a1991d4838fRichard Smith // Perform an implicit value-initialization for members beyond the end of 3332745f5147e065900267c85a5568785a1991d4838fRichard Smith // the initializer list. 3333745f5147e065900267c85a5568785a1991d4838fRichard Smith ImplicitValueInitExpr VIE(HaveInit ? Info.Ctx.IntTy : Field->getType()); 3334745f5147e065900267c85a5568785a1991d4838fRichard Smith 3335745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!EvaluateConstantExpression( 3336745f5147e065900267c85a5568785a1991d4838fRichard Smith Result.getStructField((*Field)->getFieldIndex()), 3337745f5147e065900267c85a5568785a1991d4838fRichard Smith Info, Subobject, HaveInit ? E->getInit(ElementNo++) : &VIE)) { 3338745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!Info.keepEvaluatingAfterFailure()) 3339180f47959a066795cc0f409433023af448bb0328Richard Smith return false; 3340745f5147e065900267c85a5568785a1991d4838fRichard Smith Success = false; 3341180f47959a066795cc0f409433023af448bb0328Richard Smith } 3342180f47959a066795cc0f409433023af448bb0328Richard Smith } 3343180f47959a066795cc0f409433023af448bb0328Richard Smith 3344745f5147e065900267c85a5568785a1991d4838fRichard Smith return Success; 3345180f47959a066795cc0f409433023af448bb0328Richard Smith} 3346180f47959a066795cc0f409433023af448bb0328Richard Smith 3347180f47959a066795cc0f409433023af448bb0328Richard Smithbool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { 3348180f47959a066795cc0f409433023af448bb0328Richard Smith const CXXConstructorDecl *FD = E->getConstructor(); 334951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith bool ZeroInit = E->requiresZeroInitialization(); 335051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) { 3351ec789163a42a7be654ac34aadb750b508954d53cRichard Smith // If we've already performed zero-initialization, we're already done. 3352ec789163a42a7be654ac34aadb750b508954d53cRichard Smith if (!Result.isUninit()) 3353ec789163a42a7be654ac34aadb750b508954d53cRichard Smith return true; 3354ec789163a42a7be654ac34aadb750b508954d53cRichard Smith 335551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (ZeroInit) 335651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return ZeroInitialization(E); 335751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 33586180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith const CXXRecordDecl *RD = FD->getParent(); 33596180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith if (RD->isUnion()) 33606180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith Result = APValue((FieldDecl*)0); 33616180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith else 33626180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith Result = APValue(APValue::UninitStruct(), RD->getNumBases(), 33636180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith std::distance(RD->field_begin(), RD->field_end())); 33646180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith return true; 33656180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith } 33666180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith 3367180f47959a066795cc0f409433023af448bb0328Richard Smith const FunctionDecl *Definition = 0; 3368180f47959a066795cc0f409433023af448bb0328Richard Smith FD->getBody(Definition); 3369180f47959a066795cc0f409433023af448bb0328Richard Smith 3370c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition)) 3371c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return false; 3372180f47959a066795cc0f409433023af448bb0328Richard Smith 3373610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith // Avoid materializing a temporary for an elidable copy/move constructor. 337451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (E->isElidable() && !ZeroInit) 3375180f47959a066795cc0f409433023af448bb0328Richard Smith if (const MaterializeTemporaryExpr *ME 3376180f47959a066795cc0f409433023af448bb0328Richard Smith = dyn_cast<MaterializeTemporaryExpr>(E->getArg(0))) 3377180f47959a066795cc0f409433023af448bb0328Richard Smith return Visit(ME->GetTemporaryExpr()); 3378180f47959a066795cc0f409433023af448bb0328Richard Smith 337951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (ZeroInit && !ZeroInitialization(E)) 338051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return false; 338151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 3382180f47959a066795cc0f409433023af448bb0328Richard Smith llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs()); 3383745f5147e065900267c85a5568785a1991d4838fRichard Smith return HandleConstructorCall(E->getExprLoc(), This, Args, 3384f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith cast<CXXConstructorDecl>(Definition), Info, 3385f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith Result); 3386180f47959a066795cc0f409433023af448bb0328Richard Smith} 3387180f47959a066795cc0f409433023af448bb0328Richard Smith 3388180f47959a066795cc0f409433023af448bb0328Richard Smithstatic bool EvaluateRecord(const Expr *E, const LValue &This, 3389180f47959a066795cc0f409433023af448bb0328Richard Smith APValue &Result, EvalInfo &Info) { 3390180f47959a066795cc0f409433023af448bb0328Richard Smith assert(E->isRValue() && E->getType()->isRecordType() && 3391180f47959a066795cc0f409433023af448bb0328Richard Smith "can't evaluate expression as a record rvalue"); 3392180f47959a066795cc0f409433023af448bb0328Richard Smith return RecordExprEvaluator(Info, This, Result).Visit(E); 3393180f47959a066795cc0f409433023af448bb0328Richard Smith} 3394180f47959a066795cc0f409433023af448bb0328Richard Smith 3395180f47959a066795cc0f409433023af448bb0328Richard Smith//===----------------------------------------------------------------------===// 3396e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// Temporary Evaluation 3397e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// 3398e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// Temporaries are represented in the AST as rvalues, but generally behave like 3399e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// lvalues. The full-object of which the temporary is a subobject is implicitly 3400e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// materialized so that a reference can bind to it. 3401e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===// 3402e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithnamespace { 3403e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithclass TemporaryExprEvaluator 3404e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith : public LValueExprEvaluatorBase<TemporaryExprEvaluator> { 3405e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithpublic: 3406e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith TemporaryExprEvaluator(EvalInfo &Info, LValue &Result) : 3407e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LValueExprEvaluatorBaseTy(Info, Result) {} 3408e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3409e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith /// Visit an expression which constructs the value of this temporary. 3410e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool VisitConstructExpr(const Expr *E) { 3411e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Result.set(E, Info.CurrentCall); 3412e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return EvaluateConstantExpression(Info.CurrentCall->Temporaries[E], Info, 3413e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Result, E); 3414e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 3415e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3416e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool VisitCastExpr(const CastExpr *E) { 3417e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith switch (E->getCastKind()) { 3418e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith default: 3419e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return LValueExprEvaluatorBaseTy::VisitCastExpr(E); 3420e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3421e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case CK_ConstructorConversion: 3422e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return VisitConstructExpr(E->getSubExpr()); 3423e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 3424e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 3425e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool VisitInitListExpr(const InitListExpr *E) { 3426e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return VisitConstructExpr(E); 3427e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 3428e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool VisitCXXConstructExpr(const CXXConstructExpr *E) { 3429e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return VisitConstructExpr(E); 3430e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 3431e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool VisitCallExpr(const CallExpr *E) { 3432e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return VisitConstructExpr(E); 3433e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 3434e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}; 3435e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith} // end anonymous namespace 3436e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3437e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// Evaluate an expression of record type as a temporary. 3438e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info) { 3439af2c7a194592401394233b7cbcdd3cfd0a7a38ddRichard Smith assert(E->isRValue() && E->getType()->isRecordType()); 3440e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return TemporaryExprEvaluator(Info, Result).Visit(E); 3441e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith} 3442e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3443e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===// 344459b5da6d853b4368b984700315adf7b37de05764Nate Begeman// Vector Evaluation 344559b5da6d853b4368b984700315adf7b37de05764Nate Begeman//===----------------------------------------------------------------------===// 344659b5da6d853b4368b984700315adf7b37de05764Nate Begeman 344759b5da6d853b4368b984700315adf7b37de05764Nate Begemannamespace { 3448770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramer class VectorExprEvaluator 344907fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith : public ExprEvaluatorBase<VectorExprEvaluator, bool> { 345007fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith APValue &Result; 345159b5da6d853b4368b984700315adf7b37de05764Nate Begeman public: 34521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 345307fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith VectorExprEvaluator(EvalInfo &info, APValue &Result) 345407fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith : ExprEvaluatorBaseTy(info), Result(Result) {} 34551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 345607fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith bool Success(const ArrayRef<APValue> &V, const Expr *E) { 345707fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith assert(V.size() == E->getType()->castAs<VectorType>()->getNumElements()); 345807fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith // FIXME: remove this APValue copy. 345907fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith Result = APValue(V.data(), V.size()); 346007fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith return true; 346107fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith } 346269c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith bool Success(const CCValue &V, const Expr *E) { 346369c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith assert(V.isVector()); 346407fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith Result = V; 346507fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith return true; 346607fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith } 346751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith bool ZeroInitialization(const Expr *E); 34681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 346907fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith bool VisitUnaryReal(const UnaryOperator *E) 347091110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman { return Visit(E->getSubExpr()); } 347107fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith bool VisitCastExpr(const CastExpr* E); 347207fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith bool VisitInitListExpr(const InitListExpr *E); 347307fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith bool VisitUnaryImag(const UnaryOperator *E); 347491110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div, 34752217c87bdc5ab357046a5453bdb06f469c41024eEli Friedman // binary comparisons, binary and/or/xor, 347691110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman // shufflevector, ExtVectorElementExpr 347759b5da6d853b4368b984700315adf7b37de05764Nate Begeman }; 347859b5da6d853b4368b984700315adf7b37de05764Nate Begeman} // end anonymous namespace 347959b5da6d853b4368b984700315adf7b37de05764Nate Begeman 348059b5da6d853b4368b984700315adf7b37de05764Nate Begemanstatic bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) { 3481c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith assert(E->isRValue() && E->getType()->isVectorType() &&"not a vector rvalue"); 348207fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith return VectorExprEvaluator(Info, Result).Visit(E); 348359b5da6d853b4368b984700315adf7b37de05764Nate Begeman} 348459b5da6d853b4368b984700315adf7b37de05764Nate Begeman 348507fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smithbool VectorExprEvaluator::VisitCastExpr(const CastExpr* E) { 348607fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith const VectorType *VTy = E->getType()->castAs<VectorType>(); 3487c0b8b19bd8056d6b5d831623a0825cce150f4507Nate Begeman unsigned NElts = VTy->getNumElements(); 34881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3489d62ca370b03b8c6ad58002d3399383baf744e32bRichard Smith const Expr *SE = E->getSubExpr(); 3490e8c9e9218f215ec6089f12b076c7b9d310fd5194Nate Begeman QualType SETy = SE->getType(); 349159b5da6d853b4368b984700315adf7b37de05764Nate Begeman 349246a523285928aa07bf14803178dc04616ac85994Eli Friedman switch (E->getCastKind()) { 349346a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_VectorSplat: { 349407fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith APValue Val = APValue(); 349546a523285928aa07bf14803178dc04616ac85994Eli Friedman if (SETy->isIntegerType()) { 349646a523285928aa07bf14803178dc04616ac85994Eli Friedman APSInt IntResult; 349746a523285928aa07bf14803178dc04616ac85994Eli Friedman if (!EvaluateInteger(SE, IntResult, Info)) 3498f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 349907fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith Val = APValue(IntResult); 350046a523285928aa07bf14803178dc04616ac85994Eli Friedman } else if (SETy->isRealFloatingType()) { 350146a523285928aa07bf14803178dc04616ac85994Eli Friedman APFloat F(0.0); 350246a523285928aa07bf14803178dc04616ac85994Eli Friedman if (!EvaluateFloat(SE, F, Info)) 3503f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 350407fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith Val = APValue(F); 350546a523285928aa07bf14803178dc04616ac85994Eli Friedman } else { 350607fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith return Error(E); 350746a523285928aa07bf14803178dc04616ac85994Eli Friedman } 3508c0b8b19bd8056d6b5d831623a0825cce150f4507Nate Begeman 3509c0b8b19bd8056d6b5d831623a0825cce150f4507Nate Begeman // Splat and create vector APValue. 351007fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith SmallVector<APValue, 4> Elts(NElts, Val); 351107fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith return Success(Elts, E); 3512e8c9e9218f215ec6089f12b076c7b9d310fd5194Nate Begeman } 3513e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman case CK_BitCast: { 3514e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman // Evaluate the operand into an APInt we can extract from. 3515e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman llvm::APInt SValInt; 3516e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman if (!EvalAndBitcastToAPInt(Info, SE, SValInt)) 3517e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman return false; 3518e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman // Extract the elements 3519e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman QualType EltTy = VTy->getElementType(); 3520e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman unsigned EltSize = Info.Ctx.getTypeSize(EltTy); 3521e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); 3522e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman SmallVector<APValue, 4> Elts; 3523e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman if (EltTy->isRealFloatingType()) { 3524e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(EltTy); 3525e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman bool isIEESem = &Sem != &APFloat::PPCDoubleDouble; 3526e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman unsigned FloatEltSize = EltSize; 3527e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman if (&Sem == &APFloat::x87DoubleExtended) 3528e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman FloatEltSize = 80; 3529e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman for (unsigned i = 0; i < NElts; i++) { 3530e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman llvm::APInt Elt; 3531e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman if (BigEndian) 3532e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman Elt = SValInt.rotl(i*EltSize+FloatEltSize).trunc(FloatEltSize); 3533e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman else 3534e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman Elt = SValInt.rotr(i*EltSize).trunc(FloatEltSize); 3535e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman Elts.push_back(APValue(APFloat(Elt, isIEESem))); 3536e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman } 3537e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman } else if (EltTy->isIntegerType()) { 3538e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman for (unsigned i = 0; i < NElts; i++) { 3539e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman llvm::APInt Elt; 3540e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman if (BigEndian) 3541e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman Elt = SValInt.rotl(i*EltSize+EltSize).zextOrTrunc(EltSize); 3542e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman else 3543e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman Elt = SValInt.rotr(i*EltSize).zextOrTrunc(EltSize); 3544e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman Elts.push_back(APValue(APSInt(Elt, EltTy->isSignedIntegerType()))); 3545e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman } 3546e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman } else { 3547e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman return Error(E); 3548e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman } 3549e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman return Success(Elts, E); 3550e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman } 355146a523285928aa07bf14803178dc04616ac85994Eli Friedman default: 3552c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return ExprEvaluatorBaseTy::VisitCastExpr(E); 3553c0b8b19bd8056d6b5d831623a0825cce150f4507Nate Begeman } 355459b5da6d853b4368b984700315adf7b37de05764Nate Begeman} 355559b5da6d853b4368b984700315adf7b37de05764Nate Begeman 355607fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smithbool 355759b5da6d853b4368b984700315adf7b37de05764Nate BegemanVectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) { 355807fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith const VectorType *VT = E->getType()->castAs<VectorType>(); 355959b5da6d853b4368b984700315adf7b37de05764Nate Begeman unsigned NumInits = E->getNumInits(); 356091110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman unsigned NumElements = VT->getNumElements(); 35611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 356259b5da6d853b4368b984700315adf7b37de05764Nate Begeman QualType EltTy = VT->getElementType(); 35635f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<APValue, 4> Elements; 356459b5da6d853b4368b984700315adf7b37de05764Nate Begeman 35653edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman // The number of initializers can be less than the number of 35663edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman // vector elements. For OpenCL, this can be due to nested vector 35673edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman // initialization. For GCC compatibility, missing trailing elements 35683edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman // should be initialized with zeroes. 35693edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman unsigned CountInits = 0, CountElts = 0; 35703edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman while (CountElts < NumElements) { 35713edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman // Handle nested vector initialization. 35723edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman if (CountInits < NumInits 35733edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman && E->getInit(CountInits)->getType()->isExtVectorType()) { 35743edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman APValue v; 35753edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman if (!EvaluateVector(E->getInit(CountInits), v, Info)) 35763edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman return Error(E); 35773edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman unsigned vlen = v.getVectorLength(); 35783edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman for (unsigned j = 0; j < vlen; j++) 35793edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman Elements.push_back(v.getVectorElt(j)); 35803edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman CountElts += vlen; 35813edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman } else if (EltTy->isIntegerType()) { 358259b5da6d853b4368b984700315adf7b37de05764Nate Begeman llvm::APSInt sInt(32); 35833edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman if (CountInits < NumInits) { 35843edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman if (!EvaluateInteger(E->getInit(CountInits), sInt, Info)) 35853edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman return Error(E); 35863edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman } else // trailing integer zero. 35873edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman sInt = Info.Ctx.MakeIntValue(0, EltTy); 35883edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman Elements.push_back(APValue(sInt)); 35893edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman CountElts++; 359059b5da6d853b4368b984700315adf7b37de05764Nate Begeman } else { 359159b5da6d853b4368b984700315adf7b37de05764Nate Begeman llvm::APFloat f(0.0); 35923edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman if (CountInits < NumInits) { 35933edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman if (!EvaluateFloat(E->getInit(CountInits), f, Info)) 35943edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman return Error(E); 35953edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman } else // trailing float zero. 35963edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)); 35973edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman Elements.push_back(APValue(f)); 35983edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman CountElts++; 359959b5da6d853b4368b984700315adf7b37de05764Nate Begeman } 36003edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman CountInits++; 360159b5da6d853b4368b984700315adf7b37de05764Nate Begeman } 360207fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith return Success(Elements, E); 360359b5da6d853b4368b984700315adf7b37de05764Nate Begeman} 360459b5da6d853b4368b984700315adf7b37de05764Nate Begeman 360507fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smithbool 360651201882382fb40c9456a06c7f93d6ddd4a57712Richard SmithVectorExprEvaluator::ZeroInitialization(const Expr *E) { 360707fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith const VectorType *VT = E->getType()->getAs<VectorType>(); 360891110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman QualType EltTy = VT->getElementType(); 360991110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman APValue ZeroElement; 361091110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman if (EltTy->isIntegerType()) 361191110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy)); 361291110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman else 361391110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman ZeroElement = 361491110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy))); 361591110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman 36165f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement); 361707fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith return Success(Elements, E); 361891110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman} 361991110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman 362007fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smithbool VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { 36218327fad71da34492d82c532f42a58cb4baff81a3Richard Smith VisitIgnoredValue(E->getSubExpr()); 362251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return ZeroInitialization(E); 362391110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman} 362491110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman 362559b5da6d853b4368b984700315adf7b37de05764Nate Begeman//===----------------------------------------------------------------------===// 3626cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith// Array Evaluation 3627cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith//===----------------------------------------------------------------------===// 3628cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith 3629cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smithnamespace { 3630cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith class ArrayExprEvaluator 3631cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith : public ExprEvaluatorBase<ArrayExprEvaluator, bool> { 3632180f47959a066795cc0f409433023af448bb0328Richard Smith const LValue &This; 3633cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith APValue &Result; 3634cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith public: 3635cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith 3636180f47959a066795cc0f409433023af448bb0328Richard Smith ArrayExprEvaluator(EvalInfo &Info, const LValue &This, APValue &Result) 3637180f47959a066795cc0f409433023af448bb0328Richard Smith : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {} 3638cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith 3639cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith bool Success(const APValue &V, const Expr *E) { 3640cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith assert(V.isArray() && "Expected array type"); 3641cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith Result = V; 3642cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith return true; 3643cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith } 3644cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith 364551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith bool ZeroInitialization(const Expr *E) { 3646180f47959a066795cc0f409433023af448bb0328Richard Smith const ConstantArrayType *CAT = 3647180f47959a066795cc0f409433023af448bb0328Richard Smith Info.Ctx.getAsConstantArrayType(E->getType()); 3648180f47959a066795cc0f409433023af448bb0328Richard Smith if (!CAT) 3649f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 3650180f47959a066795cc0f409433023af448bb0328Richard Smith 3651180f47959a066795cc0f409433023af448bb0328Richard Smith Result = APValue(APValue::UninitArray(), 0, 3652180f47959a066795cc0f409433023af448bb0328Richard Smith CAT->getSize().getZExtValue()); 3653180f47959a066795cc0f409433023af448bb0328Richard Smith if (!Result.hasArrayFiller()) return true; 3654180f47959a066795cc0f409433023af448bb0328Richard Smith 365551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith // Zero-initialize all elements. 3656180f47959a066795cc0f409433023af448bb0328Richard Smith LValue Subobject = This; 3657b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Subobject.addArray(Info, E, CAT); 3658180f47959a066795cc0f409433023af448bb0328Richard Smith ImplicitValueInitExpr VIE(CAT->getElementType()); 3659180f47959a066795cc0f409433023af448bb0328Richard Smith return EvaluateConstantExpression(Result.getArrayFiller(), Info, 3660180f47959a066795cc0f409433023af448bb0328Richard Smith Subobject, &VIE); 3661180f47959a066795cc0f409433023af448bb0328Richard Smith } 3662180f47959a066795cc0f409433023af448bb0328Richard Smith 3663cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith bool VisitInitListExpr(const InitListExpr *E); 3664e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool VisitCXXConstructExpr(const CXXConstructExpr *E); 3665cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith }; 3666cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith} // end anonymous namespace 3667cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith 3668180f47959a066795cc0f409433023af448bb0328Richard Smithstatic bool EvaluateArray(const Expr *E, const LValue &This, 3669180f47959a066795cc0f409433023af448bb0328Richard Smith APValue &Result, EvalInfo &Info) { 367051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith assert(E->isRValue() && E->getType()->isArrayType() && "not an array rvalue"); 3671180f47959a066795cc0f409433023af448bb0328Richard Smith return ArrayExprEvaluator(Info, This, Result).Visit(E); 3672cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith} 3673cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith 3674cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smithbool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E) { 3675cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(E->getType()); 3676cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith if (!CAT) 3677f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 3678cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith 3679974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith // C++11 [dcl.init.string]p1: A char array [...] can be initialized by [...] 3680974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith // an appropriately-typed string literal enclosed in braces. 3681ec789163a42a7be654ac34aadb750b508954d53cRichard Smith if (E->getNumInits() == 1 && E->getInit(0)->isGLValue() && 3682974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith Info.Ctx.hasSameUnqualifiedType(E->getType(), E->getInit(0)->getType())) { 3683974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith LValue LV; 3684974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith if (!EvaluateLValue(E->getInit(0), LV, Info)) 3685974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith return false; 3686974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith uint64_t NumElements = CAT->getSize().getZExtValue(); 3687974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith Result = APValue(APValue::UninitArray(), NumElements, NumElements); 3688974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith 3689974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith // Copy the string literal into the array. FIXME: Do this better. 3690b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith LV.addArray(Info, E, CAT); 3691974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith for (uint64_t I = 0; I < NumElements; ++I) { 3692974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith CCValue Char; 3693974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith if (!HandleLValueToRValueConversion(Info, E->getInit(0), 3694745f5147e065900267c85a5568785a1991d4838fRichard Smith CAT->getElementType(), LV, Char) || 3695745f5147e065900267c85a5568785a1991d4838fRichard Smith !CheckConstantExpression(Info, E->getInit(0), Char, 3696745f5147e065900267c85a5568785a1991d4838fRichard Smith Result.getArrayInitializedElt(I)) || 3697745f5147e065900267c85a5568785a1991d4838fRichard Smith !HandleLValueArrayAdjustment(Info, E->getInit(0), LV, 3698b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith CAT->getElementType(), 1)) 3699974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith return false; 3700974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith } 3701974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith return true; 3702974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith } 3703974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith 3704745f5147e065900267c85a5568785a1991d4838fRichard Smith bool Success = true; 3705745f5147e065900267c85a5568785a1991d4838fRichard Smith 3706cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith Result = APValue(APValue::UninitArray(), E->getNumInits(), 3707cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith CAT->getSize().getZExtValue()); 3708180f47959a066795cc0f409433023af448bb0328Richard Smith LValue Subobject = This; 3709b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Subobject.addArray(Info, E, CAT); 3710180f47959a066795cc0f409433023af448bb0328Richard Smith unsigned Index = 0; 3711cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith for (InitListExpr::const_iterator I = E->begin(), End = E->end(); 3712180f47959a066795cc0f409433023af448bb0328Richard Smith I != End; ++I, ++Index) { 3713180f47959a066795cc0f409433023af448bb0328Richard Smith if (!EvaluateConstantExpression(Result.getArrayInitializedElt(Index), 3714745f5147e065900267c85a5568785a1991d4838fRichard Smith Info, Subobject, cast<Expr>(*I)) || 3715745f5147e065900267c85a5568785a1991d4838fRichard Smith !HandleLValueArrayAdjustment(Info, cast<Expr>(*I), Subobject, 3716745f5147e065900267c85a5568785a1991d4838fRichard Smith CAT->getElementType(), 1)) { 3717745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!Info.keepEvaluatingAfterFailure()) 3718745f5147e065900267c85a5568785a1991d4838fRichard Smith return false; 3719745f5147e065900267c85a5568785a1991d4838fRichard Smith Success = false; 3720745f5147e065900267c85a5568785a1991d4838fRichard Smith } 3721180f47959a066795cc0f409433023af448bb0328Richard Smith } 3722cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith 3723745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!Result.hasArrayFiller()) return Success; 3724cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith assert(E->hasArrayFiller() && "no array filler for incomplete init list"); 3725180f47959a066795cc0f409433023af448bb0328Richard Smith // FIXME: The Subobject here isn't necessarily right. This rarely matters, 3726180f47959a066795cc0f409433023af448bb0328Richard Smith // but sometimes does: 3727180f47959a066795cc0f409433023af448bb0328Richard Smith // struct S { constexpr S() : p(&p) {} void *p; }; 3728180f47959a066795cc0f409433023af448bb0328Richard Smith // S s[10] = {}; 3729cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith return EvaluateConstantExpression(Result.getArrayFiller(), Info, 3730745f5147e065900267c85a5568785a1991d4838fRichard Smith Subobject, E->getArrayFiller()) && Success; 3731cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith} 3732cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith 3733e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithbool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { 3734e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(E->getType()); 3735e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!CAT) 3736f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 3737e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3738ec789163a42a7be654ac34aadb750b508954d53cRichard Smith bool HadZeroInit = !Result.isUninit(); 3739ec789163a42a7be654ac34aadb750b508954d53cRichard Smith if (!HadZeroInit) 3740ec789163a42a7be654ac34aadb750b508954d53cRichard Smith Result = APValue(APValue::UninitArray(), 0, CAT->getSize().getZExtValue()); 3741e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!Result.hasArrayFiller()) 3742e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 3743e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3744e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const CXXConstructorDecl *FD = E->getConstructor(); 37456180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith 374651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith bool ZeroInit = E->requiresZeroInitialization(); 374751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) { 3748ec789163a42a7be654ac34aadb750b508954d53cRichard Smith if (HadZeroInit) 3749ec789163a42a7be654ac34aadb750b508954d53cRichard Smith return true; 3750ec789163a42a7be654ac34aadb750b508954d53cRichard Smith 375151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (ZeroInit) { 375251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith LValue Subobject = This; 3753b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Subobject.addArray(Info, E, CAT); 375451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith ImplicitValueInitExpr VIE(CAT->getElementType()); 375551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return EvaluateConstantExpression(Result.getArrayFiller(), Info, 375651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith Subobject, &VIE); 375751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith } 375851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 37596180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith const CXXRecordDecl *RD = FD->getParent(); 37606180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith if (RD->isUnion()) 37616180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith Result.getArrayFiller() = APValue((FieldDecl*)0); 37626180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith else 37636180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith Result.getArrayFiller() = 37646180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith APValue(APValue::UninitStruct(), RD->getNumBases(), 37656180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith std::distance(RD->field_begin(), RD->field_end())); 37666180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith return true; 37676180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith } 37686180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith 3769e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const FunctionDecl *Definition = 0; 3770e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith FD->getBody(Definition); 3771e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3772c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition)) 3773c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return false; 3774e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3775e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // FIXME: The Subobject here isn't necessarily right. This rarely matters, 3776e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // but sometimes does: 3777e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // struct S { constexpr S() : p(&p) {} void *p; }; 3778e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // S s[10]; 3779e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LValue Subobject = This; 3780b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Subobject.addArray(Info, E, CAT); 378151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 3782ec789163a42a7be654ac34aadb750b508954d53cRichard Smith if (ZeroInit && !HadZeroInit) { 378351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith ImplicitValueInitExpr VIE(CAT->getElementType()); 378451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (!EvaluateConstantExpression(Result.getArrayFiller(), Info, Subobject, 378551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith &VIE)) 378651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return false; 378751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith } 378851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 3789e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs()); 3790745f5147e065900267c85a5568785a1991d4838fRichard Smith return HandleConstructorCall(E->getExprLoc(), Subobject, Args, 3791e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith cast<CXXConstructorDecl>(Definition), 3792e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Info, Result.getArrayFiller()); 3793e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith} 3794e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3795cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith//===----------------------------------------------------------------------===// 3796f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner// Integer Evaluation 3797c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// 3798c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// As a GNU extension, we support casting pointers to sufficiently-wide integer 3799c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// types and back in constant folding. Integer values are thus represented 3800c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// either as an integer-valued APValue, or as an lvalue-valued APValue. 3801f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===// 3802f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner 3803f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattnernamespace { 3804770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass IntExprEvaluator 38058cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne : public ExprEvaluatorBase<IntExprEvaluator, bool> { 380647a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith CCValue &Result; 3807f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattnerpublic: 380847a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith IntExprEvaluator(EvalInfo &info, CCValue &result) 38098cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne : ExprEvaluatorBaseTy(info), Result(result) {} 3810f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner 3811973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara bool Success(const llvm::APSInt &SI, const Expr *E) { 3812973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara assert(E->getType()->isIntegralOrEnumerationType() && 38132ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor "Invalid evaluation result."); 3814973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() && 38153f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar "Invalid evaluation result."); 3816973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && 38173f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar "Invalid evaluation result."); 381847a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith Result = CCValue(SI); 38193f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar return true; 38203f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar } 38213f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar 3822131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar bool Success(const llvm::APInt &I, const Expr *E) { 38232ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor assert(E->getType()->isIntegralOrEnumerationType() && 38242ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor "Invalid evaluation result."); 382530c37f4d2ee5811e85f692c22fb67d74ddc88079Daniel Dunbar assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && 38263f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar "Invalid evaluation result."); 382747a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith Result = CCValue(APSInt(I)); 3828575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor Result.getInt().setIsUnsigned( 3829575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor E->getType()->isUnsignedIntegerOrEnumerationType()); 3830131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return true; 3831131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar } 3832131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar 3833131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar bool Success(uint64_t Value, const Expr *E) { 38342ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor assert(E->getType()->isIntegralOrEnumerationType() && 38352ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor "Invalid evaluation result."); 383647a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith Result = CCValue(Info.Ctx.MakeIntValue(Value, E->getType())); 3837131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return true; 3838131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar } 3839131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar 38404f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck bool Success(CharUnits Size, const Expr *E) { 38414f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck return Success(Size.getQuantity(), E); 38424f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck } 38434f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck 384447a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith bool Success(const CCValue &V, const Expr *E) { 38455930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman if (V.isLValue() || V.isAddrLabelDiff()) { 3846342f1f8b0a402c5a7f8c5055db7f60a7808f1687Richard Smith Result = V; 3847342f1f8b0a402c5a7f8c5055db7f60a7808f1687Richard Smith return true; 3848342f1f8b0a402c5a7f8c5055db7f60a7808f1687Richard Smith } 38498cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return Success(V.getInt(), E); 385032fea9d18cc3658a1b01df5ca6f2ac302625c61dChris Lattner } 38511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 385251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith bool ZeroInitialization(const Expr *E) { return Success(0, E); } 3853f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith 38548cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne //===--------------------------------------------------------------------===// 38558cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne // Visitor Methods 38568cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne //===--------------------------------------------------------------------===// 3857f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner 38584c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner bool VisitIntegerLiteral(const IntegerLiteral *E) { 3859131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success(E->getValue(), E); 38604c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner } 38614c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner bool VisitCharacterLiteral(const CharacterLiteral *E) { 3862131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success(E->getValue(), E); 38634c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner } 3864043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman 3865043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman bool CheckReferencedDecl(const Expr *E, const Decl *D); 3866043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman bool VisitDeclRefExpr(const DeclRefExpr *E) { 38678cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne if (CheckReferencedDecl(E, E->getDecl())) 38688cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return true; 38698cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 38708cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return ExprEvaluatorBaseTy::VisitDeclRefExpr(E); 3871043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman } 3872043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman bool VisitMemberExpr(const MemberExpr *E) { 3873043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman if (CheckReferencedDecl(E, E->getMemberDecl())) { 3874c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith VisitIgnoredValue(E->getBase()); 3875043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman return true; 3876043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman } 38778cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 38788cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return ExprEvaluatorBaseTy::VisitMemberExpr(E); 3879043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman } 3880043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman 38818cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitCallExpr(const CallExpr *E); 3882b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattner bool VisitBinaryOperator(const BinaryOperator *E); 38838ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor bool VisitOffsetOfExpr(const OffsetOfExpr *E); 3884b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattner bool VisitUnaryOperator(const UnaryOperator *E); 3885f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner 38868cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitCastExpr(const CastExpr* E); 3887f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E); 38880518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl 38893068d117951a8df54bae9db039b56201ab10962bAnders Carlsson bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { 3890131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success(E->getValue(), E); 38913068d117951a8df54bae9db039b56201ab10962bAnders Carlsson } 38921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3893f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith // Note, GNU defines __null as an integer, not a pointer. 38943f70456b8adb0405ef2a47d51f9fc2d5937ae8aeAnders Carlsson bool VisitGNUNullExpr(const GNUNullExpr *E) { 389551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return ZeroInitialization(E); 3896664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman } 3897664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman 389864b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) { 38990dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl return Success(E->getValue(), E); 390064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl } 390164b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl 39026ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet bool VisitBinaryTypeTraitExpr(const BinaryTypeTraitExpr *E) { 39036ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet return Success(E->getValue(), E); 39046ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet } 39056ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet 390621ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) { 390721ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley return Success(E->getValue(), E); 390821ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley } 390921ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley 3910552622067dc45013d240f73952fece703f5e63bdJohn Wiegley bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) { 3911552622067dc45013d240f73952fece703f5e63bdJohn Wiegley return Success(E->getValue(), E); 3912552622067dc45013d240f73952fece703f5e63bdJohn Wiegley } 3913552622067dc45013d240f73952fece703f5e63bdJohn Wiegley 3914722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman bool VisitUnaryReal(const UnaryOperator *E); 3915664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman bool VisitUnaryImag(const UnaryOperator *E); 3916664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman 3917295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E); 3918ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor bool VisitSizeOfPackExpr(const SizeOfPackExpr *E); 3919cea8d966f826554f0679595e9371e314e8dbc1cfSebastian Redl 3920fcee0019b76f9f368f2b3d6d4048a98232593f29Chris Lattnerprivate: 39218b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck CharUnits GetAlignOfExpr(const Expr *E); 39228b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck CharUnits GetAlignOfType(QualType T); 39231bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith static QualType GetObjectType(APValue::LValueBase B); 39248cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool TryEvaluateBuiltinObjectSize(const CallExpr *E); 3925664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman // FIXME: Missing: array subscript of vector, member of vector 3926f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner}; 3927f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner} // end anonymous namespace 3928f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner 3929c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// EvaluateIntegerOrLValue - Evaluate an rvalue integral-typed expression, and 3930c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// produce either the integer value or a pointer. 3931c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// 3932c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// GCC has a heinous extension which folds casts between pointer types and 3933c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// pointer-sized integral types. We support this by allowing the evaluation of 3934c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// an integer rvalue to produce a pointer (represented as an lvalue) instead. 3935c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// Some simple arithmetic on such values is supported (they are treated much 3936c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// like char*). 3937f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool EvaluateIntegerOrLValue(const Expr *E, CCValue &Result, 393847a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith EvalInfo &Info) { 3939c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith assert(E->isRValue() && E->getType()->isIntegralOrEnumerationType()); 39408cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return IntExprEvaluator(Info, Result).Visit(E); 394169ab26a8623141f35e86817cfc6e0fbe7639a40fDaniel Dunbar} 394269ab26a8623141f35e86817cfc6e0fbe7639a40fDaniel Dunbar 3943f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info) { 394447a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith CCValue Val; 3945f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!EvaluateIntegerOrLValue(E, Val, Info)) 3946f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 3947f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!Val.isInt()) { 3948f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith // FIXME: It would be better to produce the diagnostic for casting 3949f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith // a pointer to an integer. 3950dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); 395130c37f4d2ee5811e85f692c22fb67d74ddc88079Daniel Dunbar return false; 3952f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 395330c37f4d2ee5811e85f692c22fb67d74ddc88079Daniel Dunbar Result = Val.getInt(); 395430c37f4d2ee5811e85f692c22fb67d74ddc88079Daniel Dunbar return true; 3955f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner} 3956f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner 3957f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// Check whether the given declaration can be directly converted to an integral 3958f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// rvalue. If not, no diagnostic is produced; there are other things we can 3959f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// try. 3960043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedmanbool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) { 39614c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner // Enums are integer constant exprs. 3962bfbdcd861a4364bfc21a9e5047bdbd56812d6693Abramo Bagnara if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) { 3963973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara // Check for signedness/width mismatches between E type and ECD value. 3964973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara bool SameSign = (ECD->getInitVal().isSigned() 3965973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara == E->getType()->isSignedIntegerOrEnumerationType()); 3966973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara bool SameWidth = (ECD->getInitVal().getBitWidth() 3967973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara == Info.Ctx.getIntWidth(E->getType())); 3968973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara if (SameSign && SameWidth) 3969973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara return Success(ECD->getInitVal(), E); 3970973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara else { 3971973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara // Get rid of mismatch (otherwise Success assertions will fail) 3972973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara // by computing a new value matching the type of E. 3973973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara llvm::APSInt Val = ECD->getInitVal(); 3974973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara if (!SameSign) 3975973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara Val.setIsSigned(!ECD->getInitVal().isSigned()); 3976973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara if (!SameWidth) 3977973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara Val = Val.extOrTrunc(Info.Ctx.getIntWidth(E->getType())); 3978973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara return Success(Val, E); 3979973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara } 3980bfbdcd861a4364bfc21a9e5047bdbd56812d6693Abramo Bagnara } 39818cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return false; 39824c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner} 39834c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner 3984a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way 3985a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner/// as GCC. 3986a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattnerstatic int EvaluateBuiltinClassifyType(const CallExpr *E) { 3987a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner // The following enum mimics the values returned by GCC. 39887c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl // FIXME: Does GCC differ between lvalue and rvalue references here? 3989a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner enum gcc_type_class { 3990a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner no_type_class = -1, 3991a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner void_type_class, integer_type_class, char_type_class, 3992a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner enumeral_type_class, boolean_type_class, 3993a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner pointer_type_class, reference_type_class, offset_type_class, 3994a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner real_type_class, complex_type_class, 3995a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner function_type_class, method_type_class, 3996a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner record_type_class, union_type_class, 3997a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner array_type_class, string_type_class, 3998a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner lang_type_class 3999a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner }; 40001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 40011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // If no argument was supplied, default to "no_type_class". This isn't 4002a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner // ideal, however it is what gcc does. 4003a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner if (E->getNumArgs() == 0) 4004a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return no_type_class; 40051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4006a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner QualType ArgTy = E->getArg(0)->getType(); 4007a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner if (ArgTy->isVoidType()) 4008a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return void_type_class; 4009a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner else if (ArgTy->isEnumeralType()) 4010a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return enumeral_type_class; 4011a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner else if (ArgTy->isBooleanType()) 4012a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return boolean_type_class; 4013a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner else if (ArgTy->isCharType()) 4014a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return string_type_class; // gcc doesn't appear to use char_type_class 4015a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner else if (ArgTy->isIntegerType()) 4016a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return integer_type_class; 4017a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner else if (ArgTy->isPointerType()) 4018a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return pointer_type_class; 4019a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner else if (ArgTy->isReferenceType()) 4020a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return reference_type_class; 4021a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner else if (ArgTy->isRealType()) 4022a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return real_type_class; 4023a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner else if (ArgTy->isComplexType()) 4024a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return complex_type_class; 4025a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner else if (ArgTy->isFunctionType()) 4026a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return function_type_class; 4027fb87b89fc9eb103e19fb8e4b925c23f0bd091b99Douglas Gregor else if (ArgTy->isStructureOrClassType()) 4028a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return record_type_class; 4029a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner else if (ArgTy->isUnionType()) 4030a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return union_type_class; 4031a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner else if (ArgTy->isArrayType()) 4032a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return array_type_class; 4033a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner else if (ArgTy->isUnionType()) 4034a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return union_type_class; 4035a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner else // FIXME: offset_type_class, method_type_class, & lang_type_class? 4036b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie llvm_unreachable("CallExpr::isBuiltinClassifyType(): unimplemented type"); 4037a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner} 4038a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner 403980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// EvaluateBuiltinConstantPForLValue - Determine the result of 404080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// __builtin_constant_p when applied to the given lvalue. 404180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// 404280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// An lvalue is only "constant" if it is a pointer or reference to the first 404380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// character of a string literal. 404480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smithtemplate<typename LValue> 404580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smithstatic bool EvaluateBuiltinConstantPForLValue(const LValue &LV) { 404680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith const Expr *E = LV.getLValueBase().dyn_cast<const Expr*>(); 404780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith return E && isa<StringLiteral>(E) && LV.getLValueOffset().isZero(); 404880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith} 404980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith 405080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// EvaluateBuiltinConstantP - Evaluate __builtin_constant_p as similarly to 405180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// GCC as we can manage. 405280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smithstatic bool EvaluateBuiltinConstantP(ASTContext &Ctx, const Expr *Arg) { 405380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith QualType ArgType = Arg->getType(); 405480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith 405580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // __builtin_constant_p always has one operand. The rules which gcc follows 405680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // are not precisely documented, but are as follows: 405780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // 405880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // - If the operand is of integral, floating, complex or enumeration type, 405980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // and can be folded to a known value of that type, it returns 1. 406080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // - If the operand and can be folded to a pointer to the first character 406180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // of a string literal (or such a pointer cast to an integral type), it 406280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // returns 1. 406380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // 406480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // Otherwise, it returns 0. 406580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // 406680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // FIXME: GCC also intends to return 1 for literals of aggregate types, but 406780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // its support for this does not currently work. 406880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith if (ArgType->isIntegralOrEnumerationType()) { 406980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith Expr::EvalResult Result; 407080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith if (!Arg->EvaluateAsRValue(Result, Ctx) || Result.HasSideEffects) 407180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith return false; 407280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith 407380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith APValue &V = Result.Val; 407480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith if (V.getKind() == APValue::Int) 407580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith return true; 407680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith 407780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith return EvaluateBuiltinConstantPForLValue(V); 407880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith } else if (ArgType->isFloatingType() || ArgType->isAnyComplexType()) { 407980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith return Arg->isEvaluatable(Ctx); 408080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith } else if (ArgType->isPointerType() || Arg->isGLValue()) { 408180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith LValue LV; 408280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith Expr::EvalStatus Status; 408380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith EvalInfo Info(Ctx, Status); 408480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith if ((Arg->isGLValue() ? EvaluateLValue(Arg, LV, Info) 408580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith : EvaluatePointer(Arg, LV, Info)) && 408680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith !Status.HasSideEffects) 408780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith return EvaluateBuiltinConstantPForLValue(LV); 408880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith } 408980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith 409080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // Anything else isn't considered to be sufficiently constant. 409180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith return false; 409280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith} 409380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith 409442c8f87eb60958170c46767273bf93e6c96125bfJohn McCall/// Retrieves the "underlying object type" of the given expression, 409542c8f87eb60958170c46767273bf93e6c96125bfJohn McCall/// as used by __builtin_object_size. 40961bf9a9e6a5bdc0de7939908855dcddf46b661800Richard SmithQualType IntExprEvaluator::GetObjectType(APValue::LValueBase B) { 40971bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { 40981bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith if (const VarDecl *VD = dyn_cast<VarDecl>(D)) 409942c8f87eb60958170c46767273bf93e6c96125bfJohn McCall return VD->getType(); 41001bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith } else if (const Expr *E = B.get<const Expr*>()) { 41011bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith if (isa<CompoundLiteralExpr>(E)) 41021bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith return E->getType(); 410342c8f87eb60958170c46767273bf93e6c96125bfJohn McCall } 410442c8f87eb60958170c46767273bf93e6c96125bfJohn McCall 410542c8f87eb60958170c46767273bf93e6c96125bfJohn McCall return QualType(); 410642c8f87eb60958170c46767273bf93e6c96125bfJohn McCall} 410742c8f87eb60958170c46767273bf93e6c96125bfJohn McCall 41088cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool IntExprEvaluator::TryEvaluateBuiltinObjectSize(const CallExpr *E) { 410942c8f87eb60958170c46767273bf93e6c96125bfJohn McCall // TODO: Perhaps we should let LLVM lower this? 411042c8f87eb60958170c46767273bf93e6c96125bfJohn McCall LValue Base; 411142c8f87eb60958170c46767273bf93e6c96125bfJohn McCall if (!EvaluatePointer(E->getArg(0), Base, Info)) 411242c8f87eb60958170c46767273bf93e6c96125bfJohn McCall return false; 411342c8f87eb60958170c46767273bf93e6c96125bfJohn McCall 411442c8f87eb60958170c46767273bf93e6c96125bfJohn McCall // If we can prove the base is null, lower to zero now. 41151bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith if (!Base.getLValueBase()) return Success(0, E); 411642c8f87eb60958170c46767273bf93e6c96125bfJohn McCall 41171bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith QualType T = GetObjectType(Base.getLValueBase()); 411842c8f87eb60958170c46767273bf93e6c96125bfJohn McCall if (T.isNull() || 411942c8f87eb60958170c46767273bf93e6c96125bfJohn McCall T->isIncompleteType() || 41201357869bc5983cdfbc986db1f3d18265bb34cb0eEli Friedman T->isFunctionType() || 412142c8f87eb60958170c46767273bf93e6c96125bfJohn McCall T->isVariablyModifiedType() || 412242c8f87eb60958170c46767273bf93e6c96125bfJohn McCall T->isDependentType()) 4123f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 412442c8f87eb60958170c46767273bf93e6c96125bfJohn McCall 412542c8f87eb60958170c46767273bf93e6c96125bfJohn McCall CharUnits Size = Info.Ctx.getTypeSizeInChars(T); 412642c8f87eb60958170c46767273bf93e6c96125bfJohn McCall CharUnits Offset = Base.getLValueOffset(); 412742c8f87eb60958170c46767273bf93e6c96125bfJohn McCall 412842c8f87eb60958170c46767273bf93e6c96125bfJohn McCall if (!Offset.isNegative() && Offset <= Size) 412942c8f87eb60958170c46767273bf93e6c96125bfJohn McCall Size -= Offset; 413042c8f87eb60958170c46767273bf93e6c96125bfJohn McCall else 413142c8f87eb60958170c46767273bf93e6c96125bfJohn McCall Size = CharUnits::Zero(); 41324f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck return Success(Size, E); 413342c8f87eb60958170c46767273bf93e6c96125bfJohn McCall} 413442c8f87eb60958170c46767273bf93e6c96125bfJohn McCall 41358cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { 4136180f47959a066795cc0f409433023af448bb0328Richard Smith switch (E->isBuiltinCall()) { 4137019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner default: 41388cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return ExprEvaluatorBaseTy::VisitCallExpr(E); 413964eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump 414064eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump case Builtin::BI__builtin_object_size: { 414142c8f87eb60958170c46767273bf93e6c96125bfJohn McCall if (TryEvaluateBuiltinObjectSize(E)) 414242c8f87eb60958170c46767273bf93e6c96125bfJohn McCall return true; 414364eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump 4144b2aaf51ed73a4774322a39a0dd59d0fe7e3258c0Eric Christopher // If evaluating the argument has side-effects we can't determine 4145b2aaf51ed73a4774322a39a0dd59d0fe7e3258c0Eric Christopher // the size of the object and lower it to unknown now. 4146393c247fe025ccb5f914e37e948192ea86faef8cFariborz Jahanian if (E->getArg(0)->HasSideEffects(Info.Ctx)) { 4147a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith if (E->getArg(1)->EvaluateKnownConstInt(Info.Ctx).getZExtValue() <= 1) 4148cf184655319cf7a5b811067cff9d26a5741fd161Chris Lattner return Success(-1ULL, E); 414964eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump return Success(0, E); 415064eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump } 4151c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump 4152f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 415364eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump } 415464eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump 4155019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner case Builtin::BI__builtin_classify_type: 4156131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success(EvaluateBuiltinClassifyType(E), E); 41571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 415880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith case Builtin::BI__builtin_constant_p: 415980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith return Success(EvaluateBuiltinConstantP(Info.Ctx, E->getArg(0)), E); 4160e052d46f4db91f9ba572859ffc984e85cbf5d5ffRichard Smith 416121fb98ee003e992b0c4e204d98a19e0ef544cae3Chris Lattner case Builtin::BI__builtin_eh_return_data_regno: { 4162a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith int Operand = E->getArg(0)->EvaluateKnownConstInt(Info.Ctx).getZExtValue(); 4163bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor Operand = Info.Ctx.getTargetInfo().getEHDataRegisterNumber(Operand); 416421fb98ee003e992b0c4e204d98a19e0ef544cae3Chris Lattner return Success(Operand, E); 416521fb98ee003e992b0c4e204d98a19e0ef544cae3Chris Lattner } 4166c4a2638b5ef3e2d35d872614ceb655a7a22c58beEli Friedman 4167c4a2638b5ef3e2d35d872614ceb655a7a22c58beEli Friedman case Builtin::BI__builtin_expect: 4168c4a2638b5ef3e2d35d872614ceb655a7a22c58beEli Friedman return Visit(E->getArg(0)); 416940b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith 41705726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor case Builtin::BIstrlen: 417140b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith // A call to strlen is not a constant expression. 417240b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith if (Info.getLangOpts().CPlusPlus0x) 417340b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_invalid_function) 417440b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith << /*isConstexpr*/0 << /*isConstructor*/0 << "'strlen'"; 417540b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith else 417640b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith Info.CCEDiag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); 417740b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith // Fall through. 41785726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor case Builtin::BI__builtin_strlen: 41795726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor // As an extension, we support strlen() and __builtin_strlen() as constant 41805726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor // expressions when the argument is a string literal. 41818cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne if (const StringLiteral *S 41825726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor = dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenImpCasts())) { 41835726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor // The string literal may have embedded null characters. Find the first 41845726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor // one and truncate there. 41855f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef Str = S->getString(); 41865f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef::size_type Pos = Str.find(0); 41875f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner if (Pos != StringRef::npos) 41885726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor Str = Str.substr(0, Pos); 41895726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor 41905726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor return Success(Str.size(), E); 41915726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor } 41925726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor 4193f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 4194454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman 4195454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman case Builtin::BI__atomic_is_lock_free: { 4196454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman APSInt SizeVal; 4197454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman if (!EvaluateInteger(E->getArg(0), SizeVal, Info)) 4198454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman return false; 4199454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman 4200454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman // For __atomic_is_lock_free(sizeof(_Atomic(T))), if the size is a power 4201454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman // of two less than the maximum inline atomic width, we know it is 4202454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman // lock-free. If the size isn't a power of two, or greater than the 4203454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman // maximum alignment where we promote atomics, we know it is not lock-free 4204454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman // (at least not in the sense of atomic_is_lock_free). Otherwise, 4205454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman // the answer can only be determined at runtime; for example, 16-byte 4206454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman // atomics have lock-free implementations on some, but not all, 4207454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman // x86-64 processors. 4208454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman 4209454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman // Check power-of-two. 4210454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman CharUnits Size = CharUnits::fromQuantity(SizeVal.getZExtValue()); 4211454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman if (!Size.isPowerOfTwo()) 4212454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman#if 0 4213454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman // FIXME: Suppress this folding until the ABI for the promotion width 4214454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman // settles. 4215454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman return Success(0, E); 4216454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman#else 4217f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 4218454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman#endif 4219454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman 4220454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman#if 0 4221454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman // Check against promotion width. 4222454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman // FIXME: Suppress this folding until the ABI for the promotion width 4223454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman // settles. 4224454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman unsigned PromoteWidthBits = 4225454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman Info.Ctx.getTargetInfo().getMaxAtomicPromoteWidth(); 4226454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman if (Size > Info.Ctx.toCharUnitsFromBits(PromoteWidthBits)) 4227454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman return Success(0, E); 4228454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman#endif 4229454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman 4230454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman // Check against inlining width. 4231454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman unsigned InlineWidthBits = 4232454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth(); 4233454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman if (Size <= Info.Ctx.toCharUnitsFromBits(InlineWidthBits)) 4234454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman return Success(1, E); 4235454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman 4236f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 4237454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman } 4238019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner } 42394c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner} 4240f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner 4241625b80755b603d28f36fb4212c81484d87ad08d3Richard Smithstatic bool HasSameBase(const LValue &A, const LValue &B) { 4242625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith if (!A.getLValueBase()) 4243625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith return !B.getLValueBase(); 4244625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith if (!B.getLValueBase()) 4245625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith return false; 4246625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith 42471bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith if (A.getLValueBase().getOpaqueValue() != 42481bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith B.getLValueBase().getOpaqueValue()) { 4249625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith const Decl *ADecl = GetLValueBaseDecl(A); 4250625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith if (!ADecl) 4251625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith return false; 4252625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith const Decl *BDecl = GetLValueBaseDecl(B); 42539a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith if (!BDecl || ADecl->getCanonicalDecl() != BDecl->getCanonicalDecl()) 4254625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith return false; 4255625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith } 4256625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith 4257625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith return IsGlobalLValue(A.getLValueBase()) || 4258177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith A.getLValueFrame() == B.getLValueFrame(); 4259625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith} 4260625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith 42617b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith/// Perform the given integer operation, which is known to need at most BitWidth 42627b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith/// bits, and check for overflow in the original type (if that type was not an 42637b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith/// unsigned type). 42647b48a2986345480241f3b8209f71bb21b0530b4fRichard Smithtemplate<typename Operation> 42657b48a2986345480241f3b8209f71bb21b0530b4fRichard Smithstatic APSInt CheckedIntArithmetic(EvalInfo &Info, const Expr *E, 42667b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith const APSInt &LHS, const APSInt &RHS, 42677b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith unsigned BitWidth, Operation Op) { 42687b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith if (LHS.isUnsigned()) 42697b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith return Op(LHS, RHS); 42707b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith 42717b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith APSInt Value(Op(LHS.extend(BitWidth), RHS.extend(BitWidth)), false); 42727b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith APSInt Result = Value.trunc(LHS.getBitWidth()); 42737b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith if (Result.extend(BitWidth) != Value) 42747b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith HandleOverflow(Info, E, Value, E->getType()); 42757b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith return Result; 42767b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith} 42777b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith 4278b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattnerbool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { 4279c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith if (E->isAssignmentOp()) 4280f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 4281c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 42822de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (E->getOpcode() == BO_Comma) { 42838327fad71da34492d82c532f42a58cb4baff81a3Richard Smith VisitIgnoredValue(E->getLHS()); 42848327fad71da34492d82c532f42a58cb4baff81a3Richard Smith return Visit(E->getRHS()); 4285a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman } 4286a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman 4287a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman if (E->isLogicalOp()) { 4288a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman // These need to be handled specially because the operands aren't 4289a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman // necessarily integral 4290fcb4d09531abbf2a5cf398c2f946fb3bc2875f64Anders Carlsson bool lhsResult, rhsResult; 42911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4292c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith if (EvaluateAsBooleanCondition(E->getLHS(), lhsResult, Info)) { 429351fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson // We were able to evaluate the LHS, see if we can get away with not 429451fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson // evaluating the RHS: 0 && X -> 0, 1 || X -> 1 42952de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (lhsResult == (E->getOpcode() == BO_LOr)) 42963f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar return Success(lhsResult, E); 4297a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman 4298c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith if (EvaluateAsBooleanCondition(E->getRHS(), rhsResult, Info)) { 42992de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (E->getOpcode() == BO_LOr) 4300131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success(lhsResult || rhsResult, E); 43014bbc0e05a714d4ee4918a92a4a7049dd6ef33ad0Anders Carlsson else 4302131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success(lhsResult && rhsResult, E); 43034bbc0e05a714d4ee4918a92a4a7049dd6ef33ad0Anders Carlsson } 43044bbc0e05a714d4ee4918a92a4a7049dd6ef33ad0Anders Carlsson } else { 4305f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith // FIXME: If both evaluations fail, we should produce the diagnostic from 4306f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith // the LHS. If the LHS is non-constant and the RHS is unevaluatable, it's 4307f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith // less clear how to diagnose this. 4308c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith if (EvaluateAsBooleanCondition(E->getRHS(), rhsResult, Info)) { 43094bbc0e05a714d4ee4918a92a4a7049dd6ef33ad0Anders Carlsson // We can't evaluate the LHS; however, sometimes the result 43104bbc0e05a714d4ee4918a92a4a7049dd6ef33ad0Anders Carlsson // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. 4311f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (rhsResult == (E->getOpcode() == BO_LOr)) { 4312131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar // Since we weren't able to evaluate the left hand side, it 4313fcb4d09531abbf2a5cf398c2f946fb3bc2875f64Anders Carlsson // must have had side effects. 43141e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith Info.EvalStatus.HasSideEffects = true; 4315131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar 4316131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success(rhsResult, E); 43174bbc0e05a714d4ee4918a92a4a7049dd6ef33ad0Anders Carlsson } 43184bbc0e05a714d4ee4918a92a4a7049dd6ef33ad0Anders Carlsson } 4319a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman } 4320a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman 4321a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman return false; 4322c8cc9ccc7b87a7ed1749b074f6b670bcec49abc1Chris Lattner } 432354176fdb044312b4b77c3da6682d3575b3728d30Chris Lattner 4324286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson QualType LHSTy = E->getLHS()->getType(); 4325286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson QualType RHSTy = E->getRHS()->getType(); 43264087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar 43274087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar if (LHSTy->isAnyComplexType()) { 43284087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar assert(RHSTy->isAnyComplexType() && "Invalid comparison"); 4329f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall ComplexValue LHS, RHS; 43304087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar 4331745f5147e065900267c85a5568785a1991d4838fRichard Smith bool LHSOK = EvaluateComplex(E->getLHS(), LHS, Info); 4332745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!LHSOK && !Info.keepEvaluatingAfterFailure()) 43334087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar return false; 43344087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar 4335745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK) 43364087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar return false; 43374087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar 43384087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar if (LHS.isComplexFloat()) { 43391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump APFloat::cmpResult CR_r = 43404087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal()); 43411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump APFloat::cmpResult CR_i = 43424087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag()); 43434087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar 43442de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (E->getOpcode() == BO_EQ) 4345131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success((CR_r == APFloat::cmpEqual && 4346131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar CR_i == APFloat::cmpEqual), E); 4347131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar else { 43482de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall assert(E->getOpcode() == BO_NE && 4349131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar "Invalid complex comparison."); 43501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return Success(((CR_r == APFloat::cmpGreaterThan || 4351fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang CR_r == APFloat::cmpLessThan || 4352fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang CR_r == APFloat::cmpUnordered) || 43531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump (CR_i == APFloat::cmpGreaterThan || 4354fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang CR_i == APFloat::cmpLessThan || 4355fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang CR_i == APFloat::cmpUnordered)), E); 4356131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar } 43574087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar } else { 43582de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (E->getOpcode() == BO_EQ) 4359131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() && 4360131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar LHS.getComplexIntImag() == RHS.getComplexIntImag()), E); 4361131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar else { 43622de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall assert(E->getOpcode() == BO_NE && 4363131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar "Invalid compex comparison."); 4364131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() || 4365131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar LHS.getComplexIntImag() != RHS.getComplexIntImag()), E); 4366131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar } 43674087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar } 43684087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar } 43691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4370286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson if (LHSTy->isRealFloatingType() && 4371286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson RHSTy->isRealFloatingType()) { 4372286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson APFloat RHS(0.0), LHS(0.0); 43731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4374745f5147e065900267c85a5568785a1991d4838fRichard Smith bool LHSOK = EvaluateFloat(E->getRHS(), RHS, Info); 4375745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!LHSOK && !Info.keepEvaluatingAfterFailure()) 4376286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson return false; 43771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4378745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!EvaluateFloat(E->getLHS(), LHS, Info) || !LHSOK) 4379286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson return false; 43801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4381286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson APFloat::cmpResult CR = LHS.compare(RHS); 4382529569e68d10b0fd3750fd2124faf742249b846bAnders Carlsson 4383286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson switch (E->getOpcode()) { 4384286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson default: 4385b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie llvm_unreachable("Invalid binary operator!"); 43862de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_LT: 4387131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success(CR == APFloat::cmpLessThan, E); 43882de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_GT: 4389131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success(CR == APFloat::cmpGreaterThan, E); 43902de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_LE: 4391131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E); 43922de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_GE: 43931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual, 4394131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar E); 43952de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_EQ: 4396131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success(CR == APFloat::cmpEqual, E); 43972de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_NE: 43981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return Success(CR == APFloat::cmpGreaterThan 4399fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang || CR == APFloat::cmpLessThan 4400fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang || CR == APFloat::cmpUnordered, E); 4401286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson } 4402286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson } 44031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4404ad02d7debd03ff275ac8ea27891a4ecccdb78068Eli Friedman if (LHSTy->isPointerType() && RHSTy->isPointerType()) { 4405625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith if (E->getOpcode() == BO_Sub || E->isComparisonOp()) { 4406745f5147e065900267c85a5568785a1991d4838fRichard Smith LValue LHSValue, RHSValue; 4407745f5147e065900267c85a5568785a1991d4838fRichard Smith 4408745f5147e065900267c85a5568785a1991d4838fRichard Smith bool LHSOK = EvaluatePointer(E->getLHS(), LHSValue, Info); 4409745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!LHSOK && Info.keepEvaluatingAfterFailure()) 44103068d117951a8df54bae9db039b56201ab10962bAnders Carlsson return false; 4411a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman 4412745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!EvaluatePointer(E->getRHS(), RHSValue, Info) || !LHSOK) 44133068d117951a8df54bae9db039b56201ab10962bAnders Carlsson return false; 4414a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman 4415625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith // Reject differing bases from the normal codepath; we special-case 4416625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith // comparisons to null. 4417625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith if (!HasSameBase(LHSValue, RHSValue)) { 441865639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman if (E->getOpcode() == BO_Sub) { 441965639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman // Handle &&A - &&B. 442065639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman if (!LHSValue.Offset.isZero() || !RHSValue.Offset.isZero()) 442165639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman return false; 442265639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman const Expr *LHSExpr = LHSValue.Base.dyn_cast<const Expr*>(); 442365639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman const Expr *RHSExpr = LHSValue.Base.dyn_cast<const Expr*>(); 442465639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman if (!LHSExpr || !RHSExpr) 442565639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman return false; 442665639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); 442765639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); 442865639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman if (!LHSAddrExpr || !RHSAddrExpr) 442965639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman return false; 44305930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman // Make sure both labels come from the same function. 44315930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman if (LHSAddrExpr->getLabel()->getDeclContext() != 44325930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman RHSAddrExpr->getLabel()->getDeclContext()) 44335930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman return false; 443465639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman Result = CCValue(LHSAddrExpr, RHSAddrExpr); 443565639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman return true; 443665639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman } 44379e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith // Inequalities and subtractions between unrelated pointers have 44389e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith // unspecified or undefined behavior. 44395bc86103767c2abcbfdd6518e0ccbbbb6aa59e0fEli Friedman if (!E->isEqualityOp()) 4440f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 4441ffbda40a1fb7169591dc01771f3511178a2f727cEli Friedman // A constant address may compare equal to the address of a symbol. 4442ffbda40a1fb7169591dc01771f3511178a2f727cEli Friedman // The one exception is that address of an object cannot compare equal 4443c45061bd0c0fdad4df8eea7e9e5af186d11427e5Eli Friedman // to a null pointer constant. 4444ffbda40a1fb7169591dc01771f3511178a2f727cEli Friedman if ((!LHSValue.Base && !LHSValue.Offset.isZero()) || 4445ffbda40a1fb7169591dc01771f3511178a2f727cEli Friedman (!RHSValue.Base && !RHSValue.Offset.isZero())) 4446f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 44479e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith // It's implementation-defined whether distinct literals will have 4448b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith // distinct addresses. In clang, the result of such a comparison is 4449b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith // unspecified, so it is not a constant expression. However, we do know 4450b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith // that the address of a literal will be non-null. 445174f4634781cee06e28eb741bda5d0f936fdd1948Richard Smith if ((IsLiteralLValue(LHSValue) || IsLiteralLValue(RHSValue)) && 445274f4634781cee06e28eb741bda5d0f936fdd1948Richard Smith LHSValue.Base && RHSValue.Base) 4453f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 44549e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith // We can't tell whether weak symbols will end up pointing to the same 44559e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith // object. 44569e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith if (IsWeakLValue(LHSValue) || IsWeakLValue(RHSValue)) 4457f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 44589e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith // Pointers with different bases cannot represent the same object. 4459c45061bd0c0fdad4df8eea7e9e5af186d11427e5Eli Friedman // (Note that clang defaults to -fmerge-all-constants, which can 4460c45061bd0c0fdad4df8eea7e9e5af186d11427e5Eli Friedman // lead to inconsistent results for comparisons involving the address 4461c45061bd0c0fdad4df8eea7e9e5af186d11427e5Eli Friedman // of a constant; this generally doesn't matter in practice.) 44629e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith return Success(E->getOpcode() == BO_NE, E); 44635bc86103767c2abcbfdd6518e0ccbbbb6aa59e0fEli Friedman } 4464a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman 446515efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith const CharUnits &LHSOffset = LHSValue.getLValueOffset(); 446615efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith const CharUnits &RHSOffset = RHSValue.getLValueOffset(); 446715efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith 4468f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator(); 4469f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator(); 4470f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 44712de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (E->getOpcode() == BO_Sub) { 4472f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // C++11 [expr.add]p6: 4473f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // Unless both pointers point to elements of the same array object, or 4474f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // one past the last element of the array object, the behavior is 4475f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // undefined. 4476f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && 4477f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith !AreElementsOfSameArray(getType(LHSValue.Base), 4478f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith LHSDesignator, RHSDesignator)) 4479f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith CCEDiag(E, diag::note_constexpr_pointer_subtraction_not_same_array); 4480f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 44814992bdde387c5f033bb450a716eaabc0fda52688Chris Lattner QualType Type = E->getLHS()->getType(); 44824992bdde387c5f033bb450a716eaabc0fda52688Chris Lattner QualType ElementType = Type->getAs<PointerType>()->getPointeeType(); 44833068d117951a8df54bae9db039b56201ab10962bAnders Carlsson 4484180f47959a066795cc0f409433023af448bb0328Richard Smith CharUnits ElementSize; 4485180f47959a066795cc0f409433023af448bb0328Richard Smith if (!HandleSizeof(Info, ElementType, ElementSize)) 4486180f47959a066795cc0f409433023af448bb0328Richard Smith return false; 4487a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman 448815efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith // FIXME: LLVM and GCC both compute LHSOffset - RHSOffset at runtime, 448915efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith // and produce incorrect results when it overflows. Such behavior 449015efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith // appears to be non-conforming, but is common, so perhaps we should 449115efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith // assume the standard intended for such cases to be undefined behavior 449215efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith // and check for them. 449315efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith 449415efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith // Compute (LHSOffset - RHSOffset) / Size carefully, checking for 449515efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith // overflow in the final conversion to ptrdiff_t. 449615efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith APSInt LHS( 449715efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith llvm::APInt(65, (int64_t)LHSOffset.getQuantity(), true), false); 449815efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith APSInt RHS( 449915efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith llvm::APInt(65, (int64_t)RHSOffset.getQuantity(), true), false); 450015efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith APSInt ElemSize( 450115efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith llvm::APInt(65, (int64_t)ElementSize.getQuantity(), true), false); 450215efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith APSInt TrueResult = (LHS - RHS) / ElemSize; 450315efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith APSInt Result = TrueResult.trunc(Info.Ctx.getIntWidth(E->getType())); 450415efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith 450515efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith if (Result.extend(65) != TrueResult) 450615efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith HandleOverflow(Info, E, TrueResult, E->getType()); 450715efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith return Success(Result, E); 4508ad02d7debd03ff275ac8ea27891a4ecccdb78068Eli Friedman } 4509625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith 451082f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith // C++11 [expr.rel]p3: 451182f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith // Pointers to void (after pointer conversions) can be compared, with a 451282f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith // result defined as follows: If both pointers represent the same 451382f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith // address or are both the null pointer value, the result is true if the 451482f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith // operator is <= or >= and false otherwise; otherwise the result is 451582f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith // unspecified. 451682f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith // We interpret this as applying to pointers to *cv* void. 451782f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith if (LHSTy->isVoidPointerType() && LHSOffset != RHSOffset && 4518f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith E->isRelationalOp()) 451982f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith CCEDiag(E, diag::note_constexpr_void_comparison); 452082f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith 4521f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // C++11 [expr.rel]p2: 4522f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // - If two pointers point to non-static data members of the same object, 4523f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // or to subobjects or array elements fo such members, recursively, the 4524f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // pointer to the later declared member compares greater provided the 4525f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // two members have the same access control and provided their class is 4526f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // not a union. 4527f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // [...] 4528f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // - Otherwise pointer comparisons are unspecified. 4529f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && 4530f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith E->isRelationalOp()) { 4531f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith bool WasArrayIndex; 4532f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith unsigned Mismatch = 4533f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith FindDesignatorMismatch(getType(LHSValue.Base), LHSDesignator, 4534f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith RHSDesignator, WasArrayIndex); 4535f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // At the point where the designators diverge, the comparison has a 4536f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // specified value if: 4537f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // - we are comparing array indices 4538f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // - we are comparing fields of a union, or fields with the same access 4539f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // Otherwise, the result is unspecified and thus the comparison is not a 4540f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // constant expression. 4541f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (!WasArrayIndex && Mismatch < LHSDesignator.Entries.size() && 4542f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith Mismatch < RHSDesignator.Entries.size()) { 4543f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith const FieldDecl *LF = getAsField(LHSDesignator.Entries[Mismatch]); 4544f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith const FieldDecl *RF = getAsField(RHSDesignator.Entries[Mismatch]); 4545f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (!LF && !RF) 4546f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith CCEDiag(E, diag::note_constexpr_pointer_comparison_base_classes); 4547f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith else if (!LF) 4548f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) 4549f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith << getAsBaseClass(LHSDesignator.Entries[Mismatch]) 4550f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith << RF->getParent() << RF; 4551f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith else if (!RF) 4552f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) 4553f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith << getAsBaseClass(RHSDesignator.Entries[Mismatch]) 4554f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith << LF->getParent() << LF; 4555f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith else if (!LF->getParent()->isUnion() && 4556f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith LF->getAccess() != RF->getAccess()) 4557f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith CCEDiag(E, diag::note_constexpr_pointer_comparison_differing_access) 4558f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith << LF << LF->getAccess() << RF << RF->getAccess() 4559f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith << LF->getParent(); 4560f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith } 4561f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith } 4562f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 4563625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith switch (E->getOpcode()) { 4564625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith default: llvm_unreachable("missing comparison operator"); 4565625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith case BO_LT: return Success(LHSOffset < RHSOffset, E); 4566625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith case BO_GT: return Success(LHSOffset > RHSOffset, E); 4567625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith case BO_LE: return Success(LHSOffset <= RHSOffset, E); 4568625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith case BO_GE: return Success(LHSOffset >= RHSOffset, E); 4569625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith case BO_EQ: return Success(LHSOffset == RHSOffset, E); 4570625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith case BO_NE: return Success(LHSOffset != RHSOffset, E); 4571ad02d7debd03ff275ac8ea27891a4ecccdb78068Eli Friedman } 45723068d117951a8df54bae9db039b56201ab10962bAnders Carlsson } 45733068d117951a8df54bae9db039b56201ab10962bAnders Carlsson } 4574b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith 4575b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith if (LHSTy->isMemberPointerType()) { 4576b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith assert(E->isEqualityOp() && "unexpected member pointer operation"); 4577b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith assert(RHSTy->isMemberPointerType() && "invalid comparison"); 4578b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith 4579b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith MemberPtr LHSValue, RHSValue; 4580b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith 4581b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith bool LHSOK = EvaluateMemberPointer(E->getLHS(), LHSValue, Info); 4582b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith if (!LHSOK && Info.keepEvaluatingAfterFailure()) 4583b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith return false; 4584b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith 4585b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith if (!EvaluateMemberPointer(E->getRHS(), RHSValue, Info) || !LHSOK) 4586b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith return false; 4587b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith 4588b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith // C++11 [expr.eq]p2: 4589b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith // If both operands are null, they compare equal. Otherwise if only one is 4590b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith // null, they compare unequal. 4591b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith if (!LHSValue.getDecl() || !RHSValue.getDecl()) { 4592b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith bool Equal = !LHSValue.getDecl() && !RHSValue.getDecl(); 4593b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith return Success(E->getOpcode() == BO_EQ ? Equal : !Equal, E); 4594b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith } 4595b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith 4596b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith // Otherwise if either is a pointer to a virtual member function, the 4597b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith // result is unspecified. 4598b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(LHSValue.getDecl())) 4599b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith if (MD->isVirtual()) 4600b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; 4601b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(RHSValue.getDecl())) 4602b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith if (MD->isVirtual()) 4603b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; 4604b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith 4605b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith // Otherwise they compare equal if and only if they would refer to the 4606b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith // same member of the same most derived object or the same subobject if 4607b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith // they were dereferenced with a hypothetical object of the associated 4608b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith // class type. 4609b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith bool Equal = LHSValue == RHSValue; 4610b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith return Success(E->getOpcode() == BO_EQ ? Equal : !Equal, E); 4611b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith } 4612b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith 46132ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor if (!LHSTy->isIntegralOrEnumerationType() || 46142ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor !RHSTy->isIntegralOrEnumerationType()) { 4615e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // We can't continue from here for non-integral types. 4616e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return ExprEvaluatorBaseTy::VisitBinaryOperator(E); 4617a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman } 4618a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman 4619a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman // The LHS of a constant expr is always evaluated and needed. 462047a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith CCValue LHSVal; 4621745f5147e065900267c85a5568785a1991d4838fRichard Smith 4622745f5147e065900267c85a5568785a1991d4838fRichard Smith bool LHSOK = EvaluateIntegerOrLValue(E->getLHS(), LHSVal, Info); 4623745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!LHSOK && !Info.keepEvaluatingAfterFailure()) 4624f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 4625d9f4bcda18bfbf79341edd9d381d4b6a3cffe655Eli Friedman 4626745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!Visit(E->getRHS()) || !LHSOK) 462730c37f4d2ee5811e85f692c22fb67d74ddc88079Daniel Dunbar return false; 4628745f5147e065900267c85a5568785a1991d4838fRichard Smith 462947a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith CCValue &RHSVal = Result; 463042edd0d32a729d2735a6fb152ba6bf349bf0a169Eli Friedman 463142edd0d32a729d2735a6fb152ba6bf349bf0a169Eli Friedman // Handle cases like (unsigned long)&a + 4. 4632c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith if (E->isAdditiveOp() && LHSVal.isLValue() && RHSVal.isInt()) { 4633a73058324197b7bdfd19307965954f626e26199dKen Dyck CharUnits AdditionalOffset = CharUnits::fromQuantity( 4634a73058324197b7bdfd19307965954f626e26199dKen Dyck RHSVal.getInt().getZExtValue()); 46352de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (E->getOpcode() == BO_Add) 463647a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith LHSVal.getLValueOffset() += AdditionalOffset; 463742edd0d32a729d2735a6fb152ba6bf349bf0a169Eli Friedman else 463847a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith LHSVal.getLValueOffset() -= AdditionalOffset; 463947a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith Result = LHSVal; 464042edd0d32a729d2735a6fb152ba6bf349bf0a169Eli Friedman return true; 464142edd0d32a729d2735a6fb152ba6bf349bf0a169Eli Friedman } 464242edd0d32a729d2735a6fb152ba6bf349bf0a169Eli Friedman 464342edd0d32a729d2735a6fb152ba6bf349bf0a169Eli Friedman // Handle cases like 4 + (unsigned long)&a 46442de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (E->getOpcode() == BO_Add && 4645c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith RHSVal.isLValue() && LHSVal.isInt()) { 464647a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith RHSVal.getLValueOffset() += CharUnits::fromQuantity( 464747a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith LHSVal.getInt().getZExtValue()); 464847a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith // Note that RHSVal is Result. 464942edd0d32a729d2735a6fb152ba6bf349bf0a169Eli Friedman return true; 465042edd0d32a729d2735a6fb152ba6bf349bf0a169Eli Friedman } 465142edd0d32a729d2735a6fb152ba6bf349bf0a169Eli Friedman 465265639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman if (E->getOpcode() == BO_Sub && LHSVal.isLValue() && RHSVal.isLValue()) { 465365639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman // Handle (intptr_t)&&A - (intptr_t)&&B. 465465639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman if (!LHSVal.getLValueOffset().isZero() || 465565639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman !RHSVal.getLValueOffset().isZero()) 465665639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman return false; 465765639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman const Expr *LHSExpr = LHSVal.getLValueBase().dyn_cast<const Expr*>(); 465865639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman const Expr *RHSExpr = RHSVal.getLValueBase().dyn_cast<const Expr*>(); 465965639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman if (!LHSExpr || !RHSExpr) 466065639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman return false; 466165639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); 466265639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); 466365639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman if (!LHSAddrExpr || !RHSAddrExpr) 466465639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman return false; 46655930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman // Make sure both labels come from the same function. 46665930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman if (LHSAddrExpr->getLabel()->getDeclContext() != 46675930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman RHSAddrExpr->getLabel()->getDeclContext()) 46685930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman return false; 466965639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman Result = CCValue(LHSAddrExpr, RHSAddrExpr); 467065639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman return true; 467165639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman } 467265639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman 467342edd0d32a729d2735a6fb152ba6bf349bf0a169Eli Friedman // All the following cases expect both operands to be an integer 4674c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith if (!LHSVal.isInt() || !RHSVal.isInt()) 4675f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 4676a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman 4677c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith APSInt &LHS = LHSVal.getInt(); 4678c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith APSInt &RHS = RHSVal.getInt(); 467942edd0d32a729d2735a6fb152ba6bf349bf0a169Eli Friedman 4680a25ae3d68d84d2b89907f998df6a396549589da5Anders Carlsson switch (E->getOpcode()) { 468132fea9d18cc3658a1b01df5ca6f2ac302625c61dChris Lattner default: 4682f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 46837b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith case BO_Mul: 46847b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith return Success(CheckedIntArithmetic(Info, E, LHS, RHS, 46857b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith LHS.getBitWidth() * 2, 46867b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith std::multiplies<APSInt>()), E); 46877b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith case BO_Add: 46887b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith return Success(CheckedIntArithmetic(Info, E, LHS, RHS, 46897b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith LHS.getBitWidth() + 1, 46907b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith std::plus<APSInt>()), E); 46917b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith case BO_Sub: 46927b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith return Success(CheckedIntArithmetic(Info, E, LHS, RHS, 46937b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith LHS.getBitWidth() + 1, 46947b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith std::minus<APSInt>()), E); 4695c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case BO_And: return Success(LHS & RHS, E); 4696c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case BO_Xor: return Success(LHS ^ RHS, E); 4697c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case BO_Or: return Success(LHS | RHS, E); 46982de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Div: 46993df61308ddfbdba0897b762a129b5a38750c87d0Richard Smith case BO_Rem: 470054176fdb044312b4b77c3da6682d3575b3728d30Chris Lattner if (RHS == 0) 4701f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E, diag::note_expr_divide_by_zero); 47023df61308ddfbdba0897b762a129b5a38750c87d0Richard Smith // Check for overflow case: INT_MIN / -1 or INT_MIN % -1. The latter is not 47033df61308ddfbdba0897b762a129b5a38750c87d0Richard Smith // actually undefined behavior in C++11 due to a language defect. 4704789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith if (RHS.isNegative() && RHS.isAllOnesValue() && 4705789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith LHS.isSigned() && LHS.isMinSignedValue()) 4706789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith HandleOverflow(Info, E, -LHS.extend(LHS.getBitWidth() + 1), E->getType()); 47073df61308ddfbdba0897b762a129b5a38750c87d0Richard Smith return Success(E->getOpcode() == BO_Rem ? LHS % RHS : LHS / RHS, E); 47082de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Shl: { 4709789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith // During constant-folding, a negative shift is an opposite shift. Such a 4710789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith // shift is not a constant expression. 4711091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall if (RHS.isSigned() && RHS.isNegative()) { 4712789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; 4713091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall RHS = -RHS; 4714091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall goto shift_right; 4715091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall } 4716091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall 4717091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall shift_left: 4718789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith // C++11 [expr.shift]p1: Shift width must be less than the bit width of the 4719789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith // shifted type. 4720789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); 4721789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith if (SA != RHS) { 4722789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith CCEDiag(E, diag::note_constexpr_large_shift) 4723789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith << RHS << E->getType() << LHS.getBitWidth(); 4724789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith } else if (LHS.isSigned()) { 4725789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith // C++11 [expr.shift]p2: A signed left shift must have a non-negative 4726925d8e7c0f18e03dc4bc634b3c6c1ec09373d993Richard Smith // operand, and must not overflow the corresponding unsigned type. 4727789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith if (LHS.isNegative()) 4728789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith CCEDiag(E, diag::note_constexpr_lshift_of_negative) << LHS; 4729925d8e7c0f18e03dc4bc634b3c6c1ec09373d993Richard Smith else if (LHS.countLeadingZeros() < SA) 4730925d8e7c0f18e03dc4bc634b3c6c1ec09373d993Richard Smith CCEDiag(E, diag::note_constexpr_lshift_discards); 4731789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith } 4732789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith 4733c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return Success(LHS << SA, E); 47343f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar } 47352de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Shr: { 4736789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith // During constant-folding, a negative shift is an opposite shift. Such a 4737789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith // shift is not a constant expression. 4738091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall if (RHS.isSigned() && RHS.isNegative()) { 4739789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; 4740091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall RHS = -RHS; 4741091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall goto shift_left; 4742091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall } 4743091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall 4744091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall shift_right: 4745789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith // C++11 [expr.shift]p1: Shift width must be less than the bit width of the 4746789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith // shifted type. 4747789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); 4748789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith if (SA != RHS) 4749789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith CCEDiag(E, diag::note_constexpr_large_shift) 4750789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith << RHS << E->getType() << LHS.getBitWidth(); 4751789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith 4752c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return Success(LHS >> SA, E); 47533f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar } 47541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4755c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case BO_LT: return Success(LHS < RHS, E); 4756c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case BO_GT: return Success(LHS > RHS, E); 4757c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case BO_LE: return Success(LHS <= RHS, E); 4758c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case BO_GE: return Success(LHS >= RHS, E); 4759c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case BO_EQ: return Success(LHS == RHS, E); 4760c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case BO_NE: return Success(LHS != RHS, E); 4761b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman } 4762a25ae3d68d84d2b89907f998df6a396549589da5Anders Carlsson} 4763a25ae3d68d84d2b89907f998df6a396549589da5Anders Carlsson 47648b752f10c394b140f9ef89e049cbad1a7676fc25Ken DyckCharUnits IntExprEvaluator::GetAlignOfType(QualType T) { 47655d484e8cf710207010720589d89602233de61d01Sebastian Redl // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, 47665d484e8cf710207010720589d89602233de61d01Sebastian Redl // the result is the size of the referenced type." 47675d484e8cf710207010720589d89602233de61d01Sebastian Redl // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the 47685d484e8cf710207010720589d89602233de61d01Sebastian Redl // result shall be the alignment of the referenced type." 47695d484e8cf710207010720589d89602233de61d01Sebastian Redl if (const ReferenceType *Ref = T->getAs<ReferenceType>()) 47705d484e8cf710207010720589d89602233de61d01Sebastian Redl T = Ref->getPointeeType(); 47719f1210c3280104417a4ad30f0a00825ac8fa718aChad Rosier 47729f1210c3280104417a4ad30f0a00825ac8fa718aChad Rosier // __alignof is defined to return the preferred alignment. 47739f1210c3280104417a4ad30f0a00825ac8fa718aChad Rosier return Info.Ctx.toCharUnitsFromBits( 47749f1210c3280104417a4ad30f0a00825ac8fa718aChad Rosier Info.Ctx.getPreferredTypeAlign(T.getTypePtr())); 4775e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner} 4776e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner 47778b752f10c394b140f9ef89e049cbad1a7676fc25Ken DyckCharUnits IntExprEvaluator::GetAlignOfExpr(const Expr *E) { 4778af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner E = E->IgnoreParens(); 4779af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner 4780af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner // alignof decl is always accepted, even if it doesn't make sense: we default 47811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // to 1 in those cases. 4782af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) 47838b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck return Info.Ctx.getDeclAlign(DRE->getDecl(), 47848b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck /*RefAsPointee*/true); 4785a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman 4786af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) 47878b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck return Info.Ctx.getDeclAlign(ME->getMemberDecl(), 47888b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck /*RefAsPointee*/true); 4789af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner 4790e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner return GetAlignOfType(E->getType()); 4791e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner} 4792e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner 4793e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner 4794f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne/// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with 4795f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne/// a result as the expression's type. 4796f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbournebool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr( 4797f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne const UnaryExprOrTypeTraitExpr *E) { 4798f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne switch(E->getKind()) { 4799f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne case UETT_AlignOf: { 4800e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner if (E->isArgumentType()) 48014f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck return Success(GetAlignOfType(E->getArgumentType()), E); 4802e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner else 48034f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck return Success(GetAlignOfExpr(E->getArgumentExpr()), E); 4804e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner } 4805a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman 4806f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne case UETT_VecStep: { 4807f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne QualType Ty = E->getTypeOfArgument(); 48080518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl 4809f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne if (Ty->isVectorType()) { 4810f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne unsigned n = Ty->getAs<VectorType>()->getNumElements(); 4811a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman 4812f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne // The vec_step built-in functions that take a 3-component 4813f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne // vector return 4. (OpenCL 1.1 spec 6.11.12) 4814f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne if (n == 3) 4815f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne n = 4; 4816f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne 4817f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne return Success(n, E); 4818f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne } else 4819f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne return Success(1, E); 4820f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne } 4821f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne 4822f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne case UETT_SizeOf: { 4823f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne QualType SrcTy = E->getTypeOfArgument(); 4824f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, 4825f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne // the result is the size of the referenced type." 4826f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the 4827f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne // result shall be the alignment of the referenced type." 4828f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>()) 4829f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne SrcTy = Ref->getPointeeType(); 4830f2da9dfef96dc11b7b5effb1d02cb427b2d71599Eli Friedman 4831180f47959a066795cc0f409433023af448bb0328Richard Smith CharUnits Sizeof; 4832180f47959a066795cc0f409433023af448bb0328Richard Smith if (!HandleSizeof(Info, SrcTy, Sizeof)) 4833f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne return false; 4834180f47959a066795cc0f409433023af448bb0328Richard Smith return Success(Sizeof, E); 4835f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne } 4836f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne } 4837f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne 4838f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne llvm_unreachable("unknown expr/type trait"); 4839fcee0019b76f9f368f2b3d6d4048a98232593f29Chris Lattner} 4840fcee0019b76f9f368f2b3d6d4048a98232593f29Chris Lattner 48418cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) { 48428ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor CharUnits Result; 48438cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne unsigned n = OOE->getNumComponents(); 48448ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor if (n == 0) 4845f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(OOE); 48468cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne QualType CurrentType = OOE->getTypeSourceInfo()->getType(); 48478ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor for (unsigned i = 0; i != n; ++i) { 48488ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor OffsetOfExpr::OffsetOfNode ON = OOE->getComponent(i); 48498ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor switch (ON.getKind()) { 48508ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor case OffsetOfExpr::OffsetOfNode::Array: { 48518cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne const Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex()); 48528ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor APSInt IdxResult; 48538ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor if (!EvaluateInteger(Idx, IdxResult, Info)) 48548ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor return false; 48558ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType); 48568ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor if (!AT) 4857f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(OOE); 48588ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor CurrentType = AT->getElementType(); 48598ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType); 48608ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor Result += IdxResult.getSExtValue() * ElementSize; 48618ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor break; 48628ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor } 4863f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 48648ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor case OffsetOfExpr::OffsetOfNode::Field: { 48658ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor FieldDecl *MemberDecl = ON.getField(); 48668ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor const RecordType *RT = CurrentType->getAs<RecordType>(); 4867f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!RT) 4868f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(OOE); 48698ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor RecordDecl *RD = RT->getDecl(); 48708ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); 4871ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall unsigned i = MemberDecl->getFieldIndex(); 4872cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor assert(i < RL.getFieldCount() && "offsetof field in wrong type"); 4873fb1e3bc29b667f4275e1d5a43d64ec173f4f9a7dKen Dyck Result += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i)); 48748ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor CurrentType = MemberDecl->getType().getNonReferenceType(); 48758ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor break; 48768ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor } 4877f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 48788ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor case OffsetOfExpr::OffsetOfNode::Identifier: 48798ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor llvm_unreachable("dependent __builtin_offsetof"); 4880f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 4881cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor case OffsetOfExpr::OffsetOfNode::Base: { 4882cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor CXXBaseSpecifier *BaseSpec = ON.getBase(); 4883cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor if (BaseSpec->isVirtual()) 4884f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(OOE); 4885cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor 4886cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor // Find the layout of the class whose base we are looking into. 4887cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor const RecordType *RT = CurrentType->getAs<RecordType>(); 4888f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!RT) 4889f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(OOE); 4890cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor RecordDecl *RD = RT->getDecl(); 4891cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); 4892cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor 4893cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor // Find the base class itself. 4894cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor CurrentType = BaseSpec->getType(); 4895cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor const RecordType *BaseRT = CurrentType->getAs<RecordType>(); 4896cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor if (!BaseRT) 4897f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(OOE); 4898cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor 4899cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor // Add the offset to the base. 49007c7f820d70c925b29290a8563b59615816a827fcKen Dyck Result += RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl())); 4901cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor break; 4902cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor } 49038ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor } 49048ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor } 49058cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return Success(Result, OOE); 49068ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor} 49078ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor 4908b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattnerbool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { 490975a4881047deeb3a300ff9293dc6ba8570048bb5Chris Lattner switch (E->getOpcode()) { 49104c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner default: 491175a4881047deeb3a300ff9293dc6ba8570048bb5Chris Lattner // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. 491275a4881047deeb3a300ff9293dc6ba8570048bb5Chris Lattner // See C99 6.6p3. 4913f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 49142de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_Extension: 49154c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner // FIXME: Should extension allow i-c-e extension expressions in its scope? 49164c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner // If so, we could clear the diagnostic ID. 4917f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Visit(E->getSubExpr()); 49182de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_Plus: 4919c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // The result is just the value. 4920f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Visit(E->getSubExpr()); 4921f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith case UO_Minus: { 4922f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!Visit(E->getSubExpr())) 4923f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 4924f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!Result.isInt()) return Error(E); 4925789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith const APSInt &Value = Result.getInt(); 4926789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith if (Value.isSigned() && Value.isMinSignedValue()) 4927789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith HandleOverflow(Info, E, -Value.extend(Value.getBitWidth() + 1), 4928789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith E->getType()); 4929789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith return Success(-Value, E); 4930f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 4931f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith case UO_Not: { 4932f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!Visit(E->getSubExpr())) 4933f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 4934f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!Result.isInt()) return Error(E); 4935f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Success(~Result.getInt(), E); 4936f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 4937f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith case UO_LNot: { 4938f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith bool bres; 4939f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!EvaluateAsBooleanCondition(E->getSubExpr(), bres, Info)) 4940f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 4941f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Success(!bres, E); 4942f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 494306a3675627e3b3c47b49c689c8e404a33144194aAnders Carlsson } 4944a25ae3d68d84d2b89907f998df6a396549589da5Anders Carlsson} 49451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4946732b2236ae4a4f11e7642677cebbd169c07ea877Chris Lattner/// HandleCast - This is used to evaluate implicit or explicit casts where the 4947732b2236ae4a4f11e7642677cebbd169c07ea877Chris Lattner/// result type is integer. 49488cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool IntExprEvaluator::VisitCastExpr(const CastExpr *E) { 49498cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne const Expr *SubExpr = E->getSubExpr(); 495082206e267ce6cc709797127616f64672d255b310Anders Carlsson QualType DestType = E->getType(); 4951b92dac8bc2f6f73919825f9af693a8a7e89ae1d4Daniel Dunbar QualType SrcType = SubExpr->getType(); 495282206e267ce6cc709797127616f64672d255b310Anders Carlsson 495346a523285928aa07bf14803178dc04616ac85994Eli Friedman switch (E->getCastKind()) { 495446a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_BaseToDerived: 495546a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_DerivedToBase: 495646a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_UncheckedDerivedToBase: 495746a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_Dynamic: 495846a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_ToUnion: 495946a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_ArrayToPointerDecay: 496046a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_FunctionToPointerDecay: 496146a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_NullToPointer: 496246a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_NullToMemberPointer: 496346a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_BaseToDerivedMemberPointer: 496446a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_DerivedToBaseMemberPointer: 496546a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_ConstructorConversion: 496646a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_IntegralToPointer: 496746a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_ToVoid: 496846a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_VectorSplat: 496946a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_IntegralToFloating: 497046a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_FloatingCast: 49711d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall case CK_CPointerToObjCPointerCast: 49721d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall case CK_BlockPointerToObjCPointerCast: 497346a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_AnyPointerToBlockPointerCast: 497446a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_ObjCObjectLValueCast: 497546a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_FloatingRealToComplex: 497646a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_FloatingComplexToReal: 497746a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_FloatingComplexCast: 497846a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_FloatingComplexToIntegralComplex: 497946a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_IntegralRealToComplex: 498046a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_IntegralComplexCast: 498146a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_IntegralComplexToFloatingComplex: 498246a523285928aa07bf14803178dc04616ac85994Eli Friedman llvm_unreachable("invalid cast kind for integral value"); 498346a523285928aa07bf14803178dc04616ac85994Eli Friedman 4984e50c297f92914ca996deb8b597624193273b62e4Eli Friedman case CK_BitCast: 498546a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_Dependent: 498646a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_LValueBitCast: 498733e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCProduceObject: 498833e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCConsumeObject: 498933e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCReclaimReturnedObject: 499033e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCExtendBlockObject: 4991f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 499246a523285928aa07bf14803178dc04616ac85994Eli Friedman 49937d580a4e9e47dffc3c17aa2b957ac57ca3c4e451Richard Smith case CK_UserDefinedConversion: 499446a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_LValueToRValue: 49957a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall case CK_AtomicToNonAtomic: 49967a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall case CK_NonAtomicToAtomic: 499746a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_NoOp: 4998c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return ExprEvaluatorBaseTy::VisitCastExpr(E); 499946a523285928aa07bf14803178dc04616ac85994Eli Friedman 500046a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_MemberPointerToBoolean: 500146a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_PointerToBoolean: 500246a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_IntegralToBoolean: 500346a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_FloatingToBoolean: 500446a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_FloatingComplexToBoolean: 500546a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_IntegralComplexToBoolean: { 50064efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman bool BoolResult; 5007c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith if (!EvaluateAsBooleanCondition(SubExpr, BoolResult, Info)) 50084efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman return false; 5009131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success(BoolResult, E); 50104efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman } 50114efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 501246a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_IntegralCast: { 5013732b2236ae4a4f11e7642677cebbd169c07ea877Chris Lattner if (!Visit(SubExpr)) 5014b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattner return false; 5015a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar 5016be26570e3faa009bdcefedfaf04473e518940520Eli Friedman if (!Result.isInt()) { 501765639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman // Allow casts of address-of-label differences if they are no-ops 501865639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman // or narrowing. (The narrowing case isn't actually guaranteed to 501965639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman // be constant-evaluatable except in some narrow cases which are hard 502065639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman // to detect here. We let it through on the assumption the user knows 502165639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman // what they are doing.) 502265639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman if (Result.isAddrLabelDiff()) 502365639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman return Info.Ctx.getTypeSize(DestType) <= Info.Ctx.getTypeSize(SrcType); 5024be26570e3faa009bdcefedfaf04473e518940520Eli Friedman // Only allow casts of lvalues if they are lossless. 5025be26570e3faa009bdcefedfaf04473e518940520Eli Friedman return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType); 5026be26570e3faa009bdcefedfaf04473e518940520Eli Friedman } 502730c37f4d2ee5811e85f692c22fb67d74ddc88079Daniel Dunbar 5028f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith return Success(HandleIntToIntCast(Info, E, DestType, SrcType, 5029f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith Result.getInt()), E); 5030732b2236ae4a4f11e7642677cebbd169c07ea877Chris Lattner } 50311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 503246a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_PointerToIntegral: { 5033c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; 5034c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith 5035efdb83e26f9a1fd2566afe54461216cd84814d42John McCall LValue LV; 503687eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner if (!EvaluatePointer(SubExpr, LV, Info)) 5037b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattner return false; 50384efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 5039dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar if (LV.getLValueBase()) { 5040dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar // Only allow based lvalue casts if they are lossless. 5041f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith // FIXME: Allow a larger integer size than the pointer size, and allow 5042f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith // narrowing back down to pointer width in subsequent integral casts. 5043f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith // FIXME: Check integer type's active bits, not its type size. 5044dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType)) 5045f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 5046dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar 5047b755a9da095d2f2f04444797f1e1a9511693815bRichard Smith LV.Designator.setInvalid(); 5048efdb83e26f9a1fd2566afe54461216cd84814d42John McCall LV.moveInto(Result); 5049dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar return true; 5050dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar } 50514efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 5052a73058324197b7bdfd19307965954f626e26199dKen Dyck APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(), 5053a73058324197b7bdfd19307965954f626e26199dKen Dyck SrcType); 5054f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith return Success(HandleIntToIntCast(Info, E, DestType, SrcType, AsInt), E); 50552bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson } 50564efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 505746a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_IntegralComplexToReal: { 5058f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall ComplexValue C; 50591725f683432715e5afe34d476024bd6f16eac3fcEli Friedman if (!EvaluateComplex(SubExpr, C, Info)) 50601725f683432715e5afe34d476024bd6f16eac3fcEli Friedman return false; 506146a523285928aa07bf14803178dc04616ac85994Eli Friedman return Success(C.getComplexIntReal(), E); 50621725f683432715e5afe34d476024bd6f16eac3fcEli Friedman } 50632217c87bdc5ab357046a5453bdb06f469c41024eEli Friedman 506446a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_FloatingToIntegral: { 506546a523285928aa07bf14803178dc04616ac85994Eli Friedman APFloat F(0.0); 506646a523285928aa07bf14803178dc04616ac85994Eli Friedman if (!EvaluateFloat(SubExpr, F, Info)) 506746a523285928aa07bf14803178dc04616ac85994Eli Friedman return false; 5068732b2236ae4a4f11e7642677cebbd169c07ea877Chris Lattner 5069c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith APSInt Value; 5070c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (!HandleFloatToIntCast(Info, E, SrcType, F, DestType, Value)) 5071c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return false; 5072c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return Success(Value, E); 507346a523285928aa07bf14803178dc04616ac85994Eli Friedman } 507446a523285928aa07bf14803178dc04616ac85994Eli Friedman } 50751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 507646a523285928aa07bf14803178dc04616ac85994Eli Friedman llvm_unreachable("unknown cast resulting in integral value"); 5077a25ae3d68d84d2b89907f998df6a396549589da5Anders Carlsson} 50782bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson 5079722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedmanbool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { 5080722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman if (E->getSubExpr()->getType()->isAnyComplexType()) { 5081f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall ComplexValue LV; 5082f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!EvaluateComplex(E->getSubExpr(), LV, Info)) 5083f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 5084f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!LV.isComplexInt()) 5085f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 5086722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman return Success(LV.getComplexIntReal(), E); 5087722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman } 5088722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman 5089722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman return Visit(E->getSubExpr()); 5090722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman} 5091722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman 5092664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedmanbool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { 5093722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman if (E->getSubExpr()->getType()->isComplexIntegerType()) { 5094f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall ComplexValue LV; 5095f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!EvaluateComplex(E->getSubExpr(), LV, Info)) 5096f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 5097f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!LV.isComplexInt()) 5098f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 5099722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman return Success(LV.getComplexIntImag(), E); 5100722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman } 5101722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman 51028327fad71da34492d82c532f42a58cb4baff81a3Richard Smith VisitIgnoredValue(E->getSubExpr()); 5103664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman return Success(0, E); 5104664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman} 5105664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman 5106ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregorbool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) { 5107ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor return Success(E->getPackLength(), E); 5108ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor} 5109ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor 5110295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redlbool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) { 5111295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl return Success(E->getValue(), E); 5112295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl} 5113295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl 5114f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===// 5115d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman// Float Evaluation 5116d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman//===----------------------------------------------------------------------===// 5117d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman 5118d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmannamespace { 5119770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass FloatExprEvaluator 51208cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne : public ExprEvaluatorBase<FloatExprEvaluator, bool> { 5121d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman APFloat &Result; 5122d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmanpublic: 5123d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman FloatExprEvaluator(EvalInfo &info, APFloat &result) 51248cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne : ExprEvaluatorBaseTy(info), Result(result) {} 5125d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman 512647a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith bool Success(const CCValue &V, const Expr *e) { 51278cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne Result = V.getFloat(); 51288cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return true; 51298cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne } 5130d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman 513151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith bool ZeroInitialization(const Expr *E) { 5132f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType())); 5133f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith return true; 5134f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith } 5135f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith 5136019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner bool VisitCallExpr(const CallExpr *E); 5137d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman 51385db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar bool VisitUnaryOperator(const UnaryOperator *E); 5139d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman bool VisitBinaryOperator(const BinaryOperator *E); 5140d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman bool VisitFloatingLiteral(const FloatingLiteral *E); 51418cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitCastExpr(const CastExpr *E); 51422217c87bdc5ab357046a5453bdb06f469c41024eEli Friedman 5143abd3a857ace59100305790545d1baae5877b8945John McCall bool VisitUnaryReal(const UnaryOperator *E); 5144abd3a857ace59100305790545d1baae5877b8945John McCall bool VisitUnaryImag(const UnaryOperator *E); 5145ba98d6bb414861965a1f22628494ea046785ecd4Eli Friedman 514651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith // FIXME: Missing: array subscript of vector, member of vector 5147d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman}; 5148d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman} // end anonymous namespace 5149d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman 5150d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmanstatic bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { 5151c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith assert(E->isRValue() && E->getType()->isRealFloatingType()); 51528cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return FloatExprEvaluator(Info, Result).Visit(E); 5153d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman} 5154d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman 51554ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadstatic bool TryEvaluateBuiltinNaN(const ASTContext &Context, 5156db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall QualType ResultTy, 5157db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall const Expr *Arg, 5158db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall bool SNaN, 5159db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall llvm::APFloat &Result) { 5160db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts()); 5161db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall if (!S) return false; 5162db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall 5163db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy); 5164db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall 5165db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall llvm::APInt fill; 5166db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall 5167db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall // Treat empty strings as if they were zero. 5168db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall if (S->getString().empty()) 5169db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall fill = llvm::APInt(32, 0); 5170db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall else if (S->getString().getAsInteger(0, fill)) 5171db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall return false; 5172db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall 5173db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall if (SNaN) 5174db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall Result = llvm::APFloat::getSNaN(Sem, false, &fill); 5175db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall else 5176db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall Result = llvm::APFloat::getQNaN(Sem, false, &fill); 5177db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall return true; 5178db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall} 5179db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall 5180019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattnerbool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { 5181180f47959a066795cc0f409433023af448bb0328Richard Smith switch (E->isBuiltinCall()) { 51828cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne default: 51838cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return ExprEvaluatorBaseTy::VisitCallExpr(E); 51848cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 5185019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner case Builtin::BI__builtin_huge_val: 5186019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner case Builtin::BI__builtin_huge_valf: 5187019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner case Builtin::BI__builtin_huge_vall: 5188019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner case Builtin::BI__builtin_inf: 5189019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner case Builtin::BI__builtin_inff: 51907cbed03c00e246682e5292785d01e1c120ce54bdDaniel Dunbar case Builtin::BI__builtin_infl: { 51917cbed03c00e246682e5292785d01e1c120ce54bdDaniel Dunbar const llvm::fltSemantics &Sem = 51927cbed03c00e246682e5292785d01e1c120ce54bdDaniel Dunbar Info.Ctx.getFloatTypeSemantics(E->getType()); 519334a74ab81600a40c6324fd76adb724b803dfaf91Chris Lattner Result = llvm::APFloat::getInf(Sem); 519434a74ab81600a40c6324fd76adb724b803dfaf91Chris Lattner return true; 51957cbed03c00e246682e5292785d01e1c120ce54bdDaniel Dunbar } 51961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5197db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall case Builtin::BI__builtin_nans: 5198db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall case Builtin::BI__builtin_nansf: 5199db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall case Builtin::BI__builtin_nansl: 5200f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), 5201f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith true, Result)) 5202f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 5203f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return true; 5204db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall 52059e62171a25e3a08fb5c49fb370f83faf5ae786f5Chris Lattner case Builtin::BI__builtin_nan: 52069e62171a25e3a08fb5c49fb370f83faf5ae786f5Chris Lattner case Builtin::BI__builtin_nanf: 52079e62171a25e3a08fb5c49fb370f83faf5ae786f5Chris Lattner case Builtin::BI__builtin_nanl: 52084572baba9d18c275968ac113fd73b0e3c77cccb8Mike Stump // If this is __builtin_nan() turn this into a nan, otherwise we 52099e62171a25e3a08fb5c49fb370f83faf5ae786f5Chris Lattner // can't constant fold it. 5210f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), 5211f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith false, Result)) 5212f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 5213f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return true; 52145db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar 52155db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar case Builtin::BI__builtin_fabs: 52165db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar case Builtin::BI__builtin_fabsf: 52175db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar case Builtin::BI__builtin_fabsl: 52185db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar if (!EvaluateFloat(E->getArg(0), Result, Info)) 52195db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar return false; 52201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 52215db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar if (Result.isNegative()) 52225db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar Result.changeSign(); 52235db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar return true; 52245db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar 52251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump case Builtin::BI__builtin_copysign: 52261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump case Builtin::BI__builtin_copysignf: 52275db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar case Builtin::BI__builtin_copysignl: { 52285db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar APFloat RHS(0.); 52295db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar if (!EvaluateFloat(E->getArg(0), Result, Info) || 52305db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar !EvaluateFloat(E->getArg(1), RHS, Info)) 52315db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar return false; 52325db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar Result.copySign(RHS); 52335db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar return true; 52345db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar } 5235019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner } 5236019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner} 5237019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner 5238abd3a857ace59100305790545d1baae5877b8945John McCallbool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { 523943efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman if (E->getSubExpr()->getType()->isAnyComplexType()) { 524043efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman ComplexValue CV; 524143efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman if (!EvaluateComplex(E->getSubExpr(), CV, Info)) 524243efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman return false; 524343efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman Result = CV.FloatReal; 524443efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman return true; 524543efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman } 524643efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman 524743efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman return Visit(E->getSubExpr()); 5248abd3a857ace59100305790545d1baae5877b8945John McCall} 5249abd3a857ace59100305790545d1baae5877b8945John McCall 5250abd3a857ace59100305790545d1baae5877b8945John McCallbool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { 525143efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman if (E->getSubExpr()->getType()->isAnyComplexType()) { 525243efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman ComplexValue CV; 525343efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman if (!EvaluateComplex(E->getSubExpr(), CV, Info)) 525443efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman return false; 525543efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman Result = CV.FloatImag; 525643efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman return true; 525743efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman } 525843efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman 52598327fad71da34492d82c532f42a58cb4baff81a3Richard Smith VisitIgnoredValue(E->getSubExpr()); 526043efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(E->getType()); 526143efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman Result = llvm::APFloat::getZero(Sem); 5262abd3a857ace59100305790545d1baae5877b8945John McCall return true; 5263abd3a857ace59100305790545d1baae5877b8945John McCall} 5264abd3a857ace59100305790545d1baae5877b8945John McCall 52655db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbarbool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { 52665db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar switch (E->getOpcode()) { 5267f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith default: return Error(E); 52682de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_Plus: 52697993e8a2a26bf408c2a6d45f24fffa12db336b2bRichard Smith return EvaluateFloat(E->getSubExpr(), Result, Info); 52702de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_Minus: 52717993e8a2a26bf408c2a6d45f24fffa12db336b2bRichard Smith if (!EvaluateFloat(E->getSubExpr(), Result, Info)) 52727993e8a2a26bf408c2a6d45f24fffa12db336b2bRichard Smith return false; 52735db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar Result.changeSign(); 52745db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar return true; 52755db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar } 52765db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar} 5277019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner 5278d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmanbool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { 5279e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) 5280e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return ExprEvaluatorBaseTy::VisitBinaryOperator(E); 528196e93660124c8028a4c3bcc038ab0cdd18cd7ab2Anders Carlsson 52825db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar APFloat RHS(0.0); 5283745f5147e065900267c85a5568785a1991d4838fRichard Smith bool LHSOK = EvaluateFloat(E->getLHS(), Result, Info); 5284745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!LHSOK && !Info.keepEvaluatingAfterFailure()) 5285d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman return false; 5286745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!EvaluateFloat(E->getRHS(), RHS, Info) || !LHSOK) 5287d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman return false; 5288d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman 5289d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman switch (E->getOpcode()) { 5290f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith default: return Error(E); 52912de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Mul: 5292d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman Result.multiply(RHS, APFloat::rmNearestTiesToEven); 52937b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith break; 52942de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Add: 5295d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman Result.add(RHS, APFloat::rmNearestTiesToEven); 52967b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith break; 52972de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Sub: 5298d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman Result.subtract(RHS, APFloat::rmNearestTiesToEven); 52997b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith break; 53002de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Div: 5301d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman Result.divide(RHS, APFloat::rmNearestTiesToEven); 53027b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith break; 5303d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman } 53047b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith 53057b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith if (Result.isInfinity() || Result.isNaN()) 53067b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith CCEDiag(E, diag::note_constexpr_float_arithmetic) << Result.isNaN(); 53077b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith return true; 5308d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman} 5309d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman 5310d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmanbool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { 5311d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman Result = E->getValue(); 5312d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman return true; 5313d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman} 5314d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman 53158cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) { 53168cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne const Expr* SubExpr = E->getSubExpr(); 53171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 53182a523eec6a31955be876625819b89e8dc5def707Eli Friedman switch (E->getCastKind()) { 53192a523eec6a31955be876625819b89e8dc5def707Eli Friedman default: 5320c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return ExprEvaluatorBaseTy::VisitCastExpr(E); 53212a523eec6a31955be876625819b89e8dc5def707Eli Friedman 53222a523eec6a31955be876625819b89e8dc5def707Eli Friedman case CK_IntegralToFloating: { 53234efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman APSInt IntResult; 5324c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return EvaluateInteger(SubExpr, IntResult, Info) && 5325c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith HandleIntToFloatCast(Info, E, SubExpr->getType(), IntResult, 5326c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith E->getType(), Result); 53274efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman } 53282a523eec6a31955be876625819b89e8dc5def707Eli Friedman 53292a523eec6a31955be876625819b89e8dc5def707Eli Friedman case CK_FloatingCast: { 53304efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman if (!Visit(SubExpr)) 53314efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman return false; 5332c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return HandleFloatToFloatCast(Info, E, SubExpr->getType(), E->getType(), 5333c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Result); 53344efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman } 5335f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall 53362a523eec6a31955be876625819b89e8dc5def707Eli Friedman case CK_FloatingComplexToReal: { 5337f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall ComplexValue V; 5338f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall if (!EvaluateComplex(SubExpr, V, Info)) 5339f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall return false; 5340f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall Result = V.getComplexFloatReal(); 5341f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall return true; 5342f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall } 53432a523eec6a31955be876625819b89e8dc5def707Eli Friedman } 53444efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman} 53454efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 5346d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman//===----------------------------------------------------------------------===// 5347a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar// Complex Evaluation (for float and integer) 53489ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson//===----------------------------------------------------------------------===// 53499ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson 53509ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlssonnamespace { 5351770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass ComplexExprEvaluator 53528cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne : public ExprEvaluatorBase<ComplexExprEvaluator, bool> { 5353f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall ComplexValue &Result; 53541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 53559ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlssonpublic: 5356f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result) 53578cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne : ExprEvaluatorBaseTy(info), Result(Result) {} 53581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 535947a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith bool Success(const CCValue &V, const Expr *e) { 53608cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne Result.setFrom(V); 53618cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return true; 53628cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne } 53631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 53647ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman bool ZeroInitialization(const Expr *E); 53657ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman 53668cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne //===--------------------------------------------------------------------===// 53678cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne // Visitor Methods 53688cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne //===--------------------------------------------------------------------===// 53699ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson 53708cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitImaginaryLiteral(const ImaginaryLiteral *E); 53718cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitCastExpr(const CastExpr *E); 5372b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman bool VisitBinaryOperator(const BinaryOperator *E); 537396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara bool VisitUnaryOperator(const UnaryOperator *E); 53747ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman bool VisitInitListExpr(const InitListExpr *E); 5375b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman}; 5376b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman} // end anonymous namespace 53771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5378b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedmanstatic bool EvaluateComplex(const Expr *E, ComplexValue &Result, 5379b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman EvalInfo &Info) { 5380c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith assert(E->isRValue() && E->getType()->isAnyComplexType()); 53818cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return ComplexExprEvaluator(Info, Result).Visit(E); 5382b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman} 5383b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman 53847ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedmanbool ComplexExprEvaluator::ZeroInitialization(const Expr *E) { 5385f6c17a439f3320ac620639a3ee66dbdabb93810cEli Friedman QualType ElemTy = E->getType()->getAs<ComplexType>()->getElementType(); 53867ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman if (ElemTy->isRealFloatingType()) { 53877ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman Result.makeComplexFloat(); 53887ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman APFloat Zero = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(ElemTy)); 53897ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman Result.FloatReal = Zero; 53907ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman Result.FloatImag = Zero; 53917ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman } else { 53927ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman Result.makeComplexInt(); 53937ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman APSInt Zero = Info.Ctx.MakeIntValue(0, ElemTy); 53947ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman Result.IntReal = Zero; 53957ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman Result.IntImag = Zero; 53967ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman } 53977ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman return true; 53987ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman} 53997ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman 54008cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) { 54018cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne const Expr* SubExpr = E->getSubExpr(); 5402b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman 5403b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman if (SubExpr->getType()->isRealFloatingType()) { 5404b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman Result.makeComplexFloat(); 5405b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman APFloat &Imag = Result.FloatImag; 5406b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman if (!EvaluateFloat(SubExpr, Imag, Info)) 5407b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman return false; 5408b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman 5409b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman Result.FloatReal = APFloat(Imag.getSemantics()); 5410b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman return true; 5411b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman } else { 5412b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman assert(SubExpr->getType()->isIntegerType() && 5413b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman "Unexpected imaginary literal."); 5414b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman 5415b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman Result.makeComplexInt(); 5416b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman APSInt &Imag = Result.IntImag; 5417b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman if (!EvaluateInteger(SubExpr, Imag, Info)) 5418b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman return false; 5419b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman 5420b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned()); 5421b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman return true; 5422b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman } 5423b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman} 5424b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman 54258cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) { 5426b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman 54278786da77984e81d48e0e1b2bd339809b1efc19f3John McCall switch (E->getCastKind()) { 54288786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_BitCast: 54298786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_BaseToDerived: 54308786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_DerivedToBase: 54318786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_UncheckedDerivedToBase: 54328786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_Dynamic: 54338786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_ToUnion: 54348786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_ArrayToPointerDecay: 54358786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_FunctionToPointerDecay: 54368786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_NullToPointer: 54378786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_NullToMemberPointer: 54388786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_BaseToDerivedMemberPointer: 54398786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_DerivedToBaseMemberPointer: 54408786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_MemberPointerToBoolean: 54418786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_ConstructorConversion: 54428786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_IntegralToPointer: 54438786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_PointerToIntegral: 54448786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_PointerToBoolean: 54458786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_ToVoid: 54468786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_VectorSplat: 54478786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_IntegralCast: 54488786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_IntegralToBoolean: 54498786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_IntegralToFloating: 54508786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_FloatingToIntegral: 54518786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_FloatingToBoolean: 54528786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_FloatingCast: 54531d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall case CK_CPointerToObjCPointerCast: 54541d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall case CK_BlockPointerToObjCPointerCast: 54558786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_AnyPointerToBlockPointerCast: 54568786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_ObjCObjectLValueCast: 54578786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_FloatingComplexToReal: 54588786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_FloatingComplexToBoolean: 54598786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_IntegralComplexToReal: 54608786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_IntegralComplexToBoolean: 546133e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCProduceObject: 546233e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCConsumeObject: 546333e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCReclaimReturnedObject: 546433e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCExtendBlockObject: 54658786da77984e81d48e0e1b2bd339809b1efc19f3John McCall llvm_unreachable("invalid cast kind for complex value"); 54668786da77984e81d48e0e1b2bd339809b1efc19f3John McCall 54678786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_LValueToRValue: 54687a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall case CK_AtomicToNonAtomic: 54697a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall case CK_NonAtomicToAtomic: 54708786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_NoOp: 5471c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return ExprEvaluatorBaseTy::VisitCastExpr(E); 54722bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall 54738786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_Dependent: 547446a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_LValueBitCast: 54758786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_UserDefinedConversion: 5476f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 54778786da77984e81d48e0e1b2bd339809b1efc19f3John McCall 54788786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_FloatingRealToComplex: { 5479b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman APFloat &Real = Result.FloatReal; 54808786da77984e81d48e0e1b2bd339809b1efc19f3John McCall if (!EvaluateFloat(E->getSubExpr(), Real, Info)) 5481b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman return false; 5482b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman 54838786da77984e81d48e0e1b2bd339809b1efc19f3John McCall Result.makeComplexFloat(); 54848786da77984e81d48e0e1b2bd339809b1efc19f3John McCall Result.FloatImag = APFloat(Real.getSemantics()); 54858786da77984e81d48e0e1b2bd339809b1efc19f3John McCall return true; 54868786da77984e81d48e0e1b2bd339809b1efc19f3John McCall } 54878786da77984e81d48e0e1b2bd339809b1efc19f3John McCall 54888786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_FloatingComplexCast: { 54898786da77984e81d48e0e1b2bd339809b1efc19f3John McCall if (!Visit(E->getSubExpr())) 54908786da77984e81d48e0e1b2bd339809b1efc19f3John McCall return false; 54918786da77984e81d48e0e1b2bd339809b1efc19f3John McCall 54928786da77984e81d48e0e1b2bd339809b1efc19f3John McCall QualType To = E->getType()->getAs<ComplexType>()->getElementType(); 54938786da77984e81d48e0e1b2bd339809b1efc19f3John McCall QualType From 54948786da77984e81d48e0e1b2bd339809b1efc19f3John McCall = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); 54958786da77984e81d48e0e1b2bd339809b1efc19f3John McCall 5496c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return HandleFloatToFloatCast(Info, E, From, To, Result.FloatReal) && 5497c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith HandleFloatToFloatCast(Info, E, From, To, Result.FloatImag); 54988786da77984e81d48e0e1b2bd339809b1efc19f3John McCall } 54998786da77984e81d48e0e1b2bd339809b1efc19f3John McCall 55008786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_FloatingComplexToIntegralComplex: { 55018786da77984e81d48e0e1b2bd339809b1efc19f3John McCall if (!Visit(E->getSubExpr())) 55028786da77984e81d48e0e1b2bd339809b1efc19f3John McCall return false; 55038786da77984e81d48e0e1b2bd339809b1efc19f3John McCall 55048786da77984e81d48e0e1b2bd339809b1efc19f3John McCall QualType To = E->getType()->getAs<ComplexType>()->getElementType(); 55058786da77984e81d48e0e1b2bd339809b1efc19f3John McCall QualType From 55068786da77984e81d48e0e1b2bd339809b1efc19f3John McCall = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); 55078786da77984e81d48e0e1b2bd339809b1efc19f3John McCall Result.makeComplexInt(); 5508c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return HandleFloatToIntCast(Info, E, From, Result.FloatReal, 5509c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith To, Result.IntReal) && 5510c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith HandleFloatToIntCast(Info, E, From, Result.FloatImag, 5511c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith To, Result.IntImag); 55128786da77984e81d48e0e1b2bd339809b1efc19f3John McCall } 55138786da77984e81d48e0e1b2bd339809b1efc19f3John McCall 55148786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_IntegralRealToComplex: { 5515b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman APSInt &Real = Result.IntReal; 55168786da77984e81d48e0e1b2bd339809b1efc19f3John McCall if (!EvaluateInteger(E->getSubExpr(), Real, Info)) 5517b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman return false; 55189ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson 55198786da77984e81d48e0e1b2bd339809b1efc19f3John McCall Result.makeComplexInt(); 55208786da77984e81d48e0e1b2bd339809b1efc19f3John McCall Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned()); 55218786da77984e81d48e0e1b2bd339809b1efc19f3John McCall return true; 55228786da77984e81d48e0e1b2bd339809b1efc19f3John McCall } 55238786da77984e81d48e0e1b2bd339809b1efc19f3John McCall 55248786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_IntegralComplexCast: { 55258786da77984e81d48e0e1b2bd339809b1efc19f3John McCall if (!Visit(E->getSubExpr())) 5526b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman return false; 5527ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson 55288786da77984e81d48e0e1b2bd339809b1efc19f3John McCall QualType To = E->getType()->getAs<ComplexType>()->getElementType(); 55298786da77984e81d48e0e1b2bd339809b1efc19f3John McCall QualType From 55308786da77984e81d48e0e1b2bd339809b1efc19f3John McCall = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); 55311725f683432715e5afe34d476024bd6f16eac3fcEli Friedman 5532f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith Result.IntReal = HandleIntToIntCast(Info, E, To, From, Result.IntReal); 5533f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith Result.IntImag = HandleIntToIntCast(Info, E, To, From, Result.IntImag); 55348786da77984e81d48e0e1b2bd339809b1efc19f3John McCall return true; 55358786da77984e81d48e0e1b2bd339809b1efc19f3John McCall } 55368786da77984e81d48e0e1b2bd339809b1efc19f3John McCall 55378786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_IntegralComplexToFloatingComplex: { 55388786da77984e81d48e0e1b2bd339809b1efc19f3John McCall if (!Visit(E->getSubExpr())) 55398786da77984e81d48e0e1b2bd339809b1efc19f3John McCall return false; 55408786da77984e81d48e0e1b2bd339809b1efc19f3John McCall 55418786da77984e81d48e0e1b2bd339809b1efc19f3John McCall QualType To = E->getType()->getAs<ComplexType>()->getElementType(); 55428786da77984e81d48e0e1b2bd339809b1efc19f3John McCall QualType From 55438786da77984e81d48e0e1b2bd339809b1efc19f3John McCall = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); 55448786da77984e81d48e0e1b2bd339809b1efc19f3John McCall Result.makeComplexFloat(); 5545c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return HandleIntToFloatCast(Info, E, From, Result.IntReal, 5546c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith To, Result.FloatReal) && 5547c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith HandleIntToFloatCast(Info, E, From, Result.IntImag, 5548c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith To, Result.FloatImag); 55498786da77984e81d48e0e1b2bd339809b1efc19f3John McCall } 5550ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson } 55511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 55528786da77984e81d48e0e1b2bd339809b1efc19f3John McCall llvm_unreachable("unknown cast resulting in complex value"); 55539ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson} 55549ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson 5555f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCallbool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { 5556e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) 55572ad226bdc847df6b6b6e4f832856478ab63bb3dcRichard Smith return ExprEvaluatorBaseTy::VisitBinaryOperator(E); 55582ad226bdc847df6b6b6e4f832856478ab63bb3dcRichard Smith 5559745f5147e065900267c85a5568785a1991d4838fRichard Smith bool LHSOK = Visit(E->getLHS()); 5560745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!LHSOK && !Info.keepEvaluatingAfterFailure()) 5561f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall return false; 55621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5563f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall ComplexValue RHS; 5564745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK) 5565f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall return false; 5566a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar 55673f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar assert(Result.isComplexFloat() == RHS.isComplexFloat() && 55683f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar "Invalid operands to binary operator."); 5569ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson switch (E->getOpcode()) { 5570f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith default: return Error(E); 55712de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Add: 5572a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar if (Result.isComplexFloat()) { 5573a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar Result.getComplexFloatReal().add(RHS.getComplexFloatReal(), 5574a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar APFloat::rmNearestTiesToEven); 5575a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar Result.getComplexFloatImag().add(RHS.getComplexFloatImag(), 5576a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar APFloat::rmNearestTiesToEven); 5577a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar } else { 5578a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar Result.getComplexIntReal() += RHS.getComplexIntReal(); 5579a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar Result.getComplexIntImag() += RHS.getComplexIntImag(); 5580a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar } 55813f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar break; 55822de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Sub: 5583a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar if (Result.isComplexFloat()) { 5584a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(), 5585a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar APFloat::rmNearestTiesToEven); 5586a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(), 5587a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar APFloat::rmNearestTiesToEven); 5588a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar } else { 5589a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar Result.getComplexIntReal() -= RHS.getComplexIntReal(); 5590a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar Result.getComplexIntImag() -= RHS.getComplexIntImag(); 5591a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar } 55923f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar break; 55932de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Mul: 55943f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar if (Result.isComplexFloat()) { 5595f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall ComplexValue LHS = Result; 55963f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar APFloat &LHS_r = LHS.getComplexFloatReal(); 55973f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar APFloat &LHS_i = LHS.getComplexFloatImag(); 55983f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar APFloat &RHS_r = RHS.getComplexFloatReal(); 55993f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar APFloat &RHS_i = RHS.getComplexFloatImag(); 56001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 56013f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar APFloat Tmp = LHS_r; 56023f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); 56033f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar Result.getComplexFloatReal() = Tmp; 56043f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar Tmp = LHS_i; 56053f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); 56063f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven); 56073f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar 56083f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar Tmp = LHS_r; 56093f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); 56103f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar Result.getComplexFloatImag() = Tmp; 56113f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar Tmp = LHS_i; 56123f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); 56133f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven); 56143f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar } else { 5615f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall ComplexValue LHS = Result; 56161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Result.getComplexIntReal() = 56173f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar (LHS.getComplexIntReal() * RHS.getComplexIntReal() - 56183f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar LHS.getComplexIntImag() * RHS.getComplexIntImag()); 56191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Result.getComplexIntImag() = 56203f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar (LHS.getComplexIntReal() * RHS.getComplexIntImag() + 56213f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar LHS.getComplexIntImag() * RHS.getComplexIntReal()); 56223f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar } 56233f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar break; 562496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara case BO_Div: 562596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara if (Result.isComplexFloat()) { 562696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara ComplexValue LHS = Result; 562796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara APFloat &LHS_r = LHS.getComplexFloatReal(); 562896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara APFloat &LHS_i = LHS.getComplexFloatImag(); 562996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara APFloat &RHS_r = RHS.getComplexFloatReal(); 563096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara APFloat &RHS_i = RHS.getComplexFloatImag(); 563196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara APFloat &Res_r = Result.getComplexFloatReal(); 563296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara APFloat &Res_i = Result.getComplexFloatImag(); 563396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara 563496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara APFloat Den = RHS_r; 563596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Den.multiply(RHS_r, APFloat::rmNearestTiesToEven); 563696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara APFloat Tmp = RHS_i; 563796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); 563896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Den.add(Tmp, APFloat::rmNearestTiesToEven); 563996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara 564096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Res_r = LHS_r; 564196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Res_r.multiply(RHS_r, APFloat::rmNearestTiesToEven); 564296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Tmp = LHS_i; 564396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); 564496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Res_r.add(Tmp, APFloat::rmNearestTiesToEven); 564596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Res_r.divide(Den, APFloat::rmNearestTiesToEven); 564696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara 564796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Res_i = LHS_i; 564896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Res_i.multiply(RHS_r, APFloat::rmNearestTiesToEven); 564996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Tmp = LHS_r; 565096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); 565196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Res_i.subtract(Tmp, APFloat::rmNearestTiesToEven); 565296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Res_i.divide(Den, APFloat::rmNearestTiesToEven); 565396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara } else { 5654f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0) 5655f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E, diag::note_expr_divide_by_zero); 5656f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 565796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara ComplexValue LHS = Result; 565896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() + 565996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara RHS.getComplexIntImag() * RHS.getComplexIntImag(); 566096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Result.getComplexIntReal() = 566196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara (LHS.getComplexIntReal() * RHS.getComplexIntReal() + 566296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den; 566396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Result.getComplexIntImag() = 566496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara (LHS.getComplexIntImag() * RHS.getComplexIntReal() - 566596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den; 566696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara } 566796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara break; 5668ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson } 5669ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson 5670f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall return true; 5671ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson} 5672ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson 567396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnarabool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { 567496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara // Get the operand value into 'Result'. 567596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara if (!Visit(E->getSubExpr())) 567696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara return false; 567796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara 567896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara switch (E->getOpcode()) { 567996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara default: 5680f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 568196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara case UO_Extension: 568296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara return true; 568396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara case UO_Plus: 568496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara // The result is always just the subexpr. 568596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara return true; 568696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara case UO_Minus: 568796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara if (Result.isComplexFloat()) { 568896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Result.getComplexFloatReal().changeSign(); 568996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Result.getComplexFloatImag().changeSign(); 569096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara } 569196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara else { 569296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Result.getComplexIntReal() = -Result.getComplexIntReal(); 569396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Result.getComplexIntImag() = -Result.getComplexIntImag(); 569496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara } 569596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara return true; 569696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara case UO_Not: 569796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara if (Result.isComplexFloat()) 569896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Result.getComplexFloatImag().changeSign(); 569996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara else 570096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Result.getComplexIntImag() = -Result.getComplexIntImag(); 570196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara return true; 570296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara } 570396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara} 570496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara 57057ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedmanbool ComplexExprEvaluator::VisitInitListExpr(const InitListExpr *E) { 57067ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman if (E->getNumInits() == 2) { 57077ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman if (E->getType()->isComplexType()) { 57087ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman Result.makeComplexFloat(); 57097ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman if (!EvaluateFloat(E->getInit(0), Result.FloatReal, Info)) 57107ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman return false; 57117ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman if (!EvaluateFloat(E->getInit(1), Result.FloatImag, Info)) 57127ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman return false; 57137ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman } else { 57147ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman Result.makeComplexInt(); 57157ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman if (!EvaluateInteger(E->getInit(0), Result.IntReal, Info)) 57167ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman return false; 57177ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman if (!EvaluateInteger(E->getInit(1), Result.IntImag, Info)) 57187ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman return false; 57197ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman } 57207ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman return true; 57217ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman } 57227ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman return ExprEvaluatorBaseTy::VisitInitListExpr(E); 57237ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman} 57247ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman 57259ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson//===----------------------------------------------------------------------===// 5726aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith// Void expression evaluation, primarily for a cast to void on the LHS of a 5727aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith// comma operator 5728aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith//===----------------------------------------------------------------------===// 5729aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith 5730aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smithnamespace { 5731aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smithclass VoidExprEvaluator 5732aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith : public ExprEvaluatorBase<VoidExprEvaluator, bool> { 5733aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smithpublic: 5734aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith VoidExprEvaluator(EvalInfo &Info) : ExprEvaluatorBaseTy(Info) {} 5735aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith 5736aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith bool Success(const CCValue &V, const Expr *e) { return true; } 5737aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith 5738aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith bool VisitCastExpr(const CastExpr *E) { 5739aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith switch (E->getCastKind()) { 5740aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith default: 5741aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith return ExprEvaluatorBaseTy::VisitCastExpr(E); 5742aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith case CK_ToVoid: 5743aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith VisitIgnoredValue(E->getSubExpr()); 5744aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith return true; 5745aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith } 5746aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith } 5747aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith}; 5748aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith} // end anonymous namespace 5749aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith 5750aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smithstatic bool EvaluateVoid(const Expr *E, EvalInfo &Info) { 5751aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith assert(E->isRValue() && E->getType()->isVoidType()); 5752aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith return VoidExprEvaluator(Info).Visit(E); 5753aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith} 5754aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith 5755aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith//===----------------------------------------------------------------------===// 575651f4708c00110940ca3f337961915f2ca1668375Richard Smith// Top level Expr::EvaluateAsRValue method. 5757f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===// 5758f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner 575947a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smithstatic bool Evaluate(CCValue &Result, EvalInfo &Info, const Expr *E) { 5760c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // In C, function designators are not lvalues, but we evaluate them as if they 5761c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // are. 5762c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith if (E->isGLValue() || E->getType()->isFunctionType()) { 5763c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith LValue LV; 5764c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith if (!EvaluateLValue(E, LV, Info)) 5765c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return false; 5766c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith LV.moveInto(Result); 5767c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith } else if (E->getType()->isVectorType()) { 57681e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith if (!EvaluateVector(E, Result, Info)) 576959b5da6d853b4368b984700315adf7b37de05764Nate Begeman return false; 5770575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor } else if (E->getType()->isIntegralOrEnumerationType()) { 57711e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith if (!IntExprEvaluator(Info, Result).Visit(E)) 57726dde0d5dc09f45f4d9508c964703e36fef1a0198Anders Carlsson return false; 5773efdb83e26f9a1fd2566afe54461216cd84814d42John McCall } else if (E->getType()->hasPointerRepresentation()) { 5774efdb83e26f9a1fd2566afe54461216cd84814d42John McCall LValue LV; 5775efdb83e26f9a1fd2566afe54461216cd84814d42John McCall if (!EvaluatePointer(E, LV, Info)) 57766dde0d5dc09f45f4d9508c964703e36fef1a0198Anders Carlsson return false; 57771e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith LV.moveInto(Result); 5778efdb83e26f9a1fd2566afe54461216cd84814d42John McCall } else if (E->getType()->isRealFloatingType()) { 5779efdb83e26f9a1fd2566afe54461216cd84814d42John McCall llvm::APFloat F(0.0); 5780efdb83e26f9a1fd2566afe54461216cd84814d42John McCall if (!EvaluateFloat(E, F, Info)) 57816dde0d5dc09f45f4d9508c964703e36fef1a0198Anders Carlsson return false; 578247a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith Result = CCValue(F); 5783efdb83e26f9a1fd2566afe54461216cd84814d42John McCall } else if (E->getType()->isAnyComplexType()) { 5784efdb83e26f9a1fd2566afe54461216cd84814d42John McCall ComplexValue C; 5785efdb83e26f9a1fd2566afe54461216cd84814d42John McCall if (!EvaluateComplex(E, C, Info)) 5786660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump return false; 57871e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith C.moveInto(Result); 578869c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith } else if (E->getType()->isMemberPointerType()) { 5789e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith MemberPtr P; 5790e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!EvaluateMemberPointer(E, P, Info)) 5791e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 5792e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith P.moveInto(Result); 5793e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 579451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith } else if (E->getType()->isArrayType()) { 5795180f47959a066795cc0f409433023af448bb0328Richard Smith LValue LV; 57961bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith LV.set(E, Info.CurrentCall); 5797180f47959a066795cc0f409433023af448bb0328Richard Smith if (!EvaluateArray(E, LV, Info.CurrentCall->Temporaries[E], Info)) 5798cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith return false; 5799180f47959a066795cc0f409433023af448bb0328Richard Smith Result = Info.CurrentCall->Temporaries[E]; 580051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith } else if (E->getType()->isRecordType()) { 5801180f47959a066795cc0f409433023af448bb0328Richard Smith LValue LV; 58021bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith LV.set(E, Info.CurrentCall); 5803180f47959a066795cc0f409433023af448bb0328Richard Smith if (!EvaluateRecord(E, LV, Info.CurrentCall->Temporaries[E], Info)) 5804180f47959a066795cc0f409433023af448bb0328Richard Smith return false; 5805180f47959a066795cc0f409433023af448bb0328Richard Smith Result = Info.CurrentCall->Temporaries[E]; 5806aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith } else if (E->getType()->isVoidType()) { 5807c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (Info.getLangOpts().CPlusPlus0x) 5808c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_nonliteral) 5809c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith << E->getType(); 5810c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith else 5811c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Info.CCEDiag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); 5812aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith if (!EvaluateVoid(E, Info)) 5813aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith return false; 5814c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } else if (Info.getLangOpts().CPlusPlus0x) { 5815c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Info.Diag(E->getExprLoc(), diag::note_constexpr_nonliteral) << E->getType(); 5816c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return false; 5817f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } else { 5818dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); 5819660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump return false; 5820f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 5821660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump 5822660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump return true; 5823660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump} 5824660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump 582569c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith/// EvaluateConstantExpression - Evaluate an expression as a constant expression 582669c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith/// in-place in an APValue. In some cases, the in-place evaluation is essential, 582769c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith/// since later initializers for an object can indirectly refer to subobjects 582869c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith/// which were initialized earlier. 582969c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smithstatic bool EvaluateConstantExpression(APValue &Result, EvalInfo &Info, 5830c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const LValue &This, const Expr *E, 5831c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith CheckConstantExpressionKind CCEK) { 583251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (!CheckLiteralType(Info, E)) 583351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return false; 583451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 583551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (E->isRValue()) { 583669c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith // Evaluate arrays and record types in-place, so that later initializers can 583769c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith // refer to earlier-initialized members of the object. 5838180f47959a066795cc0f409433023af448bb0328Richard Smith if (E->getType()->isArrayType()) 5839180f47959a066795cc0f409433023af448bb0328Richard Smith return EvaluateArray(E, This, Result, Info); 5840180f47959a066795cc0f409433023af448bb0328Richard Smith else if (E->getType()->isRecordType()) 5841180f47959a066795cc0f409433023af448bb0328Richard Smith return EvaluateRecord(E, This, Result, Info); 584269c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith } 584369c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith 584469c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith // For any other type, in-place evaluation is unimportant. 584569c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith CCValue CoreConstResult; 584669c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith return Evaluate(CoreConstResult, Info, E) && 5847c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith CheckConstantExpression(Info, E, CoreConstResult, Result, CCEK); 584869c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith} 584969c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith 5850f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// EvaluateAsRValue - Try to evaluate this expression, performing an implicit 5851f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// lvalue-to-rvalue cast if it is an lvalue. 5852f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result) { 585351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (!CheckLiteralType(Info, E)) 585451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return false; 585551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 5856f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith CCValue Value; 5857f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!::Evaluate(Value, Info, E)) 5858f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 5859f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 5860f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (E->isGLValue()) { 5861f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith LValue LV; 5862f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith LV.setFrom(Value); 5863f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!HandleLValueToRValueConversion(Info, E, E->getType(), LV, Value)) 5864f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 5865f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 5866f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 5867f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith // Check this core constant expression is a constant expression, and if so, 5868f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith // convert it to one. 5869f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return CheckConstantExpression(Info, E, Value, Result); 5870f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith} 5871c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 587251f4708c00110940ca3f337961915f2ca1668375Richard Smith/// EvaluateAsRValue - Return true if this is a constant which we can fold using 587356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall/// any crazy technique (that has nothing to do with language standards) that 587456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall/// we want to. If this function returns true, it returns the folded constant 5875c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// in Result. If this expression is a glvalue, an lvalue-to-rvalue conversion 5876c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// will be applied to the result. 587751f4708c00110940ca3f337961915f2ca1668375Richard Smithbool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx) const { 5878ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith // Fast-path evaluations of integer literals, since we sometimes see files 5879ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith // containing vast quantities of these. 5880ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith if (const IntegerLiteral *L = dyn_cast<IntegerLiteral>(this)) { 5881ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith Result.Val = APValue(APSInt(L->getValue(), 5882ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith L->getType()->isUnsignedIntegerType())); 5883ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith return true; 5884ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith } 5885ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith 58862d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith // FIXME: Evaluating values of large array and record types can cause 58872d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith // performance problems. Only do so in C++11 for now. 5888e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (isRValue() && (getType()->isArrayType() || getType()->isRecordType()) && 5889e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith !Ctx.getLangOptions().CPlusPlus0x) 58901445bbacf4c8de5f208ff4ccb302424a4d9e233eRichard Smith return false; 58911445bbacf4c8de5f208ff4ccb302424a4d9e233eRichard Smith 5892f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith EvalInfo Info(Ctx, Result); 5893f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return ::EvaluateAsRValue(Info, this, Result.Val); 589456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall} 589556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall 58964ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool Expr::EvaluateAsBooleanCondition(bool &Result, 58974ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad const ASTContext &Ctx) const { 5898c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith EvalResult Scratch; 589951f4708c00110940ca3f337961915f2ca1668375Richard Smith return EvaluateAsRValue(Scratch, Ctx) && 5900b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith HandleConversionToBool(CCValue(const_cast<ASTContext&>(Ctx), 5901b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Scratch.Val, CCValue::GlobalValue()), 590247a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith Result); 5903cd7a445c6b46c5585580dfb652300c8483c0cb6bJohn McCall} 5904cd7a445c6b46c5585580dfb652300c8483c0cb6bJohn McCall 590580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smithbool Expr::EvaluateAsInt(APSInt &Result, const ASTContext &Ctx, 590680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith SideEffectsKind AllowSideEffects) const { 590780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith if (!getType()->isIntegralOrEnumerationType()) 590880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith return false; 590980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith 5910c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith EvalResult ExprResult; 591180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith if (!EvaluateAsRValue(ExprResult, Ctx) || !ExprResult.Val.isInt() || 591280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith (!AllowSideEffects && ExprResult.HasSideEffects)) 5913c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return false; 5914f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 5915c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith Result = ExprResult.Val.getInt(); 5916c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return true; 5917a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith} 5918a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith 59194ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx) const { 59201b78276a75a5a0f496a82429c1ff9604d622a76dAnders Carlsson EvalInfo Info(Ctx, Result); 59211b78276a75a5a0f496a82429c1ff9604d622a76dAnders Carlsson 5922efdb83e26f9a1fd2566afe54461216cd84814d42John McCall LValue LV; 59239a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith return EvaluateLValue(this, LV, Info) && !Result.HasSideEffects && 5924c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith CheckLValueConstantExpression(Info, this, LV, Result.Val, 5925c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith CCEK_Constant); 5926b2f295c8050fb8c141bf2cf38eed0a56e99d0092Eli Friedman} 5927b2f295c8050fb8c141bf2cf38eed0a56e99d0092Eli Friedman 5928099e7f647ccda915513f2b2ec53352dc756082d3Richard Smithbool Expr::EvaluateAsInitializer(APValue &Value, const ASTContext &Ctx, 5929099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith const VarDecl *VD, 5930099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const { 59312d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith // FIXME: Evaluating initializers for large array and record types can cause 59322d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith // performance problems. Only do so in C++11 for now. 59332d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith if (isRValue() && (getType()->isArrayType() || getType()->isRecordType()) && 59342d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith !Ctx.getLangOptions().CPlusPlus0x) 59352d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith return false; 59362d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith 5937099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Expr::EvalStatus EStatus; 5938099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith EStatus.Diag = &Notes; 5939099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith 5940099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith EvalInfo InitInfo(Ctx, EStatus); 5941099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith InitInfo.setEvaluatingDecl(VD, Value); 5942099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith 594351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (!CheckLiteralType(InitInfo, this)) 594451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return false; 594551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 5946099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith LValue LVal; 5947099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith LVal.set(VD); 5948099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith 594951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith // C++11 [basic.start.init]p2: 595051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith // Variables with static storage duration or thread storage duration shall be 595151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith // zero-initialized before any other initialization takes place. 595251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith // This behavior is not present in C. 595351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (Ctx.getLangOptions().CPlusPlus && !VD->hasLocalStorage() && 595451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith !VD->getType()->isReferenceType()) { 595551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith ImplicitValueInitExpr VIE(VD->getType()); 595651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (!EvaluateConstantExpression(Value, InitInfo, LVal, &VIE)) 595751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return false; 595851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith } 595951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 5960099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith return EvaluateConstantExpression(Value, InitInfo, LVal, this) && 5961099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith !EStatus.HasSideEffects; 5962099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith} 5963099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith 596451f4708c00110940ca3f337961915f2ca1668375Richard Smith/// isEvaluatable - Call EvaluateAsRValue to see if this expression can be 596551f4708c00110940ca3f337961915f2ca1668375Richard Smith/// constant folded, but discard the result. 59664ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool Expr::isEvaluatable(const ASTContext &Ctx) const { 59674fdfb0965b396f2778091f7e6c051d17ff9791baAnders Carlsson EvalResult Result; 596851f4708c00110940ca3f337961915f2ca1668375Richard Smith return EvaluateAsRValue(Result, Ctx) && !Result.HasSideEffects; 596945b6b9d080ac56917337d73d8f1cd6374b27b05dChris Lattner} 597051fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson 59714ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool Expr::HasSideEffects(const ASTContext &Ctx) const { 59721e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith return HasSideEffect(Ctx).Visit(this); 5973393c247fe025ccb5f914e37e948192ea86faef8cFariborz Jahanian} 5974393c247fe025ccb5f914e37e948192ea86faef8cFariborz Jahanian 5975a6b8b2c09610b8bc4330e948ece8b940c2386406Richard SmithAPSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx) const { 59761c0cfd4599e816cfd7a8f348286bf0ad79652ffcAnders Carlsson EvalResult EvalResult; 597751f4708c00110940ca3f337961915f2ca1668375Richard Smith bool Result = EvaluateAsRValue(EvalResult, Ctx); 5978c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin (void)Result; 597951fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson assert(Result && "Could not evaluate expression"); 59801c0cfd4599e816cfd7a8f348286bf0ad79652ffcAnders Carlsson assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer"); 598151fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson 59821c0cfd4599e816cfd7a8f348286bf0ad79652ffcAnders Carlsson return EvalResult.Val.getInt(); 598351fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson} 5984d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 5985e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara bool Expr::EvalResult::isGlobalLValue() const { 5986e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara assert(Val.isLValue()); 5987e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara return IsGlobalLValue(Val.getLValueBase()); 5988e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara } 5989e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara 5990e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara 5991d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// isIntegerConstantExpr - this recursive routine will test if an expression is 5992d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// an integer constant expression. 5993d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 5994d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, 5995d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// comma, etc 5996d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// 5997d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof 5998d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer 5999d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// cast+dereference. 6000d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 6001d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// CheckICE - This function does the fundamental ICE checking: the returned 6002d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation. 6003d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// Note that to reduce code duplication, this helper does no evaluation 6004d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// itself; the caller checks whether the expression is evaluatable, and 6005d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// in the rare cases where CheckICE actually cares about the evaluated 6006d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// value, it calls into Evalute. 6007d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// 6008d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// Meanings of Val: 600951f4708c00110940ca3f337961915f2ca1668375Richard Smith// 0: This expression is an ICE. 6010d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// 1: This expression is not an ICE, but if it isn't evaluated, it's 6011d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// a legal subexpression for an ICE. This return value is used to handle 6012d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// the comma operator in C99 mode. 6013d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// 2: This expression is not an ICE, and is not a legal subexpression for one. 6014d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 60153c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohmannamespace { 60163c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman 6017d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCallstruct ICEDiag { 6018d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall unsigned Val; 6019d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall SourceLocation Loc; 6020d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 6021d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall public: 6022d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {} 6023d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall ICEDiag() : Val(0) {} 6024d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall}; 6025d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 60263c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman} 60273c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman 60283c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohmanstatic ICEDiag NoDiag() { return ICEDiag(); } 6029d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 6030d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCallstatic ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) { 6031d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall Expr::EvalResult EVResult; 603251f4708c00110940ca3f337961915f2ca1668375Richard Smith if (!E->EvaluateAsRValue(EVResult, Ctx) || EVResult.HasSideEffects || 6033d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall !EVResult.Val.isInt()) { 6034d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return ICEDiag(2, E->getLocStart()); 6035d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6036d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return NoDiag(); 6037d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall} 6038d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 6039d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCallstatic ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) { 6040d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall assert(!E->isValueDependent() && "Should not see value dependent exprs!"); 60412ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor if (!E->getType()->isIntegralOrEnumerationType()) { 6042d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return ICEDiag(2, E->getLocStart()); 6043d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6044d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 6045d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall switch (E->getStmtClass()) { 604663c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall#define ABSTRACT_STMT(Node) 6047d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall#define STMT(Node, Base) case Expr::Node##Class: 6048d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall#define EXPR(Node, Base) 6049d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall#include "clang/AST/StmtNodes.inc" 6050d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::PredefinedExprClass: 6051d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::FloatingLiteralClass: 6052d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ImaginaryLiteralClass: 6053d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::StringLiteralClass: 6054d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ArraySubscriptExprClass: 6055d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::MemberExprClass: 6056d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CompoundAssignOperatorClass: 6057d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CompoundLiteralExprClass: 6058d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ExtVectorElementExprClass: 6059d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::DesignatedInitExprClass: 6060d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ImplicitValueInitExprClass: 6061d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ParenListExprClass: 6062d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::VAArgExprClass: 6063d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::AddrLabelExprClass: 6064d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::StmtExprClass: 6065d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXMemberCallExprClass: 6066e08ce650a2b02410eddd1f60a4aa6b3d4be71e73Peter Collingbourne case Expr::CUDAKernelCallExprClass: 6067d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXDynamicCastExprClass: 6068d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXTypeidExprClass: 60699be88403e965cc49af76c9d33d818781d44b333eFrancois Pichet case Expr::CXXUuidofExprClass: 6070d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXNullPtrLiteralExprClass: 6071d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXThisExprClass: 6072d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXThrowExprClass: 6073d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXNewExprClass: 6074d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXDeleteExprClass: 6075d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXPseudoDestructorExprClass: 6076d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::UnresolvedLookupExprClass: 6077d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::DependentScopeDeclRefExprClass: 6078d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXConstructExprClass: 6079d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXBindTemporaryExprClass: 60804765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall case Expr::ExprWithCleanupsClass: 6081d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXTemporaryObjectExprClass: 6082d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXUnresolvedConstructExprClass: 6083d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXDependentScopeMemberExprClass: 6084d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::UnresolvedMemberExprClass: 6085d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ObjCStringLiteralClass: 6086d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ObjCEncodeExprClass: 6087d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ObjCMessageExprClass: 6088d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ObjCSelectorExprClass: 6089d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ObjCProtocolExprClass: 6090d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ObjCIvarRefExprClass: 6091d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ObjCPropertyRefExprClass: 6092d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ObjCIsaExprClass: 6093d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ShuffleVectorExprClass: 6094d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::BlockExprClass: 6095d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::BlockDeclRefExprClass: 6096d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::NoStmtClass: 60977cd7d1ad33fdf49eef83942e8855fe20d95aa1b9John McCall case Expr::OpaqueValueExprClass: 6098be230c36e32142cbdcdbe9c97511d097beeecbabDouglas Gregor case Expr::PackExpansionExprClass: 6099c7793c73ba8a343de3f2552d984851985a46f159Douglas Gregor case Expr::SubstNonTypeTemplateParmPackExprClass: 610061eee0ca33b29e102f11bab77c8b74cc00e2392bTanya Lattner case Expr::AsTypeExprClass: 6101f85e193739c953358c865005855253af4f68a497John McCall case Expr::ObjCIndirectCopyRestoreExprClass: 610203e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor case Expr::MaterializeTemporaryExprClass: 61034b9c2d235fb9449e249d74f48ecfec601650de93John McCall case Expr::PseudoObjectExprClass: 6104276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman case Expr::AtomicExprClass: 6105cea8d966f826554f0679595e9371e314e8dbc1cfSebastian Redl case Expr::InitListExprClass: 610601d08018b7cf5ce1601707cfd7a84d22015fc04eDouglas Gregor case Expr::LambdaExprClass: 6107cea8d966f826554f0679595e9371e314e8dbc1cfSebastian Redl return ICEDiag(2, E->getLocStart()); 6108cea8d966f826554f0679595e9371e314e8dbc1cfSebastian Redl 6109ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor case Expr::SizeOfPackExprClass: 6110d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::GNUNullExprClass: 6111d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // GCC considers the GNU __null value to be an integral constant expression. 6112d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return NoDiag(); 6113d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 611491a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall case Expr::SubstNonTypeTemplateParmExprClass: 611591a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall return 611691a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall CheckICE(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(), Ctx); 611791a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall 6118d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ParenExprClass: 6119d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx); 6120f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne case Expr::GenericSelectionExprClass: 6121f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne return CheckICE(cast<GenericSelectionExpr>(E)->getResultExpr(), Ctx); 6122d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::IntegerLiteralClass: 6123d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CharacterLiteralClass: 6124d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXBoolLiteralExprClass: 6125ed8abf18329df67b0abcbb3a10458bd8c1d2a595Douglas Gregor case Expr::CXXScalarValueInitExprClass: 6126d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::UnaryTypeTraitExprClass: 61276ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet case Expr::BinaryTypeTraitExprClass: 612821ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley case Expr::ArrayTypeTraitExprClass: 6129552622067dc45013d240f73952fece703f5e63bdJohn Wiegley case Expr::ExpressionTraitExprClass: 61302e156225a29407a50dd19041aa5750171ad44ea3Sebastian Redl case Expr::CXXNoexceptExprClass: 6131d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return NoDiag(); 6132d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CallExprClass: 61336cf750298d3621d8a10a6dd07fcee8e274b9d94dSean Hunt case Expr::CXXOperatorCallExprClass: { 613405830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith // C99 6.6/3 allows function calls within unevaluated subexpressions of 613505830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith // constant expressions, but they can never be ICEs because an ICE cannot 613605830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith // contain an operand of (pointer to) function type. 6137d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall const CallExpr *CE = cast<CallExpr>(E); 6138180f47959a066795cc0f409433023af448bb0328Richard Smith if (CE->isBuiltinCall()) 6139d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return CheckEvalInICE(E, Ctx); 6140d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return ICEDiag(2, E->getLocStart()); 6141d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6142d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::DeclRefExprClass: 6143d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl())) 6144d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return NoDiag(); 614503f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith if (Ctx.getLangOptions().CPlusPlus && IsConstNonVolatile(E->getType())) { 6146d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl(); 6147d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 6148d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // Parameter variables are never constants. Without this check, 6149d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // getAnyInitializer() can find a default argument, which leads 6150d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // to chaos. 6151d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (isa<ParmVarDecl>(D)) 6152d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); 6153d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 6154d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // C++ 7.1.5.1p2 6155d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // A variable of non-volatile const-qualified integral or enumeration 6156d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // type initialized by an ICE can be used in ICEs. 6157d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) { 6158db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith if (!Dcl->getType()->isIntegralOrEnumerationType()) 6159db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); 6160db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith 6161099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith const VarDecl *VD; 6162099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith // Look for a declaration of this variable that has an initializer, and 6163099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith // check whether it is an ICE. 6164099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith if (Dcl->getAnyInitializer(VD) && VD->checkInitIsICE()) 6165099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith return NoDiag(); 6166099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith else 6167099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); 6168d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6169d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6170d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return ICEDiag(2, E->getLocStart()); 6171d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::UnaryOperatorClass: { 6172d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall const UnaryOperator *Exp = cast<UnaryOperator>(E); 6173d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall switch (Exp->getOpcode()) { 61742de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_PostInc: 61752de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_PostDec: 61762de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_PreInc: 61772de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_PreDec: 61782de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_AddrOf: 61792de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_Deref: 618005830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith // C99 6.6/3 allows increment and decrement within unevaluated 618105830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith // subexpressions of constant expressions, but they can never be ICEs 618205830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith // because an ICE cannot contain an lvalue operand. 6183d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return ICEDiag(2, E->getLocStart()); 61842de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_Extension: 61852de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_LNot: 61862de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_Plus: 61872de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_Minus: 61882de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_Not: 61892de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_Real: 61902de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_Imag: 6191d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return CheckICE(Exp->getSubExpr(), Ctx); 6192d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6193d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 6194d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // OffsetOf falls through here. 6195d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6196d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::OffsetOfExprClass: { 6197d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // Note that per C99, offsetof must be an ICE. And AFAIK, using 619851f4708c00110940ca3f337961915f2ca1668375Richard Smith // EvaluateAsRValue matches the proposed gcc behavior for cases like 619905830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith // "offsetof(struct s{int x[4];}, x[1.0])". This doesn't affect 6200d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // compliance: we should warn earlier for offsetof expressions with 6201d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // array subscripts that aren't ICEs, and if the array subscripts 6202d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // are ICEs, the value of the offsetof must be an integer constant. 6203d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return CheckEvalInICE(E, Ctx); 6204d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6205f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne case Expr::UnaryExprOrTypeTraitExprClass: { 6206f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(E); 6207f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne if ((Exp->getKind() == UETT_SizeOf) && 6208f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne Exp->getTypeOfArgument()->isVariableArrayType()) 6209d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return ICEDiag(2, E->getLocStart()); 6210d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return NoDiag(); 6211d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6212d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::BinaryOperatorClass: { 6213d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall const BinaryOperator *Exp = cast<BinaryOperator>(E); 6214d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall switch (Exp->getOpcode()) { 62152de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_PtrMemD: 62162de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_PtrMemI: 62172de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Assign: 62182de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_MulAssign: 62192de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_DivAssign: 62202de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_RemAssign: 62212de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_AddAssign: 62222de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_SubAssign: 62232de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_ShlAssign: 62242de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_ShrAssign: 62252de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_AndAssign: 62262de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_XorAssign: 62272de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_OrAssign: 622805830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith // C99 6.6/3 allows assignments within unevaluated subexpressions of 622905830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith // constant expressions, but they can never be ICEs because an ICE cannot 623005830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith // contain an lvalue operand. 6231d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return ICEDiag(2, E->getLocStart()); 6232d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 62332de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Mul: 62342de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Div: 62352de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Rem: 62362de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Add: 62372de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Sub: 62382de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Shl: 62392de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Shr: 62402de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_LT: 62412de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_GT: 62422de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_LE: 62432de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_GE: 62442de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_EQ: 62452de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_NE: 62462de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_And: 62472de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Xor: 62482de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Or: 62492de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Comma: { 6250d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); 6251d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); 62522de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (Exp->getOpcode() == BO_Div || 62532de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall Exp->getOpcode() == BO_Rem) { 625451f4708c00110940ca3f337961915f2ca1668375Richard Smith // EvaluateAsRValue gives an error for undefined Div/Rem, so make sure 6255d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // we don't evaluate one. 62563b332ab132fa85c83833d74d400f6e126f52fbd2John McCall if (LHSResult.Val == 0 && RHSResult.Val == 0) { 6257a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith llvm::APSInt REval = Exp->getRHS()->EvaluateKnownConstInt(Ctx); 6258d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (REval == 0) 6259d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return ICEDiag(1, E->getLocStart()); 6260d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (REval.isSigned() && REval.isAllOnesValue()) { 6261a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith llvm::APSInt LEval = Exp->getLHS()->EvaluateKnownConstInt(Ctx); 6262d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (LEval.isMinSignedValue()) 6263d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return ICEDiag(1, E->getLocStart()); 6264d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6265d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6266d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 62672de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (Exp->getOpcode() == BO_Comma) { 6268d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (Ctx.getLangOptions().C99) { 6269d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // C99 6.6p3 introduces a strange edge case: comma can be in an ICE 6270d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // if it isn't evaluated. 6271d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (LHSResult.Val == 0 && RHSResult.Val == 0) 6272d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return ICEDiag(1, E->getLocStart()); 6273d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } else { 6274d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // In both C89 and C++, commas in ICEs are illegal. 6275d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return ICEDiag(2, E->getLocStart()); 6276d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6277d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6278d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (LHSResult.Val >= RHSResult.Val) 6279d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return LHSResult; 6280d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return RHSResult; 6281d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 62822de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_LAnd: 62832de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_LOr: { 6284d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); 6285d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); 6286d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (LHSResult.Val == 0 && RHSResult.Val == 1) { 6287d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // Rare case where the RHS has a comma "side-effect"; we need 6288d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // to actually check the condition to see whether the side 6289d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // with the comma is evaluated. 62902de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if ((Exp->getOpcode() == BO_LAnd) != 6291a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith (Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0)) 6292d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return RHSResult; 6293d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return NoDiag(); 6294d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6295d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 6296d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (LHSResult.Val >= RHSResult.Val) 6297d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return LHSResult; 6298d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return RHSResult; 6299d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6300d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6301d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6302d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ImplicitCastExprClass: 6303d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CStyleCastExprClass: 6304d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXFunctionalCastExprClass: 6305d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXStaticCastExprClass: 6306d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXReinterpretCastExprClass: 630732cb47174304bc7ec11478b9497c4e10f48273d9Richard Smith case Expr::CXXConstCastExprClass: 6308f85e193739c953358c865005855253af4f68a497John McCall case Expr::ObjCBridgedCastExprClass: { 6309d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr(); 63102116b144cf07f2574d20517187eb8863645376ebRichard Smith if (isa<ExplicitCastExpr>(E)) { 63112116b144cf07f2574d20517187eb8863645376ebRichard Smith if (const FloatingLiteral *FL 63122116b144cf07f2574d20517187eb8863645376ebRichard Smith = dyn_cast<FloatingLiteral>(SubExpr->IgnoreParenImpCasts())) { 63132116b144cf07f2574d20517187eb8863645376ebRichard Smith unsigned DestWidth = Ctx.getIntWidth(E->getType()); 63142116b144cf07f2574d20517187eb8863645376ebRichard Smith bool DestSigned = E->getType()->isSignedIntegerOrEnumerationType(); 63152116b144cf07f2574d20517187eb8863645376ebRichard Smith APSInt IgnoredVal(DestWidth, !DestSigned); 63162116b144cf07f2574d20517187eb8863645376ebRichard Smith bool Ignored; 63172116b144cf07f2574d20517187eb8863645376ebRichard Smith // If the value does not fit in the destination type, the behavior is 63182116b144cf07f2574d20517187eb8863645376ebRichard Smith // undefined, so we are not required to treat it as a constant 63192116b144cf07f2574d20517187eb8863645376ebRichard Smith // expression. 63202116b144cf07f2574d20517187eb8863645376ebRichard Smith if (FL->getValue().convertToInteger(IgnoredVal, 63212116b144cf07f2574d20517187eb8863645376ebRichard Smith llvm::APFloat::rmTowardZero, 63222116b144cf07f2574d20517187eb8863645376ebRichard Smith &Ignored) & APFloat::opInvalidOp) 63232116b144cf07f2574d20517187eb8863645376ebRichard Smith return ICEDiag(2, E->getLocStart()); 63242116b144cf07f2574d20517187eb8863645376ebRichard Smith return NoDiag(); 63252116b144cf07f2574d20517187eb8863645376ebRichard Smith } 63262116b144cf07f2574d20517187eb8863645376ebRichard Smith } 6327eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman switch (cast<CastExpr>(E)->getCastKind()) { 6328eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman case CK_LValueToRValue: 63297a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall case CK_AtomicToNonAtomic: 63307a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall case CK_NonAtomicToAtomic: 6331eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman case CK_NoOp: 6332eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman case CK_IntegralToBoolean: 6333eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman case CK_IntegralCast: 6334d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return CheckICE(SubExpr, Ctx); 6335eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman default: 6336eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman return ICEDiag(2, E->getLocStart()); 6337eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman } 6338d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 633956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall case Expr::BinaryConditionalOperatorClass: { 634056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(E); 634156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall ICEDiag CommonResult = CheckICE(Exp->getCommon(), Ctx); 634256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall if (CommonResult.Val == 2) return CommonResult; 634356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); 634456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall if (FalseResult.Val == 2) return FalseResult; 634556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall if (CommonResult.Val == 1) return CommonResult; 634656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall if (FalseResult.Val == 1 && 6347a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith Exp->getCommon()->EvaluateKnownConstInt(Ctx) == 0) return NoDiag(); 634856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall return FalseResult; 634956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall } 6350d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ConditionalOperatorClass: { 6351d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall const ConditionalOperator *Exp = cast<ConditionalOperator>(E); 6352d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // If the condition (ignoring parens) is a __builtin_constant_p call, 6353d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // then only the true side is actually considered in an integer constant 6354d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // expression, and it is fully evaluated. This is an important GNU 6355d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // extension. See GCC PR38377 for discussion. 6356d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (const CallExpr *CallCE 6357d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts())) 635880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p) 635980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith return CheckEvalInICE(E, Ctx); 6360d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx); 6361d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (CondResult.Val == 2) 6362d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return CondResult; 636363fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor 6364f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx); 6365f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); 636663fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor 6367d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (TrueResult.Val == 2) 6368d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return TrueResult; 6369d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (FalseResult.Val == 2) 6370d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return FalseResult; 6371d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (CondResult.Val == 1) 6372d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return CondResult; 6373d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (TrueResult.Val == 0 && FalseResult.Val == 0) 6374d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return NoDiag(); 6375d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // Rare case where the diagnostics depend on which side is evaluated 6376d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // Note that if we get here, CondResult is 0, and at least one of 6377d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // TrueResult and FalseResult is non-zero. 6378a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith if (Exp->getCond()->EvaluateKnownConstInt(Ctx) == 0) { 6379d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return FalseResult; 6380d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6381d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return TrueResult; 6382d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6383d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXDefaultArgExprClass: 6384d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx); 6385d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ChooseExprClass: { 6386d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx); 6387d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6388d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6389d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 63903026348bd4c13a0f83b59839f64065e0fcbea253David Blaikie llvm_unreachable("Invalid StmtClass!"); 6391d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall} 6392d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 6393f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// Evaluate an expression as a C++11 integral constant expression. 6394f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool EvaluateCPlusPlus11IntegralConstantExpr(ASTContext &Ctx, 6395f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith const Expr *E, 6396f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith llvm::APSInt *Value, 6397f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith SourceLocation *Loc) { 6398f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!E->getType()->isIntegralOrEnumerationType()) { 6399f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (Loc) *Loc = E->getExprLoc(); 6400f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 6401f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 6402f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 64034c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith APValue Result; 64044c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith if (!E->isCXX11ConstantExpr(Ctx, &Result, Loc)) 6405dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith return false; 6406dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith 64074c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith assert(Result.isInt() && "pointer cast to int is not an ICE"); 64084c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith if (Value) *Value = Result.getInt(); 6409dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith return true; 6410f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith} 6411f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 6412dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smithbool Expr::isIntegerConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const { 6413f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (Ctx.getLangOptions().CPlusPlus0x) 6414f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, 0, Loc); 6415f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 6416d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall ICEDiag d = CheckICE(this, Ctx); 6417d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (d.Val != 0) { 6418d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (Loc) *Loc = d.Loc; 6419d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return false; 6420d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6421f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return true; 6422f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith} 6423f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 6424f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithbool Expr::isIntegerConstantExpr(llvm::APSInt &Value, ASTContext &Ctx, 6425f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith SourceLocation *Loc, bool isEvaluated) const { 6426f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (Ctx.getLangOptions().CPlusPlus0x) 6427f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, &Value, Loc); 6428f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 6429f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!isIntegerConstantExpr(Ctx, Loc)) 6430f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 6431f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!EvaluateAsInt(Value, Ctx)) 6432d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall llvm_unreachable("ICE cannot be evaluated!"); 6433d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return true; 6434d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall} 64354c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith 64364c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smithbool Expr::isCXX11ConstantExpr(ASTContext &Ctx, APValue *Result, 64374c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith SourceLocation *Loc) const { 64384c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith // We support this checking in C++98 mode in order to diagnose compatibility 64394c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith // issues. 64404c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith assert(Ctx.getLangOptions().CPlusPlus); 64414c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith 64424c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith Expr::EvalStatus Status; 64434c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith llvm::SmallVector<PartialDiagnosticAt, 8> Diags; 64444c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith Status.Diag = &Diags; 64454c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith EvalInfo Info(Ctx, Status); 64464c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith 64474c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith APValue Scratch; 64484c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith bool IsConstExpr = ::EvaluateAsRValue(Info, this, Result ? *Result : Scratch); 64494c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith 64504c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith if (!Diags.empty()) { 64514c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith IsConstExpr = false; 64524c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith if (Loc) *Loc = Diags[0].first; 64534c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith } else if (!IsConstExpr) { 64544c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith // FIXME: This shouldn't happen. 64554c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith if (Loc) *Loc = getExprLoc(); 64564c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith } 64574c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith 64584c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith return IsConstExpr; 64594c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith} 6460745f5147e065900267c85a5568785a1991d4838fRichard Smith 6461745f5147e065900267c85a5568785a1991d4838fRichard Smithbool Expr::isPotentialConstantExpr(const FunctionDecl *FD, 6462745f5147e065900267c85a5568785a1991d4838fRichard Smith llvm::SmallVectorImpl< 6463745f5147e065900267c85a5568785a1991d4838fRichard Smith PartialDiagnosticAt> &Diags) { 6464745f5147e065900267c85a5568785a1991d4838fRichard Smith // FIXME: It would be useful to check constexpr function templates, but at the 6465745f5147e065900267c85a5568785a1991d4838fRichard Smith // moment the constant expression evaluator cannot cope with the non-rigorous 6466745f5147e065900267c85a5568785a1991d4838fRichard Smith // ASTs which we build for dependent expressions. 6467745f5147e065900267c85a5568785a1991d4838fRichard Smith if (FD->isDependentContext()) 6468745f5147e065900267c85a5568785a1991d4838fRichard Smith return true; 6469745f5147e065900267c85a5568785a1991d4838fRichard Smith 6470745f5147e065900267c85a5568785a1991d4838fRichard Smith Expr::EvalStatus Status; 6471745f5147e065900267c85a5568785a1991d4838fRichard Smith Status.Diag = &Diags; 6472745f5147e065900267c85a5568785a1991d4838fRichard Smith 6473745f5147e065900267c85a5568785a1991d4838fRichard Smith EvalInfo Info(FD->getASTContext(), Status); 6474745f5147e065900267c85a5568785a1991d4838fRichard Smith Info.CheckingPotentialConstantExpression = true; 6475745f5147e065900267c85a5568785a1991d4838fRichard Smith 6476745f5147e065900267c85a5568785a1991d4838fRichard Smith const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); 6477745f5147e065900267c85a5568785a1991d4838fRichard Smith const CXXRecordDecl *RD = MD ? MD->getParent()->getCanonicalDecl() : 0; 6478745f5147e065900267c85a5568785a1991d4838fRichard Smith 6479745f5147e065900267c85a5568785a1991d4838fRichard Smith // FIXME: Fabricate an arbitrary expression on the stack and pretend that it 6480745f5147e065900267c85a5568785a1991d4838fRichard Smith // is a temporary being used as the 'this' pointer. 6481745f5147e065900267c85a5568785a1991d4838fRichard Smith LValue This; 6482745f5147e065900267c85a5568785a1991d4838fRichard Smith ImplicitValueInitExpr VIE(RD ? Info.Ctx.getRecordType(RD) : Info.Ctx.IntTy); 6483745f5147e065900267c85a5568785a1991d4838fRichard Smith This.set(&VIE, Info.CurrentCall); 6484745f5147e065900267c85a5568785a1991d4838fRichard Smith 6485745f5147e065900267c85a5568785a1991d4838fRichard Smith APValue Scratch; 6486745f5147e065900267c85a5568785a1991d4838fRichard Smith ArrayRef<const Expr*> Args; 6487745f5147e065900267c85a5568785a1991d4838fRichard Smith 6488745f5147e065900267c85a5568785a1991d4838fRichard Smith SourceLocation Loc = FD->getLocation(); 6489745f5147e065900267c85a5568785a1991d4838fRichard Smith 6490745f5147e065900267c85a5568785a1991d4838fRichard Smith if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) { 6491745f5147e065900267c85a5568785a1991d4838fRichard Smith HandleConstructorCall(Loc, This, Args, CD, Info, Scratch); 6492745f5147e065900267c85a5568785a1991d4838fRichard Smith } else 6493745f5147e065900267c85a5568785a1991d4838fRichard Smith HandleFunctionCall(Loc, FD, (MD && MD->isInstance()) ? &This : 0, 6494745f5147e065900267c85a5568785a1991d4838fRichard Smith Args, FD->getBody(), Info, Scratch); 6495745f5147e065900267c85a5568785a1991d4838fRichard Smith 6496745f5147e065900267c85a5568785a1991d4838fRichard Smith return Diags.empty(); 6497745f5147e065900267c85a5568785a1991d4838fRichard Smith} 6498