Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1 | //===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===// |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Expr constant evaluator. |
| 11 | // |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 12 | // Constant expression evaluation produces four main results: |
| 13 | // |
| 14 | // * A success/failure flag indicating whether constant folding was successful. |
| 15 | // This is the 'bool' return value used by most of the code in this file. A |
| 16 | // 'false' return value indicates that constant folding has failed, and any |
| 17 | // appropriate diagnostic has already been produced. |
| 18 | // |
| 19 | // * An evaluated result, valid only if constant folding has not failed. |
| 20 | // |
| 21 | // * A flag indicating if evaluation encountered (unevaluated) side-effects. |
| 22 | // These arise in cases such as (sideEffect(), 0) and (sideEffect() || 1), |
| 23 | // where it is possible to determine the evaluated result regardless. |
| 24 | // |
| 25 | // * A set of notes indicating why the evaluation was not a constant expression |
| 26 | // (under the C++11 rules only, at the moment), or, if folding failed too, |
| 27 | // why the expression could not be folded. |
| 28 | // |
| 29 | // If we are checking for a potential constant expression, failure to constant |
| 30 | // fold a potential constant sub-expression will be indicated by a 'false' |
| 31 | // return value (the expression could not be folded) and no diagnostic (the |
| 32 | // expression is not necessarily non-constant). |
| 33 | // |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 34 | //===----------------------------------------------------------------------===// |
| 35 | |
| 36 | #include "clang/AST/APValue.h" |
| 37 | #include "clang/AST/ASTContext.h" |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 38 | #include "clang/AST/CharUnits.h" |
Anders Carlsson | 19cc4ab | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 39 | #include "clang/AST/RecordLayout.h" |
Seo Sanghyeon | 0fe52e1 | 2008-07-08 07:23:12 +0000 | [diff] [blame] | 40 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 41 | #include "clang/AST/TypeLoc.h" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 42 | #include "clang/AST/ASTDiagnostic.h" |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 43 | #include "clang/AST/Expr.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 44 | #include "clang/Basic/Builtins.h" |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 45 | #include "clang/Basic/TargetInfo.h" |
Mike Stump | 7462b39 | 2009-05-30 14:43:18 +0000 | [diff] [blame] | 46 | #include "llvm/ADT/SmallString.h" |
Mike Stump | 4572bab | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 47 | #include <cstring> |
Richard Smith | 7b48a29 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 48 | #include <functional> |
Mike Stump | 4572bab | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 49 | |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 50 | using namespace clang; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 51 | using llvm::APSInt; |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 52 | using llvm::APFloat; |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 53 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 54 | static bool IsGlobalLValue(APValue::LValueBase B); |
| 55 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 56 | namespace { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 57 | struct LValue; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 58 | struct CallStackFrame; |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 59 | struct EvalInfo; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 60 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 61 | static QualType getType(APValue::LValueBase B) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 62 | if (!B) return QualType(); |
| 63 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) |
| 64 | return D->getType(); |
| 65 | return B.get<const Expr*>()->getType(); |
| 66 | } |
| 67 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 68 | /// Get an LValue path entry, which is known to not be an array index, as a |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 69 | /// field or base class. |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 70 | static |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 71 | APValue::BaseOrMemberType getAsBaseOrMember(APValue::LValuePathEntry E) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 72 | APValue::BaseOrMemberType Value; |
| 73 | Value.setFromOpaqueValue(E.BaseOrMember); |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 74 | return Value; |
| 75 | } |
| 76 | |
| 77 | /// Get an LValue path entry, which is known to not be an array index, as a |
| 78 | /// field declaration. |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 79 | static const FieldDecl *getAsField(APValue::LValuePathEntry E) { |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 80 | return dyn_cast<FieldDecl>(getAsBaseOrMember(E).getPointer()); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 81 | } |
| 82 | /// Get an LValue path entry, which is known to not be an array index, as a |
| 83 | /// base class declaration. |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 84 | static const CXXRecordDecl *getAsBaseClass(APValue::LValuePathEntry E) { |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 85 | return dyn_cast<CXXRecordDecl>(getAsBaseOrMember(E).getPointer()); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 86 | } |
| 87 | /// Determine whether this LValue path entry for a base class names a virtual |
| 88 | /// base class. |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 89 | static bool isVirtualBaseClass(APValue::LValuePathEntry E) { |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 90 | return getAsBaseOrMember(E).getInt(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 93 | /// Find the path length and type of the most-derived subobject in the given |
| 94 | /// path, and find the size of the containing array, if any. |
| 95 | static |
| 96 | unsigned findMostDerivedSubobject(ASTContext &Ctx, QualType Base, |
| 97 | ArrayRef<APValue::LValuePathEntry> Path, |
| 98 | uint64_t &ArraySize, QualType &Type) { |
| 99 | unsigned MostDerivedLength = 0; |
| 100 | Type = Base; |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 101 | for (unsigned I = 0, N = Path.size(); I != N; ++I) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 102 | if (Type->isArrayType()) { |
| 103 | const ConstantArrayType *CAT = |
| 104 | cast<ConstantArrayType>(Ctx.getAsArrayType(Type)); |
| 105 | Type = CAT->getElementType(); |
| 106 | ArraySize = CAT->getSize().getZExtValue(); |
| 107 | MostDerivedLength = I + 1; |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 108 | } else if (Type->isAnyComplexType()) { |
| 109 | const ComplexType *CT = Type->castAs<ComplexType>(); |
| 110 | Type = CT->getElementType(); |
| 111 | ArraySize = 2; |
| 112 | MostDerivedLength = I + 1; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 113 | } else if (const FieldDecl *FD = getAsField(Path[I])) { |
| 114 | Type = FD->getType(); |
| 115 | ArraySize = 0; |
| 116 | MostDerivedLength = I + 1; |
| 117 | } else { |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 118 | // Path[I] describes a base class. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 119 | ArraySize = 0; |
| 120 | } |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 121 | } |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 122 | return MostDerivedLength; |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 125 | // The order of this enum is important for diagnostics. |
| 126 | enum CheckSubobjectKind { |
Richard Smith | b04035a | 2012-02-01 02:39:43 +0000 | [diff] [blame] | 127 | CSK_Base, CSK_Derived, CSK_Field, CSK_ArrayToPointer, CSK_ArrayIndex, |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 128 | CSK_This, CSK_Real, CSK_Imag |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 129 | }; |
| 130 | |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 131 | /// A path from a glvalue to a subobject of that glvalue. |
| 132 | struct SubobjectDesignator { |
| 133 | /// True if the subobject was named in a manner not supported by C++11. Such |
| 134 | /// lvalues can still be folded, but they are not core constant expressions |
| 135 | /// and we cannot perform lvalue-to-rvalue conversions on them. |
| 136 | bool Invalid : 1; |
| 137 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 138 | /// Is this a pointer one past the end of an object? |
| 139 | bool IsOnePastTheEnd : 1; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 140 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 141 | /// The length of the path to the most-derived object of which this is a |
| 142 | /// subobject. |
| 143 | unsigned MostDerivedPathLength : 30; |
| 144 | |
| 145 | /// The size of the array of which the most-derived object is an element, or |
| 146 | /// 0 if the most-derived object is not an array element. |
| 147 | uint64_t MostDerivedArraySize; |
| 148 | |
| 149 | /// The type of the most derived object referred to by this address. |
| 150 | QualType MostDerivedType; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 151 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 152 | typedef APValue::LValuePathEntry PathEntry; |
| 153 | |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 154 | /// The entries on the path from the glvalue to the designated subobject. |
| 155 | SmallVector<PathEntry, 8> Entries; |
| 156 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 157 | SubobjectDesignator() : Invalid(true) {} |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 158 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 159 | explicit SubobjectDesignator(QualType T) |
| 160 | : Invalid(false), IsOnePastTheEnd(false), MostDerivedPathLength(0), |
| 161 | MostDerivedArraySize(0), MostDerivedType(T) {} |
| 162 | |
| 163 | SubobjectDesignator(ASTContext &Ctx, const APValue &V) |
| 164 | : Invalid(!V.isLValue() || !V.hasLValuePath()), IsOnePastTheEnd(false), |
| 165 | MostDerivedPathLength(0), MostDerivedArraySize(0) { |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 166 | if (!Invalid) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 167 | IsOnePastTheEnd = V.isLValueOnePastTheEnd(); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 168 | ArrayRef<PathEntry> VEntries = V.getLValuePath(); |
| 169 | Entries.insert(Entries.end(), VEntries.begin(), VEntries.end()); |
| 170 | if (V.getLValueBase()) |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 171 | MostDerivedPathLength = |
| 172 | findMostDerivedSubobject(Ctx, getType(V.getLValueBase()), |
| 173 | V.getLValuePath(), MostDerivedArraySize, |
| 174 | MostDerivedType); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 178 | void setInvalid() { |
| 179 | Invalid = true; |
| 180 | Entries.clear(); |
| 181 | } |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 182 | |
| 183 | /// Determine whether this is a one-past-the-end pointer. |
| 184 | bool isOnePastTheEnd() const { |
| 185 | if (IsOnePastTheEnd) |
| 186 | return true; |
| 187 | if (MostDerivedArraySize && |
| 188 | Entries[MostDerivedPathLength - 1].ArrayIndex == MostDerivedArraySize) |
| 189 | return true; |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | /// Check that this refers to a valid subobject. |
| 194 | bool isValidSubobject() const { |
| 195 | if (Invalid) |
| 196 | return false; |
| 197 | return !isOnePastTheEnd(); |
| 198 | } |
| 199 | /// Check that this refers to a valid subobject, and if not, produce a |
| 200 | /// relevant diagnostic and set the designator as invalid. |
| 201 | bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK); |
| 202 | |
| 203 | /// Update this designator to refer to the first element within this array. |
| 204 | void addArrayUnchecked(const ConstantArrayType *CAT) { |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 205 | PathEntry Entry; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 206 | Entry.ArrayIndex = 0; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 207 | Entries.push_back(Entry); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 208 | |
| 209 | // This is a most-derived object. |
| 210 | MostDerivedType = CAT->getElementType(); |
| 211 | MostDerivedArraySize = CAT->getSize().getZExtValue(); |
| 212 | MostDerivedPathLength = Entries.size(); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 213 | } |
| 214 | /// Update this designator to refer to the given base or member of this |
| 215 | /// object. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 216 | void addDeclUnchecked(const Decl *D, bool Virtual = false) { |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 217 | PathEntry Entry; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 218 | APValue::BaseOrMemberType Value(D, Virtual); |
| 219 | Entry.BaseOrMember = Value.getOpaqueValue(); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 220 | Entries.push_back(Entry); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 221 | |
| 222 | // If this isn't a base class, it's a new most-derived object. |
| 223 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 224 | MostDerivedType = FD->getType(); |
| 225 | MostDerivedArraySize = 0; |
| 226 | MostDerivedPathLength = Entries.size(); |
| 227 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 228 | } |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 229 | /// Update this designator to refer to the given complex component. |
| 230 | void addComplexUnchecked(QualType EltTy, bool Imag) { |
| 231 | PathEntry Entry; |
| 232 | Entry.ArrayIndex = Imag; |
| 233 | Entries.push_back(Entry); |
| 234 | |
| 235 | // This is technically a most-derived object, though in practice this |
| 236 | // is unlikely to matter. |
| 237 | MostDerivedType = EltTy; |
| 238 | MostDerivedArraySize = 2; |
| 239 | MostDerivedPathLength = Entries.size(); |
| 240 | } |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 241 | void diagnosePointerArithmetic(EvalInfo &Info, const Expr *E, uint64_t N); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 242 | /// Add N to the address of this subobject. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 243 | void adjustIndex(EvalInfo &Info, const Expr *E, uint64_t N) { |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 244 | if (Invalid) return; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 245 | if (MostDerivedPathLength == Entries.size() && MostDerivedArraySize) { |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 246 | Entries.back().ArrayIndex += N; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 247 | if (Entries.back().ArrayIndex > MostDerivedArraySize) { |
| 248 | diagnosePointerArithmetic(Info, E, Entries.back().ArrayIndex); |
| 249 | setInvalid(); |
| 250 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 251 | return; |
| 252 | } |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 253 | // [expr.add]p4: For the purposes of these operators, a pointer to a |
| 254 | // nonarray object behaves the same as a pointer to the first element of |
| 255 | // an array of length one with the type of the object as its element type. |
| 256 | if (IsOnePastTheEnd && N == (uint64_t)-1) |
| 257 | IsOnePastTheEnd = false; |
| 258 | else if (!IsOnePastTheEnd && N == 1) |
| 259 | IsOnePastTheEnd = true; |
| 260 | else if (N != 0) { |
| 261 | diagnosePointerArithmetic(Info, E, uint64_t(IsOnePastTheEnd) + N); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 262 | setInvalid(); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 263 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 264 | } |
| 265 | }; |
| 266 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 267 | /// A stack frame in the constexpr call stack. |
| 268 | struct CallStackFrame { |
| 269 | EvalInfo &Info; |
| 270 | |
| 271 | /// Parent - The caller of this stack frame. |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 272 | CallStackFrame *Caller; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 273 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 274 | /// CallLoc - The location of the call expression for this call. |
| 275 | SourceLocation CallLoc; |
| 276 | |
| 277 | /// Callee - The function which was called. |
| 278 | const FunctionDecl *Callee; |
| 279 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 280 | /// Index - The call index of this call. |
| 281 | unsigned Index; |
| 282 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 283 | /// This - The binding for the this pointer in this call, if any. |
| 284 | const LValue *This; |
| 285 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 286 | /// ParmBindings - Parameter bindings for this function call, indexed by |
| 287 | /// parameters' function scope indices. |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 288 | const APValue *Arguments; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 289 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 290 | typedef llvm::DenseMap<const Expr*, APValue> MapTy; |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 291 | typedef MapTy::const_iterator temp_iterator; |
| 292 | /// Temporaries - Temporary lvalues materialized within this stack frame. |
| 293 | MapTy Temporaries; |
| 294 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 295 | CallStackFrame(EvalInfo &Info, SourceLocation CallLoc, |
| 296 | const FunctionDecl *Callee, const LValue *This, |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 297 | const APValue *Arguments); |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 298 | ~CallStackFrame(); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 299 | }; |
| 300 | |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 301 | /// A partial diagnostic which we might know in advance that we are not going |
| 302 | /// to emit. |
| 303 | class OptionalDiagnostic { |
| 304 | PartialDiagnostic *Diag; |
| 305 | |
| 306 | public: |
| 307 | explicit OptionalDiagnostic(PartialDiagnostic *Diag = 0) : Diag(Diag) {} |
| 308 | |
| 309 | template<typename T> |
| 310 | OptionalDiagnostic &operator<<(const T &v) { |
| 311 | if (Diag) |
| 312 | *Diag << v; |
| 313 | return *this; |
| 314 | } |
Richard Smith | 789f9b6 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 315 | |
| 316 | OptionalDiagnostic &operator<<(const APSInt &I) { |
| 317 | if (Diag) { |
| 318 | llvm::SmallVector<char, 32> Buffer; |
| 319 | I.toString(Buffer); |
| 320 | *Diag << StringRef(Buffer.data(), Buffer.size()); |
| 321 | } |
| 322 | return *this; |
| 323 | } |
| 324 | |
| 325 | OptionalDiagnostic &operator<<(const APFloat &F) { |
| 326 | if (Diag) { |
| 327 | llvm::SmallVector<char, 32> Buffer; |
| 328 | F.toString(Buffer); |
| 329 | *Diag << StringRef(Buffer.data(), Buffer.size()); |
| 330 | } |
| 331 | return *this; |
| 332 | } |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 333 | }; |
| 334 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 335 | /// EvalInfo - This is a private struct used by the evaluator to capture |
| 336 | /// information about a subexpression as it is folded. It retains information |
| 337 | /// about the AST context, but also maintains information about the folded |
| 338 | /// expression. |
| 339 | /// |
| 340 | /// If an expression could be evaluated, it is still possible it is not a C |
| 341 | /// "integer constant expression" or constant expression. If not, this struct |
| 342 | /// captures information about how and why not. |
| 343 | /// |
| 344 | /// One bit of information passed *into* the request for constant folding |
| 345 | /// indicates whether the subexpression is "evaluated" or not according to C |
| 346 | /// rules. For example, the RHS of (0 && foo()) is not evaluated. We can |
| 347 | /// evaluate the expression regardless of what the RHS is, but C only allows |
| 348 | /// certain things in certain situations. |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 349 | struct EvalInfo { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 350 | ASTContext &Ctx; |
Argyrios Kyrtzidis | d411a4b | 2012-02-27 20:21:34 +0000 | [diff] [blame] | 351 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 352 | /// EvalStatus - Contains information about the evaluation. |
| 353 | Expr::EvalStatus &EvalStatus; |
| 354 | |
| 355 | /// CurrentCall - The top of the constexpr call stack. |
| 356 | CallStackFrame *CurrentCall; |
| 357 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 358 | /// CallStackDepth - The number of calls in the call stack right now. |
| 359 | unsigned CallStackDepth; |
| 360 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 361 | /// NextCallIndex - The next call index to assign. |
| 362 | unsigned NextCallIndex; |
| 363 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 364 | typedef llvm::DenseMap<const OpaqueValueExpr*, APValue> MapTy; |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 365 | /// OpaqueValues - Values used as the common expression in a |
| 366 | /// BinaryConditionalOperator. |
| 367 | MapTy OpaqueValues; |
| 368 | |
| 369 | /// BottomFrame - The frame in which evaluation started. This must be |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 370 | /// initialized after CurrentCall and CallStackDepth. |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 371 | CallStackFrame BottomFrame; |
| 372 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 373 | /// EvaluatingDecl - This is the declaration whose initializer is being |
| 374 | /// evaluated, if any. |
| 375 | const VarDecl *EvaluatingDecl; |
| 376 | |
| 377 | /// EvaluatingDeclValue - This is the value being constructed for the |
| 378 | /// declaration whose initializer is being evaluated, if any. |
| 379 | APValue *EvaluatingDeclValue; |
| 380 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 381 | /// HasActiveDiagnostic - Was the previous diagnostic stored? If so, further |
| 382 | /// notes attached to it will also be stored, otherwise they will not be. |
| 383 | bool HasActiveDiagnostic; |
| 384 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 385 | /// CheckingPotentialConstantExpression - Are we checking whether the |
| 386 | /// expression is a potential constant expression? If so, some diagnostics |
| 387 | /// are suppressed. |
| 388 | bool CheckingPotentialConstantExpression; |
| 389 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 390 | EvalInfo(const ASTContext &C, Expr::EvalStatus &S) |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 391 | : Ctx(const_cast<ASTContext&>(C)), EvalStatus(S), CurrentCall(0), |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 392 | CallStackDepth(0), NextCallIndex(1), |
| 393 | BottomFrame(*this, SourceLocation(), 0, 0, 0), |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 394 | EvaluatingDecl(0), EvaluatingDeclValue(0), HasActiveDiagnostic(false), |
Argyrios Kyrtzidis | 649dfbc | 2012-03-15 18:07:13 +0000 | [diff] [blame] | 395 | CheckingPotentialConstantExpression(false) {} |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 396 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 397 | const APValue *getOpaqueValue(const OpaqueValueExpr *e) const { |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 398 | MapTy::const_iterator i = OpaqueValues.find(e); |
| 399 | if (i == OpaqueValues.end()) return 0; |
| 400 | return &i->second; |
| 401 | } |
| 402 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 403 | void setEvaluatingDecl(const VarDecl *VD, APValue &Value) { |
| 404 | EvaluatingDecl = VD; |
| 405 | EvaluatingDeclValue = &Value; |
| 406 | } |
| 407 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 408 | const LangOptions &getLangOpts() const { return Ctx.getLangOpts(); } |
Richard Smith | c18c423 | 2011-11-21 19:36:32 +0000 | [diff] [blame] | 409 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 410 | bool CheckCallLimit(SourceLocation Loc) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 411 | // Don't perform any constexpr calls (other than the call we're checking) |
| 412 | // when checking a potential constant expression. |
| 413 | if (CheckingPotentialConstantExpression && CallStackDepth > 1) |
| 414 | return false; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 415 | if (NextCallIndex == 0) { |
| 416 | // NextCallIndex has wrapped around. |
| 417 | Diag(Loc, diag::note_constexpr_call_limit_exceeded); |
| 418 | return false; |
| 419 | } |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 420 | if (CallStackDepth <= getLangOpts().ConstexprCallDepth) |
| 421 | return true; |
| 422 | Diag(Loc, diag::note_constexpr_depth_limit_exceeded) |
| 423 | << getLangOpts().ConstexprCallDepth; |
| 424 | return false; |
Richard Smith | c18c423 | 2011-11-21 19:36:32 +0000 | [diff] [blame] | 425 | } |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 426 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 427 | CallStackFrame *getCallFrame(unsigned CallIndex) { |
| 428 | assert(CallIndex && "no call index in getCallFrame"); |
| 429 | // We will eventually hit BottomFrame, which has Index 1, so Frame can't |
| 430 | // be null in this loop. |
| 431 | CallStackFrame *Frame = CurrentCall; |
| 432 | while (Frame->Index > CallIndex) |
| 433 | Frame = Frame->Caller; |
| 434 | return (Frame->Index == CallIndex) ? Frame : 0; |
| 435 | } |
| 436 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 437 | private: |
| 438 | /// Add a diagnostic to the diagnostics list. |
| 439 | PartialDiagnostic &addDiag(SourceLocation Loc, diag::kind DiagId) { |
| 440 | PartialDiagnostic PD(DiagId, Ctx.getDiagAllocator()); |
| 441 | EvalStatus.Diag->push_back(std::make_pair(Loc, PD)); |
| 442 | return EvalStatus.Diag->back().second; |
| 443 | } |
| 444 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 445 | /// Add notes containing a call stack to the current point of evaluation. |
| 446 | void addCallStack(unsigned Limit); |
| 447 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 448 | public: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 449 | /// Diagnose that the evaluation cannot be folded. |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 450 | OptionalDiagnostic Diag(SourceLocation Loc, diag::kind DiagId |
| 451 | = diag::note_invalid_subexpr_in_const_expr, |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 452 | unsigned ExtraNotes = 0) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 453 | // If we have a prior diagnostic, it will be noting that the expression |
| 454 | // isn't a constant expression. This diagnostic is more important. |
| 455 | // FIXME: We might want to show both diagnostics to the user. |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 456 | if (EvalStatus.Diag) { |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 457 | unsigned CallStackNotes = CallStackDepth - 1; |
| 458 | unsigned Limit = Ctx.getDiagnostics().getConstexprBacktraceLimit(); |
| 459 | if (Limit) |
| 460 | CallStackNotes = std::min(CallStackNotes, Limit + 1); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 461 | if (CheckingPotentialConstantExpression) |
| 462 | CallStackNotes = 0; |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 463 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 464 | HasActiveDiagnostic = true; |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 465 | EvalStatus.Diag->clear(); |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 466 | EvalStatus.Diag->reserve(1 + ExtraNotes + CallStackNotes); |
| 467 | addDiag(Loc, DiagId); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 468 | if (!CheckingPotentialConstantExpression) |
| 469 | addCallStack(Limit); |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 470 | return OptionalDiagnostic(&(*EvalStatus.Diag)[0].second); |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 471 | } |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 472 | HasActiveDiagnostic = false; |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 473 | return OptionalDiagnostic(); |
| 474 | } |
| 475 | |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 476 | OptionalDiagnostic Diag(const Expr *E, diag::kind DiagId |
| 477 | = diag::note_invalid_subexpr_in_const_expr, |
| 478 | unsigned ExtraNotes = 0) { |
| 479 | if (EvalStatus.Diag) |
| 480 | return Diag(E->getExprLoc(), DiagId, ExtraNotes); |
| 481 | HasActiveDiagnostic = false; |
| 482 | return OptionalDiagnostic(); |
| 483 | } |
| 484 | |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 485 | /// Diagnose that the evaluation does not produce a C++11 core constant |
| 486 | /// expression. |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 487 | template<typename LocArg> |
| 488 | OptionalDiagnostic CCEDiag(LocArg Loc, diag::kind DiagId |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 489 | = diag::note_invalid_subexpr_in_const_expr, |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 490 | unsigned ExtraNotes = 0) { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 491 | // Don't override a previous diagnostic. |
Eli Friedman | 51e47df | 2012-02-21 22:41:33 +0000 | [diff] [blame] | 492 | if (!EvalStatus.Diag || !EvalStatus.Diag->empty()) { |
| 493 | HasActiveDiagnostic = false; |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 494 | return OptionalDiagnostic(); |
Eli Friedman | 51e47df | 2012-02-21 22:41:33 +0000 | [diff] [blame] | 495 | } |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 496 | return Diag(Loc, DiagId, ExtraNotes); |
| 497 | } |
| 498 | |
| 499 | /// Add a note to a prior diagnostic. |
| 500 | OptionalDiagnostic Note(SourceLocation Loc, diag::kind DiagId) { |
| 501 | if (!HasActiveDiagnostic) |
| 502 | return OptionalDiagnostic(); |
| 503 | return OptionalDiagnostic(&addDiag(Loc, DiagId)); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 504 | } |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 505 | |
| 506 | /// Add a stack of notes to a prior diagnostic. |
| 507 | void addNotes(ArrayRef<PartialDiagnosticAt> Diags) { |
| 508 | if (HasActiveDiagnostic) { |
| 509 | EvalStatus.Diag->insert(EvalStatus.Diag->end(), |
| 510 | Diags.begin(), Diags.end()); |
| 511 | } |
| 512 | } |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 513 | |
| 514 | /// Should we continue evaluation as much as possible after encountering a |
| 515 | /// construct which can't be folded? |
| 516 | bool keepEvaluatingAfterFailure() { |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 517 | return CheckingPotentialConstantExpression && |
| 518 | EvalStatus.Diag && EvalStatus.Diag->empty(); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 519 | } |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 520 | }; |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 521 | |
| 522 | /// Object used to treat all foldable expressions as constant expressions. |
| 523 | struct FoldConstant { |
| 524 | bool Enabled; |
| 525 | |
| 526 | explicit FoldConstant(EvalInfo &Info) |
| 527 | : Enabled(Info.EvalStatus.Diag && Info.EvalStatus.Diag->empty() && |
| 528 | !Info.EvalStatus.HasSideEffects) { |
| 529 | } |
| 530 | // Treat the value we've computed since this object was created as constant. |
| 531 | void Fold(EvalInfo &Info) { |
| 532 | if (Enabled && !Info.EvalStatus.Diag->empty() && |
| 533 | !Info.EvalStatus.HasSideEffects) |
| 534 | Info.EvalStatus.Diag->clear(); |
| 535 | } |
| 536 | }; |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 537 | |
| 538 | /// RAII object used to suppress diagnostics and side-effects from a |
| 539 | /// speculative evaluation. |
| 540 | class SpeculativeEvaluationRAII { |
| 541 | EvalInfo &Info; |
| 542 | Expr::EvalStatus Old; |
| 543 | |
| 544 | public: |
| 545 | SpeculativeEvaluationRAII(EvalInfo &Info, |
| 546 | llvm::SmallVectorImpl<PartialDiagnosticAt> |
| 547 | *NewDiag = 0) |
| 548 | : Info(Info), Old(Info.EvalStatus) { |
| 549 | Info.EvalStatus.Diag = NewDiag; |
| 550 | } |
| 551 | ~SpeculativeEvaluationRAII() { |
| 552 | Info.EvalStatus = Old; |
| 553 | } |
| 554 | }; |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 555 | } |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 556 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 557 | bool SubobjectDesignator::checkSubobject(EvalInfo &Info, const Expr *E, |
| 558 | CheckSubobjectKind CSK) { |
| 559 | if (Invalid) |
| 560 | return false; |
| 561 | if (isOnePastTheEnd()) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 562 | Info.CCEDiag(E, diag::note_constexpr_past_end_subobject) |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 563 | << CSK; |
| 564 | setInvalid(); |
| 565 | return false; |
| 566 | } |
| 567 | return true; |
| 568 | } |
| 569 | |
| 570 | void SubobjectDesignator::diagnosePointerArithmetic(EvalInfo &Info, |
| 571 | const Expr *E, uint64_t N) { |
| 572 | if (MostDerivedPathLength == Entries.size() && MostDerivedArraySize) |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 573 | Info.CCEDiag(E, diag::note_constexpr_array_index) |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 574 | << static_cast<int>(N) << /*array*/ 0 |
| 575 | << static_cast<unsigned>(MostDerivedArraySize); |
| 576 | else |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 577 | Info.CCEDiag(E, diag::note_constexpr_array_index) |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 578 | << static_cast<int>(N) << /*non-array*/ 1; |
| 579 | setInvalid(); |
| 580 | } |
| 581 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 582 | CallStackFrame::CallStackFrame(EvalInfo &Info, SourceLocation CallLoc, |
| 583 | const FunctionDecl *Callee, const LValue *This, |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 584 | const APValue *Arguments) |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 585 | : Info(Info), Caller(Info.CurrentCall), CallLoc(CallLoc), Callee(Callee), |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 586 | Index(Info.NextCallIndex++), This(This), Arguments(Arguments) { |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 587 | Info.CurrentCall = this; |
| 588 | ++Info.CallStackDepth; |
| 589 | } |
| 590 | |
| 591 | CallStackFrame::~CallStackFrame() { |
| 592 | assert(Info.CurrentCall == this && "calls retired out of order"); |
| 593 | --Info.CallStackDepth; |
| 594 | Info.CurrentCall = Caller; |
| 595 | } |
| 596 | |
| 597 | /// Produce a string describing the given constexpr call. |
| 598 | static void describeCall(CallStackFrame *Frame, llvm::raw_ostream &Out) { |
| 599 | unsigned ArgIndex = 0; |
| 600 | bool IsMemberCall = isa<CXXMethodDecl>(Frame->Callee) && |
Richard Smith | 5ba73e1 | 2012-02-04 00:33:54 +0000 | [diff] [blame] | 601 | !isa<CXXConstructorDecl>(Frame->Callee) && |
| 602 | cast<CXXMethodDecl>(Frame->Callee)->isInstance(); |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 603 | |
| 604 | if (!IsMemberCall) |
| 605 | Out << *Frame->Callee << '('; |
| 606 | |
| 607 | for (FunctionDecl::param_const_iterator I = Frame->Callee->param_begin(), |
| 608 | E = Frame->Callee->param_end(); I != E; ++I, ++ArgIndex) { |
NAKAMURA Takumi | 5fe3122 | 2012-01-26 09:37:36 +0000 | [diff] [blame] | 609 | if (ArgIndex > (unsigned)IsMemberCall) |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 610 | Out << ", "; |
| 611 | |
| 612 | const ParmVarDecl *Param = *I; |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 613 | const APValue &Arg = Frame->Arguments[ArgIndex]; |
| 614 | Arg.printPretty(Out, Frame->Info.Ctx, Param->getType()); |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 615 | |
| 616 | if (ArgIndex == 0 && IsMemberCall) |
| 617 | Out << "->" << *Frame->Callee << '('; |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 618 | } |
| 619 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 620 | Out << ')'; |
| 621 | } |
| 622 | |
| 623 | void EvalInfo::addCallStack(unsigned Limit) { |
| 624 | // Determine which calls to skip, if any. |
| 625 | unsigned ActiveCalls = CallStackDepth - 1; |
| 626 | unsigned SkipStart = ActiveCalls, SkipEnd = SkipStart; |
| 627 | if (Limit && Limit < ActiveCalls) { |
| 628 | SkipStart = Limit / 2 + Limit % 2; |
| 629 | SkipEnd = ActiveCalls - Limit / 2; |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 632 | // Walk the call stack and add the diagnostics. |
| 633 | unsigned CallIdx = 0; |
| 634 | for (CallStackFrame *Frame = CurrentCall; Frame != &BottomFrame; |
| 635 | Frame = Frame->Caller, ++CallIdx) { |
| 636 | // Skip this call? |
| 637 | if (CallIdx >= SkipStart && CallIdx < SkipEnd) { |
| 638 | if (CallIdx == SkipStart) { |
| 639 | // Note that we're skipping calls. |
| 640 | addDiag(Frame->CallLoc, diag::note_constexpr_calls_suppressed) |
| 641 | << unsigned(ActiveCalls - Limit); |
| 642 | } |
| 643 | continue; |
| 644 | } |
| 645 | |
| 646 | llvm::SmallVector<char, 128> Buffer; |
| 647 | llvm::raw_svector_ostream Out(Buffer); |
| 648 | describeCall(Frame, Out); |
| 649 | addDiag(Frame->CallLoc, diag::note_constexpr_call_here) << Out.str(); |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | namespace { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 654 | struct ComplexValue { |
| 655 | private: |
| 656 | bool IsInt; |
| 657 | |
| 658 | public: |
| 659 | APSInt IntReal, IntImag; |
| 660 | APFloat FloatReal, FloatImag; |
| 661 | |
| 662 | ComplexValue() : FloatReal(APFloat::Bogus), FloatImag(APFloat::Bogus) {} |
| 663 | |
| 664 | void makeComplexFloat() { IsInt = false; } |
| 665 | bool isComplexFloat() const { return !IsInt; } |
| 666 | APFloat &getComplexFloatReal() { return FloatReal; } |
| 667 | APFloat &getComplexFloatImag() { return FloatImag; } |
| 668 | |
| 669 | void makeComplexInt() { IsInt = true; } |
| 670 | bool isComplexInt() const { return IsInt; } |
| 671 | APSInt &getComplexIntReal() { return IntReal; } |
| 672 | APSInt &getComplexIntImag() { return IntImag; } |
| 673 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 674 | void moveInto(APValue &v) const { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 675 | if (isComplexFloat()) |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 676 | v = APValue(FloatReal, FloatImag); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 677 | else |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 678 | v = APValue(IntReal, IntImag); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 679 | } |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 680 | void setFrom(const APValue &v) { |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 681 | assert(v.isComplexFloat() || v.isComplexInt()); |
| 682 | if (v.isComplexFloat()) { |
| 683 | makeComplexFloat(); |
| 684 | FloatReal = v.getComplexFloatReal(); |
| 685 | FloatImag = v.getComplexFloatImag(); |
| 686 | } else { |
| 687 | makeComplexInt(); |
| 688 | IntReal = v.getComplexIntReal(); |
| 689 | IntImag = v.getComplexIntImag(); |
| 690 | } |
| 691 | } |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 692 | }; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 693 | |
| 694 | struct LValue { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 695 | APValue::LValueBase Base; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 696 | CharUnits Offset; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 697 | unsigned CallIndex; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 698 | SubobjectDesignator Designator; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 699 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 700 | const APValue::LValueBase getLValueBase() const { return Base; } |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 701 | CharUnits &getLValueOffset() { return Offset; } |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 702 | const CharUnits &getLValueOffset() const { return Offset; } |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 703 | unsigned getLValueCallIndex() const { return CallIndex; } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 704 | SubobjectDesignator &getLValueDesignator() { return Designator; } |
| 705 | const SubobjectDesignator &getLValueDesignator() const { return Designator;} |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 706 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 707 | void moveInto(APValue &V) const { |
| 708 | if (Designator.Invalid) |
| 709 | V = APValue(Base, Offset, APValue::NoLValuePath(), CallIndex); |
| 710 | else |
| 711 | V = APValue(Base, Offset, Designator.Entries, |
| 712 | Designator.IsOnePastTheEnd, CallIndex); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 713 | } |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 714 | void setFrom(ASTContext &Ctx, const APValue &V) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 715 | assert(V.isLValue()); |
| 716 | Base = V.getLValueBase(); |
| 717 | Offset = V.getLValueOffset(); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 718 | CallIndex = V.getLValueCallIndex(); |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 719 | Designator = SubobjectDesignator(Ctx, V); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 722 | void set(APValue::LValueBase B, unsigned I = 0) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 723 | Base = B; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 724 | Offset = CharUnits::Zero(); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 725 | CallIndex = I; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 726 | Designator = SubobjectDesignator(getType(B)); |
| 727 | } |
| 728 | |
| 729 | // Check that this LValue is not based on a null pointer. If it is, produce |
| 730 | // a diagnostic and mark the designator as invalid. |
| 731 | bool checkNullPointer(EvalInfo &Info, const Expr *E, |
| 732 | CheckSubobjectKind CSK) { |
| 733 | if (Designator.Invalid) |
| 734 | return false; |
| 735 | if (!Base) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 736 | Info.CCEDiag(E, diag::note_constexpr_null_subobject) |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 737 | << CSK; |
| 738 | Designator.setInvalid(); |
| 739 | return false; |
| 740 | } |
| 741 | return true; |
| 742 | } |
| 743 | |
| 744 | // Check this LValue refers to an object. If not, set the designator to be |
| 745 | // invalid and emit a diagnostic. |
| 746 | bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 747 | // Outside C++11, do not build a designator referring to a subobject of |
| 748 | // any object: we won't use such a designator for anything. |
| 749 | if (!Info.getLangOpts().CPlusPlus0x) |
| 750 | Designator.setInvalid(); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 751 | return checkNullPointer(Info, E, CSK) && |
| 752 | Designator.checkSubobject(Info, E, CSK); |
| 753 | } |
| 754 | |
| 755 | void addDecl(EvalInfo &Info, const Expr *E, |
| 756 | const Decl *D, bool Virtual = false) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 757 | if (checkSubobject(Info, E, isa<FieldDecl>(D) ? CSK_Field : CSK_Base)) |
| 758 | Designator.addDeclUnchecked(D, Virtual); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 759 | } |
| 760 | void addArray(EvalInfo &Info, const Expr *E, const ConstantArrayType *CAT) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 761 | if (checkSubobject(Info, E, CSK_ArrayToPointer)) |
| 762 | Designator.addArrayUnchecked(CAT); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 763 | } |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 764 | void addComplex(EvalInfo &Info, const Expr *E, QualType EltTy, bool Imag) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 765 | if (checkSubobject(Info, E, Imag ? CSK_Imag : CSK_Real)) |
| 766 | Designator.addComplexUnchecked(EltTy, Imag); |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 767 | } |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 768 | void adjustIndex(EvalInfo &Info, const Expr *E, uint64_t N) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 769 | if (checkNullPointer(Info, E, CSK_ArrayIndex)) |
| 770 | Designator.adjustIndex(Info, E, N); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 771 | } |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 772 | }; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 773 | |
| 774 | struct MemberPtr { |
| 775 | MemberPtr() {} |
| 776 | explicit MemberPtr(const ValueDecl *Decl) : |
| 777 | DeclAndIsDerivedMember(Decl, false), Path() {} |
| 778 | |
| 779 | /// The member or (direct or indirect) field referred to by this member |
| 780 | /// pointer, or 0 if this is a null member pointer. |
| 781 | const ValueDecl *getDecl() const { |
| 782 | return DeclAndIsDerivedMember.getPointer(); |
| 783 | } |
| 784 | /// Is this actually a member of some type derived from the relevant class? |
| 785 | bool isDerivedMember() const { |
| 786 | return DeclAndIsDerivedMember.getInt(); |
| 787 | } |
| 788 | /// Get the class which the declaration actually lives in. |
| 789 | const CXXRecordDecl *getContainingRecord() const { |
| 790 | return cast<CXXRecordDecl>( |
| 791 | DeclAndIsDerivedMember.getPointer()->getDeclContext()); |
| 792 | } |
| 793 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 794 | void moveInto(APValue &V) const { |
| 795 | V = APValue(getDecl(), isDerivedMember(), Path); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 796 | } |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 797 | void setFrom(const APValue &V) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 798 | assert(V.isMemberPointer()); |
| 799 | DeclAndIsDerivedMember.setPointer(V.getMemberPointerDecl()); |
| 800 | DeclAndIsDerivedMember.setInt(V.isMemberPointerToDerivedMember()); |
| 801 | Path.clear(); |
| 802 | ArrayRef<const CXXRecordDecl*> P = V.getMemberPointerPath(); |
| 803 | Path.insert(Path.end(), P.begin(), P.end()); |
| 804 | } |
| 805 | |
| 806 | /// DeclAndIsDerivedMember - The member declaration, and a flag indicating |
| 807 | /// whether the member is a member of some class derived from the class type |
| 808 | /// of the member pointer. |
| 809 | llvm::PointerIntPair<const ValueDecl*, 1, bool> DeclAndIsDerivedMember; |
| 810 | /// Path - The path of base/derived classes from the member declaration's |
| 811 | /// class (exclusive) to the class type of the member pointer (inclusive). |
| 812 | SmallVector<const CXXRecordDecl*, 4> Path; |
| 813 | |
| 814 | /// Perform a cast towards the class of the Decl (either up or down the |
| 815 | /// hierarchy). |
| 816 | bool castBack(const CXXRecordDecl *Class) { |
| 817 | assert(!Path.empty()); |
| 818 | const CXXRecordDecl *Expected; |
| 819 | if (Path.size() >= 2) |
| 820 | Expected = Path[Path.size() - 2]; |
| 821 | else |
| 822 | Expected = getContainingRecord(); |
| 823 | if (Expected->getCanonicalDecl() != Class->getCanonicalDecl()) { |
| 824 | // C++11 [expr.static.cast]p12: In a conversion from (D::*) to (B::*), |
| 825 | // if B does not contain the original member and is not a base or |
| 826 | // derived class of the class containing the original member, the result |
| 827 | // of the cast is undefined. |
| 828 | // C++11 [conv.mem]p2 does not cover this case for a cast from (B::*) to |
| 829 | // (D::*). We consider that to be a language defect. |
| 830 | return false; |
| 831 | } |
| 832 | Path.pop_back(); |
| 833 | return true; |
| 834 | } |
| 835 | /// Perform a base-to-derived member pointer cast. |
| 836 | bool castToDerived(const CXXRecordDecl *Derived) { |
| 837 | if (!getDecl()) |
| 838 | return true; |
| 839 | if (!isDerivedMember()) { |
| 840 | Path.push_back(Derived); |
| 841 | return true; |
| 842 | } |
| 843 | if (!castBack(Derived)) |
| 844 | return false; |
| 845 | if (Path.empty()) |
| 846 | DeclAndIsDerivedMember.setInt(false); |
| 847 | return true; |
| 848 | } |
| 849 | /// Perform a derived-to-base member pointer cast. |
| 850 | bool castToBase(const CXXRecordDecl *Base) { |
| 851 | if (!getDecl()) |
| 852 | return true; |
| 853 | if (Path.empty()) |
| 854 | DeclAndIsDerivedMember.setInt(true); |
| 855 | if (isDerivedMember()) { |
| 856 | Path.push_back(Base); |
| 857 | return true; |
| 858 | } |
| 859 | return castBack(Base); |
| 860 | } |
| 861 | }; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 862 | |
Richard Smith | b02e462 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 863 | /// Compare two member pointers, which are assumed to be of the same type. |
| 864 | static bool operator==(const MemberPtr &LHS, const MemberPtr &RHS) { |
| 865 | if (!LHS.getDecl() || !RHS.getDecl()) |
| 866 | return !LHS.getDecl() && !RHS.getDecl(); |
| 867 | if (LHS.getDecl()->getCanonicalDecl() != RHS.getDecl()->getCanonicalDecl()) |
| 868 | return false; |
| 869 | return LHS.Path == RHS.Path; |
| 870 | } |
| 871 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 872 | /// Kinds of constant expression checking, for diagnostics. |
| 873 | enum CheckConstantExpressionKind { |
| 874 | CCEK_Constant, ///< A normal constant. |
| 875 | CCEK_ReturnValue, ///< A constexpr function return value. |
| 876 | CCEK_MemberInit ///< A constexpr constructor mem-initializer. |
| 877 | }; |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 878 | } |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 879 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 880 | static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 881 | static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, |
| 882 | const LValue &This, const Expr *E, |
| 883 | CheckConstantExpressionKind CCEK = CCEK_Constant, |
| 884 | bool AllowNonLiteralTypes = false); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 885 | static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info); |
| 886 | static bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 887 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
| 888 | EvalInfo &Info); |
| 889 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info); |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 890 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info); |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 891 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 892 | EvalInfo &Info); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 893 | static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 894 | static bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 895 | |
| 896 | //===----------------------------------------------------------------------===// |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 897 | // Misc utilities |
| 898 | //===----------------------------------------------------------------------===// |
| 899 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 900 | /// Should this call expression be treated as a string literal? |
| 901 | static bool IsStringLiteralCall(const CallExpr *E) { |
| 902 | unsigned Builtin = E->isBuiltinCall(); |
| 903 | return (Builtin == Builtin::BI__builtin___CFStringMakeConstantString || |
| 904 | Builtin == Builtin::BI__builtin___NSStringMakeConstantString); |
| 905 | } |
| 906 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 907 | static bool IsGlobalLValue(APValue::LValueBase B) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 908 | // C++11 [expr.const]p3 An address constant expression is a prvalue core |
| 909 | // constant expression of pointer type that evaluates to... |
| 910 | |
| 911 | // ... a null pointer value, or a prvalue core constant expression of type |
| 912 | // std::nullptr_t. |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 913 | if (!B) return true; |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 914 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 915 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
| 916 | // ... the address of an object with static storage duration, |
| 917 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 918 | return VD->hasGlobalStorage(); |
| 919 | // ... the address of a function, |
| 920 | return isa<FunctionDecl>(D); |
| 921 | } |
| 922 | |
| 923 | const Expr *E = B.get<const Expr*>(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 924 | switch (E->getStmtClass()) { |
| 925 | default: |
| 926 | return false; |
Richard Smith | b78ae97 | 2012-02-18 04:58:18 +0000 | [diff] [blame] | 927 | case Expr::CompoundLiteralExprClass: { |
| 928 | const CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E); |
| 929 | return CLE->isFileScope() && CLE->isLValue(); |
| 930 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 931 | // A string literal has static storage duration. |
| 932 | case Expr::StringLiteralClass: |
| 933 | case Expr::PredefinedExprClass: |
| 934 | case Expr::ObjCStringLiteralClass: |
| 935 | case Expr::ObjCEncodeExprClass: |
Richard Smith | 47d2145 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 936 | case Expr::CXXTypeidExprClass: |
Francois Pichet | e275a18 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 937 | case Expr::CXXUuidofExprClass: |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 938 | return true; |
| 939 | case Expr::CallExprClass: |
| 940 | return IsStringLiteralCall(cast<CallExpr>(E)); |
| 941 | // For GCC compatibility, &&label has static storage duration. |
| 942 | case Expr::AddrLabelExprClass: |
| 943 | return true; |
| 944 | // A Block literal expression may be used as the initialization value for |
| 945 | // Block variables at global or local static scope. |
| 946 | case Expr::BlockExprClass: |
| 947 | return !cast<BlockExpr>(E)->getBlockDecl()->hasCaptures(); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 948 | case Expr::ImplicitValueInitExprClass: |
| 949 | // FIXME: |
| 950 | // We can never form an lvalue with an implicit value initialization as its |
| 951 | // base through expression evaluation, so these only appear in one case: the |
| 952 | // implicit variable declaration we invent when checking whether a constexpr |
| 953 | // constructor can produce a constant expression. We must assume that such |
| 954 | // an expression might be a global lvalue. |
| 955 | return true; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 956 | } |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 957 | } |
| 958 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 959 | static void NoteLValueLocation(EvalInfo &Info, APValue::LValueBase Base) { |
| 960 | assert(Base && "no location for a null lvalue"); |
| 961 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
| 962 | if (VD) |
| 963 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 964 | else |
| 965 | Info.Note(Base.dyn_cast<const Expr*>()->getExprLoc(), |
| 966 | diag::note_constexpr_temporary_here); |
| 967 | } |
| 968 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 969 | /// Check that this reference or pointer core constant expression is a valid |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 970 | /// value for an address or reference constant expression. Return true if we |
| 971 | /// can fold this expression, whether or not it's a constant expression. |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 972 | static bool CheckLValueConstantExpression(EvalInfo &Info, SourceLocation Loc, |
| 973 | QualType Type, const LValue &LVal) { |
| 974 | bool IsReferenceType = Type->isReferenceType(); |
| 975 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 976 | APValue::LValueBase Base = LVal.getLValueBase(); |
| 977 | const SubobjectDesignator &Designator = LVal.getLValueDesignator(); |
| 978 | |
Richard Smith | b78ae97 | 2012-02-18 04:58:18 +0000 | [diff] [blame] | 979 | // Check that the object is a global. Note that the fake 'this' object we |
| 980 | // manufacture when checking potential constant expressions is conservatively |
| 981 | // assumed to be global here. |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 982 | if (!IsGlobalLValue(Base)) { |
| 983 | if (Info.getLangOpts().CPlusPlus0x) { |
| 984 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 985 | Info.Diag(Loc, diag::note_constexpr_non_global, 1) |
| 986 | << IsReferenceType << !Designator.Entries.empty() |
| 987 | << !!VD << VD; |
| 988 | NoteLValueLocation(Info, Base); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 989 | } else { |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 990 | Info.Diag(Loc); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 991 | } |
Richard Smith | 61e6162 | 2012-01-12 06:08:57 +0000 | [diff] [blame] | 992 | // Don't allow references to temporaries to escape. |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 993 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 994 | } |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 995 | assert((Info.CheckingPotentialConstantExpression || |
| 996 | LVal.getLValueCallIndex() == 0) && |
| 997 | "have call index for global lvalue"); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 998 | |
| 999 | // Allow address constant expressions to be past-the-end pointers. This is |
| 1000 | // an extension: the standard requires them to point to an object. |
| 1001 | if (!IsReferenceType) |
| 1002 | return true; |
| 1003 | |
| 1004 | // A reference constant expression must refer to an object. |
| 1005 | if (!Base) { |
| 1006 | // FIXME: diagnostic |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1007 | Info.CCEDiag(Loc); |
Richard Smith | 61e6162 | 2012-01-12 06:08:57 +0000 | [diff] [blame] | 1008 | return true; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1011 | // Does this refer one past the end of some object? |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1012 | if (Designator.isOnePastTheEnd()) { |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1013 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1014 | Info.Diag(Loc, diag::note_constexpr_past_end, 1) |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1015 | << !Designator.Entries.empty() << !!VD << VD; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1016 | NoteLValueLocation(Info, Base); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1019 | return true; |
| 1020 | } |
| 1021 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1022 | /// Check that this core constant expression is of literal type, and if not, |
| 1023 | /// produce an appropriate diagnostic. |
| 1024 | static bool CheckLiteralType(EvalInfo &Info, const Expr *E) { |
| 1025 | if (!E->isRValue() || E->getType()->isLiteralType()) |
| 1026 | return true; |
| 1027 | |
| 1028 | // Prvalue constant expressions must be of literal types. |
| 1029 | if (Info.getLangOpts().CPlusPlus0x) |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1030 | Info.Diag(E, diag::note_constexpr_nonliteral) |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1031 | << E->getType(); |
| 1032 | else |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1033 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1034 | return false; |
| 1035 | } |
| 1036 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1037 | /// Check that this core constant expression value is a valid value for a |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1038 | /// constant expression. If not, report an appropriate diagnostic. Does not |
| 1039 | /// check that the expression is of literal type. |
| 1040 | static bool CheckConstantExpression(EvalInfo &Info, SourceLocation DiagLoc, |
| 1041 | QualType Type, const APValue &Value) { |
| 1042 | // Core issue 1454: For a literal constant expression of array or class type, |
| 1043 | // each subobject of its value shall have been initialized by a constant |
| 1044 | // expression. |
| 1045 | if (Value.isArray()) { |
| 1046 | QualType EltTy = Type->castAsArrayTypeUnsafe()->getElementType(); |
| 1047 | for (unsigned I = 0, N = Value.getArrayInitializedElts(); I != N; ++I) { |
| 1048 | if (!CheckConstantExpression(Info, DiagLoc, EltTy, |
| 1049 | Value.getArrayInitializedElt(I))) |
| 1050 | return false; |
| 1051 | } |
| 1052 | if (!Value.hasArrayFiller()) |
| 1053 | return true; |
| 1054 | return CheckConstantExpression(Info, DiagLoc, EltTy, |
| 1055 | Value.getArrayFiller()); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1056 | } |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1057 | if (Value.isUnion() && Value.getUnionField()) { |
| 1058 | return CheckConstantExpression(Info, DiagLoc, |
| 1059 | Value.getUnionField()->getType(), |
| 1060 | Value.getUnionValue()); |
| 1061 | } |
| 1062 | if (Value.isStruct()) { |
| 1063 | RecordDecl *RD = Type->castAs<RecordType>()->getDecl(); |
| 1064 | if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD)) { |
| 1065 | unsigned BaseIndex = 0; |
| 1066 | for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(), |
| 1067 | End = CD->bases_end(); I != End; ++I, ++BaseIndex) { |
| 1068 | if (!CheckConstantExpression(Info, DiagLoc, I->getType(), |
| 1069 | Value.getStructBase(BaseIndex))) |
| 1070 | return false; |
| 1071 | } |
| 1072 | } |
| 1073 | for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 1074 | I != E; ++I) { |
| 1075 | if (!CheckConstantExpression(Info, DiagLoc, (*I)->getType(), |
| 1076 | Value.getStructField((*I)->getFieldIndex()))) |
| 1077 | return false; |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | if (Value.isLValue()) { |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1082 | LValue LVal; |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1083 | LVal.setFrom(Info.Ctx, Value); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1084 | return CheckLValueConstantExpression(Info, DiagLoc, Type, LVal); |
| 1085 | } |
| 1086 | |
| 1087 | // Everything else is fine. |
| 1088 | return true; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 1091 | const ValueDecl *GetLValueBaseDecl(const LValue &LVal) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1092 | return LVal.Base.dyn_cast<const ValueDecl*>(); |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | static bool IsLiteralLValue(const LValue &Value) { |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1096 | return Value.Base.dyn_cast<const Expr*>() && !Value.CallIndex; |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1099 | static bool IsWeakLValue(const LValue &Value) { |
| 1100 | const ValueDecl *Decl = GetLValueBaseDecl(Value); |
Lang Hames | 0dd7a25 | 2011-12-05 20:16:26 +0000 | [diff] [blame] | 1101 | return Decl && Decl->isWeak(); |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1104 | static bool EvalPointerValueAsBool(const APValue &Value, bool &Result) { |
John McCall | 3554283 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 1105 | // A null base expression indicates a null pointer. These are always |
| 1106 | // evaluatable, and they are false unless the offset is zero. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1107 | if (!Value.getLValueBase()) { |
| 1108 | Result = !Value.getLValueOffset().isZero(); |
John McCall | 3554283 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 1109 | return true; |
| 1110 | } |
Rafael Espindola | a7d3c04 | 2010-05-07 15:18:43 +0000 | [diff] [blame] | 1111 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1112 | // We have a non-null base. These are generally known to be true, but if it's |
| 1113 | // a weak declaration it can be null at runtime. |
John McCall | 3554283 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 1114 | Result = true; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1115 | const ValueDecl *Decl = Value.getLValueBase().dyn_cast<const ValueDecl*>(); |
Lang Hames | 0dd7a25 | 2011-12-05 20:16:26 +0000 | [diff] [blame] | 1116 | return !Decl || !Decl->isWeak(); |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1119 | static bool HandleConversionToBool(const APValue &Val, bool &Result) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1120 | switch (Val.getKind()) { |
| 1121 | case APValue::Uninitialized: |
| 1122 | return false; |
| 1123 | case APValue::Int: |
| 1124 | Result = Val.getInt().getBoolValue(); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1125 | return true; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1126 | case APValue::Float: |
| 1127 | Result = !Val.getFloat().isZero(); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1128 | return true; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1129 | case APValue::ComplexInt: |
| 1130 | Result = Val.getComplexIntReal().getBoolValue() || |
| 1131 | Val.getComplexIntImag().getBoolValue(); |
| 1132 | return true; |
| 1133 | case APValue::ComplexFloat: |
| 1134 | Result = !Val.getComplexFloatReal().isZero() || |
| 1135 | !Val.getComplexFloatImag().isZero(); |
| 1136 | return true; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1137 | case APValue::LValue: |
| 1138 | return EvalPointerValueAsBool(Val, Result); |
| 1139 | case APValue::MemberPointer: |
| 1140 | Result = Val.getMemberPointerDecl(); |
| 1141 | return true; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1142 | case APValue::Vector: |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1143 | case APValue::Array: |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1144 | case APValue::Struct: |
| 1145 | case APValue::Union: |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 1146 | case APValue::AddrLabelDiff: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1147 | return false; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1150 | llvm_unreachable("unknown APValue kind"); |
| 1151 | } |
| 1152 | |
| 1153 | static bool EvaluateAsBooleanCondition(const Expr *E, bool &Result, |
| 1154 | EvalInfo &Info) { |
| 1155 | assert(E->isRValue() && "missing lvalue-to-rvalue conv in bool condition"); |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1156 | APValue Val; |
Argyrios Kyrtzidis | d411a4b | 2012-02-27 20:21:34 +0000 | [diff] [blame] | 1157 | if (!Evaluate(Val, Info, E)) |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1158 | return false; |
Argyrios Kyrtzidis | d411a4b | 2012-02-27 20:21:34 +0000 | [diff] [blame] | 1159 | return HandleConversionToBool(Val, Result); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1162 | template<typename T> |
| 1163 | static bool HandleOverflow(EvalInfo &Info, const Expr *E, |
| 1164 | const T &SrcValue, QualType DestType) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1165 | Info.Diag(E, diag::note_constexpr_overflow) |
Richard Smith | 789f9b6 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 1166 | << SrcValue << DestType; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1167 | return false; |
| 1168 | } |
| 1169 | |
| 1170 | static bool HandleFloatToIntCast(EvalInfo &Info, const Expr *E, |
| 1171 | QualType SrcType, const APFloat &Value, |
| 1172 | QualType DestType, APSInt &Result) { |
| 1173 | unsigned DestWidth = Info.Ctx.getIntWidth(DestType); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1174 | // Determine whether we are converting to unsigned or signed. |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 1175 | bool DestSigned = DestType->isSignedIntegerOrEnumerationType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1176 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1177 | Result = APSInt(DestWidth, !DestSigned); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1178 | bool ignored; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1179 | if (Value.convertToInteger(Result, llvm::APFloat::rmTowardZero, &ignored) |
| 1180 | & APFloat::opInvalidOp) |
| 1181 | return HandleOverflow(Info, E, Value, DestType); |
| 1182 | return true; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1185 | static bool HandleFloatToFloatCast(EvalInfo &Info, const Expr *E, |
| 1186 | QualType SrcType, QualType DestType, |
| 1187 | APFloat &Result) { |
| 1188 | APFloat Value = Result; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1189 | bool ignored; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1190 | if (Result.convert(Info.Ctx.getFloatTypeSemantics(DestType), |
| 1191 | APFloat::rmNearestTiesToEven, &ignored) |
| 1192 | & APFloat::opOverflow) |
| 1193 | return HandleOverflow(Info, E, Value, DestType); |
| 1194 | return true; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 1197 | static APSInt HandleIntToIntCast(EvalInfo &Info, const Expr *E, |
| 1198 | QualType DestType, QualType SrcType, |
| 1199 | APSInt &Value) { |
| 1200 | unsigned DestWidth = Info.Ctx.getIntWidth(DestType); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1201 | APSInt Result = Value; |
| 1202 | // Figure out if this is a truncate, extend or noop cast. |
| 1203 | // If the input is signed, do a sign extend, noop, or truncate. |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1204 | Result = Result.extOrTrunc(DestWidth); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 1205 | Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType()); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1206 | return Result; |
| 1207 | } |
| 1208 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1209 | static bool HandleIntToFloatCast(EvalInfo &Info, const Expr *E, |
| 1210 | QualType SrcType, const APSInt &Value, |
| 1211 | QualType DestType, APFloat &Result) { |
| 1212 | Result = APFloat(Info.Ctx.getFloatTypeSemantics(DestType), 1); |
| 1213 | if (Result.convertFromAPInt(Value, Value.isSigned(), |
| 1214 | APFloat::rmNearestTiesToEven) |
| 1215 | & APFloat::opOverflow) |
| 1216 | return HandleOverflow(Info, E, Value, DestType); |
| 1217 | return true; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
Eli Friedman | e6a24e8 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 1220 | static bool EvalAndBitcastToAPInt(EvalInfo &Info, const Expr *E, |
| 1221 | llvm::APInt &Res) { |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1222 | APValue SVal; |
Eli Friedman | e6a24e8 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 1223 | if (!Evaluate(SVal, Info, E)) |
| 1224 | return false; |
| 1225 | if (SVal.isInt()) { |
| 1226 | Res = SVal.getInt(); |
| 1227 | return true; |
| 1228 | } |
| 1229 | if (SVal.isFloat()) { |
| 1230 | Res = SVal.getFloat().bitcastToAPInt(); |
| 1231 | return true; |
| 1232 | } |
| 1233 | if (SVal.isVector()) { |
| 1234 | QualType VecTy = E->getType(); |
| 1235 | unsigned VecSize = Info.Ctx.getTypeSize(VecTy); |
| 1236 | QualType EltTy = VecTy->castAs<VectorType>()->getElementType(); |
| 1237 | unsigned EltSize = Info.Ctx.getTypeSize(EltTy); |
| 1238 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
| 1239 | Res = llvm::APInt::getNullValue(VecSize); |
| 1240 | for (unsigned i = 0; i < SVal.getVectorLength(); i++) { |
| 1241 | APValue &Elt = SVal.getVectorElt(i); |
| 1242 | llvm::APInt EltAsInt; |
| 1243 | if (Elt.isInt()) { |
| 1244 | EltAsInt = Elt.getInt(); |
| 1245 | } else if (Elt.isFloat()) { |
| 1246 | EltAsInt = Elt.getFloat().bitcastToAPInt(); |
| 1247 | } else { |
| 1248 | // Don't try to handle vectors of anything other than int or float |
| 1249 | // (not sure if it's possible to hit this case). |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1250 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Eli Friedman | e6a24e8 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 1251 | return false; |
| 1252 | } |
| 1253 | unsigned BaseEltSize = EltAsInt.getBitWidth(); |
| 1254 | if (BigEndian) |
| 1255 | Res |= EltAsInt.zextOrTrunc(VecSize).rotr(i*EltSize+BaseEltSize); |
| 1256 | else |
| 1257 | Res |= EltAsInt.zextOrTrunc(VecSize).rotl(i*EltSize); |
| 1258 | } |
| 1259 | return true; |
| 1260 | } |
| 1261 | // Give up if the input isn't an int, float, or vector. For example, we |
| 1262 | // reject "(v4i16)(intptr_t)&a". |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1263 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Eli Friedman | e6a24e8 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 1264 | return false; |
| 1265 | } |
| 1266 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1267 | /// Cast an lvalue referring to a base subobject to a derived class, by |
| 1268 | /// truncating the lvalue's path to the given length. |
| 1269 | static bool CastToDerivedClass(EvalInfo &Info, const Expr *E, LValue &Result, |
| 1270 | const RecordDecl *TruncatedType, |
| 1271 | unsigned TruncatedElements) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1272 | SubobjectDesignator &D = Result.Designator; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1273 | |
| 1274 | // Check we actually point to a derived class object. |
| 1275 | if (TruncatedElements == D.Entries.size()) |
| 1276 | return true; |
| 1277 | assert(TruncatedElements >= D.MostDerivedPathLength && |
| 1278 | "not casting to a derived class"); |
| 1279 | if (!Result.checkSubobject(Info, E, CSK_Derived)) |
| 1280 | return false; |
| 1281 | |
| 1282 | // Truncate the path to the subobject, and remove any derived-to-base offsets. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1283 | const RecordDecl *RD = TruncatedType; |
| 1284 | for (unsigned I = TruncatedElements, N = D.Entries.size(); I != N; ++I) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1285 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 1286 | const CXXRecordDecl *Base = getAsBaseClass(D.Entries[I]); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1287 | if (isVirtualBaseClass(D.Entries[I])) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1288 | Result.Offset -= Layout.getVBaseClassOffset(Base); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1289 | else |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1290 | Result.Offset -= Layout.getBaseClassOffset(Base); |
| 1291 | RD = Base; |
| 1292 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1293 | D.Entries.resize(TruncatedElements); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1294 | return true; |
| 1295 | } |
| 1296 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1297 | static void HandleLValueDirectBase(EvalInfo &Info, const Expr *E, LValue &Obj, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1298 | const CXXRecordDecl *Derived, |
| 1299 | const CXXRecordDecl *Base, |
| 1300 | const ASTRecordLayout *RL = 0) { |
| 1301 | if (!RL) RL = &Info.Ctx.getASTRecordLayout(Derived); |
| 1302 | Obj.getLValueOffset() += RL->getBaseClassOffset(Base); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1303 | Obj.addDecl(Info, E, Base, /*Virtual*/ false); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1306 | static bool HandleLValueBase(EvalInfo &Info, const Expr *E, LValue &Obj, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1307 | const CXXRecordDecl *DerivedDecl, |
| 1308 | const CXXBaseSpecifier *Base) { |
| 1309 | const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl(); |
| 1310 | |
| 1311 | if (!Base->isVirtual()) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1312 | HandleLValueDirectBase(Info, E, Obj, DerivedDecl, BaseDecl); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1313 | return true; |
| 1314 | } |
| 1315 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1316 | SubobjectDesignator &D = Obj.Designator; |
| 1317 | if (D.Invalid) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1318 | return false; |
| 1319 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1320 | // Extract most-derived object and corresponding type. |
| 1321 | DerivedDecl = D.MostDerivedType->getAsCXXRecordDecl(); |
| 1322 | if (!CastToDerivedClass(Info, E, Obj, DerivedDecl, D.MostDerivedPathLength)) |
| 1323 | return false; |
| 1324 | |
| 1325 | // Find the virtual base class. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1326 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(DerivedDecl); |
| 1327 | Obj.getLValueOffset() += Layout.getVBaseClassOffset(BaseDecl); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1328 | Obj.addDecl(Info, E, BaseDecl, /*Virtual*/ true); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1329 | return true; |
| 1330 | } |
| 1331 | |
| 1332 | /// Update LVal to refer to the given field, which must be a member of the type |
| 1333 | /// currently described by LVal. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1334 | static void HandleLValueMember(EvalInfo &Info, const Expr *E, LValue &LVal, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1335 | const FieldDecl *FD, |
| 1336 | const ASTRecordLayout *RL = 0) { |
| 1337 | if (!RL) |
| 1338 | RL = &Info.Ctx.getASTRecordLayout(FD->getParent()); |
| 1339 | |
| 1340 | unsigned I = FD->getFieldIndex(); |
| 1341 | LVal.Offset += Info.Ctx.toCharUnitsFromBits(RL->getFieldOffset(I)); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1342 | LVal.addDecl(Info, E, FD); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 1345 | /// Update LVal to refer to the given indirect field. |
| 1346 | static void HandleLValueIndirectMember(EvalInfo &Info, const Expr *E, |
| 1347 | LValue &LVal, |
| 1348 | const IndirectFieldDecl *IFD) { |
| 1349 | for (IndirectFieldDecl::chain_iterator C = IFD->chain_begin(), |
| 1350 | CE = IFD->chain_end(); C != CE; ++C) |
| 1351 | HandleLValueMember(Info, E, LVal, cast<FieldDecl>(*C)); |
| 1352 | } |
| 1353 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1354 | /// Get the size of the given type in char units. |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1355 | static bool HandleSizeof(EvalInfo &Info, SourceLocation Loc, |
| 1356 | QualType Type, CharUnits &Size) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1357 | // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc |
| 1358 | // extension. |
| 1359 | if (Type->isVoidType() || Type->isFunctionType()) { |
| 1360 | Size = CharUnits::One(); |
| 1361 | return true; |
| 1362 | } |
| 1363 | |
| 1364 | if (!Type->isConstantSizeType()) { |
| 1365 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1366 | // FIXME: Better diagnostic. |
| 1367 | Info.Diag(Loc); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1368 | return false; |
| 1369 | } |
| 1370 | |
| 1371 | Size = Info.Ctx.getTypeSizeInChars(Type); |
| 1372 | return true; |
| 1373 | } |
| 1374 | |
| 1375 | /// Update a pointer value to model pointer arithmetic. |
| 1376 | /// \param Info - Information about the ongoing evaluation. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1377 | /// \param E - The expression being evaluated, for diagnostic purposes. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1378 | /// \param LVal - The pointer value to be updated. |
| 1379 | /// \param EltTy - The pointee type represented by LVal. |
| 1380 | /// \param Adjustment - The adjustment, in objects of type EltTy, to add. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1381 | static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E, |
| 1382 | LValue &LVal, QualType EltTy, |
| 1383 | int64_t Adjustment) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1384 | CharUnits SizeOfPointee; |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1385 | if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfPointee)) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1386 | return false; |
| 1387 | |
| 1388 | // Compute the new offset in the appropriate width. |
| 1389 | LVal.Offset += Adjustment * SizeOfPointee; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1390 | LVal.adjustIndex(Info, E, Adjustment); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1391 | return true; |
| 1392 | } |
| 1393 | |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1394 | /// Update an lvalue to refer to a component of a complex number. |
| 1395 | /// \param Info - Information about the ongoing evaluation. |
| 1396 | /// \param LVal - The lvalue to be updated. |
| 1397 | /// \param EltTy - The complex number's component type. |
| 1398 | /// \param Imag - False for the real component, true for the imaginary. |
| 1399 | static bool HandleLValueComplexElement(EvalInfo &Info, const Expr *E, |
| 1400 | LValue &LVal, QualType EltTy, |
| 1401 | bool Imag) { |
| 1402 | if (Imag) { |
| 1403 | CharUnits SizeOfComponent; |
| 1404 | if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfComponent)) |
| 1405 | return false; |
| 1406 | LVal.Offset += SizeOfComponent; |
| 1407 | } |
| 1408 | LVal.addComplex(Info, E, EltTy, Imag); |
| 1409 | return true; |
| 1410 | } |
| 1411 | |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1412 | /// Try to evaluate the initializer for a variable declaration. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1413 | static bool EvaluateVarDeclInit(EvalInfo &Info, const Expr *E, |
| 1414 | const VarDecl *VD, |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1415 | CallStackFrame *Frame, APValue &Result) { |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1416 | // If this is a parameter to an active constexpr function call, perform |
| 1417 | // argument substitution. |
| 1418 | if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(VD)) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1419 | // Assume arguments of a potential constant expression are unknown |
| 1420 | // constant expressions. |
| 1421 | if (Info.CheckingPotentialConstantExpression) |
| 1422 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1423 | if (!Frame || !Frame->Arguments) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1424 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1425 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1426 | } |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1427 | Result = Frame->Arguments[PVD->getFunctionScopeIndex()]; |
| 1428 | return true; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1429 | } |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1430 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1431 | // Dig out the initializer, and use the declaration which it's attached to. |
| 1432 | const Expr *Init = VD->getAnyInitializer(VD); |
| 1433 | if (!Init || Init->isValueDependent()) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1434 | // If we're checking a potential constant expression, the variable could be |
| 1435 | // initialized later. |
| 1436 | if (!Info.CheckingPotentialConstantExpression) |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1437 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1438 | return false; |
| 1439 | } |
| 1440 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1441 | // If we're currently evaluating the initializer of this declaration, use that |
| 1442 | // in-flight value. |
| 1443 | if (Info.EvaluatingDecl == VD) { |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1444 | Result = *Info.EvaluatingDeclValue; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1445 | return !Result.isUninit(); |
| 1446 | } |
| 1447 | |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1448 | // Never evaluate the initializer of a weak variable. We can't be sure that |
| 1449 | // this is the definition which will be used. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1450 | if (VD->isWeak()) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1451 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1452 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1453 | } |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1454 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1455 | // Check that we can fold the initializer. In C++, we will have already done |
| 1456 | // this in the cases where it matters for conformance. |
| 1457 | llvm::SmallVector<PartialDiagnosticAt, 8> Notes; |
| 1458 | if (!VD->evaluateValue(Notes)) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1459 | Info.Diag(E, diag::note_constexpr_var_init_non_constant, |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1460 | Notes.size() + 1) << VD; |
| 1461 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1462 | Info.addNotes(Notes); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1463 | return false; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1464 | } else if (!VD->checkInitIsICE()) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1465 | Info.CCEDiag(E, diag::note_constexpr_var_init_non_constant, |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1466 | Notes.size() + 1) << VD; |
| 1467 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1468 | Info.addNotes(Notes); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1469 | } |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1470 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1471 | Result = *VD->getEvaluatedValue(); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1472 | return true; |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1473 | } |
| 1474 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1475 | static bool IsConstNonVolatile(QualType T) { |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1476 | Qualifiers Quals = T.getQualifiers(); |
| 1477 | return Quals.hasConst() && !Quals.hasVolatile(); |
| 1478 | } |
| 1479 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1480 | /// Get the base index of the given base class within an APValue representing |
| 1481 | /// the given derived class. |
| 1482 | static unsigned getBaseIndex(const CXXRecordDecl *Derived, |
| 1483 | const CXXRecordDecl *Base) { |
| 1484 | Base = Base->getCanonicalDecl(); |
| 1485 | unsigned Index = 0; |
| 1486 | for (CXXRecordDecl::base_class_const_iterator I = Derived->bases_begin(), |
| 1487 | E = Derived->bases_end(); I != E; ++I, ++Index) { |
| 1488 | if (I->getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base) |
| 1489 | return Index; |
| 1490 | } |
| 1491 | |
| 1492 | llvm_unreachable("base class missing from derived class's bases list"); |
| 1493 | } |
| 1494 | |
Richard Smith | fe58720 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 1495 | /// Extract the value of a character from a string literal. CharType is used to |
| 1496 | /// determine the expected signedness of the result -- a string literal used to |
| 1497 | /// initialize an array of 'signed char' or 'unsigned char' might contain chars |
| 1498 | /// of the wrong signedness. |
Richard Smith | f3908f2 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 1499 | static APSInt ExtractStringLiteralCharacter(EvalInfo &Info, const Expr *Lit, |
Richard Smith | fe58720 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 1500 | uint64_t Index, QualType CharType) { |
Richard Smith | f3908f2 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 1501 | // FIXME: Support PredefinedExpr, ObjCEncodeExpr, MakeStringConstant |
| 1502 | const StringLiteral *S = dyn_cast<StringLiteral>(Lit); |
| 1503 | assert(S && "unexpected string literal expression kind"); |
Richard Smith | fe58720 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 1504 | assert(CharType->isIntegerType() && "unexpected character type"); |
Richard Smith | f3908f2 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 1505 | |
| 1506 | APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(), |
Richard Smith | fe58720 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 1507 | CharType->isUnsignedIntegerType()); |
Richard Smith | f3908f2 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 1508 | if (Index < S->getLength()) |
| 1509 | Value = S->getCodeUnit(Index); |
| 1510 | return Value; |
| 1511 | } |
| 1512 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1513 | /// Extract the designated sub-object of an rvalue. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1514 | static bool ExtractSubobject(EvalInfo &Info, const Expr *E, |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1515 | APValue &Obj, QualType ObjType, |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1516 | const SubobjectDesignator &Sub, QualType SubType) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1517 | if (Sub.Invalid) |
| 1518 | // A diagnostic will have already been produced. |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1519 | return false; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1520 | if (Sub.isOnePastTheEnd()) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1521 | Info.Diag(E, Info.getLangOpts().CPlusPlus0x ? |
Matt Beaumont-Gay | aa5d533 | 2011-12-21 19:36:37 +0000 | [diff] [blame] | 1522 | (unsigned)diag::note_constexpr_read_past_end : |
| 1523 | (unsigned)diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1524 | return false; |
| 1525 | } |
Richard Smith | f64699e | 2011-11-11 08:28:03 +0000 | [diff] [blame] | 1526 | if (Sub.Entries.empty()) |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1527 | return true; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1528 | if (Info.CheckingPotentialConstantExpression && Obj.isUninit()) |
| 1529 | // This object might be initialized later. |
| 1530 | return false; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1531 | |
Richard Smith | 0069b84 | 2012-03-10 00:28:11 +0000 | [diff] [blame] | 1532 | APValue *O = &Obj; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1533 | // Walk the designator's path to find the subobject. |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1534 | for (unsigned I = 0, N = Sub.Entries.size(); I != N; ++I) { |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1535 | if (ObjType->isArrayType()) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1536 | // Next subobject is an array element. |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1537 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(ObjType); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1538 | assert(CAT && "vla in literal type?"); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1539 | uint64_t Index = Sub.Entries[I].ArrayIndex; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1540 | if (CAT->getSize().ule(Index)) { |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1541 | // Note, it should not be possible to form a pointer with a valid |
| 1542 | // designator which points more than one past the end of the array. |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1543 | Info.Diag(E, Info.getLangOpts().CPlusPlus0x ? |
Matt Beaumont-Gay | aa5d533 | 2011-12-21 19:36:37 +0000 | [diff] [blame] | 1544 | (unsigned)diag::note_constexpr_read_past_end : |
| 1545 | (unsigned)diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1546 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1547 | } |
Richard Smith | f3908f2 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 1548 | // An array object is represented as either an Array APValue or as an |
| 1549 | // LValue which refers to a string literal. |
| 1550 | if (O->isLValue()) { |
| 1551 | assert(I == N - 1 && "extracting subobject of character?"); |
| 1552 | assert(!O->hasLValuePath() || O->getLValuePath().empty()); |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1553 | Obj = APValue(ExtractStringLiteralCharacter( |
Richard Smith | fe58720 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 1554 | Info, O->getLValueBase().get<const Expr*>(), Index, SubType)); |
Richard Smith | f3908f2 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 1555 | return true; |
| 1556 | } else if (O->getArrayInitializedElts() > Index) |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1557 | O = &O->getArrayInitializedElt(Index); |
| 1558 | else |
| 1559 | O = &O->getArrayFiller(); |
| 1560 | ObjType = CAT->getElementType(); |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1561 | } else if (ObjType->isAnyComplexType()) { |
| 1562 | // Next subobject is a complex number. |
| 1563 | uint64_t Index = Sub.Entries[I].ArrayIndex; |
| 1564 | if (Index > 1) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1565 | Info.Diag(E, Info.getLangOpts().CPlusPlus0x ? |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1566 | (unsigned)diag::note_constexpr_read_past_end : |
| 1567 | (unsigned)diag::note_invalid_subexpr_in_const_expr); |
| 1568 | return false; |
| 1569 | } |
| 1570 | assert(I == N - 1 && "extracting subobject of scalar?"); |
| 1571 | if (O->isComplexInt()) { |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1572 | Obj = APValue(Index ? O->getComplexIntImag() |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1573 | : O->getComplexIntReal()); |
| 1574 | } else { |
| 1575 | assert(O->isComplexFloat()); |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1576 | Obj = APValue(Index ? O->getComplexFloatImag() |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1577 | : O->getComplexFloatReal()); |
| 1578 | } |
| 1579 | return true; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1580 | } else if (const FieldDecl *Field = getAsField(Sub.Entries[I])) { |
Richard Smith | b4e5e28 | 2012-02-09 03:29:58 +0000 | [diff] [blame] | 1581 | if (Field->isMutable()) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1582 | Info.Diag(E, diag::note_constexpr_ltor_mutable, 1) |
Richard Smith | b4e5e28 | 2012-02-09 03:29:58 +0000 | [diff] [blame] | 1583 | << Field; |
| 1584 | Info.Note(Field->getLocation(), diag::note_declared_at); |
| 1585 | return false; |
| 1586 | } |
| 1587 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1588 | // Next subobject is a class, struct or union field. |
| 1589 | RecordDecl *RD = ObjType->castAs<RecordType>()->getDecl(); |
| 1590 | if (RD->isUnion()) { |
| 1591 | const FieldDecl *UnionField = O->getUnionField(); |
| 1592 | if (!UnionField || |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1593 | UnionField->getCanonicalDecl() != Field->getCanonicalDecl()) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1594 | Info.Diag(E, diag::note_constexpr_read_inactive_union_member) |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1595 | << Field << !UnionField << UnionField; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1596 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1597 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1598 | O = &O->getUnionValue(); |
| 1599 | } else |
| 1600 | O = &O->getStructField(Field->getFieldIndex()); |
| 1601 | ObjType = Field->getType(); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1602 | |
| 1603 | if (ObjType.isVolatileQualified()) { |
| 1604 | if (Info.getLangOpts().CPlusPlus) { |
| 1605 | // FIXME: Include a description of the path to the volatile subobject. |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1606 | Info.Diag(E, diag::note_constexpr_ltor_volatile_obj, 1) |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1607 | << 2 << Field; |
| 1608 | Info.Note(Field->getLocation(), diag::note_declared_at); |
| 1609 | } else { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1610 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1611 | } |
| 1612 | return false; |
| 1613 | } |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1614 | } else { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1615 | // Next subobject is a base class. |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1616 | const CXXRecordDecl *Derived = ObjType->getAsCXXRecordDecl(); |
| 1617 | const CXXRecordDecl *Base = getAsBaseClass(Sub.Entries[I]); |
| 1618 | O = &O->getStructBase(getBaseIndex(Derived, Base)); |
| 1619 | ObjType = Info.Ctx.getRecordType(Base); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1620 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1621 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1622 | if (O->isUninit()) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1623 | if (!Info.CheckingPotentialConstantExpression) |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1624 | Info.Diag(E, diag::note_constexpr_read_uninit); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1625 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1626 | } |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1627 | } |
| 1628 | |
Richard Smith | 0069b84 | 2012-03-10 00:28:11 +0000 | [diff] [blame] | 1629 | // This may look super-stupid, but it serves an important purpose: if we just |
| 1630 | // swapped Obj and *O, we'd create an object which had itself as a subobject. |
| 1631 | // To avoid the leak, we ensure that Tmp ends up owning the original complete |
| 1632 | // object, which is destroyed by Tmp's destructor. |
| 1633 | APValue Tmp; |
| 1634 | O->swap(Tmp); |
| 1635 | Obj.swap(Tmp); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1636 | return true; |
| 1637 | } |
| 1638 | |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 1639 | /// Find the position where two subobject designators diverge, or equivalently |
| 1640 | /// the length of the common initial subsequence. |
| 1641 | static unsigned FindDesignatorMismatch(QualType ObjType, |
| 1642 | const SubobjectDesignator &A, |
| 1643 | const SubobjectDesignator &B, |
| 1644 | bool &WasArrayIndex) { |
| 1645 | unsigned I = 0, N = std::min(A.Entries.size(), B.Entries.size()); |
| 1646 | for (/**/; I != N; ++I) { |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1647 | if (!ObjType.isNull() && |
| 1648 | (ObjType->isArrayType() || ObjType->isAnyComplexType())) { |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 1649 | // Next subobject is an array element. |
| 1650 | if (A.Entries[I].ArrayIndex != B.Entries[I].ArrayIndex) { |
| 1651 | WasArrayIndex = true; |
| 1652 | return I; |
| 1653 | } |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1654 | if (ObjType->isAnyComplexType()) |
| 1655 | ObjType = ObjType->castAs<ComplexType>()->getElementType(); |
| 1656 | else |
| 1657 | ObjType = ObjType->castAsArrayTypeUnsafe()->getElementType(); |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 1658 | } else { |
| 1659 | if (A.Entries[I].BaseOrMember != B.Entries[I].BaseOrMember) { |
| 1660 | WasArrayIndex = false; |
| 1661 | return I; |
| 1662 | } |
| 1663 | if (const FieldDecl *FD = getAsField(A.Entries[I])) |
| 1664 | // Next subobject is a field. |
| 1665 | ObjType = FD->getType(); |
| 1666 | else |
| 1667 | // Next subobject is a base class. |
| 1668 | ObjType = QualType(); |
| 1669 | } |
| 1670 | } |
| 1671 | WasArrayIndex = false; |
| 1672 | return I; |
| 1673 | } |
| 1674 | |
| 1675 | /// Determine whether the given subobject designators refer to elements of the |
| 1676 | /// same array object. |
| 1677 | static bool AreElementsOfSameArray(QualType ObjType, |
| 1678 | const SubobjectDesignator &A, |
| 1679 | const SubobjectDesignator &B) { |
| 1680 | if (A.Entries.size() != B.Entries.size()) |
| 1681 | return false; |
| 1682 | |
| 1683 | bool IsArray = A.MostDerivedArraySize != 0; |
| 1684 | if (IsArray && A.MostDerivedPathLength != A.Entries.size()) |
| 1685 | // A is a subobject of the array element. |
| 1686 | return false; |
| 1687 | |
| 1688 | // If A (and B) designates an array element, the last entry will be the array |
| 1689 | // index. That doesn't have to match. Otherwise, we're in the 'implicit array |
| 1690 | // of length 1' case, and the entire path must match. |
| 1691 | bool WasArrayIndex; |
| 1692 | unsigned CommonLength = FindDesignatorMismatch(ObjType, A, B, WasArrayIndex); |
| 1693 | return CommonLength >= A.Entries.size() - IsArray; |
| 1694 | } |
| 1695 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1696 | /// HandleLValueToRValueConversion - Perform an lvalue-to-rvalue conversion on |
| 1697 | /// the given lvalue. This can also be used for 'lvalue-to-lvalue' conversions |
| 1698 | /// for looking up the glvalue referred to by an entity of reference type. |
| 1699 | /// |
| 1700 | /// \param Info - Information about the ongoing evaluation. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1701 | /// \param Conv - The expression for which we are performing the conversion. |
| 1702 | /// Used for diagnostics. |
Richard Smith | 9ec7197 | 2012-02-05 01:23:16 +0000 | [diff] [blame] | 1703 | /// \param Type - The type we expect this conversion to produce, before |
| 1704 | /// stripping cv-qualifiers in the case of a non-clas type. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1705 | /// \param LVal - The glvalue on which we are attempting to perform this action. |
| 1706 | /// \param RVal - The produced value will be placed here. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1707 | static bool HandleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv, |
| 1708 | QualType Type, |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1709 | const LValue &LVal, APValue &RVal) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1710 | if (LVal.Designator.Invalid) |
| 1711 | // A diagnostic will have already been produced. |
| 1712 | return false; |
| 1713 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1714 | const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1715 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1716 | if (!LVal.Base) { |
| 1717 | // FIXME: Indirection through a null pointer deserves a specific diagnostic. |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1718 | Info.Diag(Conv, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1719 | return false; |
| 1720 | } |
| 1721 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1722 | CallStackFrame *Frame = 0; |
| 1723 | if (LVal.CallIndex) { |
| 1724 | Frame = Info.getCallFrame(LVal.CallIndex); |
| 1725 | if (!Frame) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1726 | Info.Diag(Conv, diag::note_constexpr_lifetime_ended, 1) << !Base; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1727 | NoteLValueLocation(Info, LVal.Base); |
| 1728 | return false; |
| 1729 | } |
| 1730 | } |
| 1731 | |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1732 | // C++11 DR1311: An lvalue-to-rvalue conversion on a volatile-qualified type |
| 1733 | // is not a constant expression (even if the object is non-volatile). We also |
| 1734 | // apply this rule to C++98, in order to conform to the expected 'volatile' |
| 1735 | // semantics. |
| 1736 | if (Type.isVolatileQualified()) { |
| 1737 | if (Info.getLangOpts().CPlusPlus) |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1738 | Info.Diag(Conv, diag::note_constexpr_ltor_volatile_type) << Type; |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1739 | else |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1740 | Info.Diag(Conv); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1741 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1742 | } |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1743 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1744 | if (const ValueDecl *D = LVal.Base.dyn_cast<const ValueDecl*>()) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1745 | // In C++98, const, non-volatile integers initialized with ICEs are ICEs. |
| 1746 | // In C++11, constexpr, non-volatile variables initialized with constant |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1747 | // expressions are constant expressions too. Inside constexpr functions, |
| 1748 | // parameters are constant expressions even if they're non-const. |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1749 | // In C, such things can also be folded, although they are not ICEs. |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1750 | const VarDecl *VD = dyn_cast<VarDecl>(D); |
Douglas Gregor | d2008e2 | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 1751 | if (VD) { |
| 1752 | if (const VarDecl *VDef = VD->getDefinition(Info.Ctx)) |
| 1753 | VD = VDef; |
| 1754 | } |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1755 | if (!VD || VD->isInvalidDecl()) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1756 | Info.Diag(Conv); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1757 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1758 | } |
| 1759 | |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1760 | // DR1313: If the object is volatile-qualified but the glvalue was not, |
| 1761 | // behavior is undefined so the result is not a constant expression. |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1762 | QualType VT = VD->getType(); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1763 | if (VT.isVolatileQualified()) { |
| 1764 | if (Info.getLangOpts().CPlusPlus) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1765 | Info.Diag(Conv, diag::note_constexpr_ltor_volatile_obj, 1) << 1 << VD; |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1766 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1767 | } else { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1768 | Info.Diag(Conv); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1769 | } |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1770 | return false; |
| 1771 | } |
| 1772 | |
| 1773 | if (!isa<ParmVarDecl>(VD)) { |
| 1774 | if (VD->isConstexpr()) { |
| 1775 | // OK, we can read this variable. |
| 1776 | } else if (VT->isIntegralOrEnumerationType()) { |
| 1777 | if (!VT.isConstQualified()) { |
| 1778 | if (Info.getLangOpts().CPlusPlus) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1779 | Info.Diag(Conv, diag::note_constexpr_ltor_non_const_int, 1) << VD; |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1780 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1781 | } else { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1782 | Info.Diag(Conv); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1783 | } |
| 1784 | return false; |
| 1785 | } |
| 1786 | } else if (VT->isFloatingType() && VT.isConstQualified()) { |
| 1787 | // We support folding of const floating-point types, in order to make |
| 1788 | // static const data members of such types (supported as an extension) |
| 1789 | // more useful. |
| 1790 | if (Info.getLangOpts().CPlusPlus0x) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1791 | Info.CCEDiag(Conv, diag::note_constexpr_ltor_non_constexpr, 1) << VD; |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1792 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1793 | } else { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1794 | Info.CCEDiag(Conv); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1795 | } |
| 1796 | } else { |
| 1797 | // FIXME: Allow folding of values of any literal type in all languages. |
| 1798 | if (Info.getLangOpts().CPlusPlus0x) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1799 | Info.Diag(Conv, diag::note_constexpr_ltor_non_constexpr, 1) << VD; |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1800 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1801 | } else { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1802 | Info.Diag(Conv); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1803 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1804 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1805 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1806 | } |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1807 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1808 | if (!EvaluateVarDeclInit(Info, Conv, VD, Frame, RVal)) |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1809 | return false; |
| 1810 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1811 | if (isa<ParmVarDecl>(VD) || !VD->getAnyInitializer()->isLValue()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1812 | return ExtractSubobject(Info, Conv, RVal, VT, LVal.Designator, Type); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1813 | |
| 1814 | // The declaration was initialized by an lvalue, with no lvalue-to-rvalue |
| 1815 | // conversion. This happens when the declaration and the lvalue should be |
| 1816 | // considered synonymous, for instance when initializing an array of char |
| 1817 | // from a string literal. Continue as if the initializer lvalue was the |
| 1818 | // value we were originally given. |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1819 | assert(RVal.getLValueOffset().isZero() && |
| 1820 | "offset for lvalue init of non-reference"); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1821 | Base = RVal.getLValueBase().get<const Expr*>(); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1822 | |
| 1823 | if (unsigned CallIndex = RVal.getLValueCallIndex()) { |
| 1824 | Frame = Info.getCallFrame(CallIndex); |
| 1825 | if (!Frame) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1826 | Info.Diag(Conv, diag::note_constexpr_lifetime_ended, 1) << !Base; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1827 | NoteLValueLocation(Info, RVal.getLValueBase()); |
| 1828 | return false; |
| 1829 | } |
| 1830 | } else { |
| 1831 | Frame = 0; |
| 1832 | } |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1833 | } |
| 1834 | |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1835 | // Volatile temporary objects cannot be read in constant expressions. |
| 1836 | if (Base->getType().isVolatileQualified()) { |
| 1837 | if (Info.getLangOpts().CPlusPlus) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1838 | Info.Diag(Conv, diag::note_constexpr_ltor_volatile_obj, 1) << 0; |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1839 | Info.Note(Base->getExprLoc(), diag::note_constexpr_temporary_here); |
| 1840 | } else { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1841 | Info.Diag(Conv); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1842 | } |
| 1843 | return false; |
| 1844 | } |
| 1845 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1846 | if (Frame) { |
| 1847 | // If this is a temporary expression with a nontrivial initializer, grab the |
| 1848 | // value from the relevant stack frame. |
| 1849 | RVal = Frame->Temporaries[Base]; |
| 1850 | } else if (const CompoundLiteralExpr *CLE |
| 1851 | = dyn_cast<CompoundLiteralExpr>(Base)) { |
| 1852 | // In C99, a CompoundLiteralExpr is an lvalue, and we defer evaluating the |
| 1853 | // initializer until now for such expressions. Such an expression can't be |
| 1854 | // an ICE in C, so this only matters for fold. |
| 1855 | assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?"); |
| 1856 | if (!Evaluate(RVal, Info, CLE->getInitializer())) |
| 1857 | return false; |
Richard Smith | f3908f2 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 1858 | } else if (isa<StringLiteral>(Base)) { |
| 1859 | // We represent a string literal array as an lvalue pointing at the |
| 1860 | // corresponding expression, rather than building an array of chars. |
| 1861 | // FIXME: Support PredefinedExpr, ObjCEncodeExpr, MakeStringConstant |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1862 | RVal = APValue(Base, CharUnits::Zero(), APValue::NoLValuePath(), 0); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1863 | } else { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1864 | Info.Diag(Conv, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1865 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1866 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1867 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1868 | return ExtractSubobject(Info, Conv, RVal, Base->getType(), LVal.Designator, |
| 1869 | Type); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1870 | } |
| 1871 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1872 | /// Build an lvalue for the object argument of a member function call. |
| 1873 | static bool EvaluateObjectArgument(EvalInfo &Info, const Expr *Object, |
| 1874 | LValue &This) { |
| 1875 | if (Object->getType()->isPointerType()) |
| 1876 | return EvaluatePointer(Object, This, Info); |
| 1877 | |
| 1878 | if (Object->isGLValue()) |
| 1879 | return EvaluateLValue(Object, This, Info); |
| 1880 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1881 | if (Object->getType()->isLiteralType()) |
| 1882 | return EvaluateTemporary(Object, This, Info); |
| 1883 | |
| 1884 | return false; |
| 1885 | } |
| 1886 | |
| 1887 | /// HandleMemberPointerAccess - Evaluate a member access operation and build an |
| 1888 | /// lvalue referring to the result. |
| 1889 | /// |
| 1890 | /// \param Info - Information about the ongoing evaluation. |
| 1891 | /// \param BO - The member pointer access operation. |
| 1892 | /// \param LV - Filled in with a reference to the resulting object. |
| 1893 | /// \param IncludeMember - Specifies whether the member itself is included in |
| 1894 | /// the resulting LValue subobject designator. This is not possible when |
| 1895 | /// creating a bound member function. |
| 1896 | /// \return The field or method declaration to which the member pointer refers, |
| 1897 | /// or 0 if evaluation fails. |
| 1898 | static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info, |
| 1899 | const BinaryOperator *BO, |
| 1900 | LValue &LV, |
| 1901 | bool IncludeMember = true) { |
| 1902 | assert(BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI); |
| 1903 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1904 | bool EvalObjOK = EvaluateObjectArgument(Info, BO->getLHS(), LV); |
| 1905 | if (!EvalObjOK && !Info.keepEvaluatingAfterFailure()) |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1906 | return 0; |
| 1907 | |
| 1908 | MemberPtr MemPtr; |
| 1909 | if (!EvaluateMemberPointer(BO->getRHS(), MemPtr, Info)) |
| 1910 | return 0; |
| 1911 | |
| 1912 | // C++11 [expr.mptr.oper]p6: If the second operand is the null pointer to |
| 1913 | // member value, the behavior is undefined. |
| 1914 | if (!MemPtr.getDecl()) |
| 1915 | return 0; |
| 1916 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1917 | if (!EvalObjOK) |
| 1918 | return 0; |
| 1919 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1920 | if (MemPtr.isDerivedMember()) { |
| 1921 | // This is a member of some derived class. Truncate LV appropriately. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1922 | // The end of the derived-to-base path for the base object must match the |
| 1923 | // derived-to-base path for the member pointer. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1924 | if (LV.Designator.MostDerivedPathLength + MemPtr.Path.size() > |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1925 | LV.Designator.Entries.size()) |
| 1926 | return 0; |
| 1927 | unsigned PathLengthToMember = |
| 1928 | LV.Designator.Entries.size() - MemPtr.Path.size(); |
| 1929 | for (unsigned I = 0, N = MemPtr.Path.size(); I != N; ++I) { |
| 1930 | const CXXRecordDecl *LVDecl = getAsBaseClass( |
| 1931 | LV.Designator.Entries[PathLengthToMember + I]); |
| 1932 | const CXXRecordDecl *MPDecl = MemPtr.Path[I]; |
| 1933 | if (LVDecl->getCanonicalDecl() != MPDecl->getCanonicalDecl()) |
| 1934 | return 0; |
| 1935 | } |
| 1936 | |
| 1937 | // Truncate the lvalue to the appropriate derived class. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1938 | if (!CastToDerivedClass(Info, BO, LV, MemPtr.getContainingRecord(), |
| 1939 | PathLengthToMember)) |
| 1940 | return 0; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1941 | } else if (!MemPtr.Path.empty()) { |
| 1942 | // Extend the LValue path with the member pointer's path. |
| 1943 | LV.Designator.Entries.reserve(LV.Designator.Entries.size() + |
| 1944 | MemPtr.Path.size() + IncludeMember); |
| 1945 | |
| 1946 | // Walk down to the appropriate base class. |
| 1947 | QualType LVType = BO->getLHS()->getType(); |
| 1948 | if (const PointerType *PT = LVType->getAs<PointerType>()) |
| 1949 | LVType = PT->getPointeeType(); |
| 1950 | const CXXRecordDecl *RD = LVType->getAsCXXRecordDecl(); |
| 1951 | assert(RD && "member pointer access on non-class-type expression"); |
| 1952 | // The first class in the path is that of the lvalue. |
| 1953 | for (unsigned I = 1, N = MemPtr.Path.size(); I != N; ++I) { |
| 1954 | const CXXRecordDecl *Base = MemPtr.Path[N - I - 1]; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1955 | HandleLValueDirectBase(Info, BO, LV, RD, Base); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1956 | RD = Base; |
| 1957 | } |
| 1958 | // Finally cast to the class containing the member. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1959 | HandleLValueDirectBase(Info, BO, LV, RD, MemPtr.getContainingRecord()); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1960 | } |
| 1961 | |
| 1962 | // Add the member. Note that we cannot build bound member functions here. |
| 1963 | if (IncludeMember) { |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 1964 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(MemPtr.getDecl())) |
| 1965 | HandleLValueMember(Info, BO, LV, FD); |
| 1966 | else if (const IndirectFieldDecl *IFD = |
| 1967 | dyn_cast<IndirectFieldDecl>(MemPtr.getDecl())) |
| 1968 | HandleLValueIndirectMember(Info, BO, LV, IFD); |
| 1969 | else |
| 1970 | llvm_unreachable("can't construct reference to bound member function"); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1971 | } |
| 1972 | |
| 1973 | return MemPtr.getDecl(); |
| 1974 | } |
| 1975 | |
| 1976 | /// HandleBaseToDerivedCast - Apply the given base-to-derived cast operation on |
| 1977 | /// the provided lvalue, which currently refers to the base object. |
| 1978 | static bool HandleBaseToDerivedCast(EvalInfo &Info, const CastExpr *E, |
| 1979 | LValue &Result) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1980 | SubobjectDesignator &D = Result.Designator; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1981 | if (D.Invalid || !Result.checkNullPointer(Info, E, CSK_Derived)) |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1982 | return false; |
| 1983 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1984 | QualType TargetQT = E->getType(); |
| 1985 | if (const PointerType *PT = TargetQT->getAs<PointerType>()) |
| 1986 | TargetQT = PT->getPointeeType(); |
| 1987 | |
| 1988 | // Check this cast lands within the final derived-to-base subobject path. |
| 1989 | if (D.MostDerivedPathLength + E->path_size() > D.Entries.size()) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1990 | Info.CCEDiag(E, diag::note_constexpr_invalid_downcast) |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1991 | << D.MostDerivedType << TargetQT; |
| 1992 | return false; |
| 1993 | } |
| 1994 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1995 | // Check the type of the final cast. We don't need to check the path, |
| 1996 | // since a cast can only be formed if the path is unique. |
| 1997 | unsigned NewEntriesSize = D.Entries.size() - E->path_size(); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1998 | const CXXRecordDecl *TargetType = TargetQT->getAsCXXRecordDecl(); |
| 1999 | const CXXRecordDecl *FinalType; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2000 | if (NewEntriesSize == D.MostDerivedPathLength) |
| 2001 | FinalType = D.MostDerivedType->getAsCXXRecordDecl(); |
| 2002 | else |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2003 | FinalType = getAsBaseClass(D.Entries[NewEntriesSize - 1]); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2004 | if (FinalType->getCanonicalDecl() != TargetType->getCanonicalDecl()) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 2005 | Info.CCEDiag(E, diag::note_constexpr_invalid_downcast) |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2006 | << D.MostDerivedType << TargetQT; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2007 | return false; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2008 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2009 | |
| 2010 | // Truncate the lvalue to the appropriate derived class. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2011 | return CastToDerivedClass(Info, E, Result, TargetType, NewEntriesSize); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2012 | } |
| 2013 | |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 2014 | namespace { |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2015 | enum EvalStmtResult { |
| 2016 | /// Evaluation failed. |
| 2017 | ESR_Failed, |
| 2018 | /// Hit a 'return' statement. |
| 2019 | ESR_Returned, |
| 2020 | /// Evaluation succeeded. |
| 2021 | ESR_Succeeded |
| 2022 | }; |
| 2023 | } |
| 2024 | |
| 2025 | // Evaluate a statement. |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2026 | static EvalStmtResult EvaluateStmt(APValue &Result, EvalInfo &Info, |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2027 | const Stmt *S) { |
| 2028 | switch (S->getStmtClass()) { |
| 2029 | default: |
| 2030 | return ESR_Failed; |
| 2031 | |
| 2032 | case Stmt::NullStmtClass: |
| 2033 | case Stmt::DeclStmtClass: |
| 2034 | return ESR_Succeeded; |
| 2035 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2036 | case Stmt::ReturnStmtClass: { |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2037 | const Expr *RetExpr = cast<ReturnStmt>(S)->getRetValue(); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2038 | if (!Evaluate(Result, Info, RetExpr)) |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2039 | return ESR_Failed; |
| 2040 | return ESR_Returned; |
| 2041 | } |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2042 | |
| 2043 | case Stmt::CompoundStmtClass: { |
| 2044 | const CompoundStmt *CS = cast<CompoundStmt>(S); |
| 2045 | for (CompoundStmt::const_body_iterator BI = CS->body_begin(), |
| 2046 | BE = CS->body_end(); BI != BE; ++BI) { |
| 2047 | EvalStmtResult ESR = EvaluateStmt(Result, Info, *BI); |
| 2048 | if (ESR != ESR_Succeeded) |
| 2049 | return ESR; |
| 2050 | } |
| 2051 | return ESR_Succeeded; |
| 2052 | } |
| 2053 | } |
| 2054 | } |
| 2055 | |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 2056 | /// CheckTrivialDefaultConstructor - Check whether a constructor is a trivial |
| 2057 | /// default constructor. If so, we'll fold it whether or not it's marked as |
| 2058 | /// constexpr. If it is marked as constexpr, we will never implicitly define it, |
| 2059 | /// so we need special handling. |
| 2060 | static bool CheckTrivialDefaultConstructor(EvalInfo &Info, SourceLocation Loc, |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2061 | const CXXConstructorDecl *CD, |
| 2062 | bool IsValueInitialization) { |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 2063 | if (!CD->isTrivial() || !CD->isDefaultConstructor()) |
| 2064 | return false; |
| 2065 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 2066 | // Value-initialization does not call a trivial default constructor, so such a |
| 2067 | // call is a core constant expression whether or not the constructor is |
| 2068 | // constexpr. |
| 2069 | if (!CD->isConstexpr() && !IsValueInitialization) { |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 2070 | if (Info.getLangOpts().CPlusPlus0x) { |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 2071 | // FIXME: If DiagDecl is an implicitly-declared special member function, |
| 2072 | // we should be much more explicit about why it's not constexpr. |
| 2073 | Info.CCEDiag(Loc, diag::note_constexpr_invalid_function, 1) |
| 2074 | << /*IsConstexpr*/0 << /*IsConstructor*/1 << CD; |
| 2075 | Info.Note(CD->getLocation(), diag::note_declared_at); |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 2076 | } else { |
| 2077 | Info.CCEDiag(Loc, diag::note_invalid_subexpr_in_const_expr); |
| 2078 | } |
| 2079 | } |
| 2080 | return true; |
| 2081 | } |
| 2082 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2083 | /// CheckConstexprFunction - Check that a function can be called in a constant |
| 2084 | /// expression. |
| 2085 | static bool CheckConstexprFunction(EvalInfo &Info, SourceLocation CallLoc, |
| 2086 | const FunctionDecl *Declaration, |
| 2087 | const FunctionDecl *Definition) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2088 | // Potential constant expressions can contain calls to declared, but not yet |
| 2089 | // defined, constexpr functions. |
| 2090 | if (Info.CheckingPotentialConstantExpression && !Definition && |
| 2091 | Declaration->isConstexpr()) |
| 2092 | return false; |
| 2093 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2094 | // Can we evaluate this function call? |
| 2095 | if (Definition && Definition->isConstexpr() && !Definition->isInvalidDecl()) |
| 2096 | return true; |
| 2097 | |
| 2098 | if (Info.getLangOpts().CPlusPlus0x) { |
| 2099 | const FunctionDecl *DiagDecl = Definition ? Definition : Declaration; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2100 | // FIXME: If DiagDecl is an implicitly-declared special member function, we |
| 2101 | // should be much more explicit about why it's not constexpr. |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2102 | Info.Diag(CallLoc, diag::note_constexpr_invalid_function, 1) |
| 2103 | << DiagDecl->isConstexpr() << isa<CXXConstructorDecl>(DiagDecl) |
| 2104 | << DiagDecl; |
| 2105 | Info.Note(DiagDecl->getLocation(), diag::note_declared_at); |
| 2106 | } else { |
| 2107 | Info.Diag(CallLoc, diag::note_invalid_subexpr_in_const_expr); |
| 2108 | } |
| 2109 | return false; |
| 2110 | } |
| 2111 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2112 | namespace { |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2113 | typedef SmallVector<APValue, 8> ArgVector; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2114 | } |
| 2115 | |
| 2116 | /// EvaluateArgs - Evaluate the arguments to a function call. |
| 2117 | static bool EvaluateArgs(ArrayRef<const Expr*> Args, ArgVector &ArgValues, |
| 2118 | EvalInfo &Info) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2119 | bool Success = true; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2120 | for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end(); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2121 | I != E; ++I) { |
| 2122 | if (!Evaluate(ArgValues[I - Args.begin()], Info, *I)) { |
| 2123 | // If we're checking for a potential constant expression, evaluate all |
| 2124 | // initializers even if some of them fail. |
| 2125 | if (!Info.keepEvaluatingAfterFailure()) |
| 2126 | return false; |
| 2127 | Success = false; |
| 2128 | } |
| 2129 | } |
| 2130 | return Success; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2131 | } |
| 2132 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2133 | /// Evaluate a function call. |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2134 | static bool HandleFunctionCall(SourceLocation CallLoc, |
| 2135 | const FunctionDecl *Callee, const LValue *This, |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2136 | ArrayRef<const Expr*> Args, const Stmt *Body, |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2137 | EvalInfo &Info, APValue &Result) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2138 | ArgVector ArgValues(Args.size()); |
| 2139 | if (!EvaluateArgs(Args, ArgValues, Info)) |
| 2140 | return false; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2141 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2142 | if (!Info.CheckCallLimit(CallLoc)) |
| 2143 | return false; |
| 2144 | |
| 2145 | CallStackFrame Frame(Info, CallLoc, Callee, This, ArgValues.data()); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2146 | return EvaluateStmt(Result, Info, Body) == ESR_Returned; |
| 2147 | } |
| 2148 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2149 | /// Evaluate a constructor call. |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2150 | static bool HandleConstructorCall(SourceLocation CallLoc, const LValue &This, |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2151 | ArrayRef<const Expr*> Args, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2152 | const CXXConstructorDecl *Definition, |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2153 | EvalInfo &Info, APValue &Result) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2154 | ArgVector ArgValues(Args.size()); |
| 2155 | if (!EvaluateArgs(Args, ArgValues, Info)) |
| 2156 | return false; |
| 2157 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2158 | if (!Info.CheckCallLimit(CallLoc)) |
| 2159 | return false; |
| 2160 | |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 2161 | const CXXRecordDecl *RD = Definition->getParent(); |
| 2162 | if (RD->getNumVBases()) { |
| 2163 | Info.Diag(CallLoc, diag::note_constexpr_virtual_base) << RD; |
| 2164 | return false; |
| 2165 | } |
| 2166 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2167 | CallStackFrame Frame(Info, CallLoc, Definition, &This, ArgValues.data()); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2168 | |
| 2169 | // If it's a delegating constructor, just delegate. |
| 2170 | if (Definition->isDelegatingConstructor()) { |
| 2171 | CXXConstructorDecl::init_const_iterator I = Definition->init_begin(); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2172 | return EvaluateInPlace(Result, Info, This, (*I)->getInit()); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2173 | } |
| 2174 | |
Richard Smith | 610a60c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 2175 | // For a trivial copy or move constructor, perform an APValue copy. This is |
| 2176 | // essential for unions, where the operations performed by the constructor |
| 2177 | // cannot be represented by ctor-initializers. |
Richard Smith | 610a60c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 2178 | if (Definition->isDefaulted() && |
Douglas Gregor | f6cfe8b | 2012-02-24 07:55:51 +0000 | [diff] [blame] | 2179 | ((Definition->isCopyConstructor() && Definition->isTrivial()) || |
| 2180 | (Definition->isMoveConstructor() && Definition->isTrivial()))) { |
Richard Smith | 610a60c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 2181 | LValue RHS; |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2182 | RHS.setFrom(Info.Ctx, ArgValues[0]); |
| 2183 | return HandleLValueToRValueConversion(Info, Args[0], Args[0]->getType(), |
| 2184 | RHS, Result); |
Richard Smith | 610a60c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 2185 | } |
| 2186 | |
| 2187 | // Reserve space for the struct members. |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2188 | if (!RD->isUnion() && Result.isUninit()) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2189 | Result = APValue(APValue::UninitStruct(), RD->getNumBases(), |
| 2190 | std::distance(RD->field_begin(), RD->field_end())); |
| 2191 | |
| 2192 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 2193 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2194 | bool Success = true; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2195 | unsigned BasesSeen = 0; |
| 2196 | #ifndef NDEBUG |
| 2197 | CXXRecordDecl::base_class_const_iterator BaseIt = RD->bases_begin(); |
| 2198 | #endif |
| 2199 | for (CXXConstructorDecl::init_const_iterator I = Definition->init_begin(), |
| 2200 | E = Definition->init_end(); I != E; ++I) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2201 | LValue Subobject = This; |
| 2202 | APValue *Value = &Result; |
| 2203 | |
| 2204 | // Determine the subobject to initialize. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2205 | if ((*I)->isBaseInitializer()) { |
| 2206 | QualType BaseType((*I)->getBaseClass(), 0); |
| 2207 | #ifndef NDEBUG |
| 2208 | // Non-virtual base classes are initialized in the order in the class |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 2209 | // definition. We have already checked for virtual base classes. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2210 | assert(!BaseIt->isVirtual() && "virtual base for literal type"); |
| 2211 | assert(Info.Ctx.hasSameType(BaseIt->getType(), BaseType) && |
| 2212 | "base class initializers not in expected order"); |
| 2213 | ++BaseIt; |
| 2214 | #endif |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2215 | HandleLValueDirectBase(Info, (*I)->getInit(), Subobject, RD, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2216 | BaseType->getAsCXXRecordDecl(), &Layout); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2217 | Value = &Result.getStructBase(BasesSeen++); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2218 | } else if (FieldDecl *FD = (*I)->getMember()) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2219 | HandleLValueMember(Info, (*I)->getInit(), Subobject, FD, &Layout); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2220 | if (RD->isUnion()) { |
| 2221 | Result = APValue(FD); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2222 | Value = &Result.getUnionValue(); |
| 2223 | } else { |
| 2224 | Value = &Result.getStructField(FD->getFieldIndex()); |
| 2225 | } |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2226 | } else if (IndirectFieldDecl *IFD = (*I)->getIndirectMember()) { |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2227 | // Walk the indirect field decl's chain to find the object to initialize, |
| 2228 | // and make sure we've initialized every step along it. |
| 2229 | for (IndirectFieldDecl::chain_iterator C = IFD->chain_begin(), |
| 2230 | CE = IFD->chain_end(); |
| 2231 | C != CE; ++C) { |
| 2232 | FieldDecl *FD = cast<FieldDecl>(*C); |
| 2233 | CXXRecordDecl *CD = cast<CXXRecordDecl>(FD->getParent()); |
| 2234 | // Switch the union field if it differs. This happens if we had |
| 2235 | // preceding zero-initialization, and we're now initializing a union |
| 2236 | // subobject other than the first. |
| 2237 | // FIXME: In this case, the values of the other subobjects are |
| 2238 | // specified, since zero-initialization sets all padding bits to zero. |
| 2239 | if (Value->isUninit() || |
| 2240 | (Value->isUnion() && Value->getUnionField() != FD)) { |
| 2241 | if (CD->isUnion()) |
| 2242 | *Value = APValue(FD); |
| 2243 | else |
| 2244 | *Value = APValue(APValue::UninitStruct(), CD->getNumBases(), |
| 2245 | std::distance(CD->field_begin(), CD->field_end())); |
| 2246 | } |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2247 | HandleLValueMember(Info, (*I)->getInit(), Subobject, FD); |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2248 | if (CD->isUnion()) |
| 2249 | Value = &Value->getUnionValue(); |
| 2250 | else |
| 2251 | Value = &Value->getStructField(FD->getFieldIndex()); |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2252 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2253 | } else { |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2254 | llvm_unreachable("unknown base initializer kind"); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2255 | } |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2256 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2257 | if (!EvaluateInPlace(*Value, Info, Subobject, (*I)->getInit(), |
| 2258 | (*I)->isBaseInitializer() |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2259 | ? CCEK_Constant : CCEK_MemberInit)) { |
| 2260 | // If we're checking for a potential constant expression, evaluate all |
| 2261 | // initializers even if some of them fail. |
| 2262 | if (!Info.keepEvaluatingAfterFailure()) |
| 2263 | return false; |
| 2264 | Success = false; |
| 2265 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2266 | } |
| 2267 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2268 | return Success; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2269 | } |
| 2270 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2271 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 2272 | class HasSideEffect |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2273 | : public ConstStmtVisitor<HasSideEffect, bool> { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 2274 | const ASTContext &Ctx; |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 2275 | public: |
| 2276 | |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 2277 | HasSideEffect(const ASTContext &C) : Ctx(C) {} |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 2278 | |
| 2279 | // Unhandled nodes conservatively default to having side effects. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2280 | bool VisitStmt(const Stmt *S) { |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 2281 | return true; |
| 2282 | } |
| 2283 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2284 | bool VisitParenExpr(const ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 2285 | bool VisitGenericSelectionExpr(const GenericSelectionExpr *E) { |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 2286 | return Visit(E->getResultExpr()); |
| 2287 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2288 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 2289 | if (Ctx.getCanonicalType(E->getType()).isVolatileQualified()) |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 2290 | return true; |
| 2291 | return false; |
| 2292 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2293 | bool VisitObjCIvarRefExpr(const ObjCIvarRefExpr *E) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 2294 | if (Ctx.getCanonicalType(E->getType()).isVolatileQualified()) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2295 | return true; |
| 2296 | return false; |
| 2297 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2298 | |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 2299 | // We don't want to evaluate BlockExprs multiple times, as they generate |
| 2300 | // a ton of code. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2301 | bool VisitBlockExpr(const BlockExpr *E) { return true; } |
| 2302 | bool VisitPredefinedExpr(const PredefinedExpr *E) { return false; } |
| 2303 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 2304 | { return Visit(E->getInitializer()); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2305 | bool VisitMemberExpr(const MemberExpr *E) { return Visit(E->getBase()); } |
| 2306 | bool VisitIntegerLiteral(const IntegerLiteral *E) { return false; } |
| 2307 | bool VisitFloatingLiteral(const FloatingLiteral *E) { return false; } |
| 2308 | bool VisitStringLiteral(const StringLiteral *E) { return false; } |
| 2309 | bool VisitCharacterLiteral(const CharacterLiteral *E) { return false; } |
| 2310 | bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E) |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2311 | { return false; } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2312 | bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E) |
Mike Stump | 980ca22 | 2009-10-29 20:48:09 +0000 | [diff] [blame] | 2313 | { return Visit(E->getLHS()) || Visit(E->getRHS()); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2314 | bool VisitChooseExpr(const ChooseExpr *E) |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 2315 | { return Visit(E->getChosenSubExpr(Ctx)); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2316 | bool VisitCastExpr(const CastExpr *E) { return Visit(E->getSubExpr()); } |
| 2317 | bool VisitBinAssign(const BinaryOperator *E) { return true; } |
| 2318 | bool VisitCompoundAssignOperator(const BinaryOperator *E) { return true; } |
| 2319 | bool VisitBinaryOperator(const BinaryOperator *E) |
Mike Stump | 980ca22 | 2009-10-29 20:48:09 +0000 | [diff] [blame] | 2320 | { return Visit(E->getLHS()) || Visit(E->getRHS()); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2321 | bool VisitUnaryPreInc(const UnaryOperator *E) { return true; } |
| 2322 | bool VisitUnaryPostInc(const UnaryOperator *E) { return true; } |
| 2323 | bool VisitUnaryPreDec(const UnaryOperator *E) { return true; } |
| 2324 | bool VisitUnaryPostDec(const UnaryOperator *E) { return true; } |
| 2325 | bool VisitUnaryDeref(const UnaryOperator *E) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 2326 | if (Ctx.getCanonicalType(E->getType()).isVolatileQualified()) |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 2327 | return true; |
Mike Stump | 980ca22 | 2009-10-29 20:48:09 +0000 | [diff] [blame] | 2328 | return Visit(E->getSubExpr()); |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 2329 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2330 | bool VisitUnaryOperator(const UnaryOperator *E) { return Visit(E->getSubExpr()); } |
Chris Lattner | 363ff23 | 2010-04-13 17:34:23 +0000 | [diff] [blame] | 2331 | |
| 2332 | // Has side effects if any element does. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2333 | bool VisitInitListExpr(const InitListExpr *E) { |
Chris Lattner | 363ff23 | 2010-04-13 17:34:23 +0000 | [diff] [blame] | 2334 | for (unsigned i = 0, e = E->getNumInits(); i != e; ++i) |
| 2335 | if (Visit(E->getInit(i))) return true; |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2336 | if (const Expr *filler = E->getArrayFiller()) |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 2337 | return Visit(filler); |
Chris Lattner | 363ff23 | 2010-04-13 17:34:23 +0000 | [diff] [blame] | 2338 | return false; |
| 2339 | } |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 2340 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2341 | bool VisitSizeOfPackExpr(const SizeOfPackExpr *) { return false; } |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 2342 | }; |
| 2343 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2344 | class OpaqueValueEvaluation { |
| 2345 | EvalInfo &info; |
| 2346 | OpaqueValueExpr *opaqueValue; |
| 2347 | |
| 2348 | public: |
| 2349 | OpaqueValueEvaluation(EvalInfo &info, OpaqueValueExpr *opaqueValue, |
| 2350 | Expr *value) |
| 2351 | : info(info), opaqueValue(opaqueValue) { |
| 2352 | |
| 2353 | // If evaluation fails, fail immediately. |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 2354 | if (!Evaluate(info.OpaqueValues[opaqueValue], info, value)) { |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2355 | this->opaqueValue = 0; |
| 2356 | return; |
| 2357 | } |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2358 | } |
| 2359 | |
| 2360 | bool hasError() const { return opaqueValue == 0; } |
| 2361 | |
| 2362 | ~OpaqueValueEvaluation() { |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 2363 | // FIXME: For a recursive constexpr call, an outer stack frame might have |
| 2364 | // been using this opaque value too, and will now have to re-evaluate the |
| 2365 | // source expression. |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2366 | if (opaqueValue) info.OpaqueValues.erase(opaqueValue); |
| 2367 | } |
| 2368 | }; |
| 2369 | |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 2370 | } // end anonymous namespace |
| 2371 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2372 | //===----------------------------------------------------------------------===// |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2373 | // Generic Evaluation |
| 2374 | //===----------------------------------------------------------------------===// |
| 2375 | namespace { |
| 2376 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2377 | // FIXME: RetTy is always bool. Remove it. |
| 2378 | template <class Derived, typename RetTy=bool> |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2379 | class ExprEvaluatorBase |
| 2380 | : public ConstStmtVisitor<Derived, RetTy> { |
| 2381 | private: |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2382 | RetTy DerivedSuccess(const APValue &V, const Expr *E) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2383 | return static_cast<Derived*>(this)->Success(V, E); |
| 2384 | } |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2385 | RetTy DerivedZeroInitialization(const Expr *E) { |
| 2386 | return static_cast<Derived*>(this)->ZeroInitialization(E); |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2387 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2388 | |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 2389 | // Check whether a conditional operator with a non-constant condition is a |
| 2390 | // potential constant expression. If neither arm is a potential constant |
| 2391 | // expression, then the conditional operator is not either. |
| 2392 | template<typename ConditionalOperator> |
| 2393 | void CheckPotentialConstantConditional(const ConditionalOperator *E) { |
| 2394 | assert(Info.CheckingPotentialConstantExpression); |
| 2395 | |
| 2396 | // Speculatively evaluate both arms. |
| 2397 | { |
| 2398 | llvm::SmallVector<PartialDiagnosticAt, 8> Diag; |
| 2399 | SpeculativeEvaluationRAII Speculate(Info, &Diag); |
| 2400 | |
| 2401 | StmtVisitorTy::Visit(E->getFalseExpr()); |
| 2402 | if (Diag.empty()) |
| 2403 | return; |
| 2404 | |
| 2405 | Diag.clear(); |
| 2406 | StmtVisitorTy::Visit(E->getTrueExpr()); |
| 2407 | if (Diag.empty()) |
| 2408 | return; |
| 2409 | } |
| 2410 | |
| 2411 | Error(E, diag::note_constexpr_conditional_never_const); |
| 2412 | } |
| 2413 | |
| 2414 | |
| 2415 | template<typename ConditionalOperator> |
| 2416 | bool HandleConditionalOperator(const ConditionalOperator *E) { |
| 2417 | bool BoolResult; |
| 2418 | if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info)) { |
| 2419 | if (Info.CheckingPotentialConstantExpression) |
| 2420 | CheckPotentialConstantConditional(E); |
| 2421 | return false; |
| 2422 | } |
| 2423 | |
| 2424 | Expr *EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
| 2425 | return StmtVisitorTy::Visit(EvalExpr); |
| 2426 | } |
| 2427 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2428 | protected: |
| 2429 | EvalInfo &Info; |
| 2430 | typedef ConstStmtVisitor<Derived, RetTy> StmtVisitorTy; |
| 2431 | typedef ExprEvaluatorBase ExprEvaluatorBaseTy; |
| 2432 | |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 2433 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 2434 | return Info.CCEDiag(E, D); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2435 | } |
| 2436 | |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 2437 | RetTy ZeroInitialization(const Expr *E) { return Error(E); } |
| 2438 | |
| 2439 | public: |
| 2440 | ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {} |
| 2441 | |
| 2442 | EvalInfo &getEvalInfo() { return Info; } |
| 2443 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2444 | /// Report an evaluation error. This should only be called when an error is |
| 2445 | /// first discovered. When propagating an error, just return false. |
| 2446 | bool Error(const Expr *E, diag::kind D) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 2447 | Info.Diag(E, D); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2448 | return false; |
| 2449 | } |
| 2450 | bool Error(const Expr *E) { |
| 2451 | return Error(E, diag::note_invalid_subexpr_in_const_expr); |
| 2452 | } |
| 2453 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2454 | RetTy VisitStmt(const Stmt *) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2455 | llvm_unreachable("Expression evaluator should not be called on stmts"); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2456 | } |
| 2457 | RetTy VisitExpr(const Expr *E) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2458 | return Error(E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2459 | } |
| 2460 | |
| 2461 | RetTy VisitParenExpr(const ParenExpr *E) |
| 2462 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| 2463 | RetTy VisitUnaryExtension(const UnaryOperator *E) |
| 2464 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| 2465 | RetTy VisitUnaryPlus(const UnaryOperator *E) |
| 2466 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| 2467 | RetTy VisitChooseExpr(const ChooseExpr *E) |
| 2468 | { return StmtVisitorTy::Visit(E->getChosenSubExpr(Info.Ctx)); } |
| 2469 | RetTy VisitGenericSelectionExpr(const GenericSelectionExpr *E) |
| 2470 | { return StmtVisitorTy::Visit(E->getResultExpr()); } |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 2471 | RetTy VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E) |
| 2472 | { return StmtVisitorTy::Visit(E->getReplacement()); } |
Richard Smith | 3d75ca8 | 2011-11-09 02:12:41 +0000 | [diff] [blame] | 2473 | RetTy VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) |
| 2474 | { return StmtVisitorTy::Visit(E->getExpr()); } |
Richard Smith | bc6abe9 | 2011-12-19 22:12:41 +0000 | [diff] [blame] | 2475 | // We cannot create any objects for which cleanups are required, so there is |
| 2476 | // nothing to do here; all cleanups must come from unevaluated subexpressions. |
| 2477 | RetTy VisitExprWithCleanups(const ExprWithCleanups *E) |
| 2478 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2479 | |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 2480 | RetTy VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E) { |
| 2481 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 0; |
| 2482 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
| 2483 | } |
| 2484 | RetTy VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) { |
| 2485 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 1; |
| 2486 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
| 2487 | } |
| 2488 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2489 | RetTy VisitBinaryOperator(const BinaryOperator *E) { |
| 2490 | switch (E->getOpcode()) { |
| 2491 | default: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2492 | return Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2493 | |
| 2494 | case BO_Comma: |
| 2495 | VisitIgnoredValue(E->getLHS()); |
| 2496 | return StmtVisitorTy::Visit(E->getRHS()); |
| 2497 | |
| 2498 | case BO_PtrMemD: |
| 2499 | case BO_PtrMemI: { |
| 2500 | LValue Obj; |
| 2501 | if (!HandleMemberPointerAccess(Info, E, Obj)) |
| 2502 | return false; |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2503 | APValue Result; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2504 | if (!HandleLValueToRValueConversion(Info, E, E->getType(), Obj, Result)) |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2505 | return false; |
| 2506 | return DerivedSuccess(Result, E); |
| 2507 | } |
| 2508 | } |
| 2509 | } |
| 2510 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2511 | RetTy VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) { |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 2512 | // Cache the value of the common expression. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2513 | OpaqueValueEvaluation opaque(Info, E->getOpaqueValue(), E->getCommon()); |
| 2514 | if (opaque.hasError()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2515 | return false; |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2516 | |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 2517 | return HandleConditionalOperator(E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2518 | } |
| 2519 | |
| 2520 | RetTy VisitConditionalOperator(const ConditionalOperator *E) { |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 2521 | bool IsBcpCall = false; |
| 2522 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
| 2523 | // the result is a constant expression if it can be folded without |
| 2524 | // side-effects. This is an important GNU extension. See GCC PR38377 |
| 2525 | // for discussion. |
| 2526 | if (const CallExpr *CallCE = |
| 2527 | dyn_cast<CallExpr>(E->getCond()->IgnoreParenCasts())) |
| 2528 | if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p) |
| 2529 | IsBcpCall = true; |
| 2530 | |
| 2531 | // Always assume __builtin_constant_p(...) ? ... : ... is a potential |
| 2532 | // constant expression; we can't check whether it's potentially foldable. |
| 2533 | if (Info.CheckingPotentialConstantExpression && IsBcpCall) |
| 2534 | return false; |
| 2535 | |
| 2536 | FoldConstant Fold(Info); |
| 2537 | |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 2538 | if (!HandleConditionalOperator(E)) |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 2539 | return false; |
| 2540 | |
| 2541 | if (IsBcpCall) |
| 2542 | Fold.Fold(Info); |
| 2543 | |
| 2544 | return true; |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2545 | } |
| 2546 | |
| 2547 | RetTy VisitOpaqueValueExpr(const OpaqueValueExpr *E) { |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2548 | const APValue *Value = Info.getOpaqueValue(E); |
Argyrios Kyrtzidis | 4278683 | 2011-12-09 02:44:48 +0000 | [diff] [blame] | 2549 | if (!Value) { |
| 2550 | const Expr *Source = E->getSourceExpr(); |
| 2551 | if (!Source) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2552 | return Error(E); |
Argyrios Kyrtzidis | 4278683 | 2011-12-09 02:44:48 +0000 | [diff] [blame] | 2553 | if (Source == E) { // sanity checking. |
| 2554 | assert(0 && "OpaqueValueExpr recursively refers to itself"); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2555 | return Error(E); |
Argyrios Kyrtzidis | 4278683 | 2011-12-09 02:44:48 +0000 | [diff] [blame] | 2556 | } |
| 2557 | return StmtVisitorTy::Visit(Source); |
| 2558 | } |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2559 | return DerivedSuccess(*Value, E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2560 | } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2561 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2562 | RetTy VisitCallExpr(const CallExpr *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2563 | const Expr *Callee = E->getCallee()->IgnoreParens(); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2564 | QualType CalleeType = Callee->getType(); |
| 2565 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2566 | const FunctionDecl *FD = 0; |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2567 | LValue *This = 0, ThisVal; |
| 2568 | llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs()); |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 2569 | bool HasQualifier = false; |
Richard Smith | 6c95787 | 2011-11-10 09:31:24 +0000 | [diff] [blame] | 2570 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2571 | // Extract function decl and 'this' pointer from the callee. |
| 2572 | if (CalleeType->isSpecificBuiltinType(BuiltinType::BoundMember)) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2573 | const ValueDecl *Member = 0; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2574 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(Callee)) { |
| 2575 | // Explicit bound member calls, such as x.f() or p->g(); |
| 2576 | if (!EvaluateObjectArgument(Info, ME->getBase(), ThisVal)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2577 | return false; |
| 2578 | Member = ME->getMemberDecl(); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2579 | This = &ThisVal; |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 2580 | HasQualifier = ME->hasQualifier(); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2581 | } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(Callee)) { |
| 2582 | // Indirect bound member calls ('.*' or '->*'). |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2583 | Member = HandleMemberPointerAccess(Info, BE, ThisVal, false); |
| 2584 | if (!Member) return false; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2585 | This = &ThisVal; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2586 | } else |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2587 | return Error(Callee); |
| 2588 | |
| 2589 | FD = dyn_cast<FunctionDecl>(Member); |
| 2590 | if (!FD) |
| 2591 | return Error(Callee); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2592 | } else if (CalleeType->isFunctionPointerType()) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2593 | LValue Call; |
| 2594 | if (!EvaluatePointer(Callee, Call, Info)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2595 | return false; |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2596 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2597 | if (!Call.getLValueOffset().isZero()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2598 | return Error(Callee); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2599 | FD = dyn_cast_or_null<FunctionDecl>( |
| 2600 | Call.getLValueBase().dyn_cast<const ValueDecl*>()); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2601 | if (!FD) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2602 | return Error(Callee); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2603 | |
| 2604 | // Overloaded operator calls to member functions are represented as normal |
| 2605 | // calls with '*this' as the first argument. |
| 2606 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
| 2607 | if (MD && !MD->isStatic()) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2608 | // FIXME: When selecting an implicit conversion for an overloaded |
| 2609 | // operator delete, we sometimes try to evaluate calls to conversion |
| 2610 | // operators without a 'this' parameter! |
| 2611 | if (Args.empty()) |
| 2612 | return Error(E); |
| 2613 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2614 | if (!EvaluateObjectArgument(Info, Args[0], ThisVal)) |
| 2615 | return false; |
| 2616 | This = &ThisVal; |
| 2617 | Args = Args.slice(1); |
| 2618 | } |
| 2619 | |
| 2620 | // Don't call function pointers which have been cast to some other type. |
| 2621 | if (!Info.Ctx.hasSameType(CalleeType->getPointeeType(), FD->getType())) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2622 | return Error(E); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2623 | } else |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2624 | return Error(E); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2625 | |
Richard Smith | b04035a | 2012-02-01 02:39:43 +0000 | [diff] [blame] | 2626 | if (This && !This->checkSubobject(Info, E, CSK_This)) |
| 2627 | return false; |
| 2628 | |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 2629 | // DR1358 allows virtual constexpr functions in some cases. Don't allow |
| 2630 | // calls to such functions in constant expressions. |
| 2631 | if (This && !HasQualifier && |
| 2632 | isa<CXXMethodDecl>(FD) && cast<CXXMethodDecl>(FD)->isVirtual()) |
| 2633 | return Error(E, diag::note_constexpr_virtual_call); |
| 2634 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2635 | const FunctionDecl *Definition = 0; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2636 | Stmt *Body = FD->getBody(Definition); |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2637 | APValue Result; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2638 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2639 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition) || |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2640 | !HandleFunctionCall(E->getExprLoc(), Definition, This, Args, Body, |
| 2641 | Info, Result)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2642 | return false; |
| 2643 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2644 | return DerivedSuccess(Result, E); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2645 | } |
| 2646 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2647 | RetTy VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
| 2648 | return StmtVisitorTy::Visit(E->getInitializer()); |
| 2649 | } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2650 | RetTy VisitInitListExpr(const InitListExpr *E) { |
Eli Friedman | 71523d6 | 2012-01-03 23:54:05 +0000 | [diff] [blame] | 2651 | if (E->getNumInits() == 0) |
| 2652 | return DerivedZeroInitialization(E); |
| 2653 | if (E->getNumInits() == 1) |
| 2654 | return StmtVisitorTy::Visit(E->getInit(0)); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2655 | return Error(E); |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2656 | } |
| 2657 | RetTy VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2658 | return DerivedZeroInitialization(E); |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2659 | } |
| 2660 | RetTy VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2661 | return DerivedZeroInitialization(E); |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2662 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2663 | RetTy VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2664 | return DerivedZeroInitialization(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2665 | } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2666 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2667 | /// A member expression where the object is a prvalue is itself a prvalue. |
| 2668 | RetTy VisitMemberExpr(const MemberExpr *E) { |
| 2669 | assert(!E->isArrow() && "missing call to bound member function?"); |
| 2670 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2671 | APValue Val; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2672 | if (!Evaluate(Val, Info, E->getBase())) |
| 2673 | return false; |
| 2674 | |
| 2675 | QualType BaseTy = E->getBase()->getType(); |
| 2676 | |
| 2677 | const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2678 | if (!FD) return Error(E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2679 | assert(!FD->getType()->isReferenceType() && "prvalue reference?"); |
| 2680 | assert(BaseTy->getAs<RecordType>()->getDecl()->getCanonicalDecl() == |
| 2681 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
| 2682 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2683 | SubobjectDesignator Designator(BaseTy); |
| 2684 | Designator.addDeclUnchecked(FD); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2685 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2686 | return ExtractSubobject(Info, E, Val, BaseTy, Designator, E->getType()) && |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2687 | DerivedSuccess(Val, E); |
| 2688 | } |
| 2689 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2690 | RetTy VisitCastExpr(const CastExpr *E) { |
| 2691 | switch (E->getCastKind()) { |
| 2692 | default: |
| 2693 | break; |
| 2694 | |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 2695 | case CK_AtomicToNonAtomic: |
| 2696 | case CK_NonAtomicToAtomic: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2697 | case CK_NoOp: |
Richard Smith | 7d580a4 | 2012-01-17 21:17:26 +0000 | [diff] [blame] | 2698 | case CK_UserDefinedConversion: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2699 | return StmtVisitorTy::Visit(E->getSubExpr()); |
| 2700 | |
| 2701 | case CK_LValueToRValue: { |
| 2702 | LValue LVal; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2703 | if (!EvaluateLValue(E->getSubExpr(), LVal, Info)) |
| 2704 | return false; |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2705 | APValue RVal; |
Richard Smith | 9ec7197 | 2012-02-05 01:23:16 +0000 | [diff] [blame] | 2706 | // Note, we use the subexpression's type in order to retain cv-qualifiers. |
| 2707 | if (!HandleLValueToRValueConversion(Info, E, E->getSubExpr()->getType(), |
| 2708 | LVal, RVal)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2709 | return false; |
| 2710 | return DerivedSuccess(RVal, E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2711 | } |
| 2712 | } |
| 2713 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2714 | return Error(E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2715 | } |
| 2716 | |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 2717 | /// Visit a value which is evaluated, but whose value is ignored. |
| 2718 | void VisitIgnoredValue(const Expr *E) { |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2719 | APValue Scratch; |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 2720 | if (!Evaluate(Scratch, Info, E)) |
| 2721 | Info.EvalStatus.HasSideEffects = true; |
| 2722 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2723 | }; |
| 2724 | |
| 2725 | } |
| 2726 | |
| 2727 | //===----------------------------------------------------------------------===// |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2728 | // Common base class for lvalue and temporary evaluation. |
| 2729 | //===----------------------------------------------------------------------===// |
| 2730 | namespace { |
| 2731 | template<class Derived> |
| 2732 | class LValueExprEvaluatorBase |
| 2733 | : public ExprEvaluatorBase<Derived, bool> { |
| 2734 | protected: |
| 2735 | LValue &Result; |
| 2736 | typedef LValueExprEvaluatorBase LValueExprEvaluatorBaseTy; |
| 2737 | typedef ExprEvaluatorBase<Derived, bool> ExprEvaluatorBaseTy; |
| 2738 | |
| 2739 | bool Success(APValue::LValueBase B) { |
| 2740 | Result.set(B); |
| 2741 | return true; |
| 2742 | } |
| 2743 | |
| 2744 | public: |
| 2745 | LValueExprEvaluatorBase(EvalInfo &Info, LValue &Result) : |
| 2746 | ExprEvaluatorBaseTy(Info), Result(Result) {} |
| 2747 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2748 | bool Success(const APValue &V, const Expr *E) { |
| 2749 | Result.setFrom(this->Info.Ctx, V); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2750 | return true; |
| 2751 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2752 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2753 | bool VisitMemberExpr(const MemberExpr *E) { |
| 2754 | // Handle non-static data members. |
| 2755 | QualType BaseTy; |
| 2756 | if (E->isArrow()) { |
| 2757 | if (!EvaluatePointer(E->getBase(), Result, this->Info)) |
| 2758 | return false; |
| 2759 | BaseTy = E->getBase()->getType()->getAs<PointerType>()->getPointeeType(); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2760 | } else if (E->getBase()->isRValue()) { |
Richard Smith | af2c7a1 | 2011-12-19 22:01:37 +0000 | [diff] [blame] | 2761 | assert(E->getBase()->getType()->isRecordType()); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2762 | if (!EvaluateTemporary(E->getBase(), Result, this->Info)) |
| 2763 | return false; |
| 2764 | BaseTy = E->getBase()->getType(); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2765 | } else { |
| 2766 | if (!this->Visit(E->getBase())) |
| 2767 | return false; |
| 2768 | BaseTy = E->getBase()->getType(); |
| 2769 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2770 | |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2771 | const ValueDecl *MD = E->getMemberDecl(); |
| 2772 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) { |
| 2773 | assert(BaseTy->getAs<RecordType>()->getDecl()->getCanonicalDecl() == |
| 2774 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
| 2775 | (void)BaseTy; |
| 2776 | HandleLValueMember(this->Info, E, Result, FD); |
| 2777 | } else if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(MD)) { |
| 2778 | HandleLValueIndirectMember(this->Info, E, Result, IFD); |
| 2779 | } else |
| 2780 | return this->Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2781 | |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2782 | if (MD->getType()->isReferenceType()) { |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2783 | APValue RefValue; |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2784 | if (!HandleLValueToRValueConversion(this->Info, E, MD->getType(), Result, |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2785 | RefValue)) |
| 2786 | return false; |
| 2787 | return Success(RefValue, E); |
| 2788 | } |
| 2789 | return true; |
| 2790 | } |
| 2791 | |
| 2792 | bool VisitBinaryOperator(const BinaryOperator *E) { |
| 2793 | switch (E->getOpcode()) { |
| 2794 | default: |
| 2795 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
| 2796 | |
| 2797 | case BO_PtrMemD: |
| 2798 | case BO_PtrMemI: |
| 2799 | return HandleMemberPointerAccess(this->Info, E, Result); |
| 2800 | } |
| 2801 | } |
| 2802 | |
| 2803 | bool VisitCastExpr(const CastExpr *E) { |
| 2804 | switch (E->getCastKind()) { |
| 2805 | default: |
| 2806 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 2807 | |
| 2808 | case CK_DerivedToBase: |
| 2809 | case CK_UncheckedDerivedToBase: { |
| 2810 | if (!this->Visit(E->getSubExpr())) |
| 2811 | return false; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2812 | |
| 2813 | // Now figure out the necessary offset to add to the base LV to get from |
| 2814 | // the derived class to the base class. |
| 2815 | QualType Type = E->getSubExpr()->getType(); |
| 2816 | |
| 2817 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 2818 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2819 | if (!HandleLValueBase(this->Info, E, Result, Type->getAsCXXRecordDecl(), |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2820 | *PathI)) |
| 2821 | return false; |
| 2822 | Type = (*PathI)->getType(); |
| 2823 | } |
| 2824 | |
| 2825 | return true; |
| 2826 | } |
| 2827 | } |
| 2828 | } |
| 2829 | }; |
| 2830 | } |
| 2831 | |
| 2832 | //===----------------------------------------------------------------------===// |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2833 | // LValue Evaluation |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2834 | // |
| 2835 | // This is used for evaluating lvalues (in C and C++), xvalues (in C++11), |
| 2836 | // function designators (in C), decl references to void objects (in C), and |
| 2837 | // temporaries (if building with -Wno-address-of-temporary). |
| 2838 | // |
| 2839 | // LValue evaluation produces values comprising a base expression of one of the |
| 2840 | // following types: |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2841 | // - Declarations |
| 2842 | // * VarDecl |
| 2843 | // * FunctionDecl |
| 2844 | // - Literals |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2845 | // * CompoundLiteralExpr in C |
| 2846 | // * StringLiteral |
Richard Smith | 47d2145 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 2847 | // * CXXTypeidExpr |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2848 | // * PredefinedExpr |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2849 | // * ObjCStringLiteralExpr |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2850 | // * ObjCEncodeExpr |
| 2851 | // * AddrLabelExpr |
| 2852 | // * BlockExpr |
| 2853 | // * CallExpr for a MakeStringConstant builtin |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2854 | // - Locals and temporaries |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2855 | // * Any Expr, with a CallIndex indicating the function in which the temporary |
| 2856 | // was evaluated. |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2857 | // plus an offset in bytes. |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2858 | //===----------------------------------------------------------------------===// |
| 2859 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 2860 | class LValueExprEvaluator |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2861 | : public LValueExprEvaluatorBase<LValueExprEvaluator> { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2862 | public: |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2863 | LValueExprEvaluator(EvalInfo &Info, LValue &Result) : |
| 2864 | LValueExprEvaluatorBaseTy(Info, Result) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2865 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2866 | bool VisitVarDecl(const Expr *E, const VarDecl *VD); |
| 2867 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2868 | bool VisitDeclRefExpr(const DeclRefExpr *E); |
| 2869 | bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(E); } |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 2870 | bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2871 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
| 2872 | bool VisitMemberExpr(const MemberExpr *E); |
| 2873 | bool VisitStringLiteral(const StringLiteral *E) { return Success(E); } |
| 2874 | bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(E); } |
Richard Smith | 47d2145 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 2875 | bool VisitCXXTypeidExpr(const CXXTypeidExpr *E); |
Francois Pichet | e275a18 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 2876 | bool VisitCXXUuidofExpr(const CXXUuidofExpr *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2877 | bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E); |
| 2878 | bool VisitUnaryDeref(const UnaryOperator *E); |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2879 | bool VisitUnaryReal(const UnaryOperator *E); |
| 2880 | bool VisitUnaryImag(const UnaryOperator *E); |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 2881 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2882 | bool VisitCastExpr(const CastExpr *E) { |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 2883 | switch (E->getCastKind()) { |
| 2884 | default: |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2885 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 2886 | |
Eli Friedman | db92422 | 2011-10-11 00:13:24 +0000 | [diff] [blame] | 2887 | case CK_LValueBitCast: |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 2888 | this->CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 2889 | if (!Visit(E->getSubExpr())) |
| 2890 | return false; |
| 2891 | Result.Designator.setInvalid(); |
| 2892 | return true; |
Eli Friedman | db92422 | 2011-10-11 00:13:24 +0000 | [diff] [blame] | 2893 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2894 | case CK_BaseToDerived: |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2895 | if (!Visit(E->getSubExpr())) |
| 2896 | return false; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2897 | return HandleBaseToDerivedCast(Info, E, Result); |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 2898 | } |
| 2899 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2900 | }; |
| 2901 | } // end anonymous namespace |
| 2902 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2903 | /// Evaluate an expression as an lvalue. This can be legitimately called on |
| 2904 | /// expressions which are not glvalues, in a few cases: |
| 2905 | /// * function designators in C, |
| 2906 | /// * "extern void" objects, |
| 2907 | /// * temporaries, if building with -Wno-address-of-temporary. |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2908 | static bool EvaluateLValue(const Expr* E, LValue& Result, EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2909 | assert((E->isGLValue() || E->getType()->isFunctionType() || |
| 2910 | E->getType()->isVoidType() || isa<CXXTemporaryObjectExpr>(E)) && |
| 2911 | "can't evaluate expression as an lvalue"); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2912 | return LValueExprEvaluator(Info, Result).Visit(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2913 | } |
| 2914 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2915 | bool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2916 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl())) |
| 2917 | return Success(FD); |
| 2918 | if (const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2919 | return VisitVarDecl(E, VD); |
| 2920 | return Error(E); |
| 2921 | } |
Richard Smith | 436c889 | 2011-10-24 23:14:33 +0000 | [diff] [blame] | 2922 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2923 | bool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) { |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 2924 | if (!VD->getType()->isReferenceType()) { |
| 2925 | if (isa<ParmVarDecl>(VD)) { |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2926 | Result.set(VD, Info.CurrentCall->Index); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 2927 | return true; |
| 2928 | } |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2929 | return Success(VD); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 2930 | } |
Eli Friedman | 50c39ea | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 2931 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2932 | APValue V; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2933 | if (!EvaluateVarDeclInit(Info, E, VD, Info.CurrentCall, V)) |
| 2934 | return false; |
| 2935 | return Success(V, E); |
Anders Carlsson | 35873c4 | 2008-11-24 04:41:22 +0000 | [diff] [blame] | 2936 | } |
| 2937 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 2938 | bool LValueExprEvaluator::VisitMaterializeTemporaryExpr( |
| 2939 | const MaterializeTemporaryExpr *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2940 | if (E->GetTemporaryExpr()->isRValue()) { |
Richard Smith | af2c7a1 | 2011-12-19 22:01:37 +0000 | [diff] [blame] | 2941 | if (E->getType()->isRecordType()) |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2942 | return EvaluateTemporary(E->GetTemporaryExpr(), Result, Info); |
| 2943 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2944 | Result.set(E, Info.CurrentCall->Index); |
| 2945 | return EvaluateInPlace(Info.CurrentCall->Temporaries[E], Info, |
| 2946 | Result, E->GetTemporaryExpr()); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2947 | } |
| 2948 | |
| 2949 | // Materialization of an lvalue temporary occurs when we need to force a copy |
| 2950 | // (for instance, if it's a bitfield). |
| 2951 | // FIXME: The AST should contain an lvalue-to-rvalue node for such cases. |
| 2952 | if (!Visit(E->GetTemporaryExpr())) |
| 2953 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2954 | if (!HandleLValueToRValueConversion(Info, E, E->getType(), Result, |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2955 | Info.CurrentCall->Temporaries[E])) |
| 2956 | return false; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2957 | Result.set(E, Info.CurrentCall->Index); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2958 | return true; |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 2959 | } |
| 2960 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2961 | bool |
| 2962 | LValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2963 | assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?"); |
| 2964 | // Defer visiting the literal until the lvalue-to-rvalue conversion. We can |
| 2965 | // only see this when folding in C, so there's no standard to follow here. |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2966 | return Success(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2967 | } |
| 2968 | |
Richard Smith | 47d2145 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 2969 | bool LValueExprEvaluator::VisitCXXTypeidExpr(const CXXTypeidExpr *E) { |
| 2970 | if (E->isTypeOperand()) |
| 2971 | return Success(E); |
| 2972 | CXXRecordDecl *RD = E->getExprOperand()->getType()->getAsCXXRecordDecl(); |
| 2973 | if (RD && RD->isPolymorphic()) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 2974 | Info.Diag(E, diag::note_constexpr_typeid_polymorphic) |
Richard Smith | 47d2145 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 2975 | << E->getExprOperand()->getType() |
| 2976 | << E->getExprOperand()->getSourceRange(); |
| 2977 | return false; |
| 2978 | } |
| 2979 | return Success(E); |
| 2980 | } |
| 2981 | |
Francois Pichet | e275a18 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 2982 | bool LValueExprEvaluator::VisitCXXUuidofExpr(const CXXUuidofExpr *E) { |
| 2983 | return Success(E); |
| 2984 | } |
| 2985 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2986 | bool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2987 | // Handle static data members. |
| 2988 | if (const VarDecl *VD = dyn_cast<VarDecl>(E->getMemberDecl())) { |
| 2989 | VisitIgnoredValue(E->getBase()); |
| 2990 | return VisitVarDecl(E, VD); |
| 2991 | } |
| 2992 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2993 | // Handle static member functions. |
| 2994 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl())) { |
| 2995 | if (MD->isStatic()) { |
| 2996 | VisitIgnoredValue(E->getBase()); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2997 | return Success(MD); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2998 | } |
| 2999 | } |
| 3000 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3001 | // Handle non-static data members. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3002 | return LValueExprEvaluatorBaseTy::VisitMemberExpr(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3003 | } |
| 3004 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3005 | bool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3006 | // FIXME: Deal with vectors as array subscript bases. |
| 3007 | if (E->getBase()->getType()->isVectorType()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3008 | return Error(E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3009 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 3010 | if (!EvaluatePointer(E->getBase(), Result, Info)) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3011 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3012 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 3013 | APSInt Index; |
| 3014 | if (!EvaluateInteger(E->getIdx(), Index, Info)) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3015 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3016 | int64_t IndexValue |
| 3017 | = Index.isSigned() ? Index.getSExtValue() |
| 3018 | : static_cast<int64_t>(Index.getZExtValue()); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 3019 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3020 | return HandleLValueArrayAdjustment(Info, E, Result, E->getType(), IndexValue); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 3021 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3022 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3023 | bool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3024 | return EvaluatePointer(E->getSubExpr(), Result, Info); |
Eli Friedman | e8761c8 | 2009-02-20 01:57:15 +0000 | [diff] [blame] | 3025 | } |
| 3026 | |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 3027 | bool LValueExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
| 3028 | if (!Visit(E->getSubExpr())) |
| 3029 | return false; |
| 3030 | // __real is a no-op on scalar lvalues. |
| 3031 | if (E->getSubExpr()->getType()->isAnyComplexType()) |
| 3032 | HandleLValueComplexElement(Info, E, Result, E->getType(), false); |
| 3033 | return true; |
| 3034 | } |
| 3035 | |
| 3036 | bool LValueExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
| 3037 | assert(E->getSubExpr()->getType()->isAnyComplexType() && |
| 3038 | "lvalue __imag__ on scalar?"); |
| 3039 | if (!Visit(E->getSubExpr())) |
| 3040 | return false; |
| 3041 | HandleLValueComplexElement(Info, E, Result, E->getType(), true); |
| 3042 | return true; |
| 3043 | } |
| 3044 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3045 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3046 | // Pointer Evaluation |
| 3047 | //===----------------------------------------------------------------------===// |
| 3048 | |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 3049 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 3050 | class PointerExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3051 | : public ExprEvaluatorBase<PointerExprEvaluator, bool> { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3052 | LValue &Result; |
| 3053 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3054 | bool Success(const Expr *E) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 3055 | Result.set(E); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3056 | return true; |
| 3057 | } |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 3058 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3059 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3060 | PointerExprEvaluator(EvalInfo &info, LValue &Result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3061 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3062 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3063 | bool Success(const APValue &V, const Expr *E) { |
| 3064 | Result.setFrom(Info.Ctx, V); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3065 | return true; |
| 3066 | } |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3067 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 3068 | return Success((Expr*)0); |
| 3069 | } |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 3070 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3071 | bool VisitBinaryOperator(const BinaryOperator *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3072 | bool VisitCastExpr(const CastExpr* E); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3073 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3074 | bool VisitObjCStringLiteral(const ObjCStringLiteral *E) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3075 | { return Success(E); } |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 3076 | bool VisitObjCBoxedExpr(const ObjCBoxedExpr *E) |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 3077 | { return Success(E); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3078 | bool VisitAddrLabelExpr(const AddrLabelExpr *E) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3079 | { return Success(E); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3080 | bool VisitCallExpr(const CallExpr *E); |
| 3081 | bool VisitBlockExpr(const BlockExpr *E) { |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 3082 | if (!E->getBlockDecl()->hasCaptures()) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3083 | return Success(E); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3084 | return Error(E); |
Mike Stump | b83d287 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 3085 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3086 | bool VisitCXXThisExpr(const CXXThisExpr *E) { |
| 3087 | if (!Info.CurrentCall->This) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3088 | return Error(E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3089 | Result = *Info.CurrentCall->This; |
| 3090 | return true; |
| 3091 | } |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3092 | |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 3093 | // FIXME: Missing: @protocol, @selector |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 3094 | }; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3095 | } // end anonymous namespace |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 3096 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3097 | static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3098 | assert(E->isRValue() && E->getType()->hasPointerRepresentation()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3099 | return PointerExprEvaluator(Info, Result).Visit(E); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3100 | } |
| 3101 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3102 | bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3103 | if (E->getOpcode() != BO_Add && |
| 3104 | E->getOpcode() != BO_Sub) |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3105 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3106 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3107 | const Expr *PExp = E->getLHS(); |
| 3108 | const Expr *IExp = E->getRHS(); |
| 3109 | if (IExp->getType()->isPointerType()) |
| 3110 | std::swap(PExp, IExp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3111 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3112 | bool EvalPtrOK = EvaluatePointer(PExp, Result, Info); |
| 3113 | if (!EvalPtrOK && !Info.keepEvaluatingAfterFailure()) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3114 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3115 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3116 | llvm::APSInt Offset; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3117 | if (!EvaluateInteger(IExp, Offset, Info) || !EvalPtrOK) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3118 | return false; |
| 3119 | int64_t AdditionalOffset |
| 3120 | = Offset.isSigned() ? Offset.getSExtValue() |
| 3121 | : static_cast<int64_t>(Offset.getZExtValue()); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 3122 | if (E->getOpcode() == BO_Sub) |
| 3123 | AdditionalOffset = -AdditionalOffset; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3124 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3125 | QualType Pointee = PExp->getType()->getAs<PointerType>()->getPointeeType(); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3126 | return HandleLValueArrayAdjustment(Info, E, Result, Pointee, |
| 3127 | AdditionalOffset); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3128 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3129 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3130 | bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
| 3131 | return EvaluateLValue(E->getSubExpr(), Result, Info); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3132 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3133 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3134 | bool PointerExprEvaluator::VisitCastExpr(const CastExpr* E) { |
| 3135 | const Expr* SubExpr = E->getSubExpr(); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3136 | |
Eli Friedman | 09a8a0e | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 3137 | switch (E->getCastKind()) { |
| 3138 | default: |
| 3139 | break; |
| 3140 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3141 | case CK_BitCast: |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3142 | case CK_CPointerToObjCPointerCast: |
| 3143 | case CK_BlockPointerToObjCPointerCast: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3144 | case CK_AnyPointerToBlockPointerCast: |
Richard Smith | 28c1ce7 | 2012-01-15 03:25:41 +0000 | [diff] [blame] | 3145 | if (!Visit(SubExpr)) |
| 3146 | return false; |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 3147 | // Bitcasts to cv void* are static_casts, not reinterpret_casts, so are |
| 3148 | // permitted in constant expressions in C++11. Bitcasts from cv void* are |
| 3149 | // also static_casts, but we disallow them as a resolution to DR1312. |
Richard Smith | 4cd9b8f | 2011-12-12 19:10:03 +0000 | [diff] [blame] | 3150 | if (!E->getType()->isVoidPointerType()) { |
Richard Smith | 28c1ce7 | 2012-01-15 03:25:41 +0000 | [diff] [blame] | 3151 | Result.Designator.setInvalid(); |
Richard Smith | 4cd9b8f | 2011-12-12 19:10:03 +0000 | [diff] [blame] | 3152 | if (SubExpr->getType()->isVoidPointerType()) |
| 3153 | CCEDiag(E, diag::note_constexpr_invalid_cast) |
| 3154 | << 3 << SubExpr->getType(); |
| 3155 | else |
| 3156 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 3157 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 3158 | return true; |
Eli Friedman | 09a8a0e | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 3159 | |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 3160 | case CK_DerivedToBase: |
| 3161 | case CK_UncheckedDerivedToBase: { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3162 | if (!EvaluatePointer(E->getSubExpr(), Result, Info)) |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 3163 | return false; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3164 | if (!Result.Base && Result.Offset.isZero()) |
| 3165 | return true; |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 3166 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3167 | // Now figure out the necessary offset to add to the base LV to get from |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 3168 | // the derived class to the base class. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3169 | QualType Type = |
| 3170 | E->getSubExpr()->getType()->castAs<PointerType>()->getPointeeType(); |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 3171 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3172 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 3173 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3174 | if (!HandleLValueBase(Info, E, Result, Type->getAsCXXRecordDecl(), |
| 3175 | *PathI)) |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 3176 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3177 | Type = (*PathI)->getType(); |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 3178 | } |
| 3179 | |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 3180 | return true; |
| 3181 | } |
| 3182 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3183 | case CK_BaseToDerived: |
| 3184 | if (!Visit(E->getSubExpr())) |
| 3185 | return false; |
| 3186 | if (!Result.Base && Result.Offset.isZero()) |
| 3187 | return true; |
| 3188 | return HandleBaseToDerivedCast(Info, E, Result); |
| 3189 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3190 | case CK_NullToPointer: |
Richard Smith | 49149fe | 2012-04-08 08:02:07 +0000 | [diff] [blame] | 3191 | VisitIgnoredValue(E->getSubExpr()); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3192 | return ZeroInitialization(E); |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 3193 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3194 | case CK_IntegralToPointer: { |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 3195 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 3196 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3197 | APValue Value; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3198 | if (!EvaluateIntegerOrLValue(SubExpr, Value, Info)) |
Eli Friedman | 09a8a0e | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 3199 | break; |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 3200 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3201 | if (Value.isInt()) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3202 | unsigned Size = Info.Ctx.getTypeSize(E->getType()); |
| 3203 | uint64_t N = Value.getInt().extOrTrunc(Size).getZExtValue(); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 3204 | Result.Base = (Expr*)0; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3205 | Result.Offset = CharUnits::fromQuantity(N); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3206 | Result.CallIndex = 0; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 3207 | Result.Designator.setInvalid(); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3208 | return true; |
| 3209 | } else { |
| 3210 | // Cast is of an lvalue, no need to change value. |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3211 | Result.setFrom(Info.Ctx, Value); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3212 | return true; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3213 | } |
| 3214 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3215 | case CK_ArrayToPointerDecay: |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3216 | if (SubExpr->isGLValue()) { |
| 3217 | if (!EvaluateLValue(SubExpr, Result, Info)) |
| 3218 | return false; |
| 3219 | } else { |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3220 | Result.set(SubExpr, Info.CurrentCall->Index); |
| 3221 | if (!EvaluateInPlace(Info.CurrentCall->Temporaries[SubExpr], |
| 3222 | Info, Result, SubExpr)) |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3223 | return false; |
| 3224 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 3225 | // The result is a pointer to the first element of the array. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3226 | if (const ConstantArrayType *CAT |
| 3227 | = Info.Ctx.getAsConstantArrayType(SubExpr->getType())) |
| 3228 | Result.addArray(Info, E, CAT); |
| 3229 | else |
| 3230 | Result.Designator.setInvalid(); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 3231 | return true; |
Richard Smith | 6a7c94a | 2011-10-31 20:57:44 +0000 | [diff] [blame] | 3232 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3233 | case CK_FunctionToPointerDecay: |
Richard Smith | 6a7c94a | 2011-10-31 20:57:44 +0000 | [diff] [blame] | 3234 | return EvaluateLValue(SubExpr, Result, Info); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3235 | } |
| 3236 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3237 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3238 | } |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3239 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3240 | bool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3241 | if (IsStringLiteralCall(E)) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3242 | return Success(E); |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 3243 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3244 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3245 | } |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3246 | |
| 3247 | //===----------------------------------------------------------------------===// |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3248 | // Member Pointer Evaluation |
| 3249 | //===----------------------------------------------------------------------===// |
| 3250 | |
| 3251 | namespace { |
| 3252 | class MemberPointerExprEvaluator |
| 3253 | : public ExprEvaluatorBase<MemberPointerExprEvaluator, bool> { |
| 3254 | MemberPtr &Result; |
| 3255 | |
| 3256 | bool Success(const ValueDecl *D) { |
| 3257 | Result = MemberPtr(D); |
| 3258 | return true; |
| 3259 | } |
| 3260 | public: |
| 3261 | |
| 3262 | MemberPointerExprEvaluator(EvalInfo &Info, MemberPtr &Result) |
| 3263 | : ExprEvaluatorBaseTy(Info), Result(Result) {} |
| 3264 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3265 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3266 | Result.setFrom(V); |
| 3267 | return true; |
| 3268 | } |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3269 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3270 | return Success((const ValueDecl*)0); |
| 3271 | } |
| 3272 | |
| 3273 | bool VisitCastExpr(const CastExpr *E); |
| 3274 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
| 3275 | }; |
| 3276 | } // end anonymous namespace |
| 3277 | |
| 3278 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
| 3279 | EvalInfo &Info) { |
| 3280 | assert(E->isRValue() && E->getType()->isMemberPointerType()); |
| 3281 | return MemberPointerExprEvaluator(Info, Result).Visit(E); |
| 3282 | } |
| 3283 | |
| 3284 | bool MemberPointerExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 3285 | switch (E->getCastKind()) { |
| 3286 | default: |
| 3287 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 3288 | |
| 3289 | case CK_NullToMemberPointer: |
Richard Smith | 49149fe | 2012-04-08 08:02:07 +0000 | [diff] [blame] | 3290 | VisitIgnoredValue(E->getSubExpr()); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3291 | return ZeroInitialization(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3292 | |
| 3293 | case CK_BaseToDerivedMemberPointer: { |
| 3294 | if (!Visit(E->getSubExpr())) |
| 3295 | return false; |
| 3296 | if (E->path_empty()) |
| 3297 | return true; |
| 3298 | // Base-to-derived member pointer casts store the path in derived-to-base |
| 3299 | // order, so iterate backwards. The CXXBaseSpecifier also provides us with |
| 3300 | // the wrong end of the derived->base arc, so stagger the path by one class. |
| 3301 | typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter; |
| 3302 | for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin()); |
| 3303 | PathI != PathE; ++PathI) { |
| 3304 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
| 3305 | const CXXRecordDecl *Derived = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 3306 | if (!Result.castToDerived(Derived)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3307 | return Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3308 | } |
| 3309 | const Type *FinalTy = E->getType()->castAs<MemberPointerType>()->getClass(); |
| 3310 | if (!Result.castToDerived(FinalTy->getAsCXXRecordDecl())) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3311 | return Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3312 | return true; |
| 3313 | } |
| 3314 | |
| 3315 | case CK_DerivedToBaseMemberPointer: |
| 3316 | if (!Visit(E->getSubExpr())) |
| 3317 | return false; |
| 3318 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 3319 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
| 3320 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
| 3321 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 3322 | if (!Result.castToBase(Base)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3323 | return Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3324 | } |
| 3325 | return true; |
| 3326 | } |
| 3327 | } |
| 3328 | |
| 3329 | bool MemberPointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
| 3330 | // C++11 [expr.unary.op]p3 has very strict rules on how the address of a |
| 3331 | // member can be formed. |
| 3332 | return Success(cast<DeclRefExpr>(E->getSubExpr())->getDecl()); |
| 3333 | } |
| 3334 | |
| 3335 | //===----------------------------------------------------------------------===// |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3336 | // Record Evaluation |
| 3337 | //===----------------------------------------------------------------------===// |
| 3338 | |
| 3339 | namespace { |
| 3340 | class RecordExprEvaluator |
| 3341 | : public ExprEvaluatorBase<RecordExprEvaluator, bool> { |
| 3342 | const LValue &This; |
| 3343 | APValue &Result; |
| 3344 | public: |
| 3345 | |
| 3346 | RecordExprEvaluator(EvalInfo &info, const LValue &This, APValue &Result) |
| 3347 | : ExprEvaluatorBaseTy(info), This(This), Result(Result) {} |
| 3348 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3349 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3350 | Result = V; |
| 3351 | return true; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3352 | } |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3353 | bool ZeroInitialization(const Expr *E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3354 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3355 | bool VisitCastExpr(const CastExpr *E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3356 | bool VisitInitListExpr(const InitListExpr *E); |
| 3357 | bool VisitCXXConstructExpr(const CXXConstructExpr *E); |
| 3358 | }; |
| 3359 | } |
| 3360 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3361 | /// Perform zero-initialization on an object of non-union class type. |
| 3362 | /// C++11 [dcl.init]p5: |
| 3363 | /// To zero-initialize an object or reference of type T means: |
| 3364 | /// [...] |
| 3365 | /// -- if T is a (possibly cv-qualified) non-union class type, |
| 3366 | /// each non-static data member and each base-class subobject is |
| 3367 | /// zero-initialized |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3368 | static bool HandleClassZeroInitialization(EvalInfo &Info, const Expr *E, |
| 3369 | const RecordDecl *RD, |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3370 | const LValue &This, APValue &Result) { |
| 3371 | assert(!RD->isUnion() && "Expected non-union class type"); |
| 3372 | const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD); |
| 3373 | Result = APValue(APValue::UninitStruct(), CD ? CD->getNumBases() : 0, |
| 3374 | std::distance(RD->field_begin(), RD->field_end())); |
| 3375 | |
| 3376 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 3377 | |
| 3378 | if (CD) { |
| 3379 | unsigned Index = 0; |
| 3380 | for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(), |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3381 | End = CD->bases_end(); I != End; ++I, ++Index) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3382 | const CXXRecordDecl *Base = I->getType()->getAsCXXRecordDecl(); |
| 3383 | LValue Subobject = This; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3384 | HandleLValueDirectBase(Info, E, Subobject, CD, Base, &Layout); |
| 3385 | if (!HandleClassZeroInitialization(Info, E, Base, Subobject, |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3386 | Result.getStructBase(Index))) |
| 3387 | return false; |
| 3388 | } |
| 3389 | } |
| 3390 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3391 | for (RecordDecl::field_iterator I = RD->field_begin(), End = RD->field_end(); |
| 3392 | I != End; ++I) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3393 | // -- if T is a reference type, no initialization is performed. |
| 3394 | if ((*I)->getType()->isReferenceType()) |
| 3395 | continue; |
| 3396 | |
| 3397 | LValue Subobject = This; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3398 | HandleLValueMember(Info, E, Subobject, *I, &Layout); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3399 | |
| 3400 | ImplicitValueInitExpr VIE((*I)->getType()); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3401 | if (!EvaluateInPlace( |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3402 | Result.getStructField((*I)->getFieldIndex()), Info, Subobject, &VIE)) |
| 3403 | return false; |
| 3404 | } |
| 3405 | |
| 3406 | return true; |
| 3407 | } |
| 3408 | |
| 3409 | bool RecordExprEvaluator::ZeroInitialization(const Expr *E) { |
| 3410 | const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl(); |
John McCall | 1de9d7d | 2012-04-26 18:10:01 +0000 | [diff] [blame] | 3411 | if (RD->isInvalidDecl()) return false; |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3412 | if (RD->isUnion()) { |
| 3413 | // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the |
| 3414 | // object's first non-static named data member is zero-initialized |
| 3415 | RecordDecl::field_iterator I = RD->field_begin(); |
| 3416 | if (I == RD->field_end()) { |
| 3417 | Result = APValue((const FieldDecl*)0); |
| 3418 | return true; |
| 3419 | } |
| 3420 | |
| 3421 | LValue Subobject = This; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3422 | HandleLValueMember(Info, E, Subobject, *I); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3423 | Result = APValue(*I); |
| 3424 | ImplicitValueInitExpr VIE((*I)->getType()); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3425 | return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, &VIE); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3426 | } |
| 3427 | |
Richard Smith | ce582fe | 2012-02-17 00:44:16 +0000 | [diff] [blame] | 3428 | if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->getNumVBases()) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 3429 | Info.Diag(E, diag::note_constexpr_virtual_base) << RD; |
Richard Smith | ce582fe | 2012-02-17 00:44:16 +0000 | [diff] [blame] | 3430 | return false; |
| 3431 | } |
| 3432 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3433 | return HandleClassZeroInitialization(Info, E, RD, This, Result); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3434 | } |
| 3435 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3436 | bool RecordExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 3437 | switch (E->getCastKind()) { |
| 3438 | default: |
| 3439 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 3440 | |
| 3441 | case CK_ConstructorConversion: |
| 3442 | return Visit(E->getSubExpr()); |
| 3443 | |
| 3444 | case CK_DerivedToBase: |
| 3445 | case CK_UncheckedDerivedToBase: { |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3446 | APValue DerivedObject; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3447 | if (!Evaluate(DerivedObject, Info, E->getSubExpr())) |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3448 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3449 | if (!DerivedObject.isStruct()) |
| 3450 | return Error(E->getSubExpr()); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3451 | |
| 3452 | // Derived-to-base rvalue conversion: just slice off the derived part. |
| 3453 | APValue *Value = &DerivedObject; |
| 3454 | const CXXRecordDecl *RD = E->getSubExpr()->getType()->getAsCXXRecordDecl(); |
| 3455 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 3456 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
| 3457 | assert(!(*PathI)->isVirtual() && "record rvalue with virtual base"); |
| 3458 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 3459 | Value = &Value->getStructBase(getBaseIndex(RD, Base)); |
| 3460 | RD = Base; |
| 3461 | } |
| 3462 | Result = *Value; |
| 3463 | return true; |
| 3464 | } |
| 3465 | } |
| 3466 | } |
| 3467 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3468 | bool RecordExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
Sebastian Redl | 24fe798 | 2012-02-19 14:53:49 +0000 | [diff] [blame] | 3469 | // Cannot constant-evaluate std::initializer_list inits. |
| 3470 | if (E->initializesStdInitializerList()) |
| 3471 | return false; |
| 3472 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3473 | const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl(); |
John McCall | 1de9d7d | 2012-04-26 18:10:01 +0000 | [diff] [blame] | 3474 | if (RD->isInvalidDecl()) return false; |
| 3475 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3476 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 3477 | |
| 3478 | if (RD->isUnion()) { |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 3479 | const FieldDecl *Field = E->getInitializedFieldInUnion(); |
| 3480 | Result = APValue(Field); |
| 3481 | if (!Field) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3482 | return true; |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 3483 | |
| 3484 | // If the initializer list for a union does not contain any elements, the |
| 3485 | // first element of the union is value-initialized. |
| 3486 | ImplicitValueInitExpr VIE(Field->getType()); |
| 3487 | const Expr *InitExpr = E->getNumInits() ? E->getInit(0) : &VIE; |
| 3488 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3489 | LValue Subobject = This; |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 3490 | HandleLValueMember(Info, InitExpr, Subobject, Field, &Layout); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3491 | return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, InitExpr); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3492 | } |
| 3493 | |
| 3494 | assert((!isa<CXXRecordDecl>(RD) || !cast<CXXRecordDecl>(RD)->getNumBases()) && |
| 3495 | "initializer list for class with base classes"); |
| 3496 | Result = APValue(APValue::UninitStruct(), 0, |
| 3497 | std::distance(RD->field_begin(), RD->field_end())); |
| 3498 | unsigned ElementNo = 0; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3499 | bool Success = true; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3500 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 3501 | FieldEnd = RD->field_end(); Field != FieldEnd; ++Field) { |
| 3502 | // Anonymous bit-fields are not considered members of the class for |
| 3503 | // purposes of aggregate initialization. |
| 3504 | if (Field->isUnnamedBitfield()) |
| 3505 | continue; |
| 3506 | |
| 3507 | LValue Subobject = This; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3508 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3509 | bool HaveInit = ElementNo < E->getNumInits(); |
| 3510 | |
| 3511 | // FIXME: Diagnostics here should point to the end of the initializer |
| 3512 | // list, not the start. |
| 3513 | HandleLValueMember(Info, HaveInit ? E->getInit(ElementNo) : E, Subobject, |
| 3514 | *Field, &Layout); |
| 3515 | |
| 3516 | // Perform an implicit value-initialization for members beyond the end of |
| 3517 | // the initializer list. |
| 3518 | ImplicitValueInitExpr VIE(HaveInit ? Info.Ctx.IntTy : Field->getType()); |
| 3519 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3520 | if (!EvaluateInPlace( |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3521 | Result.getStructField((*Field)->getFieldIndex()), |
| 3522 | Info, Subobject, HaveInit ? E->getInit(ElementNo++) : &VIE)) { |
| 3523 | if (!Info.keepEvaluatingAfterFailure()) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3524 | return false; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3525 | Success = false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3526 | } |
| 3527 | } |
| 3528 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3529 | return Success; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3530 | } |
| 3531 | |
| 3532 | bool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 3533 | const CXXConstructorDecl *FD = E->getConstructor(); |
John McCall | 1de9d7d | 2012-04-26 18:10:01 +0000 | [diff] [blame] | 3534 | if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl()) return false; |
| 3535 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3536 | bool ZeroInit = E->requiresZeroInitialization(); |
| 3537 | if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) { |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 3538 | // If we've already performed zero-initialization, we're already done. |
| 3539 | if (!Result.isUninit()) |
| 3540 | return true; |
| 3541 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3542 | if (ZeroInit) |
| 3543 | return ZeroInitialization(E); |
| 3544 | |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 3545 | const CXXRecordDecl *RD = FD->getParent(); |
| 3546 | if (RD->isUnion()) |
| 3547 | Result = APValue((FieldDecl*)0); |
| 3548 | else |
| 3549 | Result = APValue(APValue::UninitStruct(), RD->getNumBases(), |
| 3550 | std::distance(RD->field_begin(), RD->field_end())); |
| 3551 | return true; |
| 3552 | } |
| 3553 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3554 | const FunctionDecl *Definition = 0; |
| 3555 | FD->getBody(Definition); |
| 3556 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3557 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition)) |
| 3558 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3559 | |
Richard Smith | 610a60c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 3560 | // Avoid materializing a temporary for an elidable copy/move constructor. |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3561 | if (E->isElidable() && !ZeroInit) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3562 | if (const MaterializeTemporaryExpr *ME |
| 3563 | = dyn_cast<MaterializeTemporaryExpr>(E->getArg(0))) |
| 3564 | return Visit(ME->GetTemporaryExpr()); |
| 3565 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3566 | if (ZeroInit && !ZeroInitialization(E)) |
| 3567 | return false; |
| 3568 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3569 | llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs()); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3570 | return HandleConstructorCall(E->getExprLoc(), This, Args, |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3571 | cast<CXXConstructorDecl>(Definition), Info, |
| 3572 | Result); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3573 | } |
| 3574 | |
| 3575 | static bool EvaluateRecord(const Expr *E, const LValue &This, |
| 3576 | APValue &Result, EvalInfo &Info) { |
| 3577 | assert(E->isRValue() && E->getType()->isRecordType() && |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3578 | "can't evaluate expression as a record rvalue"); |
| 3579 | return RecordExprEvaluator(Info, This, Result).Visit(E); |
| 3580 | } |
| 3581 | |
| 3582 | //===----------------------------------------------------------------------===// |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3583 | // Temporary Evaluation |
| 3584 | // |
| 3585 | // Temporaries are represented in the AST as rvalues, but generally behave like |
| 3586 | // lvalues. The full-object of which the temporary is a subobject is implicitly |
| 3587 | // materialized so that a reference can bind to it. |
| 3588 | //===----------------------------------------------------------------------===// |
| 3589 | namespace { |
| 3590 | class TemporaryExprEvaluator |
| 3591 | : public LValueExprEvaluatorBase<TemporaryExprEvaluator> { |
| 3592 | public: |
| 3593 | TemporaryExprEvaluator(EvalInfo &Info, LValue &Result) : |
| 3594 | LValueExprEvaluatorBaseTy(Info, Result) {} |
| 3595 | |
| 3596 | /// Visit an expression which constructs the value of this temporary. |
| 3597 | bool VisitConstructExpr(const Expr *E) { |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3598 | Result.set(E, Info.CurrentCall->Index); |
| 3599 | return EvaluateInPlace(Info.CurrentCall->Temporaries[E], Info, Result, E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3600 | } |
| 3601 | |
| 3602 | bool VisitCastExpr(const CastExpr *E) { |
| 3603 | switch (E->getCastKind()) { |
| 3604 | default: |
| 3605 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
| 3606 | |
| 3607 | case CK_ConstructorConversion: |
| 3608 | return VisitConstructExpr(E->getSubExpr()); |
| 3609 | } |
| 3610 | } |
| 3611 | bool VisitInitListExpr(const InitListExpr *E) { |
| 3612 | return VisitConstructExpr(E); |
| 3613 | } |
| 3614 | bool VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 3615 | return VisitConstructExpr(E); |
| 3616 | } |
| 3617 | bool VisitCallExpr(const CallExpr *E) { |
| 3618 | return VisitConstructExpr(E); |
| 3619 | } |
| 3620 | }; |
| 3621 | } // end anonymous namespace |
| 3622 | |
| 3623 | /// Evaluate an expression of record type as a temporary. |
| 3624 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info) { |
Richard Smith | af2c7a1 | 2011-12-19 22:01:37 +0000 | [diff] [blame] | 3625 | assert(E->isRValue() && E->getType()->isRecordType()); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3626 | return TemporaryExprEvaluator(Info, Result).Visit(E); |
| 3627 | } |
| 3628 | |
| 3629 | //===----------------------------------------------------------------------===// |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3630 | // Vector Evaluation |
| 3631 | //===----------------------------------------------------------------------===// |
| 3632 | |
| 3633 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 3634 | class VectorExprEvaluator |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3635 | : public ExprEvaluatorBase<VectorExprEvaluator, bool> { |
| 3636 | APValue &Result; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3637 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3638 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3639 | VectorExprEvaluator(EvalInfo &info, APValue &Result) |
| 3640 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3641 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3642 | bool Success(const ArrayRef<APValue> &V, const Expr *E) { |
| 3643 | assert(V.size() == E->getType()->castAs<VectorType>()->getNumElements()); |
| 3644 | // FIXME: remove this APValue copy. |
| 3645 | Result = APValue(V.data(), V.size()); |
| 3646 | return true; |
| 3647 | } |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3648 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 3649 | assert(V.isVector()); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3650 | Result = V; |
| 3651 | return true; |
| 3652 | } |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3653 | bool ZeroInitialization(const Expr *E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3654 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3655 | bool VisitUnaryReal(const UnaryOperator *E) |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 3656 | { return Visit(E->getSubExpr()); } |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3657 | bool VisitCastExpr(const CastExpr* E); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3658 | bool VisitInitListExpr(const InitListExpr *E); |
| 3659 | bool VisitUnaryImag(const UnaryOperator *E); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 3660 | // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div, |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 3661 | // binary comparisons, binary and/or/xor, |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 3662 | // shufflevector, ExtVectorElementExpr |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3663 | }; |
| 3664 | } // end anonymous namespace |
| 3665 | |
| 3666 | static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3667 | assert(E->isRValue() && E->getType()->isVectorType() &&"not a vector rvalue"); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3668 | return VectorExprEvaluator(Info, Result).Visit(E); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3669 | } |
| 3670 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3671 | bool VectorExprEvaluator::VisitCastExpr(const CastExpr* E) { |
| 3672 | const VectorType *VTy = E->getType()->castAs<VectorType>(); |
Nate Begeman | c0b8b19 | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 3673 | unsigned NElts = VTy->getNumElements(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3674 | |
Richard Smith | d62ca37 | 2011-12-06 22:44:34 +0000 | [diff] [blame] | 3675 | const Expr *SE = E->getSubExpr(); |
Nate Begeman | e8c9e92 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 3676 | QualType SETy = SE->getType(); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3677 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3678 | switch (E->getCastKind()) { |
| 3679 | case CK_VectorSplat: { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3680 | APValue Val = APValue(); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3681 | if (SETy->isIntegerType()) { |
| 3682 | APSInt IntResult; |
| 3683 | if (!EvaluateInteger(SE, IntResult, Info)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3684 | return false; |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3685 | Val = APValue(IntResult); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3686 | } else if (SETy->isRealFloatingType()) { |
| 3687 | APFloat F(0.0); |
| 3688 | if (!EvaluateFloat(SE, F, Info)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3689 | return false; |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3690 | Val = APValue(F); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3691 | } else { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3692 | return Error(E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3693 | } |
Nate Begeman | c0b8b19 | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 3694 | |
| 3695 | // Splat and create vector APValue. |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3696 | SmallVector<APValue, 4> Elts(NElts, Val); |
| 3697 | return Success(Elts, E); |
Nate Begeman | e8c9e92 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 3698 | } |
Eli Friedman | e6a24e8 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 3699 | case CK_BitCast: { |
| 3700 | // Evaluate the operand into an APInt we can extract from. |
| 3701 | llvm::APInt SValInt; |
| 3702 | if (!EvalAndBitcastToAPInt(Info, SE, SValInt)) |
| 3703 | return false; |
| 3704 | // Extract the elements |
| 3705 | QualType EltTy = VTy->getElementType(); |
| 3706 | unsigned EltSize = Info.Ctx.getTypeSize(EltTy); |
| 3707 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
| 3708 | SmallVector<APValue, 4> Elts; |
| 3709 | if (EltTy->isRealFloatingType()) { |
| 3710 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(EltTy); |
| 3711 | bool isIEESem = &Sem != &APFloat::PPCDoubleDouble; |
| 3712 | unsigned FloatEltSize = EltSize; |
| 3713 | if (&Sem == &APFloat::x87DoubleExtended) |
| 3714 | FloatEltSize = 80; |
| 3715 | for (unsigned i = 0; i < NElts; i++) { |
| 3716 | llvm::APInt Elt; |
| 3717 | if (BigEndian) |
| 3718 | Elt = SValInt.rotl(i*EltSize+FloatEltSize).trunc(FloatEltSize); |
| 3719 | else |
| 3720 | Elt = SValInt.rotr(i*EltSize).trunc(FloatEltSize); |
| 3721 | Elts.push_back(APValue(APFloat(Elt, isIEESem))); |
| 3722 | } |
| 3723 | } else if (EltTy->isIntegerType()) { |
| 3724 | for (unsigned i = 0; i < NElts; i++) { |
| 3725 | llvm::APInt Elt; |
| 3726 | if (BigEndian) |
| 3727 | Elt = SValInt.rotl(i*EltSize+EltSize).zextOrTrunc(EltSize); |
| 3728 | else |
| 3729 | Elt = SValInt.rotr(i*EltSize).zextOrTrunc(EltSize); |
| 3730 | Elts.push_back(APValue(APSInt(Elt, EltTy->isSignedIntegerType()))); |
| 3731 | } |
| 3732 | } else { |
| 3733 | return Error(E); |
| 3734 | } |
| 3735 | return Success(Elts, E); |
| 3736 | } |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3737 | default: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3738 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3739 | } |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3740 | } |
| 3741 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3742 | bool |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3743 | VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3744 | const VectorType *VT = E->getType()->castAs<VectorType>(); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3745 | unsigned NumInits = E->getNumInits(); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 3746 | unsigned NumElements = VT->getNumElements(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3747 | |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3748 | QualType EltTy = VT->getElementType(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3749 | SmallVector<APValue, 4> Elements; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3750 | |
Eli Friedman | 3edd5a9 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 3751 | // The number of initializers can be less than the number of |
| 3752 | // vector elements. For OpenCL, this can be due to nested vector |
| 3753 | // initialization. For GCC compatibility, missing trailing elements |
| 3754 | // should be initialized with zeroes. |
| 3755 | unsigned CountInits = 0, CountElts = 0; |
| 3756 | while (CountElts < NumElements) { |
| 3757 | // Handle nested vector initialization. |
| 3758 | if (CountInits < NumInits |
| 3759 | && E->getInit(CountInits)->getType()->isExtVectorType()) { |
| 3760 | APValue v; |
| 3761 | if (!EvaluateVector(E->getInit(CountInits), v, Info)) |
| 3762 | return Error(E); |
| 3763 | unsigned vlen = v.getVectorLength(); |
| 3764 | for (unsigned j = 0; j < vlen; j++) |
| 3765 | Elements.push_back(v.getVectorElt(j)); |
| 3766 | CountElts += vlen; |
| 3767 | } else if (EltTy->isIntegerType()) { |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3768 | llvm::APSInt sInt(32); |
Eli Friedman | 3edd5a9 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 3769 | if (CountInits < NumInits) { |
| 3770 | if (!EvaluateInteger(E->getInit(CountInits), sInt, Info)) |
Richard Smith | 4b1f684 | 2012-03-13 20:58:32 +0000 | [diff] [blame] | 3771 | return false; |
Eli Friedman | 3edd5a9 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 3772 | } else // trailing integer zero. |
| 3773 | sInt = Info.Ctx.MakeIntValue(0, EltTy); |
| 3774 | Elements.push_back(APValue(sInt)); |
| 3775 | CountElts++; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3776 | } else { |
| 3777 | llvm::APFloat f(0.0); |
Eli Friedman | 3edd5a9 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 3778 | if (CountInits < NumInits) { |
| 3779 | if (!EvaluateFloat(E->getInit(CountInits), f, Info)) |
Richard Smith | 4b1f684 | 2012-03-13 20:58:32 +0000 | [diff] [blame] | 3780 | return false; |
Eli Friedman | 3edd5a9 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 3781 | } else // trailing float zero. |
| 3782 | f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)); |
| 3783 | Elements.push_back(APValue(f)); |
| 3784 | CountElts++; |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 3785 | } |
Eli Friedman | 3edd5a9 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 3786 | CountInits++; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3787 | } |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3788 | return Success(Elements, E); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3789 | } |
| 3790 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3791 | bool |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3792 | VectorExprEvaluator::ZeroInitialization(const Expr *E) { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3793 | const VectorType *VT = E->getType()->getAs<VectorType>(); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 3794 | QualType EltTy = VT->getElementType(); |
| 3795 | APValue ZeroElement; |
| 3796 | if (EltTy->isIntegerType()) |
| 3797 | ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy)); |
| 3798 | else |
| 3799 | ZeroElement = |
| 3800 | APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy))); |
| 3801 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3802 | SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3803 | return Success(Elements, E); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 3804 | } |
| 3805 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3806 | bool VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 3807 | VisitIgnoredValue(E->getSubExpr()); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3808 | return ZeroInitialization(E); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 3809 | } |
| 3810 | |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3811 | //===----------------------------------------------------------------------===// |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3812 | // Array Evaluation |
| 3813 | //===----------------------------------------------------------------------===// |
| 3814 | |
| 3815 | namespace { |
| 3816 | class ArrayExprEvaluator |
| 3817 | : public ExprEvaluatorBase<ArrayExprEvaluator, bool> { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3818 | const LValue &This; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3819 | APValue &Result; |
| 3820 | public: |
| 3821 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3822 | ArrayExprEvaluator(EvalInfo &Info, const LValue &This, APValue &Result) |
| 3823 | : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {} |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3824 | |
| 3825 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | f3908f2 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 3826 | assert((V.isArray() || V.isLValue()) && |
| 3827 | "expected array or string literal"); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3828 | Result = V; |
| 3829 | return true; |
| 3830 | } |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3831 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3832 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3833 | const ConstantArrayType *CAT = |
| 3834 | Info.Ctx.getAsConstantArrayType(E->getType()); |
| 3835 | if (!CAT) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3836 | return Error(E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3837 | |
| 3838 | Result = APValue(APValue::UninitArray(), 0, |
| 3839 | CAT->getSize().getZExtValue()); |
| 3840 | if (!Result.hasArrayFiller()) return true; |
| 3841 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3842 | // Zero-initialize all elements. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3843 | LValue Subobject = This; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3844 | Subobject.addArray(Info, E, CAT); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3845 | ImplicitValueInitExpr VIE(CAT->getElementType()); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3846 | return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, &VIE); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3847 | } |
| 3848 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3849 | bool VisitInitListExpr(const InitListExpr *E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3850 | bool VisitCXXConstructExpr(const CXXConstructExpr *E); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3851 | }; |
| 3852 | } // end anonymous namespace |
| 3853 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3854 | static bool EvaluateArray(const Expr *E, const LValue &This, |
| 3855 | APValue &Result, EvalInfo &Info) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3856 | assert(E->isRValue() && E->getType()->isArrayType() && "not an array rvalue"); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3857 | return ArrayExprEvaluator(Info, This, Result).Visit(E); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3858 | } |
| 3859 | |
| 3860 | bool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 3861 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(E->getType()); |
| 3862 | if (!CAT) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3863 | return Error(E); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3864 | |
Richard Smith | 974c5f9 | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 3865 | // C++11 [dcl.init.string]p1: A char array [...] can be initialized by [...] |
| 3866 | // an appropriately-typed string literal enclosed in braces. |
Richard Smith | fe58720 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 3867 | if (E->isStringLiteralInit()) { |
Richard Smith | 974c5f9 | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 3868 | LValue LV; |
| 3869 | if (!EvaluateLValue(E->getInit(0), LV, Info)) |
| 3870 | return false; |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3871 | APValue Val; |
Richard Smith | f3908f2 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 3872 | LV.moveInto(Val); |
| 3873 | return Success(Val, E); |
Richard Smith | 974c5f9 | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 3874 | } |
| 3875 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3876 | bool Success = true; |
| 3877 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3878 | Result = APValue(APValue::UninitArray(), E->getNumInits(), |
| 3879 | CAT->getSize().getZExtValue()); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3880 | LValue Subobject = This; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3881 | Subobject.addArray(Info, E, CAT); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3882 | unsigned Index = 0; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3883 | for (InitListExpr::const_iterator I = E->begin(), End = E->end(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3884 | I != End; ++I, ++Index) { |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3885 | if (!EvaluateInPlace(Result.getArrayInitializedElt(Index), |
| 3886 | Info, Subobject, cast<Expr>(*I)) || |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3887 | !HandleLValueArrayAdjustment(Info, cast<Expr>(*I), Subobject, |
| 3888 | CAT->getElementType(), 1)) { |
| 3889 | if (!Info.keepEvaluatingAfterFailure()) |
| 3890 | return false; |
| 3891 | Success = false; |
| 3892 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3893 | } |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3894 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3895 | if (!Result.hasArrayFiller()) return Success; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3896 | assert(E->hasArrayFiller() && "no array filler for incomplete init list"); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3897 | // FIXME: The Subobject here isn't necessarily right. This rarely matters, |
| 3898 | // but sometimes does: |
| 3899 | // struct S { constexpr S() : p(&p) {} void *p; }; |
| 3900 | // S s[10] = {}; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3901 | return EvaluateInPlace(Result.getArrayFiller(), Info, |
| 3902 | Subobject, E->getArrayFiller()) && Success; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3903 | } |
| 3904 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3905 | bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 3906 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(E->getType()); |
| 3907 | if (!CAT) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3908 | return Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3909 | |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 3910 | bool HadZeroInit = !Result.isUninit(); |
| 3911 | if (!HadZeroInit) |
| 3912 | Result = APValue(APValue::UninitArray(), 0, CAT->getSize().getZExtValue()); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3913 | if (!Result.hasArrayFiller()) |
| 3914 | return true; |
| 3915 | |
| 3916 | const CXXConstructorDecl *FD = E->getConstructor(); |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 3917 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3918 | bool ZeroInit = E->requiresZeroInitialization(); |
| 3919 | if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) { |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 3920 | if (HadZeroInit) |
| 3921 | return true; |
| 3922 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3923 | if (ZeroInit) { |
| 3924 | LValue Subobject = This; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3925 | Subobject.addArray(Info, E, CAT); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3926 | ImplicitValueInitExpr VIE(CAT->getElementType()); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3927 | return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, &VIE); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3928 | } |
| 3929 | |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 3930 | const CXXRecordDecl *RD = FD->getParent(); |
| 3931 | if (RD->isUnion()) |
| 3932 | Result.getArrayFiller() = APValue((FieldDecl*)0); |
| 3933 | else |
| 3934 | Result.getArrayFiller() = |
| 3935 | APValue(APValue::UninitStruct(), RD->getNumBases(), |
| 3936 | std::distance(RD->field_begin(), RD->field_end())); |
| 3937 | return true; |
| 3938 | } |
| 3939 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3940 | const FunctionDecl *Definition = 0; |
| 3941 | FD->getBody(Definition); |
| 3942 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3943 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition)) |
| 3944 | return false; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3945 | |
| 3946 | // FIXME: The Subobject here isn't necessarily right. This rarely matters, |
| 3947 | // but sometimes does: |
| 3948 | // struct S { constexpr S() : p(&p) {} void *p; }; |
| 3949 | // S s[10]; |
| 3950 | LValue Subobject = This; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3951 | Subobject.addArray(Info, E, CAT); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3952 | |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 3953 | if (ZeroInit && !HadZeroInit) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3954 | ImplicitValueInitExpr VIE(CAT->getElementType()); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3955 | if (!EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, &VIE)) |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3956 | return false; |
| 3957 | } |
| 3958 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3959 | llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs()); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3960 | return HandleConstructorCall(E->getExprLoc(), Subobject, Args, |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3961 | cast<CXXConstructorDecl>(Definition), |
| 3962 | Info, Result.getArrayFiller()); |
| 3963 | } |
| 3964 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3965 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3966 | // Integer Evaluation |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3967 | // |
| 3968 | // As a GNU extension, we support casting pointers to sufficiently-wide integer |
| 3969 | // types and back in constant folding. Integer values are thus represented |
| 3970 | // either as an integer-valued APValue, or as an lvalue-valued APValue. |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3971 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3972 | |
| 3973 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 3974 | class IntExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3975 | : public ExprEvaluatorBase<IntExprEvaluator, bool> { |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3976 | APValue &Result; |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 3977 | public: |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3978 | IntExprEvaluator(EvalInfo &info, APValue &result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3979 | : ExprEvaluatorBaseTy(info), Result(result) {} |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3980 | |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 3981 | bool Success(const llvm::APSInt &SI, const Expr *E, APValue &Result) { |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 3982 | assert(E->getType()->isIntegralOrEnumerationType() && |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3983 | "Invalid evaluation result."); |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 3984 | assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 3985 | "Invalid evaluation result."); |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 3986 | assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 3987 | "Invalid evaluation result."); |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3988 | Result = APValue(SI); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 3989 | return true; |
| 3990 | } |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 3991 | bool Success(const llvm::APSInt &SI, const Expr *E) { |
| 3992 | return Success(SI, E, Result); |
| 3993 | } |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 3994 | |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 3995 | bool Success(const llvm::APInt &I, const Expr *E, APValue &Result) { |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3996 | assert(E->getType()->isIntegralOrEnumerationType() && |
| 3997 | "Invalid evaluation result."); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 3998 | assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 3999 | "Invalid evaluation result."); |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4000 | Result = APValue(APSInt(I)); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 4001 | Result.getInt().setIsUnsigned( |
| 4002 | E->getType()->isUnsignedIntegerOrEnumerationType()); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4003 | return true; |
| 4004 | } |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4005 | bool Success(const llvm::APInt &I, const Expr *E) { |
| 4006 | return Success(I, E, Result); |
| 4007 | } |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4008 | |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4009 | bool Success(uint64_t Value, const Expr *E, APValue &Result) { |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 4010 | assert(E->getType()->isIntegralOrEnumerationType() && |
| 4011 | "Invalid evaluation result."); |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4012 | Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType())); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4013 | return true; |
| 4014 | } |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4015 | bool Success(uint64_t Value, const Expr *E) { |
| 4016 | return Success(Value, E, Result); |
| 4017 | } |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4018 | |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 4019 | bool Success(CharUnits Size, const Expr *E) { |
| 4020 | return Success(Size.getQuantity(), E); |
| 4021 | } |
| 4022 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4023 | bool Success(const APValue &V, const Expr *E) { |
Eli Friedman | 5930a4c | 2012-01-05 23:59:40 +0000 | [diff] [blame] | 4024 | if (V.isLValue() || V.isAddrLabelDiff()) { |
Richard Smith | 342f1f8 | 2011-10-29 22:55:55 +0000 | [diff] [blame] | 4025 | Result = V; |
| 4026 | return true; |
| 4027 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4028 | return Success(V.getInt(), E); |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 4029 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4030 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4031 | bool ZeroInitialization(const Expr *E) { return Success(0, E); } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4032 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4033 | //===--------------------------------------------------------------------===// |
| 4034 | // Visitor Methods |
| 4035 | //===--------------------------------------------------------------------===// |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 4036 | |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 4037 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4038 | return Success(E->getValue(), E); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 4039 | } |
| 4040 | bool VisitCharacterLiteral(const CharacterLiteral *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4041 | return Success(E->getValue(), E); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 4042 | } |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 4043 | |
| 4044 | bool CheckReferencedDecl(const Expr *E, const Decl *D); |
| 4045 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4046 | if (CheckReferencedDecl(E, E->getDecl())) |
| 4047 | return true; |
| 4048 | |
| 4049 | return ExprEvaluatorBaseTy::VisitDeclRefExpr(E); |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 4050 | } |
| 4051 | bool VisitMemberExpr(const MemberExpr *E) { |
| 4052 | if (CheckReferencedDecl(E, E->getMemberDecl())) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4053 | VisitIgnoredValue(E->getBase()); |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 4054 | return true; |
| 4055 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4056 | |
| 4057 | return ExprEvaluatorBaseTy::VisitMemberExpr(E); |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 4058 | } |
| 4059 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4060 | bool VisitCallExpr(const CallExpr *E); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 4061 | bool VisitBinaryOperator(const BinaryOperator *E); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 4062 | bool VisitOffsetOfExpr(const OffsetOfExpr *E); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 4063 | bool VisitUnaryOperator(const UnaryOperator *E); |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 4064 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4065 | bool VisitCastExpr(const CastExpr* E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 4066 | bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E); |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 4067 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4068 | bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4069 | return Success(E->getValue(), E); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4070 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4071 | |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 4072 | bool VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) { |
| 4073 | return Success(E->getValue(), E); |
| 4074 | } |
| 4075 | |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4076 | // Note, GNU defines __null as an integer, not a pointer. |
Anders Carlsson | 3f70456 | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 4077 | bool VisitGNUNullExpr(const GNUNullExpr *E) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4078 | return ZeroInitialization(E); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 4079 | } |
| 4080 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 4081 | bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) { |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4082 | return Success(E->getValue(), E); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 4083 | } |
| 4084 | |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 4085 | bool VisitBinaryTypeTraitExpr(const BinaryTypeTraitExpr *E) { |
| 4086 | return Success(E->getValue(), E); |
| 4087 | } |
| 4088 | |
Douglas Gregor | 4ca8ac2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4089 | bool VisitTypeTraitExpr(const TypeTraitExpr *E) { |
| 4090 | return Success(E->getValue(), E); |
| 4091 | } |
| 4092 | |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 4093 | bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) { |
| 4094 | return Success(E->getValue(), E); |
| 4095 | } |
| 4096 | |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 4097 | bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) { |
| 4098 | return Success(E->getValue(), E); |
| 4099 | } |
| 4100 | |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 4101 | bool VisitUnaryReal(const UnaryOperator *E); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 4102 | bool VisitUnaryImag(const UnaryOperator *E); |
| 4103 | |
Sebastian Redl | 295995c | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 4104 | bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E); |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 4105 | bool VisitSizeOfPackExpr(const SizeOfPackExpr *E); |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 4106 | |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 4107 | private: |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 4108 | CharUnits GetAlignOfExpr(const Expr *E); |
| 4109 | CharUnits GetAlignOfType(QualType T); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4110 | static QualType GetObjectType(APValue::LValueBase B); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4111 | bool TryEvaluateBuiltinObjectSize(const CallExpr *E); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 4112 | // FIXME: Missing: array subscript of vector, member of vector |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 4113 | }; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4114 | } // end anonymous namespace |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 4115 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4116 | /// EvaluateIntegerOrLValue - Evaluate an rvalue integral-typed expression, and |
| 4117 | /// produce either the integer value or a pointer. |
| 4118 | /// |
| 4119 | /// GCC has a heinous extension which folds casts between pointer types and |
| 4120 | /// pointer-sized integral types. We support this by allowing the evaluation of |
| 4121 | /// an integer rvalue to produce a pointer (represented as an lvalue) instead. |
| 4122 | /// Some simple arithmetic on such values is supported (they are treated much |
| 4123 | /// like char*). |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4124 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4125 | EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4126 | assert(E->isRValue() && E->getType()->isIntegralOrEnumerationType()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4127 | return IntExprEvaluator(Info, Result).Visit(E); |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 4128 | } |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 4129 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4130 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info) { |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4131 | APValue Val; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4132 | if (!EvaluateIntegerOrLValue(E, Val, Info)) |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 4133 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4134 | if (!Val.isInt()) { |
| 4135 | // FIXME: It would be better to produce the diagnostic for casting |
| 4136 | // a pointer to an integer. |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 4137 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4138 | return false; |
| 4139 | } |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 4140 | Result = Val.getInt(); |
| 4141 | return true; |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 4142 | } |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 4143 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4144 | /// Check whether the given declaration can be directly converted to an integral |
| 4145 | /// rvalue. If not, no diagnostic is produced; there are other things we can |
| 4146 | /// try. |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 4147 | bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) { |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 4148 | // Enums are integer constant exprs. |
Abramo Bagnara | bfbdcd8 | 2011-06-30 09:36:05 +0000 | [diff] [blame] | 4149 | if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) { |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 4150 | // Check for signedness/width mismatches between E type and ECD value. |
| 4151 | bool SameSign = (ECD->getInitVal().isSigned() |
| 4152 | == E->getType()->isSignedIntegerOrEnumerationType()); |
| 4153 | bool SameWidth = (ECD->getInitVal().getBitWidth() |
| 4154 | == Info.Ctx.getIntWidth(E->getType())); |
| 4155 | if (SameSign && SameWidth) |
| 4156 | return Success(ECD->getInitVal(), E); |
| 4157 | else { |
| 4158 | // Get rid of mismatch (otherwise Success assertions will fail) |
| 4159 | // by computing a new value matching the type of E. |
| 4160 | llvm::APSInt Val = ECD->getInitVal(); |
| 4161 | if (!SameSign) |
| 4162 | Val.setIsSigned(!ECD->getInitVal().isSigned()); |
| 4163 | if (!SameWidth) |
| 4164 | Val = Val.extOrTrunc(Info.Ctx.getIntWidth(E->getType())); |
| 4165 | return Success(Val, E); |
| 4166 | } |
Abramo Bagnara | bfbdcd8 | 2011-06-30 09:36:05 +0000 | [diff] [blame] | 4167 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4168 | return false; |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 4169 | } |
| 4170 | |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 4171 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
| 4172 | /// as GCC. |
| 4173 | static int EvaluateBuiltinClassifyType(const CallExpr *E) { |
| 4174 | // The following enum mimics the values returned by GCC. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4175 | // FIXME: Does GCC differ between lvalue and rvalue references here? |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 4176 | enum gcc_type_class { |
| 4177 | no_type_class = -1, |
| 4178 | void_type_class, integer_type_class, char_type_class, |
| 4179 | enumeral_type_class, boolean_type_class, |
| 4180 | pointer_type_class, reference_type_class, offset_type_class, |
| 4181 | real_type_class, complex_type_class, |
| 4182 | function_type_class, method_type_class, |
| 4183 | record_type_class, union_type_class, |
| 4184 | array_type_class, string_type_class, |
| 4185 | lang_type_class |
| 4186 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4187 | |
| 4188 | // If no argument was supplied, default to "no_type_class". This isn't |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 4189 | // ideal, however it is what gcc does. |
| 4190 | if (E->getNumArgs() == 0) |
| 4191 | return no_type_class; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4192 | |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 4193 | QualType ArgTy = E->getArg(0)->getType(); |
| 4194 | if (ArgTy->isVoidType()) |
| 4195 | return void_type_class; |
| 4196 | else if (ArgTy->isEnumeralType()) |
| 4197 | return enumeral_type_class; |
| 4198 | else if (ArgTy->isBooleanType()) |
| 4199 | return boolean_type_class; |
| 4200 | else if (ArgTy->isCharType()) |
| 4201 | return string_type_class; // gcc doesn't appear to use char_type_class |
| 4202 | else if (ArgTy->isIntegerType()) |
| 4203 | return integer_type_class; |
| 4204 | else if (ArgTy->isPointerType()) |
| 4205 | return pointer_type_class; |
| 4206 | else if (ArgTy->isReferenceType()) |
| 4207 | return reference_type_class; |
| 4208 | else if (ArgTy->isRealType()) |
| 4209 | return real_type_class; |
| 4210 | else if (ArgTy->isComplexType()) |
| 4211 | return complex_type_class; |
| 4212 | else if (ArgTy->isFunctionType()) |
| 4213 | return function_type_class; |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 4214 | else if (ArgTy->isStructureOrClassType()) |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 4215 | return record_type_class; |
| 4216 | else if (ArgTy->isUnionType()) |
| 4217 | return union_type_class; |
| 4218 | else if (ArgTy->isArrayType()) |
| 4219 | return array_type_class; |
| 4220 | else if (ArgTy->isUnionType()) |
| 4221 | return union_type_class; |
| 4222 | else // FIXME: offset_type_class, method_type_class, & lang_type_class? |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4223 | llvm_unreachable("CallExpr::isBuiltinClassifyType(): unimplemented type"); |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 4224 | } |
| 4225 | |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 4226 | /// EvaluateBuiltinConstantPForLValue - Determine the result of |
| 4227 | /// __builtin_constant_p when applied to the given lvalue. |
| 4228 | /// |
| 4229 | /// An lvalue is only "constant" if it is a pointer or reference to the first |
| 4230 | /// character of a string literal. |
| 4231 | template<typename LValue> |
| 4232 | static bool EvaluateBuiltinConstantPForLValue(const LValue &LV) { |
Douglas Gregor | 8e55ed1 | 2012-03-11 02:23:56 +0000 | [diff] [blame] | 4233 | const Expr *E = LV.getLValueBase().template dyn_cast<const Expr*>(); |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 4234 | return E && isa<StringLiteral>(E) && LV.getLValueOffset().isZero(); |
| 4235 | } |
| 4236 | |
| 4237 | /// EvaluateBuiltinConstantP - Evaluate __builtin_constant_p as similarly to |
| 4238 | /// GCC as we can manage. |
| 4239 | static bool EvaluateBuiltinConstantP(ASTContext &Ctx, const Expr *Arg) { |
| 4240 | QualType ArgType = Arg->getType(); |
| 4241 | |
| 4242 | // __builtin_constant_p always has one operand. The rules which gcc follows |
| 4243 | // are not precisely documented, but are as follows: |
| 4244 | // |
| 4245 | // - If the operand is of integral, floating, complex or enumeration type, |
| 4246 | // and can be folded to a known value of that type, it returns 1. |
| 4247 | // - If the operand and can be folded to a pointer to the first character |
| 4248 | // of a string literal (or such a pointer cast to an integral type), it |
| 4249 | // returns 1. |
| 4250 | // |
| 4251 | // Otherwise, it returns 0. |
| 4252 | // |
| 4253 | // FIXME: GCC also intends to return 1 for literals of aggregate types, but |
| 4254 | // its support for this does not currently work. |
| 4255 | if (ArgType->isIntegralOrEnumerationType()) { |
| 4256 | Expr::EvalResult Result; |
| 4257 | if (!Arg->EvaluateAsRValue(Result, Ctx) || Result.HasSideEffects) |
| 4258 | return false; |
| 4259 | |
| 4260 | APValue &V = Result.Val; |
| 4261 | if (V.getKind() == APValue::Int) |
| 4262 | return true; |
| 4263 | |
| 4264 | return EvaluateBuiltinConstantPForLValue(V); |
| 4265 | } else if (ArgType->isFloatingType() || ArgType->isAnyComplexType()) { |
| 4266 | return Arg->isEvaluatable(Ctx); |
| 4267 | } else if (ArgType->isPointerType() || Arg->isGLValue()) { |
| 4268 | LValue LV; |
| 4269 | Expr::EvalStatus Status; |
| 4270 | EvalInfo Info(Ctx, Status); |
| 4271 | if ((Arg->isGLValue() ? EvaluateLValue(Arg, LV, Info) |
| 4272 | : EvaluatePointer(Arg, LV, Info)) && |
| 4273 | !Status.HasSideEffects) |
| 4274 | return EvaluateBuiltinConstantPForLValue(LV); |
| 4275 | } |
| 4276 | |
| 4277 | // Anything else isn't considered to be sufficiently constant. |
| 4278 | return false; |
| 4279 | } |
| 4280 | |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4281 | /// Retrieves the "underlying object type" of the given expression, |
| 4282 | /// as used by __builtin_object_size. |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4283 | QualType IntExprEvaluator::GetObjectType(APValue::LValueBase B) { |
| 4284 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
| 4285 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4286 | return VD->getType(); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4287 | } else if (const Expr *E = B.get<const Expr*>()) { |
| 4288 | if (isa<CompoundLiteralExpr>(E)) |
| 4289 | return E->getType(); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4290 | } |
| 4291 | |
| 4292 | return QualType(); |
| 4293 | } |
| 4294 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4295 | bool IntExprEvaluator::TryEvaluateBuiltinObjectSize(const CallExpr *E) { |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4296 | // TODO: Perhaps we should let LLVM lower this? |
| 4297 | LValue Base; |
| 4298 | if (!EvaluatePointer(E->getArg(0), Base, Info)) |
| 4299 | return false; |
| 4300 | |
| 4301 | // If we can prove the base is null, lower to zero now. |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4302 | if (!Base.getLValueBase()) return Success(0, E); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4303 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4304 | QualType T = GetObjectType(Base.getLValueBase()); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4305 | if (T.isNull() || |
| 4306 | T->isIncompleteType() || |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 4307 | T->isFunctionType() || |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4308 | T->isVariablyModifiedType() || |
| 4309 | T->isDependentType()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4310 | return Error(E); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4311 | |
| 4312 | CharUnits Size = Info.Ctx.getTypeSizeInChars(T); |
| 4313 | CharUnits Offset = Base.getLValueOffset(); |
| 4314 | |
| 4315 | if (!Offset.isNegative() && Offset <= Size) |
| 4316 | Size -= Offset; |
| 4317 | else |
| 4318 | Size = CharUnits::Zero(); |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 4319 | return Success(Size, E); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4320 | } |
| 4321 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4322 | bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | 2c39d71 | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 4323 | switch (unsigned BuiltinOp = E->isBuiltinCall()) { |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 4324 | default: |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4325 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 4326 | |
| 4327 | case Builtin::BI__builtin_object_size: { |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4328 | if (TryEvaluateBuiltinObjectSize(E)) |
| 4329 | return true; |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 4330 | |
Eric Christopher | b2aaf51 | 2010-01-19 22:58:35 +0000 | [diff] [blame] | 4331 | // If evaluating the argument has side-effects we can't determine |
| 4332 | // the size of the object and lower it to unknown now. |
Fariborz Jahanian | 393c247 | 2009-11-05 18:03:03 +0000 | [diff] [blame] | 4333 | if (E->getArg(0)->HasSideEffects(Info.Ctx)) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4334 | if (E->getArg(1)->EvaluateKnownConstInt(Info.Ctx).getZExtValue() <= 1) |
Chris Lattner | cf18465 | 2009-11-03 19:48:51 +0000 | [diff] [blame] | 4335 | return Success(-1ULL, E); |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 4336 | return Success(0, E); |
| 4337 | } |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 4338 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4339 | return Error(E); |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 4340 | } |
| 4341 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 4342 | case Builtin::BI__builtin_classify_type: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4343 | return Success(EvaluateBuiltinClassifyType(E), E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4344 | |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 4345 | case Builtin::BI__builtin_constant_p: |
| 4346 | return Success(EvaluateBuiltinConstantP(Info.Ctx, E->getArg(0)), E); |
Richard Smith | e052d46 | 2011-12-09 02:04:48 +0000 | [diff] [blame] | 4347 | |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 4348 | case Builtin::BI__builtin_eh_return_data_regno: { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4349 | int Operand = E->getArg(0)->EvaluateKnownConstInt(Info.Ctx).getZExtValue(); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4350 | Operand = Info.Ctx.getTargetInfo().getEHDataRegisterNumber(Operand); |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 4351 | return Success(Operand, E); |
| 4352 | } |
Eli Friedman | c4a2638 | 2010-02-13 00:10:10 +0000 | [diff] [blame] | 4353 | |
| 4354 | case Builtin::BI__builtin_expect: |
| 4355 | return Visit(E->getArg(0)); |
Richard Smith | 40b993a | 2012-01-18 03:06:12 +0000 | [diff] [blame] | 4356 | |
Douglas Gregor | 5726d40 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 4357 | case Builtin::BIstrlen: |
Richard Smith | 40b993a | 2012-01-18 03:06:12 +0000 | [diff] [blame] | 4358 | // A call to strlen is not a constant expression. |
| 4359 | if (Info.getLangOpts().CPlusPlus0x) |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 4360 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
Richard Smith | 40b993a | 2012-01-18 03:06:12 +0000 | [diff] [blame] | 4361 | << /*isConstexpr*/0 << /*isConstructor*/0 << "'strlen'"; |
| 4362 | else |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 4363 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 40b993a | 2012-01-18 03:06:12 +0000 | [diff] [blame] | 4364 | // Fall through. |
Douglas Gregor | 5726d40 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 4365 | case Builtin::BI__builtin_strlen: |
| 4366 | // As an extension, we support strlen() and __builtin_strlen() as constant |
| 4367 | // expressions when the argument is a string literal. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4368 | if (const StringLiteral *S |
Douglas Gregor | 5726d40 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 4369 | = dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenImpCasts())) { |
| 4370 | // The string literal may have embedded null characters. Find the first |
| 4371 | // one and truncate there. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4372 | StringRef Str = S->getString(); |
| 4373 | StringRef::size_type Pos = Str.find(0); |
| 4374 | if (Pos != StringRef::npos) |
Douglas Gregor | 5726d40 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 4375 | Str = Str.substr(0, Pos); |
| 4376 | |
| 4377 | return Success(Str.size(), E); |
| 4378 | } |
| 4379 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4380 | return Error(E); |
Eli Friedman | 454b57a | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 4381 | |
Richard Smith | 2c39d71 | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 4382 | case Builtin::BI__atomic_always_lock_free: |
Richard Smith | fafbf06 | 2012-04-11 17:55:32 +0000 | [diff] [blame] | 4383 | case Builtin::BI__atomic_is_lock_free: |
| 4384 | case Builtin::BI__c11_atomic_is_lock_free: { |
Eli Friedman | 454b57a | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 4385 | APSInt SizeVal; |
| 4386 | if (!EvaluateInteger(E->getArg(0), SizeVal, Info)) |
| 4387 | return false; |
| 4388 | |
| 4389 | // For __atomic_is_lock_free(sizeof(_Atomic(T))), if the size is a power |
| 4390 | // of two less than the maximum inline atomic width, we know it is |
| 4391 | // lock-free. If the size isn't a power of two, or greater than the |
| 4392 | // maximum alignment where we promote atomics, we know it is not lock-free |
| 4393 | // (at least not in the sense of atomic_is_lock_free). Otherwise, |
| 4394 | // the answer can only be determined at runtime; for example, 16-byte |
| 4395 | // atomics have lock-free implementations on some, but not all, |
| 4396 | // x86-64 processors. |
| 4397 | |
| 4398 | // Check power-of-two. |
| 4399 | CharUnits Size = CharUnits::fromQuantity(SizeVal.getZExtValue()); |
Richard Smith | 2c39d71 | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 4400 | if (Size.isPowerOfTwo()) { |
| 4401 | // Check against inlining width. |
| 4402 | unsigned InlineWidthBits = |
| 4403 | Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth(); |
| 4404 | if (Size <= Info.Ctx.toCharUnitsFromBits(InlineWidthBits)) { |
| 4405 | if (BuiltinOp == Builtin::BI__c11_atomic_is_lock_free || |
| 4406 | Size == CharUnits::One() || |
| 4407 | E->getArg(1)->isNullPointerConstant(Info.Ctx, |
| 4408 | Expr::NPC_NeverValueDependent)) |
| 4409 | // OK, we will inline appropriately-aligned operations of this size, |
| 4410 | // and _Atomic(T) is appropriately-aligned. |
| 4411 | return Success(1, E); |
Eli Friedman | 454b57a | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 4412 | |
Richard Smith | 2c39d71 | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 4413 | QualType PointeeType = E->getArg(1)->IgnoreImpCasts()->getType()-> |
| 4414 | castAs<PointerType>()->getPointeeType(); |
| 4415 | if (!PointeeType->isIncompleteType() && |
| 4416 | Info.Ctx.getTypeAlignInChars(PointeeType) >= Size) { |
| 4417 | // OK, we will inline operations on this object. |
| 4418 | return Success(1, E); |
| 4419 | } |
| 4420 | } |
| 4421 | } |
Eli Friedman | 454b57a | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 4422 | |
Richard Smith | 2c39d71 | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 4423 | return BuiltinOp == Builtin::BI__atomic_always_lock_free ? |
| 4424 | Success(0, E) : Error(E); |
Eli Friedman | 454b57a | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 4425 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 4426 | } |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 4427 | } |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 4428 | |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 4429 | static bool HasSameBase(const LValue &A, const LValue &B) { |
| 4430 | if (!A.getLValueBase()) |
| 4431 | return !B.getLValueBase(); |
| 4432 | if (!B.getLValueBase()) |
| 4433 | return false; |
| 4434 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4435 | if (A.getLValueBase().getOpaqueValue() != |
| 4436 | B.getLValueBase().getOpaqueValue()) { |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 4437 | const Decl *ADecl = GetLValueBaseDecl(A); |
| 4438 | if (!ADecl) |
| 4439 | return false; |
| 4440 | const Decl *BDecl = GetLValueBaseDecl(B); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 4441 | if (!BDecl || ADecl->getCanonicalDecl() != BDecl->getCanonicalDecl()) |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 4442 | return false; |
| 4443 | } |
| 4444 | |
| 4445 | return IsGlobalLValue(A.getLValueBase()) || |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 4446 | A.getLValueCallIndex() == B.getLValueCallIndex(); |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 4447 | } |
| 4448 | |
Richard Smith | 7b48a29 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 4449 | /// Perform the given integer operation, which is known to need at most BitWidth |
| 4450 | /// bits, and check for overflow in the original type (if that type was not an |
| 4451 | /// unsigned type). |
| 4452 | template<typename Operation> |
| 4453 | static APSInt CheckedIntArithmetic(EvalInfo &Info, const Expr *E, |
| 4454 | const APSInt &LHS, const APSInt &RHS, |
| 4455 | unsigned BitWidth, Operation Op) { |
| 4456 | if (LHS.isUnsigned()) |
| 4457 | return Op(LHS, RHS); |
| 4458 | |
| 4459 | APSInt Value(Op(LHS.extend(BitWidth), RHS.extend(BitWidth)), false); |
| 4460 | APSInt Result = Value.trunc(LHS.getBitWidth()); |
| 4461 | if (Result.extend(BitWidth) != Value) |
| 4462 | HandleOverflow(Info, E, Value, E->getType()); |
| 4463 | return Result; |
| 4464 | } |
| 4465 | |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4466 | namespace { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4467 | |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4468 | /// \brief Data recursive integer evaluator of certain binary operators. |
| 4469 | /// |
| 4470 | /// We use a data recursive algorithm for binary operators so that we are able |
| 4471 | /// to handle extreme cases of chained binary operators without causing stack |
| 4472 | /// overflow. |
| 4473 | class DataRecursiveIntBinOpEvaluator { |
| 4474 | struct EvalResult { |
| 4475 | APValue Val; |
| 4476 | bool Failed; |
| 4477 | |
| 4478 | EvalResult() : Failed(false) { } |
| 4479 | |
| 4480 | void swap(EvalResult &RHS) { |
| 4481 | Val.swap(RHS.Val); |
| 4482 | Failed = RHS.Failed; |
| 4483 | RHS.Failed = false; |
| 4484 | } |
| 4485 | }; |
| 4486 | |
| 4487 | struct Job { |
| 4488 | const Expr *E; |
| 4489 | EvalResult LHSResult; // meaningful only for binary operator expression. |
| 4490 | enum { AnyExprKind, BinOpKind, BinOpVisitedLHSKind } Kind; |
| 4491 | |
| 4492 | Job() : StoredInfo(0) { } |
| 4493 | void startSpeculativeEval(EvalInfo &Info) { |
| 4494 | OldEvalStatus = Info.EvalStatus; |
| 4495 | Info.EvalStatus.Diag = 0; |
| 4496 | StoredInfo = &Info; |
| 4497 | } |
| 4498 | ~Job() { |
| 4499 | if (StoredInfo) { |
| 4500 | StoredInfo->EvalStatus = OldEvalStatus; |
| 4501 | } |
| 4502 | } |
| 4503 | private: |
| 4504 | EvalInfo *StoredInfo; // non-null if status changed. |
| 4505 | Expr::EvalStatus OldEvalStatus; |
| 4506 | }; |
| 4507 | |
| 4508 | SmallVector<Job, 16> Queue; |
| 4509 | |
| 4510 | IntExprEvaluator &IntEval; |
| 4511 | EvalInfo &Info; |
| 4512 | APValue &FinalResult; |
| 4513 | |
| 4514 | public: |
| 4515 | DataRecursiveIntBinOpEvaluator(IntExprEvaluator &IntEval, APValue &Result) |
| 4516 | : IntEval(IntEval), Info(IntEval.getEvalInfo()), FinalResult(Result) { } |
| 4517 | |
| 4518 | /// \brief True if \param E is a binary operator that we are going to handle |
| 4519 | /// data recursively. |
| 4520 | /// We handle binary operators that are comma, logical, or that have operands |
| 4521 | /// with integral or enumeration type. |
| 4522 | static bool shouldEnqueue(const BinaryOperator *E) { |
| 4523 | return E->getOpcode() == BO_Comma || |
| 4524 | E->isLogicalOp() || |
| 4525 | (E->getLHS()->getType()->isIntegralOrEnumerationType() && |
| 4526 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 4527 | } |
| 4528 | |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4529 | bool Traverse(const BinaryOperator *E) { |
| 4530 | enqueue(E); |
| 4531 | EvalResult PrevResult; |
Richard Trieu | b778305 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 4532 | while (!Queue.empty()) |
| 4533 | process(PrevResult); |
| 4534 | |
| 4535 | if (PrevResult.Failed) return false; |
Argyrios Kyrtzidis | 2fa975c | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 4536 | |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4537 | FinalResult.swap(PrevResult.Val); |
| 4538 | return true; |
| 4539 | } |
| 4540 | |
| 4541 | private: |
| 4542 | bool Success(uint64_t Value, const Expr *E, APValue &Result) { |
| 4543 | return IntEval.Success(Value, E, Result); |
| 4544 | } |
| 4545 | bool Success(const APSInt &Value, const Expr *E, APValue &Result) { |
| 4546 | return IntEval.Success(Value, E, Result); |
| 4547 | } |
| 4548 | bool Error(const Expr *E) { |
| 4549 | return IntEval.Error(E); |
| 4550 | } |
| 4551 | bool Error(const Expr *E, diag::kind D) { |
| 4552 | return IntEval.Error(E, D); |
| 4553 | } |
| 4554 | |
| 4555 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { |
| 4556 | return Info.CCEDiag(E, D); |
| 4557 | } |
| 4558 | |
Argyrios Kyrtzidis | 9293fff | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 4559 | // \brief Returns true if visiting the RHS is necessary, false otherwise. |
| 4560 | bool VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E, |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4561 | bool &SuppressRHSDiags); |
| 4562 | |
| 4563 | bool VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult, |
| 4564 | const BinaryOperator *E, APValue &Result); |
| 4565 | |
| 4566 | void EvaluateExpr(const Expr *E, EvalResult &Result) { |
| 4567 | Result.Failed = !Evaluate(Result.Val, Info, E); |
| 4568 | if (Result.Failed) |
| 4569 | Result.Val = APValue(); |
| 4570 | } |
| 4571 | |
Richard Trieu | b778305 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 4572 | void process(EvalResult &Result); |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4573 | |
| 4574 | void enqueue(const Expr *E) { |
| 4575 | E = E->IgnoreParens(); |
| 4576 | Queue.resize(Queue.size()+1); |
| 4577 | Queue.back().E = E; |
| 4578 | Queue.back().Kind = Job::AnyExprKind; |
| 4579 | } |
| 4580 | }; |
| 4581 | |
| 4582 | } |
| 4583 | |
| 4584 | bool DataRecursiveIntBinOpEvaluator:: |
Argyrios Kyrtzidis | 9293fff | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 4585 | VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E, |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4586 | bool &SuppressRHSDiags) { |
| 4587 | if (E->getOpcode() == BO_Comma) { |
| 4588 | // Ignore LHS but note if we could not evaluate it. |
| 4589 | if (LHSResult.Failed) |
| 4590 | Info.EvalStatus.HasSideEffects = true; |
| 4591 | return true; |
| 4592 | } |
| 4593 | |
| 4594 | if (E->isLogicalOp()) { |
| 4595 | bool lhsResult; |
| 4596 | if (HandleConversionToBool(LHSResult.Val, lhsResult)) { |
Argyrios Kyrtzidis | 2fa975c | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 4597 | // We were able to evaluate the LHS, see if we can get away with not |
| 4598 | // evaluating the RHS: 0 && X -> 0, 1 || X -> 1 |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4599 | if (lhsResult == (E->getOpcode() == BO_LOr)) { |
Argyrios Kyrtzidis | 9293fff | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 4600 | Success(lhsResult, E, LHSResult.Val); |
| 4601 | return false; // Ignore RHS |
Argyrios Kyrtzidis | 2fa975c | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 4602 | } |
| 4603 | } else { |
| 4604 | // Since we weren't able to evaluate the left hand side, it |
| 4605 | // must have had side effects. |
| 4606 | Info.EvalStatus.HasSideEffects = true; |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4607 | |
| 4608 | // We can't evaluate the LHS; however, sometimes the result |
| 4609 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
| 4610 | // Don't ignore RHS and suppress diagnostics from this arm. |
| 4611 | SuppressRHSDiags = true; |
| 4612 | } |
| 4613 | |
| 4614 | return true; |
| 4615 | } |
| 4616 | |
| 4617 | assert(E->getLHS()->getType()->isIntegralOrEnumerationType() && |
| 4618 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
| 4619 | |
| 4620 | if (LHSResult.Failed && !Info.keepEvaluatingAfterFailure()) |
Argyrios Kyrtzidis | 9293fff | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 4621 | return false; // Ignore RHS; |
| 4622 | |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4623 | return true; |
| 4624 | } |
Argyrios Kyrtzidis | 2fa975c | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 4625 | |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4626 | bool DataRecursiveIntBinOpEvaluator:: |
| 4627 | VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult, |
| 4628 | const BinaryOperator *E, APValue &Result) { |
| 4629 | if (E->getOpcode() == BO_Comma) { |
| 4630 | if (RHSResult.Failed) |
| 4631 | return false; |
| 4632 | Result = RHSResult.Val; |
| 4633 | return true; |
| 4634 | } |
| 4635 | |
| 4636 | if (E->isLogicalOp()) { |
| 4637 | bool lhsResult, rhsResult; |
| 4638 | bool LHSIsOK = HandleConversionToBool(LHSResult.Val, lhsResult); |
| 4639 | bool RHSIsOK = HandleConversionToBool(RHSResult.Val, rhsResult); |
| 4640 | |
| 4641 | if (LHSIsOK) { |
| 4642 | if (RHSIsOK) { |
| 4643 | if (E->getOpcode() == BO_LOr) |
| 4644 | return Success(lhsResult || rhsResult, E, Result); |
| 4645 | else |
| 4646 | return Success(lhsResult && rhsResult, E, Result); |
| 4647 | } |
| 4648 | } else { |
| 4649 | if (RHSIsOK) { |
Argyrios Kyrtzidis | 2fa975c | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 4650 | // We can't evaluate the LHS; however, sometimes the result |
| 4651 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
| 4652 | if (rhsResult == (E->getOpcode() == BO_LOr)) |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4653 | return Success(rhsResult, E, Result); |
Argyrios Kyrtzidis | 2fa975c | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 4654 | } |
| 4655 | } |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4656 | |
Argyrios Kyrtzidis | 2fa975c | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 4657 | return false; |
| 4658 | } |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4659 | |
| 4660 | assert(E->getLHS()->getType()->isIntegralOrEnumerationType() && |
| 4661 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
| 4662 | |
| 4663 | if (LHSResult.Failed || RHSResult.Failed) |
| 4664 | return false; |
| 4665 | |
| 4666 | const APValue &LHSVal = LHSResult.Val; |
| 4667 | const APValue &RHSVal = RHSResult.Val; |
| 4668 | |
| 4669 | // Handle cases like (unsigned long)&a + 4. |
| 4670 | if (E->isAdditiveOp() && LHSVal.isLValue() && RHSVal.isInt()) { |
| 4671 | Result = LHSVal; |
| 4672 | CharUnits AdditionalOffset = CharUnits::fromQuantity( |
| 4673 | RHSVal.getInt().getZExtValue()); |
| 4674 | if (E->getOpcode() == BO_Add) |
| 4675 | Result.getLValueOffset() += AdditionalOffset; |
| 4676 | else |
| 4677 | Result.getLValueOffset() -= AdditionalOffset; |
| 4678 | return true; |
| 4679 | } |
| 4680 | |
| 4681 | // Handle cases like 4 + (unsigned long)&a |
| 4682 | if (E->getOpcode() == BO_Add && |
| 4683 | RHSVal.isLValue() && LHSVal.isInt()) { |
| 4684 | Result = RHSVal; |
| 4685 | Result.getLValueOffset() += CharUnits::fromQuantity( |
| 4686 | LHSVal.getInt().getZExtValue()); |
| 4687 | return true; |
| 4688 | } |
| 4689 | |
| 4690 | if (E->getOpcode() == BO_Sub && LHSVal.isLValue() && RHSVal.isLValue()) { |
| 4691 | // Handle (intptr_t)&&A - (intptr_t)&&B. |
| 4692 | if (!LHSVal.getLValueOffset().isZero() || |
| 4693 | !RHSVal.getLValueOffset().isZero()) |
| 4694 | return false; |
| 4695 | const Expr *LHSExpr = LHSVal.getLValueBase().dyn_cast<const Expr*>(); |
| 4696 | const Expr *RHSExpr = RHSVal.getLValueBase().dyn_cast<const Expr*>(); |
| 4697 | if (!LHSExpr || !RHSExpr) |
| 4698 | return false; |
| 4699 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); |
| 4700 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); |
| 4701 | if (!LHSAddrExpr || !RHSAddrExpr) |
| 4702 | return false; |
| 4703 | // Make sure both labels come from the same function. |
| 4704 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
| 4705 | RHSAddrExpr->getLabel()->getDeclContext()) |
| 4706 | return false; |
| 4707 | Result = APValue(LHSAddrExpr, RHSAddrExpr); |
| 4708 | return true; |
| 4709 | } |
| 4710 | |
| 4711 | // All the following cases expect both operands to be an integer |
| 4712 | if (!LHSVal.isInt() || !RHSVal.isInt()) |
| 4713 | return Error(E); |
| 4714 | |
| 4715 | const APSInt &LHS = LHSVal.getInt(); |
| 4716 | APSInt RHS = RHSVal.getInt(); |
| 4717 | |
| 4718 | switch (E->getOpcode()) { |
| 4719 | default: |
| 4720 | return Error(E); |
| 4721 | case BO_Mul: |
| 4722 | return Success(CheckedIntArithmetic(Info, E, LHS, RHS, |
| 4723 | LHS.getBitWidth() * 2, |
| 4724 | std::multiplies<APSInt>()), E, |
| 4725 | Result); |
| 4726 | case BO_Add: |
| 4727 | return Success(CheckedIntArithmetic(Info, E, LHS, RHS, |
| 4728 | LHS.getBitWidth() + 1, |
| 4729 | std::plus<APSInt>()), E, Result); |
| 4730 | case BO_Sub: |
| 4731 | return Success(CheckedIntArithmetic(Info, E, LHS, RHS, |
| 4732 | LHS.getBitWidth() + 1, |
| 4733 | std::minus<APSInt>()), E, Result); |
| 4734 | case BO_And: return Success(LHS & RHS, E, Result); |
| 4735 | case BO_Xor: return Success(LHS ^ RHS, E, Result); |
| 4736 | case BO_Or: return Success(LHS | RHS, E, Result); |
| 4737 | case BO_Div: |
| 4738 | case BO_Rem: |
| 4739 | if (RHS == 0) |
| 4740 | return Error(E, diag::note_expr_divide_by_zero); |
| 4741 | // Check for overflow case: INT_MIN / -1 or INT_MIN % -1. The latter is |
| 4742 | // not actually undefined behavior in C++11 due to a language defect. |
| 4743 | if (RHS.isNegative() && RHS.isAllOnesValue() && |
| 4744 | LHS.isSigned() && LHS.isMinSignedValue()) |
| 4745 | HandleOverflow(Info, E, -LHS.extend(LHS.getBitWidth() + 1), E->getType()); |
| 4746 | return Success(E->getOpcode() == BO_Rem ? LHS % RHS : LHS / RHS, E, |
| 4747 | Result); |
| 4748 | case BO_Shl: { |
| 4749 | // During constant-folding, a negative shift is an opposite shift. Such |
| 4750 | // a shift is not a constant expression. |
| 4751 | if (RHS.isSigned() && RHS.isNegative()) { |
| 4752 | CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; |
| 4753 | RHS = -RHS; |
| 4754 | goto shift_right; |
| 4755 | } |
| 4756 | |
| 4757 | shift_left: |
| 4758 | // C++11 [expr.shift]p1: Shift width must be less than the bit width of |
| 4759 | // the shifted type. |
| 4760 | unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
| 4761 | if (SA != RHS) { |
| 4762 | CCEDiag(E, diag::note_constexpr_large_shift) |
| 4763 | << RHS << E->getType() << LHS.getBitWidth(); |
| 4764 | } else if (LHS.isSigned()) { |
| 4765 | // C++11 [expr.shift]p2: A signed left shift must have a non-negative |
| 4766 | // operand, and must not overflow the corresponding unsigned type. |
| 4767 | if (LHS.isNegative()) |
| 4768 | CCEDiag(E, diag::note_constexpr_lshift_of_negative) << LHS; |
| 4769 | else if (LHS.countLeadingZeros() < SA) |
| 4770 | CCEDiag(E, diag::note_constexpr_lshift_discards); |
| 4771 | } |
| 4772 | |
| 4773 | return Success(LHS << SA, E, Result); |
| 4774 | } |
| 4775 | case BO_Shr: { |
| 4776 | // During constant-folding, a negative shift is an opposite shift. Such a |
| 4777 | // shift is not a constant expression. |
| 4778 | if (RHS.isSigned() && RHS.isNegative()) { |
| 4779 | CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; |
| 4780 | RHS = -RHS; |
| 4781 | goto shift_left; |
| 4782 | } |
| 4783 | |
| 4784 | shift_right: |
| 4785 | // C++11 [expr.shift]p1: Shift width must be less than the bit width of the |
| 4786 | // shifted type. |
| 4787 | unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
| 4788 | if (SA != RHS) |
| 4789 | CCEDiag(E, diag::note_constexpr_large_shift) |
| 4790 | << RHS << E->getType() << LHS.getBitWidth(); |
| 4791 | |
| 4792 | return Success(LHS >> SA, E, Result); |
| 4793 | } |
| 4794 | |
| 4795 | case BO_LT: return Success(LHS < RHS, E, Result); |
| 4796 | case BO_GT: return Success(LHS > RHS, E, Result); |
| 4797 | case BO_LE: return Success(LHS <= RHS, E, Result); |
| 4798 | case BO_GE: return Success(LHS >= RHS, E, Result); |
| 4799 | case BO_EQ: return Success(LHS == RHS, E, Result); |
| 4800 | case BO_NE: return Success(LHS != RHS, E, Result); |
| 4801 | } |
| 4802 | } |
| 4803 | |
Richard Trieu | b778305 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 4804 | void DataRecursiveIntBinOpEvaluator::process(EvalResult &Result) { |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4805 | Job &job = Queue.back(); |
| 4806 | |
| 4807 | switch (job.Kind) { |
| 4808 | case Job::AnyExprKind: { |
| 4809 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(job.E)) { |
| 4810 | if (shouldEnqueue(Bop)) { |
| 4811 | job.Kind = Job::BinOpKind; |
| 4812 | enqueue(Bop->getLHS()); |
Richard Trieu | b778305 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 4813 | return; |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4814 | } |
| 4815 | } |
| 4816 | |
| 4817 | EvaluateExpr(job.E, Result); |
| 4818 | Queue.pop_back(); |
Richard Trieu | b778305 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 4819 | return; |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4820 | } |
| 4821 | |
| 4822 | case Job::BinOpKind: { |
| 4823 | const BinaryOperator *Bop = cast<BinaryOperator>(job.E); |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4824 | bool SuppressRHSDiags = false; |
Argyrios Kyrtzidis | 9293fff | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 4825 | if (!VisitBinOpLHSOnly(Result, Bop, SuppressRHSDiags)) { |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4826 | Queue.pop_back(); |
Richard Trieu | b778305 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 4827 | return; |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4828 | } |
| 4829 | if (SuppressRHSDiags) |
| 4830 | job.startSpeculativeEval(Info); |
Argyrios Kyrtzidis | 9293fff | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 4831 | job.LHSResult.swap(Result); |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4832 | job.Kind = Job::BinOpVisitedLHSKind; |
| 4833 | enqueue(Bop->getRHS()); |
Richard Trieu | b778305 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 4834 | return; |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4835 | } |
| 4836 | |
| 4837 | case Job::BinOpVisitedLHSKind: { |
| 4838 | const BinaryOperator *Bop = cast<BinaryOperator>(job.E); |
| 4839 | EvalResult RHS; |
| 4840 | RHS.swap(Result); |
Richard Trieu | b778305 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 4841 | Result.Failed = !VisitBinOp(job.LHSResult, RHS, Bop, Result.Val); |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4842 | Queue.pop_back(); |
Richard Trieu | b778305 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 4843 | return; |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4844 | } |
| 4845 | } |
| 4846 | |
| 4847 | llvm_unreachable("Invalid Job::Kind!"); |
| 4848 | } |
| 4849 | |
| 4850 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
| 4851 | if (E->isAssignmentOp()) |
| 4852 | return Error(E); |
| 4853 | |
| 4854 | if (DataRecursiveIntBinOpEvaluator::shouldEnqueue(E)) |
| 4855 | return DataRecursiveIntBinOpEvaluator(*this, Result).Traverse(E); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 4856 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4857 | QualType LHSTy = E->getLHS()->getType(); |
| 4858 | QualType RHSTy = E->getRHS()->getType(); |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 4859 | |
| 4860 | if (LHSTy->isAnyComplexType()) { |
| 4861 | assert(RHSTy->isAnyComplexType() && "Invalid comparison"); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 4862 | ComplexValue LHS, RHS; |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 4863 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4864 | bool LHSOK = EvaluateComplex(E->getLHS(), LHS, Info); |
| 4865 | if (!LHSOK && !Info.keepEvaluatingAfterFailure()) |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 4866 | return false; |
| 4867 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4868 | if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK) |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 4869 | return false; |
| 4870 | |
| 4871 | if (LHS.isComplexFloat()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4872 | APFloat::cmpResult CR_r = |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 4873 | LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4874 | APFloat::cmpResult CR_i = |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 4875 | LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag()); |
| 4876 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4877 | if (E->getOpcode() == BO_EQ) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4878 | return Success((CR_r == APFloat::cmpEqual && |
| 4879 | CR_i == APFloat::cmpEqual), E); |
| 4880 | else { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4881 | assert(E->getOpcode() == BO_NE && |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4882 | "Invalid complex comparison."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4883 | return Success(((CR_r == APFloat::cmpGreaterThan || |
Mon P Wang | fc39dc4 | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 4884 | CR_r == APFloat::cmpLessThan || |
| 4885 | CR_r == APFloat::cmpUnordered) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4886 | (CR_i == APFloat::cmpGreaterThan || |
Mon P Wang | fc39dc4 | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 4887 | CR_i == APFloat::cmpLessThan || |
| 4888 | CR_i == APFloat::cmpUnordered)), E); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4889 | } |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 4890 | } else { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4891 | if (E->getOpcode() == BO_EQ) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4892 | return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() && |
| 4893 | LHS.getComplexIntImag() == RHS.getComplexIntImag()), E); |
| 4894 | else { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4895 | assert(E->getOpcode() == BO_NE && |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4896 | "Invalid compex comparison."); |
| 4897 | return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() || |
| 4898 | LHS.getComplexIntImag() != RHS.getComplexIntImag()), E); |
| 4899 | } |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 4900 | } |
| 4901 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4902 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4903 | if (LHSTy->isRealFloatingType() && |
| 4904 | RHSTy->isRealFloatingType()) { |
| 4905 | APFloat RHS(0.0), LHS(0.0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4906 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4907 | bool LHSOK = EvaluateFloat(E->getRHS(), RHS, Info); |
| 4908 | if (!LHSOK && !Info.keepEvaluatingAfterFailure()) |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4909 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4910 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4911 | if (!EvaluateFloat(E->getLHS(), LHS, Info) || !LHSOK) |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4912 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4913 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4914 | APFloat::cmpResult CR = LHS.compare(RHS); |
Anders Carlsson | 529569e | 2008-11-16 22:46:56 +0000 | [diff] [blame] | 4915 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4916 | switch (E->getOpcode()) { |
| 4917 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4918 | llvm_unreachable("Invalid binary operator!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4919 | case BO_LT: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4920 | return Success(CR == APFloat::cmpLessThan, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4921 | case BO_GT: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4922 | return Success(CR == APFloat::cmpGreaterThan, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4923 | case BO_LE: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4924 | return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4925 | case BO_GE: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4926 | return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual, |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4927 | E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4928 | case BO_EQ: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4929 | return Success(CR == APFloat::cmpEqual, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4930 | case BO_NE: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4931 | return Success(CR == APFloat::cmpGreaterThan |
Mon P Wang | fc39dc4 | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 4932 | || CR == APFloat::cmpLessThan |
| 4933 | || CR == APFloat::cmpUnordered, E); |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4934 | } |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4935 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4936 | |
Eli Friedman | ad02d7d | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 4937 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 4938 | if (E->getOpcode() == BO_Sub || E->isComparisonOp()) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4939 | LValue LHSValue, RHSValue; |
| 4940 | |
| 4941 | bool LHSOK = EvaluatePointer(E->getLHS(), LHSValue, Info); |
| 4942 | if (!LHSOK && Info.keepEvaluatingAfterFailure()) |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4943 | return false; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 4944 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4945 | if (!EvaluatePointer(E->getRHS(), RHSValue, Info) || !LHSOK) |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4946 | return false; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 4947 | |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 4948 | // Reject differing bases from the normal codepath; we special-case |
| 4949 | // comparisons to null. |
| 4950 | if (!HasSameBase(LHSValue, RHSValue)) { |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 4951 | if (E->getOpcode() == BO_Sub) { |
| 4952 | // Handle &&A - &&B. |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 4953 | if (!LHSValue.Offset.isZero() || !RHSValue.Offset.isZero()) |
| 4954 | return false; |
| 4955 | const Expr *LHSExpr = LHSValue.Base.dyn_cast<const Expr*>(); |
| 4956 | const Expr *RHSExpr = LHSValue.Base.dyn_cast<const Expr*>(); |
| 4957 | if (!LHSExpr || !RHSExpr) |
| 4958 | return false; |
| 4959 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); |
| 4960 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); |
| 4961 | if (!LHSAddrExpr || !RHSAddrExpr) |
| 4962 | return false; |
Eli Friedman | 5930a4c | 2012-01-05 23:59:40 +0000 | [diff] [blame] | 4963 | // Make sure both labels come from the same function. |
| 4964 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
| 4965 | RHSAddrExpr->getLabel()->getDeclContext()) |
| 4966 | return false; |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4967 | Result = APValue(LHSAddrExpr, RHSAddrExpr); |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 4968 | return true; |
| 4969 | } |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 4970 | // Inequalities and subtractions between unrelated pointers have |
| 4971 | // unspecified or undefined behavior. |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 4972 | if (!E->isEqualityOp()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4973 | return Error(E); |
Eli Friedman | ffbda40 | 2011-10-31 22:28:05 +0000 | [diff] [blame] | 4974 | // A constant address may compare equal to the address of a symbol. |
| 4975 | // The one exception is that address of an object cannot compare equal |
Eli Friedman | c45061b | 2011-10-31 22:54:30 +0000 | [diff] [blame] | 4976 | // to a null pointer constant. |
Eli Friedman | ffbda40 | 2011-10-31 22:28:05 +0000 | [diff] [blame] | 4977 | if ((!LHSValue.Base && !LHSValue.Offset.isZero()) || |
| 4978 | (!RHSValue.Base && !RHSValue.Offset.isZero())) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4979 | return Error(E); |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 4980 | // It's implementation-defined whether distinct literals will have |
Richard Smith | b02e462 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 4981 | // distinct addresses. In clang, the result of such a comparison is |
| 4982 | // unspecified, so it is not a constant expression. However, we do know |
| 4983 | // that the address of a literal will be non-null. |
Richard Smith | 74f4634 | 2011-11-04 01:10:57 +0000 | [diff] [blame] | 4984 | if ((IsLiteralLValue(LHSValue) || IsLiteralLValue(RHSValue)) && |
| 4985 | LHSValue.Base && RHSValue.Base) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4986 | return Error(E); |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 4987 | // We can't tell whether weak symbols will end up pointing to the same |
| 4988 | // object. |
| 4989 | if (IsWeakLValue(LHSValue) || IsWeakLValue(RHSValue)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4990 | return Error(E); |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 4991 | // Pointers with different bases cannot represent the same object. |
Eli Friedman | c45061b | 2011-10-31 22:54:30 +0000 | [diff] [blame] | 4992 | // (Note that clang defaults to -fmerge-all-constants, which can |
| 4993 | // lead to inconsistent results for comparisons involving the address |
| 4994 | // of a constant; this generally doesn't matter in practice.) |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 4995 | return Success(E->getOpcode() == BO_NE, E); |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 4996 | } |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 4997 | |
Richard Smith | 15efc4d | 2012-02-01 08:10:20 +0000 | [diff] [blame] | 4998 | const CharUnits &LHSOffset = LHSValue.getLValueOffset(); |
| 4999 | const CharUnits &RHSOffset = RHSValue.getLValueOffset(); |
| 5000 | |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 5001 | SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator(); |
| 5002 | SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator(); |
| 5003 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5004 | if (E->getOpcode() == BO_Sub) { |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 5005 | // C++11 [expr.add]p6: |
| 5006 | // Unless both pointers point to elements of the same array object, or |
| 5007 | // one past the last element of the array object, the behavior is |
| 5008 | // undefined. |
| 5009 | if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && |
| 5010 | !AreElementsOfSameArray(getType(LHSValue.Base), |
| 5011 | LHSDesignator, RHSDesignator)) |
| 5012 | CCEDiag(E, diag::note_constexpr_pointer_subtraction_not_same_array); |
| 5013 | |
Chris Lattner | 4992bdd | 2010-04-20 17:13:14 +0000 | [diff] [blame] | 5014 | QualType Type = E->getLHS()->getType(); |
| 5015 | QualType ElementType = Type->getAs<PointerType>()->getPointeeType(); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 5016 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5017 | CharUnits ElementSize; |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 5018 | if (!HandleSizeof(Info, E->getExprLoc(), ElementType, ElementSize)) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5019 | return false; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 5020 | |
Richard Smith | 15efc4d | 2012-02-01 08:10:20 +0000 | [diff] [blame] | 5021 | // FIXME: LLVM and GCC both compute LHSOffset - RHSOffset at runtime, |
| 5022 | // and produce incorrect results when it overflows. Such behavior |
| 5023 | // appears to be non-conforming, but is common, so perhaps we should |
| 5024 | // assume the standard intended for such cases to be undefined behavior |
| 5025 | // and check for them. |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 5026 | |
Richard Smith | 15efc4d | 2012-02-01 08:10:20 +0000 | [diff] [blame] | 5027 | // Compute (LHSOffset - RHSOffset) / Size carefully, checking for |
| 5028 | // overflow in the final conversion to ptrdiff_t. |
| 5029 | APSInt LHS( |
| 5030 | llvm::APInt(65, (int64_t)LHSOffset.getQuantity(), true), false); |
| 5031 | APSInt RHS( |
| 5032 | llvm::APInt(65, (int64_t)RHSOffset.getQuantity(), true), false); |
| 5033 | APSInt ElemSize( |
| 5034 | llvm::APInt(65, (int64_t)ElementSize.getQuantity(), true), false); |
| 5035 | APSInt TrueResult = (LHS - RHS) / ElemSize; |
| 5036 | APSInt Result = TrueResult.trunc(Info.Ctx.getIntWidth(E->getType())); |
| 5037 | |
| 5038 | if (Result.extend(65) != TrueResult) |
| 5039 | HandleOverflow(Info, E, TrueResult, E->getType()); |
| 5040 | return Success(Result, E); |
| 5041 | } |
Richard Smith | 82f2858 | 2012-01-31 06:41:30 +0000 | [diff] [blame] | 5042 | |
| 5043 | // C++11 [expr.rel]p3: |
| 5044 | // Pointers to void (after pointer conversions) can be compared, with a |
| 5045 | // result defined as follows: If both pointers represent the same |
| 5046 | // address or are both the null pointer value, the result is true if the |
| 5047 | // operator is <= or >= and false otherwise; otherwise the result is |
| 5048 | // unspecified. |
| 5049 | // We interpret this as applying to pointers to *cv* void. |
| 5050 | if (LHSTy->isVoidPointerType() && LHSOffset != RHSOffset && |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 5051 | E->isRelationalOp()) |
Richard Smith | 82f2858 | 2012-01-31 06:41:30 +0000 | [diff] [blame] | 5052 | CCEDiag(E, diag::note_constexpr_void_comparison); |
| 5053 | |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 5054 | // C++11 [expr.rel]p2: |
| 5055 | // - If two pointers point to non-static data members of the same object, |
| 5056 | // or to subobjects or array elements fo such members, recursively, the |
| 5057 | // pointer to the later declared member compares greater provided the |
| 5058 | // two members have the same access control and provided their class is |
| 5059 | // not a union. |
| 5060 | // [...] |
| 5061 | // - Otherwise pointer comparisons are unspecified. |
| 5062 | if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && |
| 5063 | E->isRelationalOp()) { |
| 5064 | bool WasArrayIndex; |
| 5065 | unsigned Mismatch = |
| 5066 | FindDesignatorMismatch(getType(LHSValue.Base), LHSDesignator, |
| 5067 | RHSDesignator, WasArrayIndex); |
| 5068 | // At the point where the designators diverge, the comparison has a |
| 5069 | // specified value if: |
| 5070 | // - we are comparing array indices |
| 5071 | // - we are comparing fields of a union, or fields with the same access |
| 5072 | // Otherwise, the result is unspecified and thus the comparison is not a |
| 5073 | // constant expression. |
| 5074 | if (!WasArrayIndex && Mismatch < LHSDesignator.Entries.size() && |
| 5075 | Mismatch < RHSDesignator.Entries.size()) { |
| 5076 | const FieldDecl *LF = getAsField(LHSDesignator.Entries[Mismatch]); |
| 5077 | const FieldDecl *RF = getAsField(RHSDesignator.Entries[Mismatch]); |
| 5078 | if (!LF && !RF) |
| 5079 | CCEDiag(E, diag::note_constexpr_pointer_comparison_base_classes); |
| 5080 | else if (!LF) |
| 5081 | CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) |
| 5082 | << getAsBaseClass(LHSDesignator.Entries[Mismatch]) |
| 5083 | << RF->getParent() << RF; |
| 5084 | else if (!RF) |
| 5085 | CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) |
| 5086 | << getAsBaseClass(RHSDesignator.Entries[Mismatch]) |
| 5087 | << LF->getParent() << LF; |
| 5088 | else if (!LF->getParent()->isUnion() && |
| 5089 | LF->getAccess() != RF->getAccess()) |
| 5090 | CCEDiag(E, diag::note_constexpr_pointer_comparison_differing_access) |
| 5091 | << LF << LF->getAccess() << RF << RF->getAccess() |
| 5092 | << LF->getParent(); |
| 5093 | } |
| 5094 | } |
| 5095 | |
Eli Friedman | a316988 | 2012-04-16 04:30:08 +0000 | [diff] [blame] | 5096 | // The comparison here must be unsigned, and performed with the same |
| 5097 | // width as the pointer. |
Eli Friedman | a316988 | 2012-04-16 04:30:08 +0000 | [diff] [blame] | 5098 | unsigned PtrSize = Info.Ctx.getTypeSize(LHSTy); |
| 5099 | uint64_t CompareLHS = LHSOffset.getQuantity(); |
| 5100 | uint64_t CompareRHS = RHSOffset.getQuantity(); |
| 5101 | assert(PtrSize <= 64 && "Unexpected pointer width"); |
| 5102 | uint64_t Mask = ~0ULL >> (64 - PtrSize); |
| 5103 | CompareLHS &= Mask; |
| 5104 | CompareRHS &= Mask; |
| 5105 | |
Eli Friedman | 2850376 | 2012-04-16 19:23:57 +0000 | [diff] [blame] | 5106 | // If there is a base and this is a relational operator, we can only |
| 5107 | // compare pointers within the object in question; otherwise, the result |
| 5108 | // depends on where the object is located in memory. |
| 5109 | if (!LHSValue.Base.isNull() && E->isRelationalOp()) { |
| 5110 | QualType BaseTy = getType(LHSValue.Base); |
| 5111 | if (BaseTy->isIncompleteType()) |
| 5112 | return Error(E); |
| 5113 | CharUnits Size = Info.Ctx.getTypeSizeInChars(BaseTy); |
| 5114 | uint64_t OffsetLimit = Size.getQuantity(); |
| 5115 | if (CompareLHS > OffsetLimit || CompareRHS > OffsetLimit) |
| 5116 | return Error(E); |
| 5117 | } |
| 5118 | |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 5119 | switch (E->getOpcode()) { |
| 5120 | default: llvm_unreachable("missing comparison operator"); |
Eli Friedman | a316988 | 2012-04-16 04:30:08 +0000 | [diff] [blame] | 5121 | case BO_LT: return Success(CompareLHS < CompareRHS, E); |
| 5122 | case BO_GT: return Success(CompareLHS > CompareRHS, E); |
| 5123 | case BO_LE: return Success(CompareLHS <= CompareRHS, E); |
| 5124 | case BO_GE: return Success(CompareLHS >= CompareRHS, E); |
| 5125 | case BO_EQ: return Success(CompareLHS == CompareRHS, E); |
| 5126 | case BO_NE: return Success(CompareLHS != CompareRHS, E); |
Eli Friedman | ad02d7d | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 5127 | } |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 5128 | } |
| 5129 | } |
Richard Smith | b02e462 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 5130 | |
| 5131 | if (LHSTy->isMemberPointerType()) { |
| 5132 | assert(E->isEqualityOp() && "unexpected member pointer operation"); |
| 5133 | assert(RHSTy->isMemberPointerType() && "invalid comparison"); |
| 5134 | |
| 5135 | MemberPtr LHSValue, RHSValue; |
| 5136 | |
| 5137 | bool LHSOK = EvaluateMemberPointer(E->getLHS(), LHSValue, Info); |
| 5138 | if (!LHSOK && Info.keepEvaluatingAfterFailure()) |
| 5139 | return false; |
| 5140 | |
| 5141 | if (!EvaluateMemberPointer(E->getRHS(), RHSValue, Info) || !LHSOK) |
| 5142 | return false; |
| 5143 | |
| 5144 | // C++11 [expr.eq]p2: |
| 5145 | // If both operands are null, they compare equal. Otherwise if only one is |
| 5146 | // null, they compare unequal. |
| 5147 | if (!LHSValue.getDecl() || !RHSValue.getDecl()) { |
| 5148 | bool Equal = !LHSValue.getDecl() && !RHSValue.getDecl(); |
| 5149 | return Success(E->getOpcode() == BO_EQ ? Equal : !Equal, E); |
| 5150 | } |
| 5151 | |
| 5152 | // Otherwise if either is a pointer to a virtual member function, the |
| 5153 | // result is unspecified. |
| 5154 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(LHSValue.getDecl())) |
| 5155 | if (MD->isVirtual()) |
| 5156 | CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; |
| 5157 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(RHSValue.getDecl())) |
| 5158 | if (MD->isVirtual()) |
| 5159 | CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; |
| 5160 | |
| 5161 | // Otherwise they compare equal if and only if they would refer to the |
| 5162 | // same member of the same most derived object or the same subobject if |
| 5163 | // they were dereferenced with a hypothetical object of the associated |
| 5164 | // class type. |
| 5165 | bool Equal = LHSValue == RHSValue; |
| 5166 | return Success(E->getOpcode() == BO_EQ ? Equal : !Equal, E); |
| 5167 | } |
| 5168 | |
Richard Smith | 26f2cac | 2012-02-14 22:35:28 +0000 | [diff] [blame] | 5169 | if (LHSTy->isNullPtrType()) { |
| 5170 | assert(E->isComparisonOp() && "unexpected nullptr operation"); |
| 5171 | assert(RHSTy->isNullPtrType() && "missing pointer conversion"); |
| 5172 | // C++11 [expr.rel]p4, [expr.eq]p3: If two operands of type std::nullptr_t |
| 5173 | // are compared, the result is true of the operator is <=, >= or ==, and |
| 5174 | // false otherwise. |
| 5175 | BinaryOperator::Opcode Opcode = E->getOpcode(); |
| 5176 | return Success(Opcode == BO_EQ || Opcode == BO_LE || Opcode == BO_GE, E); |
| 5177 | } |
| 5178 | |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5179 | assert((!LHSTy->isIntegralOrEnumerationType() || |
| 5180 | !RHSTy->isIntegralOrEnumerationType()) && |
| 5181 | "DataRecursiveIntBinOpEvaluator should have handled integral types"); |
| 5182 | // We can't continue from here for non-integral types. |
| 5183 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 5184 | } |
| 5185 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 5186 | CharUnits IntExprEvaluator::GetAlignOfType(QualType T) { |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 5187 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 5188 | // result shall be the alignment of the referenced type." |
| 5189 | if (const ReferenceType *Ref = T->getAs<ReferenceType>()) |
| 5190 | T = Ref->getPointeeType(); |
Chad Rosier | 9f1210c | 2011-07-26 07:03:04 +0000 | [diff] [blame] | 5191 | |
| 5192 | // __alignof is defined to return the preferred alignment. |
| 5193 | return Info.Ctx.toCharUnitsFromBits( |
| 5194 | Info.Ctx.getPreferredTypeAlign(T.getTypePtr())); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 5195 | } |
| 5196 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 5197 | CharUnits IntExprEvaluator::GetAlignOfExpr(const Expr *E) { |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 5198 | E = E->IgnoreParens(); |
| 5199 | |
| 5200 | // alignof decl is always accepted, even if it doesn't make sense: we default |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5201 | // to 1 in those cases. |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 5202 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 5203 | return Info.Ctx.getDeclAlign(DRE->getDecl(), |
| 5204 | /*RefAsPointee*/true); |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 5205 | |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 5206 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 5207 | return Info.Ctx.getDeclAlign(ME->getMemberDecl(), |
| 5208 | /*RefAsPointee*/true); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 5209 | |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 5210 | return GetAlignOfType(E->getType()); |
| 5211 | } |
| 5212 | |
| 5213 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5214 | /// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with |
| 5215 | /// a result as the expression's type. |
| 5216 | bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr( |
| 5217 | const UnaryExprOrTypeTraitExpr *E) { |
| 5218 | switch(E->getKind()) { |
| 5219 | case UETT_AlignOf: { |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 5220 | if (E->isArgumentType()) |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 5221 | return Success(GetAlignOfType(E->getArgumentType()), E); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 5222 | else |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 5223 | return Success(GetAlignOfExpr(E->getArgumentExpr()), E); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 5224 | } |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 5225 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5226 | case UETT_VecStep: { |
| 5227 | QualType Ty = E->getTypeOfArgument(); |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 5228 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5229 | if (Ty->isVectorType()) { |
| 5230 | unsigned n = Ty->getAs<VectorType>()->getNumElements(); |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 5231 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5232 | // The vec_step built-in functions that take a 3-component |
| 5233 | // vector return 4. (OpenCL 1.1 spec 6.11.12) |
| 5234 | if (n == 3) |
| 5235 | n = 4; |
Eli Friedman | f2da9df | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 5236 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5237 | return Success(n, E); |
| 5238 | } else |
| 5239 | return Success(1, E); |
| 5240 | } |
| 5241 | |
| 5242 | case UETT_SizeOf: { |
| 5243 | QualType SrcTy = E->getTypeOfArgument(); |
| 5244 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 5245 | // the result is the size of the referenced type." |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5246 | if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>()) |
| 5247 | SrcTy = Ref->getPointeeType(); |
| 5248 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5249 | CharUnits Sizeof; |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 5250 | if (!HandleSizeof(Info, E->getExprLoc(), SrcTy, Sizeof)) |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5251 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5252 | return Success(Sizeof, E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5253 | } |
| 5254 | } |
| 5255 | |
| 5256 | llvm_unreachable("unknown expr/type trait"); |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 5257 | } |
| 5258 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5259 | bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) { |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 5260 | CharUnits Result; |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5261 | unsigned n = OOE->getNumComponents(); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 5262 | if (n == 0) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5263 | return Error(OOE); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5264 | QualType CurrentType = OOE->getTypeSourceInfo()->getType(); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 5265 | for (unsigned i = 0; i != n; ++i) { |
| 5266 | OffsetOfExpr::OffsetOfNode ON = OOE->getComponent(i); |
| 5267 | switch (ON.getKind()) { |
| 5268 | case OffsetOfExpr::OffsetOfNode::Array: { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5269 | const Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex()); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 5270 | APSInt IdxResult; |
| 5271 | if (!EvaluateInteger(Idx, IdxResult, Info)) |
| 5272 | return false; |
| 5273 | const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType); |
| 5274 | if (!AT) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5275 | return Error(OOE); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 5276 | CurrentType = AT->getElementType(); |
| 5277 | CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType); |
| 5278 | Result += IdxResult.getSExtValue() * ElementSize; |
| 5279 | break; |
| 5280 | } |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5281 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 5282 | case OffsetOfExpr::OffsetOfNode::Field: { |
| 5283 | FieldDecl *MemberDecl = ON.getField(); |
| 5284 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5285 | if (!RT) |
| 5286 | return Error(OOE); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 5287 | RecordDecl *RD = RT->getDecl(); |
| 5288 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 5289 | unsigned i = MemberDecl->getFieldIndex(); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 5290 | assert(i < RL.getFieldCount() && "offsetof field in wrong type"); |
Ken Dyck | fb1e3bc | 2011-01-18 01:56:16 +0000 | [diff] [blame] | 5291 | Result += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i)); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 5292 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 5293 | break; |
| 5294 | } |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5295 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 5296 | case OffsetOfExpr::OffsetOfNode::Identifier: |
| 5297 | llvm_unreachable("dependent __builtin_offsetof"); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5298 | |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 5299 | case OffsetOfExpr::OffsetOfNode::Base: { |
| 5300 | CXXBaseSpecifier *BaseSpec = ON.getBase(); |
| 5301 | if (BaseSpec->isVirtual()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5302 | return Error(OOE); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 5303 | |
| 5304 | // Find the layout of the class whose base we are looking into. |
| 5305 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5306 | if (!RT) |
| 5307 | return Error(OOE); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 5308 | RecordDecl *RD = RT->getDecl(); |
| 5309 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
| 5310 | |
| 5311 | // Find the base class itself. |
| 5312 | CurrentType = BaseSpec->getType(); |
| 5313 | const RecordType *BaseRT = CurrentType->getAs<RecordType>(); |
| 5314 | if (!BaseRT) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5315 | return Error(OOE); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 5316 | |
| 5317 | // Add the offset to the base. |
Ken Dyck | 7c7f820 | 2011-01-26 02:17:08 +0000 | [diff] [blame] | 5318 | Result += RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl())); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 5319 | break; |
| 5320 | } |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 5321 | } |
| 5322 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5323 | return Success(Result, OOE); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 5324 | } |
| 5325 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 5326 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5327 | switch (E->getOpcode()) { |
| 5328 | default: |
| 5329 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 5330 | // See C99 6.6p3. |
| 5331 | return Error(E); |
| 5332 | case UO_Extension: |
| 5333 | // FIXME: Should extension allow i-c-e extension expressions in its scope? |
| 5334 | // If so, we could clear the diagnostic ID. |
| 5335 | return Visit(E->getSubExpr()); |
| 5336 | case UO_Plus: |
| 5337 | // The result is just the value. |
| 5338 | return Visit(E->getSubExpr()); |
| 5339 | case UO_Minus: { |
| 5340 | if (!Visit(E->getSubExpr())) |
| 5341 | return false; |
| 5342 | if (!Result.isInt()) return Error(E); |
Richard Smith | 789f9b6 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 5343 | const APSInt &Value = Result.getInt(); |
| 5344 | if (Value.isSigned() && Value.isMinSignedValue()) |
| 5345 | HandleOverflow(Info, E, -Value.extend(Value.getBitWidth() + 1), |
| 5346 | E->getType()); |
| 5347 | return Success(-Value, E); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5348 | } |
| 5349 | case UO_Not: { |
| 5350 | if (!Visit(E->getSubExpr())) |
| 5351 | return false; |
| 5352 | if (!Result.isInt()) return Error(E); |
| 5353 | return Success(~Result.getInt(), E); |
| 5354 | } |
| 5355 | case UO_LNot: { |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 5356 | bool bres; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5357 | if (!EvaluateAsBooleanCondition(E->getSubExpr(), bres, Info)) |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 5358 | return false; |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5359 | return Success(!bres, E); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 5360 | } |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 5361 | } |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 5362 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5363 | |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 5364 | /// HandleCast - This is used to evaluate implicit or explicit casts where the |
| 5365 | /// result type is integer. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5366 | bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 5367 | const Expr *SubExpr = E->getSubExpr(); |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 5368 | QualType DestType = E->getType(); |
Daniel Dunbar | b92dac8 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 5369 | QualType SrcType = SubExpr->getType(); |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 5370 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5371 | switch (E->getCastKind()) { |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5372 | case CK_BaseToDerived: |
| 5373 | case CK_DerivedToBase: |
| 5374 | case CK_UncheckedDerivedToBase: |
| 5375 | case CK_Dynamic: |
| 5376 | case CK_ToUnion: |
| 5377 | case CK_ArrayToPointerDecay: |
| 5378 | case CK_FunctionToPointerDecay: |
| 5379 | case CK_NullToPointer: |
| 5380 | case CK_NullToMemberPointer: |
| 5381 | case CK_BaseToDerivedMemberPointer: |
| 5382 | case CK_DerivedToBaseMemberPointer: |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 5383 | case CK_ReinterpretMemberPointer: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5384 | case CK_ConstructorConversion: |
| 5385 | case CK_IntegralToPointer: |
| 5386 | case CK_ToVoid: |
| 5387 | case CK_VectorSplat: |
| 5388 | case CK_IntegralToFloating: |
| 5389 | case CK_FloatingCast: |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5390 | case CK_CPointerToObjCPointerCast: |
| 5391 | case CK_BlockPointerToObjCPointerCast: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5392 | case CK_AnyPointerToBlockPointerCast: |
| 5393 | case CK_ObjCObjectLValueCast: |
| 5394 | case CK_FloatingRealToComplex: |
| 5395 | case CK_FloatingComplexToReal: |
| 5396 | case CK_FloatingComplexCast: |
| 5397 | case CK_FloatingComplexToIntegralComplex: |
| 5398 | case CK_IntegralRealToComplex: |
| 5399 | case CK_IntegralComplexCast: |
| 5400 | case CK_IntegralComplexToFloatingComplex: |
| 5401 | llvm_unreachable("invalid cast kind for integral value"); |
| 5402 | |
Eli Friedman | e50c297 | 2011-03-25 19:07:11 +0000 | [diff] [blame] | 5403 | case CK_BitCast: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5404 | case CK_Dependent: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5405 | case CK_LValueBitCast: |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 5406 | case CK_ARCProduceObject: |
| 5407 | case CK_ARCConsumeObject: |
| 5408 | case CK_ARCReclaimReturnedObject: |
| 5409 | case CK_ARCExtendBlockObject: |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 5410 | case CK_CopyAndAutoreleaseBlockObject: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5411 | return Error(E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5412 | |
Richard Smith | 7d580a4 | 2012-01-17 21:17:26 +0000 | [diff] [blame] | 5413 | case CK_UserDefinedConversion: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5414 | case CK_LValueToRValue: |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 5415 | case CK_AtomicToNonAtomic: |
| 5416 | case CK_NonAtomicToAtomic: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5417 | case CK_NoOp: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5418 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5419 | |
| 5420 | case CK_MemberPointerToBoolean: |
| 5421 | case CK_PointerToBoolean: |
| 5422 | case CK_IntegralToBoolean: |
| 5423 | case CK_FloatingToBoolean: |
| 5424 | case CK_FloatingComplexToBoolean: |
| 5425 | case CK_IntegralComplexToBoolean: { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5426 | bool BoolResult; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5427 | if (!EvaluateAsBooleanCondition(SubExpr, BoolResult, Info)) |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5428 | return false; |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5429 | return Success(BoolResult, E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5430 | } |
| 5431 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5432 | case CK_IntegralCast: { |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 5433 | if (!Visit(SubExpr)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 5434 | return false; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 5435 | |
Eli Friedman | be26570 | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 5436 | if (!Result.isInt()) { |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 5437 | // Allow casts of address-of-label differences if they are no-ops |
| 5438 | // or narrowing. (The narrowing case isn't actually guaranteed to |
| 5439 | // be constant-evaluatable except in some narrow cases which are hard |
| 5440 | // to detect here. We let it through on the assumption the user knows |
| 5441 | // what they are doing.) |
| 5442 | if (Result.isAddrLabelDiff()) |
| 5443 | return Info.Ctx.getTypeSize(DestType) <= Info.Ctx.getTypeSize(SrcType); |
Eli Friedman | be26570 | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 5444 | // Only allow casts of lvalues if they are lossless. |
| 5445 | return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType); |
| 5446 | } |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 5447 | |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 5448 | return Success(HandleIntToIntCast(Info, E, DestType, SrcType, |
| 5449 | Result.getInt()), E); |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 5450 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5451 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5452 | case CK_PointerToIntegral: { |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 5453 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 5454 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5455 | LValue LV; |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 5456 | if (!EvaluatePointer(SubExpr, LV, Info)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 5457 | return false; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5458 | |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 5459 | if (LV.getLValueBase()) { |
| 5460 | // Only allow based lvalue casts if they are lossless. |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 5461 | // FIXME: Allow a larger integer size than the pointer size, and allow |
| 5462 | // narrowing back down to pointer width in subsequent integral casts. |
| 5463 | // FIXME: Check integer type's active bits, not its type size. |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 5464 | if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5465 | return Error(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5466 | |
Richard Smith | b755a9d | 2011-11-16 07:18:12 +0000 | [diff] [blame] | 5467 | LV.Designator.setInvalid(); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5468 | LV.moveInto(Result); |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 5469 | return true; |
| 5470 | } |
| 5471 | |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 5472 | APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(), |
| 5473 | SrcType); |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 5474 | return Success(HandleIntToIntCast(Info, E, DestType, SrcType, AsInt), E); |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 5475 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5476 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5477 | case CK_IntegralComplexToReal: { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5478 | ComplexValue C; |
Eli Friedman | 1725f68 | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 5479 | if (!EvaluateComplex(SubExpr, C, Info)) |
| 5480 | return false; |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5481 | return Success(C.getComplexIntReal(), E); |
Eli Friedman | 1725f68 | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 5482 | } |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 5483 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5484 | case CK_FloatingToIntegral: { |
| 5485 | APFloat F(0.0); |
| 5486 | if (!EvaluateFloat(SubExpr, F, Info)) |
| 5487 | return false; |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 5488 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5489 | APSInt Value; |
| 5490 | if (!HandleFloatToIntCast(Info, E, SrcType, F, DestType, Value)) |
| 5491 | return false; |
| 5492 | return Success(Value, E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5493 | } |
| 5494 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5495 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5496 | llvm_unreachable("unknown cast resulting in integral value"); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 5497 | } |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 5498 | |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 5499 | bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
| 5500 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5501 | ComplexValue LV; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5502 | if (!EvaluateComplex(E->getSubExpr(), LV, Info)) |
| 5503 | return false; |
| 5504 | if (!LV.isComplexInt()) |
| 5505 | return Error(E); |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 5506 | return Success(LV.getComplexIntReal(), E); |
| 5507 | } |
| 5508 | |
| 5509 | return Visit(E->getSubExpr()); |
| 5510 | } |
| 5511 | |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 5512 | bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 5513 | if (E->getSubExpr()->getType()->isComplexIntegerType()) { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5514 | ComplexValue LV; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5515 | if (!EvaluateComplex(E->getSubExpr(), LV, Info)) |
| 5516 | return false; |
| 5517 | if (!LV.isComplexInt()) |
| 5518 | return Error(E); |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 5519 | return Success(LV.getComplexIntImag(), E); |
| 5520 | } |
| 5521 | |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 5522 | VisitIgnoredValue(E->getSubExpr()); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 5523 | return Success(0, E); |
| 5524 | } |
| 5525 | |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 5526 | bool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) { |
| 5527 | return Success(E->getPackLength(), E); |
| 5528 | } |
| 5529 | |
Sebastian Redl | 295995c | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 5530 | bool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) { |
| 5531 | return Success(E->getValue(), E); |
| 5532 | } |
| 5533 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5534 | //===----------------------------------------------------------------------===// |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5535 | // Float Evaluation |
| 5536 | //===----------------------------------------------------------------------===// |
| 5537 | |
| 5538 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 5539 | class FloatExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5540 | : public ExprEvaluatorBase<FloatExprEvaluator, bool> { |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5541 | APFloat &Result; |
| 5542 | public: |
| 5543 | FloatExprEvaluator(EvalInfo &info, APFloat &result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5544 | : ExprEvaluatorBaseTy(info), Result(result) {} |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5545 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5546 | bool Success(const APValue &V, const Expr *e) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5547 | Result = V.getFloat(); |
| 5548 | return true; |
| 5549 | } |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5550 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5551 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 5552 | Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType())); |
| 5553 | return true; |
| 5554 | } |
| 5555 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 5556 | bool VisitCallExpr(const CallExpr *E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5557 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 5558 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5559 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 5560 | bool VisitFloatingLiteral(const FloatingLiteral *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5561 | bool VisitCastExpr(const CastExpr *E); |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 5562 | |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 5563 | bool VisitUnaryReal(const UnaryOperator *E); |
| 5564 | bool VisitUnaryImag(const UnaryOperator *E); |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 5565 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5566 | // FIXME: Missing: array subscript of vector, member of vector |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5567 | }; |
| 5568 | } // end anonymous namespace |
| 5569 | |
| 5570 | static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5571 | assert(E->isRValue() && E->getType()->isRealFloatingType()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5572 | return FloatExprEvaluator(Info, Result).Visit(E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5573 | } |
| 5574 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5575 | static bool TryEvaluateBuiltinNaN(const ASTContext &Context, |
John McCall | db7b72a | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 5576 | QualType ResultTy, |
| 5577 | const Expr *Arg, |
| 5578 | bool SNaN, |
| 5579 | llvm::APFloat &Result) { |
| 5580 | const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts()); |
| 5581 | if (!S) return false; |
| 5582 | |
| 5583 | const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy); |
| 5584 | |
| 5585 | llvm::APInt fill; |
| 5586 | |
| 5587 | // Treat empty strings as if they were zero. |
| 5588 | if (S->getString().empty()) |
| 5589 | fill = llvm::APInt(32, 0); |
| 5590 | else if (S->getString().getAsInteger(0, fill)) |
| 5591 | return false; |
| 5592 | |
| 5593 | if (SNaN) |
| 5594 | Result = llvm::APFloat::getSNaN(Sem, false, &fill); |
| 5595 | else |
| 5596 | Result = llvm::APFloat::getQNaN(Sem, false, &fill); |
| 5597 | return true; |
| 5598 | } |
| 5599 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 5600 | bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5601 | switch (E->isBuiltinCall()) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5602 | default: |
| 5603 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
| 5604 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 5605 | case Builtin::BI__builtin_huge_val: |
| 5606 | case Builtin::BI__builtin_huge_valf: |
| 5607 | case Builtin::BI__builtin_huge_vall: |
| 5608 | case Builtin::BI__builtin_inf: |
| 5609 | case Builtin::BI__builtin_inff: |
Daniel Dunbar | 7cbed03 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 5610 | case Builtin::BI__builtin_infl: { |
| 5611 | const llvm::fltSemantics &Sem = |
| 5612 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Chris Lattner | 34a74ab | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 5613 | Result = llvm::APFloat::getInf(Sem); |
| 5614 | return true; |
Daniel Dunbar | 7cbed03 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 5615 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5616 | |
John McCall | db7b72a | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 5617 | case Builtin::BI__builtin_nans: |
| 5618 | case Builtin::BI__builtin_nansf: |
| 5619 | case Builtin::BI__builtin_nansl: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5620 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 5621 | true, Result)) |
| 5622 | return Error(E); |
| 5623 | return true; |
John McCall | db7b72a | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 5624 | |
Chris Lattner | 9e62171 | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 5625 | case Builtin::BI__builtin_nan: |
| 5626 | case Builtin::BI__builtin_nanf: |
| 5627 | case Builtin::BI__builtin_nanl: |
Mike Stump | 4572bab | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 5628 | // If this is __builtin_nan() turn this into a nan, otherwise we |
Chris Lattner | 9e62171 | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 5629 | // can't constant fold it. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5630 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 5631 | false, Result)) |
| 5632 | return Error(E); |
| 5633 | return true; |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 5634 | |
| 5635 | case Builtin::BI__builtin_fabs: |
| 5636 | case Builtin::BI__builtin_fabsf: |
| 5637 | case Builtin::BI__builtin_fabsl: |
| 5638 | if (!EvaluateFloat(E->getArg(0), Result, Info)) |
| 5639 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5640 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 5641 | if (Result.isNegative()) |
| 5642 | Result.changeSign(); |
| 5643 | return true; |
| 5644 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5645 | case Builtin::BI__builtin_copysign: |
| 5646 | case Builtin::BI__builtin_copysignf: |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 5647 | case Builtin::BI__builtin_copysignl: { |
| 5648 | APFloat RHS(0.); |
| 5649 | if (!EvaluateFloat(E->getArg(0), Result, Info) || |
| 5650 | !EvaluateFloat(E->getArg(1), RHS, Info)) |
| 5651 | return false; |
| 5652 | Result.copySign(RHS); |
| 5653 | return true; |
| 5654 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 5655 | } |
| 5656 | } |
| 5657 | |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 5658 | bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
Eli Friedman | 43efa31 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 5659 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 5660 | ComplexValue CV; |
| 5661 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
| 5662 | return false; |
| 5663 | Result = CV.FloatReal; |
| 5664 | return true; |
| 5665 | } |
| 5666 | |
| 5667 | return Visit(E->getSubExpr()); |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 5668 | } |
| 5669 | |
| 5670 | bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | 43efa31 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 5671 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 5672 | ComplexValue CV; |
| 5673 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
| 5674 | return false; |
| 5675 | Result = CV.FloatImag; |
| 5676 | return true; |
| 5677 | } |
| 5678 | |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 5679 | VisitIgnoredValue(E->getSubExpr()); |
Eli Friedman | 43efa31 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 5680 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(E->getType()); |
| 5681 | Result = llvm::APFloat::getZero(Sem); |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 5682 | return true; |
| 5683 | } |
| 5684 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 5685 | bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 5686 | switch (E->getOpcode()) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5687 | default: return Error(E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5688 | case UO_Plus: |
Richard Smith | 7993e8a | 2011-10-30 23:17:09 +0000 | [diff] [blame] | 5689 | return EvaluateFloat(E->getSubExpr(), Result, Info); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5690 | case UO_Minus: |
Richard Smith | 7993e8a | 2011-10-30 23:17:09 +0000 | [diff] [blame] | 5691 | if (!EvaluateFloat(E->getSubExpr(), Result, Info)) |
| 5692 | return false; |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 5693 | Result.changeSign(); |
| 5694 | return true; |
| 5695 | } |
| 5696 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 5697 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5698 | bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5699 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
| 5700 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Eli Friedman | 7f92f03 | 2009-11-16 04:25:37 +0000 | [diff] [blame] | 5701 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 5702 | APFloat RHS(0.0); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5703 | bool LHSOK = EvaluateFloat(E->getLHS(), Result, Info); |
| 5704 | if (!LHSOK && !Info.keepEvaluatingAfterFailure()) |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5705 | return false; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5706 | if (!EvaluateFloat(E->getRHS(), RHS, Info) || !LHSOK) |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5707 | return false; |
| 5708 | |
| 5709 | switch (E->getOpcode()) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5710 | default: return Error(E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5711 | case BO_Mul: |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5712 | Result.multiply(RHS, APFloat::rmNearestTiesToEven); |
Richard Smith | 7b48a29 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 5713 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5714 | case BO_Add: |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5715 | Result.add(RHS, APFloat::rmNearestTiesToEven); |
Richard Smith | 7b48a29 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 5716 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5717 | case BO_Sub: |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5718 | Result.subtract(RHS, APFloat::rmNearestTiesToEven); |
Richard Smith | 7b48a29 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 5719 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5720 | case BO_Div: |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5721 | Result.divide(RHS, APFloat::rmNearestTiesToEven); |
Richard Smith | 7b48a29 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 5722 | break; |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5723 | } |
Richard Smith | 7b48a29 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 5724 | |
| 5725 | if (Result.isInfinity() || Result.isNaN()) |
| 5726 | CCEDiag(E, diag::note_constexpr_float_arithmetic) << Result.isNaN(); |
| 5727 | return true; |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5728 | } |
| 5729 | |
| 5730 | bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { |
| 5731 | Result = E->getValue(); |
| 5732 | return true; |
| 5733 | } |
| 5734 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5735 | bool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 5736 | const Expr* SubExpr = E->getSubExpr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5737 | |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 5738 | switch (E->getCastKind()) { |
| 5739 | default: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5740 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 5741 | |
| 5742 | case CK_IntegralToFloating: { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5743 | APSInt IntResult; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5744 | return EvaluateInteger(SubExpr, IntResult, Info) && |
| 5745 | HandleIntToFloatCast(Info, E, SubExpr->getType(), IntResult, |
| 5746 | E->getType(), Result); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5747 | } |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 5748 | |
| 5749 | case CK_FloatingCast: { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5750 | if (!Visit(SubExpr)) |
| 5751 | return false; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5752 | return HandleFloatToFloatCast(Info, E, SubExpr->getType(), E->getType(), |
| 5753 | Result); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5754 | } |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5755 | |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 5756 | case CK_FloatingComplexToReal: { |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5757 | ComplexValue V; |
| 5758 | if (!EvaluateComplex(SubExpr, V, Info)) |
| 5759 | return false; |
| 5760 | Result = V.getComplexFloatReal(); |
| 5761 | return true; |
| 5762 | } |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 5763 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5764 | } |
| 5765 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5766 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 5767 | // Complex Evaluation (for float and integer) |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 5768 | //===----------------------------------------------------------------------===// |
| 5769 | |
| 5770 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 5771 | class ComplexExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5772 | : public ExprEvaluatorBase<ComplexExprEvaluator, bool> { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5773 | ComplexValue &Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5774 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 5775 | public: |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5776 | ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5777 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
| 5778 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5779 | bool Success(const APValue &V, const Expr *e) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5780 | Result.setFrom(V); |
| 5781 | return true; |
| 5782 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5783 | |
Eli Friedman | 7ead5c7 | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 5784 | bool ZeroInitialization(const Expr *E); |
| 5785 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 5786 | //===--------------------------------------------------------------------===// |
| 5787 | // Visitor Methods |
| 5788 | //===--------------------------------------------------------------------===// |
| 5789 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5790 | bool VisitImaginaryLiteral(const ImaginaryLiteral *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5791 | bool VisitCastExpr(const CastExpr *E); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5792 | bool VisitBinaryOperator(const BinaryOperator *E); |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 5793 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | 7ead5c7 | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 5794 | bool VisitInitListExpr(const InitListExpr *E); |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 5795 | }; |
| 5796 | } // end anonymous namespace |
| 5797 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5798 | static bool EvaluateComplex(const Expr *E, ComplexValue &Result, |
| 5799 | EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5800 | assert(E->isRValue() && E->getType()->isAnyComplexType()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5801 | return ComplexExprEvaluator(Info, Result).Visit(E); |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 5802 | } |
| 5803 | |
Eli Friedman | 7ead5c7 | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 5804 | bool ComplexExprEvaluator::ZeroInitialization(const Expr *E) { |
Eli Friedman | f6c17a4 | 2012-01-13 23:34:56 +0000 | [diff] [blame] | 5805 | QualType ElemTy = E->getType()->getAs<ComplexType>()->getElementType(); |
Eli Friedman | 7ead5c7 | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 5806 | if (ElemTy->isRealFloatingType()) { |
| 5807 | Result.makeComplexFloat(); |
| 5808 | APFloat Zero = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(ElemTy)); |
| 5809 | Result.FloatReal = Zero; |
| 5810 | Result.FloatImag = Zero; |
| 5811 | } else { |
| 5812 | Result.makeComplexInt(); |
| 5813 | APSInt Zero = Info.Ctx.MakeIntValue(0, ElemTy); |
| 5814 | Result.IntReal = Zero; |
| 5815 | Result.IntImag = Zero; |
| 5816 | } |
| 5817 | return true; |
| 5818 | } |
| 5819 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5820 | bool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) { |
| 5821 | const Expr* SubExpr = E->getSubExpr(); |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 5822 | |
| 5823 | if (SubExpr->getType()->isRealFloatingType()) { |
| 5824 | Result.makeComplexFloat(); |
| 5825 | APFloat &Imag = Result.FloatImag; |
| 5826 | if (!EvaluateFloat(SubExpr, Imag, Info)) |
| 5827 | return false; |
| 5828 | |
| 5829 | Result.FloatReal = APFloat(Imag.getSemantics()); |
| 5830 | return true; |
| 5831 | } else { |
| 5832 | assert(SubExpr->getType()->isIntegerType() && |
| 5833 | "Unexpected imaginary literal."); |
| 5834 | |
| 5835 | Result.makeComplexInt(); |
| 5836 | APSInt &Imag = Result.IntImag; |
| 5837 | if (!EvaluateInteger(SubExpr, Imag, Info)) |
| 5838 | return false; |
| 5839 | |
| 5840 | Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned()); |
| 5841 | return true; |
| 5842 | } |
| 5843 | } |
| 5844 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5845 | bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) { |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 5846 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5847 | switch (E->getCastKind()) { |
| 5848 | case CK_BitCast: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5849 | case CK_BaseToDerived: |
| 5850 | case CK_DerivedToBase: |
| 5851 | case CK_UncheckedDerivedToBase: |
| 5852 | case CK_Dynamic: |
| 5853 | case CK_ToUnion: |
| 5854 | case CK_ArrayToPointerDecay: |
| 5855 | case CK_FunctionToPointerDecay: |
| 5856 | case CK_NullToPointer: |
| 5857 | case CK_NullToMemberPointer: |
| 5858 | case CK_BaseToDerivedMemberPointer: |
| 5859 | case CK_DerivedToBaseMemberPointer: |
| 5860 | case CK_MemberPointerToBoolean: |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 5861 | case CK_ReinterpretMemberPointer: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5862 | case CK_ConstructorConversion: |
| 5863 | case CK_IntegralToPointer: |
| 5864 | case CK_PointerToIntegral: |
| 5865 | case CK_PointerToBoolean: |
| 5866 | case CK_ToVoid: |
| 5867 | case CK_VectorSplat: |
| 5868 | case CK_IntegralCast: |
| 5869 | case CK_IntegralToBoolean: |
| 5870 | case CK_IntegralToFloating: |
| 5871 | case CK_FloatingToIntegral: |
| 5872 | case CK_FloatingToBoolean: |
| 5873 | case CK_FloatingCast: |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5874 | case CK_CPointerToObjCPointerCast: |
| 5875 | case CK_BlockPointerToObjCPointerCast: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5876 | case CK_AnyPointerToBlockPointerCast: |
| 5877 | case CK_ObjCObjectLValueCast: |
| 5878 | case CK_FloatingComplexToReal: |
| 5879 | case CK_FloatingComplexToBoolean: |
| 5880 | case CK_IntegralComplexToReal: |
| 5881 | case CK_IntegralComplexToBoolean: |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 5882 | case CK_ARCProduceObject: |
| 5883 | case CK_ARCConsumeObject: |
| 5884 | case CK_ARCReclaimReturnedObject: |
| 5885 | case CK_ARCExtendBlockObject: |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 5886 | case CK_CopyAndAutoreleaseBlockObject: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5887 | llvm_unreachable("invalid cast kind for complex value"); |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 5888 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5889 | case CK_LValueToRValue: |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 5890 | case CK_AtomicToNonAtomic: |
| 5891 | case CK_NonAtomicToAtomic: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5892 | case CK_NoOp: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5893 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5894 | |
| 5895 | case CK_Dependent: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5896 | case CK_LValueBitCast: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5897 | case CK_UserDefinedConversion: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5898 | return Error(E); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5899 | |
| 5900 | case CK_FloatingRealToComplex: { |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 5901 | APFloat &Real = Result.FloatReal; |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5902 | if (!EvaluateFloat(E->getSubExpr(), Real, Info)) |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 5903 | return false; |
| 5904 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5905 | Result.makeComplexFloat(); |
| 5906 | Result.FloatImag = APFloat(Real.getSemantics()); |
| 5907 | return true; |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 5908 | } |
| 5909 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5910 | case CK_FloatingComplexCast: { |
| 5911 | if (!Visit(E->getSubExpr())) |
| 5912 | return false; |
| 5913 | |
| 5914 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 5915 | QualType From |
| 5916 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 5917 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5918 | return HandleFloatToFloatCast(Info, E, From, To, Result.FloatReal) && |
| 5919 | HandleFloatToFloatCast(Info, E, From, To, Result.FloatImag); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5920 | } |
| 5921 | |
| 5922 | case CK_FloatingComplexToIntegralComplex: { |
| 5923 | if (!Visit(E->getSubExpr())) |
| 5924 | return false; |
| 5925 | |
| 5926 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 5927 | QualType From |
| 5928 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 5929 | Result.makeComplexInt(); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5930 | return HandleFloatToIntCast(Info, E, From, Result.FloatReal, |
| 5931 | To, Result.IntReal) && |
| 5932 | HandleFloatToIntCast(Info, E, From, Result.FloatImag, |
| 5933 | To, Result.IntImag); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5934 | } |
| 5935 | |
| 5936 | case CK_IntegralRealToComplex: { |
| 5937 | APSInt &Real = Result.IntReal; |
| 5938 | if (!EvaluateInteger(E->getSubExpr(), Real, Info)) |
| 5939 | return false; |
| 5940 | |
| 5941 | Result.makeComplexInt(); |
| 5942 | Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned()); |
| 5943 | return true; |
| 5944 | } |
| 5945 | |
| 5946 | case CK_IntegralComplexCast: { |
| 5947 | if (!Visit(E->getSubExpr())) |
| 5948 | return false; |
| 5949 | |
| 5950 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 5951 | QualType From |
| 5952 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 5953 | |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 5954 | Result.IntReal = HandleIntToIntCast(Info, E, To, From, Result.IntReal); |
| 5955 | Result.IntImag = HandleIntToIntCast(Info, E, To, From, Result.IntImag); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5956 | return true; |
| 5957 | } |
| 5958 | |
| 5959 | case CK_IntegralComplexToFloatingComplex: { |
| 5960 | if (!Visit(E->getSubExpr())) |
| 5961 | return false; |
| 5962 | |
| 5963 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 5964 | QualType From |
| 5965 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 5966 | Result.makeComplexFloat(); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5967 | return HandleIntToFloatCast(Info, E, From, Result.IntReal, |
| 5968 | To, Result.FloatReal) && |
| 5969 | HandleIntToFloatCast(Info, E, From, Result.IntImag, |
| 5970 | To, Result.FloatImag); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5971 | } |
| 5972 | } |
| 5973 | |
| 5974 | llvm_unreachable("unknown cast resulting in complex value"); |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 5975 | } |
| 5976 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5977 | bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5978 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
Richard Smith | 2ad226b | 2011-11-16 17:22:48 +0000 | [diff] [blame] | 5979 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
| 5980 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5981 | bool LHSOK = Visit(E->getLHS()); |
| 5982 | if (!LHSOK && !Info.keepEvaluatingAfterFailure()) |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5983 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5984 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5985 | ComplexValue RHS; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5986 | if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK) |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5987 | return false; |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 5988 | |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 5989 | assert(Result.isComplexFloat() == RHS.isComplexFloat() && |
| 5990 | "Invalid operands to binary operator."); |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 5991 | switch (E->getOpcode()) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5992 | default: return Error(E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5993 | case BO_Add: |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 5994 | if (Result.isComplexFloat()) { |
| 5995 | Result.getComplexFloatReal().add(RHS.getComplexFloatReal(), |
| 5996 | APFloat::rmNearestTiesToEven); |
| 5997 | Result.getComplexFloatImag().add(RHS.getComplexFloatImag(), |
| 5998 | APFloat::rmNearestTiesToEven); |
| 5999 | } else { |
| 6000 | Result.getComplexIntReal() += RHS.getComplexIntReal(); |
| 6001 | Result.getComplexIntImag() += RHS.getComplexIntImag(); |
| 6002 | } |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 6003 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6004 | case BO_Sub: |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 6005 | if (Result.isComplexFloat()) { |
| 6006 | Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(), |
| 6007 | APFloat::rmNearestTiesToEven); |
| 6008 | Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(), |
| 6009 | APFloat::rmNearestTiesToEven); |
| 6010 | } else { |
| 6011 | Result.getComplexIntReal() -= RHS.getComplexIntReal(); |
| 6012 | Result.getComplexIntImag() -= RHS.getComplexIntImag(); |
| 6013 | } |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 6014 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6015 | case BO_Mul: |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 6016 | if (Result.isComplexFloat()) { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 6017 | ComplexValue LHS = Result; |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 6018 | APFloat &LHS_r = LHS.getComplexFloatReal(); |
| 6019 | APFloat &LHS_i = LHS.getComplexFloatImag(); |
| 6020 | APFloat &RHS_r = RHS.getComplexFloatReal(); |
| 6021 | APFloat &RHS_i = RHS.getComplexFloatImag(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6022 | |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 6023 | APFloat Tmp = LHS_r; |
| 6024 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 6025 | Result.getComplexFloatReal() = Tmp; |
| 6026 | Tmp = LHS_i; |
| 6027 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 6028 | Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven); |
| 6029 | |
| 6030 | Tmp = LHS_r; |
| 6031 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 6032 | Result.getComplexFloatImag() = Tmp; |
| 6033 | Tmp = LHS_i; |
| 6034 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 6035 | Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven); |
| 6036 | } else { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 6037 | ComplexValue LHS = Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6038 | Result.getComplexIntReal() = |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 6039 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() - |
| 6040 | LHS.getComplexIntImag() * RHS.getComplexIntImag()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6041 | Result.getComplexIntImag() = |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 6042 | (LHS.getComplexIntReal() * RHS.getComplexIntImag() + |
| 6043 | LHS.getComplexIntImag() * RHS.getComplexIntReal()); |
| 6044 | } |
| 6045 | break; |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 6046 | case BO_Div: |
| 6047 | if (Result.isComplexFloat()) { |
| 6048 | ComplexValue LHS = Result; |
| 6049 | APFloat &LHS_r = LHS.getComplexFloatReal(); |
| 6050 | APFloat &LHS_i = LHS.getComplexFloatImag(); |
| 6051 | APFloat &RHS_r = RHS.getComplexFloatReal(); |
| 6052 | APFloat &RHS_i = RHS.getComplexFloatImag(); |
| 6053 | APFloat &Res_r = Result.getComplexFloatReal(); |
| 6054 | APFloat &Res_i = Result.getComplexFloatImag(); |
| 6055 | |
| 6056 | APFloat Den = RHS_r; |
| 6057 | Den.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 6058 | APFloat Tmp = RHS_i; |
| 6059 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 6060 | Den.add(Tmp, APFloat::rmNearestTiesToEven); |
| 6061 | |
| 6062 | Res_r = LHS_r; |
| 6063 | Res_r.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 6064 | Tmp = LHS_i; |
| 6065 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 6066 | Res_r.add(Tmp, APFloat::rmNearestTiesToEven); |
| 6067 | Res_r.divide(Den, APFloat::rmNearestTiesToEven); |
| 6068 | |
| 6069 | Res_i = LHS_i; |
| 6070 | Res_i.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 6071 | Tmp = LHS_r; |
| 6072 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 6073 | Res_i.subtract(Tmp, APFloat::rmNearestTiesToEven); |
| 6074 | Res_i.divide(Den, APFloat::rmNearestTiesToEven); |
| 6075 | } else { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6076 | if (RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0) |
| 6077 | return Error(E, diag::note_expr_divide_by_zero); |
| 6078 | |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 6079 | ComplexValue LHS = Result; |
| 6080 | APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() + |
| 6081 | RHS.getComplexIntImag() * RHS.getComplexIntImag(); |
| 6082 | Result.getComplexIntReal() = |
| 6083 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() + |
| 6084 | LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den; |
| 6085 | Result.getComplexIntImag() = |
| 6086 | (LHS.getComplexIntImag() * RHS.getComplexIntReal() - |
| 6087 | LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den; |
| 6088 | } |
| 6089 | break; |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 6090 | } |
| 6091 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 6092 | return true; |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 6093 | } |
| 6094 | |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 6095 | bool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
| 6096 | // Get the operand value into 'Result'. |
| 6097 | if (!Visit(E->getSubExpr())) |
| 6098 | return false; |
| 6099 | |
| 6100 | switch (E->getOpcode()) { |
| 6101 | default: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6102 | return Error(E); |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 6103 | case UO_Extension: |
| 6104 | return true; |
| 6105 | case UO_Plus: |
| 6106 | // The result is always just the subexpr. |
| 6107 | return true; |
| 6108 | case UO_Minus: |
| 6109 | if (Result.isComplexFloat()) { |
| 6110 | Result.getComplexFloatReal().changeSign(); |
| 6111 | Result.getComplexFloatImag().changeSign(); |
| 6112 | } |
| 6113 | else { |
| 6114 | Result.getComplexIntReal() = -Result.getComplexIntReal(); |
| 6115 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
| 6116 | } |
| 6117 | return true; |
| 6118 | case UO_Not: |
| 6119 | if (Result.isComplexFloat()) |
| 6120 | Result.getComplexFloatImag().changeSign(); |
| 6121 | else |
| 6122 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
| 6123 | return true; |
| 6124 | } |
| 6125 | } |
| 6126 | |
Eli Friedman | 7ead5c7 | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 6127 | bool ComplexExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 6128 | if (E->getNumInits() == 2) { |
| 6129 | if (E->getType()->isComplexType()) { |
| 6130 | Result.makeComplexFloat(); |
| 6131 | if (!EvaluateFloat(E->getInit(0), Result.FloatReal, Info)) |
| 6132 | return false; |
| 6133 | if (!EvaluateFloat(E->getInit(1), Result.FloatImag, Info)) |
| 6134 | return false; |
| 6135 | } else { |
| 6136 | Result.makeComplexInt(); |
| 6137 | if (!EvaluateInteger(E->getInit(0), Result.IntReal, Info)) |
| 6138 | return false; |
| 6139 | if (!EvaluateInteger(E->getInit(1), Result.IntImag, Info)) |
| 6140 | return false; |
| 6141 | } |
| 6142 | return true; |
| 6143 | } |
| 6144 | return ExprEvaluatorBaseTy::VisitInitListExpr(E); |
| 6145 | } |
| 6146 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 6147 | //===----------------------------------------------------------------------===// |
Richard Smith | aa9c350 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 6148 | // Void expression evaluation, primarily for a cast to void on the LHS of a |
| 6149 | // comma operator |
| 6150 | //===----------------------------------------------------------------------===// |
| 6151 | |
| 6152 | namespace { |
| 6153 | class VoidExprEvaluator |
| 6154 | : public ExprEvaluatorBase<VoidExprEvaluator, bool> { |
| 6155 | public: |
| 6156 | VoidExprEvaluator(EvalInfo &Info) : ExprEvaluatorBaseTy(Info) {} |
| 6157 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6158 | bool Success(const APValue &V, const Expr *e) { return true; } |
Richard Smith | aa9c350 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 6159 | |
| 6160 | bool VisitCastExpr(const CastExpr *E) { |
| 6161 | switch (E->getCastKind()) { |
| 6162 | default: |
| 6163 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 6164 | case CK_ToVoid: |
| 6165 | VisitIgnoredValue(E->getSubExpr()); |
| 6166 | return true; |
| 6167 | } |
| 6168 | } |
| 6169 | }; |
| 6170 | } // end anonymous namespace |
| 6171 | |
| 6172 | static bool EvaluateVoid(const Expr *E, EvalInfo &Info) { |
| 6173 | assert(E->isRValue() && E->getType()->isVoidType()); |
| 6174 | return VoidExprEvaluator(Info).Visit(E); |
| 6175 | } |
| 6176 | |
| 6177 | //===----------------------------------------------------------------------===// |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 6178 | // Top level Expr::EvaluateAsRValue method. |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 6179 | //===----------------------------------------------------------------------===// |
| 6180 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6181 | static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6182 | // In C, function designators are not lvalues, but we evaluate them as if they |
| 6183 | // are. |
| 6184 | if (E->isGLValue() || E->getType()->isFunctionType()) { |
| 6185 | LValue LV; |
| 6186 | if (!EvaluateLValue(E, LV, Info)) |
| 6187 | return false; |
| 6188 | LV.moveInto(Result); |
| 6189 | } else if (E->getType()->isVectorType()) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 6190 | if (!EvaluateVector(E, Result, Info)) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6191 | return false; |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 6192 | } else if (E->getType()->isIntegralOrEnumerationType()) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 6193 | if (!IntExprEvaluator(Info, Result).Visit(E)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 6194 | return false; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 6195 | } else if (E->getType()->hasPointerRepresentation()) { |
| 6196 | LValue LV; |
| 6197 | if (!EvaluatePointer(E, LV, Info)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 6198 | return false; |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 6199 | LV.moveInto(Result); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 6200 | } else if (E->getType()->isRealFloatingType()) { |
| 6201 | llvm::APFloat F(0.0); |
| 6202 | if (!EvaluateFloat(E, F, Info)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 6203 | return false; |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6204 | Result = APValue(F); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 6205 | } else if (E->getType()->isAnyComplexType()) { |
| 6206 | ComplexValue C; |
| 6207 | if (!EvaluateComplex(E, C, Info)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 6208 | return false; |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 6209 | C.moveInto(Result); |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 6210 | } else if (E->getType()->isMemberPointerType()) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6211 | MemberPtr P; |
| 6212 | if (!EvaluateMemberPointer(E, P, Info)) |
| 6213 | return false; |
| 6214 | P.moveInto(Result); |
| 6215 | return true; |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6216 | } else if (E->getType()->isArrayType()) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6217 | LValue LV; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6218 | LV.set(E, Info.CurrentCall->Index); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6219 | if (!EvaluateArray(E, LV, Info.CurrentCall->Temporaries[E], Info)) |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 6220 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6221 | Result = Info.CurrentCall->Temporaries[E]; |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6222 | } else if (E->getType()->isRecordType()) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6223 | LValue LV; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6224 | LV.set(E, Info.CurrentCall->Index); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6225 | if (!EvaluateRecord(E, LV, Info.CurrentCall->Temporaries[E], Info)) |
| 6226 | return false; |
| 6227 | Result = Info.CurrentCall->Temporaries[E]; |
Richard Smith | aa9c350 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 6228 | } else if (E->getType()->isVoidType()) { |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 6229 | if (Info.getLangOpts().CPlusPlus0x) |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 6230 | Info.CCEDiag(E, diag::note_constexpr_nonliteral) |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 6231 | << E->getType(); |
| 6232 | else |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 6233 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | aa9c350 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 6234 | if (!EvaluateVoid(E, Info)) |
| 6235 | return false; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 6236 | } else if (Info.getLangOpts().CPlusPlus0x) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 6237 | Info.Diag(E, diag::note_constexpr_nonliteral) << E->getType(); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 6238 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6239 | } else { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 6240 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Anders Carlsson | 9d4c157 | 2008-11-22 22:56:32 +0000 | [diff] [blame] | 6241 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6242 | } |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 6243 | |
Anders Carlsson | 5b45d4e | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 6244 | return true; |
| 6245 | } |
| 6246 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6247 | /// EvaluateInPlace - Evaluate an expression in-place in an APValue. In some |
| 6248 | /// cases, the in-place evaluation is essential, since later initializers for |
| 6249 | /// an object can indirectly refer to subobjects which were initialized earlier. |
| 6250 | static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, const LValue &This, |
| 6251 | const Expr *E, CheckConstantExpressionKind CCEK, |
| 6252 | bool AllowNonLiteralTypes) { |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 6253 | if (!AllowNonLiteralTypes && !CheckLiteralType(Info, E)) |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6254 | return false; |
| 6255 | |
| 6256 | if (E->isRValue()) { |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 6257 | // Evaluate arrays and record types in-place, so that later initializers can |
| 6258 | // refer to earlier-initialized members of the object. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6259 | if (E->getType()->isArrayType()) |
| 6260 | return EvaluateArray(E, This, Result, Info); |
| 6261 | else if (E->getType()->isRecordType()) |
| 6262 | return EvaluateRecord(E, This, Result, Info); |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 6263 | } |
| 6264 | |
| 6265 | // For any other type, in-place evaluation is unimportant. |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6266 | return Evaluate(Result, Info, E); |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 6267 | } |
| 6268 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6269 | /// EvaluateAsRValue - Try to evaluate this expression, performing an implicit |
| 6270 | /// lvalue-to-rvalue cast if it is an lvalue. |
| 6271 | static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6272 | if (!CheckLiteralType(Info, E)) |
| 6273 | return false; |
| 6274 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6275 | if (!::Evaluate(Result, Info, E)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6276 | return false; |
| 6277 | |
| 6278 | if (E->isGLValue()) { |
| 6279 | LValue LV; |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6280 | LV.setFrom(Info.Ctx, Result); |
| 6281 | if (!HandleLValueToRValueConversion(Info, E, E->getType(), LV, Result)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6282 | return false; |
| 6283 | } |
| 6284 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6285 | // Check this core constant expression is a constant expression. |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6286 | return CheckConstantExpression(Info, E->getExprLoc(), E->getType(), Result); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6287 | } |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6288 | |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 6289 | /// EvaluateAsRValue - Return true if this is a constant which we can fold using |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6290 | /// any crazy technique (that has nothing to do with language standards) that |
| 6291 | /// we want to. If this function returns true, it returns the folded constant |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6292 | /// in Result. If this expression is a glvalue, an lvalue-to-rvalue conversion |
| 6293 | /// will be applied to the result. |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 6294 | bool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx) const { |
Richard Smith | ee19f43 | 2011-12-10 01:10:13 +0000 | [diff] [blame] | 6295 | // Fast-path evaluations of integer literals, since we sometimes see files |
| 6296 | // containing vast quantities of these. |
| 6297 | if (const IntegerLiteral *L = dyn_cast<IntegerLiteral>(this)) { |
| 6298 | Result.Val = APValue(APSInt(L->getValue(), |
| 6299 | L->getType()->isUnsignedIntegerType())); |
| 6300 | return true; |
| 6301 | } |
| 6302 | |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 6303 | // FIXME: Evaluating values of large array and record types can cause |
| 6304 | // performance problems. Only do so in C++11 for now. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6305 | if (isRValue() && (getType()->isArrayType() || getType()->isRecordType()) && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6306 | !Ctx.getLangOpts().CPlusPlus0x) |
Richard Smith | 1445bba | 2011-11-10 03:30:42 +0000 | [diff] [blame] | 6307 | return false; |
| 6308 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6309 | EvalInfo Info(Ctx, Result); |
| 6310 | return ::EvaluateAsRValue(Info, this, Result.Val); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6311 | } |
| 6312 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6313 | bool Expr::EvaluateAsBooleanCondition(bool &Result, |
| 6314 | const ASTContext &Ctx) const { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6315 | EvalResult Scratch; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 6316 | return EvaluateAsRValue(Scratch, Ctx) && |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6317 | HandleConversionToBool(Scratch.Val, Result); |
John McCall | cd7a445 | 2010-01-05 23:42:56 +0000 | [diff] [blame] | 6318 | } |
| 6319 | |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 6320 | bool Expr::EvaluateAsInt(APSInt &Result, const ASTContext &Ctx, |
| 6321 | SideEffectsKind AllowSideEffects) const { |
| 6322 | if (!getType()->isIntegralOrEnumerationType()) |
| 6323 | return false; |
| 6324 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6325 | EvalResult ExprResult; |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 6326 | if (!EvaluateAsRValue(ExprResult, Ctx) || !ExprResult.Val.isInt() || |
| 6327 | (!AllowSideEffects && ExprResult.HasSideEffects)) |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6328 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6329 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6330 | Result = ExprResult.Val.getInt(); |
| 6331 | return true; |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 6332 | } |
| 6333 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6334 | bool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx) const { |
Anders Carlsson | 1b78276 | 2009-04-10 04:54:13 +0000 | [diff] [blame] | 6335 | EvalInfo Info(Ctx, Result); |
| 6336 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 6337 | LValue LV; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6338 | if (!EvaluateLValue(this, LV, Info) || Result.HasSideEffects || |
| 6339 | !CheckLValueConstantExpression(Info, getExprLoc(), |
| 6340 | Ctx.getLValueReferenceType(getType()), LV)) |
| 6341 | return false; |
| 6342 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6343 | LV.moveInto(Result.Val); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6344 | return true; |
Eli Friedman | b2f295c | 2009-09-13 10:17:44 +0000 | [diff] [blame] | 6345 | } |
| 6346 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 6347 | bool Expr::EvaluateAsInitializer(APValue &Value, const ASTContext &Ctx, |
| 6348 | const VarDecl *VD, |
| 6349 | llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const { |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 6350 | // FIXME: Evaluating initializers for large array and record types can cause |
| 6351 | // performance problems. Only do so in C++11 for now. |
| 6352 | if (isRValue() && (getType()->isArrayType() || getType()->isRecordType()) && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6353 | !Ctx.getLangOpts().CPlusPlus0x) |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 6354 | return false; |
| 6355 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 6356 | Expr::EvalStatus EStatus; |
| 6357 | EStatus.Diag = &Notes; |
| 6358 | |
| 6359 | EvalInfo InitInfo(Ctx, EStatus); |
| 6360 | InitInfo.setEvaluatingDecl(VD, Value); |
| 6361 | |
| 6362 | LValue LVal; |
| 6363 | LVal.set(VD); |
| 6364 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6365 | // C++11 [basic.start.init]p2: |
| 6366 | // Variables with static storage duration or thread storage duration shall be |
| 6367 | // zero-initialized before any other initialization takes place. |
| 6368 | // This behavior is not present in C. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6369 | if (Ctx.getLangOpts().CPlusPlus && !VD->hasLocalStorage() && |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6370 | !VD->getType()->isReferenceType()) { |
| 6371 | ImplicitValueInitExpr VIE(VD->getType()); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6372 | if (!EvaluateInPlace(Value, InitInfo, LVal, &VIE, CCEK_Constant, |
| 6373 | /*AllowNonLiteralTypes=*/true)) |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6374 | return false; |
| 6375 | } |
| 6376 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6377 | if (!EvaluateInPlace(Value, InitInfo, LVal, this, CCEK_Constant, |
| 6378 | /*AllowNonLiteralTypes=*/true) || |
| 6379 | EStatus.HasSideEffects) |
| 6380 | return false; |
| 6381 | |
| 6382 | return CheckConstantExpression(InitInfo, VD->getLocation(), VD->getType(), |
| 6383 | Value); |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 6384 | } |
| 6385 | |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 6386 | /// isEvaluatable - Call EvaluateAsRValue to see if this expression can be |
| 6387 | /// constant folded, but discard the result. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6388 | bool Expr::isEvaluatable(const ASTContext &Ctx) const { |
Anders Carlsson | 4fdfb09 | 2008-12-01 06:44:05 +0000 | [diff] [blame] | 6389 | EvalResult Result; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 6390 | return EvaluateAsRValue(Result, Ctx) && !Result.HasSideEffects; |
Chris Lattner | 45b6b9d | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 6391 | } |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 6392 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6393 | bool Expr::HasSideEffects(const ASTContext &Ctx) const { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 6394 | return HasSideEffect(Ctx).Visit(this); |
Fariborz Jahanian | 393c247 | 2009-11-05 18:03:03 +0000 | [diff] [blame] | 6395 | } |
| 6396 | |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 6397 | APSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx) const { |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 6398 | EvalResult EvalResult; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 6399 | bool Result = EvaluateAsRValue(EvalResult, Ctx); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 6400 | (void)Result; |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 6401 | assert(Result && "Could not evaluate expression"); |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 6402 | assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer"); |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 6403 | |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 6404 | return EvalResult.Val.getInt(); |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 6405 | } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6406 | |
Abramo Bagnara | e17a643 | 2010-05-14 17:07:14 +0000 | [diff] [blame] | 6407 | bool Expr::EvalResult::isGlobalLValue() const { |
| 6408 | assert(Val.isLValue()); |
| 6409 | return IsGlobalLValue(Val.getLValueBase()); |
| 6410 | } |
| 6411 | |
| 6412 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6413 | /// isIntegerConstantExpr - this recursive routine will test if an expression is |
| 6414 | /// an integer constant expression. |
| 6415 | |
| 6416 | /// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, |
| 6417 | /// comma, etc |
| 6418 | /// |
| 6419 | /// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof |
| 6420 | /// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer |
| 6421 | /// cast+dereference. |
| 6422 | |
| 6423 | // CheckICE - This function does the fundamental ICE checking: the returned |
| 6424 | // ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation. |
| 6425 | // Note that to reduce code duplication, this helper does no evaluation |
| 6426 | // itself; the caller checks whether the expression is evaluatable, and |
| 6427 | // in the rare cases where CheckICE actually cares about the evaluated |
| 6428 | // value, it calls into Evalute. |
| 6429 | // |
| 6430 | // Meanings of Val: |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 6431 | // 0: This expression is an ICE. |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6432 | // 1: This expression is not an ICE, but if it isn't evaluated, it's |
| 6433 | // a legal subexpression for an ICE. This return value is used to handle |
| 6434 | // the comma operator in C99 mode. |
| 6435 | // 2: This expression is not an ICE, and is not a legal subexpression for one. |
| 6436 | |
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 6437 | namespace { |
| 6438 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6439 | struct ICEDiag { |
| 6440 | unsigned Val; |
| 6441 | SourceLocation Loc; |
| 6442 | |
| 6443 | public: |
| 6444 | ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {} |
| 6445 | ICEDiag() : Val(0) {} |
| 6446 | }; |
| 6447 | |
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 6448 | } |
| 6449 | |
| 6450 | static ICEDiag NoDiag() { return ICEDiag(); } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6451 | |
| 6452 | static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) { |
| 6453 | Expr::EvalResult EVResult; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 6454 | if (!E->EvaluateAsRValue(EVResult, Ctx) || EVResult.HasSideEffects || |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6455 | !EVResult.Val.isInt()) { |
| 6456 | return ICEDiag(2, E->getLocStart()); |
| 6457 | } |
| 6458 | return NoDiag(); |
| 6459 | } |
| 6460 | |
| 6461 | static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) { |
| 6462 | assert(!E->isValueDependent() && "Should not see value dependent exprs!"); |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 6463 | if (!E->getType()->isIntegralOrEnumerationType()) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6464 | return ICEDiag(2, E->getLocStart()); |
| 6465 | } |
| 6466 | |
| 6467 | switch (E->getStmtClass()) { |
John McCall | 63c00d7 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 6468 | #define ABSTRACT_STMT(Node) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6469 | #define STMT(Node, Base) case Expr::Node##Class: |
| 6470 | #define EXPR(Node, Base) |
| 6471 | #include "clang/AST/StmtNodes.inc" |
| 6472 | case Expr::PredefinedExprClass: |
| 6473 | case Expr::FloatingLiteralClass: |
| 6474 | case Expr::ImaginaryLiteralClass: |
| 6475 | case Expr::StringLiteralClass: |
| 6476 | case Expr::ArraySubscriptExprClass: |
| 6477 | case Expr::MemberExprClass: |
| 6478 | case Expr::CompoundAssignOperatorClass: |
| 6479 | case Expr::CompoundLiteralExprClass: |
| 6480 | case Expr::ExtVectorElementExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6481 | case Expr::DesignatedInitExprClass: |
| 6482 | case Expr::ImplicitValueInitExprClass: |
| 6483 | case Expr::ParenListExprClass: |
| 6484 | case Expr::VAArgExprClass: |
| 6485 | case Expr::AddrLabelExprClass: |
| 6486 | case Expr::StmtExprClass: |
| 6487 | case Expr::CXXMemberCallExprClass: |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 6488 | case Expr::CUDAKernelCallExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6489 | case Expr::CXXDynamicCastExprClass: |
| 6490 | case Expr::CXXTypeidExprClass: |
Francois Pichet | 9be8840 | 2010-09-08 23:47:05 +0000 | [diff] [blame] | 6491 | case Expr::CXXUuidofExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6492 | case Expr::CXXNullPtrLiteralExprClass: |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 6493 | case Expr::UserDefinedLiteralClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6494 | case Expr::CXXThisExprClass: |
| 6495 | case Expr::CXXThrowExprClass: |
| 6496 | case Expr::CXXNewExprClass: |
| 6497 | case Expr::CXXDeleteExprClass: |
| 6498 | case Expr::CXXPseudoDestructorExprClass: |
| 6499 | case Expr::UnresolvedLookupExprClass: |
| 6500 | case Expr::DependentScopeDeclRefExprClass: |
| 6501 | case Expr::CXXConstructExprClass: |
| 6502 | case Expr::CXXBindTemporaryExprClass: |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 6503 | case Expr::ExprWithCleanupsClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6504 | case Expr::CXXTemporaryObjectExprClass: |
| 6505 | case Expr::CXXUnresolvedConstructExprClass: |
| 6506 | case Expr::CXXDependentScopeMemberExprClass: |
| 6507 | case Expr::UnresolvedMemberExprClass: |
| 6508 | case Expr::ObjCStringLiteralClass: |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 6509 | case Expr::ObjCBoxedExprClass: |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 6510 | case Expr::ObjCArrayLiteralClass: |
| 6511 | case Expr::ObjCDictionaryLiteralClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6512 | case Expr::ObjCEncodeExprClass: |
| 6513 | case Expr::ObjCMessageExprClass: |
| 6514 | case Expr::ObjCSelectorExprClass: |
| 6515 | case Expr::ObjCProtocolExprClass: |
| 6516 | case Expr::ObjCIvarRefExprClass: |
| 6517 | case Expr::ObjCPropertyRefExprClass: |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 6518 | case Expr::ObjCSubscriptRefExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6519 | case Expr::ObjCIsaExprClass: |
| 6520 | case Expr::ShuffleVectorExprClass: |
| 6521 | case Expr::BlockExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6522 | case Expr::NoStmtClass: |
John McCall | 7cd7d1a | 2010-11-15 23:31:06 +0000 | [diff] [blame] | 6523 | case Expr::OpaqueValueExprClass: |
Douglas Gregor | be230c3 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 6524 | case Expr::PackExpansionExprClass: |
Douglas Gregor | c7793c7 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 6525 | case Expr::SubstNonTypeTemplateParmPackExprClass: |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 6526 | case Expr::AsTypeExprClass: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6527 | case Expr::ObjCIndirectCopyRestoreExprClass: |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 6528 | case Expr::MaterializeTemporaryExprClass: |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 6529 | case Expr::PseudoObjectExprClass: |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 6530 | case Expr::AtomicExprClass: |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 6531 | case Expr::InitListExprClass: |
Douglas Gregor | 01d0801 | 2012-02-07 10:09:13 +0000 | [diff] [blame] | 6532 | case Expr::LambdaExprClass: |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 6533 | return ICEDiag(2, E->getLocStart()); |
| 6534 | |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 6535 | case Expr::SizeOfPackExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6536 | case Expr::GNUNullExprClass: |
| 6537 | // GCC considers the GNU __null value to be an integral constant expression. |
| 6538 | return NoDiag(); |
| 6539 | |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 6540 | case Expr::SubstNonTypeTemplateParmExprClass: |
| 6541 | return |
| 6542 | CheckICE(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(), Ctx); |
| 6543 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6544 | case Expr::ParenExprClass: |
| 6545 | return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx); |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 6546 | case Expr::GenericSelectionExprClass: |
| 6547 | return CheckICE(cast<GenericSelectionExpr>(E)->getResultExpr(), Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6548 | case Expr::IntegerLiteralClass: |
| 6549 | case Expr::CharacterLiteralClass: |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 6550 | case Expr::ObjCBoolLiteralExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6551 | case Expr::CXXBoolLiteralExprClass: |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 6552 | case Expr::CXXScalarValueInitExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6553 | case Expr::UnaryTypeTraitExprClass: |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 6554 | case Expr::BinaryTypeTraitExprClass: |
Douglas Gregor | 4ca8ac2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 6555 | case Expr::TypeTraitExprClass: |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 6556 | case Expr::ArrayTypeTraitExprClass: |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 6557 | case Expr::ExpressionTraitExprClass: |
Sebastian Redl | 2e15622 | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 6558 | case Expr::CXXNoexceptExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6559 | return NoDiag(); |
| 6560 | case Expr::CallExprClass: |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 6561 | case Expr::CXXOperatorCallExprClass: { |
Richard Smith | 0583014 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 6562 | // C99 6.6/3 allows function calls within unevaluated subexpressions of |
| 6563 | // constant expressions, but they can never be ICEs because an ICE cannot |
| 6564 | // contain an operand of (pointer to) function type. |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6565 | const CallExpr *CE = cast<CallExpr>(E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6566 | if (CE->isBuiltinCall()) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6567 | return CheckEvalInICE(E, Ctx); |
| 6568 | return ICEDiag(2, E->getLocStart()); |
| 6569 | } |
Richard Smith | 359c89d | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 6570 | case Expr::DeclRefExprClass: { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6571 | if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl())) |
| 6572 | return NoDiag(); |
Richard Smith | 359c89d | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 6573 | const ValueDecl *D = dyn_cast<ValueDecl>(cast<DeclRefExpr>(E)->getDecl()); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6574 | if (Ctx.getLangOpts().CPlusPlus && |
Richard Smith | 359c89d | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 6575 | D && IsConstNonVolatile(D->getType())) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6576 | // Parameter variables are never constants. Without this check, |
| 6577 | // getAnyInitializer() can find a default argument, which leads |
| 6578 | // to chaos. |
| 6579 | if (isa<ParmVarDecl>(D)) |
| 6580 | return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); |
| 6581 | |
| 6582 | // C++ 7.1.5.1p2 |
| 6583 | // A variable of non-volatile const-qualified integral or enumeration |
| 6584 | // type initialized by an ICE can be used in ICEs. |
| 6585 | if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) { |
Richard Smith | db1822c | 2011-11-08 01:31:09 +0000 | [diff] [blame] | 6586 | if (!Dcl->getType()->isIntegralOrEnumerationType()) |
| 6587 | return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); |
| 6588 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 6589 | const VarDecl *VD; |
| 6590 | // Look for a declaration of this variable that has an initializer, and |
| 6591 | // check whether it is an ICE. |
| 6592 | if (Dcl->getAnyInitializer(VD) && VD->checkInitIsICE()) |
| 6593 | return NoDiag(); |
| 6594 | else |
| 6595 | return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6596 | } |
| 6597 | } |
| 6598 | return ICEDiag(2, E->getLocStart()); |
Richard Smith | 359c89d | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 6599 | } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6600 | case Expr::UnaryOperatorClass: { |
| 6601 | const UnaryOperator *Exp = cast<UnaryOperator>(E); |
| 6602 | switch (Exp->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6603 | case UO_PostInc: |
| 6604 | case UO_PostDec: |
| 6605 | case UO_PreInc: |
| 6606 | case UO_PreDec: |
| 6607 | case UO_AddrOf: |
| 6608 | case UO_Deref: |
Richard Smith | 0583014 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 6609 | // C99 6.6/3 allows increment and decrement within unevaluated |
| 6610 | // subexpressions of constant expressions, but they can never be ICEs |
| 6611 | // because an ICE cannot contain an lvalue operand. |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6612 | return ICEDiag(2, E->getLocStart()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6613 | case UO_Extension: |
| 6614 | case UO_LNot: |
| 6615 | case UO_Plus: |
| 6616 | case UO_Minus: |
| 6617 | case UO_Not: |
| 6618 | case UO_Real: |
| 6619 | case UO_Imag: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6620 | return CheckICE(Exp->getSubExpr(), Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6621 | } |
| 6622 | |
| 6623 | // OffsetOf falls through here. |
| 6624 | } |
| 6625 | case Expr::OffsetOfExprClass: { |
| 6626 | // Note that per C99, offsetof must be an ICE. And AFAIK, using |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 6627 | // EvaluateAsRValue matches the proposed gcc behavior for cases like |
Richard Smith | 0583014 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 6628 | // "offsetof(struct s{int x[4];}, x[1.0])". This doesn't affect |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6629 | // compliance: we should warn earlier for offsetof expressions with |
| 6630 | // array subscripts that aren't ICEs, and if the array subscripts |
| 6631 | // are ICEs, the value of the offsetof must be an integer constant. |
| 6632 | return CheckEvalInICE(E, Ctx); |
| 6633 | } |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 6634 | case Expr::UnaryExprOrTypeTraitExprClass: { |
| 6635 | const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(E); |
| 6636 | if ((Exp->getKind() == UETT_SizeOf) && |
| 6637 | Exp->getTypeOfArgument()->isVariableArrayType()) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6638 | return ICEDiag(2, E->getLocStart()); |
| 6639 | return NoDiag(); |
| 6640 | } |
| 6641 | case Expr::BinaryOperatorClass: { |
| 6642 | const BinaryOperator *Exp = cast<BinaryOperator>(E); |
| 6643 | switch (Exp->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6644 | case BO_PtrMemD: |
| 6645 | case BO_PtrMemI: |
| 6646 | case BO_Assign: |
| 6647 | case BO_MulAssign: |
| 6648 | case BO_DivAssign: |
| 6649 | case BO_RemAssign: |
| 6650 | case BO_AddAssign: |
| 6651 | case BO_SubAssign: |
| 6652 | case BO_ShlAssign: |
| 6653 | case BO_ShrAssign: |
| 6654 | case BO_AndAssign: |
| 6655 | case BO_XorAssign: |
| 6656 | case BO_OrAssign: |
Richard Smith | 0583014 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 6657 | // C99 6.6/3 allows assignments within unevaluated subexpressions of |
| 6658 | // constant expressions, but they can never be ICEs because an ICE cannot |
| 6659 | // contain an lvalue operand. |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6660 | return ICEDiag(2, E->getLocStart()); |
| 6661 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6662 | case BO_Mul: |
| 6663 | case BO_Div: |
| 6664 | case BO_Rem: |
| 6665 | case BO_Add: |
| 6666 | case BO_Sub: |
| 6667 | case BO_Shl: |
| 6668 | case BO_Shr: |
| 6669 | case BO_LT: |
| 6670 | case BO_GT: |
| 6671 | case BO_LE: |
| 6672 | case BO_GE: |
| 6673 | case BO_EQ: |
| 6674 | case BO_NE: |
| 6675 | case BO_And: |
| 6676 | case BO_Xor: |
| 6677 | case BO_Or: |
| 6678 | case BO_Comma: { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6679 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 6680 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6681 | if (Exp->getOpcode() == BO_Div || |
| 6682 | Exp->getOpcode() == BO_Rem) { |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 6683 | // EvaluateAsRValue gives an error for undefined Div/Rem, so make sure |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6684 | // we don't evaluate one. |
John McCall | 3b332ab | 2011-02-26 08:27:17 +0000 | [diff] [blame] | 6685 | if (LHSResult.Val == 0 && RHSResult.Val == 0) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 6686 | llvm::APSInt REval = Exp->getRHS()->EvaluateKnownConstInt(Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6687 | if (REval == 0) |
| 6688 | return ICEDiag(1, E->getLocStart()); |
| 6689 | if (REval.isSigned() && REval.isAllOnesValue()) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 6690 | llvm::APSInt LEval = Exp->getLHS()->EvaluateKnownConstInt(Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6691 | if (LEval.isMinSignedValue()) |
| 6692 | return ICEDiag(1, E->getLocStart()); |
| 6693 | } |
| 6694 | } |
| 6695 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6696 | if (Exp->getOpcode() == BO_Comma) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6697 | if (Ctx.getLangOpts().C99) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6698 | // C99 6.6p3 introduces a strange edge case: comma can be in an ICE |
| 6699 | // if it isn't evaluated. |
| 6700 | if (LHSResult.Val == 0 && RHSResult.Val == 0) |
| 6701 | return ICEDiag(1, E->getLocStart()); |
| 6702 | } else { |
| 6703 | // In both C89 and C++, commas in ICEs are illegal. |
| 6704 | return ICEDiag(2, E->getLocStart()); |
| 6705 | } |
| 6706 | } |
| 6707 | if (LHSResult.Val >= RHSResult.Val) |
| 6708 | return LHSResult; |
| 6709 | return RHSResult; |
| 6710 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6711 | case BO_LAnd: |
| 6712 | case BO_LOr: { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6713 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 6714 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
| 6715 | if (LHSResult.Val == 0 && RHSResult.Val == 1) { |
| 6716 | // Rare case where the RHS has a comma "side-effect"; we need |
| 6717 | // to actually check the condition to see whether the side |
| 6718 | // with the comma is evaluated. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6719 | if ((Exp->getOpcode() == BO_LAnd) != |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 6720 | (Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0)) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6721 | return RHSResult; |
| 6722 | return NoDiag(); |
| 6723 | } |
| 6724 | |
| 6725 | if (LHSResult.Val >= RHSResult.Val) |
| 6726 | return LHSResult; |
| 6727 | return RHSResult; |
| 6728 | } |
| 6729 | } |
| 6730 | } |
| 6731 | case Expr::ImplicitCastExprClass: |
| 6732 | case Expr::CStyleCastExprClass: |
| 6733 | case Expr::CXXFunctionalCastExprClass: |
| 6734 | case Expr::CXXStaticCastExprClass: |
| 6735 | case Expr::CXXReinterpretCastExprClass: |
Richard Smith | 32cb471 | 2011-10-24 18:26:35 +0000 | [diff] [blame] | 6736 | case Expr::CXXConstCastExprClass: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6737 | case Expr::ObjCBridgedCastExprClass: { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6738 | const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr(); |
Richard Smith | 2116b14 | 2011-12-18 02:33:09 +0000 | [diff] [blame] | 6739 | if (isa<ExplicitCastExpr>(E)) { |
| 6740 | if (const FloatingLiteral *FL |
| 6741 | = dyn_cast<FloatingLiteral>(SubExpr->IgnoreParenImpCasts())) { |
| 6742 | unsigned DestWidth = Ctx.getIntWidth(E->getType()); |
| 6743 | bool DestSigned = E->getType()->isSignedIntegerOrEnumerationType(); |
| 6744 | APSInt IgnoredVal(DestWidth, !DestSigned); |
| 6745 | bool Ignored; |
| 6746 | // If the value does not fit in the destination type, the behavior is |
| 6747 | // undefined, so we are not required to treat it as a constant |
| 6748 | // expression. |
| 6749 | if (FL->getValue().convertToInteger(IgnoredVal, |
| 6750 | llvm::APFloat::rmTowardZero, |
| 6751 | &Ignored) & APFloat::opInvalidOp) |
| 6752 | return ICEDiag(2, E->getLocStart()); |
| 6753 | return NoDiag(); |
| 6754 | } |
| 6755 | } |
Eli Friedman | eea0e81 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 6756 | switch (cast<CastExpr>(E)->getCastKind()) { |
| 6757 | case CK_LValueToRValue: |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 6758 | case CK_AtomicToNonAtomic: |
| 6759 | case CK_NonAtomicToAtomic: |
Eli Friedman | eea0e81 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 6760 | case CK_NoOp: |
| 6761 | case CK_IntegralToBoolean: |
| 6762 | case CK_IntegralCast: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6763 | return CheckICE(SubExpr, Ctx); |
Eli Friedman | eea0e81 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 6764 | default: |
Eli Friedman | eea0e81 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 6765 | return ICEDiag(2, E->getLocStart()); |
| 6766 | } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6767 | } |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6768 | case Expr::BinaryConditionalOperatorClass: { |
| 6769 | const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(E); |
| 6770 | ICEDiag CommonResult = CheckICE(Exp->getCommon(), Ctx); |
| 6771 | if (CommonResult.Val == 2) return CommonResult; |
| 6772 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
| 6773 | if (FalseResult.Val == 2) return FalseResult; |
| 6774 | if (CommonResult.Val == 1) return CommonResult; |
| 6775 | if (FalseResult.Val == 1 && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 6776 | Exp->getCommon()->EvaluateKnownConstInt(Ctx) == 0) return NoDiag(); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6777 | return FalseResult; |
| 6778 | } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6779 | case Expr::ConditionalOperatorClass: { |
| 6780 | const ConditionalOperator *Exp = cast<ConditionalOperator>(E); |
| 6781 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
| 6782 | // then only the true side is actually considered in an integer constant |
| 6783 | // expression, and it is fully evaluated. This is an important GNU |
| 6784 | // extension. See GCC PR38377 for discussion. |
| 6785 | if (const CallExpr *CallCE |
| 6786 | = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts())) |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 6787 | if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p) |
| 6788 | return CheckEvalInICE(E, Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6789 | ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6790 | if (CondResult.Val == 2) |
| 6791 | return CondResult; |
Douglas Gregor | 63fe681 | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 6792 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6793 | ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx); |
| 6794 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
Douglas Gregor | 63fe681 | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 6795 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6796 | if (TrueResult.Val == 2) |
| 6797 | return TrueResult; |
| 6798 | if (FalseResult.Val == 2) |
| 6799 | return FalseResult; |
| 6800 | if (CondResult.Val == 1) |
| 6801 | return CondResult; |
| 6802 | if (TrueResult.Val == 0 && FalseResult.Val == 0) |
| 6803 | return NoDiag(); |
| 6804 | // Rare case where the diagnostics depend on which side is evaluated |
| 6805 | // Note that if we get here, CondResult is 0, and at least one of |
| 6806 | // TrueResult and FalseResult is non-zero. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 6807 | if (Exp->getCond()->EvaluateKnownConstInt(Ctx) == 0) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6808 | return FalseResult; |
| 6809 | } |
| 6810 | return TrueResult; |
| 6811 | } |
| 6812 | case Expr::CXXDefaultArgExprClass: |
| 6813 | return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx); |
| 6814 | case Expr::ChooseExprClass: { |
| 6815 | return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx); |
| 6816 | } |
| 6817 | } |
| 6818 | |
David Blaikie | 3026348 | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 6819 | llvm_unreachable("Invalid StmtClass!"); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6820 | } |
| 6821 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6822 | /// Evaluate an expression as a C++11 integral constant expression. |
| 6823 | static bool EvaluateCPlusPlus11IntegralConstantExpr(ASTContext &Ctx, |
| 6824 | const Expr *E, |
| 6825 | llvm::APSInt *Value, |
| 6826 | SourceLocation *Loc) { |
| 6827 | if (!E->getType()->isIntegralOrEnumerationType()) { |
| 6828 | if (Loc) *Loc = E->getExprLoc(); |
| 6829 | return false; |
| 6830 | } |
| 6831 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6832 | APValue Result; |
| 6833 | if (!E->isCXX11ConstantExpr(Ctx, &Result, Loc)) |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 6834 | return false; |
| 6835 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6836 | assert(Result.isInt() && "pointer cast to int is not an ICE"); |
| 6837 | if (Value) *Value = Result.getInt(); |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 6838 | return true; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6839 | } |
| 6840 | |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 6841 | bool Expr::isIntegerConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6842 | if (Ctx.getLangOpts().CPlusPlus0x) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6843 | return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, 0, Loc); |
| 6844 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6845 | ICEDiag d = CheckICE(this, Ctx); |
| 6846 | if (d.Val != 0) { |
| 6847 | if (Loc) *Loc = d.Loc; |
| 6848 | return false; |
| 6849 | } |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6850 | return true; |
| 6851 | } |
| 6852 | |
| 6853 | bool Expr::isIntegerConstantExpr(llvm::APSInt &Value, ASTContext &Ctx, |
| 6854 | SourceLocation *Loc, bool isEvaluated) const { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6855 | if (Ctx.getLangOpts().CPlusPlus0x) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6856 | return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, &Value, Loc); |
| 6857 | |
| 6858 | if (!isIntegerConstantExpr(Ctx, Loc)) |
| 6859 | return false; |
| 6860 | if (!EvaluateAsInt(Value, Ctx)) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6861 | llvm_unreachable("ICE cannot be evaluated!"); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6862 | return true; |
| 6863 | } |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6864 | |
Richard Smith | 70488e2 | 2012-02-14 21:38:30 +0000 | [diff] [blame] | 6865 | bool Expr::isCXX98IntegralConstantExpr(ASTContext &Ctx) const { |
| 6866 | return CheckICE(this, Ctx).Val == 0; |
| 6867 | } |
| 6868 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6869 | bool Expr::isCXX11ConstantExpr(ASTContext &Ctx, APValue *Result, |
| 6870 | SourceLocation *Loc) const { |
| 6871 | // We support this checking in C++98 mode in order to diagnose compatibility |
| 6872 | // issues. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6873 | assert(Ctx.getLangOpts().CPlusPlus); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6874 | |
Richard Smith | 70488e2 | 2012-02-14 21:38:30 +0000 | [diff] [blame] | 6875 | // Build evaluation settings. |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6876 | Expr::EvalStatus Status; |
| 6877 | llvm::SmallVector<PartialDiagnosticAt, 8> Diags; |
| 6878 | Status.Diag = &Diags; |
| 6879 | EvalInfo Info(Ctx, Status); |
| 6880 | |
| 6881 | APValue Scratch; |
| 6882 | bool IsConstExpr = ::EvaluateAsRValue(Info, this, Result ? *Result : Scratch); |
| 6883 | |
| 6884 | if (!Diags.empty()) { |
| 6885 | IsConstExpr = false; |
| 6886 | if (Loc) *Loc = Diags[0].first; |
| 6887 | } else if (!IsConstExpr) { |
| 6888 | // FIXME: This shouldn't happen. |
| 6889 | if (Loc) *Loc = getExprLoc(); |
| 6890 | } |
| 6891 | |
| 6892 | return IsConstExpr; |
| 6893 | } |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6894 | |
| 6895 | bool Expr::isPotentialConstantExpr(const FunctionDecl *FD, |
| 6896 | llvm::SmallVectorImpl< |
| 6897 | PartialDiagnosticAt> &Diags) { |
| 6898 | // FIXME: It would be useful to check constexpr function templates, but at the |
| 6899 | // moment the constant expression evaluator cannot cope with the non-rigorous |
| 6900 | // ASTs which we build for dependent expressions. |
| 6901 | if (FD->isDependentContext()) |
| 6902 | return true; |
| 6903 | |
| 6904 | Expr::EvalStatus Status; |
| 6905 | Status.Diag = &Diags; |
| 6906 | |
| 6907 | EvalInfo Info(FD->getASTContext(), Status); |
| 6908 | Info.CheckingPotentialConstantExpression = true; |
| 6909 | |
| 6910 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
| 6911 | const CXXRecordDecl *RD = MD ? MD->getParent()->getCanonicalDecl() : 0; |
| 6912 | |
| 6913 | // FIXME: Fabricate an arbitrary expression on the stack and pretend that it |
| 6914 | // is a temporary being used as the 'this' pointer. |
| 6915 | LValue This; |
| 6916 | ImplicitValueInitExpr VIE(RD ? Info.Ctx.getRecordType(RD) : Info.Ctx.IntTy); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6917 | This.set(&VIE, Info.CurrentCall->Index); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6918 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6919 | ArrayRef<const Expr*> Args; |
| 6920 | |
| 6921 | SourceLocation Loc = FD->getLocation(); |
| 6922 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6923 | APValue Scratch; |
| 6924 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6925 | HandleConstructorCall(Loc, This, Args, CD, Info, Scratch); |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6926 | else |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6927 | HandleFunctionCall(Loc, FD, (MD && MD->isInstance()) ? &This : 0, |
| 6928 | Args, FD->getBody(), Info, Scratch); |
| 6929 | |
| 6930 | return Diags.empty(); |
| 6931 | } |