Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1 | //===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===// |
Anders Carlsson | 7a241ba | 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 | 253c2a3 | 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 | 7a241ba | 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 | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 38 | #include "clang/AST/ASTDiagnostic.h" |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 39 | #include "clang/AST/CharUnits.h" |
Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 40 | #include "clang/AST/Expr.h" |
Anders Carlsson | 15b73de | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 41 | #include "clang/AST/RecordLayout.h" |
Seo Sanghyeon | 1904f44 | 2008-07-08 07:23:12 +0000 | [diff] [blame] | 42 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 43 | #include "clang/AST/TypeLoc.h" |
Chris Lattner | 15ba949 | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 44 | #include "clang/Basic/Builtins.h" |
Anders Carlsson | 374b93d | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 45 | #include "clang/Basic/TargetInfo.h" |
Mike Stump | b807c9c | 2009-05-30 14:43:18 +0000 | [diff] [blame] | 46 | #include "llvm/ADT/SmallString.h" |
Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 47 | #include "llvm/Support/raw_ostream.h" |
Mike Stump | 2346cd2 | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 48 | #include <cstring> |
Richard Smith | c804232 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 49 | #include <functional> |
Mike Stump | 2346cd2 | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 50 | |
Anders Carlsson | 7a241ba | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 51 | using namespace clang; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 52 | using llvm::APSInt; |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 53 | using llvm::APFloat; |
Anders Carlsson | 7a241ba | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 54 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 55 | static bool IsGlobalLValue(APValue::LValueBase B); |
| 56 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 57 | namespace { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 58 | struct LValue; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 59 | struct CallStackFrame; |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 60 | struct EvalInfo; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 61 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 62 | static QualType getType(APValue::LValueBase B) { |
Richard Smith | ce40ad6 | 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 | d62306a | 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 | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 70 | /// field or base class. |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 71 | static |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 72 | APValue::BaseOrMemberType getAsBaseOrMember(APValue::LValuePathEntry E) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 73 | APValue::BaseOrMemberType Value; |
| 74 | Value.setFromOpaqueValue(E.BaseOrMember); |
Richard Smith | 84f6dcf | 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 | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 80 | static const FieldDecl *getAsField(APValue::LValuePathEntry E) { |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 81 | return dyn_cast<FieldDecl>(getAsBaseOrMember(E).getPointer()); |
Richard Smith | d62306a | 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 | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 85 | static const CXXRecordDecl *getAsBaseClass(APValue::LValuePathEntry E) { |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 86 | return dyn_cast<CXXRecordDecl>(getAsBaseOrMember(E).getPointer()); |
Richard Smith | d62306a | 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 | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 90 | static bool isVirtualBaseClass(APValue::LValuePathEntry E) { |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 91 | return getAsBaseOrMember(E).getInt(); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Richard Smith | a8105bc | 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 | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 102 | for (unsigned I = 0, N = Path.size(); I != N; ++I) { |
Richard Smith | a8105bc | 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 | 66c9699 | 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 | a8105bc | 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 | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 119 | // Path[I] describes a base class. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 120 | ArraySize = 0; |
| 121 | } |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 122 | } |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 123 | return MostDerivedLength; |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 126 | // The order of this enum is important for diagnostics. |
| 127 | enum CheckSubobjectKind { |
Richard Smith | 47b3493 | 2012-02-01 02:39:43 +0000 | [diff] [blame] | 128 | CSK_Base, CSK_Derived, CSK_Field, CSK_ArrayToPointer, CSK_ArrayIndex, |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 129 | CSK_This, CSK_Real, CSK_Imag |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 130 | }; |
| 131 | |
Richard Smith | 96e0c10 | 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 | a8105bc | 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 | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 141 | |
Richard Smith | a8105bc | 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 | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 152 | |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 153 | typedef APValue::LValuePathEntry PathEntry; |
| 154 | |
Richard Smith | 96e0c10 | 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 | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 158 | SubobjectDesignator() : Invalid(true) {} |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 159 | |
Richard Smith | a8105bc | 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 | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 167 | if (!Invalid) { |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 168 | IsOnePastTheEnd = V.isLValueOnePastTheEnd(); |
Richard Smith | 8081560 | 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 | a8105bc | 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 | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 179 | void setInvalid() { |
| 180 | Invalid = true; |
| 181 | Entries.clear(); |
| 182 | } |
Richard Smith | a8105bc | 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 | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 206 | PathEntry Entry; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 207 | Entry.ArrayIndex = 0; |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 208 | Entries.push_back(Entry); |
Richard Smith | a8105bc | 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 | 96e0c10 | 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 | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 217 | void addDeclUnchecked(const Decl *D, bool Virtual = false) { |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 218 | PathEntry Entry; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 219 | APValue::BaseOrMemberType Value(D, Virtual); |
| 220 | Entry.BaseOrMember = Value.getOpaqueValue(); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 221 | Entries.push_back(Entry); |
Richard Smith | a8105bc | 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 | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 229 | } |
Richard Smith | 66c9699 | 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 | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 242 | void diagnosePointerArithmetic(EvalInfo &Info, const Expr *E, uint64_t N); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 243 | /// Add N to the address of this subobject. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 244 | void adjustIndex(EvalInfo &Info, const Expr *E, uint64_t N) { |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 245 | if (Invalid) return; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 246 | if (MostDerivedPathLength == Entries.size() && MostDerivedArraySize) { |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 247 | Entries.back().ArrayIndex += N; |
Richard Smith | a8105bc | 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 | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 252 | return; |
| 253 | } |
Richard Smith | a8105bc | 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 | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 263 | setInvalid(); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 264 | } |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 265 | } |
| 266 | }; |
| 267 | |
Richard Smith | 254a73d | 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 | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 273 | CallStackFrame *Caller; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 274 | |
Richard Smith | f6f003a | 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 | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 281 | /// Index - The call index of this call. |
| 282 | unsigned Index; |
| 283 | |
Richard Smith | d62306a | 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 | 254a73d | 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 | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 289 | APValue *Arguments; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 290 | |
Eli Friedman | 4830ec8 | 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. |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 293 | typedef std::map<const void*, APValue> MapTy; |
Richard Smith | 4e4c78ff | 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 | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 298 | CallStackFrame(EvalInfo &Info, SourceLocation CallLoc, |
| 299 | const FunctionDecl *Callee, const LValue *This, |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 300 | APValue *Arguments); |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 301 | ~CallStackFrame(); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 302 | }; |
| 303 | |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 304 | /// Temporarily override 'this'. |
| 305 | class ThisOverrideRAII { |
| 306 | public: |
| 307 | ThisOverrideRAII(CallStackFrame &Frame, const LValue *NewThis, bool Enable) |
| 308 | : Frame(Frame), OldThis(Frame.This) { |
| 309 | if (Enable) |
| 310 | Frame.This = NewThis; |
| 311 | } |
| 312 | ~ThisOverrideRAII() { |
| 313 | Frame.This = OldThis; |
| 314 | } |
| 315 | private: |
| 316 | CallStackFrame &Frame; |
| 317 | const LValue *OldThis; |
| 318 | }; |
| 319 | |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 320 | /// A partial diagnostic which we might know in advance that we are not going |
| 321 | /// to emit. |
| 322 | class OptionalDiagnostic { |
| 323 | PartialDiagnostic *Diag; |
| 324 | |
| 325 | public: |
| 326 | explicit OptionalDiagnostic(PartialDiagnostic *Diag = 0) : Diag(Diag) {} |
| 327 | |
| 328 | template<typename T> |
| 329 | OptionalDiagnostic &operator<<(const T &v) { |
| 330 | if (Diag) |
| 331 | *Diag << v; |
| 332 | return *this; |
| 333 | } |
Richard Smith | fe80003 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 334 | |
| 335 | OptionalDiagnostic &operator<<(const APSInt &I) { |
| 336 | if (Diag) { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 337 | SmallVector<char, 32> Buffer; |
Richard Smith | fe80003 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 338 | I.toString(Buffer); |
| 339 | *Diag << StringRef(Buffer.data(), Buffer.size()); |
| 340 | } |
| 341 | return *this; |
| 342 | } |
| 343 | |
| 344 | OptionalDiagnostic &operator<<(const APFloat &F) { |
| 345 | if (Diag) { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 346 | SmallVector<char, 32> Buffer; |
Richard Smith | fe80003 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 347 | F.toString(Buffer); |
| 348 | *Diag << StringRef(Buffer.data(), Buffer.size()); |
| 349 | } |
| 350 | return *this; |
| 351 | } |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 352 | }; |
| 353 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 354 | /// EvalInfo - This is a private struct used by the evaluator to capture |
| 355 | /// information about a subexpression as it is folded. It retains information |
| 356 | /// about the AST context, but also maintains information about the folded |
| 357 | /// expression. |
| 358 | /// |
| 359 | /// If an expression could be evaluated, it is still possible it is not a C |
| 360 | /// "integer constant expression" or constant expression. If not, this struct |
| 361 | /// captures information about how and why not. |
| 362 | /// |
| 363 | /// One bit of information passed *into* the request for constant folding |
| 364 | /// indicates whether the subexpression is "evaluated" or not according to C |
| 365 | /// rules. For example, the RHS of (0 && foo()) is not evaluated. We can |
| 366 | /// evaluate the expression regardless of what the RHS is, but C only allows |
| 367 | /// certain things in certain situations. |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 368 | struct EvalInfo { |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 369 | ASTContext &Ctx; |
Argyrios Kyrtzidis | 91d0098 | 2012-02-27 20:21:34 +0000 | [diff] [blame] | 370 | |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 371 | /// EvalStatus - Contains information about the evaluation. |
| 372 | Expr::EvalStatus &EvalStatus; |
| 373 | |
| 374 | /// CurrentCall - The top of the constexpr call stack. |
| 375 | CallStackFrame *CurrentCall; |
| 376 | |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 377 | /// CallStackDepth - The number of calls in the call stack right now. |
| 378 | unsigned CallStackDepth; |
| 379 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 380 | /// NextCallIndex - The next call index to assign. |
| 381 | unsigned NextCallIndex; |
| 382 | |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 383 | /// BottomFrame - The frame in which evaluation started. This must be |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 384 | /// initialized after CurrentCall and CallStackDepth. |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 385 | CallStackFrame BottomFrame; |
| 386 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 387 | /// EvaluatingDecl - This is the declaration whose initializer is being |
| 388 | /// evaluated, if any. |
| 389 | const VarDecl *EvaluatingDecl; |
| 390 | |
| 391 | /// EvaluatingDeclValue - This is the value being constructed for the |
| 392 | /// declaration whose initializer is being evaluated, if any. |
| 393 | APValue *EvaluatingDeclValue; |
| 394 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 395 | /// HasActiveDiagnostic - Was the previous diagnostic stored? If so, further |
| 396 | /// notes attached to it will also be stored, otherwise they will not be. |
| 397 | bool HasActiveDiagnostic; |
| 398 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 399 | /// CheckingPotentialConstantExpression - Are we checking whether the |
| 400 | /// expression is a potential constant expression? If so, some diagnostics |
| 401 | /// are suppressed. |
| 402 | bool CheckingPotentialConstantExpression; |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 403 | |
| 404 | bool IntOverflowCheckMode; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 405 | |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 406 | EvalInfo(const ASTContext &C, Expr::EvalStatus &S, |
| 407 | bool OverflowCheckMode=false) |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 408 | : Ctx(const_cast<ASTContext&>(C)), EvalStatus(S), CurrentCall(0), |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 409 | CallStackDepth(0), NextCallIndex(1), |
| 410 | BottomFrame(*this, SourceLocation(), 0, 0, 0), |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 411 | EvaluatingDecl(0), EvaluatingDeclValue(0), HasActiveDiagnostic(false), |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 412 | CheckingPotentialConstantExpression(false), |
| 413 | IntOverflowCheckMode(OverflowCheckMode) {} |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 414 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 415 | void setEvaluatingDecl(const VarDecl *VD, APValue &Value) { |
| 416 | EvaluatingDecl = VD; |
| 417 | EvaluatingDeclValue = &Value; |
| 418 | } |
| 419 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 420 | const LangOptions &getLangOpts() const { return Ctx.getLangOpts(); } |
Richard Smith | 9a56882 | 2011-11-21 19:36:32 +0000 | [diff] [blame] | 421 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 422 | bool CheckCallLimit(SourceLocation Loc) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 423 | // Don't perform any constexpr calls (other than the call we're checking) |
| 424 | // when checking a potential constant expression. |
| 425 | if (CheckingPotentialConstantExpression && CallStackDepth > 1) |
| 426 | return false; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 427 | if (NextCallIndex == 0) { |
| 428 | // NextCallIndex has wrapped around. |
| 429 | Diag(Loc, diag::note_constexpr_call_limit_exceeded); |
| 430 | return false; |
| 431 | } |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 432 | if (CallStackDepth <= getLangOpts().ConstexprCallDepth) |
| 433 | return true; |
| 434 | Diag(Loc, diag::note_constexpr_depth_limit_exceeded) |
| 435 | << getLangOpts().ConstexprCallDepth; |
| 436 | return false; |
Richard Smith | 9a56882 | 2011-11-21 19:36:32 +0000 | [diff] [blame] | 437 | } |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 438 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 439 | CallStackFrame *getCallFrame(unsigned CallIndex) { |
| 440 | assert(CallIndex && "no call index in getCallFrame"); |
| 441 | // We will eventually hit BottomFrame, which has Index 1, so Frame can't |
| 442 | // be null in this loop. |
| 443 | CallStackFrame *Frame = CurrentCall; |
| 444 | while (Frame->Index > CallIndex) |
| 445 | Frame = Frame->Caller; |
| 446 | return (Frame->Index == CallIndex) ? Frame : 0; |
| 447 | } |
| 448 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 449 | private: |
| 450 | /// Add a diagnostic to the diagnostics list. |
| 451 | PartialDiagnostic &addDiag(SourceLocation Loc, diag::kind DiagId) { |
| 452 | PartialDiagnostic PD(DiagId, Ctx.getDiagAllocator()); |
| 453 | EvalStatus.Diag->push_back(std::make_pair(Loc, PD)); |
| 454 | return EvalStatus.Diag->back().second; |
| 455 | } |
| 456 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 457 | /// Add notes containing a call stack to the current point of evaluation. |
| 458 | void addCallStack(unsigned Limit); |
| 459 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 460 | public: |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 461 | /// Diagnose that the evaluation cannot be folded. |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 462 | OptionalDiagnostic Diag(SourceLocation Loc, diag::kind DiagId |
| 463 | = diag::note_invalid_subexpr_in_const_expr, |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 464 | unsigned ExtraNotes = 0) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 465 | // If we have a prior diagnostic, it will be noting that the expression |
| 466 | // isn't a constant expression. This diagnostic is more important. |
| 467 | // FIXME: We might want to show both diagnostics to the user. |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 468 | if (EvalStatus.Diag) { |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 469 | unsigned CallStackNotes = CallStackDepth - 1; |
| 470 | unsigned Limit = Ctx.getDiagnostics().getConstexprBacktraceLimit(); |
| 471 | if (Limit) |
| 472 | CallStackNotes = std::min(CallStackNotes, Limit + 1); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 473 | if (CheckingPotentialConstantExpression) |
| 474 | CallStackNotes = 0; |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 475 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 476 | HasActiveDiagnostic = true; |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 477 | EvalStatus.Diag->clear(); |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 478 | EvalStatus.Diag->reserve(1 + ExtraNotes + CallStackNotes); |
| 479 | addDiag(Loc, DiagId); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 480 | if (!CheckingPotentialConstantExpression) |
| 481 | addCallStack(Limit); |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 482 | return OptionalDiagnostic(&(*EvalStatus.Diag)[0].second); |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 483 | } |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 484 | HasActiveDiagnostic = false; |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 485 | return OptionalDiagnostic(); |
| 486 | } |
| 487 | |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 488 | OptionalDiagnostic Diag(const Expr *E, diag::kind DiagId |
| 489 | = diag::note_invalid_subexpr_in_const_expr, |
| 490 | unsigned ExtraNotes = 0) { |
| 491 | if (EvalStatus.Diag) |
| 492 | return Diag(E->getExprLoc(), DiagId, ExtraNotes); |
| 493 | HasActiveDiagnostic = false; |
| 494 | return OptionalDiagnostic(); |
| 495 | } |
| 496 | |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 497 | bool getIntOverflowCheckMode() { return IntOverflowCheckMode; } |
| 498 | |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 499 | /// Diagnose that the evaluation does not produce a C++11 core constant |
| 500 | /// expression. |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 501 | template<typename LocArg> |
| 502 | OptionalDiagnostic CCEDiag(LocArg Loc, diag::kind DiagId |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 503 | = diag::note_invalid_subexpr_in_const_expr, |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 504 | unsigned ExtraNotes = 0) { |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 505 | // Don't override a previous diagnostic. |
Eli Friedman | ebea9af | 2012-02-21 22:41:33 +0000 | [diff] [blame] | 506 | if (!EvalStatus.Diag || !EvalStatus.Diag->empty()) { |
| 507 | HasActiveDiagnostic = false; |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 508 | return OptionalDiagnostic(); |
Eli Friedman | ebea9af | 2012-02-21 22:41:33 +0000 | [diff] [blame] | 509 | } |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 510 | return Diag(Loc, DiagId, ExtraNotes); |
| 511 | } |
| 512 | |
| 513 | /// Add a note to a prior diagnostic. |
| 514 | OptionalDiagnostic Note(SourceLocation Loc, diag::kind DiagId) { |
| 515 | if (!HasActiveDiagnostic) |
| 516 | return OptionalDiagnostic(); |
| 517 | return OptionalDiagnostic(&addDiag(Loc, DiagId)); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 518 | } |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 519 | |
| 520 | /// Add a stack of notes to a prior diagnostic. |
| 521 | void addNotes(ArrayRef<PartialDiagnosticAt> Diags) { |
| 522 | if (HasActiveDiagnostic) { |
| 523 | EvalStatus.Diag->insert(EvalStatus.Diag->end(), |
| 524 | Diags.begin(), Diags.end()); |
| 525 | } |
| 526 | } |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 527 | |
| 528 | /// Should we continue evaluation as much as possible after encountering a |
| 529 | /// construct which can't be folded? |
| 530 | bool keepEvaluatingAfterFailure() { |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 531 | // Should return true in IntOverflowCheckMode, so that we check for |
| 532 | // overflow even if some subexpressions can't be evaluated as constants. |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 533 | return IntOverflowCheckMode || |
| 534 | (CheckingPotentialConstantExpression && |
| 535 | EvalStatus.Diag && EvalStatus.Diag->empty()); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 536 | } |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 537 | }; |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 538 | |
| 539 | /// Object used to treat all foldable expressions as constant expressions. |
| 540 | struct FoldConstant { |
| 541 | bool Enabled; |
| 542 | |
| 543 | explicit FoldConstant(EvalInfo &Info) |
| 544 | : Enabled(Info.EvalStatus.Diag && Info.EvalStatus.Diag->empty() && |
| 545 | !Info.EvalStatus.HasSideEffects) { |
| 546 | } |
| 547 | // Treat the value we've computed since this object was created as constant. |
| 548 | void Fold(EvalInfo &Info) { |
| 549 | if (Enabled && !Info.EvalStatus.Diag->empty() && |
| 550 | !Info.EvalStatus.HasSideEffects) |
| 551 | Info.EvalStatus.Diag->clear(); |
| 552 | } |
| 553 | }; |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 554 | |
| 555 | /// RAII object used to suppress diagnostics and side-effects from a |
| 556 | /// speculative evaluation. |
| 557 | class SpeculativeEvaluationRAII { |
| 558 | EvalInfo &Info; |
| 559 | Expr::EvalStatus Old; |
| 560 | |
| 561 | public: |
| 562 | SpeculativeEvaluationRAII(EvalInfo &Info, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 563 | SmallVectorImpl<PartialDiagnosticAt> *NewDiag = 0) |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 564 | : Info(Info), Old(Info.EvalStatus) { |
| 565 | Info.EvalStatus.Diag = NewDiag; |
| 566 | } |
| 567 | ~SpeculativeEvaluationRAII() { |
| 568 | Info.EvalStatus = Old; |
| 569 | } |
| 570 | }; |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 571 | } |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 572 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 573 | bool SubobjectDesignator::checkSubobject(EvalInfo &Info, const Expr *E, |
| 574 | CheckSubobjectKind CSK) { |
| 575 | if (Invalid) |
| 576 | return false; |
| 577 | if (isOnePastTheEnd()) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 578 | Info.CCEDiag(E, diag::note_constexpr_past_end_subobject) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 579 | << CSK; |
| 580 | setInvalid(); |
| 581 | return false; |
| 582 | } |
| 583 | return true; |
| 584 | } |
| 585 | |
| 586 | void SubobjectDesignator::diagnosePointerArithmetic(EvalInfo &Info, |
| 587 | const Expr *E, uint64_t N) { |
| 588 | if (MostDerivedPathLength == Entries.size() && MostDerivedArraySize) |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 589 | Info.CCEDiag(E, diag::note_constexpr_array_index) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 590 | << static_cast<int>(N) << /*array*/ 0 |
| 591 | << static_cast<unsigned>(MostDerivedArraySize); |
| 592 | else |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 593 | Info.CCEDiag(E, diag::note_constexpr_array_index) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 594 | << static_cast<int>(N) << /*non-array*/ 1; |
| 595 | setInvalid(); |
| 596 | } |
| 597 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 598 | CallStackFrame::CallStackFrame(EvalInfo &Info, SourceLocation CallLoc, |
| 599 | const FunctionDecl *Callee, const LValue *This, |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 600 | APValue *Arguments) |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 601 | : Info(Info), Caller(Info.CurrentCall), CallLoc(CallLoc), Callee(Callee), |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 602 | Index(Info.NextCallIndex++), This(This), Arguments(Arguments) { |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 603 | Info.CurrentCall = this; |
| 604 | ++Info.CallStackDepth; |
| 605 | } |
| 606 | |
| 607 | CallStackFrame::~CallStackFrame() { |
| 608 | assert(Info.CurrentCall == this && "calls retired out of order"); |
| 609 | --Info.CallStackDepth; |
| 610 | Info.CurrentCall = Caller; |
| 611 | } |
| 612 | |
| 613 | /// Produce a string describing the given constexpr call. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 614 | static void describeCall(CallStackFrame *Frame, raw_ostream &Out) { |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 615 | unsigned ArgIndex = 0; |
| 616 | bool IsMemberCall = isa<CXXMethodDecl>(Frame->Callee) && |
Richard Smith | 74388b4 | 2012-02-04 00:33:54 +0000 | [diff] [blame] | 617 | !isa<CXXConstructorDecl>(Frame->Callee) && |
| 618 | cast<CXXMethodDecl>(Frame->Callee)->isInstance(); |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 619 | |
| 620 | if (!IsMemberCall) |
| 621 | Out << *Frame->Callee << '('; |
| 622 | |
| 623 | for (FunctionDecl::param_const_iterator I = Frame->Callee->param_begin(), |
| 624 | E = Frame->Callee->param_end(); I != E; ++I, ++ArgIndex) { |
NAKAMURA Takumi | b8efa1e | 2012-01-26 09:37:36 +0000 | [diff] [blame] | 625 | if (ArgIndex > (unsigned)IsMemberCall) |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 626 | Out << ", "; |
| 627 | |
| 628 | const ParmVarDecl *Param = *I; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 629 | const APValue &Arg = Frame->Arguments[ArgIndex]; |
| 630 | Arg.printPretty(Out, Frame->Info.Ctx, Param->getType()); |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 631 | |
| 632 | if (ArgIndex == 0 && IsMemberCall) |
| 633 | Out << "->" << *Frame->Callee << '('; |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 634 | } |
| 635 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 636 | Out << ')'; |
| 637 | } |
| 638 | |
| 639 | void EvalInfo::addCallStack(unsigned Limit) { |
| 640 | // Determine which calls to skip, if any. |
| 641 | unsigned ActiveCalls = CallStackDepth - 1; |
| 642 | unsigned SkipStart = ActiveCalls, SkipEnd = SkipStart; |
| 643 | if (Limit && Limit < ActiveCalls) { |
| 644 | SkipStart = Limit / 2 + Limit % 2; |
| 645 | SkipEnd = ActiveCalls - Limit / 2; |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 648 | // Walk the call stack and add the diagnostics. |
| 649 | unsigned CallIdx = 0; |
| 650 | for (CallStackFrame *Frame = CurrentCall; Frame != &BottomFrame; |
| 651 | Frame = Frame->Caller, ++CallIdx) { |
| 652 | // Skip this call? |
| 653 | if (CallIdx >= SkipStart && CallIdx < SkipEnd) { |
| 654 | if (CallIdx == SkipStart) { |
| 655 | // Note that we're skipping calls. |
| 656 | addDiag(Frame->CallLoc, diag::note_constexpr_calls_suppressed) |
| 657 | << unsigned(ActiveCalls - Limit); |
| 658 | } |
| 659 | continue; |
| 660 | } |
| 661 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 662 | SmallVector<char, 128> Buffer; |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 663 | llvm::raw_svector_ostream Out(Buffer); |
| 664 | describeCall(Frame, Out); |
| 665 | addDiag(Frame->CallLoc, diag::note_constexpr_call_here) << Out.str(); |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | namespace { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 670 | struct ComplexValue { |
| 671 | private: |
| 672 | bool IsInt; |
| 673 | |
| 674 | public: |
| 675 | APSInt IntReal, IntImag; |
| 676 | APFloat FloatReal, FloatImag; |
| 677 | |
| 678 | ComplexValue() : FloatReal(APFloat::Bogus), FloatImag(APFloat::Bogus) {} |
| 679 | |
| 680 | void makeComplexFloat() { IsInt = false; } |
| 681 | bool isComplexFloat() const { return !IsInt; } |
| 682 | APFloat &getComplexFloatReal() { return FloatReal; } |
| 683 | APFloat &getComplexFloatImag() { return FloatImag; } |
| 684 | |
| 685 | void makeComplexInt() { IsInt = true; } |
| 686 | bool isComplexInt() const { return IsInt; } |
| 687 | APSInt &getComplexIntReal() { return IntReal; } |
| 688 | APSInt &getComplexIntImag() { return IntImag; } |
| 689 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 690 | void moveInto(APValue &v) const { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 691 | if (isComplexFloat()) |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 692 | v = APValue(FloatReal, FloatImag); |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 693 | else |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 694 | v = APValue(IntReal, IntImag); |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 695 | } |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 696 | void setFrom(const APValue &v) { |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 697 | assert(v.isComplexFloat() || v.isComplexInt()); |
| 698 | if (v.isComplexFloat()) { |
| 699 | makeComplexFloat(); |
| 700 | FloatReal = v.getComplexFloatReal(); |
| 701 | FloatImag = v.getComplexFloatImag(); |
| 702 | } else { |
| 703 | makeComplexInt(); |
| 704 | IntReal = v.getComplexIntReal(); |
| 705 | IntImag = v.getComplexIntImag(); |
| 706 | } |
| 707 | } |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 708 | }; |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 709 | |
| 710 | struct LValue { |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 711 | APValue::LValueBase Base; |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 712 | CharUnits Offset; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 713 | unsigned CallIndex; |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 714 | SubobjectDesignator Designator; |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 715 | |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 716 | const APValue::LValueBase getLValueBase() const { return Base; } |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 717 | CharUnits &getLValueOffset() { return Offset; } |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 718 | const CharUnits &getLValueOffset() const { return Offset; } |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 719 | unsigned getLValueCallIndex() const { return CallIndex; } |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 720 | SubobjectDesignator &getLValueDesignator() { return Designator; } |
| 721 | const SubobjectDesignator &getLValueDesignator() const { return Designator;} |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 722 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 723 | void moveInto(APValue &V) const { |
| 724 | if (Designator.Invalid) |
| 725 | V = APValue(Base, Offset, APValue::NoLValuePath(), CallIndex); |
| 726 | else |
| 727 | V = APValue(Base, Offset, Designator.Entries, |
| 728 | Designator.IsOnePastTheEnd, CallIndex); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 729 | } |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 730 | void setFrom(ASTContext &Ctx, const APValue &V) { |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 731 | assert(V.isLValue()); |
| 732 | Base = V.getLValueBase(); |
| 733 | Offset = V.getLValueOffset(); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 734 | CallIndex = V.getLValueCallIndex(); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 735 | Designator = SubobjectDesignator(Ctx, V); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 736 | } |
| 737 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 738 | void set(APValue::LValueBase B, unsigned I = 0) { |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 739 | Base = B; |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 740 | Offset = CharUnits::Zero(); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 741 | CallIndex = I; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 742 | Designator = SubobjectDesignator(getType(B)); |
| 743 | } |
| 744 | |
| 745 | // Check that this LValue is not based on a null pointer. If it is, produce |
| 746 | // a diagnostic and mark the designator as invalid. |
| 747 | bool checkNullPointer(EvalInfo &Info, const Expr *E, |
| 748 | CheckSubobjectKind CSK) { |
| 749 | if (Designator.Invalid) |
| 750 | return false; |
| 751 | if (!Base) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 752 | Info.CCEDiag(E, diag::note_constexpr_null_subobject) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 753 | << CSK; |
| 754 | Designator.setInvalid(); |
| 755 | return false; |
| 756 | } |
| 757 | return true; |
| 758 | } |
| 759 | |
| 760 | // Check this LValue refers to an object. If not, set the designator to be |
| 761 | // invalid and emit a diagnostic. |
| 762 | bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 763 | // Outside C++11, do not build a designator referring to a subobject of |
| 764 | // any object: we won't use such a designator for anything. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 765 | if (!Info.getLangOpts().CPlusPlus11) |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 766 | Designator.setInvalid(); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 767 | return checkNullPointer(Info, E, CSK) && |
| 768 | Designator.checkSubobject(Info, E, CSK); |
| 769 | } |
| 770 | |
| 771 | void addDecl(EvalInfo &Info, const Expr *E, |
| 772 | const Decl *D, bool Virtual = false) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 773 | if (checkSubobject(Info, E, isa<FieldDecl>(D) ? CSK_Field : CSK_Base)) |
| 774 | Designator.addDeclUnchecked(D, Virtual); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 775 | } |
| 776 | void addArray(EvalInfo &Info, const Expr *E, const ConstantArrayType *CAT) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 777 | if (checkSubobject(Info, E, CSK_ArrayToPointer)) |
| 778 | Designator.addArrayUnchecked(CAT); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 779 | } |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 780 | void addComplex(EvalInfo &Info, const Expr *E, QualType EltTy, bool Imag) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 781 | if (checkSubobject(Info, E, Imag ? CSK_Imag : CSK_Real)) |
| 782 | Designator.addComplexUnchecked(EltTy, Imag); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 783 | } |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 784 | void adjustIndex(EvalInfo &Info, const Expr *E, uint64_t N) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 785 | if (checkNullPointer(Info, E, CSK_ArrayIndex)) |
| 786 | Designator.adjustIndex(Info, E, N); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 787 | } |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 788 | }; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 789 | |
| 790 | struct MemberPtr { |
| 791 | MemberPtr() {} |
| 792 | explicit MemberPtr(const ValueDecl *Decl) : |
| 793 | DeclAndIsDerivedMember(Decl, false), Path() {} |
| 794 | |
| 795 | /// The member or (direct or indirect) field referred to by this member |
| 796 | /// pointer, or 0 if this is a null member pointer. |
| 797 | const ValueDecl *getDecl() const { |
| 798 | return DeclAndIsDerivedMember.getPointer(); |
| 799 | } |
| 800 | /// Is this actually a member of some type derived from the relevant class? |
| 801 | bool isDerivedMember() const { |
| 802 | return DeclAndIsDerivedMember.getInt(); |
| 803 | } |
| 804 | /// Get the class which the declaration actually lives in. |
| 805 | const CXXRecordDecl *getContainingRecord() const { |
| 806 | return cast<CXXRecordDecl>( |
| 807 | DeclAndIsDerivedMember.getPointer()->getDeclContext()); |
| 808 | } |
| 809 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 810 | void moveInto(APValue &V) const { |
| 811 | V = APValue(getDecl(), isDerivedMember(), Path); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 812 | } |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 813 | void setFrom(const APValue &V) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 814 | assert(V.isMemberPointer()); |
| 815 | DeclAndIsDerivedMember.setPointer(V.getMemberPointerDecl()); |
| 816 | DeclAndIsDerivedMember.setInt(V.isMemberPointerToDerivedMember()); |
| 817 | Path.clear(); |
| 818 | ArrayRef<const CXXRecordDecl*> P = V.getMemberPointerPath(); |
| 819 | Path.insert(Path.end(), P.begin(), P.end()); |
| 820 | } |
| 821 | |
| 822 | /// DeclAndIsDerivedMember - The member declaration, and a flag indicating |
| 823 | /// whether the member is a member of some class derived from the class type |
| 824 | /// of the member pointer. |
| 825 | llvm::PointerIntPair<const ValueDecl*, 1, bool> DeclAndIsDerivedMember; |
| 826 | /// Path - The path of base/derived classes from the member declaration's |
| 827 | /// class (exclusive) to the class type of the member pointer (inclusive). |
| 828 | SmallVector<const CXXRecordDecl*, 4> Path; |
| 829 | |
| 830 | /// Perform a cast towards the class of the Decl (either up or down the |
| 831 | /// hierarchy). |
| 832 | bool castBack(const CXXRecordDecl *Class) { |
| 833 | assert(!Path.empty()); |
| 834 | const CXXRecordDecl *Expected; |
| 835 | if (Path.size() >= 2) |
| 836 | Expected = Path[Path.size() - 2]; |
| 837 | else |
| 838 | Expected = getContainingRecord(); |
| 839 | if (Expected->getCanonicalDecl() != Class->getCanonicalDecl()) { |
| 840 | // C++11 [expr.static.cast]p12: In a conversion from (D::*) to (B::*), |
| 841 | // if B does not contain the original member and is not a base or |
| 842 | // derived class of the class containing the original member, the result |
| 843 | // of the cast is undefined. |
| 844 | // C++11 [conv.mem]p2 does not cover this case for a cast from (B::*) to |
| 845 | // (D::*). We consider that to be a language defect. |
| 846 | return false; |
| 847 | } |
| 848 | Path.pop_back(); |
| 849 | return true; |
| 850 | } |
| 851 | /// Perform a base-to-derived member pointer cast. |
| 852 | bool castToDerived(const CXXRecordDecl *Derived) { |
| 853 | if (!getDecl()) |
| 854 | return true; |
| 855 | if (!isDerivedMember()) { |
| 856 | Path.push_back(Derived); |
| 857 | return true; |
| 858 | } |
| 859 | if (!castBack(Derived)) |
| 860 | return false; |
| 861 | if (Path.empty()) |
| 862 | DeclAndIsDerivedMember.setInt(false); |
| 863 | return true; |
| 864 | } |
| 865 | /// Perform a derived-to-base member pointer cast. |
| 866 | bool castToBase(const CXXRecordDecl *Base) { |
| 867 | if (!getDecl()) |
| 868 | return true; |
| 869 | if (Path.empty()) |
| 870 | DeclAndIsDerivedMember.setInt(true); |
| 871 | if (isDerivedMember()) { |
| 872 | Path.push_back(Base); |
| 873 | return true; |
| 874 | } |
| 875 | return castBack(Base); |
| 876 | } |
| 877 | }; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 878 | |
Richard Smith | 7bb0067 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 879 | /// Compare two member pointers, which are assumed to be of the same type. |
| 880 | static bool operator==(const MemberPtr &LHS, const MemberPtr &RHS) { |
| 881 | if (!LHS.getDecl() || !RHS.getDecl()) |
| 882 | return !LHS.getDecl() && !RHS.getDecl(); |
| 883 | if (LHS.getDecl()->getCanonicalDecl() != RHS.getDecl()->getCanonicalDecl()) |
| 884 | return false; |
| 885 | return LHS.Path == RHS.Path; |
| 886 | } |
| 887 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 888 | /// Kinds of constant expression checking, for diagnostics. |
| 889 | enum CheckConstantExpressionKind { |
| 890 | CCEK_Constant, ///< A normal constant. |
| 891 | CCEK_ReturnValue, ///< A constexpr function return value. |
| 892 | CCEK_MemberInit ///< A constexpr constructor mem-initializer. |
| 893 | }; |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 894 | } |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 895 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 896 | static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 897 | static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, |
| 898 | const LValue &This, const Expr *E, |
| 899 | CheckConstantExpressionKind CCEK = CCEK_Constant, |
| 900 | bool AllowNonLiteralTypes = false); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 901 | static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info); |
| 902 | static bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 903 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
| 904 | EvalInfo &Info); |
| 905 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info); |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 906 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 907 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, |
Chris Lattner | 6c4d255 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 908 | EvalInfo &Info); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 909 | static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info); |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 910 | static bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 911 | |
| 912 | //===----------------------------------------------------------------------===// |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 913 | // Misc utilities |
| 914 | //===----------------------------------------------------------------------===// |
| 915 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 916 | /// Evaluate an expression to see if it had side-effects, and discard its |
| 917 | /// result. |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 918 | /// \return \c true if the caller should keep evaluating. |
| 919 | static bool EvaluateIgnoredValue(EvalInfo &Info, const Expr *E) { |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 920 | APValue Scratch; |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 921 | if (!Evaluate(Scratch, Info, E)) { |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 922 | Info.EvalStatus.HasSideEffects = true; |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 923 | return Info.keepEvaluatingAfterFailure(); |
| 924 | } |
| 925 | return true; |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 926 | } |
| 927 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 928 | /// Should this call expression be treated as a string literal? |
| 929 | static bool IsStringLiteralCall(const CallExpr *E) { |
| 930 | unsigned Builtin = E->isBuiltinCall(); |
| 931 | return (Builtin == Builtin::BI__builtin___CFStringMakeConstantString || |
| 932 | Builtin == Builtin::BI__builtin___NSStringMakeConstantString); |
| 933 | } |
| 934 | |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 935 | static bool IsGlobalLValue(APValue::LValueBase B) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 936 | // C++11 [expr.const]p3 An address constant expression is a prvalue core |
| 937 | // constant expression of pointer type that evaluates to... |
| 938 | |
| 939 | // ... a null pointer value, or a prvalue core constant expression of type |
| 940 | // std::nullptr_t. |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 941 | if (!B) return true; |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 942 | |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 943 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
| 944 | // ... the address of an object with static storage duration, |
| 945 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 946 | return VD->hasGlobalStorage(); |
| 947 | // ... the address of a function, |
| 948 | return isa<FunctionDecl>(D); |
| 949 | } |
| 950 | |
| 951 | const Expr *E = B.get<const Expr*>(); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 952 | switch (E->getStmtClass()) { |
| 953 | default: |
| 954 | return false; |
Richard Smith | 0dea49e | 2012-02-18 04:58:18 +0000 | [diff] [blame] | 955 | case Expr::CompoundLiteralExprClass: { |
| 956 | const CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E); |
| 957 | return CLE->isFileScope() && CLE->isLValue(); |
| 958 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 959 | // A string literal has static storage duration. |
| 960 | case Expr::StringLiteralClass: |
| 961 | case Expr::PredefinedExprClass: |
| 962 | case Expr::ObjCStringLiteralClass: |
| 963 | case Expr::ObjCEncodeExprClass: |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 964 | case Expr::CXXTypeidExprClass: |
Francois Pichet | 0066db9 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 965 | case Expr::CXXUuidofExprClass: |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 966 | return true; |
| 967 | case Expr::CallExprClass: |
| 968 | return IsStringLiteralCall(cast<CallExpr>(E)); |
| 969 | // For GCC compatibility, &&label has static storage duration. |
| 970 | case Expr::AddrLabelExprClass: |
| 971 | return true; |
| 972 | // A Block literal expression may be used as the initialization value for |
| 973 | // Block variables at global or local static scope. |
| 974 | case Expr::BlockExprClass: |
| 975 | return !cast<BlockExpr>(E)->getBlockDecl()->hasCaptures(); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 976 | case Expr::ImplicitValueInitExprClass: |
| 977 | // FIXME: |
| 978 | // We can never form an lvalue with an implicit value initialization as its |
| 979 | // base through expression evaluation, so these only appear in one case: the |
| 980 | // implicit variable declaration we invent when checking whether a constexpr |
| 981 | // constructor can produce a constant expression. We must assume that such |
| 982 | // an expression might be a global lvalue. |
| 983 | return true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 984 | } |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 985 | } |
| 986 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 987 | static void NoteLValueLocation(EvalInfo &Info, APValue::LValueBase Base) { |
| 988 | assert(Base && "no location for a null lvalue"); |
| 989 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
| 990 | if (VD) |
| 991 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 992 | else |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 993 | Info.Note(Base.get<const Expr*>()->getExprLoc(), |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 994 | diag::note_constexpr_temporary_here); |
| 995 | } |
| 996 | |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 997 | /// Check that this reference or pointer core constant expression is a valid |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 998 | /// value for an address or reference constant expression. Return true if we |
| 999 | /// can fold this expression, whether or not it's a constant expression. |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1000 | static bool CheckLValueConstantExpression(EvalInfo &Info, SourceLocation Loc, |
| 1001 | QualType Type, const LValue &LVal) { |
| 1002 | bool IsReferenceType = Type->isReferenceType(); |
| 1003 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1004 | APValue::LValueBase Base = LVal.getLValueBase(); |
| 1005 | const SubobjectDesignator &Designator = LVal.getLValueDesignator(); |
| 1006 | |
Richard Smith | 0dea49e | 2012-02-18 04:58:18 +0000 | [diff] [blame] | 1007 | // Check that the object is a global. Note that the fake 'this' object we |
| 1008 | // manufacture when checking potential constant expressions is conservatively |
| 1009 | // assumed to be global here. |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1010 | if (!IsGlobalLValue(Base)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1011 | if (Info.getLangOpts().CPlusPlus11) { |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1012 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1013 | Info.Diag(Loc, diag::note_constexpr_non_global, 1) |
| 1014 | << IsReferenceType << !Designator.Entries.empty() |
| 1015 | << !!VD << VD; |
| 1016 | NoteLValueLocation(Info, Base); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1017 | } else { |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1018 | Info.Diag(Loc); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1019 | } |
Richard Smith | 02ab9c2 | 2012-01-12 06:08:57 +0000 | [diff] [blame] | 1020 | // Don't allow references to temporaries to escape. |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1021 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1022 | } |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1023 | assert((Info.CheckingPotentialConstantExpression || |
| 1024 | LVal.getLValueCallIndex() == 0) && |
| 1025 | "have call index for global lvalue"); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1026 | |
Hans Wennborg | cb9ad99 | 2012-08-29 18:27:29 +0000 | [diff] [blame] | 1027 | // Check if this is a thread-local variable. |
| 1028 | if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) { |
| 1029 | if (const VarDecl *Var = dyn_cast<const VarDecl>(VD)) { |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 1030 | if (Var->getTLSKind()) |
Hans Wennborg | cb9ad99 | 2012-08-29 18:27:29 +0000 | [diff] [blame] | 1031 | return false; |
| 1032 | } |
| 1033 | } |
| 1034 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1035 | // Allow address constant expressions to be past-the-end pointers. This is |
| 1036 | // an extension: the standard requires them to point to an object. |
| 1037 | if (!IsReferenceType) |
| 1038 | return true; |
| 1039 | |
| 1040 | // A reference constant expression must refer to an object. |
| 1041 | if (!Base) { |
| 1042 | // FIXME: diagnostic |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1043 | Info.CCEDiag(Loc); |
Richard Smith | 02ab9c2 | 2012-01-12 06:08:57 +0000 | [diff] [blame] | 1044 | return true; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1047 | // Does this refer one past the end of some object? |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1048 | if (Designator.isOnePastTheEnd()) { |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1049 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1050 | Info.Diag(Loc, diag::note_constexpr_past_end, 1) |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1051 | << !Designator.Entries.empty() << !!VD << VD; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1052 | NoteLValueLocation(Info, Base); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1055 | return true; |
| 1056 | } |
| 1057 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1058 | /// Check that this core constant expression is of literal type, and if not, |
| 1059 | /// produce an appropriate diagnostic. |
| 1060 | static bool CheckLiteralType(EvalInfo &Info, const Expr *E) { |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1061 | if (!E->isRValue() || E->getType()->isLiteralType(Info.Ctx)) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1062 | return true; |
| 1063 | |
| 1064 | // Prvalue constant expressions must be of literal types. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1065 | if (Info.getLangOpts().CPlusPlus11) |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1066 | Info.Diag(E, diag::note_constexpr_nonliteral) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1067 | << E->getType(); |
| 1068 | else |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1069 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1070 | return false; |
| 1071 | } |
| 1072 | |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1073 | /// Check that this core constant expression value is a valid value for a |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1074 | /// constant expression. If not, report an appropriate diagnostic. Does not |
| 1075 | /// check that the expression is of literal type. |
| 1076 | static bool CheckConstantExpression(EvalInfo &Info, SourceLocation DiagLoc, |
| 1077 | QualType Type, const APValue &Value) { |
| 1078 | // Core issue 1454: For a literal constant expression of array or class type, |
| 1079 | // each subobject of its value shall have been initialized by a constant |
| 1080 | // expression. |
| 1081 | if (Value.isArray()) { |
| 1082 | QualType EltTy = Type->castAsArrayTypeUnsafe()->getElementType(); |
| 1083 | for (unsigned I = 0, N = Value.getArrayInitializedElts(); I != N; ++I) { |
| 1084 | if (!CheckConstantExpression(Info, DiagLoc, EltTy, |
| 1085 | Value.getArrayInitializedElt(I))) |
| 1086 | return false; |
| 1087 | } |
| 1088 | if (!Value.hasArrayFiller()) |
| 1089 | return true; |
| 1090 | return CheckConstantExpression(Info, DiagLoc, EltTy, |
| 1091 | Value.getArrayFiller()); |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1092 | } |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1093 | if (Value.isUnion() && Value.getUnionField()) { |
| 1094 | return CheckConstantExpression(Info, DiagLoc, |
| 1095 | Value.getUnionField()->getType(), |
| 1096 | Value.getUnionValue()); |
| 1097 | } |
| 1098 | if (Value.isStruct()) { |
| 1099 | RecordDecl *RD = Type->castAs<RecordType>()->getDecl(); |
| 1100 | if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD)) { |
| 1101 | unsigned BaseIndex = 0; |
| 1102 | for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(), |
| 1103 | End = CD->bases_end(); I != End; ++I, ++BaseIndex) { |
| 1104 | if (!CheckConstantExpression(Info, DiagLoc, I->getType(), |
| 1105 | Value.getStructBase(BaseIndex))) |
| 1106 | return false; |
| 1107 | } |
| 1108 | } |
| 1109 | for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 1110 | I != E; ++I) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1111 | if (!CheckConstantExpression(Info, DiagLoc, I->getType(), |
| 1112 | Value.getStructField(I->getFieldIndex()))) |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1113 | return false; |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | if (Value.isLValue()) { |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1118 | LValue LVal; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1119 | LVal.setFrom(Info.Ctx, Value); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1120 | return CheckLValueConstantExpression(Info, DiagLoc, Type, LVal); |
| 1121 | } |
| 1122 | |
| 1123 | // Everything else is fine. |
| 1124 | return true; |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
Richard Smith | 83c6821 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 1127 | const ValueDecl *GetLValueBaseDecl(const LValue &LVal) { |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1128 | return LVal.Base.dyn_cast<const ValueDecl*>(); |
Richard Smith | 83c6821 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | static bool IsLiteralLValue(const LValue &Value) { |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1132 | return Value.Base.dyn_cast<const Expr*>() && !Value.CallIndex; |
Richard Smith | 83c6821 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
Richard Smith | cecf184 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1135 | static bool IsWeakLValue(const LValue &Value) { |
| 1136 | const ValueDecl *Decl = GetLValueBaseDecl(Value); |
Lang Hames | d42bb47 | 2011-12-05 20:16:26 +0000 | [diff] [blame] | 1137 | return Decl && Decl->isWeak(); |
Richard Smith | cecf184 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1140 | static bool EvalPointerValueAsBool(const APValue &Value, bool &Result) { |
John McCall | eb3e4f3 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 1141 | // A null base expression indicates a null pointer. These are always |
| 1142 | // evaluatable, and they are false unless the offset is zero. |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1143 | if (!Value.getLValueBase()) { |
| 1144 | Result = !Value.getLValueOffset().isZero(); |
John McCall | eb3e4f3 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 1145 | return true; |
| 1146 | } |
Rafael Espindola | a1f9cc1 | 2010-05-07 15:18:43 +0000 | [diff] [blame] | 1147 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1148 | // We have a non-null base. These are generally known to be true, but if it's |
| 1149 | // a weak declaration it can be null at runtime. |
John McCall | eb3e4f3 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 1150 | Result = true; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1151 | const ValueDecl *Decl = Value.getLValueBase().dyn_cast<const ValueDecl*>(); |
Lang Hames | d42bb47 | 2011-12-05 20:16:26 +0000 | [diff] [blame] | 1152 | return !Decl || !Decl->isWeak(); |
Eli Friedman | 334046a | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1155 | static bool HandleConversionToBool(const APValue &Val, bool &Result) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1156 | switch (Val.getKind()) { |
| 1157 | case APValue::Uninitialized: |
| 1158 | return false; |
| 1159 | case APValue::Int: |
| 1160 | Result = Val.getInt().getBoolValue(); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1161 | return true; |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1162 | case APValue::Float: |
| 1163 | Result = !Val.getFloat().isZero(); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1164 | return true; |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1165 | case APValue::ComplexInt: |
| 1166 | Result = Val.getComplexIntReal().getBoolValue() || |
| 1167 | Val.getComplexIntImag().getBoolValue(); |
| 1168 | return true; |
| 1169 | case APValue::ComplexFloat: |
| 1170 | Result = !Val.getComplexFloatReal().isZero() || |
| 1171 | !Val.getComplexFloatImag().isZero(); |
| 1172 | return true; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1173 | case APValue::LValue: |
| 1174 | return EvalPointerValueAsBool(Val, Result); |
| 1175 | case APValue::MemberPointer: |
| 1176 | Result = Val.getMemberPointerDecl(); |
| 1177 | return true; |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1178 | case APValue::Vector: |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1179 | case APValue::Array: |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1180 | case APValue::Struct: |
| 1181 | case APValue::Union: |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 1182 | case APValue::AddrLabelDiff: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1183 | return false; |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1184 | } |
| 1185 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1186 | llvm_unreachable("unknown APValue kind"); |
| 1187 | } |
| 1188 | |
| 1189 | static bool EvaluateAsBooleanCondition(const Expr *E, bool &Result, |
| 1190 | EvalInfo &Info) { |
| 1191 | assert(E->isRValue() && "missing lvalue-to-rvalue conv in bool condition"); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1192 | APValue Val; |
Argyrios Kyrtzidis | 91d0098 | 2012-02-27 20:21:34 +0000 | [diff] [blame] | 1193 | if (!Evaluate(Val, Info, E)) |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1194 | return false; |
Argyrios Kyrtzidis | 91d0098 | 2012-02-27 20:21:34 +0000 | [diff] [blame] | 1195 | return HandleConversionToBool(Val, Result); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1198 | template<typename T> |
Eli Friedman | 4eafb6b | 2012-07-17 21:03:05 +0000 | [diff] [blame] | 1199 | static void HandleOverflow(EvalInfo &Info, const Expr *E, |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1200 | const T &SrcValue, QualType DestType) { |
Eli Friedman | 4eafb6b | 2012-07-17 21:03:05 +0000 | [diff] [blame] | 1201 | Info.CCEDiag(E, diag::note_constexpr_overflow) |
Richard Smith | fe80003 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 1202 | << SrcValue << DestType; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1203 | } |
| 1204 | |
| 1205 | static bool HandleFloatToIntCast(EvalInfo &Info, const Expr *E, |
| 1206 | QualType SrcType, const APFloat &Value, |
| 1207 | QualType DestType, APSInt &Result) { |
| 1208 | unsigned DestWidth = Info.Ctx.getIntWidth(DestType); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1209 | // Determine whether we are converting to unsigned or signed. |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 1210 | bool DestSigned = DestType->isSignedIntegerOrEnumerationType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1211 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1212 | Result = APSInt(DestWidth, !DestSigned); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1213 | bool ignored; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1214 | if (Value.convertToInteger(Result, llvm::APFloat::rmTowardZero, &ignored) |
| 1215 | & APFloat::opInvalidOp) |
Eli Friedman | 4eafb6b | 2012-07-17 21:03:05 +0000 | [diff] [blame] | 1216 | HandleOverflow(Info, E, Value, DestType); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1217 | return true; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1220 | static bool HandleFloatToFloatCast(EvalInfo &Info, const Expr *E, |
| 1221 | QualType SrcType, QualType DestType, |
| 1222 | APFloat &Result) { |
| 1223 | APFloat Value = Result; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1224 | bool ignored; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1225 | if (Result.convert(Info.Ctx.getFloatTypeSemantics(DestType), |
| 1226 | APFloat::rmNearestTiesToEven, &ignored) |
| 1227 | & APFloat::opOverflow) |
Eli Friedman | 4eafb6b | 2012-07-17 21:03:05 +0000 | [diff] [blame] | 1228 | HandleOverflow(Info, E, Value, DestType); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1229 | return true; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 1232 | static APSInt HandleIntToIntCast(EvalInfo &Info, const Expr *E, |
| 1233 | QualType DestType, QualType SrcType, |
| 1234 | APSInt &Value) { |
| 1235 | unsigned DestWidth = Info.Ctx.getIntWidth(DestType); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1236 | APSInt Result = Value; |
| 1237 | // Figure out if this is a truncate, extend or noop cast. |
| 1238 | // If the input is signed, do a sign extend, noop, or truncate. |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1239 | Result = Result.extOrTrunc(DestWidth); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 1240 | Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType()); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1241 | return Result; |
| 1242 | } |
| 1243 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1244 | static bool HandleIntToFloatCast(EvalInfo &Info, const Expr *E, |
| 1245 | QualType SrcType, const APSInt &Value, |
| 1246 | QualType DestType, APFloat &Result) { |
| 1247 | Result = APFloat(Info.Ctx.getFloatTypeSemantics(DestType), 1); |
| 1248 | if (Result.convertFromAPInt(Value, Value.isSigned(), |
| 1249 | APFloat::rmNearestTiesToEven) |
| 1250 | & APFloat::opOverflow) |
Eli Friedman | 4eafb6b | 2012-07-17 21:03:05 +0000 | [diff] [blame] | 1251 | HandleOverflow(Info, E, Value, DestType); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1252 | return true; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 1255 | static bool EvalAndBitcastToAPInt(EvalInfo &Info, const Expr *E, |
| 1256 | llvm::APInt &Res) { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1257 | APValue SVal; |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 1258 | if (!Evaluate(SVal, Info, E)) |
| 1259 | return false; |
| 1260 | if (SVal.isInt()) { |
| 1261 | Res = SVal.getInt(); |
| 1262 | return true; |
| 1263 | } |
| 1264 | if (SVal.isFloat()) { |
| 1265 | Res = SVal.getFloat().bitcastToAPInt(); |
| 1266 | return true; |
| 1267 | } |
| 1268 | if (SVal.isVector()) { |
| 1269 | QualType VecTy = E->getType(); |
| 1270 | unsigned VecSize = Info.Ctx.getTypeSize(VecTy); |
| 1271 | QualType EltTy = VecTy->castAs<VectorType>()->getElementType(); |
| 1272 | unsigned EltSize = Info.Ctx.getTypeSize(EltTy); |
| 1273 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
| 1274 | Res = llvm::APInt::getNullValue(VecSize); |
| 1275 | for (unsigned i = 0; i < SVal.getVectorLength(); i++) { |
| 1276 | APValue &Elt = SVal.getVectorElt(i); |
| 1277 | llvm::APInt EltAsInt; |
| 1278 | if (Elt.isInt()) { |
| 1279 | EltAsInt = Elt.getInt(); |
| 1280 | } else if (Elt.isFloat()) { |
| 1281 | EltAsInt = Elt.getFloat().bitcastToAPInt(); |
| 1282 | } else { |
| 1283 | // Don't try to handle vectors of anything other than int or float |
| 1284 | // (not sure if it's possible to hit this case). |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1285 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 1286 | return false; |
| 1287 | } |
| 1288 | unsigned BaseEltSize = EltAsInt.getBitWidth(); |
| 1289 | if (BigEndian) |
| 1290 | Res |= EltAsInt.zextOrTrunc(VecSize).rotr(i*EltSize+BaseEltSize); |
| 1291 | else |
| 1292 | Res |= EltAsInt.zextOrTrunc(VecSize).rotl(i*EltSize); |
| 1293 | } |
| 1294 | return true; |
| 1295 | } |
| 1296 | // Give up if the input isn't an int, float, or vector. For example, we |
| 1297 | // reject "(v4i16)(intptr_t)&a". |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1298 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 1299 | return false; |
| 1300 | } |
| 1301 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1302 | /// Cast an lvalue referring to a base subobject to a derived class, by |
| 1303 | /// truncating the lvalue's path to the given length. |
| 1304 | static bool CastToDerivedClass(EvalInfo &Info, const Expr *E, LValue &Result, |
| 1305 | const RecordDecl *TruncatedType, |
| 1306 | unsigned TruncatedElements) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1307 | SubobjectDesignator &D = Result.Designator; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1308 | |
| 1309 | // Check we actually point to a derived class object. |
| 1310 | if (TruncatedElements == D.Entries.size()) |
| 1311 | return true; |
| 1312 | assert(TruncatedElements >= D.MostDerivedPathLength && |
| 1313 | "not casting to a derived class"); |
| 1314 | if (!Result.checkSubobject(Info, E, CSK_Derived)) |
| 1315 | return false; |
| 1316 | |
| 1317 | // Truncate the path to the subobject, and remove any derived-to-base offsets. |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1318 | const RecordDecl *RD = TruncatedType; |
| 1319 | for (unsigned I = TruncatedElements, N = D.Entries.size(); I != N; ++I) { |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1320 | if (RD->isInvalidDecl()) return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1321 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 1322 | const CXXRecordDecl *Base = getAsBaseClass(D.Entries[I]); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1323 | if (isVirtualBaseClass(D.Entries[I])) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1324 | Result.Offset -= Layout.getVBaseClassOffset(Base); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1325 | else |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1326 | Result.Offset -= Layout.getBaseClassOffset(Base); |
| 1327 | RD = Base; |
| 1328 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1329 | D.Entries.resize(TruncatedElements); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1330 | return true; |
| 1331 | } |
| 1332 | |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1333 | static bool HandleLValueDirectBase(EvalInfo &Info, const Expr *E, LValue &Obj, |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1334 | const CXXRecordDecl *Derived, |
| 1335 | const CXXRecordDecl *Base, |
| 1336 | const ASTRecordLayout *RL = 0) { |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1337 | if (!RL) { |
| 1338 | if (Derived->isInvalidDecl()) return false; |
| 1339 | RL = &Info.Ctx.getASTRecordLayout(Derived); |
| 1340 | } |
| 1341 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1342 | Obj.getLValueOffset() += RL->getBaseClassOffset(Base); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1343 | Obj.addDecl(Info, E, Base, /*Virtual*/ false); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1344 | return true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1347 | static bool HandleLValueBase(EvalInfo &Info, const Expr *E, LValue &Obj, |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1348 | const CXXRecordDecl *DerivedDecl, |
| 1349 | const CXXBaseSpecifier *Base) { |
| 1350 | const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl(); |
| 1351 | |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1352 | if (!Base->isVirtual()) |
| 1353 | return HandleLValueDirectBase(Info, E, Obj, DerivedDecl, BaseDecl); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1354 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1355 | SubobjectDesignator &D = Obj.Designator; |
| 1356 | if (D.Invalid) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1357 | return false; |
| 1358 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1359 | // Extract most-derived object and corresponding type. |
| 1360 | DerivedDecl = D.MostDerivedType->getAsCXXRecordDecl(); |
| 1361 | if (!CastToDerivedClass(Info, E, Obj, DerivedDecl, D.MostDerivedPathLength)) |
| 1362 | return false; |
| 1363 | |
| 1364 | // Find the virtual base class. |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1365 | if (DerivedDecl->isInvalidDecl()) return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1366 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(DerivedDecl); |
| 1367 | Obj.getLValueOffset() += Layout.getVBaseClassOffset(BaseDecl); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1368 | Obj.addDecl(Info, E, BaseDecl, /*Virtual*/ true); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1369 | return true; |
| 1370 | } |
| 1371 | |
| 1372 | /// Update LVal to refer to the given field, which must be a member of the type |
| 1373 | /// currently described by LVal. |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1374 | static bool HandleLValueMember(EvalInfo &Info, const Expr *E, LValue &LVal, |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1375 | const FieldDecl *FD, |
| 1376 | const ASTRecordLayout *RL = 0) { |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1377 | if (!RL) { |
| 1378 | if (FD->getParent()->isInvalidDecl()) return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1379 | RL = &Info.Ctx.getASTRecordLayout(FD->getParent()); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1380 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1381 | |
| 1382 | unsigned I = FD->getFieldIndex(); |
| 1383 | LVal.Offset += Info.Ctx.toCharUnitsFromBits(RL->getFieldOffset(I)); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1384 | LVal.addDecl(Info, E, FD); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1385 | return true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 1388 | /// Update LVal to refer to the given indirect field. |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1389 | static bool HandleLValueIndirectMember(EvalInfo &Info, const Expr *E, |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 1390 | LValue &LVal, |
| 1391 | const IndirectFieldDecl *IFD) { |
| 1392 | for (IndirectFieldDecl::chain_iterator C = IFD->chain_begin(), |
| 1393 | CE = IFD->chain_end(); C != CE; ++C) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1394 | if (!HandleLValueMember(Info, E, LVal, cast<FieldDecl>(*C))) |
| 1395 | return false; |
| 1396 | return true; |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 1397 | } |
| 1398 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1399 | /// Get the size of the given type in char units. |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1400 | static bool HandleSizeof(EvalInfo &Info, SourceLocation Loc, |
| 1401 | QualType Type, CharUnits &Size) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1402 | // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc |
| 1403 | // extension. |
| 1404 | if (Type->isVoidType() || Type->isFunctionType()) { |
| 1405 | Size = CharUnits::One(); |
| 1406 | return true; |
| 1407 | } |
| 1408 | |
| 1409 | if (!Type->isConstantSizeType()) { |
| 1410 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1411 | // FIXME: Better diagnostic. |
| 1412 | Info.Diag(Loc); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1413 | return false; |
| 1414 | } |
| 1415 | |
| 1416 | Size = Info.Ctx.getTypeSizeInChars(Type); |
| 1417 | return true; |
| 1418 | } |
| 1419 | |
| 1420 | /// Update a pointer value to model pointer arithmetic. |
| 1421 | /// \param Info - Information about the ongoing evaluation. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1422 | /// \param E - The expression being evaluated, for diagnostic purposes. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1423 | /// \param LVal - The pointer value to be updated. |
| 1424 | /// \param EltTy - The pointee type represented by LVal. |
| 1425 | /// \param Adjustment - The adjustment, in objects of type EltTy, to add. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1426 | static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E, |
| 1427 | LValue &LVal, QualType EltTy, |
| 1428 | int64_t Adjustment) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1429 | CharUnits SizeOfPointee; |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1430 | if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfPointee)) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1431 | return false; |
| 1432 | |
| 1433 | // Compute the new offset in the appropriate width. |
| 1434 | LVal.Offset += Adjustment * SizeOfPointee; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1435 | LVal.adjustIndex(Info, E, Adjustment); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1436 | return true; |
| 1437 | } |
| 1438 | |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1439 | /// Update an lvalue to refer to a component of a complex number. |
| 1440 | /// \param Info - Information about the ongoing evaluation. |
| 1441 | /// \param LVal - The lvalue to be updated. |
| 1442 | /// \param EltTy - The complex number's component type. |
| 1443 | /// \param Imag - False for the real component, true for the imaginary. |
| 1444 | static bool HandleLValueComplexElement(EvalInfo &Info, const Expr *E, |
| 1445 | LValue &LVal, QualType EltTy, |
| 1446 | bool Imag) { |
| 1447 | if (Imag) { |
| 1448 | CharUnits SizeOfComponent; |
| 1449 | if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfComponent)) |
| 1450 | return false; |
| 1451 | LVal.Offset += SizeOfComponent; |
| 1452 | } |
| 1453 | LVal.addComplex(Info, E, EltTy, Imag); |
| 1454 | return true; |
| 1455 | } |
| 1456 | |
Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1457 | /// Try to evaluate the initializer for a variable declaration. |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1458 | /// |
| 1459 | /// \param Info Information about the ongoing evaluation. |
| 1460 | /// \param E An expression to be used when printing diagnostics. |
| 1461 | /// \param VD The variable whose initializer should be obtained. |
| 1462 | /// \param Frame The frame in which the variable was created. Must be null |
| 1463 | /// if this variable is not local to the evaluation. |
| 1464 | /// \param Result Filled in with a pointer to the value of the variable. |
| 1465 | static bool evaluateVarDeclInit(EvalInfo &Info, const Expr *E, |
| 1466 | const VarDecl *VD, CallStackFrame *Frame, |
| 1467 | APValue *&Result) { |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1468 | // If this is a parameter to an active constexpr function call, perform |
| 1469 | // argument substitution. |
| 1470 | if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(VD)) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1471 | // Assume arguments of a potential constant expression are unknown |
| 1472 | // constant expressions. |
| 1473 | if (Info.CheckingPotentialConstantExpression) |
| 1474 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1475 | if (!Frame || !Frame->Arguments) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1476 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1477 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1478 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1479 | Result = &Frame->Arguments[PVD->getFunctionScopeIndex()]; |
Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1480 | return true; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1481 | } |
Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1482 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1483 | // If this is a local variable, dig out its value. |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1484 | if (Frame) { |
| 1485 | Result = &Frame->Temporaries[VD]; |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1486 | // If we've carried on past an unevaluatable local variable initializer, |
| 1487 | // we can't go any further. This can happen during potential constant |
| 1488 | // expression checking. |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1489 | return !Result->isUninit(); |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1492 | // Dig out the initializer, and use the declaration which it's attached to. |
| 1493 | const Expr *Init = VD->getAnyInitializer(VD); |
| 1494 | if (!Init || Init->isValueDependent()) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1495 | // If we're checking a potential constant expression, the variable could be |
| 1496 | // initialized later. |
| 1497 | if (!Info.CheckingPotentialConstantExpression) |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1498 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1499 | return false; |
| 1500 | } |
| 1501 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1502 | // If we're currently evaluating the initializer of this declaration, use that |
| 1503 | // in-flight value. |
| 1504 | if (Info.EvaluatingDecl == VD) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1505 | Result = Info.EvaluatingDeclValue; |
| 1506 | return !Result->isUninit(); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
Richard Smith | cecf184 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1509 | // Never evaluate the initializer of a weak variable. We can't be sure that |
| 1510 | // this is the definition which will be used. |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1511 | if (VD->isWeak()) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1512 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | cecf184 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1513 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1514 | } |
Richard Smith | cecf184 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1515 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1516 | // Check that we can fold the initializer. In C++, we will have already done |
| 1517 | // this in the cases where it matters for conformance. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1518 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1519 | if (!VD->evaluateValue(Notes)) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1520 | Info.Diag(E, diag::note_constexpr_var_init_non_constant, |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1521 | Notes.size() + 1) << VD; |
| 1522 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1523 | Info.addNotes(Notes); |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1524 | return false; |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1525 | } else if (!VD->checkInitIsICE()) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1526 | Info.CCEDiag(E, diag::note_constexpr_var_init_non_constant, |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1527 | Notes.size() + 1) << VD; |
| 1528 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1529 | Info.addNotes(Notes); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1530 | } |
Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1531 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1532 | Result = VD->getEvaluatedValue(); |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1533 | return true; |
Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1534 | } |
| 1535 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1536 | static bool IsConstNonVolatile(QualType T) { |
Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1537 | Qualifiers Quals = T.getQualifiers(); |
| 1538 | return Quals.hasConst() && !Quals.hasVolatile(); |
| 1539 | } |
| 1540 | |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1541 | /// Get the base index of the given base class within an APValue representing |
| 1542 | /// the given derived class. |
| 1543 | static unsigned getBaseIndex(const CXXRecordDecl *Derived, |
| 1544 | const CXXRecordDecl *Base) { |
| 1545 | Base = Base->getCanonicalDecl(); |
| 1546 | unsigned Index = 0; |
| 1547 | for (CXXRecordDecl::base_class_const_iterator I = Derived->bases_begin(), |
| 1548 | E = Derived->bases_end(); I != E; ++I, ++Index) { |
| 1549 | if (I->getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base) |
| 1550 | return Index; |
| 1551 | } |
| 1552 | |
| 1553 | llvm_unreachable("base class missing from derived class's bases list"); |
| 1554 | } |
| 1555 | |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1556 | /// Extract the value of a character from a string literal. |
| 1557 | static APSInt extractStringLiteralCharacter(EvalInfo &Info, const Expr *Lit, |
| 1558 | uint64_t Index) { |
Richard Smith | 14a9413 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 1559 | // FIXME: Support PredefinedExpr, ObjCEncodeExpr, MakeStringConstant |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1560 | const StringLiteral *S = cast<StringLiteral>(Lit); |
| 1561 | const ConstantArrayType *CAT = |
| 1562 | Info.Ctx.getAsConstantArrayType(S->getType()); |
| 1563 | assert(CAT && "string literal isn't an array"); |
| 1564 | QualType CharType = CAT->getElementType(); |
Richard Smith | 9ec1e48 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 1565 | assert(CharType->isIntegerType() && "unexpected character type"); |
Richard Smith | 14a9413 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 1566 | |
| 1567 | APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(), |
Richard Smith | 9ec1e48 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 1568 | CharType->isUnsignedIntegerType()); |
Richard Smith | 14a9413 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 1569 | if (Index < S->getLength()) |
| 1570 | Value = S->getCodeUnit(Index); |
| 1571 | return Value; |
| 1572 | } |
| 1573 | |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1574 | // Expand a string literal into an array of characters. |
| 1575 | static void expandStringLiteral(EvalInfo &Info, const Expr *Lit, |
| 1576 | APValue &Result) { |
| 1577 | const StringLiteral *S = cast<StringLiteral>(Lit); |
| 1578 | const ConstantArrayType *CAT = |
| 1579 | Info.Ctx.getAsConstantArrayType(S->getType()); |
| 1580 | assert(CAT && "string literal isn't an array"); |
| 1581 | QualType CharType = CAT->getElementType(); |
| 1582 | assert(CharType->isIntegerType() && "unexpected character type"); |
| 1583 | |
| 1584 | unsigned Elts = CAT->getSize().getZExtValue(); |
| 1585 | Result = APValue(APValue::UninitArray(), |
| 1586 | std::min(S->getLength(), Elts), Elts); |
| 1587 | APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(), |
| 1588 | CharType->isUnsignedIntegerType()); |
| 1589 | if (Result.hasArrayFiller()) |
| 1590 | Result.getArrayFiller() = APValue(Value); |
| 1591 | for (unsigned I = 0, N = Result.getArrayInitializedElts(); I != N; ++I) { |
| 1592 | Value = S->getCodeUnit(I); |
| 1593 | Result.getArrayInitializedElt(I) = APValue(Value); |
| 1594 | } |
| 1595 | } |
| 1596 | |
| 1597 | // Expand an array so that it has more than Index filled elements. |
| 1598 | static void expandArray(APValue &Array, unsigned Index) { |
| 1599 | unsigned Size = Array.getArraySize(); |
| 1600 | assert(Index < Size); |
| 1601 | |
| 1602 | // Always at least double the number of elements for which we store a value. |
| 1603 | unsigned OldElts = Array.getArrayInitializedElts(); |
| 1604 | unsigned NewElts = std::max(Index+1, OldElts * 2); |
| 1605 | NewElts = std::min(Size, std::max(NewElts, 8u)); |
| 1606 | |
| 1607 | // Copy the data across. |
| 1608 | APValue NewValue(APValue::UninitArray(), NewElts, Size); |
| 1609 | for (unsigned I = 0; I != OldElts; ++I) |
| 1610 | NewValue.getArrayInitializedElt(I).swap(Array.getArrayInitializedElt(I)); |
| 1611 | for (unsigned I = OldElts; I != NewElts; ++I) |
| 1612 | NewValue.getArrayInitializedElt(I) = Array.getArrayFiller(); |
| 1613 | if (NewValue.hasArrayFiller()) |
| 1614 | NewValue.getArrayFiller() = Array.getArrayFiller(); |
| 1615 | Array.swap(NewValue); |
| 1616 | } |
| 1617 | |
| 1618 | /// Kinds of access we can perform on an object. |
| 1619 | enum AccessKinds { |
| 1620 | AK_Read, |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 1621 | AK_Assign, |
| 1622 | AK_Increment, |
| 1623 | AK_Decrement |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1624 | }; |
| 1625 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1626 | /// A handle to a complete object (an object that is not a subobject of |
| 1627 | /// another object). |
| 1628 | struct CompleteObject { |
| 1629 | /// The value of the complete object. |
| 1630 | APValue *Value; |
| 1631 | /// The type of the complete object. |
| 1632 | QualType Type; |
| 1633 | |
| 1634 | CompleteObject() : Value(0) {} |
| 1635 | CompleteObject(APValue *Value, QualType Type) |
| 1636 | : Value(Value), Type(Type) { |
| 1637 | assert(Value && "missing value for complete object"); |
| 1638 | } |
| 1639 | |
| 1640 | operator bool() const { return Value; } |
| 1641 | }; |
| 1642 | |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1643 | /// Find the designated sub-object of an rvalue. |
| 1644 | template<typename SubobjectHandler> |
| 1645 | typename SubobjectHandler::result_type |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1646 | findSubobject(EvalInfo &Info, const Expr *E, const CompleteObject &Obj, |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1647 | const SubobjectDesignator &Sub, SubobjectHandler &handler) { |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1648 | if (Sub.Invalid) |
| 1649 | // A diagnostic will have already been produced. |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1650 | return handler.failed(); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1651 | if (Sub.isOnePastTheEnd()) { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1652 | if (Info.getLangOpts().CPlusPlus11) |
| 1653 | Info.Diag(E, diag::note_constexpr_access_past_end) |
| 1654 | << handler.AccessKind; |
| 1655 | else |
| 1656 | Info.Diag(E); |
| 1657 | return handler.failed(); |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1658 | } |
Richard Smith | 6804be5 | 2011-11-11 08:28:03 +0000 | [diff] [blame] | 1659 | if (Sub.Entries.empty()) |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1660 | return handler.found(*Obj.Value, Obj.Type); |
| 1661 | if (Info.CheckingPotentialConstantExpression && Obj.Value->isUninit()) |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1662 | // This object might be initialized later. |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1663 | return handler.failed(); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1664 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1665 | APValue *O = Obj.Value; |
| 1666 | QualType ObjType = Obj.Type; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1667 | // Walk the designator's path to find the subobject. |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1668 | for (unsigned I = 0, N = Sub.Entries.size(); I != N; ++I) { |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1669 | if (ObjType->isArrayType()) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1670 | // Next subobject is an array element. |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1671 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(ObjType); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1672 | assert(CAT && "vla in literal type?"); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1673 | uint64_t Index = Sub.Entries[I].ArrayIndex; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1674 | if (CAT->getSize().ule(Index)) { |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1675 | // Note, it should not be possible to form a pointer with a valid |
| 1676 | // designator which points more than one past the end of the array. |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1677 | if (Info.getLangOpts().CPlusPlus11) |
| 1678 | Info.Diag(E, diag::note_constexpr_access_past_end) |
| 1679 | << handler.AccessKind; |
| 1680 | else |
| 1681 | Info.Diag(E); |
| 1682 | return handler.failed(); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1683 | } |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1684 | |
| 1685 | ObjType = CAT->getElementType(); |
| 1686 | |
Richard Smith | 14a9413 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 1687 | // An array object is represented as either an Array APValue or as an |
| 1688 | // LValue which refers to a string literal. |
| 1689 | if (O->isLValue()) { |
| 1690 | assert(I == N - 1 && "extracting subobject of character?"); |
| 1691 | assert(!O->hasLValuePath() || O->getLValuePath().empty()); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1692 | if (handler.AccessKind != AK_Read) |
| 1693 | expandStringLiteral(Info, O->getLValueBase().get<const Expr *>(), |
| 1694 | *O); |
| 1695 | else |
| 1696 | return handler.foundString(*O, ObjType, Index); |
| 1697 | } |
| 1698 | |
| 1699 | if (O->getArrayInitializedElts() > Index) |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1700 | O = &O->getArrayInitializedElt(Index); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1701 | else if (handler.AccessKind != AK_Read) { |
| 1702 | expandArray(*O, Index); |
| 1703 | O = &O->getArrayInitializedElt(Index); |
| 1704 | } else |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1705 | O = &O->getArrayFiller(); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1706 | } else if (ObjType->isAnyComplexType()) { |
| 1707 | // Next subobject is a complex number. |
| 1708 | uint64_t Index = Sub.Entries[I].ArrayIndex; |
| 1709 | if (Index > 1) { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1710 | if (Info.getLangOpts().CPlusPlus11) |
| 1711 | Info.Diag(E, diag::note_constexpr_access_past_end) |
| 1712 | << handler.AccessKind; |
| 1713 | else |
| 1714 | Info.Diag(E); |
| 1715 | return handler.failed(); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1716 | } |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1717 | |
| 1718 | bool WasConstQualified = ObjType.isConstQualified(); |
| 1719 | ObjType = ObjType->castAs<ComplexType>()->getElementType(); |
| 1720 | if (WasConstQualified) |
| 1721 | ObjType.addConst(); |
| 1722 | |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1723 | assert(I == N - 1 && "extracting subobject of scalar?"); |
| 1724 | if (O->isComplexInt()) { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1725 | return handler.found(Index ? O->getComplexIntImag() |
| 1726 | : O->getComplexIntReal(), ObjType); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1727 | } else { |
| 1728 | assert(O->isComplexFloat()); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1729 | return handler.found(Index ? O->getComplexFloatImag() |
| 1730 | : O->getComplexFloatReal(), ObjType); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1731 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1732 | } else if (const FieldDecl *Field = getAsField(Sub.Entries[I])) { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1733 | if (Field->isMutable() && handler.AccessKind == AK_Read) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1734 | Info.Diag(E, diag::note_constexpr_ltor_mutable, 1) |
Richard Smith | 5a294e6 | 2012-02-09 03:29:58 +0000 | [diff] [blame] | 1735 | << Field; |
| 1736 | Info.Note(Field->getLocation(), diag::note_declared_at); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1737 | return handler.failed(); |
Richard Smith | 5a294e6 | 2012-02-09 03:29:58 +0000 | [diff] [blame] | 1738 | } |
| 1739 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1740 | // Next subobject is a class, struct or union field. |
| 1741 | RecordDecl *RD = ObjType->castAs<RecordType>()->getDecl(); |
| 1742 | if (RD->isUnion()) { |
| 1743 | const FieldDecl *UnionField = O->getUnionField(); |
| 1744 | if (!UnionField || |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1745 | UnionField->getCanonicalDecl() != Field->getCanonicalDecl()) { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1746 | Info.Diag(E, diag::note_constexpr_access_inactive_union_member) |
| 1747 | << handler.AccessKind << Field << !UnionField << UnionField; |
| 1748 | return handler.failed(); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1749 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1750 | O = &O->getUnionValue(); |
| 1751 | } else |
| 1752 | O = &O->getStructField(Field->getFieldIndex()); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1753 | |
| 1754 | bool WasConstQualified = ObjType.isConstQualified(); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1755 | ObjType = Field->getType(); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1756 | if (WasConstQualified && !Field->isMutable()) |
| 1757 | ObjType.addConst(); |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1758 | |
| 1759 | if (ObjType.isVolatileQualified()) { |
| 1760 | if (Info.getLangOpts().CPlusPlus) { |
| 1761 | // FIXME: Include a description of the path to the volatile subobject. |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1762 | Info.Diag(E, diag::note_constexpr_access_volatile_obj, 1) |
| 1763 | << handler.AccessKind << 2 << Field; |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1764 | Info.Note(Field->getLocation(), diag::note_declared_at); |
| 1765 | } else { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1766 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1767 | } |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1768 | return handler.failed(); |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1769 | } |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1770 | } else { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1771 | // Next subobject is a base class. |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1772 | const CXXRecordDecl *Derived = ObjType->getAsCXXRecordDecl(); |
| 1773 | const CXXRecordDecl *Base = getAsBaseClass(Sub.Entries[I]); |
| 1774 | O = &O->getStructBase(getBaseIndex(Derived, Base)); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1775 | |
| 1776 | bool WasConstQualified = ObjType.isConstQualified(); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1777 | ObjType = Info.Ctx.getRecordType(Base); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1778 | if (WasConstQualified) |
| 1779 | ObjType.addConst(); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1780 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1781 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1782 | if (O->isUninit()) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1783 | if (!Info.CheckingPotentialConstantExpression) |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1784 | Info.Diag(E, diag::note_constexpr_access_uninit) << handler.AccessKind; |
| 1785 | return handler.failed(); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1786 | } |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1787 | } |
| 1788 | |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1789 | return handler.found(*O, ObjType); |
| 1790 | } |
| 1791 | |
Benjamin Kramer | 62498ab | 2013-04-26 22:01:47 +0000 | [diff] [blame] | 1792 | namespace { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1793 | struct ExtractSubobjectHandler { |
| 1794 | EvalInfo &Info; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1795 | APValue &Result; |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1796 | |
| 1797 | static const AccessKinds AccessKind = AK_Read; |
| 1798 | |
| 1799 | typedef bool result_type; |
| 1800 | bool failed() { return false; } |
| 1801 | bool found(APValue &Subobj, QualType SubobjType) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1802 | Result = Subobj; |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1803 | return true; |
| 1804 | } |
| 1805 | bool found(APSInt &Value, QualType SubobjType) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1806 | Result = APValue(Value); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1807 | return true; |
| 1808 | } |
| 1809 | bool found(APFloat &Value, QualType SubobjType) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1810 | Result = APValue(Value); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1811 | return true; |
| 1812 | } |
| 1813 | bool foundString(APValue &Subobj, QualType SubobjType, uint64_t Character) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1814 | Result = APValue(extractStringLiteralCharacter( |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1815 | Info, Subobj.getLValueBase().get<const Expr *>(), Character)); |
| 1816 | return true; |
| 1817 | } |
| 1818 | }; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1819 | } // end anonymous namespace |
| 1820 | |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1821 | const AccessKinds ExtractSubobjectHandler::AccessKind; |
| 1822 | |
| 1823 | /// Extract the designated sub-object of an rvalue. |
| 1824 | static bool extractSubobject(EvalInfo &Info, const Expr *E, |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1825 | const CompleteObject &Obj, |
| 1826 | const SubobjectDesignator &Sub, |
| 1827 | APValue &Result) { |
| 1828 | ExtractSubobjectHandler Handler = { Info, Result }; |
| 1829 | return findSubobject(Info, E, Obj, Sub, Handler); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1830 | } |
| 1831 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1832 | namespace { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1833 | struct ModifySubobjectHandler { |
| 1834 | EvalInfo &Info; |
| 1835 | APValue &NewVal; |
| 1836 | const Expr *E; |
| 1837 | |
| 1838 | typedef bool result_type; |
| 1839 | static const AccessKinds AccessKind = AK_Assign; |
| 1840 | |
| 1841 | bool checkConst(QualType QT) { |
| 1842 | // Assigning to a const object has undefined behavior. |
| 1843 | if (QT.isConstQualified()) { |
| 1844 | Info.Diag(E, diag::note_constexpr_modify_const_type) << QT; |
| 1845 | return false; |
| 1846 | } |
| 1847 | return true; |
| 1848 | } |
| 1849 | |
| 1850 | bool failed() { return false; } |
| 1851 | bool found(APValue &Subobj, QualType SubobjType) { |
| 1852 | if (!checkConst(SubobjType)) |
| 1853 | return false; |
| 1854 | // We've been given ownership of NewVal, so just swap it in. |
| 1855 | Subobj.swap(NewVal); |
| 1856 | return true; |
| 1857 | } |
| 1858 | bool found(APSInt &Value, QualType SubobjType) { |
| 1859 | if (!checkConst(SubobjType)) |
| 1860 | return false; |
| 1861 | if (!NewVal.isInt()) { |
| 1862 | // Maybe trying to write a cast pointer value into a complex? |
| 1863 | Info.Diag(E); |
| 1864 | return false; |
| 1865 | } |
| 1866 | Value = NewVal.getInt(); |
| 1867 | return true; |
| 1868 | } |
| 1869 | bool found(APFloat &Value, QualType SubobjType) { |
| 1870 | if (!checkConst(SubobjType)) |
| 1871 | return false; |
| 1872 | Value = NewVal.getFloat(); |
| 1873 | return true; |
| 1874 | } |
| 1875 | bool foundString(APValue &Subobj, QualType SubobjType, uint64_t Character) { |
| 1876 | llvm_unreachable("shouldn't encounter string elements with ExpandArrays"); |
| 1877 | } |
| 1878 | }; |
Benjamin Kramer | 62498ab | 2013-04-26 22:01:47 +0000 | [diff] [blame] | 1879 | } // end anonymous namespace |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1880 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1881 | const AccessKinds ModifySubobjectHandler::AccessKind; |
| 1882 | |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1883 | /// Update the designated sub-object of an rvalue to the given value. |
| 1884 | static bool modifySubobject(EvalInfo &Info, const Expr *E, |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1885 | const CompleteObject &Obj, |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1886 | const SubobjectDesignator &Sub, |
| 1887 | APValue &NewVal) { |
| 1888 | ModifySubobjectHandler Handler = { Info, NewVal, E }; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1889 | return findSubobject(Info, E, Obj, Sub, Handler); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1890 | } |
| 1891 | |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 1892 | /// Find the position where two subobject designators diverge, or equivalently |
| 1893 | /// the length of the common initial subsequence. |
| 1894 | static unsigned FindDesignatorMismatch(QualType ObjType, |
| 1895 | const SubobjectDesignator &A, |
| 1896 | const SubobjectDesignator &B, |
| 1897 | bool &WasArrayIndex) { |
| 1898 | unsigned I = 0, N = std::min(A.Entries.size(), B.Entries.size()); |
| 1899 | for (/**/; I != N; ++I) { |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1900 | if (!ObjType.isNull() && |
| 1901 | (ObjType->isArrayType() || ObjType->isAnyComplexType())) { |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 1902 | // Next subobject is an array element. |
| 1903 | if (A.Entries[I].ArrayIndex != B.Entries[I].ArrayIndex) { |
| 1904 | WasArrayIndex = true; |
| 1905 | return I; |
| 1906 | } |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1907 | if (ObjType->isAnyComplexType()) |
| 1908 | ObjType = ObjType->castAs<ComplexType>()->getElementType(); |
| 1909 | else |
| 1910 | ObjType = ObjType->castAsArrayTypeUnsafe()->getElementType(); |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 1911 | } else { |
| 1912 | if (A.Entries[I].BaseOrMember != B.Entries[I].BaseOrMember) { |
| 1913 | WasArrayIndex = false; |
| 1914 | return I; |
| 1915 | } |
| 1916 | if (const FieldDecl *FD = getAsField(A.Entries[I])) |
| 1917 | // Next subobject is a field. |
| 1918 | ObjType = FD->getType(); |
| 1919 | else |
| 1920 | // Next subobject is a base class. |
| 1921 | ObjType = QualType(); |
| 1922 | } |
| 1923 | } |
| 1924 | WasArrayIndex = false; |
| 1925 | return I; |
| 1926 | } |
| 1927 | |
| 1928 | /// Determine whether the given subobject designators refer to elements of the |
| 1929 | /// same array object. |
| 1930 | static bool AreElementsOfSameArray(QualType ObjType, |
| 1931 | const SubobjectDesignator &A, |
| 1932 | const SubobjectDesignator &B) { |
| 1933 | if (A.Entries.size() != B.Entries.size()) |
| 1934 | return false; |
| 1935 | |
| 1936 | bool IsArray = A.MostDerivedArraySize != 0; |
| 1937 | if (IsArray && A.MostDerivedPathLength != A.Entries.size()) |
| 1938 | // A is a subobject of the array element. |
| 1939 | return false; |
| 1940 | |
| 1941 | // If A (and B) designates an array element, the last entry will be the array |
| 1942 | // index. That doesn't have to match. Otherwise, we're in the 'implicit array |
| 1943 | // of length 1' case, and the entire path must match. |
| 1944 | bool WasArrayIndex; |
| 1945 | unsigned CommonLength = FindDesignatorMismatch(ObjType, A, B, WasArrayIndex); |
| 1946 | return CommonLength >= A.Entries.size() - IsArray; |
| 1947 | } |
| 1948 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1949 | /// Find the complete object to which an LValue refers. |
| 1950 | CompleteObject findCompleteObject(EvalInfo &Info, const Expr *E, AccessKinds AK, |
| 1951 | const LValue &LVal, QualType LValType) { |
| 1952 | if (!LVal.Base) { |
| 1953 | Info.Diag(E, diag::note_constexpr_access_null) << AK; |
| 1954 | return CompleteObject(); |
| 1955 | } |
| 1956 | |
| 1957 | CallStackFrame *Frame = 0; |
| 1958 | if (LVal.CallIndex) { |
| 1959 | Frame = Info.getCallFrame(LVal.CallIndex); |
| 1960 | if (!Frame) { |
| 1961 | Info.Diag(E, diag::note_constexpr_lifetime_ended, 1) |
| 1962 | << AK << LVal.Base.is<const ValueDecl*>(); |
| 1963 | NoteLValueLocation(Info, LVal.Base); |
| 1964 | return CompleteObject(); |
| 1965 | } |
| 1966 | } else if (AK != AK_Read) { |
| 1967 | Info.Diag(E, diag::note_constexpr_modify_global); |
| 1968 | return CompleteObject(); |
| 1969 | } |
| 1970 | |
| 1971 | // C++11 DR1311: An lvalue-to-rvalue conversion on a volatile-qualified type |
| 1972 | // is not a constant expression (even if the object is non-volatile). We also |
| 1973 | // apply this rule to C++98, in order to conform to the expected 'volatile' |
| 1974 | // semantics. |
| 1975 | if (LValType.isVolatileQualified()) { |
| 1976 | if (Info.getLangOpts().CPlusPlus) |
| 1977 | Info.Diag(E, diag::note_constexpr_access_volatile_type) |
| 1978 | << AK << LValType; |
| 1979 | else |
| 1980 | Info.Diag(E); |
| 1981 | return CompleteObject(); |
| 1982 | } |
| 1983 | |
| 1984 | // Compute value storage location and type of base object. |
| 1985 | APValue *BaseVal = 0; |
| 1986 | QualType BaseType; |
| 1987 | |
| 1988 | if (const ValueDecl *D = LVal.Base.dyn_cast<const ValueDecl*>()) { |
| 1989 | // In C++98, const, non-volatile integers initialized with ICEs are ICEs. |
| 1990 | // In C++11, constexpr, non-volatile variables initialized with constant |
| 1991 | // expressions are constant expressions too. Inside constexpr functions, |
| 1992 | // parameters are constant expressions even if they're non-const. |
| 1993 | // In C++1y, objects local to a constant expression (those with a Frame) are |
| 1994 | // both readable and writable inside constant expressions. |
| 1995 | // In C, such things can also be folded, although they are not ICEs. |
| 1996 | const VarDecl *VD = dyn_cast<VarDecl>(D); |
| 1997 | if (VD) { |
| 1998 | if (const VarDecl *VDef = VD->getDefinition(Info.Ctx)) |
| 1999 | VD = VDef; |
| 2000 | } |
| 2001 | if (!VD || VD->isInvalidDecl()) { |
| 2002 | Info.Diag(E); |
| 2003 | return CompleteObject(); |
| 2004 | } |
| 2005 | |
| 2006 | // Accesses of volatile-qualified objects are not allowed. |
| 2007 | BaseType = VD->getType(); |
| 2008 | if (BaseType.isVolatileQualified()) { |
| 2009 | if (Info.getLangOpts().CPlusPlus) { |
| 2010 | Info.Diag(E, diag::note_constexpr_access_volatile_obj, 1) |
| 2011 | << AK << 1 << VD; |
| 2012 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 2013 | } else { |
| 2014 | Info.Diag(E); |
| 2015 | } |
| 2016 | return CompleteObject(); |
| 2017 | } |
| 2018 | |
| 2019 | // Unless we're looking at a local variable or argument in a constexpr call, |
| 2020 | // the variable we're reading must be const. |
| 2021 | if (!Frame) { |
| 2022 | assert(AK == AK_Read && "can't modify non-local"); |
| 2023 | if (VD->isConstexpr()) { |
| 2024 | // OK, we can read this variable. |
| 2025 | } else if (BaseType->isIntegralOrEnumerationType()) { |
| 2026 | if (!BaseType.isConstQualified()) { |
| 2027 | if (Info.getLangOpts().CPlusPlus) { |
| 2028 | Info.Diag(E, diag::note_constexpr_ltor_non_const_int, 1) << VD; |
| 2029 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 2030 | } else { |
| 2031 | Info.Diag(E); |
| 2032 | } |
| 2033 | return CompleteObject(); |
| 2034 | } |
| 2035 | } else if (BaseType->isFloatingType() && BaseType.isConstQualified()) { |
| 2036 | // We support folding of const floating-point types, in order to make |
| 2037 | // static const data members of such types (supported as an extension) |
| 2038 | // more useful. |
| 2039 | if (Info.getLangOpts().CPlusPlus11) { |
| 2040 | Info.CCEDiag(E, diag::note_constexpr_ltor_non_constexpr, 1) << VD; |
| 2041 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 2042 | } else { |
| 2043 | Info.CCEDiag(E); |
| 2044 | } |
| 2045 | } else { |
| 2046 | // FIXME: Allow folding of values of any literal type in all languages. |
| 2047 | if (Info.getLangOpts().CPlusPlus11) { |
| 2048 | Info.Diag(E, diag::note_constexpr_ltor_non_constexpr, 1) << VD; |
| 2049 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 2050 | } else { |
| 2051 | Info.Diag(E); |
| 2052 | } |
| 2053 | return CompleteObject(); |
| 2054 | } |
| 2055 | } |
| 2056 | |
| 2057 | if (!evaluateVarDeclInit(Info, E, VD, Frame, BaseVal)) |
| 2058 | return CompleteObject(); |
| 2059 | } else { |
| 2060 | const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); |
| 2061 | |
| 2062 | if (!Frame) { |
| 2063 | Info.Diag(E); |
| 2064 | return CompleteObject(); |
| 2065 | } |
| 2066 | |
| 2067 | BaseType = Base->getType(); |
| 2068 | BaseVal = &Frame->Temporaries[Base]; |
| 2069 | |
| 2070 | // Volatile temporary objects cannot be accessed in constant expressions. |
| 2071 | if (BaseType.isVolatileQualified()) { |
| 2072 | if (Info.getLangOpts().CPlusPlus) { |
| 2073 | Info.Diag(E, diag::note_constexpr_access_volatile_obj, 1) |
| 2074 | << AK << 0; |
| 2075 | Info.Note(Base->getExprLoc(), diag::note_constexpr_temporary_here); |
| 2076 | } else { |
| 2077 | Info.Diag(E); |
| 2078 | } |
| 2079 | return CompleteObject(); |
| 2080 | } |
| 2081 | } |
| 2082 | |
| 2083 | // In C++1y, we can't safely access any mutable state when checking a |
| 2084 | // potential constant expression. |
| 2085 | if (Frame && Info.getLangOpts().CPlusPlus1y && |
| 2086 | Info.CheckingPotentialConstantExpression) |
| 2087 | return CompleteObject(); |
| 2088 | |
| 2089 | return CompleteObject(BaseVal, BaseType); |
| 2090 | } |
| 2091 | |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 2092 | /// \brief Perform an lvalue-to-rvalue conversion on the given glvalue. This |
| 2093 | /// can also be used for 'lvalue-to-lvalue' conversions for looking up the |
| 2094 | /// glvalue referred to by an entity of reference type. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2095 | /// |
| 2096 | /// \param Info - Information about the ongoing evaluation. |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2097 | /// \param Conv - The expression for which we are performing the conversion. |
| 2098 | /// Used for diagnostics. |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2099 | /// \param Type - The type of the glvalue (before stripping cv-qualifiers in the |
| 2100 | /// case of a non-class type). |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2101 | /// \param LVal - The glvalue on which we are attempting to perform this action. |
| 2102 | /// \param RVal - The produced value will be placed here. |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 2103 | static bool handleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv, |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2104 | QualType Type, |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2105 | const LValue &LVal, APValue &RVal) { |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2106 | if (LVal.Designator.Invalid) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2107 | return false; |
| 2108 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2109 | // Check for special cases where there is no existing APValue to look at. |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2110 | const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2111 | if (!LVal.Designator.Invalid && Base && !LVal.CallIndex && |
| 2112 | !Type.isVolatileQualified()) { |
| 2113 | if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(Base)) { |
| 2114 | // In C99, a CompoundLiteralExpr is an lvalue, and we defer evaluating the |
| 2115 | // initializer until now for such expressions. Such an expression can't be |
| 2116 | // an ICE in C, so this only matters for fold. |
| 2117 | assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?"); |
| 2118 | if (Type.isVolatileQualified()) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 2119 | Info.Diag(Conv); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 2120 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2121 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2122 | APValue Lit; |
| 2123 | if (!Evaluate(Lit, Info, CLE->getInitializer())) |
| 2124 | return false; |
| 2125 | CompleteObject LitObj(&Lit, Base->getType()); |
| 2126 | return extractSubobject(Info, Conv, LitObj, LVal.Designator, RVal); |
| 2127 | } else if (isa<StringLiteral>(Base)) { |
| 2128 | // We represent a string literal array as an lvalue pointing at the |
| 2129 | // corresponding expression, rather than building an array of chars. |
| 2130 | // FIXME: Support PredefinedExpr, ObjCEncodeExpr, MakeStringConstant |
| 2131 | APValue Str(Base, CharUnits::Zero(), APValue::NoLValuePath(), 0); |
| 2132 | CompleteObject StrObj(&Str, Base->getType()); |
| 2133 | return extractSubobject(Info, Conv, StrObj, LVal.Designator, RVal); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 2134 | } |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2135 | } |
| 2136 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2137 | CompleteObject Obj = findCompleteObject(Info, Conv, AK_Read, LVal, Type); |
| 2138 | return Obj && extractSubobject(Info, Conv, Obj, LVal.Designator, RVal); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2139 | } |
| 2140 | |
| 2141 | /// Perform an assignment of Val to LVal. Takes ownership of Val. |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 2142 | static bool handleAssignment(EvalInfo &Info, const Expr *E, const LValue &LVal, |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2143 | QualType LValType, APValue &Val) { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2144 | if (LVal.Designator.Invalid) |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2145 | return false; |
| 2146 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2147 | if (!Info.getLangOpts().CPlusPlus1y) { |
| 2148 | Info.Diag(E); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2149 | return false; |
| 2150 | } |
| 2151 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2152 | CompleteObject Obj = findCompleteObject(Info, E, AK_Assign, LVal, LValType); |
| 2153 | return Obj && modifySubobject(Info, E, Obj, LVal.Designator, Val); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2154 | } |
| 2155 | |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 2156 | static bool isOverflowingIntegerType(ASTContext &Ctx, QualType T) { |
| 2157 | return T->isSignedIntegerType() && |
| 2158 | Ctx.getIntWidth(T) >= Ctx.getIntWidth(Ctx.IntTy); |
| 2159 | } |
| 2160 | |
| 2161 | namespace { |
| 2162 | struct IncDecSubobjectHandler { |
| 2163 | EvalInfo &Info; |
| 2164 | const Expr *E; |
| 2165 | AccessKinds AccessKind; |
| 2166 | APValue *Old; |
| 2167 | |
| 2168 | typedef bool result_type; |
| 2169 | |
| 2170 | bool checkConst(QualType QT) { |
| 2171 | // Assigning to a const object has undefined behavior. |
| 2172 | if (QT.isConstQualified()) { |
| 2173 | Info.Diag(E, diag::note_constexpr_modify_const_type) << QT; |
| 2174 | return false; |
| 2175 | } |
| 2176 | return true; |
| 2177 | } |
| 2178 | |
| 2179 | bool failed() { return false; } |
| 2180 | bool found(APValue &Subobj, QualType SubobjType) { |
| 2181 | // Stash the old value. Also clear Old, so we don't clobber it later |
| 2182 | // if we're post-incrementing a complex. |
| 2183 | if (Old) { |
| 2184 | *Old = Subobj; |
| 2185 | Old = 0; |
| 2186 | } |
| 2187 | |
| 2188 | switch (Subobj.getKind()) { |
| 2189 | case APValue::Int: |
| 2190 | return found(Subobj.getInt(), SubobjType); |
| 2191 | case APValue::Float: |
| 2192 | return found(Subobj.getFloat(), SubobjType); |
| 2193 | case APValue::ComplexInt: |
| 2194 | return found(Subobj.getComplexIntReal(), |
| 2195 | SubobjType->castAs<ComplexType>()->getElementType() |
| 2196 | .withCVRQualifiers(SubobjType.getCVRQualifiers())); |
| 2197 | case APValue::ComplexFloat: |
| 2198 | return found(Subobj.getComplexFloatReal(), |
| 2199 | SubobjType->castAs<ComplexType>()->getElementType() |
| 2200 | .withCVRQualifiers(SubobjType.getCVRQualifiers())); |
| 2201 | case APValue::LValue: |
| 2202 | return foundPointer(Subobj, SubobjType); |
| 2203 | default: |
| 2204 | // FIXME: can this happen? |
| 2205 | Info.Diag(E); |
| 2206 | return false; |
| 2207 | } |
| 2208 | } |
| 2209 | bool found(APSInt &Value, QualType SubobjType) { |
| 2210 | if (!checkConst(SubobjType)) |
| 2211 | return false; |
| 2212 | |
| 2213 | if (!SubobjType->isIntegerType()) { |
| 2214 | // We don't support increment / decrement on integer-cast-to-pointer |
| 2215 | // values. |
| 2216 | Info.Diag(E); |
| 2217 | return false; |
| 2218 | } |
| 2219 | |
| 2220 | if (Old) *Old = APValue(Value); |
| 2221 | |
| 2222 | // bool arithmetic promotes to int, and the conversion back to bool |
| 2223 | // doesn't reduce mod 2^n, so special-case it. |
| 2224 | if (SubobjType->isBooleanType()) { |
| 2225 | if (AccessKind == AK_Increment) |
| 2226 | Value = 1; |
| 2227 | else |
| 2228 | Value = !Value; |
| 2229 | return true; |
| 2230 | } |
| 2231 | |
| 2232 | bool WasNegative = Value.isNegative(); |
| 2233 | if (AccessKind == AK_Increment) { |
| 2234 | ++Value; |
| 2235 | |
| 2236 | if (!WasNegative && Value.isNegative() && |
| 2237 | isOverflowingIntegerType(Info.Ctx, SubobjType)) { |
| 2238 | APSInt ActualValue(Value, /*IsUnsigned*/true); |
| 2239 | HandleOverflow(Info, E, ActualValue, SubobjType); |
| 2240 | } |
| 2241 | } else { |
| 2242 | --Value; |
| 2243 | |
| 2244 | if (WasNegative && !Value.isNegative() && |
| 2245 | isOverflowingIntegerType(Info.Ctx, SubobjType)) { |
| 2246 | unsigned BitWidth = Value.getBitWidth(); |
| 2247 | APSInt ActualValue(Value.sext(BitWidth + 1), /*IsUnsigned*/false); |
| 2248 | ActualValue.setBit(BitWidth); |
| 2249 | HandleOverflow(Info, E, ActualValue, SubobjType); |
| 2250 | } |
| 2251 | } |
| 2252 | return true; |
| 2253 | } |
| 2254 | bool found(APFloat &Value, QualType SubobjType) { |
| 2255 | if (!checkConst(SubobjType)) |
| 2256 | return false; |
| 2257 | |
| 2258 | if (Old) *Old = APValue(Value); |
| 2259 | |
| 2260 | APFloat One(Value.getSemantics(), 1); |
| 2261 | if (AccessKind == AK_Increment) |
| 2262 | Value.add(One, APFloat::rmNearestTiesToEven); |
| 2263 | else |
| 2264 | Value.subtract(One, APFloat::rmNearestTiesToEven); |
| 2265 | return true; |
| 2266 | } |
| 2267 | bool foundPointer(APValue &Subobj, QualType SubobjType) { |
| 2268 | if (!checkConst(SubobjType)) |
| 2269 | return false; |
| 2270 | |
| 2271 | QualType PointeeType; |
| 2272 | if (const PointerType *PT = SubobjType->getAs<PointerType>()) |
| 2273 | PointeeType = PT->getPointeeType(); |
| 2274 | else { |
| 2275 | Info.Diag(E); |
| 2276 | return false; |
| 2277 | } |
| 2278 | |
| 2279 | LValue LVal; |
| 2280 | LVal.setFrom(Info.Ctx, Subobj); |
| 2281 | if (!HandleLValueArrayAdjustment(Info, E, LVal, PointeeType, |
| 2282 | AccessKind == AK_Increment ? 1 : -1)) |
| 2283 | return false; |
| 2284 | LVal.moveInto(Subobj); |
| 2285 | return true; |
| 2286 | } |
| 2287 | bool foundString(APValue &Subobj, QualType SubobjType, uint64_t Character) { |
| 2288 | llvm_unreachable("shouldn't encounter string elements here"); |
| 2289 | } |
| 2290 | }; |
| 2291 | } // end anonymous namespace |
| 2292 | |
| 2293 | /// Perform an increment or decrement on LVal. |
| 2294 | static bool handleIncDec(EvalInfo &Info, const Expr *E, const LValue &LVal, |
| 2295 | QualType LValType, bool IsIncrement, APValue *Old) { |
| 2296 | if (LVal.Designator.Invalid) |
| 2297 | return false; |
| 2298 | |
| 2299 | if (!Info.getLangOpts().CPlusPlus1y) { |
| 2300 | Info.Diag(E); |
| 2301 | return false; |
| 2302 | } |
| 2303 | |
| 2304 | AccessKinds AK = IsIncrement ? AK_Increment : AK_Decrement; |
| 2305 | CompleteObject Obj = findCompleteObject(Info, E, AK, LVal, LValType); |
| 2306 | IncDecSubobjectHandler Handler = { Info, E, AK, Old }; |
| 2307 | return Obj && findSubobject(Info, E, Obj, LVal.Designator, Handler); |
| 2308 | } |
| 2309 | |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2310 | /// Build an lvalue for the object argument of a member function call. |
| 2311 | static bool EvaluateObjectArgument(EvalInfo &Info, const Expr *Object, |
| 2312 | LValue &This) { |
| 2313 | if (Object->getType()->isPointerType()) |
| 2314 | return EvaluatePointer(Object, This, Info); |
| 2315 | |
| 2316 | if (Object->isGLValue()) |
| 2317 | return EvaluateLValue(Object, This, Info); |
| 2318 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 2319 | if (Object->getType()->isLiteralType(Info.Ctx)) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2320 | return EvaluateTemporary(Object, This, Info); |
| 2321 | |
| 2322 | return false; |
| 2323 | } |
| 2324 | |
| 2325 | /// HandleMemberPointerAccess - Evaluate a member access operation and build an |
| 2326 | /// lvalue referring to the result. |
| 2327 | /// |
| 2328 | /// \param Info - Information about the ongoing evaluation. |
| 2329 | /// \param BO - The member pointer access operation. |
| 2330 | /// \param LV - Filled in with a reference to the resulting object. |
| 2331 | /// \param IncludeMember - Specifies whether the member itself is included in |
| 2332 | /// the resulting LValue subobject designator. This is not possible when |
| 2333 | /// creating a bound member function. |
| 2334 | /// \return The field or method declaration to which the member pointer refers, |
| 2335 | /// or 0 if evaluation fails. |
| 2336 | static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info, |
| 2337 | const BinaryOperator *BO, |
| 2338 | LValue &LV, |
| 2339 | bool IncludeMember = true) { |
| 2340 | assert(BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI); |
| 2341 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2342 | bool EvalObjOK = EvaluateObjectArgument(Info, BO->getLHS(), LV); |
| 2343 | if (!EvalObjOK && !Info.keepEvaluatingAfterFailure()) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2344 | return 0; |
| 2345 | |
| 2346 | MemberPtr MemPtr; |
| 2347 | if (!EvaluateMemberPointer(BO->getRHS(), MemPtr, Info)) |
| 2348 | return 0; |
| 2349 | |
| 2350 | // C++11 [expr.mptr.oper]p6: If the second operand is the null pointer to |
| 2351 | // member value, the behavior is undefined. |
| 2352 | if (!MemPtr.getDecl()) |
| 2353 | return 0; |
| 2354 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2355 | if (!EvalObjOK) |
| 2356 | return 0; |
| 2357 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2358 | if (MemPtr.isDerivedMember()) { |
| 2359 | // This is a member of some derived class. Truncate LV appropriately. |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2360 | // The end of the derived-to-base path for the base object must match the |
| 2361 | // derived-to-base path for the member pointer. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2362 | if (LV.Designator.MostDerivedPathLength + MemPtr.Path.size() > |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2363 | LV.Designator.Entries.size()) |
| 2364 | return 0; |
| 2365 | unsigned PathLengthToMember = |
| 2366 | LV.Designator.Entries.size() - MemPtr.Path.size(); |
| 2367 | for (unsigned I = 0, N = MemPtr.Path.size(); I != N; ++I) { |
| 2368 | const CXXRecordDecl *LVDecl = getAsBaseClass( |
| 2369 | LV.Designator.Entries[PathLengthToMember + I]); |
| 2370 | const CXXRecordDecl *MPDecl = MemPtr.Path[I]; |
| 2371 | if (LVDecl->getCanonicalDecl() != MPDecl->getCanonicalDecl()) |
| 2372 | return 0; |
| 2373 | } |
| 2374 | |
| 2375 | // Truncate the lvalue to the appropriate derived class. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2376 | if (!CastToDerivedClass(Info, BO, LV, MemPtr.getContainingRecord(), |
| 2377 | PathLengthToMember)) |
| 2378 | return 0; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2379 | } else if (!MemPtr.Path.empty()) { |
| 2380 | // Extend the LValue path with the member pointer's path. |
| 2381 | LV.Designator.Entries.reserve(LV.Designator.Entries.size() + |
| 2382 | MemPtr.Path.size() + IncludeMember); |
| 2383 | |
| 2384 | // Walk down to the appropriate base class. |
| 2385 | QualType LVType = BO->getLHS()->getType(); |
| 2386 | if (const PointerType *PT = LVType->getAs<PointerType>()) |
| 2387 | LVType = PT->getPointeeType(); |
| 2388 | const CXXRecordDecl *RD = LVType->getAsCXXRecordDecl(); |
| 2389 | assert(RD && "member pointer access on non-class-type expression"); |
| 2390 | // The first class in the path is that of the lvalue. |
| 2391 | for (unsigned I = 1, N = MemPtr.Path.size(); I != N; ++I) { |
| 2392 | const CXXRecordDecl *Base = MemPtr.Path[N - I - 1]; |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2393 | if (!HandleLValueDirectBase(Info, BO, LV, RD, Base)) |
| 2394 | return 0; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2395 | RD = Base; |
| 2396 | } |
| 2397 | // Finally cast to the class containing the member. |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2398 | if (!HandleLValueDirectBase(Info, BO, LV, RD, MemPtr.getContainingRecord())) |
| 2399 | return 0; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2400 | } |
| 2401 | |
| 2402 | // Add the member. Note that we cannot build bound member functions here. |
| 2403 | if (IncludeMember) { |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2404 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(MemPtr.getDecl())) { |
| 2405 | if (!HandleLValueMember(Info, BO, LV, FD)) |
| 2406 | return 0; |
| 2407 | } else if (const IndirectFieldDecl *IFD = |
| 2408 | dyn_cast<IndirectFieldDecl>(MemPtr.getDecl())) { |
| 2409 | if (!HandleLValueIndirectMember(Info, BO, LV, IFD)) |
| 2410 | return 0; |
| 2411 | } else { |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2412 | llvm_unreachable("can't construct reference to bound member function"); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2413 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2414 | } |
| 2415 | |
| 2416 | return MemPtr.getDecl(); |
| 2417 | } |
| 2418 | |
| 2419 | /// HandleBaseToDerivedCast - Apply the given base-to-derived cast operation on |
| 2420 | /// the provided lvalue, which currently refers to the base object. |
| 2421 | static bool HandleBaseToDerivedCast(EvalInfo &Info, const CastExpr *E, |
| 2422 | LValue &Result) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2423 | SubobjectDesignator &D = Result.Designator; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2424 | if (D.Invalid || !Result.checkNullPointer(Info, E, CSK_Derived)) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2425 | return false; |
| 2426 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2427 | QualType TargetQT = E->getType(); |
| 2428 | if (const PointerType *PT = TargetQT->getAs<PointerType>()) |
| 2429 | TargetQT = PT->getPointeeType(); |
| 2430 | |
| 2431 | // Check this cast lands within the final derived-to-base subobject path. |
| 2432 | if (D.MostDerivedPathLength + E->path_size() > D.Entries.size()) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 2433 | Info.CCEDiag(E, diag::note_constexpr_invalid_downcast) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2434 | << D.MostDerivedType << TargetQT; |
| 2435 | return false; |
| 2436 | } |
| 2437 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2438 | // Check the type of the final cast. We don't need to check the path, |
| 2439 | // since a cast can only be formed if the path is unique. |
| 2440 | unsigned NewEntriesSize = D.Entries.size() - E->path_size(); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2441 | const CXXRecordDecl *TargetType = TargetQT->getAsCXXRecordDecl(); |
| 2442 | const CXXRecordDecl *FinalType; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2443 | if (NewEntriesSize == D.MostDerivedPathLength) |
| 2444 | FinalType = D.MostDerivedType->getAsCXXRecordDecl(); |
| 2445 | else |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2446 | FinalType = getAsBaseClass(D.Entries[NewEntriesSize - 1]); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2447 | if (FinalType->getCanonicalDecl() != TargetType->getCanonicalDecl()) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 2448 | Info.CCEDiag(E, diag::note_constexpr_invalid_downcast) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2449 | << D.MostDerivedType << TargetQT; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2450 | return false; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2451 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2452 | |
| 2453 | // Truncate the lvalue to the appropriate derived class. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2454 | return CastToDerivedClass(Info, E, Result, TargetType, NewEntriesSize); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2455 | } |
| 2456 | |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 2457 | namespace { |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2458 | enum EvalStmtResult { |
| 2459 | /// Evaluation failed. |
| 2460 | ESR_Failed, |
| 2461 | /// Hit a 'return' statement. |
| 2462 | ESR_Returned, |
| 2463 | /// Evaluation succeeded. |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 2464 | ESR_Succeeded, |
| 2465 | /// Hit a 'continue' statement. |
| 2466 | ESR_Continue, |
| 2467 | /// Hit a 'break' statement. |
| 2468 | ESR_Break |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2469 | }; |
| 2470 | } |
| 2471 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 2472 | static bool EvaluateDecl(EvalInfo &Info, const Decl *D) { |
| 2473 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 2474 | // We don't need to evaluate the initializer for a static local. |
| 2475 | if (!VD->hasLocalStorage()) |
| 2476 | return true; |
| 2477 | |
| 2478 | LValue Result; |
| 2479 | Result.set(VD, Info.CurrentCall->Index); |
| 2480 | APValue &Val = Info.CurrentCall->Temporaries[VD]; |
| 2481 | |
| 2482 | if (!EvaluateInPlace(Val, Info, Result, VD->getInit())) { |
| 2483 | // Wipe out any partially-computed value, to allow tracking that this |
| 2484 | // evaluation failed. |
| 2485 | Val = APValue(); |
| 2486 | return false; |
| 2487 | } |
| 2488 | } |
| 2489 | |
| 2490 | return true; |
| 2491 | } |
| 2492 | |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 2493 | /// Evaluate a condition (either a variable declaration or an expression). |
| 2494 | static bool EvaluateCond(EvalInfo &Info, const VarDecl *CondDecl, |
| 2495 | const Expr *Cond, bool &Result) { |
| 2496 | if (CondDecl && !EvaluateDecl(Info, CondDecl)) |
| 2497 | return false; |
| 2498 | return EvaluateAsBooleanCondition(Cond, Result, Info); |
| 2499 | } |
| 2500 | |
| 2501 | static EvalStmtResult EvaluateStmt(APValue &Result, EvalInfo &Info, |
| 2502 | const Stmt *S); |
| 2503 | |
| 2504 | /// Evaluate the body of a loop, and translate the result as appropriate. |
| 2505 | static EvalStmtResult EvaluateLoopBody(APValue &Result, EvalInfo &Info, |
| 2506 | const Stmt *Body) { |
| 2507 | switch (EvalStmtResult ESR = EvaluateStmt(Result, Info, Body)) { |
| 2508 | case ESR_Break: |
| 2509 | return ESR_Succeeded; |
| 2510 | case ESR_Succeeded: |
| 2511 | case ESR_Continue: |
| 2512 | return ESR_Continue; |
| 2513 | case ESR_Failed: |
| 2514 | case ESR_Returned: |
| 2515 | return ESR; |
| 2516 | } |
Hans Wennborg | 9242bd1 | 2013-05-06 15:13:34 +0000 | [diff] [blame] | 2517 | llvm_unreachable("Invalid EvalStmtResult!"); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 2518 | } |
| 2519 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2520 | // Evaluate a statement. |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2521 | static EvalStmtResult EvaluateStmt(APValue &Result, EvalInfo &Info, |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2522 | const Stmt *S) { |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 2523 | // FIXME: Mark all temporaries in the current frame as destroyed at |
| 2524 | // the end of each full-expression. |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2525 | switch (S->getStmtClass()) { |
| 2526 | default: |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 2527 | if (const Expr *E = dyn_cast<Expr>(S)) { |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 2528 | // Don't bother evaluating beyond an expression-statement which couldn't |
| 2529 | // be evaluated. |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 2530 | if (!EvaluateIgnoredValue(Info, E)) |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 2531 | return ESR_Failed; |
| 2532 | return ESR_Succeeded; |
| 2533 | } |
| 2534 | |
| 2535 | Info.Diag(S->getLocStart()); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2536 | return ESR_Failed; |
| 2537 | |
| 2538 | case Stmt::NullStmtClass: |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2539 | return ESR_Succeeded; |
| 2540 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 2541 | case Stmt::DeclStmtClass: { |
| 2542 | const DeclStmt *DS = cast<DeclStmt>(S); |
| 2543 | for (DeclStmt::const_decl_iterator DclIt = DS->decl_begin(), |
| 2544 | DclEnd = DS->decl_end(); DclIt != DclEnd; ++DclIt) |
| 2545 | if (!EvaluateDecl(Info, *DclIt) && !Info.keepEvaluatingAfterFailure()) |
| 2546 | return ESR_Failed; |
| 2547 | return ESR_Succeeded; |
| 2548 | } |
| 2549 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2550 | case Stmt::ReturnStmtClass: { |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2551 | const Expr *RetExpr = cast<ReturnStmt>(S)->getRetValue(); |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 2552 | if (RetExpr && !Evaluate(Result, Info, RetExpr)) |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2553 | return ESR_Failed; |
| 2554 | return ESR_Returned; |
| 2555 | } |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2556 | |
| 2557 | case Stmt::CompoundStmtClass: { |
| 2558 | const CompoundStmt *CS = cast<CompoundStmt>(S); |
| 2559 | for (CompoundStmt::const_body_iterator BI = CS->body_begin(), |
| 2560 | BE = CS->body_end(); BI != BE; ++BI) { |
| 2561 | EvalStmtResult ESR = EvaluateStmt(Result, Info, *BI); |
| 2562 | if (ESR != ESR_Succeeded) |
| 2563 | return ESR; |
| 2564 | } |
| 2565 | return ESR_Succeeded; |
| 2566 | } |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 2567 | |
| 2568 | case Stmt::IfStmtClass: { |
| 2569 | const IfStmt *IS = cast<IfStmt>(S); |
| 2570 | |
| 2571 | // Evaluate the condition, as either a var decl or as an expression. |
| 2572 | bool Cond; |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 2573 | if (!EvaluateCond(Info, IS->getConditionVariable(), IS->getCond(), Cond)) |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 2574 | return ESR_Failed; |
| 2575 | |
| 2576 | if (const Stmt *SubStmt = Cond ? IS->getThen() : IS->getElse()) { |
| 2577 | EvalStmtResult ESR = EvaluateStmt(Result, Info, SubStmt); |
| 2578 | if (ESR != ESR_Succeeded) |
| 2579 | return ESR; |
| 2580 | } |
| 2581 | return ESR_Succeeded; |
| 2582 | } |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 2583 | |
| 2584 | case Stmt::WhileStmtClass: { |
| 2585 | const WhileStmt *WS = cast<WhileStmt>(S); |
| 2586 | while (true) { |
| 2587 | bool Continue; |
| 2588 | if (!EvaluateCond(Info, WS->getConditionVariable(), WS->getCond(), |
| 2589 | Continue)) |
| 2590 | return ESR_Failed; |
| 2591 | if (!Continue) |
| 2592 | break; |
| 2593 | |
| 2594 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, WS->getBody()); |
| 2595 | if (ESR != ESR_Continue) |
| 2596 | return ESR; |
| 2597 | } |
| 2598 | return ESR_Succeeded; |
| 2599 | } |
| 2600 | |
| 2601 | case Stmt::DoStmtClass: { |
| 2602 | const DoStmt *DS = cast<DoStmt>(S); |
| 2603 | bool Continue; |
| 2604 | do { |
| 2605 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, DS->getBody()); |
| 2606 | if (ESR != ESR_Continue) |
| 2607 | return ESR; |
| 2608 | |
| 2609 | if (!EvaluateAsBooleanCondition(DS->getCond(), Continue, Info)) |
| 2610 | return ESR_Failed; |
| 2611 | } while (Continue); |
| 2612 | return ESR_Succeeded; |
| 2613 | } |
| 2614 | |
| 2615 | case Stmt::ForStmtClass: { |
| 2616 | const ForStmt *FS = cast<ForStmt>(S); |
| 2617 | if (FS->getInit()) { |
| 2618 | EvalStmtResult ESR = EvaluateStmt(Result, Info, FS->getInit()); |
| 2619 | if (ESR != ESR_Succeeded) |
| 2620 | return ESR; |
| 2621 | } |
| 2622 | while (true) { |
| 2623 | bool Continue = true; |
| 2624 | if (FS->getCond() && !EvaluateCond(Info, FS->getConditionVariable(), |
| 2625 | FS->getCond(), Continue)) |
| 2626 | return ESR_Failed; |
| 2627 | if (!Continue) |
| 2628 | break; |
| 2629 | |
| 2630 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, FS->getBody()); |
| 2631 | if (ESR != ESR_Continue) |
| 2632 | return ESR; |
| 2633 | |
| 2634 | if (FS->getInc() && !EvaluateIgnoredValue(Info, FS->getInc())) |
| 2635 | return ESR_Failed; |
| 2636 | } |
| 2637 | return ESR_Succeeded; |
| 2638 | } |
| 2639 | |
Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 2640 | case Stmt::CXXForRangeStmtClass: { |
| 2641 | const CXXForRangeStmt *FS = cast<CXXForRangeStmt>(S); |
| 2642 | |
| 2643 | // Initialize the __range variable. |
| 2644 | EvalStmtResult ESR = EvaluateStmt(Result, Info, FS->getRangeStmt()); |
| 2645 | if (ESR != ESR_Succeeded) |
| 2646 | return ESR; |
| 2647 | |
| 2648 | // Create the __begin and __end iterators. |
| 2649 | ESR = EvaluateStmt(Result, Info, FS->getBeginEndStmt()); |
| 2650 | if (ESR != ESR_Succeeded) |
| 2651 | return ESR; |
| 2652 | |
| 2653 | while (true) { |
| 2654 | // Condition: __begin != __end. |
| 2655 | bool Continue = true; |
| 2656 | if (!EvaluateAsBooleanCondition(FS->getCond(), Continue, Info)) |
| 2657 | return ESR_Failed; |
| 2658 | if (!Continue) |
| 2659 | break; |
| 2660 | |
| 2661 | // User's variable declaration, initialized by *__begin. |
| 2662 | ESR = EvaluateStmt(Result, Info, FS->getLoopVarStmt()); |
| 2663 | if (ESR != ESR_Succeeded) |
| 2664 | return ESR; |
| 2665 | |
| 2666 | // Loop body. |
| 2667 | ESR = EvaluateLoopBody(Result, Info, FS->getBody()); |
| 2668 | if (ESR != ESR_Continue) |
| 2669 | return ESR; |
| 2670 | |
| 2671 | // Increment: ++__begin |
| 2672 | if (!EvaluateIgnoredValue(Info, FS->getInc())) |
| 2673 | return ESR_Failed; |
| 2674 | } |
| 2675 | |
| 2676 | return ESR_Succeeded; |
| 2677 | } |
| 2678 | |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 2679 | case Stmt::ContinueStmtClass: |
| 2680 | return ESR_Continue; |
| 2681 | |
| 2682 | case Stmt::BreakStmtClass: |
| 2683 | return ESR_Break; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2684 | } |
| 2685 | } |
| 2686 | |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 2687 | /// CheckTrivialDefaultConstructor - Check whether a constructor is a trivial |
| 2688 | /// default constructor. If so, we'll fold it whether or not it's marked as |
| 2689 | /// constexpr. If it is marked as constexpr, we will never implicitly define it, |
| 2690 | /// so we need special handling. |
| 2691 | static bool CheckTrivialDefaultConstructor(EvalInfo &Info, SourceLocation Loc, |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2692 | const CXXConstructorDecl *CD, |
| 2693 | bool IsValueInitialization) { |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 2694 | if (!CD->isTrivial() || !CD->isDefaultConstructor()) |
| 2695 | return false; |
| 2696 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 2697 | // Value-initialization does not call a trivial default constructor, so such a |
| 2698 | // call is a core constant expression whether or not the constructor is |
| 2699 | // constexpr. |
| 2700 | if (!CD->isConstexpr() && !IsValueInitialization) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2701 | if (Info.getLangOpts().CPlusPlus11) { |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 2702 | // FIXME: If DiagDecl is an implicitly-declared special member function, |
| 2703 | // we should be much more explicit about why it's not constexpr. |
| 2704 | Info.CCEDiag(Loc, diag::note_constexpr_invalid_function, 1) |
| 2705 | << /*IsConstexpr*/0 << /*IsConstructor*/1 << CD; |
| 2706 | Info.Note(CD->getLocation(), diag::note_declared_at); |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 2707 | } else { |
| 2708 | Info.CCEDiag(Loc, diag::note_invalid_subexpr_in_const_expr); |
| 2709 | } |
| 2710 | } |
| 2711 | return true; |
| 2712 | } |
| 2713 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2714 | /// CheckConstexprFunction - Check that a function can be called in a constant |
| 2715 | /// expression. |
| 2716 | static bool CheckConstexprFunction(EvalInfo &Info, SourceLocation CallLoc, |
| 2717 | const FunctionDecl *Declaration, |
| 2718 | const FunctionDecl *Definition) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2719 | // Potential constant expressions can contain calls to declared, but not yet |
| 2720 | // defined, constexpr functions. |
| 2721 | if (Info.CheckingPotentialConstantExpression && !Definition && |
| 2722 | Declaration->isConstexpr()) |
| 2723 | return false; |
| 2724 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2725 | // Can we evaluate this function call? |
| 2726 | if (Definition && Definition->isConstexpr() && !Definition->isInvalidDecl()) |
| 2727 | return true; |
| 2728 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2729 | if (Info.getLangOpts().CPlusPlus11) { |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2730 | const FunctionDecl *DiagDecl = Definition ? Definition : Declaration; |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2731 | // FIXME: If DiagDecl is an implicitly-declared special member function, we |
| 2732 | // should be much more explicit about why it's not constexpr. |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2733 | Info.Diag(CallLoc, diag::note_constexpr_invalid_function, 1) |
| 2734 | << DiagDecl->isConstexpr() << isa<CXXConstructorDecl>(DiagDecl) |
| 2735 | << DiagDecl; |
| 2736 | Info.Note(DiagDecl->getLocation(), diag::note_declared_at); |
| 2737 | } else { |
| 2738 | Info.Diag(CallLoc, diag::note_invalid_subexpr_in_const_expr); |
| 2739 | } |
| 2740 | return false; |
| 2741 | } |
| 2742 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2743 | namespace { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2744 | typedef SmallVector<APValue, 8> ArgVector; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2745 | } |
| 2746 | |
| 2747 | /// EvaluateArgs - Evaluate the arguments to a function call. |
| 2748 | static bool EvaluateArgs(ArrayRef<const Expr*> Args, ArgVector &ArgValues, |
| 2749 | EvalInfo &Info) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2750 | bool Success = true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2751 | for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end(); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2752 | I != E; ++I) { |
| 2753 | if (!Evaluate(ArgValues[I - Args.begin()], Info, *I)) { |
| 2754 | // If we're checking for a potential constant expression, evaluate all |
| 2755 | // initializers even if some of them fail. |
| 2756 | if (!Info.keepEvaluatingAfterFailure()) |
| 2757 | return false; |
| 2758 | Success = false; |
| 2759 | } |
| 2760 | } |
| 2761 | return Success; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2762 | } |
| 2763 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2764 | /// Evaluate a function call. |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2765 | static bool HandleFunctionCall(SourceLocation CallLoc, |
| 2766 | const FunctionDecl *Callee, const LValue *This, |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2767 | ArrayRef<const Expr*> Args, const Stmt *Body, |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2768 | EvalInfo &Info, APValue &Result) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2769 | ArgVector ArgValues(Args.size()); |
| 2770 | if (!EvaluateArgs(Args, ArgValues, Info)) |
| 2771 | return false; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2772 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2773 | if (!Info.CheckCallLimit(CallLoc)) |
| 2774 | return false; |
| 2775 | |
| 2776 | CallStackFrame Frame(Info, CallLoc, Callee, This, ArgValues.data()); |
Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame^] | 2777 | |
| 2778 | // For a trivial copy or move assignment, perform an APValue copy. This is |
| 2779 | // essential for unions, where the operations performed by the assignment |
| 2780 | // operator cannot be represented as statements. |
| 2781 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Callee); |
| 2782 | if (MD && MD->isDefaulted() && MD->isTrivial()) { |
| 2783 | assert(This && |
| 2784 | (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator())); |
| 2785 | LValue RHS; |
| 2786 | RHS.setFrom(Info.Ctx, ArgValues[0]); |
| 2787 | APValue RHSValue; |
| 2788 | if (!handleLValueToRValueConversion(Info, Args[0], Args[0]->getType(), |
| 2789 | RHS, RHSValue)) |
| 2790 | return false; |
| 2791 | if (!handleAssignment(Info, Args[0], *This, MD->getThisType(Info.Ctx), |
| 2792 | RHSValue)) |
| 2793 | return false; |
| 2794 | This->moveInto(Result); |
| 2795 | return true; |
| 2796 | } |
| 2797 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 2798 | EvalStmtResult ESR = EvaluateStmt(Result, Info, Body); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2799 | if (ESR == ESR_Succeeded) { |
| 2800 | if (Callee->getResultType()->isVoidType()) |
| 2801 | return true; |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 2802 | Info.Diag(Callee->getLocEnd(), diag::note_constexpr_no_return); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2803 | } |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 2804 | return ESR == ESR_Returned; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2805 | } |
| 2806 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2807 | /// Evaluate a constructor call. |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2808 | static bool HandleConstructorCall(SourceLocation CallLoc, const LValue &This, |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2809 | ArrayRef<const Expr*> Args, |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2810 | const CXXConstructorDecl *Definition, |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2811 | EvalInfo &Info, APValue &Result) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2812 | ArgVector ArgValues(Args.size()); |
| 2813 | if (!EvaluateArgs(Args, ArgValues, Info)) |
| 2814 | return false; |
| 2815 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2816 | if (!Info.CheckCallLimit(CallLoc)) |
| 2817 | return false; |
| 2818 | |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 2819 | const CXXRecordDecl *RD = Definition->getParent(); |
| 2820 | if (RD->getNumVBases()) { |
| 2821 | Info.Diag(CallLoc, diag::note_constexpr_virtual_base) << RD; |
| 2822 | return false; |
| 2823 | } |
| 2824 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2825 | CallStackFrame Frame(Info, CallLoc, Definition, &This, ArgValues.data()); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2826 | |
| 2827 | // If it's a delegating constructor, just delegate. |
| 2828 | if (Definition->isDelegatingConstructor()) { |
| 2829 | CXXConstructorDecl::init_const_iterator I = Definition->init_begin(); |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 2830 | if (!EvaluateInPlace(Result, Info, This, (*I)->getInit())) |
| 2831 | return false; |
| 2832 | return EvaluateStmt(Result, Info, Definition->getBody()) != ESR_Failed; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2833 | } |
| 2834 | |
Richard Smith | 1bc5c2c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 2835 | // For a trivial copy or move constructor, perform an APValue copy. This is |
| 2836 | // essential for unions, where the operations performed by the constructor |
| 2837 | // cannot be represented by ctor-initializers. |
Richard Smith | 1bc5c2c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 2838 | if (Definition->isDefaulted() && |
Douglas Gregor | 093d4be | 2012-02-24 07:55:51 +0000 | [diff] [blame] | 2839 | ((Definition->isCopyConstructor() && Definition->isTrivial()) || |
| 2840 | (Definition->isMoveConstructor() && Definition->isTrivial()))) { |
Richard Smith | 1bc5c2c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 2841 | LValue RHS; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2842 | RHS.setFrom(Info.Ctx, ArgValues[0]); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 2843 | return handleLValueToRValueConversion(Info, Args[0], Args[0]->getType(), |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2844 | RHS, Result); |
Richard Smith | 1bc5c2c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 2845 | } |
| 2846 | |
| 2847 | // Reserve space for the struct members. |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2848 | if (!RD->isUnion() && Result.isUninit()) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2849 | Result = APValue(APValue::UninitStruct(), RD->getNumBases(), |
| 2850 | std::distance(RD->field_begin(), RD->field_end())); |
| 2851 | |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2852 | if (RD->isInvalidDecl()) return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2853 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 2854 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2855 | bool Success = true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2856 | unsigned BasesSeen = 0; |
| 2857 | #ifndef NDEBUG |
| 2858 | CXXRecordDecl::base_class_const_iterator BaseIt = RD->bases_begin(); |
| 2859 | #endif |
| 2860 | for (CXXConstructorDecl::init_const_iterator I = Definition->init_begin(), |
| 2861 | E = Definition->init_end(); I != E; ++I) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2862 | LValue Subobject = This; |
| 2863 | APValue *Value = &Result; |
| 2864 | |
| 2865 | // Determine the subobject to initialize. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2866 | if ((*I)->isBaseInitializer()) { |
| 2867 | QualType BaseType((*I)->getBaseClass(), 0); |
| 2868 | #ifndef NDEBUG |
| 2869 | // Non-virtual base classes are initialized in the order in the class |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 2870 | // definition. We have already checked for virtual base classes. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2871 | assert(!BaseIt->isVirtual() && "virtual base for literal type"); |
| 2872 | assert(Info.Ctx.hasSameType(BaseIt->getType(), BaseType) && |
| 2873 | "base class initializers not in expected order"); |
| 2874 | ++BaseIt; |
| 2875 | #endif |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2876 | if (!HandleLValueDirectBase(Info, (*I)->getInit(), Subobject, RD, |
| 2877 | BaseType->getAsCXXRecordDecl(), &Layout)) |
| 2878 | return false; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2879 | Value = &Result.getStructBase(BasesSeen++); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2880 | } else if (FieldDecl *FD = (*I)->getMember()) { |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2881 | if (!HandleLValueMember(Info, (*I)->getInit(), Subobject, FD, &Layout)) |
| 2882 | return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2883 | if (RD->isUnion()) { |
| 2884 | Result = APValue(FD); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2885 | Value = &Result.getUnionValue(); |
| 2886 | } else { |
| 2887 | Value = &Result.getStructField(FD->getFieldIndex()); |
| 2888 | } |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2889 | } else if (IndirectFieldDecl *IFD = (*I)->getIndirectMember()) { |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2890 | // Walk the indirect field decl's chain to find the object to initialize, |
| 2891 | // and make sure we've initialized every step along it. |
| 2892 | for (IndirectFieldDecl::chain_iterator C = IFD->chain_begin(), |
| 2893 | CE = IFD->chain_end(); |
| 2894 | C != CE; ++C) { |
| 2895 | FieldDecl *FD = cast<FieldDecl>(*C); |
| 2896 | CXXRecordDecl *CD = cast<CXXRecordDecl>(FD->getParent()); |
| 2897 | // Switch the union field if it differs. This happens if we had |
| 2898 | // preceding zero-initialization, and we're now initializing a union |
| 2899 | // subobject other than the first. |
| 2900 | // FIXME: In this case, the values of the other subobjects are |
| 2901 | // specified, since zero-initialization sets all padding bits to zero. |
| 2902 | if (Value->isUninit() || |
| 2903 | (Value->isUnion() && Value->getUnionField() != FD)) { |
| 2904 | if (CD->isUnion()) |
| 2905 | *Value = APValue(FD); |
| 2906 | else |
| 2907 | *Value = APValue(APValue::UninitStruct(), CD->getNumBases(), |
| 2908 | std::distance(CD->field_begin(), CD->field_end())); |
| 2909 | } |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2910 | if (!HandleLValueMember(Info, (*I)->getInit(), Subobject, FD)) |
| 2911 | return false; |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2912 | if (CD->isUnion()) |
| 2913 | Value = &Value->getUnionValue(); |
| 2914 | else |
| 2915 | Value = &Value->getStructField(FD->getFieldIndex()); |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2916 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2917 | } else { |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2918 | llvm_unreachable("unknown base initializer kind"); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2919 | } |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2920 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2921 | if (!EvaluateInPlace(*Value, Info, Subobject, (*I)->getInit(), |
| 2922 | (*I)->isBaseInitializer() |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2923 | ? CCEK_Constant : CCEK_MemberInit)) { |
| 2924 | // If we're checking for a potential constant expression, evaluate all |
| 2925 | // initializers even if some of them fail. |
| 2926 | if (!Info.keepEvaluatingAfterFailure()) |
| 2927 | return false; |
| 2928 | Success = false; |
| 2929 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2930 | } |
| 2931 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 2932 | return Success && |
| 2933 | EvaluateStmt(Result, Info, Definition->getBody()) != ESR_Failed; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2934 | } |
| 2935 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2936 | //===----------------------------------------------------------------------===// |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2937 | // Generic Evaluation |
| 2938 | //===----------------------------------------------------------------------===// |
| 2939 | namespace { |
| 2940 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2941 | // FIXME: RetTy is always bool. Remove it. |
| 2942 | template <class Derived, typename RetTy=bool> |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2943 | class ExprEvaluatorBase |
| 2944 | : public ConstStmtVisitor<Derived, RetTy> { |
| 2945 | private: |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2946 | RetTy DerivedSuccess(const APValue &V, const Expr *E) { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2947 | return static_cast<Derived*>(this)->Success(V, E); |
| 2948 | } |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2949 | RetTy DerivedZeroInitialization(const Expr *E) { |
| 2950 | return static_cast<Derived*>(this)->ZeroInitialization(E); |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2951 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2952 | |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 2953 | // Check whether a conditional operator with a non-constant condition is a |
| 2954 | // potential constant expression. If neither arm is a potential constant |
| 2955 | // expression, then the conditional operator is not either. |
| 2956 | template<typename ConditionalOperator> |
| 2957 | void CheckPotentialConstantConditional(const ConditionalOperator *E) { |
| 2958 | assert(Info.CheckingPotentialConstantExpression); |
| 2959 | |
| 2960 | // Speculatively evaluate both arms. |
| 2961 | { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2962 | SmallVector<PartialDiagnosticAt, 8> Diag; |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 2963 | SpeculativeEvaluationRAII Speculate(Info, &Diag); |
| 2964 | |
| 2965 | StmtVisitorTy::Visit(E->getFalseExpr()); |
| 2966 | if (Diag.empty()) |
| 2967 | return; |
| 2968 | |
| 2969 | Diag.clear(); |
| 2970 | StmtVisitorTy::Visit(E->getTrueExpr()); |
| 2971 | if (Diag.empty()) |
| 2972 | return; |
| 2973 | } |
| 2974 | |
| 2975 | Error(E, diag::note_constexpr_conditional_never_const); |
| 2976 | } |
| 2977 | |
| 2978 | |
| 2979 | template<typename ConditionalOperator> |
| 2980 | bool HandleConditionalOperator(const ConditionalOperator *E) { |
| 2981 | bool BoolResult; |
| 2982 | if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info)) { |
| 2983 | if (Info.CheckingPotentialConstantExpression) |
| 2984 | CheckPotentialConstantConditional(E); |
| 2985 | return false; |
| 2986 | } |
| 2987 | |
| 2988 | Expr *EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
| 2989 | return StmtVisitorTy::Visit(EvalExpr); |
| 2990 | } |
| 2991 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2992 | protected: |
| 2993 | EvalInfo &Info; |
| 2994 | typedef ConstStmtVisitor<Derived, RetTy> StmtVisitorTy; |
| 2995 | typedef ExprEvaluatorBase ExprEvaluatorBaseTy; |
| 2996 | |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 2997 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 2998 | return Info.CCEDiag(E, D); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2999 | } |
| 3000 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 3001 | RetTy ZeroInitialization(const Expr *E) { return Error(E); } |
| 3002 | |
| 3003 | public: |
| 3004 | ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {} |
| 3005 | |
| 3006 | EvalInfo &getEvalInfo() { return Info; } |
| 3007 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3008 | /// Report an evaluation error. This should only be called when an error is |
| 3009 | /// first discovered. When propagating an error, just return false. |
| 3010 | bool Error(const Expr *E, diag::kind D) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 3011 | Info.Diag(E, D); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3012 | return false; |
| 3013 | } |
| 3014 | bool Error(const Expr *E) { |
| 3015 | return Error(E, diag::note_invalid_subexpr_in_const_expr); |
| 3016 | } |
| 3017 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3018 | RetTy VisitStmt(const Stmt *) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3019 | llvm_unreachable("Expression evaluator should not be called on stmts"); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3020 | } |
| 3021 | RetTy VisitExpr(const Expr *E) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3022 | return Error(E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3023 | } |
| 3024 | |
| 3025 | RetTy VisitParenExpr(const ParenExpr *E) |
| 3026 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| 3027 | RetTy VisitUnaryExtension(const UnaryOperator *E) |
| 3028 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| 3029 | RetTy VisitUnaryPlus(const UnaryOperator *E) |
| 3030 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| 3031 | RetTy VisitChooseExpr(const ChooseExpr *E) |
| 3032 | { return StmtVisitorTy::Visit(E->getChosenSubExpr(Info.Ctx)); } |
| 3033 | RetTy VisitGenericSelectionExpr(const GenericSelectionExpr *E) |
| 3034 | { return StmtVisitorTy::Visit(E->getResultExpr()); } |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3035 | RetTy VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E) |
| 3036 | { return StmtVisitorTy::Visit(E->getReplacement()); } |
Richard Smith | f8120ca | 2011-11-09 02:12:41 +0000 | [diff] [blame] | 3037 | RetTy VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) |
| 3038 | { return StmtVisitorTy::Visit(E->getExpr()); } |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 3039 | RetTy VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *E) |
| 3040 | { return StmtVisitorTy::Visit(E->getExpr()); } |
Richard Smith | 5894a91 | 2011-12-19 22:12:41 +0000 | [diff] [blame] | 3041 | // We cannot create any objects for which cleanups are required, so there is |
| 3042 | // nothing to do here; all cleanups must come from unevaluated subexpressions. |
| 3043 | RetTy VisitExprWithCleanups(const ExprWithCleanups *E) |
| 3044 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3045 | |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 3046 | RetTy VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E) { |
| 3047 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 0; |
| 3048 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
| 3049 | } |
| 3050 | RetTy VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) { |
| 3051 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 1; |
| 3052 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
| 3053 | } |
| 3054 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3055 | RetTy VisitBinaryOperator(const BinaryOperator *E) { |
| 3056 | switch (E->getOpcode()) { |
| 3057 | default: |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3058 | return Error(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3059 | |
| 3060 | case BO_Comma: |
| 3061 | VisitIgnoredValue(E->getLHS()); |
| 3062 | return StmtVisitorTy::Visit(E->getRHS()); |
| 3063 | |
| 3064 | case BO_PtrMemD: |
| 3065 | case BO_PtrMemI: { |
| 3066 | LValue Obj; |
| 3067 | if (!HandleMemberPointerAccess(Info, E, Obj)) |
| 3068 | return false; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3069 | APValue Result; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3070 | if (!handleLValueToRValueConversion(Info, E, E->getType(), Obj, Result)) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3071 | return false; |
| 3072 | return DerivedSuccess(Result, E); |
| 3073 | } |
| 3074 | } |
| 3075 | } |
| 3076 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3077 | RetTy VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) { |
Richard Smith | 26d4cc1 | 2012-06-26 08:12:11 +0000 | [diff] [blame] | 3078 | // Evaluate and cache the common expression. We treat it as a temporary, |
| 3079 | // even though it's not quite the same thing. |
| 3080 | if (!Evaluate(Info.CurrentCall->Temporaries[E->getOpaqueValue()], |
| 3081 | Info, E->getCommon())) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3082 | return false; |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3083 | |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 3084 | return HandleConditionalOperator(E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3085 | } |
| 3086 | |
| 3087 | RetTy VisitConditionalOperator(const ConditionalOperator *E) { |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 3088 | bool IsBcpCall = false; |
| 3089 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
| 3090 | // the result is a constant expression if it can be folded without |
| 3091 | // side-effects. This is an important GNU extension. See GCC PR38377 |
| 3092 | // for discussion. |
| 3093 | if (const CallExpr *CallCE = |
| 3094 | dyn_cast<CallExpr>(E->getCond()->IgnoreParenCasts())) |
| 3095 | if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p) |
| 3096 | IsBcpCall = true; |
| 3097 | |
| 3098 | // Always assume __builtin_constant_p(...) ? ... : ... is a potential |
| 3099 | // constant expression; we can't check whether it's potentially foldable. |
| 3100 | if (Info.CheckingPotentialConstantExpression && IsBcpCall) |
| 3101 | return false; |
| 3102 | |
| 3103 | FoldConstant Fold(Info); |
| 3104 | |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 3105 | if (!HandleConditionalOperator(E)) |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 3106 | return false; |
| 3107 | |
| 3108 | if (IsBcpCall) |
| 3109 | Fold.Fold(Info); |
| 3110 | |
| 3111 | return true; |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3112 | } |
| 3113 | |
| 3114 | RetTy VisitOpaqueValueExpr(const OpaqueValueExpr *E) { |
Richard Smith | 26d4cc1 | 2012-06-26 08:12:11 +0000 | [diff] [blame] | 3115 | APValue &Value = Info.CurrentCall->Temporaries[E]; |
| 3116 | if (Value.isUninit()) { |
Argyrios Kyrtzidis | fac35c0 | 2011-12-09 02:44:48 +0000 | [diff] [blame] | 3117 | const Expr *Source = E->getSourceExpr(); |
| 3118 | if (!Source) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3119 | return Error(E); |
Argyrios Kyrtzidis | fac35c0 | 2011-12-09 02:44:48 +0000 | [diff] [blame] | 3120 | if (Source == E) { // sanity checking. |
| 3121 | assert(0 && "OpaqueValueExpr recursively refers to itself"); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3122 | return Error(E); |
Argyrios Kyrtzidis | fac35c0 | 2011-12-09 02:44:48 +0000 | [diff] [blame] | 3123 | } |
| 3124 | return StmtVisitorTy::Visit(Source); |
| 3125 | } |
Richard Smith | 26d4cc1 | 2012-06-26 08:12:11 +0000 | [diff] [blame] | 3126 | return DerivedSuccess(Value, E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3127 | } |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 3128 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3129 | RetTy VisitCallExpr(const CallExpr *E) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3130 | const Expr *Callee = E->getCallee()->IgnoreParens(); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3131 | QualType CalleeType = Callee->getType(); |
| 3132 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3133 | const FunctionDecl *FD = 0; |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3134 | LValue *This = 0, ThisVal; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3135 | ArrayRef<const Expr *> Args(E->getArgs(), E->getNumArgs()); |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 3136 | bool HasQualifier = false; |
Richard Smith | 656d49d | 2011-11-10 09:31:24 +0000 | [diff] [blame] | 3137 | |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3138 | // Extract function decl and 'this' pointer from the callee. |
| 3139 | if (CalleeType->isSpecificBuiltinType(BuiltinType::BoundMember)) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3140 | const ValueDecl *Member = 0; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3141 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(Callee)) { |
| 3142 | // Explicit bound member calls, such as x.f() or p->g(); |
| 3143 | if (!EvaluateObjectArgument(Info, ME->getBase(), ThisVal)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3144 | return false; |
| 3145 | Member = ME->getMemberDecl(); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3146 | This = &ThisVal; |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 3147 | HasQualifier = ME->hasQualifier(); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3148 | } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(Callee)) { |
| 3149 | // Indirect bound member calls ('.*' or '->*'). |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3150 | Member = HandleMemberPointerAccess(Info, BE, ThisVal, false); |
| 3151 | if (!Member) return false; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3152 | This = &ThisVal; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3153 | } else |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3154 | return Error(Callee); |
| 3155 | |
| 3156 | FD = dyn_cast<FunctionDecl>(Member); |
| 3157 | if (!FD) |
| 3158 | return Error(Callee); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3159 | } else if (CalleeType->isFunctionPointerType()) { |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3160 | LValue Call; |
| 3161 | if (!EvaluatePointer(Callee, Call, Info)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3162 | return false; |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3163 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3164 | if (!Call.getLValueOffset().isZero()) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3165 | return Error(Callee); |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 3166 | FD = dyn_cast_or_null<FunctionDecl>( |
| 3167 | Call.getLValueBase().dyn_cast<const ValueDecl*>()); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3168 | if (!FD) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3169 | return Error(Callee); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3170 | |
| 3171 | // Overloaded operator calls to member functions are represented as normal |
| 3172 | // calls with '*this' as the first argument. |
| 3173 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
| 3174 | if (MD && !MD->isStatic()) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3175 | // FIXME: When selecting an implicit conversion for an overloaded |
| 3176 | // operator delete, we sometimes try to evaluate calls to conversion |
| 3177 | // operators without a 'this' parameter! |
| 3178 | if (Args.empty()) |
| 3179 | return Error(E); |
| 3180 | |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3181 | if (!EvaluateObjectArgument(Info, Args[0], ThisVal)) |
| 3182 | return false; |
| 3183 | This = &ThisVal; |
| 3184 | Args = Args.slice(1); |
| 3185 | } |
| 3186 | |
| 3187 | // Don't call function pointers which have been cast to some other type. |
| 3188 | if (!Info.Ctx.hasSameType(CalleeType->getPointeeType(), FD->getType())) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3189 | return Error(E); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3190 | } else |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3191 | return Error(E); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3192 | |
Richard Smith | 47b3493 | 2012-02-01 02:39:43 +0000 | [diff] [blame] | 3193 | if (This && !This->checkSubobject(Info, E, CSK_This)) |
| 3194 | return false; |
| 3195 | |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 3196 | // DR1358 allows virtual constexpr functions in some cases. Don't allow |
| 3197 | // calls to such functions in constant expressions. |
| 3198 | if (This && !HasQualifier && |
| 3199 | isa<CXXMethodDecl>(FD) && cast<CXXMethodDecl>(FD)->isVirtual()) |
| 3200 | return Error(E, diag::note_constexpr_virtual_call); |
| 3201 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3202 | const FunctionDecl *Definition = 0; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3203 | Stmt *Body = FD->getBody(Definition); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3204 | APValue Result; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3205 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3206 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition) || |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3207 | !HandleFunctionCall(E->getExprLoc(), Definition, This, Args, Body, |
| 3208 | Info, Result)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3209 | return false; |
| 3210 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3211 | return DerivedSuccess(Result, E); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3212 | } |
| 3213 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3214 | RetTy VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
| 3215 | return StmtVisitorTy::Visit(E->getInitializer()); |
| 3216 | } |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 3217 | RetTy VisitInitListExpr(const InitListExpr *E) { |
Eli Friedman | 90dc175 | 2012-01-03 23:54:05 +0000 | [diff] [blame] | 3218 | if (E->getNumInits() == 0) |
| 3219 | return DerivedZeroInitialization(E); |
| 3220 | if (E->getNumInits() == 1) |
| 3221 | return StmtVisitorTy::Visit(E->getInit(0)); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3222 | return Error(E); |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 3223 | } |
| 3224 | RetTy VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3225 | return DerivedZeroInitialization(E); |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 3226 | } |
| 3227 | RetTy VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3228 | return DerivedZeroInitialization(E); |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 3229 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3230 | RetTy VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3231 | return DerivedZeroInitialization(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3232 | } |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 3233 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3234 | /// A member expression where the object is a prvalue is itself a prvalue. |
| 3235 | RetTy VisitMemberExpr(const MemberExpr *E) { |
| 3236 | assert(!E->isArrow() && "missing call to bound member function?"); |
| 3237 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3238 | APValue Val; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3239 | if (!Evaluate(Val, Info, E->getBase())) |
| 3240 | return false; |
| 3241 | |
| 3242 | QualType BaseTy = E->getBase()->getType(); |
| 3243 | |
| 3244 | const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3245 | if (!FD) return Error(E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3246 | assert(!FD->getType()->isReferenceType() && "prvalue reference?"); |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 3247 | assert(BaseTy->castAs<RecordType>()->getDecl()->getCanonicalDecl() == |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3248 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
| 3249 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3250 | CompleteObject Obj(&Val, BaseTy); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3251 | SubobjectDesignator Designator(BaseTy); |
| 3252 | Designator.addDeclUnchecked(FD); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3253 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3254 | APValue Result; |
| 3255 | return extractSubobject(Info, E, Obj, Designator, Result) && |
| 3256 | DerivedSuccess(Result, E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3257 | } |
| 3258 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3259 | RetTy VisitCastExpr(const CastExpr *E) { |
| 3260 | switch (E->getCastKind()) { |
| 3261 | default: |
| 3262 | break; |
| 3263 | |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 3264 | case CK_AtomicToNonAtomic: |
| 3265 | case CK_NonAtomicToAtomic: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3266 | case CK_NoOp: |
Richard Smith | 4ef685b | 2012-01-17 21:17:26 +0000 | [diff] [blame] | 3267 | case CK_UserDefinedConversion: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3268 | return StmtVisitorTy::Visit(E->getSubExpr()); |
| 3269 | |
| 3270 | case CK_LValueToRValue: { |
| 3271 | LValue LVal; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3272 | if (!EvaluateLValue(E->getSubExpr(), LVal, Info)) |
| 3273 | return false; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3274 | APValue RVal; |
Richard Smith | c82fae6 | 2012-02-05 01:23:16 +0000 | [diff] [blame] | 3275 | // Note, we use the subexpression's type in order to retain cv-qualifiers. |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3276 | if (!handleLValueToRValueConversion(Info, E, E->getSubExpr()->getType(), |
Richard Smith | c82fae6 | 2012-02-05 01:23:16 +0000 | [diff] [blame] | 3277 | LVal, RVal)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3278 | return false; |
| 3279 | return DerivedSuccess(RVal, E); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3280 | } |
| 3281 | } |
| 3282 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3283 | return Error(E); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3284 | } |
| 3285 | |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3286 | RetTy VisitUnaryPostInc(const UnaryOperator *UO) { |
| 3287 | return VisitUnaryPostIncDec(UO); |
| 3288 | } |
| 3289 | RetTy VisitUnaryPostDec(const UnaryOperator *UO) { |
| 3290 | return VisitUnaryPostIncDec(UO); |
| 3291 | } |
| 3292 | RetTy VisitUnaryPostIncDec(const UnaryOperator *UO) { |
| 3293 | if (!Info.getLangOpts().CPlusPlus1y && !Info.keepEvaluatingAfterFailure()) |
| 3294 | return Error(UO); |
| 3295 | |
| 3296 | LValue LVal; |
| 3297 | if (!EvaluateLValue(UO->getSubExpr(), LVal, Info)) |
| 3298 | return false; |
| 3299 | APValue RVal; |
| 3300 | if (!handleIncDec(this->Info, UO, LVal, UO->getSubExpr()->getType(), |
| 3301 | UO->isIncrementOp(), &RVal)) |
| 3302 | return false; |
| 3303 | return DerivedSuccess(RVal, UO); |
| 3304 | } |
| 3305 | |
Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 3306 | /// Visit a value which is evaluated, but whose value is ignored. |
| 3307 | void VisitIgnoredValue(const Expr *E) { |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3308 | EvaluateIgnoredValue(Info, E); |
Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 3309 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3310 | }; |
| 3311 | |
| 3312 | } |
| 3313 | |
| 3314 | //===----------------------------------------------------------------------===// |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3315 | // Common base class for lvalue and temporary evaluation. |
| 3316 | //===----------------------------------------------------------------------===// |
| 3317 | namespace { |
| 3318 | template<class Derived> |
| 3319 | class LValueExprEvaluatorBase |
| 3320 | : public ExprEvaluatorBase<Derived, bool> { |
| 3321 | protected: |
| 3322 | LValue &Result; |
| 3323 | typedef LValueExprEvaluatorBase LValueExprEvaluatorBaseTy; |
| 3324 | typedef ExprEvaluatorBase<Derived, bool> ExprEvaluatorBaseTy; |
| 3325 | |
| 3326 | bool Success(APValue::LValueBase B) { |
| 3327 | Result.set(B); |
| 3328 | return true; |
| 3329 | } |
| 3330 | |
| 3331 | public: |
| 3332 | LValueExprEvaluatorBase(EvalInfo &Info, LValue &Result) : |
| 3333 | ExprEvaluatorBaseTy(Info), Result(Result) {} |
| 3334 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3335 | bool Success(const APValue &V, const Expr *E) { |
| 3336 | Result.setFrom(this->Info.Ctx, V); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3337 | return true; |
| 3338 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3339 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3340 | bool VisitMemberExpr(const MemberExpr *E) { |
| 3341 | // Handle non-static data members. |
| 3342 | QualType BaseTy; |
| 3343 | if (E->isArrow()) { |
| 3344 | if (!EvaluatePointer(E->getBase(), Result, this->Info)) |
| 3345 | return false; |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 3346 | BaseTy = E->getBase()->getType()->castAs<PointerType>()->getPointeeType(); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3347 | } else if (E->getBase()->isRValue()) { |
Richard Smith | d0b111c | 2011-12-19 22:01:37 +0000 | [diff] [blame] | 3348 | assert(E->getBase()->getType()->isRecordType()); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3349 | if (!EvaluateTemporary(E->getBase(), Result, this->Info)) |
| 3350 | return false; |
| 3351 | BaseTy = E->getBase()->getType(); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3352 | } else { |
| 3353 | if (!this->Visit(E->getBase())) |
| 3354 | return false; |
| 3355 | BaseTy = E->getBase()->getType(); |
| 3356 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3357 | |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 3358 | const ValueDecl *MD = E->getMemberDecl(); |
| 3359 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) { |
| 3360 | assert(BaseTy->getAs<RecordType>()->getDecl()->getCanonicalDecl() == |
| 3361 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
| 3362 | (void)BaseTy; |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3363 | if (!HandleLValueMember(this->Info, E, Result, FD)) |
| 3364 | return false; |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 3365 | } else if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(MD)) { |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3366 | if (!HandleLValueIndirectMember(this->Info, E, Result, IFD)) |
| 3367 | return false; |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 3368 | } else |
| 3369 | return this->Error(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3370 | |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 3371 | if (MD->getType()->isReferenceType()) { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3372 | APValue RefValue; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3373 | if (!handleLValueToRValueConversion(this->Info, E, MD->getType(), Result, |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3374 | RefValue)) |
| 3375 | return false; |
| 3376 | return Success(RefValue, E); |
| 3377 | } |
| 3378 | return true; |
| 3379 | } |
| 3380 | |
| 3381 | bool VisitBinaryOperator(const BinaryOperator *E) { |
| 3382 | switch (E->getOpcode()) { |
| 3383 | default: |
| 3384 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
| 3385 | |
| 3386 | case BO_PtrMemD: |
| 3387 | case BO_PtrMemI: |
| 3388 | return HandleMemberPointerAccess(this->Info, E, Result); |
| 3389 | } |
| 3390 | } |
| 3391 | |
| 3392 | bool VisitCastExpr(const CastExpr *E) { |
| 3393 | switch (E->getCastKind()) { |
| 3394 | default: |
| 3395 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 3396 | |
| 3397 | case CK_DerivedToBase: |
| 3398 | case CK_UncheckedDerivedToBase: { |
| 3399 | if (!this->Visit(E->getSubExpr())) |
| 3400 | return false; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3401 | |
| 3402 | // Now figure out the necessary offset to add to the base LV to get from |
| 3403 | // the derived class to the base class. |
| 3404 | QualType Type = E->getSubExpr()->getType(); |
| 3405 | |
| 3406 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 3407 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3408 | if (!HandleLValueBase(this->Info, E, Result, Type->getAsCXXRecordDecl(), |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3409 | *PathI)) |
| 3410 | return false; |
| 3411 | Type = (*PathI)->getType(); |
| 3412 | } |
| 3413 | |
| 3414 | return true; |
| 3415 | } |
| 3416 | } |
| 3417 | } |
| 3418 | }; |
| 3419 | } |
| 3420 | |
| 3421 | //===----------------------------------------------------------------------===// |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3422 | // LValue Evaluation |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3423 | // |
| 3424 | // This is used for evaluating lvalues (in C and C++), xvalues (in C++11), |
| 3425 | // function designators (in C), decl references to void objects (in C), and |
| 3426 | // temporaries (if building with -Wno-address-of-temporary). |
| 3427 | // |
| 3428 | // LValue evaluation produces values comprising a base expression of one of the |
| 3429 | // following types: |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 3430 | // - Declarations |
| 3431 | // * VarDecl |
| 3432 | // * FunctionDecl |
| 3433 | // - Literals |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3434 | // * CompoundLiteralExpr in C |
| 3435 | // * StringLiteral |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 3436 | // * CXXTypeidExpr |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3437 | // * PredefinedExpr |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3438 | // * ObjCStringLiteralExpr |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3439 | // * ObjCEncodeExpr |
| 3440 | // * AddrLabelExpr |
| 3441 | // * BlockExpr |
| 3442 | // * CallExpr for a MakeStringConstant builtin |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 3443 | // - Locals and temporaries |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3444 | // * Any Expr, with a CallIndex indicating the function in which the temporary |
| 3445 | // was evaluated. |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 3446 | // plus an offset in bytes. |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3447 | //===----------------------------------------------------------------------===// |
| 3448 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 3449 | class LValueExprEvaluator |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3450 | : public LValueExprEvaluatorBase<LValueExprEvaluator> { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3451 | public: |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3452 | LValueExprEvaluator(EvalInfo &Info, LValue &Result) : |
| 3453 | LValueExprEvaluatorBaseTy(Info, Result) {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3454 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3455 | bool VisitVarDecl(const Expr *E, const VarDecl *VD); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3456 | bool VisitUnaryPreIncDec(const UnaryOperator *UO); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3457 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3458 | bool VisitDeclRefExpr(const DeclRefExpr *E); |
| 3459 | bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(E); } |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 3460 | bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3461 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
| 3462 | bool VisitMemberExpr(const MemberExpr *E); |
| 3463 | bool VisitStringLiteral(const StringLiteral *E) { return Success(E); } |
| 3464 | bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(E); } |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 3465 | bool VisitCXXTypeidExpr(const CXXTypeidExpr *E); |
Francois Pichet | 0066db9 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 3466 | bool VisitCXXUuidofExpr(const CXXUuidofExpr *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3467 | bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E); |
| 3468 | bool VisitUnaryDeref(const UnaryOperator *E); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 3469 | bool VisitUnaryReal(const UnaryOperator *E); |
| 3470 | bool VisitUnaryImag(const UnaryOperator *E); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3471 | bool VisitUnaryPreInc(const UnaryOperator *UO) { |
| 3472 | return VisitUnaryPreIncDec(UO); |
| 3473 | } |
| 3474 | bool VisitUnaryPreDec(const UnaryOperator *UO) { |
| 3475 | return VisitUnaryPreIncDec(UO); |
| 3476 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3477 | bool VisitBinAssign(const BinaryOperator *BO); |
| 3478 | bool VisitCompoundAssignOperator(const CompoundAssignOperator *CAO); |
Anders Carlsson | de55f64 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 3479 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3480 | bool VisitCastExpr(const CastExpr *E) { |
Anders Carlsson | de55f64 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 3481 | switch (E->getCastKind()) { |
| 3482 | default: |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3483 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
Anders Carlsson | de55f64 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 3484 | |
Eli Friedman | ce3e02a | 2011-10-11 00:13:24 +0000 | [diff] [blame] | 3485 | case CK_LValueBitCast: |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 3486 | this->CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 3487 | if (!Visit(E->getSubExpr())) |
| 3488 | return false; |
| 3489 | Result.Designator.setInvalid(); |
| 3490 | return true; |
Eli Friedman | ce3e02a | 2011-10-11 00:13:24 +0000 | [diff] [blame] | 3491 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3492 | case CK_BaseToDerived: |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3493 | if (!Visit(E->getSubExpr())) |
| 3494 | return false; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3495 | return HandleBaseToDerivedCast(Info, E, Result); |
Anders Carlsson | de55f64 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 3496 | } |
| 3497 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3498 | }; |
| 3499 | } // end anonymous namespace |
| 3500 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3501 | /// Evaluate an expression as an lvalue. This can be legitimately called on |
Richard Smith | 9f8400e | 2013-05-01 19:00:39 +0000 | [diff] [blame] | 3502 | /// expressions which are not glvalues, in two cases: |
| 3503 | /// * function designators in C, and |
| 3504 | /// * "extern void" objects |
| 3505 | static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info) { |
| 3506 | assert(E->isGLValue() || E->getType()->isFunctionType() || |
| 3507 | E->getType()->isVoidType()); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3508 | return LValueExprEvaluator(Info, Result).Visit(E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3509 | } |
| 3510 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3511 | bool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) { |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 3512 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl())) |
| 3513 | return Success(FD); |
| 3514 | if (const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3515 | return VisitVarDecl(E, VD); |
| 3516 | return Error(E); |
| 3517 | } |
Richard Smith | 733237d | 2011-10-24 23:14:33 +0000 | [diff] [blame] | 3518 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3519 | bool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3520 | CallStackFrame *Frame = 0; |
| 3521 | if (VD->hasLocalStorage() && Info.CurrentCall->Index > 1) |
| 3522 | Frame = Info.CurrentCall; |
| 3523 | |
Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 3524 | if (!VD->getType()->isReferenceType()) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3525 | if (Frame) { |
| 3526 | Result.set(VD, Frame->Index); |
Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 3527 | return true; |
| 3528 | } |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 3529 | return Success(VD); |
Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 3530 | } |
Eli Friedman | 751aa72b7 | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 3531 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3532 | APValue *V; |
| 3533 | if (!evaluateVarDeclInit(Info, E, VD, Frame, V)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3534 | return false; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3535 | return Success(*V, E); |
Anders Carlsson | a42ee44 | 2008-11-24 04:41:22 +0000 | [diff] [blame] | 3536 | } |
| 3537 | |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 3538 | bool LValueExprEvaluator::VisitMaterializeTemporaryExpr( |
| 3539 | const MaterializeTemporaryExpr *E) { |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3540 | if (E->getType()->isRecordType()) |
| 3541 | return EvaluateTemporary(E->GetTemporaryExpr(), Result, Info); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3542 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3543 | Result.set(E, Info.CurrentCall->Index); |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3544 | return EvaluateInPlace(Info.CurrentCall->Temporaries[E], Info, |
| 3545 | Result, E->GetTemporaryExpr()); |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 3546 | } |
| 3547 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3548 | bool |
| 3549 | LValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3550 | assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?"); |
| 3551 | // Defer visiting the literal until the lvalue-to-rvalue conversion. We can |
| 3552 | // only see this when folding in C, so there's no standard to follow here. |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3553 | return Success(E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3554 | } |
| 3555 | |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 3556 | bool LValueExprEvaluator::VisitCXXTypeidExpr(const CXXTypeidExpr *E) { |
Richard Smith | 6f3d435 | 2012-10-17 23:52:07 +0000 | [diff] [blame] | 3557 | if (!E->isPotentiallyEvaluated()) |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 3558 | return Success(E); |
Richard Smith | 6f3d435 | 2012-10-17 23:52:07 +0000 | [diff] [blame] | 3559 | |
| 3560 | Info.Diag(E, diag::note_constexpr_typeid_polymorphic) |
| 3561 | << E->getExprOperand()->getType() |
| 3562 | << E->getExprOperand()->getSourceRange(); |
| 3563 | return false; |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 3564 | } |
| 3565 | |
Francois Pichet | 0066db9 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 3566 | bool LValueExprEvaluator::VisitCXXUuidofExpr(const CXXUuidofExpr *E) { |
| 3567 | return Success(E); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3568 | } |
Francois Pichet | 0066db9 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 3569 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3570 | bool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3571 | // Handle static data members. |
| 3572 | if (const VarDecl *VD = dyn_cast<VarDecl>(E->getMemberDecl())) { |
| 3573 | VisitIgnoredValue(E->getBase()); |
| 3574 | return VisitVarDecl(E, VD); |
| 3575 | } |
| 3576 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3577 | // Handle static member functions. |
| 3578 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl())) { |
| 3579 | if (MD->isStatic()) { |
| 3580 | VisitIgnoredValue(E->getBase()); |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 3581 | return Success(MD); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3582 | } |
| 3583 | } |
| 3584 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3585 | // Handle non-static data members. |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3586 | return LValueExprEvaluatorBaseTy::VisitMemberExpr(E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3587 | } |
| 3588 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3589 | bool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3590 | // FIXME: Deal with vectors as array subscript bases. |
| 3591 | if (E->getBase()->getType()->isVectorType()) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3592 | return Error(E); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3593 | |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 3594 | if (!EvaluatePointer(E->getBase(), Result, Info)) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3595 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3596 | |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 3597 | APSInt Index; |
| 3598 | if (!EvaluateInteger(E->getIdx(), Index, Info)) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3599 | return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3600 | int64_t IndexValue |
| 3601 | = Index.isSigned() ? Index.getSExtValue() |
| 3602 | : static_cast<int64_t>(Index.getZExtValue()); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 3603 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3604 | return HandleLValueArrayAdjustment(Info, E, Result, E->getType(), IndexValue); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 3605 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3606 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3607 | bool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3608 | return EvaluatePointer(E->getSubExpr(), Result, Info); |
Eli Friedman | 0b8337c | 2009-02-20 01:57:15 +0000 | [diff] [blame] | 3609 | } |
| 3610 | |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 3611 | bool LValueExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
| 3612 | if (!Visit(E->getSubExpr())) |
| 3613 | return false; |
| 3614 | // __real is a no-op on scalar lvalues. |
| 3615 | if (E->getSubExpr()->getType()->isAnyComplexType()) |
| 3616 | HandleLValueComplexElement(Info, E, Result, E->getType(), false); |
| 3617 | return true; |
| 3618 | } |
| 3619 | |
| 3620 | bool LValueExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
| 3621 | assert(E->getSubExpr()->getType()->isAnyComplexType() && |
| 3622 | "lvalue __imag__ on scalar?"); |
| 3623 | if (!Visit(E->getSubExpr())) |
| 3624 | return false; |
| 3625 | HandleLValueComplexElement(Info, E, Result, E->getType(), true); |
| 3626 | return true; |
| 3627 | } |
| 3628 | |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3629 | bool LValueExprEvaluator::VisitUnaryPreIncDec(const UnaryOperator *UO) { |
| 3630 | if (!Info.getLangOpts().CPlusPlus1y && !Info.keepEvaluatingAfterFailure()) |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3631 | return Error(UO); |
| 3632 | |
| 3633 | if (!this->Visit(UO->getSubExpr())) |
| 3634 | return false; |
| 3635 | |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3636 | return handleIncDec( |
| 3637 | this->Info, UO, Result, UO->getSubExpr()->getType(), |
| 3638 | UO->isIncrementOp(), 0); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3639 | } |
| 3640 | |
| 3641 | bool LValueExprEvaluator::VisitCompoundAssignOperator( |
| 3642 | const CompoundAssignOperator *CAO) { |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3643 | if (!Info.getLangOpts().CPlusPlus1y && !Info.keepEvaluatingAfterFailure()) |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3644 | return Error(CAO); |
| 3645 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3646 | APValue RHS; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3647 | |
| 3648 | // The overall lvalue result is the result of evaluating the LHS. |
| 3649 | if (!this->Visit(CAO->getLHS())) { |
| 3650 | if (Info.keepEvaluatingAfterFailure()) |
| 3651 | Evaluate(RHS, this->Info, CAO->getRHS()); |
| 3652 | return false; |
| 3653 | } |
| 3654 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3655 | if (!Evaluate(RHS, this->Info, CAO->getRHS())) |
| 3656 | return false; |
| 3657 | |
| 3658 | // FIXME: |
| 3659 | //return handleCompoundAssignment( |
| 3660 | // this->Info, CAO, |
| 3661 | // Result, CAO->getLHS()->getType(), CAO->getComputationLHSType(), |
| 3662 | // RHS, CAO->getRHS()->getType(), |
| 3663 | // CAO->getOpForCompoundAssignment(CAO->getOpcode()), |
| 3664 | // CAO->getComputationResultType()); |
| 3665 | return Error(CAO); |
| 3666 | } |
| 3667 | |
| 3668 | bool LValueExprEvaluator::VisitBinAssign(const BinaryOperator *E) { |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3669 | if (!Info.getLangOpts().CPlusPlus1y && !Info.keepEvaluatingAfterFailure()) |
| 3670 | return Error(E); |
| 3671 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3672 | APValue NewVal; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3673 | |
| 3674 | if (!this->Visit(E->getLHS())) { |
| 3675 | if (Info.keepEvaluatingAfterFailure()) |
| 3676 | Evaluate(NewVal, this->Info, E->getRHS()); |
| 3677 | return false; |
| 3678 | } |
| 3679 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3680 | if (!Evaluate(NewVal, this->Info, E->getRHS())) |
| 3681 | return false; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3682 | |
| 3683 | return handleAssignment(this->Info, E, Result, E->getLHS()->getType(), |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3684 | NewVal); |
| 3685 | } |
| 3686 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3687 | //===----------------------------------------------------------------------===// |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3688 | // Pointer Evaluation |
| 3689 | //===----------------------------------------------------------------------===// |
| 3690 | |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 3691 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 3692 | class PointerExprEvaluator |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3693 | : public ExprEvaluatorBase<PointerExprEvaluator, bool> { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3694 | LValue &Result; |
| 3695 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3696 | bool Success(const Expr *E) { |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 3697 | Result.set(E); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3698 | return true; |
| 3699 | } |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 3700 | public: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3701 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3702 | PointerExprEvaluator(EvalInfo &info, LValue &Result) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3703 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3704 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3705 | bool Success(const APValue &V, const Expr *E) { |
| 3706 | Result.setFrom(Info.Ctx, V); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3707 | return true; |
| 3708 | } |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3709 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 3710 | return Success((Expr*)0); |
| 3711 | } |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 3712 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3713 | bool VisitBinaryOperator(const BinaryOperator *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3714 | bool VisitCastExpr(const CastExpr* E); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3715 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3716 | bool VisitObjCStringLiteral(const ObjCStringLiteral *E) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3717 | { return Success(E); } |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 3718 | bool VisitObjCBoxedExpr(const ObjCBoxedExpr *E) |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 3719 | { return Success(E); } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3720 | bool VisitAddrLabelExpr(const AddrLabelExpr *E) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3721 | { return Success(E); } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3722 | bool VisitCallExpr(const CallExpr *E); |
| 3723 | bool VisitBlockExpr(const BlockExpr *E) { |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 3724 | if (!E->getBlockDecl()->hasCaptures()) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3725 | return Success(E); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3726 | return Error(E); |
Mike Stump | a670332 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 3727 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3728 | bool VisitCXXThisExpr(const CXXThisExpr *E) { |
| 3729 | if (!Info.CurrentCall->This) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3730 | return Error(E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3731 | Result = *Info.CurrentCall->This; |
| 3732 | return true; |
| 3733 | } |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3734 | |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 3735 | // FIXME: Missing: @protocol, @selector |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 3736 | }; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3737 | } // end anonymous namespace |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 3738 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3739 | static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3740 | assert(E->isRValue() && E->getType()->hasPointerRepresentation()); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3741 | return PointerExprEvaluator(Info, Result).Visit(E); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3742 | } |
| 3743 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3744 | bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3745 | if (E->getOpcode() != BO_Add && |
| 3746 | E->getOpcode() != BO_Sub) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3747 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3748 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3749 | const Expr *PExp = E->getLHS(); |
| 3750 | const Expr *IExp = E->getRHS(); |
| 3751 | if (IExp->getType()->isPointerType()) |
| 3752 | std::swap(PExp, IExp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3753 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3754 | bool EvalPtrOK = EvaluatePointer(PExp, Result, Info); |
| 3755 | if (!EvalPtrOK && !Info.keepEvaluatingAfterFailure()) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3756 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3757 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3758 | llvm::APSInt Offset; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3759 | if (!EvaluateInteger(IExp, Offset, Info) || !EvalPtrOK) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3760 | return false; |
| 3761 | int64_t AdditionalOffset |
| 3762 | = Offset.isSigned() ? Offset.getSExtValue() |
| 3763 | : static_cast<int64_t>(Offset.getZExtValue()); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 3764 | if (E->getOpcode() == BO_Sub) |
| 3765 | AdditionalOffset = -AdditionalOffset; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3766 | |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 3767 | QualType Pointee = PExp->getType()->castAs<PointerType>()->getPointeeType(); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3768 | return HandleLValueArrayAdjustment(Info, E, Result, Pointee, |
| 3769 | AdditionalOffset); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3770 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3771 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3772 | bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
| 3773 | return EvaluateLValue(E->getSubExpr(), Result, Info); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3774 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3775 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3776 | bool PointerExprEvaluator::VisitCastExpr(const CastExpr* E) { |
| 3777 | const Expr* SubExpr = E->getSubExpr(); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3778 | |
Eli Friedman | 847a2bc | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 3779 | switch (E->getCastKind()) { |
| 3780 | default: |
| 3781 | break; |
| 3782 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3783 | case CK_BitCast: |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3784 | case CK_CPointerToObjCPointerCast: |
| 3785 | case CK_BlockPointerToObjCPointerCast: |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3786 | case CK_AnyPointerToBlockPointerCast: |
Richard Smith | b19ac0d | 2012-01-15 03:25:41 +0000 | [diff] [blame] | 3787 | if (!Visit(SubExpr)) |
| 3788 | return false; |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 3789 | // Bitcasts to cv void* are static_casts, not reinterpret_casts, so are |
| 3790 | // permitted in constant expressions in C++11. Bitcasts from cv void* are |
| 3791 | // also static_casts, but we disallow them as a resolution to DR1312. |
Richard Smith | ff07af1 | 2011-12-12 19:10:03 +0000 | [diff] [blame] | 3792 | if (!E->getType()->isVoidPointerType()) { |
Richard Smith | b19ac0d | 2012-01-15 03:25:41 +0000 | [diff] [blame] | 3793 | Result.Designator.setInvalid(); |
Richard Smith | ff07af1 | 2011-12-12 19:10:03 +0000 | [diff] [blame] | 3794 | if (SubExpr->getType()->isVoidPointerType()) |
| 3795 | CCEDiag(E, diag::note_constexpr_invalid_cast) |
| 3796 | << 3 << SubExpr->getType(); |
| 3797 | else |
| 3798 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 3799 | } |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 3800 | return true; |
Eli Friedman | 847a2bc | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 3801 | |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 3802 | case CK_DerivedToBase: |
| 3803 | case CK_UncheckedDerivedToBase: { |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3804 | if (!EvaluatePointer(E->getSubExpr(), Result, Info)) |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 3805 | return false; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3806 | if (!Result.Base && Result.Offset.isZero()) |
| 3807 | return true; |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 3808 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3809 | // Now figure out the necessary offset to add to the base LV to get from |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 3810 | // the derived class to the base class. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3811 | QualType Type = |
| 3812 | E->getSubExpr()->getType()->castAs<PointerType>()->getPointeeType(); |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 3813 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3814 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 3815 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3816 | if (!HandleLValueBase(Info, E, Result, Type->getAsCXXRecordDecl(), |
| 3817 | *PathI)) |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 3818 | return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3819 | Type = (*PathI)->getType(); |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 3820 | } |
| 3821 | |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 3822 | return true; |
| 3823 | } |
| 3824 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3825 | case CK_BaseToDerived: |
| 3826 | if (!Visit(E->getSubExpr())) |
| 3827 | return false; |
| 3828 | if (!Result.Base && Result.Offset.isZero()) |
| 3829 | return true; |
| 3830 | return HandleBaseToDerivedCast(Info, E, Result); |
| 3831 | |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3832 | case CK_NullToPointer: |
Richard Smith | 4051ff7 | 2012-04-08 08:02:07 +0000 | [diff] [blame] | 3833 | VisitIgnoredValue(E->getSubExpr()); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3834 | return ZeroInitialization(E); |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 3835 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3836 | case CK_IntegralToPointer: { |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 3837 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 3838 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3839 | APValue Value; |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3840 | if (!EvaluateIntegerOrLValue(SubExpr, Value, Info)) |
Eli Friedman | 847a2bc | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 3841 | break; |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 3842 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3843 | if (Value.isInt()) { |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3844 | unsigned Size = Info.Ctx.getTypeSize(E->getType()); |
| 3845 | uint64_t N = Value.getInt().extOrTrunc(Size).getZExtValue(); |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 3846 | Result.Base = (Expr*)0; |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3847 | Result.Offset = CharUnits::fromQuantity(N); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3848 | Result.CallIndex = 0; |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 3849 | Result.Designator.setInvalid(); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3850 | return true; |
| 3851 | } else { |
| 3852 | // Cast is of an lvalue, no need to change value. |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3853 | Result.setFrom(Info.Ctx, Value); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3854 | return true; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3855 | } |
| 3856 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3857 | case CK_ArrayToPointerDecay: |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3858 | if (SubExpr->isGLValue()) { |
| 3859 | if (!EvaluateLValue(SubExpr, Result, Info)) |
| 3860 | return false; |
| 3861 | } else { |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3862 | Result.set(SubExpr, Info.CurrentCall->Index); |
| 3863 | if (!EvaluateInPlace(Info.CurrentCall->Temporaries[SubExpr], |
| 3864 | Info, Result, SubExpr)) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3865 | return false; |
| 3866 | } |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 3867 | // The result is a pointer to the first element of the array. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3868 | if (const ConstantArrayType *CAT |
| 3869 | = Info.Ctx.getAsConstantArrayType(SubExpr->getType())) |
| 3870 | Result.addArray(Info, E, CAT); |
| 3871 | else |
| 3872 | Result.Designator.setInvalid(); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 3873 | return true; |
Richard Smith | dd78544 | 2011-10-31 20:57:44 +0000 | [diff] [blame] | 3874 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3875 | case CK_FunctionToPointerDecay: |
Richard Smith | dd78544 | 2011-10-31 20:57:44 +0000 | [diff] [blame] | 3876 | return EvaluateLValue(SubExpr, Result, Info); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3877 | } |
| 3878 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3879 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3880 | } |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3881 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3882 | bool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3883 | if (IsStringLiteralCall(E)) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3884 | return Success(E); |
Eli Friedman | c69d454 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 3885 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3886 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3887 | } |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3888 | |
| 3889 | //===----------------------------------------------------------------------===// |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3890 | // Member Pointer Evaluation |
| 3891 | //===----------------------------------------------------------------------===// |
| 3892 | |
| 3893 | namespace { |
| 3894 | class MemberPointerExprEvaluator |
| 3895 | : public ExprEvaluatorBase<MemberPointerExprEvaluator, bool> { |
| 3896 | MemberPtr &Result; |
| 3897 | |
| 3898 | bool Success(const ValueDecl *D) { |
| 3899 | Result = MemberPtr(D); |
| 3900 | return true; |
| 3901 | } |
| 3902 | public: |
| 3903 | |
| 3904 | MemberPointerExprEvaluator(EvalInfo &Info, MemberPtr &Result) |
| 3905 | : ExprEvaluatorBaseTy(Info), Result(Result) {} |
| 3906 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3907 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3908 | Result.setFrom(V); |
| 3909 | return true; |
| 3910 | } |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3911 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3912 | return Success((const ValueDecl*)0); |
| 3913 | } |
| 3914 | |
| 3915 | bool VisitCastExpr(const CastExpr *E); |
| 3916 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
| 3917 | }; |
| 3918 | } // end anonymous namespace |
| 3919 | |
| 3920 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
| 3921 | EvalInfo &Info) { |
| 3922 | assert(E->isRValue() && E->getType()->isMemberPointerType()); |
| 3923 | return MemberPointerExprEvaluator(Info, Result).Visit(E); |
| 3924 | } |
| 3925 | |
| 3926 | bool MemberPointerExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 3927 | switch (E->getCastKind()) { |
| 3928 | default: |
| 3929 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 3930 | |
| 3931 | case CK_NullToMemberPointer: |
Richard Smith | 4051ff7 | 2012-04-08 08:02:07 +0000 | [diff] [blame] | 3932 | VisitIgnoredValue(E->getSubExpr()); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3933 | return ZeroInitialization(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3934 | |
| 3935 | case CK_BaseToDerivedMemberPointer: { |
| 3936 | if (!Visit(E->getSubExpr())) |
| 3937 | return false; |
| 3938 | if (E->path_empty()) |
| 3939 | return true; |
| 3940 | // Base-to-derived member pointer casts store the path in derived-to-base |
| 3941 | // order, so iterate backwards. The CXXBaseSpecifier also provides us with |
| 3942 | // the wrong end of the derived->base arc, so stagger the path by one class. |
| 3943 | typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter; |
| 3944 | for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin()); |
| 3945 | PathI != PathE; ++PathI) { |
| 3946 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
| 3947 | const CXXRecordDecl *Derived = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 3948 | if (!Result.castToDerived(Derived)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3949 | return Error(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3950 | } |
| 3951 | const Type *FinalTy = E->getType()->castAs<MemberPointerType>()->getClass(); |
| 3952 | if (!Result.castToDerived(FinalTy->getAsCXXRecordDecl())) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3953 | return Error(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3954 | return true; |
| 3955 | } |
| 3956 | |
| 3957 | case CK_DerivedToBaseMemberPointer: |
| 3958 | if (!Visit(E->getSubExpr())) |
| 3959 | return false; |
| 3960 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 3961 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
| 3962 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
| 3963 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 3964 | if (!Result.castToBase(Base)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3965 | return Error(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3966 | } |
| 3967 | return true; |
| 3968 | } |
| 3969 | } |
| 3970 | |
| 3971 | bool MemberPointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
| 3972 | // C++11 [expr.unary.op]p3 has very strict rules on how the address of a |
| 3973 | // member can be formed. |
| 3974 | return Success(cast<DeclRefExpr>(E->getSubExpr())->getDecl()); |
| 3975 | } |
| 3976 | |
| 3977 | //===----------------------------------------------------------------------===// |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3978 | // Record Evaluation |
| 3979 | //===----------------------------------------------------------------------===// |
| 3980 | |
| 3981 | namespace { |
| 3982 | class RecordExprEvaluator |
| 3983 | : public ExprEvaluatorBase<RecordExprEvaluator, bool> { |
| 3984 | const LValue &This; |
| 3985 | APValue &Result; |
| 3986 | public: |
| 3987 | |
| 3988 | RecordExprEvaluator(EvalInfo &info, const LValue &This, APValue &Result) |
| 3989 | : ExprEvaluatorBaseTy(info), This(This), Result(Result) {} |
| 3990 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3991 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 3992 | Result = V; |
| 3993 | return true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3994 | } |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3995 | bool ZeroInitialization(const Expr *E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3996 | |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3997 | bool VisitCastExpr(const CastExpr *E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3998 | bool VisitInitListExpr(const InitListExpr *E); |
| 3999 | bool VisitCXXConstructExpr(const CXXConstructExpr *E); |
| 4000 | }; |
| 4001 | } |
| 4002 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4003 | /// Perform zero-initialization on an object of non-union class type. |
| 4004 | /// C++11 [dcl.init]p5: |
| 4005 | /// To zero-initialize an object or reference of type T means: |
| 4006 | /// [...] |
| 4007 | /// -- if T is a (possibly cv-qualified) non-union class type, |
| 4008 | /// each non-static data member and each base-class subobject is |
| 4009 | /// zero-initialized |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4010 | static bool HandleClassZeroInitialization(EvalInfo &Info, const Expr *E, |
| 4011 | const RecordDecl *RD, |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4012 | const LValue &This, APValue &Result) { |
| 4013 | assert(!RD->isUnion() && "Expected non-union class type"); |
| 4014 | const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD); |
| 4015 | Result = APValue(APValue::UninitStruct(), CD ? CD->getNumBases() : 0, |
| 4016 | std::distance(RD->field_begin(), RD->field_end())); |
| 4017 | |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4018 | if (RD->isInvalidDecl()) return false; |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4019 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 4020 | |
| 4021 | if (CD) { |
| 4022 | unsigned Index = 0; |
| 4023 | for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(), |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4024 | End = CD->bases_end(); I != End; ++I, ++Index) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4025 | const CXXRecordDecl *Base = I->getType()->getAsCXXRecordDecl(); |
| 4026 | LValue Subobject = This; |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4027 | if (!HandleLValueDirectBase(Info, E, Subobject, CD, Base, &Layout)) |
| 4028 | return false; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4029 | if (!HandleClassZeroInitialization(Info, E, Base, Subobject, |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4030 | Result.getStructBase(Index))) |
| 4031 | return false; |
| 4032 | } |
| 4033 | } |
| 4034 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4035 | for (RecordDecl::field_iterator I = RD->field_begin(), End = RD->field_end(); |
| 4036 | I != End; ++I) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4037 | // -- if T is a reference type, no initialization is performed. |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 4038 | if (I->getType()->isReferenceType()) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4039 | continue; |
| 4040 | |
| 4041 | LValue Subobject = This; |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4042 | if (!HandleLValueMember(Info, E, Subobject, *I, &Layout)) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4043 | return false; |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4044 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 4045 | ImplicitValueInitExpr VIE(I->getType()); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 4046 | if (!EvaluateInPlace( |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 4047 | Result.getStructField(I->getFieldIndex()), Info, Subobject, &VIE)) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4048 | return false; |
| 4049 | } |
| 4050 | |
| 4051 | return true; |
| 4052 | } |
| 4053 | |
| 4054 | bool RecordExprEvaluator::ZeroInitialization(const Expr *E) { |
| 4055 | const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl(); |
John McCall | 3c79d88 | 2012-04-26 18:10:01 +0000 | [diff] [blame] | 4056 | if (RD->isInvalidDecl()) return false; |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4057 | if (RD->isUnion()) { |
| 4058 | // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the |
| 4059 | // object's first non-static named data member is zero-initialized |
| 4060 | RecordDecl::field_iterator I = RD->field_begin(); |
| 4061 | if (I == RD->field_end()) { |
| 4062 | Result = APValue((const FieldDecl*)0); |
| 4063 | return true; |
| 4064 | } |
| 4065 | |
| 4066 | LValue Subobject = This; |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4067 | if (!HandleLValueMember(Info, E, Subobject, *I)) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4068 | return false; |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4069 | Result = APValue(*I); |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 4070 | ImplicitValueInitExpr VIE(I->getType()); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 4071 | return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, &VIE); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4072 | } |
| 4073 | |
Richard Smith | 5d10860 | 2012-02-17 00:44:16 +0000 | [diff] [blame] | 4074 | if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->getNumVBases()) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 4075 | Info.Diag(E, diag::note_constexpr_virtual_base) << RD; |
Richard Smith | 5d10860 | 2012-02-17 00:44:16 +0000 | [diff] [blame] | 4076 | return false; |
| 4077 | } |
| 4078 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4079 | return HandleClassZeroInitialization(Info, E, RD, This, Result); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4080 | } |
| 4081 | |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4082 | bool RecordExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 4083 | switch (E->getCastKind()) { |
| 4084 | default: |
| 4085 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 4086 | |
| 4087 | case CK_ConstructorConversion: |
| 4088 | return Visit(E->getSubExpr()); |
| 4089 | |
| 4090 | case CK_DerivedToBase: |
| 4091 | case CK_UncheckedDerivedToBase: { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4092 | APValue DerivedObject; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4093 | if (!Evaluate(DerivedObject, Info, E->getSubExpr())) |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4094 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4095 | if (!DerivedObject.isStruct()) |
| 4096 | return Error(E->getSubExpr()); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4097 | |
| 4098 | // Derived-to-base rvalue conversion: just slice off the derived part. |
| 4099 | APValue *Value = &DerivedObject; |
| 4100 | const CXXRecordDecl *RD = E->getSubExpr()->getType()->getAsCXXRecordDecl(); |
| 4101 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 4102 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
| 4103 | assert(!(*PathI)->isVirtual() && "record rvalue with virtual base"); |
| 4104 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 4105 | Value = &Value->getStructBase(getBaseIndex(RD, Base)); |
| 4106 | RD = Base; |
| 4107 | } |
| 4108 | Result = *Value; |
| 4109 | return true; |
| 4110 | } |
| 4111 | } |
| 4112 | } |
| 4113 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4114 | bool RecordExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
Sebastian Redl | e6c32e6 | 2012-02-19 14:53:49 +0000 | [diff] [blame] | 4115 | // Cannot constant-evaluate std::initializer_list inits. |
| 4116 | if (E->initializesStdInitializerList()) |
| 4117 | return false; |
| 4118 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4119 | const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl(); |
John McCall | 3c79d88 | 2012-04-26 18:10:01 +0000 | [diff] [blame] | 4120 | if (RD->isInvalidDecl()) return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4121 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 4122 | |
| 4123 | if (RD->isUnion()) { |
Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 4124 | const FieldDecl *Field = E->getInitializedFieldInUnion(); |
| 4125 | Result = APValue(Field); |
| 4126 | if (!Field) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4127 | return true; |
Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 4128 | |
| 4129 | // If the initializer list for a union does not contain any elements, the |
| 4130 | // first element of the union is value-initialized. |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 4131 | // FIXME: The element should be initialized from an initializer list. |
| 4132 | // Is this difference ever observable for initializer lists which |
| 4133 | // we don't build? |
Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 4134 | ImplicitValueInitExpr VIE(Field->getType()); |
| 4135 | const Expr *InitExpr = E->getNumInits() ? E->getInit(0) : &VIE; |
| 4136 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4137 | LValue Subobject = This; |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4138 | if (!HandleLValueMember(Info, InitExpr, Subobject, Field, &Layout)) |
| 4139 | return false; |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 4140 | |
| 4141 | // Temporarily override This, in case there's a CXXDefaultInitExpr in here. |
| 4142 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This, |
| 4143 | isa<CXXDefaultInitExpr>(InitExpr)); |
| 4144 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 4145 | return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, InitExpr); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4146 | } |
| 4147 | |
| 4148 | assert((!isa<CXXRecordDecl>(RD) || !cast<CXXRecordDecl>(RD)->getNumBases()) && |
| 4149 | "initializer list for class with base classes"); |
| 4150 | Result = APValue(APValue::UninitStruct(), 0, |
| 4151 | std::distance(RD->field_begin(), RD->field_end())); |
| 4152 | unsigned ElementNo = 0; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4153 | bool Success = true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4154 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 4155 | FieldEnd = RD->field_end(); Field != FieldEnd; ++Field) { |
| 4156 | // Anonymous bit-fields are not considered members of the class for |
| 4157 | // purposes of aggregate initialization. |
| 4158 | if (Field->isUnnamedBitfield()) |
| 4159 | continue; |
| 4160 | |
| 4161 | LValue Subobject = This; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4162 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4163 | bool HaveInit = ElementNo < E->getNumInits(); |
| 4164 | |
| 4165 | // FIXME: Diagnostics here should point to the end of the initializer |
| 4166 | // list, not the start. |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4167 | if (!HandleLValueMember(Info, HaveInit ? E->getInit(ElementNo) : E, |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4168 | Subobject, *Field, &Layout)) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4169 | return false; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4170 | |
| 4171 | // Perform an implicit value-initialization for members beyond the end of |
| 4172 | // the initializer list. |
| 4173 | ImplicitValueInitExpr VIE(HaveInit ? Info.Ctx.IntTy : Field->getType()); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 4174 | const Expr *Init = HaveInit ? E->getInit(ElementNo++) : &VIE; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4175 | |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 4176 | // Temporarily override This, in case there's a CXXDefaultInitExpr in here. |
| 4177 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This, |
| 4178 | isa<CXXDefaultInitExpr>(Init)); |
| 4179 | |
| 4180 | if (!EvaluateInPlace(Result.getStructField(Field->getFieldIndex()), Info, |
| 4181 | Subobject, Init)) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4182 | if (!Info.keepEvaluatingAfterFailure()) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4183 | return false; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4184 | Success = false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4185 | } |
| 4186 | } |
| 4187 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4188 | return Success; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4189 | } |
| 4190 | |
| 4191 | bool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 4192 | const CXXConstructorDecl *FD = E->getConstructor(); |
John McCall | 3c79d88 | 2012-04-26 18:10:01 +0000 | [diff] [blame] | 4193 | if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl()) return false; |
| 4194 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4195 | bool ZeroInit = E->requiresZeroInitialization(); |
| 4196 | if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) { |
Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 4197 | // If we've already performed zero-initialization, we're already done. |
| 4198 | if (!Result.isUninit()) |
| 4199 | return true; |
| 4200 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4201 | if (ZeroInit) |
| 4202 | return ZeroInitialization(E); |
| 4203 | |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4204 | const CXXRecordDecl *RD = FD->getParent(); |
| 4205 | if (RD->isUnion()) |
| 4206 | Result = APValue((FieldDecl*)0); |
| 4207 | else |
| 4208 | Result = APValue(APValue::UninitStruct(), RD->getNumBases(), |
| 4209 | std::distance(RD->field_begin(), RD->field_end())); |
| 4210 | return true; |
| 4211 | } |
| 4212 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4213 | const FunctionDecl *Definition = 0; |
| 4214 | FD->getBody(Definition); |
| 4215 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4216 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition)) |
| 4217 | return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4218 | |
Richard Smith | 1bc5c2c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 4219 | // Avoid materializing a temporary for an elidable copy/move constructor. |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4220 | if (E->isElidable() && !ZeroInit) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4221 | if (const MaterializeTemporaryExpr *ME |
| 4222 | = dyn_cast<MaterializeTemporaryExpr>(E->getArg(0))) |
| 4223 | return Visit(ME->GetTemporaryExpr()); |
| 4224 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4225 | if (ZeroInit && !ZeroInitialization(E)) |
| 4226 | return false; |
| 4227 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4228 | ArrayRef<const Expr *> Args(E->getArgs(), E->getNumArgs()); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4229 | return HandleConstructorCall(E->getExprLoc(), This, Args, |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4230 | cast<CXXConstructorDecl>(Definition), Info, |
| 4231 | Result); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4232 | } |
| 4233 | |
| 4234 | static bool EvaluateRecord(const Expr *E, const LValue &This, |
| 4235 | APValue &Result, EvalInfo &Info) { |
| 4236 | assert(E->isRValue() && E->getType()->isRecordType() && |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4237 | "can't evaluate expression as a record rvalue"); |
| 4238 | return RecordExprEvaluator(Info, This, Result).Visit(E); |
| 4239 | } |
| 4240 | |
| 4241 | //===----------------------------------------------------------------------===// |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4242 | // Temporary Evaluation |
| 4243 | // |
| 4244 | // Temporaries are represented in the AST as rvalues, but generally behave like |
| 4245 | // lvalues. The full-object of which the temporary is a subobject is implicitly |
| 4246 | // materialized so that a reference can bind to it. |
| 4247 | //===----------------------------------------------------------------------===// |
| 4248 | namespace { |
| 4249 | class TemporaryExprEvaluator |
| 4250 | : public LValueExprEvaluatorBase<TemporaryExprEvaluator> { |
| 4251 | public: |
| 4252 | TemporaryExprEvaluator(EvalInfo &Info, LValue &Result) : |
| 4253 | LValueExprEvaluatorBaseTy(Info, Result) {} |
| 4254 | |
| 4255 | /// Visit an expression which constructs the value of this temporary. |
| 4256 | bool VisitConstructExpr(const Expr *E) { |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 4257 | Result.set(E, Info.CurrentCall->Index); |
| 4258 | return EvaluateInPlace(Info.CurrentCall->Temporaries[E], Info, Result, E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4259 | } |
| 4260 | |
| 4261 | bool VisitCastExpr(const CastExpr *E) { |
| 4262 | switch (E->getCastKind()) { |
| 4263 | default: |
| 4264 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
| 4265 | |
| 4266 | case CK_ConstructorConversion: |
| 4267 | return VisitConstructExpr(E->getSubExpr()); |
| 4268 | } |
| 4269 | } |
| 4270 | bool VisitInitListExpr(const InitListExpr *E) { |
| 4271 | return VisitConstructExpr(E); |
| 4272 | } |
| 4273 | bool VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 4274 | return VisitConstructExpr(E); |
| 4275 | } |
| 4276 | bool VisitCallExpr(const CallExpr *E) { |
| 4277 | return VisitConstructExpr(E); |
| 4278 | } |
| 4279 | }; |
| 4280 | } // end anonymous namespace |
| 4281 | |
| 4282 | /// Evaluate an expression of record type as a temporary. |
| 4283 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info) { |
Richard Smith | d0b111c | 2011-12-19 22:01:37 +0000 | [diff] [blame] | 4284 | assert(E->isRValue() && E->getType()->isRecordType()); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4285 | return TemporaryExprEvaluator(Info, Result).Visit(E); |
| 4286 | } |
| 4287 | |
| 4288 | //===----------------------------------------------------------------------===// |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4289 | // Vector Evaluation |
| 4290 | //===----------------------------------------------------------------------===// |
| 4291 | |
| 4292 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 4293 | class VectorExprEvaluator |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 4294 | : public ExprEvaluatorBase<VectorExprEvaluator, bool> { |
| 4295 | APValue &Result; |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4296 | public: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4297 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 4298 | VectorExprEvaluator(EvalInfo &info, APValue &Result) |
| 4299 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4300 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 4301 | bool Success(const ArrayRef<APValue> &V, const Expr *E) { |
| 4302 | assert(V.size() == E->getType()->castAs<VectorType>()->getNumElements()); |
| 4303 | // FIXME: remove this APValue copy. |
| 4304 | Result = APValue(V.data(), V.size()); |
| 4305 | return true; |
| 4306 | } |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4307 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | ed5165f | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 4308 | assert(V.isVector()); |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 4309 | Result = V; |
| 4310 | return true; |
| 4311 | } |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4312 | bool ZeroInitialization(const Expr *E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4313 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 4314 | bool VisitUnaryReal(const UnaryOperator *E) |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 4315 | { return Visit(E->getSubExpr()); } |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 4316 | bool VisitCastExpr(const CastExpr* E); |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 4317 | bool VisitInitListExpr(const InitListExpr *E); |
| 4318 | bool VisitUnaryImag(const UnaryOperator *E); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 4319 | // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div, |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 4320 | // binary comparisons, binary and/or/xor, |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 4321 | // shufflevector, ExtVectorElementExpr |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4322 | }; |
| 4323 | } // end anonymous namespace |
| 4324 | |
| 4325 | static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4326 | assert(E->isRValue() && E->getType()->isVectorType() &&"not a vector rvalue"); |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 4327 | return VectorExprEvaluator(Info, Result).Visit(E); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4328 | } |
| 4329 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 4330 | bool VectorExprEvaluator::VisitCastExpr(const CastExpr* E) { |
| 4331 | const VectorType *VTy = E->getType()->castAs<VectorType>(); |
Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 4332 | unsigned NElts = VTy->getNumElements(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4333 | |
Richard Smith | 161f09a | 2011-12-06 22:44:34 +0000 | [diff] [blame] | 4334 | const Expr *SE = E->getSubExpr(); |
Nate Begeman | 2ffd384 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 4335 | QualType SETy = SE->getType(); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4336 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 4337 | switch (E->getCastKind()) { |
| 4338 | case CK_VectorSplat: { |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 4339 | APValue Val = APValue(); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 4340 | if (SETy->isIntegerType()) { |
| 4341 | APSInt IntResult; |
| 4342 | if (!EvaluateInteger(SE, IntResult, Info)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4343 | return false; |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 4344 | Val = APValue(IntResult); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 4345 | } else if (SETy->isRealFloatingType()) { |
| 4346 | APFloat F(0.0); |
| 4347 | if (!EvaluateFloat(SE, F, Info)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4348 | return false; |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 4349 | Val = APValue(F); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 4350 | } else { |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 4351 | return Error(E); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 4352 | } |
Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 4353 | |
| 4354 | // Splat and create vector APValue. |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 4355 | SmallVector<APValue, 4> Elts(NElts, Val); |
| 4356 | return Success(Elts, E); |
Nate Begeman | 2ffd384 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 4357 | } |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 4358 | case CK_BitCast: { |
| 4359 | // Evaluate the operand into an APInt we can extract from. |
| 4360 | llvm::APInt SValInt; |
| 4361 | if (!EvalAndBitcastToAPInt(Info, SE, SValInt)) |
| 4362 | return false; |
| 4363 | // Extract the elements |
| 4364 | QualType EltTy = VTy->getElementType(); |
| 4365 | unsigned EltSize = Info.Ctx.getTypeSize(EltTy); |
| 4366 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
| 4367 | SmallVector<APValue, 4> Elts; |
| 4368 | if (EltTy->isRealFloatingType()) { |
| 4369 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(EltTy); |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 4370 | unsigned FloatEltSize = EltSize; |
| 4371 | if (&Sem == &APFloat::x87DoubleExtended) |
| 4372 | FloatEltSize = 80; |
| 4373 | for (unsigned i = 0; i < NElts; i++) { |
| 4374 | llvm::APInt Elt; |
| 4375 | if (BigEndian) |
| 4376 | Elt = SValInt.rotl(i*EltSize+FloatEltSize).trunc(FloatEltSize); |
| 4377 | else |
| 4378 | Elt = SValInt.rotr(i*EltSize).trunc(FloatEltSize); |
Tim Northover | 178723a | 2013-01-22 09:46:51 +0000 | [diff] [blame] | 4379 | Elts.push_back(APValue(APFloat(Sem, Elt))); |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 4380 | } |
| 4381 | } else if (EltTy->isIntegerType()) { |
| 4382 | for (unsigned i = 0; i < NElts; i++) { |
| 4383 | llvm::APInt Elt; |
| 4384 | if (BigEndian) |
| 4385 | Elt = SValInt.rotl(i*EltSize+EltSize).zextOrTrunc(EltSize); |
| 4386 | else |
| 4387 | Elt = SValInt.rotr(i*EltSize).zextOrTrunc(EltSize); |
| 4388 | Elts.push_back(APValue(APSInt(Elt, EltTy->isSignedIntegerType()))); |
| 4389 | } |
| 4390 | } else { |
| 4391 | return Error(E); |
| 4392 | } |
| 4393 | return Success(Elts, E); |
| 4394 | } |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 4395 | default: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4396 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 4397 | } |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4398 | } |
| 4399 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 4400 | bool |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4401 | VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 4402 | const VectorType *VT = E->getType()->castAs<VectorType>(); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4403 | unsigned NumInits = E->getNumInits(); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 4404 | unsigned NumElements = VT->getNumElements(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4405 | |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4406 | QualType EltTy = VT->getElementType(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4407 | SmallVector<APValue, 4> Elements; |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4408 | |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 4409 | // The number of initializers can be less than the number of |
| 4410 | // vector elements. For OpenCL, this can be due to nested vector |
| 4411 | // initialization. For GCC compatibility, missing trailing elements |
| 4412 | // should be initialized with zeroes. |
| 4413 | unsigned CountInits = 0, CountElts = 0; |
| 4414 | while (CountElts < NumElements) { |
| 4415 | // Handle nested vector initialization. |
| 4416 | if (CountInits < NumInits |
| 4417 | && E->getInit(CountInits)->getType()->isExtVectorType()) { |
| 4418 | APValue v; |
| 4419 | if (!EvaluateVector(E->getInit(CountInits), v, Info)) |
| 4420 | return Error(E); |
| 4421 | unsigned vlen = v.getVectorLength(); |
| 4422 | for (unsigned j = 0; j < vlen; j++) |
| 4423 | Elements.push_back(v.getVectorElt(j)); |
| 4424 | CountElts += vlen; |
| 4425 | } else if (EltTy->isIntegerType()) { |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4426 | llvm::APSInt sInt(32); |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 4427 | if (CountInits < NumInits) { |
| 4428 | if (!EvaluateInteger(E->getInit(CountInits), sInt, Info)) |
Richard Smith | ac2f0b1 | 2012-03-13 20:58:32 +0000 | [diff] [blame] | 4429 | return false; |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 4430 | } else // trailing integer zero. |
| 4431 | sInt = Info.Ctx.MakeIntValue(0, EltTy); |
| 4432 | Elements.push_back(APValue(sInt)); |
| 4433 | CountElts++; |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4434 | } else { |
| 4435 | llvm::APFloat f(0.0); |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 4436 | if (CountInits < NumInits) { |
| 4437 | if (!EvaluateFloat(E->getInit(CountInits), f, Info)) |
Richard Smith | ac2f0b1 | 2012-03-13 20:58:32 +0000 | [diff] [blame] | 4438 | return false; |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 4439 | } else // trailing float zero. |
| 4440 | f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)); |
| 4441 | Elements.push_back(APValue(f)); |
| 4442 | CountElts++; |
John McCall | 875679e | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 4443 | } |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 4444 | CountInits++; |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4445 | } |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 4446 | return Success(Elements, E); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4447 | } |
| 4448 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 4449 | bool |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4450 | VectorExprEvaluator::ZeroInitialization(const Expr *E) { |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 4451 | const VectorType *VT = E->getType()->getAs<VectorType>(); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 4452 | QualType EltTy = VT->getElementType(); |
| 4453 | APValue ZeroElement; |
| 4454 | if (EltTy->isIntegerType()) |
| 4455 | ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy)); |
| 4456 | else |
| 4457 | ZeroElement = |
| 4458 | APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy))); |
| 4459 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4460 | SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement); |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 4461 | return Success(Elements, E); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 4462 | } |
| 4463 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 4464 | bool VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 4465 | VisitIgnoredValue(E->getSubExpr()); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4466 | return ZeroInitialization(E); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 4467 | } |
| 4468 | |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4469 | //===----------------------------------------------------------------------===// |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 4470 | // Array Evaluation |
| 4471 | //===----------------------------------------------------------------------===// |
| 4472 | |
| 4473 | namespace { |
| 4474 | class ArrayExprEvaluator |
| 4475 | : public ExprEvaluatorBase<ArrayExprEvaluator, bool> { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4476 | const LValue &This; |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 4477 | APValue &Result; |
| 4478 | public: |
| 4479 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4480 | ArrayExprEvaluator(EvalInfo &Info, const LValue &This, APValue &Result) |
| 4481 | : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {} |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 4482 | |
| 4483 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | 14a9413 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 4484 | assert((V.isArray() || V.isLValue()) && |
| 4485 | "expected array or string literal"); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 4486 | Result = V; |
| 4487 | return true; |
| 4488 | } |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 4489 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4490 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4491 | const ConstantArrayType *CAT = |
| 4492 | Info.Ctx.getAsConstantArrayType(E->getType()); |
| 4493 | if (!CAT) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4494 | return Error(E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4495 | |
| 4496 | Result = APValue(APValue::UninitArray(), 0, |
| 4497 | CAT->getSize().getZExtValue()); |
| 4498 | if (!Result.hasArrayFiller()) return true; |
| 4499 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4500 | // Zero-initialize all elements. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4501 | LValue Subobject = This; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4502 | Subobject.addArray(Info, E, CAT); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4503 | ImplicitValueInitExpr VIE(CAT->getElementType()); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 4504 | return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, &VIE); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4505 | } |
| 4506 | |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 4507 | bool VisitInitListExpr(const InitListExpr *E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4508 | bool VisitCXXConstructExpr(const CXXConstructExpr *E); |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 4509 | bool VisitCXXConstructExpr(const CXXConstructExpr *E, |
| 4510 | const LValue &Subobject, |
| 4511 | APValue *Value, QualType Type); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 4512 | }; |
| 4513 | } // end anonymous namespace |
| 4514 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4515 | static bool EvaluateArray(const Expr *E, const LValue &This, |
| 4516 | APValue &Result, EvalInfo &Info) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4517 | assert(E->isRValue() && E->getType()->isArrayType() && "not an array rvalue"); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4518 | return ArrayExprEvaluator(Info, This, Result).Visit(E); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 4519 | } |
| 4520 | |
| 4521 | bool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 4522 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(E->getType()); |
| 4523 | if (!CAT) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4524 | return Error(E); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 4525 | |
Richard Smith | ca2cfbf | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 4526 | // C++11 [dcl.init.string]p1: A char array [...] can be initialized by [...] |
| 4527 | // an appropriately-typed string literal enclosed in braces. |
Richard Smith | 9ec1e48 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 4528 | if (E->isStringLiteralInit()) { |
Richard Smith | ca2cfbf | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 4529 | LValue LV; |
| 4530 | if (!EvaluateLValue(E->getInit(0), LV, Info)) |
| 4531 | return false; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4532 | APValue Val; |
Richard Smith | 14a9413 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 4533 | LV.moveInto(Val); |
| 4534 | return Success(Val, E); |
Richard Smith | ca2cfbf | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 4535 | } |
| 4536 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4537 | bool Success = true; |
| 4538 | |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 4539 | assert((!Result.isArray() || Result.getArrayInitializedElts() == 0) && |
| 4540 | "zero-initialized array shouldn't have any initialized elts"); |
| 4541 | APValue Filler; |
| 4542 | if (Result.isArray() && Result.hasArrayFiller()) |
| 4543 | Filler = Result.getArrayFiller(); |
| 4544 | |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 4545 | unsigned NumEltsToInit = E->getNumInits(); |
| 4546 | unsigned NumElts = CAT->getSize().getZExtValue(); |
| 4547 | const Expr *FillerExpr = E->hasArrayFiller() ? E->getArrayFiller() : 0; |
| 4548 | |
| 4549 | // If the initializer might depend on the array index, run it for each |
| 4550 | // array element. For now, just whitelist non-class value-initialization. |
| 4551 | if (NumEltsToInit != NumElts && !isa<ImplicitValueInitExpr>(FillerExpr)) |
| 4552 | NumEltsToInit = NumElts; |
| 4553 | |
| 4554 | Result = APValue(APValue::UninitArray(), NumEltsToInit, NumElts); |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 4555 | |
| 4556 | // If the array was previously zero-initialized, preserve the |
| 4557 | // zero-initialized values. |
| 4558 | if (!Filler.isUninit()) { |
| 4559 | for (unsigned I = 0, E = Result.getArrayInitializedElts(); I != E; ++I) |
| 4560 | Result.getArrayInitializedElt(I) = Filler; |
| 4561 | if (Result.hasArrayFiller()) |
| 4562 | Result.getArrayFiller() = Filler; |
| 4563 | } |
| 4564 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4565 | LValue Subobject = This; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4566 | Subobject.addArray(Info, E, CAT); |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 4567 | for (unsigned Index = 0; Index != NumEltsToInit; ++Index) { |
| 4568 | const Expr *Init = |
| 4569 | Index < E->getNumInits() ? E->getInit(Index) : FillerExpr; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 4570 | if (!EvaluateInPlace(Result.getArrayInitializedElt(Index), |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 4571 | Info, Subobject, Init) || |
| 4572 | !HandleLValueArrayAdjustment(Info, Init, Subobject, |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4573 | CAT->getElementType(), 1)) { |
| 4574 | if (!Info.keepEvaluatingAfterFailure()) |
| 4575 | return false; |
| 4576 | Success = false; |
| 4577 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4578 | } |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 4579 | |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 4580 | if (!Result.hasArrayFiller()) |
| 4581 | return Success; |
| 4582 | |
| 4583 | // If we get here, we have a trivial filler, which we can just evaluate |
| 4584 | // once and splat over the rest of the array elements. |
| 4585 | assert(FillerExpr && "no array filler for incomplete init list"); |
| 4586 | return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, |
| 4587 | FillerExpr) && Success; |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 4588 | } |
| 4589 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4590 | bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 4591 | return VisitCXXConstructExpr(E, This, &Result, E->getType()); |
| 4592 | } |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 4593 | |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 4594 | bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E, |
| 4595 | const LValue &Subobject, |
| 4596 | APValue *Value, |
| 4597 | QualType Type) { |
| 4598 | bool HadZeroInit = !Value->isUninit(); |
| 4599 | |
| 4600 | if (const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(Type)) { |
| 4601 | unsigned N = CAT->getSize().getZExtValue(); |
| 4602 | |
| 4603 | // Preserve the array filler if we had prior zero-initialization. |
| 4604 | APValue Filler = |
| 4605 | HadZeroInit && Value->hasArrayFiller() ? Value->getArrayFiller() |
| 4606 | : APValue(); |
| 4607 | |
| 4608 | *Value = APValue(APValue::UninitArray(), N, N); |
| 4609 | |
| 4610 | if (HadZeroInit) |
| 4611 | for (unsigned I = 0; I != N; ++I) |
| 4612 | Value->getArrayInitializedElt(I) = Filler; |
| 4613 | |
| 4614 | // Initialize the elements. |
| 4615 | LValue ArrayElt = Subobject; |
| 4616 | ArrayElt.addArray(Info, E, CAT); |
| 4617 | for (unsigned I = 0; I != N; ++I) |
| 4618 | if (!VisitCXXConstructExpr(E, ArrayElt, &Value->getArrayInitializedElt(I), |
| 4619 | CAT->getElementType()) || |
| 4620 | !HandleLValueArrayAdjustment(Info, E, ArrayElt, |
| 4621 | CAT->getElementType(), 1)) |
| 4622 | return false; |
| 4623 | |
| 4624 | return true; |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 4625 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4626 | |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 4627 | if (!Type->isRecordType()) |
Richard Smith | 9fce7bc | 2012-07-10 22:12:55 +0000 | [diff] [blame] | 4628 | return Error(E); |
| 4629 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4630 | const CXXConstructorDecl *FD = E->getConstructor(); |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4631 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4632 | bool ZeroInit = E->requiresZeroInitialization(); |
| 4633 | if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) { |
Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 4634 | if (HadZeroInit) |
| 4635 | return true; |
| 4636 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4637 | if (ZeroInit) { |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 4638 | ImplicitValueInitExpr VIE(Type); |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 4639 | return EvaluateInPlace(*Value, Info, Subobject, &VIE); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4640 | } |
| 4641 | |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4642 | const CXXRecordDecl *RD = FD->getParent(); |
| 4643 | if (RD->isUnion()) |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 4644 | *Value = APValue((FieldDecl*)0); |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4645 | else |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 4646 | *Value = |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4647 | APValue(APValue::UninitStruct(), RD->getNumBases(), |
| 4648 | std::distance(RD->field_begin(), RD->field_end())); |
| 4649 | return true; |
| 4650 | } |
| 4651 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4652 | const FunctionDecl *Definition = 0; |
| 4653 | FD->getBody(Definition); |
| 4654 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4655 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition)) |
| 4656 | return false; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4657 | |
Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 4658 | if (ZeroInit && !HadZeroInit) { |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 4659 | ImplicitValueInitExpr VIE(Type); |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 4660 | if (!EvaluateInPlace(*Value, Info, Subobject, &VIE)) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4661 | return false; |
| 4662 | } |
| 4663 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4664 | ArrayRef<const Expr *> Args(E->getArgs(), E->getNumArgs()); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4665 | return HandleConstructorCall(E->getExprLoc(), Subobject, Args, |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4666 | cast<CXXConstructorDecl>(Definition), |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 4667 | Info, *Value); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4668 | } |
| 4669 | |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 4670 | //===----------------------------------------------------------------------===// |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4671 | // Integer Evaluation |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4672 | // |
| 4673 | // As a GNU extension, we support casting pointers to sufficiently-wide integer |
| 4674 | // types and back in constant folding. Integer values are thus represented |
| 4675 | // either as an integer-valued APValue, or as an lvalue-valued APValue. |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4676 | //===----------------------------------------------------------------------===// |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4677 | |
| 4678 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 4679 | class IntExprEvaluator |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4680 | : public ExprEvaluatorBase<IntExprEvaluator, bool> { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4681 | APValue &Result; |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 4682 | public: |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4683 | IntExprEvaluator(EvalInfo &info, APValue &result) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4684 | : ExprEvaluatorBaseTy(info), Result(result) {} |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4685 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4686 | bool Success(const llvm::APSInt &SI, const Expr *E, APValue &Result) { |
Abramo Bagnara | 9ae292d | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 4687 | assert(E->getType()->isIntegralOrEnumerationType() && |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 4688 | "Invalid evaluation result."); |
Abramo Bagnara | 9ae292d | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 4689 | assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() && |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 4690 | "Invalid evaluation result."); |
Abramo Bagnara | 9ae292d | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 4691 | assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 4692 | "Invalid evaluation result."); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4693 | Result = APValue(SI); |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 4694 | return true; |
| 4695 | } |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4696 | bool Success(const llvm::APSInt &SI, const Expr *E) { |
| 4697 | return Success(SI, E, Result); |
| 4698 | } |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 4699 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4700 | bool Success(const llvm::APInt &I, const Expr *E, APValue &Result) { |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 4701 | assert(E->getType()->isIntegralOrEnumerationType() && |
| 4702 | "Invalid evaluation result."); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 4703 | assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 4704 | "Invalid evaluation result."); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4705 | Result = APValue(APSInt(I)); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 4706 | Result.getInt().setIsUnsigned( |
| 4707 | E->getType()->isUnsignedIntegerOrEnumerationType()); |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4708 | return true; |
| 4709 | } |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4710 | bool Success(const llvm::APInt &I, const Expr *E) { |
| 4711 | return Success(I, E, Result); |
| 4712 | } |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4713 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4714 | bool Success(uint64_t Value, const Expr *E, APValue &Result) { |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 4715 | assert(E->getType()->isIntegralOrEnumerationType() && |
| 4716 | "Invalid evaluation result."); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4717 | Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType())); |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4718 | return true; |
| 4719 | } |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4720 | bool Success(uint64_t Value, const Expr *E) { |
| 4721 | return Success(Value, E, Result); |
| 4722 | } |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4723 | |
Ken Dyck | dbc0191 | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 4724 | bool Success(CharUnits Size, const Expr *E) { |
| 4725 | return Success(Size.getQuantity(), E); |
| 4726 | } |
| 4727 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4728 | bool Success(const APValue &V, const Expr *E) { |
Eli Friedman | b1bc368 | 2012-01-05 23:59:40 +0000 | [diff] [blame] | 4729 | if (V.isLValue() || V.isAddrLabelDiff()) { |
Richard Smith | 9c8d1c5 | 2011-10-29 22:55:55 +0000 | [diff] [blame] | 4730 | Result = V; |
| 4731 | return true; |
| 4732 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4733 | return Success(V.getInt(), E); |
Chris Lattner | fac05ae | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 4734 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4735 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4736 | bool ZeroInitialization(const Expr *E) { return Success(0, E); } |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4737 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4738 | //===--------------------------------------------------------------------===// |
| 4739 | // Visitor Methods |
| 4740 | //===--------------------------------------------------------------------===// |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 4741 | |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 4742 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4743 | return Success(E->getValue(), E); |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 4744 | } |
| 4745 | bool VisitCharacterLiteral(const CharacterLiteral *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4746 | return Success(E->getValue(), E); |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 4747 | } |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 4748 | |
| 4749 | bool CheckReferencedDecl(const Expr *E, const Decl *D); |
| 4750 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4751 | if (CheckReferencedDecl(E, E->getDecl())) |
| 4752 | return true; |
| 4753 | |
| 4754 | return ExprEvaluatorBaseTy::VisitDeclRefExpr(E); |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 4755 | } |
| 4756 | bool VisitMemberExpr(const MemberExpr *E) { |
| 4757 | if (CheckReferencedDecl(E, E->getMemberDecl())) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4758 | VisitIgnoredValue(E->getBase()); |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 4759 | return true; |
| 4760 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4761 | |
| 4762 | return ExprEvaluatorBaseTy::VisitMemberExpr(E); |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 4763 | } |
| 4764 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4765 | bool VisitCallExpr(const CallExpr *E); |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 4766 | bool VisitBinaryOperator(const BinaryOperator *E); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 4767 | bool VisitOffsetOfExpr(const OffsetOfExpr *E); |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 4768 | bool VisitUnaryOperator(const UnaryOperator *E); |
Anders Carlsson | 374b93d | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 4769 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4770 | bool VisitCastExpr(const CastExpr* E); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 4771 | bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E); |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 4772 | |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4773 | bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4774 | return Success(E->getValue(), E); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4775 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4776 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 4777 | bool VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) { |
| 4778 | return Success(E->getValue(), E); |
| 4779 | } |
| 4780 | |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4781 | // Note, GNU defines __null as an integer, not a pointer. |
Anders Carlsson | 39def3a | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 4782 | bool VisitGNUNullExpr(const GNUNullExpr *E) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4783 | return ZeroInitialization(E); |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 4784 | } |
| 4785 | |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 4786 | bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) { |
Sebastian Redl | 8eb06f1 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 4787 | return Success(E->getValue(), E); |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 4788 | } |
| 4789 | |
Francois Pichet | 9dfa3ce | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 4790 | bool VisitBinaryTypeTraitExpr(const BinaryTypeTraitExpr *E) { |
| 4791 | return Success(E->getValue(), E); |
| 4792 | } |
| 4793 | |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 4794 | bool VisitTypeTraitExpr(const TypeTraitExpr *E) { |
| 4795 | return Success(E->getValue(), E); |
| 4796 | } |
| 4797 | |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 4798 | bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) { |
| 4799 | return Success(E->getValue(), E); |
| 4800 | } |
| 4801 | |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 4802 | bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) { |
| 4803 | return Success(E->getValue(), E); |
| 4804 | } |
| 4805 | |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 4806 | bool VisitUnaryReal(const UnaryOperator *E); |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 4807 | bool VisitUnaryImag(const UnaryOperator *E); |
| 4808 | |
Sebastian Redl | 5f0180d | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 4809 | bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E); |
Douglas Gregor | 820ba7b | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 4810 | bool VisitSizeOfPackExpr(const SizeOfPackExpr *E); |
Sebastian Redl | 12757ab | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 4811 | |
Chris Lattner | f8d7f72 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 4812 | private: |
Ken Dyck | 160146e | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 4813 | CharUnits GetAlignOfExpr(const Expr *E); |
| 4814 | CharUnits GetAlignOfType(QualType T); |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4815 | static QualType GetObjectType(APValue::LValueBase B); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4816 | bool TryEvaluateBuiltinObjectSize(const CallExpr *E); |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 4817 | // FIXME: Missing: array subscript of vector, member of vector |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 4818 | }; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4819 | } // end anonymous namespace |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 4820 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4821 | /// EvaluateIntegerOrLValue - Evaluate an rvalue integral-typed expression, and |
| 4822 | /// produce either the integer value or a pointer. |
| 4823 | /// |
| 4824 | /// GCC has a heinous extension which folds casts between pointer types and |
| 4825 | /// pointer-sized integral types. We support this by allowing the evaluation of |
| 4826 | /// an integer rvalue to produce a pointer (represented as an lvalue) instead. |
| 4827 | /// Some simple arithmetic on such values is supported (they are treated much |
| 4828 | /// like char*). |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4829 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4830 | EvalInfo &Info) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4831 | assert(E->isRValue() && E->getType()->isIntegralOrEnumerationType()); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4832 | return IntExprEvaluator(Info, Result).Visit(E); |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 4833 | } |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 4834 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4835 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info) { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4836 | APValue Val; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4837 | if (!EvaluateIntegerOrLValue(E, Val, Info)) |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 4838 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4839 | if (!Val.isInt()) { |
| 4840 | // FIXME: It would be better to produce the diagnostic for casting |
| 4841 | // a pointer to an integer. |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 4842 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4843 | return false; |
| 4844 | } |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 4845 | Result = Val.getInt(); |
| 4846 | return true; |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 4847 | } |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 4848 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4849 | /// Check whether the given declaration can be directly converted to an integral |
| 4850 | /// rvalue. If not, no diagnostic is produced; there are other things we can |
| 4851 | /// try. |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 4852 | bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) { |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 4853 | // Enums are integer constant exprs. |
Abramo Bagnara | 2caedf4 | 2011-06-30 09:36:05 +0000 | [diff] [blame] | 4854 | if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) { |
Abramo Bagnara | 9ae292d | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 4855 | // Check for signedness/width mismatches between E type and ECD value. |
| 4856 | bool SameSign = (ECD->getInitVal().isSigned() |
| 4857 | == E->getType()->isSignedIntegerOrEnumerationType()); |
| 4858 | bool SameWidth = (ECD->getInitVal().getBitWidth() |
| 4859 | == Info.Ctx.getIntWidth(E->getType())); |
| 4860 | if (SameSign && SameWidth) |
| 4861 | return Success(ECD->getInitVal(), E); |
| 4862 | else { |
| 4863 | // Get rid of mismatch (otherwise Success assertions will fail) |
| 4864 | // by computing a new value matching the type of E. |
| 4865 | llvm::APSInt Val = ECD->getInitVal(); |
| 4866 | if (!SameSign) |
| 4867 | Val.setIsSigned(!ECD->getInitVal().isSigned()); |
| 4868 | if (!SameWidth) |
| 4869 | Val = Val.extOrTrunc(Info.Ctx.getIntWidth(E->getType())); |
| 4870 | return Success(Val, E); |
| 4871 | } |
Abramo Bagnara | 2caedf4 | 2011-06-30 09:36:05 +0000 | [diff] [blame] | 4872 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4873 | return false; |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 4874 | } |
| 4875 | |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 4876 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
| 4877 | /// as GCC. |
| 4878 | static int EvaluateBuiltinClassifyType(const CallExpr *E) { |
| 4879 | // The following enum mimics the values returned by GCC. |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4880 | // FIXME: Does GCC differ between lvalue and rvalue references here? |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 4881 | enum gcc_type_class { |
| 4882 | no_type_class = -1, |
| 4883 | void_type_class, integer_type_class, char_type_class, |
| 4884 | enumeral_type_class, boolean_type_class, |
| 4885 | pointer_type_class, reference_type_class, offset_type_class, |
| 4886 | real_type_class, complex_type_class, |
| 4887 | function_type_class, method_type_class, |
| 4888 | record_type_class, union_type_class, |
| 4889 | array_type_class, string_type_class, |
| 4890 | lang_type_class |
| 4891 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4892 | |
| 4893 | // If no argument was supplied, default to "no_type_class". This isn't |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 4894 | // ideal, however it is what gcc does. |
| 4895 | if (E->getNumArgs() == 0) |
| 4896 | return no_type_class; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4897 | |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 4898 | QualType ArgTy = E->getArg(0)->getType(); |
| 4899 | if (ArgTy->isVoidType()) |
| 4900 | return void_type_class; |
| 4901 | else if (ArgTy->isEnumeralType()) |
| 4902 | return enumeral_type_class; |
| 4903 | else if (ArgTy->isBooleanType()) |
| 4904 | return boolean_type_class; |
| 4905 | else if (ArgTy->isCharType()) |
| 4906 | return string_type_class; // gcc doesn't appear to use char_type_class |
| 4907 | else if (ArgTy->isIntegerType()) |
| 4908 | return integer_type_class; |
| 4909 | else if (ArgTy->isPointerType()) |
| 4910 | return pointer_type_class; |
| 4911 | else if (ArgTy->isReferenceType()) |
| 4912 | return reference_type_class; |
| 4913 | else if (ArgTy->isRealType()) |
| 4914 | return real_type_class; |
| 4915 | else if (ArgTy->isComplexType()) |
| 4916 | return complex_type_class; |
| 4917 | else if (ArgTy->isFunctionType()) |
| 4918 | return function_type_class; |
Douglas Gregor | 8385a06 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 4919 | else if (ArgTy->isStructureOrClassType()) |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 4920 | return record_type_class; |
| 4921 | else if (ArgTy->isUnionType()) |
| 4922 | return union_type_class; |
| 4923 | else if (ArgTy->isArrayType()) |
| 4924 | return array_type_class; |
| 4925 | else if (ArgTy->isUnionType()) |
| 4926 | return union_type_class; |
| 4927 | else // FIXME: offset_type_class, method_type_class, & lang_type_class? |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4928 | llvm_unreachable("CallExpr::isBuiltinClassifyType(): unimplemented type"); |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 4929 | } |
| 4930 | |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 4931 | /// EvaluateBuiltinConstantPForLValue - Determine the result of |
| 4932 | /// __builtin_constant_p when applied to the given lvalue. |
| 4933 | /// |
| 4934 | /// An lvalue is only "constant" if it is a pointer or reference to the first |
| 4935 | /// character of a string literal. |
| 4936 | template<typename LValue> |
| 4937 | static bool EvaluateBuiltinConstantPForLValue(const LValue &LV) { |
Douglas Gregor | f31cee6 | 2012-03-11 02:23:56 +0000 | [diff] [blame] | 4938 | const Expr *E = LV.getLValueBase().template dyn_cast<const Expr*>(); |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 4939 | return E && isa<StringLiteral>(E) && LV.getLValueOffset().isZero(); |
| 4940 | } |
| 4941 | |
| 4942 | /// EvaluateBuiltinConstantP - Evaluate __builtin_constant_p as similarly to |
| 4943 | /// GCC as we can manage. |
| 4944 | static bool EvaluateBuiltinConstantP(ASTContext &Ctx, const Expr *Arg) { |
| 4945 | QualType ArgType = Arg->getType(); |
| 4946 | |
| 4947 | // __builtin_constant_p always has one operand. The rules which gcc follows |
| 4948 | // are not precisely documented, but are as follows: |
| 4949 | // |
| 4950 | // - If the operand is of integral, floating, complex or enumeration type, |
| 4951 | // and can be folded to a known value of that type, it returns 1. |
| 4952 | // - If the operand and can be folded to a pointer to the first character |
| 4953 | // of a string literal (or such a pointer cast to an integral type), it |
| 4954 | // returns 1. |
| 4955 | // |
| 4956 | // Otherwise, it returns 0. |
| 4957 | // |
| 4958 | // FIXME: GCC also intends to return 1 for literals of aggregate types, but |
| 4959 | // its support for this does not currently work. |
| 4960 | if (ArgType->isIntegralOrEnumerationType()) { |
| 4961 | Expr::EvalResult Result; |
| 4962 | if (!Arg->EvaluateAsRValue(Result, Ctx) || Result.HasSideEffects) |
| 4963 | return false; |
| 4964 | |
| 4965 | APValue &V = Result.Val; |
| 4966 | if (V.getKind() == APValue::Int) |
| 4967 | return true; |
| 4968 | |
| 4969 | return EvaluateBuiltinConstantPForLValue(V); |
| 4970 | } else if (ArgType->isFloatingType() || ArgType->isAnyComplexType()) { |
| 4971 | return Arg->isEvaluatable(Ctx); |
| 4972 | } else if (ArgType->isPointerType() || Arg->isGLValue()) { |
| 4973 | LValue LV; |
| 4974 | Expr::EvalStatus Status; |
| 4975 | EvalInfo Info(Ctx, Status); |
| 4976 | if ((Arg->isGLValue() ? EvaluateLValue(Arg, LV, Info) |
| 4977 | : EvaluatePointer(Arg, LV, Info)) && |
| 4978 | !Status.HasSideEffects) |
| 4979 | return EvaluateBuiltinConstantPForLValue(LV); |
| 4980 | } |
| 4981 | |
| 4982 | // Anything else isn't considered to be sufficiently constant. |
| 4983 | return false; |
| 4984 | } |
| 4985 | |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4986 | /// Retrieves the "underlying object type" of the given expression, |
| 4987 | /// as used by __builtin_object_size. |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4988 | QualType IntExprEvaluator::GetObjectType(APValue::LValueBase B) { |
| 4989 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
| 4990 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4991 | return VD->getType(); |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4992 | } else if (const Expr *E = B.get<const Expr*>()) { |
| 4993 | if (isa<CompoundLiteralExpr>(E)) |
| 4994 | return E->getType(); |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4995 | } |
| 4996 | |
| 4997 | return QualType(); |
| 4998 | } |
| 4999 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5000 | bool IntExprEvaluator::TryEvaluateBuiltinObjectSize(const CallExpr *E) { |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 5001 | LValue Base; |
Richard Smith | 01ade17 | 2012-05-23 04:13:20 +0000 | [diff] [blame] | 5002 | |
| 5003 | { |
| 5004 | // The operand of __builtin_object_size is never evaluated for side-effects. |
| 5005 | // If there are any, but we can determine the pointed-to object anyway, then |
| 5006 | // ignore the side-effects. |
| 5007 | SpeculativeEvaluationRAII SpeculativeEval(Info); |
| 5008 | if (!EvaluatePointer(E->getArg(0), Base, Info)) |
| 5009 | return false; |
| 5010 | } |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 5011 | |
| 5012 | // If we can prove the base is null, lower to zero now. |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 5013 | if (!Base.getLValueBase()) return Success(0, E); |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 5014 | |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 5015 | QualType T = GetObjectType(Base.getLValueBase()); |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 5016 | if (T.isNull() || |
| 5017 | T->isIncompleteType() || |
Eli Friedman | a170cd6 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 5018 | T->isFunctionType() || |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 5019 | T->isVariablyModifiedType() || |
| 5020 | T->isDependentType()) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5021 | return Error(E); |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 5022 | |
| 5023 | CharUnits Size = Info.Ctx.getTypeSizeInChars(T); |
| 5024 | CharUnits Offset = Base.getLValueOffset(); |
| 5025 | |
| 5026 | if (!Offset.isNegative() && Offset <= Size) |
| 5027 | Size -= Offset; |
| 5028 | else |
| 5029 | Size = CharUnits::Zero(); |
Ken Dyck | dbc0191 | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 5030 | return Success(Size, E); |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 5031 | } |
| 5032 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5033 | bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | 01ba47d | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 5034 | switch (unsigned BuiltinOp = E->isBuiltinCall()) { |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 5035 | default: |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5036 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 5037 | |
| 5038 | case Builtin::BI__builtin_object_size: { |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 5039 | if (TryEvaluateBuiltinObjectSize(E)) |
| 5040 | return true; |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 5041 | |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 5042 | // If evaluating the argument has side-effects, we can't determine the size |
| 5043 | // of the object, and so we lower it to unknown now. CodeGen relies on us to |
| 5044 | // handle all cases where the expression has side-effects. |
Fariborz Jahanian | 4127b8e | 2009-11-05 18:03:03 +0000 | [diff] [blame] | 5045 | if (E->getArg(0)->HasSideEffects(Info.Ctx)) { |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5046 | if (E->getArg(1)->EvaluateKnownConstInt(Info.Ctx).getZExtValue() <= 1) |
Chris Lattner | 4f10559 | 2009-11-03 19:48:51 +0000 | [diff] [blame] | 5047 | return Success(-1ULL, E); |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 5048 | return Success(0, E); |
| 5049 | } |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 5050 | |
Richard Smith | 01ade17 | 2012-05-23 04:13:20 +0000 | [diff] [blame] | 5051 | // Expression had no side effects, but we couldn't statically determine the |
| 5052 | // size of the referenced object. |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5053 | return Error(E); |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 5054 | } |
| 5055 | |
Benjamin Kramer | a801f4a | 2012-10-06 14:42:22 +0000 | [diff] [blame] | 5056 | case Builtin::BI__builtin_bswap16: |
Richard Smith | 80ac9ef | 2012-09-28 20:20:52 +0000 | [diff] [blame] | 5057 | case Builtin::BI__builtin_bswap32: |
| 5058 | case Builtin::BI__builtin_bswap64: { |
| 5059 | APSInt Val; |
| 5060 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 5061 | return false; |
| 5062 | |
| 5063 | return Success(Val.byteSwap(), E); |
| 5064 | } |
| 5065 | |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 5066 | case Builtin::BI__builtin_classify_type: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5067 | return Success(EvaluateBuiltinClassifyType(E), E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5068 | |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 5069 | case Builtin::BI__builtin_constant_p: |
| 5070 | return Success(EvaluateBuiltinConstantP(Info.Ctx, E->getArg(0)), E); |
Richard Smith | 10c7c90 | 2011-12-09 02:04:48 +0000 | [diff] [blame] | 5071 | |
Chris Lattner | d545ad1 | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 5072 | case Builtin::BI__builtin_eh_return_data_regno: { |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5073 | int Operand = E->getArg(0)->EvaluateKnownConstInt(Info.Ctx).getZExtValue(); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 5074 | Operand = Info.Ctx.getTargetInfo().getEHDataRegisterNumber(Operand); |
Chris Lattner | d545ad1 | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 5075 | return Success(Operand, E); |
| 5076 | } |
Eli Friedman | d5c9399 | 2010-02-13 00:10:10 +0000 | [diff] [blame] | 5077 | |
| 5078 | case Builtin::BI__builtin_expect: |
| 5079 | return Visit(E->getArg(0)); |
Richard Smith | 9cf080f | 2012-01-18 03:06:12 +0000 | [diff] [blame] | 5080 | |
Douglas Gregor | 6a6dac2 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 5081 | case Builtin::BIstrlen: |
Richard Smith | 9cf080f | 2012-01-18 03:06:12 +0000 | [diff] [blame] | 5082 | // A call to strlen is not a constant expression. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5083 | if (Info.getLangOpts().CPlusPlus11) |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 5084 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
Richard Smith | 9cf080f | 2012-01-18 03:06:12 +0000 | [diff] [blame] | 5085 | << /*isConstexpr*/0 << /*isConstructor*/0 << "'strlen'"; |
| 5086 | else |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 5087 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 9cf080f | 2012-01-18 03:06:12 +0000 | [diff] [blame] | 5088 | // Fall through. |
Douglas Gregor | 6a6dac2 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 5089 | case Builtin::BI__builtin_strlen: |
| 5090 | // As an extension, we support strlen() and __builtin_strlen() as constant |
| 5091 | // expressions when the argument is a string literal. |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5092 | if (const StringLiteral *S |
Douglas Gregor | 6a6dac2 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 5093 | = dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenImpCasts())) { |
| 5094 | // The string literal may have embedded null characters. Find the first |
| 5095 | // one and truncate there. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5096 | StringRef Str = S->getString(); |
| 5097 | StringRef::size_type Pos = Str.find(0); |
| 5098 | if (Pos != StringRef::npos) |
Douglas Gregor | 6a6dac2 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 5099 | Str = Str.substr(0, Pos); |
| 5100 | |
| 5101 | return Success(Str.size(), E); |
| 5102 | } |
| 5103 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5104 | return Error(E); |
Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 5105 | |
Richard Smith | 01ba47d | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 5106 | case Builtin::BI__atomic_always_lock_free: |
Richard Smith | b1e36c6 | 2012-04-11 17:55:32 +0000 | [diff] [blame] | 5107 | case Builtin::BI__atomic_is_lock_free: |
| 5108 | case Builtin::BI__c11_atomic_is_lock_free: { |
Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 5109 | APSInt SizeVal; |
| 5110 | if (!EvaluateInteger(E->getArg(0), SizeVal, Info)) |
| 5111 | return false; |
| 5112 | |
| 5113 | // For __atomic_is_lock_free(sizeof(_Atomic(T))), if the size is a power |
| 5114 | // of two less than the maximum inline atomic width, we know it is |
| 5115 | // lock-free. If the size isn't a power of two, or greater than the |
| 5116 | // maximum alignment where we promote atomics, we know it is not lock-free |
| 5117 | // (at least not in the sense of atomic_is_lock_free). Otherwise, |
| 5118 | // the answer can only be determined at runtime; for example, 16-byte |
| 5119 | // atomics have lock-free implementations on some, but not all, |
| 5120 | // x86-64 processors. |
| 5121 | |
| 5122 | // Check power-of-two. |
| 5123 | CharUnits Size = CharUnits::fromQuantity(SizeVal.getZExtValue()); |
Richard Smith | 01ba47d | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 5124 | if (Size.isPowerOfTwo()) { |
| 5125 | // Check against inlining width. |
| 5126 | unsigned InlineWidthBits = |
| 5127 | Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth(); |
| 5128 | if (Size <= Info.Ctx.toCharUnitsFromBits(InlineWidthBits)) { |
| 5129 | if (BuiltinOp == Builtin::BI__c11_atomic_is_lock_free || |
| 5130 | Size == CharUnits::One() || |
| 5131 | E->getArg(1)->isNullPointerConstant(Info.Ctx, |
| 5132 | Expr::NPC_NeverValueDependent)) |
| 5133 | // OK, we will inline appropriately-aligned operations of this size, |
| 5134 | // and _Atomic(T) is appropriately-aligned. |
| 5135 | return Success(1, E); |
Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 5136 | |
Richard Smith | 01ba47d | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 5137 | QualType PointeeType = E->getArg(1)->IgnoreImpCasts()->getType()-> |
| 5138 | castAs<PointerType>()->getPointeeType(); |
| 5139 | if (!PointeeType->isIncompleteType() && |
| 5140 | Info.Ctx.getTypeAlignInChars(PointeeType) >= Size) { |
| 5141 | // OK, we will inline operations on this object. |
| 5142 | return Success(1, E); |
| 5143 | } |
| 5144 | } |
| 5145 | } |
Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 5146 | |
Richard Smith | 01ba47d | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 5147 | return BuiltinOp == Builtin::BI__atomic_always_lock_free ? |
| 5148 | Success(0, E) : Error(E); |
Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 5149 | } |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 5150 | } |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 5151 | } |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 5152 | |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 5153 | static bool HasSameBase(const LValue &A, const LValue &B) { |
| 5154 | if (!A.getLValueBase()) |
| 5155 | return !B.getLValueBase(); |
| 5156 | if (!B.getLValueBase()) |
| 5157 | return false; |
| 5158 | |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 5159 | if (A.getLValueBase().getOpaqueValue() != |
| 5160 | B.getLValueBase().getOpaqueValue()) { |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 5161 | const Decl *ADecl = GetLValueBaseDecl(A); |
| 5162 | if (!ADecl) |
| 5163 | return false; |
| 5164 | const Decl *BDecl = GetLValueBaseDecl(B); |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 5165 | if (!BDecl || ADecl->getCanonicalDecl() != BDecl->getCanonicalDecl()) |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 5166 | return false; |
| 5167 | } |
| 5168 | |
| 5169 | return IsGlobalLValue(A.getLValueBase()) || |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 5170 | A.getLValueCallIndex() == B.getLValueCallIndex(); |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 5171 | } |
| 5172 | |
Richard Smith | c804232 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 5173 | /// Perform the given integer operation, which is known to need at most BitWidth |
| 5174 | /// bits, and check for overflow in the original type (if that type was not an |
| 5175 | /// unsigned type). |
| 5176 | template<typename Operation> |
| 5177 | static APSInt CheckedIntArithmetic(EvalInfo &Info, const Expr *E, |
| 5178 | const APSInt &LHS, const APSInt &RHS, |
| 5179 | unsigned BitWidth, Operation Op) { |
| 5180 | if (LHS.isUnsigned()) |
| 5181 | return Op(LHS, RHS); |
| 5182 | |
| 5183 | APSInt Value(Op(LHS.extend(BitWidth), RHS.extend(BitWidth)), false); |
| 5184 | APSInt Result = Value.trunc(LHS.getBitWidth()); |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 5185 | if (Result.extend(BitWidth) != Value) { |
| 5186 | if (Info.getIntOverflowCheckMode()) |
| 5187 | Info.Ctx.getDiagnostics().Report(E->getExprLoc(), |
| 5188 | diag::warn_integer_constant_overflow) |
| 5189 | << Result.toString(10) << E->getType(); |
| 5190 | else |
| 5191 | HandleOverflow(Info, E, Value, E->getType()); |
| 5192 | } |
Richard Smith | c804232 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 5193 | return Result; |
| 5194 | } |
| 5195 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5196 | namespace { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5197 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5198 | /// \brief Data recursive integer evaluator of certain binary operators. |
| 5199 | /// |
| 5200 | /// We use a data recursive algorithm for binary operators so that we are able |
| 5201 | /// to handle extreme cases of chained binary operators without causing stack |
| 5202 | /// overflow. |
| 5203 | class DataRecursiveIntBinOpEvaluator { |
| 5204 | struct EvalResult { |
| 5205 | APValue Val; |
| 5206 | bool Failed; |
| 5207 | |
| 5208 | EvalResult() : Failed(false) { } |
| 5209 | |
| 5210 | void swap(EvalResult &RHS) { |
| 5211 | Val.swap(RHS.Val); |
| 5212 | Failed = RHS.Failed; |
| 5213 | RHS.Failed = false; |
| 5214 | } |
| 5215 | }; |
| 5216 | |
| 5217 | struct Job { |
| 5218 | const Expr *E; |
| 5219 | EvalResult LHSResult; // meaningful only for binary operator expression. |
| 5220 | enum { AnyExprKind, BinOpKind, BinOpVisitedLHSKind } Kind; |
| 5221 | |
| 5222 | Job() : StoredInfo(0) { } |
| 5223 | void startSpeculativeEval(EvalInfo &Info) { |
| 5224 | OldEvalStatus = Info.EvalStatus; |
| 5225 | Info.EvalStatus.Diag = 0; |
| 5226 | StoredInfo = &Info; |
| 5227 | } |
| 5228 | ~Job() { |
| 5229 | if (StoredInfo) { |
| 5230 | StoredInfo->EvalStatus = OldEvalStatus; |
| 5231 | } |
| 5232 | } |
| 5233 | private: |
| 5234 | EvalInfo *StoredInfo; // non-null if status changed. |
| 5235 | Expr::EvalStatus OldEvalStatus; |
| 5236 | }; |
| 5237 | |
| 5238 | SmallVector<Job, 16> Queue; |
| 5239 | |
| 5240 | IntExprEvaluator &IntEval; |
| 5241 | EvalInfo &Info; |
| 5242 | APValue &FinalResult; |
| 5243 | |
| 5244 | public: |
| 5245 | DataRecursiveIntBinOpEvaluator(IntExprEvaluator &IntEval, APValue &Result) |
| 5246 | : IntEval(IntEval), Info(IntEval.getEvalInfo()), FinalResult(Result) { } |
| 5247 | |
| 5248 | /// \brief True if \param E is a binary operator that we are going to handle |
| 5249 | /// data recursively. |
| 5250 | /// We handle binary operators that are comma, logical, or that have operands |
| 5251 | /// with integral or enumeration type. |
| 5252 | static bool shouldEnqueue(const BinaryOperator *E) { |
| 5253 | return E->getOpcode() == BO_Comma || |
| 5254 | E->isLogicalOp() || |
| 5255 | (E->getLHS()->getType()->isIntegralOrEnumerationType() && |
| 5256 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 5257 | } |
| 5258 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5259 | bool Traverse(const BinaryOperator *E) { |
| 5260 | enqueue(E); |
| 5261 | EvalResult PrevResult; |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 5262 | while (!Queue.empty()) |
| 5263 | process(PrevResult); |
| 5264 | |
| 5265 | if (PrevResult.Failed) return false; |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 5266 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5267 | FinalResult.swap(PrevResult.Val); |
| 5268 | return true; |
| 5269 | } |
| 5270 | |
| 5271 | private: |
| 5272 | bool Success(uint64_t Value, const Expr *E, APValue &Result) { |
| 5273 | return IntEval.Success(Value, E, Result); |
| 5274 | } |
| 5275 | bool Success(const APSInt &Value, const Expr *E, APValue &Result) { |
| 5276 | return IntEval.Success(Value, E, Result); |
| 5277 | } |
| 5278 | bool Error(const Expr *E) { |
| 5279 | return IntEval.Error(E); |
| 5280 | } |
| 5281 | bool Error(const Expr *E, diag::kind D) { |
| 5282 | return IntEval.Error(E, D); |
| 5283 | } |
| 5284 | |
| 5285 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { |
| 5286 | return Info.CCEDiag(E, D); |
| 5287 | } |
| 5288 | |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 5289 | // \brief Returns true if visiting the RHS is necessary, false otherwise. |
| 5290 | bool VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E, |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5291 | bool &SuppressRHSDiags); |
| 5292 | |
| 5293 | bool VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult, |
| 5294 | const BinaryOperator *E, APValue &Result); |
| 5295 | |
| 5296 | void EvaluateExpr(const Expr *E, EvalResult &Result) { |
| 5297 | Result.Failed = !Evaluate(Result.Val, Info, E); |
| 5298 | if (Result.Failed) |
| 5299 | Result.Val = APValue(); |
| 5300 | } |
| 5301 | |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 5302 | void process(EvalResult &Result); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5303 | |
| 5304 | void enqueue(const Expr *E) { |
| 5305 | E = E->IgnoreParens(); |
| 5306 | Queue.resize(Queue.size()+1); |
| 5307 | Queue.back().E = E; |
| 5308 | Queue.back().Kind = Job::AnyExprKind; |
| 5309 | } |
| 5310 | }; |
| 5311 | |
| 5312 | } |
| 5313 | |
| 5314 | bool DataRecursiveIntBinOpEvaluator:: |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 5315 | VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E, |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5316 | bool &SuppressRHSDiags) { |
| 5317 | if (E->getOpcode() == BO_Comma) { |
| 5318 | // Ignore LHS but note if we could not evaluate it. |
| 5319 | if (LHSResult.Failed) |
| 5320 | Info.EvalStatus.HasSideEffects = true; |
| 5321 | return true; |
| 5322 | } |
| 5323 | |
| 5324 | if (E->isLogicalOp()) { |
| 5325 | bool lhsResult; |
| 5326 | if (HandleConversionToBool(LHSResult.Val, lhsResult)) { |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 5327 | // We were able to evaluate the LHS, see if we can get away with not |
| 5328 | // evaluating the RHS: 0 && X -> 0, 1 || X -> 1 |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5329 | if (lhsResult == (E->getOpcode() == BO_LOr)) { |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 5330 | Success(lhsResult, E, LHSResult.Val); |
| 5331 | return false; // Ignore RHS |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 5332 | } |
| 5333 | } else { |
| 5334 | // Since we weren't able to evaluate the left hand side, it |
| 5335 | // must have had side effects. |
| 5336 | Info.EvalStatus.HasSideEffects = true; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5337 | |
| 5338 | // We can't evaluate the LHS; however, sometimes the result |
| 5339 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
| 5340 | // Don't ignore RHS and suppress diagnostics from this arm. |
| 5341 | SuppressRHSDiags = true; |
| 5342 | } |
| 5343 | |
| 5344 | return true; |
| 5345 | } |
| 5346 | |
| 5347 | assert(E->getLHS()->getType()->isIntegralOrEnumerationType() && |
| 5348 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
| 5349 | |
| 5350 | if (LHSResult.Failed && !Info.keepEvaluatingAfterFailure()) |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 5351 | return false; // Ignore RHS; |
| 5352 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5353 | return true; |
| 5354 | } |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 5355 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5356 | bool DataRecursiveIntBinOpEvaluator:: |
| 5357 | VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult, |
| 5358 | const BinaryOperator *E, APValue &Result) { |
| 5359 | if (E->getOpcode() == BO_Comma) { |
| 5360 | if (RHSResult.Failed) |
| 5361 | return false; |
| 5362 | Result = RHSResult.Val; |
| 5363 | return true; |
| 5364 | } |
| 5365 | |
| 5366 | if (E->isLogicalOp()) { |
| 5367 | bool lhsResult, rhsResult; |
| 5368 | bool LHSIsOK = HandleConversionToBool(LHSResult.Val, lhsResult); |
| 5369 | bool RHSIsOK = HandleConversionToBool(RHSResult.Val, rhsResult); |
| 5370 | |
| 5371 | if (LHSIsOK) { |
| 5372 | if (RHSIsOK) { |
| 5373 | if (E->getOpcode() == BO_LOr) |
| 5374 | return Success(lhsResult || rhsResult, E, Result); |
| 5375 | else |
| 5376 | return Success(lhsResult && rhsResult, E, Result); |
| 5377 | } |
| 5378 | } else { |
| 5379 | if (RHSIsOK) { |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 5380 | // We can't evaluate the LHS; however, sometimes the result |
| 5381 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
| 5382 | if (rhsResult == (E->getOpcode() == BO_LOr)) |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5383 | return Success(rhsResult, E, Result); |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 5384 | } |
| 5385 | } |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5386 | |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 5387 | return false; |
| 5388 | } |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5389 | |
| 5390 | assert(E->getLHS()->getType()->isIntegralOrEnumerationType() && |
| 5391 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
| 5392 | |
| 5393 | if (LHSResult.Failed || RHSResult.Failed) |
| 5394 | return false; |
| 5395 | |
| 5396 | const APValue &LHSVal = LHSResult.Val; |
| 5397 | const APValue &RHSVal = RHSResult.Val; |
| 5398 | |
| 5399 | // Handle cases like (unsigned long)&a + 4. |
| 5400 | if (E->isAdditiveOp() && LHSVal.isLValue() && RHSVal.isInt()) { |
| 5401 | Result = LHSVal; |
| 5402 | CharUnits AdditionalOffset = CharUnits::fromQuantity( |
| 5403 | RHSVal.getInt().getZExtValue()); |
| 5404 | if (E->getOpcode() == BO_Add) |
| 5405 | Result.getLValueOffset() += AdditionalOffset; |
| 5406 | else |
| 5407 | Result.getLValueOffset() -= AdditionalOffset; |
| 5408 | return true; |
| 5409 | } |
| 5410 | |
| 5411 | // Handle cases like 4 + (unsigned long)&a |
| 5412 | if (E->getOpcode() == BO_Add && |
| 5413 | RHSVal.isLValue() && LHSVal.isInt()) { |
| 5414 | Result = RHSVal; |
| 5415 | Result.getLValueOffset() += CharUnits::fromQuantity( |
| 5416 | LHSVal.getInt().getZExtValue()); |
| 5417 | return true; |
| 5418 | } |
| 5419 | |
| 5420 | if (E->getOpcode() == BO_Sub && LHSVal.isLValue() && RHSVal.isLValue()) { |
| 5421 | // Handle (intptr_t)&&A - (intptr_t)&&B. |
| 5422 | if (!LHSVal.getLValueOffset().isZero() || |
| 5423 | !RHSVal.getLValueOffset().isZero()) |
| 5424 | return false; |
| 5425 | const Expr *LHSExpr = LHSVal.getLValueBase().dyn_cast<const Expr*>(); |
| 5426 | const Expr *RHSExpr = RHSVal.getLValueBase().dyn_cast<const Expr*>(); |
| 5427 | if (!LHSExpr || !RHSExpr) |
| 5428 | return false; |
| 5429 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); |
| 5430 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); |
| 5431 | if (!LHSAddrExpr || !RHSAddrExpr) |
| 5432 | return false; |
| 5433 | // Make sure both labels come from the same function. |
| 5434 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
| 5435 | RHSAddrExpr->getLabel()->getDeclContext()) |
| 5436 | return false; |
| 5437 | Result = APValue(LHSAddrExpr, RHSAddrExpr); |
| 5438 | return true; |
| 5439 | } |
| 5440 | |
| 5441 | // All the following cases expect both operands to be an integer |
| 5442 | if (!LHSVal.isInt() || !RHSVal.isInt()) |
| 5443 | return Error(E); |
| 5444 | |
| 5445 | const APSInt &LHS = LHSVal.getInt(); |
| 5446 | APSInt RHS = RHSVal.getInt(); |
| 5447 | |
| 5448 | switch (E->getOpcode()) { |
| 5449 | default: |
| 5450 | return Error(E); |
| 5451 | case BO_Mul: |
| 5452 | return Success(CheckedIntArithmetic(Info, E, LHS, RHS, |
| 5453 | LHS.getBitWidth() * 2, |
| 5454 | std::multiplies<APSInt>()), E, |
| 5455 | Result); |
| 5456 | case BO_Add: |
| 5457 | return Success(CheckedIntArithmetic(Info, E, LHS, RHS, |
| 5458 | LHS.getBitWidth() + 1, |
| 5459 | std::plus<APSInt>()), E, Result); |
| 5460 | case BO_Sub: |
| 5461 | return Success(CheckedIntArithmetic(Info, E, LHS, RHS, |
| 5462 | LHS.getBitWidth() + 1, |
| 5463 | std::minus<APSInt>()), E, Result); |
| 5464 | case BO_And: return Success(LHS & RHS, E, Result); |
| 5465 | case BO_Xor: return Success(LHS ^ RHS, E, Result); |
| 5466 | case BO_Or: return Success(LHS | RHS, E, Result); |
| 5467 | case BO_Div: |
| 5468 | case BO_Rem: |
| 5469 | if (RHS == 0) |
| 5470 | return Error(E, diag::note_expr_divide_by_zero); |
| 5471 | // Check for overflow case: INT_MIN / -1 or INT_MIN % -1. The latter is |
| 5472 | // not actually undefined behavior in C++11 due to a language defect. |
| 5473 | if (RHS.isNegative() && RHS.isAllOnesValue() && |
| 5474 | LHS.isSigned() && LHS.isMinSignedValue()) |
| 5475 | HandleOverflow(Info, E, -LHS.extend(LHS.getBitWidth() + 1), E->getType()); |
| 5476 | return Success(E->getOpcode() == BO_Rem ? LHS % RHS : LHS / RHS, E, |
| 5477 | Result); |
| 5478 | case BO_Shl: { |
David Tweed | 042e088 | 2013-01-07 16:43:27 +0000 | [diff] [blame] | 5479 | if (Info.getLangOpts().OpenCL) |
| 5480 | // OpenCL 6.3j: shift values are effectively % word size of LHS. |
Joey Gouly | 0942e0b | 2013-01-29 15:09:40 +0000 | [diff] [blame] | 5481 | RHS &= APSInt(llvm::APInt(RHS.getBitWidth(), |
David Tweed | 042e088 | 2013-01-07 16:43:27 +0000 | [diff] [blame] | 5482 | static_cast<uint64_t>(LHS.getBitWidth() - 1)), |
| 5483 | RHS.isUnsigned()); |
| 5484 | else if (RHS.isSigned() && RHS.isNegative()) { |
| 5485 | // During constant-folding, a negative shift is an opposite shift. Such |
| 5486 | // a shift is not a constant expression. |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5487 | CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; |
| 5488 | RHS = -RHS; |
| 5489 | goto shift_right; |
| 5490 | } |
| 5491 | |
| 5492 | shift_left: |
| 5493 | // C++11 [expr.shift]p1: Shift width must be less than the bit width of |
| 5494 | // the shifted type. |
| 5495 | unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
| 5496 | if (SA != RHS) { |
| 5497 | CCEDiag(E, diag::note_constexpr_large_shift) |
| 5498 | << RHS << E->getType() << LHS.getBitWidth(); |
| 5499 | } else if (LHS.isSigned()) { |
| 5500 | // C++11 [expr.shift]p2: A signed left shift must have a non-negative |
| 5501 | // operand, and must not overflow the corresponding unsigned type. |
| 5502 | if (LHS.isNegative()) |
| 5503 | CCEDiag(E, diag::note_constexpr_lshift_of_negative) << LHS; |
| 5504 | else if (LHS.countLeadingZeros() < SA) |
| 5505 | CCEDiag(E, diag::note_constexpr_lshift_discards); |
| 5506 | } |
| 5507 | |
| 5508 | return Success(LHS << SA, E, Result); |
| 5509 | } |
| 5510 | case BO_Shr: { |
David Tweed | 042e088 | 2013-01-07 16:43:27 +0000 | [diff] [blame] | 5511 | if (Info.getLangOpts().OpenCL) |
| 5512 | // OpenCL 6.3j: shift values are effectively % word size of LHS. |
Joey Gouly | 0942e0b | 2013-01-29 15:09:40 +0000 | [diff] [blame] | 5513 | RHS &= APSInt(llvm::APInt(RHS.getBitWidth(), |
David Tweed | 042e088 | 2013-01-07 16:43:27 +0000 | [diff] [blame] | 5514 | static_cast<uint64_t>(LHS.getBitWidth() - 1)), |
| 5515 | RHS.isUnsigned()); |
| 5516 | else if (RHS.isSigned() && RHS.isNegative()) { |
| 5517 | // During constant-folding, a negative shift is an opposite shift. Such a |
| 5518 | // shift is not a constant expression. |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5519 | CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; |
| 5520 | RHS = -RHS; |
| 5521 | goto shift_left; |
| 5522 | } |
| 5523 | |
| 5524 | shift_right: |
| 5525 | // C++11 [expr.shift]p1: Shift width must be less than the bit width of the |
| 5526 | // shifted type. |
| 5527 | unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
| 5528 | if (SA != RHS) |
| 5529 | CCEDiag(E, diag::note_constexpr_large_shift) |
| 5530 | << RHS << E->getType() << LHS.getBitWidth(); |
| 5531 | |
| 5532 | return Success(LHS >> SA, E, Result); |
| 5533 | } |
| 5534 | |
| 5535 | case BO_LT: return Success(LHS < RHS, E, Result); |
| 5536 | case BO_GT: return Success(LHS > RHS, E, Result); |
| 5537 | case BO_LE: return Success(LHS <= RHS, E, Result); |
| 5538 | case BO_GE: return Success(LHS >= RHS, E, Result); |
| 5539 | case BO_EQ: return Success(LHS == RHS, E, Result); |
| 5540 | case BO_NE: return Success(LHS != RHS, E, Result); |
| 5541 | } |
| 5542 | } |
| 5543 | |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 5544 | void DataRecursiveIntBinOpEvaluator::process(EvalResult &Result) { |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5545 | Job &job = Queue.back(); |
| 5546 | |
| 5547 | switch (job.Kind) { |
| 5548 | case Job::AnyExprKind: { |
| 5549 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(job.E)) { |
| 5550 | if (shouldEnqueue(Bop)) { |
| 5551 | job.Kind = Job::BinOpKind; |
| 5552 | enqueue(Bop->getLHS()); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 5553 | return; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5554 | } |
| 5555 | } |
| 5556 | |
| 5557 | EvaluateExpr(job.E, Result); |
| 5558 | Queue.pop_back(); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 5559 | return; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5560 | } |
| 5561 | |
| 5562 | case Job::BinOpKind: { |
| 5563 | const BinaryOperator *Bop = cast<BinaryOperator>(job.E); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5564 | bool SuppressRHSDiags = false; |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 5565 | if (!VisitBinOpLHSOnly(Result, Bop, SuppressRHSDiags)) { |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5566 | Queue.pop_back(); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 5567 | return; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5568 | } |
| 5569 | if (SuppressRHSDiags) |
| 5570 | job.startSpeculativeEval(Info); |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 5571 | job.LHSResult.swap(Result); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5572 | job.Kind = Job::BinOpVisitedLHSKind; |
| 5573 | enqueue(Bop->getRHS()); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 5574 | return; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5575 | } |
| 5576 | |
| 5577 | case Job::BinOpVisitedLHSKind: { |
| 5578 | const BinaryOperator *Bop = cast<BinaryOperator>(job.E); |
| 5579 | EvalResult RHS; |
| 5580 | RHS.swap(Result); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 5581 | Result.Failed = !VisitBinOp(job.LHSResult, RHS, Bop, Result.Val); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5582 | Queue.pop_back(); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 5583 | return; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5584 | } |
| 5585 | } |
| 5586 | |
| 5587 | llvm_unreachable("Invalid Job::Kind!"); |
| 5588 | } |
| 5589 | |
| 5590 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
| 5591 | if (E->isAssignmentOp()) |
| 5592 | return Error(E); |
| 5593 | |
| 5594 | if (DataRecursiveIntBinOpEvaluator::shouldEnqueue(E)) |
| 5595 | return DataRecursiveIntBinOpEvaluator(*this, Result).Traverse(E); |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 5596 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 5597 | QualType LHSTy = E->getLHS()->getType(); |
| 5598 | QualType RHSTy = E->getRHS()->getType(); |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 5599 | |
| 5600 | if (LHSTy->isAnyComplexType()) { |
| 5601 | assert(RHSTy->isAnyComplexType() && "Invalid comparison"); |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5602 | ComplexValue LHS, RHS; |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 5603 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5604 | bool LHSOK = EvaluateComplex(E->getLHS(), LHS, Info); |
| 5605 | if (!LHSOK && !Info.keepEvaluatingAfterFailure()) |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 5606 | return false; |
| 5607 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5608 | if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK) |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 5609 | return false; |
| 5610 | |
| 5611 | if (LHS.isComplexFloat()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5612 | APFloat::cmpResult CR_r = |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 5613 | LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5614 | APFloat::cmpResult CR_i = |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 5615 | LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag()); |
| 5616 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5617 | if (E->getOpcode() == BO_EQ) |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5618 | return Success((CR_r == APFloat::cmpEqual && |
| 5619 | CR_i == APFloat::cmpEqual), E); |
| 5620 | else { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5621 | assert(E->getOpcode() == BO_NE && |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5622 | "Invalid complex comparison."); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5623 | return Success(((CR_r == APFloat::cmpGreaterThan || |
Mon P Wang | 75c645c | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 5624 | CR_r == APFloat::cmpLessThan || |
| 5625 | CR_r == APFloat::cmpUnordered) || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5626 | (CR_i == APFloat::cmpGreaterThan || |
Mon P Wang | 75c645c | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 5627 | CR_i == APFloat::cmpLessThan || |
| 5628 | CR_i == APFloat::cmpUnordered)), E); |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5629 | } |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 5630 | } else { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5631 | if (E->getOpcode() == BO_EQ) |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5632 | return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() && |
| 5633 | LHS.getComplexIntImag() == RHS.getComplexIntImag()), E); |
| 5634 | else { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5635 | assert(E->getOpcode() == BO_NE && |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5636 | "Invalid compex comparison."); |
| 5637 | return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() || |
| 5638 | LHS.getComplexIntImag() != RHS.getComplexIntImag()), E); |
| 5639 | } |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 5640 | } |
| 5641 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5642 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 5643 | if (LHSTy->isRealFloatingType() && |
| 5644 | RHSTy->isRealFloatingType()) { |
| 5645 | APFloat RHS(0.0), LHS(0.0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5646 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5647 | bool LHSOK = EvaluateFloat(E->getRHS(), RHS, Info); |
| 5648 | if (!LHSOK && !Info.keepEvaluatingAfterFailure()) |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 5649 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5650 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5651 | if (!EvaluateFloat(E->getLHS(), LHS, Info) || !LHSOK) |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 5652 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5653 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 5654 | APFloat::cmpResult CR = LHS.compare(RHS); |
Anders Carlsson | 899c705 | 2008-11-16 22:46:56 +0000 | [diff] [blame] | 5655 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 5656 | switch (E->getOpcode()) { |
| 5657 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5658 | llvm_unreachable("Invalid binary operator!"); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5659 | case BO_LT: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5660 | return Success(CR == APFloat::cmpLessThan, E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5661 | case BO_GT: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5662 | return Success(CR == APFloat::cmpGreaterThan, E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5663 | case BO_LE: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5664 | return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5665 | case BO_GE: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5666 | return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual, |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5667 | E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5668 | case BO_EQ: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5669 | return Success(CR == APFloat::cmpEqual, E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5670 | case BO_NE: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5671 | return Success(CR == APFloat::cmpGreaterThan |
Mon P Wang | 75c645c | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 5672 | || CR == APFloat::cmpLessThan |
| 5673 | || CR == APFloat::cmpUnordered, E); |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 5674 | } |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 5675 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5676 | |
Eli Friedman | a38da57 | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 5677 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 5678 | if (E->getOpcode() == BO_Sub || E->isComparisonOp()) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5679 | LValue LHSValue, RHSValue; |
| 5680 | |
| 5681 | bool LHSOK = EvaluatePointer(E->getLHS(), LHSValue, Info); |
| 5682 | if (!LHSOK && Info.keepEvaluatingAfterFailure()) |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 5683 | return false; |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 5684 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5685 | if (!EvaluatePointer(E->getRHS(), RHSValue, Info) || !LHSOK) |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 5686 | return false; |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 5687 | |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 5688 | // Reject differing bases from the normal codepath; we special-case |
| 5689 | // comparisons to null. |
| 5690 | if (!HasSameBase(LHSValue, RHSValue)) { |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 5691 | if (E->getOpcode() == BO_Sub) { |
| 5692 | // Handle &&A - &&B. |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 5693 | if (!LHSValue.Offset.isZero() || !RHSValue.Offset.isZero()) |
| 5694 | return false; |
| 5695 | const Expr *LHSExpr = LHSValue.Base.dyn_cast<const Expr*>(); |
Benjamin Kramer | daa09612 | 2012-10-03 14:15:39 +0000 | [diff] [blame] | 5696 | const Expr *RHSExpr = RHSValue.Base.dyn_cast<const Expr*>(); |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 5697 | if (!LHSExpr || !RHSExpr) |
| 5698 | return false; |
| 5699 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); |
| 5700 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); |
| 5701 | if (!LHSAddrExpr || !RHSAddrExpr) |
| 5702 | return false; |
Eli Friedman | b1bc368 | 2012-01-05 23:59:40 +0000 | [diff] [blame] | 5703 | // Make sure both labels come from the same function. |
| 5704 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
| 5705 | RHSAddrExpr->getLabel()->getDeclContext()) |
| 5706 | return false; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5707 | Result = APValue(LHSAddrExpr, RHSAddrExpr); |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 5708 | return true; |
| 5709 | } |
Richard Smith | 83c6821 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 5710 | // Inequalities and subtractions between unrelated pointers have |
| 5711 | // unspecified or undefined behavior. |
Eli Friedman | 334046a | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 5712 | if (!E->isEqualityOp()) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5713 | return Error(E); |
Eli Friedman | c6be94b | 2011-10-31 22:28:05 +0000 | [diff] [blame] | 5714 | // A constant address may compare equal to the address of a symbol. |
| 5715 | // The one exception is that address of an object cannot compare equal |
Eli Friedman | 42fbd62 | 2011-10-31 22:54:30 +0000 | [diff] [blame] | 5716 | // to a null pointer constant. |
Eli Friedman | c6be94b | 2011-10-31 22:28:05 +0000 | [diff] [blame] | 5717 | if ((!LHSValue.Base && !LHSValue.Offset.isZero()) || |
| 5718 | (!RHSValue.Base && !RHSValue.Offset.isZero())) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5719 | return Error(E); |
Richard Smith | 83c6821 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 5720 | // It's implementation-defined whether distinct literals will have |
Richard Smith | 7bb0067 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 5721 | // distinct addresses. In clang, the result of such a comparison is |
| 5722 | // unspecified, so it is not a constant expression. However, we do know |
| 5723 | // that the address of a literal will be non-null. |
Richard Smith | e9e20dd3 | 2011-11-04 01:10:57 +0000 | [diff] [blame] | 5724 | if ((IsLiteralLValue(LHSValue) || IsLiteralLValue(RHSValue)) && |
| 5725 | LHSValue.Base && RHSValue.Base) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5726 | return Error(E); |
Richard Smith | 83c6821 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 5727 | // We can't tell whether weak symbols will end up pointing to the same |
| 5728 | // object. |
| 5729 | if (IsWeakLValue(LHSValue) || IsWeakLValue(RHSValue)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5730 | return Error(E); |
Richard Smith | 83c6821 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 5731 | // Pointers with different bases cannot represent the same object. |
Eli Friedman | 42fbd62 | 2011-10-31 22:54:30 +0000 | [diff] [blame] | 5732 | // (Note that clang defaults to -fmerge-all-constants, which can |
| 5733 | // lead to inconsistent results for comparisons involving the address |
| 5734 | // of a constant; this generally doesn't matter in practice.) |
Richard Smith | 83c6821 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 5735 | return Success(E->getOpcode() == BO_NE, E); |
Eli Friedman | 334046a | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 5736 | } |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 5737 | |
Richard Smith | 1b47041 | 2012-02-01 08:10:20 +0000 | [diff] [blame] | 5738 | const CharUnits &LHSOffset = LHSValue.getLValueOffset(); |
| 5739 | const CharUnits &RHSOffset = RHSValue.getLValueOffset(); |
| 5740 | |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 5741 | SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator(); |
| 5742 | SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator(); |
| 5743 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5744 | if (E->getOpcode() == BO_Sub) { |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 5745 | // C++11 [expr.add]p6: |
| 5746 | // Unless both pointers point to elements of the same array object, or |
| 5747 | // one past the last element of the array object, the behavior is |
| 5748 | // undefined. |
| 5749 | if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && |
| 5750 | !AreElementsOfSameArray(getType(LHSValue.Base), |
| 5751 | LHSDesignator, RHSDesignator)) |
| 5752 | CCEDiag(E, diag::note_constexpr_pointer_subtraction_not_same_array); |
| 5753 | |
Chris Lattner | 882bdf2 | 2010-04-20 17:13:14 +0000 | [diff] [blame] | 5754 | QualType Type = E->getLHS()->getType(); |
| 5755 | QualType ElementType = Type->getAs<PointerType>()->getPointeeType(); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 5756 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5757 | CharUnits ElementSize; |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 5758 | if (!HandleSizeof(Info, E->getExprLoc(), ElementType, ElementSize)) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5759 | return false; |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 5760 | |
Richard Smith | 1b47041 | 2012-02-01 08:10:20 +0000 | [diff] [blame] | 5761 | // FIXME: LLVM and GCC both compute LHSOffset - RHSOffset at runtime, |
| 5762 | // and produce incorrect results when it overflows. Such behavior |
| 5763 | // appears to be non-conforming, but is common, so perhaps we should |
| 5764 | // assume the standard intended for such cases to be undefined behavior |
| 5765 | // and check for them. |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 5766 | |
Richard Smith | 1b47041 | 2012-02-01 08:10:20 +0000 | [diff] [blame] | 5767 | // Compute (LHSOffset - RHSOffset) / Size carefully, checking for |
| 5768 | // overflow in the final conversion to ptrdiff_t. |
| 5769 | APSInt LHS( |
| 5770 | llvm::APInt(65, (int64_t)LHSOffset.getQuantity(), true), false); |
| 5771 | APSInt RHS( |
| 5772 | llvm::APInt(65, (int64_t)RHSOffset.getQuantity(), true), false); |
| 5773 | APSInt ElemSize( |
| 5774 | llvm::APInt(65, (int64_t)ElementSize.getQuantity(), true), false); |
| 5775 | APSInt TrueResult = (LHS - RHS) / ElemSize; |
| 5776 | APSInt Result = TrueResult.trunc(Info.Ctx.getIntWidth(E->getType())); |
| 5777 | |
| 5778 | if (Result.extend(65) != TrueResult) |
| 5779 | HandleOverflow(Info, E, TrueResult, E->getType()); |
| 5780 | return Success(Result, E); |
| 5781 | } |
Richard Smith | de21b24 | 2012-01-31 06:41:30 +0000 | [diff] [blame] | 5782 | |
| 5783 | // C++11 [expr.rel]p3: |
| 5784 | // Pointers to void (after pointer conversions) can be compared, with a |
| 5785 | // result defined as follows: If both pointers represent the same |
| 5786 | // address or are both the null pointer value, the result is true if the |
| 5787 | // operator is <= or >= and false otherwise; otherwise the result is |
| 5788 | // unspecified. |
| 5789 | // We interpret this as applying to pointers to *cv* void. |
| 5790 | if (LHSTy->isVoidPointerType() && LHSOffset != RHSOffset && |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 5791 | E->isRelationalOp()) |
Richard Smith | de21b24 | 2012-01-31 06:41:30 +0000 | [diff] [blame] | 5792 | CCEDiag(E, diag::note_constexpr_void_comparison); |
| 5793 | |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 5794 | // C++11 [expr.rel]p2: |
| 5795 | // - If two pointers point to non-static data members of the same object, |
| 5796 | // or to subobjects or array elements fo such members, recursively, the |
| 5797 | // pointer to the later declared member compares greater provided the |
| 5798 | // two members have the same access control and provided their class is |
| 5799 | // not a union. |
| 5800 | // [...] |
| 5801 | // - Otherwise pointer comparisons are unspecified. |
| 5802 | if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && |
| 5803 | E->isRelationalOp()) { |
| 5804 | bool WasArrayIndex; |
| 5805 | unsigned Mismatch = |
| 5806 | FindDesignatorMismatch(getType(LHSValue.Base), LHSDesignator, |
| 5807 | RHSDesignator, WasArrayIndex); |
| 5808 | // At the point where the designators diverge, the comparison has a |
| 5809 | // specified value if: |
| 5810 | // - we are comparing array indices |
| 5811 | // - we are comparing fields of a union, or fields with the same access |
| 5812 | // Otherwise, the result is unspecified and thus the comparison is not a |
| 5813 | // constant expression. |
| 5814 | if (!WasArrayIndex && Mismatch < LHSDesignator.Entries.size() && |
| 5815 | Mismatch < RHSDesignator.Entries.size()) { |
| 5816 | const FieldDecl *LF = getAsField(LHSDesignator.Entries[Mismatch]); |
| 5817 | const FieldDecl *RF = getAsField(RHSDesignator.Entries[Mismatch]); |
| 5818 | if (!LF && !RF) |
| 5819 | CCEDiag(E, diag::note_constexpr_pointer_comparison_base_classes); |
| 5820 | else if (!LF) |
| 5821 | CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) |
| 5822 | << getAsBaseClass(LHSDesignator.Entries[Mismatch]) |
| 5823 | << RF->getParent() << RF; |
| 5824 | else if (!RF) |
| 5825 | CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) |
| 5826 | << getAsBaseClass(RHSDesignator.Entries[Mismatch]) |
| 5827 | << LF->getParent() << LF; |
| 5828 | else if (!LF->getParent()->isUnion() && |
| 5829 | LF->getAccess() != RF->getAccess()) |
| 5830 | CCEDiag(E, diag::note_constexpr_pointer_comparison_differing_access) |
| 5831 | << LF << LF->getAccess() << RF << RF->getAccess() |
| 5832 | << LF->getParent(); |
| 5833 | } |
| 5834 | } |
| 5835 | |
Eli Friedman | 6c31cb4 | 2012-04-16 04:30:08 +0000 | [diff] [blame] | 5836 | // The comparison here must be unsigned, and performed with the same |
| 5837 | // width as the pointer. |
Eli Friedman | 6c31cb4 | 2012-04-16 04:30:08 +0000 | [diff] [blame] | 5838 | unsigned PtrSize = Info.Ctx.getTypeSize(LHSTy); |
| 5839 | uint64_t CompareLHS = LHSOffset.getQuantity(); |
| 5840 | uint64_t CompareRHS = RHSOffset.getQuantity(); |
| 5841 | assert(PtrSize <= 64 && "Unexpected pointer width"); |
| 5842 | uint64_t Mask = ~0ULL >> (64 - PtrSize); |
| 5843 | CompareLHS &= Mask; |
| 5844 | CompareRHS &= Mask; |
| 5845 | |
Eli Friedman | 2f5b7c5 | 2012-04-16 19:23:57 +0000 | [diff] [blame] | 5846 | // If there is a base and this is a relational operator, we can only |
| 5847 | // compare pointers within the object in question; otherwise, the result |
| 5848 | // depends on where the object is located in memory. |
| 5849 | if (!LHSValue.Base.isNull() && E->isRelationalOp()) { |
| 5850 | QualType BaseTy = getType(LHSValue.Base); |
| 5851 | if (BaseTy->isIncompleteType()) |
| 5852 | return Error(E); |
| 5853 | CharUnits Size = Info.Ctx.getTypeSizeInChars(BaseTy); |
| 5854 | uint64_t OffsetLimit = Size.getQuantity(); |
| 5855 | if (CompareLHS > OffsetLimit || CompareRHS > OffsetLimit) |
| 5856 | return Error(E); |
| 5857 | } |
| 5858 | |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 5859 | switch (E->getOpcode()) { |
| 5860 | default: llvm_unreachable("missing comparison operator"); |
Eli Friedman | 6c31cb4 | 2012-04-16 04:30:08 +0000 | [diff] [blame] | 5861 | case BO_LT: return Success(CompareLHS < CompareRHS, E); |
| 5862 | case BO_GT: return Success(CompareLHS > CompareRHS, E); |
| 5863 | case BO_LE: return Success(CompareLHS <= CompareRHS, E); |
| 5864 | case BO_GE: return Success(CompareLHS >= CompareRHS, E); |
| 5865 | case BO_EQ: return Success(CompareLHS == CompareRHS, E); |
| 5866 | case BO_NE: return Success(CompareLHS != CompareRHS, E); |
Eli Friedman | a38da57 | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 5867 | } |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 5868 | } |
| 5869 | } |
Richard Smith | 7bb0067 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 5870 | |
| 5871 | if (LHSTy->isMemberPointerType()) { |
| 5872 | assert(E->isEqualityOp() && "unexpected member pointer operation"); |
| 5873 | assert(RHSTy->isMemberPointerType() && "invalid comparison"); |
| 5874 | |
| 5875 | MemberPtr LHSValue, RHSValue; |
| 5876 | |
| 5877 | bool LHSOK = EvaluateMemberPointer(E->getLHS(), LHSValue, Info); |
| 5878 | if (!LHSOK && Info.keepEvaluatingAfterFailure()) |
| 5879 | return false; |
| 5880 | |
| 5881 | if (!EvaluateMemberPointer(E->getRHS(), RHSValue, Info) || !LHSOK) |
| 5882 | return false; |
| 5883 | |
| 5884 | // C++11 [expr.eq]p2: |
| 5885 | // If both operands are null, they compare equal. Otherwise if only one is |
| 5886 | // null, they compare unequal. |
| 5887 | if (!LHSValue.getDecl() || !RHSValue.getDecl()) { |
| 5888 | bool Equal = !LHSValue.getDecl() && !RHSValue.getDecl(); |
| 5889 | return Success(E->getOpcode() == BO_EQ ? Equal : !Equal, E); |
| 5890 | } |
| 5891 | |
| 5892 | // Otherwise if either is a pointer to a virtual member function, the |
| 5893 | // result is unspecified. |
| 5894 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(LHSValue.getDecl())) |
| 5895 | if (MD->isVirtual()) |
| 5896 | CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; |
| 5897 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(RHSValue.getDecl())) |
| 5898 | if (MD->isVirtual()) |
| 5899 | CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; |
| 5900 | |
| 5901 | // Otherwise they compare equal if and only if they would refer to the |
| 5902 | // same member of the same most derived object or the same subobject if |
| 5903 | // they were dereferenced with a hypothetical object of the associated |
| 5904 | // class type. |
| 5905 | bool Equal = LHSValue == RHSValue; |
| 5906 | return Success(E->getOpcode() == BO_EQ ? Equal : !Equal, E); |
| 5907 | } |
| 5908 | |
Richard Smith | ab44d9b | 2012-02-14 22:35:28 +0000 | [diff] [blame] | 5909 | if (LHSTy->isNullPtrType()) { |
| 5910 | assert(E->isComparisonOp() && "unexpected nullptr operation"); |
| 5911 | assert(RHSTy->isNullPtrType() && "missing pointer conversion"); |
| 5912 | // C++11 [expr.rel]p4, [expr.eq]p3: If two operands of type std::nullptr_t |
| 5913 | // are compared, the result is true of the operator is <=, >= or ==, and |
| 5914 | // false otherwise. |
| 5915 | BinaryOperator::Opcode Opcode = E->getOpcode(); |
| 5916 | return Success(Opcode == BO_EQ || Opcode == BO_LE || Opcode == BO_GE, E); |
| 5917 | } |
| 5918 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5919 | assert((!LHSTy->isIntegralOrEnumerationType() || |
| 5920 | !RHSTy->isIntegralOrEnumerationType()) && |
| 5921 | "DataRecursiveIntBinOpEvaluator should have handled integral types"); |
| 5922 | // We can't continue from here for non-integral types. |
| 5923 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 5924 | } |
| 5925 | |
Ken Dyck | 160146e | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 5926 | CharUnits IntExprEvaluator::GetAlignOfType(QualType T) { |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 5927 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 5928 | // result shall be the alignment of the referenced type." |
| 5929 | if (const ReferenceType *Ref = T->getAs<ReferenceType>()) |
| 5930 | T = Ref->getPointeeType(); |
Chad Rosier | 99ee782 | 2011-07-26 07:03:04 +0000 | [diff] [blame] | 5931 | |
| 5932 | // __alignof is defined to return the preferred alignment. |
| 5933 | return Info.Ctx.toCharUnitsFromBits( |
| 5934 | Info.Ctx.getPreferredTypeAlign(T.getTypePtr())); |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 5935 | } |
| 5936 | |
Ken Dyck | 160146e | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 5937 | CharUnits IntExprEvaluator::GetAlignOfExpr(const Expr *E) { |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 5938 | E = E->IgnoreParens(); |
| 5939 | |
John McCall | 768439e | 2013-05-06 07:40:34 +0000 | [diff] [blame] | 5940 | // The kinds of expressions that we have special-case logic here for |
| 5941 | // should be kept up to date with the special checks for those |
| 5942 | // expressions in Sema. |
| 5943 | |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 5944 | // alignof decl is always accepted, even if it doesn't make sense: we default |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5945 | // to 1 in those cases. |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 5946 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
Ken Dyck | 160146e | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 5947 | return Info.Ctx.getDeclAlign(DRE->getDecl(), |
| 5948 | /*RefAsPointee*/true); |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 5949 | |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 5950 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Ken Dyck | 160146e | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 5951 | return Info.Ctx.getDeclAlign(ME->getMemberDecl(), |
| 5952 | /*RefAsPointee*/true); |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 5953 | |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 5954 | return GetAlignOfType(E->getType()); |
| 5955 | } |
| 5956 | |
| 5957 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5958 | /// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with |
| 5959 | /// a result as the expression's type. |
| 5960 | bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr( |
| 5961 | const UnaryExprOrTypeTraitExpr *E) { |
| 5962 | switch(E->getKind()) { |
| 5963 | case UETT_AlignOf: { |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 5964 | if (E->isArgumentType()) |
Ken Dyck | dbc0191 | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 5965 | return Success(GetAlignOfType(E->getArgumentType()), E); |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 5966 | else |
Ken Dyck | dbc0191 | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 5967 | return Success(GetAlignOfExpr(E->getArgumentExpr()), E); |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 5968 | } |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 5969 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5970 | case UETT_VecStep: { |
| 5971 | QualType Ty = E->getTypeOfArgument(); |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 5972 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5973 | if (Ty->isVectorType()) { |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 5974 | unsigned n = Ty->castAs<VectorType>()->getNumElements(); |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 5975 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5976 | // The vec_step built-in functions that take a 3-component |
| 5977 | // vector return 4. (OpenCL 1.1 spec 6.11.12) |
| 5978 | if (n == 3) |
| 5979 | n = 4; |
Eli Friedman | 2aa38fe | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 5980 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5981 | return Success(n, E); |
| 5982 | } else |
| 5983 | return Success(1, E); |
| 5984 | } |
| 5985 | |
| 5986 | case UETT_SizeOf: { |
| 5987 | QualType SrcTy = E->getTypeOfArgument(); |
| 5988 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 5989 | // the result is the size of the referenced type." |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5990 | if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>()) |
| 5991 | SrcTy = Ref->getPointeeType(); |
| 5992 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5993 | CharUnits Sizeof; |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 5994 | if (!HandleSizeof(Info, E->getExprLoc(), SrcTy, Sizeof)) |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5995 | return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5996 | return Success(Sizeof, E); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5997 | } |
| 5998 | } |
| 5999 | |
| 6000 | llvm_unreachable("unknown expr/type trait"); |
Chris Lattner | f8d7f72 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 6001 | } |
| 6002 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6003 | bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) { |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6004 | CharUnits Result; |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6005 | unsigned n = OOE->getNumComponents(); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6006 | if (n == 0) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6007 | return Error(OOE); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6008 | QualType CurrentType = OOE->getTypeSourceInfo()->getType(); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6009 | for (unsigned i = 0; i != n; ++i) { |
| 6010 | OffsetOfExpr::OffsetOfNode ON = OOE->getComponent(i); |
| 6011 | switch (ON.getKind()) { |
| 6012 | case OffsetOfExpr::OffsetOfNode::Array: { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6013 | const Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex()); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6014 | APSInt IdxResult; |
| 6015 | if (!EvaluateInteger(Idx, IdxResult, Info)) |
| 6016 | return false; |
| 6017 | const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType); |
| 6018 | if (!AT) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6019 | return Error(OOE); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6020 | CurrentType = AT->getElementType(); |
| 6021 | CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType); |
| 6022 | Result += IdxResult.getSExtValue() * ElementSize; |
| 6023 | break; |
| 6024 | } |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6025 | |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6026 | case OffsetOfExpr::OffsetOfNode::Field: { |
| 6027 | FieldDecl *MemberDecl = ON.getField(); |
| 6028 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6029 | if (!RT) |
| 6030 | return Error(OOE); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6031 | RecordDecl *RD = RT->getDecl(); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 6032 | if (RD->isInvalidDecl()) return false; |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6033 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 6034 | unsigned i = MemberDecl->getFieldIndex(); |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 6035 | assert(i < RL.getFieldCount() && "offsetof field in wrong type"); |
Ken Dyck | 86a7fcc | 2011-01-18 01:56:16 +0000 | [diff] [blame] | 6036 | Result += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i)); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6037 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 6038 | break; |
| 6039 | } |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6040 | |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6041 | case OffsetOfExpr::OffsetOfNode::Identifier: |
| 6042 | llvm_unreachable("dependent __builtin_offsetof"); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6043 | |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 6044 | case OffsetOfExpr::OffsetOfNode::Base: { |
| 6045 | CXXBaseSpecifier *BaseSpec = ON.getBase(); |
| 6046 | if (BaseSpec->isVirtual()) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6047 | return Error(OOE); |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 6048 | |
| 6049 | // Find the layout of the class whose base we are looking into. |
| 6050 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6051 | if (!RT) |
| 6052 | return Error(OOE); |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 6053 | RecordDecl *RD = RT->getDecl(); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 6054 | if (RD->isInvalidDecl()) return false; |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 6055 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
| 6056 | |
| 6057 | // Find the base class itself. |
| 6058 | CurrentType = BaseSpec->getType(); |
| 6059 | const RecordType *BaseRT = CurrentType->getAs<RecordType>(); |
| 6060 | if (!BaseRT) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6061 | return Error(OOE); |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 6062 | |
| 6063 | // Add the offset to the base. |
Ken Dyck | 02155cb | 2011-01-26 02:17:08 +0000 | [diff] [blame] | 6064 | Result += RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl())); |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 6065 | break; |
| 6066 | } |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6067 | } |
| 6068 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6069 | return Success(Result, OOE); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6070 | } |
| 6071 | |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 6072 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6073 | switch (E->getOpcode()) { |
| 6074 | default: |
| 6075 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 6076 | // See C99 6.6p3. |
| 6077 | return Error(E); |
| 6078 | case UO_Extension: |
| 6079 | // FIXME: Should extension allow i-c-e extension expressions in its scope? |
| 6080 | // If so, we could clear the diagnostic ID. |
| 6081 | return Visit(E->getSubExpr()); |
| 6082 | case UO_Plus: |
| 6083 | // The result is just the value. |
| 6084 | return Visit(E->getSubExpr()); |
| 6085 | case UO_Minus: { |
| 6086 | if (!Visit(E->getSubExpr())) |
| 6087 | return false; |
| 6088 | if (!Result.isInt()) return Error(E); |
Richard Smith | fe80003 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 6089 | const APSInt &Value = Result.getInt(); |
| 6090 | if (Value.isSigned() && Value.isMinSignedValue()) |
| 6091 | HandleOverflow(Info, E, -Value.extend(Value.getBitWidth() + 1), |
| 6092 | E->getType()); |
| 6093 | return Success(-Value, E); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6094 | } |
| 6095 | case UO_Not: { |
| 6096 | if (!Visit(E->getSubExpr())) |
| 6097 | return false; |
| 6098 | if (!Result.isInt()) return Error(E); |
| 6099 | return Success(~Result.getInt(), E); |
| 6100 | } |
| 6101 | case UO_LNot: { |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 6102 | bool bres; |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6103 | if (!EvaluateAsBooleanCondition(E->getSubExpr(), bres, Info)) |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 6104 | return false; |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 6105 | return Success(!bres, E); |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 6106 | } |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 6107 | } |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 6108 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6109 | |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 6110 | /// HandleCast - This is used to evaluate implicit or explicit casts where the |
| 6111 | /// result type is integer. |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6112 | bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 6113 | const Expr *SubExpr = E->getSubExpr(); |
Anders Carlsson | 27b8c5c | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 6114 | QualType DestType = E->getType(); |
Daniel Dunbar | cf04aa1 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 6115 | QualType SrcType = SubExpr->getType(); |
Anders Carlsson | 27b8c5c | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 6116 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6117 | switch (E->getCastKind()) { |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6118 | case CK_BaseToDerived: |
| 6119 | case CK_DerivedToBase: |
| 6120 | case CK_UncheckedDerivedToBase: |
| 6121 | case CK_Dynamic: |
| 6122 | case CK_ToUnion: |
| 6123 | case CK_ArrayToPointerDecay: |
| 6124 | case CK_FunctionToPointerDecay: |
| 6125 | case CK_NullToPointer: |
| 6126 | case CK_NullToMemberPointer: |
| 6127 | case CK_BaseToDerivedMemberPointer: |
| 6128 | case CK_DerivedToBaseMemberPointer: |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 6129 | case CK_ReinterpretMemberPointer: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6130 | case CK_ConstructorConversion: |
| 6131 | case CK_IntegralToPointer: |
| 6132 | case CK_ToVoid: |
| 6133 | case CK_VectorSplat: |
| 6134 | case CK_IntegralToFloating: |
| 6135 | case CK_FloatingCast: |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 6136 | case CK_CPointerToObjCPointerCast: |
| 6137 | case CK_BlockPointerToObjCPointerCast: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6138 | case CK_AnyPointerToBlockPointerCast: |
| 6139 | case CK_ObjCObjectLValueCast: |
| 6140 | case CK_FloatingRealToComplex: |
| 6141 | case CK_FloatingComplexToReal: |
| 6142 | case CK_FloatingComplexCast: |
| 6143 | case CK_FloatingComplexToIntegralComplex: |
| 6144 | case CK_IntegralRealToComplex: |
| 6145 | case CK_IntegralComplexCast: |
| 6146 | case CK_IntegralComplexToFloatingComplex: |
Eli Friedman | 34866c7 | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 6147 | case CK_BuiltinFnToFnPtr: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6148 | case CK_ZeroToOCLEvent: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6149 | llvm_unreachable("invalid cast kind for integral value"); |
| 6150 | |
Eli Friedman | 9faf2f9 | 2011-03-25 19:07:11 +0000 | [diff] [blame] | 6151 | case CK_BitCast: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6152 | case CK_Dependent: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6153 | case CK_LValueBitCast: |
John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 6154 | case CK_ARCProduceObject: |
| 6155 | case CK_ARCConsumeObject: |
| 6156 | case CK_ARCReclaimReturnedObject: |
| 6157 | case CK_ARCExtendBlockObject: |
Douglas Gregor | ed90df3 | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 6158 | case CK_CopyAndAutoreleaseBlockObject: |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6159 | return Error(E); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6160 | |
Richard Smith | 4ef685b | 2012-01-17 21:17:26 +0000 | [diff] [blame] | 6161 | case CK_UserDefinedConversion: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6162 | case CK_LValueToRValue: |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 6163 | case CK_AtomicToNonAtomic: |
| 6164 | case CK_NonAtomicToAtomic: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6165 | case CK_NoOp: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6166 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6167 | |
| 6168 | case CK_MemberPointerToBoolean: |
| 6169 | case CK_PointerToBoolean: |
| 6170 | case CK_IntegralToBoolean: |
| 6171 | case CK_FloatingToBoolean: |
| 6172 | case CK_FloatingComplexToBoolean: |
| 6173 | case CK_IntegralComplexToBoolean: { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 6174 | bool BoolResult; |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6175 | if (!EvaluateAsBooleanCondition(SubExpr, BoolResult, Info)) |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 6176 | return false; |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 6177 | return Success(BoolResult, E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 6178 | } |
| 6179 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6180 | case CK_IntegralCast: { |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 6181 | if (!Visit(SubExpr)) |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 6182 | return false; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 6183 | |
Eli Friedman | 742421e | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 6184 | if (!Result.isInt()) { |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 6185 | // Allow casts of address-of-label differences if they are no-ops |
| 6186 | // or narrowing. (The narrowing case isn't actually guaranteed to |
| 6187 | // be constant-evaluatable except in some narrow cases which are hard |
| 6188 | // to detect here. We let it through on the assumption the user knows |
| 6189 | // what they are doing.) |
| 6190 | if (Result.isAddrLabelDiff()) |
| 6191 | return Info.Ctx.getTypeSize(DestType) <= Info.Ctx.getTypeSize(SrcType); |
Eli Friedman | 742421e | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 6192 | // Only allow casts of lvalues if they are lossless. |
| 6193 | return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType); |
| 6194 | } |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 6195 | |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 6196 | return Success(HandleIntToIntCast(Info, E, DestType, SrcType, |
| 6197 | Result.getInt()), E); |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 6198 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6199 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6200 | case CK_PointerToIntegral: { |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 6201 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 6202 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 6203 | LValue LV; |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 6204 | if (!EvaluatePointer(SubExpr, LV, Info)) |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 6205 | return false; |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 6206 | |
Daniel Dunbar | 1c8560d | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 6207 | if (LV.getLValueBase()) { |
| 6208 | // Only allow based lvalue casts if they are lossless. |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 6209 | // FIXME: Allow a larger integer size than the pointer size, and allow |
| 6210 | // narrowing back down to pointer width in subsequent integral casts. |
| 6211 | // FIXME: Check integer type's active bits, not its type size. |
Daniel Dunbar | 1c8560d | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 6212 | if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6213 | return Error(E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 6214 | |
Richard Smith | cf74da7 | 2011-11-16 07:18:12 +0000 | [diff] [blame] | 6215 | LV.Designator.setInvalid(); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 6216 | LV.moveInto(Result); |
Daniel Dunbar | 1c8560d | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 6217 | return true; |
| 6218 | } |
| 6219 | |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 6220 | APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(), |
| 6221 | SrcType); |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 6222 | return Success(HandleIntToIntCast(Info, E, DestType, SrcType, AsInt), E); |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 6223 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 6224 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6225 | case CK_IntegralComplexToReal: { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 6226 | ComplexValue C; |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 6227 | if (!EvaluateComplex(SubExpr, C, Info)) |
| 6228 | return false; |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6229 | return Success(C.getComplexIntReal(), E); |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 6230 | } |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 6231 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6232 | case CK_FloatingToIntegral: { |
| 6233 | APFloat F(0.0); |
| 6234 | if (!EvaluateFloat(SubExpr, F, Info)) |
| 6235 | return false; |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 6236 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 6237 | APSInt Value; |
| 6238 | if (!HandleFloatToIntCast(Info, E, SrcType, F, DestType, Value)) |
| 6239 | return false; |
| 6240 | return Success(Value, E); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6241 | } |
| 6242 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6243 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6244 | llvm_unreachable("unknown cast resulting in integral value"); |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 6245 | } |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 6246 | |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 6247 | bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
| 6248 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 6249 | ComplexValue LV; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6250 | if (!EvaluateComplex(E->getSubExpr(), LV, Info)) |
| 6251 | return false; |
| 6252 | if (!LV.isComplexInt()) |
| 6253 | return Error(E); |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 6254 | return Success(LV.getComplexIntReal(), E); |
| 6255 | } |
| 6256 | |
| 6257 | return Visit(E->getSubExpr()); |
| 6258 | } |
| 6259 | |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 6260 | bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 6261 | if (E->getSubExpr()->getType()->isComplexIntegerType()) { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 6262 | ComplexValue LV; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6263 | if (!EvaluateComplex(E->getSubExpr(), LV, Info)) |
| 6264 | return false; |
| 6265 | if (!LV.isComplexInt()) |
| 6266 | return Error(E); |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 6267 | return Success(LV.getComplexIntImag(), E); |
| 6268 | } |
| 6269 | |
Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 6270 | VisitIgnoredValue(E->getSubExpr()); |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 6271 | return Success(0, E); |
| 6272 | } |
| 6273 | |
Douglas Gregor | 820ba7b | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 6274 | bool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) { |
| 6275 | return Success(E->getPackLength(), E); |
| 6276 | } |
| 6277 | |
Sebastian Redl | 5f0180d | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 6278 | bool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) { |
| 6279 | return Success(E->getValue(), E); |
| 6280 | } |
| 6281 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 6282 | //===----------------------------------------------------------------------===// |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 6283 | // Float Evaluation |
| 6284 | //===----------------------------------------------------------------------===// |
| 6285 | |
| 6286 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 6287 | class FloatExprEvaluator |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6288 | : public ExprEvaluatorBase<FloatExprEvaluator, bool> { |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 6289 | APFloat &Result; |
| 6290 | public: |
| 6291 | FloatExprEvaluator(EvalInfo &info, APFloat &result) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6292 | : ExprEvaluatorBaseTy(info), Result(result) {} |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 6293 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6294 | bool Success(const APValue &V, const Expr *e) { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6295 | Result = V.getFloat(); |
| 6296 | return true; |
| 6297 | } |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 6298 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6299 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 6300 | Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType())); |
| 6301 | return true; |
| 6302 | } |
| 6303 | |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 6304 | bool VisitCallExpr(const CallExpr *E); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 6305 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 6306 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 6307 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 6308 | bool VisitFloatingLiteral(const FloatingLiteral *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6309 | bool VisitCastExpr(const CastExpr *E); |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 6310 | |
John McCall | b1fb0d3 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 6311 | bool VisitUnaryReal(const UnaryOperator *E); |
| 6312 | bool VisitUnaryImag(const UnaryOperator *E); |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 6313 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6314 | // FIXME: Missing: array subscript of vector, member of vector |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 6315 | }; |
| 6316 | } // end anonymous namespace |
| 6317 | |
| 6318 | static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6319 | assert(E->isRValue() && E->getType()->isRealFloatingType()); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6320 | return FloatExprEvaluator(Info, Result).Visit(E); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 6321 | } |
| 6322 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6323 | static bool TryEvaluateBuiltinNaN(const ASTContext &Context, |
John McCall | 1629149 | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 6324 | QualType ResultTy, |
| 6325 | const Expr *Arg, |
| 6326 | bool SNaN, |
| 6327 | llvm::APFloat &Result) { |
| 6328 | const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts()); |
| 6329 | if (!S) return false; |
| 6330 | |
| 6331 | const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy); |
| 6332 | |
| 6333 | llvm::APInt fill; |
| 6334 | |
| 6335 | // Treat empty strings as if they were zero. |
| 6336 | if (S->getString().empty()) |
| 6337 | fill = llvm::APInt(32, 0); |
| 6338 | else if (S->getString().getAsInteger(0, fill)) |
| 6339 | return false; |
| 6340 | |
| 6341 | if (SNaN) |
| 6342 | Result = llvm::APFloat::getSNaN(Sem, false, &fill); |
| 6343 | else |
| 6344 | Result = llvm::APFloat::getQNaN(Sem, false, &fill); |
| 6345 | return true; |
| 6346 | } |
| 6347 | |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 6348 | bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6349 | switch (E->isBuiltinCall()) { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6350 | default: |
| 6351 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
| 6352 | |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 6353 | case Builtin::BI__builtin_huge_val: |
| 6354 | case Builtin::BI__builtin_huge_valf: |
| 6355 | case Builtin::BI__builtin_huge_vall: |
| 6356 | case Builtin::BI__builtin_inf: |
| 6357 | case Builtin::BI__builtin_inff: |
Daniel Dunbar | 1be9f88 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 6358 | case Builtin::BI__builtin_infl: { |
| 6359 | const llvm::fltSemantics &Sem = |
| 6360 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Chris Lattner | 37346e0 | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 6361 | Result = llvm::APFloat::getInf(Sem); |
| 6362 | return true; |
Daniel Dunbar | 1be9f88 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 6363 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6364 | |
John McCall | 1629149 | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 6365 | case Builtin::BI__builtin_nans: |
| 6366 | case Builtin::BI__builtin_nansf: |
| 6367 | case Builtin::BI__builtin_nansl: |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6368 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 6369 | true, Result)) |
| 6370 | return Error(E); |
| 6371 | return true; |
John McCall | 1629149 | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 6372 | |
Chris Lattner | 0b7282e | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 6373 | case Builtin::BI__builtin_nan: |
| 6374 | case Builtin::BI__builtin_nanf: |
| 6375 | case Builtin::BI__builtin_nanl: |
Mike Stump | 2346cd2 | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 6376 | // If this is __builtin_nan() turn this into a nan, otherwise we |
Chris Lattner | 0b7282e | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 6377 | // can't constant fold it. |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6378 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 6379 | false, Result)) |
| 6380 | return Error(E); |
| 6381 | return true; |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 6382 | |
| 6383 | case Builtin::BI__builtin_fabs: |
| 6384 | case Builtin::BI__builtin_fabsf: |
| 6385 | case Builtin::BI__builtin_fabsl: |
| 6386 | if (!EvaluateFloat(E->getArg(0), Result, Info)) |
| 6387 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6388 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 6389 | if (Result.isNegative()) |
| 6390 | Result.changeSign(); |
| 6391 | return true; |
| 6392 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6393 | case Builtin::BI__builtin_copysign: |
| 6394 | case Builtin::BI__builtin_copysignf: |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 6395 | case Builtin::BI__builtin_copysignl: { |
| 6396 | APFloat RHS(0.); |
| 6397 | if (!EvaluateFloat(E->getArg(0), Result, Info) || |
| 6398 | !EvaluateFloat(E->getArg(1), RHS, Info)) |
| 6399 | return false; |
| 6400 | Result.copySign(RHS); |
| 6401 | return true; |
| 6402 | } |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 6403 | } |
| 6404 | } |
| 6405 | |
John McCall | b1fb0d3 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 6406 | bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
Eli Friedman | 9571953 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 6407 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 6408 | ComplexValue CV; |
| 6409 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
| 6410 | return false; |
| 6411 | Result = CV.FloatReal; |
| 6412 | return true; |
| 6413 | } |
| 6414 | |
| 6415 | return Visit(E->getSubExpr()); |
John McCall | b1fb0d3 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 6416 | } |
| 6417 | |
| 6418 | bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | 9571953 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 6419 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 6420 | ComplexValue CV; |
| 6421 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
| 6422 | return false; |
| 6423 | Result = CV.FloatImag; |
| 6424 | return true; |
| 6425 | } |
| 6426 | |
Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 6427 | VisitIgnoredValue(E->getSubExpr()); |
Eli Friedman | 9571953 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 6428 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(E->getType()); |
| 6429 | Result = llvm::APFloat::getZero(Sem); |
John McCall | b1fb0d3 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 6430 | return true; |
| 6431 | } |
| 6432 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 6433 | bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 6434 | switch (E->getOpcode()) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6435 | default: return Error(E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6436 | case UO_Plus: |
Richard Smith | 390cd49 | 2011-10-30 23:17:09 +0000 | [diff] [blame] | 6437 | return EvaluateFloat(E->getSubExpr(), Result, Info); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6438 | case UO_Minus: |
Richard Smith | 390cd49 | 2011-10-30 23:17:09 +0000 | [diff] [blame] | 6439 | if (!EvaluateFloat(E->getSubExpr(), Result, Info)) |
| 6440 | return false; |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 6441 | Result.changeSign(); |
| 6442 | return true; |
| 6443 | } |
| 6444 | } |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 6445 | |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 6446 | bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6447 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
| 6448 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Eli Friedman | 141fbf3 | 2009-11-16 04:25:37 +0000 | [diff] [blame] | 6449 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 6450 | APFloat RHS(0.0); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6451 | bool LHSOK = EvaluateFloat(E->getLHS(), Result, Info); |
| 6452 | if (!LHSOK && !Info.keepEvaluatingAfterFailure()) |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 6453 | return false; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6454 | if (!EvaluateFloat(E->getRHS(), RHS, Info) || !LHSOK) |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 6455 | return false; |
| 6456 | |
| 6457 | switch (E->getOpcode()) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6458 | default: return Error(E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6459 | case BO_Mul: |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 6460 | Result.multiply(RHS, APFloat::rmNearestTiesToEven); |
Richard Smith | c804232 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 6461 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6462 | case BO_Add: |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 6463 | Result.add(RHS, APFloat::rmNearestTiesToEven); |
Richard Smith | c804232 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 6464 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6465 | case BO_Sub: |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 6466 | Result.subtract(RHS, APFloat::rmNearestTiesToEven); |
Richard Smith | c804232 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 6467 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6468 | case BO_Div: |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 6469 | Result.divide(RHS, APFloat::rmNearestTiesToEven); |
Richard Smith | c804232 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 6470 | break; |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 6471 | } |
Richard Smith | c804232 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 6472 | |
| 6473 | if (Result.isInfinity() || Result.isNaN()) |
| 6474 | CCEDiag(E, diag::note_constexpr_float_arithmetic) << Result.isNaN(); |
| 6475 | return true; |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 6476 | } |
| 6477 | |
| 6478 | bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { |
| 6479 | Result = E->getValue(); |
| 6480 | return true; |
| 6481 | } |
| 6482 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6483 | bool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 6484 | const Expr* SubExpr = E->getSubExpr(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6485 | |
Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 6486 | switch (E->getCastKind()) { |
| 6487 | default: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6488 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 6489 | |
| 6490 | case CK_IntegralToFloating: { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 6491 | APSInt IntResult; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 6492 | return EvaluateInteger(SubExpr, IntResult, Info) && |
| 6493 | HandleIntToFloatCast(Info, E, SubExpr->getType(), IntResult, |
| 6494 | E->getType(), Result); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 6495 | } |
Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 6496 | |
| 6497 | case CK_FloatingCast: { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 6498 | if (!Visit(SubExpr)) |
| 6499 | return false; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 6500 | return HandleFloatToFloatCast(Info, E, SubExpr->getType(), E->getType(), |
| 6501 | Result); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 6502 | } |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 6503 | |
Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 6504 | case CK_FloatingComplexToReal: { |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 6505 | ComplexValue V; |
| 6506 | if (!EvaluateComplex(SubExpr, V, Info)) |
| 6507 | return false; |
| 6508 | Result = V.getComplexFloatReal(); |
| 6509 | return true; |
| 6510 | } |
Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 6511 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 6512 | } |
| 6513 | |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 6514 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 6515 | // Complex Evaluation (for float and integer) |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 6516 | //===----------------------------------------------------------------------===// |
| 6517 | |
| 6518 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 6519 | class ComplexExprEvaluator |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6520 | : public ExprEvaluatorBase<ComplexExprEvaluator, bool> { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 6521 | ComplexValue &Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6522 | |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 6523 | public: |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 6524 | ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6525 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
| 6526 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6527 | bool Success(const APValue &V, const Expr *e) { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6528 | Result.setFrom(V); |
| 6529 | return true; |
| 6530 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6531 | |
Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 6532 | bool ZeroInitialization(const Expr *E); |
| 6533 | |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 6534 | //===--------------------------------------------------------------------===// |
| 6535 | // Visitor Methods |
| 6536 | //===--------------------------------------------------------------------===// |
| 6537 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6538 | bool VisitImaginaryLiteral(const ImaginaryLiteral *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6539 | bool VisitCastExpr(const CastExpr *E); |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 6540 | bool VisitBinaryOperator(const BinaryOperator *E); |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 6541 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 6542 | bool VisitInitListExpr(const InitListExpr *E); |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 6543 | }; |
| 6544 | } // end anonymous namespace |
| 6545 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 6546 | static bool EvaluateComplex(const Expr *E, ComplexValue &Result, |
| 6547 | EvalInfo &Info) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6548 | assert(E->isRValue() && E->getType()->isAnyComplexType()); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6549 | return ComplexExprEvaluator(Info, Result).Visit(E); |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 6550 | } |
| 6551 | |
Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 6552 | bool ComplexExprEvaluator::ZeroInitialization(const Expr *E) { |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 6553 | QualType ElemTy = E->getType()->castAs<ComplexType>()->getElementType(); |
Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 6554 | if (ElemTy->isRealFloatingType()) { |
| 6555 | Result.makeComplexFloat(); |
| 6556 | APFloat Zero = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(ElemTy)); |
| 6557 | Result.FloatReal = Zero; |
| 6558 | Result.FloatImag = Zero; |
| 6559 | } else { |
| 6560 | Result.makeComplexInt(); |
| 6561 | APSInt Zero = Info.Ctx.MakeIntValue(0, ElemTy); |
| 6562 | Result.IntReal = Zero; |
| 6563 | Result.IntImag = Zero; |
| 6564 | } |
| 6565 | return true; |
| 6566 | } |
| 6567 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6568 | bool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) { |
| 6569 | const Expr* SubExpr = E->getSubExpr(); |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 6570 | |
| 6571 | if (SubExpr->getType()->isRealFloatingType()) { |
| 6572 | Result.makeComplexFloat(); |
| 6573 | APFloat &Imag = Result.FloatImag; |
| 6574 | if (!EvaluateFloat(SubExpr, Imag, Info)) |
| 6575 | return false; |
| 6576 | |
| 6577 | Result.FloatReal = APFloat(Imag.getSemantics()); |
| 6578 | return true; |
| 6579 | } else { |
| 6580 | assert(SubExpr->getType()->isIntegerType() && |
| 6581 | "Unexpected imaginary literal."); |
| 6582 | |
| 6583 | Result.makeComplexInt(); |
| 6584 | APSInt &Imag = Result.IntImag; |
| 6585 | if (!EvaluateInteger(SubExpr, Imag, Info)) |
| 6586 | return false; |
| 6587 | |
| 6588 | Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned()); |
| 6589 | return true; |
| 6590 | } |
| 6591 | } |
| 6592 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6593 | bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) { |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 6594 | |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 6595 | switch (E->getCastKind()) { |
| 6596 | case CK_BitCast: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 6597 | case CK_BaseToDerived: |
| 6598 | case CK_DerivedToBase: |
| 6599 | case CK_UncheckedDerivedToBase: |
| 6600 | case CK_Dynamic: |
| 6601 | case CK_ToUnion: |
| 6602 | case CK_ArrayToPointerDecay: |
| 6603 | case CK_FunctionToPointerDecay: |
| 6604 | case CK_NullToPointer: |
| 6605 | case CK_NullToMemberPointer: |
| 6606 | case CK_BaseToDerivedMemberPointer: |
| 6607 | case CK_DerivedToBaseMemberPointer: |
| 6608 | case CK_MemberPointerToBoolean: |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 6609 | case CK_ReinterpretMemberPointer: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 6610 | case CK_ConstructorConversion: |
| 6611 | case CK_IntegralToPointer: |
| 6612 | case CK_PointerToIntegral: |
| 6613 | case CK_PointerToBoolean: |
| 6614 | case CK_ToVoid: |
| 6615 | case CK_VectorSplat: |
| 6616 | case CK_IntegralCast: |
| 6617 | case CK_IntegralToBoolean: |
| 6618 | case CK_IntegralToFloating: |
| 6619 | case CK_FloatingToIntegral: |
| 6620 | case CK_FloatingToBoolean: |
| 6621 | case CK_FloatingCast: |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 6622 | case CK_CPointerToObjCPointerCast: |
| 6623 | case CK_BlockPointerToObjCPointerCast: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 6624 | case CK_AnyPointerToBlockPointerCast: |
| 6625 | case CK_ObjCObjectLValueCast: |
| 6626 | case CK_FloatingComplexToReal: |
| 6627 | case CK_FloatingComplexToBoolean: |
| 6628 | case CK_IntegralComplexToReal: |
| 6629 | case CK_IntegralComplexToBoolean: |
John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 6630 | case CK_ARCProduceObject: |
| 6631 | case CK_ARCConsumeObject: |
| 6632 | case CK_ARCReclaimReturnedObject: |
| 6633 | case CK_ARCExtendBlockObject: |
Douglas Gregor | ed90df3 | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 6634 | case CK_CopyAndAutoreleaseBlockObject: |
Eli Friedman | 34866c7 | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 6635 | case CK_BuiltinFnToFnPtr: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6636 | case CK_ZeroToOCLEvent: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 6637 | llvm_unreachable("invalid cast kind for complex value"); |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 6638 | |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 6639 | case CK_LValueToRValue: |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 6640 | case CK_AtomicToNonAtomic: |
| 6641 | case CK_NonAtomicToAtomic: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 6642 | case CK_NoOp: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6643 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 6644 | |
| 6645 | case CK_Dependent: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6646 | case CK_LValueBitCast: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 6647 | case CK_UserDefinedConversion: |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6648 | return Error(E); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 6649 | |
| 6650 | case CK_FloatingRealToComplex: { |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 6651 | APFloat &Real = Result.FloatReal; |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 6652 | if (!EvaluateFloat(E->getSubExpr(), Real, Info)) |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 6653 | return false; |
| 6654 | |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 6655 | Result.makeComplexFloat(); |
| 6656 | Result.FloatImag = APFloat(Real.getSemantics()); |
| 6657 | return true; |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 6658 | } |
| 6659 | |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 6660 | case CK_FloatingComplexCast: { |
| 6661 | if (!Visit(E->getSubExpr())) |
| 6662 | return false; |
| 6663 | |
| 6664 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 6665 | QualType From |
| 6666 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 6667 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 6668 | return HandleFloatToFloatCast(Info, E, From, To, Result.FloatReal) && |
| 6669 | HandleFloatToFloatCast(Info, E, From, To, Result.FloatImag); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 6670 | } |
| 6671 | |
| 6672 | case CK_FloatingComplexToIntegralComplex: { |
| 6673 | if (!Visit(E->getSubExpr())) |
| 6674 | return false; |
| 6675 | |
| 6676 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 6677 | QualType From |
| 6678 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 6679 | Result.makeComplexInt(); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 6680 | return HandleFloatToIntCast(Info, E, From, Result.FloatReal, |
| 6681 | To, Result.IntReal) && |
| 6682 | HandleFloatToIntCast(Info, E, From, Result.FloatImag, |
| 6683 | To, Result.IntImag); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 6684 | } |
| 6685 | |
| 6686 | case CK_IntegralRealToComplex: { |
| 6687 | APSInt &Real = Result.IntReal; |
| 6688 | if (!EvaluateInteger(E->getSubExpr(), Real, Info)) |
| 6689 | return false; |
| 6690 | |
| 6691 | Result.makeComplexInt(); |
| 6692 | Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned()); |
| 6693 | return true; |
| 6694 | } |
| 6695 | |
| 6696 | case CK_IntegralComplexCast: { |
| 6697 | if (!Visit(E->getSubExpr())) |
| 6698 | return false; |
| 6699 | |
| 6700 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 6701 | QualType From |
| 6702 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 6703 | |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 6704 | Result.IntReal = HandleIntToIntCast(Info, E, To, From, Result.IntReal); |
| 6705 | Result.IntImag = HandleIntToIntCast(Info, E, To, From, Result.IntImag); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 6706 | return true; |
| 6707 | } |
| 6708 | |
| 6709 | case CK_IntegralComplexToFloatingComplex: { |
| 6710 | if (!Visit(E->getSubExpr())) |
| 6711 | return false; |
| 6712 | |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 6713 | QualType To = E->getType()->castAs<ComplexType>()->getElementType(); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 6714 | QualType From |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 6715 | = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType(); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 6716 | Result.makeComplexFloat(); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 6717 | return HandleIntToFloatCast(Info, E, From, Result.IntReal, |
| 6718 | To, Result.FloatReal) && |
| 6719 | HandleIntToFloatCast(Info, E, From, Result.IntImag, |
| 6720 | To, Result.FloatImag); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 6721 | } |
| 6722 | } |
| 6723 | |
| 6724 | llvm_unreachable("unknown cast resulting in complex value"); |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 6725 | } |
| 6726 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 6727 | bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6728 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
Richard Smith | 10f4d06 | 2011-11-16 17:22:48 +0000 | [diff] [blame] | 6729 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
| 6730 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6731 | bool LHSOK = Visit(E->getLHS()); |
| 6732 | if (!LHSOK && !Info.keepEvaluatingAfterFailure()) |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 6733 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6734 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 6735 | ComplexValue RHS; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6736 | if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK) |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 6737 | return false; |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 6738 | |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 6739 | assert(Result.isComplexFloat() == RHS.isComplexFloat() && |
| 6740 | "Invalid operands to binary operator."); |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 6741 | switch (E->getOpcode()) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6742 | default: return Error(E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6743 | case BO_Add: |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 6744 | if (Result.isComplexFloat()) { |
| 6745 | Result.getComplexFloatReal().add(RHS.getComplexFloatReal(), |
| 6746 | APFloat::rmNearestTiesToEven); |
| 6747 | Result.getComplexFloatImag().add(RHS.getComplexFloatImag(), |
| 6748 | APFloat::rmNearestTiesToEven); |
| 6749 | } else { |
| 6750 | Result.getComplexIntReal() += RHS.getComplexIntReal(); |
| 6751 | Result.getComplexIntImag() += RHS.getComplexIntImag(); |
| 6752 | } |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 6753 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6754 | case BO_Sub: |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 6755 | if (Result.isComplexFloat()) { |
| 6756 | Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(), |
| 6757 | APFloat::rmNearestTiesToEven); |
| 6758 | Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(), |
| 6759 | APFloat::rmNearestTiesToEven); |
| 6760 | } else { |
| 6761 | Result.getComplexIntReal() -= RHS.getComplexIntReal(); |
| 6762 | Result.getComplexIntImag() -= RHS.getComplexIntImag(); |
| 6763 | } |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 6764 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6765 | case BO_Mul: |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 6766 | if (Result.isComplexFloat()) { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 6767 | ComplexValue LHS = Result; |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 6768 | APFloat &LHS_r = LHS.getComplexFloatReal(); |
| 6769 | APFloat &LHS_i = LHS.getComplexFloatImag(); |
| 6770 | APFloat &RHS_r = RHS.getComplexFloatReal(); |
| 6771 | APFloat &RHS_i = RHS.getComplexFloatImag(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6772 | |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 6773 | APFloat Tmp = LHS_r; |
| 6774 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 6775 | Result.getComplexFloatReal() = Tmp; |
| 6776 | Tmp = LHS_i; |
| 6777 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 6778 | Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven); |
| 6779 | |
| 6780 | Tmp = LHS_r; |
| 6781 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 6782 | Result.getComplexFloatImag() = Tmp; |
| 6783 | Tmp = LHS_i; |
| 6784 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 6785 | Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven); |
| 6786 | } else { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 6787 | ComplexValue LHS = Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6788 | Result.getComplexIntReal() = |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 6789 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() - |
| 6790 | LHS.getComplexIntImag() * RHS.getComplexIntImag()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6791 | Result.getComplexIntImag() = |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 6792 | (LHS.getComplexIntReal() * RHS.getComplexIntImag() + |
| 6793 | LHS.getComplexIntImag() * RHS.getComplexIntReal()); |
| 6794 | } |
| 6795 | break; |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 6796 | case BO_Div: |
| 6797 | if (Result.isComplexFloat()) { |
| 6798 | ComplexValue LHS = Result; |
| 6799 | APFloat &LHS_r = LHS.getComplexFloatReal(); |
| 6800 | APFloat &LHS_i = LHS.getComplexFloatImag(); |
| 6801 | APFloat &RHS_r = RHS.getComplexFloatReal(); |
| 6802 | APFloat &RHS_i = RHS.getComplexFloatImag(); |
| 6803 | APFloat &Res_r = Result.getComplexFloatReal(); |
| 6804 | APFloat &Res_i = Result.getComplexFloatImag(); |
| 6805 | |
| 6806 | APFloat Den = RHS_r; |
| 6807 | Den.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 6808 | APFloat Tmp = RHS_i; |
| 6809 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 6810 | Den.add(Tmp, APFloat::rmNearestTiesToEven); |
| 6811 | |
| 6812 | Res_r = LHS_r; |
| 6813 | Res_r.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 6814 | Tmp = LHS_i; |
| 6815 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 6816 | Res_r.add(Tmp, APFloat::rmNearestTiesToEven); |
| 6817 | Res_r.divide(Den, APFloat::rmNearestTiesToEven); |
| 6818 | |
| 6819 | Res_i = LHS_i; |
| 6820 | Res_i.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 6821 | Tmp = LHS_r; |
| 6822 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 6823 | Res_i.subtract(Tmp, APFloat::rmNearestTiesToEven); |
| 6824 | Res_i.divide(Den, APFloat::rmNearestTiesToEven); |
| 6825 | } else { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6826 | if (RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0) |
| 6827 | return Error(E, diag::note_expr_divide_by_zero); |
| 6828 | |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 6829 | ComplexValue LHS = Result; |
| 6830 | APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() + |
| 6831 | RHS.getComplexIntImag() * RHS.getComplexIntImag(); |
| 6832 | Result.getComplexIntReal() = |
| 6833 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() + |
| 6834 | LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den; |
| 6835 | Result.getComplexIntImag() = |
| 6836 | (LHS.getComplexIntImag() * RHS.getComplexIntReal() - |
| 6837 | LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den; |
| 6838 | } |
| 6839 | break; |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 6840 | } |
| 6841 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 6842 | return true; |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 6843 | } |
| 6844 | |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 6845 | bool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
| 6846 | // Get the operand value into 'Result'. |
| 6847 | if (!Visit(E->getSubExpr())) |
| 6848 | return false; |
| 6849 | |
| 6850 | switch (E->getOpcode()) { |
| 6851 | default: |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6852 | return Error(E); |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 6853 | case UO_Extension: |
| 6854 | return true; |
| 6855 | case UO_Plus: |
| 6856 | // The result is always just the subexpr. |
| 6857 | return true; |
| 6858 | case UO_Minus: |
| 6859 | if (Result.isComplexFloat()) { |
| 6860 | Result.getComplexFloatReal().changeSign(); |
| 6861 | Result.getComplexFloatImag().changeSign(); |
| 6862 | } |
| 6863 | else { |
| 6864 | Result.getComplexIntReal() = -Result.getComplexIntReal(); |
| 6865 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
| 6866 | } |
| 6867 | return true; |
| 6868 | case UO_Not: |
| 6869 | if (Result.isComplexFloat()) |
| 6870 | Result.getComplexFloatImag().changeSign(); |
| 6871 | else |
| 6872 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
| 6873 | return true; |
| 6874 | } |
| 6875 | } |
| 6876 | |
Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 6877 | bool ComplexExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 6878 | if (E->getNumInits() == 2) { |
| 6879 | if (E->getType()->isComplexType()) { |
| 6880 | Result.makeComplexFloat(); |
| 6881 | if (!EvaluateFloat(E->getInit(0), Result.FloatReal, Info)) |
| 6882 | return false; |
| 6883 | if (!EvaluateFloat(E->getInit(1), Result.FloatImag, Info)) |
| 6884 | return false; |
| 6885 | } else { |
| 6886 | Result.makeComplexInt(); |
| 6887 | if (!EvaluateInteger(E->getInit(0), Result.IntReal, Info)) |
| 6888 | return false; |
| 6889 | if (!EvaluateInteger(E->getInit(1), Result.IntImag, Info)) |
| 6890 | return false; |
| 6891 | } |
| 6892 | return true; |
| 6893 | } |
| 6894 | return ExprEvaluatorBaseTy::VisitInitListExpr(E); |
| 6895 | } |
| 6896 | |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 6897 | //===----------------------------------------------------------------------===// |
Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 6898 | // Void expression evaluation, primarily for a cast to void on the LHS of a |
| 6899 | // comma operator |
| 6900 | //===----------------------------------------------------------------------===// |
| 6901 | |
| 6902 | namespace { |
| 6903 | class VoidExprEvaluator |
| 6904 | : public ExprEvaluatorBase<VoidExprEvaluator, bool> { |
| 6905 | public: |
| 6906 | VoidExprEvaluator(EvalInfo &Info) : ExprEvaluatorBaseTy(Info) {} |
| 6907 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6908 | bool Success(const APValue &V, const Expr *e) { return true; } |
Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 6909 | |
| 6910 | bool VisitCastExpr(const CastExpr *E) { |
| 6911 | switch (E->getCastKind()) { |
| 6912 | default: |
| 6913 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 6914 | case CK_ToVoid: |
| 6915 | VisitIgnoredValue(E->getSubExpr()); |
| 6916 | return true; |
| 6917 | } |
| 6918 | } |
| 6919 | }; |
| 6920 | } // end anonymous namespace |
| 6921 | |
| 6922 | static bool EvaluateVoid(const Expr *E, EvalInfo &Info) { |
| 6923 | assert(E->isRValue() && E->getType()->isVoidType()); |
| 6924 | return VoidExprEvaluator(Info).Visit(E); |
| 6925 | } |
| 6926 | |
| 6927 | //===----------------------------------------------------------------------===// |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 6928 | // Top level Expr::EvaluateAsRValue method. |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 6929 | //===----------------------------------------------------------------------===// |
| 6930 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6931 | static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6932 | // In C, function designators are not lvalues, but we evaluate them as if they |
| 6933 | // are. |
| 6934 | if (E->isGLValue() || E->getType()->isFunctionType()) { |
| 6935 | LValue LV; |
| 6936 | if (!EvaluateLValue(E, LV, Info)) |
| 6937 | return false; |
| 6938 | LV.moveInto(Result); |
| 6939 | } else if (E->getType()->isVectorType()) { |
Richard Smith | 725810a | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 6940 | if (!EvaluateVector(E, Result, Info)) |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6941 | return false; |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 6942 | } else if (E->getType()->isIntegralOrEnumerationType()) { |
Richard Smith | 725810a | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 6943 | if (!IntExprEvaluator(Info, Result).Visit(E)) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 6944 | return false; |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 6945 | } else if (E->getType()->hasPointerRepresentation()) { |
| 6946 | LValue LV; |
| 6947 | if (!EvaluatePointer(E, LV, Info)) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 6948 | return false; |
Richard Smith | 725810a | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 6949 | LV.moveInto(Result); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 6950 | } else if (E->getType()->isRealFloatingType()) { |
| 6951 | llvm::APFloat F(0.0); |
| 6952 | if (!EvaluateFloat(E, F, Info)) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 6953 | return false; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6954 | Result = APValue(F); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 6955 | } else if (E->getType()->isAnyComplexType()) { |
| 6956 | ComplexValue C; |
| 6957 | if (!EvaluateComplex(E, C, Info)) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 6958 | return false; |
Richard Smith | 725810a | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 6959 | C.moveInto(Result); |
Richard Smith | ed5165f | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 6960 | } else if (E->getType()->isMemberPointerType()) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6961 | MemberPtr P; |
| 6962 | if (!EvaluateMemberPointer(E, P, Info)) |
| 6963 | return false; |
| 6964 | P.moveInto(Result); |
| 6965 | return true; |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6966 | } else if (E->getType()->isArrayType()) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6967 | LValue LV; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6968 | LV.set(E, Info.CurrentCall->Index); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6969 | if (!EvaluateArray(E, LV, Info.CurrentCall->Temporaries[E], Info)) |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 6970 | return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6971 | Result = Info.CurrentCall->Temporaries[E]; |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6972 | } else if (E->getType()->isRecordType()) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6973 | LValue LV; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6974 | LV.set(E, Info.CurrentCall->Index); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6975 | if (!EvaluateRecord(E, LV, Info.CurrentCall->Temporaries[E], Info)) |
| 6976 | return false; |
| 6977 | Result = Info.CurrentCall->Temporaries[E]; |
Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 6978 | } else if (E->getType()->isVoidType()) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6979 | if (!Info.getLangOpts().CPlusPlus11) |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 6980 | Info.CCEDiag(E, diag::note_constexpr_nonliteral) |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 6981 | << E->getType(); |
Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 6982 | if (!EvaluateVoid(E, Info)) |
| 6983 | return false; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6984 | } else if (Info.getLangOpts().CPlusPlus11) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 6985 | Info.Diag(E, diag::note_constexpr_nonliteral) << E->getType(); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 6986 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6987 | } else { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 6988 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Anders Carlsson | 7c282e4 | 2008-11-22 22:56:32 +0000 | [diff] [blame] | 6989 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6990 | } |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 6991 | |
Anders Carlsson | 7b6f0af | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 6992 | return true; |
| 6993 | } |
| 6994 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6995 | /// EvaluateInPlace - Evaluate an expression in-place in an APValue. In some |
| 6996 | /// cases, the in-place evaluation is essential, since later initializers for |
| 6997 | /// an object can indirectly refer to subobjects which were initialized earlier. |
| 6998 | static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, const LValue &This, |
| 6999 | const Expr *E, CheckConstantExpressionKind CCEK, |
| 7000 | bool AllowNonLiteralTypes) { |
Richard Smith | 6331c40 | 2012-02-13 22:16:19 +0000 | [diff] [blame] | 7001 | if (!AllowNonLiteralTypes && !CheckLiteralType(Info, E)) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 7002 | return false; |
| 7003 | |
| 7004 | if (E->isRValue()) { |
Richard Smith | ed5165f | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 7005 | // Evaluate arrays and record types in-place, so that later initializers can |
| 7006 | // refer to earlier-initialized members of the object. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7007 | if (E->getType()->isArrayType()) |
| 7008 | return EvaluateArray(E, This, Result, Info); |
| 7009 | else if (E->getType()->isRecordType()) |
| 7010 | return EvaluateRecord(E, This, Result, Info); |
Richard Smith | ed5165f | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 7011 | } |
| 7012 | |
| 7013 | // For any other type, in-place evaluation is unimportant. |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7014 | return Evaluate(Result, Info, E); |
Richard Smith | ed5165f | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 7015 | } |
| 7016 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7017 | /// EvaluateAsRValue - Try to evaluate this expression, performing an implicit |
| 7018 | /// lvalue-to-rvalue cast if it is an lvalue. |
| 7019 | static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 7020 | if (!CheckLiteralType(Info, E)) |
| 7021 | return false; |
| 7022 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7023 | if (!::Evaluate(Result, Info, E)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7024 | return false; |
| 7025 | |
| 7026 | if (E->isGLValue()) { |
| 7027 | LValue LV; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7028 | LV.setFrom(Info.Ctx, Result); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 7029 | if (!handleLValueToRValueConversion(Info, E, E->getType(), LV, Result)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7030 | return false; |
| 7031 | } |
| 7032 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7033 | // Check this core constant expression is a constant expression. |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 7034 | return CheckConstantExpression(Info, E->getExprLoc(), E->getType(), Result); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7035 | } |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7036 | |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 7037 | static bool FastEvaluateAsRValue(const Expr *Exp, Expr::EvalResult &Result, |
| 7038 | const ASTContext &Ctx, bool &IsConst) { |
| 7039 | // Fast-path evaluations of integer literals, since we sometimes see files |
| 7040 | // containing vast quantities of these. |
| 7041 | if (const IntegerLiteral *L = dyn_cast<IntegerLiteral>(Exp)) { |
| 7042 | Result.Val = APValue(APSInt(L->getValue(), |
| 7043 | L->getType()->isUnsignedIntegerType())); |
| 7044 | IsConst = true; |
| 7045 | return true; |
| 7046 | } |
| 7047 | |
| 7048 | // FIXME: Evaluating values of large array and record types can cause |
| 7049 | // performance problems. Only do so in C++11 for now. |
| 7050 | if (Exp->isRValue() && (Exp->getType()->isArrayType() || |
| 7051 | Exp->getType()->isRecordType()) && |
| 7052 | !Ctx.getLangOpts().CPlusPlus11) { |
| 7053 | IsConst = false; |
| 7054 | return true; |
| 7055 | } |
| 7056 | return false; |
| 7057 | } |
| 7058 | |
| 7059 | |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 7060 | /// EvaluateAsRValue - Return true if this is a constant which we can fold using |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 7061 | /// any crazy technique (that has nothing to do with language standards) that |
| 7062 | /// we want to. If this function returns true, it returns the folded constant |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7063 | /// in Result. If this expression is a glvalue, an lvalue-to-rvalue conversion |
| 7064 | /// will be applied to the result. |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 7065 | bool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx) const { |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 7066 | bool IsConst; |
| 7067 | if (FastEvaluateAsRValue(this, Result, Ctx, IsConst)) |
| 7068 | return IsConst; |
| 7069 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7070 | EvalInfo Info(Ctx, Result); |
| 7071 | return ::EvaluateAsRValue(Info, this, Result.Val); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 7072 | } |
| 7073 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7074 | bool Expr::EvaluateAsBooleanCondition(bool &Result, |
| 7075 | const ASTContext &Ctx) const { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7076 | EvalResult Scratch; |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 7077 | return EvaluateAsRValue(Scratch, Ctx) && |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7078 | HandleConversionToBool(Scratch.Val, Result); |
John McCall | 1be1c63 | 2010-01-05 23:42:56 +0000 | [diff] [blame] | 7079 | } |
| 7080 | |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 7081 | bool Expr::EvaluateAsInt(APSInt &Result, const ASTContext &Ctx, |
| 7082 | SideEffectsKind AllowSideEffects) const { |
| 7083 | if (!getType()->isIntegralOrEnumerationType()) |
| 7084 | return false; |
| 7085 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7086 | EvalResult ExprResult; |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 7087 | if (!EvaluateAsRValue(ExprResult, Ctx) || !ExprResult.Val.isInt() || |
| 7088 | (!AllowSideEffects && ExprResult.HasSideEffects)) |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7089 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7090 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7091 | Result = ExprResult.Val.getInt(); |
| 7092 | return true; |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 7093 | } |
| 7094 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7095 | bool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx) const { |
Anders Carlsson | 4316812 | 2009-04-10 04:54:13 +0000 | [diff] [blame] | 7096 | EvalInfo Info(Ctx, Result); |
| 7097 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 7098 | LValue LV; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 7099 | if (!EvaluateLValue(this, LV, Info) || Result.HasSideEffects || |
| 7100 | !CheckLValueConstantExpression(Info, getExprLoc(), |
| 7101 | Ctx.getLValueReferenceType(getType()), LV)) |
| 7102 | return false; |
| 7103 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7104 | LV.moveInto(Result.Val); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 7105 | return true; |
Eli Friedman | 7d45c48 | 2009-09-13 10:17:44 +0000 | [diff] [blame] | 7106 | } |
| 7107 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 7108 | bool Expr::EvaluateAsInitializer(APValue &Value, const ASTContext &Ctx, |
| 7109 | const VarDecl *VD, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 7110 | SmallVectorImpl<PartialDiagnosticAt> &Notes) const { |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 7111 | // FIXME: Evaluating initializers for large array and record types can cause |
| 7112 | // performance problems. Only do so in C++11 for now. |
| 7113 | if (isRValue() && (getType()->isArrayType() || getType()->isRecordType()) && |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7114 | !Ctx.getLangOpts().CPlusPlus11) |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 7115 | return false; |
| 7116 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 7117 | Expr::EvalStatus EStatus; |
| 7118 | EStatus.Diag = &Notes; |
| 7119 | |
| 7120 | EvalInfo InitInfo(Ctx, EStatus); |
| 7121 | InitInfo.setEvaluatingDecl(VD, Value); |
| 7122 | |
| 7123 | LValue LVal; |
| 7124 | LVal.set(VD); |
| 7125 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 7126 | // C++11 [basic.start.init]p2: |
| 7127 | // Variables with static storage duration or thread storage duration shall be |
| 7128 | // zero-initialized before any other initialization takes place. |
| 7129 | // This behavior is not present in C. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7130 | if (Ctx.getLangOpts().CPlusPlus && !VD->hasLocalStorage() && |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 7131 | !VD->getType()->isReferenceType()) { |
| 7132 | ImplicitValueInitExpr VIE(VD->getType()); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 7133 | if (!EvaluateInPlace(Value, InitInfo, LVal, &VIE, CCEK_Constant, |
| 7134 | /*AllowNonLiteralTypes=*/true)) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 7135 | return false; |
| 7136 | } |
| 7137 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 7138 | if (!EvaluateInPlace(Value, InitInfo, LVal, this, CCEK_Constant, |
| 7139 | /*AllowNonLiteralTypes=*/true) || |
| 7140 | EStatus.HasSideEffects) |
| 7141 | return false; |
| 7142 | |
| 7143 | return CheckConstantExpression(InitInfo, VD->getLocation(), VD->getType(), |
| 7144 | Value); |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 7145 | } |
| 7146 | |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 7147 | /// isEvaluatable - Call EvaluateAsRValue to see if this expression can be |
| 7148 | /// constant folded, but discard the result. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7149 | bool Expr::isEvaluatable(const ASTContext &Ctx) const { |
Anders Carlsson | 5b3638b | 2008-12-01 06:44:05 +0000 | [diff] [blame] | 7150 | EvalResult Result; |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 7151 | return EvaluateAsRValue(Result, Ctx) && !Result.HasSideEffects; |
Chris Lattner | cb13691 | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 7152 | } |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 7153 | |
Fariborz Jahanian | 8b115b7 | 2013-01-09 23:04:56 +0000 | [diff] [blame] | 7154 | APSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 7155 | SmallVectorImpl<PartialDiagnosticAt> *Diag) const { |
Anders Carlsson | 6736d1a2 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 7156 | EvalResult EvalResult; |
Fariborz Jahanian | 8b115b7 | 2013-01-09 23:04:56 +0000 | [diff] [blame] | 7157 | EvalResult.Diag = Diag; |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 7158 | bool Result = EvaluateAsRValue(EvalResult, Ctx); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 7159 | (void)Result; |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 7160 | assert(Result && "Could not evaluate expression"); |
Anders Carlsson | 6736d1a2 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 7161 | assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer"); |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 7162 | |
Anders Carlsson | 6736d1a2 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 7163 | return EvalResult.Val.getInt(); |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 7164 | } |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7165 | |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 7166 | void Expr::EvaluateForOverflow(const ASTContext &Ctx, |
| 7167 | SmallVectorImpl<PartialDiagnosticAt> *Diags) const { |
| 7168 | bool IsConst; |
| 7169 | EvalResult EvalResult; |
| 7170 | EvalResult.Diag = Diags; |
| 7171 | if (!FastEvaluateAsRValue(this, EvalResult, Ctx, IsConst)) { |
| 7172 | EvalInfo Info(Ctx, EvalResult, true); |
| 7173 | (void)::EvaluateAsRValue(Info, this, EvalResult.Val); |
| 7174 | } |
| 7175 | } |
| 7176 | |
Abramo Bagnara | f819945 | 2010-05-14 17:07:14 +0000 | [diff] [blame] | 7177 | bool Expr::EvalResult::isGlobalLValue() const { |
| 7178 | assert(Val.isLValue()); |
| 7179 | return IsGlobalLValue(Val.getLValueBase()); |
| 7180 | } |
| 7181 | |
| 7182 | |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7183 | /// isIntegerConstantExpr - this recursive routine will test if an expression is |
| 7184 | /// an integer constant expression. |
| 7185 | |
| 7186 | /// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, |
| 7187 | /// comma, etc |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7188 | |
| 7189 | // CheckICE - This function does the fundamental ICE checking: the returned |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7190 | // ICEDiag contains an ICEKind indicating whether the expression is an ICE, |
| 7191 | // and a (possibly null) SourceLocation indicating the location of the problem. |
| 7192 | // |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7193 | // Note that to reduce code duplication, this helper does no evaluation |
| 7194 | // itself; the caller checks whether the expression is evaluatable, and |
| 7195 | // in the rare cases where CheckICE actually cares about the evaluated |
| 7196 | // value, it calls into Evalute. |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7197 | |
Dan Gohman | 28ade55 | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 7198 | namespace { |
| 7199 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7200 | enum ICEKind { |
| 7201 | /// This expression is an ICE. |
| 7202 | IK_ICE, |
| 7203 | /// This expression is not an ICE, but if it isn't evaluated, it's |
| 7204 | /// a legal subexpression for an ICE. This return value is used to handle |
| 7205 | /// the comma operator in C99 mode, and non-constant subexpressions. |
| 7206 | IK_ICEIfUnevaluated, |
| 7207 | /// This expression is not an ICE, and is not a legal subexpression for one. |
| 7208 | IK_NotICE |
| 7209 | }; |
| 7210 | |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7211 | struct ICEDiag { |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7212 | ICEKind Kind; |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7213 | SourceLocation Loc; |
| 7214 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7215 | ICEDiag(ICEKind IK, SourceLocation l) : Kind(IK), Loc(l) {} |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7216 | }; |
| 7217 | |
Dan Gohman | 28ade55 | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 7218 | } |
| 7219 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7220 | static ICEDiag NoDiag() { return ICEDiag(IK_ICE, SourceLocation()); } |
| 7221 | |
| 7222 | static ICEDiag Worst(ICEDiag A, ICEDiag B) { return A.Kind >= B.Kind ? A : B; } |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7223 | |
| 7224 | static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) { |
| 7225 | Expr::EvalResult EVResult; |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 7226 | if (!E->EvaluateAsRValue(EVResult, Ctx) || EVResult.HasSideEffects || |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7227 | !EVResult.Val.isInt()) |
| 7228 | return ICEDiag(IK_NotICE, E->getLocStart()); |
| 7229 | |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7230 | return NoDiag(); |
| 7231 | } |
| 7232 | |
| 7233 | static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) { |
| 7234 | assert(!E->isValueDependent() && "Should not see value dependent exprs!"); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7235 | if (!E->getType()->isIntegralOrEnumerationType()) |
| 7236 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7237 | |
| 7238 | switch (E->getStmtClass()) { |
John McCall | bd06678 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 7239 | #define ABSTRACT_STMT(Node) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7240 | #define STMT(Node, Base) case Expr::Node##Class: |
| 7241 | #define EXPR(Node, Base) |
| 7242 | #include "clang/AST/StmtNodes.inc" |
| 7243 | case Expr::PredefinedExprClass: |
| 7244 | case Expr::FloatingLiteralClass: |
| 7245 | case Expr::ImaginaryLiteralClass: |
| 7246 | case Expr::StringLiteralClass: |
| 7247 | case Expr::ArraySubscriptExprClass: |
| 7248 | case Expr::MemberExprClass: |
| 7249 | case Expr::CompoundAssignOperatorClass: |
| 7250 | case Expr::CompoundLiteralExprClass: |
| 7251 | case Expr::ExtVectorElementExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7252 | case Expr::DesignatedInitExprClass: |
| 7253 | case Expr::ImplicitValueInitExprClass: |
| 7254 | case Expr::ParenListExprClass: |
| 7255 | case Expr::VAArgExprClass: |
| 7256 | case Expr::AddrLabelExprClass: |
| 7257 | case Expr::StmtExprClass: |
| 7258 | case Expr::CXXMemberCallExprClass: |
Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 7259 | case Expr::CUDAKernelCallExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7260 | case Expr::CXXDynamicCastExprClass: |
| 7261 | case Expr::CXXTypeidExprClass: |
Francois Pichet | 5cc0a67 | 2010-09-08 23:47:05 +0000 | [diff] [blame] | 7262 | case Expr::CXXUuidofExprClass: |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 7263 | case Expr::MSPropertyRefExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7264 | case Expr::CXXNullPtrLiteralExprClass: |
Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 7265 | case Expr::UserDefinedLiteralClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7266 | case Expr::CXXThisExprClass: |
| 7267 | case Expr::CXXThrowExprClass: |
| 7268 | case Expr::CXXNewExprClass: |
| 7269 | case Expr::CXXDeleteExprClass: |
| 7270 | case Expr::CXXPseudoDestructorExprClass: |
| 7271 | case Expr::UnresolvedLookupExprClass: |
| 7272 | case Expr::DependentScopeDeclRefExprClass: |
| 7273 | case Expr::CXXConstructExprClass: |
| 7274 | case Expr::CXXBindTemporaryExprClass: |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 7275 | case Expr::ExprWithCleanupsClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7276 | case Expr::CXXTemporaryObjectExprClass: |
| 7277 | case Expr::CXXUnresolvedConstructExprClass: |
| 7278 | case Expr::CXXDependentScopeMemberExprClass: |
| 7279 | case Expr::UnresolvedMemberExprClass: |
| 7280 | case Expr::ObjCStringLiteralClass: |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 7281 | case Expr::ObjCBoxedExprClass: |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 7282 | case Expr::ObjCArrayLiteralClass: |
| 7283 | case Expr::ObjCDictionaryLiteralClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7284 | case Expr::ObjCEncodeExprClass: |
| 7285 | case Expr::ObjCMessageExprClass: |
| 7286 | case Expr::ObjCSelectorExprClass: |
| 7287 | case Expr::ObjCProtocolExprClass: |
| 7288 | case Expr::ObjCIvarRefExprClass: |
| 7289 | case Expr::ObjCPropertyRefExprClass: |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 7290 | case Expr::ObjCSubscriptRefExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7291 | case Expr::ObjCIsaExprClass: |
| 7292 | case Expr::ShuffleVectorExprClass: |
| 7293 | case Expr::BlockExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7294 | case Expr::NoStmtClass: |
John McCall | 8d69a21 | 2010-11-15 23:31:06 +0000 | [diff] [blame] | 7295 | case Expr::OpaqueValueExprClass: |
Douglas Gregor | e8e9dd6 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 7296 | case Expr::PackExpansionExprClass: |
Douglas Gregor | cdbc539 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 7297 | case Expr::SubstNonTypeTemplateParmPackExprClass: |
Richard Smith | b15fe3a | 2012-09-12 00:56:43 +0000 | [diff] [blame] | 7298 | case Expr::FunctionParmPackExprClass: |
Tanya Lattner | 55808c1 | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 7299 | case Expr::AsTypeExprClass: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7300 | case Expr::ObjCIndirectCopyRestoreExprClass: |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 7301 | case Expr::MaterializeTemporaryExprClass: |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 7302 | case Expr::PseudoObjectExprClass: |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 7303 | case Expr::AtomicExprClass: |
Sebastian Redl | 12757ab | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 7304 | case Expr::InitListExprClass: |
Douglas Gregor | e31e606 | 2012-02-07 10:09:13 +0000 | [diff] [blame] | 7305 | case Expr::LambdaExprClass: |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7306 | return ICEDiag(IK_NotICE, E->getLocStart()); |
Sebastian Redl | 12757ab | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 7307 | |
Douglas Gregor | 820ba7b | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 7308 | case Expr::SizeOfPackExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7309 | case Expr::GNUNullExprClass: |
| 7310 | // GCC considers the GNU __null value to be an integral constant expression. |
| 7311 | return NoDiag(); |
| 7312 | |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 7313 | case Expr::SubstNonTypeTemplateParmExprClass: |
| 7314 | return |
| 7315 | CheckICE(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(), Ctx); |
| 7316 | |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7317 | case Expr::ParenExprClass: |
| 7318 | return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx); |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 7319 | case Expr::GenericSelectionExprClass: |
| 7320 | return CheckICE(cast<GenericSelectionExpr>(E)->getResultExpr(), Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7321 | case Expr::IntegerLiteralClass: |
| 7322 | case Expr::CharacterLiteralClass: |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 7323 | case Expr::ObjCBoolLiteralExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7324 | case Expr::CXXBoolLiteralExprClass: |
Douglas Gregor | 747eb78 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 7325 | case Expr::CXXScalarValueInitExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7326 | case Expr::UnaryTypeTraitExprClass: |
Francois Pichet | 9dfa3ce | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 7327 | case Expr::BinaryTypeTraitExprClass: |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 7328 | case Expr::TypeTraitExprClass: |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 7329 | case Expr::ArrayTypeTraitExprClass: |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 7330 | case Expr::ExpressionTraitExprClass: |
Sebastian Redl | 4202c0f | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 7331 | case Expr::CXXNoexceptExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7332 | return NoDiag(); |
| 7333 | case Expr::CallExprClass: |
Alexis Hunt | 3b79186 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 7334 | case Expr::CXXOperatorCallExprClass: { |
Richard Smith | 62f6595 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 7335 | // C99 6.6/3 allows function calls within unevaluated subexpressions of |
| 7336 | // constant expressions, but they can never be ICEs because an ICE cannot |
| 7337 | // contain an operand of (pointer to) function type. |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7338 | const CallExpr *CE = cast<CallExpr>(E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7339 | if (CE->isBuiltinCall()) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7340 | return CheckEvalInICE(E, Ctx); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7341 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7342 | } |
Richard Smith | 6365c91 | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 7343 | case Expr::DeclRefExprClass: { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7344 | if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl())) |
| 7345 | return NoDiag(); |
Richard Smith | 6365c91 | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 7346 | const ValueDecl *D = dyn_cast<ValueDecl>(cast<DeclRefExpr>(E)->getDecl()); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7347 | if (Ctx.getLangOpts().CPlusPlus && |
Richard Smith | 6365c91 | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 7348 | D && IsConstNonVolatile(D->getType())) { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7349 | // Parameter variables are never constants. Without this check, |
| 7350 | // getAnyInitializer() can find a default argument, which leads |
| 7351 | // to chaos. |
| 7352 | if (isa<ParmVarDecl>(D)) |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7353 | return ICEDiag(IK_NotICE, cast<DeclRefExpr>(E)->getLocation()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7354 | |
| 7355 | // C++ 7.1.5.1p2 |
| 7356 | // A variable of non-volatile const-qualified integral or enumeration |
| 7357 | // type initialized by an ICE can be used in ICEs. |
| 7358 | if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) { |
Richard Smith | ec8dcd2 | 2011-11-08 01:31:09 +0000 | [diff] [blame] | 7359 | if (!Dcl->getType()->isIntegralOrEnumerationType()) |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7360 | return ICEDiag(IK_NotICE, cast<DeclRefExpr>(E)->getLocation()); |
Richard Smith | ec8dcd2 | 2011-11-08 01:31:09 +0000 | [diff] [blame] | 7361 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 7362 | const VarDecl *VD; |
| 7363 | // Look for a declaration of this variable that has an initializer, and |
| 7364 | // check whether it is an ICE. |
| 7365 | if (Dcl->getAnyInitializer(VD) && VD->checkInitIsICE()) |
| 7366 | return NoDiag(); |
| 7367 | else |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7368 | return ICEDiag(IK_NotICE, cast<DeclRefExpr>(E)->getLocation()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7369 | } |
| 7370 | } |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7371 | return ICEDiag(IK_NotICE, E->getLocStart()); |
Richard Smith | 6365c91 | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 7372 | } |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7373 | case Expr::UnaryOperatorClass: { |
| 7374 | const UnaryOperator *Exp = cast<UnaryOperator>(E); |
| 7375 | switch (Exp->getOpcode()) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7376 | case UO_PostInc: |
| 7377 | case UO_PostDec: |
| 7378 | case UO_PreInc: |
| 7379 | case UO_PreDec: |
| 7380 | case UO_AddrOf: |
| 7381 | case UO_Deref: |
Richard Smith | 62f6595 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 7382 | // C99 6.6/3 allows increment and decrement within unevaluated |
| 7383 | // subexpressions of constant expressions, but they can never be ICEs |
| 7384 | // because an ICE cannot contain an lvalue operand. |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7385 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7386 | case UO_Extension: |
| 7387 | case UO_LNot: |
| 7388 | case UO_Plus: |
| 7389 | case UO_Minus: |
| 7390 | case UO_Not: |
| 7391 | case UO_Real: |
| 7392 | case UO_Imag: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7393 | return CheckICE(Exp->getSubExpr(), Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7394 | } |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7395 | |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7396 | // OffsetOf falls through here. |
| 7397 | } |
| 7398 | case Expr::OffsetOfExprClass: { |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7399 | // Note that per C99, offsetof must be an ICE. And AFAIK, using |
| 7400 | // EvaluateAsRValue matches the proposed gcc behavior for cases like |
| 7401 | // "offsetof(struct s{int x[4];}, x[1.0])". This doesn't affect |
| 7402 | // compliance: we should warn earlier for offsetof expressions with |
| 7403 | // array subscripts that aren't ICEs, and if the array subscripts |
| 7404 | // are ICEs, the value of the offsetof must be an integer constant. |
| 7405 | return CheckEvalInICE(E, Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7406 | } |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 7407 | case Expr::UnaryExprOrTypeTraitExprClass: { |
| 7408 | const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(E); |
| 7409 | if ((Exp->getKind() == UETT_SizeOf) && |
| 7410 | Exp->getTypeOfArgument()->isVariableArrayType()) |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7411 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7412 | return NoDiag(); |
| 7413 | } |
| 7414 | case Expr::BinaryOperatorClass: { |
| 7415 | const BinaryOperator *Exp = cast<BinaryOperator>(E); |
| 7416 | switch (Exp->getOpcode()) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7417 | case BO_PtrMemD: |
| 7418 | case BO_PtrMemI: |
| 7419 | case BO_Assign: |
| 7420 | case BO_MulAssign: |
| 7421 | case BO_DivAssign: |
| 7422 | case BO_RemAssign: |
| 7423 | case BO_AddAssign: |
| 7424 | case BO_SubAssign: |
| 7425 | case BO_ShlAssign: |
| 7426 | case BO_ShrAssign: |
| 7427 | case BO_AndAssign: |
| 7428 | case BO_XorAssign: |
| 7429 | case BO_OrAssign: |
Richard Smith | 62f6595 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 7430 | // C99 6.6/3 allows assignments within unevaluated subexpressions of |
| 7431 | // constant expressions, but they can never be ICEs because an ICE cannot |
| 7432 | // contain an lvalue operand. |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7433 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7434 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7435 | case BO_Mul: |
| 7436 | case BO_Div: |
| 7437 | case BO_Rem: |
| 7438 | case BO_Add: |
| 7439 | case BO_Sub: |
| 7440 | case BO_Shl: |
| 7441 | case BO_Shr: |
| 7442 | case BO_LT: |
| 7443 | case BO_GT: |
| 7444 | case BO_LE: |
| 7445 | case BO_GE: |
| 7446 | case BO_EQ: |
| 7447 | case BO_NE: |
| 7448 | case BO_And: |
| 7449 | case BO_Xor: |
| 7450 | case BO_Or: |
| 7451 | case BO_Comma: { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7452 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 7453 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7454 | if (Exp->getOpcode() == BO_Div || |
| 7455 | Exp->getOpcode() == BO_Rem) { |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 7456 | // EvaluateAsRValue gives an error for undefined Div/Rem, so make sure |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7457 | // we don't evaluate one. |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7458 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE) { |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 7459 | llvm::APSInt REval = Exp->getRHS()->EvaluateKnownConstInt(Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7460 | if (REval == 0) |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7461 | return ICEDiag(IK_ICEIfUnevaluated, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7462 | if (REval.isSigned() && REval.isAllOnesValue()) { |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 7463 | llvm::APSInt LEval = Exp->getLHS()->EvaluateKnownConstInt(Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7464 | if (LEval.isMinSignedValue()) |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7465 | return ICEDiag(IK_ICEIfUnevaluated, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7466 | } |
| 7467 | } |
| 7468 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7469 | if (Exp->getOpcode() == BO_Comma) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7470 | if (Ctx.getLangOpts().C99) { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7471 | // C99 6.6p3 introduces a strange edge case: comma can be in an ICE |
| 7472 | // if it isn't evaluated. |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7473 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE) |
| 7474 | return ICEDiag(IK_ICEIfUnevaluated, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7475 | } else { |
| 7476 | // In both C89 and C++, commas in ICEs are illegal. |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7477 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7478 | } |
| 7479 | } |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7480 | return Worst(LHSResult, RHSResult); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7481 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7482 | case BO_LAnd: |
| 7483 | case BO_LOr: { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7484 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 7485 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7486 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICEIfUnevaluated) { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7487 | // Rare case where the RHS has a comma "side-effect"; we need |
| 7488 | // to actually check the condition to see whether the side |
| 7489 | // with the comma is evaluated. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7490 | if ((Exp->getOpcode() == BO_LAnd) != |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 7491 | (Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0)) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7492 | return RHSResult; |
| 7493 | return NoDiag(); |
| 7494 | } |
| 7495 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7496 | return Worst(LHSResult, RHSResult); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7497 | } |
| 7498 | } |
| 7499 | } |
| 7500 | case Expr::ImplicitCastExprClass: |
| 7501 | case Expr::CStyleCastExprClass: |
| 7502 | case Expr::CXXFunctionalCastExprClass: |
| 7503 | case Expr::CXXStaticCastExprClass: |
| 7504 | case Expr::CXXReinterpretCastExprClass: |
Richard Smith | c3e31e7 | 2011-10-24 18:26:35 +0000 | [diff] [blame] | 7505 | case Expr::CXXConstCastExprClass: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7506 | case Expr::ObjCBridgedCastExprClass: { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7507 | const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr(); |
Richard Smith | 0b973d0 | 2011-12-18 02:33:09 +0000 | [diff] [blame] | 7508 | if (isa<ExplicitCastExpr>(E)) { |
| 7509 | if (const FloatingLiteral *FL |
| 7510 | = dyn_cast<FloatingLiteral>(SubExpr->IgnoreParenImpCasts())) { |
| 7511 | unsigned DestWidth = Ctx.getIntWidth(E->getType()); |
| 7512 | bool DestSigned = E->getType()->isSignedIntegerOrEnumerationType(); |
| 7513 | APSInt IgnoredVal(DestWidth, !DestSigned); |
| 7514 | bool Ignored; |
| 7515 | // If the value does not fit in the destination type, the behavior is |
| 7516 | // undefined, so we are not required to treat it as a constant |
| 7517 | // expression. |
| 7518 | if (FL->getValue().convertToInteger(IgnoredVal, |
| 7519 | llvm::APFloat::rmTowardZero, |
| 7520 | &Ignored) & APFloat::opInvalidOp) |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7521 | return ICEDiag(IK_NotICE, E->getLocStart()); |
Richard Smith | 0b973d0 | 2011-12-18 02:33:09 +0000 | [diff] [blame] | 7522 | return NoDiag(); |
| 7523 | } |
| 7524 | } |
Eli Friedman | 76d4e43 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 7525 | switch (cast<CastExpr>(E)->getCastKind()) { |
| 7526 | case CK_LValueToRValue: |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 7527 | case CK_AtomicToNonAtomic: |
| 7528 | case CK_NonAtomicToAtomic: |
Eli Friedman | 76d4e43 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 7529 | case CK_NoOp: |
| 7530 | case CK_IntegralToBoolean: |
| 7531 | case CK_IntegralCast: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7532 | return CheckICE(SubExpr, Ctx); |
Eli Friedman | 76d4e43 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 7533 | default: |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7534 | return ICEDiag(IK_NotICE, E->getLocStart()); |
Eli Friedman | 76d4e43 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 7535 | } |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7536 | } |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 7537 | case Expr::BinaryConditionalOperatorClass: { |
| 7538 | const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(E); |
| 7539 | ICEDiag CommonResult = CheckICE(Exp->getCommon(), Ctx); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7540 | if (CommonResult.Kind == IK_NotICE) return CommonResult; |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 7541 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7542 | if (FalseResult.Kind == IK_NotICE) return FalseResult; |
| 7543 | if (CommonResult.Kind == IK_ICEIfUnevaluated) return CommonResult; |
| 7544 | if (FalseResult.Kind == IK_ICEIfUnevaluated && |
Richard Smith | 74fc721 | 2012-12-28 12:53:55 +0000 | [diff] [blame] | 7545 | Exp->getCommon()->EvaluateKnownConstInt(Ctx) != 0) return NoDiag(); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 7546 | return FalseResult; |
| 7547 | } |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7548 | case Expr::ConditionalOperatorClass: { |
| 7549 | const ConditionalOperator *Exp = cast<ConditionalOperator>(E); |
| 7550 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
| 7551 | // then only the true side is actually considered in an integer constant |
| 7552 | // expression, and it is fully evaluated. This is an important GNU |
| 7553 | // extension. See GCC PR38377 for discussion. |
| 7554 | if (const CallExpr *CallCE |
| 7555 | = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts())) |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 7556 | if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p) |
| 7557 | return CheckEvalInICE(E, Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7558 | ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7559 | if (CondResult.Kind == IK_NotICE) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7560 | return CondResult; |
Douglas Gregor | fcafc6e | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 7561 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7562 | ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx); |
| 7563 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
Douglas Gregor | fcafc6e | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 7564 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7565 | if (TrueResult.Kind == IK_NotICE) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7566 | return TrueResult; |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7567 | if (FalseResult.Kind == IK_NotICE) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7568 | return FalseResult; |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7569 | if (CondResult.Kind == IK_ICEIfUnevaluated) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7570 | return CondResult; |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7571 | if (TrueResult.Kind == IK_ICE && FalseResult.Kind == IK_ICE) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7572 | return NoDiag(); |
| 7573 | // Rare case where the diagnostics depend on which side is evaluated |
| 7574 | // Note that if we get here, CondResult is 0, and at least one of |
| 7575 | // TrueResult and FalseResult is non-zero. |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7576 | if (Exp->getCond()->EvaluateKnownConstInt(Ctx) == 0) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7577 | return FalseResult; |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7578 | return TrueResult; |
| 7579 | } |
| 7580 | case Expr::CXXDefaultArgExprClass: |
| 7581 | return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 7582 | case Expr::CXXDefaultInitExprClass: |
| 7583 | return CheckICE(cast<CXXDefaultInitExpr>(E)->getExpr(), Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7584 | case Expr::ChooseExprClass: { |
| 7585 | return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx); |
| 7586 | } |
| 7587 | } |
| 7588 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 7589 | llvm_unreachable("Invalid StmtClass!"); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7590 | } |
| 7591 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7592 | /// Evaluate an expression as a C++11 integral constant expression. |
| 7593 | static bool EvaluateCPlusPlus11IntegralConstantExpr(ASTContext &Ctx, |
| 7594 | const Expr *E, |
| 7595 | llvm::APSInt *Value, |
| 7596 | SourceLocation *Loc) { |
| 7597 | if (!E->getType()->isIntegralOrEnumerationType()) { |
| 7598 | if (Loc) *Loc = E->getExprLoc(); |
| 7599 | return false; |
| 7600 | } |
| 7601 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7602 | APValue Result; |
| 7603 | if (!E->isCXX11ConstantExpr(Ctx, &Result, Loc)) |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 7604 | return false; |
| 7605 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7606 | assert(Result.isInt() && "pointer cast to int is not an ICE"); |
| 7607 | if (Value) *Value = Result.getInt(); |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 7608 | return true; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7609 | } |
| 7610 | |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 7611 | bool Expr::isIntegerConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7612 | if (Ctx.getLangOpts().CPlusPlus11) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7613 | return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, 0, Loc); |
| 7614 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7615 | ICEDiag D = CheckICE(this, Ctx); |
| 7616 | if (D.Kind != IK_ICE) { |
| 7617 | if (Loc) *Loc = D.Loc; |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7618 | return false; |
| 7619 | } |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7620 | return true; |
| 7621 | } |
| 7622 | |
| 7623 | bool Expr::isIntegerConstantExpr(llvm::APSInt &Value, ASTContext &Ctx, |
| 7624 | SourceLocation *Loc, bool isEvaluated) const { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7625 | if (Ctx.getLangOpts().CPlusPlus11) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7626 | return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, &Value, Loc); |
| 7627 | |
| 7628 | if (!isIntegerConstantExpr(Ctx, Loc)) |
| 7629 | return false; |
| 7630 | if (!EvaluateAsInt(Value, Ctx)) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7631 | llvm_unreachable("ICE cannot be evaluated!"); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 7632 | return true; |
| 7633 | } |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7634 | |
Richard Smith | 98a0a49 | 2012-02-14 21:38:30 +0000 | [diff] [blame] | 7635 | bool Expr::isCXX98IntegralConstantExpr(ASTContext &Ctx) const { |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 7636 | return CheckICE(this, Ctx).Kind == IK_ICE; |
Richard Smith | 98a0a49 | 2012-02-14 21:38:30 +0000 | [diff] [blame] | 7637 | } |
| 7638 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7639 | bool Expr::isCXX11ConstantExpr(ASTContext &Ctx, APValue *Result, |
| 7640 | SourceLocation *Loc) const { |
| 7641 | // We support this checking in C++98 mode in order to diagnose compatibility |
| 7642 | // issues. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7643 | assert(Ctx.getLangOpts().CPlusPlus); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7644 | |
Richard Smith | 98a0a49 | 2012-02-14 21:38:30 +0000 | [diff] [blame] | 7645 | // Build evaluation settings. |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7646 | Expr::EvalStatus Status; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 7647 | SmallVector<PartialDiagnosticAt, 8> Diags; |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7648 | Status.Diag = &Diags; |
| 7649 | EvalInfo Info(Ctx, Status); |
| 7650 | |
| 7651 | APValue Scratch; |
| 7652 | bool IsConstExpr = ::EvaluateAsRValue(Info, this, Result ? *Result : Scratch); |
| 7653 | |
| 7654 | if (!Diags.empty()) { |
| 7655 | IsConstExpr = false; |
| 7656 | if (Loc) *Loc = Diags[0].first; |
| 7657 | } else if (!IsConstExpr) { |
| 7658 | // FIXME: This shouldn't happen. |
| 7659 | if (Loc) *Loc = getExprLoc(); |
| 7660 | } |
| 7661 | |
| 7662 | return IsConstExpr; |
| 7663 | } |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 7664 | |
| 7665 | bool Expr::isPotentialConstantExpr(const FunctionDecl *FD, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 7666 | SmallVectorImpl< |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 7667 | PartialDiagnosticAt> &Diags) { |
| 7668 | // FIXME: It would be useful to check constexpr function templates, but at the |
| 7669 | // moment the constant expression evaluator cannot cope with the non-rigorous |
| 7670 | // ASTs which we build for dependent expressions. |
| 7671 | if (FD->isDependentContext()) |
| 7672 | return true; |
| 7673 | |
| 7674 | Expr::EvalStatus Status; |
| 7675 | Status.Diag = &Diags; |
| 7676 | |
| 7677 | EvalInfo Info(FD->getASTContext(), Status); |
| 7678 | Info.CheckingPotentialConstantExpression = true; |
| 7679 | |
| 7680 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
| 7681 | const CXXRecordDecl *RD = MD ? MD->getParent()->getCanonicalDecl() : 0; |
| 7682 | |
| 7683 | // FIXME: Fabricate an arbitrary expression on the stack and pretend that it |
| 7684 | // is a temporary being used as the 'this' pointer. |
| 7685 | LValue This; |
| 7686 | ImplicitValueInitExpr VIE(RD ? Info.Ctx.getRecordType(RD) : Info.Ctx.IntTy); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 7687 | This.set(&VIE, Info.CurrentCall->Index); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 7688 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 7689 | ArrayRef<const Expr*> Args; |
| 7690 | |
| 7691 | SourceLocation Loc = FD->getLocation(); |
| 7692 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7693 | APValue Scratch; |
| 7694 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 7695 | HandleConstructorCall(Loc, This, Args, CD, Info, Scratch); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7696 | else |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 7697 | HandleFunctionCall(Loc, FD, (MD && MD->isInstance()) ? &This : 0, |
| 7698 | Args, FD->getBody(), Info, Scratch); |
| 7699 | |
| 7700 | return Diags.empty(); |
| 7701 | } |