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" |
Benjamin Kramer | a93d0f2 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 38 | #include "clang/AST/ASTDiagnostic.h" |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 39 | #include "clang/AST/CharUnits.h" |
Benjamin Kramer | a93d0f2 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 40 | #include "clang/AST/Expr.h" |
Anders Carlsson | 19cc4ab | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 41 | #include "clang/AST/RecordLayout.h" |
Seo Sanghyeon | 0fe52e1 | 2008-07-08 07:23:12 +0000 | [diff] [blame] | 42 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 43 | #include "clang/AST/TypeLoc.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" |
Benjamin Kramer | a93d0f2 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 47 | #include "llvm/Support/raw_ostream.h" |
Mike Stump | 4572bab | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 48 | #include <cstring> |
Richard Smith | 7b48a29 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 49 | #include <functional> |
Mike Stump | 4572bab | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 50 | |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 51 | using namespace clang; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 52 | using llvm::APSInt; |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 53 | using llvm::APFloat; |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 54 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 55 | static bool IsGlobalLValue(APValue::LValueBase B); |
| 56 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 57 | namespace { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 58 | struct LValue; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 59 | struct CallStackFrame; |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 60 | struct EvalInfo; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 61 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 62 | static QualType getType(APValue::LValueBase B) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 63 | if (!B) return QualType(); |
| 64 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) |
| 65 | return D->getType(); |
| 66 | return B.get<const Expr*>()->getType(); |
| 67 | } |
| 68 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 69 | /// 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] | 70 | /// field or base class. |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 71 | static |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 72 | APValue::BaseOrMemberType getAsBaseOrMember(APValue::LValuePathEntry E) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 73 | APValue::BaseOrMemberType Value; |
| 74 | Value.setFromOpaqueValue(E.BaseOrMember); |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 75 | return Value; |
| 76 | } |
| 77 | |
| 78 | /// Get an LValue path entry, which is known to not be an array index, as a |
| 79 | /// field declaration. |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 80 | static const FieldDecl *getAsField(APValue::LValuePathEntry E) { |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 81 | return dyn_cast<FieldDecl>(getAsBaseOrMember(E).getPointer()); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 82 | } |
| 83 | /// Get an LValue path entry, which is known to not be an array index, as a |
| 84 | /// base class declaration. |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 85 | static const CXXRecordDecl *getAsBaseClass(APValue::LValuePathEntry E) { |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 86 | return dyn_cast<CXXRecordDecl>(getAsBaseOrMember(E).getPointer()); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 87 | } |
| 88 | /// Determine whether this LValue path entry for a base class names a virtual |
| 89 | /// base class. |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 90 | static bool isVirtualBaseClass(APValue::LValuePathEntry E) { |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 91 | return getAsBaseOrMember(E).getInt(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 94 | /// Find the path length and type of the most-derived subobject in the given |
| 95 | /// path, and find the size of the containing array, if any. |
| 96 | static |
| 97 | unsigned findMostDerivedSubobject(ASTContext &Ctx, QualType Base, |
| 98 | ArrayRef<APValue::LValuePathEntry> Path, |
| 99 | uint64_t &ArraySize, QualType &Type) { |
| 100 | unsigned MostDerivedLength = 0; |
| 101 | Type = Base; |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 102 | for (unsigned I = 0, N = Path.size(); I != N; ++I) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 103 | if (Type->isArrayType()) { |
| 104 | const ConstantArrayType *CAT = |
| 105 | cast<ConstantArrayType>(Ctx.getAsArrayType(Type)); |
| 106 | Type = CAT->getElementType(); |
| 107 | ArraySize = CAT->getSize().getZExtValue(); |
| 108 | MostDerivedLength = I + 1; |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 109 | } else if (Type->isAnyComplexType()) { |
| 110 | const ComplexType *CT = Type->castAs<ComplexType>(); |
| 111 | Type = CT->getElementType(); |
| 112 | ArraySize = 2; |
| 113 | MostDerivedLength = I + 1; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 114 | } else if (const FieldDecl *FD = getAsField(Path[I])) { |
| 115 | Type = FD->getType(); |
| 116 | ArraySize = 0; |
| 117 | MostDerivedLength = I + 1; |
| 118 | } else { |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 119 | // Path[I] describes a base class. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 120 | ArraySize = 0; |
| 121 | } |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 122 | } |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 123 | return MostDerivedLength; |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 126 | // The order of this enum is important for diagnostics. |
| 127 | enum CheckSubobjectKind { |
Richard Smith | b04035a | 2012-02-01 02:39:43 +0000 | [diff] [blame] | 128 | CSK_Base, CSK_Derived, CSK_Field, CSK_ArrayToPointer, CSK_ArrayIndex, |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 129 | CSK_This, CSK_Real, CSK_Imag |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 130 | }; |
| 131 | |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 132 | /// A path from a glvalue to a subobject of that glvalue. |
| 133 | struct SubobjectDesignator { |
| 134 | /// True if the subobject was named in a manner not supported by C++11. Such |
| 135 | /// lvalues can still be folded, but they are not core constant expressions |
| 136 | /// and we cannot perform lvalue-to-rvalue conversions on them. |
| 137 | bool Invalid : 1; |
| 138 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 139 | /// Is this a pointer one past the end of an object? |
| 140 | bool IsOnePastTheEnd : 1; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 141 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 142 | /// The length of the path to the most-derived object of which this is a |
| 143 | /// subobject. |
| 144 | unsigned MostDerivedPathLength : 30; |
| 145 | |
| 146 | /// The size of the array of which the most-derived object is an element, or |
| 147 | /// 0 if the most-derived object is not an array element. |
| 148 | uint64_t MostDerivedArraySize; |
| 149 | |
| 150 | /// The type of the most derived object referred to by this address. |
| 151 | QualType MostDerivedType; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 152 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 153 | typedef APValue::LValuePathEntry PathEntry; |
| 154 | |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 155 | /// The entries on the path from the glvalue to the designated subobject. |
| 156 | SmallVector<PathEntry, 8> Entries; |
| 157 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 158 | SubobjectDesignator() : Invalid(true) {} |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 159 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 160 | explicit SubobjectDesignator(QualType T) |
| 161 | : Invalid(false), IsOnePastTheEnd(false), MostDerivedPathLength(0), |
| 162 | MostDerivedArraySize(0), MostDerivedType(T) {} |
| 163 | |
| 164 | SubobjectDesignator(ASTContext &Ctx, const APValue &V) |
| 165 | : Invalid(!V.isLValue() || !V.hasLValuePath()), IsOnePastTheEnd(false), |
| 166 | MostDerivedPathLength(0), MostDerivedArraySize(0) { |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 167 | if (!Invalid) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 168 | IsOnePastTheEnd = V.isLValueOnePastTheEnd(); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 169 | ArrayRef<PathEntry> VEntries = V.getLValuePath(); |
| 170 | Entries.insert(Entries.end(), VEntries.begin(), VEntries.end()); |
| 171 | if (V.getLValueBase()) |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 172 | MostDerivedPathLength = |
| 173 | findMostDerivedSubobject(Ctx, getType(V.getLValueBase()), |
| 174 | V.getLValuePath(), MostDerivedArraySize, |
| 175 | MostDerivedType); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 179 | void setInvalid() { |
| 180 | Invalid = true; |
| 181 | Entries.clear(); |
| 182 | } |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 183 | |
| 184 | /// Determine whether this is a one-past-the-end pointer. |
| 185 | bool isOnePastTheEnd() const { |
| 186 | if (IsOnePastTheEnd) |
| 187 | return true; |
| 188 | if (MostDerivedArraySize && |
| 189 | Entries[MostDerivedPathLength - 1].ArrayIndex == MostDerivedArraySize) |
| 190 | return true; |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | /// Check that this refers to a valid subobject. |
| 195 | bool isValidSubobject() const { |
| 196 | if (Invalid) |
| 197 | return false; |
| 198 | return !isOnePastTheEnd(); |
| 199 | } |
| 200 | /// Check that this refers to a valid subobject, and if not, produce a |
| 201 | /// relevant diagnostic and set the designator as invalid. |
| 202 | bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK); |
| 203 | |
| 204 | /// Update this designator to refer to the first element within this array. |
| 205 | void addArrayUnchecked(const ConstantArrayType *CAT) { |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 206 | PathEntry Entry; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 207 | Entry.ArrayIndex = 0; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 208 | Entries.push_back(Entry); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 209 | |
| 210 | // This is a most-derived object. |
| 211 | MostDerivedType = CAT->getElementType(); |
| 212 | MostDerivedArraySize = CAT->getSize().getZExtValue(); |
| 213 | MostDerivedPathLength = Entries.size(); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 214 | } |
| 215 | /// Update this designator to refer to the given base or member of this |
| 216 | /// object. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 217 | void addDeclUnchecked(const Decl *D, bool Virtual = false) { |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 218 | PathEntry Entry; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 219 | APValue::BaseOrMemberType Value(D, Virtual); |
| 220 | Entry.BaseOrMember = Value.getOpaqueValue(); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 221 | Entries.push_back(Entry); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 222 | |
| 223 | // If this isn't a base class, it's a new most-derived object. |
| 224 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 225 | MostDerivedType = FD->getType(); |
| 226 | MostDerivedArraySize = 0; |
| 227 | MostDerivedPathLength = Entries.size(); |
| 228 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 229 | } |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 230 | /// Update this designator to refer to the given complex component. |
| 231 | void addComplexUnchecked(QualType EltTy, bool Imag) { |
| 232 | PathEntry Entry; |
| 233 | Entry.ArrayIndex = Imag; |
| 234 | Entries.push_back(Entry); |
| 235 | |
| 236 | // This is technically a most-derived object, though in practice this |
| 237 | // is unlikely to matter. |
| 238 | MostDerivedType = EltTy; |
| 239 | MostDerivedArraySize = 2; |
| 240 | MostDerivedPathLength = Entries.size(); |
| 241 | } |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 242 | void diagnosePointerArithmetic(EvalInfo &Info, const Expr *E, uint64_t N); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 243 | /// Add N to the address of this subobject. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 244 | void adjustIndex(EvalInfo &Info, const Expr *E, uint64_t N) { |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 245 | if (Invalid) return; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 246 | if (MostDerivedPathLength == Entries.size() && MostDerivedArraySize) { |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 247 | Entries.back().ArrayIndex += N; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 248 | if (Entries.back().ArrayIndex > MostDerivedArraySize) { |
| 249 | diagnosePointerArithmetic(Info, E, Entries.back().ArrayIndex); |
| 250 | setInvalid(); |
| 251 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 252 | return; |
| 253 | } |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 254 | // [expr.add]p4: For the purposes of these operators, a pointer to a |
| 255 | // nonarray object behaves the same as a pointer to the first element of |
| 256 | // an array of length one with the type of the object as its element type. |
| 257 | if (IsOnePastTheEnd && N == (uint64_t)-1) |
| 258 | IsOnePastTheEnd = false; |
| 259 | else if (!IsOnePastTheEnd && N == 1) |
| 260 | IsOnePastTheEnd = true; |
| 261 | else if (N != 0) { |
| 262 | diagnosePointerArithmetic(Info, E, uint64_t(IsOnePastTheEnd) + N); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 263 | setInvalid(); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 264 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 265 | } |
| 266 | }; |
| 267 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 268 | /// A stack frame in the constexpr call stack. |
| 269 | struct CallStackFrame { |
| 270 | EvalInfo &Info; |
| 271 | |
| 272 | /// Parent - The caller of this stack frame. |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 273 | CallStackFrame *Caller; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 274 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 275 | /// CallLoc - The location of the call expression for this call. |
| 276 | SourceLocation CallLoc; |
| 277 | |
| 278 | /// Callee - The function which was called. |
| 279 | const FunctionDecl *Callee; |
| 280 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 281 | /// Index - The call index of this call. |
| 282 | unsigned Index; |
| 283 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 284 | /// This - The binding for the this pointer in this call, if any. |
| 285 | const LValue *This; |
| 286 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 287 | /// ParmBindings - Parameter bindings for this function call, indexed by |
| 288 | /// parameters' function scope indices. |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 289 | const APValue *Arguments; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 290 | |
Eli Friedman | f6172ae | 2012-06-25 21:21:08 +0000 | [diff] [blame] | 291 | // Note that we intentionally use std::map here so that references to |
| 292 | // values are stable. |
| 293 | typedef std::map<const Expr*, APValue> MapTy; |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 294 | typedef MapTy::const_iterator temp_iterator; |
| 295 | /// Temporaries - Temporary lvalues materialized within this stack frame. |
| 296 | MapTy Temporaries; |
| 297 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 298 | CallStackFrame(EvalInfo &Info, SourceLocation CallLoc, |
| 299 | const FunctionDecl *Callee, const LValue *This, |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 300 | const APValue *Arguments); |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 301 | ~CallStackFrame(); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 302 | }; |
| 303 | |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 304 | /// A partial diagnostic which we might know in advance that we are not going |
| 305 | /// to emit. |
| 306 | class OptionalDiagnostic { |
| 307 | PartialDiagnostic *Diag; |
| 308 | |
| 309 | public: |
| 310 | explicit OptionalDiagnostic(PartialDiagnostic *Diag = 0) : Diag(Diag) {} |
| 311 | |
| 312 | template<typename T> |
| 313 | OptionalDiagnostic &operator<<(const T &v) { |
| 314 | if (Diag) |
| 315 | *Diag << v; |
| 316 | return *this; |
| 317 | } |
Richard Smith | 789f9b6 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 318 | |
| 319 | OptionalDiagnostic &operator<<(const APSInt &I) { |
| 320 | if (Diag) { |
| 321 | llvm::SmallVector<char, 32> Buffer; |
| 322 | I.toString(Buffer); |
| 323 | *Diag << StringRef(Buffer.data(), Buffer.size()); |
| 324 | } |
| 325 | return *this; |
| 326 | } |
| 327 | |
| 328 | OptionalDiagnostic &operator<<(const APFloat &F) { |
| 329 | if (Diag) { |
| 330 | llvm::SmallVector<char, 32> Buffer; |
| 331 | F.toString(Buffer); |
| 332 | *Diag << StringRef(Buffer.data(), Buffer.size()); |
| 333 | } |
| 334 | return *this; |
| 335 | } |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 336 | }; |
| 337 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 338 | /// EvalInfo - This is a private struct used by the evaluator to capture |
| 339 | /// information about a subexpression as it is folded. It retains information |
| 340 | /// about the AST context, but also maintains information about the folded |
| 341 | /// expression. |
| 342 | /// |
| 343 | /// If an expression could be evaluated, it is still possible it is not a C |
| 344 | /// "integer constant expression" or constant expression. If not, this struct |
| 345 | /// captures information about how and why not. |
| 346 | /// |
| 347 | /// One bit of information passed *into* the request for constant folding |
| 348 | /// indicates whether the subexpression is "evaluated" or not according to C |
| 349 | /// rules. For example, the RHS of (0 && foo()) is not evaluated. We can |
| 350 | /// evaluate the expression regardless of what the RHS is, but C only allows |
| 351 | /// certain things in certain situations. |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 352 | struct EvalInfo { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 353 | ASTContext &Ctx; |
Argyrios Kyrtzidis | d411a4b | 2012-02-27 20:21:34 +0000 | [diff] [blame] | 354 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 355 | /// EvalStatus - Contains information about the evaluation. |
| 356 | Expr::EvalStatus &EvalStatus; |
| 357 | |
| 358 | /// CurrentCall - The top of the constexpr call stack. |
| 359 | CallStackFrame *CurrentCall; |
| 360 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 361 | /// CallStackDepth - The number of calls in the call stack right now. |
| 362 | unsigned CallStackDepth; |
| 363 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 364 | /// NextCallIndex - The next call index to assign. |
| 365 | unsigned NextCallIndex; |
| 366 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 367 | /// BottomFrame - The frame in which evaluation started. This must be |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 368 | /// initialized after CurrentCall and CallStackDepth. |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 369 | CallStackFrame BottomFrame; |
| 370 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 371 | /// EvaluatingDecl - This is the declaration whose initializer is being |
| 372 | /// evaluated, if any. |
| 373 | const VarDecl *EvaluatingDecl; |
| 374 | |
| 375 | /// EvaluatingDeclValue - This is the value being constructed for the |
| 376 | /// declaration whose initializer is being evaluated, if any. |
| 377 | APValue *EvaluatingDeclValue; |
| 378 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 379 | /// HasActiveDiagnostic - Was the previous diagnostic stored? If so, further |
| 380 | /// notes attached to it will also be stored, otherwise they will not be. |
| 381 | bool HasActiveDiagnostic; |
| 382 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 383 | /// CheckingPotentialConstantExpression - Are we checking whether the |
| 384 | /// expression is a potential constant expression? If so, some diagnostics |
| 385 | /// are suppressed. |
| 386 | bool CheckingPotentialConstantExpression; |
| 387 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 388 | EvalInfo(const ASTContext &C, Expr::EvalStatus &S) |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 389 | : Ctx(const_cast<ASTContext&>(C)), EvalStatus(S), CurrentCall(0), |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 390 | CallStackDepth(0), NextCallIndex(1), |
| 391 | BottomFrame(*this, SourceLocation(), 0, 0, 0), |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 392 | EvaluatingDecl(0), EvaluatingDeclValue(0), HasActiveDiagnostic(false), |
Argyrios Kyrtzidis | 649dfbc | 2012-03-15 18:07:13 +0000 | [diff] [blame] | 393 | CheckingPotentialConstantExpression(false) {} |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 394 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 395 | void setEvaluatingDecl(const VarDecl *VD, APValue &Value) { |
| 396 | EvaluatingDecl = VD; |
| 397 | EvaluatingDeclValue = &Value; |
| 398 | } |
| 399 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 400 | const LangOptions &getLangOpts() const { return Ctx.getLangOpts(); } |
Richard Smith | c18c423 | 2011-11-21 19:36:32 +0000 | [diff] [blame] | 401 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 402 | bool CheckCallLimit(SourceLocation Loc) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 403 | // Don't perform any constexpr calls (other than the call we're checking) |
| 404 | // when checking a potential constant expression. |
| 405 | if (CheckingPotentialConstantExpression && CallStackDepth > 1) |
| 406 | return false; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 407 | if (NextCallIndex == 0) { |
| 408 | // NextCallIndex has wrapped around. |
| 409 | Diag(Loc, diag::note_constexpr_call_limit_exceeded); |
| 410 | return false; |
| 411 | } |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 412 | if (CallStackDepth <= getLangOpts().ConstexprCallDepth) |
| 413 | return true; |
| 414 | Diag(Loc, diag::note_constexpr_depth_limit_exceeded) |
| 415 | << getLangOpts().ConstexprCallDepth; |
| 416 | return false; |
Richard Smith | c18c423 | 2011-11-21 19:36:32 +0000 | [diff] [blame] | 417 | } |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 418 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 419 | CallStackFrame *getCallFrame(unsigned CallIndex) { |
| 420 | assert(CallIndex && "no call index in getCallFrame"); |
| 421 | // We will eventually hit BottomFrame, which has Index 1, so Frame can't |
| 422 | // be null in this loop. |
| 423 | CallStackFrame *Frame = CurrentCall; |
| 424 | while (Frame->Index > CallIndex) |
| 425 | Frame = Frame->Caller; |
| 426 | return (Frame->Index == CallIndex) ? Frame : 0; |
| 427 | } |
| 428 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 429 | private: |
| 430 | /// Add a diagnostic to the diagnostics list. |
| 431 | PartialDiagnostic &addDiag(SourceLocation Loc, diag::kind DiagId) { |
| 432 | PartialDiagnostic PD(DiagId, Ctx.getDiagAllocator()); |
| 433 | EvalStatus.Diag->push_back(std::make_pair(Loc, PD)); |
| 434 | return EvalStatus.Diag->back().second; |
| 435 | } |
| 436 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 437 | /// Add notes containing a call stack to the current point of evaluation. |
| 438 | void addCallStack(unsigned Limit); |
| 439 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 440 | public: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 441 | /// Diagnose that the evaluation cannot be folded. |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 442 | OptionalDiagnostic Diag(SourceLocation Loc, diag::kind DiagId |
| 443 | = diag::note_invalid_subexpr_in_const_expr, |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 444 | unsigned ExtraNotes = 0) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 445 | // If we have a prior diagnostic, it will be noting that the expression |
| 446 | // isn't a constant expression. This diagnostic is more important. |
| 447 | // FIXME: We might want to show both diagnostics to the user. |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 448 | if (EvalStatus.Diag) { |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 449 | unsigned CallStackNotes = CallStackDepth - 1; |
| 450 | unsigned Limit = Ctx.getDiagnostics().getConstexprBacktraceLimit(); |
| 451 | if (Limit) |
| 452 | CallStackNotes = std::min(CallStackNotes, Limit + 1); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 453 | if (CheckingPotentialConstantExpression) |
| 454 | CallStackNotes = 0; |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 455 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 456 | HasActiveDiagnostic = true; |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 457 | EvalStatus.Diag->clear(); |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 458 | EvalStatus.Diag->reserve(1 + ExtraNotes + CallStackNotes); |
| 459 | addDiag(Loc, DiagId); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 460 | if (!CheckingPotentialConstantExpression) |
| 461 | addCallStack(Limit); |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 462 | return OptionalDiagnostic(&(*EvalStatus.Diag)[0].second); |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 463 | } |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 464 | HasActiveDiagnostic = false; |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 465 | return OptionalDiagnostic(); |
| 466 | } |
| 467 | |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 468 | OptionalDiagnostic Diag(const Expr *E, diag::kind DiagId |
| 469 | = diag::note_invalid_subexpr_in_const_expr, |
| 470 | unsigned ExtraNotes = 0) { |
| 471 | if (EvalStatus.Diag) |
| 472 | return Diag(E->getExprLoc(), DiagId, ExtraNotes); |
| 473 | HasActiveDiagnostic = false; |
| 474 | return OptionalDiagnostic(); |
| 475 | } |
| 476 | |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 477 | /// Diagnose that the evaluation does not produce a C++11 core constant |
| 478 | /// expression. |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 479 | template<typename LocArg> |
| 480 | OptionalDiagnostic CCEDiag(LocArg Loc, diag::kind DiagId |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 481 | = diag::note_invalid_subexpr_in_const_expr, |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 482 | unsigned ExtraNotes = 0) { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 483 | // Don't override a previous diagnostic. |
Eli Friedman | 51e47df | 2012-02-21 22:41:33 +0000 | [diff] [blame] | 484 | if (!EvalStatus.Diag || !EvalStatus.Diag->empty()) { |
| 485 | HasActiveDiagnostic = false; |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 486 | return OptionalDiagnostic(); |
Eli Friedman | 51e47df | 2012-02-21 22:41:33 +0000 | [diff] [blame] | 487 | } |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 488 | return Diag(Loc, DiagId, ExtraNotes); |
| 489 | } |
| 490 | |
| 491 | /// Add a note to a prior diagnostic. |
| 492 | OptionalDiagnostic Note(SourceLocation Loc, diag::kind DiagId) { |
| 493 | if (!HasActiveDiagnostic) |
| 494 | return OptionalDiagnostic(); |
| 495 | return OptionalDiagnostic(&addDiag(Loc, DiagId)); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 496 | } |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 497 | |
| 498 | /// Add a stack of notes to a prior diagnostic. |
| 499 | void addNotes(ArrayRef<PartialDiagnosticAt> Diags) { |
| 500 | if (HasActiveDiagnostic) { |
| 501 | EvalStatus.Diag->insert(EvalStatus.Diag->end(), |
| 502 | Diags.begin(), Diags.end()); |
| 503 | } |
| 504 | } |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 505 | |
| 506 | /// Should we continue evaluation as much as possible after encountering a |
| 507 | /// construct which can't be folded? |
| 508 | bool keepEvaluatingAfterFailure() { |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 509 | return CheckingPotentialConstantExpression && |
| 510 | EvalStatus.Diag && EvalStatus.Diag->empty(); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 511 | } |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 512 | }; |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 513 | |
| 514 | /// Object used to treat all foldable expressions as constant expressions. |
| 515 | struct FoldConstant { |
| 516 | bool Enabled; |
| 517 | |
| 518 | explicit FoldConstant(EvalInfo &Info) |
| 519 | : Enabled(Info.EvalStatus.Diag && Info.EvalStatus.Diag->empty() && |
| 520 | !Info.EvalStatus.HasSideEffects) { |
| 521 | } |
| 522 | // Treat the value we've computed since this object was created as constant. |
| 523 | void Fold(EvalInfo &Info) { |
| 524 | if (Enabled && !Info.EvalStatus.Diag->empty() && |
| 525 | !Info.EvalStatus.HasSideEffects) |
| 526 | Info.EvalStatus.Diag->clear(); |
| 527 | } |
| 528 | }; |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 529 | |
| 530 | /// RAII object used to suppress diagnostics and side-effects from a |
| 531 | /// speculative evaluation. |
| 532 | class SpeculativeEvaluationRAII { |
| 533 | EvalInfo &Info; |
| 534 | Expr::EvalStatus Old; |
| 535 | |
| 536 | public: |
| 537 | SpeculativeEvaluationRAII(EvalInfo &Info, |
| 538 | llvm::SmallVectorImpl<PartialDiagnosticAt> |
| 539 | *NewDiag = 0) |
| 540 | : Info(Info), Old(Info.EvalStatus) { |
| 541 | Info.EvalStatus.Diag = NewDiag; |
| 542 | } |
| 543 | ~SpeculativeEvaluationRAII() { |
| 544 | Info.EvalStatus = Old; |
| 545 | } |
| 546 | }; |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 547 | } |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 548 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 549 | bool SubobjectDesignator::checkSubobject(EvalInfo &Info, const Expr *E, |
| 550 | CheckSubobjectKind CSK) { |
| 551 | if (Invalid) |
| 552 | return false; |
| 553 | if (isOnePastTheEnd()) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 554 | Info.CCEDiag(E, diag::note_constexpr_past_end_subobject) |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 555 | << CSK; |
| 556 | setInvalid(); |
| 557 | return false; |
| 558 | } |
| 559 | return true; |
| 560 | } |
| 561 | |
| 562 | void SubobjectDesignator::diagnosePointerArithmetic(EvalInfo &Info, |
| 563 | const Expr *E, uint64_t N) { |
| 564 | if (MostDerivedPathLength == Entries.size() && MostDerivedArraySize) |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 565 | Info.CCEDiag(E, diag::note_constexpr_array_index) |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 566 | << static_cast<int>(N) << /*array*/ 0 |
| 567 | << static_cast<unsigned>(MostDerivedArraySize); |
| 568 | else |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 569 | Info.CCEDiag(E, diag::note_constexpr_array_index) |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 570 | << static_cast<int>(N) << /*non-array*/ 1; |
| 571 | setInvalid(); |
| 572 | } |
| 573 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 574 | CallStackFrame::CallStackFrame(EvalInfo &Info, SourceLocation CallLoc, |
| 575 | const FunctionDecl *Callee, const LValue *This, |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 576 | const APValue *Arguments) |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 577 | : Info(Info), Caller(Info.CurrentCall), CallLoc(CallLoc), Callee(Callee), |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 578 | Index(Info.NextCallIndex++), This(This), Arguments(Arguments) { |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 579 | Info.CurrentCall = this; |
| 580 | ++Info.CallStackDepth; |
| 581 | } |
| 582 | |
| 583 | CallStackFrame::~CallStackFrame() { |
| 584 | assert(Info.CurrentCall == this && "calls retired out of order"); |
| 585 | --Info.CallStackDepth; |
| 586 | Info.CurrentCall = Caller; |
| 587 | } |
| 588 | |
| 589 | /// Produce a string describing the given constexpr call. |
| 590 | static void describeCall(CallStackFrame *Frame, llvm::raw_ostream &Out) { |
| 591 | unsigned ArgIndex = 0; |
| 592 | bool IsMemberCall = isa<CXXMethodDecl>(Frame->Callee) && |
Richard Smith | 5ba73e1 | 2012-02-04 00:33:54 +0000 | [diff] [blame] | 593 | !isa<CXXConstructorDecl>(Frame->Callee) && |
| 594 | cast<CXXMethodDecl>(Frame->Callee)->isInstance(); |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 595 | |
| 596 | if (!IsMemberCall) |
| 597 | Out << *Frame->Callee << '('; |
| 598 | |
| 599 | for (FunctionDecl::param_const_iterator I = Frame->Callee->param_begin(), |
| 600 | E = Frame->Callee->param_end(); I != E; ++I, ++ArgIndex) { |
NAKAMURA Takumi | 5fe3122 | 2012-01-26 09:37:36 +0000 | [diff] [blame] | 601 | if (ArgIndex > (unsigned)IsMemberCall) |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 602 | Out << ", "; |
| 603 | |
| 604 | const ParmVarDecl *Param = *I; |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 605 | const APValue &Arg = Frame->Arguments[ArgIndex]; |
| 606 | Arg.printPretty(Out, Frame->Info.Ctx, Param->getType()); |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 607 | |
| 608 | if (ArgIndex == 0 && IsMemberCall) |
| 609 | Out << "->" << *Frame->Callee << '('; |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 612 | Out << ')'; |
| 613 | } |
| 614 | |
| 615 | void EvalInfo::addCallStack(unsigned Limit) { |
| 616 | // Determine which calls to skip, if any. |
| 617 | unsigned ActiveCalls = CallStackDepth - 1; |
| 618 | unsigned SkipStart = ActiveCalls, SkipEnd = SkipStart; |
| 619 | if (Limit && Limit < ActiveCalls) { |
| 620 | SkipStart = Limit / 2 + Limit % 2; |
| 621 | SkipEnd = ActiveCalls - Limit / 2; |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 624 | // Walk the call stack and add the diagnostics. |
| 625 | unsigned CallIdx = 0; |
| 626 | for (CallStackFrame *Frame = CurrentCall; Frame != &BottomFrame; |
| 627 | Frame = Frame->Caller, ++CallIdx) { |
| 628 | // Skip this call? |
| 629 | if (CallIdx >= SkipStart && CallIdx < SkipEnd) { |
| 630 | if (CallIdx == SkipStart) { |
| 631 | // Note that we're skipping calls. |
| 632 | addDiag(Frame->CallLoc, diag::note_constexpr_calls_suppressed) |
| 633 | << unsigned(ActiveCalls - Limit); |
| 634 | } |
| 635 | continue; |
| 636 | } |
| 637 | |
| 638 | llvm::SmallVector<char, 128> Buffer; |
| 639 | llvm::raw_svector_ostream Out(Buffer); |
| 640 | describeCall(Frame, Out); |
| 641 | addDiag(Frame->CallLoc, diag::note_constexpr_call_here) << Out.str(); |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | namespace { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 646 | struct ComplexValue { |
| 647 | private: |
| 648 | bool IsInt; |
| 649 | |
| 650 | public: |
| 651 | APSInt IntReal, IntImag; |
| 652 | APFloat FloatReal, FloatImag; |
| 653 | |
| 654 | ComplexValue() : FloatReal(APFloat::Bogus), FloatImag(APFloat::Bogus) {} |
| 655 | |
| 656 | void makeComplexFloat() { IsInt = false; } |
| 657 | bool isComplexFloat() const { return !IsInt; } |
| 658 | APFloat &getComplexFloatReal() { return FloatReal; } |
| 659 | APFloat &getComplexFloatImag() { return FloatImag; } |
| 660 | |
| 661 | void makeComplexInt() { IsInt = true; } |
| 662 | bool isComplexInt() const { return IsInt; } |
| 663 | APSInt &getComplexIntReal() { return IntReal; } |
| 664 | APSInt &getComplexIntImag() { return IntImag; } |
| 665 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 666 | void moveInto(APValue &v) const { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 667 | if (isComplexFloat()) |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 668 | v = APValue(FloatReal, FloatImag); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 669 | else |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 670 | v = APValue(IntReal, IntImag); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 671 | } |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 672 | void setFrom(const APValue &v) { |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 673 | assert(v.isComplexFloat() || v.isComplexInt()); |
| 674 | if (v.isComplexFloat()) { |
| 675 | makeComplexFloat(); |
| 676 | FloatReal = v.getComplexFloatReal(); |
| 677 | FloatImag = v.getComplexFloatImag(); |
| 678 | } else { |
| 679 | makeComplexInt(); |
| 680 | IntReal = v.getComplexIntReal(); |
| 681 | IntImag = v.getComplexIntImag(); |
| 682 | } |
| 683 | } |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 684 | }; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 685 | |
| 686 | struct LValue { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 687 | APValue::LValueBase Base; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 688 | CharUnits Offset; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 689 | unsigned CallIndex; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 690 | SubobjectDesignator Designator; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 691 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 692 | const APValue::LValueBase getLValueBase() const { return Base; } |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 693 | CharUnits &getLValueOffset() { return Offset; } |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 694 | const CharUnits &getLValueOffset() const { return Offset; } |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 695 | unsigned getLValueCallIndex() const { return CallIndex; } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 696 | SubobjectDesignator &getLValueDesignator() { return Designator; } |
| 697 | const SubobjectDesignator &getLValueDesignator() const { return Designator;} |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 698 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 699 | void moveInto(APValue &V) const { |
| 700 | if (Designator.Invalid) |
| 701 | V = APValue(Base, Offset, APValue::NoLValuePath(), CallIndex); |
| 702 | else |
| 703 | V = APValue(Base, Offset, Designator.Entries, |
| 704 | Designator.IsOnePastTheEnd, CallIndex); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 705 | } |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 706 | void setFrom(ASTContext &Ctx, const APValue &V) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 707 | assert(V.isLValue()); |
| 708 | Base = V.getLValueBase(); |
| 709 | Offset = V.getLValueOffset(); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 710 | CallIndex = V.getLValueCallIndex(); |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 711 | Designator = SubobjectDesignator(Ctx, V); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 712 | } |
| 713 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 714 | void set(APValue::LValueBase B, unsigned I = 0) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 715 | Base = B; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 716 | Offset = CharUnits::Zero(); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 717 | CallIndex = I; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 718 | Designator = SubobjectDesignator(getType(B)); |
| 719 | } |
| 720 | |
| 721 | // Check that this LValue is not based on a null pointer. If it is, produce |
| 722 | // a diagnostic and mark the designator as invalid. |
| 723 | bool checkNullPointer(EvalInfo &Info, const Expr *E, |
| 724 | CheckSubobjectKind CSK) { |
| 725 | if (Designator.Invalid) |
| 726 | return false; |
| 727 | if (!Base) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 728 | Info.CCEDiag(E, diag::note_constexpr_null_subobject) |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 729 | << CSK; |
| 730 | Designator.setInvalid(); |
| 731 | return false; |
| 732 | } |
| 733 | return true; |
| 734 | } |
| 735 | |
| 736 | // Check this LValue refers to an object. If not, set the designator to be |
| 737 | // invalid and emit a diagnostic. |
| 738 | bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 739 | // Outside C++11, do not build a designator referring to a subobject of |
| 740 | // any object: we won't use such a designator for anything. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 741 | if (!Info.getLangOpts().CPlusPlus11) |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 742 | Designator.setInvalid(); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 743 | return checkNullPointer(Info, E, CSK) && |
| 744 | Designator.checkSubobject(Info, E, CSK); |
| 745 | } |
| 746 | |
| 747 | void addDecl(EvalInfo &Info, const Expr *E, |
| 748 | const Decl *D, bool Virtual = false) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 749 | if (checkSubobject(Info, E, isa<FieldDecl>(D) ? CSK_Field : CSK_Base)) |
| 750 | Designator.addDeclUnchecked(D, Virtual); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 751 | } |
| 752 | void addArray(EvalInfo &Info, const Expr *E, const ConstantArrayType *CAT) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 753 | if (checkSubobject(Info, E, CSK_ArrayToPointer)) |
| 754 | Designator.addArrayUnchecked(CAT); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 755 | } |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 756 | void addComplex(EvalInfo &Info, const Expr *E, QualType EltTy, bool Imag) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 757 | if (checkSubobject(Info, E, Imag ? CSK_Imag : CSK_Real)) |
| 758 | Designator.addComplexUnchecked(EltTy, Imag); |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 759 | } |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 760 | void adjustIndex(EvalInfo &Info, const Expr *E, uint64_t N) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 761 | if (checkNullPointer(Info, E, CSK_ArrayIndex)) |
| 762 | Designator.adjustIndex(Info, E, N); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 763 | } |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 764 | }; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 765 | |
| 766 | struct MemberPtr { |
| 767 | MemberPtr() {} |
| 768 | explicit MemberPtr(const ValueDecl *Decl) : |
| 769 | DeclAndIsDerivedMember(Decl, false), Path() {} |
| 770 | |
| 771 | /// The member or (direct or indirect) field referred to by this member |
| 772 | /// pointer, or 0 if this is a null member pointer. |
| 773 | const ValueDecl *getDecl() const { |
| 774 | return DeclAndIsDerivedMember.getPointer(); |
| 775 | } |
| 776 | /// Is this actually a member of some type derived from the relevant class? |
| 777 | bool isDerivedMember() const { |
| 778 | return DeclAndIsDerivedMember.getInt(); |
| 779 | } |
| 780 | /// Get the class which the declaration actually lives in. |
| 781 | const CXXRecordDecl *getContainingRecord() const { |
| 782 | return cast<CXXRecordDecl>( |
| 783 | DeclAndIsDerivedMember.getPointer()->getDeclContext()); |
| 784 | } |
| 785 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 786 | void moveInto(APValue &V) const { |
| 787 | V = APValue(getDecl(), isDerivedMember(), Path); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 788 | } |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 789 | void setFrom(const APValue &V) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 790 | assert(V.isMemberPointer()); |
| 791 | DeclAndIsDerivedMember.setPointer(V.getMemberPointerDecl()); |
| 792 | DeclAndIsDerivedMember.setInt(V.isMemberPointerToDerivedMember()); |
| 793 | Path.clear(); |
| 794 | ArrayRef<const CXXRecordDecl*> P = V.getMemberPointerPath(); |
| 795 | Path.insert(Path.end(), P.begin(), P.end()); |
| 796 | } |
| 797 | |
| 798 | /// DeclAndIsDerivedMember - The member declaration, and a flag indicating |
| 799 | /// whether the member is a member of some class derived from the class type |
| 800 | /// of the member pointer. |
| 801 | llvm::PointerIntPair<const ValueDecl*, 1, bool> DeclAndIsDerivedMember; |
| 802 | /// Path - The path of base/derived classes from the member declaration's |
| 803 | /// class (exclusive) to the class type of the member pointer (inclusive). |
| 804 | SmallVector<const CXXRecordDecl*, 4> Path; |
| 805 | |
| 806 | /// Perform a cast towards the class of the Decl (either up or down the |
| 807 | /// hierarchy). |
| 808 | bool castBack(const CXXRecordDecl *Class) { |
| 809 | assert(!Path.empty()); |
| 810 | const CXXRecordDecl *Expected; |
| 811 | if (Path.size() >= 2) |
| 812 | Expected = Path[Path.size() - 2]; |
| 813 | else |
| 814 | Expected = getContainingRecord(); |
| 815 | if (Expected->getCanonicalDecl() != Class->getCanonicalDecl()) { |
| 816 | // C++11 [expr.static.cast]p12: In a conversion from (D::*) to (B::*), |
| 817 | // if B does not contain the original member and is not a base or |
| 818 | // derived class of the class containing the original member, the result |
| 819 | // of the cast is undefined. |
| 820 | // C++11 [conv.mem]p2 does not cover this case for a cast from (B::*) to |
| 821 | // (D::*). We consider that to be a language defect. |
| 822 | return false; |
| 823 | } |
| 824 | Path.pop_back(); |
| 825 | return true; |
| 826 | } |
| 827 | /// Perform a base-to-derived member pointer cast. |
| 828 | bool castToDerived(const CXXRecordDecl *Derived) { |
| 829 | if (!getDecl()) |
| 830 | return true; |
| 831 | if (!isDerivedMember()) { |
| 832 | Path.push_back(Derived); |
| 833 | return true; |
| 834 | } |
| 835 | if (!castBack(Derived)) |
| 836 | return false; |
| 837 | if (Path.empty()) |
| 838 | DeclAndIsDerivedMember.setInt(false); |
| 839 | return true; |
| 840 | } |
| 841 | /// Perform a derived-to-base member pointer cast. |
| 842 | bool castToBase(const CXXRecordDecl *Base) { |
| 843 | if (!getDecl()) |
| 844 | return true; |
| 845 | if (Path.empty()) |
| 846 | DeclAndIsDerivedMember.setInt(true); |
| 847 | if (isDerivedMember()) { |
| 848 | Path.push_back(Base); |
| 849 | return true; |
| 850 | } |
| 851 | return castBack(Base); |
| 852 | } |
| 853 | }; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 854 | |
Richard Smith | b02e462 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 855 | /// Compare two member pointers, which are assumed to be of the same type. |
| 856 | static bool operator==(const MemberPtr &LHS, const MemberPtr &RHS) { |
| 857 | if (!LHS.getDecl() || !RHS.getDecl()) |
| 858 | return !LHS.getDecl() && !RHS.getDecl(); |
| 859 | if (LHS.getDecl()->getCanonicalDecl() != RHS.getDecl()->getCanonicalDecl()) |
| 860 | return false; |
| 861 | return LHS.Path == RHS.Path; |
| 862 | } |
| 863 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 864 | /// Kinds of constant expression checking, for diagnostics. |
| 865 | enum CheckConstantExpressionKind { |
| 866 | CCEK_Constant, ///< A normal constant. |
| 867 | CCEK_ReturnValue, ///< A constexpr function return value. |
| 868 | CCEK_MemberInit ///< A constexpr constructor mem-initializer. |
| 869 | }; |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 870 | } |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 871 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 872 | static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 873 | static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, |
| 874 | const LValue &This, const Expr *E, |
| 875 | CheckConstantExpressionKind CCEK = CCEK_Constant, |
| 876 | bool AllowNonLiteralTypes = false); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 877 | static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info); |
| 878 | static bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 879 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
| 880 | EvalInfo &Info); |
| 881 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info); |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 882 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info); |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 883 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 884 | EvalInfo &Info); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 885 | static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 886 | static bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 887 | |
| 888 | //===----------------------------------------------------------------------===// |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 889 | // Misc utilities |
| 890 | //===----------------------------------------------------------------------===// |
| 891 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 892 | /// Should this call expression be treated as a string literal? |
| 893 | static bool IsStringLiteralCall(const CallExpr *E) { |
| 894 | unsigned Builtin = E->isBuiltinCall(); |
| 895 | return (Builtin == Builtin::BI__builtin___CFStringMakeConstantString || |
| 896 | Builtin == Builtin::BI__builtin___NSStringMakeConstantString); |
| 897 | } |
| 898 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 899 | static bool IsGlobalLValue(APValue::LValueBase B) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 900 | // C++11 [expr.const]p3 An address constant expression is a prvalue core |
| 901 | // constant expression of pointer type that evaluates to... |
| 902 | |
| 903 | // ... a null pointer value, or a prvalue core constant expression of type |
| 904 | // std::nullptr_t. |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 905 | if (!B) return true; |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 906 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 907 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
| 908 | // ... the address of an object with static storage duration, |
| 909 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 910 | return VD->hasGlobalStorage(); |
| 911 | // ... the address of a function, |
| 912 | return isa<FunctionDecl>(D); |
| 913 | } |
| 914 | |
| 915 | const Expr *E = B.get<const Expr*>(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 916 | switch (E->getStmtClass()) { |
| 917 | default: |
| 918 | return false; |
Richard Smith | b78ae97 | 2012-02-18 04:58:18 +0000 | [diff] [blame] | 919 | case Expr::CompoundLiteralExprClass: { |
| 920 | const CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E); |
| 921 | return CLE->isFileScope() && CLE->isLValue(); |
| 922 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 923 | // A string literal has static storage duration. |
| 924 | case Expr::StringLiteralClass: |
| 925 | case Expr::PredefinedExprClass: |
| 926 | case Expr::ObjCStringLiteralClass: |
| 927 | case Expr::ObjCEncodeExprClass: |
Richard Smith | 47d2145 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 928 | case Expr::CXXTypeidExprClass: |
Francois Pichet | e275a18 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 929 | case Expr::CXXUuidofExprClass: |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 930 | return true; |
| 931 | case Expr::CallExprClass: |
| 932 | return IsStringLiteralCall(cast<CallExpr>(E)); |
| 933 | // For GCC compatibility, &&label has static storage duration. |
| 934 | case Expr::AddrLabelExprClass: |
| 935 | return true; |
| 936 | // A Block literal expression may be used as the initialization value for |
| 937 | // Block variables at global or local static scope. |
| 938 | case Expr::BlockExprClass: |
| 939 | return !cast<BlockExpr>(E)->getBlockDecl()->hasCaptures(); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 940 | case Expr::ImplicitValueInitExprClass: |
| 941 | // FIXME: |
| 942 | // We can never form an lvalue with an implicit value initialization as its |
| 943 | // base through expression evaluation, so these only appear in one case: the |
| 944 | // implicit variable declaration we invent when checking whether a constexpr |
| 945 | // constructor can produce a constant expression. We must assume that such |
| 946 | // an expression might be a global lvalue. |
| 947 | return true; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 948 | } |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 949 | } |
| 950 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 951 | static void NoteLValueLocation(EvalInfo &Info, APValue::LValueBase Base) { |
| 952 | assert(Base && "no location for a null lvalue"); |
| 953 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
| 954 | if (VD) |
| 955 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 956 | else |
Ted Kremenek | 890f0f1 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 957 | Info.Note(Base.get<const Expr*>()->getExprLoc(), |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 958 | diag::note_constexpr_temporary_here); |
| 959 | } |
| 960 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 961 | /// Check that this reference or pointer core constant expression is a valid |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 962 | /// value for an address or reference constant expression. Return true if we |
| 963 | /// can fold this expression, whether or not it's a constant expression. |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 964 | static bool CheckLValueConstantExpression(EvalInfo &Info, SourceLocation Loc, |
| 965 | QualType Type, const LValue &LVal) { |
| 966 | bool IsReferenceType = Type->isReferenceType(); |
| 967 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 968 | APValue::LValueBase Base = LVal.getLValueBase(); |
| 969 | const SubobjectDesignator &Designator = LVal.getLValueDesignator(); |
| 970 | |
Richard Smith | b78ae97 | 2012-02-18 04:58:18 +0000 | [diff] [blame] | 971 | // Check that the object is a global. Note that the fake 'this' object we |
| 972 | // manufacture when checking potential constant expressions is conservatively |
| 973 | // assumed to be global here. |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 974 | if (!IsGlobalLValue(Base)) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 975 | if (Info.getLangOpts().CPlusPlus11) { |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 976 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 977 | Info.Diag(Loc, diag::note_constexpr_non_global, 1) |
| 978 | << IsReferenceType << !Designator.Entries.empty() |
| 979 | << !!VD << VD; |
| 980 | NoteLValueLocation(Info, Base); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 981 | } else { |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 982 | Info.Diag(Loc); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 983 | } |
Richard Smith | 61e6162 | 2012-01-12 06:08:57 +0000 | [diff] [blame] | 984 | // Don't allow references to temporaries to escape. |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 985 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 986 | } |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 987 | assert((Info.CheckingPotentialConstantExpression || |
| 988 | LVal.getLValueCallIndex() == 0) && |
| 989 | "have call index for global lvalue"); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 990 | |
Hans Wennborg | 48def65 | 2012-08-29 18:27:29 +0000 | [diff] [blame] | 991 | // Check if this is a thread-local variable. |
| 992 | if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) { |
| 993 | if (const VarDecl *Var = dyn_cast<const VarDecl>(VD)) { |
| 994 | if (Var->isThreadSpecified()) |
| 995 | return false; |
| 996 | } |
| 997 | } |
| 998 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 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. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1029 | if (Info.getLangOpts().CPlusPlus11) |
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) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1075 | if (!CheckConstantExpression(Info, DiagLoc, I->getType(), |
| 1076 | Value.getStructField(I->getFieldIndex()))) |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 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> |
Eli Friedman | 26dc97c | 2012-07-17 21:03:05 +0000 | [diff] [blame] | 1163 | static void HandleOverflow(EvalInfo &Info, const Expr *E, |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1164 | const T &SrcValue, QualType DestType) { |
Eli Friedman | 26dc97c | 2012-07-17 21:03:05 +0000 | [diff] [blame] | 1165 | Info.CCEDiag(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 | } |
| 1168 | |
| 1169 | static bool HandleFloatToIntCast(EvalInfo &Info, const Expr *E, |
| 1170 | QualType SrcType, const APFloat &Value, |
| 1171 | QualType DestType, APSInt &Result) { |
| 1172 | unsigned DestWidth = Info.Ctx.getIntWidth(DestType); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1173 | // Determine whether we are converting to unsigned or signed. |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 1174 | bool DestSigned = DestType->isSignedIntegerOrEnumerationType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1175 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1176 | Result = APSInt(DestWidth, !DestSigned); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1177 | bool ignored; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1178 | if (Value.convertToInteger(Result, llvm::APFloat::rmTowardZero, &ignored) |
| 1179 | & APFloat::opInvalidOp) |
Eli Friedman | 26dc97c | 2012-07-17 21:03:05 +0000 | [diff] [blame] | 1180 | HandleOverflow(Info, E, Value, DestType); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1181 | return true; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1184 | static bool HandleFloatToFloatCast(EvalInfo &Info, const Expr *E, |
| 1185 | QualType SrcType, QualType DestType, |
| 1186 | APFloat &Result) { |
| 1187 | APFloat Value = Result; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1188 | bool ignored; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1189 | if (Result.convert(Info.Ctx.getFloatTypeSemantics(DestType), |
| 1190 | APFloat::rmNearestTiesToEven, &ignored) |
| 1191 | & APFloat::opOverflow) |
Eli Friedman | 26dc97c | 2012-07-17 21:03:05 +0000 | [diff] [blame] | 1192 | HandleOverflow(Info, E, Value, DestType); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1193 | return true; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 1196 | static APSInt HandleIntToIntCast(EvalInfo &Info, const Expr *E, |
| 1197 | QualType DestType, QualType SrcType, |
| 1198 | APSInt &Value) { |
| 1199 | unsigned DestWidth = Info.Ctx.getIntWidth(DestType); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1200 | APSInt Result = Value; |
| 1201 | // Figure out if this is a truncate, extend or noop cast. |
| 1202 | // If the input is signed, do a sign extend, noop, or truncate. |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1203 | Result = Result.extOrTrunc(DestWidth); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 1204 | Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType()); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1205 | return Result; |
| 1206 | } |
| 1207 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1208 | static bool HandleIntToFloatCast(EvalInfo &Info, const Expr *E, |
| 1209 | QualType SrcType, const APSInt &Value, |
| 1210 | QualType DestType, APFloat &Result) { |
| 1211 | Result = APFloat(Info.Ctx.getFloatTypeSemantics(DestType), 1); |
| 1212 | if (Result.convertFromAPInt(Value, Value.isSigned(), |
| 1213 | APFloat::rmNearestTiesToEven) |
| 1214 | & APFloat::opOverflow) |
Eli Friedman | 26dc97c | 2012-07-17 21:03:05 +0000 | [diff] [blame] | 1215 | HandleOverflow(Info, E, Value, DestType); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1216 | return true; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
Eli Friedman | e6a24e8 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 1219 | static bool EvalAndBitcastToAPInt(EvalInfo &Info, const Expr *E, |
| 1220 | llvm::APInt &Res) { |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1221 | APValue SVal; |
Eli Friedman | e6a24e8 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 1222 | if (!Evaluate(SVal, Info, E)) |
| 1223 | return false; |
| 1224 | if (SVal.isInt()) { |
| 1225 | Res = SVal.getInt(); |
| 1226 | return true; |
| 1227 | } |
| 1228 | if (SVal.isFloat()) { |
| 1229 | Res = SVal.getFloat().bitcastToAPInt(); |
| 1230 | return true; |
| 1231 | } |
| 1232 | if (SVal.isVector()) { |
| 1233 | QualType VecTy = E->getType(); |
| 1234 | unsigned VecSize = Info.Ctx.getTypeSize(VecTy); |
| 1235 | QualType EltTy = VecTy->castAs<VectorType>()->getElementType(); |
| 1236 | unsigned EltSize = Info.Ctx.getTypeSize(EltTy); |
| 1237 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
| 1238 | Res = llvm::APInt::getNullValue(VecSize); |
| 1239 | for (unsigned i = 0; i < SVal.getVectorLength(); i++) { |
| 1240 | APValue &Elt = SVal.getVectorElt(i); |
| 1241 | llvm::APInt EltAsInt; |
| 1242 | if (Elt.isInt()) { |
| 1243 | EltAsInt = Elt.getInt(); |
| 1244 | } else if (Elt.isFloat()) { |
| 1245 | EltAsInt = Elt.getFloat().bitcastToAPInt(); |
| 1246 | } else { |
| 1247 | // Don't try to handle vectors of anything other than int or float |
| 1248 | // (not sure if it's possible to hit this case). |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1249 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Eli Friedman | e6a24e8 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 1250 | return false; |
| 1251 | } |
| 1252 | unsigned BaseEltSize = EltAsInt.getBitWidth(); |
| 1253 | if (BigEndian) |
| 1254 | Res |= EltAsInt.zextOrTrunc(VecSize).rotr(i*EltSize+BaseEltSize); |
| 1255 | else |
| 1256 | Res |= EltAsInt.zextOrTrunc(VecSize).rotl(i*EltSize); |
| 1257 | } |
| 1258 | return true; |
| 1259 | } |
| 1260 | // Give up if the input isn't an int, float, or vector. For example, we |
| 1261 | // reject "(v4i16)(intptr_t)&a". |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1262 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Eli Friedman | e6a24e8 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 1263 | return false; |
| 1264 | } |
| 1265 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1266 | /// Cast an lvalue referring to a base subobject to a derived class, by |
| 1267 | /// truncating the lvalue's path to the given length. |
| 1268 | static bool CastToDerivedClass(EvalInfo &Info, const Expr *E, LValue &Result, |
| 1269 | const RecordDecl *TruncatedType, |
| 1270 | unsigned TruncatedElements) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1271 | SubobjectDesignator &D = Result.Designator; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1272 | |
| 1273 | // Check we actually point to a derived class object. |
| 1274 | if (TruncatedElements == D.Entries.size()) |
| 1275 | return true; |
| 1276 | assert(TruncatedElements >= D.MostDerivedPathLength && |
| 1277 | "not casting to a derived class"); |
| 1278 | if (!Result.checkSubobject(Info, E, CSK_Derived)) |
| 1279 | return false; |
| 1280 | |
| 1281 | // 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] | 1282 | const RecordDecl *RD = TruncatedType; |
| 1283 | for (unsigned I = TruncatedElements, N = D.Entries.size(); I != N; ++I) { |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1284 | if (RD->isInvalidDecl()) return false; |
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 | |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1297 | static bool 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) { |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1301 | if (!RL) { |
| 1302 | if (Derived->isInvalidDecl()) return false; |
| 1303 | RL = &Info.Ctx.getASTRecordLayout(Derived); |
| 1304 | } |
| 1305 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1306 | Obj.getLValueOffset() += RL->getBaseClassOffset(Base); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1307 | Obj.addDecl(Info, E, Base, /*Virtual*/ false); |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1308 | return true; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1309 | } |
| 1310 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1311 | static bool HandleLValueBase(EvalInfo &Info, const Expr *E, LValue &Obj, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1312 | const CXXRecordDecl *DerivedDecl, |
| 1313 | const CXXBaseSpecifier *Base) { |
| 1314 | const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl(); |
| 1315 | |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1316 | if (!Base->isVirtual()) |
| 1317 | return HandleLValueDirectBase(Info, E, Obj, DerivedDecl, BaseDecl); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1318 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1319 | SubobjectDesignator &D = Obj.Designator; |
| 1320 | if (D.Invalid) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1321 | return false; |
| 1322 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1323 | // Extract most-derived object and corresponding type. |
| 1324 | DerivedDecl = D.MostDerivedType->getAsCXXRecordDecl(); |
| 1325 | if (!CastToDerivedClass(Info, E, Obj, DerivedDecl, D.MostDerivedPathLength)) |
| 1326 | return false; |
| 1327 | |
| 1328 | // Find the virtual base class. |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1329 | if (DerivedDecl->isInvalidDecl()) return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1330 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(DerivedDecl); |
| 1331 | Obj.getLValueOffset() += Layout.getVBaseClassOffset(BaseDecl); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1332 | Obj.addDecl(Info, E, BaseDecl, /*Virtual*/ true); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1333 | return true; |
| 1334 | } |
| 1335 | |
| 1336 | /// Update LVal to refer to the given field, which must be a member of the type |
| 1337 | /// currently described by LVal. |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1338 | static bool HandleLValueMember(EvalInfo &Info, const Expr *E, LValue &LVal, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1339 | const FieldDecl *FD, |
| 1340 | const ASTRecordLayout *RL = 0) { |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1341 | if (!RL) { |
| 1342 | if (FD->getParent()->isInvalidDecl()) return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1343 | RL = &Info.Ctx.getASTRecordLayout(FD->getParent()); |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1344 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1345 | |
| 1346 | unsigned I = FD->getFieldIndex(); |
| 1347 | LVal.Offset += Info.Ctx.toCharUnitsFromBits(RL->getFieldOffset(I)); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1348 | LVal.addDecl(Info, E, FD); |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1349 | return true; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 1352 | /// Update LVal to refer to the given indirect field. |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1353 | static bool HandleLValueIndirectMember(EvalInfo &Info, const Expr *E, |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 1354 | LValue &LVal, |
| 1355 | const IndirectFieldDecl *IFD) { |
| 1356 | for (IndirectFieldDecl::chain_iterator C = IFD->chain_begin(), |
| 1357 | CE = IFD->chain_end(); C != CE; ++C) |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1358 | if (!HandleLValueMember(Info, E, LVal, cast<FieldDecl>(*C))) |
| 1359 | return false; |
| 1360 | return true; |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1363 | /// Get the size of the given type in char units. |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1364 | static bool HandleSizeof(EvalInfo &Info, SourceLocation Loc, |
| 1365 | QualType Type, CharUnits &Size) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1366 | // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc |
| 1367 | // extension. |
| 1368 | if (Type->isVoidType() || Type->isFunctionType()) { |
| 1369 | Size = CharUnits::One(); |
| 1370 | return true; |
| 1371 | } |
| 1372 | |
| 1373 | if (!Type->isConstantSizeType()) { |
| 1374 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1375 | // FIXME: Better diagnostic. |
| 1376 | Info.Diag(Loc); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1377 | return false; |
| 1378 | } |
| 1379 | |
| 1380 | Size = Info.Ctx.getTypeSizeInChars(Type); |
| 1381 | return true; |
| 1382 | } |
| 1383 | |
| 1384 | /// Update a pointer value to model pointer arithmetic. |
| 1385 | /// \param Info - Information about the ongoing evaluation. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1386 | /// \param E - The expression being evaluated, for diagnostic purposes. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1387 | /// \param LVal - The pointer value to be updated. |
| 1388 | /// \param EltTy - The pointee type represented by LVal. |
| 1389 | /// \param Adjustment - The adjustment, in objects of type EltTy, to add. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1390 | static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E, |
| 1391 | LValue &LVal, QualType EltTy, |
| 1392 | int64_t Adjustment) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1393 | CharUnits SizeOfPointee; |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1394 | if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfPointee)) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1395 | return false; |
| 1396 | |
| 1397 | // Compute the new offset in the appropriate width. |
| 1398 | LVal.Offset += Adjustment * SizeOfPointee; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1399 | LVal.adjustIndex(Info, E, Adjustment); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1400 | return true; |
| 1401 | } |
| 1402 | |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1403 | /// Update an lvalue to refer to a component of a complex number. |
| 1404 | /// \param Info - Information about the ongoing evaluation. |
| 1405 | /// \param LVal - The lvalue to be updated. |
| 1406 | /// \param EltTy - The complex number's component type. |
| 1407 | /// \param Imag - False for the real component, true for the imaginary. |
| 1408 | static bool HandleLValueComplexElement(EvalInfo &Info, const Expr *E, |
| 1409 | LValue &LVal, QualType EltTy, |
| 1410 | bool Imag) { |
| 1411 | if (Imag) { |
| 1412 | CharUnits SizeOfComponent; |
| 1413 | if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfComponent)) |
| 1414 | return false; |
| 1415 | LVal.Offset += SizeOfComponent; |
| 1416 | } |
| 1417 | LVal.addComplex(Info, E, EltTy, Imag); |
| 1418 | return true; |
| 1419 | } |
| 1420 | |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1421 | /// Try to evaluate the initializer for a variable declaration. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1422 | static bool EvaluateVarDeclInit(EvalInfo &Info, const Expr *E, |
| 1423 | const VarDecl *VD, |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1424 | CallStackFrame *Frame, APValue &Result) { |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1425 | // If this is a parameter to an active constexpr function call, perform |
| 1426 | // argument substitution. |
| 1427 | if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(VD)) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1428 | // Assume arguments of a potential constant expression are unknown |
| 1429 | // constant expressions. |
| 1430 | if (Info.CheckingPotentialConstantExpression) |
| 1431 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1432 | if (!Frame || !Frame->Arguments) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1433 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1434 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1435 | } |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1436 | Result = Frame->Arguments[PVD->getFunctionScopeIndex()]; |
| 1437 | return true; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1438 | } |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1439 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1440 | // Dig out the initializer, and use the declaration which it's attached to. |
| 1441 | const Expr *Init = VD->getAnyInitializer(VD); |
| 1442 | if (!Init || Init->isValueDependent()) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1443 | // If we're checking a potential constant expression, the variable could be |
| 1444 | // initialized later. |
| 1445 | if (!Info.CheckingPotentialConstantExpression) |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1446 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1447 | return false; |
| 1448 | } |
| 1449 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1450 | // If we're currently evaluating the initializer of this declaration, use that |
| 1451 | // in-flight value. |
| 1452 | if (Info.EvaluatingDecl == VD) { |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1453 | Result = *Info.EvaluatingDeclValue; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1454 | return !Result.isUninit(); |
| 1455 | } |
| 1456 | |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1457 | // Never evaluate the initializer of a weak variable. We can't be sure that |
| 1458 | // this is the definition which will be used. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1459 | if (VD->isWeak()) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1460 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1461 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1462 | } |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1463 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1464 | // Check that we can fold the initializer. In C++, we will have already done |
| 1465 | // this in the cases where it matters for conformance. |
| 1466 | llvm::SmallVector<PartialDiagnosticAt, 8> Notes; |
| 1467 | if (!VD->evaluateValue(Notes)) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1468 | Info.Diag(E, diag::note_constexpr_var_init_non_constant, |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1469 | Notes.size() + 1) << VD; |
| 1470 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1471 | Info.addNotes(Notes); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1472 | return false; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1473 | } else if (!VD->checkInitIsICE()) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1474 | Info.CCEDiag(E, diag::note_constexpr_var_init_non_constant, |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1475 | Notes.size() + 1) << VD; |
| 1476 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1477 | Info.addNotes(Notes); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1478 | } |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1479 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1480 | Result = *VD->getEvaluatedValue(); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1481 | return true; |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1482 | } |
| 1483 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1484 | static bool IsConstNonVolatile(QualType T) { |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1485 | Qualifiers Quals = T.getQualifiers(); |
| 1486 | return Quals.hasConst() && !Quals.hasVolatile(); |
| 1487 | } |
| 1488 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1489 | /// Get the base index of the given base class within an APValue representing |
| 1490 | /// the given derived class. |
| 1491 | static unsigned getBaseIndex(const CXXRecordDecl *Derived, |
| 1492 | const CXXRecordDecl *Base) { |
| 1493 | Base = Base->getCanonicalDecl(); |
| 1494 | unsigned Index = 0; |
| 1495 | for (CXXRecordDecl::base_class_const_iterator I = Derived->bases_begin(), |
| 1496 | E = Derived->bases_end(); I != E; ++I, ++Index) { |
| 1497 | if (I->getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base) |
| 1498 | return Index; |
| 1499 | } |
| 1500 | |
| 1501 | llvm_unreachable("base class missing from derived class's bases list"); |
| 1502 | } |
| 1503 | |
Richard Smith | fe58720 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 1504 | /// Extract the value of a character from a string literal. CharType is used to |
| 1505 | /// determine the expected signedness of the result -- a string literal used to |
| 1506 | /// initialize an array of 'signed char' or 'unsigned char' might contain chars |
| 1507 | /// of the wrong signedness. |
Richard Smith | f3908f2 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 1508 | static APSInt ExtractStringLiteralCharacter(EvalInfo &Info, const Expr *Lit, |
Richard Smith | fe58720 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 1509 | uint64_t Index, QualType CharType) { |
Richard Smith | f3908f2 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 1510 | // FIXME: Support PredefinedExpr, ObjCEncodeExpr, MakeStringConstant |
| 1511 | const StringLiteral *S = dyn_cast<StringLiteral>(Lit); |
| 1512 | assert(S && "unexpected string literal expression kind"); |
Richard Smith | fe58720 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 1513 | assert(CharType->isIntegerType() && "unexpected character type"); |
Richard Smith | f3908f2 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 1514 | |
| 1515 | APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(), |
Richard Smith | fe58720 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 1516 | CharType->isUnsignedIntegerType()); |
Richard Smith | f3908f2 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 1517 | if (Index < S->getLength()) |
| 1518 | Value = S->getCodeUnit(Index); |
| 1519 | return Value; |
| 1520 | } |
| 1521 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1522 | /// Extract the designated sub-object of an rvalue. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1523 | static bool ExtractSubobject(EvalInfo &Info, const Expr *E, |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1524 | APValue &Obj, QualType ObjType, |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1525 | const SubobjectDesignator &Sub, QualType SubType) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1526 | if (Sub.Invalid) |
| 1527 | // A diagnostic will have already been produced. |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1528 | return false; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1529 | if (Sub.isOnePastTheEnd()) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1530 | Info.Diag(E, Info.getLangOpts().CPlusPlus11 ? |
Matt Beaumont-Gay | aa5d533 | 2011-12-21 19:36:37 +0000 | [diff] [blame] | 1531 | (unsigned)diag::note_constexpr_read_past_end : |
| 1532 | (unsigned)diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1533 | return false; |
| 1534 | } |
Richard Smith | f64699e | 2011-11-11 08:28:03 +0000 | [diff] [blame] | 1535 | if (Sub.Entries.empty()) |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1536 | return true; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1537 | if (Info.CheckingPotentialConstantExpression && Obj.isUninit()) |
| 1538 | // This object might be initialized later. |
| 1539 | return false; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1540 | |
Richard Smith | 0069b84 | 2012-03-10 00:28:11 +0000 | [diff] [blame] | 1541 | APValue *O = &Obj; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1542 | // Walk the designator's path to find the subobject. |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1543 | for (unsigned I = 0, N = Sub.Entries.size(); I != N; ++I) { |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1544 | if (ObjType->isArrayType()) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1545 | // Next subobject is an array element. |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1546 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(ObjType); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1547 | assert(CAT && "vla in literal type?"); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1548 | uint64_t Index = Sub.Entries[I].ArrayIndex; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1549 | if (CAT->getSize().ule(Index)) { |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1550 | // Note, it should not be possible to form a pointer with a valid |
| 1551 | // designator which points more than one past the end of the array. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1552 | Info.Diag(E, Info.getLangOpts().CPlusPlus11 ? |
Matt Beaumont-Gay | aa5d533 | 2011-12-21 19:36:37 +0000 | [diff] [blame] | 1553 | (unsigned)diag::note_constexpr_read_past_end : |
| 1554 | (unsigned)diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1555 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1556 | } |
Richard Smith | f3908f2 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 1557 | // An array object is represented as either an Array APValue or as an |
| 1558 | // LValue which refers to a string literal. |
| 1559 | if (O->isLValue()) { |
| 1560 | assert(I == N - 1 && "extracting subobject of character?"); |
| 1561 | assert(!O->hasLValuePath() || O->getLValuePath().empty()); |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1562 | Obj = APValue(ExtractStringLiteralCharacter( |
Richard Smith | fe58720 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 1563 | Info, O->getLValueBase().get<const Expr*>(), Index, SubType)); |
Richard Smith | f3908f2 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 1564 | return true; |
| 1565 | } else if (O->getArrayInitializedElts() > Index) |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1566 | O = &O->getArrayInitializedElt(Index); |
| 1567 | else |
| 1568 | O = &O->getArrayFiller(); |
| 1569 | ObjType = CAT->getElementType(); |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1570 | } else if (ObjType->isAnyComplexType()) { |
| 1571 | // Next subobject is a complex number. |
| 1572 | uint64_t Index = Sub.Entries[I].ArrayIndex; |
| 1573 | if (Index > 1) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1574 | Info.Diag(E, Info.getLangOpts().CPlusPlus11 ? |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1575 | (unsigned)diag::note_constexpr_read_past_end : |
| 1576 | (unsigned)diag::note_invalid_subexpr_in_const_expr); |
| 1577 | return false; |
| 1578 | } |
| 1579 | assert(I == N - 1 && "extracting subobject of scalar?"); |
| 1580 | if (O->isComplexInt()) { |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1581 | Obj = APValue(Index ? O->getComplexIntImag() |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1582 | : O->getComplexIntReal()); |
| 1583 | } else { |
| 1584 | assert(O->isComplexFloat()); |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1585 | Obj = APValue(Index ? O->getComplexFloatImag() |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1586 | : O->getComplexFloatReal()); |
| 1587 | } |
| 1588 | return true; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1589 | } else if (const FieldDecl *Field = getAsField(Sub.Entries[I])) { |
Richard Smith | b4e5e28 | 2012-02-09 03:29:58 +0000 | [diff] [blame] | 1590 | if (Field->isMutable()) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1591 | Info.Diag(E, diag::note_constexpr_ltor_mutable, 1) |
Richard Smith | b4e5e28 | 2012-02-09 03:29:58 +0000 | [diff] [blame] | 1592 | << Field; |
| 1593 | Info.Note(Field->getLocation(), diag::note_declared_at); |
| 1594 | return false; |
| 1595 | } |
| 1596 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1597 | // Next subobject is a class, struct or union field. |
| 1598 | RecordDecl *RD = ObjType->castAs<RecordType>()->getDecl(); |
| 1599 | if (RD->isUnion()) { |
| 1600 | const FieldDecl *UnionField = O->getUnionField(); |
| 1601 | if (!UnionField || |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1602 | UnionField->getCanonicalDecl() != Field->getCanonicalDecl()) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1603 | Info.Diag(E, diag::note_constexpr_read_inactive_union_member) |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1604 | << Field << !UnionField << UnionField; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1605 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1606 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1607 | O = &O->getUnionValue(); |
| 1608 | } else |
| 1609 | O = &O->getStructField(Field->getFieldIndex()); |
| 1610 | ObjType = Field->getType(); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1611 | |
| 1612 | if (ObjType.isVolatileQualified()) { |
| 1613 | if (Info.getLangOpts().CPlusPlus) { |
| 1614 | // FIXME: Include a description of the path to the volatile subobject. |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1615 | Info.Diag(E, diag::note_constexpr_ltor_volatile_obj, 1) |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1616 | << 2 << Field; |
| 1617 | Info.Note(Field->getLocation(), diag::note_declared_at); |
| 1618 | } else { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1619 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1620 | } |
| 1621 | return false; |
| 1622 | } |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1623 | } else { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1624 | // Next subobject is a base class. |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1625 | const CXXRecordDecl *Derived = ObjType->getAsCXXRecordDecl(); |
| 1626 | const CXXRecordDecl *Base = getAsBaseClass(Sub.Entries[I]); |
| 1627 | O = &O->getStructBase(getBaseIndex(Derived, Base)); |
| 1628 | ObjType = Info.Ctx.getRecordType(Base); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1629 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1630 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1631 | if (O->isUninit()) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1632 | if (!Info.CheckingPotentialConstantExpression) |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1633 | Info.Diag(E, diag::note_constexpr_read_uninit); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1634 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1635 | } |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1636 | } |
| 1637 | |
Richard Smith | 0069b84 | 2012-03-10 00:28:11 +0000 | [diff] [blame] | 1638 | // This may look super-stupid, but it serves an important purpose: if we just |
| 1639 | // swapped Obj and *O, we'd create an object which had itself as a subobject. |
| 1640 | // To avoid the leak, we ensure that Tmp ends up owning the original complete |
| 1641 | // object, which is destroyed by Tmp's destructor. |
| 1642 | APValue Tmp; |
| 1643 | O->swap(Tmp); |
| 1644 | Obj.swap(Tmp); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1645 | return true; |
| 1646 | } |
| 1647 | |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 1648 | /// Find the position where two subobject designators diverge, or equivalently |
| 1649 | /// the length of the common initial subsequence. |
| 1650 | static unsigned FindDesignatorMismatch(QualType ObjType, |
| 1651 | const SubobjectDesignator &A, |
| 1652 | const SubobjectDesignator &B, |
| 1653 | bool &WasArrayIndex) { |
| 1654 | unsigned I = 0, N = std::min(A.Entries.size(), B.Entries.size()); |
| 1655 | for (/**/; I != N; ++I) { |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1656 | if (!ObjType.isNull() && |
| 1657 | (ObjType->isArrayType() || ObjType->isAnyComplexType())) { |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 1658 | // Next subobject is an array element. |
| 1659 | if (A.Entries[I].ArrayIndex != B.Entries[I].ArrayIndex) { |
| 1660 | WasArrayIndex = true; |
| 1661 | return I; |
| 1662 | } |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1663 | if (ObjType->isAnyComplexType()) |
| 1664 | ObjType = ObjType->castAs<ComplexType>()->getElementType(); |
| 1665 | else |
| 1666 | ObjType = ObjType->castAsArrayTypeUnsafe()->getElementType(); |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 1667 | } else { |
| 1668 | if (A.Entries[I].BaseOrMember != B.Entries[I].BaseOrMember) { |
| 1669 | WasArrayIndex = false; |
| 1670 | return I; |
| 1671 | } |
| 1672 | if (const FieldDecl *FD = getAsField(A.Entries[I])) |
| 1673 | // Next subobject is a field. |
| 1674 | ObjType = FD->getType(); |
| 1675 | else |
| 1676 | // Next subobject is a base class. |
| 1677 | ObjType = QualType(); |
| 1678 | } |
| 1679 | } |
| 1680 | WasArrayIndex = false; |
| 1681 | return I; |
| 1682 | } |
| 1683 | |
| 1684 | /// Determine whether the given subobject designators refer to elements of the |
| 1685 | /// same array object. |
| 1686 | static bool AreElementsOfSameArray(QualType ObjType, |
| 1687 | const SubobjectDesignator &A, |
| 1688 | const SubobjectDesignator &B) { |
| 1689 | if (A.Entries.size() != B.Entries.size()) |
| 1690 | return false; |
| 1691 | |
| 1692 | bool IsArray = A.MostDerivedArraySize != 0; |
| 1693 | if (IsArray && A.MostDerivedPathLength != A.Entries.size()) |
| 1694 | // A is a subobject of the array element. |
| 1695 | return false; |
| 1696 | |
| 1697 | // If A (and B) designates an array element, the last entry will be the array |
| 1698 | // index. That doesn't have to match. Otherwise, we're in the 'implicit array |
| 1699 | // of length 1' case, and the entire path must match. |
| 1700 | bool WasArrayIndex; |
| 1701 | unsigned CommonLength = FindDesignatorMismatch(ObjType, A, B, WasArrayIndex); |
| 1702 | return CommonLength >= A.Entries.size() - IsArray; |
| 1703 | } |
| 1704 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1705 | /// HandleLValueToRValueConversion - Perform an lvalue-to-rvalue conversion on |
| 1706 | /// the given lvalue. This can also be used for 'lvalue-to-lvalue' conversions |
| 1707 | /// for looking up the glvalue referred to by an entity of reference type. |
| 1708 | /// |
| 1709 | /// \param Info - Information about the ongoing evaluation. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1710 | /// \param Conv - The expression for which we are performing the conversion. |
| 1711 | /// Used for diagnostics. |
Richard Smith | 9ec7197 | 2012-02-05 01:23:16 +0000 | [diff] [blame] | 1712 | /// \param Type - The type we expect this conversion to produce, before |
| 1713 | /// stripping cv-qualifiers in the case of a non-clas type. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1714 | /// \param LVal - The glvalue on which we are attempting to perform this action. |
| 1715 | /// \param RVal - The produced value will be placed here. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1716 | static bool HandleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv, |
| 1717 | QualType Type, |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1718 | const LValue &LVal, APValue &RVal) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1719 | if (LVal.Designator.Invalid) |
| 1720 | // A diagnostic will have already been produced. |
| 1721 | return false; |
| 1722 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1723 | const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1724 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1725 | if (!LVal.Base) { |
| 1726 | // FIXME: Indirection through a null pointer deserves a specific diagnostic. |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1727 | Info.Diag(Conv, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1728 | return false; |
| 1729 | } |
| 1730 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1731 | CallStackFrame *Frame = 0; |
| 1732 | if (LVal.CallIndex) { |
| 1733 | Frame = Info.getCallFrame(LVal.CallIndex); |
| 1734 | if (!Frame) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1735 | Info.Diag(Conv, diag::note_constexpr_lifetime_ended, 1) << !Base; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1736 | NoteLValueLocation(Info, LVal.Base); |
| 1737 | return false; |
| 1738 | } |
| 1739 | } |
| 1740 | |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1741 | // C++11 DR1311: An lvalue-to-rvalue conversion on a volatile-qualified type |
| 1742 | // is not a constant expression (even if the object is non-volatile). We also |
| 1743 | // apply this rule to C++98, in order to conform to the expected 'volatile' |
| 1744 | // semantics. |
| 1745 | if (Type.isVolatileQualified()) { |
| 1746 | if (Info.getLangOpts().CPlusPlus) |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1747 | Info.Diag(Conv, diag::note_constexpr_ltor_volatile_type) << Type; |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1748 | else |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1749 | Info.Diag(Conv); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1750 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1751 | } |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1752 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1753 | if (const ValueDecl *D = LVal.Base.dyn_cast<const ValueDecl*>()) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1754 | // In C++98, const, non-volatile integers initialized with ICEs are ICEs. |
| 1755 | // In C++11, constexpr, non-volatile variables initialized with constant |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1756 | // expressions are constant expressions too. Inside constexpr functions, |
| 1757 | // parameters are constant expressions even if they're non-const. |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1758 | // 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] | 1759 | const VarDecl *VD = dyn_cast<VarDecl>(D); |
Douglas Gregor | d2008e2 | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 1760 | if (VD) { |
| 1761 | if (const VarDecl *VDef = VD->getDefinition(Info.Ctx)) |
| 1762 | VD = VDef; |
| 1763 | } |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1764 | if (!VD || VD->isInvalidDecl()) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1765 | Info.Diag(Conv); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1766 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1767 | } |
| 1768 | |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1769 | // DR1313: If the object is volatile-qualified but the glvalue was not, |
| 1770 | // behavior is undefined so the result is not a constant expression. |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1771 | QualType VT = VD->getType(); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1772 | if (VT.isVolatileQualified()) { |
| 1773 | if (Info.getLangOpts().CPlusPlus) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1774 | Info.Diag(Conv, diag::note_constexpr_ltor_volatile_obj, 1) << 1 << VD; |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1775 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1776 | } else { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1777 | Info.Diag(Conv); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1778 | } |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1779 | return false; |
| 1780 | } |
| 1781 | |
| 1782 | if (!isa<ParmVarDecl>(VD)) { |
| 1783 | if (VD->isConstexpr()) { |
| 1784 | // OK, we can read this variable. |
| 1785 | } else if (VT->isIntegralOrEnumerationType()) { |
| 1786 | if (!VT.isConstQualified()) { |
| 1787 | if (Info.getLangOpts().CPlusPlus) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1788 | Info.Diag(Conv, diag::note_constexpr_ltor_non_const_int, 1) << VD; |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1789 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1790 | } else { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1791 | Info.Diag(Conv); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1792 | } |
| 1793 | return false; |
| 1794 | } |
| 1795 | } else if (VT->isFloatingType() && VT.isConstQualified()) { |
| 1796 | // We support folding of const floating-point types, in order to make |
| 1797 | // static const data members of such types (supported as an extension) |
| 1798 | // more useful. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1799 | if (Info.getLangOpts().CPlusPlus11) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1800 | Info.CCEDiag(Conv, diag::note_constexpr_ltor_non_constexpr, 1) << VD; |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1801 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1802 | } else { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1803 | Info.CCEDiag(Conv); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1804 | } |
| 1805 | } else { |
| 1806 | // FIXME: Allow folding of values of any literal type in all languages. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1807 | if (Info.getLangOpts().CPlusPlus11) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1808 | Info.Diag(Conv, diag::note_constexpr_ltor_non_constexpr, 1) << VD; |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1809 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1810 | } else { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1811 | Info.Diag(Conv); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1812 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1813 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1814 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1815 | } |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1816 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1817 | if (!EvaluateVarDeclInit(Info, Conv, VD, Frame, RVal)) |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1818 | return false; |
| 1819 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1820 | if (isa<ParmVarDecl>(VD) || !VD->getAnyInitializer()->isLValue()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1821 | return ExtractSubobject(Info, Conv, RVal, VT, LVal.Designator, Type); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1822 | |
| 1823 | // The declaration was initialized by an lvalue, with no lvalue-to-rvalue |
| 1824 | // conversion. This happens when the declaration and the lvalue should be |
| 1825 | // considered synonymous, for instance when initializing an array of char |
| 1826 | // from a string literal. Continue as if the initializer lvalue was the |
| 1827 | // value we were originally given. |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1828 | assert(RVal.getLValueOffset().isZero() && |
| 1829 | "offset for lvalue init of non-reference"); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1830 | Base = RVal.getLValueBase().get<const Expr*>(); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1831 | |
| 1832 | if (unsigned CallIndex = RVal.getLValueCallIndex()) { |
| 1833 | Frame = Info.getCallFrame(CallIndex); |
| 1834 | if (!Frame) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1835 | Info.Diag(Conv, diag::note_constexpr_lifetime_ended, 1) << !Base; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1836 | NoteLValueLocation(Info, RVal.getLValueBase()); |
| 1837 | return false; |
| 1838 | } |
| 1839 | } else { |
| 1840 | Frame = 0; |
| 1841 | } |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1842 | } |
| 1843 | |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1844 | // Volatile temporary objects cannot be read in constant expressions. |
| 1845 | if (Base->getType().isVolatileQualified()) { |
| 1846 | if (Info.getLangOpts().CPlusPlus) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1847 | Info.Diag(Conv, diag::note_constexpr_ltor_volatile_obj, 1) << 0; |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1848 | Info.Note(Base->getExprLoc(), diag::note_constexpr_temporary_here); |
| 1849 | } else { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1850 | Info.Diag(Conv); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1851 | } |
| 1852 | return false; |
| 1853 | } |
| 1854 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1855 | if (Frame) { |
| 1856 | // If this is a temporary expression with a nontrivial initializer, grab the |
| 1857 | // value from the relevant stack frame. |
| 1858 | RVal = Frame->Temporaries[Base]; |
| 1859 | } else if (const CompoundLiteralExpr *CLE |
| 1860 | = dyn_cast<CompoundLiteralExpr>(Base)) { |
| 1861 | // In C99, a CompoundLiteralExpr is an lvalue, and we defer evaluating the |
| 1862 | // initializer until now for such expressions. Such an expression can't be |
| 1863 | // an ICE in C, so this only matters for fold. |
| 1864 | assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?"); |
| 1865 | if (!Evaluate(RVal, Info, CLE->getInitializer())) |
| 1866 | return false; |
Richard Smith | f3908f2 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 1867 | } else if (isa<StringLiteral>(Base)) { |
| 1868 | // We represent a string literal array as an lvalue pointing at the |
| 1869 | // corresponding expression, rather than building an array of chars. |
| 1870 | // FIXME: Support PredefinedExpr, ObjCEncodeExpr, MakeStringConstant |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1871 | RVal = APValue(Base, CharUnits::Zero(), APValue::NoLValuePath(), 0); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1872 | } else { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1873 | Info.Diag(Conv, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1874 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1875 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1876 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1877 | return ExtractSubobject(Info, Conv, RVal, Base->getType(), LVal.Designator, |
| 1878 | Type); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1879 | } |
| 1880 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1881 | /// Build an lvalue for the object argument of a member function call. |
| 1882 | static bool EvaluateObjectArgument(EvalInfo &Info, const Expr *Object, |
| 1883 | LValue &This) { |
| 1884 | if (Object->getType()->isPointerType()) |
| 1885 | return EvaluatePointer(Object, This, Info); |
| 1886 | |
| 1887 | if (Object->isGLValue()) |
| 1888 | return EvaluateLValue(Object, This, Info); |
| 1889 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1890 | if (Object->getType()->isLiteralType()) |
| 1891 | return EvaluateTemporary(Object, This, Info); |
| 1892 | |
| 1893 | return false; |
| 1894 | } |
| 1895 | |
| 1896 | /// HandleMemberPointerAccess - Evaluate a member access operation and build an |
| 1897 | /// lvalue referring to the result. |
| 1898 | /// |
| 1899 | /// \param Info - Information about the ongoing evaluation. |
| 1900 | /// \param BO - The member pointer access operation. |
| 1901 | /// \param LV - Filled in with a reference to the resulting object. |
| 1902 | /// \param IncludeMember - Specifies whether the member itself is included in |
| 1903 | /// the resulting LValue subobject designator. This is not possible when |
| 1904 | /// creating a bound member function. |
| 1905 | /// \return The field or method declaration to which the member pointer refers, |
| 1906 | /// or 0 if evaluation fails. |
| 1907 | static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info, |
| 1908 | const BinaryOperator *BO, |
| 1909 | LValue &LV, |
| 1910 | bool IncludeMember = true) { |
| 1911 | assert(BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI); |
| 1912 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1913 | bool EvalObjOK = EvaluateObjectArgument(Info, BO->getLHS(), LV); |
| 1914 | if (!EvalObjOK && !Info.keepEvaluatingAfterFailure()) |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1915 | return 0; |
| 1916 | |
| 1917 | MemberPtr MemPtr; |
| 1918 | if (!EvaluateMemberPointer(BO->getRHS(), MemPtr, Info)) |
| 1919 | return 0; |
| 1920 | |
| 1921 | // C++11 [expr.mptr.oper]p6: If the second operand is the null pointer to |
| 1922 | // member value, the behavior is undefined. |
| 1923 | if (!MemPtr.getDecl()) |
| 1924 | return 0; |
| 1925 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1926 | if (!EvalObjOK) |
| 1927 | return 0; |
| 1928 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1929 | if (MemPtr.isDerivedMember()) { |
| 1930 | // This is a member of some derived class. Truncate LV appropriately. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1931 | // The end of the derived-to-base path for the base object must match the |
| 1932 | // derived-to-base path for the member pointer. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1933 | if (LV.Designator.MostDerivedPathLength + MemPtr.Path.size() > |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1934 | LV.Designator.Entries.size()) |
| 1935 | return 0; |
| 1936 | unsigned PathLengthToMember = |
| 1937 | LV.Designator.Entries.size() - MemPtr.Path.size(); |
| 1938 | for (unsigned I = 0, N = MemPtr.Path.size(); I != N; ++I) { |
| 1939 | const CXXRecordDecl *LVDecl = getAsBaseClass( |
| 1940 | LV.Designator.Entries[PathLengthToMember + I]); |
| 1941 | const CXXRecordDecl *MPDecl = MemPtr.Path[I]; |
| 1942 | if (LVDecl->getCanonicalDecl() != MPDecl->getCanonicalDecl()) |
| 1943 | return 0; |
| 1944 | } |
| 1945 | |
| 1946 | // Truncate the lvalue to the appropriate derived class. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1947 | if (!CastToDerivedClass(Info, BO, LV, MemPtr.getContainingRecord(), |
| 1948 | PathLengthToMember)) |
| 1949 | return 0; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1950 | } else if (!MemPtr.Path.empty()) { |
| 1951 | // Extend the LValue path with the member pointer's path. |
| 1952 | LV.Designator.Entries.reserve(LV.Designator.Entries.size() + |
| 1953 | MemPtr.Path.size() + IncludeMember); |
| 1954 | |
| 1955 | // Walk down to the appropriate base class. |
| 1956 | QualType LVType = BO->getLHS()->getType(); |
| 1957 | if (const PointerType *PT = LVType->getAs<PointerType>()) |
| 1958 | LVType = PT->getPointeeType(); |
| 1959 | const CXXRecordDecl *RD = LVType->getAsCXXRecordDecl(); |
| 1960 | assert(RD && "member pointer access on non-class-type expression"); |
| 1961 | // The first class in the path is that of the lvalue. |
| 1962 | for (unsigned I = 1, N = MemPtr.Path.size(); I != N; ++I) { |
| 1963 | const CXXRecordDecl *Base = MemPtr.Path[N - I - 1]; |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1964 | if (!HandleLValueDirectBase(Info, BO, LV, RD, Base)) |
| 1965 | return 0; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1966 | RD = Base; |
| 1967 | } |
| 1968 | // Finally cast to the class containing the member. |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1969 | if (!HandleLValueDirectBase(Info, BO, LV, RD, MemPtr.getContainingRecord())) |
| 1970 | return 0; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1971 | } |
| 1972 | |
| 1973 | // Add the member. Note that we cannot build bound member functions here. |
| 1974 | if (IncludeMember) { |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1975 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(MemPtr.getDecl())) { |
| 1976 | if (!HandleLValueMember(Info, BO, LV, FD)) |
| 1977 | return 0; |
| 1978 | } else if (const IndirectFieldDecl *IFD = |
| 1979 | dyn_cast<IndirectFieldDecl>(MemPtr.getDecl())) { |
| 1980 | if (!HandleLValueIndirectMember(Info, BO, LV, IFD)) |
| 1981 | return 0; |
| 1982 | } else { |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 1983 | llvm_unreachable("can't construct reference to bound member function"); |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1984 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1985 | } |
| 1986 | |
| 1987 | return MemPtr.getDecl(); |
| 1988 | } |
| 1989 | |
| 1990 | /// HandleBaseToDerivedCast - Apply the given base-to-derived cast operation on |
| 1991 | /// the provided lvalue, which currently refers to the base object. |
| 1992 | static bool HandleBaseToDerivedCast(EvalInfo &Info, const CastExpr *E, |
| 1993 | LValue &Result) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1994 | SubobjectDesignator &D = Result.Designator; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1995 | if (D.Invalid || !Result.checkNullPointer(Info, E, CSK_Derived)) |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1996 | return false; |
| 1997 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1998 | QualType TargetQT = E->getType(); |
| 1999 | if (const PointerType *PT = TargetQT->getAs<PointerType>()) |
| 2000 | TargetQT = PT->getPointeeType(); |
| 2001 | |
| 2002 | // Check this cast lands within the final derived-to-base subobject path. |
| 2003 | if (D.MostDerivedPathLength + E->path_size() > D.Entries.size()) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 2004 | Info.CCEDiag(E, diag::note_constexpr_invalid_downcast) |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2005 | << D.MostDerivedType << TargetQT; |
| 2006 | return false; |
| 2007 | } |
| 2008 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2009 | // Check the type of the final cast. We don't need to check the path, |
| 2010 | // since a cast can only be formed if the path is unique. |
| 2011 | unsigned NewEntriesSize = D.Entries.size() - E->path_size(); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2012 | const CXXRecordDecl *TargetType = TargetQT->getAsCXXRecordDecl(); |
| 2013 | const CXXRecordDecl *FinalType; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2014 | if (NewEntriesSize == D.MostDerivedPathLength) |
| 2015 | FinalType = D.MostDerivedType->getAsCXXRecordDecl(); |
| 2016 | else |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2017 | FinalType = getAsBaseClass(D.Entries[NewEntriesSize - 1]); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2018 | if (FinalType->getCanonicalDecl() != TargetType->getCanonicalDecl()) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 2019 | Info.CCEDiag(E, diag::note_constexpr_invalid_downcast) |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2020 | << D.MostDerivedType << TargetQT; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2021 | return false; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2022 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2023 | |
| 2024 | // Truncate the lvalue to the appropriate derived class. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2025 | return CastToDerivedClass(Info, E, Result, TargetType, NewEntriesSize); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 2028 | namespace { |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2029 | enum EvalStmtResult { |
| 2030 | /// Evaluation failed. |
| 2031 | ESR_Failed, |
| 2032 | /// Hit a 'return' statement. |
| 2033 | ESR_Returned, |
| 2034 | /// Evaluation succeeded. |
| 2035 | ESR_Succeeded |
| 2036 | }; |
| 2037 | } |
| 2038 | |
| 2039 | // Evaluate a statement. |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2040 | static EvalStmtResult EvaluateStmt(APValue &Result, EvalInfo &Info, |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2041 | const Stmt *S) { |
| 2042 | switch (S->getStmtClass()) { |
| 2043 | default: |
| 2044 | return ESR_Failed; |
| 2045 | |
| 2046 | case Stmt::NullStmtClass: |
| 2047 | case Stmt::DeclStmtClass: |
| 2048 | return ESR_Succeeded; |
| 2049 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2050 | case Stmt::ReturnStmtClass: { |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2051 | const Expr *RetExpr = cast<ReturnStmt>(S)->getRetValue(); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2052 | if (!Evaluate(Result, Info, RetExpr)) |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2053 | return ESR_Failed; |
| 2054 | return ESR_Returned; |
| 2055 | } |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2056 | |
| 2057 | case Stmt::CompoundStmtClass: { |
| 2058 | const CompoundStmt *CS = cast<CompoundStmt>(S); |
| 2059 | for (CompoundStmt::const_body_iterator BI = CS->body_begin(), |
| 2060 | BE = CS->body_end(); BI != BE; ++BI) { |
| 2061 | EvalStmtResult ESR = EvaluateStmt(Result, Info, *BI); |
| 2062 | if (ESR != ESR_Succeeded) |
| 2063 | return ESR; |
| 2064 | } |
| 2065 | return ESR_Succeeded; |
| 2066 | } |
| 2067 | } |
| 2068 | } |
| 2069 | |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 2070 | /// CheckTrivialDefaultConstructor - Check whether a constructor is a trivial |
| 2071 | /// default constructor. If so, we'll fold it whether or not it's marked as |
| 2072 | /// constexpr. If it is marked as constexpr, we will never implicitly define it, |
| 2073 | /// so we need special handling. |
| 2074 | static bool CheckTrivialDefaultConstructor(EvalInfo &Info, SourceLocation Loc, |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2075 | const CXXConstructorDecl *CD, |
| 2076 | bool IsValueInitialization) { |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 2077 | if (!CD->isTrivial() || !CD->isDefaultConstructor()) |
| 2078 | return false; |
| 2079 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 2080 | // Value-initialization does not call a trivial default constructor, so such a |
| 2081 | // call is a core constant expression whether or not the constructor is |
| 2082 | // constexpr. |
| 2083 | if (!CD->isConstexpr() && !IsValueInitialization) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2084 | if (Info.getLangOpts().CPlusPlus11) { |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 2085 | // FIXME: If DiagDecl is an implicitly-declared special member function, |
| 2086 | // we should be much more explicit about why it's not constexpr. |
| 2087 | Info.CCEDiag(Loc, diag::note_constexpr_invalid_function, 1) |
| 2088 | << /*IsConstexpr*/0 << /*IsConstructor*/1 << CD; |
| 2089 | Info.Note(CD->getLocation(), diag::note_declared_at); |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 2090 | } else { |
| 2091 | Info.CCEDiag(Loc, diag::note_invalid_subexpr_in_const_expr); |
| 2092 | } |
| 2093 | } |
| 2094 | return true; |
| 2095 | } |
| 2096 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2097 | /// CheckConstexprFunction - Check that a function can be called in a constant |
| 2098 | /// expression. |
| 2099 | static bool CheckConstexprFunction(EvalInfo &Info, SourceLocation CallLoc, |
| 2100 | const FunctionDecl *Declaration, |
| 2101 | const FunctionDecl *Definition) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2102 | // Potential constant expressions can contain calls to declared, but not yet |
| 2103 | // defined, constexpr functions. |
| 2104 | if (Info.CheckingPotentialConstantExpression && !Definition && |
| 2105 | Declaration->isConstexpr()) |
| 2106 | return false; |
| 2107 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2108 | // Can we evaluate this function call? |
| 2109 | if (Definition && Definition->isConstexpr() && !Definition->isInvalidDecl()) |
| 2110 | return true; |
| 2111 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2112 | if (Info.getLangOpts().CPlusPlus11) { |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2113 | const FunctionDecl *DiagDecl = Definition ? Definition : Declaration; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2114 | // FIXME: If DiagDecl is an implicitly-declared special member function, we |
| 2115 | // should be much more explicit about why it's not constexpr. |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2116 | Info.Diag(CallLoc, diag::note_constexpr_invalid_function, 1) |
| 2117 | << DiagDecl->isConstexpr() << isa<CXXConstructorDecl>(DiagDecl) |
| 2118 | << DiagDecl; |
| 2119 | Info.Note(DiagDecl->getLocation(), diag::note_declared_at); |
| 2120 | } else { |
| 2121 | Info.Diag(CallLoc, diag::note_invalid_subexpr_in_const_expr); |
| 2122 | } |
| 2123 | return false; |
| 2124 | } |
| 2125 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2126 | namespace { |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2127 | typedef SmallVector<APValue, 8> ArgVector; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2128 | } |
| 2129 | |
| 2130 | /// EvaluateArgs - Evaluate the arguments to a function call. |
| 2131 | static bool EvaluateArgs(ArrayRef<const Expr*> Args, ArgVector &ArgValues, |
| 2132 | EvalInfo &Info) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2133 | bool Success = true; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2134 | for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end(); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2135 | I != E; ++I) { |
| 2136 | if (!Evaluate(ArgValues[I - Args.begin()], Info, *I)) { |
| 2137 | // If we're checking for a potential constant expression, evaluate all |
| 2138 | // initializers even if some of them fail. |
| 2139 | if (!Info.keepEvaluatingAfterFailure()) |
| 2140 | return false; |
| 2141 | Success = false; |
| 2142 | } |
| 2143 | } |
| 2144 | return Success; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2145 | } |
| 2146 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2147 | /// Evaluate a function call. |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2148 | static bool HandleFunctionCall(SourceLocation CallLoc, |
| 2149 | const FunctionDecl *Callee, const LValue *This, |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2150 | ArrayRef<const Expr*> Args, const Stmt *Body, |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2151 | EvalInfo &Info, APValue &Result) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2152 | ArgVector ArgValues(Args.size()); |
| 2153 | if (!EvaluateArgs(Args, ArgValues, Info)) |
| 2154 | return false; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2155 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2156 | if (!Info.CheckCallLimit(CallLoc)) |
| 2157 | return false; |
| 2158 | |
| 2159 | CallStackFrame Frame(Info, CallLoc, Callee, This, ArgValues.data()); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2160 | return EvaluateStmt(Result, Info, Body) == ESR_Returned; |
| 2161 | } |
| 2162 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2163 | /// Evaluate a constructor call. |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2164 | static bool HandleConstructorCall(SourceLocation CallLoc, const LValue &This, |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2165 | ArrayRef<const Expr*> Args, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2166 | const CXXConstructorDecl *Definition, |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2167 | EvalInfo &Info, APValue &Result) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2168 | ArgVector ArgValues(Args.size()); |
| 2169 | if (!EvaluateArgs(Args, ArgValues, Info)) |
| 2170 | return false; |
| 2171 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2172 | if (!Info.CheckCallLimit(CallLoc)) |
| 2173 | return false; |
| 2174 | |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 2175 | const CXXRecordDecl *RD = Definition->getParent(); |
| 2176 | if (RD->getNumVBases()) { |
| 2177 | Info.Diag(CallLoc, diag::note_constexpr_virtual_base) << RD; |
| 2178 | return false; |
| 2179 | } |
| 2180 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2181 | CallStackFrame Frame(Info, CallLoc, Definition, &This, ArgValues.data()); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2182 | |
| 2183 | // If it's a delegating constructor, just delegate. |
| 2184 | if (Definition->isDelegatingConstructor()) { |
| 2185 | CXXConstructorDecl::init_const_iterator I = Definition->init_begin(); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2186 | return EvaluateInPlace(Result, Info, This, (*I)->getInit()); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2187 | } |
| 2188 | |
Richard Smith | 610a60c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 2189 | // For a trivial copy or move constructor, perform an APValue copy. This is |
| 2190 | // essential for unions, where the operations performed by the constructor |
| 2191 | // cannot be represented by ctor-initializers. |
Richard Smith | 610a60c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 2192 | if (Definition->isDefaulted() && |
Douglas Gregor | f6cfe8b | 2012-02-24 07:55:51 +0000 | [diff] [blame] | 2193 | ((Definition->isCopyConstructor() && Definition->isTrivial()) || |
| 2194 | (Definition->isMoveConstructor() && Definition->isTrivial()))) { |
Richard Smith | 610a60c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 2195 | LValue RHS; |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2196 | RHS.setFrom(Info.Ctx, ArgValues[0]); |
| 2197 | return HandleLValueToRValueConversion(Info, Args[0], Args[0]->getType(), |
| 2198 | RHS, Result); |
Richard Smith | 610a60c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 2199 | } |
| 2200 | |
| 2201 | // Reserve space for the struct members. |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2202 | if (!RD->isUnion() && Result.isUninit()) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2203 | Result = APValue(APValue::UninitStruct(), RD->getNumBases(), |
| 2204 | std::distance(RD->field_begin(), RD->field_end())); |
| 2205 | |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2206 | if (RD->isInvalidDecl()) return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2207 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 2208 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2209 | bool Success = true; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2210 | unsigned BasesSeen = 0; |
| 2211 | #ifndef NDEBUG |
| 2212 | CXXRecordDecl::base_class_const_iterator BaseIt = RD->bases_begin(); |
| 2213 | #endif |
| 2214 | for (CXXConstructorDecl::init_const_iterator I = Definition->init_begin(), |
| 2215 | E = Definition->init_end(); I != E; ++I) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2216 | LValue Subobject = This; |
| 2217 | APValue *Value = &Result; |
| 2218 | |
| 2219 | // Determine the subobject to initialize. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2220 | if ((*I)->isBaseInitializer()) { |
| 2221 | QualType BaseType((*I)->getBaseClass(), 0); |
| 2222 | #ifndef NDEBUG |
| 2223 | // Non-virtual base classes are initialized in the order in the class |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 2224 | // definition. We have already checked for virtual base classes. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2225 | assert(!BaseIt->isVirtual() && "virtual base for literal type"); |
| 2226 | assert(Info.Ctx.hasSameType(BaseIt->getType(), BaseType) && |
| 2227 | "base class initializers not in expected order"); |
| 2228 | ++BaseIt; |
| 2229 | #endif |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2230 | if (!HandleLValueDirectBase(Info, (*I)->getInit(), Subobject, RD, |
| 2231 | BaseType->getAsCXXRecordDecl(), &Layout)) |
| 2232 | return false; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2233 | Value = &Result.getStructBase(BasesSeen++); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2234 | } else if (FieldDecl *FD = (*I)->getMember()) { |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2235 | if (!HandleLValueMember(Info, (*I)->getInit(), Subobject, FD, &Layout)) |
| 2236 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2237 | if (RD->isUnion()) { |
| 2238 | Result = APValue(FD); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2239 | Value = &Result.getUnionValue(); |
| 2240 | } else { |
| 2241 | Value = &Result.getStructField(FD->getFieldIndex()); |
| 2242 | } |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2243 | } else if (IndirectFieldDecl *IFD = (*I)->getIndirectMember()) { |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2244 | // Walk the indirect field decl's chain to find the object to initialize, |
| 2245 | // and make sure we've initialized every step along it. |
| 2246 | for (IndirectFieldDecl::chain_iterator C = IFD->chain_begin(), |
| 2247 | CE = IFD->chain_end(); |
| 2248 | C != CE; ++C) { |
| 2249 | FieldDecl *FD = cast<FieldDecl>(*C); |
| 2250 | CXXRecordDecl *CD = cast<CXXRecordDecl>(FD->getParent()); |
| 2251 | // Switch the union field if it differs. This happens if we had |
| 2252 | // preceding zero-initialization, and we're now initializing a union |
| 2253 | // subobject other than the first. |
| 2254 | // FIXME: In this case, the values of the other subobjects are |
| 2255 | // specified, since zero-initialization sets all padding bits to zero. |
| 2256 | if (Value->isUninit() || |
| 2257 | (Value->isUnion() && Value->getUnionField() != FD)) { |
| 2258 | if (CD->isUnion()) |
| 2259 | *Value = APValue(FD); |
| 2260 | else |
| 2261 | *Value = APValue(APValue::UninitStruct(), CD->getNumBases(), |
| 2262 | std::distance(CD->field_begin(), CD->field_end())); |
| 2263 | } |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2264 | if (!HandleLValueMember(Info, (*I)->getInit(), Subobject, FD)) |
| 2265 | return false; |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2266 | if (CD->isUnion()) |
| 2267 | Value = &Value->getUnionValue(); |
| 2268 | else |
| 2269 | Value = &Value->getStructField(FD->getFieldIndex()); |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2270 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2271 | } else { |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2272 | llvm_unreachable("unknown base initializer kind"); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2273 | } |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2274 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2275 | if (!EvaluateInPlace(*Value, Info, Subobject, (*I)->getInit(), |
| 2276 | (*I)->isBaseInitializer() |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2277 | ? CCEK_Constant : CCEK_MemberInit)) { |
| 2278 | // If we're checking for a potential constant expression, evaluate all |
| 2279 | // initializers even if some of them fail. |
| 2280 | if (!Info.keepEvaluatingAfterFailure()) |
| 2281 | return false; |
| 2282 | Success = false; |
| 2283 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2284 | } |
| 2285 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2286 | return Success; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2287 | } |
| 2288 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2289 | //===----------------------------------------------------------------------===// |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2290 | // Generic Evaluation |
| 2291 | //===----------------------------------------------------------------------===// |
| 2292 | namespace { |
| 2293 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2294 | // FIXME: RetTy is always bool. Remove it. |
| 2295 | template <class Derived, typename RetTy=bool> |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2296 | class ExprEvaluatorBase |
| 2297 | : public ConstStmtVisitor<Derived, RetTy> { |
| 2298 | private: |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2299 | RetTy DerivedSuccess(const APValue &V, const Expr *E) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2300 | return static_cast<Derived*>(this)->Success(V, E); |
| 2301 | } |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2302 | RetTy DerivedZeroInitialization(const Expr *E) { |
| 2303 | return static_cast<Derived*>(this)->ZeroInitialization(E); |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2304 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2305 | |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 2306 | // Check whether a conditional operator with a non-constant condition is a |
| 2307 | // potential constant expression. If neither arm is a potential constant |
| 2308 | // expression, then the conditional operator is not either. |
| 2309 | template<typename ConditionalOperator> |
| 2310 | void CheckPotentialConstantConditional(const ConditionalOperator *E) { |
| 2311 | assert(Info.CheckingPotentialConstantExpression); |
| 2312 | |
| 2313 | // Speculatively evaluate both arms. |
| 2314 | { |
| 2315 | llvm::SmallVector<PartialDiagnosticAt, 8> Diag; |
| 2316 | SpeculativeEvaluationRAII Speculate(Info, &Diag); |
| 2317 | |
| 2318 | StmtVisitorTy::Visit(E->getFalseExpr()); |
| 2319 | if (Diag.empty()) |
| 2320 | return; |
| 2321 | |
| 2322 | Diag.clear(); |
| 2323 | StmtVisitorTy::Visit(E->getTrueExpr()); |
| 2324 | if (Diag.empty()) |
| 2325 | return; |
| 2326 | } |
| 2327 | |
| 2328 | Error(E, diag::note_constexpr_conditional_never_const); |
| 2329 | } |
| 2330 | |
| 2331 | |
| 2332 | template<typename ConditionalOperator> |
| 2333 | bool HandleConditionalOperator(const ConditionalOperator *E) { |
| 2334 | bool BoolResult; |
| 2335 | if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info)) { |
| 2336 | if (Info.CheckingPotentialConstantExpression) |
| 2337 | CheckPotentialConstantConditional(E); |
| 2338 | return false; |
| 2339 | } |
| 2340 | |
| 2341 | Expr *EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
| 2342 | return StmtVisitorTy::Visit(EvalExpr); |
| 2343 | } |
| 2344 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2345 | protected: |
| 2346 | EvalInfo &Info; |
| 2347 | typedef ConstStmtVisitor<Derived, RetTy> StmtVisitorTy; |
| 2348 | typedef ExprEvaluatorBase ExprEvaluatorBaseTy; |
| 2349 | |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 2350 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 2351 | return Info.CCEDiag(E, D); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2352 | } |
| 2353 | |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 2354 | RetTy ZeroInitialization(const Expr *E) { return Error(E); } |
| 2355 | |
| 2356 | public: |
| 2357 | ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {} |
| 2358 | |
| 2359 | EvalInfo &getEvalInfo() { return Info; } |
| 2360 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2361 | /// Report an evaluation error. This should only be called when an error is |
| 2362 | /// first discovered. When propagating an error, just return false. |
| 2363 | bool Error(const Expr *E, diag::kind D) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 2364 | Info.Diag(E, D); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2365 | return false; |
| 2366 | } |
| 2367 | bool Error(const Expr *E) { |
| 2368 | return Error(E, diag::note_invalid_subexpr_in_const_expr); |
| 2369 | } |
| 2370 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2371 | RetTy VisitStmt(const Stmt *) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2372 | llvm_unreachable("Expression evaluator should not be called on stmts"); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2373 | } |
| 2374 | RetTy VisitExpr(const Expr *E) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2375 | return Error(E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2376 | } |
| 2377 | |
| 2378 | RetTy VisitParenExpr(const ParenExpr *E) |
| 2379 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| 2380 | RetTy VisitUnaryExtension(const UnaryOperator *E) |
| 2381 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| 2382 | RetTy VisitUnaryPlus(const UnaryOperator *E) |
| 2383 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| 2384 | RetTy VisitChooseExpr(const ChooseExpr *E) |
| 2385 | { return StmtVisitorTy::Visit(E->getChosenSubExpr(Info.Ctx)); } |
| 2386 | RetTy VisitGenericSelectionExpr(const GenericSelectionExpr *E) |
| 2387 | { return StmtVisitorTy::Visit(E->getResultExpr()); } |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 2388 | RetTy VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E) |
| 2389 | { return StmtVisitorTy::Visit(E->getReplacement()); } |
Richard Smith | 3d75ca8 | 2011-11-09 02:12:41 +0000 | [diff] [blame] | 2390 | RetTy VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) |
| 2391 | { return StmtVisitorTy::Visit(E->getExpr()); } |
Richard Smith | bc6abe9 | 2011-12-19 22:12:41 +0000 | [diff] [blame] | 2392 | // We cannot create any objects for which cleanups are required, so there is |
| 2393 | // nothing to do here; all cleanups must come from unevaluated subexpressions. |
| 2394 | RetTy VisitExprWithCleanups(const ExprWithCleanups *E) |
| 2395 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2396 | |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 2397 | RetTy VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E) { |
| 2398 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 0; |
| 2399 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
| 2400 | } |
| 2401 | RetTy VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) { |
| 2402 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 1; |
| 2403 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
| 2404 | } |
| 2405 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2406 | RetTy VisitBinaryOperator(const BinaryOperator *E) { |
| 2407 | switch (E->getOpcode()) { |
| 2408 | default: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2409 | return Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2410 | |
| 2411 | case BO_Comma: |
| 2412 | VisitIgnoredValue(E->getLHS()); |
| 2413 | return StmtVisitorTy::Visit(E->getRHS()); |
| 2414 | |
| 2415 | case BO_PtrMemD: |
| 2416 | case BO_PtrMemI: { |
| 2417 | LValue Obj; |
| 2418 | if (!HandleMemberPointerAccess(Info, E, Obj)) |
| 2419 | return false; |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2420 | APValue Result; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2421 | if (!HandleLValueToRValueConversion(Info, E, E->getType(), Obj, Result)) |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2422 | return false; |
| 2423 | return DerivedSuccess(Result, E); |
| 2424 | } |
| 2425 | } |
| 2426 | } |
| 2427 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2428 | RetTy VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) { |
Richard Smith | e92b1f4 | 2012-06-26 08:12:11 +0000 | [diff] [blame] | 2429 | // Evaluate and cache the common expression. We treat it as a temporary, |
| 2430 | // even though it's not quite the same thing. |
| 2431 | if (!Evaluate(Info.CurrentCall->Temporaries[E->getOpaqueValue()], |
| 2432 | Info, E->getCommon())) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2433 | return false; |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2434 | |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 2435 | return HandleConditionalOperator(E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2436 | } |
| 2437 | |
| 2438 | RetTy VisitConditionalOperator(const ConditionalOperator *E) { |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 2439 | bool IsBcpCall = false; |
| 2440 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
| 2441 | // the result is a constant expression if it can be folded without |
| 2442 | // side-effects. This is an important GNU extension. See GCC PR38377 |
| 2443 | // for discussion. |
| 2444 | if (const CallExpr *CallCE = |
| 2445 | dyn_cast<CallExpr>(E->getCond()->IgnoreParenCasts())) |
| 2446 | if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p) |
| 2447 | IsBcpCall = true; |
| 2448 | |
| 2449 | // Always assume __builtin_constant_p(...) ? ... : ... is a potential |
| 2450 | // constant expression; we can't check whether it's potentially foldable. |
| 2451 | if (Info.CheckingPotentialConstantExpression && IsBcpCall) |
| 2452 | return false; |
| 2453 | |
| 2454 | FoldConstant Fold(Info); |
| 2455 | |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 2456 | if (!HandleConditionalOperator(E)) |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 2457 | return false; |
| 2458 | |
| 2459 | if (IsBcpCall) |
| 2460 | Fold.Fold(Info); |
| 2461 | |
| 2462 | return true; |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2463 | } |
| 2464 | |
| 2465 | RetTy VisitOpaqueValueExpr(const OpaqueValueExpr *E) { |
Richard Smith | e92b1f4 | 2012-06-26 08:12:11 +0000 | [diff] [blame] | 2466 | APValue &Value = Info.CurrentCall->Temporaries[E]; |
| 2467 | if (Value.isUninit()) { |
Argyrios Kyrtzidis | 4278683 | 2011-12-09 02:44:48 +0000 | [diff] [blame] | 2468 | const Expr *Source = E->getSourceExpr(); |
| 2469 | if (!Source) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2470 | return Error(E); |
Argyrios Kyrtzidis | 4278683 | 2011-12-09 02:44:48 +0000 | [diff] [blame] | 2471 | if (Source == E) { // sanity checking. |
| 2472 | assert(0 && "OpaqueValueExpr recursively refers to itself"); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2473 | return Error(E); |
Argyrios Kyrtzidis | 4278683 | 2011-12-09 02:44:48 +0000 | [diff] [blame] | 2474 | } |
| 2475 | return StmtVisitorTy::Visit(Source); |
| 2476 | } |
Richard Smith | e92b1f4 | 2012-06-26 08:12:11 +0000 | [diff] [blame] | 2477 | return DerivedSuccess(Value, E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2478 | } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2479 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2480 | RetTy VisitCallExpr(const CallExpr *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2481 | const Expr *Callee = E->getCallee()->IgnoreParens(); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2482 | QualType CalleeType = Callee->getType(); |
| 2483 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2484 | const FunctionDecl *FD = 0; |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2485 | LValue *This = 0, ThisVal; |
| 2486 | llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs()); |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 2487 | bool HasQualifier = false; |
Richard Smith | 6c95787 | 2011-11-10 09:31:24 +0000 | [diff] [blame] | 2488 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2489 | // Extract function decl and 'this' pointer from the callee. |
| 2490 | if (CalleeType->isSpecificBuiltinType(BuiltinType::BoundMember)) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2491 | const ValueDecl *Member = 0; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2492 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(Callee)) { |
| 2493 | // Explicit bound member calls, such as x.f() or p->g(); |
| 2494 | if (!EvaluateObjectArgument(Info, ME->getBase(), ThisVal)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2495 | return false; |
| 2496 | Member = ME->getMemberDecl(); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2497 | This = &ThisVal; |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 2498 | HasQualifier = ME->hasQualifier(); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2499 | } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(Callee)) { |
| 2500 | // Indirect bound member calls ('.*' or '->*'). |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2501 | Member = HandleMemberPointerAccess(Info, BE, ThisVal, false); |
| 2502 | if (!Member) return false; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2503 | This = &ThisVal; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2504 | } else |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2505 | return Error(Callee); |
| 2506 | |
| 2507 | FD = dyn_cast<FunctionDecl>(Member); |
| 2508 | if (!FD) |
| 2509 | return Error(Callee); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2510 | } else if (CalleeType->isFunctionPointerType()) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2511 | LValue Call; |
| 2512 | if (!EvaluatePointer(Callee, Call, Info)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2513 | return false; |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2514 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2515 | if (!Call.getLValueOffset().isZero()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2516 | return Error(Callee); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2517 | FD = dyn_cast_or_null<FunctionDecl>( |
| 2518 | Call.getLValueBase().dyn_cast<const ValueDecl*>()); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2519 | if (!FD) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2520 | return Error(Callee); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2521 | |
| 2522 | // Overloaded operator calls to member functions are represented as normal |
| 2523 | // calls with '*this' as the first argument. |
| 2524 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
| 2525 | if (MD && !MD->isStatic()) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2526 | // FIXME: When selecting an implicit conversion for an overloaded |
| 2527 | // operator delete, we sometimes try to evaluate calls to conversion |
| 2528 | // operators without a 'this' parameter! |
| 2529 | if (Args.empty()) |
| 2530 | return Error(E); |
| 2531 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2532 | if (!EvaluateObjectArgument(Info, Args[0], ThisVal)) |
| 2533 | return false; |
| 2534 | This = &ThisVal; |
| 2535 | Args = Args.slice(1); |
| 2536 | } |
| 2537 | |
| 2538 | // Don't call function pointers which have been cast to some other type. |
| 2539 | if (!Info.Ctx.hasSameType(CalleeType->getPointeeType(), FD->getType())) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2540 | return Error(E); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2541 | } else |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2542 | return Error(E); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2543 | |
Richard Smith | b04035a | 2012-02-01 02:39:43 +0000 | [diff] [blame] | 2544 | if (This && !This->checkSubobject(Info, E, CSK_This)) |
| 2545 | return false; |
| 2546 | |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 2547 | // DR1358 allows virtual constexpr functions in some cases. Don't allow |
| 2548 | // calls to such functions in constant expressions. |
| 2549 | if (This && !HasQualifier && |
| 2550 | isa<CXXMethodDecl>(FD) && cast<CXXMethodDecl>(FD)->isVirtual()) |
| 2551 | return Error(E, diag::note_constexpr_virtual_call); |
| 2552 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2553 | const FunctionDecl *Definition = 0; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2554 | Stmt *Body = FD->getBody(Definition); |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2555 | APValue Result; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2556 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2557 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition) || |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2558 | !HandleFunctionCall(E->getExprLoc(), Definition, This, Args, Body, |
| 2559 | Info, Result)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2560 | return false; |
| 2561 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2562 | return DerivedSuccess(Result, E); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2563 | } |
| 2564 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2565 | RetTy VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
| 2566 | return StmtVisitorTy::Visit(E->getInitializer()); |
| 2567 | } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2568 | RetTy VisitInitListExpr(const InitListExpr *E) { |
Eli Friedman | 71523d6 | 2012-01-03 23:54:05 +0000 | [diff] [blame] | 2569 | if (E->getNumInits() == 0) |
| 2570 | return DerivedZeroInitialization(E); |
| 2571 | if (E->getNumInits() == 1) |
| 2572 | return StmtVisitorTy::Visit(E->getInit(0)); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2573 | return Error(E); |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2574 | } |
| 2575 | RetTy VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2576 | return DerivedZeroInitialization(E); |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2577 | } |
| 2578 | RetTy VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2579 | return DerivedZeroInitialization(E); |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2580 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2581 | RetTy VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2582 | return DerivedZeroInitialization(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2583 | } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2584 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2585 | /// A member expression where the object is a prvalue is itself a prvalue. |
| 2586 | RetTy VisitMemberExpr(const MemberExpr *E) { |
| 2587 | assert(!E->isArrow() && "missing call to bound member function?"); |
| 2588 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2589 | APValue Val; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2590 | if (!Evaluate(Val, Info, E->getBase())) |
| 2591 | return false; |
| 2592 | |
| 2593 | QualType BaseTy = E->getBase()->getType(); |
| 2594 | |
| 2595 | const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2596 | if (!FD) return Error(E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2597 | assert(!FD->getType()->isReferenceType() && "prvalue reference?"); |
Ted Kremenek | 890f0f1 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 2598 | assert(BaseTy->castAs<RecordType>()->getDecl()->getCanonicalDecl() == |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2599 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
| 2600 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2601 | SubobjectDesignator Designator(BaseTy); |
| 2602 | Designator.addDeclUnchecked(FD); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2603 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2604 | return ExtractSubobject(Info, E, Val, BaseTy, Designator, E->getType()) && |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2605 | DerivedSuccess(Val, E); |
| 2606 | } |
| 2607 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2608 | RetTy VisitCastExpr(const CastExpr *E) { |
| 2609 | switch (E->getCastKind()) { |
| 2610 | default: |
| 2611 | break; |
| 2612 | |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 2613 | case CK_AtomicToNonAtomic: |
| 2614 | case CK_NonAtomicToAtomic: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2615 | case CK_NoOp: |
Richard Smith | 7d580a4 | 2012-01-17 21:17:26 +0000 | [diff] [blame] | 2616 | case CK_UserDefinedConversion: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2617 | return StmtVisitorTy::Visit(E->getSubExpr()); |
| 2618 | |
| 2619 | case CK_LValueToRValue: { |
| 2620 | LValue LVal; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2621 | if (!EvaluateLValue(E->getSubExpr(), LVal, Info)) |
| 2622 | return false; |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2623 | APValue RVal; |
Richard Smith | 9ec7197 | 2012-02-05 01:23:16 +0000 | [diff] [blame] | 2624 | // Note, we use the subexpression's type in order to retain cv-qualifiers. |
| 2625 | if (!HandleLValueToRValueConversion(Info, E, E->getSubExpr()->getType(), |
| 2626 | LVal, RVal)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2627 | return false; |
| 2628 | return DerivedSuccess(RVal, E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2629 | } |
| 2630 | } |
| 2631 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2632 | return Error(E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2633 | } |
| 2634 | |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 2635 | /// Visit a value which is evaluated, but whose value is ignored. |
| 2636 | void VisitIgnoredValue(const Expr *E) { |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2637 | APValue Scratch; |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 2638 | if (!Evaluate(Scratch, Info, E)) |
| 2639 | Info.EvalStatus.HasSideEffects = true; |
| 2640 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2641 | }; |
| 2642 | |
| 2643 | } |
| 2644 | |
| 2645 | //===----------------------------------------------------------------------===// |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2646 | // Common base class for lvalue and temporary evaluation. |
| 2647 | //===----------------------------------------------------------------------===// |
| 2648 | namespace { |
| 2649 | template<class Derived> |
| 2650 | class LValueExprEvaluatorBase |
| 2651 | : public ExprEvaluatorBase<Derived, bool> { |
| 2652 | protected: |
| 2653 | LValue &Result; |
| 2654 | typedef LValueExprEvaluatorBase LValueExprEvaluatorBaseTy; |
| 2655 | typedef ExprEvaluatorBase<Derived, bool> ExprEvaluatorBaseTy; |
| 2656 | |
| 2657 | bool Success(APValue::LValueBase B) { |
| 2658 | Result.set(B); |
| 2659 | return true; |
| 2660 | } |
| 2661 | |
| 2662 | public: |
| 2663 | LValueExprEvaluatorBase(EvalInfo &Info, LValue &Result) : |
| 2664 | ExprEvaluatorBaseTy(Info), Result(Result) {} |
| 2665 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2666 | bool Success(const APValue &V, const Expr *E) { |
| 2667 | Result.setFrom(this->Info.Ctx, V); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2668 | return true; |
| 2669 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2670 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2671 | bool VisitMemberExpr(const MemberExpr *E) { |
| 2672 | // Handle non-static data members. |
| 2673 | QualType BaseTy; |
| 2674 | if (E->isArrow()) { |
| 2675 | if (!EvaluatePointer(E->getBase(), Result, this->Info)) |
| 2676 | return false; |
Ted Kremenek | 890f0f1 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 2677 | BaseTy = E->getBase()->getType()->castAs<PointerType>()->getPointeeType(); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2678 | } else if (E->getBase()->isRValue()) { |
Richard Smith | af2c7a1 | 2011-12-19 22:01:37 +0000 | [diff] [blame] | 2679 | assert(E->getBase()->getType()->isRecordType()); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2680 | if (!EvaluateTemporary(E->getBase(), Result, this->Info)) |
| 2681 | return false; |
| 2682 | BaseTy = E->getBase()->getType(); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2683 | } else { |
| 2684 | if (!this->Visit(E->getBase())) |
| 2685 | return false; |
| 2686 | BaseTy = E->getBase()->getType(); |
| 2687 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2688 | |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2689 | const ValueDecl *MD = E->getMemberDecl(); |
| 2690 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) { |
| 2691 | assert(BaseTy->getAs<RecordType>()->getDecl()->getCanonicalDecl() == |
| 2692 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
| 2693 | (void)BaseTy; |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2694 | if (!HandleLValueMember(this->Info, E, Result, FD)) |
| 2695 | return false; |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2696 | } else if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(MD)) { |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2697 | if (!HandleLValueIndirectMember(this->Info, E, Result, IFD)) |
| 2698 | return false; |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2699 | } else |
| 2700 | return this->Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2701 | |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2702 | if (MD->getType()->isReferenceType()) { |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2703 | APValue RefValue; |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2704 | if (!HandleLValueToRValueConversion(this->Info, E, MD->getType(), Result, |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2705 | RefValue)) |
| 2706 | return false; |
| 2707 | return Success(RefValue, E); |
| 2708 | } |
| 2709 | return true; |
| 2710 | } |
| 2711 | |
| 2712 | bool VisitBinaryOperator(const BinaryOperator *E) { |
| 2713 | switch (E->getOpcode()) { |
| 2714 | default: |
| 2715 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
| 2716 | |
| 2717 | case BO_PtrMemD: |
| 2718 | case BO_PtrMemI: |
| 2719 | return HandleMemberPointerAccess(this->Info, E, Result); |
| 2720 | } |
| 2721 | } |
| 2722 | |
| 2723 | bool VisitCastExpr(const CastExpr *E) { |
| 2724 | switch (E->getCastKind()) { |
| 2725 | default: |
| 2726 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 2727 | |
| 2728 | case CK_DerivedToBase: |
| 2729 | case CK_UncheckedDerivedToBase: { |
| 2730 | if (!this->Visit(E->getSubExpr())) |
| 2731 | return false; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2732 | |
| 2733 | // Now figure out the necessary offset to add to the base LV to get from |
| 2734 | // the derived class to the base class. |
| 2735 | QualType Type = E->getSubExpr()->getType(); |
| 2736 | |
| 2737 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 2738 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2739 | if (!HandleLValueBase(this->Info, E, Result, Type->getAsCXXRecordDecl(), |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2740 | *PathI)) |
| 2741 | return false; |
| 2742 | Type = (*PathI)->getType(); |
| 2743 | } |
| 2744 | |
| 2745 | return true; |
| 2746 | } |
| 2747 | } |
| 2748 | } |
| 2749 | }; |
| 2750 | } |
| 2751 | |
| 2752 | //===----------------------------------------------------------------------===// |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2753 | // LValue Evaluation |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2754 | // |
| 2755 | // This is used for evaluating lvalues (in C and C++), xvalues (in C++11), |
| 2756 | // function designators (in C), decl references to void objects (in C), and |
| 2757 | // temporaries (if building with -Wno-address-of-temporary). |
| 2758 | // |
| 2759 | // LValue evaluation produces values comprising a base expression of one of the |
| 2760 | // following types: |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2761 | // - Declarations |
| 2762 | // * VarDecl |
| 2763 | // * FunctionDecl |
| 2764 | // - Literals |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2765 | // * CompoundLiteralExpr in C |
| 2766 | // * StringLiteral |
Richard Smith | 47d2145 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 2767 | // * CXXTypeidExpr |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2768 | // * PredefinedExpr |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2769 | // * ObjCStringLiteralExpr |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2770 | // * ObjCEncodeExpr |
| 2771 | // * AddrLabelExpr |
| 2772 | // * BlockExpr |
| 2773 | // * CallExpr for a MakeStringConstant builtin |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2774 | // - Locals and temporaries |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2775 | // * Any Expr, with a CallIndex indicating the function in which the temporary |
| 2776 | // was evaluated. |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2777 | // plus an offset in bytes. |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2778 | //===----------------------------------------------------------------------===// |
| 2779 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 2780 | class LValueExprEvaluator |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2781 | : public LValueExprEvaluatorBase<LValueExprEvaluator> { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2782 | public: |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2783 | LValueExprEvaluator(EvalInfo &Info, LValue &Result) : |
| 2784 | LValueExprEvaluatorBaseTy(Info, Result) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2785 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2786 | bool VisitVarDecl(const Expr *E, const VarDecl *VD); |
| 2787 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2788 | bool VisitDeclRefExpr(const DeclRefExpr *E); |
| 2789 | bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(E); } |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 2790 | bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2791 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
| 2792 | bool VisitMemberExpr(const MemberExpr *E); |
| 2793 | bool VisitStringLiteral(const StringLiteral *E) { return Success(E); } |
| 2794 | bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(E); } |
Richard Smith | 47d2145 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 2795 | bool VisitCXXTypeidExpr(const CXXTypeidExpr *E); |
Francois Pichet | e275a18 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 2796 | bool VisitCXXUuidofExpr(const CXXUuidofExpr *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2797 | bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E); |
| 2798 | bool VisitUnaryDeref(const UnaryOperator *E); |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2799 | bool VisitUnaryReal(const UnaryOperator *E); |
| 2800 | bool VisitUnaryImag(const UnaryOperator *E); |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 2801 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2802 | bool VisitCastExpr(const CastExpr *E) { |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 2803 | switch (E->getCastKind()) { |
| 2804 | default: |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2805 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 2806 | |
Eli Friedman | db92422 | 2011-10-11 00:13:24 +0000 | [diff] [blame] | 2807 | case CK_LValueBitCast: |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 2808 | this->CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 2809 | if (!Visit(E->getSubExpr())) |
| 2810 | return false; |
| 2811 | Result.Designator.setInvalid(); |
| 2812 | return true; |
Eli Friedman | db92422 | 2011-10-11 00:13:24 +0000 | [diff] [blame] | 2813 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2814 | case CK_BaseToDerived: |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2815 | if (!Visit(E->getSubExpr())) |
| 2816 | return false; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2817 | return HandleBaseToDerivedCast(Info, E, Result); |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 2818 | } |
| 2819 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2820 | }; |
| 2821 | } // end anonymous namespace |
| 2822 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2823 | /// Evaluate an expression as an lvalue. This can be legitimately called on |
| 2824 | /// expressions which are not glvalues, in a few cases: |
| 2825 | /// * function designators in C, |
| 2826 | /// * "extern void" objects, |
| 2827 | /// * temporaries, if building with -Wno-address-of-temporary. |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2828 | static bool EvaluateLValue(const Expr* E, LValue& Result, EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2829 | assert((E->isGLValue() || E->getType()->isFunctionType() || |
| 2830 | E->getType()->isVoidType() || isa<CXXTemporaryObjectExpr>(E)) && |
| 2831 | "can't evaluate expression as an lvalue"); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2832 | return LValueExprEvaluator(Info, Result).Visit(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2833 | } |
| 2834 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2835 | bool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2836 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl())) |
| 2837 | return Success(FD); |
| 2838 | if (const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2839 | return VisitVarDecl(E, VD); |
| 2840 | return Error(E); |
| 2841 | } |
Richard Smith | 436c889 | 2011-10-24 23:14:33 +0000 | [diff] [blame] | 2842 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2843 | bool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) { |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 2844 | if (!VD->getType()->isReferenceType()) { |
| 2845 | if (isa<ParmVarDecl>(VD)) { |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2846 | Result.set(VD, Info.CurrentCall->Index); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 2847 | return true; |
| 2848 | } |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2849 | return Success(VD); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 2850 | } |
Eli Friedman | 50c39ea | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 2851 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2852 | APValue V; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2853 | if (!EvaluateVarDeclInit(Info, E, VD, Info.CurrentCall, V)) |
| 2854 | return false; |
| 2855 | return Success(V, E); |
Anders Carlsson | 35873c4 | 2008-11-24 04:41:22 +0000 | [diff] [blame] | 2856 | } |
| 2857 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 2858 | bool LValueExprEvaluator::VisitMaterializeTemporaryExpr( |
| 2859 | const MaterializeTemporaryExpr *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2860 | if (E->GetTemporaryExpr()->isRValue()) { |
Richard Smith | af2c7a1 | 2011-12-19 22:01:37 +0000 | [diff] [blame] | 2861 | if (E->getType()->isRecordType()) |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2862 | return EvaluateTemporary(E->GetTemporaryExpr(), Result, Info); |
| 2863 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2864 | Result.set(E, Info.CurrentCall->Index); |
| 2865 | return EvaluateInPlace(Info.CurrentCall->Temporaries[E], Info, |
| 2866 | Result, E->GetTemporaryExpr()); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2867 | } |
| 2868 | |
| 2869 | // Materialization of an lvalue temporary occurs when we need to force a copy |
| 2870 | // (for instance, if it's a bitfield). |
| 2871 | // FIXME: The AST should contain an lvalue-to-rvalue node for such cases. |
| 2872 | if (!Visit(E->GetTemporaryExpr())) |
| 2873 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2874 | if (!HandleLValueToRValueConversion(Info, E, E->getType(), Result, |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2875 | Info.CurrentCall->Temporaries[E])) |
| 2876 | return false; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2877 | Result.set(E, Info.CurrentCall->Index); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2878 | return true; |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 2879 | } |
| 2880 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2881 | bool |
| 2882 | LValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2883 | assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?"); |
| 2884 | // Defer visiting the literal until the lvalue-to-rvalue conversion. We can |
| 2885 | // 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] | 2886 | return Success(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2887 | } |
| 2888 | |
Richard Smith | 47d2145 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 2889 | bool LValueExprEvaluator::VisitCXXTypeidExpr(const CXXTypeidExpr *E) { |
Richard Smith | 9be36ab | 2012-10-17 23:52:07 +0000 | [diff] [blame] | 2890 | if (!E->isPotentiallyEvaluated()) |
Richard Smith | 47d2145 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 2891 | return Success(E); |
Richard Smith | 9be36ab | 2012-10-17 23:52:07 +0000 | [diff] [blame] | 2892 | |
| 2893 | Info.Diag(E, diag::note_constexpr_typeid_polymorphic) |
| 2894 | << E->getExprOperand()->getType() |
| 2895 | << E->getExprOperand()->getSourceRange(); |
| 2896 | return false; |
Richard Smith | 47d2145 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 2897 | } |
| 2898 | |
Francois Pichet | e275a18 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 2899 | bool LValueExprEvaluator::VisitCXXUuidofExpr(const CXXUuidofExpr *E) { |
| 2900 | return Success(E); |
| 2901 | } |
| 2902 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2903 | bool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2904 | // Handle static data members. |
| 2905 | if (const VarDecl *VD = dyn_cast<VarDecl>(E->getMemberDecl())) { |
| 2906 | VisitIgnoredValue(E->getBase()); |
| 2907 | return VisitVarDecl(E, VD); |
| 2908 | } |
| 2909 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2910 | // Handle static member functions. |
| 2911 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl())) { |
| 2912 | if (MD->isStatic()) { |
| 2913 | VisitIgnoredValue(E->getBase()); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2914 | return Success(MD); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2915 | } |
| 2916 | } |
| 2917 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2918 | // Handle non-static data members. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2919 | return LValueExprEvaluatorBaseTy::VisitMemberExpr(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2920 | } |
| 2921 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2922 | bool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2923 | // FIXME: Deal with vectors as array subscript bases. |
| 2924 | if (E->getBase()->getType()->isVectorType()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2925 | return Error(E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2926 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 2927 | if (!EvaluatePointer(E->getBase(), Result, Info)) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2928 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2929 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 2930 | APSInt Index; |
| 2931 | if (!EvaluateInteger(E->getIdx(), Index, Info)) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2932 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2933 | int64_t IndexValue |
| 2934 | = Index.isSigned() ? Index.getSExtValue() |
| 2935 | : static_cast<int64_t>(Index.getZExtValue()); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 2936 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2937 | return HandleLValueArrayAdjustment(Info, E, Result, E->getType(), IndexValue); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 2938 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2939 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2940 | bool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2941 | return EvaluatePointer(E->getSubExpr(), Result, Info); |
Eli Friedman | e8761c8 | 2009-02-20 01:57:15 +0000 | [diff] [blame] | 2942 | } |
| 2943 | |
Richard Smith | 8602401 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2944 | bool LValueExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
| 2945 | if (!Visit(E->getSubExpr())) |
| 2946 | return false; |
| 2947 | // __real is a no-op on scalar lvalues. |
| 2948 | if (E->getSubExpr()->getType()->isAnyComplexType()) |
| 2949 | HandleLValueComplexElement(Info, E, Result, E->getType(), false); |
| 2950 | return true; |
| 2951 | } |
| 2952 | |
| 2953 | bool LValueExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
| 2954 | assert(E->getSubExpr()->getType()->isAnyComplexType() && |
| 2955 | "lvalue __imag__ on scalar?"); |
| 2956 | if (!Visit(E->getSubExpr())) |
| 2957 | return false; |
| 2958 | HandleLValueComplexElement(Info, E, Result, E->getType(), true); |
| 2959 | return true; |
| 2960 | } |
| 2961 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2962 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2963 | // Pointer Evaluation |
| 2964 | //===----------------------------------------------------------------------===// |
| 2965 | |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 2966 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 2967 | class PointerExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2968 | : public ExprEvaluatorBase<PointerExprEvaluator, bool> { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2969 | LValue &Result; |
| 2970 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2971 | bool Success(const Expr *E) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2972 | Result.set(E); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2973 | return true; |
| 2974 | } |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 2975 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2976 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2977 | PointerExprEvaluator(EvalInfo &info, LValue &Result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2978 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2979 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2980 | bool Success(const APValue &V, const Expr *E) { |
| 2981 | Result.setFrom(Info.Ctx, V); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2982 | return true; |
| 2983 | } |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2984 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2985 | return Success((Expr*)0); |
| 2986 | } |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 2987 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2988 | bool VisitBinaryOperator(const BinaryOperator *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2989 | bool VisitCastExpr(const CastExpr* E); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2990 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2991 | bool VisitObjCStringLiteral(const ObjCStringLiteral *E) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2992 | { return Success(E); } |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 2993 | bool VisitObjCBoxedExpr(const ObjCBoxedExpr *E) |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2994 | { return Success(E); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2995 | bool VisitAddrLabelExpr(const AddrLabelExpr *E) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2996 | { return Success(E); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2997 | bool VisitCallExpr(const CallExpr *E); |
| 2998 | bool VisitBlockExpr(const BlockExpr *E) { |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2999 | if (!E->getBlockDecl()->hasCaptures()) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3000 | return Success(E); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3001 | return Error(E); |
Mike Stump | b83d287 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 3002 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3003 | bool VisitCXXThisExpr(const CXXThisExpr *E) { |
| 3004 | if (!Info.CurrentCall->This) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3005 | return Error(E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3006 | Result = *Info.CurrentCall->This; |
| 3007 | return true; |
| 3008 | } |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3009 | |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 3010 | // FIXME: Missing: @protocol, @selector |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 3011 | }; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3012 | } // end anonymous namespace |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 3013 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3014 | static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3015 | assert(E->isRValue() && E->getType()->hasPointerRepresentation()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3016 | return PointerExprEvaluator(Info, Result).Visit(E); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3017 | } |
| 3018 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3019 | bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3020 | if (E->getOpcode() != BO_Add && |
| 3021 | E->getOpcode() != BO_Sub) |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3022 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3023 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3024 | const Expr *PExp = E->getLHS(); |
| 3025 | const Expr *IExp = E->getRHS(); |
| 3026 | if (IExp->getType()->isPointerType()) |
| 3027 | std::swap(PExp, IExp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3028 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3029 | bool EvalPtrOK = EvaluatePointer(PExp, Result, Info); |
| 3030 | if (!EvalPtrOK && !Info.keepEvaluatingAfterFailure()) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3031 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3032 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3033 | llvm::APSInt Offset; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3034 | if (!EvaluateInteger(IExp, Offset, Info) || !EvalPtrOK) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3035 | return false; |
| 3036 | int64_t AdditionalOffset |
| 3037 | = Offset.isSigned() ? Offset.getSExtValue() |
| 3038 | : static_cast<int64_t>(Offset.getZExtValue()); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 3039 | if (E->getOpcode() == BO_Sub) |
| 3040 | AdditionalOffset = -AdditionalOffset; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3041 | |
Ted Kremenek | 890f0f1 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 3042 | QualType Pointee = PExp->getType()->castAs<PointerType>()->getPointeeType(); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3043 | return HandleLValueArrayAdjustment(Info, E, Result, Pointee, |
| 3044 | AdditionalOffset); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3045 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3046 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3047 | bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
| 3048 | return EvaluateLValue(E->getSubExpr(), Result, Info); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3049 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3050 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3051 | bool PointerExprEvaluator::VisitCastExpr(const CastExpr* E) { |
| 3052 | const Expr* SubExpr = E->getSubExpr(); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3053 | |
Eli Friedman | 09a8a0e | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 3054 | switch (E->getCastKind()) { |
| 3055 | default: |
| 3056 | break; |
| 3057 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3058 | case CK_BitCast: |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3059 | case CK_CPointerToObjCPointerCast: |
| 3060 | case CK_BlockPointerToObjCPointerCast: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3061 | case CK_AnyPointerToBlockPointerCast: |
Richard Smith | 28c1ce7 | 2012-01-15 03:25:41 +0000 | [diff] [blame] | 3062 | if (!Visit(SubExpr)) |
| 3063 | return false; |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 3064 | // Bitcasts to cv void* are static_casts, not reinterpret_casts, so are |
| 3065 | // permitted in constant expressions in C++11. Bitcasts from cv void* are |
| 3066 | // also static_casts, but we disallow them as a resolution to DR1312. |
Richard Smith | 4cd9b8f | 2011-12-12 19:10:03 +0000 | [diff] [blame] | 3067 | if (!E->getType()->isVoidPointerType()) { |
Richard Smith | 28c1ce7 | 2012-01-15 03:25:41 +0000 | [diff] [blame] | 3068 | Result.Designator.setInvalid(); |
Richard Smith | 4cd9b8f | 2011-12-12 19:10:03 +0000 | [diff] [blame] | 3069 | if (SubExpr->getType()->isVoidPointerType()) |
| 3070 | CCEDiag(E, diag::note_constexpr_invalid_cast) |
| 3071 | << 3 << SubExpr->getType(); |
| 3072 | else |
| 3073 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 3074 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 3075 | return true; |
Eli Friedman | 09a8a0e | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 3076 | |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 3077 | case CK_DerivedToBase: |
| 3078 | case CK_UncheckedDerivedToBase: { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3079 | if (!EvaluatePointer(E->getSubExpr(), Result, Info)) |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 3080 | return false; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3081 | if (!Result.Base && Result.Offset.isZero()) |
| 3082 | return true; |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 3083 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3084 | // 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] | 3085 | // the derived class to the base class. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3086 | QualType Type = |
| 3087 | E->getSubExpr()->getType()->castAs<PointerType>()->getPointeeType(); |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 3088 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3089 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 3090 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3091 | if (!HandleLValueBase(Info, E, Result, Type->getAsCXXRecordDecl(), |
| 3092 | *PathI)) |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 3093 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3094 | Type = (*PathI)->getType(); |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 3095 | } |
| 3096 | |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 3097 | return true; |
| 3098 | } |
| 3099 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3100 | case CK_BaseToDerived: |
| 3101 | if (!Visit(E->getSubExpr())) |
| 3102 | return false; |
| 3103 | if (!Result.Base && Result.Offset.isZero()) |
| 3104 | return true; |
| 3105 | return HandleBaseToDerivedCast(Info, E, Result); |
| 3106 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3107 | case CK_NullToPointer: |
Richard Smith | 49149fe | 2012-04-08 08:02:07 +0000 | [diff] [blame] | 3108 | VisitIgnoredValue(E->getSubExpr()); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3109 | return ZeroInitialization(E); |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 3110 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3111 | case CK_IntegralToPointer: { |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 3112 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 3113 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3114 | APValue Value; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3115 | if (!EvaluateIntegerOrLValue(SubExpr, Value, Info)) |
Eli Friedman | 09a8a0e | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 3116 | break; |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 3117 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3118 | if (Value.isInt()) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3119 | unsigned Size = Info.Ctx.getTypeSize(E->getType()); |
| 3120 | uint64_t N = Value.getInt().extOrTrunc(Size).getZExtValue(); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 3121 | Result.Base = (Expr*)0; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3122 | Result.Offset = CharUnits::fromQuantity(N); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3123 | Result.CallIndex = 0; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 3124 | Result.Designator.setInvalid(); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3125 | return true; |
| 3126 | } else { |
| 3127 | // Cast is of an lvalue, no need to change value. |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3128 | Result.setFrom(Info.Ctx, Value); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3129 | return true; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3130 | } |
| 3131 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3132 | case CK_ArrayToPointerDecay: |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3133 | if (SubExpr->isGLValue()) { |
| 3134 | if (!EvaluateLValue(SubExpr, Result, Info)) |
| 3135 | return false; |
| 3136 | } else { |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3137 | Result.set(SubExpr, Info.CurrentCall->Index); |
| 3138 | if (!EvaluateInPlace(Info.CurrentCall->Temporaries[SubExpr], |
| 3139 | Info, Result, SubExpr)) |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3140 | return false; |
| 3141 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 3142 | // The result is a pointer to the first element of the array. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3143 | if (const ConstantArrayType *CAT |
| 3144 | = Info.Ctx.getAsConstantArrayType(SubExpr->getType())) |
| 3145 | Result.addArray(Info, E, CAT); |
| 3146 | else |
| 3147 | Result.Designator.setInvalid(); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 3148 | return true; |
Richard Smith | 6a7c94a | 2011-10-31 20:57:44 +0000 | [diff] [blame] | 3149 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3150 | case CK_FunctionToPointerDecay: |
Richard Smith | 6a7c94a | 2011-10-31 20:57:44 +0000 | [diff] [blame] | 3151 | return EvaluateLValue(SubExpr, Result, Info); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3152 | } |
| 3153 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3154 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3155 | } |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3156 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3157 | bool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3158 | if (IsStringLiteralCall(E)) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3159 | return Success(E); |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 3160 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3161 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3162 | } |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3163 | |
| 3164 | //===----------------------------------------------------------------------===// |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3165 | // Member Pointer Evaluation |
| 3166 | //===----------------------------------------------------------------------===// |
| 3167 | |
| 3168 | namespace { |
| 3169 | class MemberPointerExprEvaluator |
| 3170 | : public ExprEvaluatorBase<MemberPointerExprEvaluator, bool> { |
| 3171 | MemberPtr &Result; |
| 3172 | |
| 3173 | bool Success(const ValueDecl *D) { |
| 3174 | Result = MemberPtr(D); |
| 3175 | return true; |
| 3176 | } |
| 3177 | public: |
| 3178 | |
| 3179 | MemberPointerExprEvaluator(EvalInfo &Info, MemberPtr &Result) |
| 3180 | : ExprEvaluatorBaseTy(Info), Result(Result) {} |
| 3181 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3182 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3183 | Result.setFrom(V); |
| 3184 | return true; |
| 3185 | } |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3186 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3187 | return Success((const ValueDecl*)0); |
| 3188 | } |
| 3189 | |
| 3190 | bool VisitCastExpr(const CastExpr *E); |
| 3191 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
| 3192 | }; |
| 3193 | } // end anonymous namespace |
| 3194 | |
| 3195 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
| 3196 | EvalInfo &Info) { |
| 3197 | assert(E->isRValue() && E->getType()->isMemberPointerType()); |
| 3198 | return MemberPointerExprEvaluator(Info, Result).Visit(E); |
| 3199 | } |
| 3200 | |
| 3201 | bool MemberPointerExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 3202 | switch (E->getCastKind()) { |
| 3203 | default: |
| 3204 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 3205 | |
| 3206 | case CK_NullToMemberPointer: |
Richard Smith | 49149fe | 2012-04-08 08:02:07 +0000 | [diff] [blame] | 3207 | VisitIgnoredValue(E->getSubExpr()); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3208 | return ZeroInitialization(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3209 | |
| 3210 | case CK_BaseToDerivedMemberPointer: { |
| 3211 | if (!Visit(E->getSubExpr())) |
| 3212 | return false; |
| 3213 | if (E->path_empty()) |
| 3214 | return true; |
| 3215 | // Base-to-derived member pointer casts store the path in derived-to-base |
| 3216 | // order, so iterate backwards. The CXXBaseSpecifier also provides us with |
| 3217 | // the wrong end of the derived->base arc, so stagger the path by one class. |
| 3218 | typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter; |
| 3219 | for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin()); |
| 3220 | PathI != PathE; ++PathI) { |
| 3221 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
| 3222 | const CXXRecordDecl *Derived = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 3223 | if (!Result.castToDerived(Derived)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3224 | return Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3225 | } |
| 3226 | const Type *FinalTy = E->getType()->castAs<MemberPointerType>()->getClass(); |
| 3227 | if (!Result.castToDerived(FinalTy->getAsCXXRecordDecl())) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3228 | return Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3229 | return true; |
| 3230 | } |
| 3231 | |
| 3232 | case CK_DerivedToBaseMemberPointer: |
| 3233 | if (!Visit(E->getSubExpr())) |
| 3234 | return false; |
| 3235 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 3236 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
| 3237 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
| 3238 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 3239 | if (!Result.castToBase(Base)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3240 | return Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3241 | } |
| 3242 | return true; |
| 3243 | } |
| 3244 | } |
| 3245 | |
| 3246 | bool MemberPointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
| 3247 | // C++11 [expr.unary.op]p3 has very strict rules on how the address of a |
| 3248 | // member can be formed. |
| 3249 | return Success(cast<DeclRefExpr>(E->getSubExpr())->getDecl()); |
| 3250 | } |
| 3251 | |
| 3252 | //===----------------------------------------------------------------------===// |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3253 | // Record Evaluation |
| 3254 | //===----------------------------------------------------------------------===// |
| 3255 | |
| 3256 | namespace { |
| 3257 | class RecordExprEvaluator |
| 3258 | : public ExprEvaluatorBase<RecordExprEvaluator, bool> { |
| 3259 | const LValue &This; |
| 3260 | APValue &Result; |
| 3261 | public: |
| 3262 | |
| 3263 | RecordExprEvaluator(EvalInfo &info, const LValue &This, APValue &Result) |
| 3264 | : ExprEvaluatorBaseTy(info), This(This), Result(Result) {} |
| 3265 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3266 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3267 | Result = V; |
| 3268 | return true; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3269 | } |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3270 | bool ZeroInitialization(const Expr *E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3271 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3272 | bool VisitCastExpr(const CastExpr *E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3273 | bool VisitInitListExpr(const InitListExpr *E); |
| 3274 | bool VisitCXXConstructExpr(const CXXConstructExpr *E); |
| 3275 | }; |
| 3276 | } |
| 3277 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3278 | /// Perform zero-initialization on an object of non-union class type. |
| 3279 | /// C++11 [dcl.init]p5: |
| 3280 | /// To zero-initialize an object or reference of type T means: |
| 3281 | /// [...] |
| 3282 | /// -- if T is a (possibly cv-qualified) non-union class type, |
| 3283 | /// each non-static data member and each base-class subobject is |
| 3284 | /// zero-initialized |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3285 | static bool HandleClassZeroInitialization(EvalInfo &Info, const Expr *E, |
| 3286 | const RecordDecl *RD, |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3287 | const LValue &This, APValue &Result) { |
| 3288 | assert(!RD->isUnion() && "Expected non-union class type"); |
| 3289 | const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD); |
| 3290 | Result = APValue(APValue::UninitStruct(), CD ? CD->getNumBases() : 0, |
| 3291 | std::distance(RD->field_begin(), RD->field_end())); |
| 3292 | |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3293 | if (RD->isInvalidDecl()) return false; |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3294 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 3295 | |
| 3296 | if (CD) { |
| 3297 | unsigned Index = 0; |
| 3298 | for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(), |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3299 | End = CD->bases_end(); I != End; ++I, ++Index) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3300 | const CXXRecordDecl *Base = I->getType()->getAsCXXRecordDecl(); |
| 3301 | LValue Subobject = This; |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3302 | if (!HandleLValueDirectBase(Info, E, Subobject, CD, Base, &Layout)) |
| 3303 | return false; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3304 | if (!HandleClassZeroInitialization(Info, E, Base, Subobject, |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3305 | Result.getStructBase(Index))) |
| 3306 | return false; |
| 3307 | } |
| 3308 | } |
| 3309 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3310 | for (RecordDecl::field_iterator I = RD->field_begin(), End = RD->field_end(); |
| 3311 | I != End; ++I) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3312 | // -- if T is a reference type, no initialization is performed. |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 3313 | if (I->getType()->isReferenceType()) |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3314 | continue; |
| 3315 | |
| 3316 | LValue Subobject = This; |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3317 | if (!HandleLValueMember(Info, E, Subobject, *I, &Layout)) |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3318 | return false; |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3319 | |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 3320 | ImplicitValueInitExpr VIE(I->getType()); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3321 | if (!EvaluateInPlace( |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 3322 | Result.getStructField(I->getFieldIndex()), Info, Subobject, &VIE)) |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3323 | return false; |
| 3324 | } |
| 3325 | |
| 3326 | return true; |
| 3327 | } |
| 3328 | |
| 3329 | bool RecordExprEvaluator::ZeroInitialization(const Expr *E) { |
| 3330 | const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl(); |
John McCall | 1de9d7d | 2012-04-26 18:10:01 +0000 | [diff] [blame] | 3331 | if (RD->isInvalidDecl()) return false; |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3332 | if (RD->isUnion()) { |
| 3333 | // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the |
| 3334 | // object's first non-static named data member is zero-initialized |
| 3335 | RecordDecl::field_iterator I = RD->field_begin(); |
| 3336 | if (I == RD->field_end()) { |
| 3337 | Result = APValue((const FieldDecl*)0); |
| 3338 | return true; |
| 3339 | } |
| 3340 | |
| 3341 | LValue Subobject = This; |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3342 | if (!HandleLValueMember(Info, E, Subobject, *I)) |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3343 | return false; |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3344 | Result = APValue(*I); |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 3345 | ImplicitValueInitExpr VIE(I->getType()); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3346 | return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, &VIE); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3347 | } |
| 3348 | |
Richard Smith | ce582fe | 2012-02-17 00:44:16 +0000 | [diff] [blame] | 3349 | if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->getNumVBases()) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 3350 | Info.Diag(E, diag::note_constexpr_virtual_base) << RD; |
Richard Smith | ce582fe | 2012-02-17 00:44:16 +0000 | [diff] [blame] | 3351 | return false; |
| 3352 | } |
| 3353 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3354 | return HandleClassZeroInitialization(Info, E, RD, This, Result); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3355 | } |
| 3356 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3357 | bool RecordExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 3358 | switch (E->getCastKind()) { |
| 3359 | default: |
| 3360 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 3361 | |
| 3362 | case CK_ConstructorConversion: |
| 3363 | return Visit(E->getSubExpr()); |
| 3364 | |
| 3365 | case CK_DerivedToBase: |
| 3366 | case CK_UncheckedDerivedToBase: { |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3367 | APValue DerivedObject; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3368 | if (!Evaluate(DerivedObject, Info, E->getSubExpr())) |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3369 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3370 | if (!DerivedObject.isStruct()) |
| 3371 | return Error(E->getSubExpr()); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3372 | |
| 3373 | // Derived-to-base rvalue conversion: just slice off the derived part. |
| 3374 | APValue *Value = &DerivedObject; |
| 3375 | const CXXRecordDecl *RD = E->getSubExpr()->getType()->getAsCXXRecordDecl(); |
| 3376 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 3377 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
| 3378 | assert(!(*PathI)->isVirtual() && "record rvalue with virtual base"); |
| 3379 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 3380 | Value = &Value->getStructBase(getBaseIndex(RD, Base)); |
| 3381 | RD = Base; |
| 3382 | } |
| 3383 | Result = *Value; |
| 3384 | return true; |
| 3385 | } |
| 3386 | } |
| 3387 | } |
| 3388 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3389 | bool RecordExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
Sebastian Redl | 24fe798 | 2012-02-19 14:53:49 +0000 | [diff] [blame] | 3390 | // Cannot constant-evaluate std::initializer_list inits. |
| 3391 | if (E->initializesStdInitializerList()) |
| 3392 | return false; |
| 3393 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3394 | const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl(); |
John McCall | 1de9d7d | 2012-04-26 18:10:01 +0000 | [diff] [blame] | 3395 | if (RD->isInvalidDecl()) return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3396 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 3397 | |
| 3398 | if (RD->isUnion()) { |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 3399 | const FieldDecl *Field = E->getInitializedFieldInUnion(); |
| 3400 | Result = APValue(Field); |
| 3401 | if (!Field) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3402 | return true; |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 3403 | |
| 3404 | // If the initializer list for a union does not contain any elements, the |
| 3405 | // first element of the union is value-initialized. |
| 3406 | ImplicitValueInitExpr VIE(Field->getType()); |
| 3407 | const Expr *InitExpr = E->getNumInits() ? E->getInit(0) : &VIE; |
| 3408 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3409 | LValue Subobject = This; |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3410 | if (!HandleLValueMember(Info, InitExpr, Subobject, Field, &Layout)) |
| 3411 | return false; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3412 | return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, InitExpr); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3413 | } |
| 3414 | |
| 3415 | assert((!isa<CXXRecordDecl>(RD) || !cast<CXXRecordDecl>(RD)->getNumBases()) && |
| 3416 | "initializer list for class with base classes"); |
| 3417 | Result = APValue(APValue::UninitStruct(), 0, |
| 3418 | std::distance(RD->field_begin(), RD->field_end())); |
| 3419 | unsigned ElementNo = 0; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3420 | bool Success = true; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3421 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 3422 | FieldEnd = RD->field_end(); Field != FieldEnd; ++Field) { |
| 3423 | // Anonymous bit-fields are not considered members of the class for |
| 3424 | // purposes of aggregate initialization. |
| 3425 | if (Field->isUnnamedBitfield()) |
| 3426 | continue; |
| 3427 | |
| 3428 | LValue Subobject = This; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3429 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3430 | bool HaveInit = ElementNo < E->getNumInits(); |
| 3431 | |
| 3432 | // FIXME: Diagnostics here should point to the end of the initializer |
| 3433 | // list, not the start. |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3434 | if (!HandleLValueMember(Info, HaveInit ? E->getInit(ElementNo) : E, |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3435 | Subobject, *Field, &Layout)) |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3436 | return false; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3437 | |
| 3438 | // Perform an implicit value-initialization for members beyond the end of |
| 3439 | // the initializer list. |
| 3440 | ImplicitValueInitExpr VIE(HaveInit ? Info.Ctx.IntTy : Field->getType()); |
| 3441 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3442 | if (!EvaluateInPlace( |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 3443 | Result.getStructField(Field->getFieldIndex()), |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3444 | Info, Subobject, HaveInit ? E->getInit(ElementNo++) : &VIE)) { |
| 3445 | if (!Info.keepEvaluatingAfterFailure()) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3446 | return false; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3447 | Success = false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3448 | } |
| 3449 | } |
| 3450 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3451 | return Success; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3452 | } |
| 3453 | |
| 3454 | bool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 3455 | const CXXConstructorDecl *FD = E->getConstructor(); |
John McCall | 1de9d7d | 2012-04-26 18:10:01 +0000 | [diff] [blame] | 3456 | if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl()) return false; |
| 3457 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3458 | bool ZeroInit = E->requiresZeroInitialization(); |
| 3459 | if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) { |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 3460 | // If we've already performed zero-initialization, we're already done. |
| 3461 | if (!Result.isUninit()) |
| 3462 | return true; |
| 3463 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3464 | if (ZeroInit) |
| 3465 | return ZeroInitialization(E); |
| 3466 | |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 3467 | const CXXRecordDecl *RD = FD->getParent(); |
| 3468 | if (RD->isUnion()) |
| 3469 | Result = APValue((FieldDecl*)0); |
| 3470 | else |
| 3471 | Result = APValue(APValue::UninitStruct(), RD->getNumBases(), |
| 3472 | std::distance(RD->field_begin(), RD->field_end())); |
| 3473 | return true; |
| 3474 | } |
| 3475 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3476 | const FunctionDecl *Definition = 0; |
| 3477 | FD->getBody(Definition); |
| 3478 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3479 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition)) |
| 3480 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3481 | |
Richard Smith | 610a60c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 3482 | // Avoid materializing a temporary for an elidable copy/move constructor. |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3483 | if (E->isElidable() && !ZeroInit) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3484 | if (const MaterializeTemporaryExpr *ME |
| 3485 | = dyn_cast<MaterializeTemporaryExpr>(E->getArg(0))) |
| 3486 | return Visit(ME->GetTemporaryExpr()); |
| 3487 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3488 | if (ZeroInit && !ZeroInitialization(E)) |
| 3489 | return false; |
| 3490 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3491 | llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs()); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3492 | return HandleConstructorCall(E->getExprLoc(), This, Args, |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3493 | cast<CXXConstructorDecl>(Definition), Info, |
| 3494 | Result); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3495 | } |
| 3496 | |
| 3497 | static bool EvaluateRecord(const Expr *E, const LValue &This, |
| 3498 | APValue &Result, EvalInfo &Info) { |
| 3499 | assert(E->isRValue() && E->getType()->isRecordType() && |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3500 | "can't evaluate expression as a record rvalue"); |
| 3501 | return RecordExprEvaluator(Info, This, Result).Visit(E); |
| 3502 | } |
| 3503 | |
| 3504 | //===----------------------------------------------------------------------===// |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3505 | // Temporary Evaluation |
| 3506 | // |
| 3507 | // Temporaries are represented in the AST as rvalues, but generally behave like |
| 3508 | // lvalues. The full-object of which the temporary is a subobject is implicitly |
| 3509 | // materialized so that a reference can bind to it. |
| 3510 | //===----------------------------------------------------------------------===// |
| 3511 | namespace { |
| 3512 | class TemporaryExprEvaluator |
| 3513 | : public LValueExprEvaluatorBase<TemporaryExprEvaluator> { |
| 3514 | public: |
| 3515 | TemporaryExprEvaluator(EvalInfo &Info, LValue &Result) : |
| 3516 | LValueExprEvaluatorBaseTy(Info, Result) {} |
| 3517 | |
| 3518 | /// Visit an expression which constructs the value of this temporary. |
| 3519 | bool VisitConstructExpr(const Expr *E) { |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3520 | Result.set(E, Info.CurrentCall->Index); |
| 3521 | return EvaluateInPlace(Info.CurrentCall->Temporaries[E], Info, Result, E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3522 | } |
| 3523 | |
| 3524 | bool VisitCastExpr(const CastExpr *E) { |
| 3525 | switch (E->getCastKind()) { |
| 3526 | default: |
| 3527 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
| 3528 | |
| 3529 | case CK_ConstructorConversion: |
| 3530 | return VisitConstructExpr(E->getSubExpr()); |
| 3531 | } |
| 3532 | } |
| 3533 | bool VisitInitListExpr(const InitListExpr *E) { |
| 3534 | return VisitConstructExpr(E); |
| 3535 | } |
| 3536 | bool VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 3537 | return VisitConstructExpr(E); |
| 3538 | } |
| 3539 | bool VisitCallExpr(const CallExpr *E) { |
| 3540 | return VisitConstructExpr(E); |
| 3541 | } |
| 3542 | }; |
| 3543 | } // end anonymous namespace |
| 3544 | |
| 3545 | /// Evaluate an expression of record type as a temporary. |
| 3546 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info) { |
Richard Smith | af2c7a1 | 2011-12-19 22:01:37 +0000 | [diff] [blame] | 3547 | assert(E->isRValue() && E->getType()->isRecordType()); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3548 | return TemporaryExprEvaluator(Info, Result).Visit(E); |
| 3549 | } |
| 3550 | |
| 3551 | //===----------------------------------------------------------------------===// |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3552 | // Vector Evaluation |
| 3553 | //===----------------------------------------------------------------------===// |
| 3554 | |
| 3555 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 3556 | class VectorExprEvaluator |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3557 | : public ExprEvaluatorBase<VectorExprEvaluator, bool> { |
| 3558 | APValue &Result; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3559 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3560 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3561 | VectorExprEvaluator(EvalInfo &info, APValue &Result) |
| 3562 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3563 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3564 | bool Success(const ArrayRef<APValue> &V, const Expr *E) { |
| 3565 | assert(V.size() == E->getType()->castAs<VectorType>()->getNumElements()); |
| 3566 | // FIXME: remove this APValue copy. |
| 3567 | Result = APValue(V.data(), V.size()); |
| 3568 | return true; |
| 3569 | } |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3570 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 3571 | assert(V.isVector()); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3572 | Result = V; |
| 3573 | return true; |
| 3574 | } |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3575 | bool ZeroInitialization(const Expr *E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3576 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3577 | bool VisitUnaryReal(const UnaryOperator *E) |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 3578 | { return Visit(E->getSubExpr()); } |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3579 | bool VisitCastExpr(const CastExpr* E); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3580 | bool VisitInitListExpr(const InitListExpr *E); |
| 3581 | bool VisitUnaryImag(const UnaryOperator *E); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 3582 | // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div, |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 3583 | // binary comparisons, binary and/or/xor, |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 3584 | // shufflevector, ExtVectorElementExpr |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3585 | }; |
| 3586 | } // end anonymous namespace |
| 3587 | |
| 3588 | static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3589 | assert(E->isRValue() && E->getType()->isVectorType() &&"not a vector rvalue"); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3590 | return VectorExprEvaluator(Info, Result).Visit(E); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3591 | } |
| 3592 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3593 | bool VectorExprEvaluator::VisitCastExpr(const CastExpr* E) { |
| 3594 | const VectorType *VTy = E->getType()->castAs<VectorType>(); |
Nate Begeman | c0b8b19 | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 3595 | unsigned NElts = VTy->getNumElements(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3596 | |
Richard Smith | d62ca37 | 2011-12-06 22:44:34 +0000 | [diff] [blame] | 3597 | const Expr *SE = E->getSubExpr(); |
Nate Begeman | e8c9e92 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 3598 | QualType SETy = SE->getType(); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3599 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3600 | switch (E->getCastKind()) { |
| 3601 | case CK_VectorSplat: { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3602 | APValue Val = APValue(); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3603 | if (SETy->isIntegerType()) { |
| 3604 | APSInt IntResult; |
| 3605 | if (!EvaluateInteger(SE, IntResult, Info)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3606 | return false; |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3607 | Val = APValue(IntResult); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3608 | } else if (SETy->isRealFloatingType()) { |
| 3609 | APFloat F(0.0); |
| 3610 | if (!EvaluateFloat(SE, F, Info)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3611 | return false; |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3612 | Val = APValue(F); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3613 | } else { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3614 | return Error(E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3615 | } |
Nate Begeman | c0b8b19 | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 3616 | |
| 3617 | // Splat and create vector APValue. |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3618 | SmallVector<APValue, 4> Elts(NElts, Val); |
| 3619 | return Success(Elts, E); |
Nate Begeman | e8c9e92 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 3620 | } |
Eli Friedman | e6a24e8 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 3621 | case CK_BitCast: { |
| 3622 | // Evaluate the operand into an APInt we can extract from. |
| 3623 | llvm::APInt SValInt; |
| 3624 | if (!EvalAndBitcastToAPInt(Info, SE, SValInt)) |
| 3625 | return false; |
| 3626 | // Extract the elements |
| 3627 | QualType EltTy = VTy->getElementType(); |
| 3628 | unsigned EltSize = Info.Ctx.getTypeSize(EltTy); |
| 3629 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
| 3630 | SmallVector<APValue, 4> Elts; |
| 3631 | if (EltTy->isRealFloatingType()) { |
| 3632 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(EltTy); |
| 3633 | bool isIEESem = &Sem != &APFloat::PPCDoubleDouble; |
| 3634 | unsigned FloatEltSize = EltSize; |
| 3635 | if (&Sem == &APFloat::x87DoubleExtended) |
| 3636 | FloatEltSize = 80; |
| 3637 | for (unsigned i = 0; i < NElts; i++) { |
| 3638 | llvm::APInt Elt; |
| 3639 | if (BigEndian) |
| 3640 | Elt = SValInt.rotl(i*EltSize+FloatEltSize).trunc(FloatEltSize); |
| 3641 | else |
| 3642 | Elt = SValInt.rotr(i*EltSize).trunc(FloatEltSize); |
| 3643 | Elts.push_back(APValue(APFloat(Elt, isIEESem))); |
| 3644 | } |
| 3645 | } else if (EltTy->isIntegerType()) { |
| 3646 | for (unsigned i = 0; i < NElts; i++) { |
| 3647 | llvm::APInt Elt; |
| 3648 | if (BigEndian) |
| 3649 | Elt = SValInt.rotl(i*EltSize+EltSize).zextOrTrunc(EltSize); |
| 3650 | else |
| 3651 | Elt = SValInt.rotr(i*EltSize).zextOrTrunc(EltSize); |
| 3652 | Elts.push_back(APValue(APSInt(Elt, EltTy->isSignedIntegerType()))); |
| 3653 | } |
| 3654 | } else { |
| 3655 | return Error(E); |
| 3656 | } |
| 3657 | return Success(Elts, E); |
| 3658 | } |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3659 | default: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3660 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3661 | } |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3662 | } |
| 3663 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3664 | bool |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3665 | VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3666 | const VectorType *VT = E->getType()->castAs<VectorType>(); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3667 | unsigned NumInits = E->getNumInits(); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 3668 | unsigned NumElements = VT->getNumElements(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3669 | |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3670 | QualType EltTy = VT->getElementType(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3671 | SmallVector<APValue, 4> Elements; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3672 | |
Eli Friedman | 3edd5a9 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 3673 | // The number of initializers can be less than the number of |
| 3674 | // vector elements. For OpenCL, this can be due to nested vector |
| 3675 | // initialization. For GCC compatibility, missing trailing elements |
| 3676 | // should be initialized with zeroes. |
| 3677 | unsigned CountInits = 0, CountElts = 0; |
| 3678 | while (CountElts < NumElements) { |
| 3679 | // Handle nested vector initialization. |
| 3680 | if (CountInits < NumInits |
| 3681 | && E->getInit(CountInits)->getType()->isExtVectorType()) { |
| 3682 | APValue v; |
| 3683 | if (!EvaluateVector(E->getInit(CountInits), v, Info)) |
| 3684 | return Error(E); |
| 3685 | unsigned vlen = v.getVectorLength(); |
| 3686 | for (unsigned j = 0; j < vlen; j++) |
| 3687 | Elements.push_back(v.getVectorElt(j)); |
| 3688 | CountElts += vlen; |
| 3689 | } else if (EltTy->isIntegerType()) { |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3690 | llvm::APSInt sInt(32); |
Eli Friedman | 3edd5a9 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 3691 | if (CountInits < NumInits) { |
| 3692 | if (!EvaluateInteger(E->getInit(CountInits), sInt, Info)) |
Richard Smith | 4b1f684 | 2012-03-13 20:58:32 +0000 | [diff] [blame] | 3693 | return false; |
Eli Friedman | 3edd5a9 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 3694 | } else // trailing integer zero. |
| 3695 | sInt = Info.Ctx.MakeIntValue(0, EltTy); |
| 3696 | Elements.push_back(APValue(sInt)); |
| 3697 | CountElts++; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3698 | } else { |
| 3699 | llvm::APFloat f(0.0); |
Eli Friedman | 3edd5a9 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 3700 | if (CountInits < NumInits) { |
| 3701 | if (!EvaluateFloat(E->getInit(CountInits), f, Info)) |
Richard Smith | 4b1f684 | 2012-03-13 20:58:32 +0000 | [diff] [blame] | 3702 | return false; |
Eli Friedman | 3edd5a9 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 3703 | } else // trailing float zero. |
| 3704 | f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)); |
| 3705 | Elements.push_back(APValue(f)); |
| 3706 | CountElts++; |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 3707 | } |
Eli Friedman | 3edd5a9 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 3708 | CountInits++; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3709 | } |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3710 | return Success(Elements, E); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3711 | } |
| 3712 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3713 | bool |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3714 | VectorExprEvaluator::ZeroInitialization(const Expr *E) { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3715 | const VectorType *VT = E->getType()->getAs<VectorType>(); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 3716 | QualType EltTy = VT->getElementType(); |
| 3717 | APValue ZeroElement; |
| 3718 | if (EltTy->isIntegerType()) |
| 3719 | ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy)); |
| 3720 | else |
| 3721 | ZeroElement = |
| 3722 | APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy))); |
| 3723 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3724 | SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3725 | return Success(Elements, E); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 3726 | } |
| 3727 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3728 | bool VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 3729 | VisitIgnoredValue(E->getSubExpr()); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3730 | return ZeroInitialization(E); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 3731 | } |
| 3732 | |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3733 | //===----------------------------------------------------------------------===// |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3734 | // Array Evaluation |
| 3735 | //===----------------------------------------------------------------------===// |
| 3736 | |
| 3737 | namespace { |
| 3738 | class ArrayExprEvaluator |
| 3739 | : public ExprEvaluatorBase<ArrayExprEvaluator, bool> { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3740 | const LValue &This; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3741 | APValue &Result; |
| 3742 | public: |
| 3743 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3744 | ArrayExprEvaluator(EvalInfo &Info, const LValue &This, APValue &Result) |
| 3745 | : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {} |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3746 | |
| 3747 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | f3908f2 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 3748 | assert((V.isArray() || V.isLValue()) && |
| 3749 | "expected array or string literal"); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3750 | Result = V; |
| 3751 | return true; |
| 3752 | } |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3753 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3754 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3755 | const ConstantArrayType *CAT = |
| 3756 | Info.Ctx.getAsConstantArrayType(E->getType()); |
| 3757 | if (!CAT) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3758 | return Error(E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3759 | |
| 3760 | Result = APValue(APValue::UninitArray(), 0, |
| 3761 | CAT->getSize().getZExtValue()); |
| 3762 | if (!Result.hasArrayFiller()) return true; |
| 3763 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3764 | // Zero-initialize all elements. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3765 | LValue Subobject = This; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3766 | Subobject.addArray(Info, E, CAT); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3767 | ImplicitValueInitExpr VIE(CAT->getElementType()); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3768 | return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, &VIE); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3769 | } |
| 3770 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3771 | bool VisitInitListExpr(const InitListExpr *E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3772 | bool VisitCXXConstructExpr(const CXXConstructExpr *E); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3773 | }; |
| 3774 | } // end anonymous namespace |
| 3775 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3776 | static bool EvaluateArray(const Expr *E, const LValue &This, |
| 3777 | APValue &Result, EvalInfo &Info) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3778 | assert(E->isRValue() && E->getType()->isArrayType() && "not an array rvalue"); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3779 | return ArrayExprEvaluator(Info, This, Result).Visit(E); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3780 | } |
| 3781 | |
| 3782 | bool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 3783 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(E->getType()); |
| 3784 | if (!CAT) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3785 | return Error(E); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3786 | |
Richard Smith | 974c5f9 | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 3787 | // C++11 [dcl.init.string]p1: A char array [...] can be initialized by [...] |
| 3788 | // an appropriately-typed string literal enclosed in braces. |
Richard Smith | fe58720 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 3789 | if (E->isStringLiteralInit()) { |
Richard Smith | 974c5f9 | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 3790 | LValue LV; |
| 3791 | if (!EvaluateLValue(E->getInit(0), LV, Info)) |
| 3792 | return false; |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3793 | APValue Val; |
Richard Smith | f3908f2 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 3794 | LV.moveInto(Val); |
| 3795 | return Success(Val, E); |
Richard Smith | 974c5f9 | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 3796 | } |
| 3797 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3798 | bool Success = true; |
| 3799 | |
Richard Smith | de31aa7 | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 3800 | assert((!Result.isArray() || Result.getArrayInitializedElts() == 0) && |
| 3801 | "zero-initialized array shouldn't have any initialized elts"); |
| 3802 | APValue Filler; |
| 3803 | if (Result.isArray() && Result.hasArrayFiller()) |
| 3804 | Filler = Result.getArrayFiller(); |
| 3805 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3806 | Result = APValue(APValue::UninitArray(), E->getNumInits(), |
| 3807 | CAT->getSize().getZExtValue()); |
Richard Smith | de31aa7 | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 3808 | |
| 3809 | // If the array was previously zero-initialized, preserve the |
| 3810 | // zero-initialized values. |
| 3811 | if (!Filler.isUninit()) { |
| 3812 | for (unsigned I = 0, E = Result.getArrayInitializedElts(); I != E; ++I) |
| 3813 | Result.getArrayInitializedElt(I) = Filler; |
| 3814 | if (Result.hasArrayFiller()) |
| 3815 | Result.getArrayFiller() = Filler; |
| 3816 | } |
| 3817 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3818 | LValue Subobject = This; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3819 | Subobject.addArray(Info, E, CAT); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3820 | unsigned Index = 0; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3821 | for (InitListExpr::const_iterator I = E->begin(), End = E->end(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3822 | I != End; ++I, ++Index) { |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3823 | if (!EvaluateInPlace(Result.getArrayInitializedElt(Index), |
| 3824 | Info, Subobject, cast<Expr>(*I)) || |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3825 | !HandleLValueArrayAdjustment(Info, cast<Expr>(*I), Subobject, |
| 3826 | CAT->getElementType(), 1)) { |
| 3827 | if (!Info.keepEvaluatingAfterFailure()) |
| 3828 | return false; |
| 3829 | Success = false; |
| 3830 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3831 | } |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3832 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3833 | if (!Result.hasArrayFiller()) return Success; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3834 | assert(E->hasArrayFiller() && "no array filler for incomplete init list"); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3835 | // FIXME: The Subobject here isn't necessarily right. This rarely matters, |
| 3836 | // but sometimes does: |
| 3837 | // struct S { constexpr S() : p(&p) {} void *p; }; |
| 3838 | // S s[10] = {}; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3839 | return EvaluateInPlace(Result.getArrayFiller(), Info, |
| 3840 | Subobject, E->getArrayFiller()) && Success; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3841 | } |
| 3842 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3843 | bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
Richard Smith | de31aa7 | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 3844 | // FIXME: The Subobject here isn't necessarily right. This rarely matters, |
| 3845 | // but sometimes does: |
| 3846 | // struct S { constexpr S() : p(&p) {} void *p; }; |
| 3847 | // S s[10]; |
| 3848 | LValue Subobject = This; |
| 3849 | |
| 3850 | APValue *Value = &Result; |
| 3851 | bool HadZeroInit = true; |
Richard Smith | a4334df | 2012-07-10 22:12:55 +0000 | [diff] [blame] | 3852 | QualType ElemTy = E->getType(); |
| 3853 | while (const ConstantArrayType *CAT = |
| 3854 | Info.Ctx.getAsConstantArrayType(ElemTy)) { |
Richard Smith | de31aa7 | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 3855 | Subobject.addArray(Info, E, CAT); |
| 3856 | HadZeroInit &= !Value->isUninit(); |
| 3857 | if (!HadZeroInit) |
| 3858 | *Value = APValue(APValue::UninitArray(), 0, CAT->getSize().getZExtValue()); |
| 3859 | if (!Value->hasArrayFiller()) |
| 3860 | return true; |
Richard Smith | de31aa7 | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 3861 | Value = &Value->getArrayFiller(); |
Richard Smith | a4334df | 2012-07-10 22:12:55 +0000 | [diff] [blame] | 3862 | ElemTy = CAT->getElementType(); |
Richard Smith | de31aa7 | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 3863 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3864 | |
Richard Smith | a4334df | 2012-07-10 22:12:55 +0000 | [diff] [blame] | 3865 | if (!ElemTy->isRecordType()) |
| 3866 | return Error(E); |
| 3867 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3868 | const CXXConstructorDecl *FD = E->getConstructor(); |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 3869 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3870 | bool ZeroInit = E->requiresZeroInitialization(); |
| 3871 | if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) { |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 3872 | if (HadZeroInit) |
| 3873 | return true; |
| 3874 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3875 | if (ZeroInit) { |
Richard Smith | a4334df | 2012-07-10 22:12:55 +0000 | [diff] [blame] | 3876 | ImplicitValueInitExpr VIE(ElemTy); |
Richard Smith | de31aa7 | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 3877 | return EvaluateInPlace(*Value, Info, Subobject, &VIE); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3878 | } |
| 3879 | |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 3880 | const CXXRecordDecl *RD = FD->getParent(); |
| 3881 | if (RD->isUnion()) |
Richard Smith | de31aa7 | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 3882 | *Value = APValue((FieldDecl*)0); |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 3883 | else |
Richard Smith | de31aa7 | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 3884 | *Value = |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 3885 | APValue(APValue::UninitStruct(), RD->getNumBases(), |
| 3886 | std::distance(RD->field_begin(), RD->field_end())); |
| 3887 | return true; |
| 3888 | } |
| 3889 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3890 | const FunctionDecl *Definition = 0; |
| 3891 | FD->getBody(Definition); |
| 3892 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3893 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition)) |
| 3894 | return false; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3895 | |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 3896 | if (ZeroInit && !HadZeroInit) { |
Richard Smith | a4334df | 2012-07-10 22:12:55 +0000 | [diff] [blame] | 3897 | ImplicitValueInitExpr VIE(ElemTy); |
Richard Smith | de31aa7 | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 3898 | if (!EvaluateInPlace(*Value, Info, Subobject, &VIE)) |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3899 | return false; |
| 3900 | } |
| 3901 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3902 | llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs()); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3903 | return HandleConstructorCall(E->getExprLoc(), Subobject, Args, |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3904 | cast<CXXConstructorDecl>(Definition), |
Richard Smith | de31aa7 | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 3905 | Info, *Value); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3906 | } |
| 3907 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3908 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3909 | // Integer Evaluation |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3910 | // |
| 3911 | // As a GNU extension, we support casting pointers to sufficiently-wide integer |
| 3912 | // types and back in constant folding. Integer values are thus represented |
| 3913 | // either as an integer-valued APValue, or as an lvalue-valued APValue. |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3914 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3915 | |
| 3916 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 3917 | class IntExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3918 | : public ExprEvaluatorBase<IntExprEvaluator, bool> { |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3919 | APValue &Result; |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 3920 | public: |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3921 | IntExprEvaluator(EvalInfo &info, APValue &result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3922 | : ExprEvaluatorBaseTy(info), Result(result) {} |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3923 | |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 3924 | bool Success(const llvm::APSInt &SI, const Expr *E, APValue &Result) { |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 3925 | assert(E->getType()->isIntegralOrEnumerationType() && |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3926 | "Invalid evaluation result."); |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 3927 | assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 3928 | "Invalid evaluation result."); |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 3929 | assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 3930 | "Invalid evaluation result."); |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3931 | Result = APValue(SI); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 3932 | return true; |
| 3933 | } |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 3934 | bool Success(const llvm::APSInt &SI, const Expr *E) { |
| 3935 | return Success(SI, E, Result); |
| 3936 | } |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 3937 | |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 3938 | bool Success(const llvm::APInt &I, const Expr *E, APValue &Result) { |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3939 | assert(E->getType()->isIntegralOrEnumerationType() && |
| 3940 | "Invalid evaluation result."); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 3941 | assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 3942 | "Invalid evaluation result."); |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3943 | Result = APValue(APSInt(I)); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3944 | Result.getInt().setIsUnsigned( |
| 3945 | E->getType()->isUnsignedIntegerOrEnumerationType()); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3946 | return true; |
| 3947 | } |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 3948 | bool Success(const llvm::APInt &I, const Expr *E) { |
| 3949 | return Success(I, E, Result); |
| 3950 | } |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3951 | |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 3952 | bool Success(uint64_t Value, const Expr *E, APValue &Result) { |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3953 | assert(E->getType()->isIntegralOrEnumerationType() && |
| 3954 | "Invalid evaluation result."); |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3955 | Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType())); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3956 | return true; |
| 3957 | } |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 3958 | bool Success(uint64_t Value, const Expr *E) { |
| 3959 | return Success(Value, E, Result); |
| 3960 | } |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3961 | |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 3962 | bool Success(CharUnits Size, const Expr *E) { |
| 3963 | return Success(Size.getQuantity(), E); |
| 3964 | } |
| 3965 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3966 | bool Success(const APValue &V, const Expr *E) { |
Eli Friedman | 5930a4c | 2012-01-05 23:59:40 +0000 | [diff] [blame] | 3967 | if (V.isLValue() || V.isAddrLabelDiff()) { |
Richard Smith | 342f1f8 | 2011-10-29 22:55:55 +0000 | [diff] [blame] | 3968 | Result = V; |
| 3969 | return true; |
| 3970 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3971 | return Success(V.getInt(), E); |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 3972 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3973 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3974 | bool ZeroInitialization(const Expr *E) { return Success(0, E); } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 3975 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3976 | //===--------------------------------------------------------------------===// |
| 3977 | // Visitor Methods |
| 3978 | //===--------------------------------------------------------------------===// |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 3979 | |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 3980 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3981 | return Success(E->getValue(), E); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 3982 | } |
| 3983 | bool VisitCharacterLiteral(const CharacterLiteral *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3984 | return Success(E->getValue(), E); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 3985 | } |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 3986 | |
| 3987 | bool CheckReferencedDecl(const Expr *E, const Decl *D); |
| 3988 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3989 | if (CheckReferencedDecl(E, E->getDecl())) |
| 3990 | return true; |
| 3991 | |
| 3992 | return ExprEvaluatorBaseTy::VisitDeclRefExpr(E); |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 3993 | } |
| 3994 | bool VisitMemberExpr(const MemberExpr *E) { |
| 3995 | if (CheckReferencedDecl(E, E->getMemberDecl())) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3996 | VisitIgnoredValue(E->getBase()); |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 3997 | return true; |
| 3998 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3999 | |
| 4000 | return ExprEvaluatorBaseTy::VisitMemberExpr(E); |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 4001 | } |
| 4002 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4003 | bool VisitCallExpr(const CallExpr *E); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 4004 | bool VisitBinaryOperator(const BinaryOperator *E); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 4005 | bool VisitOffsetOfExpr(const OffsetOfExpr *E); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 4006 | bool VisitUnaryOperator(const UnaryOperator *E); |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 4007 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4008 | bool VisitCastExpr(const CastExpr* E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 4009 | bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E); |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 4010 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4011 | bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4012 | return Success(E->getValue(), E); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4013 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4014 | |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 4015 | bool VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) { |
| 4016 | return Success(E->getValue(), E); |
| 4017 | } |
| 4018 | |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4019 | // Note, GNU defines __null as an integer, not a pointer. |
Anders Carlsson | 3f70456 | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 4020 | bool VisitGNUNullExpr(const GNUNullExpr *E) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4021 | return ZeroInitialization(E); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 4022 | } |
| 4023 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 4024 | bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) { |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4025 | return Success(E->getValue(), E); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 4026 | } |
| 4027 | |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 4028 | bool VisitBinaryTypeTraitExpr(const BinaryTypeTraitExpr *E) { |
| 4029 | return Success(E->getValue(), E); |
| 4030 | } |
| 4031 | |
Douglas Gregor | 4ca8ac2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4032 | bool VisitTypeTraitExpr(const TypeTraitExpr *E) { |
| 4033 | return Success(E->getValue(), E); |
| 4034 | } |
| 4035 | |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 4036 | bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) { |
| 4037 | return Success(E->getValue(), E); |
| 4038 | } |
| 4039 | |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 4040 | bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) { |
| 4041 | return Success(E->getValue(), E); |
| 4042 | } |
| 4043 | |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 4044 | bool VisitUnaryReal(const UnaryOperator *E); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 4045 | bool VisitUnaryImag(const UnaryOperator *E); |
| 4046 | |
Sebastian Redl | 295995c | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 4047 | bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E); |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 4048 | bool VisitSizeOfPackExpr(const SizeOfPackExpr *E); |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 4049 | |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 4050 | private: |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 4051 | CharUnits GetAlignOfExpr(const Expr *E); |
| 4052 | CharUnits GetAlignOfType(QualType T); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4053 | static QualType GetObjectType(APValue::LValueBase B); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4054 | bool TryEvaluateBuiltinObjectSize(const CallExpr *E); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 4055 | // FIXME: Missing: array subscript of vector, member of vector |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 4056 | }; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4057 | } // end anonymous namespace |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 4058 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4059 | /// EvaluateIntegerOrLValue - Evaluate an rvalue integral-typed expression, and |
| 4060 | /// produce either the integer value or a pointer. |
| 4061 | /// |
| 4062 | /// GCC has a heinous extension which folds casts between pointer types and |
| 4063 | /// pointer-sized integral types. We support this by allowing the evaluation of |
| 4064 | /// an integer rvalue to produce a pointer (represented as an lvalue) instead. |
| 4065 | /// Some simple arithmetic on such values is supported (they are treated much |
| 4066 | /// like char*). |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4067 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4068 | EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4069 | assert(E->isRValue() && E->getType()->isIntegralOrEnumerationType()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4070 | return IntExprEvaluator(Info, Result).Visit(E); |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 4071 | } |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 4072 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4073 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info) { |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4074 | APValue Val; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4075 | if (!EvaluateIntegerOrLValue(E, Val, Info)) |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 4076 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4077 | if (!Val.isInt()) { |
| 4078 | // FIXME: It would be better to produce the diagnostic for casting |
| 4079 | // a pointer to an integer. |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 4080 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4081 | return false; |
| 4082 | } |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 4083 | Result = Val.getInt(); |
| 4084 | return true; |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 4085 | } |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 4086 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4087 | /// Check whether the given declaration can be directly converted to an integral |
| 4088 | /// rvalue. If not, no diagnostic is produced; there are other things we can |
| 4089 | /// try. |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 4090 | bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) { |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 4091 | // Enums are integer constant exprs. |
Abramo Bagnara | bfbdcd8 | 2011-06-30 09:36:05 +0000 | [diff] [blame] | 4092 | if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) { |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 4093 | // Check for signedness/width mismatches between E type and ECD value. |
| 4094 | bool SameSign = (ECD->getInitVal().isSigned() |
| 4095 | == E->getType()->isSignedIntegerOrEnumerationType()); |
| 4096 | bool SameWidth = (ECD->getInitVal().getBitWidth() |
| 4097 | == Info.Ctx.getIntWidth(E->getType())); |
| 4098 | if (SameSign && SameWidth) |
| 4099 | return Success(ECD->getInitVal(), E); |
| 4100 | else { |
| 4101 | // Get rid of mismatch (otherwise Success assertions will fail) |
| 4102 | // by computing a new value matching the type of E. |
| 4103 | llvm::APSInt Val = ECD->getInitVal(); |
| 4104 | if (!SameSign) |
| 4105 | Val.setIsSigned(!ECD->getInitVal().isSigned()); |
| 4106 | if (!SameWidth) |
| 4107 | Val = Val.extOrTrunc(Info.Ctx.getIntWidth(E->getType())); |
| 4108 | return Success(Val, E); |
| 4109 | } |
Abramo Bagnara | bfbdcd8 | 2011-06-30 09:36:05 +0000 | [diff] [blame] | 4110 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4111 | return false; |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 4112 | } |
| 4113 | |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 4114 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
| 4115 | /// as GCC. |
| 4116 | static int EvaluateBuiltinClassifyType(const CallExpr *E) { |
| 4117 | // The following enum mimics the values returned by GCC. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4118 | // FIXME: Does GCC differ between lvalue and rvalue references here? |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 4119 | enum gcc_type_class { |
| 4120 | no_type_class = -1, |
| 4121 | void_type_class, integer_type_class, char_type_class, |
| 4122 | enumeral_type_class, boolean_type_class, |
| 4123 | pointer_type_class, reference_type_class, offset_type_class, |
| 4124 | real_type_class, complex_type_class, |
| 4125 | function_type_class, method_type_class, |
| 4126 | record_type_class, union_type_class, |
| 4127 | array_type_class, string_type_class, |
| 4128 | lang_type_class |
| 4129 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4130 | |
| 4131 | // 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] | 4132 | // ideal, however it is what gcc does. |
| 4133 | if (E->getNumArgs() == 0) |
| 4134 | return no_type_class; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4135 | |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 4136 | QualType ArgTy = E->getArg(0)->getType(); |
| 4137 | if (ArgTy->isVoidType()) |
| 4138 | return void_type_class; |
| 4139 | else if (ArgTy->isEnumeralType()) |
| 4140 | return enumeral_type_class; |
| 4141 | else if (ArgTy->isBooleanType()) |
| 4142 | return boolean_type_class; |
| 4143 | else if (ArgTy->isCharType()) |
| 4144 | return string_type_class; // gcc doesn't appear to use char_type_class |
| 4145 | else if (ArgTy->isIntegerType()) |
| 4146 | return integer_type_class; |
| 4147 | else if (ArgTy->isPointerType()) |
| 4148 | return pointer_type_class; |
| 4149 | else if (ArgTy->isReferenceType()) |
| 4150 | return reference_type_class; |
| 4151 | else if (ArgTy->isRealType()) |
| 4152 | return real_type_class; |
| 4153 | else if (ArgTy->isComplexType()) |
| 4154 | return complex_type_class; |
| 4155 | else if (ArgTy->isFunctionType()) |
| 4156 | return function_type_class; |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 4157 | else if (ArgTy->isStructureOrClassType()) |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 4158 | return record_type_class; |
| 4159 | else if (ArgTy->isUnionType()) |
| 4160 | return union_type_class; |
| 4161 | else if (ArgTy->isArrayType()) |
| 4162 | return array_type_class; |
| 4163 | else if (ArgTy->isUnionType()) |
| 4164 | return union_type_class; |
| 4165 | else // FIXME: offset_type_class, method_type_class, & lang_type_class? |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4166 | llvm_unreachable("CallExpr::isBuiltinClassifyType(): unimplemented type"); |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 4167 | } |
| 4168 | |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 4169 | /// EvaluateBuiltinConstantPForLValue - Determine the result of |
| 4170 | /// __builtin_constant_p when applied to the given lvalue. |
| 4171 | /// |
| 4172 | /// An lvalue is only "constant" if it is a pointer or reference to the first |
| 4173 | /// character of a string literal. |
| 4174 | template<typename LValue> |
| 4175 | static bool EvaluateBuiltinConstantPForLValue(const LValue &LV) { |
Douglas Gregor | 8e55ed1 | 2012-03-11 02:23:56 +0000 | [diff] [blame] | 4176 | const Expr *E = LV.getLValueBase().template dyn_cast<const Expr*>(); |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 4177 | return E && isa<StringLiteral>(E) && LV.getLValueOffset().isZero(); |
| 4178 | } |
| 4179 | |
| 4180 | /// EvaluateBuiltinConstantP - Evaluate __builtin_constant_p as similarly to |
| 4181 | /// GCC as we can manage. |
| 4182 | static bool EvaluateBuiltinConstantP(ASTContext &Ctx, const Expr *Arg) { |
| 4183 | QualType ArgType = Arg->getType(); |
| 4184 | |
| 4185 | // __builtin_constant_p always has one operand. The rules which gcc follows |
| 4186 | // are not precisely documented, but are as follows: |
| 4187 | // |
| 4188 | // - If the operand is of integral, floating, complex or enumeration type, |
| 4189 | // and can be folded to a known value of that type, it returns 1. |
| 4190 | // - If the operand and can be folded to a pointer to the first character |
| 4191 | // of a string literal (or such a pointer cast to an integral type), it |
| 4192 | // returns 1. |
| 4193 | // |
| 4194 | // Otherwise, it returns 0. |
| 4195 | // |
| 4196 | // FIXME: GCC also intends to return 1 for literals of aggregate types, but |
| 4197 | // its support for this does not currently work. |
| 4198 | if (ArgType->isIntegralOrEnumerationType()) { |
| 4199 | Expr::EvalResult Result; |
| 4200 | if (!Arg->EvaluateAsRValue(Result, Ctx) || Result.HasSideEffects) |
| 4201 | return false; |
| 4202 | |
| 4203 | APValue &V = Result.Val; |
| 4204 | if (V.getKind() == APValue::Int) |
| 4205 | return true; |
| 4206 | |
| 4207 | return EvaluateBuiltinConstantPForLValue(V); |
| 4208 | } else if (ArgType->isFloatingType() || ArgType->isAnyComplexType()) { |
| 4209 | return Arg->isEvaluatable(Ctx); |
| 4210 | } else if (ArgType->isPointerType() || Arg->isGLValue()) { |
| 4211 | LValue LV; |
| 4212 | Expr::EvalStatus Status; |
| 4213 | EvalInfo Info(Ctx, Status); |
| 4214 | if ((Arg->isGLValue() ? EvaluateLValue(Arg, LV, Info) |
| 4215 | : EvaluatePointer(Arg, LV, Info)) && |
| 4216 | !Status.HasSideEffects) |
| 4217 | return EvaluateBuiltinConstantPForLValue(LV); |
| 4218 | } |
| 4219 | |
| 4220 | // Anything else isn't considered to be sufficiently constant. |
| 4221 | return false; |
| 4222 | } |
| 4223 | |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4224 | /// Retrieves the "underlying object type" of the given expression, |
| 4225 | /// as used by __builtin_object_size. |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4226 | QualType IntExprEvaluator::GetObjectType(APValue::LValueBase B) { |
| 4227 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
| 4228 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4229 | return VD->getType(); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4230 | } else if (const Expr *E = B.get<const Expr*>()) { |
| 4231 | if (isa<CompoundLiteralExpr>(E)) |
| 4232 | return E->getType(); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4233 | } |
| 4234 | |
| 4235 | return QualType(); |
| 4236 | } |
| 4237 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4238 | bool IntExprEvaluator::TryEvaluateBuiltinObjectSize(const CallExpr *E) { |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4239 | LValue Base; |
Richard Smith | c679485 | 2012-05-23 04:13:20 +0000 | [diff] [blame] | 4240 | |
| 4241 | { |
| 4242 | // The operand of __builtin_object_size is never evaluated for side-effects. |
| 4243 | // If there are any, but we can determine the pointed-to object anyway, then |
| 4244 | // ignore the side-effects. |
| 4245 | SpeculativeEvaluationRAII SpeculativeEval(Info); |
| 4246 | if (!EvaluatePointer(E->getArg(0), Base, Info)) |
| 4247 | return false; |
| 4248 | } |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4249 | |
| 4250 | // If we can prove the base is null, lower to zero now. |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4251 | if (!Base.getLValueBase()) return Success(0, E); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4252 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4253 | QualType T = GetObjectType(Base.getLValueBase()); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4254 | if (T.isNull() || |
| 4255 | T->isIncompleteType() || |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 4256 | T->isFunctionType() || |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4257 | T->isVariablyModifiedType() || |
| 4258 | T->isDependentType()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4259 | return Error(E); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4260 | |
| 4261 | CharUnits Size = Info.Ctx.getTypeSizeInChars(T); |
| 4262 | CharUnits Offset = Base.getLValueOffset(); |
| 4263 | |
| 4264 | if (!Offset.isNegative() && Offset <= Size) |
| 4265 | Size -= Offset; |
| 4266 | else |
| 4267 | Size = CharUnits::Zero(); |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 4268 | return Success(Size, E); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4269 | } |
| 4270 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4271 | bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | 2c39d71 | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 4272 | switch (unsigned BuiltinOp = E->isBuiltinCall()) { |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 4273 | default: |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4274 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 4275 | |
| 4276 | case Builtin::BI__builtin_object_size: { |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4277 | if (TryEvaluateBuiltinObjectSize(E)) |
| 4278 | return true; |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 4279 | |
Richard Smith | 8ae4ec2 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 4280 | // If evaluating the argument has side-effects, we can't determine the size |
| 4281 | // of the object, and so we lower it to unknown now. CodeGen relies on us to |
| 4282 | // handle all cases where the expression has side-effects. |
Fariborz Jahanian | 393c247 | 2009-11-05 18:03:03 +0000 | [diff] [blame] | 4283 | if (E->getArg(0)->HasSideEffects(Info.Ctx)) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4284 | if (E->getArg(1)->EvaluateKnownConstInt(Info.Ctx).getZExtValue() <= 1) |
Chris Lattner | cf18465 | 2009-11-03 19:48:51 +0000 | [diff] [blame] | 4285 | return Success(-1ULL, E); |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 4286 | return Success(0, E); |
| 4287 | } |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 4288 | |
Richard Smith | c679485 | 2012-05-23 04:13:20 +0000 | [diff] [blame] | 4289 | // Expression had no side effects, but we couldn't statically determine the |
| 4290 | // size of the referenced object. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4291 | return Error(E); |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 4292 | } |
| 4293 | |
Benjamin Kramer | d190057 | 2012-10-06 14:42:22 +0000 | [diff] [blame] | 4294 | case Builtin::BI__builtin_bswap16: |
Richard Smith | 70d38f3 | 2012-09-28 20:20:52 +0000 | [diff] [blame] | 4295 | case Builtin::BI__builtin_bswap32: |
| 4296 | case Builtin::BI__builtin_bswap64: { |
| 4297 | APSInt Val; |
| 4298 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 4299 | return false; |
| 4300 | |
| 4301 | return Success(Val.byteSwap(), E); |
| 4302 | } |
| 4303 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 4304 | case Builtin::BI__builtin_classify_type: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4305 | return Success(EvaluateBuiltinClassifyType(E), E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4306 | |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 4307 | case Builtin::BI__builtin_constant_p: |
| 4308 | return Success(EvaluateBuiltinConstantP(Info.Ctx, E->getArg(0)), E); |
Richard Smith | e052d46 | 2011-12-09 02:04:48 +0000 | [diff] [blame] | 4309 | |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 4310 | case Builtin::BI__builtin_eh_return_data_regno: { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4311 | int Operand = E->getArg(0)->EvaluateKnownConstInt(Info.Ctx).getZExtValue(); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4312 | Operand = Info.Ctx.getTargetInfo().getEHDataRegisterNumber(Operand); |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 4313 | return Success(Operand, E); |
| 4314 | } |
Eli Friedman | c4a2638 | 2010-02-13 00:10:10 +0000 | [diff] [blame] | 4315 | |
| 4316 | case Builtin::BI__builtin_expect: |
| 4317 | return Visit(E->getArg(0)); |
Richard Smith | 40b993a | 2012-01-18 03:06:12 +0000 | [diff] [blame] | 4318 | |
Douglas Gregor | 5726d40 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 4319 | case Builtin::BIstrlen: |
Richard Smith | 40b993a | 2012-01-18 03:06:12 +0000 | [diff] [blame] | 4320 | // A call to strlen is not a constant expression. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4321 | if (Info.getLangOpts().CPlusPlus11) |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 4322 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
Richard Smith | 40b993a | 2012-01-18 03:06:12 +0000 | [diff] [blame] | 4323 | << /*isConstexpr*/0 << /*isConstructor*/0 << "'strlen'"; |
| 4324 | else |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 4325 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 40b993a | 2012-01-18 03:06:12 +0000 | [diff] [blame] | 4326 | // Fall through. |
Douglas Gregor | 5726d40 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 4327 | case Builtin::BI__builtin_strlen: |
| 4328 | // As an extension, we support strlen() and __builtin_strlen() as constant |
| 4329 | // expressions when the argument is a string literal. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4330 | if (const StringLiteral *S |
Douglas Gregor | 5726d40 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 4331 | = dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenImpCasts())) { |
| 4332 | // The string literal may have embedded null characters. Find the first |
| 4333 | // one and truncate there. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4334 | StringRef Str = S->getString(); |
| 4335 | StringRef::size_type Pos = Str.find(0); |
| 4336 | if (Pos != StringRef::npos) |
Douglas Gregor | 5726d40 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 4337 | Str = Str.substr(0, Pos); |
| 4338 | |
| 4339 | return Success(Str.size(), E); |
| 4340 | } |
| 4341 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4342 | return Error(E); |
Eli Friedman | 454b57a | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 4343 | |
Richard Smith | 2c39d71 | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 4344 | case Builtin::BI__atomic_always_lock_free: |
Richard Smith | fafbf06 | 2012-04-11 17:55:32 +0000 | [diff] [blame] | 4345 | case Builtin::BI__atomic_is_lock_free: |
| 4346 | case Builtin::BI__c11_atomic_is_lock_free: { |
Eli Friedman | 454b57a | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 4347 | APSInt SizeVal; |
| 4348 | if (!EvaluateInteger(E->getArg(0), SizeVal, Info)) |
| 4349 | return false; |
| 4350 | |
| 4351 | // For __atomic_is_lock_free(sizeof(_Atomic(T))), if the size is a power |
| 4352 | // of two less than the maximum inline atomic width, we know it is |
| 4353 | // lock-free. If the size isn't a power of two, or greater than the |
| 4354 | // maximum alignment where we promote atomics, we know it is not lock-free |
| 4355 | // (at least not in the sense of atomic_is_lock_free). Otherwise, |
| 4356 | // the answer can only be determined at runtime; for example, 16-byte |
| 4357 | // atomics have lock-free implementations on some, but not all, |
| 4358 | // x86-64 processors. |
| 4359 | |
| 4360 | // Check power-of-two. |
| 4361 | CharUnits Size = CharUnits::fromQuantity(SizeVal.getZExtValue()); |
Richard Smith | 2c39d71 | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 4362 | if (Size.isPowerOfTwo()) { |
| 4363 | // Check against inlining width. |
| 4364 | unsigned InlineWidthBits = |
| 4365 | Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth(); |
| 4366 | if (Size <= Info.Ctx.toCharUnitsFromBits(InlineWidthBits)) { |
| 4367 | if (BuiltinOp == Builtin::BI__c11_atomic_is_lock_free || |
| 4368 | Size == CharUnits::One() || |
| 4369 | E->getArg(1)->isNullPointerConstant(Info.Ctx, |
| 4370 | Expr::NPC_NeverValueDependent)) |
| 4371 | // OK, we will inline appropriately-aligned operations of this size, |
| 4372 | // and _Atomic(T) is appropriately-aligned. |
| 4373 | return Success(1, E); |
Eli Friedman | 454b57a | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 4374 | |
Richard Smith | 2c39d71 | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 4375 | QualType PointeeType = E->getArg(1)->IgnoreImpCasts()->getType()-> |
| 4376 | castAs<PointerType>()->getPointeeType(); |
| 4377 | if (!PointeeType->isIncompleteType() && |
| 4378 | Info.Ctx.getTypeAlignInChars(PointeeType) >= Size) { |
| 4379 | // OK, we will inline operations on this object. |
| 4380 | return Success(1, E); |
| 4381 | } |
| 4382 | } |
| 4383 | } |
Eli Friedman | 454b57a | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 4384 | |
Richard Smith | 2c39d71 | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 4385 | return BuiltinOp == Builtin::BI__atomic_always_lock_free ? |
| 4386 | Success(0, E) : Error(E); |
Eli Friedman | 454b57a | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 4387 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 4388 | } |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 4389 | } |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 4390 | |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 4391 | static bool HasSameBase(const LValue &A, const LValue &B) { |
| 4392 | if (!A.getLValueBase()) |
| 4393 | return !B.getLValueBase(); |
| 4394 | if (!B.getLValueBase()) |
| 4395 | return false; |
| 4396 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4397 | if (A.getLValueBase().getOpaqueValue() != |
| 4398 | B.getLValueBase().getOpaqueValue()) { |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 4399 | const Decl *ADecl = GetLValueBaseDecl(A); |
| 4400 | if (!ADecl) |
| 4401 | return false; |
| 4402 | const Decl *BDecl = GetLValueBaseDecl(B); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 4403 | if (!BDecl || ADecl->getCanonicalDecl() != BDecl->getCanonicalDecl()) |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 4404 | return false; |
| 4405 | } |
| 4406 | |
| 4407 | return IsGlobalLValue(A.getLValueBase()) || |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 4408 | A.getLValueCallIndex() == B.getLValueCallIndex(); |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 4409 | } |
| 4410 | |
Richard Smith | 7b48a29 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 4411 | /// Perform the given integer operation, which is known to need at most BitWidth |
| 4412 | /// bits, and check for overflow in the original type (if that type was not an |
| 4413 | /// unsigned type). |
| 4414 | template<typename Operation> |
| 4415 | static APSInt CheckedIntArithmetic(EvalInfo &Info, const Expr *E, |
| 4416 | const APSInt &LHS, const APSInt &RHS, |
| 4417 | unsigned BitWidth, Operation Op) { |
| 4418 | if (LHS.isUnsigned()) |
| 4419 | return Op(LHS, RHS); |
| 4420 | |
| 4421 | APSInt Value(Op(LHS.extend(BitWidth), RHS.extend(BitWidth)), false); |
| 4422 | APSInt Result = Value.trunc(LHS.getBitWidth()); |
| 4423 | if (Result.extend(BitWidth) != Value) |
| 4424 | HandleOverflow(Info, E, Value, E->getType()); |
| 4425 | return Result; |
| 4426 | } |
| 4427 | |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4428 | namespace { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4429 | |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4430 | /// \brief Data recursive integer evaluator of certain binary operators. |
| 4431 | /// |
| 4432 | /// We use a data recursive algorithm for binary operators so that we are able |
| 4433 | /// to handle extreme cases of chained binary operators without causing stack |
| 4434 | /// overflow. |
| 4435 | class DataRecursiveIntBinOpEvaluator { |
| 4436 | struct EvalResult { |
| 4437 | APValue Val; |
| 4438 | bool Failed; |
| 4439 | |
| 4440 | EvalResult() : Failed(false) { } |
| 4441 | |
| 4442 | void swap(EvalResult &RHS) { |
| 4443 | Val.swap(RHS.Val); |
| 4444 | Failed = RHS.Failed; |
| 4445 | RHS.Failed = false; |
| 4446 | } |
| 4447 | }; |
| 4448 | |
| 4449 | struct Job { |
| 4450 | const Expr *E; |
| 4451 | EvalResult LHSResult; // meaningful only for binary operator expression. |
| 4452 | enum { AnyExprKind, BinOpKind, BinOpVisitedLHSKind } Kind; |
| 4453 | |
| 4454 | Job() : StoredInfo(0) { } |
| 4455 | void startSpeculativeEval(EvalInfo &Info) { |
| 4456 | OldEvalStatus = Info.EvalStatus; |
| 4457 | Info.EvalStatus.Diag = 0; |
| 4458 | StoredInfo = &Info; |
| 4459 | } |
| 4460 | ~Job() { |
| 4461 | if (StoredInfo) { |
| 4462 | StoredInfo->EvalStatus = OldEvalStatus; |
| 4463 | } |
| 4464 | } |
| 4465 | private: |
| 4466 | EvalInfo *StoredInfo; // non-null if status changed. |
| 4467 | Expr::EvalStatus OldEvalStatus; |
| 4468 | }; |
| 4469 | |
| 4470 | SmallVector<Job, 16> Queue; |
| 4471 | |
| 4472 | IntExprEvaluator &IntEval; |
| 4473 | EvalInfo &Info; |
| 4474 | APValue &FinalResult; |
| 4475 | |
| 4476 | public: |
| 4477 | DataRecursiveIntBinOpEvaluator(IntExprEvaluator &IntEval, APValue &Result) |
| 4478 | : IntEval(IntEval), Info(IntEval.getEvalInfo()), FinalResult(Result) { } |
| 4479 | |
| 4480 | /// \brief True if \param E is a binary operator that we are going to handle |
| 4481 | /// data recursively. |
| 4482 | /// We handle binary operators that are comma, logical, or that have operands |
| 4483 | /// with integral or enumeration type. |
| 4484 | static bool shouldEnqueue(const BinaryOperator *E) { |
| 4485 | return E->getOpcode() == BO_Comma || |
| 4486 | E->isLogicalOp() || |
| 4487 | (E->getLHS()->getType()->isIntegralOrEnumerationType() && |
| 4488 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 4489 | } |
| 4490 | |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4491 | bool Traverse(const BinaryOperator *E) { |
| 4492 | enqueue(E); |
| 4493 | EvalResult PrevResult; |
Richard Trieu | b778305 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 4494 | while (!Queue.empty()) |
| 4495 | process(PrevResult); |
| 4496 | |
| 4497 | if (PrevResult.Failed) return false; |
Argyrios Kyrtzidis | 2fa975c | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 4498 | |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4499 | FinalResult.swap(PrevResult.Val); |
| 4500 | return true; |
| 4501 | } |
| 4502 | |
| 4503 | private: |
| 4504 | bool Success(uint64_t Value, const Expr *E, APValue &Result) { |
| 4505 | return IntEval.Success(Value, E, Result); |
| 4506 | } |
| 4507 | bool Success(const APSInt &Value, const Expr *E, APValue &Result) { |
| 4508 | return IntEval.Success(Value, E, Result); |
| 4509 | } |
| 4510 | bool Error(const Expr *E) { |
| 4511 | return IntEval.Error(E); |
| 4512 | } |
| 4513 | bool Error(const Expr *E, diag::kind D) { |
| 4514 | return IntEval.Error(E, D); |
| 4515 | } |
| 4516 | |
| 4517 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { |
| 4518 | return Info.CCEDiag(E, D); |
| 4519 | } |
| 4520 | |
Argyrios Kyrtzidis | 9293fff | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 4521 | // \brief Returns true if visiting the RHS is necessary, false otherwise. |
| 4522 | bool VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E, |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4523 | bool &SuppressRHSDiags); |
| 4524 | |
| 4525 | bool VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult, |
| 4526 | const BinaryOperator *E, APValue &Result); |
| 4527 | |
| 4528 | void EvaluateExpr(const Expr *E, EvalResult &Result) { |
| 4529 | Result.Failed = !Evaluate(Result.Val, Info, E); |
| 4530 | if (Result.Failed) |
| 4531 | Result.Val = APValue(); |
| 4532 | } |
| 4533 | |
Richard Trieu | b778305 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 4534 | void process(EvalResult &Result); |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4535 | |
| 4536 | void enqueue(const Expr *E) { |
| 4537 | E = E->IgnoreParens(); |
| 4538 | Queue.resize(Queue.size()+1); |
| 4539 | Queue.back().E = E; |
| 4540 | Queue.back().Kind = Job::AnyExprKind; |
| 4541 | } |
| 4542 | }; |
| 4543 | |
| 4544 | } |
| 4545 | |
| 4546 | bool DataRecursiveIntBinOpEvaluator:: |
Argyrios Kyrtzidis | 9293fff | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 4547 | VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E, |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4548 | bool &SuppressRHSDiags) { |
| 4549 | if (E->getOpcode() == BO_Comma) { |
| 4550 | // Ignore LHS but note if we could not evaluate it. |
| 4551 | if (LHSResult.Failed) |
| 4552 | Info.EvalStatus.HasSideEffects = true; |
| 4553 | return true; |
| 4554 | } |
| 4555 | |
| 4556 | if (E->isLogicalOp()) { |
| 4557 | bool lhsResult; |
| 4558 | if (HandleConversionToBool(LHSResult.Val, lhsResult)) { |
Argyrios Kyrtzidis | 2fa975c | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 4559 | // We were able to evaluate the LHS, see if we can get away with not |
| 4560 | // evaluating the RHS: 0 && X -> 0, 1 || X -> 1 |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4561 | if (lhsResult == (E->getOpcode() == BO_LOr)) { |
Argyrios Kyrtzidis | 9293fff | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 4562 | Success(lhsResult, E, LHSResult.Val); |
| 4563 | return false; // Ignore RHS |
Argyrios Kyrtzidis | 2fa975c | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 4564 | } |
| 4565 | } else { |
| 4566 | // Since we weren't able to evaluate the left hand side, it |
| 4567 | // must have had side effects. |
| 4568 | Info.EvalStatus.HasSideEffects = true; |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4569 | |
| 4570 | // We can't evaluate the LHS; however, sometimes the result |
| 4571 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
| 4572 | // Don't ignore RHS and suppress diagnostics from this arm. |
| 4573 | SuppressRHSDiags = true; |
| 4574 | } |
| 4575 | |
| 4576 | return true; |
| 4577 | } |
| 4578 | |
| 4579 | assert(E->getLHS()->getType()->isIntegralOrEnumerationType() && |
| 4580 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
| 4581 | |
| 4582 | if (LHSResult.Failed && !Info.keepEvaluatingAfterFailure()) |
Argyrios Kyrtzidis | 9293fff | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 4583 | return false; // Ignore RHS; |
| 4584 | |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4585 | return true; |
| 4586 | } |
Argyrios Kyrtzidis | 2fa975c | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 4587 | |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4588 | bool DataRecursiveIntBinOpEvaluator:: |
| 4589 | VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult, |
| 4590 | const BinaryOperator *E, APValue &Result) { |
| 4591 | if (E->getOpcode() == BO_Comma) { |
| 4592 | if (RHSResult.Failed) |
| 4593 | return false; |
| 4594 | Result = RHSResult.Val; |
| 4595 | return true; |
| 4596 | } |
| 4597 | |
| 4598 | if (E->isLogicalOp()) { |
| 4599 | bool lhsResult, rhsResult; |
| 4600 | bool LHSIsOK = HandleConversionToBool(LHSResult.Val, lhsResult); |
| 4601 | bool RHSIsOK = HandleConversionToBool(RHSResult.Val, rhsResult); |
| 4602 | |
| 4603 | if (LHSIsOK) { |
| 4604 | if (RHSIsOK) { |
| 4605 | if (E->getOpcode() == BO_LOr) |
| 4606 | return Success(lhsResult || rhsResult, E, Result); |
| 4607 | else |
| 4608 | return Success(lhsResult && rhsResult, E, Result); |
| 4609 | } |
| 4610 | } else { |
| 4611 | if (RHSIsOK) { |
Argyrios Kyrtzidis | 2fa975c | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 4612 | // We can't evaluate the LHS; however, sometimes the result |
| 4613 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
| 4614 | if (rhsResult == (E->getOpcode() == BO_LOr)) |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4615 | return Success(rhsResult, E, Result); |
Argyrios Kyrtzidis | 2fa975c | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 4616 | } |
| 4617 | } |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4618 | |
Argyrios Kyrtzidis | 2fa975c | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 4619 | return false; |
| 4620 | } |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4621 | |
| 4622 | assert(E->getLHS()->getType()->isIntegralOrEnumerationType() && |
| 4623 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
| 4624 | |
| 4625 | if (LHSResult.Failed || RHSResult.Failed) |
| 4626 | return false; |
| 4627 | |
| 4628 | const APValue &LHSVal = LHSResult.Val; |
| 4629 | const APValue &RHSVal = RHSResult.Val; |
| 4630 | |
| 4631 | // Handle cases like (unsigned long)&a + 4. |
| 4632 | if (E->isAdditiveOp() && LHSVal.isLValue() && RHSVal.isInt()) { |
| 4633 | Result = LHSVal; |
| 4634 | CharUnits AdditionalOffset = CharUnits::fromQuantity( |
| 4635 | RHSVal.getInt().getZExtValue()); |
| 4636 | if (E->getOpcode() == BO_Add) |
| 4637 | Result.getLValueOffset() += AdditionalOffset; |
| 4638 | else |
| 4639 | Result.getLValueOffset() -= AdditionalOffset; |
| 4640 | return true; |
| 4641 | } |
| 4642 | |
| 4643 | // Handle cases like 4 + (unsigned long)&a |
| 4644 | if (E->getOpcode() == BO_Add && |
| 4645 | RHSVal.isLValue() && LHSVal.isInt()) { |
| 4646 | Result = RHSVal; |
| 4647 | Result.getLValueOffset() += CharUnits::fromQuantity( |
| 4648 | LHSVal.getInt().getZExtValue()); |
| 4649 | return true; |
| 4650 | } |
| 4651 | |
| 4652 | if (E->getOpcode() == BO_Sub && LHSVal.isLValue() && RHSVal.isLValue()) { |
| 4653 | // Handle (intptr_t)&&A - (intptr_t)&&B. |
| 4654 | if (!LHSVal.getLValueOffset().isZero() || |
| 4655 | !RHSVal.getLValueOffset().isZero()) |
| 4656 | return false; |
| 4657 | const Expr *LHSExpr = LHSVal.getLValueBase().dyn_cast<const Expr*>(); |
| 4658 | const Expr *RHSExpr = RHSVal.getLValueBase().dyn_cast<const Expr*>(); |
| 4659 | if (!LHSExpr || !RHSExpr) |
| 4660 | return false; |
| 4661 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); |
| 4662 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); |
| 4663 | if (!LHSAddrExpr || !RHSAddrExpr) |
| 4664 | return false; |
| 4665 | // Make sure both labels come from the same function. |
| 4666 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
| 4667 | RHSAddrExpr->getLabel()->getDeclContext()) |
| 4668 | return false; |
| 4669 | Result = APValue(LHSAddrExpr, RHSAddrExpr); |
| 4670 | return true; |
| 4671 | } |
| 4672 | |
| 4673 | // All the following cases expect both operands to be an integer |
| 4674 | if (!LHSVal.isInt() || !RHSVal.isInt()) |
| 4675 | return Error(E); |
| 4676 | |
| 4677 | const APSInt &LHS = LHSVal.getInt(); |
| 4678 | APSInt RHS = RHSVal.getInt(); |
| 4679 | |
| 4680 | switch (E->getOpcode()) { |
| 4681 | default: |
| 4682 | return Error(E); |
| 4683 | case BO_Mul: |
| 4684 | return Success(CheckedIntArithmetic(Info, E, LHS, RHS, |
| 4685 | LHS.getBitWidth() * 2, |
| 4686 | std::multiplies<APSInt>()), E, |
| 4687 | Result); |
| 4688 | case BO_Add: |
| 4689 | return Success(CheckedIntArithmetic(Info, E, LHS, RHS, |
| 4690 | LHS.getBitWidth() + 1, |
| 4691 | std::plus<APSInt>()), E, Result); |
| 4692 | case BO_Sub: |
| 4693 | return Success(CheckedIntArithmetic(Info, E, LHS, RHS, |
| 4694 | LHS.getBitWidth() + 1, |
| 4695 | std::minus<APSInt>()), E, Result); |
| 4696 | case BO_And: return Success(LHS & RHS, E, Result); |
| 4697 | case BO_Xor: return Success(LHS ^ RHS, E, Result); |
| 4698 | case BO_Or: return Success(LHS | RHS, E, Result); |
| 4699 | case BO_Div: |
| 4700 | case BO_Rem: |
| 4701 | if (RHS == 0) |
| 4702 | return Error(E, diag::note_expr_divide_by_zero); |
| 4703 | // Check for overflow case: INT_MIN / -1 or INT_MIN % -1. The latter is |
| 4704 | // not actually undefined behavior in C++11 due to a language defect. |
| 4705 | if (RHS.isNegative() && RHS.isAllOnesValue() && |
| 4706 | LHS.isSigned() && LHS.isMinSignedValue()) |
| 4707 | HandleOverflow(Info, E, -LHS.extend(LHS.getBitWidth() + 1), E->getType()); |
| 4708 | return Success(E->getOpcode() == BO_Rem ? LHS % RHS : LHS / RHS, E, |
| 4709 | Result); |
| 4710 | case BO_Shl: { |
| 4711 | // During constant-folding, a negative shift is an opposite shift. Such |
| 4712 | // a shift is not a constant expression. |
| 4713 | if (RHS.isSigned() && RHS.isNegative()) { |
| 4714 | CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; |
| 4715 | RHS = -RHS; |
| 4716 | goto shift_right; |
| 4717 | } |
| 4718 | |
| 4719 | shift_left: |
| 4720 | // C++11 [expr.shift]p1: Shift width must be less than the bit width of |
| 4721 | // the shifted type. |
| 4722 | unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
| 4723 | if (SA != RHS) { |
| 4724 | CCEDiag(E, diag::note_constexpr_large_shift) |
| 4725 | << RHS << E->getType() << LHS.getBitWidth(); |
| 4726 | } else if (LHS.isSigned()) { |
| 4727 | // C++11 [expr.shift]p2: A signed left shift must have a non-negative |
| 4728 | // operand, and must not overflow the corresponding unsigned type. |
| 4729 | if (LHS.isNegative()) |
| 4730 | CCEDiag(E, diag::note_constexpr_lshift_of_negative) << LHS; |
| 4731 | else if (LHS.countLeadingZeros() < SA) |
| 4732 | CCEDiag(E, diag::note_constexpr_lshift_discards); |
| 4733 | } |
| 4734 | |
| 4735 | return Success(LHS << SA, E, Result); |
| 4736 | } |
| 4737 | case BO_Shr: { |
| 4738 | // During constant-folding, a negative shift is an opposite shift. Such a |
| 4739 | // shift is not a constant expression. |
| 4740 | if (RHS.isSigned() && RHS.isNegative()) { |
| 4741 | CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; |
| 4742 | RHS = -RHS; |
| 4743 | goto shift_left; |
| 4744 | } |
| 4745 | |
| 4746 | shift_right: |
| 4747 | // C++11 [expr.shift]p1: Shift width must be less than the bit width of the |
| 4748 | // shifted type. |
| 4749 | unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
| 4750 | if (SA != RHS) |
| 4751 | CCEDiag(E, diag::note_constexpr_large_shift) |
| 4752 | << RHS << E->getType() << LHS.getBitWidth(); |
| 4753 | |
| 4754 | return Success(LHS >> SA, E, Result); |
| 4755 | } |
| 4756 | |
| 4757 | case BO_LT: return Success(LHS < RHS, E, Result); |
| 4758 | case BO_GT: return Success(LHS > RHS, E, Result); |
| 4759 | case BO_LE: return Success(LHS <= RHS, E, Result); |
| 4760 | case BO_GE: return Success(LHS >= RHS, E, Result); |
| 4761 | case BO_EQ: return Success(LHS == RHS, E, Result); |
| 4762 | case BO_NE: return Success(LHS != RHS, E, Result); |
| 4763 | } |
| 4764 | } |
| 4765 | |
Richard Trieu | b778305 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 4766 | void DataRecursiveIntBinOpEvaluator::process(EvalResult &Result) { |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4767 | Job &job = Queue.back(); |
| 4768 | |
| 4769 | switch (job.Kind) { |
| 4770 | case Job::AnyExprKind: { |
| 4771 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(job.E)) { |
| 4772 | if (shouldEnqueue(Bop)) { |
| 4773 | job.Kind = Job::BinOpKind; |
| 4774 | enqueue(Bop->getLHS()); |
Richard Trieu | b778305 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 4775 | return; |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4776 | } |
| 4777 | } |
| 4778 | |
| 4779 | EvaluateExpr(job.E, Result); |
| 4780 | Queue.pop_back(); |
Richard Trieu | b778305 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 4781 | return; |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4782 | } |
| 4783 | |
| 4784 | case Job::BinOpKind: { |
| 4785 | const BinaryOperator *Bop = cast<BinaryOperator>(job.E); |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4786 | bool SuppressRHSDiags = false; |
Argyrios Kyrtzidis | 9293fff | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 4787 | if (!VisitBinOpLHSOnly(Result, Bop, SuppressRHSDiags)) { |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4788 | Queue.pop_back(); |
Richard Trieu | b778305 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 4789 | return; |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4790 | } |
| 4791 | if (SuppressRHSDiags) |
| 4792 | job.startSpeculativeEval(Info); |
Argyrios Kyrtzidis | 9293fff | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 4793 | job.LHSResult.swap(Result); |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4794 | job.Kind = Job::BinOpVisitedLHSKind; |
| 4795 | enqueue(Bop->getRHS()); |
Richard Trieu | b778305 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 4796 | return; |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4797 | } |
| 4798 | |
| 4799 | case Job::BinOpVisitedLHSKind: { |
| 4800 | const BinaryOperator *Bop = cast<BinaryOperator>(job.E); |
| 4801 | EvalResult RHS; |
| 4802 | RHS.swap(Result); |
Richard Trieu | b778305 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 4803 | Result.Failed = !VisitBinOp(job.LHSResult, RHS, Bop, Result.Val); |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4804 | Queue.pop_back(); |
Richard Trieu | b778305 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 4805 | return; |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4806 | } |
| 4807 | } |
| 4808 | |
| 4809 | llvm_unreachable("Invalid Job::Kind!"); |
| 4810 | } |
| 4811 | |
| 4812 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
| 4813 | if (E->isAssignmentOp()) |
| 4814 | return Error(E); |
| 4815 | |
| 4816 | if (DataRecursiveIntBinOpEvaluator::shouldEnqueue(E)) |
| 4817 | return DataRecursiveIntBinOpEvaluator(*this, Result).Traverse(E); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 4818 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4819 | QualType LHSTy = E->getLHS()->getType(); |
| 4820 | QualType RHSTy = E->getRHS()->getType(); |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 4821 | |
| 4822 | if (LHSTy->isAnyComplexType()) { |
| 4823 | assert(RHSTy->isAnyComplexType() && "Invalid comparison"); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 4824 | ComplexValue LHS, RHS; |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 4825 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4826 | bool LHSOK = EvaluateComplex(E->getLHS(), LHS, Info); |
| 4827 | if (!LHSOK && !Info.keepEvaluatingAfterFailure()) |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 4828 | return false; |
| 4829 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4830 | if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK) |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 4831 | return false; |
| 4832 | |
| 4833 | if (LHS.isComplexFloat()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4834 | APFloat::cmpResult CR_r = |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 4835 | LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4836 | APFloat::cmpResult CR_i = |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 4837 | LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag()); |
| 4838 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4839 | if (E->getOpcode() == BO_EQ) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4840 | return Success((CR_r == APFloat::cmpEqual && |
| 4841 | CR_i == APFloat::cmpEqual), E); |
| 4842 | else { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4843 | assert(E->getOpcode() == BO_NE && |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4844 | "Invalid complex comparison."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4845 | return Success(((CR_r == APFloat::cmpGreaterThan || |
Mon P Wang | fc39dc4 | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 4846 | CR_r == APFloat::cmpLessThan || |
| 4847 | CR_r == APFloat::cmpUnordered) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4848 | (CR_i == APFloat::cmpGreaterThan || |
Mon P Wang | fc39dc4 | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 4849 | CR_i == APFloat::cmpLessThan || |
| 4850 | CR_i == APFloat::cmpUnordered)), E); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4851 | } |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 4852 | } else { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4853 | if (E->getOpcode() == BO_EQ) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4854 | return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() && |
| 4855 | LHS.getComplexIntImag() == RHS.getComplexIntImag()), E); |
| 4856 | else { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4857 | assert(E->getOpcode() == BO_NE && |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4858 | "Invalid compex comparison."); |
| 4859 | return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() || |
| 4860 | LHS.getComplexIntImag() != RHS.getComplexIntImag()), E); |
| 4861 | } |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 4862 | } |
| 4863 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4864 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4865 | if (LHSTy->isRealFloatingType() && |
| 4866 | RHSTy->isRealFloatingType()) { |
| 4867 | APFloat RHS(0.0), LHS(0.0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4868 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4869 | bool LHSOK = EvaluateFloat(E->getRHS(), RHS, Info); |
| 4870 | if (!LHSOK && !Info.keepEvaluatingAfterFailure()) |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4871 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4872 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4873 | if (!EvaluateFloat(E->getLHS(), LHS, Info) || !LHSOK) |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4874 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4875 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4876 | APFloat::cmpResult CR = LHS.compare(RHS); |
Anders Carlsson | 529569e | 2008-11-16 22:46:56 +0000 | [diff] [blame] | 4877 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4878 | switch (E->getOpcode()) { |
| 4879 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4880 | llvm_unreachable("Invalid binary operator!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4881 | case BO_LT: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4882 | return Success(CR == APFloat::cmpLessThan, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4883 | case BO_GT: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4884 | return Success(CR == APFloat::cmpGreaterThan, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4885 | case BO_LE: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4886 | return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4887 | case BO_GE: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4888 | return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual, |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4889 | E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4890 | case BO_EQ: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4891 | return Success(CR == APFloat::cmpEqual, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4892 | case BO_NE: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4893 | return Success(CR == APFloat::cmpGreaterThan |
Mon P Wang | fc39dc4 | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 4894 | || CR == APFloat::cmpLessThan |
| 4895 | || CR == APFloat::cmpUnordered, E); |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4896 | } |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4897 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4898 | |
Eli Friedman | ad02d7d | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 4899 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 4900 | if (E->getOpcode() == BO_Sub || E->isComparisonOp()) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4901 | LValue LHSValue, RHSValue; |
| 4902 | |
| 4903 | bool LHSOK = EvaluatePointer(E->getLHS(), LHSValue, Info); |
| 4904 | if (!LHSOK && Info.keepEvaluatingAfterFailure()) |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4905 | return false; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 4906 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4907 | if (!EvaluatePointer(E->getRHS(), RHSValue, Info) || !LHSOK) |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4908 | return false; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 4909 | |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 4910 | // Reject differing bases from the normal codepath; we special-case |
| 4911 | // comparisons to null. |
| 4912 | if (!HasSameBase(LHSValue, RHSValue)) { |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 4913 | if (E->getOpcode() == BO_Sub) { |
| 4914 | // Handle &&A - &&B. |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 4915 | if (!LHSValue.Offset.isZero() || !RHSValue.Offset.isZero()) |
| 4916 | return false; |
| 4917 | const Expr *LHSExpr = LHSValue.Base.dyn_cast<const Expr*>(); |
Benjamin Kramer | 7b2f93c | 2012-10-03 14:15:39 +0000 | [diff] [blame] | 4918 | const Expr *RHSExpr = RHSValue.Base.dyn_cast<const Expr*>(); |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 4919 | if (!LHSExpr || !RHSExpr) |
| 4920 | return false; |
| 4921 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); |
| 4922 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); |
| 4923 | if (!LHSAddrExpr || !RHSAddrExpr) |
| 4924 | return false; |
Eli Friedman | 5930a4c | 2012-01-05 23:59:40 +0000 | [diff] [blame] | 4925 | // Make sure both labels come from the same function. |
| 4926 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
| 4927 | RHSAddrExpr->getLabel()->getDeclContext()) |
| 4928 | return false; |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4929 | Result = APValue(LHSAddrExpr, RHSAddrExpr); |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 4930 | return true; |
| 4931 | } |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 4932 | // Inequalities and subtractions between unrelated pointers have |
| 4933 | // unspecified or undefined behavior. |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 4934 | if (!E->isEqualityOp()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4935 | return Error(E); |
Eli Friedman | ffbda40 | 2011-10-31 22:28:05 +0000 | [diff] [blame] | 4936 | // A constant address may compare equal to the address of a symbol. |
| 4937 | // The one exception is that address of an object cannot compare equal |
Eli Friedman | c45061b | 2011-10-31 22:54:30 +0000 | [diff] [blame] | 4938 | // to a null pointer constant. |
Eli Friedman | ffbda40 | 2011-10-31 22:28:05 +0000 | [diff] [blame] | 4939 | if ((!LHSValue.Base && !LHSValue.Offset.isZero()) || |
| 4940 | (!RHSValue.Base && !RHSValue.Offset.isZero())) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4941 | return Error(E); |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 4942 | // It's implementation-defined whether distinct literals will have |
Richard Smith | b02e462 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 4943 | // distinct addresses. In clang, the result of such a comparison is |
| 4944 | // unspecified, so it is not a constant expression. However, we do know |
| 4945 | // that the address of a literal will be non-null. |
Richard Smith | 74f4634 | 2011-11-04 01:10:57 +0000 | [diff] [blame] | 4946 | if ((IsLiteralLValue(LHSValue) || IsLiteralLValue(RHSValue)) && |
| 4947 | LHSValue.Base && RHSValue.Base) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4948 | return Error(E); |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 4949 | // We can't tell whether weak symbols will end up pointing to the same |
| 4950 | // object. |
| 4951 | if (IsWeakLValue(LHSValue) || IsWeakLValue(RHSValue)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4952 | return Error(E); |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 4953 | // Pointers with different bases cannot represent the same object. |
Eli Friedman | c45061b | 2011-10-31 22:54:30 +0000 | [diff] [blame] | 4954 | // (Note that clang defaults to -fmerge-all-constants, which can |
| 4955 | // lead to inconsistent results for comparisons involving the address |
| 4956 | // of a constant; this generally doesn't matter in practice.) |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 4957 | return Success(E->getOpcode() == BO_NE, E); |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 4958 | } |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 4959 | |
Richard Smith | 15efc4d | 2012-02-01 08:10:20 +0000 | [diff] [blame] | 4960 | const CharUnits &LHSOffset = LHSValue.getLValueOffset(); |
| 4961 | const CharUnits &RHSOffset = RHSValue.getLValueOffset(); |
| 4962 | |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 4963 | SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator(); |
| 4964 | SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator(); |
| 4965 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4966 | if (E->getOpcode() == BO_Sub) { |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 4967 | // C++11 [expr.add]p6: |
| 4968 | // Unless both pointers point to elements of the same array object, or |
| 4969 | // one past the last element of the array object, the behavior is |
| 4970 | // undefined. |
| 4971 | if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && |
| 4972 | !AreElementsOfSameArray(getType(LHSValue.Base), |
| 4973 | LHSDesignator, RHSDesignator)) |
| 4974 | CCEDiag(E, diag::note_constexpr_pointer_subtraction_not_same_array); |
| 4975 | |
Chris Lattner | 4992bdd | 2010-04-20 17:13:14 +0000 | [diff] [blame] | 4976 | QualType Type = E->getLHS()->getType(); |
| 4977 | QualType ElementType = Type->getAs<PointerType>()->getPointeeType(); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4978 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4979 | CharUnits ElementSize; |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 4980 | if (!HandleSizeof(Info, E->getExprLoc(), ElementType, ElementSize)) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4981 | return false; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 4982 | |
Richard Smith | 15efc4d | 2012-02-01 08:10:20 +0000 | [diff] [blame] | 4983 | // FIXME: LLVM and GCC both compute LHSOffset - RHSOffset at runtime, |
| 4984 | // and produce incorrect results when it overflows. Such behavior |
| 4985 | // appears to be non-conforming, but is common, so perhaps we should |
| 4986 | // assume the standard intended for such cases to be undefined behavior |
| 4987 | // and check for them. |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 4988 | |
Richard Smith | 15efc4d | 2012-02-01 08:10:20 +0000 | [diff] [blame] | 4989 | // Compute (LHSOffset - RHSOffset) / Size carefully, checking for |
| 4990 | // overflow in the final conversion to ptrdiff_t. |
| 4991 | APSInt LHS( |
| 4992 | llvm::APInt(65, (int64_t)LHSOffset.getQuantity(), true), false); |
| 4993 | APSInt RHS( |
| 4994 | llvm::APInt(65, (int64_t)RHSOffset.getQuantity(), true), false); |
| 4995 | APSInt ElemSize( |
| 4996 | llvm::APInt(65, (int64_t)ElementSize.getQuantity(), true), false); |
| 4997 | APSInt TrueResult = (LHS - RHS) / ElemSize; |
| 4998 | APSInt Result = TrueResult.trunc(Info.Ctx.getIntWidth(E->getType())); |
| 4999 | |
| 5000 | if (Result.extend(65) != TrueResult) |
| 5001 | HandleOverflow(Info, E, TrueResult, E->getType()); |
| 5002 | return Success(Result, E); |
| 5003 | } |
Richard Smith | 82f2858 | 2012-01-31 06:41:30 +0000 | [diff] [blame] | 5004 | |
| 5005 | // C++11 [expr.rel]p3: |
| 5006 | // Pointers to void (after pointer conversions) can be compared, with a |
| 5007 | // result defined as follows: If both pointers represent the same |
| 5008 | // address or are both the null pointer value, the result is true if the |
| 5009 | // operator is <= or >= and false otherwise; otherwise the result is |
| 5010 | // unspecified. |
| 5011 | // We interpret this as applying to pointers to *cv* void. |
| 5012 | if (LHSTy->isVoidPointerType() && LHSOffset != RHSOffset && |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 5013 | E->isRelationalOp()) |
Richard Smith | 82f2858 | 2012-01-31 06:41:30 +0000 | [diff] [blame] | 5014 | CCEDiag(E, diag::note_constexpr_void_comparison); |
| 5015 | |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 5016 | // C++11 [expr.rel]p2: |
| 5017 | // - If two pointers point to non-static data members of the same object, |
| 5018 | // or to subobjects or array elements fo such members, recursively, the |
| 5019 | // pointer to the later declared member compares greater provided the |
| 5020 | // two members have the same access control and provided their class is |
| 5021 | // not a union. |
| 5022 | // [...] |
| 5023 | // - Otherwise pointer comparisons are unspecified. |
| 5024 | if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && |
| 5025 | E->isRelationalOp()) { |
| 5026 | bool WasArrayIndex; |
| 5027 | unsigned Mismatch = |
| 5028 | FindDesignatorMismatch(getType(LHSValue.Base), LHSDesignator, |
| 5029 | RHSDesignator, WasArrayIndex); |
| 5030 | // At the point where the designators diverge, the comparison has a |
| 5031 | // specified value if: |
| 5032 | // - we are comparing array indices |
| 5033 | // - we are comparing fields of a union, or fields with the same access |
| 5034 | // Otherwise, the result is unspecified and thus the comparison is not a |
| 5035 | // constant expression. |
| 5036 | if (!WasArrayIndex && Mismatch < LHSDesignator.Entries.size() && |
| 5037 | Mismatch < RHSDesignator.Entries.size()) { |
| 5038 | const FieldDecl *LF = getAsField(LHSDesignator.Entries[Mismatch]); |
| 5039 | const FieldDecl *RF = getAsField(RHSDesignator.Entries[Mismatch]); |
| 5040 | if (!LF && !RF) |
| 5041 | CCEDiag(E, diag::note_constexpr_pointer_comparison_base_classes); |
| 5042 | else if (!LF) |
| 5043 | CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) |
| 5044 | << getAsBaseClass(LHSDesignator.Entries[Mismatch]) |
| 5045 | << RF->getParent() << RF; |
| 5046 | else if (!RF) |
| 5047 | CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) |
| 5048 | << getAsBaseClass(RHSDesignator.Entries[Mismatch]) |
| 5049 | << LF->getParent() << LF; |
| 5050 | else if (!LF->getParent()->isUnion() && |
| 5051 | LF->getAccess() != RF->getAccess()) |
| 5052 | CCEDiag(E, diag::note_constexpr_pointer_comparison_differing_access) |
| 5053 | << LF << LF->getAccess() << RF << RF->getAccess() |
| 5054 | << LF->getParent(); |
| 5055 | } |
| 5056 | } |
| 5057 | |
Eli Friedman | a316988 | 2012-04-16 04:30:08 +0000 | [diff] [blame] | 5058 | // The comparison here must be unsigned, and performed with the same |
| 5059 | // width as the pointer. |
Eli Friedman | a316988 | 2012-04-16 04:30:08 +0000 | [diff] [blame] | 5060 | unsigned PtrSize = Info.Ctx.getTypeSize(LHSTy); |
| 5061 | uint64_t CompareLHS = LHSOffset.getQuantity(); |
| 5062 | uint64_t CompareRHS = RHSOffset.getQuantity(); |
| 5063 | assert(PtrSize <= 64 && "Unexpected pointer width"); |
| 5064 | uint64_t Mask = ~0ULL >> (64 - PtrSize); |
| 5065 | CompareLHS &= Mask; |
| 5066 | CompareRHS &= Mask; |
| 5067 | |
Eli Friedman | 2850376 | 2012-04-16 19:23:57 +0000 | [diff] [blame] | 5068 | // If there is a base and this is a relational operator, we can only |
| 5069 | // compare pointers within the object in question; otherwise, the result |
| 5070 | // depends on where the object is located in memory. |
| 5071 | if (!LHSValue.Base.isNull() && E->isRelationalOp()) { |
| 5072 | QualType BaseTy = getType(LHSValue.Base); |
| 5073 | if (BaseTy->isIncompleteType()) |
| 5074 | return Error(E); |
| 5075 | CharUnits Size = Info.Ctx.getTypeSizeInChars(BaseTy); |
| 5076 | uint64_t OffsetLimit = Size.getQuantity(); |
| 5077 | if (CompareLHS > OffsetLimit || CompareRHS > OffsetLimit) |
| 5078 | return Error(E); |
| 5079 | } |
| 5080 | |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 5081 | switch (E->getOpcode()) { |
| 5082 | default: llvm_unreachable("missing comparison operator"); |
Eli Friedman | a316988 | 2012-04-16 04:30:08 +0000 | [diff] [blame] | 5083 | case BO_LT: return Success(CompareLHS < CompareRHS, E); |
| 5084 | case BO_GT: return Success(CompareLHS > CompareRHS, E); |
| 5085 | case BO_LE: return Success(CompareLHS <= CompareRHS, E); |
| 5086 | case BO_GE: return Success(CompareLHS >= CompareRHS, E); |
| 5087 | case BO_EQ: return Success(CompareLHS == CompareRHS, E); |
| 5088 | case BO_NE: return Success(CompareLHS != CompareRHS, E); |
Eli Friedman | ad02d7d | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 5089 | } |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 5090 | } |
| 5091 | } |
Richard Smith | b02e462 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 5092 | |
| 5093 | if (LHSTy->isMemberPointerType()) { |
| 5094 | assert(E->isEqualityOp() && "unexpected member pointer operation"); |
| 5095 | assert(RHSTy->isMemberPointerType() && "invalid comparison"); |
| 5096 | |
| 5097 | MemberPtr LHSValue, RHSValue; |
| 5098 | |
| 5099 | bool LHSOK = EvaluateMemberPointer(E->getLHS(), LHSValue, Info); |
| 5100 | if (!LHSOK && Info.keepEvaluatingAfterFailure()) |
| 5101 | return false; |
| 5102 | |
| 5103 | if (!EvaluateMemberPointer(E->getRHS(), RHSValue, Info) || !LHSOK) |
| 5104 | return false; |
| 5105 | |
| 5106 | // C++11 [expr.eq]p2: |
| 5107 | // If both operands are null, they compare equal. Otherwise if only one is |
| 5108 | // null, they compare unequal. |
| 5109 | if (!LHSValue.getDecl() || !RHSValue.getDecl()) { |
| 5110 | bool Equal = !LHSValue.getDecl() && !RHSValue.getDecl(); |
| 5111 | return Success(E->getOpcode() == BO_EQ ? Equal : !Equal, E); |
| 5112 | } |
| 5113 | |
| 5114 | // Otherwise if either is a pointer to a virtual member function, the |
| 5115 | // result is unspecified. |
| 5116 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(LHSValue.getDecl())) |
| 5117 | if (MD->isVirtual()) |
| 5118 | CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; |
| 5119 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(RHSValue.getDecl())) |
| 5120 | if (MD->isVirtual()) |
| 5121 | CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; |
| 5122 | |
| 5123 | // Otherwise they compare equal if and only if they would refer to the |
| 5124 | // same member of the same most derived object or the same subobject if |
| 5125 | // they were dereferenced with a hypothetical object of the associated |
| 5126 | // class type. |
| 5127 | bool Equal = LHSValue == RHSValue; |
| 5128 | return Success(E->getOpcode() == BO_EQ ? Equal : !Equal, E); |
| 5129 | } |
| 5130 | |
Richard Smith | 26f2cac | 2012-02-14 22:35:28 +0000 | [diff] [blame] | 5131 | if (LHSTy->isNullPtrType()) { |
| 5132 | assert(E->isComparisonOp() && "unexpected nullptr operation"); |
| 5133 | assert(RHSTy->isNullPtrType() && "missing pointer conversion"); |
| 5134 | // C++11 [expr.rel]p4, [expr.eq]p3: If two operands of type std::nullptr_t |
| 5135 | // are compared, the result is true of the operator is <=, >= or ==, and |
| 5136 | // false otherwise. |
| 5137 | BinaryOperator::Opcode Opcode = E->getOpcode(); |
| 5138 | return Success(Opcode == BO_EQ || Opcode == BO_LE || Opcode == BO_GE, E); |
| 5139 | } |
| 5140 | |
Argyrios Kyrtzidis | cc2f77a | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5141 | assert((!LHSTy->isIntegralOrEnumerationType() || |
| 5142 | !RHSTy->isIntegralOrEnumerationType()) && |
| 5143 | "DataRecursiveIntBinOpEvaluator should have handled integral types"); |
| 5144 | // We can't continue from here for non-integral types. |
| 5145 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 5146 | } |
| 5147 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 5148 | CharUnits IntExprEvaluator::GetAlignOfType(QualType T) { |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 5149 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 5150 | // result shall be the alignment of the referenced type." |
| 5151 | if (const ReferenceType *Ref = T->getAs<ReferenceType>()) |
| 5152 | T = Ref->getPointeeType(); |
Chad Rosier | 9f1210c | 2011-07-26 07:03:04 +0000 | [diff] [blame] | 5153 | |
| 5154 | // __alignof is defined to return the preferred alignment. |
| 5155 | return Info.Ctx.toCharUnitsFromBits( |
| 5156 | Info.Ctx.getPreferredTypeAlign(T.getTypePtr())); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 5157 | } |
| 5158 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 5159 | CharUnits IntExprEvaluator::GetAlignOfExpr(const Expr *E) { |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 5160 | E = E->IgnoreParens(); |
| 5161 | |
| 5162 | // 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] | 5163 | // to 1 in those cases. |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 5164 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 5165 | return Info.Ctx.getDeclAlign(DRE->getDecl(), |
| 5166 | /*RefAsPointee*/true); |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 5167 | |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 5168 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 5169 | return Info.Ctx.getDeclAlign(ME->getMemberDecl(), |
| 5170 | /*RefAsPointee*/true); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 5171 | |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 5172 | return GetAlignOfType(E->getType()); |
| 5173 | } |
| 5174 | |
| 5175 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5176 | /// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with |
| 5177 | /// a result as the expression's type. |
| 5178 | bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr( |
| 5179 | const UnaryExprOrTypeTraitExpr *E) { |
| 5180 | switch(E->getKind()) { |
| 5181 | case UETT_AlignOf: { |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 5182 | if (E->isArgumentType()) |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 5183 | return Success(GetAlignOfType(E->getArgumentType()), E); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 5184 | else |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 5185 | return Success(GetAlignOfExpr(E->getArgumentExpr()), E); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 5186 | } |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 5187 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5188 | case UETT_VecStep: { |
| 5189 | QualType Ty = E->getTypeOfArgument(); |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 5190 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5191 | if (Ty->isVectorType()) { |
Ted Kremenek | 890f0f1 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 5192 | unsigned n = Ty->castAs<VectorType>()->getNumElements(); |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 5193 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5194 | // The vec_step built-in functions that take a 3-component |
| 5195 | // vector return 4. (OpenCL 1.1 spec 6.11.12) |
| 5196 | if (n == 3) |
| 5197 | n = 4; |
Eli Friedman | f2da9df | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 5198 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5199 | return Success(n, E); |
| 5200 | } else |
| 5201 | return Success(1, E); |
| 5202 | } |
| 5203 | |
| 5204 | case UETT_SizeOf: { |
| 5205 | QualType SrcTy = E->getTypeOfArgument(); |
| 5206 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 5207 | // the result is the size of the referenced type." |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5208 | if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>()) |
| 5209 | SrcTy = Ref->getPointeeType(); |
| 5210 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5211 | CharUnits Sizeof; |
Richard Smith | 74e1ad9 | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 5212 | if (!HandleSizeof(Info, E->getExprLoc(), SrcTy, Sizeof)) |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5213 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5214 | return Success(Sizeof, E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5215 | } |
| 5216 | } |
| 5217 | |
| 5218 | llvm_unreachable("unknown expr/type trait"); |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 5219 | } |
| 5220 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5221 | bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) { |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 5222 | CharUnits Result; |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5223 | unsigned n = OOE->getNumComponents(); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 5224 | if (n == 0) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5225 | return Error(OOE); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5226 | QualType CurrentType = OOE->getTypeSourceInfo()->getType(); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 5227 | for (unsigned i = 0; i != n; ++i) { |
| 5228 | OffsetOfExpr::OffsetOfNode ON = OOE->getComponent(i); |
| 5229 | switch (ON.getKind()) { |
| 5230 | case OffsetOfExpr::OffsetOfNode::Array: { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5231 | const Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex()); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 5232 | APSInt IdxResult; |
| 5233 | if (!EvaluateInteger(Idx, IdxResult, Info)) |
| 5234 | return false; |
| 5235 | const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType); |
| 5236 | if (!AT) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5237 | return Error(OOE); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 5238 | CurrentType = AT->getElementType(); |
| 5239 | CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType); |
| 5240 | Result += IdxResult.getSExtValue() * ElementSize; |
| 5241 | break; |
| 5242 | } |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5243 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 5244 | case OffsetOfExpr::OffsetOfNode::Field: { |
| 5245 | FieldDecl *MemberDecl = ON.getField(); |
| 5246 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5247 | if (!RT) |
| 5248 | return Error(OOE); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 5249 | RecordDecl *RD = RT->getDecl(); |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 5250 | if (RD->isInvalidDecl()) return false; |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 5251 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 5252 | unsigned i = MemberDecl->getFieldIndex(); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 5253 | assert(i < RL.getFieldCount() && "offsetof field in wrong type"); |
Ken Dyck | fb1e3bc | 2011-01-18 01:56:16 +0000 | [diff] [blame] | 5254 | Result += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i)); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 5255 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 5256 | break; |
| 5257 | } |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5258 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 5259 | case OffsetOfExpr::OffsetOfNode::Identifier: |
| 5260 | llvm_unreachable("dependent __builtin_offsetof"); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5261 | |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 5262 | case OffsetOfExpr::OffsetOfNode::Base: { |
| 5263 | CXXBaseSpecifier *BaseSpec = ON.getBase(); |
| 5264 | if (BaseSpec->isVirtual()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5265 | return Error(OOE); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 5266 | |
| 5267 | // Find the layout of the class whose base we are looking into. |
| 5268 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5269 | if (!RT) |
| 5270 | return Error(OOE); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 5271 | RecordDecl *RD = RT->getDecl(); |
John McCall | 8d59dee | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 5272 | if (RD->isInvalidDecl()) return false; |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 5273 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
| 5274 | |
| 5275 | // Find the base class itself. |
| 5276 | CurrentType = BaseSpec->getType(); |
| 5277 | const RecordType *BaseRT = CurrentType->getAs<RecordType>(); |
| 5278 | if (!BaseRT) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5279 | return Error(OOE); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 5280 | |
| 5281 | // Add the offset to the base. |
Ken Dyck | 7c7f820 | 2011-01-26 02:17:08 +0000 | [diff] [blame] | 5282 | Result += RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl())); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 5283 | break; |
| 5284 | } |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 5285 | } |
| 5286 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5287 | return Success(Result, OOE); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 5288 | } |
| 5289 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 5290 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5291 | switch (E->getOpcode()) { |
| 5292 | default: |
| 5293 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 5294 | // See C99 6.6p3. |
| 5295 | return Error(E); |
| 5296 | case UO_Extension: |
| 5297 | // FIXME: Should extension allow i-c-e extension expressions in its scope? |
| 5298 | // If so, we could clear the diagnostic ID. |
| 5299 | return Visit(E->getSubExpr()); |
| 5300 | case UO_Plus: |
| 5301 | // The result is just the value. |
| 5302 | return Visit(E->getSubExpr()); |
| 5303 | case UO_Minus: { |
| 5304 | if (!Visit(E->getSubExpr())) |
| 5305 | return false; |
| 5306 | if (!Result.isInt()) return Error(E); |
Richard Smith | 789f9b6 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 5307 | const APSInt &Value = Result.getInt(); |
| 5308 | if (Value.isSigned() && Value.isMinSignedValue()) |
| 5309 | HandleOverflow(Info, E, -Value.extend(Value.getBitWidth() + 1), |
| 5310 | E->getType()); |
| 5311 | return Success(-Value, E); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5312 | } |
| 5313 | case UO_Not: { |
| 5314 | if (!Visit(E->getSubExpr())) |
| 5315 | return false; |
| 5316 | if (!Result.isInt()) return Error(E); |
| 5317 | return Success(~Result.getInt(), E); |
| 5318 | } |
| 5319 | case UO_LNot: { |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 5320 | bool bres; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5321 | if (!EvaluateAsBooleanCondition(E->getSubExpr(), bres, Info)) |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 5322 | return false; |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5323 | return Success(!bres, E); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 5324 | } |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 5325 | } |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 5326 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5327 | |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 5328 | /// HandleCast - This is used to evaluate implicit or explicit casts where the |
| 5329 | /// result type is integer. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5330 | bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 5331 | const Expr *SubExpr = E->getSubExpr(); |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 5332 | QualType DestType = E->getType(); |
Daniel Dunbar | b92dac8 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 5333 | QualType SrcType = SubExpr->getType(); |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 5334 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5335 | switch (E->getCastKind()) { |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5336 | case CK_BaseToDerived: |
| 5337 | case CK_DerivedToBase: |
| 5338 | case CK_UncheckedDerivedToBase: |
| 5339 | case CK_Dynamic: |
| 5340 | case CK_ToUnion: |
| 5341 | case CK_ArrayToPointerDecay: |
| 5342 | case CK_FunctionToPointerDecay: |
| 5343 | case CK_NullToPointer: |
| 5344 | case CK_NullToMemberPointer: |
| 5345 | case CK_BaseToDerivedMemberPointer: |
| 5346 | case CK_DerivedToBaseMemberPointer: |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 5347 | case CK_ReinterpretMemberPointer: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5348 | case CK_ConstructorConversion: |
| 5349 | case CK_IntegralToPointer: |
| 5350 | case CK_ToVoid: |
| 5351 | case CK_VectorSplat: |
| 5352 | case CK_IntegralToFloating: |
| 5353 | case CK_FloatingCast: |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5354 | case CK_CPointerToObjCPointerCast: |
| 5355 | case CK_BlockPointerToObjCPointerCast: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5356 | case CK_AnyPointerToBlockPointerCast: |
| 5357 | case CK_ObjCObjectLValueCast: |
| 5358 | case CK_FloatingRealToComplex: |
| 5359 | case CK_FloatingComplexToReal: |
| 5360 | case CK_FloatingComplexCast: |
| 5361 | case CK_FloatingComplexToIntegralComplex: |
| 5362 | case CK_IntegralRealToComplex: |
| 5363 | case CK_IntegralComplexCast: |
| 5364 | case CK_IntegralComplexToFloatingComplex: |
Eli Friedman | a6c66ce | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 5365 | case CK_BuiltinFnToFnPtr: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5366 | llvm_unreachable("invalid cast kind for integral value"); |
| 5367 | |
Eli Friedman | e50c297 | 2011-03-25 19:07:11 +0000 | [diff] [blame] | 5368 | case CK_BitCast: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5369 | case CK_Dependent: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5370 | case CK_LValueBitCast: |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 5371 | case CK_ARCProduceObject: |
| 5372 | case CK_ARCConsumeObject: |
| 5373 | case CK_ARCReclaimReturnedObject: |
| 5374 | case CK_ARCExtendBlockObject: |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 5375 | case CK_CopyAndAutoreleaseBlockObject: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5376 | return Error(E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5377 | |
Richard Smith | 7d580a4 | 2012-01-17 21:17:26 +0000 | [diff] [blame] | 5378 | case CK_UserDefinedConversion: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5379 | case CK_LValueToRValue: |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 5380 | case CK_AtomicToNonAtomic: |
| 5381 | case CK_NonAtomicToAtomic: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5382 | case CK_NoOp: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5383 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5384 | |
| 5385 | case CK_MemberPointerToBoolean: |
| 5386 | case CK_PointerToBoolean: |
| 5387 | case CK_IntegralToBoolean: |
| 5388 | case CK_FloatingToBoolean: |
| 5389 | case CK_FloatingComplexToBoolean: |
| 5390 | case CK_IntegralComplexToBoolean: { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5391 | bool BoolResult; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5392 | if (!EvaluateAsBooleanCondition(SubExpr, BoolResult, Info)) |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5393 | return false; |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5394 | return Success(BoolResult, E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5395 | } |
| 5396 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5397 | case CK_IntegralCast: { |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 5398 | if (!Visit(SubExpr)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 5399 | return false; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 5400 | |
Eli Friedman | be26570 | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 5401 | if (!Result.isInt()) { |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 5402 | // Allow casts of address-of-label differences if they are no-ops |
| 5403 | // or narrowing. (The narrowing case isn't actually guaranteed to |
| 5404 | // be constant-evaluatable except in some narrow cases which are hard |
| 5405 | // to detect here. We let it through on the assumption the user knows |
| 5406 | // what they are doing.) |
| 5407 | if (Result.isAddrLabelDiff()) |
| 5408 | return Info.Ctx.getTypeSize(DestType) <= Info.Ctx.getTypeSize(SrcType); |
Eli Friedman | be26570 | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 5409 | // Only allow casts of lvalues if they are lossless. |
| 5410 | return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType); |
| 5411 | } |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 5412 | |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 5413 | return Success(HandleIntToIntCast(Info, E, DestType, SrcType, |
| 5414 | Result.getInt()), E); |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 5415 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5416 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5417 | case CK_PointerToIntegral: { |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 5418 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 5419 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5420 | LValue LV; |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 5421 | if (!EvaluatePointer(SubExpr, LV, Info)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 5422 | return false; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5423 | |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 5424 | if (LV.getLValueBase()) { |
| 5425 | // Only allow based lvalue casts if they are lossless. |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 5426 | // FIXME: Allow a larger integer size than the pointer size, and allow |
| 5427 | // narrowing back down to pointer width in subsequent integral casts. |
| 5428 | // FIXME: Check integer type's active bits, not its type size. |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 5429 | if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5430 | return Error(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5431 | |
Richard Smith | b755a9d | 2011-11-16 07:18:12 +0000 | [diff] [blame] | 5432 | LV.Designator.setInvalid(); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5433 | LV.moveInto(Result); |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 5434 | return true; |
| 5435 | } |
| 5436 | |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 5437 | APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(), |
| 5438 | SrcType); |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 5439 | return Success(HandleIntToIntCast(Info, E, DestType, SrcType, AsInt), E); |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 5440 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5441 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5442 | case CK_IntegralComplexToReal: { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5443 | ComplexValue C; |
Eli Friedman | 1725f68 | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 5444 | if (!EvaluateComplex(SubExpr, C, Info)) |
| 5445 | return false; |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5446 | return Success(C.getComplexIntReal(), E); |
Eli Friedman | 1725f68 | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 5447 | } |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 5448 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5449 | case CK_FloatingToIntegral: { |
| 5450 | APFloat F(0.0); |
| 5451 | if (!EvaluateFloat(SubExpr, F, Info)) |
| 5452 | return false; |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 5453 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5454 | APSInt Value; |
| 5455 | if (!HandleFloatToIntCast(Info, E, SrcType, F, DestType, Value)) |
| 5456 | return false; |
| 5457 | return Success(Value, E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5458 | } |
| 5459 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5460 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5461 | llvm_unreachable("unknown cast resulting in integral value"); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 5462 | } |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 5463 | |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 5464 | bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
| 5465 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5466 | ComplexValue LV; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5467 | if (!EvaluateComplex(E->getSubExpr(), LV, Info)) |
| 5468 | return false; |
| 5469 | if (!LV.isComplexInt()) |
| 5470 | return Error(E); |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 5471 | return Success(LV.getComplexIntReal(), E); |
| 5472 | } |
| 5473 | |
| 5474 | return Visit(E->getSubExpr()); |
| 5475 | } |
| 5476 | |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 5477 | bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 5478 | if (E->getSubExpr()->getType()->isComplexIntegerType()) { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5479 | ComplexValue LV; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5480 | if (!EvaluateComplex(E->getSubExpr(), LV, Info)) |
| 5481 | return false; |
| 5482 | if (!LV.isComplexInt()) |
| 5483 | return Error(E); |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 5484 | return Success(LV.getComplexIntImag(), E); |
| 5485 | } |
| 5486 | |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 5487 | VisitIgnoredValue(E->getSubExpr()); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 5488 | return Success(0, E); |
| 5489 | } |
| 5490 | |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 5491 | bool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) { |
| 5492 | return Success(E->getPackLength(), E); |
| 5493 | } |
| 5494 | |
Sebastian Redl | 295995c | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 5495 | bool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) { |
| 5496 | return Success(E->getValue(), E); |
| 5497 | } |
| 5498 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5499 | //===----------------------------------------------------------------------===// |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5500 | // Float Evaluation |
| 5501 | //===----------------------------------------------------------------------===// |
| 5502 | |
| 5503 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 5504 | class FloatExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5505 | : public ExprEvaluatorBase<FloatExprEvaluator, bool> { |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5506 | APFloat &Result; |
| 5507 | public: |
| 5508 | FloatExprEvaluator(EvalInfo &info, APFloat &result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5509 | : ExprEvaluatorBaseTy(info), Result(result) {} |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5510 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5511 | bool Success(const APValue &V, const Expr *e) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5512 | Result = V.getFloat(); |
| 5513 | return true; |
| 5514 | } |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5515 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5516 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 5517 | Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType())); |
| 5518 | return true; |
| 5519 | } |
| 5520 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 5521 | bool VisitCallExpr(const CallExpr *E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5522 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 5523 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5524 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 5525 | bool VisitFloatingLiteral(const FloatingLiteral *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5526 | bool VisitCastExpr(const CastExpr *E); |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 5527 | |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 5528 | bool VisitUnaryReal(const UnaryOperator *E); |
| 5529 | bool VisitUnaryImag(const UnaryOperator *E); |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 5530 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5531 | // FIXME: Missing: array subscript of vector, member of vector |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5532 | }; |
| 5533 | } // end anonymous namespace |
| 5534 | |
| 5535 | static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5536 | assert(E->isRValue() && E->getType()->isRealFloatingType()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5537 | return FloatExprEvaluator(Info, Result).Visit(E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5538 | } |
| 5539 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5540 | static bool TryEvaluateBuiltinNaN(const ASTContext &Context, |
John McCall | db7b72a | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 5541 | QualType ResultTy, |
| 5542 | const Expr *Arg, |
| 5543 | bool SNaN, |
| 5544 | llvm::APFloat &Result) { |
| 5545 | const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts()); |
| 5546 | if (!S) return false; |
| 5547 | |
| 5548 | const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy); |
| 5549 | |
| 5550 | llvm::APInt fill; |
| 5551 | |
| 5552 | // Treat empty strings as if they were zero. |
| 5553 | if (S->getString().empty()) |
| 5554 | fill = llvm::APInt(32, 0); |
| 5555 | else if (S->getString().getAsInteger(0, fill)) |
| 5556 | return false; |
| 5557 | |
| 5558 | if (SNaN) |
| 5559 | Result = llvm::APFloat::getSNaN(Sem, false, &fill); |
| 5560 | else |
| 5561 | Result = llvm::APFloat::getQNaN(Sem, false, &fill); |
| 5562 | return true; |
| 5563 | } |
| 5564 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 5565 | bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5566 | switch (E->isBuiltinCall()) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5567 | default: |
| 5568 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
| 5569 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 5570 | case Builtin::BI__builtin_huge_val: |
| 5571 | case Builtin::BI__builtin_huge_valf: |
| 5572 | case Builtin::BI__builtin_huge_vall: |
| 5573 | case Builtin::BI__builtin_inf: |
| 5574 | case Builtin::BI__builtin_inff: |
Daniel Dunbar | 7cbed03 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 5575 | case Builtin::BI__builtin_infl: { |
| 5576 | const llvm::fltSemantics &Sem = |
| 5577 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Chris Lattner | 34a74ab | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 5578 | Result = llvm::APFloat::getInf(Sem); |
| 5579 | return true; |
Daniel Dunbar | 7cbed03 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 5580 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5581 | |
John McCall | db7b72a | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 5582 | case Builtin::BI__builtin_nans: |
| 5583 | case Builtin::BI__builtin_nansf: |
| 5584 | case Builtin::BI__builtin_nansl: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5585 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 5586 | true, Result)) |
| 5587 | return Error(E); |
| 5588 | return true; |
John McCall | db7b72a | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 5589 | |
Chris Lattner | 9e62171 | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 5590 | case Builtin::BI__builtin_nan: |
| 5591 | case Builtin::BI__builtin_nanf: |
| 5592 | case Builtin::BI__builtin_nanl: |
Mike Stump | 4572bab | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 5593 | // If this is __builtin_nan() turn this into a nan, otherwise we |
Chris Lattner | 9e62171 | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 5594 | // can't constant fold it. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5595 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 5596 | false, Result)) |
| 5597 | return Error(E); |
| 5598 | return true; |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 5599 | |
| 5600 | case Builtin::BI__builtin_fabs: |
| 5601 | case Builtin::BI__builtin_fabsf: |
| 5602 | case Builtin::BI__builtin_fabsl: |
| 5603 | if (!EvaluateFloat(E->getArg(0), Result, Info)) |
| 5604 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5605 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 5606 | if (Result.isNegative()) |
| 5607 | Result.changeSign(); |
| 5608 | return true; |
| 5609 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5610 | case Builtin::BI__builtin_copysign: |
| 5611 | case Builtin::BI__builtin_copysignf: |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 5612 | case Builtin::BI__builtin_copysignl: { |
| 5613 | APFloat RHS(0.); |
| 5614 | if (!EvaluateFloat(E->getArg(0), Result, Info) || |
| 5615 | !EvaluateFloat(E->getArg(1), RHS, Info)) |
| 5616 | return false; |
| 5617 | Result.copySign(RHS); |
| 5618 | return true; |
| 5619 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 5620 | } |
| 5621 | } |
| 5622 | |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 5623 | bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
Eli Friedman | 43efa31 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 5624 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 5625 | ComplexValue CV; |
| 5626 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
| 5627 | return false; |
| 5628 | Result = CV.FloatReal; |
| 5629 | return true; |
| 5630 | } |
| 5631 | |
| 5632 | return Visit(E->getSubExpr()); |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 5633 | } |
| 5634 | |
| 5635 | bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | 43efa31 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 5636 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 5637 | ComplexValue CV; |
| 5638 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
| 5639 | return false; |
| 5640 | Result = CV.FloatImag; |
| 5641 | return true; |
| 5642 | } |
| 5643 | |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 5644 | VisitIgnoredValue(E->getSubExpr()); |
Eli Friedman | 43efa31 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 5645 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(E->getType()); |
| 5646 | Result = llvm::APFloat::getZero(Sem); |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 5647 | return true; |
| 5648 | } |
| 5649 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 5650 | bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 5651 | switch (E->getOpcode()) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5652 | default: return Error(E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5653 | case UO_Plus: |
Richard Smith | 7993e8a | 2011-10-30 23:17:09 +0000 | [diff] [blame] | 5654 | return EvaluateFloat(E->getSubExpr(), Result, Info); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5655 | case UO_Minus: |
Richard Smith | 7993e8a | 2011-10-30 23:17:09 +0000 | [diff] [blame] | 5656 | if (!EvaluateFloat(E->getSubExpr(), Result, Info)) |
| 5657 | return false; |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 5658 | Result.changeSign(); |
| 5659 | return true; |
| 5660 | } |
| 5661 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 5662 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5663 | bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5664 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
| 5665 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Eli Friedman | 7f92f03 | 2009-11-16 04:25:37 +0000 | [diff] [blame] | 5666 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 5667 | APFloat RHS(0.0); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5668 | bool LHSOK = EvaluateFloat(E->getLHS(), Result, Info); |
| 5669 | if (!LHSOK && !Info.keepEvaluatingAfterFailure()) |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5670 | return false; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5671 | if (!EvaluateFloat(E->getRHS(), RHS, Info) || !LHSOK) |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5672 | return false; |
| 5673 | |
| 5674 | switch (E->getOpcode()) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5675 | default: return Error(E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5676 | case BO_Mul: |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5677 | Result.multiply(RHS, APFloat::rmNearestTiesToEven); |
Richard Smith | 7b48a29 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 5678 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5679 | case BO_Add: |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5680 | Result.add(RHS, APFloat::rmNearestTiesToEven); |
Richard Smith | 7b48a29 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 5681 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5682 | case BO_Sub: |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5683 | Result.subtract(RHS, APFloat::rmNearestTiesToEven); |
Richard Smith | 7b48a29 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 5684 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5685 | case BO_Div: |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5686 | Result.divide(RHS, APFloat::rmNearestTiesToEven); |
Richard Smith | 7b48a29 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 5687 | break; |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5688 | } |
Richard Smith | 7b48a29 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 5689 | |
| 5690 | if (Result.isInfinity() || Result.isNaN()) |
| 5691 | CCEDiag(E, diag::note_constexpr_float_arithmetic) << Result.isNaN(); |
| 5692 | return true; |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5693 | } |
| 5694 | |
| 5695 | bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { |
| 5696 | Result = E->getValue(); |
| 5697 | return true; |
| 5698 | } |
| 5699 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5700 | bool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 5701 | const Expr* SubExpr = E->getSubExpr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5702 | |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 5703 | switch (E->getCastKind()) { |
| 5704 | default: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5705 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 5706 | |
| 5707 | case CK_IntegralToFloating: { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5708 | APSInt IntResult; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5709 | return EvaluateInteger(SubExpr, IntResult, Info) && |
| 5710 | HandleIntToFloatCast(Info, E, SubExpr->getType(), IntResult, |
| 5711 | E->getType(), Result); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5712 | } |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 5713 | |
| 5714 | case CK_FloatingCast: { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5715 | if (!Visit(SubExpr)) |
| 5716 | return false; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5717 | return HandleFloatToFloatCast(Info, E, SubExpr->getType(), E->getType(), |
| 5718 | Result); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5719 | } |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5720 | |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 5721 | case CK_FloatingComplexToReal: { |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5722 | ComplexValue V; |
| 5723 | if (!EvaluateComplex(SubExpr, V, Info)) |
| 5724 | return false; |
| 5725 | Result = V.getComplexFloatReal(); |
| 5726 | return true; |
| 5727 | } |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 5728 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5729 | } |
| 5730 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5731 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 5732 | // Complex Evaluation (for float and integer) |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 5733 | //===----------------------------------------------------------------------===// |
| 5734 | |
| 5735 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 5736 | class ComplexExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5737 | : public ExprEvaluatorBase<ComplexExprEvaluator, bool> { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5738 | ComplexValue &Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5739 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 5740 | public: |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5741 | ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5742 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
| 5743 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5744 | bool Success(const APValue &V, const Expr *e) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5745 | Result.setFrom(V); |
| 5746 | return true; |
| 5747 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5748 | |
Eli Friedman | 7ead5c7 | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 5749 | bool ZeroInitialization(const Expr *E); |
| 5750 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 5751 | //===--------------------------------------------------------------------===// |
| 5752 | // Visitor Methods |
| 5753 | //===--------------------------------------------------------------------===// |
| 5754 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5755 | bool VisitImaginaryLiteral(const ImaginaryLiteral *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5756 | bool VisitCastExpr(const CastExpr *E); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5757 | bool VisitBinaryOperator(const BinaryOperator *E); |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 5758 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | 7ead5c7 | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 5759 | bool VisitInitListExpr(const InitListExpr *E); |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 5760 | }; |
| 5761 | } // end anonymous namespace |
| 5762 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5763 | static bool EvaluateComplex(const Expr *E, ComplexValue &Result, |
| 5764 | EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5765 | assert(E->isRValue() && E->getType()->isAnyComplexType()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5766 | return ComplexExprEvaluator(Info, Result).Visit(E); |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 5767 | } |
| 5768 | |
Eli Friedman | 7ead5c7 | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 5769 | bool ComplexExprEvaluator::ZeroInitialization(const Expr *E) { |
Ted Kremenek | 890f0f1 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 5770 | QualType ElemTy = E->getType()->castAs<ComplexType>()->getElementType(); |
Eli Friedman | 7ead5c7 | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 5771 | if (ElemTy->isRealFloatingType()) { |
| 5772 | Result.makeComplexFloat(); |
| 5773 | APFloat Zero = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(ElemTy)); |
| 5774 | Result.FloatReal = Zero; |
| 5775 | Result.FloatImag = Zero; |
| 5776 | } else { |
| 5777 | Result.makeComplexInt(); |
| 5778 | APSInt Zero = Info.Ctx.MakeIntValue(0, ElemTy); |
| 5779 | Result.IntReal = Zero; |
| 5780 | Result.IntImag = Zero; |
| 5781 | } |
| 5782 | return true; |
| 5783 | } |
| 5784 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5785 | bool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) { |
| 5786 | const Expr* SubExpr = E->getSubExpr(); |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 5787 | |
| 5788 | if (SubExpr->getType()->isRealFloatingType()) { |
| 5789 | Result.makeComplexFloat(); |
| 5790 | APFloat &Imag = Result.FloatImag; |
| 5791 | if (!EvaluateFloat(SubExpr, Imag, Info)) |
| 5792 | return false; |
| 5793 | |
| 5794 | Result.FloatReal = APFloat(Imag.getSemantics()); |
| 5795 | return true; |
| 5796 | } else { |
| 5797 | assert(SubExpr->getType()->isIntegerType() && |
| 5798 | "Unexpected imaginary literal."); |
| 5799 | |
| 5800 | Result.makeComplexInt(); |
| 5801 | APSInt &Imag = Result.IntImag; |
| 5802 | if (!EvaluateInteger(SubExpr, Imag, Info)) |
| 5803 | return false; |
| 5804 | |
| 5805 | Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned()); |
| 5806 | return true; |
| 5807 | } |
| 5808 | } |
| 5809 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5810 | bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) { |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 5811 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5812 | switch (E->getCastKind()) { |
| 5813 | case CK_BitCast: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5814 | case CK_BaseToDerived: |
| 5815 | case CK_DerivedToBase: |
| 5816 | case CK_UncheckedDerivedToBase: |
| 5817 | case CK_Dynamic: |
| 5818 | case CK_ToUnion: |
| 5819 | case CK_ArrayToPointerDecay: |
| 5820 | case CK_FunctionToPointerDecay: |
| 5821 | case CK_NullToPointer: |
| 5822 | case CK_NullToMemberPointer: |
| 5823 | case CK_BaseToDerivedMemberPointer: |
| 5824 | case CK_DerivedToBaseMemberPointer: |
| 5825 | case CK_MemberPointerToBoolean: |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 5826 | case CK_ReinterpretMemberPointer: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5827 | case CK_ConstructorConversion: |
| 5828 | case CK_IntegralToPointer: |
| 5829 | case CK_PointerToIntegral: |
| 5830 | case CK_PointerToBoolean: |
| 5831 | case CK_ToVoid: |
| 5832 | case CK_VectorSplat: |
| 5833 | case CK_IntegralCast: |
| 5834 | case CK_IntegralToBoolean: |
| 5835 | case CK_IntegralToFloating: |
| 5836 | case CK_FloatingToIntegral: |
| 5837 | case CK_FloatingToBoolean: |
| 5838 | case CK_FloatingCast: |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5839 | case CK_CPointerToObjCPointerCast: |
| 5840 | case CK_BlockPointerToObjCPointerCast: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5841 | case CK_AnyPointerToBlockPointerCast: |
| 5842 | case CK_ObjCObjectLValueCast: |
| 5843 | case CK_FloatingComplexToReal: |
| 5844 | case CK_FloatingComplexToBoolean: |
| 5845 | case CK_IntegralComplexToReal: |
| 5846 | case CK_IntegralComplexToBoolean: |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 5847 | case CK_ARCProduceObject: |
| 5848 | case CK_ARCConsumeObject: |
| 5849 | case CK_ARCReclaimReturnedObject: |
| 5850 | case CK_ARCExtendBlockObject: |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 5851 | case CK_CopyAndAutoreleaseBlockObject: |
Eli Friedman | a6c66ce | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 5852 | case CK_BuiltinFnToFnPtr: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5853 | llvm_unreachable("invalid cast kind for complex value"); |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 5854 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5855 | case CK_LValueToRValue: |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 5856 | case CK_AtomicToNonAtomic: |
| 5857 | case CK_NonAtomicToAtomic: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5858 | case CK_NoOp: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5859 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5860 | |
| 5861 | case CK_Dependent: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5862 | case CK_LValueBitCast: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5863 | case CK_UserDefinedConversion: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5864 | return Error(E); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5865 | |
| 5866 | case CK_FloatingRealToComplex: { |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 5867 | APFloat &Real = Result.FloatReal; |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5868 | if (!EvaluateFloat(E->getSubExpr(), Real, Info)) |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 5869 | return false; |
| 5870 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5871 | Result.makeComplexFloat(); |
| 5872 | Result.FloatImag = APFloat(Real.getSemantics()); |
| 5873 | return true; |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 5874 | } |
| 5875 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5876 | case CK_FloatingComplexCast: { |
| 5877 | if (!Visit(E->getSubExpr())) |
| 5878 | return false; |
| 5879 | |
| 5880 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 5881 | QualType From |
| 5882 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 5883 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5884 | return HandleFloatToFloatCast(Info, E, From, To, Result.FloatReal) && |
| 5885 | HandleFloatToFloatCast(Info, E, From, To, Result.FloatImag); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5886 | } |
| 5887 | |
| 5888 | case CK_FloatingComplexToIntegralComplex: { |
| 5889 | if (!Visit(E->getSubExpr())) |
| 5890 | return false; |
| 5891 | |
| 5892 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 5893 | QualType From |
| 5894 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 5895 | Result.makeComplexInt(); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5896 | return HandleFloatToIntCast(Info, E, From, Result.FloatReal, |
| 5897 | To, Result.IntReal) && |
| 5898 | HandleFloatToIntCast(Info, E, From, Result.FloatImag, |
| 5899 | To, Result.IntImag); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5900 | } |
| 5901 | |
| 5902 | case CK_IntegralRealToComplex: { |
| 5903 | APSInt &Real = Result.IntReal; |
| 5904 | if (!EvaluateInteger(E->getSubExpr(), Real, Info)) |
| 5905 | return false; |
| 5906 | |
| 5907 | Result.makeComplexInt(); |
| 5908 | Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned()); |
| 5909 | return true; |
| 5910 | } |
| 5911 | |
| 5912 | case CK_IntegralComplexCast: { |
| 5913 | if (!Visit(E->getSubExpr())) |
| 5914 | return false; |
| 5915 | |
| 5916 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 5917 | QualType From |
| 5918 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 5919 | |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 5920 | Result.IntReal = HandleIntToIntCast(Info, E, To, From, Result.IntReal); |
| 5921 | Result.IntImag = HandleIntToIntCast(Info, E, To, From, Result.IntImag); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5922 | return true; |
| 5923 | } |
| 5924 | |
| 5925 | case CK_IntegralComplexToFloatingComplex: { |
| 5926 | if (!Visit(E->getSubExpr())) |
| 5927 | return false; |
| 5928 | |
Ted Kremenek | 890f0f1 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 5929 | QualType To = E->getType()->castAs<ComplexType>()->getElementType(); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5930 | QualType From |
Ted Kremenek | 890f0f1 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 5931 | = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType(); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5932 | Result.makeComplexFloat(); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5933 | return HandleIntToFloatCast(Info, E, From, Result.IntReal, |
| 5934 | To, Result.FloatReal) && |
| 5935 | HandleIntToFloatCast(Info, E, From, Result.IntImag, |
| 5936 | To, Result.FloatImag); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5937 | } |
| 5938 | } |
| 5939 | |
| 5940 | llvm_unreachable("unknown cast resulting in complex value"); |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 5941 | } |
| 5942 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5943 | bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5944 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
Richard Smith | 2ad226b | 2011-11-16 17:22:48 +0000 | [diff] [blame] | 5945 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
| 5946 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5947 | bool LHSOK = Visit(E->getLHS()); |
| 5948 | if (!LHSOK && !Info.keepEvaluatingAfterFailure()) |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5949 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5950 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5951 | ComplexValue RHS; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5952 | if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK) |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5953 | return false; |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 5954 | |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 5955 | assert(Result.isComplexFloat() == RHS.isComplexFloat() && |
| 5956 | "Invalid operands to binary operator."); |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 5957 | switch (E->getOpcode()) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5958 | default: return Error(E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5959 | case BO_Add: |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 5960 | if (Result.isComplexFloat()) { |
| 5961 | Result.getComplexFloatReal().add(RHS.getComplexFloatReal(), |
| 5962 | APFloat::rmNearestTiesToEven); |
| 5963 | Result.getComplexFloatImag().add(RHS.getComplexFloatImag(), |
| 5964 | APFloat::rmNearestTiesToEven); |
| 5965 | } else { |
| 5966 | Result.getComplexIntReal() += RHS.getComplexIntReal(); |
| 5967 | Result.getComplexIntImag() += RHS.getComplexIntImag(); |
| 5968 | } |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 5969 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5970 | case BO_Sub: |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 5971 | if (Result.isComplexFloat()) { |
| 5972 | Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(), |
| 5973 | APFloat::rmNearestTiesToEven); |
| 5974 | Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(), |
| 5975 | APFloat::rmNearestTiesToEven); |
| 5976 | } else { |
| 5977 | Result.getComplexIntReal() -= RHS.getComplexIntReal(); |
| 5978 | Result.getComplexIntImag() -= RHS.getComplexIntImag(); |
| 5979 | } |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 5980 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5981 | case BO_Mul: |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 5982 | if (Result.isComplexFloat()) { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5983 | ComplexValue LHS = Result; |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 5984 | APFloat &LHS_r = LHS.getComplexFloatReal(); |
| 5985 | APFloat &LHS_i = LHS.getComplexFloatImag(); |
| 5986 | APFloat &RHS_r = RHS.getComplexFloatReal(); |
| 5987 | APFloat &RHS_i = RHS.getComplexFloatImag(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5988 | |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 5989 | APFloat Tmp = LHS_r; |
| 5990 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 5991 | Result.getComplexFloatReal() = Tmp; |
| 5992 | Tmp = LHS_i; |
| 5993 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 5994 | Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven); |
| 5995 | |
| 5996 | Tmp = LHS_r; |
| 5997 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 5998 | Result.getComplexFloatImag() = Tmp; |
| 5999 | Tmp = LHS_i; |
| 6000 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 6001 | Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven); |
| 6002 | } else { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 6003 | ComplexValue LHS = Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6004 | Result.getComplexIntReal() = |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 6005 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() - |
| 6006 | LHS.getComplexIntImag() * RHS.getComplexIntImag()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6007 | Result.getComplexIntImag() = |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 6008 | (LHS.getComplexIntReal() * RHS.getComplexIntImag() + |
| 6009 | LHS.getComplexIntImag() * RHS.getComplexIntReal()); |
| 6010 | } |
| 6011 | break; |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 6012 | case BO_Div: |
| 6013 | if (Result.isComplexFloat()) { |
| 6014 | ComplexValue LHS = Result; |
| 6015 | APFloat &LHS_r = LHS.getComplexFloatReal(); |
| 6016 | APFloat &LHS_i = LHS.getComplexFloatImag(); |
| 6017 | APFloat &RHS_r = RHS.getComplexFloatReal(); |
| 6018 | APFloat &RHS_i = RHS.getComplexFloatImag(); |
| 6019 | APFloat &Res_r = Result.getComplexFloatReal(); |
| 6020 | APFloat &Res_i = Result.getComplexFloatImag(); |
| 6021 | |
| 6022 | APFloat Den = RHS_r; |
| 6023 | Den.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 6024 | APFloat Tmp = RHS_i; |
| 6025 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 6026 | Den.add(Tmp, APFloat::rmNearestTiesToEven); |
| 6027 | |
| 6028 | Res_r = LHS_r; |
| 6029 | Res_r.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 6030 | Tmp = LHS_i; |
| 6031 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 6032 | Res_r.add(Tmp, APFloat::rmNearestTiesToEven); |
| 6033 | Res_r.divide(Den, APFloat::rmNearestTiesToEven); |
| 6034 | |
| 6035 | Res_i = LHS_i; |
| 6036 | Res_i.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 6037 | Tmp = LHS_r; |
| 6038 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 6039 | Res_i.subtract(Tmp, APFloat::rmNearestTiesToEven); |
| 6040 | Res_i.divide(Den, APFloat::rmNearestTiesToEven); |
| 6041 | } else { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6042 | if (RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0) |
| 6043 | return Error(E, diag::note_expr_divide_by_zero); |
| 6044 | |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 6045 | ComplexValue LHS = Result; |
| 6046 | APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() + |
| 6047 | RHS.getComplexIntImag() * RHS.getComplexIntImag(); |
| 6048 | Result.getComplexIntReal() = |
| 6049 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() + |
| 6050 | LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den; |
| 6051 | Result.getComplexIntImag() = |
| 6052 | (LHS.getComplexIntImag() * RHS.getComplexIntReal() - |
| 6053 | LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den; |
| 6054 | } |
| 6055 | break; |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 6056 | } |
| 6057 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 6058 | return true; |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 6059 | } |
| 6060 | |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 6061 | bool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
| 6062 | // Get the operand value into 'Result'. |
| 6063 | if (!Visit(E->getSubExpr())) |
| 6064 | return false; |
| 6065 | |
| 6066 | switch (E->getOpcode()) { |
| 6067 | default: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6068 | return Error(E); |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 6069 | case UO_Extension: |
| 6070 | return true; |
| 6071 | case UO_Plus: |
| 6072 | // The result is always just the subexpr. |
| 6073 | return true; |
| 6074 | case UO_Minus: |
| 6075 | if (Result.isComplexFloat()) { |
| 6076 | Result.getComplexFloatReal().changeSign(); |
| 6077 | Result.getComplexFloatImag().changeSign(); |
| 6078 | } |
| 6079 | else { |
| 6080 | Result.getComplexIntReal() = -Result.getComplexIntReal(); |
| 6081 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
| 6082 | } |
| 6083 | return true; |
| 6084 | case UO_Not: |
| 6085 | if (Result.isComplexFloat()) |
| 6086 | Result.getComplexFloatImag().changeSign(); |
| 6087 | else |
| 6088 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
| 6089 | return true; |
| 6090 | } |
| 6091 | } |
| 6092 | |
Eli Friedman | 7ead5c7 | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 6093 | bool ComplexExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 6094 | if (E->getNumInits() == 2) { |
| 6095 | if (E->getType()->isComplexType()) { |
| 6096 | Result.makeComplexFloat(); |
| 6097 | if (!EvaluateFloat(E->getInit(0), Result.FloatReal, Info)) |
| 6098 | return false; |
| 6099 | if (!EvaluateFloat(E->getInit(1), Result.FloatImag, Info)) |
| 6100 | return false; |
| 6101 | } else { |
| 6102 | Result.makeComplexInt(); |
| 6103 | if (!EvaluateInteger(E->getInit(0), Result.IntReal, Info)) |
| 6104 | return false; |
| 6105 | if (!EvaluateInteger(E->getInit(1), Result.IntImag, Info)) |
| 6106 | return false; |
| 6107 | } |
| 6108 | return true; |
| 6109 | } |
| 6110 | return ExprEvaluatorBaseTy::VisitInitListExpr(E); |
| 6111 | } |
| 6112 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 6113 | //===----------------------------------------------------------------------===// |
Richard Smith | aa9c350 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 6114 | // Void expression evaluation, primarily for a cast to void on the LHS of a |
| 6115 | // comma operator |
| 6116 | //===----------------------------------------------------------------------===// |
| 6117 | |
| 6118 | namespace { |
| 6119 | class VoidExprEvaluator |
| 6120 | : public ExprEvaluatorBase<VoidExprEvaluator, bool> { |
| 6121 | public: |
| 6122 | VoidExprEvaluator(EvalInfo &Info) : ExprEvaluatorBaseTy(Info) {} |
| 6123 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6124 | bool Success(const APValue &V, const Expr *e) { return true; } |
Richard Smith | aa9c350 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 6125 | |
| 6126 | bool VisitCastExpr(const CastExpr *E) { |
| 6127 | switch (E->getCastKind()) { |
| 6128 | default: |
| 6129 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 6130 | case CK_ToVoid: |
| 6131 | VisitIgnoredValue(E->getSubExpr()); |
| 6132 | return true; |
| 6133 | } |
| 6134 | } |
| 6135 | }; |
| 6136 | } // end anonymous namespace |
| 6137 | |
| 6138 | static bool EvaluateVoid(const Expr *E, EvalInfo &Info) { |
| 6139 | assert(E->isRValue() && E->getType()->isVoidType()); |
| 6140 | return VoidExprEvaluator(Info).Visit(E); |
| 6141 | } |
| 6142 | |
| 6143 | //===----------------------------------------------------------------------===// |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 6144 | // Top level Expr::EvaluateAsRValue method. |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 6145 | //===----------------------------------------------------------------------===// |
| 6146 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6147 | static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6148 | // In C, function designators are not lvalues, but we evaluate them as if they |
| 6149 | // are. |
| 6150 | if (E->isGLValue() || E->getType()->isFunctionType()) { |
| 6151 | LValue LV; |
| 6152 | if (!EvaluateLValue(E, LV, Info)) |
| 6153 | return false; |
| 6154 | LV.moveInto(Result); |
| 6155 | } else if (E->getType()->isVectorType()) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 6156 | if (!EvaluateVector(E, Result, Info)) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6157 | return false; |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 6158 | } else if (E->getType()->isIntegralOrEnumerationType()) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 6159 | if (!IntExprEvaluator(Info, Result).Visit(E)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 6160 | return false; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 6161 | } else if (E->getType()->hasPointerRepresentation()) { |
| 6162 | LValue LV; |
| 6163 | if (!EvaluatePointer(E, LV, Info)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 6164 | return false; |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 6165 | LV.moveInto(Result); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 6166 | } else if (E->getType()->isRealFloatingType()) { |
| 6167 | llvm::APFloat F(0.0); |
| 6168 | if (!EvaluateFloat(E, F, Info)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 6169 | return false; |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6170 | Result = APValue(F); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 6171 | } else if (E->getType()->isAnyComplexType()) { |
| 6172 | ComplexValue C; |
| 6173 | if (!EvaluateComplex(E, C, Info)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 6174 | return false; |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 6175 | C.moveInto(Result); |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 6176 | } else if (E->getType()->isMemberPointerType()) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6177 | MemberPtr P; |
| 6178 | if (!EvaluateMemberPointer(E, P, Info)) |
| 6179 | return false; |
| 6180 | P.moveInto(Result); |
| 6181 | return true; |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6182 | } else if (E->getType()->isArrayType()) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6183 | LValue LV; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6184 | LV.set(E, Info.CurrentCall->Index); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6185 | if (!EvaluateArray(E, LV, Info.CurrentCall->Temporaries[E], Info)) |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 6186 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6187 | Result = Info.CurrentCall->Temporaries[E]; |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6188 | } else if (E->getType()->isRecordType()) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6189 | LValue LV; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6190 | LV.set(E, Info.CurrentCall->Index); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6191 | if (!EvaluateRecord(E, LV, Info.CurrentCall->Temporaries[E], Info)) |
| 6192 | return false; |
| 6193 | Result = Info.CurrentCall->Temporaries[E]; |
Richard Smith | aa9c350 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 6194 | } else if (E->getType()->isVoidType()) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6195 | if (!Info.getLangOpts().CPlusPlus11) |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 6196 | Info.CCEDiag(E, diag::note_constexpr_nonliteral) |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 6197 | << E->getType(); |
Richard Smith | aa9c350 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 6198 | if (!EvaluateVoid(E, Info)) |
| 6199 | return false; |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6200 | } else if (Info.getLangOpts().CPlusPlus11) { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 6201 | Info.Diag(E, diag::note_constexpr_nonliteral) << E->getType(); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 6202 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6203 | } else { |
Richard Smith | 5cfc7d8 | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 6204 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Anders Carlsson | 9d4c157 | 2008-11-22 22:56:32 +0000 | [diff] [blame] | 6205 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6206 | } |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 6207 | |
Anders Carlsson | 5b45d4e | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 6208 | return true; |
| 6209 | } |
| 6210 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6211 | /// EvaluateInPlace - Evaluate an expression in-place in an APValue. In some |
| 6212 | /// cases, the in-place evaluation is essential, since later initializers for |
| 6213 | /// an object can indirectly refer to subobjects which were initialized earlier. |
| 6214 | static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, const LValue &This, |
| 6215 | const Expr *E, CheckConstantExpressionKind CCEK, |
| 6216 | bool AllowNonLiteralTypes) { |
Richard Smith | 7ca4850 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 6217 | if (!AllowNonLiteralTypes && !CheckLiteralType(Info, E)) |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6218 | return false; |
| 6219 | |
| 6220 | if (E->isRValue()) { |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 6221 | // Evaluate arrays and record types in-place, so that later initializers can |
| 6222 | // refer to earlier-initialized members of the object. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6223 | if (E->getType()->isArrayType()) |
| 6224 | return EvaluateArray(E, This, Result, Info); |
| 6225 | else if (E->getType()->isRecordType()) |
| 6226 | return EvaluateRecord(E, This, Result, Info); |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 6227 | } |
| 6228 | |
| 6229 | // For any other type, in-place evaluation is unimportant. |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6230 | return Evaluate(Result, Info, E); |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 6231 | } |
| 6232 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6233 | /// EvaluateAsRValue - Try to evaluate this expression, performing an implicit |
| 6234 | /// lvalue-to-rvalue cast if it is an lvalue. |
| 6235 | static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6236 | if (!CheckLiteralType(Info, E)) |
| 6237 | return false; |
| 6238 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6239 | if (!::Evaluate(Result, Info, E)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6240 | return false; |
| 6241 | |
| 6242 | if (E->isGLValue()) { |
| 6243 | LValue LV; |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6244 | LV.setFrom(Info.Ctx, Result); |
| 6245 | if (!HandleLValueToRValueConversion(Info, E, E->getType(), LV, Result)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6246 | return false; |
| 6247 | } |
| 6248 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6249 | // Check this core constant expression is a constant expression. |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6250 | return CheckConstantExpression(Info, E->getExprLoc(), E->getType(), Result); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6251 | } |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6252 | |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 6253 | /// 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] | 6254 | /// any crazy technique (that has nothing to do with language standards) that |
| 6255 | /// 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] | 6256 | /// in Result. If this expression is a glvalue, an lvalue-to-rvalue conversion |
| 6257 | /// will be applied to the result. |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 6258 | bool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx) const { |
Richard Smith | ee19f43 | 2011-12-10 01:10:13 +0000 | [diff] [blame] | 6259 | // Fast-path evaluations of integer literals, since we sometimes see files |
| 6260 | // containing vast quantities of these. |
| 6261 | if (const IntegerLiteral *L = dyn_cast<IntegerLiteral>(this)) { |
| 6262 | Result.Val = APValue(APSInt(L->getValue(), |
| 6263 | L->getType()->isUnsignedIntegerType())); |
| 6264 | return true; |
| 6265 | } |
| 6266 | |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 6267 | // FIXME: Evaluating values of large array and record types can cause |
| 6268 | // performance problems. Only do so in C++11 for now. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6269 | if (isRValue() && (getType()->isArrayType() || getType()->isRecordType()) && |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6270 | !Ctx.getLangOpts().CPlusPlus11) |
Richard Smith | 1445bba | 2011-11-10 03:30:42 +0000 | [diff] [blame] | 6271 | return false; |
| 6272 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6273 | EvalInfo Info(Ctx, Result); |
| 6274 | return ::EvaluateAsRValue(Info, this, Result.Val); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6275 | } |
| 6276 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6277 | bool Expr::EvaluateAsBooleanCondition(bool &Result, |
| 6278 | const ASTContext &Ctx) const { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6279 | EvalResult Scratch; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 6280 | return EvaluateAsRValue(Scratch, Ctx) && |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6281 | HandleConversionToBool(Scratch.Val, Result); |
John McCall | cd7a445 | 2010-01-05 23:42:56 +0000 | [diff] [blame] | 6282 | } |
| 6283 | |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 6284 | bool Expr::EvaluateAsInt(APSInt &Result, const ASTContext &Ctx, |
| 6285 | SideEffectsKind AllowSideEffects) const { |
| 6286 | if (!getType()->isIntegralOrEnumerationType()) |
| 6287 | return false; |
| 6288 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6289 | EvalResult ExprResult; |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 6290 | if (!EvaluateAsRValue(ExprResult, Ctx) || !ExprResult.Val.isInt() || |
| 6291 | (!AllowSideEffects && ExprResult.HasSideEffects)) |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6292 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6293 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6294 | Result = ExprResult.Val.getInt(); |
| 6295 | return true; |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 6296 | } |
| 6297 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6298 | bool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx) const { |
Anders Carlsson | 1b78276 | 2009-04-10 04:54:13 +0000 | [diff] [blame] | 6299 | EvalInfo Info(Ctx, Result); |
| 6300 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 6301 | LValue LV; |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6302 | if (!EvaluateLValue(this, LV, Info) || Result.HasSideEffects || |
| 6303 | !CheckLValueConstantExpression(Info, getExprLoc(), |
| 6304 | Ctx.getLValueReferenceType(getType()), LV)) |
| 6305 | return false; |
| 6306 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6307 | LV.moveInto(Result.Val); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6308 | return true; |
Eli Friedman | b2f295c | 2009-09-13 10:17:44 +0000 | [diff] [blame] | 6309 | } |
| 6310 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 6311 | bool Expr::EvaluateAsInitializer(APValue &Value, const ASTContext &Ctx, |
| 6312 | const VarDecl *VD, |
| 6313 | llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const { |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 6314 | // FIXME: Evaluating initializers for large array and record types can cause |
| 6315 | // performance problems. Only do so in C++11 for now. |
| 6316 | if (isRValue() && (getType()->isArrayType() || getType()->isRecordType()) && |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6317 | !Ctx.getLangOpts().CPlusPlus11) |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 6318 | return false; |
| 6319 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 6320 | Expr::EvalStatus EStatus; |
| 6321 | EStatus.Diag = &Notes; |
| 6322 | |
| 6323 | EvalInfo InitInfo(Ctx, EStatus); |
| 6324 | InitInfo.setEvaluatingDecl(VD, Value); |
| 6325 | |
| 6326 | LValue LVal; |
| 6327 | LVal.set(VD); |
| 6328 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6329 | // C++11 [basic.start.init]p2: |
| 6330 | // Variables with static storage duration or thread storage duration shall be |
| 6331 | // zero-initialized before any other initialization takes place. |
| 6332 | // This behavior is not present in C. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6333 | if (Ctx.getLangOpts().CPlusPlus && !VD->hasLocalStorage() && |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6334 | !VD->getType()->isReferenceType()) { |
| 6335 | ImplicitValueInitExpr VIE(VD->getType()); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6336 | if (!EvaluateInPlace(Value, InitInfo, LVal, &VIE, CCEK_Constant, |
| 6337 | /*AllowNonLiteralTypes=*/true)) |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6338 | return false; |
| 6339 | } |
| 6340 | |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6341 | if (!EvaluateInPlace(Value, InitInfo, LVal, this, CCEK_Constant, |
| 6342 | /*AllowNonLiteralTypes=*/true) || |
| 6343 | EStatus.HasSideEffects) |
| 6344 | return false; |
| 6345 | |
| 6346 | return CheckConstantExpression(InitInfo, VD->getLocation(), VD->getType(), |
| 6347 | Value); |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 6348 | } |
| 6349 | |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 6350 | /// isEvaluatable - Call EvaluateAsRValue to see if this expression can be |
| 6351 | /// constant folded, but discard the result. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6352 | bool Expr::isEvaluatable(const ASTContext &Ctx) const { |
Anders Carlsson | 4fdfb09 | 2008-12-01 06:44:05 +0000 | [diff] [blame] | 6353 | EvalResult Result; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 6354 | return EvaluateAsRValue(Result, Ctx) && !Result.HasSideEffects; |
Chris Lattner | 45b6b9d | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 6355 | } |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 6356 | |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 6357 | APSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx) const { |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 6358 | EvalResult EvalResult; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 6359 | bool Result = EvaluateAsRValue(EvalResult, Ctx); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 6360 | (void)Result; |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 6361 | assert(Result && "Could not evaluate expression"); |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 6362 | assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer"); |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 6363 | |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 6364 | return EvalResult.Val.getInt(); |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 6365 | } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6366 | |
Abramo Bagnara | e17a643 | 2010-05-14 17:07:14 +0000 | [diff] [blame] | 6367 | bool Expr::EvalResult::isGlobalLValue() const { |
| 6368 | assert(Val.isLValue()); |
| 6369 | return IsGlobalLValue(Val.getLValueBase()); |
| 6370 | } |
| 6371 | |
| 6372 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6373 | /// isIntegerConstantExpr - this recursive routine will test if an expression is |
| 6374 | /// an integer constant expression. |
| 6375 | |
| 6376 | /// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, |
| 6377 | /// comma, etc |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6378 | |
| 6379 | // CheckICE - This function does the fundamental ICE checking: the returned |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 6380 | // ICEDiag contains an ICEKind indicating whether the expression is an ICE, |
| 6381 | // and a (possibly null) SourceLocation indicating the location of the problem. |
| 6382 | // |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6383 | // Note that to reduce code duplication, this helper does no evaluation |
| 6384 | // itself; the caller checks whether the expression is evaluatable, and |
| 6385 | // in the rare cases where CheckICE actually cares about the evaluated |
| 6386 | // value, it calls into Evalute. |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6387 | |
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 6388 | namespace { |
| 6389 | |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 6390 | enum ICEKind { |
| 6391 | /// This expression is an ICE. |
| 6392 | IK_ICE, |
| 6393 | /// This expression is not an ICE, but if it isn't evaluated, it's |
| 6394 | /// a legal subexpression for an ICE. This return value is used to handle |
| 6395 | /// the comma operator in C99 mode, and non-constant subexpressions. |
| 6396 | IK_ICEIfUnevaluated, |
| 6397 | /// This expression is not an ICE, and is not a legal subexpression for one. |
| 6398 | IK_NotICE |
| 6399 | }; |
| 6400 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6401 | struct ICEDiag { |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 6402 | ICEKind Kind; |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6403 | SourceLocation Loc; |
| 6404 | |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 6405 | ICEDiag(ICEKind IK, SourceLocation l) : Kind(IK), Loc(l) {} |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6406 | }; |
| 6407 | |
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 6408 | } |
| 6409 | |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 6410 | static ICEDiag NoDiag() { return ICEDiag(IK_ICE, SourceLocation()); } |
| 6411 | |
| 6412 | static ICEDiag Worst(ICEDiag A, ICEDiag B) { return A.Kind >= B.Kind ? A : B; } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6413 | |
| 6414 | static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) { |
| 6415 | Expr::EvalResult EVResult; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 6416 | if (!E->EvaluateAsRValue(EVResult, Ctx) || EVResult.HasSideEffects || |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 6417 | !EVResult.Val.isInt()) |
| 6418 | return ICEDiag(IK_NotICE, E->getLocStart()); |
| 6419 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6420 | return NoDiag(); |
| 6421 | } |
| 6422 | |
| 6423 | static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) { |
| 6424 | assert(!E->isValueDependent() && "Should not see value dependent exprs!"); |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 6425 | if (!E->getType()->isIntegralOrEnumerationType()) |
| 6426 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6427 | |
| 6428 | switch (E->getStmtClass()) { |
John McCall | 63c00d7 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 6429 | #define ABSTRACT_STMT(Node) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6430 | #define STMT(Node, Base) case Expr::Node##Class: |
| 6431 | #define EXPR(Node, Base) |
| 6432 | #include "clang/AST/StmtNodes.inc" |
| 6433 | case Expr::PredefinedExprClass: |
| 6434 | case Expr::FloatingLiteralClass: |
| 6435 | case Expr::ImaginaryLiteralClass: |
| 6436 | case Expr::StringLiteralClass: |
| 6437 | case Expr::ArraySubscriptExprClass: |
| 6438 | case Expr::MemberExprClass: |
| 6439 | case Expr::CompoundAssignOperatorClass: |
| 6440 | case Expr::CompoundLiteralExprClass: |
| 6441 | case Expr::ExtVectorElementExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6442 | case Expr::DesignatedInitExprClass: |
| 6443 | case Expr::ImplicitValueInitExprClass: |
| 6444 | case Expr::ParenListExprClass: |
| 6445 | case Expr::VAArgExprClass: |
| 6446 | case Expr::AddrLabelExprClass: |
| 6447 | case Expr::StmtExprClass: |
| 6448 | case Expr::CXXMemberCallExprClass: |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 6449 | case Expr::CUDAKernelCallExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6450 | case Expr::CXXDynamicCastExprClass: |
| 6451 | case Expr::CXXTypeidExprClass: |
Francois Pichet | 9be8840 | 2010-09-08 23:47:05 +0000 | [diff] [blame] | 6452 | case Expr::CXXUuidofExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6453 | case Expr::CXXNullPtrLiteralExprClass: |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 6454 | case Expr::UserDefinedLiteralClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6455 | case Expr::CXXThisExprClass: |
| 6456 | case Expr::CXXThrowExprClass: |
| 6457 | case Expr::CXXNewExprClass: |
| 6458 | case Expr::CXXDeleteExprClass: |
| 6459 | case Expr::CXXPseudoDestructorExprClass: |
| 6460 | case Expr::UnresolvedLookupExprClass: |
| 6461 | case Expr::DependentScopeDeclRefExprClass: |
| 6462 | case Expr::CXXConstructExprClass: |
| 6463 | case Expr::CXXBindTemporaryExprClass: |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 6464 | case Expr::ExprWithCleanupsClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6465 | case Expr::CXXTemporaryObjectExprClass: |
| 6466 | case Expr::CXXUnresolvedConstructExprClass: |
| 6467 | case Expr::CXXDependentScopeMemberExprClass: |
| 6468 | case Expr::UnresolvedMemberExprClass: |
| 6469 | case Expr::ObjCStringLiteralClass: |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 6470 | case Expr::ObjCBoxedExprClass: |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 6471 | case Expr::ObjCArrayLiteralClass: |
| 6472 | case Expr::ObjCDictionaryLiteralClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6473 | case Expr::ObjCEncodeExprClass: |
| 6474 | case Expr::ObjCMessageExprClass: |
| 6475 | case Expr::ObjCSelectorExprClass: |
| 6476 | case Expr::ObjCProtocolExprClass: |
| 6477 | case Expr::ObjCIvarRefExprClass: |
| 6478 | case Expr::ObjCPropertyRefExprClass: |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 6479 | case Expr::ObjCSubscriptRefExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6480 | case Expr::ObjCIsaExprClass: |
| 6481 | case Expr::ShuffleVectorExprClass: |
| 6482 | case Expr::BlockExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6483 | case Expr::NoStmtClass: |
John McCall | 7cd7d1a | 2010-11-15 23:31:06 +0000 | [diff] [blame] | 6484 | case Expr::OpaqueValueExprClass: |
Douglas Gregor | be230c3 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 6485 | case Expr::PackExpansionExprClass: |
Douglas Gregor | c7793c7 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 6486 | case Expr::SubstNonTypeTemplateParmPackExprClass: |
Richard Smith | 9a4db03 | 2012-09-12 00:56:43 +0000 | [diff] [blame] | 6487 | case Expr::FunctionParmPackExprClass: |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 6488 | case Expr::AsTypeExprClass: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6489 | case Expr::ObjCIndirectCopyRestoreExprClass: |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 6490 | case Expr::MaterializeTemporaryExprClass: |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 6491 | case Expr::PseudoObjectExprClass: |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 6492 | case Expr::AtomicExprClass: |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 6493 | case Expr::InitListExprClass: |
Douglas Gregor | 01d0801 | 2012-02-07 10:09:13 +0000 | [diff] [blame] | 6494 | case Expr::LambdaExprClass: |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 6495 | return ICEDiag(IK_NotICE, E->getLocStart()); |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 6496 | |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 6497 | case Expr::SizeOfPackExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6498 | case Expr::GNUNullExprClass: |
| 6499 | // GCC considers the GNU __null value to be an integral constant expression. |
| 6500 | return NoDiag(); |
| 6501 | |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 6502 | case Expr::SubstNonTypeTemplateParmExprClass: |
| 6503 | return |
| 6504 | CheckICE(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(), Ctx); |
| 6505 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6506 | case Expr::ParenExprClass: |
| 6507 | return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx); |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 6508 | case Expr::GenericSelectionExprClass: |
| 6509 | return CheckICE(cast<GenericSelectionExpr>(E)->getResultExpr(), Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6510 | case Expr::IntegerLiteralClass: |
| 6511 | case Expr::CharacterLiteralClass: |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 6512 | case Expr::ObjCBoolLiteralExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6513 | case Expr::CXXBoolLiteralExprClass: |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 6514 | case Expr::CXXScalarValueInitExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6515 | case Expr::UnaryTypeTraitExprClass: |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 6516 | case Expr::BinaryTypeTraitExprClass: |
Douglas Gregor | 4ca8ac2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 6517 | case Expr::TypeTraitExprClass: |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 6518 | case Expr::ArrayTypeTraitExprClass: |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 6519 | case Expr::ExpressionTraitExprClass: |
Sebastian Redl | 2e15622 | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 6520 | case Expr::CXXNoexceptExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6521 | return NoDiag(); |
| 6522 | case Expr::CallExprClass: |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 6523 | case Expr::CXXOperatorCallExprClass: { |
Richard Smith | 0583014 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 6524 | // C99 6.6/3 allows function calls within unevaluated subexpressions of |
| 6525 | // constant expressions, but they can never be ICEs because an ICE cannot |
| 6526 | // contain an operand of (pointer to) function type. |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6527 | const CallExpr *CE = cast<CallExpr>(E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6528 | if (CE->isBuiltinCall()) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6529 | return CheckEvalInICE(E, Ctx); |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 6530 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6531 | } |
Richard Smith | 359c89d | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 6532 | case Expr::DeclRefExprClass: { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6533 | if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl())) |
| 6534 | return NoDiag(); |
Richard Smith | 359c89d | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 6535 | const ValueDecl *D = dyn_cast<ValueDecl>(cast<DeclRefExpr>(E)->getDecl()); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6536 | if (Ctx.getLangOpts().CPlusPlus && |
Richard Smith | 359c89d | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 6537 | D && IsConstNonVolatile(D->getType())) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6538 | // Parameter variables are never constants. Without this check, |
| 6539 | // getAnyInitializer() can find a default argument, which leads |
| 6540 | // to chaos. |
| 6541 | if (isa<ParmVarDecl>(D)) |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 6542 | return ICEDiag(IK_NotICE, cast<DeclRefExpr>(E)->getLocation()); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6543 | |
| 6544 | // C++ 7.1.5.1p2 |
| 6545 | // A variable of non-volatile const-qualified integral or enumeration |
| 6546 | // type initialized by an ICE can be used in ICEs. |
| 6547 | if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) { |
Richard Smith | db1822c | 2011-11-08 01:31:09 +0000 | [diff] [blame] | 6548 | if (!Dcl->getType()->isIntegralOrEnumerationType()) |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 6549 | return ICEDiag(IK_NotICE, cast<DeclRefExpr>(E)->getLocation()); |
Richard Smith | db1822c | 2011-11-08 01:31:09 +0000 | [diff] [blame] | 6550 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 6551 | const VarDecl *VD; |
| 6552 | // Look for a declaration of this variable that has an initializer, and |
| 6553 | // check whether it is an ICE. |
| 6554 | if (Dcl->getAnyInitializer(VD) && VD->checkInitIsICE()) |
| 6555 | return NoDiag(); |
| 6556 | else |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 6557 | return ICEDiag(IK_NotICE, cast<DeclRefExpr>(E)->getLocation()); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6558 | } |
| 6559 | } |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 6560 | return ICEDiag(IK_NotICE, E->getLocStart()); |
Richard Smith | 359c89d | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 6561 | } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6562 | case Expr::UnaryOperatorClass: { |
| 6563 | const UnaryOperator *Exp = cast<UnaryOperator>(E); |
| 6564 | switch (Exp->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6565 | case UO_PostInc: |
| 6566 | case UO_PostDec: |
| 6567 | case UO_PreInc: |
| 6568 | case UO_PreDec: |
| 6569 | case UO_AddrOf: |
| 6570 | case UO_Deref: |
Richard Smith | 0583014 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 6571 | // C99 6.6/3 allows increment and decrement within unevaluated |
| 6572 | // subexpressions of constant expressions, but they can never be ICEs |
| 6573 | // because an ICE cannot contain an lvalue operand. |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 6574 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6575 | case UO_Extension: |
| 6576 | case UO_LNot: |
| 6577 | case UO_Plus: |
| 6578 | case UO_Minus: |
| 6579 | case UO_Not: |
| 6580 | case UO_Real: |
| 6581 | case UO_Imag: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6582 | return CheckICE(Exp->getSubExpr(), Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6583 | } |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 6584 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6585 | // OffsetOf falls through here. |
| 6586 | } |
| 6587 | case Expr::OffsetOfExprClass: { |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 6588 | // Note that per C99, offsetof must be an ICE. And AFAIK, using |
| 6589 | // EvaluateAsRValue matches the proposed gcc behavior for cases like |
| 6590 | // "offsetof(struct s{int x[4];}, x[1.0])". This doesn't affect |
| 6591 | // compliance: we should warn earlier for offsetof expressions with |
| 6592 | // array subscripts that aren't ICEs, and if the array subscripts |
| 6593 | // are ICEs, the value of the offsetof must be an integer constant. |
| 6594 | return CheckEvalInICE(E, Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6595 | } |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 6596 | case Expr::UnaryExprOrTypeTraitExprClass: { |
| 6597 | const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(E); |
| 6598 | if ((Exp->getKind() == UETT_SizeOf) && |
| 6599 | Exp->getTypeOfArgument()->isVariableArrayType()) |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 6600 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6601 | return NoDiag(); |
| 6602 | } |
| 6603 | case Expr::BinaryOperatorClass: { |
| 6604 | const BinaryOperator *Exp = cast<BinaryOperator>(E); |
| 6605 | switch (Exp->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6606 | case BO_PtrMemD: |
| 6607 | case BO_PtrMemI: |
| 6608 | case BO_Assign: |
| 6609 | case BO_MulAssign: |
| 6610 | case BO_DivAssign: |
| 6611 | case BO_RemAssign: |
| 6612 | case BO_AddAssign: |
| 6613 | case BO_SubAssign: |
| 6614 | case BO_ShlAssign: |
| 6615 | case BO_ShrAssign: |
| 6616 | case BO_AndAssign: |
| 6617 | case BO_XorAssign: |
| 6618 | case BO_OrAssign: |
Richard Smith | 0583014 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 6619 | // C99 6.6/3 allows assignments within unevaluated subexpressions of |
| 6620 | // constant expressions, but they can never be ICEs because an ICE cannot |
| 6621 | // contain an lvalue operand. |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 6622 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6623 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6624 | case BO_Mul: |
| 6625 | case BO_Div: |
| 6626 | case BO_Rem: |
| 6627 | case BO_Add: |
| 6628 | case BO_Sub: |
| 6629 | case BO_Shl: |
| 6630 | case BO_Shr: |
| 6631 | case BO_LT: |
| 6632 | case BO_GT: |
| 6633 | case BO_LE: |
| 6634 | case BO_GE: |
| 6635 | case BO_EQ: |
| 6636 | case BO_NE: |
| 6637 | case BO_And: |
| 6638 | case BO_Xor: |
| 6639 | case BO_Or: |
| 6640 | case BO_Comma: { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6641 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 6642 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6643 | if (Exp->getOpcode() == BO_Div || |
| 6644 | Exp->getOpcode() == BO_Rem) { |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 6645 | // EvaluateAsRValue gives an error for undefined Div/Rem, so make sure |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6646 | // we don't evaluate one. |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 6647 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 6648 | llvm::APSInt REval = Exp->getRHS()->EvaluateKnownConstInt(Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6649 | if (REval == 0) |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 6650 | return ICEDiag(IK_ICEIfUnevaluated, E->getLocStart()); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6651 | if (REval.isSigned() && REval.isAllOnesValue()) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 6652 | llvm::APSInt LEval = Exp->getLHS()->EvaluateKnownConstInt(Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6653 | if (LEval.isMinSignedValue()) |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 6654 | return ICEDiag(IK_ICEIfUnevaluated, E->getLocStart()); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6655 | } |
| 6656 | } |
| 6657 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6658 | if (Exp->getOpcode() == BO_Comma) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6659 | if (Ctx.getLangOpts().C99) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6660 | // C99 6.6p3 introduces a strange edge case: comma can be in an ICE |
| 6661 | // if it isn't evaluated. |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 6662 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE) |
| 6663 | return ICEDiag(IK_ICEIfUnevaluated, E->getLocStart()); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6664 | } else { |
| 6665 | // In both C89 and C++, commas in ICEs are illegal. |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 6666 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6667 | } |
| 6668 | } |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 6669 | return Worst(LHSResult, RHSResult); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6670 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6671 | case BO_LAnd: |
| 6672 | case BO_LOr: { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6673 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 6674 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 6675 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICEIfUnevaluated) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6676 | // Rare case where the RHS has a comma "side-effect"; we need |
| 6677 | // to actually check the condition to see whether the side |
| 6678 | // with the comma is evaluated. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6679 | if ((Exp->getOpcode() == BO_LAnd) != |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 6680 | (Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0)) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6681 | return RHSResult; |
| 6682 | return NoDiag(); |
| 6683 | } |
| 6684 | |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 6685 | return Worst(LHSResult, RHSResult); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6686 | } |
| 6687 | } |
| 6688 | } |
| 6689 | case Expr::ImplicitCastExprClass: |
| 6690 | case Expr::CStyleCastExprClass: |
| 6691 | case Expr::CXXFunctionalCastExprClass: |
| 6692 | case Expr::CXXStaticCastExprClass: |
| 6693 | case Expr::CXXReinterpretCastExprClass: |
Richard Smith | 32cb471 | 2011-10-24 18:26:35 +0000 | [diff] [blame] | 6694 | case Expr::CXXConstCastExprClass: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6695 | case Expr::ObjCBridgedCastExprClass: { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6696 | const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr(); |
Richard Smith | 2116b14 | 2011-12-18 02:33:09 +0000 | [diff] [blame] | 6697 | if (isa<ExplicitCastExpr>(E)) { |
| 6698 | if (const FloatingLiteral *FL |
| 6699 | = dyn_cast<FloatingLiteral>(SubExpr->IgnoreParenImpCasts())) { |
| 6700 | unsigned DestWidth = Ctx.getIntWidth(E->getType()); |
| 6701 | bool DestSigned = E->getType()->isSignedIntegerOrEnumerationType(); |
| 6702 | APSInt IgnoredVal(DestWidth, !DestSigned); |
| 6703 | bool Ignored; |
| 6704 | // If the value does not fit in the destination type, the behavior is |
| 6705 | // undefined, so we are not required to treat it as a constant |
| 6706 | // expression. |
| 6707 | if (FL->getValue().convertToInteger(IgnoredVal, |
| 6708 | llvm::APFloat::rmTowardZero, |
| 6709 | &Ignored) & APFloat::opInvalidOp) |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 6710 | return ICEDiag(IK_NotICE, E->getLocStart()); |
Richard Smith | 2116b14 | 2011-12-18 02:33:09 +0000 | [diff] [blame] | 6711 | return NoDiag(); |
| 6712 | } |
| 6713 | } |
Eli Friedman | eea0e81 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 6714 | switch (cast<CastExpr>(E)->getCastKind()) { |
| 6715 | case CK_LValueToRValue: |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 6716 | case CK_AtomicToNonAtomic: |
| 6717 | case CK_NonAtomicToAtomic: |
Eli Friedman | eea0e81 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 6718 | case CK_NoOp: |
| 6719 | case CK_IntegralToBoolean: |
| 6720 | case CK_IntegralCast: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6721 | return CheckICE(SubExpr, Ctx); |
Eli Friedman | eea0e81 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 6722 | default: |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 6723 | return ICEDiag(IK_NotICE, E->getLocStart()); |
Eli Friedman | eea0e81 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 6724 | } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6725 | } |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6726 | case Expr::BinaryConditionalOperatorClass: { |
| 6727 | const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(E); |
| 6728 | ICEDiag CommonResult = CheckICE(Exp->getCommon(), Ctx); |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 6729 | if (CommonResult.Kind == IK_NotICE) return CommonResult; |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6730 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 6731 | if (FalseResult.Kind == IK_NotICE) return FalseResult; |
| 6732 | if (CommonResult.Kind == IK_ICEIfUnevaluated) return CommonResult; |
| 6733 | if (FalseResult.Kind == IK_ICEIfUnevaluated && |
Richard Smith | 9b403c5 | 2012-12-28 12:53:55 +0000 | [diff] [blame] | 6734 | Exp->getCommon()->EvaluateKnownConstInt(Ctx) != 0) return NoDiag(); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6735 | return FalseResult; |
| 6736 | } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6737 | case Expr::ConditionalOperatorClass: { |
| 6738 | const ConditionalOperator *Exp = cast<ConditionalOperator>(E); |
| 6739 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
| 6740 | // then only the true side is actually considered in an integer constant |
| 6741 | // expression, and it is fully evaluated. This is an important GNU |
| 6742 | // extension. See GCC PR38377 for discussion. |
| 6743 | if (const CallExpr *CallCE |
| 6744 | = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts())) |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 6745 | if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p) |
| 6746 | return CheckEvalInICE(E, Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6747 | ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx); |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 6748 | if (CondResult.Kind == IK_NotICE) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6749 | return CondResult; |
Douglas Gregor | 63fe681 | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 6750 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6751 | ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx); |
| 6752 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
Douglas Gregor | 63fe681 | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 6753 | |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 6754 | if (TrueResult.Kind == IK_NotICE) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6755 | return TrueResult; |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 6756 | if (FalseResult.Kind == IK_NotICE) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6757 | return FalseResult; |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 6758 | if (CondResult.Kind == IK_ICEIfUnevaluated) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6759 | return CondResult; |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 6760 | if (TrueResult.Kind == IK_ICE && FalseResult.Kind == IK_ICE) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6761 | return NoDiag(); |
| 6762 | // Rare case where the diagnostics depend on which side is evaluated |
| 6763 | // Note that if we get here, CondResult is 0, and at least one of |
| 6764 | // TrueResult and FalseResult is non-zero. |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 6765 | if (Exp->getCond()->EvaluateKnownConstInt(Ctx) == 0) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6766 | return FalseResult; |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6767 | return TrueResult; |
| 6768 | } |
| 6769 | case Expr::CXXDefaultArgExprClass: |
| 6770 | return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx); |
| 6771 | case Expr::ChooseExprClass: { |
| 6772 | return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx); |
| 6773 | } |
| 6774 | } |
| 6775 | |
David Blaikie | 3026348 | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 6776 | llvm_unreachable("Invalid StmtClass!"); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6777 | } |
| 6778 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6779 | /// Evaluate an expression as a C++11 integral constant expression. |
| 6780 | static bool EvaluateCPlusPlus11IntegralConstantExpr(ASTContext &Ctx, |
| 6781 | const Expr *E, |
| 6782 | llvm::APSInt *Value, |
| 6783 | SourceLocation *Loc) { |
| 6784 | if (!E->getType()->isIntegralOrEnumerationType()) { |
| 6785 | if (Loc) *Loc = E->getExprLoc(); |
| 6786 | return false; |
| 6787 | } |
| 6788 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6789 | APValue Result; |
| 6790 | if (!E->isCXX11ConstantExpr(Ctx, &Result, Loc)) |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 6791 | return false; |
| 6792 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6793 | assert(Result.isInt() && "pointer cast to int is not an ICE"); |
| 6794 | if (Value) *Value = Result.getInt(); |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 6795 | return true; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6796 | } |
| 6797 | |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 6798 | bool Expr::isIntegerConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6799 | if (Ctx.getLangOpts().CPlusPlus11) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6800 | return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, 0, Loc); |
| 6801 | |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 6802 | ICEDiag D = CheckICE(this, Ctx); |
| 6803 | if (D.Kind != IK_ICE) { |
| 6804 | if (Loc) *Loc = D.Loc; |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6805 | return false; |
| 6806 | } |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6807 | return true; |
| 6808 | } |
| 6809 | |
| 6810 | bool Expr::isIntegerConstantExpr(llvm::APSInt &Value, ASTContext &Ctx, |
| 6811 | SourceLocation *Loc, bool isEvaluated) const { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6812 | if (Ctx.getLangOpts().CPlusPlus11) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6813 | return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, &Value, Loc); |
| 6814 | |
| 6815 | if (!isIntegerConstantExpr(Ctx, Loc)) |
| 6816 | return false; |
| 6817 | if (!EvaluateAsInt(Value, Ctx)) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6818 | llvm_unreachable("ICE cannot be evaluated!"); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6819 | return true; |
| 6820 | } |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6821 | |
Richard Smith | 70488e2 | 2012-02-14 21:38:30 +0000 | [diff] [blame] | 6822 | bool Expr::isCXX98IntegralConstantExpr(ASTContext &Ctx) const { |
Richard Smith | ceb59d9 | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 6823 | return CheckICE(this, Ctx).Kind == IK_ICE; |
Richard Smith | 70488e2 | 2012-02-14 21:38:30 +0000 | [diff] [blame] | 6824 | } |
| 6825 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6826 | bool Expr::isCXX11ConstantExpr(ASTContext &Ctx, APValue *Result, |
| 6827 | SourceLocation *Loc) const { |
| 6828 | // We support this checking in C++98 mode in order to diagnose compatibility |
| 6829 | // issues. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6830 | assert(Ctx.getLangOpts().CPlusPlus); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6831 | |
Richard Smith | 70488e2 | 2012-02-14 21:38:30 +0000 | [diff] [blame] | 6832 | // Build evaluation settings. |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6833 | Expr::EvalStatus Status; |
| 6834 | llvm::SmallVector<PartialDiagnosticAt, 8> Diags; |
| 6835 | Status.Diag = &Diags; |
| 6836 | EvalInfo Info(Ctx, Status); |
| 6837 | |
| 6838 | APValue Scratch; |
| 6839 | bool IsConstExpr = ::EvaluateAsRValue(Info, this, Result ? *Result : Scratch); |
| 6840 | |
| 6841 | if (!Diags.empty()) { |
| 6842 | IsConstExpr = false; |
| 6843 | if (Loc) *Loc = Diags[0].first; |
| 6844 | } else if (!IsConstExpr) { |
| 6845 | // FIXME: This shouldn't happen. |
| 6846 | if (Loc) *Loc = getExprLoc(); |
| 6847 | } |
| 6848 | |
| 6849 | return IsConstExpr; |
| 6850 | } |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6851 | |
| 6852 | bool Expr::isPotentialConstantExpr(const FunctionDecl *FD, |
| 6853 | llvm::SmallVectorImpl< |
| 6854 | PartialDiagnosticAt> &Diags) { |
| 6855 | // FIXME: It would be useful to check constexpr function templates, but at the |
| 6856 | // moment the constant expression evaluator cannot cope with the non-rigorous |
| 6857 | // ASTs which we build for dependent expressions. |
| 6858 | if (FD->isDependentContext()) |
| 6859 | return true; |
| 6860 | |
| 6861 | Expr::EvalStatus Status; |
| 6862 | Status.Diag = &Diags; |
| 6863 | |
| 6864 | EvalInfo Info(FD->getASTContext(), Status); |
| 6865 | Info.CheckingPotentialConstantExpression = true; |
| 6866 | |
| 6867 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
| 6868 | const CXXRecordDecl *RD = MD ? MD->getParent()->getCanonicalDecl() : 0; |
| 6869 | |
| 6870 | // FIXME: Fabricate an arbitrary expression on the stack and pretend that it |
| 6871 | // is a temporary being used as the 'this' pointer. |
| 6872 | LValue This; |
| 6873 | ImplicitValueInitExpr VIE(RD ? Info.Ctx.getRecordType(RD) : Info.Ctx.IntTy); |
Richard Smith | 83587db | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6874 | This.set(&VIE, Info.CurrentCall->Index); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6875 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6876 | ArrayRef<const Expr*> Args; |
| 6877 | |
| 6878 | SourceLocation Loc = FD->getLocation(); |
| 6879 | |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6880 | APValue Scratch; |
| 6881 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6882 | HandleConstructorCall(Loc, This, Args, CD, Info, Scratch); |
Richard Smith | 1aa0be8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6883 | else |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6884 | HandleFunctionCall(Loc, FD, (MD && MD->isInstance()) ? &This : 0, |
| 6885 | Args, FD->getBody(), Info, Scratch); |
| 6886 | |
| 6887 | return Diags.empty(); |
| 6888 | } |