ExprConstant.cpp revision 4d4e5c1ae83f4510caa486b3ad19de13048f9f04
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 8587ca4850a3e3530fa6c93b64b740446e32c97f992Richard Smith = CCEK_Constant, 8597ca4850a3e3530fa6c93b64b740446e32c97f992Richard Smith bool AllowNonLiteralTypes = false); 860efdb83e26f9a1fd2566afe54461216cd84814d42John McCallstatic bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info); 861efdb83e26f9a1fd2566afe54461216cd84814d42John McCallstatic bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info); 862e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, 863e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith EvalInfo &Info); 864e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info); 86587eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattnerstatic bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info); 86647a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smithstatic bool EvaluateIntegerOrLValue(const Expr *E, CCValue &Result, 867d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner EvalInfo &Info); 868d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmanstatic bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info); 869f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCallstatic bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info); 870f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner 871f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===// 8724efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman// Misc utilities 8734efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman//===----------------------------------------------------------------------===// 8744efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 875180f47959a066795cc0f409433023af448bb0328Richard Smith/// Should this call expression be treated as a string literal? 876180f47959a066795cc0f409433023af448bb0328Richard Smithstatic bool IsStringLiteralCall(const CallExpr *E) { 877180f47959a066795cc0f409433023af448bb0328Richard Smith unsigned Builtin = E->isBuiltinCall(); 878180f47959a066795cc0f409433023af448bb0328Richard Smith return (Builtin == Builtin::BI__builtin___CFStringMakeConstantString || 879180f47959a066795cc0f409433023af448bb0328Richard Smith Builtin == Builtin::BI__builtin___NSStringMakeConstantString); 880180f47959a066795cc0f409433023af448bb0328Richard Smith} 881180f47959a066795cc0f409433023af448bb0328Richard Smith 8821bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smithstatic bool IsGlobalLValue(APValue::LValueBase B) { 883180f47959a066795cc0f409433023af448bb0328Richard Smith // C++11 [expr.const]p3 An address constant expression is a prvalue core 884180f47959a066795cc0f409433023af448bb0328Richard Smith // constant expression of pointer type that evaluates to... 885180f47959a066795cc0f409433023af448bb0328Richard Smith 886180f47959a066795cc0f409433023af448bb0328Richard Smith // ... a null pointer value, or a prvalue core constant expression of type 887180f47959a066795cc0f409433023af448bb0328Richard Smith // std::nullptr_t. 8881bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith if (!B) return true; 88942c8f87eb60958170c46767273bf93e6c96125bfJohn McCall 8901bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { 891180f47959a066795cc0f409433023af448bb0328Richard Smith // ... the address of an object with static storage duration, 8921bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith if (const VarDecl *VD = dyn_cast<VarDecl>(D)) 89342c8f87eb60958170c46767273bf93e6c96125bfJohn McCall return VD->hasGlobalStorage(); 8941bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith // ... the address of a function, 8951bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith return isa<FunctionDecl>(D); 89642c8f87eb60958170c46767273bf93e6c96125bfJohn McCall } 8971bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith 8981bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith const Expr *E = B.get<const Expr*>(); 8991bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith switch (E->getStmtClass()) { 9001bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith default: 9011bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith return false; 902180f47959a066795cc0f409433023af448bb0328Richard Smith case Expr::CompoundLiteralExprClass: 903180f47959a066795cc0f409433023af448bb0328Richard Smith return cast<CompoundLiteralExpr>(E)->isFileScope(); 904180f47959a066795cc0f409433023af448bb0328Richard Smith // A string literal has static storage duration. 905180f47959a066795cc0f409433023af448bb0328Richard Smith case Expr::StringLiteralClass: 906180f47959a066795cc0f409433023af448bb0328Richard Smith case Expr::PredefinedExprClass: 907180f47959a066795cc0f409433023af448bb0328Richard Smith case Expr::ObjCStringLiteralClass: 908180f47959a066795cc0f409433023af448bb0328Richard Smith case Expr::ObjCEncodeExprClass: 90947d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith case Expr::CXXTypeidExprClass: 910180f47959a066795cc0f409433023af448bb0328Richard Smith return true; 911180f47959a066795cc0f409433023af448bb0328Richard Smith case Expr::CallExprClass: 912180f47959a066795cc0f409433023af448bb0328Richard Smith return IsStringLiteralCall(cast<CallExpr>(E)); 913180f47959a066795cc0f409433023af448bb0328Richard Smith // For GCC compatibility, &&label has static storage duration. 914180f47959a066795cc0f409433023af448bb0328Richard Smith case Expr::AddrLabelExprClass: 915180f47959a066795cc0f409433023af448bb0328Richard Smith return true; 916180f47959a066795cc0f409433023af448bb0328Richard Smith // A Block literal expression may be used as the initialization value for 917180f47959a066795cc0f409433023af448bb0328Richard Smith // Block variables at global or local static scope. 918180f47959a066795cc0f409433023af448bb0328Richard Smith case Expr::BlockExprClass: 919180f47959a066795cc0f409433023af448bb0328Richard Smith return !cast<BlockExpr>(E)->getBlockDecl()->hasCaptures(); 920745f5147e065900267c85a5568785a1991d4838fRichard Smith case Expr::ImplicitValueInitExprClass: 921745f5147e065900267c85a5568785a1991d4838fRichard Smith // FIXME: 922745f5147e065900267c85a5568785a1991d4838fRichard Smith // We can never form an lvalue with an implicit value initialization as its 923745f5147e065900267c85a5568785a1991d4838fRichard Smith // base through expression evaluation, so these only appear in one case: the 924745f5147e065900267c85a5568785a1991d4838fRichard Smith // implicit variable declaration we invent when checking whether a constexpr 925745f5147e065900267c85a5568785a1991d4838fRichard Smith // constructor can produce a constant expression. We must assume that such 926745f5147e065900267c85a5568785a1991d4838fRichard Smith // an expression might be a global lvalue. 927745f5147e065900267c85a5568785a1991d4838fRichard Smith return true; 928180f47959a066795cc0f409433023af448bb0328Richard Smith } 92942c8f87eb60958170c46767273bf93e6c96125bfJohn McCall} 93042c8f87eb60958170c46767273bf93e6c96125bfJohn McCall 9319a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith/// Check that this reference or pointer core constant expression is a valid 932b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith/// value for an address or reference constant expression. Type T should be 93361e616206413d1779c7545c7a8ad1ce1129ad9c1Richard Smith/// either LValue or CCValue. Return true if we can fold this expression, 93461e616206413d1779c7545c7a8ad1ce1129ad9c1Richard Smith/// whether or not it's a constant expression. 9359a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smithtemplate<typename T> 936f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool CheckLValueConstantExpression(EvalInfo &Info, const Expr *E, 937c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const T &LVal, APValue &Value, 938c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith CheckConstantExpressionKind CCEK) { 939c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith APValue::LValueBase Base = LVal.getLValueBase(); 940c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const SubobjectDesignator &Designator = LVal.getLValueDesignator(); 941c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 942c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (!IsGlobalLValue(Base)) { 943c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (Info.getLangOpts().CPlusPlus0x) { 944c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); 945c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Info.Diag(E->getExprLoc(), diag::note_constexpr_non_global, 1) 946c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith << E->isGLValue() << !Designator.Entries.empty() 947c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith << !!VD << CCEK << VD; 948c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (VD) 949c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Info.Note(VD->getLocation(), diag::note_declared_at); 950c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith else 951c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Info.Note(Base.dyn_cast<const Expr*>()->getExprLoc(), 952c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith diag::note_constexpr_temporary_here); 953c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } else { 9547098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Diag(E->getExprLoc()); 955c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } 95661e616206413d1779c7545c7a8ad1ce1129ad9c1Richard Smith // Don't allow references to temporaries to escape. 95769c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith return false; 958f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 95969c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith 960b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith bool IsReferenceType = E->isGLValue(); 961b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 962b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (Designator.Invalid) { 96361e616206413d1779c7545c7a8ad1ce1129ad9c1Richard Smith // This is not a core constant expression. An appropriate diagnostic will 96461e616206413d1779c7545c7a8ad1ce1129ad9c1Richard Smith // have already been produced. 9659a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith Value = APValue(LVal.getLValueBase(), LVal.getLValueOffset(), 9669a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith APValue::NoLValuePath()); 9679a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith return true; 9689a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith } 9699a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith 970b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Value = APValue(LVal.getLValueBase(), LVal.getLValueOffset(), 971b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Designator.Entries, Designator.IsOnePastTheEnd); 972b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 973b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // Allow address constant expressions to be past-the-end pointers. This is 974b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // an extension: the standard requires them to point to an object. 975b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (!IsReferenceType) 976b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return true; 977b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 978b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // A reference constant expression must refer to an object. 979b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (!Base) { 980b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // FIXME: diagnostic 981b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Info.CCEDiag(E->getExprLoc()); 98261e616206413d1779c7545c7a8ad1ce1129ad9c1Richard Smith return true; 983b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } 984b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 985c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith // Does this refer one past the end of some object? 986b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (Designator.isOnePastTheEnd()) { 987c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); 988c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Info.Diag(E->getExprLoc(), diag::note_constexpr_past_end, 1) 989c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith << !Designator.Entries.empty() << !!VD << VD; 990c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (VD) 991c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Info.Note(VD->getLocation(), diag::note_declared_at); 992c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith else 993c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Info.Note(Base.dyn_cast<const Expr*>()->getExprLoc(), 994c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith diag::note_constexpr_temporary_here); 995c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } 996c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 99769c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith return true; 99847a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith} 99947a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith 100051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// Check that this core constant expression is of literal type, and if not, 100151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// produce an appropriate diagnostic. 100251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smithstatic bool CheckLiteralType(EvalInfo &Info, const Expr *E) { 100351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (!E->isRValue() || E->getType()->isLiteralType()) 100451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return true; 100551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 100651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith // Prvalue constant expressions must be of literal types. 100751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (Info.getLangOpts().CPlusPlus0x) 100851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith Info.Diag(E->getExprLoc(), diag::note_constexpr_nonliteral) 100951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith << E->getType(); 101051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith else 101151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); 101251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return false; 101351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith} 101451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 10159a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith/// Check that this core constant expression value is a valid value for a 10169a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith/// constant expression, and if it is, produce the corresponding constant value. 101751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// If not, report an appropriate diagnostic. Does not check that the expression 101851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// is of literal type. 1019f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool CheckConstantExpression(EvalInfo &Info, const Expr *E, 1020c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const CCValue &CCValue, APValue &Value, 1021c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith CheckConstantExpressionKind CCEK 1022c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith = CCEK_Constant) { 10239a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith if (!CCValue.isLValue()) { 10249a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith Value = CCValue; 10259a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith return true; 10269a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith } 1027c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return CheckLValueConstantExpression(Info, E, CCValue, Value, CCEK); 10289a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith} 10299a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith 10309e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smithconst ValueDecl *GetLValueBaseDecl(const LValue &LVal) { 10311bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith return LVal.Base.dyn_cast<const ValueDecl*>(); 10329e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith} 10339e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith 10349e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smithstatic bool IsLiteralLValue(const LValue &Value) { 10351bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith return Value.Base.dyn_cast<const Expr*>() && !Value.Frame; 10369e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith} 10379e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith 103865ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smithstatic bool IsWeakLValue(const LValue &Value) { 103965ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith const ValueDecl *Decl = GetLValueBaseDecl(Value); 10400dd7a25e8d679de1dc0ce788222d6dee0e879885Lang Hames return Decl && Decl->isWeak(); 104165ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith} 104265ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith 1043e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic bool EvalPointerValueAsBool(const CCValue &Value, bool &Result) { 10443554283157190e67918fad4221a5e6faf9317362John McCall // A null base expression indicates a null pointer. These are always 10453554283157190e67918fad4221a5e6faf9317362John McCall // evaluatable, and they are false unless the offset is zero. 1046e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!Value.getLValueBase()) { 1047e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Result = !Value.getLValueOffset().isZero(); 10483554283157190e67918fad4221a5e6faf9317362John McCall return true; 10493554283157190e67918fad4221a5e6faf9317362John McCall } 10503554283157190e67918fad4221a5e6faf9317362John McCall 1051e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // We have a non-null base. These are generally known to be true, but if it's 1052e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // a weak declaration it can be null at runtime. 10533554283157190e67918fad4221a5e6faf9317362John McCall Result = true; 1054e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const ValueDecl *Decl = Value.getLValueBase().dyn_cast<const ValueDecl*>(); 10550dd7a25e8d679de1dc0ce788222d6dee0e879885Lang Hames return !Decl || !Decl->isWeak(); 10565bc86103767c2abcbfdd6518e0ccbbbb6aa59e0fEli Friedman} 10575bc86103767c2abcbfdd6518e0ccbbbb6aa59e0fEli Friedman 105847a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smithstatic bool HandleConversionToBool(const CCValue &Val, bool &Result) { 1059c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith switch (Val.getKind()) { 1060c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case APValue::Uninitialized: 1061c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return false; 1062c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case APValue::Int: 1063c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith Result = Val.getInt().getBoolValue(); 106441bf4f38348561a0f12c10d34f1673cd19a6eb04Richard Smith return true; 1065c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case APValue::Float: 1066c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith Result = !Val.getFloat().isZero(); 1067a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman return true; 1068c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case APValue::ComplexInt: 1069c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith Result = Val.getComplexIntReal().getBoolValue() || 1070c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith Val.getComplexIntImag().getBoolValue(); 1071c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return true; 1072c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case APValue::ComplexFloat: 1073c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith Result = !Val.getComplexFloatReal().isZero() || 1074c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith !Val.getComplexFloatImag().isZero(); 1075436c8898cd1c93c5bacd3fcc4ac586bc5cd77062Richard Smith return true; 1076e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case APValue::LValue: 1077e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return EvalPointerValueAsBool(Val, Result); 1078e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case APValue::MemberPointer: 1079e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Result = Val.getMemberPointerDecl(); 1080e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 1081c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case APValue::Vector: 1082cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith case APValue::Array: 1083180f47959a066795cc0f409433023af448bb0328Richard Smith case APValue::Struct: 1084180f47959a066795cc0f409433023af448bb0328Richard Smith case APValue::Union: 108565639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman case APValue::AddrLabelDiff: 1086c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return false; 10874efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman } 10884efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 1089c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith llvm_unreachable("unknown APValue kind"); 1090c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith} 1091c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 1092c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smithstatic bool EvaluateAsBooleanCondition(const Expr *E, bool &Result, 1093c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith EvalInfo &Info) { 1094c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith assert(E->isRValue() && "missing lvalue-to-rvalue conv in bool condition"); 109547a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith CCValue Val; 1096c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith if (!Evaluate(Val, Info, E)) 1097c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return false; 1098c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return HandleConversionToBool(Val, Result); 10994efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman} 11004efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 1101c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithtemplate<typename T> 1102c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstatic bool HandleOverflow(EvalInfo &Info, const Expr *E, 1103c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const T &SrcValue, QualType DestType) { 1104c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Info.Diag(E->getExprLoc(), diag::note_constexpr_overflow) 1105789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith << SrcValue << DestType; 1106c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return false; 1107c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith} 1108c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 1109c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstatic bool HandleFloatToIntCast(EvalInfo &Info, const Expr *E, 1110c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith QualType SrcType, const APFloat &Value, 1111c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith QualType DestType, APSInt &Result) { 1112c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith unsigned DestWidth = Info.Ctx.getIntWidth(DestType); 1113a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar // Determine whether we are converting to unsigned or signed. 1114575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor bool DestSigned = DestType->isSignedIntegerOrEnumerationType(); 11151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1116c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Result = APSInt(DestWidth, !DestSigned); 1117a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar bool ignored; 1118c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (Value.convertToInteger(Result, llvm::APFloat::rmTowardZero, &ignored) 1119c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith & APFloat::opInvalidOp) 1120c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return HandleOverflow(Info, E, Value, DestType); 1121c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return true; 1122a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar} 1123a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar 1124c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstatic bool HandleFloatToFloatCast(EvalInfo &Info, const Expr *E, 1125c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith QualType SrcType, QualType DestType, 1126c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith APFloat &Result) { 1127c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith APFloat Value = Result; 1128a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar bool ignored; 1129c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (Result.convert(Info.Ctx.getFloatTypeSemantics(DestType), 1130c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith APFloat::rmNearestTiesToEven, &ignored) 1131c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith & APFloat::opOverflow) 1132c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return HandleOverflow(Info, E, Value, DestType); 1133c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return true; 1134a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar} 1135a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar 1136f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smithstatic APSInt HandleIntToIntCast(EvalInfo &Info, const Expr *E, 1137f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith QualType DestType, QualType SrcType, 1138f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith APSInt &Value) { 1139f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith unsigned DestWidth = Info.Ctx.getIntWidth(DestType); 1140a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar APSInt Result = Value; 1141a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar // Figure out if this is a truncate, extend or noop cast. 1142a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar // If the input is signed, do a sign extend, noop, or truncate. 11439f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad Result = Result.extOrTrunc(DestWidth); 1144575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType()); 1145a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar return Result; 1146a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar} 1147a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar 1148c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstatic bool HandleIntToFloatCast(EvalInfo &Info, const Expr *E, 1149c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith QualType SrcType, const APSInt &Value, 1150c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith QualType DestType, APFloat &Result) { 1151c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Result = APFloat(Info.Ctx.getFloatTypeSemantics(DestType), 1); 1152c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (Result.convertFromAPInt(Value, Value.isSigned(), 1153c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith APFloat::rmNearestTiesToEven) 1154c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith & APFloat::opOverflow) 1155c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return HandleOverflow(Info, E, Value, DestType); 1156c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return true; 1157a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar} 1158a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar 1159e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedmanstatic bool EvalAndBitcastToAPInt(EvalInfo &Info, const Expr *E, 1160e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman llvm::APInt &Res) { 1161e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman CCValue SVal; 1162e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman if (!Evaluate(SVal, Info, E)) 1163e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman return false; 1164e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman if (SVal.isInt()) { 1165e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman Res = SVal.getInt(); 1166e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman return true; 1167e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman } 1168e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman if (SVal.isFloat()) { 1169e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman Res = SVal.getFloat().bitcastToAPInt(); 1170e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman return true; 1171e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman } 1172e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman if (SVal.isVector()) { 1173e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman QualType VecTy = E->getType(); 1174e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman unsigned VecSize = Info.Ctx.getTypeSize(VecTy); 1175e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman QualType EltTy = VecTy->castAs<VectorType>()->getElementType(); 1176e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman unsigned EltSize = Info.Ctx.getTypeSize(EltTy); 1177e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); 1178e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman Res = llvm::APInt::getNullValue(VecSize); 1179e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman for (unsigned i = 0; i < SVal.getVectorLength(); i++) { 1180e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman APValue &Elt = SVal.getVectorElt(i); 1181e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman llvm::APInt EltAsInt; 1182e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman if (Elt.isInt()) { 1183e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman EltAsInt = Elt.getInt(); 1184e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman } else if (Elt.isFloat()) { 1185e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman EltAsInt = Elt.getFloat().bitcastToAPInt(); 1186e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman } else { 1187e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman // Don't try to handle vectors of anything other than int or float 1188e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman // (not sure if it's possible to hit this case). 1189e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); 1190e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman return false; 1191e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman } 1192e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman unsigned BaseEltSize = EltAsInt.getBitWidth(); 1193e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman if (BigEndian) 1194e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman Res |= EltAsInt.zextOrTrunc(VecSize).rotr(i*EltSize+BaseEltSize); 1195e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman else 1196e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman Res |= EltAsInt.zextOrTrunc(VecSize).rotl(i*EltSize); 1197e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman } 1198e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman return true; 1199e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman } 1200e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman // Give up if the input isn't an int, float, or vector. For example, we 1201e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman // reject "(v4i16)(intptr_t)&a". 1202e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); 1203e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman return false; 1204e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman} 1205e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman 1206b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith/// Cast an lvalue referring to a base subobject to a derived class, by 1207b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith/// truncating the lvalue's path to the given length. 1208b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithstatic bool CastToDerivedClass(EvalInfo &Info, const Expr *E, LValue &Result, 1209b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith const RecordDecl *TruncatedType, 1210b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith unsigned TruncatedElements) { 1211b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith SubobjectDesignator &D = Result.Designator; 1212180f47959a066795cc0f409433023af448bb0328Richard Smith 1213b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // Check we actually point to a derived class object. 1214b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (TruncatedElements == D.Entries.size()) 1215b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return true; 1216b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith assert(TruncatedElements >= D.MostDerivedPathLength && 1217b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith "not casting to a derived class"); 1218b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (!Result.checkSubobject(Info, E, CSK_Derived)) 1219180f47959a066795cc0f409433023af448bb0328Richard Smith return false; 1220180f47959a066795cc0f409433023af448bb0328Richard Smith 1221b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // Truncate the path to the subobject, and remove any derived-to-base offsets. 1222e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const RecordDecl *RD = TruncatedType; 1223e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith for (unsigned I = TruncatedElements, N = D.Entries.size(); I != N; ++I) { 1224180f47959a066795cc0f409433023af448bb0328Richard Smith const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); 1225180f47959a066795cc0f409433023af448bb0328Richard Smith const CXXRecordDecl *Base = getAsBaseClass(D.Entries[I]); 1226e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (isVirtualBaseClass(D.Entries[I])) 1227180f47959a066795cc0f409433023af448bb0328Richard Smith Result.Offset -= Layout.getVBaseClassOffset(Base); 1228e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith else 1229180f47959a066795cc0f409433023af448bb0328Richard Smith Result.Offset -= Layout.getBaseClassOffset(Base); 1230180f47959a066795cc0f409433023af448bb0328Richard Smith RD = Base; 1231180f47959a066795cc0f409433023af448bb0328Richard Smith } 1232e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith D.Entries.resize(TruncatedElements); 1233180f47959a066795cc0f409433023af448bb0328Richard Smith return true; 1234180f47959a066795cc0f409433023af448bb0328Richard Smith} 1235180f47959a066795cc0f409433023af448bb0328Richard Smith 1236b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithstatic void HandleLValueDirectBase(EvalInfo &Info, const Expr *E, LValue &Obj, 1237180f47959a066795cc0f409433023af448bb0328Richard Smith const CXXRecordDecl *Derived, 1238180f47959a066795cc0f409433023af448bb0328Richard Smith const CXXRecordDecl *Base, 1239180f47959a066795cc0f409433023af448bb0328Richard Smith const ASTRecordLayout *RL = 0) { 1240180f47959a066795cc0f409433023af448bb0328Richard Smith if (!RL) RL = &Info.Ctx.getASTRecordLayout(Derived); 1241180f47959a066795cc0f409433023af448bb0328Richard Smith Obj.getLValueOffset() += RL->getBaseClassOffset(Base); 1242b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Obj.addDecl(Info, E, Base, /*Virtual*/ false); 1243180f47959a066795cc0f409433023af448bb0328Richard Smith} 1244180f47959a066795cc0f409433023af448bb0328Richard Smith 1245b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithstatic bool HandleLValueBase(EvalInfo &Info, const Expr *E, LValue &Obj, 1246180f47959a066795cc0f409433023af448bb0328Richard Smith const CXXRecordDecl *DerivedDecl, 1247180f47959a066795cc0f409433023af448bb0328Richard Smith const CXXBaseSpecifier *Base) { 1248180f47959a066795cc0f409433023af448bb0328Richard Smith const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl(); 1249180f47959a066795cc0f409433023af448bb0328Richard Smith 1250180f47959a066795cc0f409433023af448bb0328Richard Smith if (!Base->isVirtual()) { 1251b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith HandleLValueDirectBase(Info, E, Obj, DerivedDecl, BaseDecl); 1252180f47959a066795cc0f409433023af448bb0328Richard Smith return true; 1253180f47959a066795cc0f409433023af448bb0328Richard Smith } 1254180f47959a066795cc0f409433023af448bb0328Richard Smith 1255b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith SubobjectDesignator &D = Obj.Designator; 1256b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (D.Invalid) 1257b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return false; 1258b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 1259180f47959a066795cc0f409433023af448bb0328Richard Smith // Extract most-derived object and corresponding type. 1260b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith DerivedDecl = D.MostDerivedType->getAsCXXRecordDecl(); 1261b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (!CastToDerivedClass(Info, E, Obj, DerivedDecl, D.MostDerivedPathLength)) 1262180f47959a066795cc0f409433023af448bb0328Richard Smith return false; 1263180f47959a066795cc0f409433023af448bb0328Richard Smith 1264b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // Find the virtual base class. 1265180f47959a066795cc0f409433023af448bb0328Richard Smith const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(DerivedDecl); 1266180f47959a066795cc0f409433023af448bb0328Richard Smith Obj.getLValueOffset() += Layout.getVBaseClassOffset(BaseDecl); 1267b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Obj.addDecl(Info, E, BaseDecl, /*Virtual*/ true); 1268180f47959a066795cc0f409433023af448bb0328Richard Smith return true; 1269180f47959a066795cc0f409433023af448bb0328Richard Smith} 1270180f47959a066795cc0f409433023af448bb0328Richard Smith 1271180f47959a066795cc0f409433023af448bb0328Richard Smith/// Update LVal to refer to the given field, which must be a member of the type 1272180f47959a066795cc0f409433023af448bb0328Richard Smith/// currently described by LVal. 1273b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithstatic void HandleLValueMember(EvalInfo &Info, const Expr *E, LValue &LVal, 1274180f47959a066795cc0f409433023af448bb0328Richard Smith const FieldDecl *FD, 1275180f47959a066795cc0f409433023af448bb0328Richard Smith const ASTRecordLayout *RL = 0) { 1276180f47959a066795cc0f409433023af448bb0328Richard Smith if (!RL) 1277180f47959a066795cc0f409433023af448bb0328Richard Smith RL = &Info.Ctx.getASTRecordLayout(FD->getParent()); 1278180f47959a066795cc0f409433023af448bb0328Richard Smith 1279180f47959a066795cc0f409433023af448bb0328Richard Smith unsigned I = FD->getFieldIndex(); 1280180f47959a066795cc0f409433023af448bb0328Richard Smith LVal.Offset += Info.Ctx.toCharUnitsFromBits(RL->getFieldOffset(I)); 1281b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith LVal.addDecl(Info, E, FD); 1282180f47959a066795cc0f409433023af448bb0328Richard Smith} 1283180f47959a066795cc0f409433023af448bb0328Richard Smith 1284d9b02e726262e4009dda830998bb934172ac0020Richard Smith/// Update LVal to refer to the given indirect field. 1285d9b02e726262e4009dda830998bb934172ac0020Richard Smithstatic void HandleLValueIndirectMember(EvalInfo &Info, const Expr *E, 1286d9b02e726262e4009dda830998bb934172ac0020Richard Smith LValue &LVal, 1287d9b02e726262e4009dda830998bb934172ac0020Richard Smith const IndirectFieldDecl *IFD) { 1288d9b02e726262e4009dda830998bb934172ac0020Richard Smith for (IndirectFieldDecl::chain_iterator C = IFD->chain_begin(), 1289d9b02e726262e4009dda830998bb934172ac0020Richard Smith CE = IFD->chain_end(); C != CE; ++C) 1290d9b02e726262e4009dda830998bb934172ac0020Richard Smith HandleLValueMember(Info, E, LVal, cast<FieldDecl>(*C)); 1291d9b02e726262e4009dda830998bb934172ac0020Richard Smith} 1292d9b02e726262e4009dda830998bb934172ac0020Richard Smith 1293180f47959a066795cc0f409433023af448bb0328Richard Smith/// Get the size of the given type in char units. 1294180f47959a066795cc0f409433023af448bb0328Richard Smithstatic bool HandleSizeof(EvalInfo &Info, QualType Type, CharUnits &Size) { 1295180f47959a066795cc0f409433023af448bb0328Richard Smith // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc 1296180f47959a066795cc0f409433023af448bb0328Richard Smith // extension. 1297180f47959a066795cc0f409433023af448bb0328Richard Smith if (Type->isVoidType() || Type->isFunctionType()) { 1298180f47959a066795cc0f409433023af448bb0328Richard Smith Size = CharUnits::One(); 1299180f47959a066795cc0f409433023af448bb0328Richard Smith return true; 1300180f47959a066795cc0f409433023af448bb0328Richard Smith } 1301180f47959a066795cc0f409433023af448bb0328Richard Smith 1302180f47959a066795cc0f409433023af448bb0328Richard Smith if (!Type->isConstantSizeType()) { 1303180f47959a066795cc0f409433023af448bb0328Richard Smith // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. 1304b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // FIXME: Diagnostic. 1305180f47959a066795cc0f409433023af448bb0328Richard Smith return false; 1306180f47959a066795cc0f409433023af448bb0328Richard Smith } 1307180f47959a066795cc0f409433023af448bb0328Richard Smith 1308180f47959a066795cc0f409433023af448bb0328Richard Smith Size = Info.Ctx.getTypeSizeInChars(Type); 1309180f47959a066795cc0f409433023af448bb0328Richard Smith return true; 1310180f47959a066795cc0f409433023af448bb0328Richard Smith} 1311180f47959a066795cc0f409433023af448bb0328Richard Smith 1312180f47959a066795cc0f409433023af448bb0328Richard Smith/// Update a pointer value to model pointer arithmetic. 1313180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param Info - Information about the ongoing evaluation. 1314b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith/// \param E - The expression being evaluated, for diagnostic purposes. 1315180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param LVal - The pointer value to be updated. 1316180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param EltTy - The pointee type represented by LVal. 1317180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param Adjustment - The adjustment, in objects of type EltTy, to add. 1318b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithstatic bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E, 1319b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith LValue &LVal, QualType EltTy, 1320b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith int64_t Adjustment) { 1321180f47959a066795cc0f409433023af448bb0328Richard Smith CharUnits SizeOfPointee; 1322180f47959a066795cc0f409433023af448bb0328Richard Smith if (!HandleSizeof(Info, EltTy, SizeOfPointee)) 1323180f47959a066795cc0f409433023af448bb0328Richard Smith return false; 1324180f47959a066795cc0f409433023af448bb0328Richard Smith 1325180f47959a066795cc0f409433023af448bb0328Richard Smith // Compute the new offset in the appropriate width. 1326180f47959a066795cc0f409433023af448bb0328Richard Smith LVal.Offset += Adjustment * SizeOfPointee; 1327b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith LVal.adjustIndex(Info, E, Adjustment); 1328180f47959a066795cc0f409433023af448bb0328Richard Smith return true; 1329180f47959a066795cc0f409433023af448bb0328Richard Smith} 1330180f47959a066795cc0f409433023af448bb0328Richard Smith 133103f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith/// Try to evaluate the initializer for a variable declaration. 1332f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool EvaluateVarDeclInit(EvalInfo &Info, const Expr *E, 1333f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith const VarDecl *VD, 1334177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith CallStackFrame *Frame, CCValue &Result) { 1335d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith // If this is a parameter to an active constexpr function call, perform 1336d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith // argument substitution. 1337d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(VD)) { 1338745f5147e065900267c85a5568785a1991d4838fRichard Smith // Assume arguments of a potential constant expression are unknown 1339745f5147e065900267c85a5568785a1991d4838fRichard Smith // constant expressions. 1340745f5147e065900267c85a5568785a1991d4838fRichard Smith if (Info.CheckingPotentialConstantExpression) 1341745f5147e065900267c85a5568785a1991d4838fRichard Smith return false; 1342f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!Frame || !Frame->Arguments) { 1343dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); 1344177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith return false; 1345f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 1346177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith Result = Frame->Arguments[PVD->getFunctionScopeIndex()]; 1347177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith return true; 1348d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith } 134903f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith 1350099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith // Dig out the initializer, and use the declaration which it's attached to. 1351099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith const Expr *Init = VD->getAnyInitializer(VD); 1352099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith if (!Init || Init->isValueDependent()) { 1353745f5147e065900267c85a5568785a1991d4838fRichard Smith // If we're checking a potential constant expression, the variable could be 1354745f5147e065900267c85a5568785a1991d4838fRichard Smith // initialized later. 1355745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!Info.CheckingPotentialConstantExpression) 1356745f5147e065900267c85a5568785a1991d4838fRichard Smith Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); 1357099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith return false; 1358099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith } 1359099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith 1360180f47959a066795cc0f409433023af448bb0328Richard Smith // If we're currently evaluating the initializer of this declaration, use that 1361180f47959a066795cc0f409433023af448bb0328Richard Smith // in-flight value. 1362180f47959a066795cc0f409433023af448bb0328Richard Smith if (Info.EvaluatingDecl == VD) { 1363b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Result = CCValue(Info.Ctx, *Info.EvaluatingDeclValue, 1364b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith CCValue::GlobalValue()); 1365180f47959a066795cc0f409433023af448bb0328Richard Smith return !Result.isUninit(); 1366180f47959a066795cc0f409433023af448bb0328Richard Smith } 1367180f47959a066795cc0f409433023af448bb0328Richard Smith 136865ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith // Never evaluate the initializer of a weak variable. We can't be sure that 136965ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith // this is the definition which will be used. 1370f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (VD->isWeak()) { 1371dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); 137265ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith return false; 1373f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 137465ac598c7ba7e36f2ad611f2bb39cc957053be5dRichard Smith 1375099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith // Check that we can fold the initializer. In C++, we will have already done 1376099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith // this in the cases where it matters for conformance. 1377099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith llvm::SmallVector<PartialDiagnosticAt, 8> Notes; 1378099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith if (!VD->evaluateValue(Notes)) { 1379099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Info.Diag(E->getExprLoc(), diag::note_constexpr_var_init_non_constant, 1380099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Notes.size() + 1) << VD; 1381099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Info.Note(VD->getLocation(), diag::note_declared_at); 1382099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Info.addNotes(Notes); 138347a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith return false; 1384099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith } else if (!VD->checkInitIsICE()) { 1385099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_var_init_non_constant, 1386099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Notes.size() + 1) << VD; 1387099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Info.Note(VD->getLocation(), diag::note_declared_at); 1388099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Info.addNotes(Notes); 1389f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 139003f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith 1391b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Result = CCValue(Info.Ctx, *VD->getEvaluatedValue(), CCValue::GlobalValue()); 139247a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith return true; 139303f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith} 139403f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith 1395c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smithstatic bool IsConstNonVolatile(QualType T) { 139603f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith Qualifiers Quals = T.getQualifiers(); 139703f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith return Quals.hasConst() && !Quals.hasVolatile(); 139803f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith} 139903f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith 140059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith/// Get the base index of the given base class within an APValue representing 140159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith/// the given derived class. 140259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithstatic unsigned getBaseIndex(const CXXRecordDecl *Derived, 140359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith const CXXRecordDecl *Base) { 140459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith Base = Base->getCanonicalDecl(); 140559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith unsigned Index = 0; 140659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith for (CXXRecordDecl::base_class_const_iterator I = Derived->bases_begin(), 140759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith E = Derived->bases_end(); I != E; ++I, ++Index) { 140859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith if (I->getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base) 140959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith return Index; 141059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith } 141159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith 141259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith llvm_unreachable("base class missing from derived class's bases list"); 141359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith} 141459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith 1415cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith/// Extract the designated sub-object of an rvalue. 1416f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool ExtractSubobject(EvalInfo &Info, const Expr *E, 1417f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith CCValue &Obj, QualType ObjType, 1418cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith const SubobjectDesignator &Sub, QualType SubType) { 1419b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (Sub.Invalid) 1420b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // A diagnostic will have already been produced. 1421cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith return false; 1422b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (Sub.isOnePastTheEnd()) { 14237098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Diag(E->getExprLoc(), Info.getLangOpts().CPlusPlus0x ? 1424aa5d533427d803e52ee42b250ffd6645ef5ccb0fMatt Beaumont-Gay (unsigned)diag::note_constexpr_read_past_end : 1425aa5d533427d803e52ee42b250ffd6645ef5ccb0fMatt Beaumont-Gay (unsigned)diag::note_invalid_subexpr_in_const_expr); 14267098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith return false; 14277098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } 1428f64699e8db3946e21b5f4a0421cbc58a3e439599Richard Smith if (Sub.Entries.empty()) 1429cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith return true; 1430745f5147e065900267c85a5568785a1991d4838fRichard Smith if (Info.CheckingPotentialConstantExpression && Obj.isUninit()) 1431745f5147e065900267c85a5568785a1991d4838fRichard Smith // This object might be initialized later. 1432745f5147e065900267c85a5568785a1991d4838fRichard Smith return false; 1433cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith 1434cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith assert(!Obj.isLValue() && "extracting subobject of lvalue"); 1435cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith const APValue *O = &Obj; 1436180f47959a066795cc0f409433023af448bb0328Richard Smith // Walk the designator's path to find the subobject. 1437cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith for (unsigned I = 0, N = Sub.Entries.size(); I != N; ++I) { 1438cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith if (ObjType->isArrayType()) { 1439180f47959a066795cc0f409433023af448bb0328Richard Smith // Next subobject is an array element. 1440cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(ObjType); 1441f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith assert(CAT && "vla in literal type?"); 1442cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith uint64_t Index = Sub.Entries[I].ArrayIndex; 1443f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (CAT->getSize().ule(Index)) { 14447098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // Note, it should not be possible to form a pointer with a valid 14457098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // designator which points more than one past the end of the array. 14467098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Diag(E->getExprLoc(), Info.getLangOpts().CPlusPlus0x ? 1447aa5d533427d803e52ee42b250ffd6645ef5ccb0fMatt Beaumont-Gay (unsigned)diag::note_constexpr_read_past_end : 1448aa5d533427d803e52ee42b250ffd6645ef5ccb0fMatt Beaumont-Gay (unsigned)diag::note_invalid_subexpr_in_const_expr); 1449cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith return false; 1450f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 1451cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith if (O->getArrayInitializedElts() > Index) 1452cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith O = &O->getArrayInitializedElt(Index); 1453cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith else 1454cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith O = &O->getArrayFiller(); 1455cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith ObjType = CAT->getElementType(); 1456180f47959a066795cc0f409433023af448bb0328Richard Smith } else if (const FieldDecl *Field = getAsField(Sub.Entries[I])) { 1457b4e5e286a5cd156247720b1eb204abaa8e09568dRichard Smith if (Field->isMutable()) { 1458b4e5e286a5cd156247720b1eb204abaa8e09568dRichard Smith Info.Diag(E->getExprLoc(), diag::note_constexpr_ltor_mutable, 1) 1459b4e5e286a5cd156247720b1eb204abaa8e09568dRichard Smith << Field; 1460b4e5e286a5cd156247720b1eb204abaa8e09568dRichard Smith Info.Note(Field->getLocation(), diag::note_declared_at); 1461b4e5e286a5cd156247720b1eb204abaa8e09568dRichard Smith return false; 1462b4e5e286a5cd156247720b1eb204abaa8e09568dRichard Smith } 1463b4e5e286a5cd156247720b1eb204abaa8e09568dRichard Smith 1464180f47959a066795cc0f409433023af448bb0328Richard Smith // Next subobject is a class, struct or union field. 1465180f47959a066795cc0f409433023af448bb0328Richard Smith RecordDecl *RD = ObjType->castAs<RecordType>()->getDecl(); 1466180f47959a066795cc0f409433023af448bb0328Richard Smith if (RD->isUnion()) { 1467180f47959a066795cc0f409433023af448bb0328Richard Smith const FieldDecl *UnionField = O->getUnionField(); 1468180f47959a066795cc0f409433023af448bb0328Richard Smith if (!UnionField || 1469f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith UnionField->getCanonicalDecl() != Field->getCanonicalDecl()) { 14707098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Diag(E->getExprLoc(), 14717098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith diag::note_constexpr_read_inactive_union_member) 14727098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith << Field << !UnionField << UnionField; 1473180f47959a066795cc0f409433023af448bb0328Richard Smith return false; 1474f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 1475180f47959a066795cc0f409433023af448bb0328Richard Smith O = &O->getUnionValue(); 1476180f47959a066795cc0f409433023af448bb0328Richard Smith } else 1477180f47959a066795cc0f409433023af448bb0328Richard Smith O = &O->getStructField(Field->getFieldIndex()); 1478180f47959a066795cc0f409433023af448bb0328Richard Smith ObjType = Field->getType(); 14797098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith 14807098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (ObjType.isVolatileQualified()) { 14817098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (Info.getLangOpts().CPlusPlus) { 14827098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // FIXME: Include a description of the path to the volatile subobject. 14837098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Diag(E->getExprLoc(), diag::note_constexpr_ltor_volatile_obj, 1) 14847098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith << 2 << Field; 14857098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Note(Field->getLocation(), diag::note_declared_at); 14867098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } else { 14877098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); 14887098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } 14897098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith return false; 14907098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } 1491cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith } else { 1492180f47959a066795cc0f409433023af448bb0328Richard Smith // Next subobject is a base class. 149359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith const CXXRecordDecl *Derived = ObjType->getAsCXXRecordDecl(); 149459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith const CXXRecordDecl *Base = getAsBaseClass(Sub.Entries[I]); 149559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith O = &O->getStructBase(getBaseIndex(Derived, Base)); 149659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith ObjType = Info.Ctx.getRecordType(Base); 1497cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith } 1498180f47959a066795cc0f409433023af448bb0328Richard Smith 1499f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (O->isUninit()) { 1500745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!Info.CheckingPotentialConstantExpression) 1501745f5147e065900267c85a5568785a1991d4838fRichard Smith Info.Diag(E->getExprLoc(), diag::note_constexpr_read_uninit); 1502180f47959a066795cc0f409433023af448bb0328Richard Smith return false; 1503f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 1504cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith } 1505cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith 1506b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Obj = CCValue(Info.Ctx, *O, CCValue::GlobalValue()); 1507cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith return true; 1508cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith} 1509cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith 1510f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith/// Find the position where two subobject designators diverge, or equivalently 1511f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith/// the length of the common initial subsequence. 1512f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smithstatic unsigned FindDesignatorMismatch(QualType ObjType, 1513f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith const SubobjectDesignator &A, 1514f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith const SubobjectDesignator &B, 1515f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith bool &WasArrayIndex) { 1516f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith unsigned I = 0, N = std::min(A.Entries.size(), B.Entries.size()); 1517f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith for (/**/; I != N; ++I) { 1518f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (!ObjType.isNull() && ObjType->isArrayType()) { 1519f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // Next subobject is an array element. 1520f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (A.Entries[I].ArrayIndex != B.Entries[I].ArrayIndex) { 1521f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith WasArrayIndex = true; 1522f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith return I; 1523f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith } 1524f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith ObjType = ObjType->castAsArrayTypeUnsafe()->getElementType(); 1525f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith } else { 1526f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (A.Entries[I].BaseOrMember != B.Entries[I].BaseOrMember) { 1527f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith WasArrayIndex = false; 1528f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith return I; 1529f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith } 1530f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (const FieldDecl *FD = getAsField(A.Entries[I])) 1531f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // Next subobject is a field. 1532f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith ObjType = FD->getType(); 1533f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith else 1534f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // Next subobject is a base class. 1535f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith ObjType = QualType(); 1536f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith } 1537f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith } 1538f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith WasArrayIndex = false; 1539f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith return I; 1540f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith} 1541f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 1542f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith/// Determine whether the given subobject designators refer to elements of the 1543f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith/// same array object. 1544f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smithstatic bool AreElementsOfSameArray(QualType ObjType, 1545f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith const SubobjectDesignator &A, 1546f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith const SubobjectDesignator &B) { 1547f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (A.Entries.size() != B.Entries.size()) 1548f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith return false; 1549f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 1550f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith bool IsArray = A.MostDerivedArraySize != 0; 1551f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (IsArray && A.MostDerivedPathLength != A.Entries.size()) 1552f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // A is a subobject of the array element. 1553f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith return false; 1554f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 1555f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // If A (and B) designates an array element, the last entry will be the array 1556f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // index. That doesn't have to match. Otherwise, we're in the 'implicit array 1557f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // of length 1' case, and the entire path must match. 1558f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith bool WasArrayIndex; 1559f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith unsigned CommonLength = FindDesignatorMismatch(ObjType, A, B, WasArrayIndex); 1560f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith return CommonLength >= A.Entries.size() - IsArray; 1561f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith} 1562f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 1563180f47959a066795cc0f409433023af448bb0328Richard Smith/// HandleLValueToRValueConversion - Perform an lvalue-to-rvalue conversion on 1564180f47959a066795cc0f409433023af448bb0328Richard Smith/// the given lvalue. This can also be used for 'lvalue-to-lvalue' conversions 1565180f47959a066795cc0f409433023af448bb0328Richard Smith/// for looking up the glvalue referred to by an entity of reference type. 1566180f47959a066795cc0f409433023af448bb0328Richard Smith/// 1567180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param Info - Information about the ongoing evaluation. 1568f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// \param Conv - The expression for which we are performing the conversion. 1569f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// Used for diagnostics. 15709ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith/// \param Type - The type we expect this conversion to produce, before 15719ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith/// stripping cv-qualifiers in the case of a non-clas type. 1572180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param LVal - The glvalue on which we are attempting to perform this action. 1573180f47959a066795cc0f409433023af448bb0328Richard Smith/// \param RVal - The produced value will be placed here. 1574f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool HandleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv, 1575f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith QualType Type, 1576cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith const LValue &LVal, CCValue &RVal) { 15777098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // In C, an lvalue-to-rvalue conversion is never a constant expression. 15787098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (!Info.getLangOpts().CPlusPlus) 15797098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.CCEDiag(Conv->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); 15807098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith 1581b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (LVal.Designator.Invalid) 1582b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // A diagnostic will have already been produced. 1583b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return false; 1584b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 15851bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); 1586177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith CallStackFrame *Frame = LVal.Frame; 15877098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith SourceLocation Loc = Conv->getExprLoc(); 1588c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 1589f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!LVal.Base) { 1590f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith // FIXME: Indirection through a null pointer deserves a specific diagnostic. 15917098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Diag(Loc, diag::note_invalid_subexpr_in_const_expr); 15927098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith return false; 15937098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } 15947098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith 15957098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // C++11 DR1311: An lvalue-to-rvalue conversion on a volatile-qualified type 15967098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // is not a constant expression (even if the object is non-volatile). We also 15977098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // apply this rule to C++98, in order to conform to the expected 'volatile' 15987098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // semantics. 15997098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (Type.isVolatileQualified()) { 16007098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (Info.getLangOpts().CPlusPlus) 16017098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Diag(Loc, diag::note_constexpr_ltor_volatile_type) << Type; 16027098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith else 16037098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Diag(Loc); 1604c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return false; 1605f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 1606c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 16071bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith if (const ValueDecl *D = LVal.Base.dyn_cast<const ValueDecl*>()) { 1608c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // In C++98, const, non-volatile integers initialized with ICEs are ICEs. 1609c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // In C++11, constexpr, non-volatile variables initialized with constant 1610d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith // expressions are constant expressions too. Inside constexpr functions, 1611d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith // parameters are constant expressions even if they're non-const. 1612c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // In C, such things can also be folded, although they are not ICEs. 1613c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith const VarDecl *VD = dyn_cast<VarDecl>(D); 1614f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (const VarDecl *VDef = VD->getDefinition()) 1615f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith VD = VDef; 1616f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!VD || VD->isInvalidDecl()) { 16177098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Diag(Loc); 16180a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith return false; 1619f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 1620f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 16217098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // DR1313: If the object is volatile-qualified but the glvalue was not, 16227098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // behavior is undefined so the result is not a constant expression. 16231bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith QualType VT = VD->getType(); 16247098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (VT.isVolatileQualified()) { 16257098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (Info.getLangOpts().CPlusPlus) { 16267098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Diag(Loc, diag::note_constexpr_ltor_volatile_obj, 1) << 1 << VD; 16277098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Note(VD->getLocation(), diag::note_declared_at); 16287098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } else { 16297098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Diag(Loc); 1630f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 16317098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith return false; 16327098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } 16337098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith 16347098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (!isa<ParmVarDecl>(VD)) { 16357098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (VD->isConstexpr()) { 16367098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // OK, we can read this variable. 16377098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } else if (VT->isIntegralOrEnumerationType()) { 16387098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (!VT.isConstQualified()) { 16397098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (Info.getLangOpts().CPlusPlus) { 16407098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Diag(Loc, diag::note_constexpr_ltor_non_const_int, 1) << VD; 16417098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Note(VD->getLocation(), diag::note_declared_at); 16427098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } else { 16437098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Diag(Loc); 16447098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } 16457098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith return false; 16467098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } 16477098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } else if (VT->isFloatingType() && VT.isConstQualified()) { 16487098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // We support folding of const floating-point types, in order to make 16497098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // static const data members of such types (supported as an extension) 16507098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // more useful. 16517098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (Info.getLangOpts().CPlusPlus0x) { 16527098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.CCEDiag(Loc, diag::note_constexpr_ltor_non_constexpr, 1) << VD; 16537098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Note(VD->getLocation(), diag::note_declared_at); 16547098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } else { 16557098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.CCEDiag(Loc); 16567098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } 16577098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } else { 16587098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // FIXME: Allow folding of values of any literal type in all languages. 16597098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (Info.getLangOpts().CPlusPlus0x) { 16607098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Diag(Loc, diag::note_constexpr_ltor_non_constexpr, 1) << VD; 16617098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Note(VD->getLocation(), diag::note_declared_at); 16627098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } else { 16637098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Diag(Loc); 16647098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } 16650a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith return false; 1666f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 16670a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith } 16687098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith 1669f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!EvaluateVarDeclInit(Info, Conv, VD, Frame, RVal)) 1670c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return false; 1671c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 167247a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith if (isa<ParmVarDecl>(VD) || !VD->getAnyInitializer()->isLValue()) 1673f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return ExtractSubobject(Info, Conv, RVal, VT, LVal.Designator, Type); 1674c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 1675c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // The declaration was initialized by an lvalue, with no lvalue-to-rvalue 1676c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // conversion. This happens when the declaration and the lvalue should be 1677c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // considered synonymous, for instance when initializing an array of char 1678c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // from a string literal. Continue as if the initializer lvalue was the 1679c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // value we were originally given. 16800a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith assert(RVal.getLValueOffset().isZero() && 16810a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith "offset for lvalue init of non-reference"); 16821bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith Base = RVal.getLValueBase().get<const Expr*>(); 1683177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith Frame = RVal.getLValueFrame(); 1684c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith } 1685c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 16867098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // Volatile temporary objects cannot be read in constant expressions. 16877098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (Base->getType().isVolatileQualified()) { 16887098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (Info.getLangOpts().CPlusPlus) { 16897098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Diag(Loc, diag::note_constexpr_ltor_volatile_obj, 1) << 0; 16907098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Note(Base->getExprLoc(), diag::note_constexpr_temporary_here); 16917098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } else { 16927098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Diag(Loc); 16937098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } 16947098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith return false; 16957098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith } 16967098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith 16970a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith // FIXME: Support PredefinedExpr, ObjCEncodeExpr, MakeStringConstant 16980a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith if (const StringLiteral *S = dyn_cast<StringLiteral>(Base)) { 16990a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith const SubobjectDesignator &Designator = LVal.Designator; 1700f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (Designator.Invalid || Designator.Entries.size() != 1) { 1701dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith Info.Diag(Conv->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); 17020a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith return false; 1703f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 17040a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith 17050a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith assert(Type->isIntegerType() && "string element not integer type"); 17069a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith uint64_t Index = Designator.Entries[0].ArrayIndex; 17077098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith const ConstantArrayType *CAT = 17087098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Ctx.getAsConstantArrayType(S->getType()); 17097098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith if (Index >= CAT->getSize().getZExtValue()) { 17107098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // Note, it should not be possible to form a pointer which points more 17117098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // than one past the end of the array without producing a prior const expr 17127098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // diagnostic. 17137098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith Info.Diag(Loc, diag::note_constexpr_read_past_end); 17140a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith return false; 1715f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 17160a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(), 17170a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith Type->isUnsignedIntegerType()); 17180a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith if (Index < S->getLength()) 17190a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith Value = S->getCodeUnit(Index); 17200a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith RVal = CCValue(Value); 17210a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith return true; 17220a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith } 17230a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith 1724177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith if (Frame) { 1725cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith // If this is a temporary expression with a nontrivial initializer, grab the 1726cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith // value from the relevant stack frame. 1727177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith RVal = Frame->Temporaries[Base]; 1728cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith } else if (const CompoundLiteralExpr *CLE 1729cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith = dyn_cast<CompoundLiteralExpr>(Base)) { 1730cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith // In C99, a CompoundLiteralExpr is an lvalue, and we defer evaluating the 1731cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith // initializer until now for such expressions. Such an expression can't be 1732cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith // an ICE in C, so this only matters for fold. 1733c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?"); 1734cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith if (!Evaluate(RVal, Info, CLE->getInitializer())) 1735cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith return false; 1736f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } else { 1737dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith Info.Diag(Conv->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); 1738cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith return false; 1739f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 1740c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 1741f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return ExtractSubobject(Info, Conv, RVal, Base->getType(), LVal.Designator, 1742f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith Type); 1743c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith} 1744c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 174559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith/// Build an lvalue for the object argument of a member function call. 174659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithstatic bool EvaluateObjectArgument(EvalInfo &Info, const Expr *Object, 174759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith LValue &This) { 174859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith if (Object->getType()->isPointerType()) 174959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith return EvaluatePointer(Object, This, Info); 175059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith 175159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith if (Object->isGLValue()) 175259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith return EvaluateLValue(Object, This, Info); 175359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith 1754e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (Object->getType()->isLiteralType()) 1755e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return EvaluateTemporary(Object, This, Info); 1756e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 1757e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 1758e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith} 1759e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 1760e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// HandleMemberPointerAccess - Evaluate a member access operation and build an 1761e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// lvalue referring to the result. 1762e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// 1763e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// \param Info - Information about the ongoing evaluation. 1764e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// \param BO - The member pointer access operation. 1765e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// \param LV - Filled in with a reference to the resulting object. 1766e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// \param IncludeMember - Specifies whether the member itself is included in 1767e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// the resulting LValue subobject designator. This is not possible when 1768e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// creating a bound member function. 1769e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// \return The field or method declaration to which the member pointer refers, 1770e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// or 0 if evaluation fails. 1771e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info, 1772e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const BinaryOperator *BO, 1773e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LValue &LV, 1774e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool IncludeMember = true) { 1775e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith assert(BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI); 1776e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 1777745f5147e065900267c85a5568785a1991d4838fRichard Smith bool EvalObjOK = EvaluateObjectArgument(Info, BO->getLHS(), LV); 1778745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!EvalObjOK && !Info.keepEvaluatingAfterFailure()) 1779e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return 0; 1780e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 1781e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith MemberPtr MemPtr; 1782e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!EvaluateMemberPointer(BO->getRHS(), MemPtr, Info)) 1783e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return 0; 1784e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 1785e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // C++11 [expr.mptr.oper]p6: If the second operand is the null pointer to 1786e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // member value, the behavior is undefined. 1787e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!MemPtr.getDecl()) 1788e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return 0; 1789e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 1790745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!EvalObjOK) 1791745f5147e065900267c85a5568785a1991d4838fRichard Smith return 0; 1792745f5147e065900267c85a5568785a1991d4838fRichard Smith 1793e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (MemPtr.isDerivedMember()) { 1794e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // This is a member of some derived class. Truncate LV appropriately. 1795e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // The end of the derived-to-base path for the base object must match the 1796e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // derived-to-base path for the member pointer. 1797b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (LV.Designator.MostDerivedPathLength + MemPtr.Path.size() > 1798e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LV.Designator.Entries.size()) 1799e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return 0; 1800e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith unsigned PathLengthToMember = 1801e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LV.Designator.Entries.size() - MemPtr.Path.size(); 1802e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith for (unsigned I = 0, N = MemPtr.Path.size(); I != N; ++I) { 1803e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const CXXRecordDecl *LVDecl = getAsBaseClass( 1804e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LV.Designator.Entries[PathLengthToMember + I]); 1805e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const CXXRecordDecl *MPDecl = MemPtr.Path[I]; 1806e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (LVDecl->getCanonicalDecl() != MPDecl->getCanonicalDecl()) 1807e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return 0; 1808e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 1809e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 1810e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // Truncate the lvalue to the appropriate derived class. 1811b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (!CastToDerivedClass(Info, BO, LV, MemPtr.getContainingRecord(), 1812b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith PathLengthToMember)) 1813b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return 0; 1814e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } else if (!MemPtr.Path.empty()) { 1815e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // Extend the LValue path with the member pointer's path. 1816e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LV.Designator.Entries.reserve(LV.Designator.Entries.size() + 1817e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith MemPtr.Path.size() + IncludeMember); 1818e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 1819e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // Walk down to the appropriate base class. 1820e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith QualType LVType = BO->getLHS()->getType(); 1821e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (const PointerType *PT = LVType->getAs<PointerType>()) 1822e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LVType = PT->getPointeeType(); 1823e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const CXXRecordDecl *RD = LVType->getAsCXXRecordDecl(); 1824e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith assert(RD && "member pointer access on non-class-type expression"); 1825e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // The first class in the path is that of the lvalue. 1826e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith for (unsigned I = 1, N = MemPtr.Path.size(); I != N; ++I) { 1827e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const CXXRecordDecl *Base = MemPtr.Path[N - I - 1]; 1828b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith HandleLValueDirectBase(Info, BO, LV, RD, Base); 1829e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith RD = Base; 1830e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 1831e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // Finally cast to the class containing the member. 1832b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith HandleLValueDirectBase(Info, BO, LV, RD, MemPtr.getContainingRecord()); 1833e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 1834e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 1835e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // Add the member. Note that we cannot build bound member functions here. 1836e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (IncludeMember) { 1837d9b02e726262e4009dda830998bb934172ac0020Richard Smith if (const FieldDecl *FD = dyn_cast<FieldDecl>(MemPtr.getDecl())) 1838d9b02e726262e4009dda830998bb934172ac0020Richard Smith HandleLValueMember(Info, BO, LV, FD); 1839d9b02e726262e4009dda830998bb934172ac0020Richard Smith else if (const IndirectFieldDecl *IFD = 1840d9b02e726262e4009dda830998bb934172ac0020Richard Smith dyn_cast<IndirectFieldDecl>(MemPtr.getDecl())) 1841d9b02e726262e4009dda830998bb934172ac0020Richard Smith HandleLValueIndirectMember(Info, BO, LV, IFD); 1842d9b02e726262e4009dda830998bb934172ac0020Richard Smith else 1843d9b02e726262e4009dda830998bb934172ac0020Richard Smith llvm_unreachable("can't construct reference to bound member function"); 1844e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 1845e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 1846e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return MemPtr.getDecl(); 1847e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith} 1848e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 1849e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// HandleBaseToDerivedCast - Apply the given base-to-derived cast operation on 1850e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// the provided lvalue, which currently refers to the base object. 1851e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic bool HandleBaseToDerivedCast(EvalInfo &Info, const CastExpr *E, 1852e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LValue &Result) { 1853e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith SubobjectDesignator &D = Result.Designator; 1854b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (D.Invalid || !Result.checkNullPointer(Info, E, CSK_Derived)) 1855e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 1856e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 1857e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith QualType TargetQT = E->getType(); 1858e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (const PointerType *PT = TargetQT->getAs<PointerType>()) 1859e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith TargetQT = PT->getPointeeType(); 1860b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 1861b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // Check this cast lands within the final derived-to-base subobject path. 1862b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (D.MostDerivedPathLength + E->path_size() > D.Entries.size()) { 1863b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_invalid_downcast) 1864b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith << D.MostDerivedType << TargetQT; 1865b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return false; 1866b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } 1867b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 1868b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // Check the type of the final cast. We don't need to check the path, 1869b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith // since a cast can only be formed if the path is unique. 1870b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith unsigned NewEntriesSize = D.Entries.size() - E->path_size(); 1871e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const CXXRecordDecl *TargetType = TargetQT->getAsCXXRecordDecl(); 1872e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const CXXRecordDecl *FinalType; 1873b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (NewEntriesSize == D.MostDerivedPathLength) 1874b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith FinalType = D.MostDerivedType->getAsCXXRecordDecl(); 1875b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith else 1876e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith FinalType = getAsBaseClass(D.Entries[NewEntriesSize - 1]); 1877b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (FinalType->getCanonicalDecl() != TargetType->getCanonicalDecl()) { 1878b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_invalid_downcast) 1879b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith << D.MostDerivedType << TargetQT; 1880e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 1881b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } 1882e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 1883e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // Truncate the lvalue to the appropriate derived class. 1884b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return CastToDerivedClass(Info, E, Result, TargetType, NewEntriesSize); 188559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith} 188659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith 1887c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stumpnamespace { 1888d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smithenum EvalStmtResult { 1889d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith /// Evaluation failed. 1890d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith ESR_Failed, 1891d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith /// Hit a 'return' statement. 1892d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith ESR_Returned, 1893d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith /// Evaluation succeeded. 1894d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith ESR_Succeeded 1895d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith}; 1896d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith} 1897d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 1898d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith// Evaluate a statement. 1899c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstatic EvalStmtResult EvaluateStmt(APValue &Result, EvalInfo &Info, 1900d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith const Stmt *S) { 1901d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith switch (S->getStmtClass()) { 1902d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith default: 1903d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith return ESR_Failed; 1904d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 1905d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith case Stmt::NullStmtClass: 1906d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith case Stmt::DeclStmtClass: 1907d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith return ESR_Succeeded; 1908d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 1909c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith case Stmt::ReturnStmtClass: { 1910c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith CCValue CCResult; 1911c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const Expr *RetExpr = cast<ReturnStmt>(S)->getRetValue(); 1912c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (!Evaluate(CCResult, Info, RetExpr) || 1913c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith !CheckConstantExpression(Info, RetExpr, CCResult, Result, 1914c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith CCEK_ReturnValue)) 1915c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return ESR_Failed; 1916c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return ESR_Returned; 1917c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } 1918d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 1919d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith case Stmt::CompoundStmtClass: { 1920d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith const CompoundStmt *CS = cast<CompoundStmt>(S); 1921d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith for (CompoundStmt::const_body_iterator BI = CS->body_begin(), 1922d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith BE = CS->body_end(); BI != BE; ++BI) { 1923d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith EvalStmtResult ESR = EvaluateStmt(Result, Info, *BI); 1924d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith if (ESR != ESR_Succeeded) 1925d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith return ESR; 1926d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith } 1927d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith return ESR_Succeeded; 1928d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith } 1929d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith } 1930d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith} 1931d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 19326180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith/// CheckTrivialDefaultConstructor - Check whether a constructor is a trivial 19336180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith/// default constructor. If so, we'll fold it whether or not it's marked as 19346180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith/// constexpr. If it is marked as constexpr, we will never implicitly define it, 19356180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith/// so we need special handling. 19366180245e9f63d2927b185ec251fb75aba30f1cacRichard Smithstatic bool CheckTrivialDefaultConstructor(EvalInfo &Info, SourceLocation Loc, 193751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith const CXXConstructorDecl *CD, 193851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith bool IsValueInitialization) { 19396180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith if (!CD->isTrivial() || !CD->isDefaultConstructor()) 19406180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith return false; 19416180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith 19424c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith // Value-initialization does not call a trivial default constructor, so such a 19434c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith // call is a core constant expression whether or not the constructor is 19444c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith // constexpr. 19454c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith if (!CD->isConstexpr() && !IsValueInitialization) { 19466180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith if (Info.getLangOpts().CPlusPlus0x) { 19474c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith // FIXME: If DiagDecl is an implicitly-declared special member function, 19484c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith // we should be much more explicit about why it's not constexpr. 19494c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith Info.CCEDiag(Loc, diag::note_constexpr_invalid_function, 1) 19504c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith << /*IsConstexpr*/0 << /*IsConstructor*/1 << CD; 19514c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith Info.Note(CD->getLocation(), diag::note_declared_at); 19526180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith } else { 19536180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith Info.CCEDiag(Loc, diag::note_invalid_subexpr_in_const_expr); 19546180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith } 19556180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith } 19566180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith return true; 19576180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith} 19586180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith 1959c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith/// CheckConstexprFunction - Check that a function can be called in a constant 1960c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith/// expression. 1961c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstatic bool CheckConstexprFunction(EvalInfo &Info, SourceLocation CallLoc, 1962c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const FunctionDecl *Declaration, 1963c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const FunctionDecl *Definition) { 1964745f5147e065900267c85a5568785a1991d4838fRichard Smith // Potential constant expressions can contain calls to declared, but not yet 1965745f5147e065900267c85a5568785a1991d4838fRichard Smith // defined, constexpr functions. 1966745f5147e065900267c85a5568785a1991d4838fRichard Smith if (Info.CheckingPotentialConstantExpression && !Definition && 1967745f5147e065900267c85a5568785a1991d4838fRichard Smith Declaration->isConstexpr()) 1968745f5147e065900267c85a5568785a1991d4838fRichard Smith return false; 1969745f5147e065900267c85a5568785a1991d4838fRichard Smith 1970c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith // Can we evaluate this function call? 1971c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (Definition && Definition->isConstexpr() && !Definition->isInvalidDecl()) 1972c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return true; 1973c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 1974c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (Info.getLangOpts().CPlusPlus0x) { 1975c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const FunctionDecl *DiagDecl = Definition ? Definition : Declaration; 1976099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith // FIXME: If DiagDecl is an implicitly-declared special member function, we 1977099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith // should be much more explicit about why it's not constexpr. 1978c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Info.Diag(CallLoc, diag::note_constexpr_invalid_function, 1) 1979c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith << DiagDecl->isConstexpr() << isa<CXXConstructorDecl>(DiagDecl) 1980c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith << DiagDecl; 1981c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Info.Note(DiagDecl->getLocation(), diag::note_declared_at); 1982c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } else { 1983c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Info.Diag(CallLoc, diag::note_invalid_subexpr_in_const_expr); 1984c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } 1985c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return false; 1986c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith} 1987c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 1988180f47959a066795cc0f409433023af448bb0328Richard Smithnamespace { 1989cd99b072e8b32075da6233cbf14ea58a5c82de4fRichard Smithtypedef SmallVector<CCValue, 8> ArgVector; 1990180f47959a066795cc0f409433023af448bb0328Richard Smith} 1991180f47959a066795cc0f409433023af448bb0328Richard Smith 1992180f47959a066795cc0f409433023af448bb0328Richard Smith/// EvaluateArgs - Evaluate the arguments to a function call. 1993180f47959a066795cc0f409433023af448bb0328Richard Smithstatic bool EvaluateArgs(ArrayRef<const Expr*> Args, ArgVector &ArgValues, 1994180f47959a066795cc0f409433023af448bb0328Richard Smith EvalInfo &Info) { 1995745f5147e065900267c85a5568785a1991d4838fRichard Smith bool Success = true; 1996180f47959a066795cc0f409433023af448bb0328Richard Smith for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end(); 1997745f5147e065900267c85a5568785a1991d4838fRichard Smith I != E; ++I) { 1998745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!Evaluate(ArgValues[I - Args.begin()], Info, *I)) { 1999745f5147e065900267c85a5568785a1991d4838fRichard Smith // If we're checking for a potential constant expression, evaluate all 2000745f5147e065900267c85a5568785a1991d4838fRichard Smith // initializers even if some of them fail. 2001745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!Info.keepEvaluatingAfterFailure()) 2002745f5147e065900267c85a5568785a1991d4838fRichard Smith return false; 2003745f5147e065900267c85a5568785a1991d4838fRichard Smith Success = false; 2004745f5147e065900267c85a5568785a1991d4838fRichard Smith } 2005745f5147e065900267c85a5568785a1991d4838fRichard Smith } 2006745f5147e065900267c85a5568785a1991d4838fRichard Smith return Success; 2007180f47959a066795cc0f409433023af448bb0328Richard Smith} 2008180f47959a066795cc0f409433023af448bb0328Richard Smith 2009d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith/// Evaluate a function call. 2010745f5147e065900267c85a5568785a1991d4838fRichard Smithstatic bool HandleFunctionCall(SourceLocation CallLoc, 2011745f5147e065900267c85a5568785a1991d4838fRichard Smith const FunctionDecl *Callee, const LValue *This, 2012f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith ArrayRef<const Expr*> Args, const Stmt *Body, 2013c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith EvalInfo &Info, APValue &Result) { 2014180f47959a066795cc0f409433023af448bb0328Richard Smith ArgVector ArgValues(Args.size()); 2015180f47959a066795cc0f409433023af448bb0328Richard Smith if (!EvaluateArgs(Args, ArgValues, Info)) 2016180f47959a066795cc0f409433023af448bb0328Richard Smith return false; 2017d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 2018745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!Info.CheckCallLimit(CallLoc)) 2019745f5147e065900267c85a5568785a1991d4838fRichard Smith return false; 2020745f5147e065900267c85a5568785a1991d4838fRichard Smith 2021745f5147e065900267c85a5568785a1991d4838fRichard Smith CallStackFrame Frame(Info, CallLoc, Callee, This, ArgValues.data()); 2022d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith return EvaluateStmt(Result, Info, Body) == ESR_Returned; 2023d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith} 2024d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 2025180f47959a066795cc0f409433023af448bb0328Richard Smith/// Evaluate a constructor call. 2026745f5147e065900267c85a5568785a1991d4838fRichard Smithstatic bool HandleConstructorCall(SourceLocation CallLoc, const LValue &This, 202759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith ArrayRef<const Expr*> Args, 2028180f47959a066795cc0f409433023af448bb0328Richard Smith const CXXConstructorDecl *Definition, 202951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith EvalInfo &Info, APValue &Result) { 2030180f47959a066795cc0f409433023af448bb0328Richard Smith ArgVector ArgValues(Args.size()); 2031180f47959a066795cc0f409433023af448bb0328Richard Smith if (!EvaluateArgs(Args, ArgValues, Info)) 2032180f47959a066795cc0f409433023af448bb0328Richard Smith return false; 2033180f47959a066795cc0f409433023af448bb0328Richard Smith 2034745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!Info.CheckCallLimit(CallLoc)) 2035745f5147e065900267c85a5568785a1991d4838fRichard Smith return false; 2036745f5147e065900267c85a5568785a1991d4838fRichard Smith 203786c3ae46250cdcc57778c27826060779a92f3815Richard Smith const CXXRecordDecl *RD = Definition->getParent(); 203886c3ae46250cdcc57778c27826060779a92f3815Richard Smith if (RD->getNumVBases()) { 203986c3ae46250cdcc57778c27826060779a92f3815Richard Smith Info.Diag(CallLoc, diag::note_constexpr_virtual_base) << RD; 204086c3ae46250cdcc57778c27826060779a92f3815Richard Smith return false; 204186c3ae46250cdcc57778c27826060779a92f3815Richard Smith } 204286c3ae46250cdcc57778c27826060779a92f3815Richard Smith 2043745f5147e065900267c85a5568785a1991d4838fRichard Smith CallStackFrame Frame(Info, CallLoc, Definition, &This, ArgValues.data()); 2044180f47959a066795cc0f409433023af448bb0328Richard Smith 2045180f47959a066795cc0f409433023af448bb0328Richard Smith // If it's a delegating constructor, just delegate. 2046180f47959a066795cc0f409433023af448bb0328Richard Smith if (Definition->isDelegatingConstructor()) { 2047180f47959a066795cc0f409433023af448bb0328Richard Smith CXXConstructorDecl::init_const_iterator I = Definition->init_begin(); 2048180f47959a066795cc0f409433023af448bb0328Richard Smith return EvaluateConstantExpression(Result, Info, This, (*I)->getInit()); 2049180f47959a066795cc0f409433023af448bb0328Richard Smith } 2050180f47959a066795cc0f409433023af448bb0328Richard Smith 2051610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith // For a trivial copy or move constructor, perform an APValue copy. This is 2052610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith // essential for unions, where the operations performed by the constructor 2053610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith // cannot be represented by ctor-initializers. 2054610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith if (Definition->isDefaulted() && 2055610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith ((Definition->isCopyConstructor() && RD->hasTrivialCopyConstructor()) || 2056610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith (Definition->isMoveConstructor() && RD->hasTrivialMoveConstructor()))) { 2057610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith LValue RHS; 2058610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith RHS.setFrom(ArgValues[0]); 2059610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith CCValue Value; 2060745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!HandleLValueToRValueConversion(Info, Args[0], Args[0]->getType(), 2061745f5147e065900267c85a5568785a1991d4838fRichard Smith RHS, Value)) 2062745f5147e065900267c85a5568785a1991d4838fRichard Smith return false; 2063745f5147e065900267c85a5568785a1991d4838fRichard Smith assert((Value.isStruct() || Value.isUnion()) && 2064745f5147e065900267c85a5568785a1991d4838fRichard Smith "trivial copy/move from non-class type?"); 2065745f5147e065900267c85a5568785a1991d4838fRichard Smith // Any CCValue of class type must already be a constant expression. 2066745f5147e065900267c85a5568785a1991d4838fRichard Smith Result = Value; 2067745f5147e065900267c85a5568785a1991d4838fRichard Smith return true; 2068610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith } 2069610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith 2070610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith // Reserve space for the struct members. 207151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (!RD->isUnion() && Result.isUninit()) 2072180f47959a066795cc0f409433023af448bb0328Richard Smith Result = APValue(APValue::UninitStruct(), RD->getNumBases(), 2073180f47959a066795cc0f409433023af448bb0328Richard Smith std::distance(RD->field_begin(), RD->field_end())); 2074180f47959a066795cc0f409433023af448bb0328Richard Smith 2075180f47959a066795cc0f409433023af448bb0328Richard Smith const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); 2076180f47959a066795cc0f409433023af448bb0328Richard Smith 2077745f5147e065900267c85a5568785a1991d4838fRichard Smith bool Success = true; 2078180f47959a066795cc0f409433023af448bb0328Richard Smith unsigned BasesSeen = 0; 2079180f47959a066795cc0f409433023af448bb0328Richard Smith#ifndef NDEBUG 2080180f47959a066795cc0f409433023af448bb0328Richard Smith CXXRecordDecl::base_class_const_iterator BaseIt = RD->bases_begin(); 2081180f47959a066795cc0f409433023af448bb0328Richard Smith#endif 2082180f47959a066795cc0f409433023af448bb0328Richard Smith for (CXXConstructorDecl::init_const_iterator I = Definition->init_begin(), 2083180f47959a066795cc0f409433023af448bb0328Richard Smith E = Definition->init_end(); I != E; ++I) { 2084745f5147e065900267c85a5568785a1991d4838fRichard Smith LValue Subobject = This; 2085745f5147e065900267c85a5568785a1991d4838fRichard Smith APValue *Value = &Result; 2086745f5147e065900267c85a5568785a1991d4838fRichard Smith 2087745f5147e065900267c85a5568785a1991d4838fRichard Smith // Determine the subobject to initialize. 2088180f47959a066795cc0f409433023af448bb0328Richard Smith if ((*I)->isBaseInitializer()) { 2089180f47959a066795cc0f409433023af448bb0328Richard Smith QualType BaseType((*I)->getBaseClass(), 0); 2090180f47959a066795cc0f409433023af448bb0328Richard Smith#ifndef NDEBUG 2091180f47959a066795cc0f409433023af448bb0328Richard Smith // Non-virtual base classes are initialized in the order in the class 209286c3ae46250cdcc57778c27826060779a92f3815Richard Smith // definition. We have already checked for virtual base classes. 2093180f47959a066795cc0f409433023af448bb0328Richard Smith assert(!BaseIt->isVirtual() && "virtual base for literal type"); 2094180f47959a066795cc0f409433023af448bb0328Richard Smith assert(Info.Ctx.hasSameType(BaseIt->getType(), BaseType) && 2095180f47959a066795cc0f409433023af448bb0328Richard Smith "base class initializers not in expected order"); 2096180f47959a066795cc0f409433023af448bb0328Richard Smith ++BaseIt; 2097180f47959a066795cc0f409433023af448bb0328Richard Smith#endif 2098b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith HandleLValueDirectBase(Info, (*I)->getInit(), Subobject, RD, 2099180f47959a066795cc0f409433023af448bb0328Richard Smith BaseType->getAsCXXRecordDecl(), &Layout); 2100745f5147e065900267c85a5568785a1991d4838fRichard Smith Value = &Result.getStructBase(BasesSeen++); 2101180f47959a066795cc0f409433023af448bb0328Richard Smith } else if (FieldDecl *FD = (*I)->getMember()) { 2102b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith HandleLValueMember(Info, (*I)->getInit(), Subobject, FD, &Layout); 2103180f47959a066795cc0f409433023af448bb0328Richard Smith if (RD->isUnion()) { 2104180f47959a066795cc0f409433023af448bb0328Richard Smith Result = APValue(FD); 2105745f5147e065900267c85a5568785a1991d4838fRichard Smith Value = &Result.getUnionValue(); 2106745f5147e065900267c85a5568785a1991d4838fRichard Smith } else { 2107745f5147e065900267c85a5568785a1991d4838fRichard Smith Value = &Result.getStructField(FD->getFieldIndex()); 2108745f5147e065900267c85a5568785a1991d4838fRichard Smith } 2109d9b02e726262e4009dda830998bb934172ac0020Richard Smith } else if (IndirectFieldDecl *IFD = (*I)->getIndirectMember()) { 2110d9b02e726262e4009dda830998bb934172ac0020Richard Smith // Walk the indirect field decl's chain to find the object to initialize, 2111d9b02e726262e4009dda830998bb934172ac0020Richard Smith // and make sure we've initialized every step along it. 2112d9b02e726262e4009dda830998bb934172ac0020Richard Smith for (IndirectFieldDecl::chain_iterator C = IFD->chain_begin(), 2113d9b02e726262e4009dda830998bb934172ac0020Richard Smith CE = IFD->chain_end(); 2114d9b02e726262e4009dda830998bb934172ac0020Richard Smith C != CE; ++C) { 2115d9b02e726262e4009dda830998bb934172ac0020Richard Smith FieldDecl *FD = cast<FieldDecl>(*C); 2116d9b02e726262e4009dda830998bb934172ac0020Richard Smith CXXRecordDecl *CD = cast<CXXRecordDecl>(FD->getParent()); 2117d9b02e726262e4009dda830998bb934172ac0020Richard Smith // Switch the union field if it differs. This happens if we had 2118d9b02e726262e4009dda830998bb934172ac0020Richard Smith // preceding zero-initialization, and we're now initializing a union 2119d9b02e726262e4009dda830998bb934172ac0020Richard Smith // subobject other than the first. 2120d9b02e726262e4009dda830998bb934172ac0020Richard Smith // FIXME: In this case, the values of the other subobjects are 2121d9b02e726262e4009dda830998bb934172ac0020Richard Smith // specified, since zero-initialization sets all padding bits to zero. 2122d9b02e726262e4009dda830998bb934172ac0020Richard Smith if (Value->isUninit() || 2123d9b02e726262e4009dda830998bb934172ac0020Richard Smith (Value->isUnion() && Value->getUnionField() != FD)) { 2124d9b02e726262e4009dda830998bb934172ac0020Richard Smith if (CD->isUnion()) 2125d9b02e726262e4009dda830998bb934172ac0020Richard Smith *Value = APValue(FD); 2126d9b02e726262e4009dda830998bb934172ac0020Richard Smith else 2127d9b02e726262e4009dda830998bb934172ac0020Richard Smith *Value = APValue(APValue::UninitStruct(), CD->getNumBases(), 2128d9b02e726262e4009dda830998bb934172ac0020Richard Smith std::distance(CD->field_begin(), CD->field_end())); 2129d9b02e726262e4009dda830998bb934172ac0020Richard Smith } 2130745f5147e065900267c85a5568785a1991d4838fRichard Smith HandleLValueMember(Info, (*I)->getInit(), Subobject, FD); 2131d9b02e726262e4009dda830998bb934172ac0020Richard Smith if (CD->isUnion()) 2132d9b02e726262e4009dda830998bb934172ac0020Richard Smith Value = &Value->getUnionValue(); 2133d9b02e726262e4009dda830998bb934172ac0020Richard Smith else 2134d9b02e726262e4009dda830998bb934172ac0020Richard Smith Value = &Value->getStructField(FD->getFieldIndex()); 2135d9b02e726262e4009dda830998bb934172ac0020Richard Smith } 2136180f47959a066795cc0f409433023af448bb0328Richard Smith } else { 2137d9b02e726262e4009dda830998bb934172ac0020Richard Smith llvm_unreachable("unknown base initializer kind"); 2138180f47959a066795cc0f409433023af448bb0328Richard Smith } 2139745f5147e065900267c85a5568785a1991d4838fRichard Smith 2140745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!EvaluateConstantExpression(*Value, Info, Subobject, (*I)->getInit(), 2141745f5147e065900267c85a5568785a1991d4838fRichard Smith (*I)->isBaseInitializer() 2142745f5147e065900267c85a5568785a1991d4838fRichard Smith ? CCEK_Constant : CCEK_MemberInit)) { 2143745f5147e065900267c85a5568785a1991d4838fRichard Smith // If we're checking for a potential constant expression, evaluate all 2144745f5147e065900267c85a5568785a1991d4838fRichard Smith // initializers even if some of them fail. 2145745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!Info.keepEvaluatingAfterFailure()) 2146745f5147e065900267c85a5568785a1991d4838fRichard Smith return false; 2147745f5147e065900267c85a5568785a1991d4838fRichard Smith Success = false; 2148745f5147e065900267c85a5568785a1991d4838fRichard Smith } 2149180f47959a066795cc0f409433023af448bb0328Richard Smith } 2150180f47959a066795cc0f409433023af448bb0328Richard Smith 2151745f5147e065900267c85a5568785a1991d4838fRichard Smith return Success; 2152180f47959a066795cc0f409433023af448bb0328Richard Smith} 2153180f47959a066795cc0f409433023af448bb0328Richard Smith 2154d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smithnamespace { 2155770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass HasSideEffect 21568cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne : public ConstStmtVisitor<HasSideEffect, bool> { 21571e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith const ASTContext &Ctx; 2158c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stumppublic: 2159c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump 21601e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith HasSideEffect(const ASTContext &C) : Ctx(C) {} 2161c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump 2162c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump // Unhandled nodes conservatively default to having side effects. 21638cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitStmt(const Stmt *S) { 2164c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump return true; 2165c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump } 2166c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump 21678cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitParenExpr(const ParenExpr *E) { return Visit(E->getSubExpr()); } 21688cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitGenericSelectionExpr(const GenericSelectionExpr *E) { 2169f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne return Visit(E->getResultExpr()); 2170f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne } 21718cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitDeclRefExpr(const DeclRefExpr *E) { 21721e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith if (Ctx.getCanonicalType(E->getType()).isVolatileQualified()) 2173c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump return true; 2174c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump return false; 2175c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump } 2176f85e193739c953358c865005855253af4f68a497John McCall bool VisitObjCIvarRefExpr(const ObjCIvarRefExpr *E) { 21771e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith if (Ctx.getCanonicalType(E->getType()).isVolatileQualified()) 2178f85e193739c953358c865005855253af4f68a497John McCall return true; 2179f85e193739c953358c865005855253af4f68a497John McCall return false; 2180f85e193739c953358c865005855253af4f68a497John McCall } 2181f85e193739c953358c865005855253af4f68a497John McCall bool VisitBlockDeclRefExpr (const BlockDeclRefExpr *E) { 21821e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith if (Ctx.getCanonicalType(E->getType()).isVolatileQualified()) 2183f85e193739c953358c865005855253af4f68a497John McCall return true; 2184f85e193739c953358c865005855253af4f68a497John McCall return false; 2185f85e193739c953358c865005855253af4f68a497John McCall } 2186f85e193739c953358c865005855253af4f68a497John McCall 2187c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump // We don't want to evaluate BlockExprs multiple times, as they generate 2188c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump // a ton of code. 21898cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitBlockExpr(const BlockExpr *E) { return true; } 21908cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitPredefinedExpr(const PredefinedExpr *E) { return false; } 21918cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) 2192c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump { return Visit(E->getInitializer()); } 21938cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitMemberExpr(const MemberExpr *E) { return Visit(E->getBase()); } 21948cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitIntegerLiteral(const IntegerLiteral *E) { return false; } 21958cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitFloatingLiteral(const FloatingLiteral *E) { return false; } 21968cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitStringLiteral(const StringLiteral *E) { return false; } 21978cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitCharacterLiteral(const CharacterLiteral *E) { return false; } 21988cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E) 2199f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne { return false; } 22008cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E) 2201980ca220848d27ef55ef4c2d37423461a8ed0da3Mike Stump { return Visit(E->getLHS()) || Visit(E->getRHS()); } 22028cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitChooseExpr(const ChooseExpr *E) 22031e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith { return Visit(E->getChosenSubExpr(Ctx)); } 22048cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitCastExpr(const CastExpr *E) { return Visit(E->getSubExpr()); } 22058cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitBinAssign(const BinaryOperator *E) { return true; } 22068cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitCompoundAssignOperator(const BinaryOperator *E) { return true; } 22078cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitBinaryOperator(const BinaryOperator *E) 2208980ca220848d27ef55ef4c2d37423461a8ed0da3Mike Stump { return Visit(E->getLHS()) || Visit(E->getRHS()); } 22098cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitUnaryPreInc(const UnaryOperator *E) { return true; } 22108cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitUnaryPostInc(const UnaryOperator *E) { return true; } 22118cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitUnaryPreDec(const UnaryOperator *E) { return true; } 22128cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitUnaryPostDec(const UnaryOperator *E) { return true; } 22138cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitUnaryDeref(const UnaryOperator *E) { 22141e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith if (Ctx.getCanonicalType(E->getType()).isVolatileQualified()) 2215c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump return true; 2216980ca220848d27ef55ef4c2d37423461a8ed0da3Mike Stump return Visit(E->getSubExpr()); 2217c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump } 22188cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitUnaryOperator(const UnaryOperator *E) { return Visit(E->getSubExpr()); } 2219363ff23cfddb51abe4ee4212a6dd3c9b534fcc8bChris Lattner 2220363ff23cfddb51abe4ee4212a6dd3c9b534fcc8bChris Lattner // Has side effects if any element does. 22218cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitInitListExpr(const InitListExpr *E) { 2222363ff23cfddb51abe4ee4212a6dd3c9b534fcc8bChris Lattner for (unsigned i = 0, e = E->getNumInits(); i != e; ++i) 2223363ff23cfddb51abe4ee4212a6dd3c9b534fcc8bChris Lattner if (Visit(E->getInit(i))) return true; 22248cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne if (const Expr *filler = E->getArrayFiller()) 22254423ac0282acb8ba801eb05b38712438dc0c1e3eArgyrios Kyrtzidis return Visit(filler); 2226363ff23cfddb51abe4ee4212a6dd3c9b534fcc8bChris Lattner return false; 2227363ff23cfddb51abe4ee4212a6dd3c9b534fcc8bChris Lattner } 2228ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor 22298cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitSizeOfPackExpr(const SizeOfPackExpr *) { return false; } 2230c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump}; 2231c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump 223256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCallclass OpaqueValueEvaluation { 223356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall EvalInfo &info; 223456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall OpaqueValueExpr *opaqueValue; 223556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall 223656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCallpublic: 223756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall OpaqueValueEvaluation(EvalInfo &info, OpaqueValueExpr *opaqueValue, 223856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall Expr *value) 223956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall : info(info), opaqueValue(opaqueValue) { 224056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall 224156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall // If evaluation fails, fail immediately. 22421e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith if (!Evaluate(info.OpaqueValues[opaqueValue], info, value)) { 224356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall this->opaqueValue = 0; 224456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall return; 224556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall } 224656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall } 224756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall 224856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall bool hasError() const { return opaqueValue == 0; } 224956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall 225056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall ~OpaqueValueEvaluation() { 22511e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith // FIXME: This will not work for recursive constexpr functions using opaque 22521e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith // values. Restore the former value. 225356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall if (opaqueValue) info.OpaqueValues.erase(opaqueValue); 225456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall } 225556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall}; 225656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall 2257c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump} // end anonymous namespace 2258c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump 22594efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman//===----------------------------------------------------------------------===// 22608cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne// Generic Evaluation 22618cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne//===----------------------------------------------------------------------===// 22628cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournenamespace { 22638cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 2264f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith// FIXME: RetTy is always bool. Remove it. 2265f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithtemplate <class Derived, typename RetTy=bool> 22668cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourneclass ExprEvaluatorBase 22678cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne : public ConstStmtVisitor<Derived, RetTy> { 22688cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourneprivate: 226947a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith RetTy DerivedSuccess(const CCValue &V, const Expr *E) { 22708cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return static_cast<Derived*>(this)->Success(V, E); 22718cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne } 227251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith RetTy DerivedZeroInitialization(const Expr *E) { 227351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return static_cast<Derived*>(this)->ZeroInitialization(E); 2274f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith } 22758cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 22768cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourneprotected: 22778cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne EvalInfo &Info; 22788cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne typedef ConstStmtVisitor<Derived, RetTy> StmtVisitorTy; 22798cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne typedef ExprEvaluatorBase ExprEvaluatorBaseTy; 22808cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 2281dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { 2282d5093420308784e31071c8cd723a58d74159f393Richard Smith return Info.CCEDiag(E->getExprLoc(), D); 2283f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 2284f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 2285f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith /// Report an evaluation error. This should only be called when an error is 2286f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith /// first discovered. When propagating an error, just return false. 2287f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith bool Error(const Expr *E, diag::kind D) { 2288dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith Info.Diag(E->getExprLoc(), D); 2289f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 2290f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 2291f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith bool Error(const Expr *E) { 2292f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E, diag::note_invalid_subexpr_in_const_expr); 2293f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 2294f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 229551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith RetTy ZeroInitialization(const Expr *E) { return Error(E); } 2296f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith 22978cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournepublic: 22988cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {} 22998cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 23008cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne RetTy VisitStmt(const Stmt *) { 2301b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie llvm_unreachable("Expression evaluator should not be called on stmts"); 23028cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne } 23038cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne RetTy VisitExpr(const Expr *E) { 2304f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 23058cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne } 23068cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 23078cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne RetTy VisitParenExpr(const ParenExpr *E) 23088cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne { return StmtVisitorTy::Visit(E->getSubExpr()); } 23098cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne RetTy VisitUnaryExtension(const UnaryOperator *E) 23108cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne { return StmtVisitorTy::Visit(E->getSubExpr()); } 23118cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne RetTy VisitUnaryPlus(const UnaryOperator *E) 23128cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne { return StmtVisitorTy::Visit(E->getSubExpr()); } 23138cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne RetTy VisitChooseExpr(const ChooseExpr *E) 23148cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne { return StmtVisitorTy::Visit(E->getChosenSubExpr(Info.Ctx)); } 23158cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne RetTy VisitGenericSelectionExpr(const GenericSelectionExpr *E) 23168cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne { return StmtVisitorTy::Visit(E->getResultExpr()); } 231791a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall RetTy VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E) 231891a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall { return StmtVisitorTy::Visit(E->getReplacement()); } 23193d75ca836205856077c18e30e9447accbd85f751Richard Smith RetTy VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) 23203d75ca836205856077c18e30e9447accbd85f751Richard Smith { return StmtVisitorTy::Visit(E->getExpr()); } 2321bc6abe93a5d6b1305411f8b6f54c2caa686ddc69Richard Smith // We cannot create any objects for which cleanups are required, so there is 2322bc6abe93a5d6b1305411f8b6f54c2caa686ddc69Richard Smith // nothing to do here; all cleanups must come from unevaluated subexpressions. 2323bc6abe93a5d6b1305411f8b6f54c2caa686ddc69Richard Smith RetTy VisitExprWithCleanups(const ExprWithCleanups *E) 2324bc6abe93a5d6b1305411f8b6f54c2caa686ddc69Richard Smith { return StmtVisitorTy::Visit(E->getSubExpr()); } 23258cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 2326c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith RetTy VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E) { 2327c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith CCEDiag(E, diag::note_constexpr_invalid_cast) << 0; 2328c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith return static_cast<Derived*>(this)->VisitCastExpr(E); 2329c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith } 2330c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith RetTy VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) { 2331c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith CCEDiag(E, diag::note_constexpr_invalid_cast) << 1; 2332c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith return static_cast<Derived*>(this)->VisitCastExpr(E); 2333c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith } 2334c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith 2335e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith RetTy VisitBinaryOperator(const BinaryOperator *E) { 2336e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith switch (E->getOpcode()) { 2337e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith default: 2338f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 2339e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2340e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case BO_Comma: 2341e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith VisitIgnoredValue(E->getLHS()); 2342e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return StmtVisitorTy::Visit(E->getRHS()); 2343e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2344e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case BO_PtrMemD: 2345e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case BO_PtrMemI: { 2346e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LValue Obj; 2347e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!HandleMemberPointerAccess(Info, E, Obj)) 2348e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 2349e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith CCValue Result; 2350f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!HandleLValueToRValueConversion(Info, E, E->getType(), Obj, Result)) 2351e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 2352e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return DerivedSuccess(Result, E); 2353e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2354e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2355e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2356e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 23578cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne RetTy VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) { 23588cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne OpaqueValueEvaluation opaque(Info, E->getOpaqueValue(), E->getCommon()); 23598cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne if (opaque.hasError()) 2360f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 23618cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 23628cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool cond; 2363c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith if (!EvaluateAsBooleanCondition(E->getCond(), cond, Info)) 2364f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 23658cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 23668cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return StmtVisitorTy::Visit(cond ? E->getTrueExpr() : E->getFalseExpr()); 23678cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne } 23688cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 23698cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne RetTy VisitConditionalOperator(const ConditionalOperator *E) { 2370f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith bool IsBcpCall = false; 2371f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // If the condition (ignoring parens) is a __builtin_constant_p call, 2372f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // the result is a constant expression if it can be folded without 2373f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // side-effects. This is an important GNU extension. See GCC PR38377 2374f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // for discussion. 2375f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (const CallExpr *CallCE = 2376f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith dyn_cast<CallExpr>(E->getCond()->IgnoreParenCasts())) 2377f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p) 2378f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith IsBcpCall = true; 2379f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 2380f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // Always assume __builtin_constant_p(...) ? ... : ... is a potential 2381f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // constant expression; we can't check whether it's potentially foldable. 2382f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (Info.CheckingPotentialConstantExpression && IsBcpCall) 2383f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith return false; 2384f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 2385f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith FoldConstant Fold(Info); 2386f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 23878cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool BoolResult; 2388c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info)) 2389f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 23908cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 2391c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith Expr *EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); 2392f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (!StmtVisitorTy::Visit(EvalExpr)) 2393f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith return false; 2394f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 2395f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (IsBcpCall) 2396f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith Fold.Fold(Info); 2397f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 2398f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith return true; 23998cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne } 24008cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 24018cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne RetTy VisitOpaqueValueExpr(const OpaqueValueExpr *E) { 240247a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith const CCValue *Value = Info.getOpaqueValue(E); 240342786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis if (!Value) { 240442786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis const Expr *Source = E->getSourceExpr(); 240542786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis if (!Source) 2406f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 240742786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis if (Source == E) { // sanity checking. 240842786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis assert(0 && "OpaqueValueExpr recursively refers to itself"); 2409f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 241042786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis } 241142786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis return StmtVisitorTy::Visit(Source); 241242786839cff1ccbe4d883b81d01846c5d774ffc6Argyrios Kyrtzidis } 241347a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith return DerivedSuccess(*Value, E); 24148cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne } 2415f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith 2416d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith RetTy VisitCallExpr(const CallExpr *E) { 2417e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const Expr *Callee = E->getCallee()->IgnoreParens(); 2418d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith QualType CalleeType = Callee->getType(); 2419d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 24206142ca7790aa09a6e13592b70f142cc4bbcadcaeDevang Patel const FunctionDecl *FD = 0; 242159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith LValue *This = 0, ThisVal; 242259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs()); 242386c3ae46250cdcc57778c27826060779a92f3815Richard Smith bool HasQualifier = false; 242459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith 242559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith // Extract function decl and 'this' pointer from the callee. 242659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith if (CalleeType->isSpecificBuiltinType(BuiltinType::BoundMember)) { 2427f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith const ValueDecl *Member = 0; 2428e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (const MemberExpr *ME = dyn_cast<MemberExpr>(Callee)) { 2429e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // Explicit bound member calls, such as x.f() or p->g(); 2430e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!EvaluateObjectArgument(Info, ME->getBase(), ThisVal)) 2431f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 2432f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith Member = ME->getMemberDecl(); 2433e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith This = &ThisVal; 243486c3ae46250cdcc57778c27826060779a92f3815Richard Smith HasQualifier = ME->hasQualifier(); 2435e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(Callee)) { 2436e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // Indirect bound member calls ('.*' or '->*'). 2437f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith Member = HandleMemberPointerAccess(Info, BE, ThisVal, false); 2438f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!Member) return false; 2439e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith This = &ThisVal; 2440e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } else 2441f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(Callee); 2442f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 2443f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith FD = dyn_cast<FunctionDecl>(Member); 2444f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!FD) 2445f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(Callee); 244659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith } else if (CalleeType->isFunctionPointerType()) { 2447b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith LValue Call; 2448b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (!EvaluatePointer(Callee, Call, Info)) 2449f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 245059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith 2451b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (!Call.getLValueOffset().isZero()) 2452f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(Callee); 24531bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith FD = dyn_cast_or_null<FunctionDecl>( 24541bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith Call.getLValueBase().dyn_cast<const ValueDecl*>()); 245559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith if (!FD) 2456f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(Callee); 245759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith 245859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith // Overloaded operator calls to member functions are represented as normal 245959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith // calls with '*this' as the first argument. 246059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); 246159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith if (MD && !MD->isStatic()) { 2462f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith // FIXME: When selecting an implicit conversion for an overloaded 2463f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith // operator delete, we sometimes try to evaluate calls to conversion 2464f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith // operators without a 'this' parameter! 2465f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (Args.empty()) 2466f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 2467f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 246859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith if (!EvaluateObjectArgument(Info, Args[0], ThisVal)) 246959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith return false; 247059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith This = &ThisVal; 247159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith Args = Args.slice(1); 247259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith } 2473d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 247459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith // Don't call function pointers which have been cast to some other type. 247559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith if (!Info.Ctx.hasSameType(CalleeType->getPointeeType(), FD->getType())) 2476f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 247759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith } else 2478f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 2479d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 2480b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith if (This && !This->checkSubobject(Info, E, CSK_This)) 2481b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith return false; 2482b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith 248386c3ae46250cdcc57778c27826060779a92f3815Richard Smith // DR1358 allows virtual constexpr functions in some cases. Don't allow 248486c3ae46250cdcc57778c27826060779a92f3815Richard Smith // calls to such functions in constant expressions. 248586c3ae46250cdcc57778c27826060779a92f3815Richard Smith if (This && !HasQualifier && 248686c3ae46250cdcc57778c27826060779a92f3815Richard Smith isa<CXXMethodDecl>(FD) && cast<CXXMethodDecl>(FD)->isVirtual()) 248786c3ae46250cdcc57778c27826060779a92f3815Richard Smith return Error(E, diag::note_constexpr_virtual_call); 248886c3ae46250cdcc57778c27826060779a92f3815Richard Smith 2489c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const FunctionDecl *Definition = 0; 2490d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith Stmt *Body = FD->getBody(Definition); 249169c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith APValue Result; 2492d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 2493c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition) || 2494745f5147e065900267c85a5568785a1991d4838fRichard Smith !HandleFunctionCall(E->getExprLoc(), Definition, This, Args, Body, 2495745f5147e065900267c85a5568785a1991d4838fRichard Smith Info, Result)) 2496f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 2497d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 2498b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return DerivedSuccess(CCValue(Info.Ctx, Result, CCValue::GlobalValue()), E); 2499d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith } 2500d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 2501c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith RetTy VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { 2502c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return StmtVisitorTy::Visit(E->getInitializer()); 2503c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith } 2504f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith RetTy VisitInitListExpr(const InitListExpr *E) { 250571523d6c41e1599fc42f420d02dd2895fd8f65d4Eli Friedman if (E->getNumInits() == 0) 250671523d6c41e1599fc42f420d02dd2895fd8f65d4Eli Friedman return DerivedZeroInitialization(E); 250771523d6c41e1599fc42f420d02dd2895fd8f65d4Eli Friedman if (E->getNumInits() == 1) 250871523d6c41e1599fc42f420d02dd2895fd8f65d4Eli Friedman return StmtVisitorTy::Visit(E->getInit(0)); 2509f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 2510f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith } 2511f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith RetTy VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { 251251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return DerivedZeroInitialization(E); 2513f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith } 2514f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith RetTy VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) { 251551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return DerivedZeroInitialization(E); 2516f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith } 2517e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith RetTy VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) { 251851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return DerivedZeroInitialization(E); 2519e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2520f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith 2521180f47959a066795cc0f409433023af448bb0328Richard Smith /// A member expression where the object is a prvalue is itself a prvalue. 2522180f47959a066795cc0f409433023af448bb0328Richard Smith RetTy VisitMemberExpr(const MemberExpr *E) { 2523180f47959a066795cc0f409433023af448bb0328Richard Smith assert(!E->isArrow() && "missing call to bound member function?"); 2524180f47959a066795cc0f409433023af448bb0328Richard Smith 2525180f47959a066795cc0f409433023af448bb0328Richard Smith CCValue Val; 2526180f47959a066795cc0f409433023af448bb0328Richard Smith if (!Evaluate(Val, Info, E->getBase())) 2527180f47959a066795cc0f409433023af448bb0328Richard Smith return false; 2528180f47959a066795cc0f409433023af448bb0328Richard Smith 2529180f47959a066795cc0f409433023af448bb0328Richard Smith QualType BaseTy = E->getBase()->getType(); 2530180f47959a066795cc0f409433023af448bb0328Richard Smith 2531180f47959a066795cc0f409433023af448bb0328Richard Smith const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); 2532f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!FD) return Error(E); 2533180f47959a066795cc0f409433023af448bb0328Richard Smith assert(!FD->getType()->isReferenceType() && "prvalue reference?"); 2534180f47959a066795cc0f409433023af448bb0328Richard Smith assert(BaseTy->getAs<RecordType>()->getDecl()->getCanonicalDecl() == 2535180f47959a066795cc0f409433023af448bb0328Richard Smith FD->getParent()->getCanonicalDecl() && "record / field mismatch"); 2536180f47959a066795cc0f409433023af448bb0328Richard Smith 2537b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith SubobjectDesignator Designator(BaseTy); 2538b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Designator.addDeclUnchecked(FD); 2539180f47959a066795cc0f409433023af448bb0328Richard Smith 2540f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return ExtractSubobject(Info, E, Val, BaseTy, Designator, E->getType()) && 2541180f47959a066795cc0f409433023af448bb0328Richard Smith DerivedSuccess(Val, E); 2542180f47959a066795cc0f409433023af448bb0328Richard Smith } 2543180f47959a066795cc0f409433023af448bb0328Richard Smith 2544c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith RetTy VisitCastExpr(const CastExpr *E) { 2545c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith switch (E->getCastKind()) { 2546c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith default: 2547c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith break; 2548c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 25497a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall case CK_AtomicToNonAtomic: 25507a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall case CK_NonAtomicToAtomic: 2551c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case CK_NoOp: 25527d580a4e9e47dffc3c17aa2b957ac57ca3c4e451Richard Smith case CK_UserDefinedConversion: 2553c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return StmtVisitorTy::Visit(E->getSubExpr()); 2554c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 2555c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case CK_LValueToRValue: { 2556c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith LValue LVal; 2557f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!EvaluateLValue(E->getSubExpr(), LVal, Info)) 2558f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 2559f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith CCValue RVal; 25609ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith // Note, we use the subexpression's type in order to retain cv-qualifiers. 25619ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith if (!HandleLValueToRValueConversion(Info, E, E->getSubExpr()->getType(), 25629ec7197796a2730d54ae7f632553b5311b2ba3b5Richard Smith LVal, RVal)) 2563f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 2564f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return DerivedSuccess(RVal, E); 2565c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith } 2566c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith } 2567c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 2568f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 2569c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith } 2570c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 25718327fad71da34492d82c532f42a58cb4baff81a3Richard Smith /// Visit a value which is evaluated, but whose value is ignored. 25728327fad71da34492d82c532f42a58cb4baff81a3Richard Smith void VisitIgnoredValue(const Expr *E) { 257347a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith CCValue Scratch; 25748327fad71da34492d82c532f42a58cb4baff81a3Richard Smith if (!Evaluate(Scratch, Info, E)) 25758327fad71da34492d82c532f42a58cb4baff81a3Richard Smith Info.EvalStatus.HasSideEffects = true; 25768327fad71da34492d82c532f42a58cb4baff81a3Richard Smith } 25778cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne}; 25788cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 25798cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne} 25808cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 25818cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne//===----------------------------------------------------------------------===// 2582e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// Common base class for lvalue and temporary evaluation. 2583e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===// 2584e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithnamespace { 2585e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithtemplate<class Derived> 2586e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithclass LValueExprEvaluatorBase 2587e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith : public ExprEvaluatorBase<Derived, bool> { 2588e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithprotected: 2589e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LValue &Result; 2590e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith typedef LValueExprEvaluatorBase LValueExprEvaluatorBaseTy; 2591e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith typedef ExprEvaluatorBase<Derived, bool> ExprEvaluatorBaseTy; 2592e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2593e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool Success(APValue::LValueBase B) { 2594e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Result.set(B); 2595e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 2596e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2597e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2598e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithpublic: 2599e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LValueExprEvaluatorBase(EvalInfo &Info, LValue &Result) : 2600e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith ExprEvaluatorBaseTy(Info), Result(Result) {} 2601e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2602e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool Success(const CCValue &V, const Expr *E) { 2603e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Result.setFrom(V); 2604e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 2605e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2606e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2607e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool VisitMemberExpr(const MemberExpr *E) { 2608e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // Handle non-static data members. 2609e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith QualType BaseTy; 2610e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (E->isArrow()) { 2611e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!EvaluatePointer(E->getBase(), Result, this->Info)) 2612e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 2613e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith BaseTy = E->getBase()->getType()->getAs<PointerType>()->getPointeeType(); 2614c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } else if (E->getBase()->isRValue()) { 2615af2c7a194592401394233b7cbcdd3cfd0a7a38ddRichard Smith assert(E->getBase()->getType()->isRecordType()); 2616c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (!EvaluateTemporary(E->getBase(), Result, this->Info)) 2617c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return false; 2618c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith BaseTy = E->getBase()->getType(); 2619e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } else { 2620e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!this->Visit(E->getBase())) 2621e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 2622e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith BaseTy = E->getBase()->getType(); 2623e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2624e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2625d9b02e726262e4009dda830998bb934172ac0020Richard Smith const ValueDecl *MD = E->getMemberDecl(); 2626d9b02e726262e4009dda830998bb934172ac0020Richard Smith if (const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) { 2627d9b02e726262e4009dda830998bb934172ac0020Richard Smith assert(BaseTy->getAs<RecordType>()->getDecl()->getCanonicalDecl() == 2628d9b02e726262e4009dda830998bb934172ac0020Richard Smith FD->getParent()->getCanonicalDecl() && "record / field mismatch"); 2629d9b02e726262e4009dda830998bb934172ac0020Richard Smith (void)BaseTy; 2630d9b02e726262e4009dda830998bb934172ac0020Richard Smith HandleLValueMember(this->Info, E, Result, FD); 2631d9b02e726262e4009dda830998bb934172ac0020Richard Smith } else if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(MD)) { 2632d9b02e726262e4009dda830998bb934172ac0020Richard Smith HandleLValueIndirectMember(this->Info, E, Result, IFD); 2633d9b02e726262e4009dda830998bb934172ac0020Richard Smith } else 2634d9b02e726262e4009dda830998bb934172ac0020Richard Smith return this->Error(E); 2635e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2636d9b02e726262e4009dda830998bb934172ac0020Richard Smith if (MD->getType()->isReferenceType()) { 2637e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith CCValue RefValue; 2638d9b02e726262e4009dda830998bb934172ac0020Richard Smith if (!HandleLValueToRValueConversion(this->Info, E, MD->getType(), Result, 2639e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith RefValue)) 2640e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 2641e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return Success(RefValue, E); 2642e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2643e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 2644e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2645e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2646e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool VisitBinaryOperator(const BinaryOperator *E) { 2647e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith switch (E->getOpcode()) { 2648e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith default: 2649e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return ExprEvaluatorBaseTy::VisitBinaryOperator(E); 2650e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2651e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case BO_PtrMemD: 2652e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case BO_PtrMemI: 2653e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return HandleMemberPointerAccess(this->Info, E, Result); 2654e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2655e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2656e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2657e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool VisitCastExpr(const CastExpr *E) { 2658e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith switch (E->getCastKind()) { 2659e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith default: 2660e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return ExprEvaluatorBaseTy::VisitCastExpr(E); 2661e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2662e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case CK_DerivedToBase: 2663e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case CK_UncheckedDerivedToBase: { 2664e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!this->Visit(E->getSubExpr())) 2665e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 2666e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2667e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // Now figure out the necessary offset to add to the base LV to get from 2668e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // the derived class to the base class. 2669e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith QualType Type = E->getSubExpr()->getType(); 2670e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2671e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith for (CastExpr::path_const_iterator PathI = E->path_begin(), 2672e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith PathE = E->path_end(); PathI != PathE; ++PathI) { 2673b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (!HandleLValueBase(this->Info, E, Result, Type->getAsCXXRecordDecl(), 2674e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith *PathI)) 2675e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 2676e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Type = (*PathI)->getType(); 2677e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2678e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2679e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 2680e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2681e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2682e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2683e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}; 2684e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith} 2685e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2686e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===// 26874efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman// LValue Evaluation 2688c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// 2689c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// This is used for evaluating lvalues (in C and C++), xvalues (in C++11), 2690c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// function designators (in C), decl references to void objects (in C), and 2691c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// temporaries (if building with -Wno-address-of-temporary). 2692c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// 2693c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// LValue evaluation produces values comprising a base expression of one of the 2694c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// following types: 26951bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith// - Declarations 26961bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith// * VarDecl 26971bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith// * FunctionDecl 26981bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith// - Literals 2699c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// * CompoundLiteralExpr in C 2700c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// * StringLiteral 270147d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith// * CXXTypeidExpr 2702c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// * PredefinedExpr 2703180f47959a066795cc0f409433023af448bb0328Richard Smith// * ObjCStringLiteralExpr 2704c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// * ObjCEncodeExpr 2705c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// * AddrLabelExpr 2706c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// * BlockExpr 2707c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// * CallExpr for a MakeStringConstant builtin 27081bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith// - Locals and temporaries 27091bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith// * Any Expr, with a Frame indicating the function in which the temporary was 27101bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith// evaluated. 27111bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith// plus an offset in bytes. 27124efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman//===----------------------------------------------------------------------===// 27134efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedmannamespace { 2714770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass LValueExprEvaluator 2715e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith : public LValueExprEvaluatorBase<LValueExprEvaluator> { 27164efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedmanpublic: 2717e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LValueExprEvaluator(EvalInfo &Info, LValue &Result) : 2718e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LValueExprEvaluatorBaseTy(Info, Result) {} 27191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2720c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith bool VisitVarDecl(const Expr *E, const VarDecl *VD); 2721c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 27228cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitDeclRefExpr(const DeclRefExpr *E); 27238cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(E); } 2724bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E); 27258cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); 27268cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitMemberExpr(const MemberExpr *E); 27278cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitStringLiteral(const StringLiteral *E) { return Success(E); } 27288cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(E); } 272947d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith bool VisitCXXTypeidExpr(const CXXTypeidExpr *E); 27308cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E); 27318cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitUnaryDeref(const UnaryOperator *E); 27328cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 27338cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitCastExpr(const CastExpr *E) { 273426bc220377705292a0519a71d3ea3aef68fcfec6Anders Carlsson switch (E->getCastKind()) { 273526bc220377705292a0519a71d3ea3aef68fcfec6Anders Carlsson default: 2736e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return LValueExprEvaluatorBaseTy::VisitCastExpr(E); 273726bc220377705292a0519a71d3ea3aef68fcfec6Anders Carlsson 2738db924224b51b153f24fbe492102d4edebcbbb7f4Eli Friedman case CK_LValueBitCast: 2739c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith this->CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; 27400a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith if (!Visit(E->getSubExpr())) 27410a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith return false; 27420a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith Result.Designator.setInvalid(); 27430a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith return true; 2744db924224b51b153f24fbe492102d4edebcbbb7f4Eli Friedman 2745e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case CK_BaseToDerived: 2746180f47959a066795cc0f409433023af448bb0328Richard Smith if (!Visit(E->getSubExpr())) 2747180f47959a066795cc0f409433023af448bb0328Richard Smith return false; 2748e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return HandleBaseToDerivedCast(Info, E, Result); 274926bc220377705292a0519a71d3ea3aef68fcfec6Anders Carlsson } 275026bc220377705292a0519a71d3ea3aef68fcfec6Anders Carlsson } 2751cea8d966f826554f0679595e9371e314e8dbc1cfSebastian Redl 2752ba98d6bb414861965a1f22628494ea046785ecd4Eli Friedman // FIXME: Missing: __real__, __imag__ 27538cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 27544efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman}; 27554efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman} // end anonymous namespace 27564efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 2757c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// Evaluate an expression as an lvalue. This can be legitimately called on 2758c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// expressions which are not glvalues, in a few cases: 2759c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// * function designators in C, 2760c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// * "extern void" objects, 2761c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// * temporaries, if building with -Wno-address-of-temporary. 2762efdb83e26f9a1fd2566afe54461216cd84814d42John McCallstatic bool EvaluateLValue(const Expr* E, LValue& Result, EvalInfo &Info) { 2763c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith assert((E->isGLValue() || E->getType()->isFunctionType() || 2764c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith E->getType()->isVoidType() || isa<CXXTemporaryObjectExpr>(E)) && 2765c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith "can't evaluate expression as an lvalue"); 27668cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return LValueExprEvaluator(Info, Result).Visit(E); 27674efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman} 27684efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 27698cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) { 27701bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl())) 27711bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith return Success(FD); 27721bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith if (const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) 2773c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return VisitVarDecl(E, VD); 2774c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return Error(E); 2775c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith} 2776436c8898cd1c93c5bacd3fcc4ac586bc5cd77062Richard Smith 2777c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smithbool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) { 2778177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith if (!VD->getType()->isReferenceType()) { 2779177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith if (isa<ParmVarDecl>(VD)) { 27801bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith Result.set(VD, Info.CurrentCall); 2781177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith return true; 2782177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith } 27831bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith return Success(VD); 2784177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith } 278550c39ea4858265f3f5f42a0c624557ce2281936bEli Friedman 278647a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith CCValue V; 2787f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!EvaluateVarDeclInit(Info, E, VD, Info.CurrentCall, V)) 2788f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 2789f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Success(V, E); 279035873c49adad211ff466e34342a52665742794f5Anders Carlsson} 279135873c49adad211ff466e34342a52665742794f5Anders Carlsson 2792bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smithbool LValueExprEvaluator::VisitMaterializeTemporaryExpr( 2793bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith const MaterializeTemporaryExpr *E) { 2794e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (E->GetTemporaryExpr()->isRValue()) { 2795af2c7a194592401394233b7cbcdd3cfd0a7a38ddRichard Smith if (E->getType()->isRecordType()) 2796e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return EvaluateTemporary(E->GetTemporaryExpr(), Result, Info); 2797e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2798e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Result.set(E, Info.CurrentCall); 2799e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return EvaluateConstantExpression(Info.CurrentCall->Temporaries[E], Info, 2800e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Result, E->GetTemporaryExpr()); 2801e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 2802e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 2803e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // Materialization of an lvalue temporary occurs when we need to force a copy 2804e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // (for instance, if it's a bitfield). 2805e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // FIXME: The AST should contain an lvalue-to-rvalue node for such cases. 2806e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!Visit(E->GetTemporaryExpr())) 2807e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 2808f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!HandleLValueToRValueConversion(Info, E, E->getType(), Result, 2809e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Info.CurrentCall->Temporaries[E])) 2810e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 28111bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith Result.set(E, Info.CurrentCall); 2812e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 2813bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith} 2814bd552efbeff3a64a1c400d2bba18f13f84abd8abRichard Smith 28158cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool 28168cad3046be06ea73ff8892d947697a21d7a440d3Peter CollingbourneLValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { 2817c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?"); 2818c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // Defer visiting the literal until the lvalue-to-rvalue conversion. We can 2819c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // only see this when folding in C, so there's no standard to follow here. 2820efdb83e26f9a1fd2566afe54461216cd84814d42John McCall return Success(E); 28214efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman} 28224efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 282347d2145675099893d702be4bc06bd9f26d8ddd13Richard Smithbool LValueExprEvaluator::VisitCXXTypeidExpr(const CXXTypeidExpr *E) { 282447d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith if (E->isTypeOperand()) 282547d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith return Success(E); 282647d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith CXXRecordDecl *RD = E->getExprOperand()->getType()->getAsCXXRecordDecl(); 282747d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith if (RD && RD->isPolymorphic()) { 282847d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith Info.Diag(E->getExprLoc(), diag::note_constexpr_typeid_polymorphic) 282947d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith << E->getExprOperand()->getType() 283047d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith << E->getExprOperand()->getSourceRange(); 283147d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith return false; 283247d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith } 283347d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith return Success(E); 283447d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith} 283547d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith 28368cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) { 2837c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // Handle static data members. 2838c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith if (const VarDecl *VD = dyn_cast<VarDecl>(E->getMemberDecl())) { 2839c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith VisitIgnoredValue(E->getBase()); 2840c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return VisitVarDecl(E, VD); 2841c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith } 2842c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 2843d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith // Handle static member functions. 2844d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl())) { 2845d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith if (MD->isStatic()) { 2846d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith VisitIgnoredValue(E->getBase()); 28471bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith return Success(MD); 2848d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith } 2849d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith } 2850d0dcceae2a8ca0e37b5dd471a704de8583d49c95Richard Smith 2851180f47959a066795cc0f409433023af448bb0328Richard Smith // Handle non-static data members. 2852e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return LValueExprEvaluatorBaseTy::VisitMemberExpr(E); 28534efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman} 28544efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 28558cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) { 2856c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // FIXME: Deal with vectors as array subscript bases. 2857c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith if (E->getBase()->getType()->isVectorType()) 2858f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 2859c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 28603068d117951a8df54bae9db039b56201ab10962bAnders Carlsson if (!EvaluatePointer(E->getBase(), Result, Info)) 2861efdb83e26f9a1fd2566afe54461216cd84814d42John McCall return false; 28621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 28633068d117951a8df54bae9db039b56201ab10962bAnders Carlsson APSInt Index; 28643068d117951a8df54bae9db039b56201ab10962bAnders Carlsson if (!EvaluateInteger(E->getIdx(), Index, Info)) 2865efdb83e26f9a1fd2566afe54461216cd84814d42John McCall return false; 2866180f47959a066795cc0f409433023af448bb0328Richard Smith int64_t IndexValue 2867180f47959a066795cc0f409433023af448bb0328Richard Smith = Index.isSigned() ? Index.getSExtValue() 2868180f47959a066795cc0f409433023af448bb0328Richard Smith : static_cast<int64_t>(Index.getZExtValue()); 28693068d117951a8df54bae9db039b56201ab10962bAnders Carlsson 2870b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return HandleLValueArrayAdjustment(Info, E, Result, E->getType(), IndexValue); 28713068d117951a8df54bae9db039b56201ab10962bAnders Carlsson} 28724efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 28738cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) { 2874efdb83e26f9a1fd2566afe54461216cd84814d42John McCall return EvaluatePointer(E->getSubExpr(), Result, Info); 2875e8761c8fe2ee6b628104a0885f49fd3c21c08a4fEli Friedman} 2876e8761c8fe2ee6b628104a0885f49fd3c21c08a4fEli Friedman 28774efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman//===----------------------------------------------------------------------===// 2878f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner// Pointer Evaluation 2879f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===// 2880f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner 2881c754aa62643e66ab967ca32ae8b0b3fc419bba25Anders Carlssonnamespace { 2882770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass PointerExprEvaluator 28838cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne : public ExprEvaluatorBase<PointerExprEvaluator, bool> { 2884efdb83e26f9a1fd2566afe54461216cd84814d42John McCall LValue &Result; 2885efdb83e26f9a1fd2566afe54461216cd84814d42John McCall 28868cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool Success(const Expr *E) { 28871bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith Result.set(E); 2888efdb83e26f9a1fd2566afe54461216cd84814d42John McCall return true; 2889efdb83e26f9a1fd2566afe54461216cd84814d42John McCall } 28902bad1687fe6f00e10767a691a33b070b151902b6Anders Carlssonpublic: 28911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2892efdb83e26f9a1fd2566afe54461216cd84814d42John McCall PointerExprEvaluator(EvalInfo &info, LValue &Result) 28938cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne : ExprEvaluatorBaseTy(info), Result(Result) {} 2894f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner 289547a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith bool Success(const CCValue &V, const Expr *E) { 28968cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne Result.setFrom(V); 28978cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return true; 28982bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson } 289951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith bool ZeroInitialization(const Expr *E) { 2900f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith return Success((Expr*)0); 2901f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith } 29022bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson 2903efdb83e26f9a1fd2566afe54461216cd84814d42John McCall bool VisitBinaryOperator(const BinaryOperator *E); 29048cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitCastExpr(const CastExpr* E); 2905efdb83e26f9a1fd2566afe54461216cd84814d42John McCall bool VisitUnaryAddrOf(const UnaryOperator *E); 29068cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitObjCStringLiteral(const ObjCStringLiteral *E) 2907efdb83e26f9a1fd2566afe54461216cd84814d42John McCall { return Success(E); } 29088cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitAddrLabelExpr(const AddrLabelExpr *E) 2909efdb83e26f9a1fd2566afe54461216cd84814d42John McCall { return Success(E); } 29108cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitCallExpr(const CallExpr *E); 29118cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitBlockExpr(const BlockExpr *E) { 2912469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall if (!E->getBlockDecl()->hasCaptures()) 2913efdb83e26f9a1fd2566afe54461216cd84814d42John McCall return Success(E); 2914f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 2915b83d287bc7f47d36fb0751a481e2ef9308b37252Mike Stump } 2916180f47959a066795cc0f409433023af448bb0328Richard Smith bool VisitCXXThisExpr(const CXXThisExpr *E) { 2917180f47959a066795cc0f409433023af448bb0328Richard Smith if (!Info.CurrentCall->This) 2918f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 2919180f47959a066795cc0f409433023af448bb0328Richard Smith Result = *Info.CurrentCall->This; 2920180f47959a066795cc0f409433023af448bb0328Richard Smith return true; 2921180f47959a066795cc0f409433023af448bb0328Richard Smith } 292256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall 2923ba98d6bb414861965a1f22628494ea046785ecd4Eli Friedman // FIXME: Missing: @protocol, @selector 29242bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson}; 2925f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner} // end anonymous namespace 29262bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson 2927efdb83e26f9a1fd2566afe54461216cd84814d42John McCallstatic bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info) { 2928c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith assert(E->isRValue() && E->getType()->hasPointerRepresentation()); 29298cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return PointerExprEvaluator(Info, Result).Visit(E); 2930f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner} 2931650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson 2932efdb83e26f9a1fd2566afe54461216cd84814d42John McCallbool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { 29332de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (E->getOpcode() != BO_Add && 29342de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall E->getOpcode() != BO_Sub) 2935e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return ExprEvaluatorBaseTy::VisitBinaryOperator(E); 29361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2937650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson const Expr *PExp = E->getLHS(); 2938650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson const Expr *IExp = E->getRHS(); 2939650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson if (IExp->getType()->isPointerType()) 2940f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner std::swap(PExp, IExp); 29411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2942745f5147e065900267c85a5568785a1991d4838fRichard Smith bool EvalPtrOK = EvaluatePointer(PExp, Result, Info); 2943745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!EvalPtrOK && !Info.keepEvaluatingAfterFailure()) 2944efdb83e26f9a1fd2566afe54461216cd84814d42John McCall return false; 29451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2946efdb83e26f9a1fd2566afe54461216cd84814d42John McCall llvm::APSInt Offset; 2947745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!EvaluateInteger(IExp, Offset, Info) || !EvalPtrOK) 2948efdb83e26f9a1fd2566afe54461216cd84814d42John McCall return false; 2949efdb83e26f9a1fd2566afe54461216cd84814d42John McCall int64_t AdditionalOffset 2950efdb83e26f9a1fd2566afe54461216cd84814d42John McCall = Offset.isSigned() ? Offset.getSExtValue() 2951efdb83e26f9a1fd2566afe54461216cd84814d42John McCall : static_cast<int64_t>(Offset.getZExtValue()); 29520a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith if (E->getOpcode() == BO_Sub) 29530a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith AdditionalOffset = -AdditionalOffset; 2954650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson 2955180f47959a066795cc0f409433023af448bb0328Richard Smith QualType Pointee = PExp->getType()->getAs<PointerType>()->getPointeeType(); 2956b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return HandleLValueArrayAdjustment(Info, E, Result, Pointee, 2957b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith AdditionalOffset); 2958650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson} 29594efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 2960efdb83e26f9a1fd2566afe54461216cd84814d42John McCallbool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { 2961efdb83e26f9a1fd2566afe54461216cd84814d42John McCall return EvaluateLValue(E->getSubExpr(), Result, Info); 29624efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman} 29631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 29648cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool PointerExprEvaluator::VisitCastExpr(const CastExpr* E) { 29658cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne const Expr* SubExpr = E->getSubExpr(); 2966650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson 296709a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman switch (E->getCastKind()) { 296809a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman default: 296909a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman break; 297009a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman 29712de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case CK_BitCast: 29721d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall case CK_CPointerToObjCPointerCast: 29731d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall case CK_BlockPointerToObjCPointerCast: 29742de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case CK_AnyPointerToBlockPointerCast: 297528c1ce789322ab99f9b5887015d63ec5f088957aRichard Smith if (!Visit(SubExpr)) 297628c1ce789322ab99f9b5887015d63ec5f088957aRichard Smith return false; 2977c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith // Bitcasts to cv void* are static_casts, not reinterpret_casts, so are 2978c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith // permitted in constant expressions in C++11. Bitcasts from cv void* are 2979c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith // also static_casts, but we disallow them as a resolution to DR1312. 29804cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith if (!E->getType()->isVoidPointerType()) { 298128c1ce789322ab99f9b5887015d63ec5f088957aRichard Smith Result.Designator.setInvalid(); 29824cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith if (SubExpr->getType()->isVoidPointerType()) 29834cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith CCEDiag(E, diag::note_constexpr_invalid_cast) 29844cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith << 3 << SubExpr->getType(); 29854cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith else 29864cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; 29874cd9b8f7fb2cebf614e6e2bc766fad27ffd2e9deRichard Smith } 29880a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith return true; 298909a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman 29905c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson case CK_DerivedToBase: 29915c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson case CK_UncheckedDerivedToBase: { 299247a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith if (!EvaluatePointer(E->getSubExpr(), Result, Info)) 29935c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson return false; 2994e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!Result.Base && Result.Offset.isZero()) 2995e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 29965c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson 2997180f47959a066795cc0f409433023af448bb0328Richard Smith // Now figure out the necessary offset to add to the base LV to get from 29985c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson // the derived class to the base class. 2999180f47959a066795cc0f409433023af448bb0328Richard Smith QualType Type = 3000180f47959a066795cc0f409433023af448bb0328Richard Smith E->getSubExpr()->getType()->castAs<PointerType>()->getPointeeType(); 30015c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson 3002180f47959a066795cc0f409433023af448bb0328Richard Smith for (CastExpr::path_const_iterator PathI = E->path_begin(), 30035c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson PathE = E->path_end(); PathI != PathE; ++PathI) { 3004b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (!HandleLValueBase(Info, E, Result, Type->getAsCXXRecordDecl(), 3005b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith *PathI)) 30065c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson return false; 3007180f47959a066795cc0f409433023af448bb0328Richard Smith Type = (*PathI)->getType(); 30085c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson } 30095c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson 30105c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson return true; 30115c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson } 30125c5a764fcd256df6f6cfbce5cdd2a2dfb2c45e95Anders Carlsson 3013e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case CK_BaseToDerived: 3014e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!Visit(E->getSubExpr())) 3015e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 3016e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!Result.Base && Result.Offset.isZero()) 3017e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 3018e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return HandleBaseToDerivedCast(Info, E, Result); 3019e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 302047a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith case CK_NullToPointer: 302151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return ZeroInitialization(E); 3022404cd1669c3ba138a9ae0a619bd689cce5aae271John McCall 30232de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case CK_IntegralToPointer: { 3024c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; 3025c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith 302647a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith CCValue Value; 3027efdb83e26f9a1fd2566afe54461216cd84814d42John McCall if (!EvaluateIntegerOrLValue(SubExpr, Value, Info)) 302809a8a0e6ec1252cad52666e9dbb21002b9c80f38Eli Friedman break; 302969ab26a8623141f35e86817cfc6e0fbe7639a40fDaniel Dunbar 3030efdb83e26f9a1fd2566afe54461216cd84814d42John McCall if (Value.isInt()) { 303147a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith unsigned Size = Info.Ctx.getTypeSize(E->getType()); 303247a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith uint64_t N = Value.getInt().extOrTrunc(Size).getZExtValue(); 30331bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith Result.Base = (Expr*)0; 303447a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith Result.Offset = CharUnits::fromQuantity(N); 3035177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith Result.Frame = 0; 30360a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith Result.Designator.setInvalid(); 3037efdb83e26f9a1fd2566afe54461216cd84814d42John McCall return true; 3038efdb83e26f9a1fd2566afe54461216cd84814d42John McCall } else { 3039efdb83e26f9a1fd2566afe54461216cd84814d42John McCall // Cast is of an lvalue, no need to change value. 304047a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith Result.setFrom(Value); 3041efdb83e26f9a1fd2566afe54461216cd84814d42John McCall return true; 3042650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson } 3043650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson } 30442de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case CK_ArrayToPointerDecay: 3045e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (SubExpr->isGLValue()) { 3046e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!EvaluateLValue(SubExpr, Result, Info)) 3047e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 3048e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } else { 3049e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Result.set(SubExpr, Info.CurrentCall); 3050e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!EvaluateConstantExpression(Info.CurrentCall->Temporaries[SubExpr], 3051e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Info, Result, SubExpr)) 3052e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 3053e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 30540a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith // The result is a pointer to the first element of the array. 3055b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (const ConstantArrayType *CAT 3056b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith = Info.Ctx.getAsConstantArrayType(SubExpr->getType())) 3057b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Result.addArray(Info, E, CAT); 3058b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith else 3059b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Result.Designator.setInvalid(); 30600a3bdb646ee0318667f4cebec6792d2548fb9950Richard Smith return true; 30616a7c94af983717e2c2d6aebe42cb4737c1c7b9e6Richard Smith 30622de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case CK_FunctionToPointerDecay: 30636a7c94af983717e2c2d6aebe42cb4737c1c7b9e6Richard Smith return EvaluateLValue(SubExpr, Result, Info); 30644efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman } 30654efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 3066c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return ExprEvaluatorBaseTy::VisitCastExpr(E); 30671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump} 3068650c92fdcc27a950a8a848ecab6a74e6f5e80788Anders Carlsson 30698cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) { 3070180f47959a066795cc0f409433023af448bb0328Richard Smith if (IsStringLiteralCall(E)) 3071efdb83e26f9a1fd2566afe54461216cd84814d42John McCall return Success(E); 307256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall 30738cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return ExprEvaluatorBaseTy::VisitCallExpr(E); 30744efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman} 3075f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner 3076f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===// 3077e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// Member Pointer Evaluation 3078e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===// 3079e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3080e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithnamespace { 3081e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithclass MemberPointerExprEvaluator 3082e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith : public ExprEvaluatorBase<MemberPointerExprEvaluator, bool> { 3083e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith MemberPtr &Result; 3084e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3085e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool Success(const ValueDecl *D) { 3086e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Result = MemberPtr(D); 3087e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 3088e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 3089e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithpublic: 3090e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3091e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith MemberPointerExprEvaluator(EvalInfo &Info, MemberPtr &Result) 3092e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith : ExprEvaluatorBaseTy(Info), Result(Result) {} 3093e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3094e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool Success(const CCValue &V, const Expr *E) { 3095e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Result.setFrom(V); 3096e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 3097e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 309851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith bool ZeroInitialization(const Expr *E) { 3099e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return Success((const ValueDecl*)0); 3100e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 3101e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3102e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool VisitCastExpr(const CastExpr *E); 3103e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool VisitUnaryAddrOf(const UnaryOperator *E); 3104e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}; 3105e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith} // end anonymous namespace 3106e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3107e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, 3108e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith EvalInfo &Info) { 3109e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith assert(E->isRValue() && E->getType()->isMemberPointerType()); 3110e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return MemberPointerExprEvaluator(Info, Result).Visit(E); 3111e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith} 3112e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3113e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithbool MemberPointerExprEvaluator::VisitCastExpr(const CastExpr *E) { 3114e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith switch (E->getCastKind()) { 3115e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith default: 3116e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return ExprEvaluatorBaseTy::VisitCastExpr(E); 3117e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3118e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case CK_NullToMemberPointer: 311951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return ZeroInitialization(E); 3120e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3121e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case CK_BaseToDerivedMemberPointer: { 3122e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!Visit(E->getSubExpr())) 3123e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 3124e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (E->path_empty()) 3125e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 3126e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // Base-to-derived member pointer casts store the path in derived-to-base 3127e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // order, so iterate backwards. The CXXBaseSpecifier also provides us with 3128e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // the wrong end of the derived->base arc, so stagger the path by one class. 3129e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter; 3130e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin()); 3131e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith PathI != PathE; ++PathI) { 3132e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); 3133e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const CXXRecordDecl *Derived = (*PathI)->getType()->getAsCXXRecordDecl(); 3134e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!Result.castToDerived(Derived)) 3135f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 3136e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 3137e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const Type *FinalTy = E->getType()->castAs<MemberPointerType>()->getClass(); 3138e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!Result.castToDerived(FinalTy->getAsCXXRecordDecl())) 3139f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 3140e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 3141e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 3142e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3143e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case CK_DerivedToBaseMemberPointer: 3144e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!Visit(E->getSubExpr())) 3145e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 3146e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith for (CastExpr::path_const_iterator PathI = E->path_begin(), 3147e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith PathE = E->path_end(); PathI != PathE; ++PathI) { 3148e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); 3149e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); 3150e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!Result.castToBase(Base)) 3151f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 3152e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 3153e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 3154e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 3155e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith} 3156e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3157e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithbool MemberPointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { 3158e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // C++11 [expr.unary.op]p3 has very strict rules on how the address of a 3159e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // member can be formed. 3160e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return Success(cast<DeclRefExpr>(E->getSubExpr())->getDecl()); 3161e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith} 3162e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3163e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===// 3164180f47959a066795cc0f409433023af448bb0328Richard Smith// Record Evaluation 3165180f47959a066795cc0f409433023af448bb0328Richard Smith//===----------------------------------------------------------------------===// 3166180f47959a066795cc0f409433023af448bb0328Richard Smith 3167180f47959a066795cc0f409433023af448bb0328Richard Smithnamespace { 3168180f47959a066795cc0f409433023af448bb0328Richard Smith class RecordExprEvaluator 3169180f47959a066795cc0f409433023af448bb0328Richard Smith : public ExprEvaluatorBase<RecordExprEvaluator, bool> { 3170180f47959a066795cc0f409433023af448bb0328Richard Smith const LValue &This; 3171180f47959a066795cc0f409433023af448bb0328Richard Smith APValue &Result; 3172180f47959a066795cc0f409433023af448bb0328Richard Smith public: 3173180f47959a066795cc0f409433023af448bb0328Richard Smith 3174180f47959a066795cc0f409433023af448bb0328Richard Smith RecordExprEvaluator(EvalInfo &info, const LValue &This, APValue &Result) 3175180f47959a066795cc0f409433023af448bb0328Richard Smith : ExprEvaluatorBaseTy(info), This(This), Result(Result) {} 3176180f47959a066795cc0f409433023af448bb0328Richard Smith 3177180f47959a066795cc0f409433023af448bb0328Richard Smith bool Success(const CCValue &V, const Expr *E) { 3178f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return CheckConstantExpression(Info, E, V, Result); 3179180f47959a066795cc0f409433023af448bb0328Richard Smith } 318051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith bool ZeroInitialization(const Expr *E); 3181180f47959a066795cc0f409433023af448bb0328Richard Smith 318259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith bool VisitCastExpr(const CastExpr *E); 3183180f47959a066795cc0f409433023af448bb0328Richard Smith bool VisitInitListExpr(const InitListExpr *E); 3184180f47959a066795cc0f409433023af448bb0328Richard Smith bool VisitCXXConstructExpr(const CXXConstructExpr *E); 3185180f47959a066795cc0f409433023af448bb0328Richard Smith }; 3186180f47959a066795cc0f409433023af448bb0328Richard Smith} 3187180f47959a066795cc0f409433023af448bb0328Richard Smith 318851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// Perform zero-initialization on an object of non-union class type. 318951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// C++11 [dcl.init]p5: 319051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// To zero-initialize an object or reference of type T means: 319151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// [...] 319251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// -- if T is a (possibly cv-qualified) non-union class type, 319351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// each non-static data member and each base-class subobject is 319451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith/// zero-initialized 3195b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smithstatic bool HandleClassZeroInitialization(EvalInfo &Info, const Expr *E, 3196b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith const RecordDecl *RD, 319751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith const LValue &This, APValue &Result) { 319851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith assert(!RD->isUnion() && "Expected non-union class type"); 319951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD); 320051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith Result = APValue(APValue::UninitStruct(), CD ? CD->getNumBases() : 0, 320151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith std::distance(RD->field_begin(), RD->field_end())); 320251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 320351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); 320451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 320551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (CD) { 320651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith unsigned Index = 0; 320751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(), 3208b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith End = CD->bases_end(); I != End; ++I, ++Index) { 320951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith const CXXRecordDecl *Base = I->getType()->getAsCXXRecordDecl(); 321051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith LValue Subobject = This; 3211b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith HandleLValueDirectBase(Info, E, Subobject, CD, Base, &Layout); 3212b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith if (!HandleClassZeroInitialization(Info, E, Base, Subobject, 321351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith Result.getStructBase(Index))) 321451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return false; 321551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith } 321651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith } 321751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 3218b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith for (RecordDecl::field_iterator I = RD->field_begin(), End = RD->field_end(); 3219b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith I != End; ++I) { 322051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith // -- if T is a reference type, no initialization is performed. 322151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if ((*I)->getType()->isReferenceType()) 322251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith continue; 322351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 322451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith LValue Subobject = This; 3225b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith HandleLValueMember(Info, E, Subobject, *I, &Layout); 322651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 322751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith ImplicitValueInitExpr VIE((*I)->getType()); 322851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (!EvaluateConstantExpression( 322951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith Result.getStructField((*I)->getFieldIndex()), Info, Subobject, &VIE)) 323051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return false; 323151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith } 323251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 323351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return true; 323451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith} 323551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 323651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smithbool RecordExprEvaluator::ZeroInitialization(const Expr *E) { 323751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl(); 323851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (RD->isUnion()) { 323951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the 324051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith // object's first non-static named data member is zero-initialized 324151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith RecordDecl::field_iterator I = RD->field_begin(); 324251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (I == RD->field_end()) { 324351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith Result = APValue((const FieldDecl*)0); 324451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return true; 324551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith } 324651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 324751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith LValue Subobject = This; 3248b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith HandleLValueMember(Info, E, Subobject, *I); 324951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith Result = APValue(*I); 325051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith ImplicitValueInitExpr VIE((*I)->getType()); 325151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return EvaluateConstantExpression(Result.getUnionValue(), Info, 325251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith Subobject, &VIE); 325351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith } 325451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 3255b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith return HandleClassZeroInitialization(Info, E, RD, This, Result); 325651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith} 325751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 325859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smithbool RecordExprEvaluator::VisitCastExpr(const CastExpr *E) { 325959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith switch (E->getCastKind()) { 326059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith default: 326159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith return ExprEvaluatorBaseTy::VisitCastExpr(E); 326259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith 326359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith case CK_ConstructorConversion: 326459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith return Visit(E->getSubExpr()); 326559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith 326659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith case CK_DerivedToBase: 326759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith case CK_UncheckedDerivedToBase: { 326859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith CCValue DerivedObject; 3269f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!Evaluate(DerivedObject, Info, E->getSubExpr())) 327059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith return false; 3271f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!DerivedObject.isStruct()) 3272f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E->getSubExpr()); 327359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith 327459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith // Derived-to-base rvalue conversion: just slice off the derived part. 327559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith APValue *Value = &DerivedObject; 327659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith const CXXRecordDecl *RD = E->getSubExpr()->getType()->getAsCXXRecordDecl(); 327759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith for (CastExpr::path_const_iterator PathI = E->path_begin(), 327859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith PathE = E->path_end(); PathI != PathE; ++PathI) { 327959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith assert(!(*PathI)->isVirtual() && "record rvalue with virtual base"); 328059efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); 328159efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith Value = &Value->getStructBase(getBaseIndex(RD, Base)); 328259efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith RD = Base; 328359efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith } 328459efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith Result = *Value; 328559efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith return true; 328659efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith } 328759efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith } 328859efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith} 328959efe266b804330f4c1f3a1b0ff783e67dd90378Richard Smith 3290180f47959a066795cc0f409433023af448bb0328Richard Smithbool RecordExprEvaluator::VisitInitListExpr(const InitListExpr *E) { 3291180f47959a066795cc0f409433023af448bb0328Richard Smith const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl(); 3292180f47959a066795cc0f409433023af448bb0328Richard Smith const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); 3293180f47959a066795cc0f409433023af448bb0328Richard Smith 3294180f47959a066795cc0f409433023af448bb0328Richard Smith if (RD->isUnion()) { 3295ec789163a42a7be654ac34aadb750b508954d53cRichard Smith const FieldDecl *Field = E->getInitializedFieldInUnion(); 3296ec789163a42a7be654ac34aadb750b508954d53cRichard Smith Result = APValue(Field); 3297ec789163a42a7be654ac34aadb750b508954d53cRichard Smith if (!Field) 3298180f47959a066795cc0f409433023af448bb0328Richard Smith return true; 3299ec789163a42a7be654ac34aadb750b508954d53cRichard Smith 3300ec789163a42a7be654ac34aadb750b508954d53cRichard Smith // If the initializer list for a union does not contain any elements, the 3301ec789163a42a7be654ac34aadb750b508954d53cRichard Smith // first element of the union is value-initialized. 3302ec789163a42a7be654ac34aadb750b508954d53cRichard Smith ImplicitValueInitExpr VIE(Field->getType()); 3303ec789163a42a7be654ac34aadb750b508954d53cRichard Smith const Expr *InitExpr = E->getNumInits() ? E->getInit(0) : &VIE; 3304ec789163a42a7be654ac34aadb750b508954d53cRichard Smith 3305180f47959a066795cc0f409433023af448bb0328Richard Smith LValue Subobject = This; 3306ec789163a42a7be654ac34aadb750b508954d53cRichard Smith HandleLValueMember(Info, InitExpr, Subobject, Field, &Layout); 3307180f47959a066795cc0f409433023af448bb0328Richard Smith return EvaluateConstantExpression(Result.getUnionValue(), Info, 3308ec789163a42a7be654ac34aadb750b508954d53cRichard Smith Subobject, InitExpr); 3309180f47959a066795cc0f409433023af448bb0328Richard Smith } 3310180f47959a066795cc0f409433023af448bb0328Richard Smith 3311180f47959a066795cc0f409433023af448bb0328Richard Smith assert((!isa<CXXRecordDecl>(RD) || !cast<CXXRecordDecl>(RD)->getNumBases()) && 3312180f47959a066795cc0f409433023af448bb0328Richard Smith "initializer list for class with base classes"); 3313180f47959a066795cc0f409433023af448bb0328Richard Smith Result = APValue(APValue::UninitStruct(), 0, 3314180f47959a066795cc0f409433023af448bb0328Richard Smith std::distance(RD->field_begin(), RD->field_end())); 3315180f47959a066795cc0f409433023af448bb0328Richard Smith unsigned ElementNo = 0; 3316745f5147e065900267c85a5568785a1991d4838fRichard Smith bool Success = true; 3317180f47959a066795cc0f409433023af448bb0328Richard Smith for (RecordDecl::field_iterator Field = RD->field_begin(), 3318180f47959a066795cc0f409433023af448bb0328Richard Smith FieldEnd = RD->field_end(); Field != FieldEnd; ++Field) { 3319180f47959a066795cc0f409433023af448bb0328Richard Smith // Anonymous bit-fields are not considered members of the class for 3320180f47959a066795cc0f409433023af448bb0328Richard Smith // purposes of aggregate initialization. 3321180f47959a066795cc0f409433023af448bb0328Richard Smith if (Field->isUnnamedBitfield()) 3322180f47959a066795cc0f409433023af448bb0328Richard Smith continue; 3323180f47959a066795cc0f409433023af448bb0328Richard Smith 3324180f47959a066795cc0f409433023af448bb0328Richard Smith LValue Subobject = This; 3325180f47959a066795cc0f409433023af448bb0328Richard Smith 3326745f5147e065900267c85a5568785a1991d4838fRichard Smith bool HaveInit = ElementNo < E->getNumInits(); 3327745f5147e065900267c85a5568785a1991d4838fRichard Smith 3328745f5147e065900267c85a5568785a1991d4838fRichard Smith // FIXME: Diagnostics here should point to the end of the initializer 3329745f5147e065900267c85a5568785a1991d4838fRichard Smith // list, not the start. 3330745f5147e065900267c85a5568785a1991d4838fRichard Smith HandleLValueMember(Info, HaveInit ? E->getInit(ElementNo) : E, Subobject, 3331745f5147e065900267c85a5568785a1991d4838fRichard Smith *Field, &Layout); 3332745f5147e065900267c85a5568785a1991d4838fRichard Smith 3333745f5147e065900267c85a5568785a1991d4838fRichard Smith // Perform an implicit value-initialization for members beyond the end of 3334745f5147e065900267c85a5568785a1991d4838fRichard Smith // the initializer list. 3335745f5147e065900267c85a5568785a1991d4838fRichard Smith ImplicitValueInitExpr VIE(HaveInit ? Info.Ctx.IntTy : Field->getType()); 3336745f5147e065900267c85a5568785a1991d4838fRichard Smith 3337745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!EvaluateConstantExpression( 3338745f5147e065900267c85a5568785a1991d4838fRichard Smith Result.getStructField((*Field)->getFieldIndex()), 3339745f5147e065900267c85a5568785a1991d4838fRichard Smith Info, Subobject, HaveInit ? E->getInit(ElementNo++) : &VIE)) { 3340745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!Info.keepEvaluatingAfterFailure()) 3341180f47959a066795cc0f409433023af448bb0328Richard Smith return false; 3342745f5147e065900267c85a5568785a1991d4838fRichard Smith Success = false; 3343180f47959a066795cc0f409433023af448bb0328Richard Smith } 3344180f47959a066795cc0f409433023af448bb0328Richard Smith } 3345180f47959a066795cc0f409433023af448bb0328Richard Smith 3346745f5147e065900267c85a5568785a1991d4838fRichard Smith return Success; 3347180f47959a066795cc0f409433023af448bb0328Richard Smith} 3348180f47959a066795cc0f409433023af448bb0328Richard Smith 3349180f47959a066795cc0f409433023af448bb0328Richard Smithbool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { 3350180f47959a066795cc0f409433023af448bb0328Richard Smith const CXXConstructorDecl *FD = E->getConstructor(); 335151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith bool ZeroInit = E->requiresZeroInitialization(); 335251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) { 3353ec789163a42a7be654ac34aadb750b508954d53cRichard Smith // If we've already performed zero-initialization, we're already done. 3354ec789163a42a7be654ac34aadb750b508954d53cRichard Smith if (!Result.isUninit()) 3355ec789163a42a7be654ac34aadb750b508954d53cRichard Smith return true; 3356ec789163a42a7be654ac34aadb750b508954d53cRichard Smith 335751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (ZeroInit) 335851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return ZeroInitialization(E); 335951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 33606180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith const CXXRecordDecl *RD = FD->getParent(); 33616180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith if (RD->isUnion()) 33626180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith Result = APValue((FieldDecl*)0); 33636180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith else 33646180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith Result = APValue(APValue::UninitStruct(), RD->getNumBases(), 33656180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith std::distance(RD->field_begin(), RD->field_end())); 33666180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith return true; 33676180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith } 33686180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith 3369180f47959a066795cc0f409433023af448bb0328Richard Smith const FunctionDecl *Definition = 0; 3370180f47959a066795cc0f409433023af448bb0328Richard Smith FD->getBody(Definition); 3371180f47959a066795cc0f409433023af448bb0328Richard Smith 3372c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition)) 3373c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return false; 3374180f47959a066795cc0f409433023af448bb0328Richard Smith 3375610a60c0e68e34db5a5247d6102e58f37510fef8Richard Smith // Avoid materializing a temporary for an elidable copy/move constructor. 337651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (E->isElidable() && !ZeroInit) 3377180f47959a066795cc0f409433023af448bb0328Richard Smith if (const MaterializeTemporaryExpr *ME 3378180f47959a066795cc0f409433023af448bb0328Richard Smith = dyn_cast<MaterializeTemporaryExpr>(E->getArg(0))) 3379180f47959a066795cc0f409433023af448bb0328Richard Smith return Visit(ME->GetTemporaryExpr()); 3380180f47959a066795cc0f409433023af448bb0328Richard Smith 338151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (ZeroInit && !ZeroInitialization(E)) 338251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return false; 338351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 3384180f47959a066795cc0f409433023af448bb0328Richard Smith llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs()); 3385745f5147e065900267c85a5568785a1991d4838fRichard Smith return HandleConstructorCall(E->getExprLoc(), This, Args, 3386f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith cast<CXXConstructorDecl>(Definition), Info, 3387f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith Result); 3388180f47959a066795cc0f409433023af448bb0328Richard Smith} 3389180f47959a066795cc0f409433023af448bb0328Richard Smith 3390180f47959a066795cc0f409433023af448bb0328Richard Smithstatic bool EvaluateRecord(const Expr *E, const LValue &This, 3391180f47959a066795cc0f409433023af448bb0328Richard Smith APValue &Result, EvalInfo &Info) { 3392180f47959a066795cc0f409433023af448bb0328Richard Smith assert(E->isRValue() && E->getType()->isRecordType() && 3393180f47959a066795cc0f409433023af448bb0328Richard Smith "can't evaluate expression as a record rvalue"); 3394180f47959a066795cc0f409433023af448bb0328Richard Smith return RecordExprEvaluator(Info, This, Result).Visit(E); 3395180f47959a066795cc0f409433023af448bb0328Richard Smith} 3396180f47959a066795cc0f409433023af448bb0328Richard Smith 3397180f47959a066795cc0f409433023af448bb0328Richard Smith//===----------------------------------------------------------------------===// 3398e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// Temporary Evaluation 3399e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// 3400e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// Temporaries are represented in the AST as rvalues, but generally behave like 3401e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// lvalues. The full-object of which the temporary is a subobject is implicitly 3402e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith// materialized so that a reference can bind to it. 3403e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===// 3404e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithnamespace { 3405e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithclass TemporaryExprEvaluator 3406e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith : public LValueExprEvaluatorBase<TemporaryExprEvaluator> { 3407e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithpublic: 3408e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith TemporaryExprEvaluator(EvalInfo &Info, LValue &Result) : 3409e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LValueExprEvaluatorBaseTy(Info, Result) {} 3410e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3411e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith /// Visit an expression which constructs the value of this temporary. 3412e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool VisitConstructExpr(const Expr *E) { 3413e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Result.set(E, Info.CurrentCall); 3414e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return EvaluateConstantExpression(Info.CurrentCall->Temporaries[E], Info, 3415e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Result, E); 3416e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 3417e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3418e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool VisitCastExpr(const CastExpr *E) { 3419e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith switch (E->getCastKind()) { 3420e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith default: 3421e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return LValueExprEvaluatorBaseTy::VisitCastExpr(E); 3422e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3423e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith case CK_ConstructorConversion: 3424e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return VisitConstructExpr(E->getSubExpr()); 3425e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 3426e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 3427e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool VisitInitListExpr(const InitListExpr *E) { 3428e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return VisitConstructExpr(E); 3429e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 3430e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool VisitCXXConstructExpr(const CXXConstructExpr *E) { 3431e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return VisitConstructExpr(E); 3432e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 3433e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool VisitCallExpr(const CallExpr *E) { 3434e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return VisitConstructExpr(E); 3435e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith } 3436e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith}; 3437e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith} // end anonymous namespace 3438e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3439e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith/// Evaluate an expression of record type as a temporary. 3440e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithstatic bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info) { 3441af2c7a194592401394233b7cbcdd3cfd0a7a38ddRichard Smith assert(E->isRValue() && E->getType()->isRecordType()); 3442e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return TemporaryExprEvaluator(Info, Result).Visit(E); 3443e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith} 3444e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3445e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith//===----------------------------------------------------------------------===// 344659b5da6d853b4368b984700315adf7b37de05764Nate Begeman// Vector Evaluation 344759b5da6d853b4368b984700315adf7b37de05764Nate Begeman//===----------------------------------------------------------------------===// 344859b5da6d853b4368b984700315adf7b37de05764Nate Begeman 344959b5da6d853b4368b984700315adf7b37de05764Nate Begemannamespace { 3450770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramer class VectorExprEvaluator 345107fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith : public ExprEvaluatorBase<VectorExprEvaluator, bool> { 345207fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith APValue &Result; 345359b5da6d853b4368b984700315adf7b37de05764Nate Begeman public: 34541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 345507fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith VectorExprEvaluator(EvalInfo &info, APValue &Result) 345607fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith : ExprEvaluatorBaseTy(info), Result(Result) {} 34571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 345807fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith bool Success(const ArrayRef<APValue> &V, const Expr *E) { 345907fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith assert(V.size() == E->getType()->castAs<VectorType>()->getNumElements()); 346007fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith // FIXME: remove this APValue copy. 346107fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith Result = APValue(V.data(), V.size()); 346207fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith return true; 346307fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith } 346469c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith bool Success(const CCValue &V, const Expr *E) { 346569c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith assert(V.isVector()); 346607fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith Result = V; 346707fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith return true; 346807fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith } 346951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith bool ZeroInitialization(const Expr *E); 34701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 347107fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith bool VisitUnaryReal(const UnaryOperator *E) 347291110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman { return Visit(E->getSubExpr()); } 347307fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith bool VisitCastExpr(const CastExpr* E); 347407fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith bool VisitInitListExpr(const InitListExpr *E); 347507fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith bool VisitUnaryImag(const UnaryOperator *E); 347691110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div, 34772217c87bdc5ab357046a5453bdb06f469c41024eEli Friedman // binary comparisons, binary and/or/xor, 347891110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman // shufflevector, ExtVectorElementExpr 347959b5da6d853b4368b984700315adf7b37de05764Nate Begeman }; 348059b5da6d853b4368b984700315adf7b37de05764Nate Begeman} // end anonymous namespace 348159b5da6d853b4368b984700315adf7b37de05764Nate Begeman 348259b5da6d853b4368b984700315adf7b37de05764Nate Begemanstatic bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) { 3483c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith assert(E->isRValue() && E->getType()->isVectorType() &&"not a vector rvalue"); 348407fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith return VectorExprEvaluator(Info, Result).Visit(E); 348559b5da6d853b4368b984700315adf7b37de05764Nate Begeman} 348659b5da6d853b4368b984700315adf7b37de05764Nate Begeman 348707fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smithbool VectorExprEvaluator::VisitCastExpr(const CastExpr* E) { 348807fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith const VectorType *VTy = E->getType()->castAs<VectorType>(); 3489c0b8b19bd8056d6b5d831623a0825cce150f4507Nate Begeman unsigned NElts = VTy->getNumElements(); 34901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3491d62ca370b03b8c6ad58002d3399383baf744e32bRichard Smith const Expr *SE = E->getSubExpr(); 3492e8c9e9218f215ec6089f12b076c7b9d310fd5194Nate Begeman QualType SETy = SE->getType(); 349359b5da6d853b4368b984700315adf7b37de05764Nate Begeman 349446a523285928aa07bf14803178dc04616ac85994Eli Friedman switch (E->getCastKind()) { 349546a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_VectorSplat: { 349607fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith APValue Val = APValue(); 349746a523285928aa07bf14803178dc04616ac85994Eli Friedman if (SETy->isIntegerType()) { 349846a523285928aa07bf14803178dc04616ac85994Eli Friedman APSInt IntResult; 349946a523285928aa07bf14803178dc04616ac85994Eli Friedman if (!EvaluateInteger(SE, IntResult, Info)) 3500f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 350107fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith Val = APValue(IntResult); 350246a523285928aa07bf14803178dc04616ac85994Eli Friedman } else if (SETy->isRealFloatingType()) { 350346a523285928aa07bf14803178dc04616ac85994Eli Friedman APFloat F(0.0); 350446a523285928aa07bf14803178dc04616ac85994Eli Friedman if (!EvaluateFloat(SE, F, Info)) 3505f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 350607fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith Val = APValue(F); 350746a523285928aa07bf14803178dc04616ac85994Eli Friedman } else { 350807fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith return Error(E); 350946a523285928aa07bf14803178dc04616ac85994Eli Friedman } 3510c0b8b19bd8056d6b5d831623a0825cce150f4507Nate Begeman 3511c0b8b19bd8056d6b5d831623a0825cce150f4507Nate Begeman // Splat and create vector APValue. 351207fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith SmallVector<APValue, 4> Elts(NElts, Val); 351307fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith return Success(Elts, E); 3514e8c9e9218f215ec6089f12b076c7b9d310fd5194Nate Begeman } 3515e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman case CK_BitCast: { 3516e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman // Evaluate the operand into an APInt we can extract from. 3517e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman llvm::APInt SValInt; 3518e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman if (!EvalAndBitcastToAPInt(Info, SE, SValInt)) 3519e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman return false; 3520e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman // Extract the elements 3521e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman QualType EltTy = VTy->getElementType(); 3522e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman unsigned EltSize = Info.Ctx.getTypeSize(EltTy); 3523e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); 3524e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman SmallVector<APValue, 4> Elts; 3525e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman if (EltTy->isRealFloatingType()) { 3526e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(EltTy); 3527e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman bool isIEESem = &Sem != &APFloat::PPCDoubleDouble; 3528e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman unsigned FloatEltSize = EltSize; 3529e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman if (&Sem == &APFloat::x87DoubleExtended) 3530e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman FloatEltSize = 80; 3531e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman for (unsigned i = 0; i < NElts; i++) { 3532e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman llvm::APInt Elt; 3533e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman if (BigEndian) 3534e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman Elt = SValInt.rotl(i*EltSize+FloatEltSize).trunc(FloatEltSize); 3535e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman else 3536e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman Elt = SValInt.rotr(i*EltSize).trunc(FloatEltSize); 3537e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman Elts.push_back(APValue(APFloat(Elt, isIEESem))); 3538e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman } 3539e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman } else if (EltTy->isIntegerType()) { 3540e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman for (unsigned i = 0; i < NElts; i++) { 3541e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman llvm::APInt Elt; 3542e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman if (BigEndian) 3543e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman Elt = SValInt.rotl(i*EltSize+EltSize).zextOrTrunc(EltSize); 3544e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman else 3545e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman Elt = SValInt.rotr(i*EltSize).zextOrTrunc(EltSize); 3546e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman Elts.push_back(APValue(APSInt(Elt, EltTy->isSignedIntegerType()))); 3547e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman } 3548e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman } else { 3549e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman return Error(E); 3550e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman } 3551e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman return Success(Elts, E); 3552e6a24e83e71f361c7b7de82cf24ee6f5ddc7f1c2Eli Friedman } 355346a523285928aa07bf14803178dc04616ac85994Eli Friedman default: 3554c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return ExprEvaluatorBaseTy::VisitCastExpr(E); 3555c0b8b19bd8056d6b5d831623a0825cce150f4507Nate Begeman } 355659b5da6d853b4368b984700315adf7b37de05764Nate Begeman} 355759b5da6d853b4368b984700315adf7b37de05764Nate Begeman 355807fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smithbool 355959b5da6d853b4368b984700315adf7b37de05764Nate BegemanVectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) { 356007fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith const VectorType *VT = E->getType()->castAs<VectorType>(); 356159b5da6d853b4368b984700315adf7b37de05764Nate Begeman unsigned NumInits = E->getNumInits(); 356291110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman unsigned NumElements = VT->getNumElements(); 35631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 356459b5da6d853b4368b984700315adf7b37de05764Nate Begeman QualType EltTy = VT->getElementType(); 35655f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<APValue, 4> Elements; 356659b5da6d853b4368b984700315adf7b37de05764Nate Begeman 35673edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman // The number of initializers can be less than the number of 35683edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman // vector elements. For OpenCL, this can be due to nested vector 35693edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman // initialization. For GCC compatibility, missing trailing elements 35703edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman // should be initialized with zeroes. 35713edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman unsigned CountInits = 0, CountElts = 0; 35723edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman while (CountElts < NumElements) { 35733edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman // Handle nested vector initialization. 35743edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman if (CountInits < NumInits 35753edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman && E->getInit(CountInits)->getType()->isExtVectorType()) { 35763edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman APValue v; 35773edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman if (!EvaluateVector(E->getInit(CountInits), v, Info)) 35783edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman return Error(E); 35793edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman unsigned vlen = v.getVectorLength(); 35803edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman for (unsigned j = 0; j < vlen; j++) 35813edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman Elements.push_back(v.getVectorElt(j)); 35823edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman CountElts += vlen; 35833edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman } else if (EltTy->isIntegerType()) { 358459b5da6d853b4368b984700315adf7b37de05764Nate Begeman llvm::APSInt sInt(32); 35853edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman if (CountInits < NumInits) { 35863edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman if (!EvaluateInteger(E->getInit(CountInits), sInt, Info)) 35873edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman return Error(E); 35883edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman } else // trailing integer zero. 35893edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman sInt = Info.Ctx.MakeIntValue(0, EltTy); 35903edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman Elements.push_back(APValue(sInt)); 35913edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman CountElts++; 359259b5da6d853b4368b984700315adf7b37de05764Nate Begeman } else { 359359b5da6d853b4368b984700315adf7b37de05764Nate Begeman llvm::APFloat f(0.0); 35943edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman if (CountInits < NumInits) { 35953edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman if (!EvaluateFloat(E->getInit(CountInits), f, Info)) 35963edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman return Error(E); 35973edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman } else // trailing float zero. 35983edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)); 35993edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman Elements.push_back(APValue(f)); 36003edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman CountElts++; 360159b5da6d853b4368b984700315adf7b37de05764Nate Begeman } 36023edd5a99332dd8ec94a545476dc3c9ed50dec78fEli Friedman CountInits++; 360359b5da6d853b4368b984700315adf7b37de05764Nate Begeman } 360407fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith return Success(Elements, E); 360559b5da6d853b4368b984700315adf7b37de05764Nate Begeman} 360659b5da6d853b4368b984700315adf7b37de05764Nate Begeman 360707fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smithbool 360851201882382fb40c9456a06c7f93d6ddd4a57712Richard SmithVectorExprEvaluator::ZeroInitialization(const Expr *E) { 360907fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith const VectorType *VT = E->getType()->getAs<VectorType>(); 361091110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman QualType EltTy = VT->getElementType(); 361191110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman APValue ZeroElement; 361291110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman if (EltTy->isIntegerType()) 361391110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy)); 361491110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman else 361591110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman ZeroElement = 361691110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy))); 361791110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman 36185f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement); 361907fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smith return Success(Elements, E); 362091110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman} 362191110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman 362207fc657e3077531805b0e2dbf8f8964d48daa38bRichard Smithbool VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { 36238327fad71da34492d82c532f42a58cb4baff81a3Richard Smith VisitIgnoredValue(E->getSubExpr()); 362451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return ZeroInitialization(E); 362591110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman} 362691110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman 362759b5da6d853b4368b984700315adf7b37de05764Nate Begeman//===----------------------------------------------------------------------===// 3628cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith// Array Evaluation 3629cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith//===----------------------------------------------------------------------===// 3630cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith 3631cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smithnamespace { 3632cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith class ArrayExprEvaluator 3633cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith : public ExprEvaluatorBase<ArrayExprEvaluator, bool> { 3634180f47959a066795cc0f409433023af448bb0328Richard Smith const LValue &This; 3635cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith APValue &Result; 3636cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith public: 3637cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith 3638180f47959a066795cc0f409433023af448bb0328Richard Smith ArrayExprEvaluator(EvalInfo &Info, const LValue &This, APValue &Result) 3639180f47959a066795cc0f409433023af448bb0328Richard Smith : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {} 3640cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith 3641cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith bool Success(const APValue &V, const Expr *E) { 3642cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith assert(V.isArray() && "Expected array type"); 3643cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith Result = V; 3644cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith return true; 3645cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith } 3646cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith 364751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith bool ZeroInitialization(const Expr *E) { 3648180f47959a066795cc0f409433023af448bb0328Richard Smith const ConstantArrayType *CAT = 3649180f47959a066795cc0f409433023af448bb0328Richard Smith Info.Ctx.getAsConstantArrayType(E->getType()); 3650180f47959a066795cc0f409433023af448bb0328Richard Smith if (!CAT) 3651f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 3652180f47959a066795cc0f409433023af448bb0328Richard Smith 3653180f47959a066795cc0f409433023af448bb0328Richard Smith Result = APValue(APValue::UninitArray(), 0, 3654180f47959a066795cc0f409433023af448bb0328Richard Smith CAT->getSize().getZExtValue()); 3655180f47959a066795cc0f409433023af448bb0328Richard Smith if (!Result.hasArrayFiller()) return true; 3656180f47959a066795cc0f409433023af448bb0328Richard Smith 365751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith // Zero-initialize all elements. 3658180f47959a066795cc0f409433023af448bb0328Richard Smith LValue Subobject = This; 3659b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Subobject.addArray(Info, E, CAT); 3660180f47959a066795cc0f409433023af448bb0328Richard Smith ImplicitValueInitExpr VIE(CAT->getElementType()); 3661180f47959a066795cc0f409433023af448bb0328Richard Smith return EvaluateConstantExpression(Result.getArrayFiller(), Info, 3662180f47959a066795cc0f409433023af448bb0328Richard Smith Subobject, &VIE); 3663180f47959a066795cc0f409433023af448bb0328Richard Smith } 3664180f47959a066795cc0f409433023af448bb0328Richard Smith 3665cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith bool VisitInitListExpr(const InitListExpr *E); 3666e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith bool VisitCXXConstructExpr(const CXXConstructExpr *E); 3667cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith }; 3668cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith} // end anonymous namespace 3669cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith 3670180f47959a066795cc0f409433023af448bb0328Richard Smithstatic bool EvaluateArray(const Expr *E, const LValue &This, 3671180f47959a066795cc0f409433023af448bb0328Richard Smith APValue &Result, EvalInfo &Info) { 367251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith assert(E->isRValue() && E->getType()->isArrayType() && "not an array rvalue"); 3673180f47959a066795cc0f409433023af448bb0328Richard Smith return ArrayExprEvaluator(Info, This, Result).Visit(E); 3674cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith} 3675cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith 3676cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smithbool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E) { 3677cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(E->getType()); 3678cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith if (!CAT) 3679f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 3680cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith 3681974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith // C++11 [dcl.init.string]p1: A char array [...] can be initialized by [...] 3682974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith // an appropriately-typed string literal enclosed in braces. 3683ec789163a42a7be654ac34aadb750b508954d53cRichard Smith if (E->getNumInits() == 1 && E->getInit(0)->isGLValue() && 3684974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith Info.Ctx.hasSameUnqualifiedType(E->getType(), E->getInit(0)->getType())) { 3685974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith LValue LV; 3686974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith if (!EvaluateLValue(E->getInit(0), LV, Info)) 3687974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith return false; 3688974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith uint64_t NumElements = CAT->getSize().getZExtValue(); 3689974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith Result = APValue(APValue::UninitArray(), NumElements, NumElements); 3690974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith 3691974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith // Copy the string literal into the array. FIXME: Do this better. 3692b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith LV.addArray(Info, E, CAT); 3693974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith for (uint64_t I = 0; I < NumElements; ++I) { 3694974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith CCValue Char; 3695974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith if (!HandleLValueToRValueConversion(Info, E->getInit(0), 3696745f5147e065900267c85a5568785a1991d4838fRichard Smith CAT->getElementType(), LV, Char) || 3697745f5147e065900267c85a5568785a1991d4838fRichard Smith !CheckConstantExpression(Info, E->getInit(0), Char, 3698745f5147e065900267c85a5568785a1991d4838fRichard Smith Result.getArrayInitializedElt(I)) || 3699745f5147e065900267c85a5568785a1991d4838fRichard Smith !HandleLValueArrayAdjustment(Info, E->getInit(0), LV, 3700b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith CAT->getElementType(), 1)) 3701974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith return false; 3702974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith } 3703974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith return true; 3704974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith } 3705974c5f93d0ce4f0699a6f0a4402f6b367da495e3Richard Smith 3706745f5147e065900267c85a5568785a1991d4838fRichard Smith bool Success = true; 3707745f5147e065900267c85a5568785a1991d4838fRichard Smith 3708cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith Result = APValue(APValue::UninitArray(), E->getNumInits(), 3709cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith CAT->getSize().getZExtValue()); 3710180f47959a066795cc0f409433023af448bb0328Richard Smith LValue Subobject = This; 3711b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Subobject.addArray(Info, E, CAT); 3712180f47959a066795cc0f409433023af448bb0328Richard Smith unsigned Index = 0; 3713cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith for (InitListExpr::const_iterator I = E->begin(), End = E->end(); 3714180f47959a066795cc0f409433023af448bb0328Richard Smith I != End; ++I, ++Index) { 3715180f47959a066795cc0f409433023af448bb0328Richard Smith if (!EvaluateConstantExpression(Result.getArrayInitializedElt(Index), 3716745f5147e065900267c85a5568785a1991d4838fRichard Smith Info, Subobject, cast<Expr>(*I)) || 3717745f5147e065900267c85a5568785a1991d4838fRichard Smith !HandleLValueArrayAdjustment(Info, cast<Expr>(*I), Subobject, 3718745f5147e065900267c85a5568785a1991d4838fRichard Smith CAT->getElementType(), 1)) { 3719745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!Info.keepEvaluatingAfterFailure()) 3720745f5147e065900267c85a5568785a1991d4838fRichard Smith return false; 3721745f5147e065900267c85a5568785a1991d4838fRichard Smith Success = false; 3722745f5147e065900267c85a5568785a1991d4838fRichard Smith } 3723180f47959a066795cc0f409433023af448bb0328Richard Smith } 3724cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith 3725745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!Result.hasArrayFiller()) return Success; 3726cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith assert(E->hasArrayFiller() && "no array filler for incomplete init list"); 3727180f47959a066795cc0f409433023af448bb0328Richard Smith // FIXME: The Subobject here isn't necessarily right. This rarely matters, 3728180f47959a066795cc0f409433023af448bb0328Richard Smith // but sometimes does: 3729180f47959a066795cc0f409433023af448bb0328Richard Smith // struct S { constexpr S() : p(&p) {} void *p; }; 3730180f47959a066795cc0f409433023af448bb0328Richard Smith // S s[10] = {}; 3731cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith return EvaluateConstantExpression(Result.getArrayFiller(), Info, 3732745f5147e065900267c85a5568785a1991d4838fRichard Smith Subobject, E->getArrayFiller()) && Success; 3733cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith} 3734cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith 3735e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smithbool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { 3736e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(E->getType()); 3737e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!CAT) 3738f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 3739e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3740ec789163a42a7be654ac34aadb750b508954d53cRichard Smith bool HadZeroInit = !Result.isUninit(); 3741ec789163a42a7be654ac34aadb750b508954d53cRichard Smith if (!HadZeroInit) 3742ec789163a42a7be654ac34aadb750b508954d53cRichard Smith Result = APValue(APValue::UninitArray(), 0, CAT->getSize().getZExtValue()); 3743e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!Result.hasArrayFiller()) 3744e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 3745e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3746e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const CXXConstructorDecl *FD = E->getConstructor(); 37476180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith 374851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith bool ZeroInit = E->requiresZeroInitialization(); 374951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) { 3750ec789163a42a7be654ac34aadb750b508954d53cRichard Smith if (HadZeroInit) 3751ec789163a42a7be654ac34aadb750b508954d53cRichard Smith return true; 3752ec789163a42a7be654ac34aadb750b508954d53cRichard Smith 375351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (ZeroInit) { 375451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith LValue Subobject = This; 3755b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Subobject.addArray(Info, E, CAT); 375651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith ImplicitValueInitExpr VIE(CAT->getElementType()); 375751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return EvaluateConstantExpression(Result.getArrayFiller(), Info, 375851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith Subobject, &VIE); 375951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith } 376051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 37616180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith const CXXRecordDecl *RD = FD->getParent(); 37626180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith if (RD->isUnion()) 37636180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith Result.getArrayFiller() = APValue((FieldDecl*)0); 37646180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith else 37656180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith Result.getArrayFiller() = 37666180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith APValue(APValue::UninitStruct(), RD->getNumBases(), 37676180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith std::distance(RD->field_begin(), RD->field_end())); 37686180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith return true; 37696180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith } 37706180245e9f63d2927b185ec251fb75aba30f1cacRichard Smith 3771e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith const FunctionDecl *Definition = 0; 3772e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith FD->getBody(Definition); 3773e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3774c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition)) 3775c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return false; 3776e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3777e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // FIXME: The Subobject here isn't necessarily right. This rarely matters, 3778e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // but sometimes does: 3779e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // struct S { constexpr S() : p(&p) {} void *p; }; 3780e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // S s[10]; 3781e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith LValue Subobject = This; 3782b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Subobject.addArray(Info, E, CAT); 378351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 3784ec789163a42a7be654ac34aadb750b508954d53cRichard Smith if (ZeroInit && !HadZeroInit) { 378551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith ImplicitValueInitExpr VIE(CAT->getElementType()); 378651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (!EvaluateConstantExpression(Result.getArrayFiller(), Info, Subobject, 378751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith &VIE)) 378851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return false; 378951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith } 379051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 3791e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs()); 3792745f5147e065900267c85a5568785a1991d4838fRichard Smith return HandleConstructorCall(E->getExprLoc(), Subobject, Args, 3793e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith cast<CXXConstructorDecl>(Definition), 3794e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith Info, Result.getArrayFiller()); 3795e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith} 3796e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith 3797cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith//===----------------------------------------------------------------------===// 3798f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner// Integer Evaluation 3799c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// 3800c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// As a GNU extension, we support casting pointers to sufficiently-wide integer 3801c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// types and back in constant folding. Integer values are thus represented 3802c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith// either as an integer-valued APValue, or as an lvalue-valued APValue. 3803f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===// 3804f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner 3805f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattnernamespace { 3806770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass IntExprEvaluator 38078cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne : public ExprEvaluatorBase<IntExprEvaluator, bool> { 380847a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith CCValue &Result; 3809f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattnerpublic: 381047a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith IntExprEvaluator(EvalInfo &info, CCValue &result) 38118cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne : ExprEvaluatorBaseTy(info), Result(result) {} 3812f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner 3813973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara bool Success(const llvm::APSInt &SI, const Expr *E) { 3814973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara assert(E->getType()->isIntegralOrEnumerationType() && 38152ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor "Invalid evaluation result."); 3816973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() && 38173f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar "Invalid evaluation result."); 3818973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && 38193f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar "Invalid evaluation result."); 382047a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith Result = CCValue(SI); 38213f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar return true; 38223f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar } 38233f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar 3824131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar bool Success(const llvm::APInt &I, const Expr *E) { 38252ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor assert(E->getType()->isIntegralOrEnumerationType() && 38262ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor "Invalid evaluation result."); 382730c37f4d2ee5811e85f692c22fb67d74ddc88079Daniel Dunbar assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && 38283f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar "Invalid evaluation result."); 382947a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith Result = CCValue(APSInt(I)); 3830575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor Result.getInt().setIsUnsigned( 3831575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor E->getType()->isUnsignedIntegerOrEnumerationType()); 3832131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return true; 3833131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar } 3834131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar 3835131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar bool Success(uint64_t Value, const Expr *E) { 38362ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor assert(E->getType()->isIntegralOrEnumerationType() && 38372ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor "Invalid evaluation result."); 383847a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith Result = CCValue(Info.Ctx.MakeIntValue(Value, E->getType())); 3839131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return true; 3840131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar } 3841131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar 38424f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck bool Success(CharUnits Size, const Expr *E) { 38434f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck return Success(Size.getQuantity(), E); 38444f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck } 38454f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck 384647a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith bool Success(const CCValue &V, const Expr *E) { 38475930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman if (V.isLValue() || V.isAddrLabelDiff()) { 3848342f1f8b0a402c5a7f8c5055db7f60a7808f1687Richard Smith Result = V; 3849342f1f8b0a402c5a7f8c5055db7f60a7808f1687Richard Smith return true; 3850342f1f8b0a402c5a7f8c5055db7f60a7808f1687Richard Smith } 38518cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return Success(V.getInt(), E); 385232fea9d18cc3658a1b01df5ca6f2ac302625c61dChris Lattner } 38531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 385451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith bool ZeroInitialization(const Expr *E) { return Success(0, E); } 3855f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith 38568cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne //===--------------------------------------------------------------------===// 38578cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne // Visitor Methods 38588cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne //===--------------------------------------------------------------------===// 3859f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner 38604c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner bool VisitIntegerLiteral(const IntegerLiteral *E) { 3861131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success(E->getValue(), E); 38624c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner } 38634c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner bool VisitCharacterLiteral(const CharacterLiteral *E) { 3864131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success(E->getValue(), E); 38654c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner } 3866043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman 3867043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman bool CheckReferencedDecl(const Expr *E, const Decl *D); 3868043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman bool VisitDeclRefExpr(const DeclRefExpr *E) { 38698cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne if (CheckReferencedDecl(E, E->getDecl())) 38708cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return true; 38718cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 38728cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return ExprEvaluatorBaseTy::VisitDeclRefExpr(E); 3873043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman } 3874043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman bool VisitMemberExpr(const MemberExpr *E) { 3875043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman if (CheckReferencedDecl(E, E->getMemberDecl())) { 3876c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith VisitIgnoredValue(E->getBase()); 3877043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman return true; 3878043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman } 38798cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 38808cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return ExprEvaluatorBaseTy::VisitMemberExpr(E); 3881043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman } 3882043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedman 38838cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitCallExpr(const CallExpr *E); 3884b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattner bool VisitBinaryOperator(const BinaryOperator *E); 38858ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor bool VisitOffsetOfExpr(const OffsetOfExpr *E); 3886b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattner bool VisitUnaryOperator(const UnaryOperator *E); 3887f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner 38888cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitCastExpr(const CastExpr* E); 3889f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E); 38900518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl 38913068d117951a8df54bae9db039b56201ab10962bAnders Carlsson bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { 3892131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success(E->getValue(), E); 38933068d117951a8df54bae9db039b56201ab10962bAnders Carlsson } 38941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3895f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith // Note, GNU defines __null as an integer, not a pointer. 38963f70456b8adb0405ef2a47d51f9fc2d5937ae8aeAnders Carlsson bool VisitGNUNullExpr(const GNUNullExpr *E) { 389751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return ZeroInitialization(E); 3898664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman } 3899664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman 390064b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) { 39010dfd848fa4c9664852ba8c929a8bd3fce93ddca2Sebastian Redl return Success(E->getValue(), E); 390264b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl } 390364b45f7e0d3167f040841ac2920aead7f080730dSebastian Redl 39046ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet bool VisitBinaryTypeTraitExpr(const BinaryTypeTraitExpr *E) { 39056ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet return Success(E->getValue(), E); 39066ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet } 39076ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet 390821ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) { 390921ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley return Success(E->getValue(), E); 391021ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley } 391121ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley 3912552622067dc45013d240f73952fece703f5e63bdJohn Wiegley bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) { 3913552622067dc45013d240f73952fece703f5e63bdJohn Wiegley return Success(E->getValue(), E); 3914552622067dc45013d240f73952fece703f5e63bdJohn Wiegley } 3915552622067dc45013d240f73952fece703f5e63bdJohn Wiegley 3916722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman bool VisitUnaryReal(const UnaryOperator *E); 3917664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman bool VisitUnaryImag(const UnaryOperator *E); 3918664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman 3919295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E); 3920ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor bool VisitSizeOfPackExpr(const SizeOfPackExpr *E); 3921cea8d966f826554f0679595e9371e314e8dbc1cfSebastian Redl 3922fcee0019b76f9f368f2b3d6d4048a98232593f29Chris Lattnerprivate: 39238b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck CharUnits GetAlignOfExpr(const Expr *E); 39248b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck CharUnits GetAlignOfType(QualType T); 39251bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith static QualType GetObjectType(APValue::LValueBase B); 39268cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool TryEvaluateBuiltinObjectSize(const CallExpr *E); 3927664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman // FIXME: Missing: array subscript of vector, member of vector 3928f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner}; 3929f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner} // end anonymous namespace 3930f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner 3931c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// EvaluateIntegerOrLValue - Evaluate an rvalue integral-typed expression, and 3932c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// produce either the integer value or a pointer. 3933c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// 3934c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// GCC has a heinous extension which folds casts between pointer types and 3935c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// pointer-sized integral types. We support this by allowing the evaluation of 3936c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// an integer rvalue to produce a pointer (represented as an lvalue) instead. 3937c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// Some simple arithmetic on such values is supported (they are treated much 3938c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// like char*). 3939f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool EvaluateIntegerOrLValue(const Expr *E, CCValue &Result, 394047a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith EvalInfo &Info) { 3941c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith assert(E->isRValue() && E->getType()->isIntegralOrEnumerationType()); 39428cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return IntExprEvaluator(Info, Result).Visit(E); 394369ab26a8623141f35e86817cfc6e0fbe7639a40fDaniel Dunbar} 394469ab26a8623141f35e86817cfc6e0fbe7639a40fDaniel Dunbar 3945f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info) { 394647a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith CCValue Val; 3947f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!EvaluateIntegerOrLValue(E, Val, Info)) 3948f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 3949f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!Val.isInt()) { 3950f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith // FIXME: It would be better to produce the diagnostic for casting 3951f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith // a pointer to an integer. 3952dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); 395330c37f4d2ee5811e85f692c22fb67d74ddc88079Daniel Dunbar return false; 3954f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 395530c37f4d2ee5811e85f692c22fb67d74ddc88079Daniel Dunbar Result = Val.getInt(); 395630c37f4d2ee5811e85f692c22fb67d74ddc88079Daniel Dunbar return true; 3957f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner} 3958f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner 3959f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// Check whether the given declaration can be directly converted to an integral 3960f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// rvalue. If not, no diagnostic is produced; there are other things we can 3961f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// try. 3962043097507f99b1156bfd8bad41e7d5166ae4b9b6Eli Friedmanbool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) { 39634c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner // Enums are integer constant exprs. 3964bfbdcd861a4364bfc21a9e5047bdbd56812d6693Abramo Bagnara if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) { 3965973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara // Check for signedness/width mismatches between E type and ECD value. 3966973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara bool SameSign = (ECD->getInitVal().isSigned() 3967973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara == E->getType()->isSignedIntegerOrEnumerationType()); 3968973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara bool SameWidth = (ECD->getInitVal().getBitWidth() 3969973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara == Info.Ctx.getIntWidth(E->getType())); 3970973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara if (SameSign && SameWidth) 3971973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara return Success(ECD->getInitVal(), E); 3972973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara else { 3973973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara // Get rid of mismatch (otherwise Success assertions will fail) 3974973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara // by computing a new value matching the type of E. 3975973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara llvm::APSInt Val = ECD->getInitVal(); 3976973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara if (!SameSign) 3977973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara Val.setIsSigned(!ECD->getInitVal().isSigned()); 3978973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara if (!SameWidth) 3979973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara Val = Val.extOrTrunc(Info.Ctx.getIntWidth(E->getType())); 3980973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara return Success(Val, E); 3981973c4fc0b0a88f9cea273b16f2082788a6e57d74Abramo Bagnara } 3982bfbdcd861a4364bfc21a9e5047bdbd56812d6693Abramo Bagnara } 39838cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return false; 39844c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner} 39854c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner 3986a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way 3987a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner/// as GCC. 3988a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattnerstatic int EvaluateBuiltinClassifyType(const CallExpr *E) { 3989a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner // The following enum mimics the values returned by GCC. 39907c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl // FIXME: Does GCC differ between lvalue and rvalue references here? 3991a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner enum gcc_type_class { 3992a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner no_type_class = -1, 3993a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner void_type_class, integer_type_class, char_type_class, 3994a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner enumeral_type_class, boolean_type_class, 3995a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner pointer_type_class, reference_type_class, offset_type_class, 3996a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner real_type_class, complex_type_class, 3997a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner function_type_class, method_type_class, 3998a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner record_type_class, union_type_class, 3999a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner array_type_class, string_type_class, 4000a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner lang_type_class 4001a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner }; 40021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 40031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // If no argument was supplied, default to "no_type_class". This isn't 4004a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner // ideal, however it is what gcc does. 4005a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner if (E->getNumArgs() == 0) 4006a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return no_type_class; 40071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4008a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner QualType ArgTy = E->getArg(0)->getType(); 4009a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner if (ArgTy->isVoidType()) 4010a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return void_type_class; 4011a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner else if (ArgTy->isEnumeralType()) 4012a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return enumeral_type_class; 4013a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner else if (ArgTy->isBooleanType()) 4014a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return boolean_type_class; 4015a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner else if (ArgTy->isCharType()) 4016a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return string_type_class; // gcc doesn't appear to use char_type_class 4017a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner else if (ArgTy->isIntegerType()) 4018a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return integer_type_class; 4019a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner else if (ArgTy->isPointerType()) 4020a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return pointer_type_class; 4021a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner else if (ArgTy->isReferenceType()) 4022a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return reference_type_class; 4023a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner else if (ArgTy->isRealType()) 4024a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return real_type_class; 4025a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner else if (ArgTy->isComplexType()) 4026a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return complex_type_class; 4027a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner else if (ArgTy->isFunctionType()) 4028a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return function_type_class; 4029fb87b89fc9eb103e19fb8e4b925c23f0bd091b99Douglas Gregor else if (ArgTy->isStructureOrClassType()) 4030a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return record_type_class; 4031a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner else if (ArgTy->isUnionType()) 4032a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return union_type_class; 4033a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner else if (ArgTy->isArrayType()) 4034a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return array_type_class; 4035a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner else if (ArgTy->isUnionType()) 4036a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner return union_type_class; 4037a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner else // FIXME: offset_type_class, method_type_class, & lang_type_class? 4038b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie llvm_unreachable("CallExpr::isBuiltinClassifyType(): unimplemented type"); 4039a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner} 4040a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner 404180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// EvaluateBuiltinConstantPForLValue - Determine the result of 404280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// __builtin_constant_p when applied to the given lvalue. 404380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// 404480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// An lvalue is only "constant" if it is a pointer or reference to the first 404580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// character of a string literal. 404680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smithtemplate<typename LValue> 404780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smithstatic bool EvaluateBuiltinConstantPForLValue(const LValue &LV) { 404880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith const Expr *E = LV.getLValueBase().dyn_cast<const Expr*>(); 404980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith return E && isa<StringLiteral>(E) && LV.getLValueOffset().isZero(); 405080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith} 405180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith 405280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// EvaluateBuiltinConstantP - Evaluate __builtin_constant_p as similarly to 405380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith/// GCC as we can manage. 405480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smithstatic bool EvaluateBuiltinConstantP(ASTContext &Ctx, const Expr *Arg) { 405580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith QualType ArgType = Arg->getType(); 405680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith 405780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // __builtin_constant_p always has one operand. The rules which gcc follows 405880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // are not precisely documented, but are as follows: 405980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // 406080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // - If the operand is of integral, floating, complex or enumeration type, 406180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // and can be folded to a known value of that type, it returns 1. 406280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // - If the operand and can be folded to a pointer to the first character 406380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // of a string literal (or such a pointer cast to an integral type), it 406480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // returns 1. 406580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // 406680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // Otherwise, it returns 0. 406780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // 406880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // FIXME: GCC also intends to return 1 for literals of aggregate types, but 406980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // its support for this does not currently work. 407080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith if (ArgType->isIntegralOrEnumerationType()) { 407180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith Expr::EvalResult Result; 407280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith if (!Arg->EvaluateAsRValue(Result, Ctx) || Result.HasSideEffects) 407380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith return false; 407480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith 407580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith APValue &V = Result.Val; 407680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith if (V.getKind() == APValue::Int) 407780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith return true; 407880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith 407980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith return EvaluateBuiltinConstantPForLValue(V); 408080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith } else if (ArgType->isFloatingType() || ArgType->isAnyComplexType()) { 408180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith return Arg->isEvaluatable(Ctx); 408280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith } else if (ArgType->isPointerType() || Arg->isGLValue()) { 408380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith LValue LV; 408480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith Expr::EvalStatus Status; 408580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith EvalInfo Info(Ctx, Status); 408680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith if ((Arg->isGLValue() ? EvaluateLValue(Arg, LV, Info) 408780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith : EvaluatePointer(Arg, LV, Info)) && 408880d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith !Status.HasSideEffects) 408980d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith return EvaluateBuiltinConstantPForLValue(LV); 409080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith } 409180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith 409280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith // Anything else isn't considered to be sufficiently constant. 409380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith return false; 409480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith} 409580d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith 409642c8f87eb60958170c46767273bf93e6c96125bfJohn McCall/// Retrieves the "underlying object type" of the given expression, 409742c8f87eb60958170c46767273bf93e6c96125bfJohn McCall/// as used by __builtin_object_size. 40981bf9a9e6a5bdc0de7939908855dcddf46b661800Richard SmithQualType IntExprEvaluator::GetObjectType(APValue::LValueBase B) { 40991bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { 41001bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith if (const VarDecl *VD = dyn_cast<VarDecl>(D)) 410142c8f87eb60958170c46767273bf93e6c96125bfJohn McCall return VD->getType(); 41021bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith } else if (const Expr *E = B.get<const Expr*>()) { 41031bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith if (isa<CompoundLiteralExpr>(E)) 41041bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith return E->getType(); 410542c8f87eb60958170c46767273bf93e6c96125bfJohn McCall } 410642c8f87eb60958170c46767273bf93e6c96125bfJohn McCall 410742c8f87eb60958170c46767273bf93e6c96125bfJohn McCall return QualType(); 410842c8f87eb60958170c46767273bf93e6c96125bfJohn McCall} 410942c8f87eb60958170c46767273bf93e6c96125bfJohn McCall 41108cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool IntExprEvaluator::TryEvaluateBuiltinObjectSize(const CallExpr *E) { 411142c8f87eb60958170c46767273bf93e6c96125bfJohn McCall // TODO: Perhaps we should let LLVM lower this? 411242c8f87eb60958170c46767273bf93e6c96125bfJohn McCall LValue Base; 411342c8f87eb60958170c46767273bf93e6c96125bfJohn McCall if (!EvaluatePointer(E->getArg(0), Base, Info)) 411442c8f87eb60958170c46767273bf93e6c96125bfJohn McCall return false; 411542c8f87eb60958170c46767273bf93e6c96125bfJohn McCall 411642c8f87eb60958170c46767273bf93e6c96125bfJohn McCall // If we can prove the base is null, lower to zero now. 41171bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith if (!Base.getLValueBase()) return Success(0, E); 411842c8f87eb60958170c46767273bf93e6c96125bfJohn McCall 41191bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith QualType T = GetObjectType(Base.getLValueBase()); 412042c8f87eb60958170c46767273bf93e6c96125bfJohn McCall if (T.isNull() || 412142c8f87eb60958170c46767273bf93e6c96125bfJohn McCall T->isIncompleteType() || 41221357869bc5983cdfbc986db1f3d18265bb34cb0eEli Friedman T->isFunctionType() || 412342c8f87eb60958170c46767273bf93e6c96125bfJohn McCall T->isVariablyModifiedType() || 412442c8f87eb60958170c46767273bf93e6c96125bfJohn McCall T->isDependentType()) 4125f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 412642c8f87eb60958170c46767273bf93e6c96125bfJohn McCall 412742c8f87eb60958170c46767273bf93e6c96125bfJohn McCall CharUnits Size = Info.Ctx.getTypeSizeInChars(T); 412842c8f87eb60958170c46767273bf93e6c96125bfJohn McCall CharUnits Offset = Base.getLValueOffset(); 412942c8f87eb60958170c46767273bf93e6c96125bfJohn McCall 413042c8f87eb60958170c46767273bf93e6c96125bfJohn McCall if (!Offset.isNegative() && Offset <= Size) 413142c8f87eb60958170c46767273bf93e6c96125bfJohn McCall Size -= Offset; 413242c8f87eb60958170c46767273bf93e6c96125bfJohn McCall else 413342c8f87eb60958170c46767273bf93e6c96125bfJohn McCall Size = CharUnits::Zero(); 41344f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck return Success(Size, E); 413542c8f87eb60958170c46767273bf93e6c96125bfJohn McCall} 413642c8f87eb60958170c46767273bf93e6c96125bfJohn McCall 41378cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { 4138180f47959a066795cc0f409433023af448bb0328Richard Smith switch (E->isBuiltinCall()) { 4139019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner default: 41408cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return ExprEvaluatorBaseTy::VisitCallExpr(E); 414164eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump 414264eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump case Builtin::BI__builtin_object_size: { 414342c8f87eb60958170c46767273bf93e6c96125bfJohn McCall if (TryEvaluateBuiltinObjectSize(E)) 414442c8f87eb60958170c46767273bf93e6c96125bfJohn McCall return true; 414564eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump 4146b2aaf51ed73a4774322a39a0dd59d0fe7e3258c0Eric Christopher // If evaluating the argument has side-effects we can't determine 4147b2aaf51ed73a4774322a39a0dd59d0fe7e3258c0Eric Christopher // the size of the object and lower it to unknown now. 4148393c247fe025ccb5f914e37e948192ea86faef8cFariborz Jahanian if (E->getArg(0)->HasSideEffects(Info.Ctx)) { 4149a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith if (E->getArg(1)->EvaluateKnownConstInt(Info.Ctx).getZExtValue() <= 1) 4150cf184655319cf7a5b811067cff9d26a5741fd161Chris Lattner return Success(-1ULL, E); 415164eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump return Success(0, E); 415264eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump } 4153c4c9045dabfc0f0d37dea1b3eb2992654d5b2db1Mike Stump 4154f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 415564eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump } 415664eda9e50b593f935c95bd1edc98c4bfda03f601Mike Stump 4157019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner case Builtin::BI__builtin_classify_type: 4158131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success(EvaluateBuiltinClassifyType(E), E); 41591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 416080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith case Builtin::BI__builtin_constant_p: 416180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith return Success(EvaluateBuiltinConstantP(Info.Ctx, E->getArg(0)), E); 4162e052d46f4db91f9ba572859ffc984e85cbf5d5ffRichard Smith 416321fb98ee003e992b0c4e204d98a19e0ef544cae3Chris Lattner case Builtin::BI__builtin_eh_return_data_regno: { 4164a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith int Operand = E->getArg(0)->EvaluateKnownConstInt(Info.Ctx).getZExtValue(); 4165bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor Operand = Info.Ctx.getTargetInfo().getEHDataRegisterNumber(Operand); 416621fb98ee003e992b0c4e204d98a19e0ef544cae3Chris Lattner return Success(Operand, E); 416721fb98ee003e992b0c4e204d98a19e0ef544cae3Chris Lattner } 4168c4a2638b5ef3e2d35d872614ceb655a7a22c58beEli Friedman 4169c4a2638b5ef3e2d35d872614ceb655a7a22c58beEli Friedman case Builtin::BI__builtin_expect: 4170c4a2638b5ef3e2d35d872614ceb655a7a22c58beEli Friedman return Visit(E->getArg(0)); 417140b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith 41725726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor case Builtin::BIstrlen: 417340b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith // A call to strlen is not a constant expression. 417440b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith if (Info.getLangOpts().CPlusPlus0x) 417540b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_invalid_function) 417640b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith << /*isConstexpr*/0 << /*isConstructor*/0 << "'strlen'"; 417740b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith else 417840b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith Info.CCEDiag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); 417940b993a826728214c869ee4fbc9d296a2e1e1f71Richard Smith // Fall through. 41805726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor case Builtin::BI__builtin_strlen: 41815726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor // As an extension, we support strlen() and __builtin_strlen() as constant 41825726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor // expressions when the argument is a string literal. 41838cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne if (const StringLiteral *S 41845726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor = dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenImpCasts())) { 41855726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor // The string literal may have embedded null characters. Find the first 41865726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor // one and truncate there. 41875f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef Str = S->getString(); 41885f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef::size_type Pos = Str.find(0); 41895f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner if (Pos != StringRef::npos) 41905726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor Str = Str.substr(0, Pos); 41915726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor 41925726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor return Success(Str.size(), E); 41935726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor } 41945726d405e71f11feaaf0c8f518abe26e909537a4Douglas Gregor 4195f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 4196454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman 4197454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman case Builtin::BI__atomic_is_lock_free: { 4198454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman APSInt SizeVal; 4199454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman if (!EvaluateInteger(E->getArg(0), SizeVal, Info)) 4200454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman return false; 4201454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman 4202454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman // For __atomic_is_lock_free(sizeof(_Atomic(T))), if the size is a power 4203454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman // of two less than the maximum inline atomic width, we know it is 4204454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman // lock-free. If the size isn't a power of two, or greater than the 4205454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman // maximum alignment where we promote atomics, we know it is not lock-free 4206454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman // (at least not in the sense of atomic_is_lock_free). Otherwise, 4207454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman // the answer can only be determined at runtime; for example, 16-byte 4208454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman // atomics have lock-free implementations on some, but not all, 4209454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman // x86-64 processors. 4210454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman 4211454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman // Check power-of-two. 4212454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman CharUnits Size = CharUnits::fromQuantity(SizeVal.getZExtValue()); 4213454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman if (!Size.isPowerOfTwo()) 4214454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman#if 0 4215454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman // FIXME: Suppress this folding until the ABI for the promotion width 4216454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman // settles. 4217454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman return Success(0, E); 4218454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman#else 4219f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 4220454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman#endif 4221454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman 4222454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman#if 0 4223454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman // Check against promotion width. 4224454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman // FIXME: Suppress this folding until the ABI for the promotion width 4225454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman // settles. 4226454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman unsigned PromoteWidthBits = 4227454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman Info.Ctx.getTargetInfo().getMaxAtomicPromoteWidth(); 4228454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman if (Size > Info.Ctx.toCharUnitsFromBits(PromoteWidthBits)) 4229454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman return Success(0, E); 4230454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman#endif 4231454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman 4232454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman // Check against inlining width. 4233454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman unsigned InlineWidthBits = 4234454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth(); 4235454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman if (Size <= Info.Ctx.toCharUnitsFromBits(InlineWidthBits)) 4236454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman return Success(1, E); 4237454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman 4238f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 4239454b57ac42c9ce0bed9b7a99c2ed5a18fbcd286bEli Friedman } 4240019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner } 42414c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner} 4242f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner 4243625b80755b603d28f36fb4212c81484d87ad08d3Richard Smithstatic bool HasSameBase(const LValue &A, const LValue &B) { 4244625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith if (!A.getLValueBase()) 4245625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith return !B.getLValueBase(); 4246625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith if (!B.getLValueBase()) 4247625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith return false; 4248625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith 42491bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith if (A.getLValueBase().getOpaqueValue() != 42501bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith B.getLValueBase().getOpaqueValue()) { 4251625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith const Decl *ADecl = GetLValueBaseDecl(A); 4252625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith if (!ADecl) 4253625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith return false; 4254625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith const Decl *BDecl = GetLValueBaseDecl(B); 42559a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith if (!BDecl || ADecl->getCanonicalDecl() != BDecl->getCanonicalDecl()) 4256625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith return false; 4257625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith } 4258625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith 4259625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith return IsGlobalLValue(A.getLValueBase()) || 4260177dce777596e68d111d6d3e6046f3ddfc96bd07Richard Smith A.getLValueFrame() == B.getLValueFrame(); 4261625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith} 4262625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith 42637b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith/// Perform the given integer operation, which is known to need at most BitWidth 42647b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith/// bits, and check for overflow in the original type (if that type was not an 42657b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith/// unsigned type). 42667b48a2986345480241f3b8209f71bb21b0530b4fRichard Smithtemplate<typename Operation> 42677b48a2986345480241f3b8209f71bb21b0530b4fRichard Smithstatic APSInt CheckedIntArithmetic(EvalInfo &Info, const Expr *E, 42687b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith const APSInt &LHS, const APSInt &RHS, 42697b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith unsigned BitWidth, Operation Op) { 42707b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith if (LHS.isUnsigned()) 42717b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith return Op(LHS, RHS); 42727b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith 42737b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith APSInt Value(Op(LHS.extend(BitWidth), RHS.extend(BitWidth)), false); 42747b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith APSInt Result = Value.trunc(LHS.getBitWidth()); 42757b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith if (Result.extend(BitWidth) != Value) 42767b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith HandleOverflow(Info, E, Value, E->getType()); 42777b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith return Result; 42787b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith} 42797b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith 4280b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattnerbool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { 4281c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith if (E->isAssignmentOp()) 4282f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 4283c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 42842de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (E->getOpcode() == BO_Comma) { 42858327fad71da34492d82c532f42a58cb4baff81a3Richard Smith VisitIgnoredValue(E->getLHS()); 42868327fad71da34492d82c532f42a58cb4baff81a3Richard Smith return Visit(E->getRHS()); 4287a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman } 4288a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman 4289a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman if (E->isLogicalOp()) { 4290a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman // These need to be handled specially because the operands aren't 4291a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman // necessarily integral 4292fcb4d09531abbf2a5cf398c2f946fb3bc2875f64Anders Carlsson bool lhsResult, rhsResult; 42931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4294c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith if (EvaluateAsBooleanCondition(E->getLHS(), lhsResult, Info)) { 429551fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson // We were able to evaluate the LHS, see if we can get away with not 429651fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson // evaluating the RHS: 0 && X -> 0, 1 || X -> 1 42972de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (lhsResult == (E->getOpcode() == BO_LOr)) 42983f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar return Success(lhsResult, E); 4299a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman 4300c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith if (EvaluateAsBooleanCondition(E->getRHS(), rhsResult, Info)) { 43012de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (E->getOpcode() == BO_LOr) 4302131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success(lhsResult || rhsResult, E); 43034bbc0e05a714d4ee4918a92a4a7049dd6ef33ad0Anders Carlsson else 4304131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success(lhsResult && rhsResult, E); 43054bbc0e05a714d4ee4918a92a4a7049dd6ef33ad0Anders Carlsson } 43064bbc0e05a714d4ee4918a92a4a7049dd6ef33ad0Anders Carlsson } else { 4307f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith // FIXME: If both evaluations fail, we should produce the diagnostic from 4308f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith // the LHS. If the LHS is non-constant and the RHS is unevaluatable, it's 4309f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith // less clear how to diagnose this. 4310c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith if (EvaluateAsBooleanCondition(E->getRHS(), rhsResult, Info)) { 43114bbc0e05a714d4ee4918a92a4a7049dd6ef33ad0Anders Carlsson // We can't evaluate the LHS; however, sometimes the result 43124bbc0e05a714d4ee4918a92a4a7049dd6ef33ad0Anders Carlsson // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. 4313f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (rhsResult == (E->getOpcode() == BO_LOr)) { 4314131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar // Since we weren't able to evaluate the left hand side, it 4315fcb4d09531abbf2a5cf398c2f946fb3bc2875f64Anders Carlsson // must have had side effects. 43161e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith Info.EvalStatus.HasSideEffects = true; 4317131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar 4318131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success(rhsResult, E); 43194bbc0e05a714d4ee4918a92a4a7049dd6ef33ad0Anders Carlsson } 43204bbc0e05a714d4ee4918a92a4a7049dd6ef33ad0Anders Carlsson } 4321a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman } 4322a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman 4323a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman return false; 4324c8cc9ccc7b87a7ed1749b074f6b670bcec49abc1Chris Lattner } 432554176fdb044312b4b77c3da6682d3575b3728d30Chris Lattner 4326286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson QualType LHSTy = E->getLHS()->getType(); 4327286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson QualType RHSTy = E->getRHS()->getType(); 43284087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar 43294087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar if (LHSTy->isAnyComplexType()) { 43304087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar assert(RHSTy->isAnyComplexType() && "Invalid comparison"); 4331f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall ComplexValue LHS, RHS; 43324087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar 4333745f5147e065900267c85a5568785a1991d4838fRichard Smith bool LHSOK = EvaluateComplex(E->getLHS(), LHS, Info); 4334745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!LHSOK && !Info.keepEvaluatingAfterFailure()) 43354087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar return false; 43364087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar 4337745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK) 43384087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar return false; 43394087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar 43404087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar if (LHS.isComplexFloat()) { 43411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump APFloat::cmpResult CR_r = 43424087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal()); 43431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump APFloat::cmpResult CR_i = 43444087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag()); 43454087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar 43462de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (E->getOpcode() == BO_EQ) 4347131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success((CR_r == APFloat::cmpEqual && 4348131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar CR_i == APFloat::cmpEqual), E); 4349131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar else { 43502de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall assert(E->getOpcode() == BO_NE && 4351131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar "Invalid complex comparison."); 43521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return Success(((CR_r == APFloat::cmpGreaterThan || 4353fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang CR_r == APFloat::cmpLessThan || 4354fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang CR_r == APFloat::cmpUnordered) || 43551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump (CR_i == APFloat::cmpGreaterThan || 4356fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang CR_i == APFloat::cmpLessThan || 4357fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang CR_i == APFloat::cmpUnordered)), E); 4358131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar } 43594087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar } else { 43602de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (E->getOpcode() == BO_EQ) 4361131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() && 4362131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar LHS.getComplexIntImag() == RHS.getComplexIntImag()), E); 4363131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar else { 43642de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall assert(E->getOpcode() == BO_NE && 4365131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar "Invalid compex comparison."); 4366131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() || 4367131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar LHS.getComplexIntImag() != RHS.getComplexIntImag()), E); 4368131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar } 43694087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar } 43704087e24f73d05d96ac2d259679751d054d3ddfbcDaniel Dunbar } 43711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4372286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson if (LHSTy->isRealFloatingType() && 4373286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson RHSTy->isRealFloatingType()) { 4374286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson APFloat RHS(0.0), LHS(0.0); 43751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4376745f5147e065900267c85a5568785a1991d4838fRichard Smith bool LHSOK = EvaluateFloat(E->getRHS(), RHS, Info); 4377745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!LHSOK && !Info.keepEvaluatingAfterFailure()) 4378286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson return false; 43791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4380745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!EvaluateFloat(E->getLHS(), LHS, Info) || !LHSOK) 4381286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson return false; 43821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4383286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson APFloat::cmpResult CR = LHS.compare(RHS); 4384529569e68d10b0fd3750fd2124faf742249b846bAnders Carlsson 4385286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson switch (E->getOpcode()) { 4386286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson default: 4387b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie llvm_unreachable("Invalid binary operator!"); 43882de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_LT: 4389131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success(CR == APFloat::cmpLessThan, E); 43902de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_GT: 4391131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success(CR == APFloat::cmpGreaterThan, E); 43922de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_LE: 4393131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E); 43942de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_GE: 43951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual, 4396131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar E); 43972de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_EQ: 4398131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success(CR == APFloat::cmpEqual, E); 43992de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_NE: 44001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return Success(CR == APFloat::cmpGreaterThan 4401fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang || CR == APFloat::cmpLessThan 4402fc39dc4c7886723310419b1869cf651ca55b7af4Mon P Wang || CR == APFloat::cmpUnordered, E); 4403286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson } 4404286f85e791dda3634fee7f6c67f0ed92296c028fAnders Carlsson } 44051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4406ad02d7debd03ff275ac8ea27891a4ecccdb78068Eli Friedman if (LHSTy->isPointerType() && RHSTy->isPointerType()) { 4407625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith if (E->getOpcode() == BO_Sub || E->isComparisonOp()) { 4408745f5147e065900267c85a5568785a1991d4838fRichard Smith LValue LHSValue, RHSValue; 4409745f5147e065900267c85a5568785a1991d4838fRichard Smith 4410745f5147e065900267c85a5568785a1991d4838fRichard Smith bool LHSOK = EvaluatePointer(E->getLHS(), LHSValue, Info); 4411745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!LHSOK && Info.keepEvaluatingAfterFailure()) 44123068d117951a8df54bae9db039b56201ab10962bAnders Carlsson return false; 4413a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman 4414745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!EvaluatePointer(E->getRHS(), RHSValue, Info) || !LHSOK) 44153068d117951a8df54bae9db039b56201ab10962bAnders Carlsson return false; 4416a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman 4417625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith // Reject differing bases from the normal codepath; we special-case 4418625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith // comparisons to null. 4419625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith if (!HasSameBase(LHSValue, RHSValue)) { 442065639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman if (E->getOpcode() == BO_Sub) { 442165639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman // Handle &&A - &&B. 442265639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman if (!LHSValue.Offset.isZero() || !RHSValue.Offset.isZero()) 442365639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman return false; 442465639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman const Expr *LHSExpr = LHSValue.Base.dyn_cast<const Expr*>(); 442565639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman const Expr *RHSExpr = LHSValue.Base.dyn_cast<const Expr*>(); 442665639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman if (!LHSExpr || !RHSExpr) 442765639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman return false; 442865639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); 442965639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); 443065639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman if (!LHSAddrExpr || !RHSAddrExpr) 443165639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman return false; 44325930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman // Make sure both labels come from the same function. 44335930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman if (LHSAddrExpr->getLabel()->getDeclContext() != 44345930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman RHSAddrExpr->getLabel()->getDeclContext()) 44355930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman return false; 443665639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman Result = CCValue(LHSAddrExpr, RHSAddrExpr); 443765639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman return true; 443865639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman } 44399e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith // Inequalities and subtractions between unrelated pointers have 44409e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith // unspecified or undefined behavior. 44415bc86103767c2abcbfdd6518e0ccbbbb6aa59e0fEli Friedman if (!E->isEqualityOp()) 4442f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 4443ffbda40a1fb7169591dc01771f3511178a2f727cEli Friedman // A constant address may compare equal to the address of a symbol. 4444ffbda40a1fb7169591dc01771f3511178a2f727cEli Friedman // The one exception is that address of an object cannot compare equal 4445c45061bd0c0fdad4df8eea7e9e5af186d11427e5Eli Friedman // to a null pointer constant. 4446ffbda40a1fb7169591dc01771f3511178a2f727cEli Friedman if ((!LHSValue.Base && !LHSValue.Offset.isZero()) || 4447ffbda40a1fb7169591dc01771f3511178a2f727cEli Friedman (!RHSValue.Base && !RHSValue.Offset.isZero())) 4448f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 44499e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith // It's implementation-defined whether distinct literals will have 4450b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith // distinct addresses. In clang, the result of such a comparison is 4451b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith // unspecified, so it is not a constant expression. However, we do know 4452b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith // that the address of a literal will be non-null. 445374f4634781cee06e28eb741bda5d0f936fdd1948Richard Smith if ((IsLiteralLValue(LHSValue) || IsLiteralLValue(RHSValue)) && 445474f4634781cee06e28eb741bda5d0f936fdd1948Richard Smith LHSValue.Base && RHSValue.Base) 4455f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 44569e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith // We can't tell whether weak symbols will end up pointing to the same 44579e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith // object. 44589e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith if (IsWeakLValue(LHSValue) || IsWeakLValue(RHSValue)) 4459f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 44609e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith // Pointers with different bases cannot represent the same object. 4461c45061bd0c0fdad4df8eea7e9e5af186d11427e5Eli Friedman // (Note that clang defaults to -fmerge-all-constants, which can 4462c45061bd0c0fdad4df8eea7e9e5af186d11427e5Eli Friedman // lead to inconsistent results for comparisons involving the address 4463c45061bd0c0fdad4df8eea7e9e5af186d11427e5Eli Friedman // of a constant; this generally doesn't matter in practice.) 44649e36b533af1b2fa9f32c4372c4081abdd86f47e0Richard Smith return Success(E->getOpcode() == BO_NE, E); 44655bc86103767c2abcbfdd6518e0ccbbbb6aa59e0fEli Friedman } 4466a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman 446715efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith const CharUnits &LHSOffset = LHSValue.getLValueOffset(); 446815efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith const CharUnits &RHSOffset = RHSValue.getLValueOffset(); 446915efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith 4470f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator(); 4471f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator(); 4472f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 44732de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (E->getOpcode() == BO_Sub) { 4474f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // C++11 [expr.add]p6: 4475f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // Unless both pointers point to elements of the same array object, or 4476f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // one past the last element of the array object, the behavior is 4477f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // undefined. 4478f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && 4479f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith !AreElementsOfSameArray(getType(LHSValue.Base), 4480f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith LHSDesignator, RHSDesignator)) 4481f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith CCEDiag(E, diag::note_constexpr_pointer_subtraction_not_same_array); 4482f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 44834992bdde387c5f033bb450a716eaabc0fda52688Chris Lattner QualType Type = E->getLHS()->getType(); 44844992bdde387c5f033bb450a716eaabc0fda52688Chris Lattner QualType ElementType = Type->getAs<PointerType>()->getPointeeType(); 44853068d117951a8df54bae9db039b56201ab10962bAnders Carlsson 4486180f47959a066795cc0f409433023af448bb0328Richard Smith CharUnits ElementSize; 4487180f47959a066795cc0f409433023af448bb0328Richard Smith if (!HandleSizeof(Info, ElementType, ElementSize)) 4488180f47959a066795cc0f409433023af448bb0328Richard Smith return false; 4489a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman 449015efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith // FIXME: LLVM and GCC both compute LHSOffset - RHSOffset at runtime, 449115efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith // and produce incorrect results when it overflows. Such behavior 449215efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith // appears to be non-conforming, but is common, so perhaps we should 449315efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith // assume the standard intended for such cases to be undefined behavior 449415efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith // and check for them. 449515efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith 449615efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith // Compute (LHSOffset - RHSOffset) / Size carefully, checking for 449715efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith // overflow in the final conversion to ptrdiff_t. 449815efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith APSInt LHS( 449915efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith llvm::APInt(65, (int64_t)LHSOffset.getQuantity(), true), false); 450015efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith APSInt RHS( 450115efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith llvm::APInt(65, (int64_t)RHSOffset.getQuantity(), true), false); 450215efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith APSInt ElemSize( 450315efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith llvm::APInt(65, (int64_t)ElementSize.getQuantity(), true), false); 450415efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith APSInt TrueResult = (LHS - RHS) / ElemSize; 450515efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith APSInt Result = TrueResult.trunc(Info.Ctx.getIntWidth(E->getType())); 450615efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith 450715efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith if (Result.extend(65) != TrueResult) 450815efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith HandleOverflow(Info, E, TrueResult, E->getType()); 450915efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith return Success(Result, E); 4510ad02d7debd03ff275ac8ea27891a4ecccdb78068Eli Friedman } 4511625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith 451282f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith // C++11 [expr.rel]p3: 451382f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith // Pointers to void (after pointer conversions) can be compared, with a 451482f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith // result defined as follows: If both pointers represent the same 451582f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith // address or are both the null pointer value, the result is true if the 451682f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith // operator is <= or >= and false otherwise; otherwise the result is 451782f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith // unspecified. 451882f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith // We interpret this as applying to pointers to *cv* void. 451982f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith if (LHSTy->isVoidPointerType() && LHSOffset != RHSOffset && 4520f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith E->isRelationalOp()) 452182f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith CCEDiag(E, diag::note_constexpr_void_comparison); 452282f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith 4523f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // C++11 [expr.rel]p2: 4524f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // - If two pointers point to non-static data members of the same object, 4525f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // or to subobjects or array elements fo such members, recursively, the 4526f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // pointer to the later declared member compares greater provided the 4527f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // two members have the same access control and provided their class is 4528f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // not a union. 4529f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // [...] 4530f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // - Otherwise pointer comparisons are unspecified. 4531f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && 4532f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith E->isRelationalOp()) { 4533f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith bool WasArrayIndex; 4534f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith unsigned Mismatch = 4535f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith FindDesignatorMismatch(getType(LHSValue.Base), LHSDesignator, 4536f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith RHSDesignator, WasArrayIndex); 4537f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // At the point where the designators diverge, the comparison has a 4538f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // specified value if: 4539f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // - we are comparing array indices 4540f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // - we are comparing fields of a union, or fields with the same access 4541f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // Otherwise, the result is unspecified and thus the comparison is not a 4542f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // constant expression. 4543f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (!WasArrayIndex && Mismatch < LHSDesignator.Entries.size() && 4544f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith Mismatch < RHSDesignator.Entries.size()) { 4545f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith const FieldDecl *LF = getAsField(LHSDesignator.Entries[Mismatch]); 4546f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith const FieldDecl *RF = getAsField(RHSDesignator.Entries[Mismatch]); 4547f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith if (!LF && !RF) 4548f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith CCEDiag(E, diag::note_constexpr_pointer_comparison_base_classes); 4549f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith else if (!LF) 4550f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) 4551f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith << getAsBaseClass(LHSDesignator.Entries[Mismatch]) 4552f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith << RF->getParent() << RF; 4553f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith else if (!RF) 4554f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) 4555f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith << getAsBaseClass(RHSDesignator.Entries[Mismatch]) 4556f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith << LF->getParent() << LF; 4557f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith else if (!LF->getParent()->isUnion() && 4558f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith LF->getAccess() != RF->getAccess()) 4559f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith CCEDiag(E, diag::note_constexpr_pointer_comparison_differing_access) 4560f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith << LF << LF->getAccess() << RF << RF->getAccess() 4561f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith << LF->getParent(); 4562f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith } 4563f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith } 4564f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 4565625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith switch (E->getOpcode()) { 4566625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith default: llvm_unreachable("missing comparison operator"); 4567625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith case BO_LT: return Success(LHSOffset < RHSOffset, E); 4568625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith case BO_GT: return Success(LHSOffset > RHSOffset, E); 4569625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith case BO_LE: return Success(LHSOffset <= RHSOffset, E); 4570625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith case BO_GE: return Success(LHSOffset >= RHSOffset, E); 4571625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith case BO_EQ: return Success(LHSOffset == RHSOffset, E); 4572625b80755b603d28f36fb4212c81484d87ad08d3Richard Smith case BO_NE: return Success(LHSOffset != RHSOffset, E); 4573ad02d7debd03ff275ac8ea27891a4ecccdb78068Eli Friedman } 45743068d117951a8df54bae9db039b56201ab10962bAnders Carlsson } 45753068d117951a8df54bae9db039b56201ab10962bAnders Carlsson } 4576b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith 4577b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith if (LHSTy->isMemberPointerType()) { 4578b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith assert(E->isEqualityOp() && "unexpected member pointer operation"); 4579b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith assert(RHSTy->isMemberPointerType() && "invalid comparison"); 4580b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith 4581b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith MemberPtr LHSValue, RHSValue; 4582b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith 4583b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith bool LHSOK = EvaluateMemberPointer(E->getLHS(), LHSValue, Info); 4584b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith if (!LHSOK && Info.keepEvaluatingAfterFailure()) 4585b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith return false; 4586b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith 4587b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith if (!EvaluateMemberPointer(E->getRHS(), RHSValue, Info) || !LHSOK) 4588b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith return false; 4589b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith 4590b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith // C++11 [expr.eq]p2: 4591b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith // If both operands are null, they compare equal. Otherwise if only one is 4592b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith // null, they compare unequal. 4593b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith if (!LHSValue.getDecl() || !RHSValue.getDecl()) { 4594b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith bool Equal = !LHSValue.getDecl() && !RHSValue.getDecl(); 4595b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith return Success(E->getOpcode() == BO_EQ ? Equal : !Equal, E); 4596b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith } 4597b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith 4598b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith // Otherwise if either is a pointer to a virtual member function, the 4599b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith // result is unspecified. 4600b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(LHSValue.getDecl())) 4601b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith if (MD->isVirtual()) 4602b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; 4603b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(RHSValue.getDecl())) 4604b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith if (MD->isVirtual()) 4605b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; 4606b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith 4607b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith // Otherwise they compare equal if and only if they would refer to the 4608b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith // same member of the same most derived object or the same subobject if 4609b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith // they were dereferenced with a hypothetical object of the associated 4610b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith // class type. 4611b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith bool Equal = LHSValue == RHSValue; 4612b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith return Success(E->getOpcode() == BO_EQ ? Equal : !Equal, E); 4613b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith } 4614b02e4629f78a0c0c0adf9d66b644e5932a781c7eRichard Smith 461526f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith if (LHSTy->isNullPtrType()) { 461626f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith assert(E->isComparisonOp() && "unexpected nullptr operation"); 461726f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith assert(RHSTy->isNullPtrType() && "missing pointer conversion"); 461826f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith // C++11 [expr.rel]p4, [expr.eq]p3: If two operands of type std::nullptr_t 461926f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith // are compared, the result is true of the operator is <=, >= or ==, and 462026f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith // false otherwise. 462126f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith BinaryOperator::Opcode Opcode = E->getOpcode(); 462226f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith return Success(Opcode == BO_EQ || Opcode == BO_LE || Opcode == BO_GE, E); 462326f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith } 462426f2cac83eeb4317738d74b9e567d3d58aa04ed9Richard Smith 46252ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor if (!LHSTy->isIntegralOrEnumerationType() || 46262ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor !RHSTy->isIntegralOrEnumerationType()) { 4627e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith // We can't continue from here for non-integral types. 4628e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return ExprEvaluatorBaseTy::VisitBinaryOperator(E); 4629a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman } 4630a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman 4631a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman // The LHS of a constant expr is always evaluated and needed. 463247a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith CCValue LHSVal; 4633745f5147e065900267c85a5568785a1991d4838fRichard Smith 4634745f5147e065900267c85a5568785a1991d4838fRichard Smith bool LHSOK = EvaluateIntegerOrLValue(E->getLHS(), LHSVal, Info); 4635745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!LHSOK && !Info.keepEvaluatingAfterFailure()) 4636f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 4637d9f4bcda18bfbf79341edd9d381d4b6a3cffe655Eli Friedman 4638745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!Visit(E->getRHS()) || !LHSOK) 463930c37f4d2ee5811e85f692c22fb67d74ddc88079Daniel Dunbar return false; 4640745f5147e065900267c85a5568785a1991d4838fRichard Smith 464147a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith CCValue &RHSVal = Result; 464242edd0d32a729d2735a6fb152ba6bf349bf0a169Eli Friedman 464342edd0d32a729d2735a6fb152ba6bf349bf0a169Eli Friedman // Handle cases like (unsigned long)&a + 4. 4644c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith if (E->isAdditiveOp() && LHSVal.isLValue() && RHSVal.isInt()) { 4645a73058324197b7bdfd19307965954f626e26199dKen Dyck CharUnits AdditionalOffset = CharUnits::fromQuantity( 4646a73058324197b7bdfd19307965954f626e26199dKen Dyck RHSVal.getInt().getZExtValue()); 46472de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (E->getOpcode() == BO_Add) 464847a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith LHSVal.getLValueOffset() += AdditionalOffset; 464942edd0d32a729d2735a6fb152ba6bf349bf0a169Eli Friedman else 465047a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith LHSVal.getLValueOffset() -= AdditionalOffset; 465147a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith Result = LHSVal; 465242edd0d32a729d2735a6fb152ba6bf349bf0a169Eli Friedman return true; 465342edd0d32a729d2735a6fb152ba6bf349bf0a169Eli Friedman } 465442edd0d32a729d2735a6fb152ba6bf349bf0a169Eli Friedman 465542edd0d32a729d2735a6fb152ba6bf349bf0a169Eli Friedman // Handle cases like 4 + (unsigned long)&a 46562de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (E->getOpcode() == BO_Add && 4657c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith RHSVal.isLValue() && LHSVal.isInt()) { 465847a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith RHSVal.getLValueOffset() += CharUnits::fromQuantity( 465947a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith LHSVal.getInt().getZExtValue()); 466047a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith // Note that RHSVal is Result. 466142edd0d32a729d2735a6fb152ba6bf349bf0a169Eli Friedman return true; 466242edd0d32a729d2735a6fb152ba6bf349bf0a169Eli Friedman } 466342edd0d32a729d2735a6fb152ba6bf349bf0a169Eli Friedman 466465639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman if (E->getOpcode() == BO_Sub && LHSVal.isLValue() && RHSVal.isLValue()) { 466565639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman // Handle (intptr_t)&&A - (intptr_t)&&B. 466665639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman if (!LHSVal.getLValueOffset().isZero() || 466765639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman !RHSVal.getLValueOffset().isZero()) 466865639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman return false; 466965639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman const Expr *LHSExpr = LHSVal.getLValueBase().dyn_cast<const Expr*>(); 467065639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman const Expr *RHSExpr = RHSVal.getLValueBase().dyn_cast<const Expr*>(); 467165639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman if (!LHSExpr || !RHSExpr) 467265639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman return false; 467365639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); 467465639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); 467565639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman if (!LHSAddrExpr || !RHSAddrExpr) 467665639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman return false; 46775930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman // Make sure both labels come from the same function. 46785930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman if (LHSAddrExpr->getLabel()->getDeclContext() != 46795930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman RHSAddrExpr->getLabel()->getDeclContext()) 46805930a4c5224eea3b0558655f7f8c9ea027ef573eEli Friedman return false; 468165639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman Result = CCValue(LHSAddrExpr, RHSAddrExpr); 468265639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman return true; 468365639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman } 468465639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman 468542edd0d32a729d2735a6fb152ba6bf349bf0a169Eli Friedman // All the following cases expect both operands to be an integer 4686c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith if (!LHSVal.isInt() || !RHSVal.isInt()) 4687f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 4688a6afa768aa7bd3102a2807aa720917e4a1771e4eEli Friedman 4689c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith APSInt &LHS = LHSVal.getInt(); 4690c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith APSInt &RHS = RHSVal.getInt(); 469142edd0d32a729d2735a6fb152ba6bf349bf0a169Eli Friedman 4692a25ae3d68d84d2b89907f998df6a396549589da5Anders Carlsson switch (E->getOpcode()) { 469332fea9d18cc3658a1b01df5ca6f2ac302625c61dChris Lattner default: 4694f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 46957b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith case BO_Mul: 46967b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith return Success(CheckedIntArithmetic(Info, E, LHS, RHS, 46977b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith LHS.getBitWidth() * 2, 46987b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith std::multiplies<APSInt>()), E); 46997b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith case BO_Add: 47007b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith return Success(CheckedIntArithmetic(Info, E, LHS, RHS, 47017b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith LHS.getBitWidth() + 1, 47027b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith std::plus<APSInt>()), E); 47037b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith case BO_Sub: 47047b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith return Success(CheckedIntArithmetic(Info, E, LHS, RHS, 47057b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith LHS.getBitWidth() + 1, 47067b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith std::minus<APSInt>()), E); 4707c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case BO_And: return Success(LHS & RHS, E); 4708c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case BO_Xor: return Success(LHS ^ RHS, E); 4709c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case BO_Or: return Success(LHS | RHS, E); 47102de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Div: 47113df61308ddfbdba0897b762a129b5a38750c87d0Richard Smith case BO_Rem: 471254176fdb044312b4b77c3da6682d3575b3728d30Chris Lattner if (RHS == 0) 4713f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E, diag::note_expr_divide_by_zero); 47143df61308ddfbdba0897b762a129b5a38750c87d0Richard Smith // Check for overflow case: INT_MIN / -1 or INT_MIN % -1. The latter is not 47153df61308ddfbdba0897b762a129b5a38750c87d0Richard Smith // actually undefined behavior in C++11 due to a language defect. 4716789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith if (RHS.isNegative() && RHS.isAllOnesValue() && 4717789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith LHS.isSigned() && LHS.isMinSignedValue()) 4718789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith HandleOverflow(Info, E, -LHS.extend(LHS.getBitWidth() + 1), E->getType()); 47193df61308ddfbdba0897b762a129b5a38750c87d0Richard Smith return Success(E->getOpcode() == BO_Rem ? LHS % RHS : LHS / RHS, E); 47202de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Shl: { 4721789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith // During constant-folding, a negative shift is an opposite shift. Such a 4722789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith // shift is not a constant expression. 4723091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall if (RHS.isSigned() && RHS.isNegative()) { 4724789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; 4725091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall RHS = -RHS; 4726091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall goto shift_right; 4727091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall } 4728091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall 4729091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall shift_left: 4730789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith // C++11 [expr.shift]p1: Shift width must be less than the bit width of the 4731789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith // shifted type. 4732789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); 4733789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith if (SA != RHS) { 4734789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith CCEDiag(E, diag::note_constexpr_large_shift) 4735789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith << RHS << E->getType() << LHS.getBitWidth(); 4736789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith } else if (LHS.isSigned()) { 4737789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith // C++11 [expr.shift]p2: A signed left shift must have a non-negative 4738925d8e7c0f18e03dc4bc634b3c6c1ec09373d993Richard Smith // operand, and must not overflow the corresponding unsigned type. 4739789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith if (LHS.isNegative()) 4740789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith CCEDiag(E, diag::note_constexpr_lshift_of_negative) << LHS; 4741925d8e7c0f18e03dc4bc634b3c6c1ec09373d993Richard Smith else if (LHS.countLeadingZeros() < SA) 4742925d8e7c0f18e03dc4bc634b3c6c1ec09373d993Richard Smith CCEDiag(E, diag::note_constexpr_lshift_discards); 4743789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith } 4744789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith 4745c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return Success(LHS << SA, E); 47463f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar } 47472de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Shr: { 4748789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith // During constant-folding, a negative shift is an opposite shift. Such a 4749789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith // shift is not a constant expression. 4750091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall if (RHS.isSigned() && RHS.isNegative()) { 4751789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; 4752091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall RHS = -RHS; 4753091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall goto shift_left; 4754091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall } 4755091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall 4756091f23f1d6d4bcffd6641cda72a6831e08c02ea7John McCall shift_right: 4757789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith // C++11 [expr.shift]p1: Shift width must be less than the bit width of the 4758789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith // shifted type. 4759789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); 4760789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith if (SA != RHS) 4761789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith CCEDiag(E, diag::note_constexpr_large_shift) 4762789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith << RHS << E->getType() << LHS.getBitWidth(); 4763789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith 4764c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return Success(LHS >> SA, E); 47653f7d995390009fede92b333a040da80e1ce90997Daniel Dunbar } 47661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4767c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case BO_LT: return Success(LHS < RHS, E); 4768c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case BO_GT: return Success(LHS > RHS, E); 4769c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case BO_LE: return Success(LHS <= RHS, E); 4770c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case BO_GE: return Success(LHS >= RHS, E); 4771c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case BO_EQ: return Success(LHS == RHS, E); 4772c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith case BO_NE: return Success(LHS != RHS, E); 4773b11e77836dd0867955c5abf32baf1c3e6c7f81e1Eli Friedman } 4774a25ae3d68d84d2b89907f998df6a396549589da5Anders Carlsson} 4775a25ae3d68d84d2b89907f998df6a396549589da5Anders Carlsson 47768b752f10c394b140f9ef89e049cbad1a7676fc25Ken DyckCharUnits IntExprEvaluator::GetAlignOfType(QualType T) { 47775d484e8cf710207010720589d89602233de61d01Sebastian Redl // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, 47785d484e8cf710207010720589d89602233de61d01Sebastian Redl // the result is the size of the referenced type." 47795d484e8cf710207010720589d89602233de61d01Sebastian Redl // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the 47805d484e8cf710207010720589d89602233de61d01Sebastian Redl // result shall be the alignment of the referenced type." 47815d484e8cf710207010720589d89602233de61d01Sebastian Redl if (const ReferenceType *Ref = T->getAs<ReferenceType>()) 47825d484e8cf710207010720589d89602233de61d01Sebastian Redl T = Ref->getPointeeType(); 47839f1210c3280104417a4ad30f0a00825ac8fa718aChad Rosier 47849f1210c3280104417a4ad30f0a00825ac8fa718aChad Rosier // __alignof is defined to return the preferred alignment. 47859f1210c3280104417a4ad30f0a00825ac8fa718aChad Rosier return Info.Ctx.toCharUnitsFromBits( 47869f1210c3280104417a4ad30f0a00825ac8fa718aChad Rosier Info.Ctx.getPreferredTypeAlign(T.getTypePtr())); 4787e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner} 4788e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner 47898b752f10c394b140f9ef89e049cbad1a7676fc25Ken DyckCharUnits IntExprEvaluator::GetAlignOfExpr(const Expr *E) { 4790af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner E = E->IgnoreParens(); 4791af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner 4792af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner // alignof decl is always accepted, even if it doesn't make sense: we default 47931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // to 1 in those cases. 4794af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) 47958b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck return Info.Ctx.getDeclAlign(DRE->getDecl(), 47968b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck /*RefAsPointee*/true); 4797a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman 4798af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) 47998b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck return Info.Ctx.getDeclAlign(ME->getMemberDecl(), 48008b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck /*RefAsPointee*/true); 4801af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner 4802e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner return GetAlignOfType(E->getType()); 4803e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner} 4804e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner 4805e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner 4806f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne/// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with 4807f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne/// a result as the expression's type. 4808f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbournebool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr( 4809f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne const UnaryExprOrTypeTraitExpr *E) { 4810f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne switch(E->getKind()) { 4811f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne case UETT_AlignOf: { 4812e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner if (E->isArgumentType()) 48134f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck return Success(GetAlignOfType(E->getArgumentType()), E); 4814e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner else 48154f3bc8f7aa90b72832b03bee9201c98f4bb6b4d1Ken Dyck return Success(GetAlignOfExpr(E->getArgumentExpr()), E); 4816e9feb475d72ba50dc29cec62a8c47cae721065ebChris Lattner } 4817a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman 4818f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne case UETT_VecStep: { 4819f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne QualType Ty = E->getTypeOfArgument(); 48200518999d3adcc289997bd974dce90cc97f5c1c44Sebastian Redl 4821f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne if (Ty->isVectorType()) { 4822f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne unsigned n = Ty->getAs<VectorType>()->getNumElements(); 4823a1f47c447a919c6a05c63801cb6a52c4c288e2ccEli Friedman 4824f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne // The vec_step built-in functions that take a 3-component 4825f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne // vector return 4. (OpenCL 1.1 spec 6.11.12) 4826f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne if (n == 3) 4827f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne n = 4; 4828f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne 4829f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne return Success(n, E); 4830f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne } else 4831f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne return Success(1, E); 4832f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne } 4833f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne 4834f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne case UETT_SizeOf: { 4835f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne QualType SrcTy = E->getTypeOfArgument(); 4836f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, 4837f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne // the result is the size of the referenced type." 4838f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the 4839f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne // result shall be the alignment of the referenced type." 4840f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>()) 4841f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne SrcTy = Ref->getPointeeType(); 4842f2da9dfef96dc11b7b5effb1d02cb427b2d71599Eli Friedman 4843180f47959a066795cc0f409433023af448bb0328Richard Smith CharUnits Sizeof; 4844180f47959a066795cc0f409433023af448bb0328Richard Smith if (!HandleSizeof(Info, SrcTy, Sizeof)) 4845f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne return false; 4846180f47959a066795cc0f409433023af448bb0328Richard Smith return Success(Sizeof, E); 4847f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne } 4848f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne } 4849f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne 4850f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne llvm_unreachable("unknown expr/type trait"); 4851fcee0019b76f9f368f2b3d6d4048a98232593f29Chris Lattner} 4852fcee0019b76f9f368f2b3d6d4048a98232593f29Chris Lattner 48538cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) { 48548ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor CharUnits Result; 48558cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne unsigned n = OOE->getNumComponents(); 48568ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor if (n == 0) 4857f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(OOE); 48588cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne QualType CurrentType = OOE->getTypeSourceInfo()->getType(); 48598ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor for (unsigned i = 0; i != n; ++i) { 48608ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor OffsetOfExpr::OffsetOfNode ON = OOE->getComponent(i); 48618ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor switch (ON.getKind()) { 48628ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor case OffsetOfExpr::OffsetOfNode::Array: { 48638cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne const Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex()); 48648ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor APSInt IdxResult; 48658ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor if (!EvaluateInteger(Idx, IdxResult, Info)) 48668ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor return false; 48678ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType); 48688ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor if (!AT) 4869f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(OOE); 48708ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor CurrentType = AT->getElementType(); 48718ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType); 48728ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor Result += IdxResult.getSExtValue() * ElementSize; 48738ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor break; 48748ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor } 4875f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 48768ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor case OffsetOfExpr::OffsetOfNode::Field: { 48778ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor FieldDecl *MemberDecl = ON.getField(); 48788ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor const RecordType *RT = CurrentType->getAs<RecordType>(); 4879f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!RT) 4880f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(OOE); 48818ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor RecordDecl *RD = RT->getDecl(); 48828ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); 4883ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall unsigned i = MemberDecl->getFieldIndex(); 4884cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor assert(i < RL.getFieldCount() && "offsetof field in wrong type"); 4885fb1e3bc29b667f4275e1d5a43d64ec173f4f9a7dKen Dyck Result += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i)); 48868ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor CurrentType = MemberDecl->getType().getNonReferenceType(); 48878ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor break; 48888ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor } 4889f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 48908ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor case OffsetOfExpr::OffsetOfNode::Identifier: 48918ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor llvm_unreachable("dependent __builtin_offsetof"); 4892f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 4893cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor case OffsetOfExpr::OffsetOfNode::Base: { 4894cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor CXXBaseSpecifier *BaseSpec = ON.getBase(); 4895cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor if (BaseSpec->isVirtual()) 4896f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(OOE); 4897cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor 4898cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor // Find the layout of the class whose base we are looking into. 4899cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor const RecordType *RT = CurrentType->getAs<RecordType>(); 4900f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!RT) 4901f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(OOE); 4902cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor RecordDecl *RD = RT->getDecl(); 4903cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); 4904cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor 4905cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor // Find the base class itself. 4906cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor CurrentType = BaseSpec->getType(); 4907cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor const RecordType *BaseRT = CurrentType->getAs<RecordType>(); 4908cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor if (!BaseRT) 4909f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(OOE); 4910cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor 4911cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor // Add the offset to the base. 49127c7f820d70c925b29290a8563b59615816a827fcKen Dyck Result += RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl())); 4913cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor break; 4914cc8a5d5f90bbbbcb46f342117b851b7e07ec34f1Douglas Gregor } 49158ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor } 49168ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor } 49178cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return Success(Result, OOE); 49188ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor} 49198ecdb65716cd7914ffb2eeee993fa9039fcd31e8Douglas Gregor 4920b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattnerbool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { 492175a4881047deeb3a300ff9293dc6ba8570048bb5Chris Lattner switch (E->getOpcode()) { 49224c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner default: 492375a4881047deeb3a300ff9293dc6ba8570048bb5Chris Lattner // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. 492475a4881047deeb3a300ff9293dc6ba8570048bb5Chris Lattner // See C99 6.6p3. 4925f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 49262de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_Extension: 49274c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner // FIXME: Should extension allow i-c-e extension expressions in its scope? 49284c4867e140327fa3b56306fa03c64c8e6a7c95efChris Lattner // If so, we could clear the diagnostic ID. 4929f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Visit(E->getSubExpr()); 49302de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_Plus: 4931c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // The result is just the value. 4932f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Visit(E->getSubExpr()); 4933f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith case UO_Minus: { 4934f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!Visit(E->getSubExpr())) 4935f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 4936f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!Result.isInt()) return Error(E); 4937789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith const APSInt &Value = Result.getInt(); 4938789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith if (Value.isSigned() && Value.isMinSignedValue()) 4939789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith HandleOverflow(Info, E, -Value.extend(Value.getBitWidth() + 1), 4940789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith E->getType()); 4941789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith return Success(-Value, E); 4942f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 4943f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith case UO_Not: { 4944f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!Visit(E->getSubExpr())) 4945f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 4946f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!Result.isInt()) return Error(E); 4947f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Success(~Result.getInt(), E); 4948f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 4949f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith case UO_LNot: { 4950f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith bool bres; 4951f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!EvaluateAsBooleanCondition(E->getSubExpr(), bres, Info)) 4952f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 4953f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Success(!bres, E); 4954f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 495506a3675627e3b3c47b49c689c8e404a33144194aAnders Carlsson } 4956a25ae3d68d84d2b89907f998df6a396549589da5Anders Carlsson} 49571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4958732b2236ae4a4f11e7642677cebbd169c07ea877Chris Lattner/// HandleCast - This is used to evaluate implicit or explicit casts where the 4959732b2236ae4a4f11e7642677cebbd169c07ea877Chris Lattner/// result type is integer. 49608cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool IntExprEvaluator::VisitCastExpr(const CastExpr *E) { 49618cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne const Expr *SubExpr = E->getSubExpr(); 496282206e267ce6cc709797127616f64672d255b310Anders Carlsson QualType DestType = E->getType(); 4963b92dac8bc2f6f73919825f9af693a8a7e89ae1d4Daniel Dunbar QualType SrcType = SubExpr->getType(); 496482206e267ce6cc709797127616f64672d255b310Anders Carlsson 496546a523285928aa07bf14803178dc04616ac85994Eli Friedman switch (E->getCastKind()) { 496646a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_BaseToDerived: 496746a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_DerivedToBase: 496846a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_UncheckedDerivedToBase: 496946a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_Dynamic: 497046a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_ToUnion: 497146a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_ArrayToPointerDecay: 497246a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_FunctionToPointerDecay: 497346a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_NullToPointer: 497446a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_NullToMemberPointer: 497546a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_BaseToDerivedMemberPointer: 497646a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_DerivedToBaseMemberPointer: 49774d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall case CK_ReinterpretMemberPointer: 497846a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_ConstructorConversion: 497946a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_IntegralToPointer: 498046a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_ToVoid: 498146a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_VectorSplat: 498246a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_IntegralToFloating: 498346a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_FloatingCast: 49841d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall case CK_CPointerToObjCPointerCast: 49851d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall case CK_BlockPointerToObjCPointerCast: 498646a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_AnyPointerToBlockPointerCast: 498746a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_ObjCObjectLValueCast: 498846a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_FloatingRealToComplex: 498946a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_FloatingComplexToReal: 499046a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_FloatingComplexCast: 499146a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_FloatingComplexToIntegralComplex: 499246a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_IntegralRealToComplex: 499346a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_IntegralComplexCast: 499446a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_IntegralComplexToFloatingComplex: 499546a523285928aa07bf14803178dc04616ac85994Eli Friedman llvm_unreachable("invalid cast kind for integral value"); 499646a523285928aa07bf14803178dc04616ac85994Eli Friedman 4997e50c297f92914ca996deb8b597624193273b62e4Eli Friedman case CK_BitCast: 499846a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_Dependent: 499946a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_LValueBitCast: 500033e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCProduceObject: 500133e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCConsumeObject: 500233e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCReclaimReturnedObject: 500333e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCExtendBlockObject: 5004f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 500546a523285928aa07bf14803178dc04616ac85994Eli Friedman 50067d580a4e9e47dffc3c17aa2b957ac57ca3c4e451Richard Smith case CK_UserDefinedConversion: 500746a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_LValueToRValue: 50087a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall case CK_AtomicToNonAtomic: 50097a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall case CK_NonAtomicToAtomic: 501046a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_NoOp: 5011c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return ExprEvaluatorBaseTy::VisitCastExpr(E); 501246a523285928aa07bf14803178dc04616ac85994Eli Friedman 501346a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_MemberPointerToBoolean: 501446a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_PointerToBoolean: 501546a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_IntegralToBoolean: 501646a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_FloatingToBoolean: 501746a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_FloatingComplexToBoolean: 501846a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_IntegralComplexToBoolean: { 50194efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman bool BoolResult; 5020c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith if (!EvaluateAsBooleanCondition(SubExpr, BoolResult, Info)) 50214efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman return false; 5022131eb438d8c216b2e2a4f8fa8158ea88b787dc14Daniel Dunbar return Success(BoolResult, E); 50234efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman } 50244efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 502546a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_IntegralCast: { 5026732b2236ae4a4f11e7642677cebbd169c07ea877Chris Lattner if (!Visit(SubExpr)) 5027b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattner return false; 5028a2cfd34952204c9a160fe1a5da5ba2f231df891dDaniel Dunbar 5029be26570e3faa009bdcefedfaf04473e518940520Eli Friedman if (!Result.isInt()) { 503065639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman // Allow casts of address-of-label differences if they are no-ops 503165639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman // or narrowing. (The narrowing case isn't actually guaranteed to 503265639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman // be constant-evaluatable except in some narrow cases which are hard 503365639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman // to detect here. We let it through on the assumption the user knows 503465639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman // what they are doing.) 503565639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman if (Result.isAddrLabelDiff()) 503665639284118d54ddf2e51a05d2ffccda567fe246Eli Friedman return Info.Ctx.getTypeSize(DestType) <= Info.Ctx.getTypeSize(SrcType); 5037be26570e3faa009bdcefedfaf04473e518940520Eli Friedman // Only allow casts of lvalues if they are lossless. 5038be26570e3faa009bdcefedfaf04473e518940520Eli Friedman return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType); 5039be26570e3faa009bdcefedfaf04473e518940520Eli Friedman } 504030c37f4d2ee5811e85f692c22fb67d74ddc88079Daniel Dunbar 5041f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith return Success(HandleIntToIntCast(Info, E, DestType, SrcType, 5042f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith Result.getInt()), E); 5043732b2236ae4a4f11e7642677cebbd169c07ea877Chris Lattner } 50441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 504546a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_PointerToIntegral: { 5046c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; 5047c216a01c96d83bd9a90e214af64913e93d39aaccRichard Smith 5048efdb83e26f9a1fd2566afe54461216cd84814d42John McCall LValue LV; 504987eae5ecf94e38baa20d9a327b8f73f8bdc72436Chris Lattner if (!EvaluatePointer(SubExpr, LV, Info)) 5050b542afe02d317411d53b3541946f9f2a8f509a11Chris Lattner return false; 50514efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 5052dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar if (LV.getLValueBase()) { 5053dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar // Only allow based lvalue casts if they are lossless. 5054f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith // FIXME: Allow a larger integer size than the pointer size, and allow 5055f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith // narrowing back down to pointer width in subsequent integral casts. 5056f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith // FIXME: Check integer type's active bits, not its type size. 5057dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType)) 5058f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 5059dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar 5060b755a9da095d2f2f04444797f1e1a9511693815bRichard Smith LV.Designator.setInvalid(); 5061efdb83e26f9a1fd2566afe54461216cd84814d42John McCall LV.moveInto(Result); 5062dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar return true; 5063dd2116462ae311043986ae8b7fba27e68c1b2e66Daniel Dunbar } 50644efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 5065a73058324197b7bdfd19307965954f626e26199dKen Dyck APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(), 5066a73058324197b7bdfd19307965954f626e26199dKen Dyck SrcType); 5067f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith return Success(HandleIntToIntCast(Info, E, DestType, SrcType, AsInt), E); 50682bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson } 50694efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 507046a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_IntegralComplexToReal: { 5071f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall ComplexValue C; 50721725f683432715e5afe34d476024bd6f16eac3fcEli Friedman if (!EvaluateComplex(SubExpr, C, Info)) 50731725f683432715e5afe34d476024bd6f16eac3fcEli Friedman return false; 507446a523285928aa07bf14803178dc04616ac85994Eli Friedman return Success(C.getComplexIntReal(), E); 50751725f683432715e5afe34d476024bd6f16eac3fcEli Friedman } 50762217c87bdc5ab357046a5453bdb06f469c41024eEli Friedman 507746a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_FloatingToIntegral: { 507846a523285928aa07bf14803178dc04616ac85994Eli Friedman APFloat F(0.0); 507946a523285928aa07bf14803178dc04616ac85994Eli Friedman if (!EvaluateFloat(SubExpr, F, Info)) 508046a523285928aa07bf14803178dc04616ac85994Eli Friedman return false; 5081732b2236ae4a4f11e7642677cebbd169c07ea877Chris Lattner 5082c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith APSInt Value; 5083c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (!HandleFloatToIntCast(Info, E, SrcType, F, DestType, Value)) 5084c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return false; 5085c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return Success(Value, E); 508646a523285928aa07bf14803178dc04616ac85994Eli Friedman } 508746a523285928aa07bf14803178dc04616ac85994Eli Friedman } 50881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 508946a523285928aa07bf14803178dc04616ac85994Eli Friedman llvm_unreachable("unknown cast resulting in integral value"); 5090a25ae3d68d84d2b89907f998df6a396549589da5Anders Carlsson} 50912bad1687fe6f00e10767a691a33b070b151902b6Anders Carlsson 5092722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedmanbool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { 5093722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman if (E->getSubExpr()->getType()->isAnyComplexType()) { 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.getComplexIntReal(), E); 5100722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman } 5101722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman 5102722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman return Visit(E->getSubExpr()); 5103722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman} 5104722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman 5105664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedmanbool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { 5106722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman if (E->getSubExpr()->getType()->isComplexIntegerType()) { 5107f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall ComplexValue LV; 5108f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!EvaluateComplex(E->getSubExpr(), LV, Info)) 5109f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 5110f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!LV.isComplexInt()) 5111f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 5112722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman return Success(LV.getComplexIntImag(), E); 5113722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman } 5114722c717cd833e410ca6e7976d78baea16995e0c4Eli Friedman 51158327fad71da34492d82c532f42a58cb4baff81a3Richard Smith VisitIgnoredValue(E->getSubExpr()); 5116664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman return Success(0, E); 5117664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman} 5118664a104ba0b8f47b8908ec6af694d9646adba1fcEli Friedman 5119ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregorbool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) { 5120ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor return Success(E->getPackLength(), E); 5121ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor} 5122ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor 5123295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redlbool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) { 5124295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl return Success(E->getValue(), E); 5125295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl} 5126295995c9c3196416372c9cd35d9cedb6da37bd3dSebastian Redl 5127f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===// 5128d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman// Float Evaluation 5129d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman//===----------------------------------------------------------------------===// 5130d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman 5131d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmannamespace { 5132770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass FloatExprEvaluator 51338cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne : public ExprEvaluatorBase<FloatExprEvaluator, bool> { 5134d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman APFloat &Result; 5135d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmanpublic: 5136d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman FloatExprEvaluator(EvalInfo &info, APFloat &result) 51378cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne : ExprEvaluatorBaseTy(info), Result(result) {} 5138d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman 513947a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith bool Success(const CCValue &V, const Expr *e) { 51408cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne Result = V.getFloat(); 51418cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return true; 51428cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne } 5143d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman 514451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith bool ZeroInitialization(const Expr *E) { 5145f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType())); 5146f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith return true; 5147f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith } 5148f10d9171ac24380ca94c71847a9270a05b791cefRichard Smith 5149019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner bool VisitCallExpr(const CallExpr *E); 5150d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman 51515db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar bool VisitUnaryOperator(const UnaryOperator *E); 5152d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman bool VisitBinaryOperator(const BinaryOperator *E); 5153d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman bool VisitFloatingLiteral(const FloatingLiteral *E); 51548cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitCastExpr(const CastExpr *E); 51552217c87bdc5ab357046a5453bdb06f469c41024eEli Friedman 5156abd3a857ace59100305790545d1baae5877b8945John McCall bool VisitUnaryReal(const UnaryOperator *E); 5157abd3a857ace59100305790545d1baae5877b8945John McCall bool VisitUnaryImag(const UnaryOperator *E); 5158ba98d6bb414861965a1f22628494ea046785ecd4Eli Friedman 515951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith // FIXME: Missing: array subscript of vector, member of vector 5160d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman}; 5161d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman} // end anonymous namespace 5162d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman 5163d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmanstatic bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { 5164c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith assert(E->isRValue() && E->getType()->isRealFloatingType()); 51658cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return FloatExprEvaluator(Info, Result).Visit(E); 5166d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman} 5167d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman 51684ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadstatic bool TryEvaluateBuiltinNaN(const ASTContext &Context, 5169db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall QualType ResultTy, 5170db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall const Expr *Arg, 5171db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall bool SNaN, 5172db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall llvm::APFloat &Result) { 5173db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts()); 5174db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall if (!S) return false; 5175db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall 5176db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy); 5177db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall 5178db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall llvm::APInt fill; 5179db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall 5180db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall // Treat empty strings as if they were zero. 5181db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall if (S->getString().empty()) 5182db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall fill = llvm::APInt(32, 0); 5183db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall else if (S->getString().getAsInteger(0, fill)) 5184db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall return false; 5185db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall 5186db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall if (SNaN) 5187db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall Result = llvm::APFloat::getSNaN(Sem, false, &fill); 5188db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall else 5189db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall Result = llvm::APFloat::getQNaN(Sem, false, &fill); 5190db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall return true; 5191db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall} 5192db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall 5193019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattnerbool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { 5194180f47959a066795cc0f409433023af448bb0328Richard Smith switch (E->isBuiltinCall()) { 51958cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne default: 51968cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return ExprEvaluatorBaseTy::VisitCallExpr(E); 51978cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne 5198019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner case Builtin::BI__builtin_huge_val: 5199019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner case Builtin::BI__builtin_huge_valf: 5200019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner case Builtin::BI__builtin_huge_vall: 5201019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner case Builtin::BI__builtin_inf: 5202019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner case Builtin::BI__builtin_inff: 52037cbed03c00e246682e5292785d01e1c120ce54bdDaniel Dunbar case Builtin::BI__builtin_infl: { 52047cbed03c00e246682e5292785d01e1c120ce54bdDaniel Dunbar const llvm::fltSemantics &Sem = 52057cbed03c00e246682e5292785d01e1c120ce54bdDaniel Dunbar Info.Ctx.getFloatTypeSemantics(E->getType()); 520634a74ab81600a40c6324fd76adb724b803dfaf91Chris Lattner Result = llvm::APFloat::getInf(Sem); 520734a74ab81600a40c6324fd76adb724b803dfaf91Chris Lattner return true; 52087cbed03c00e246682e5292785d01e1c120ce54bdDaniel Dunbar } 52091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5210db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall case Builtin::BI__builtin_nans: 5211db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall case Builtin::BI__builtin_nansf: 5212db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall case Builtin::BI__builtin_nansl: 5213f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), 5214f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith true, Result)) 5215f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 5216f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return true; 5217db7b72a82a6834680ccf1eeb51dc57e6d935c655John McCall 52189e62171a25e3a08fb5c49fb370f83faf5ae786f5Chris Lattner case Builtin::BI__builtin_nan: 52199e62171a25e3a08fb5c49fb370f83faf5ae786f5Chris Lattner case Builtin::BI__builtin_nanf: 52209e62171a25e3a08fb5c49fb370f83faf5ae786f5Chris Lattner case Builtin::BI__builtin_nanl: 52214572baba9d18c275968ac113fd73b0e3c77cccb8Mike Stump // If this is __builtin_nan() turn this into a nan, otherwise we 52229e62171a25e3a08fb5c49fb370f83faf5ae786f5Chris Lattner // can't constant fold it. 5223f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), 5224f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith false, Result)) 5225f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 5226f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return true; 52275db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar 52285db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar case Builtin::BI__builtin_fabs: 52295db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar case Builtin::BI__builtin_fabsf: 52305db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar case Builtin::BI__builtin_fabsl: 52315db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar if (!EvaluateFloat(E->getArg(0), Result, Info)) 52325db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar return false; 52331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 52345db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar if (Result.isNegative()) 52355db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar Result.changeSign(); 52365db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar return true; 52375db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar 52381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump case Builtin::BI__builtin_copysign: 52391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump case Builtin::BI__builtin_copysignf: 52405db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar case Builtin::BI__builtin_copysignl: { 52415db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar APFloat RHS(0.); 52425db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar if (!EvaluateFloat(E->getArg(0), Result, Info) || 52435db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar !EvaluateFloat(E->getArg(1), RHS, Info)) 52445db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar return false; 52455db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar Result.copySign(RHS); 52465db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar return true; 52475db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar } 5248019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner } 5249019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner} 5250019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner 5251abd3a857ace59100305790545d1baae5877b8945John McCallbool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { 525243efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman if (E->getSubExpr()->getType()->isAnyComplexType()) { 525343efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman ComplexValue CV; 525443efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman if (!EvaluateComplex(E->getSubExpr(), CV, Info)) 525543efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman return false; 525643efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman Result = CV.FloatReal; 525743efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman return true; 525843efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman } 525943efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman 526043efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman return Visit(E->getSubExpr()); 5261abd3a857ace59100305790545d1baae5877b8945John McCall} 5262abd3a857ace59100305790545d1baae5877b8945John McCall 5263abd3a857ace59100305790545d1baae5877b8945John McCallbool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { 526443efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman if (E->getSubExpr()->getType()->isAnyComplexType()) { 526543efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman ComplexValue CV; 526643efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman if (!EvaluateComplex(E->getSubExpr(), CV, Info)) 526743efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman return false; 526843efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman Result = CV.FloatImag; 526943efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman return true; 527043efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman } 527143efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman 52728327fad71da34492d82c532f42a58cb4baff81a3Richard Smith VisitIgnoredValue(E->getSubExpr()); 527343efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(E->getType()); 527443efa31fe601bb7c3f132f02246dc3c903d9f361Eli Friedman Result = llvm::APFloat::getZero(Sem); 5275abd3a857ace59100305790545d1baae5877b8945John McCall return true; 5276abd3a857ace59100305790545d1baae5877b8945John McCall} 5277abd3a857ace59100305790545d1baae5877b8945John McCall 52785db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbarbool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { 52795db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar switch (E->getOpcode()) { 5280f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith default: return Error(E); 52812de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_Plus: 52827993e8a2a26bf408c2a6d45f24fffa12db336b2bRichard Smith return EvaluateFloat(E->getSubExpr(), Result, Info); 52832de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_Minus: 52847993e8a2a26bf408c2a6d45f24fffa12db336b2bRichard Smith if (!EvaluateFloat(E->getSubExpr(), Result, Info)) 52857993e8a2a26bf408c2a6d45f24fffa12db336b2bRichard Smith return false; 52865db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar Result.changeSign(); 52875db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar return true; 52885db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar } 52895db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar} 5290019f4e858e78587f2241ff1a76c747d7bcd7578cChris Lattner 5291d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmanbool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { 5292e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) 5293e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return ExprEvaluatorBaseTy::VisitBinaryOperator(E); 529496e93660124c8028a4c3bcc038ab0cdd18cd7ab2Anders Carlsson 52955db4b3f3ed9f769d5b02c1d1ccc52bfd71fb9afbDaniel Dunbar APFloat RHS(0.0); 5296745f5147e065900267c85a5568785a1991d4838fRichard Smith bool LHSOK = EvaluateFloat(E->getLHS(), Result, Info); 5297745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!LHSOK && !Info.keepEvaluatingAfterFailure()) 5298d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman return false; 5299745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!EvaluateFloat(E->getRHS(), RHS, Info) || !LHSOK) 5300d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman return false; 5301d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman 5302d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman switch (E->getOpcode()) { 5303f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith default: return Error(E); 53042de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Mul: 5305d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman Result.multiply(RHS, APFloat::rmNearestTiesToEven); 53067b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith break; 53072de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Add: 5308d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman Result.add(RHS, APFloat::rmNearestTiesToEven); 53097b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith break; 53102de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Sub: 5311d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman Result.subtract(RHS, APFloat::rmNearestTiesToEven); 53127b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith break; 53132de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Div: 5314d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman Result.divide(RHS, APFloat::rmNearestTiesToEven); 53157b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith break; 5316d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman } 53177b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith 53187b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith if (Result.isInfinity() || Result.isNaN()) 53197b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith CCEDiag(E, diag::note_constexpr_float_arithmetic) << Result.isNaN(); 53207b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith return true; 5321d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman} 5322d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman 5323d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedmanbool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { 5324d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman Result = E->getValue(); 5325d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman return true; 5326d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman} 5327d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman 53288cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) { 53298cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne const Expr* SubExpr = E->getSubExpr(); 53301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 53312a523eec6a31955be876625819b89e8dc5def707Eli Friedman switch (E->getCastKind()) { 53322a523eec6a31955be876625819b89e8dc5def707Eli Friedman default: 5333c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return ExprEvaluatorBaseTy::VisitCastExpr(E); 53342a523eec6a31955be876625819b89e8dc5def707Eli Friedman 53352a523eec6a31955be876625819b89e8dc5def707Eli Friedman case CK_IntegralToFloating: { 53364efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman APSInt IntResult; 5337c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return EvaluateInteger(SubExpr, IntResult, Info) && 5338c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith HandleIntToFloatCast(Info, E, SubExpr->getType(), IntResult, 5339c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith E->getType(), Result); 53404efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman } 53412a523eec6a31955be876625819b89e8dc5def707Eli Friedman 53422a523eec6a31955be876625819b89e8dc5def707Eli Friedman case CK_FloatingCast: { 53434efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman if (!Visit(SubExpr)) 53444efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman return false; 5345c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return HandleFloatToFloatCast(Info, E, SubExpr->getType(), E->getType(), 5346c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Result); 53474efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman } 5348f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall 53492a523eec6a31955be876625819b89e8dc5def707Eli Friedman case CK_FloatingComplexToReal: { 5350f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall ComplexValue V; 5351f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall if (!EvaluateComplex(SubExpr, V, Info)) 5352f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall return false; 5353f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall Result = V.getComplexFloatReal(); 5354f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall return true; 5355f3ea8cfe6b1c2ef0702efe130561e9e66708d799John McCall } 53562a523eec6a31955be876625819b89e8dc5def707Eli Friedman } 53574efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman} 53584efaa276bc0ce8f7baf6138ead11915f3e3e58d9Eli Friedman 5359d8bfe7f25a695ca947effbccdf9ecbe3e018e221Eli Friedman//===----------------------------------------------------------------------===// 5360a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar// Complex Evaluation (for float and integer) 53619ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson//===----------------------------------------------------------------------===// 53629ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson 53639ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlssonnamespace { 5364770b4a8834670e9427d3ce5a1a8472eb86f45fd2Benjamin Kramerclass ComplexExprEvaluator 53658cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne : public ExprEvaluatorBase<ComplexExprEvaluator, bool> { 5366f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall ComplexValue &Result; 53671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 53689ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlssonpublic: 5369f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result) 53708cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne : ExprEvaluatorBaseTy(info), Result(Result) {} 53711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 537247a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith bool Success(const CCValue &V, const Expr *e) { 53738cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne Result.setFrom(V); 53748cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return true; 53758cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne } 53761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 53777ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman bool ZeroInitialization(const Expr *E); 53787ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman 53798cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne //===--------------------------------------------------------------------===// 53808cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne // Visitor Methods 53818cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne //===--------------------------------------------------------------------===// 53829ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson 53838cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitImaginaryLiteral(const ImaginaryLiteral *E); 53848cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne bool VisitCastExpr(const CastExpr *E); 5385b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman bool VisitBinaryOperator(const BinaryOperator *E); 538696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara bool VisitUnaryOperator(const UnaryOperator *E); 53877ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman bool VisitInitListExpr(const InitListExpr *E); 5388b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman}; 5389b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman} // end anonymous namespace 53901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5391b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedmanstatic bool EvaluateComplex(const Expr *E, ComplexValue &Result, 5392b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman EvalInfo &Info) { 5393c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith assert(E->isRValue() && E->getType()->isAnyComplexType()); 53948cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne return ComplexExprEvaluator(Info, Result).Visit(E); 5395b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman} 5396b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman 53977ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedmanbool ComplexExprEvaluator::ZeroInitialization(const Expr *E) { 5398f6c17a439f3320ac620639a3ee66dbdabb93810cEli Friedman QualType ElemTy = E->getType()->getAs<ComplexType>()->getElementType(); 53997ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman if (ElemTy->isRealFloatingType()) { 54007ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman Result.makeComplexFloat(); 54017ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman APFloat Zero = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(ElemTy)); 54027ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman Result.FloatReal = Zero; 54037ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman Result.FloatImag = Zero; 54047ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman } else { 54057ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman Result.makeComplexInt(); 54067ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman APSInt Zero = Info.Ctx.MakeIntValue(0, ElemTy); 54077ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman Result.IntReal = Zero; 54087ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman Result.IntImag = Zero; 54097ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman } 54107ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman return true; 54117ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman} 54127ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman 54138cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) { 54148cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbourne const Expr* SubExpr = E->getSubExpr(); 5415b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman 5416b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman if (SubExpr->getType()->isRealFloatingType()) { 5417b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman Result.makeComplexFloat(); 5418b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman APFloat &Imag = Result.FloatImag; 5419b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman if (!EvaluateFloat(SubExpr, Imag, Info)) 5420b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman return false; 5421b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman 5422b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman Result.FloatReal = APFloat(Imag.getSemantics()); 5423b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman return true; 5424b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman } else { 5425b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman assert(SubExpr->getType()->isIntegerType() && 5426b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman "Unexpected imaginary literal."); 5427b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman 5428b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman Result.makeComplexInt(); 5429b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman APSInt &Imag = Result.IntImag; 5430b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman if (!EvaluateInteger(SubExpr, Imag, Info)) 5431b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman return false; 5432b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman 5433b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned()); 5434b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman return true; 5435b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman } 5436b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman} 5437b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman 54388cad3046be06ea73ff8892d947697a21d7a440d3Peter Collingbournebool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) { 5439b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman 54408786da77984e81d48e0e1b2bd339809b1efc19f3John McCall switch (E->getCastKind()) { 54418786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_BitCast: 54428786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_BaseToDerived: 54438786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_DerivedToBase: 54448786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_UncheckedDerivedToBase: 54458786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_Dynamic: 54468786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_ToUnion: 54478786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_ArrayToPointerDecay: 54488786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_FunctionToPointerDecay: 54498786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_NullToPointer: 54508786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_NullToMemberPointer: 54518786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_BaseToDerivedMemberPointer: 54528786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_DerivedToBaseMemberPointer: 54538786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_MemberPointerToBoolean: 54544d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall case CK_ReinterpretMemberPointer: 54558786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_ConstructorConversion: 54568786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_IntegralToPointer: 54578786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_PointerToIntegral: 54588786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_PointerToBoolean: 54598786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_ToVoid: 54608786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_VectorSplat: 54618786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_IntegralCast: 54628786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_IntegralToBoolean: 54638786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_IntegralToFloating: 54648786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_FloatingToIntegral: 54658786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_FloatingToBoolean: 54668786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_FloatingCast: 54671d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall case CK_CPointerToObjCPointerCast: 54681d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall case CK_BlockPointerToObjCPointerCast: 54698786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_AnyPointerToBlockPointerCast: 54708786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_ObjCObjectLValueCast: 54718786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_FloatingComplexToReal: 54728786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_FloatingComplexToBoolean: 54738786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_IntegralComplexToReal: 54748786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_IntegralComplexToBoolean: 547533e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCProduceObject: 547633e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCConsumeObject: 547733e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCReclaimReturnedObject: 547833e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCExtendBlockObject: 54798786da77984e81d48e0e1b2bd339809b1efc19f3John McCall llvm_unreachable("invalid cast kind for complex value"); 54808786da77984e81d48e0e1b2bd339809b1efc19f3John McCall 54818786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_LValueToRValue: 54827a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall case CK_AtomicToNonAtomic: 54837a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall case CK_NonAtomicToAtomic: 54848786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_NoOp: 5485c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return ExprEvaluatorBaseTy::VisitCastExpr(E); 54862bb5d00fcf71a7b4d478d478be778fff0494aff6John McCall 54878786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_Dependent: 548846a523285928aa07bf14803178dc04616ac85994Eli Friedman case CK_LValueBitCast: 54898786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_UserDefinedConversion: 5490f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 54918786da77984e81d48e0e1b2bd339809b1efc19f3John McCall 54928786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_FloatingRealToComplex: { 5493b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman APFloat &Real = Result.FloatReal; 54948786da77984e81d48e0e1b2bd339809b1efc19f3John McCall if (!EvaluateFloat(E->getSubExpr(), Real, Info)) 5495b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman return false; 5496b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman 54978786da77984e81d48e0e1b2bd339809b1efc19f3John McCall Result.makeComplexFloat(); 54988786da77984e81d48e0e1b2bd339809b1efc19f3John McCall Result.FloatImag = APFloat(Real.getSemantics()); 54998786da77984e81d48e0e1b2bd339809b1efc19f3John McCall return true; 55008786da77984e81d48e0e1b2bd339809b1efc19f3John McCall } 55018786da77984e81d48e0e1b2bd339809b1efc19f3John McCall 55028786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_FloatingComplexCast: { 55038786da77984e81d48e0e1b2bd339809b1efc19f3John McCall if (!Visit(E->getSubExpr())) 55048786da77984e81d48e0e1b2bd339809b1efc19f3John McCall return false; 55058786da77984e81d48e0e1b2bd339809b1efc19f3John McCall 55068786da77984e81d48e0e1b2bd339809b1efc19f3John McCall QualType To = E->getType()->getAs<ComplexType>()->getElementType(); 55078786da77984e81d48e0e1b2bd339809b1efc19f3John McCall QualType From 55088786da77984e81d48e0e1b2bd339809b1efc19f3John McCall = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); 55098786da77984e81d48e0e1b2bd339809b1efc19f3John McCall 5510c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return HandleFloatToFloatCast(Info, E, From, To, Result.FloatReal) && 5511c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith HandleFloatToFloatCast(Info, E, From, To, Result.FloatImag); 55128786da77984e81d48e0e1b2bd339809b1efc19f3John McCall } 55138786da77984e81d48e0e1b2bd339809b1efc19f3John McCall 55148786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_FloatingComplexToIntegralComplex: { 55158786da77984e81d48e0e1b2bd339809b1efc19f3John McCall if (!Visit(E->getSubExpr())) 55168786da77984e81d48e0e1b2bd339809b1efc19f3John McCall return false; 55178786da77984e81d48e0e1b2bd339809b1efc19f3John McCall 55188786da77984e81d48e0e1b2bd339809b1efc19f3John McCall QualType To = E->getType()->getAs<ComplexType>()->getElementType(); 55198786da77984e81d48e0e1b2bd339809b1efc19f3John McCall QualType From 55208786da77984e81d48e0e1b2bd339809b1efc19f3John McCall = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); 55218786da77984e81d48e0e1b2bd339809b1efc19f3John McCall Result.makeComplexInt(); 5522c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return HandleFloatToIntCast(Info, E, From, Result.FloatReal, 5523c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith To, Result.IntReal) && 5524c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith HandleFloatToIntCast(Info, E, From, Result.FloatImag, 5525c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith To, Result.IntImag); 55268786da77984e81d48e0e1b2bd339809b1efc19f3John McCall } 55278786da77984e81d48e0e1b2bd339809b1efc19f3John McCall 55288786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_IntegralRealToComplex: { 5529b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman APSInt &Real = Result.IntReal; 55308786da77984e81d48e0e1b2bd339809b1efc19f3John McCall if (!EvaluateInteger(E->getSubExpr(), Real, Info)) 5531b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman return false; 55329ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson 55338786da77984e81d48e0e1b2bd339809b1efc19f3John McCall Result.makeComplexInt(); 55348786da77984e81d48e0e1b2bd339809b1efc19f3John McCall Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned()); 55358786da77984e81d48e0e1b2bd339809b1efc19f3John McCall return true; 55368786da77984e81d48e0e1b2bd339809b1efc19f3John McCall } 55378786da77984e81d48e0e1b2bd339809b1efc19f3John McCall 55388786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_IntegralComplexCast: { 55398786da77984e81d48e0e1b2bd339809b1efc19f3John McCall if (!Visit(E->getSubExpr())) 5540b2dc7f59fe4c762cba73badc3bbc6f356fcd7b5bEli Friedman return false; 5541ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson 55428786da77984e81d48e0e1b2bd339809b1efc19f3John McCall QualType To = E->getType()->getAs<ComplexType>()->getElementType(); 55438786da77984e81d48e0e1b2bd339809b1efc19f3John McCall QualType From 55448786da77984e81d48e0e1b2bd339809b1efc19f3John McCall = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); 55451725f683432715e5afe34d476024bd6f16eac3fcEli Friedman 5546f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith Result.IntReal = HandleIntToIntCast(Info, E, To, From, Result.IntReal); 5547f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith Result.IntImag = HandleIntToIntCast(Info, E, To, From, Result.IntImag); 55488786da77984e81d48e0e1b2bd339809b1efc19f3John McCall return true; 55498786da77984e81d48e0e1b2bd339809b1efc19f3John McCall } 55508786da77984e81d48e0e1b2bd339809b1efc19f3John McCall 55518786da77984e81d48e0e1b2bd339809b1efc19f3John McCall case CK_IntegralComplexToFloatingComplex: { 55528786da77984e81d48e0e1b2bd339809b1efc19f3John McCall if (!Visit(E->getSubExpr())) 55538786da77984e81d48e0e1b2bd339809b1efc19f3John McCall return false; 55548786da77984e81d48e0e1b2bd339809b1efc19f3John McCall 55558786da77984e81d48e0e1b2bd339809b1efc19f3John McCall QualType To = E->getType()->getAs<ComplexType>()->getElementType(); 55568786da77984e81d48e0e1b2bd339809b1efc19f3John McCall QualType From 55578786da77984e81d48e0e1b2bd339809b1efc19f3John McCall = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); 55588786da77984e81d48e0e1b2bd339809b1efc19f3John McCall Result.makeComplexFloat(); 5559c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return HandleIntToFloatCast(Info, E, From, Result.IntReal, 5560c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith To, Result.FloatReal) && 5561c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith HandleIntToFloatCast(Info, E, From, Result.IntImag, 5562c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith To, Result.FloatImag); 55638786da77984e81d48e0e1b2bd339809b1efc19f3John McCall } 5564ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson } 55651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 55668786da77984e81d48e0e1b2bd339809b1efc19f3John McCall llvm_unreachable("unknown cast resulting in complex value"); 55679ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson} 55689ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson 5569f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCallbool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { 5570e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) 55712ad226bdc847df6b6b6e4f832856478ab63bb3dcRichard Smith return ExprEvaluatorBaseTy::VisitBinaryOperator(E); 55722ad226bdc847df6b6b6e4f832856478ab63bb3dcRichard Smith 5573745f5147e065900267c85a5568785a1991d4838fRichard Smith bool LHSOK = Visit(E->getLHS()); 5574745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!LHSOK && !Info.keepEvaluatingAfterFailure()) 5575f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall return false; 55761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5577f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall ComplexValue RHS; 5578745f5147e065900267c85a5568785a1991d4838fRichard Smith if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK) 5579f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall return false; 5580a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar 55813f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar assert(Result.isComplexFloat() == RHS.isComplexFloat() && 55823f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar "Invalid operands to binary operator."); 5583ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson switch (E->getOpcode()) { 5584f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith default: return Error(E); 55852de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Add: 5586a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar if (Result.isComplexFloat()) { 5587a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar Result.getComplexFloatReal().add(RHS.getComplexFloatReal(), 5588a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar APFloat::rmNearestTiesToEven); 5589a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar Result.getComplexFloatImag().add(RHS.getComplexFloatImag(), 5590a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar APFloat::rmNearestTiesToEven); 5591a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar } else { 5592a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar Result.getComplexIntReal() += RHS.getComplexIntReal(); 5593a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar Result.getComplexIntImag() += RHS.getComplexIntImag(); 5594a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar } 55953f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar break; 55962de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Sub: 5597a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar if (Result.isComplexFloat()) { 5598a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(), 5599a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar APFloat::rmNearestTiesToEven); 5600a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(), 5601a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar APFloat::rmNearestTiesToEven); 5602a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar } else { 5603a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar Result.getComplexIntReal() -= RHS.getComplexIntReal(); 5604a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar Result.getComplexIntImag() -= RHS.getComplexIntImag(); 5605a5fd07bbc5e4bae542c06643da3fbfe4967a9379Daniel Dunbar } 56063f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar break; 56072de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Mul: 56083f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar if (Result.isComplexFloat()) { 5609f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall ComplexValue LHS = Result; 56103f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar APFloat &LHS_r = LHS.getComplexFloatReal(); 56113f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar APFloat &LHS_i = LHS.getComplexFloatImag(); 56123f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar APFloat &RHS_r = RHS.getComplexFloatReal(); 56133f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar APFloat &RHS_i = RHS.getComplexFloatImag(); 56141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 56153f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar APFloat Tmp = LHS_r; 56163f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); 56173f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar Result.getComplexFloatReal() = Tmp; 56183f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar Tmp = LHS_i; 56193f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); 56203f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven); 56213f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar 56223f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar Tmp = LHS_r; 56233f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); 56243f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar Result.getComplexFloatImag() = Tmp; 56253f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar Tmp = LHS_i; 56263f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); 56273f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven); 56283f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar } else { 5629f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall ComplexValue LHS = Result; 56301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Result.getComplexIntReal() = 56313f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar (LHS.getComplexIntReal() * RHS.getComplexIntReal() - 56323f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar LHS.getComplexIntImag() * RHS.getComplexIntImag()); 56331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Result.getComplexIntImag() = 56343f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar (LHS.getComplexIntReal() * RHS.getComplexIntImag() + 56353f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar LHS.getComplexIntImag() * RHS.getComplexIntReal()); 56363f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar } 56373f2798757c9ee353e207e18115e2e966432a4beeDaniel Dunbar break; 563896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara case BO_Div: 563996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara if (Result.isComplexFloat()) { 564096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara ComplexValue LHS = Result; 564196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara APFloat &LHS_r = LHS.getComplexFloatReal(); 564296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara APFloat &LHS_i = LHS.getComplexFloatImag(); 564396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara APFloat &RHS_r = RHS.getComplexFloatReal(); 564496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara APFloat &RHS_i = RHS.getComplexFloatImag(); 564596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara APFloat &Res_r = Result.getComplexFloatReal(); 564696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara APFloat &Res_i = Result.getComplexFloatImag(); 564796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara 564896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara APFloat Den = RHS_r; 564996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Den.multiply(RHS_r, APFloat::rmNearestTiesToEven); 565096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara APFloat Tmp = RHS_i; 565196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); 565296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Den.add(Tmp, APFloat::rmNearestTiesToEven); 565396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara 565496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Res_r = LHS_r; 565596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Res_r.multiply(RHS_r, APFloat::rmNearestTiesToEven); 565696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Tmp = LHS_i; 565796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); 565896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Res_r.add(Tmp, APFloat::rmNearestTiesToEven); 565996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Res_r.divide(Den, APFloat::rmNearestTiesToEven); 566096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara 566196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Res_i = LHS_i; 566296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Res_i.multiply(RHS_r, APFloat::rmNearestTiesToEven); 566396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Tmp = LHS_r; 566496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); 566596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Res_i.subtract(Tmp, APFloat::rmNearestTiesToEven); 566696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Res_i.divide(Den, APFloat::rmNearestTiesToEven); 566796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara } else { 5668f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0) 5669f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E, diag::note_expr_divide_by_zero); 5670f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 567196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara ComplexValue LHS = Result; 567296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() + 567396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara RHS.getComplexIntImag() * RHS.getComplexIntImag(); 567496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Result.getComplexIntReal() = 567596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara (LHS.getComplexIntReal() * RHS.getComplexIntReal() + 567696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den; 567796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Result.getComplexIntImag() = 567896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara (LHS.getComplexIntImag() * RHS.getComplexIntReal() - 567996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den; 568096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara } 568196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara break; 5682ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson } 5683ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson 5684f4cf1a18d09d57b757b3cb47eab36c1457091ef7John McCall return true; 5685ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson} 5686ccc3fce5697e33f005990f9795e1c7cb8b4559ecAnders Carlsson 568796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnarabool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { 568896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara // Get the operand value into 'Result'. 568996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara if (!Visit(E->getSubExpr())) 569096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara return false; 569196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara 569296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara switch (E->getOpcode()) { 569396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara default: 5694f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return Error(E); 569596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara case UO_Extension: 569696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara return true; 569796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara case UO_Plus: 569896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara // The result is always just the subexpr. 569996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara return true; 570096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara case UO_Minus: 570196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara if (Result.isComplexFloat()) { 570296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Result.getComplexFloatReal().changeSign(); 570396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Result.getComplexFloatImag().changeSign(); 570496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara } 570596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara else { 570696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Result.getComplexIntReal() = -Result.getComplexIntReal(); 570796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Result.getComplexIntImag() = -Result.getComplexIntImag(); 570896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara } 570996fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara return true; 571096fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara case UO_Not: 571196fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara if (Result.isComplexFloat()) 571296fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Result.getComplexFloatImag().changeSign(); 571396fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara else 571496fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara Result.getComplexIntImag() = -Result.getComplexIntImag(); 571596fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara return true; 571696fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara } 571796fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara} 571896fc8e4086df323c49f17cac594db1d2f066a2e9Abramo Bagnara 57197ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedmanbool ComplexExprEvaluator::VisitInitListExpr(const InitListExpr *E) { 57207ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman if (E->getNumInits() == 2) { 57217ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman if (E->getType()->isComplexType()) { 57227ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman Result.makeComplexFloat(); 57237ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman if (!EvaluateFloat(E->getInit(0), Result.FloatReal, Info)) 57247ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman return false; 57257ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman if (!EvaluateFloat(E->getInit(1), Result.FloatImag, Info)) 57267ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman return false; 57277ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman } else { 57287ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman Result.makeComplexInt(); 57297ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman if (!EvaluateInteger(E->getInit(0), Result.IntReal, Info)) 57307ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman return false; 57317ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman if (!EvaluateInteger(E->getInit(1), Result.IntImag, Info)) 57327ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman return false; 57337ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman } 57347ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman return true; 57357ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman } 57367ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman return ExprEvaluatorBaseTy::VisitInitListExpr(E); 57377ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman} 57387ead5c7b6fd48cf549e55b4db499c26ecf88ae75Eli Friedman 57399ad16aebc0e840a5e7d425da72eb6cbe25e4b58cAnders Carlsson//===----------------------------------------------------------------------===// 5740aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith// Void expression evaluation, primarily for a cast to void on the LHS of a 5741aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith// comma operator 5742aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith//===----------------------------------------------------------------------===// 5743aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith 5744aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smithnamespace { 5745aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smithclass VoidExprEvaluator 5746aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith : public ExprEvaluatorBase<VoidExprEvaluator, bool> { 5747aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smithpublic: 5748aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith VoidExprEvaluator(EvalInfo &Info) : ExprEvaluatorBaseTy(Info) {} 5749aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith 5750aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith bool Success(const CCValue &V, const Expr *e) { return true; } 5751aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith 5752aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith bool VisitCastExpr(const CastExpr *E) { 5753aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith switch (E->getCastKind()) { 5754aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith default: 5755aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith return ExprEvaluatorBaseTy::VisitCastExpr(E); 5756aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith case CK_ToVoid: 5757aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith VisitIgnoredValue(E->getSubExpr()); 5758aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith return true; 5759aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith } 5760aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith } 5761aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith}; 5762aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith} // end anonymous namespace 5763aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith 5764aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smithstatic bool EvaluateVoid(const Expr *E, EvalInfo &Info) { 5765aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith assert(E->isRValue() && E->getType()->isVoidType()); 5766aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith return VoidExprEvaluator(Info).Visit(E); 5767aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith} 5768aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith 5769aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith//===----------------------------------------------------------------------===// 577051f4708c00110940ca3f337961915f2ca1668375Richard Smith// Top level Expr::EvaluateAsRValue method. 5771f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner//===----------------------------------------------------------------------===// 5772f5eeb055ecbadbc25c83df0867cdada2c2559dcfChris Lattner 577347a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smithstatic bool Evaluate(CCValue &Result, EvalInfo &Info, const Expr *E) { 5774c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // In C, function designators are not lvalues, but we evaluate them as if they 5775c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith // are. 5776c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith if (E->isGLValue() || E->getType()->isFunctionType()) { 5777c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith LValue LV; 5778c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith if (!EvaluateLValue(E, LV, Info)) 5779c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return false; 5780c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith LV.moveInto(Result); 5781c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith } else if (E->getType()->isVectorType()) { 57821e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith if (!EvaluateVector(E, Result, Info)) 578359b5da6d853b4368b984700315adf7b37de05764Nate Begeman return false; 5784575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor } else if (E->getType()->isIntegralOrEnumerationType()) { 57851e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith if (!IntExprEvaluator(Info, Result).Visit(E)) 57866dde0d5dc09f45f4d9508c964703e36fef1a0198Anders Carlsson return false; 5787efdb83e26f9a1fd2566afe54461216cd84814d42John McCall } else if (E->getType()->hasPointerRepresentation()) { 5788efdb83e26f9a1fd2566afe54461216cd84814d42John McCall LValue LV; 5789efdb83e26f9a1fd2566afe54461216cd84814d42John McCall if (!EvaluatePointer(E, LV, Info)) 57906dde0d5dc09f45f4d9508c964703e36fef1a0198Anders Carlsson return false; 57911e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith LV.moveInto(Result); 5792efdb83e26f9a1fd2566afe54461216cd84814d42John McCall } else if (E->getType()->isRealFloatingType()) { 5793efdb83e26f9a1fd2566afe54461216cd84814d42John McCall llvm::APFloat F(0.0); 5794efdb83e26f9a1fd2566afe54461216cd84814d42John McCall if (!EvaluateFloat(E, F, Info)) 57956dde0d5dc09f45f4d9508c964703e36fef1a0198Anders Carlsson return false; 579647a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith Result = CCValue(F); 5797efdb83e26f9a1fd2566afe54461216cd84814d42John McCall } else if (E->getType()->isAnyComplexType()) { 5798efdb83e26f9a1fd2566afe54461216cd84814d42John McCall ComplexValue C; 5799efdb83e26f9a1fd2566afe54461216cd84814d42John McCall if (!EvaluateComplex(E, C, Info)) 5800660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump return false; 58011e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith C.moveInto(Result); 580269c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith } else if (E->getType()->isMemberPointerType()) { 5803e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith MemberPtr P; 5804e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (!EvaluateMemberPointer(E, P, Info)) 5805e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return false; 5806e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith P.moveInto(Result); 5807e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith return true; 580851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith } else if (E->getType()->isArrayType()) { 5809180f47959a066795cc0f409433023af448bb0328Richard Smith LValue LV; 58101bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith LV.set(E, Info.CurrentCall); 5811180f47959a066795cc0f409433023af448bb0328Richard Smith if (!EvaluateArray(E, LV, Info.CurrentCall->Temporaries[E], Info)) 5812cc5d4f637cdf83adc174b96d2bfe27cef1cf0f36Richard Smith return false; 5813180f47959a066795cc0f409433023af448bb0328Richard Smith Result = Info.CurrentCall->Temporaries[E]; 581451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith } else if (E->getType()->isRecordType()) { 5815180f47959a066795cc0f409433023af448bb0328Richard Smith LValue LV; 58161bf9a9e6a5bdc0de7939908855dcddf46b661800Richard Smith LV.set(E, Info.CurrentCall); 5817180f47959a066795cc0f409433023af448bb0328Richard Smith if (!EvaluateRecord(E, LV, Info.CurrentCall->Temporaries[E], Info)) 5818180f47959a066795cc0f409433023af448bb0328Richard Smith return false; 5819180f47959a066795cc0f409433023af448bb0328Richard Smith Result = Info.CurrentCall->Temporaries[E]; 5820aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith } else if (E->getType()->isVoidType()) { 5821c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith if (Info.getLangOpts().CPlusPlus0x) 5822c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_nonliteral) 5823c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith << E->getType(); 5824c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith else 5825c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Info.CCEDiag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); 5826aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith if (!EvaluateVoid(E, Info)) 5827aa9c3503867bc52e1f61c4da676116db1b1cdf01Richard Smith return false; 5828c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } else if (Info.getLangOpts().CPlusPlus0x) { 5829c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Info.Diag(E->getExprLoc(), diag::note_constexpr_nonliteral) << E->getType(); 5830c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return false; 5831f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } else { 5832dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); 5833660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump return false; 5834f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 5835660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump 5836660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump return true; 5837660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump} 5838660e6f79a138a30a437c02142f23e7ef4eb21b2eMike Stump 583969c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith/// EvaluateConstantExpression - Evaluate an expression as a constant expression 584069c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith/// in-place in an APValue. In some cases, the in-place evaluation is essential, 584169c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith/// since later initializers for an object can indirectly refer to subobjects 584269c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith/// which were initialized earlier. 584369c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smithstatic bool EvaluateConstantExpression(APValue &Result, EvalInfo &Info, 5844c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const LValue &This, const Expr *E, 58457ca4850a3e3530fa6c93b64b740446e32c97f992Richard Smith CheckConstantExpressionKind CCEK, 58467ca4850a3e3530fa6c93b64b740446e32c97f992Richard Smith bool AllowNonLiteralTypes) { 58477ca4850a3e3530fa6c93b64b740446e32c97f992Richard Smith if (!AllowNonLiteralTypes && !CheckLiteralType(Info, E)) 584851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return false; 584951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 585051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (E->isRValue()) { 585169c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith // Evaluate arrays and record types in-place, so that later initializers can 585269c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith // refer to earlier-initialized members of the object. 5853180f47959a066795cc0f409433023af448bb0328Richard Smith if (E->getType()->isArrayType()) 5854180f47959a066795cc0f409433023af448bb0328Richard Smith return EvaluateArray(E, This, Result, Info); 5855180f47959a066795cc0f409433023af448bb0328Richard Smith else if (E->getType()->isRecordType()) 5856180f47959a066795cc0f409433023af448bb0328Richard Smith return EvaluateRecord(E, This, Result, Info); 585769c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith } 585869c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith 585969c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith // For any other type, in-place evaluation is unimportant. 586069c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith CCValue CoreConstResult; 586169c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith return Evaluate(CoreConstResult, Info, E) && 5862c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith CheckConstantExpression(Info, E, CoreConstResult, Result, CCEK); 586369c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith} 586469c2c50498dadfa6bb99baba52187e3cfa0ac78aRichard Smith 5865f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// EvaluateAsRValue - Try to evaluate this expression, performing an implicit 5866f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// lvalue-to-rvalue cast if it is an lvalue. 5867f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result) { 586851201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (!CheckLiteralType(Info, E)) 586951201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return false; 587051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 5871f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith CCValue Value; 5872f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!::Evaluate(Value, Info, E)) 5873f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 5874f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 5875f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (E->isGLValue()) { 5876f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith LValue LV; 5877f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith LV.setFrom(Value); 5878f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!HandleLValueToRValueConversion(Info, E, E->getType(), LV, Value)) 5879f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 5880f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 5881f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 5882f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith // Check this core constant expression is a constant expression, and if so, 5883f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith // convert it to one. 5884f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return CheckConstantExpression(Info, E, Value, Result); 5885f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith} 5886c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith 588751f4708c00110940ca3f337961915f2ca1668375Richard Smith/// EvaluateAsRValue - Return true if this is a constant which we can fold using 588856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall/// any crazy technique (that has nothing to do with language standards) that 588956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall/// we want to. If this function returns true, it returns the folded constant 5890c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// in Result. If this expression is a glvalue, an lvalue-to-rvalue conversion 5891c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith/// will be applied to the result. 589251f4708c00110940ca3f337961915f2ca1668375Richard Smithbool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx) const { 5893ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith // Fast-path evaluations of integer literals, since we sometimes see files 5894ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith // containing vast quantities of these. 5895ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith if (const IntegerLiteral *L = dyn_cast<IntegerLiteral>(this)) { 5896ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith Result.Val = APValue(APSInt(L->getValue(), 5897ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith L->getType()->isUnsignedIntegerType())); 5898ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith return true; 5899ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith } 5900ee19f43bf8973bfcccb7329e32a4198641767949Richard Smith 59012d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith // FIXME: Evaluating values of large array and record types can cause 59022d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith // performance problems. Only do so in C++11 for now. 5903e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith if (isRValue() && (getType()->isArrayType() || getType()->isRecordType()) && 5904e24f5fc8c763f1b5536b8d70dd510ca959db3a80Richard Smith !Ctx.getLangOptions().CPlusPlus0x) 59051445bbacf4c8de5f208ff4ccb302424a4d9e233eRichard Smith return false; 59061445bbacf4c8de5f208ff4ccb302424a4d9e233eRichard Smith 5907f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith EvalInfo Info(Ctx, Result); 5908f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return ::EvaluateAsRValue(Info, this, Result.Val); 590956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall} 591056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall 59114ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool Expr::EvaluateAsBooleanCondition(bool &Result, 59124ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad const ASTContext &Ctx) const { 5913c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith EvalResult Scratch; 591451f4708c00110940ca3f337961915f2ca1668375Richard Smith return EvaluateAsRValue(Scratch, Ctx) && 5915b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith HandleConversionToBool(CCValue(const_cast<ASTContext&>(Ctx), 5916b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith Scratch.Val, CCValue::GlobalValue()), 591747a1eed1cdd36edbefc318f29be6c0f3212b0c41Richard Smith Result); 5918cd7a445c6b46c5585580dfb652300c8483c0cb6bJohn McCall} 5919cd7a445c6b46c5585580dfb652300c8483c0cb6bJohn McCall 592080d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smithbool Expr::EvaluateAsInt(APSInt &Result, const ASTContext &Ctx, 592180d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith SideEffectsKind AllowSideEffects) const { 592280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith if (!getType()->isIntegralOrEnumerationType()) 592380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith return false; 592480d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith 5925c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith EvalResult ExprResult; 592680d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith if (!EvaluateAsRValue(ExprResult, Ctx) || !ExprResult.Val.isInt() || 592780d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith (!AllowSideEffects && ExprResult.HasSideEffects)) 5928c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return false; 5929f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 5930c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith Result = ExprResult.Val.getInt(); 5931c49bd11f96c2378969822f1f1b814ffa8f2bfee4Richard Smith return true; 5932a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith} 5933a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith 59344ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx) const { 59351b78276a75a5a0f496a82429c1ff9604d622a76dAnders Carlsson EvalInfo Info(Ctx, Result); 59361b78276a75a5a0f496a82429c1ff9604d622a76dAnders Carlsson 5937efdb83e26f9a1fd2566afe54461216cd84814d42John McCall LValue LV; 59389a17a680c74ef661bf3d864029adf7e74d9cb5b8Richard Smith return EvaluateLValue(this, LV, Info) && !Result.HasSideEffects && 5939c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith CheckLValueConstantExpression(Info, this, LV, Result.Val, 5940c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith CCEK_Constant); 5941b2f295c8050fb8c141bf2cf38eed0a56e99d0092Eli Friedman} 5942b2f295c8050fb8c141bf2cf38eed0a56e99d0092Eli Friedman 5943099e7f647ccda915513f2b2ec53352dc756082d3Richard Smithbool Expr::EvaluateAsInitializer(APValue &Value, const ASTContext &Ctx, 5944099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith const VarDecl *VD, 5945099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const { 59462d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith // FIXME: Evaluating initializers for large array and record types can cause 59472d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith // performance problems. Only do so in C++11 for now. 59482d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith if (isRValue() && (getType()->isArrayType() || getType()->isRecordType()) && 59492d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith !Ctx.getLangOptions().CPlusPlus0x) 59502d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith return false; 59512d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith 5952099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith Expr::EvalStatus EStatus; 5953099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith EStatus.Diag = &Notes; 5954099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith 5955099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith EvalInfo InitInfo(Ctx, EStatus); 5956099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith InitInfo.setEvaluatingDecl(VD, Value); 5957099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith 5958099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith LValue LVal; 5959099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith LVal.set(VD); 5960099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith 596151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith // C++11 [basic.start.init]p2: 596251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith // Variables with static storage duration or thread storage duration shall be 596351201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith // zero-initialized before any other initialization takes place. 596451201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith // This behavior is not present in C. 596551201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith if (Ctx.getLangOptions().CPlusPlus && !VD->hasLocalStorage() && 596651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith !VD->getType()->isReferenceType()) { 596751201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith ImplicitValueInitExpr VIE(VD->getType()); 59687ca4850a3e3530fa6c93b64b740446e32c97f992Richard Smith if (!EvaluateConstantExpression(Value, InitInfo, LVal, &VIE, CCEK_Constant, 59697ca4850a3e3530fa6c93b64b740446e32c97f992Richard Smith /*AllowNonLiteralTypes=*/true)) 597051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith return false; 597151201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith } 597251201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith 59737ca4850a3e3530fa6c93b64b740446e32c97f992Richard Smith return EvaluateConstantExpression(Value, InitInfo, LVal, this, CCEK_Constant, 59747ca4850a3e3530fa6c93b64b740446e32c97f992Richard Smith /*AllowNonLiteralTypes=*/true) && 5975099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith !EStatus.HasSideEffects; 5976099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith} 5977099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith 597851f4708c00110940ca3f337961915f2ca1668375Richard Smith/// isEvaluatable - Call EvaluateAsRValue to see if this expression can be 597951f4708c00110940ca3f337961915f2ca1668375Richard Smith/// constant folded, but discard the result. 59804ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool Expr::isEvaluatable(const ASTContext &Ctx) const { 59814fdfb0965b396f2778091f7e6c051d17ff9791baAnders Carlsson EvalResult Result; 598251f4708c00110940ca3f337961915f2ca1668375Richard Smith return EvaluateAsRValue(Result, Ctx) && !Result.HasSideEffects; 598345b6b9d080ac56917337d73d8f1cd6374b27b05dChris Lattner} 598451fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson 59854ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool Expr::HasSideEffects(const ASTContext &Ctx) const { 59861e12c59e8f9bb76c23628c4e0d0a1dfced0b1fa0Richard Smith return HasSideEffect(Ctx).Visit(this); 5987393c247fe025ccb5f914e37e948192ea86faef8cFariborz Jahanian} 5988393c247fe025ccb5f914e37e948192ea86faef8cFariborz Jahanian 5989a6b8b2c09610b8bc4330e948ece8b940c2386406Richard SmithAPSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx) const { 59901c0cfd4599e816cfd7a8f348286bf0ad79652ffcAnders Carlsson EvalResult EvalResult; 599151f4708c00110940ca3f337961915f2ca1668375Richard Smith bool Result = EvaluateAsRValue(EvalResult, Ctx); 5992c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin (void)Result; 599351fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson assert(Result && "Could not evaluate expression"); 59941c0cfd4599e816cfd7a8f348286bf0ad79652ffcAnders Carlsson assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer"); 599551fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson 59961c0cfd4599e816cfd7a8f348286bf0ad79652ffcAnders Carlsson return EvalResult.Val.getInt(); 599751fe996231b1d7199f76e4005ff4c943d5deeecdAnders Carlsson} 5998d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 5999e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara bool Expr::EvalResult::isGlobalLValue() const { 6000e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara assert(Val.isLValue()); 6001e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara return IsGlobalLValue(Val.getLValueBase()); 6002e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara } 6003e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara 6004e17a6436b429e4b18a5e157061fb13bbc677b3b0Abramo Bagnara 6005d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// isIntegerConstantExpr - this recursive routine will test if an expression is 6006d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// an integer constant expression. 6007d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 6008d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, 6009d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// comma, etc 6010d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// 6011d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof 6012d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer 6013d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall/// cast+dereference. 6014d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 6015d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// CheckICE - This function does the fundamental ICE checking: the returned 6016d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation. 6017d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// Note that to reduce code duplication, this helper does no evaluation 6018d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// itself; the caller checks whether the expression is evaluatable, and 6019d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// in the rare cases where CheckICE actually cares about the evaluated 6020d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// value, it calls into Evalute. 6021d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// 6022d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// Meanings of Val: 602351f4708c00110940ca3f337961915f2ca1668375Richard Smith// 0: This expression is an ICE. 6024d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// 1: This expression is not an ICE, but if it isn't evaluated, it's 6025d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// a legal subexpression for an ICE. This return value is used to handle 6026d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// the comma operator in C99 mode. 6027d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall// 2: This expression is not an ICE, and is not a legal subexpression for one. 6028d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 60293c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohmannamespace { 60303c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman 6031d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCallstruct ICEDiag { 6032d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall unsigned Val; 6033d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall SourceLocation Loc; 6034d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 6035d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall public: 6036d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {} 6037d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall ICEDiag() : Val(0) {} 6038d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall}; 6039d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 60403c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman} 60413c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohman 60423c46e8db99196179b30e7ac5c20c4efd5f3926d7Dan Gohmanstatic ICEDiag NoDiag() { return ICEDiag(); } 6043d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 6044d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCallstatic ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) { 6045d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall Expr::EvalResult EVResult; 604651f4708c00110940ca3f337961915f2ca1668375Richard Smith if (!E->EvaluateAsRValue(EVResult, Ctx) || EVResult.HasSideEffects || 6047d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall !EVResult.Val.isInt()) { 6048d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return ICEDiag(2, E->getLocStart()); 6049d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6050d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return NoDiag(); 6051d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall} 6052d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 6053d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCallstatic ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) { 6054d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall assert(!E->isValueDependent() && "Should not see value dependent exprs!"); 60552ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor if (!E->getType()->isIntegralOrEnumerationType()) { 6056d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return ICEDiag(2, E->getLocStart()); 6057d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6058d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 6059d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall switch (E->getStmtClass()) { 606063c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall#define ABSTRACT_STMT(Node) 6061d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall#define STMT(Node, Base) case Expr::Node##Class: 6062d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall#define EXPR(Node, Base) 6063d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall#include "clang/AST/StmtNodes.inc" 6064d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::PredefinedExprClass: 6065d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::FloatingLiteralClass: 6066d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ImaginaryLiteralClass: 6067d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::StringLiteralClass: 6068d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ArraySubscriptExprClass: 6069d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::MemberExprClass: 6070d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CompoundAssignOperatorClass: 6071d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CompoundLiteralExprClass: 6072d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ExtVectorElementExprClass: 6073d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::DesignatedInitExprClass: 6074d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ImplicitValueInitExprClass: 6075d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ParenListExprClass: 6076d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::VAArgExprClass: 6077d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::AddrLabelExprClass: 6078d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::StmtExprClass: 6079d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXMemberCallExprClass: 6080e08ce650a2b02410eddd1f60a4aa6b3d4be71e73Peter Collingbourne case Expr::CUDAKernelCallExprClass: 6081d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXDynamicCastExprClass: 6082d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXTypeidExprClass: 60839be88403e965cc49af76c9d33d818781d44b333eFrancois Pichet case Expr::CXXUuidofExprClass: 6084d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXNullPtrLiteralExprClass: 6085d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXThisExprClass: 6086d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXThrowExprClass: 6087d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXNewExprClass: 6088d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXDeleteExprClass: 6089d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXPseudoDestructorExprClass: 6090d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::UnresolvedLookupExprClass: 6091d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::DependentScopeDeclRefExprClass: 6092d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXConstructExprClass: 6093d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXBindTemporaryExprClass: 60944765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall case Expr::ExprWithCleanupsClass: 6095d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXTemporaryObjectExprClass: 6096d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXUnresolvedConstructExprClass: 6097d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXDependentScopeMemberExprClass: 6098d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::UnresolvedMemberExprClass: 6099d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ObjCStringLiteralClass: 6100d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ObjCEncodeExprClass: 6101d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ObjCMessageExprClass: 6102d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ObjCSelectorExprClass: 6103d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ObjCProtocolExprClass: 6104d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ObjCIvarRefExprClass: 6105d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ObjCPropertyRefExprClass: 6106d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ObjCIsaExprClass: 6107d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ShuffleVectorExprClass: 6108d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::BlockExprClass: 6109d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::BlockDeclRefExprClass: 6110d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::NoStmtClass: 61117cd7d1ad33fdf49eef83942e8855fe20d95aa1b9John McCall case Expr::OpaqueValueExprClass: 6112be230c36e32142cbdcdbe9c97511d097beeecbabDouglas Gregor case Expr::PackExpansionExprClass: 6113c7793c73ba8a343de3f2552d984851985a46f159Douglas Gregor case Expr::SubstNonTypeTemplateParmPackExprClass: 611461eee0ca33b29e102f11bab77c8b74cc00e2392bTanya Lattner case Expr::AsTypeExprClass: 6115f85e193739c953358c865005855253af4f68a497John McCall case Expr::ObjCIndirectCopyRestoreExprClass: 611603e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor case Expr::MaterializeTemporaryExprClass: 61174b9c2d235fb9449e249d74f48ecfec601650de93John McCall case Expr::PseudoObjectExprClass: 6118276b061970939293f1abaf694bd3ef05b2cbda79Eli Friedman case Expr::AtomicExprClass: 6119cea8d966f826554f0679595e9371e314e8dbc1cfSebastian Redl case Expr::InitListExprClass: 612001d08018b7cf5ce1601707cfd7a84d22015fc04eDouglas Gregor case Expr::LambdaExprClass: 6121cea8d966f826554f0679595e9371e314e8dbc1cfSebastian Redl return ICEDiag(2, E->getLocStart()); 6122cea8d966f826554f0679595e9371e314e8dbc1cfSebastian Redl 6123ee8aff06f6a96214731de17b2cb6df407c6c1820Douglas Gregor case Expr::SizeOfPackExprClass: 6124d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::GNUNullExprClass: 6125d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // GCC considers the GNU __null value to be an integral constant expression. 6126d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return NoDiag(); 6127d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 612891a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall case Expr::SubstNonTypeTemplateParmExprClass: 612991a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall return 613091a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall CheckICE(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(), Ctx); 613191a5755ad73c5dc1dfb167e448fdd74e75a6df56John McCall 6132d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ParenExprClass: 6133d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx); 6134f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne case Expr::GenericSelectionExprClass: 6135f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne return CheckICE(cast<GenericSelectionExpr>(E)->getResultExpr(), Ctx); 6136d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::IntegerLiteralClass: 6137d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CharacterLiteralClass: 6138d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXBoolLiteralExprClass: 6139ed8abf18329df67b0abcbb3a10458bd8c1d2a595Douglas Gregor case Expr::CXXScalarValueInitExprClass: 6140d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::UnaryTypeTraitExprClass: 61416ad6f2848d7652ab2991286eb48be440d3493b28Francois Pichet case Expr::BinaryTypeTraitExprClass: 614221ff2e516b0e0bc8c1dbf965cb3d44bac3c64330John Wiegley case Expr::ArrayTypeTraitExprClass: 6143552622067dc45013d240f73952fece703f5e63bdJohn Wiegley case Expr::ExpressionTraitExprClass: 61442e156225a29407a50dd19041aa5750171ad44ea3Sebastian Redl case Expr::CXXNoexceptExprClass: 6145d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return NoDiag(); 6146d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CallExprClass: 61476cf750298d3621d8a10a6dd07fcee8e274b9d94dSean Hunt case Expr::CXXOperatorCallExprClass: { 614805830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith // C99 6.6/3 allows function calls within unevaluated subexpressions of 614905830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith // constant expressions, but they can never be ICEs because an ICE cannot 615005830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith // contain an operand of (pointer to) function type. 6151d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall const CallExpr *CE = cast<CallExpr>(E); 6152180f47959a066795cc0f409433023af448bb0328Richard Smith if (CE->isBuiltinCall()) 6153d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return CheckEvalInICE(E, Ctx); 6154d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return ICEDiag(2, E->getLocStart()); 6155d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6156d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::DeclRefExprClass: 6157d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl())) 6158d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return NoDiag(); 615903f96110bc2c2c773e06a42982b17a03dd2e5379Richard Smith if (Ctx.getLangOptions().CPlusPlus && IsConstNonVolatile(E->getType())) { 6160d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl(); 6161d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 6162d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // Parameter variables are never constants. Without this check, 6163d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // getAnyInitializer() can find a default argument, which leads 6164d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // to chaos. 6165d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (isa<ParmVarDecl>(D)) 6166d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); 6167d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 6168d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // C++ 7.1.5.1p2 6169d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // A variable of non-volatile const-qualified integral or enumeration 6170d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // type initialized by an ICE can be used in ICEs. 6171d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) { 6172db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith if (!Dcl->getType()->isIntegralOrEnumerationType()) 6173db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); 6174db1822c6de43ff4aa5fa00234bf8222f6f4816e8Richard Smith 6175099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith const VarDecl *VD; 6176099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith // Look for a declaration of this variable that has an initializer, and 6177099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith // check whether it is an ICE. 6178099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith if (Dcl->getAnyInitializer(VD) && VD->checkInitIsICE()) 6179099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith return NoDiag(); 6180099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith else 6181099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); 6182d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6183d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6184d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return ICEDiag(2, E->getLocStart()); 6185d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::UnaryOperatorClass: { 6186d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall const UnaryOperator *Exp = cast<UnaryOperator>(E); 6187d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall switch (Exp->getOpcode()) { 61882de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_PostInc: 61892de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_PostDec: 61902de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_PreInc: 61912de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_PreDec: 61922de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_AddrOf: 61932de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_Deref: 619405830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith // C99 6.6/3 allows increment and decrement within unevaluated 619505830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith // subexpressions of constant expressions, but they can never be ICEs 619605830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith // because an ICE cannot contain an lvalue operand. 6197d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return ICEDiag(2, E->getLocStart()); 61982de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_Extension: 61992de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_LNot: 62002de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_Plus: 62012de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_Minus: 62022de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_Not: 62032de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_Real: 62042de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case UO_Imag: 6205d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return CheckICE(Exp->getSubExpr(), Ctx); 6206d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6207d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 6208d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // OffsetOf falls through here. 6209d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6210d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::OffsetOfExprClass: { 6211d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // Note that per C99, offsetof must be an ICE. And AFAIK, using 621251f4708c00110940ca3f337961915f2ca1668375Richard Smith // EvaluateAsRValue matches the proposed gcc behavior for cases like 621305830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith // "offsetof(struct s{int x[4];}, x[1.0])". This doesn't affect 6214d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // compliance: we should warn earlier for offsetof expressions with 6215d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // array subscripts that aren't ICEs, and if the array subscripts 6216d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // are ICEs, the value of the offsetof must be an integer constant. 6217d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return CheckEvalInICE(E, Ctx); 6218d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6219f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne case Expr::UnaryExprOrTypeTraitExprClass: { 6220f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(E); 6221f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne if ((Exp->getKind() == UETT_SizeOf) && 6222f4e3cfbe8abd124be6341ef5d714819b4fbd9082Peter Collingbourne Exp->getTypeOfArgument()->isVariableArrayType()) 6223d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return ICEDiag(2, E->getLocStart()); 6224d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return NoDiag(); 6225d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6226d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::BinaryOperatorClass: { 6227d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall const BinaryOperator *Exp = cast<BinaryOperator>(E); 6228d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall switch (Exp->getOpcode()) { 62292de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_PtrMemD: 62302de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_PtrMemI: 62312de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Assign: 62322de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_MulAssign: 62332de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_DivAssign: 62342de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_RemAssign: 62352de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_AddAssign: 62362de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_SubAssign: 62372de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_ShlAssign: 62382de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_ShrAssign: 62392de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_AndAssign: 62402de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_XorAssign: 62412de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_OrAssign: 624205830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith // C99 6.6/3 allows assignments within unevaluated subexpressions of 624305830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith // constant expressions, but they can never be ICEs because an ICE cannot 624405830143fa8c70b8bc46c96b93018455d8a2ca92Richard Smith // contain an lvalue operand. 6245d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return ICEDiag(2, E->getLocStart()); 6246d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 62472de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Mul: 62482de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Div: 62492de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Rem: 62502de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Add: 62512de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Sub: 62522de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Shl: 62532de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Shr: 62542de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_LT: 62552de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_GT: 62562de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_LE: 62572de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_GE: 62582de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_EQ: 62592de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_NE: 62602de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_And: 62612de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Xor: 62622de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Or: 62632de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_Comma: { 6264d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); 6265d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); 62662de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (Exp->getOpcode() == BO_Div || 62672de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall Exp->getOpcode() == BO_Rem) { 626851f4708c00110940ca3f337961915f2ca1668375Richard Smith // EvaluateAsRValue gives an error for undefined Div/Rem, so make sure 6269d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // we don't evaluate one. 62703b332ab132fa85c83833d74d400f6e126f52fbd2John McCall if (LHSResult.Val == 0 && RHSResult.Val == 0) { 6271a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith llvm::APSInt REval = Exp->getRHS()->EvaluateKnownConstInt(Ctx); 6272d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (REval == 0) 6273d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return ICEDiag(1, E->getLocStart()); 6274d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (REval.isSigned() && REval.isAllOnesValue()) { 6275a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith llvm::APSInt LEval = Exp->getLHS()->EvaluateKnownConstInt(Ctx); 6276d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (LEval.isMinSignedValue()) 6277d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return ICEDiag(1, E->getLocStart()); 6278d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6279d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6280d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 62812de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if (Exp->getOpcode() == BO_Comma) { 6282d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (Ctx.getLangOptions().C99) { 6283d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // C99 6.6p3 introduces a strange edge case: comma can be in an ICE 6284d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // if it isn't evaluated. 6285d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (LHSResult.Val == 0 && RHSResult.Val == 0) 6286d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return ICEDiag(1, E->getLocStart()); 6287d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } else { 6288d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // In both C89 and C++, commas in ICEs are illegal. 6289d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return ICEDiag(2, E->getLocStart()); 6290d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6291d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6292d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (LHSResult.Val >= RHSResult.Val) 6293d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return LHSResult; 6294d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return RHSResult; 6295d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 62962de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_LAnd: 62972de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_LOr: { 6298d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); 6299d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); 6300d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (LHSResult.Val == 0 && RHSResult.Val == 1) { 6301d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // Rare case where the RHS has a comma "side-effect"; we need 6302d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // to actually check the condition to see whether the side 6303d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // with the comma is evaluated. 63042de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall if ((Exp->getOpcode() == BO_LAnd) != 6305a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith (Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0)) 6306d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return RHSResult; 6307d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return NoDiag(); 6308d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6309d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 6310d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (LHSResult.Val >= RHSResult.Val) 6311d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return LHSResult; 6312d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return RHSResult; 6313d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6314d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6315d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6316d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ImplicitCastExprClass: 6317d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CStyleCastExprClass: 6318d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXFunctionalCastExprClass: 6319d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXStaticCastExprClass: 6320d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXReinterpretCastExprClass: 632132cb47174304bc7ec11478b9497c4e10f48273d9Richard Smith case Expr::CXXConstCastExprClass: 6322f85e193739c953358c865005855253af4f68a497John McCall case Expr::ObjCBridgedCastExprClass: { 6323d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr(); 63242116b144cf07f2574d20517187eb8863645376ebRichard Smith if (isa<ExplicitCastExpr>(E)) { 63252116b144cf07f2574d20517187eb8863645376ebRichard Smith if (const FloatingLiteral *FL 63262116b144cf07f2574d20517187eb8863645376ebRichard Smith = dyn_cast<FloatingLiteral>(SubExpr->IgnoreParenImpCasts())) { 63272116b144cf07f2574d20517187eb8863645376ebRichard Smith unsigned DestWidth = Ctx.getIntWidth(E->getType()); 63282116b144cf07f2574d20517187eb8863645376ebRichard Smith bool DestSigned = E->getType()->isSignedIntegerOrEnumerationType(); 63292116b144cf07f2574d20517187eb8863645376ebRichard Smith APSInt IgnoredVal(DestWidth, !DestSigned); 63302116b144cf07f2574d20517187eb8863645376ebRichard Smith bool Ignored; 63312116b144cf07f2574d20517187eb8863645376ebRichard Smith // If the value does not fit in the destination type, the behavior is 63322116b144cf07f2574d20517187eb8863645376ebRichard Smith // undefined, so we are not required to treat it as a constant 63332116b144cf07f2574d20517187eb8863645376ebRichard Smith // expression. 63342116b144cf07f2574d20517187eb8863645376ebRichard Smith if (FL->getValue().convertToInteger(IgnoredVal, 63352116b144cf07f2574d20517187eb8863645376ebRichard Smith llvm::APFloat::rmTowardZero, 63362116b144cf07f2574d20517187eb8863645376ebRichard Smith &Ignored) & APFloat::opInvalidOp) 63372116b144cf07f2574d20517187eb8863645376ebRichard Smith return ICEDiag(2, E->getLocStart()); 63382116b144cf07f2574d20517187eb8863645376ebRichard Smith return NoDiag(); 63392116b144cf07f2574d20517187eb8863645376ebRichard Smith } 63402116b144cf07f2574d20517187eb8863645376ebRichard Smith } 6341eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman switch (cast<CastExpr>(E)->getCastKind()) { 6342eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman case CK_LValueToRValue: 63437a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall case CK_AtomicToNonAtomic: 63447a7ee3033e44b45630981355460ef89efa0bdcc4David Chisnall case CK_NonAtomicToAtomic: 6345eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman case CK_NoOp: 6346eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman case CK_IntegralToBoolean: 6347eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman case CK_IntegralCast: 6348d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return CheckICE(SubExpr, Ctx); 6349eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman default: 6350eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman return ICEDiag(2, E->getLocStart()); 6351eea0e817c609c662f3fef61bb257fddf1ae8f7b7Eli Friedman } 6352d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 635356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall case Expr::BinaryConditionalOperatorClass: { 635456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(E); 635556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall ICEDiag CommonResult = CheckICE(Exp->getCommon(), Ctx); 635656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall if (CommonResult.Val == 2) return CommonResult; 635756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); 635856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall if (FalseResult.Val == 2) return FalseResult; 635956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall if (CommonResult.Val == 1) return CommonResult; 636056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall if (FalseResult.Val == 1 && 6361a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith Exp->getCommon()->EvaluateKnownConstInt(Ctx) == 0) return NoDiag(); 636256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall return FalseResult; 636356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall } 6364d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ConditionalOperatorClass: { 6365d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall const ConditionalOperator *Exp = cast<ConditionalOperator>(E); 6366d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // If the condition (ignoring parens) is a __builtin_constant_p call, 6367d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // then only the true side is actually considered in an integer constant 6368d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // expression, and it is fully evaluated. This is an important GNU 6369d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // extension. See GCC PR38377 for discussion. 6370d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (const CallExpr *CallCE 6371d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts())) 637280d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p) 637380d4b55db94db2172a04617d1a80feca6bbcea5cRichard Smith return CheckEvalInICE(E, Ctx); 6374d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx); 6375d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (CondResult.Val == 2) 6376d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return CondResult; 637763fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor 6378f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx); 6379f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); 638063fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor 6381d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (TrueResult.Val == 2) 6382d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return TrueResult; 6383d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (FalseResult.Val == 2) 6384d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return FalseResult; 6385d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (CondResult.Val == 1) 6386d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return CondResult; 6387d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (TrueResult.Val == 0 && FalseResult.Val == 0) 6388d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return NoDiag(); 6389d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // Rare case where the diagnostics depend on which side is evaluated 6390d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // Note that if we get here, CondResult is 0, and at least one of 6391d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall // TrueResult and FalseResult is non-zero. 6392a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith if (Exp->getCond()->EvaluateKnownConstInt(Ctx) == 0) { 6393d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return FalseResult; 6394d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6395d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return TrueResult; 6396d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6397d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::CXXDefaultArgExprClass: 6398d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx); 6399d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall case Expr::ChooseExprClass: { 6400d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx); 6401d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6402d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6403d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 64043026348bd4c13a0f83b59839f64065e0fcbea253David Blaikie llvm_unreachable("Invalid StmtClass!"); 6405d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall} 6406d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall 6407f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith/// Evaluate an expression as a C++11 integral constant expression. 6408f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithstatic bool EvaluateCPlusPlus11IntegralConstantExpr(ASTContext &Ctx, 6409f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith const Expr *E, 6410f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith llvm::APSInt *Value, 6411f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith SourceLocation *Loc) { 6412f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!E->getType()->isIntegralOrEnumerationType()) { 6413f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (Loc) *Loc = E->getExprLoc(); 6414f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 6415f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith } 6416f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 64174c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith APValue Result; 64184c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith if (!E->isCXX11ConstantExpr(Ctx, &Result, Loc)) 6419dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith return false; 6420dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith 64214c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith assert(Result.isInt() && "pointer cast to int is not an ICE"); 64224c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith if (Value) *Value = Result.getInt(); 6423dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smith return true; 6424f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith} 6425f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 6426dd1f29b6d686899bfd033f26e16cb1621e5549e8Richard Smithbool Expr::isIntegerConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const { 6427f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (Ctx.getLangOptions().CPlusPlus0x) 6428f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, 0, Loc); 6429f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 6430d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall ICEDiag d = CheckICE(this, Ctx); 6431d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (d.Val != 0) { 6432d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall if (Loc) *Loc = d.Loc; 6433d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return false; 6434d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall } 6435f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return true; 6436f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith} 6437f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 6438f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smithbool Expr::isIntegerConstantExpr(llvm::APSInt &Value, ASTContext &Ctx, 6439f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith SourceLocation *Loc, bool isEvaluated) const { 6440f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (Ctx.getLangOptions().CPlusPlus0x) 6441f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, &Value, Loc); 6442f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith 6443f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!isIntegerConstantExpr(Ctx, Loc)) 6444f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith return false; 6445f48fdb0937e67f691393f9ffdf75653e5128ea13Richard Smith if (!EvaluateAsInt(Value, Ctx)) 6446d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall llvm_unreachable("ICE cannot be evaluated!"); 6447d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall return true; 6448d905f5ad540c415d1a21b4f8b7bd715bfb7bb920John McCall} 64494c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith 645070488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smithbool Expr::isCXX98IntegralConstantExpr(ASTContext &Ctx) const { 645170488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smith return CheckICE(this, Ctx).Val == 0; 645270488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smith} 645370488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smith 64544c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smithbool Expr::isCXX11ConstantExpr(ASTContext &Ctx, APValue *Result, 64554c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith SourceLocation *Loc) const { 64564c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith // We support this checking in C++98 mode in order to diagnose compatibility 64574c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith // issues. 64584c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith assert(Ctx.getLangOptions().CPlusPlus); 64594c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith 646070488e201ccd94d4bb1ef0868cc13cca2b7d4ff6Richard Smith // Build evaluation settings. 64614c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith Expr::EvalStatus Status; 64624c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith llvm::SmallVector<PartialDiagnosticAt, 8> Diags; 64634c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith Status.Diag = &Diags; 64644c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith EvalInfo Info(Ctx, Status); 64654c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith 64664c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith APValue Scratch; 64674c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith bool IsConstExpr = ::EvaluateAsRValue(Info, this, Result ? *Result : Scratch); 64684c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith 64694c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith if (!Diags.empty()) { 64704c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith IsConstExpr = false; 64714c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith if (Loc) *Loc = Diags[0].first; 64724c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith } else if (!IsConstExpr) { 64734c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith // FIXME: This shouldn't happen. 64744c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith if (Loc) *Loc = getExprLoc(); 64754c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith } 64764c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith 64774c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith return IsConstExpr; 64784c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith} 6479745f5147e065900267c85a5568785a1991d4838fRichard Smith 6480745f5147e065900267c85a5568785a1991d4838fRichard Smithbool Expr::isPotentialConstantExpr(const FunctionDecl *FD, 6481745f5147e065900267c85a5568785a1991d4838fRichard Smith llvm::SmallVectorImpl< 6482745f5147e065900267c85a5568785a1991d4838fRichard Smith PartialDiagnosticAt> &Diags) { 6483745f5147e065900267c85a5568785a1991d4838fRichard Smith // FIXME: It would be useful to check constexpr function templates, but at the 6484745f5147e065900267c85a5568785a1991d4838fRichard Smith // moment the constant expression evaluator cannot cope with the non-rigorous 6485745f5147e065900267c85a5568785a1991d4838fRichard Smith // ASTs which we build for dependent expressions. 6486745f5147e065900267c85a5568785a1991d4838fRichard Smith if (FD->isDependentContext()) 6487745f5147e065900267c85a5568785a1991d4838fRichard Smith return true; 6488745f5147e065900267c85a5568785a1991d4838fRichard Smith 6489745f5147e065900267c85a5568785a1991d4838fRichard Smith Expr::EvalStatus Status; 6490745f5147e065900267c85a5568785a1991d4838fRichard Smith Status.Diag = &Diags; 6491745f5147e065900267c85a5568785a1991d4838fRichard Smith 6492745f5147e065900267c85a5568785a1991d4838fRichard Smith EvalInfo Info(FD->getASTContext(), Status); 6493745f5147e065900267c85a5568785a1991d4838fRichard Smith Info.CheckingPotentialConstantExpression = true; 6494745f5147e065900267c85a5568785a1991d4838fRichard Smith 6495745f5147e065900267c85a5568785a1991d4838fRichard Smith const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); 6496745f5147e065900267c85a5568785a1991d4838fRichard Smith const CXXRecordDecl *RD = MD ? MD->getParent()->getCanonicalDecl() : 0; 6497745f5147e065900267c85a5568785a1991d4838fRichard Smith 6498745f5147e065900267c85a5568785a1991d4838fRichard Smith // FIXME: Fabricate an arbitrary expression on the stack and pretend that it 6499745f5147e065900267c85a5568785a1991d4838fRichard Smith // is a temporary being used as the 'this' pointer. 6500745f5147e065900267c85a5568785a1991d4838fRichard Smith LValue This; 6501745f5147e065900267c85a5568785a1991d4838fRichard Smith ImplicitValueInitExpr VIE(RD ? Info.Ctx.getRecordType(RD) : Info.Ctx.IntTy); 6502745f5147e065900267c85a5568785a1991d4838fRichard Smith This.set(&VIE, Info.CurrentCall); 6503745f5147e065900267c85a5568785a1991d4838fRichard Smith 6504745f5147e065900267c85a5568785a1991d4838fRichard Smith APValue Scratch; 6505745f5147e065900267c85a5568785a1991d4838fRichard Smith ArrayRef<const Expr*> Args; 6506745f5147e065900267c85a5568785a1991d4838fRichard Smith 6507745f5147e065900267c85a5568785a1991d4838fRichard Smith SourceLocation Loc = FD->getLocation(); 6508745f5147e065900267c85a5568785a1991d4838fRichard Smith 6509745f5147e065900267c85a5568785a1991d4838fRichard Smith if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) { 6510745f5147e065900267c85a5568785a1991d4838fRichard Smith HandleConstructorCall(Loc, This, Args, CD, Info, Scratch); 6511745f5147e065900267c85a5568785a1991d4838fRichard Smith } else 6512745f5147e065900267c85a5568785a1991d4838fRichard Smith HandleFunctionCall(Loc, FD, (MD && MD->isInstance()) ? &This : 0, 6513745f5147e065900267c85a5568785a1991d4838fRichard Smith Args, FD->getBody(), Info, Scratch); 6514745f5147e065900267c85a5568785a1991d4838fRichard Smith 6515745f5147e065900267c85a5568785a1991d4838fRichard Smith return Diags.empty(); 6516745f5147e065900267c85a5568785a1991d4838fRichard Smith} 6517